• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 COMPILER_OPTIMIZER_IR_GRAPH_CHECKER_MACROS_H
17 #define COMPILER_OPTIMIZER_IR_GRAPH_CHECKER_MACROS_H
18 
19 #ifndef ENABLE_LIBABCKIT
20 // CC-OFFNXT(G.PRE.02) should be with define
21 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
22 #define CHECKER_ASSERT(cond) ASSERT((cond))
23 // CC-OFFNXT(G.PRE.02) should be with define
24 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
25 #define CHECKER_EQ(lhs, rhs) CHECK_EQ(lhs, rhs)
26 
27 // CC-OFFNXT(G.PRE.02) should be with define
28 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
29 #define CHECKER_DO_IF_NOT(cond, func) ASSERT_DO((cond), func)
30 // CC-OFFNXT(G.PRE.02) should be with define
31 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
32 #define CHECKER_DO_IF_NOT_VISITOR_INTERNAL(visitor, klass, cond, func) ASSERT_DO((cond), func)
33 // CC-OFFNXT(G.PRE.02) should be with define
34 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
35 #define CHECKER_DO_IF_NOT_AND_PRINT(cond, func) ASSERT_DO((cond), func; PrintFailedMethodAndPass();)
36 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
37 #define CHECKER_DO_IF_NOT_AND_PRINT_VISITOR(visitor, cond, func) \
38     ASSERT_DO((cond), func; PrintFailedMethodAndPassVisitor(visitor);)
39 // CC-OFFNXT(G.FMT.16) project code style
40 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
41 #define CHECKER_IF_NOT_PRINT(cond) CHECKER_DO_IF_NOT_AND_PRINT(cond, )
42 // CC-OFFNXT(G.FMT.16) project code style
43 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
44 #define CHECKER_IF_NOT_PRINT_VISITOR(visitor, cond) CHECKER_DO_IF_NOT_AND_PRINT_VISITOR(visitor, cond, )
45 // CC-OFFNXT(G.PRE.02) should be with define
46 #define CHECK_STATUS()
47 #else
48 
49 // CC-OFFNXT(G.PRE.02) should be with define
50 #define ABCKIT_ASSERT(cond)                                    \
51     if (UNLIKELY(!(cond))) {                                   \
52         std::cerr << "The assertion is not true" << std::endl; \
53         reinterpret_cast<InstChecker *>(v)->SetStatus(false);  \
54     }
55 // CC-OFFNXT(G.PRE.02) should be with define
56 #define CHECKER_ASSERT(cond)                                          \
57     if (reinterpret_cast<InstChecker *>(v)->GetGraph()->IsAbcKit()) { \
58         ABCKIT_ASSERT((cond));                                        \
59     } else {                                                          \
60         ASSERT((cond));                                               \
61     }
62 // CC-OFFNXT(G.PRE.02) should be with define
63 #define ABCKIT_CHECK_EQ(lhs, rhs)                             \
64     if (UNLIKELY(!(lhs == rhs))) {                            \
65         std::cerr << "The values are not equal" << std::endl; \
66         reinterpret_cast<InstChecker *>(v)->SetStatus(false); \
67     }
68 // CC-OFFNXT(G.PRE.02) should be with define
69 #define CHECKER_EQ(lhs, rhs)                                          \
70     if (reinterpret_cast<InstChecker *>(v)->GetGraph()->IsAbcKit()) { \
71         ABCKIT_CHECK_EQ((lhs), (rhs));                                \
72     } else {                                                          \
73         CHECK_EQ(lhs, rhs);                                           \
74     }
75 // CC-OFFNXT(G.PRE.02) should be with define
76 #define ABCKIT_DO_IF_NOT_VISITOR(visitor, cond, func)                \
77     if (UNLIKELY(!(cond))) {                                         \
78         func;                                                        \
79         reinterpret_cast<GraphChecker *>(visitor)->SetStatus(false); \
80     }
81 // CC-OFFNXT(G.PRE.02) should be with define
82 #define ABCKIT_DO_IF_NOT(cond, func) \
83     if (UNLIKELY(!(cond))) {         \
84         func;                        \
85         SetStatus(false);            \
86     }
87 // CC-OFFNXT(G.PRE.02) should be with define
88 #define CHECKER_DO_IF_NOT(cond, func)   \
89     if (GetGraph()->IsAbcKit()) {       \
90         ABCKIT_DO_IF_NOT((cond), func); \
91     } else {                            \
92         ASSERT_DO(cond, func);          \
93     }
94 // CC-OFFNXT(G.PRE.02) should be with define
95 #define CHECKER_DO_IF_NOT_VISITOR_INTERNAL(visitor, klass, cond, func) \
96     if (reinterpret_cast<klass>(visitor)->GetGraph()->IsAbcKit()) {    \
97         ABCKIT_DO_IF_NOT_VISITOR(visitor, (cond), func);               \
98     } else {                                                           \
99         ASSERT_DO(cond, func);                                         \
100     }
101 
102 // CC-OFFNXT(G.PRE.02) should be with define
103 #define CHECKER_DO_IF_NOT_AND_PRINT(cond, func) CHECKER_DO_IF_NOT((cond), func; PrintFailedMethodAndPass();)
104 // CC-OFFNXT(G.PRE.02) should be with define
105 #define CHECKER_DO_IF_NOT_AND_PRINT_VISITOR(visitor, cond, func) \
106     CHECKER_DO_IF_NOT_VISITOR_INTERNAL(visitor, GraphChecker *, (cond), func; PrintFailedMethodAndPassVisitor(visitor);)
107 // CC-OFFNXT(G.PRE.02) should be with define
108 // CC-OFFNXT(G.FMT.16) project code style
109 #define CHECKER_IF_NOT_PRINT(cond) CHECKER_DO_IF_NOT_AND_PRINT(cond, )
110 // CC-OFFNXT(G.PRE.02) should be with define
111 // CC-OFFNXT(G.FMT.16) project code style
112 #define CHECKER_IF_NOT_PRINT_VISITOR(visitor, cond) CHECKER_DO_IF_NOT_AND_PRINT_VISITOR(visitor, cond, )
113 // CC-OFFNXT(G.PRE.02) should be with define
114 #define CHECK_STATUS()                                        \
115     if (!GetStatus() && GetGraph()->IsAbcKit()) {             \
116         PrintFailedMethodAndPass();                           \
117         /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \
118         return false;                                         \
119     }
120 #endif
121 
122 // CC-OFFNXT(G.PRE.02) should be with define
123 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
124 #define CHECKER_MESSAGE_IF_NOT_AND_PRINT(cond, message) \
125     CHECKER_DO_IF_NOT((cond), std::cerr << (message) << std::endl; PrintFailedMethodAndPass();)
126 // CC-OFFNXT(G.PRE.02) should be with define
127 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
128 #define CHECKER_MESSAGE_IF_NOT_AND_PRINT_VISITOR(visitor, cond, message)                                     \
129     CHECKER_DO_IF_NOT_VISITOR_INTERNAL(visitor, GraphChecker *, (cond), std::cerr << (message) << std::endl; \
130                                        PrintFailedMethodAndPassVisitor(visitor);)
131 
132 #endif  // COMPILER_OPTIMIZER_IR_GRAPH_CHECKER_MACROS_H
133