xref: /llvm-project/clang/unittests/Tooling/RecursiveASTVisitorTests/CallbacksLeaf.cpp (revision 0741a2c9caca864fc30af2104712d02accdc12e6)
1*0741a2c9SStefan Pintilie //===--- unittests/Tooling/RecursiveASTVisitorTests/CallbacksLeaf.cpp -----===//
2*0741a2c9SStefan Pintilie //
3*0741a2c9SStefan Pintilie // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0741a2c9SStefan Pintilie // See https://llvm.org/LICENSE.txt for license information.
5*0741a2c9SStefan Pintilie // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0741a2c9SStefan Pintilie //
7*0741a2c9SStefan Pintilie //===----------------------------------------------------------------------===//
8*0741a2c9SStefan Pintilie 
9*0741a2c9SStefan Pintilie #include "CallbacksCommon.h"
10*0741a2c9SStefan Pintilie 
TEST(RecursiveASTVisitor,StmtCallbacks_TraverseLeaf)11*0741a2c9SStefan Pintilie TEST(RecursiveASTVisitor, StmtCallbacks_TraverseLeaf) {
12*0741a2c9SStefan Pintilie   class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
13*0741a2c9SStefan Pintilie   public:
14*0741a2c9SStefan Pintilie     RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
15*0741a2c9SStefan Pintilie         : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
16*0741a2c9SStefan Pintilie 
17*0741a2c9SStefan Pintilie     bool TraverseIntegerLiteral(IntegerLiteral *IL) {
18*0741a2c9SStefan Pintilie       recordCallback(__func__, IL, [&]() {
19*0741a2c9SStefan Pintilie         RecordingVisitorBase::TraverseIntegerLiteral(IL);
20*0741a2c9SStefan Pintilie       });
21*0741a2c9SStefan Pintilie       return true;
22*0741a2c9SStefan Pintilie     }
23*0741a2c9SStefan Pintilie 
24*0741a2c9SStefan Pintilie     bool WalkUpFromStmt(Stmt *S) {
25*0741a2c9SStefan Pintilie       recordCallback(__func__, S,
26*0741a2c9SStefan Pintilie                      [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
27*0741a2c9SStefan Pintilie       return true;
28*0741a2c9SStefan Pintilie     }
29*0741a2c9SStefan Pintilie   };
30*0741a2c9SStefan Pintilie 
31*0741a2c9SStefan Pintilie   StringRef Code = R"cpp(
32*0741a2c9SStefan Pintilie void add(int, int);
33*0741a2c9SStefan Pintilie void test() {
34*0741a2c9SStefan Pintilie   1;
35*0741a2c9SStefan Pintilie   2 + 3;
36*0741a2c9SStefan Pintilie   add(4, 5);
37*0741a2c9SStefan Pintilie }
38*0741a2c9SStefan Pintilie )cpp";
39*0741a2c9SStefan Pintilie 
40*0741a2c9SStefan Pintilie   EXPECT_TRUE(visitorCallbackLogEqual(
41*0741a2c9SStefan Pintilie       RecordingVisitor(ShouldTraversePostOrder::No), Code,
42*0741a2c9SStefan Pintilie       R"txt(
43*0741a2c9SStefan Pintilie WalkUpFromStmt CompoundStmt
44*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(1)
45*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(1)
46*0741a2c9SStefan Pintilie WalkUpFromStmt BinaryOperator(+)
47*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(2)
48*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(2)
49*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(3)
50*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(3)
51*0741a2c9SStefan Pintilie WalkUpFromStmt CallExpr(add)
52*0741a2c9SStefan Pintilie WalkUpFromStmt ImplicitCastExpr
53*0741a2c9SStefan Pintilie WalkUpFromStmt DeclRefExpr(add)
54*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(4)
55*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(4)
56*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(5)
57*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(5)
58*0741a2c9SStefan Pintilie )txt"));
59*0741a2c9SStefan Pintilie 
60*0741a2c9SStefan Pintilie   EXPECT_TRUE(visitorCallbackLogEqual(
61*0741a2c9SStefan Pintilie       RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
62*0741a2c9SStefan Pintilie       R"txt(
63*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(1)
64*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(1)
65*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(2)
66*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(2)
67*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(3)
68*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(3)
69*0741a2c9SStefan Pintilie WalkUpFromStmt BinaryOperator(+)
70*0741a2c9SStefan Pintilie WalkUpFromStmt DeclRefExpr(add)
71*0741a2c9SStefan Pintilie WalkUpFromStmt ImplicitCastExpr
72*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(4)
73*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(4)
74*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(5)
75*0741a2c9SStefan Pintilie   WalkUpFromStmt IntegerLiteral(5)
76*0741a2c9SStefan Pintilie WalkUpFromStmt CallExpr(add)
77*0741a2c9SStefan Pintilie WalkUpFromStmt CompoundStmt
78*0741a2c9SStefan Pintilie )txt"));
79*0741a2c9SStefan Pintilie }
80*0741a2c9SStefan Pintilie 
TEST(RecursiveASTVisitor,StmtCallbacks_TraverseLeaf_WalkUpFromLeaf)81*0741a2c9SStefan Pintilie TEST(RecursiveASTVisitor, StmtCallbacks_TraverseLeaf_WalkUpFromLeaf) {
82*0741a2c9SStefan Pintilie   class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
83*0741a2c9SStefan Pintilie   public:
84*0741a2c9SStefan Pintilie     RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
85*0741a2c9SStefan Pintilie         : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
86*0741a2c9SStefan Pintilie 
87*0741a2c9SStefan Pintilie     bool TraverseIntegerLiteral(IntegerLiteral *IL) {
88*0741a2c9SStefan Pintilie       recordCallback(__func__, IL, [&]() {
89*0741a2c9SStefan Pintilie         RecordingVisitorBase::TraverseIntegerLiteral(IL);
90*0741a2c9SStefan Pintilie       });
91*0741a2c9SStefan Pintilie       return true;
92*0741a2c9SStefan Pintilie     }
93*0741a2c9SStefan Pintilie 
94*0741a2c9SStefan Pintilie     bool WalkUpFromStmt(Stmt *S) {
95*0741a2c9SStefan Pintilie       recordCallback(__func__, S,
96*0741a2c9SStefan Pintilie                      [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
97*0741a2c9SStefan Pintilie       return true;
98*0741a2c9SStefan Pintilie     }
99*0741a2c9SStefan Pintilie 
100*0741a2c9SStefan Pintilie     bool WalkUpFromExpr(Expr *E) {
101*0741a2c9SStefan Pintilie       recordCallback(__func__, E,
102*0741a2c9SStefan Pintilie                      [&]() { RecordingVisitorBase::WalkUpFromExpr(E); });
103*0741a2c9SStefan Pintilie       return true;
104*0741a2c9SStefan Pintilie     }
105*0741a2c9SStefan Pintilie 
106*0741a2c9SStefan Pintilie     bool WalkUpFromIntegerLiteral(IntegerLiteral *IL) {
107*0741a2c9SStefan Pintilie       recordCallback(__func__, IL, [&]() {
108*0741a2c9SStefan Pintilie         RecordingVisitorBase::WalkUpFromIntegerLiteral(IL);
109*0741a2c9SStefan Pintilie       });
110*0741a2c9SStefan Pintilie       return true;
111*0741a2c9SStefan Pintilie     }
112*0741a2c9SStefan Pintilie   };
113*0741a2c9SStefan Pintilie 
114*0741a2c9SStefan Pintilie   StringRef Code = R"cpp(
115*0741a2c9SStefan Pintilie void add(int, int);
116*0741a2c9SStefan Pintilie void test() {
117*0741a2c9SStefan Pintilie   1;
118*0741a2c9SStefan Pintilie   2 + 3;
119*0741a2c9SStefan Pintilie   add(4, 5);
120*0741a2c9SStefan Pintilie }
121*0741a2c9SStefan Pintilie )cpp";
122*0741a2c9SStefan Pintilie 
123*0741a2c9SStefan Pintilie   EXPECT_TRUE(visitorCallbackLogEqual(
124*0741a2c9SStefan Pintilie       RecordingVisitor(ShouldTraversePostOrder::No), Code,
125*0741a2c9SStefan Pintilie       R"txt(
126*0741a2c9SStefan Pintilie WalkUpFromStmt CompoundStmt
127*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(1)
128*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(1)
129*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(1)
130*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(1)
131*0741a2c9SStefan Pintilie WalkUpFromExpr BinaryOperator(+)
132*0741a2c9SStefan Pintilie   WalkUpFromStmt BinaryOperator(+)
133*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(2)
134*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(2)
135*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(2)
136*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(2)
137*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(3)
138*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(3)
139*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(3)
140*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(3)
141*0741a2c9SStefan Pintilie WalkUpFromExpr CallExpr(add)
142*0741a2c9SStefan Pintilie   WalkUpFromStmt CallExpr(add)
143*0741a2c9SStefan Pintilie WalkUpFromExpr ImplicitCastExpr
144*0741a2c9SStefan Pintilie   WalkUpFromStmt ImplicitCastExpr
145*0741a2c9SStefan Pintilie WalkUpFromExpr DeclRefExpr(add)
146*0741a2c9SStefan Pintilie   WalkUpFromStmt DeclRefExpr(add)
147*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(4)
148*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(4)
149*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(4)
150*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(4)
151*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(5)
152*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(5)
153*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(5)
154*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(5)
155*0741a2c9SStefan Pintilie )txt"));
156*0741a2c9SStefan Pintilie 
157*0741a2c9SStefan Pintilie   EXPECT_TRUE(visitorCallbackLogEqual(
158*0741a2c9SStefan Pintilie       RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
159*0741a2c9SStefan Pintilie       R"txt(
160*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(1)
161*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(1)
162*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(1)
163*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(1)
164*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(2)
165*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(2)
166*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(2)
167*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(2)
168*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(3)
169*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(3)
170*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(3)
171*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(3)
172*0741a2c9SStefan Pintilie WalkUpFromExpr BinaryOperator(+)
173*0741a2c9SStefan Pintilie   WalkUpFromStmt BinaryOperator(+)
174*0741a2c9SStefan Pintilie WalkUpFromExpr DeclRefExpr(add)
175*0741a2c9SStefan Pintilie   WalkUpFromStmt DeclRefExpr(add)
176*0741a2c9SStefan Pintilie WalkUpFromExpr ImplicitCastExpr
177*0741a2c9SStefan Pintilie   WalkUpFromStmt ImplicitCastExpr
178*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(4)
179*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(4)
180*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(4)
181*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(4)
182*0741a2c9SStefan Pintilie TraverseIntegerLiteral IntegerLiteral(5)
183*0741a2c9SStefan Pintilie   WalkUpFromIntegerLiteral IntegerLiteral(5)
184*0741a2c9SStefan Pintilie     WalkUpFromExpr IntegerLiteral(5)
185*0741a2c9SStefan Pintilie       WalkUpFromStmt IntegerLiteral(5)
186*0741a2c9SStefan Pintilie WalkUpFromExpr CallExpr(add)
187*0741a2c9SStefan Pintilie   WalkUpFromStmt CallExpr(add)
188*0741a2c9SStefan Pintilie WalkUpFromStmt CompoundStmt
189*0741a2c9SStefan Pintilie )txt"));
190*0741a2c9SStefan Pintilie }
191*0741a2c9SStefan Pintilie 
TEST(RecursiveASTVisitor,StmtCallbacks_WalkUpFromLeaf)192*0741a2c9SStefan Pintilie TEST(RecursiveASTVisitor, StmtCallbacks_WalkUpFromLeaf) {
193*0741a2c9SStefan Pintilie   class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
194*0741a2c9SStefan Pintilie   public:
195*0741a2c9SStefan Pintilie     RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
196*0741a2c9SStefan Pintilie         : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
197*0741a2c9SStefan Pintilie 
198*0741a2c9SStefan Pintilie     bool WalkUpFromStmt(Stmt *S) {
199*0741a2c9SStefan Pintilie       recordCallback(__func__, S,
200*0741a2c9SStefan Pintilie                      [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
201*0741a2c9SStefan Pintilie       return true;
202*0741a2c9SStefan Pintilie     }
203*0741a2c9SStefan Pintilie 
204*0741a2c9SStefan Pintilie     bool WalkUpFromExpr(Expr *E) {
205*0741a2c9SStefan Pintilie       recordCallback(__func__, E,
206*0741a2c9SStefan Pintilie                      [&]() { RecordingVisitorBase::WalkUpFromExpr(E); });
207*0741a2c9SStefan Pintilie       return true;
208*0741a2c9SStefan Pintilie     }
209*0741a2c9SStefan Pintilie 
210*0741a2c9SStefan Pintilie     bool WalkUpFromIntegerLiteral(IntegerLiteral *IL) {
211*0741a2c9SStefan Pintilie       recordCallback(__func__, IL, [&]() {
212*0741a2c9SStefan Pintilie         RecordingVisitorBase::WalkUpFromIntegerLiteral(IL);
213*0741a2c9SStefan Pintilie       });
214*0741a2c9SStefan Pintilie       return true;
215*0741a2c9SStefan Pintilie     }
216*0741a2c9SStefan Pintilie   };
217*0741a2c9SStefan Pintilie 
218*0741a2c9SStefan Pintilie   StringRef Code = R"cpp(
219*0741a2c9SStefan Pintilie void add(int, int);
220*0741a2c9SStefan Pintilie void test() {
221*0741a2c9SStefan Pintilie   1;
222*0741a2c9SStefan Pintilie   2 + 3;
223*0741a2c9SStefan Pintilie   add(4, 5);
224*0741a2c9SStefan Pintilie }
225*0741a2c9SStefan Pintilie )cpp";
226*0741a2c9SStefan Pintilie 
227*0741a2c9SStefan Pintilie   EXPECT_TRUE(visitorCallbackLogEqual(
228*0741a2c9SStefan Pintilie       RecordingVisitor(ShouldTraversePostOrder::No), Code,
229*0741a2c9SStefan Pintilie       R"txt(
230*0741a2c9SStefan Pintilie WalkUpFromStmt CompoundStmt
231*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(1)
232*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(1)
233*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(1)
234*0741a2c9SStefan Pintilie WalkUpFromExpr BinaryOperator(+)
235*0741a2c9SStefan Pintilie   WalkUpFromStmt BinaryOperator(+)
236*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(2)
237*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(2)
238*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(2)
239*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(3)
240*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(3)
241*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(3)
242*0741a2c9SStefan Pintilie WalkUpFromExpr CallExpr(add)
243*0741a2c9SStefan Pintilie   WalkUpFromStmt CallExpr(add)
244*0741a2c9SStefan Pintilie WalkUpFromExpr ImplicitCastExpr
245*0741a2c9SStefan Pintilie   WalkUpFromStmt ImplicitCastExpr
246*0741a2c9SStefan Pintilie WalkUpFromExpr DeclRefExpr(add)
247*0741a2c9SStefan Pintilie   WalkUpFromStmt DeclRefExpr(add)
248*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(4)
249*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(4)
250*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(4)
251*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(5)
252*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(5)
253*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(5)
254*0741a2c9SStefan Pintilie )txt"));
255*0741a2c9SStefan Pintilie 
256*0741a2c9SStefan Pintilie   EXPECT_TRUE(visitorCallbackLogEqual(
257*0741a2c9SStefan Pintilie       RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
258*0741a2c9SStefan Pintilie       R"txt(
259*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(1)
260*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(1)
261*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(1)
262*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(2)
263*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(2)
264*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(2)
265*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(3)
266*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(3)
267*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(3)
268*0741a2c9SStefan Pintilie WalkUpFromExpr BinaryOperator(+)
269*0741a2c9SStefan Pintilie   WalkUpFromStmt BinaryOperator(+)
270*0741a2c9SStefan Pintilie WalkUpFromExpr DeclRefExpr(add)
271*0741a2c9SStefan Pintilie   WalkUpFromStmt DeclRefExpr(add)
272*0741a2c9SStefan Pintilie WalkUpFromExpr ImplicitCastExpr
273*0741a2c9SStefan Pintilie   WalkUpFromStmt ImplicitCastExpr
274*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(4)
275*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(4)
276*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(4)
277*0741a2c9SStefan Pintilie WalkUpFromIntegerLiteral IntegerLiteral(5)
278*0741a2c9SStefan Pintilie   WalkUpFromExpr IntegerLiteral(5)
279*0741a2c9SStefan Pintilie     WalkUpFromStmt IntegerLiteral(5)
280*0741a2c9SStefan Pintilie WalkUpFromExpr CallExpr(add)
281*0741a2c9SStefan Pintilie   WalkUpFromStmt CallExpr(add)
282*0741a2c9SStefan Pintilie WalkUpFromStmt CompoundStmt
283*0741a2c9SStefan Pintilie )txt"));
284*0741a2c9SStefan Pintilie }
285