• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_SCANNER_H
17 #define API_SCANNER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "helpers/visit_helper/visit_helper-inl.h"
24 #include "libabckit/include/c/abckit.h"
25 #include "libabckit/include/c/ir_core.h"
26 #include "libabckit/include/c/metadata_core.h"
27 #include "libabckit/include/c/isa/isa_dynamic.h"
28 
29 struct ApiInfo {
30     std::string source;
31     std::string objName;
32     std::string propName;
33 };
34 
35 struct UsageInfo {
36     std::string source;
37     std::string funcName;
38     size_t idx;
39 };
40 
41 using ApiUsageMap = std::unordered_map<size_t, std::vector<UsageInfo>>;
42 
43 using SuspectsMap = std::unordered_map<AbckitCoreImportDescriptor *, size_t>;
44 
45 class ApiScanner {
46 public:
47     ApiScanner(enum AbckitApiVersion version, AbckitFile *file, std::vector<ApiInfo> apiList);
48 
GetApiUsages()49     const ApiUsageMap &GetApiUsages()
50     {
51         CollectApiUsages();
52         return apiUsages_;
53     }
54 
55     std::string ApiUsageMapGetString() const;
56 
57     bool IsUsagesEqual(const std::vector<UsageInfo> &otherUsages) const;
58 
59 private:
60     void CollectApiUsages();
61 
62     void CollectUsageInMethod(AbckitCoreFunction *method, SuspectsMap &suspects);
63 
64     std::string GetString(AbckitString *str) const;
65 
AddApiUsage(size_t apiIndex,const UsageInfo & usage)66     void AddApiUsage(size_t apiIndex, const UsageInfo &usage)
67     {
68         auto iter = apiUsages_.find(apiIndex);
69         if (iter == apiUsages_.end()) {
70             std::vector<UsageInfo> usages;
71             usages.emplace_back(usage);
72             apiUsages_.emplace(apiIndex, usages);
73         } else {
74             iter->second.emplace_back(usage);
75         }
76     }
77     std::string GetMethodName(AbckitCoreFunction *method);
78     bool GetSuspects(AbckitCoreModule *mod, SuspectsMap &suspects);
79     bool IsLoadApi(AbckitCoreImportDescriptor *id, size_t apiIndex, AbckitInst *inst);
80 
81 private:
82     const AbckitApi *impl_ = nullptr;
83     const AbckitInspectApi *implI_ = nullptr;
84     const AbckitGraphApi *implG_ = nullptr;
85     const AbckitIsaApiDynamic *dynG_ = nullptr;
86     AbckitFile *file_ = nullptr;
87     VisitHelper vh_;
88     std::vector<ApiInfo> apiList_;
89     ApiUsageMap apiUsages_;
90 };
91 
92 #endif /* API_SCANNER_H */
93