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 "libabckit/include/c/abckit.h"
17 #include "libabckit/include/c/statuses.h"
18 #include "libabckit/include/c/metadata_core.h"
19 #include "libabckit/include/c/ir_core.h"
20 #include "libabckit/src/include_v2/c/isa/isa_static.h"
21
22 #include "helpers/helpers.h"
23 #include "helpers/helpers_runtime.h"
24
25 #include <gtest/gtest.h>
26 #include <cstddef>
27 #include <cstdint>
28
29 // NOLINTBEGIN(readability-magic-numbers)
30
31 namespace libabckit::test {
32 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37
38 static constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static.abc";
39
40 class LibAbcKitBasicBlocksTest : public ::testing::Test {};
41
42 // Test: test-kind=api, api=GraphApiImpl::bbCreateEmpty, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBcreateEmptyBlock_1)43 TEST_F(LibAbcKitBasicBlocksTest, BBcreateEmptyBlock_1)
44 {
45 helpers::TransformMethod(
46 INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
47 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
48 auto *start = g_implG->gGetStartBasicBlock(graph);
49 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
50 auto *main = helpers::BBgetSuccBlocks(start)[0];
51 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52 auto *bb = g_implG->bbCreateEmpty(graph);
53 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54
55 g_implG->bbDisconnectSuccBlock(start, 0);
56 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57 g_implG->bbInsertSuccBlock(start, bb, 0);
58 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59 g_implG->bbInsertSuccBlock(bb, main, 0);
60 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
61
62 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
63 {{{}, {1}, {{1, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}}},
64 {{0}, {2}, {}},
65 {{1},
66 {3},
67 {
68 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
69 }},
70 {{2}, {}, {}}});
71 helpers::VerifyGraph(graph, bbSchemas);
72
73 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
74 },
75 []([[maybe_unused]] AbckitGraph *graph) {});
76 }
77
78 // Test: test-kind=api, api=GraphApiImpl::bbDisconnectSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBdisconnectSuccBlock)79 TEST_F(LibAbcKitBasicBlocksTest, BBdisconnectSuccBlock)
80 {
81 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
82 auto *start = g_implG->gGetStartBasicBlock(graph);
83 auto *next = helpers::BBgetSuccBlocks(start)[0];
84 g_implG->bbDisconnectSuccBlock(start, 0);
85 ASSERT_TRUE(helpers::BBgetSuccBlocks(start).empty());
86 ASSERT_TRUE(helpers::BBgetPredBlocks(next).empty());
87 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
88 g_implG->bbAppendSuccBlock(start, next);
89 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
90 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
91 {{{}, {1}, {{3, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}}},
92 {{0},
93 {2},
94 {
95 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
96 }},
97 {{1}, {}, {}}});
98 // CC-OFFNXT(G.FMT.02)
99 helpers::VerifyGraph(graph, bbSchemas);
100 };
101 helpers::InspectMethod(INPUT_PATH, "test", cb);
102 }
103
104 // Test: test-kind=api, api=GraphApiImpl::bbGetGraph, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetGraph_1)105 TEST_F(LibAbcKitBasicBlocksTest, BBgetGraph_1)
106 {
107 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
108 auto *start = g_implG->gGetStartBasicBlock(graph);
109 auto *bb = g_implG->bbCreateEmpty(graph);
110 auto *end = g_implG->gGetEndBasicBlock(graph);
111
112 g_implG->bbAppendSuccBlock(start, bb);
113 g_implG->bbAppendSuccBlock(bb, end);
114 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
115 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
116 {{{}, {2, 1}, {{3, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}}},
117 {{0}, {3}, {}},
118 {{0},
119 {3},
120 {
121 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
122 }},
123 {{2, 1}, {}, {}}});
124 helpers::VerifyGraph(graph, bbSchemas);
125 ASSERT_EQ(graph, graph);
126 };
127 helpers::InspectMethod(INPUT_PATH, "test", cb);
128 }
129
130 // Test: test-kind=api, api=GraphApiImpl::bbGetFirstInst, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetFirstInst_2)131 TEST_F(LibAbcKitBasicBlocksTest, BBgetFirstInst_2)
132 {
133 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
134 auto *start = g_implG->gGetStartBasicBlock(graph);
135 auto *bb = helpers::BBgetSuccBlocks(start)[0];
136 auto *inst = g_implG->bbGetFirstInst(bb);
137
138 ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID);
139 };
140 helpers::InspectMethod(INPUT_PATH, "test", cb);
141 }
142
143 // Test: test-kind=api, api=GraphApiImpl::bbGetLastInst, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetLastInst_1)144 TEST_F(LibAbcKitBasicBlocksTest, BBgetLastInst_1)
145 {
146 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
147 auto *start = g_implG->gGetStartBasicBlock(graph);
148 auto *bb = helpers::BBgetSuccBlocks(start)[0];
149 auto *inst = g_implG->bbGetLastInst(bb);
150
151 ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID);
152 };
153 helpers::InspectMethod(INPUT_PATH, "test", cb);
154 }
155
156 // Test: test-kind=api, api=GraphApiImpl::bbGetTrueBranch, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetTrueBranch_1)157 TEST_F(LibAbcKitBasicBlocksTest, BBgetTrueBranch_1)
158 {
159 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
160 auto *start = g_implG->gGetStartBasicBlock(graph);
161 auto *cmpB = helpers::BBgetSuccBlocks(start)[0];
162 auto *trueB = g_implG->bbGetTrueBranch(cmpB);
163 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
164 auto *inst = g_implG->bbGetFirstInst(trueB);
165
166 ASSERT_NE(inst, nullptr);
167 ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN);
168
169 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
170 {{{},
171 {1},
172 {
173 {9, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
174 {1, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
175 {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
176 {5, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
177 {7, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
178 }},
179 {{0},
180 {3, 2},
181 {
182 {4, ABCKIT_ISA_API_STATIC_OPCODE_IF, {3, 1}},
183 }},
184 {{1},
185 {4},
186 {
187 {6, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {5}},
188 }},
189 {{1},
190 {4},
191 {
192 {8, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {7}},
193 }},
194 {{2, 3}, {}, {}}});
195 helpers::VerifyGraph(graph, bbSchemas);
196 };
197 helpers::InspectMethod(INPUT_PATH, "test2", cb);
198 }
199
200 // Test: test-kind=api, api=GraphApiImpl::bbGetFalseBranch, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetFalseBranch_1)201 TEST_F(LibAbcKitBasicBlocksTest, BBgetFalseBranch_1)
202 {
203 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
204 auto *start = g_implG->gGetStartBasicBlock(graph);
205 auto *cmpB = helpers::BBgetSuccBlocks(start)[0];
206 auto *trueB = g_implG->bbGetFalseBranch(cmpB);
207 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
208 auto *inst = g_implG->bbGetFirstInst(trueB);
209
210 ASSERT_NE(inst, nullptr);
211 ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN);
212
213 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
214 {{{},
215 {1},
216 {
217 {9, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
218 {1, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
219 {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
220 {5, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
221 {7, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
222 }},
223 {{0},
224 {3, 2},
225 {
226 {4, ABCKIT_ISA_API_STATIC_OPCODE_IF, {3, 1}},
227 }},
228 {{1},
229 {4},
230 {
231 {6, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {5}},
232 }},
233 {{1},
234 {4},
235 {
236 {8, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {7}},
237 }},
238 {{2, 3}, {}, {}}});
239 helpers::VerifyGraph(graph, bbSchemas);
240 };
241 helpers::InspectMethod(INPUT_PATH, "test2", cb);
242 }
243
244 // Test: test-kind=api, api=GraphApiImpl::bbAddInstFront, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBaddInstFront_4)245 TEST_F(LibAbcKitBasicBlocksTest, BBaddInstFront_4)
246 {
247 helpers::TransformMethod(
248 INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
249 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
250 auto *start = g_implG->gGetStartBasicBlock(graph);
251 auto *bb = helpers::BBgetSuccBlocks(start)[0];
252 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT);
253 auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
254 auto *negInst = g_statG->iCreateNeg(graph, newInst);
255 g_implG->bbAddInstFront(bb, negInst);
256 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
257 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
258 {{{},
259 {1},
260 {{5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
261 {{0},
262 {2},
263 {
264 {4, ABCKIT_ISA_API_STATIC_OPCODE_NEG, {3}},
265 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
266 }},
267 {{1}, {}, {}}});
268 helpers::VerifyGraph(graph, bbSchemas);
269
270 ASSERT_EQ(inst, g_implG->iGetNext(newInst));
271 },
272 []([[maybe_unused]] AbckitGraph *graph) {});
273 }
274
275 // Test: test-kind=api, api=GraphApiImpl::bbAddInstBack, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBaddInstBack_2)276 TEST_F(LibAbcKitBasicBlocksTest, BBaddInstBack_2)
277 {
278 helpers::TransformMethod(
279 INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
280 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
281 auto *start = g_implG->gGetStartBasicBlock(graph);
282 auto *bb = helpers::BBgetSuccBlocks(start)[0];
283 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT);
284 auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
285 auto *negInst = g_statG->iCreateNeg(graph, newInst);
286
287 g_implG->bbDisconnectSuccBlock(start, 0);
288 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
289 auto *empty = g_implG->bbCreateEmpty(graph);
290 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
291 g_implG->bbAppendSuccBlock(start, empty);
292 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
293 g_implG->bbAppendSuccBlock(empty, bb);
294 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
295 g_implG->bbAddInstBack(empty, negInst);
296 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
297 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
298 {{{},
299 {1},
300 {{5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
301 {{0},
302 {2},
303 {
304 {4, ABCKIT_ISA_API_STATIC_OPCODE_NEG, {3}},
305 }},
306 {{1},
307 {3},
308 {
309 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
310 }},
311 {{2}, {}, {}}});
312 helpers::VerifyGraph(graph, bbSchemas);
313
314 ASSERT_EQ(inst, g_implG->iGetNext(newInst));
315 },
316 []([[maybe_unused]] AbckitGraph *graph) {});
317 }
318
319 // Test: test-kind=api, api=GraphApiImpl::bbAddInstBack, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBaddInstBack_4)320 TEST_F(LibAbcKitBasicBlocksTest, BBaddInstBack_4)
321 {
322 helpers::TransformMethod(
323 INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
324 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
325 auto *start = g_implG->gGetStartBasicBlock(graph);
326 auto *newIns = g_implG->gFindOrCreateConstantI64(graph, 1);
327
328 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
329 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
330 {{{},
331 {1},
332 {{4, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
333 {{0},
334 {2},
335 {
336 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
337 }},
338 {{1}, {}, {}}});
339 helpers::VerifyGraph(graph, bbSchemas);
340
341 ASSERT_EQ(newIns, g_implG->bbGetLastInst(start));
342 },
343 []([[maybe_unused]] AbckitGraph *graph) {});
344 }
345
346 // Test: test-kind=api, api=GraphApiImpl::bbVisitSuccBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitSuccBlocks_1)347 TEST_F(LibAbcKitBasicBlocksTest, BBvisitSuccBlocks_1)
348 {
349 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
350 auto *start = g_implG->gGetStartBasicBlock(graph);
351 auto *bb = helpers::BBgetSuccBlocks(start)[0];
352
353 uint32_t counter = 0;
354 auto cb = [](AbckitBasicBlock *, void *data) {
355 (*(reinterpret_cast<uint32_t *>(data)))++;
356 return true;
357 };
358 g_implG->bbVisitSuccBlocks(bb, &counter, cb);
359 // CC-OFFNXT(G.FMT.02)
360 ASSERT_EQ(counter, 1);
361 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
362 };
363 helpers::InspectMethod(INPUT_PATH, "test", cb);
364 }
365
366 // Test: test-kind=api, api=GraphApiImpl::bbVisitPredBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitPredBlocks_1)367 TEST_F(LibAbcKitBasicBlocksTest, BBvisitPredBlocks_1)
368 {
369 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
370 auto *start = g_implG->gGetStartBasicBlock(graph);
371 auto *bb = helpers::BBgetSuccBlocks(start)[0];
372
373 uint32_t counter = 0;
374 auto cb = [](AbckitBasicBlock *, void *data) {
375 (*(reinterpret_cast<uint32_t *>(data)))++;
376 return true;
377 };
378 g_implG->bbVisitPredBlocks(bb, &counter, cb);
379
380 ASSERT_EQ(counter, 1);
381 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
382 };
383
384 helpers::InspectMethod(INPUT_PATH, "test", cb);
385 }
386
387 // Test: test-kind=api, api=GraphApiImpl::bbVisitDominatedBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitDominatedBlocks_1)388 TEST_F(LibAbcKitBasicBlocksTest, BBvisitDominatedBlocks_1)
389 {
390 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
391 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
392 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
393 auto *start = g_implG->gGetStartBasicBlock(graph);
394 auto *bb = helpers::BBgetSuccBlocks(start)[0];
395
396 uint32_t counter = 0;
397 auto cb = [](AbckitBasicBlock *, void *data) {
398 (*(reinterpret_cast<uint32_t *>(data)))++;
399 return true;
400 };
401 g_implG->bbVisitDominatedBlocks(bb, &counter, cb);
402
403 ASSERT_EQ(counter, 1);
404 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
405 });
406 g_impl->closeFile(file);
407 }
408
409 // Test: test-kind=api, api=GraphApiImpl::bbGetSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetSuccBlock_1)410 TEST_F(LibAbcKitBasicBlocksTest, BBgetSuccBlock_1)
411 {
412 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
413 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
414 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
415 auto *start = g_implG->gGetStartBasicBlock(graph);
416 auto *bb = g_implG->bbGetSuccBlock(start, 0);
417
418 ASSERT_EQ(bb, helpers::BBgetSuccBlocks(start)[0]);
419 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
420 });
421 g_impl->closeFile(file);
422 }
423
424 // Test: test-kind=api, api=GraphApiImpl::bbIsStart, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisStart_1)425 TEST_F(LibAbcKitBasicBlocksTest, BBisStart_1)
426 {
427 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
428 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
429 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
430 auto *start = g_implG->gGetStartBasicBlock(graph);
431
432 ASSERT_TRUE(g_implG->bbIsStart(start));
433 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
434 });
435 g_impl->closeFile(file);
436 }
437
438 // Test: test-kind=api, api=GraphApiImpl::bbIsEnd, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisEnd_1)439 TEST_F(LibAbcKitBasicBlocksTest, BBisEnd_1)
440 {
441 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
442 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
443 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
444 auto *end = g_implG->gGetEndBasicBlock(graph);
445
446 ASSERT_TRUE(g_implG->bbIsEnd(end));
447 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
448 });
449 g_impl->closeFile(file);
450 }
451
452 // Test: test-kind=api, api=GraphApiImpl::bbGetImmediateDominator, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetImmediateDominator_1)453 TEST_F(LibAbcKitBasicBlocksTest, BBgetImmediateDominator_1)
454 {
455 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
456 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
457 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
458 auto *start = g_implG->gGetStartBasicBlock(graph);
459 auto *bb = helpers::BBgetSuccBlocks(start)[0];
460 g_implG->bbGetImmediateDominator(bb);
461 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
462 });
463 g_impl->closeFile(file);
464 }
465
466 // Test: test-kind=api, api=GraphApiImpl::bbGetSuccBlockCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetSuccBlockCount_1)467 TEST_F(LibAbcKitBasicBlocksTest, BBgetSuccBlockCount_1)
468 {
469 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
470 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
471 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
472 auto *start = g_implG->gGetStartBasicBlock(graph);
473
474 ASSERT_EQ(g_implG->bbGetSuccBlockCount(start), 1);
475 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
476 });
477 g_impl->closeFile(file);
478 }
479
480 // Test: test-kind=api, api=GraphApiImpl::bbGetPredBlockCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetPredBlockCount_1)481 TEST_F(LibAbcKitBasicBlocksTest, BBgetPredBlockCount_1)
482 {
483 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
484 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
485 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
486 auto *start = g_implG->gGetStartBasicBlock(graph);
487 auto *bb = helpers::BBgetSuccBlocks(start)[0];
488
489 ASSERT_EQ(g_implG->bbGetPredBlockCount(bb), 1);
490 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
491 });
492 g_impl->closeFile(file);
493 }
494
495 // Test: test-kind=api, api=GraphApiImpl::bbDump, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBdump_1)496 TEST_F(LibAbcKitBasicBlocksTest, BBdump_1)
497 {
498 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
499 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
500 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
501 auto *start = g_implG->gGetStartBasicBlock(graph);
502 LIBABCKIT_LOG_DUMP(g_implG->bbDump(start, 0), DEBUG);
503 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
504 });
505 g_impl->closeFile(file);
506 }
507
508 // Test: test-kind=api, api=GraphApiImpl::bbGetNumberOfInstructions, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetNumberOfInstructions_1)509 TEST_F(LibAbcKitBasicBlocksTest, BBgetNumberOfInstructions_1)
510 {
511 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
512 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
513 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
514 auto *start = g_implG->gGetStartBasicBlock(graph);
515 auto *bb = helpers::BBgetSuccBlocks(start)[0];
516 ASSERT_EQ(g_implG->bbGetNumberOfInstructions(bb), 1);
517 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
518 });
519 g_impl->closeFile(file);
520 }
521
522 // Test: test-kind=api, api=GraphApiImpl::gGetBasicBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetBasicBlock_1)523 TEST_F(LibAbcKitBasicBlocksTest, GgetBasicBlock_1)
524 {
525 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
526 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
527 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
528 auto *bb = g_implG->gGetBasicBlock(graph, 1);
529 ASSERT_EQ(bb, g_implG->gGetStartBasicBlock(graph));
530 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
531 });
532 g_impl->closeFile(file);
533 }
534
535 // Test: test-kind=api, api=GraphApiImpl::gGetBasicBlock, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitBasicBlocksTest,GgetBasicBlockBigId)536 TEST_F(LibAbcKitBasicBlocksTest, GgetBasicBlockBigId)
537 {
538 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
539 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
540 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
541 auto bbVectorSize = g_implG->gGetNumberOfBasicBlocks(graph);
542 auto *bb = g_implG->gGetBasicBlock(graph, bbVectorSize + 1);
543 ASSERT_EQ(bb, nullptr);
544 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
545 });
546 g_impl->closeFile(file);
547 }
548
549 // Test: test-kind=api, api=GraphApiImpl::gGetParameter, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetParameter_1)550 TEST_F(LibAbcKitBasicBlocksTest, GgetParameter_1)
551 {
552 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
553 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
554 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
555 auto *start = g_implG->gGetStartBasicBlock(graph);
556 auto *inst = g_implG->bbGetFirstInst(start);
557 ASSERT_EQ(inst, g_implG->gGetParameter(graph, 0));
558 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
559 });
560 g_impl->closeFile(file);
561 }
562
563 // Test: test-kind=api, api=GraphApiImpl::gGetParameter, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitBasicBlocksTest,GgetParameterBigId)564 TEST_F(LibAbcKitBasicBlocksTest, GgetParameterBigId)
565 {
566 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
567 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
568 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
569 auto size = g_implG->gGetNumberOfParameters(graph);
570 auto *inst = g_implG->gGetParameter(graph, size + 1);
571 ASSERT_EQ(inst, nullptr);
572 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
573 });
574 g_impl->closeFile(file);
575 }
576
577 // Test: test-kind=api, api=GraphApiImpl::gGetNumberOfParameters, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetNumberOfParameters)578 TEST_F(LibAbcKitBasicBlocksTest, GgetNumberOfParameters)
579 {
580 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
581 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
582 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
583 uint32_t num = g_implG->gGetNumberOfParameters(graph);
584 ASSERT_EQ(num, 2U);
585 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
586 });
587 g_impl->closeFile(file);
588 }
589
590 // Test: test-kind=api, api=GraphApiImpl::gGetNumberOfBasicBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetNumberOfBasicBlocks_1)591 TEST_F(LibAbcKitBasicBlocksTest, GgetNumberOfBasicBlocks_1)
592 {
593 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
594 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
595 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
596 g_implG->gGetNumberOfBasicBlocks(graph);
597 ASSERT_EQ(g_implG->gGetNumberOfBasicBlocks(graph), 5U);
598 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
599 });
600 g_impl->closeFile(file);
601 }
602
603 // Test: test-kind=api, api=GraphApiImpl::bbGetId, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetId_1)604 TEST_F(LibAbcKitBasicBlocksTest, BBgetId_1)
605 {
606 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
607 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
608 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
609 auto *start = g_implG->gGetStartBasicBlock(graph);
610 ASSERT_EQ(g_implG->bbGetId(start), 3U);
611 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
612 });
613 g_impl->closeFile(file);
614 }
615
616 // Test: test-kind=api, api=GraphApiImpl::bbGetPredBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetPredBlock_1)617 TEST_F(LibAbcKitBasicBlocksTest, BBgetPredBlock_1)
618 {
619 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
620 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
621 helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
622 auto *start = g_implG->gGetStartBasicBlock(graph);
623 auto *bb = g_implG->bbGetSuccBlock(start, 0U);
624 ASSERT_EQ(g_implG->bbGetPredBlock(bb, 0U), helpers::BBgetPredBlocks(bb)[0U]);
625 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
626 });
627 g_impl->closeFile(file);
628 }
629
630 // Test: test-kind=api, api=GraphApiImpl::bbInsertSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBinsertSuccBlock_1)631 TEST_F(LibAbcKitBasicBlocksTest, BBinsertSuccBlock_1)
632 {
633 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
634 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
635 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
636 auto *start = g_implG->gGetStartBasicBlock(graph);
637 auto *bb = g_implG->bbGetSuccBlock(start, 0U);
638 auto *empty = g_implG->bbCreateEmpty(graph);
639 g_implG->bbInsertSuccBlock(bb, empty, 2U);
640 ASSERT_EQ(g_implG->bbGetSuccBlock(bb, 2U), helpers::BBgetSuccBlocks(bb)[2U]);
641 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
642 g_implG->bbDisconnectSuccBlock(bb, 2U);
643 });
644 g_impl->closeFile(file);
645 }
646
647 // Test: test-kind=api, api=GraphApiImpl::bbAppendSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBappendSuccBlock_1)648 TEST_F(LibAbcKitBasicBlocksTest, BBappendSuccBlock_1)
649 {
650 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
651 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
652 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
653 auto *start = g_implG->gGetStartBasicBlock(graph);
654 auto *bb = g_implG->bbGetSuccBlock(start, 0U);
655 auto *empty = g_implG->bbCreateEmpty(graph);
656 g_implG->bbAppendSuccBlock(bb, empty);
657 ASSERT_EQ(g_implG->bbGetSuccBlock(bb, 2U), helpers::BBgetSuccBlocks(bb)[2U]);
658 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
659 g_implG->bbDisconnectSuccBlock(bb, g_implG->bbGetSuccBlockCount(bb) - 1U);
660 });
661 g_impl->closeFile(file);
662 }
663
664 // Test: test-kind=api, api=GraphApiImpl::bbRemoveAllInsts, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,bbRemoveAllInsts_1)665 TEST_F(LibAbcKitBasicBlocksTest, bbRemoveAllInsts_1)
666 {
667 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
668 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
669 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
670 auto *empty = g_implG->bbCreateEmpty(graph);
671 auto *constInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
672 auto *newInst = g_statG->iCreateNeg(graph, constInst);
673 g_implG->bbAddInstFront(empty, newInst);
674 ASSERT_EQ(g_implG->bbGetNumberOfInstructions(empty), 1);
675 g_implG->bbRemoveAllInsts(empty);
676 ASSERT_EQ(g_implG->bbGetNumberOfInstructions(empty), 0);
677 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
678 });
679 g_impl->closeFile(file);
680 }
681
682 // Test: test-kind=api, api=GraphApiImpl::bbIsLoopPrehead, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisLoopPrehead_1)683 TEST_F(LibAbcKitBasicBlocksTest, BBisLoopPrehead_1)
684 {
685 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
686 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
687 helpers::TransformMethod(file, "test4", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
688 auto *start = g_implG->gGetStartBasicBlock(graph);
689 auto *bb = g_implG->bbGetSuccBlock(start, 0);
690 ASSERT_TRUE(g_implG->bbIsLoopPrehead(bb));
691 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
692 });
693 g_impl->closeFile(file);
694 }
695
696 // Test: test-kind=api, api=GraphApiImpl::bbIsLoopHead, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisLoopHead_1)697 TEST_F(LibAbcKitBasicBlocksTest, BBisLoopHead_1)
698 {
699 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
700 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
701 helpers::TransformMethod(file, "test4", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
702 auto *start = g_implG->gGetStartBasicBlock(graph);
703 auto *bb = g_implG->bbGetSuccBlock(start, 0);
704 auto *bb2 = g_implG->bbGetSuccBlock(bb, 0);
705 ASSERT_TRUE(g_implG->bbIsLoopHead(bb2));
706 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
707 });
708 g_impl->closeFile(file);
709 }
710
711 // Test: test-kind=api, api=GraphApiImpl::bbSplitBlockAfterInstruction, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,DISABLED_BBsplitBlockAfterInstruction_1)712 TEST_F(LibAbcKitBasicBlocksTest, DISABLED_BBsplitBlockAfterInstruction_1)
713 {
714 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
715 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
716 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
717 auto *start = g_implG->gGetStartBasicBlock(graph);
718 auto *bb = g_implG->bbGetSuccBlock(start, 0);
719 auto *bb2 = g_implG->bbGetSuccBlock(bb, 0);
720 auto *inst = g_implG->bbGetFirstInst(bb2);
721 ASSERT_EQ(g_implG->gGetNumberOfBasicBlocks(graph), 5U);
722 auto *newBb = g_implG->bbSplitBlockAfterInstruction(bb2, inst, false);
723 ASSERT_EQ(g_implG->gGetNumberOfBasicBlocks(graph), 6U);
724 g_implG->bbAppendSuccBlock(bb2, newBb);
725 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
726 });
727 g_impl->closeFile(file);
728 }
729
730 // Test: test-kind=api, api=GraphApiImpl::bbCheckDominance, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBcheckDominance_1)731 TEST_F(LibAbcKitBasicBlocksTest, BBcheckDominance_1)
732 {
733 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
734 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
735 helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
736 auto *start = g_implG->gGetStartBasicBlock(graph);
737 auto *bb = g_implG->bbGetSuccBlock(start, 0);
738 ASSERT_TRUE(g_implG->bbCheckDominance(bb, start));
739 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
740 });
741 g_impl->closeFile(file);
742 }
743
744 // Test: test-kind=api, api=GraphApiImpl::bbVisitSuccBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitSuccBlocksStatic_2)745 TEST_F(LibAbcKitBasicBlocksTest, BBvisitSuccBlocksStatic_2)
746 {
747 auto output = helpers::ExecuteStaticAbc(INPUT_PATH, "basic_blocks_static/ETSGLOBAL", "visit_call");
748 EXPECT_TRUE(helpers::Match(output, "0\n1\n"));
749 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
750 AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
751 AbckitBasicBlock *ifBB = g_implG->bbGetSuccBlock(startBB, 0);
752 std::vector<AbckitBasicBlock *> succBBs;
753 struct VisitData {
754 const AbckitIsaApiStatic *gStatG;
755 const AbckitGraphApi *implG;
756 AbckitGraph *graph;
757 int newPrintInt;
758 } data {g_statG, g_implG, graph, 42};
759 g_implG->bbVisitSuccBlocks(ifBB, &data, [](AbckitBasicBlock *succBasicBlock, void *d) -> bool {
760 auto *gStatG = static_cast<struct VisitData *>(d)->gStatG;
761 auto *gImplG = static_cast<struct VisitData *>(d)->implG;
762 auto *graph = static_cast<struct VisitData *>(d)->graph;
763 int newPrintInt = static_cast<struct VisitData *>(d)->newPrintInt++;
764 auto *curInst = gImplG->bbGetFirstInst(succBasicBlock);
765 while (curInst != nullptr && gStatG->iGetOpcode(curInst) != ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC) {
766 curInst = gImplG->iGetNext(curInst);
767 }
768 if (curInst == nullptr) {
769 return false;
770 }
771 gImplG->iSetInput(curInst, gImplG->gFindOrCreateConstantU64(graph, newPrintInt), 1);
772 return true;
773 });
774 };
775 auto abcIn = INPUT_PATH;
776 auto abcOut = ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_visit.abc";
777 helpers::TransformMethod(abcIn, abcOut, "visit_succ_blocks", cb);
778 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_visit.abc",
779 "basic_blocks_static/ETSGLOBAL", "visit_call");
780 EXPECT_TRUE(helpers::Match(output, "43\n42\n"));
781 }
782
783 // Test: test-kind=api, api=GraphApiImpl::bbIsTryBegin, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisTryBegin)784 TEST_F(LibAbcKitBasicBlocksTest, BBisTryBegin)
785 {
786 helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
787 auto *startBB = g_implG->gGetStartBasicBlock(graph);
788 AbckitBasicBlock *tryBegin = g_implG->bbGetSuccBlock(startBB, 0);
789 ASSERT_TRUE(g_implG->bbIsTryBegin(tryBegin));
790 });
791 }
792
793 // Test: test-kind=api, api=GraphApiImpl::bbIsTryEnd, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisTryEnd)794 TEST_F(LibAbcKitBasicBlocksTest, BBisTryEnd)
795 {
796 helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
797 auto *endBB = g_implG->gGetEndBasicBlock(graph);
798 AbckitBasicBlock *tryEnd = g_implG->bbGetPredBlock(g_implG->bbGetPredBlock(endBB, 1), 1);
799 ASSERT_TRUE(g_implG->bbIsTryEnd(tryEnd));
800 });
801 }
802
803 // Test: test-kind=api, api=GraphApiImpl::bbIsCatchBegin, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisCatchBegin)804 TEST_F(LibAbcKitBasicBlocksTest, BBisCatchBegin)
805 {
806 helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
807 auto *startBB = g_implG->gGetStartBasicBlock(graph);
808 AbckitBasicBlock *tryBegin = g_implG->bbGetSuccBlock(startBB, 0);
809 AbckitBasicBlock *catchBegin = g_implG->bbGetSuccBlock(tryBegin, 1);
810 ASSERT_TRUE(g_implG->bbIsCatchBegin(catchBegin));
811 });
812 }
813
814 // Test: test-kind=api, api=GraphApiImpl::bbIsCatch, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisCatch)815 TEST_F(LibAbcKitBasicBlocksTest, BBisCatch)
816 {
817 helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
818 auto *endBB = g_implG->gGetEndBasicBlock(graph);
819 AbckitBasicBlock *catchBB = g_implG->bbGetPredBlock(endBB, 1);
820 ASSERT_TRUE(g_implG->bbIsCatch(catchBB));
821 });
822 }
823
824 // Test: test-kind=api, api=GraphApiImpl::bbIsTry, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisTry)825 TEST_F(LibAbcKitBasicBlocksTest, BBisTry)
826 {
827 auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
828 auto *startBB = g_implG->gGetStartBasicBlock(graph);
829 AbckitBasicBlock *tryBB = g_implG->bbGetSuccBlock(g_implG->bbGetSuccBlock(startBB, 0), 0);
830 // CC-OFFNXT(G.FMT.02)
831 ASSERT_TRUE(g_implG->bbIsTry(tryBB));
832 };
833 helpers::InspectMethod(INPUT_PATH, "test3", cb);
834 }
835
836 } // namespace libabckit::test
837
838 // NOLINTEND(readability-magic-numbers)
839