1 | //===- CallbacksBinaryOperator.cpp ----------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "CallbacksCommon.h" |
10 | |
11 | TEST(RecursiveASTVisitor, StmtCallbacks_TraverseBinaryOperator) { |
12 | class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> { |
13 | public: |
14 | RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue) |
15 | : RecordingVisitorBase(ShouldTraversePostOrderValue) {} |
16 | |
17 | bool TraverseBinaryOperator(BinaryOperator *BO) { |
18 | recordCallback(__func__, BO, [&]() { |
19 | RecordingVisitorBase::TraverseBinaryOperator(BO); |
20 | }); |
21 | return true; |
22 | } |
23 | |
24 | bool WalkUpFromStmt(Stmt *S) { |
25 | recordCallback(CallbackName: __func__, S, |
26 | CallDefaultFn: [&]() { RecordingVisitorBase::WalkUpFromStmt(S); }); |
27 | return true; |
28 | } |
29 | }; |
30 | |
31 | StringRef Code = R"cpp( |
32 | void test() { |
33 | 1; |
34 | 2 + 3; |
35 | 4; |
36 | } |
37 | )cpp" ; |
38 | |
39 | EXPECT_TRUE(visitorCallbackLogEqual( |
40 | RecordingVisitor(ShouldTraversePostOrder::No), Code, |
41 | R"txt( |
42 | WalkUpFromStmt CompoundStmt |
43 | WalkUpFromStmt IntegerLiteral(1) |
44 | TraverseBinaryOperator BinaryOperator(+) |
45 | WalkUpFromStmt BinaryOperator(+) |
46 | WalkUpFromStmt IntegerLiteral(2) |
47 | WalkUpFromStmt IntegerLiteral(3) |
48 | WalkUpFromStmt IntegerLiteral(4) |
49 | )txt" )); |
50 | |
51 | EXPECT_TRUE(visitorCallbackLogEqual( |
52 | RecordingVisitor(ShouldTraversePostOrder::Yes), Code, |
53 | R"txt( |
54 | WalkUpFromStmt IntegerLiteral(1) |
55 | TraverseBinaryOperator BinaryOperator(+) |
56 | WalkUpFromStmt IntegerLiteral(2) |
57 | WalkUpFromStmt IntegerLiteral(3) |
58 | WalkUpFromStmt BinaryOperator(+) |
59 | WalkUpFromStmt IntegerLiteral(4) |
60 | WalkUpFromStmt CompoundStmt |
61 | )txt" )); |
62 | } |
63 | |
64 | TEST(RecursiveASTVisitor, |
65 | StmtCallbacks_TraverseBinaryOperator_WalkUpFromBinaryOperator) { |
66 | class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> { |
67 | public: |
68 | RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue) |
69 | : RecordingVisitorBase(ShouldTraversePostOrderValue) {} |
70 | |
71 | bool TraverseBinaryOperator(BinaryOperator *BO) { |
72 | recordCallback(__func__, BO, [&]() { |
73 | RecordingVisitorBase::TraverseBinaryOperator(BO); |
74 | }); |
75 | return true; |
76 | } |
77 | |
78 | bool WalkUpFromStmt(Stmt *S) { |
79 | recordCallback(CallbackName: __func__, S, |
80 | CallDefaultFn: [&]() { RecordingVisitorBase::WalkUpFromStmt(S); }); |
81 | return true; |
82 | } |
83 | |
84 | bool WalkUpFromExpr(Expr *E) { |
85 | recordCallback(__func__, E, |
86 | [&]() { RecordingVisitorBase::WalkUpFromExpr(E); }); |
87 | return true; |
88 | } |
89 | |
90 | bool WalkUpFromBinaryOperator(BinaryOperator *BO) { |
91 | recordCallback(__func__, BO, [&]() { |
92 | RecordingVisitorBase::WalkUpFromBinaryOperator(BO); |
93 | }); |
94 | return true; |
95 | } |
96 | }; |
97 | |
98 | StringRef Code = R"cpp( |
99 | void test() { |
100 | 1; |
101 | 2 + 3; |
102 | 4; |
103 | } |
104 | )cpp" ; |
105 | |
106 | EXPECT_TRUE(visitorCallbackLogEqual( |
107 | RecordingVisitor(ShouldTraversePostOrder::No), Code, |
108 | R"txt( |
109 | WalkUpFromStmt CompoundStmt |
110 | WalkUpFromExpr IntegerLiteral(1) |
111 | WalkUpFromStmt IntegerLiteral(1) |
112 | TraverseBinaryOperator BinaryOperator(+) |
113 | WalkUpFromBinaryOperator BinaryOperator(+) |
114 | WalkUpFromExpr BinaryOperator(+) |
115 | WalkUpFromStmt BinaryOperator(+) |
116 | WalkUpFromExpr IntegerLiteral(2) |
117 | WalkUpFromStmt IntegerLiteral(2) |
118 | WalkUpFromExpr IntegerLiteral(3) |
119 | WalkUpFromStmt IntegerLiteral(3) |
120 | WalkUpFromExpr IntegerLiteral(4) |
121 | WalkUpFromStmt IntegerLiteral(4) |
122 | )txt" )); |
123 | |
124 | EXPECT_TRUE(visitorCallbackLogEqual( |
125 | RecordingVisitor(ShouldTraversePostOrder::Yes), Code, |
126 | R"txt( |
127 | WalkUpFromExpr IntegerLiteral(1) |
128 | WalkUpFromStmt IntegerLiteral(1) |
129 | TraverseBinaryOperator BinaryOperator(+) |
130 | WalkUpFromExpr IntegerLiteral(2) |
131 | WalkUpFromStmt IntegerLiteral(2) |
132 | WalkUpFromExpr IntegerLiteral(3) |
133 | WalkUpFromStmt IntegerLiteral(3) |
134 | WalkUpFromBinaryOperator BinaryOperator(+) |
135 | WalkUpFromExpr BinaryOperator(+) |
136 | WalkUpFromStmt BinaryOperator(+) |
137 | WalkUpFromExpr IntegerLiteral(4) |
138 | WalkUpFromStmt IntegerLiteral(4) |
139 | WalkUpFromStmt CompoundStmt |
140 | )txt" )); |
141 | } |
142 | |
143 | TEST(RecursiveASTVisitor, StmtCallbacks_WalkUpFromBinaryOperator) { |
144 | class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> { |
145 | public: |
146 | RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue) |
147 | : RecordingVisitorBase(ShouldTraversePostOrderValue) {} |
148 | |
149 | bool WalkUpFromStmt(Stmt *S) { |
150 | recordCallback(CallbackName: __func__, S, |
151 | CallDefaultFn: [&]() { RecordingVisitorBase::WalkUpFromStmt(S); }); |
152 | return true; |
153 | } |
154 | |
155 | bool WalkUpFromExpr(Expr *E) { |
156 | recordCallback(__func__, E, |
157 | [&]() { RecordingVisitorBase::WalkUpFromExpr(E); }); |
158 | return true; |
159 | } |
160 | |
161 | bool WalkUpFromBinaryOperator(BinaryOperator *BO) { |
162 | recordCallback(__func__, BO, [&]() { |
163 | RecordingVisitorBase::WalkUpFromBinaryOperator(BO); |
164 | }); |
165 | return true; |
166 | } |
167 | }; |
168 | |
169 | StringRef Code = R"cpp( |
170 | void test() { |
171 | 1; |
172 | 2 + 3; |
173 | 4; |
174 | } |
175 | )cpp" ; |
176 | |
177 | EXPECT_TRUE(visitorCallbackLogEqual( |
178 | RecordingVisitor(ShouldTraversePostOrder::No), Code, |
179 | R"txt( |
180 | WalkUpFromStmt CompoundStmt |
181 | WalkUpFromExpr IntegerLiteral(1) |
182 | WalkUpFromStmt IntegerLiteral(1) |
183 | WalkUpFromBinaryOperator BinaryOperator(+) |
184 | WalkUpFromExpr BinaryOperator(+) |
185 | WalkUpFromStmt BinaryOperator(+) |
186 | WalkUpFromExpr IntegerLiteral(2) |
187 | WalkUpFromStmt IntegerLiteral(2) |
188 | WalkUpFromExpr IntegerLiteral(3) |
189 | WalkUpFromStmt IntegerLiteral(3) |
190 | WalkUpFromExpr IntegerLiteral(4) |
191 | WalkUpFromStmt IntegerLiteral(4) |
192 | )txt" )); |
193 | |
194 | EXPECT_TRUE(visitorCallbackLogEqual( |
195 | RecordingVisitor(ShouldTraversePostOrder::Yes), Code, |
196 | R"txt( |
197 | WalkUpFromExpr IntegerLiteral(1) |
198 | WalkUpFromStmt IntegerLiteral(1) |
199 | WalkUpFromExpr IntegerLiteral(2) |
200 | WalkUpFromStmt IntegerLiteral(2) |
201 | WalkUpFromExpr IntegerLiteral(3) |
202 | WalkUpFromStmt IntegerLiteral(3) |
203 | WalkUpFromBinaryOperator BinaryOperator(+) |
204 | WalkUpFromExpr BinaryOperator(+) |
205 | WalkUpFromStmt BinaryOperator(+) |
206 | WalkUpFromExpr IntegerLiteral(4) |
207 | WalkUpFromStmt IntegerLiteral(4) |
208 | WalkUpFromStmt CompoundStmt |
209 | )txt" )); |
210 | } |
211 | |