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 #include <gtest/gtest.h>
17
18 #include "libabckit/include/c/extensions/arkts/metadata_arkts.h"
19 #include "helpers/helpers.h"
20 #include "metadata_inspect_impl.h"
21
22 #include <functional>
23
24 namespace {
25 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
26 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
27 auto g_implArkI = AbckitGetArktsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30
TransformMethod(AbckitCoreFunction * method)31 void TransformMethod(AbckitCoreFunction *method)
32 {
33 if (g_implI->moduleGetTarget(g_implI->functionGetModule(method)) == ABCKIT_TARGET_ARK_TS_V2) {
34 auto *arktsMethod = g_implArkI->coreFunctionToArktsFunction(method);
35 if (g_implArkI->functionIsNative(arktsMethod)) {
36 return;
37 }
38 }
39 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
40
41 std::cout << "[function name]: " << g_implI->abckitStringToString((g_implI->functionGetName(method))) << std::endl;
42
43 AbckitGraph *graph = g_implI->createGraphFromFunction(method);
44 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
45
46 std::function<bool(AbckitBasicBlock *)> enumerateInsts = [&](AbckitBasicBlock *bb) -> bool {
47 auto inst = g_implG->bbGetFirstInst(bb);
48 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
49 auto instNum = g_implG->bbGetNumberOfInstructions(bb);
50 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
51 for (int i = 0; i < instNum; ++i) {
52 g_implG->iDump(inst, 1);
53 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54 auto isCallInst = g_implG->iCheckIsCall(inst);
55 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
56 std::cout << "[isCallInst]: " << isCallInst << std::endl;
57 inst = g_implG->iGetNext(inst);
58 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59 }
60 return true;
61 };
62
63 g_implG->gVisitBlocksRpo(graph, &enumerateInsts, [](AbckitBasicBlock *bb, void *cb) -> bool {
64 return (*reinterpret_cast<std::function<bool(AbckitBasicBlock *)> *>(cb))(bb);
65 });
66 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
67 g_implM->functionSetGraph(method, graph);
68 g_impl->destroyGraph(graph);
69 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
70 }
71 } // namespace
72
73 namespace libabckit::test {
74
75 class AbckitRegressionTestCAPI : public ::testing::Test {};
76
77 static std::function<bool(AbckitCoreClass *)> g_cbClass;
78
__anon3fc794730402(AbckitCoreFunction *f) 79 static std::function<bool(AbckitCoreFunction *)> g_cbFunc = [](AbckitCoreFunction *f) -> bool {
80 g_implI->functionEnumerateNestedFunctions(f, &g_cbFunc, [](AbckitCoreFunction *f, void *cb) {
81 return (*reinterpret_cast<std::function<bool(AbckitCoreFunction *)> *>(cb))(f);
82 });
83 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
84
85 g_implI->functionEnumerateNestedClasses(f, &g_cbClass, [](AbckitCoreClass *c, void *cb) -> bool {
86 return (*reinterpret_cast<std::function<bool(AbckitCoreClass *)> *>(cb))(c);
87 });
88 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
89
90 TransformMethod(f);
91
92 return true;
93 };
94
__anon3fc794730702(AbckitCoreNamespace *n) 95 static std::function<void(AbckitCoreNamespace *)> g_cbNamespce = [](AbckitCoreNamespace *n) {
96 g_implI->namespaceEnumerateNamespaces(n, &g_cbNamespce, [](AbckitCoreNamespace *n, void *cb) -> bool {
97 return (*reinterpret_cast<std::function<bool(AbckitCoreNamespace *)> *>(cb))(n);
98 });
99 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
100 g_implI->namespaceEnumerateClasses(n, &g_cbClass, [](AbckitCoreClass *c, void *cb) -> bool {
101 return (*reinterpret_cast<std::function<bool(AbckitCoreClass *)> *>(cb))(c);
102 });
103 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
104 g_implI->namespaceEnumerateTopLevelFunctions(n, (void *)&g_cbFunc, [](AbckitCoreFunction *f, void *cb) -> bool {
105 return (*reinterpret_cast<std::function<bool(AbckitCoreFunction *)> *>(cb))(f);
106 });
107 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
108 };
109
__anon3fc794730b02(AbckitCoreModule *m) 110 static std::function<void(AbckitCoreModule *)> g_cbModule = [](AbckitCoreModule *m) -> bool {
111 g_implI->moduleEnumerateNamespaces(m, &g_cbNamespce, [](AbckitCoreNamespace *n, void *cb) {
112 return (*reinterpret_cast<std::function<bool(AbckitCoreNamespace *)> *>(cb))(n);
113 });
114 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
115 g_implI->moduleEnumerateClasses(m, &g_cbClass, [](AbckitCoreClass *c, void *cb) {
116 return (*reinterpret_cast<std::function<bool(AbckitCoreClass *)> *>(cb))(c);
117 });
118 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
119 g_implI->moduleEnumerateTopLevelFunctions(m, (void *)&g_cbFunc, [](AbckitCoreFunction *m, void *cb) {
120 return (*reinterpret_cast<std::function<bool(AbckitCoreFunction *)> *>(cb))(m);
121 });
122 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
123 return true;
124 };
125
126 // Test: test-kind=regression, abc-kind=ArkTS1, category=positive, extension=cpp
TEST_F(AbckitRegressionTestCAPI,LibAbcKitTestIssueIB2T4M)127 TEST_F(AbckitRegressionTestCAPI, LibAbcKitTestIssueIB2T4M)
128 {
129 std::string inputPath = ABCKIT_ABC_DIR "regression/issue_IB2T4M/issue_IB2T4M.abc";
130 AbckitFile *file = g_impl->openAbc(inputPath.c_str(), inputPath.size());
131 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
132
133 g_cbClass = [](AbckitCoreClass *c) -> bool {
134 g_implI->classEnumerateMethods(c, (void *)&g_cbFunc, [](AbckitCoreFunction *m, void *cb) -> bool {
135 return (*reinterpret_cast<std::function<bool(AbckitCoreFunction *)> *>(cb))(m);
136 });
137 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
138 return true;
139 };
140
141 g_implI->fileEnumerateModules(file, &g_cbModule, [](AbckitCoreModule *m, void *cb) -> bool {
142 return (*reinterpret_cast<std::function<bool(AbckitCoreModule *)> *>(cb))(m);
143 });
144 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
145
146 g_impl->closeFile(file);
147 }
148
149 } // namespace libabckit::test
150