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

source code of clang/unittests/Tooling/RecursiveASTVisitorTests/CallbacksLeaf.cpp