• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-2025 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/isa/isa_static.h"
18 #include "libabckit/include/c/metadata_core.h"
19 
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22 
23 #include <gtest/gtest.h>
24 
25 namespace libabckit::test {
26 
27 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 
TransformIR(AbckitGraph * graph)33 void TransformIR(AbckitGraph *graph)
34 {
35     AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
36     std::vector<AbckitBasicBlock *> succBBs = helpers::BBgetSuccBlocks(startBB);
37     AbckitBasicBlock *endBB = g_implG->gGetEndBasicBlock(graph);
38 
39     AbckitInst *param1 = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
40     ASSERT_NE(param1, nullptr);
41     AbckitInst *param0 = g_implG->iGetPrev(param1);
42     ASSERT_NE(param0, nullptr);
43 
44     AbckitBasicBlock *ifBB = g_implG->bbCreateEmpty(graph);
45     g_implG->bbAppendSuccBlock(startBB, ifBB);
46     AbckitInst *len = g_statG->iCreateLenArray(graph, param0);
47     g_implG->bbAddInstBack(ifBB, len);
48     AbckitInst *ifInst = g_statG->iCreateIf(graph, len, param1, ABCKIT_ISA_API_STATIC_CONDITION_CODE_CC_GT);
49 
50     g_implG->bbAddInstBack(ifBB, ifInst);
51 
52     g_implG->bbAppendSuccBlock(ifBB, succBBs[0]);
53     g_implG->bbDisconnectSuccBlock(startBB, 0);
54 
55     AbckitBasicBlock *falseBB = g_implG->bbCreateEmpty(graph);
56     g_implG->bbAppendSuccBlock(ifBB, falseBB);
57     auto *constm1 = g_implG->gFindOrCreateConstantI32(graph, -1);
58 
59     g_implG->bbAppendSuccBlock(falseBB, endBB);
60 
61     AbckitInst *ret = g_statG->iCreateReturn(graph, constm1);
62 
63     g_implG->bbAddInstBack(falseBB, ret);
64 }
65 
66 class AbckitScenarioTest : public ::testing::Test {};
67 
68 // Test: test-kind=scenario, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(AbckitScenarioTest,LibAbcKitTestStaticParameterCheck)69 TEST_F(AbckitScenarioTest, LibAbcKitTestStaticParameterCheck)
70 {
71     auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "scenarios/parameter_check/parameter_check_static.abc",
72                                             "parameter_check_static/ETSGLOBAL", "main");
73     EXPECT_TRUE(helpers::Match(output,
74                                "buisiness logic...\n"
75                                "1\n"
76                                "buisiness logic...\n"
77                                "2\n"
78                                "buisiness logic...\n"
79                                "3\n"));
80     // NOTE: Add method search
81     helpers::TransformMethod(
82         ABCKIT_ABC_DIR "scenarios/parameter_check/parameter_check_static.abc",
83         ABCKIT_ABC_DIR "scenarios/parameter_check/parameter_check_static_modified.abc", "handle",
84         // CC-OFFNXT(C_RULE_ID_ONE_STATEMENT_ONE_LINE) local codestyle conflict
85         // CC-OFFNXT(G.FMT.04) project code style
86         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) { TransformIR(graph); },
87         []([[maybe_unused]] AbckitGraph *graph) {});
88 
89     output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "scenarios/parameter_check/parameter_check_static_modified.abc",
90                                        "parameter_check_static/ETSGLOBAL", "main");
91     EXPECT_TRUE(helpers::Match(output,
92                                "buisiness logic...\n"
93                                "1\n"
94                                "-1\n"
95                                "-1\n"));
96 }
97 
98 }  // namespace libabckit::test
99