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

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