• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TEST(RecursiveASTVisitor,StmtCallbacks_TraverseCallExpr)11 TEST(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(__func__, S,
25                      [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
26       return true;
27     }
28   };
29 
30   StringRef Code = R"cpp(
31 void add(int, int);
32 void 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(
42 WalkUpFromStmt CompoundStmt
43 WalkUpFromStmt IntegerLiteral(1)
44 WalkUpFromStmt BinaryOperator(+)
45 WalkUpFromStmt IntegerLiteral(2)
46 WalkUpFromStmt IntegerLiteral(3)
47 TraverseCallExpr 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(
58 WalkUpFromStmt IntegerLiteral(1)
59 WalkUpFromStmt IntegerLiteral(2)
60 WalkUpFromStmt IntegerLiteral(3)
61 WalkUpFromStmt BinaryOperator(+)
62 TraverseCallExpr CallExpr(add)
63   WalkUpFromStmt DeclRefExpr(add)
64   WalkUpFromStmt ImplicitCastExpr
65   WalkUpFromStmt IntegerLiteral(4)
66   WalkUpFromStmt IntegerLiteral(5)
67   WalkUpFromStmt CallExpr(add)
68 WalkUpFromStmt CompoundStmt
69 )txt"));
70 }
71 
TEST(RecursiveASTVisitor,StmtCallbacks_TraverseCallExpr_WalkUpFromCallExpr)72 TEST(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(__func__, S,
86                      [&]() { 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(
104 void add(int, int);
105 void 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(
115 WalkUpFromStmt CompoundStmt
116 WalkUpFromExpr IntegerLiteral(1)
117   WalkUpFromStmt IntegerLiteral(1)
118 WalkUpFromExpr BinaryOperator(+)
119   WalkUpFromStmt BinaryOperator(+)
120 WalkUpFromExpr IntegerLiteral(2)
121   WalkUpFromStmt IntegerLiteral(2)
122 WalkUpFromExpr IntegerLiteral(3)
123   WalkUpFromStmt IntegerLiteral(3)
124 TraverseCallExpr 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(
141 WalkUpFromExpr IntegerLiteral(1)
142   WalkUpFromStmt IntegerLiteral(1)
143 WalkUpFromExpr IntegerLiteral(2)
144   WalkUpFromStmt IntegerLiteral(2)
145 WalkUpFromExpr IntegerLiteral(3)
146   WalkUpFromStmt IntegerLiteral(3)
147 WalkUpFromExpr BinaryOperator(+)
148   WalkUpFromStmt BinaryOperator(+)
149 TraverseCallExpr 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)
161 WalkUpFromStmt CompoundStmt
162 )txt"));
163 }
164 
TEST(RecursiveASTVisitor,StmtCallbacks_WalkUpFromCallExpr)165 TEST(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(__func__, S,
173                      [&]() { 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(
191 void add(int, int);
192 void 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(
202 WalkUpFromStmt CompoundStmt
203 WalkUpFromExpr IntegerLiteral(1)
204   WalkUpFromStmt IntegerLiteral(1)
205 WalkUpFromExpr BinaryOperator(+)
206   WalkUpFromStmt BinaryOperator(+)
207 WalkUpFromExpr IntegerLiteral(2)
208   WalkUpFromStmt IntegerLiteral(2)
209 WalkUpFromExpr IntegerLiteral(3)
210   WalkUpFromStmt IntegerLiteral(3)
211 WalkUpFromCallExpr CallExpr(add)
212   WalkUpFromExpr CallExpr(add)
213     WalkUpFromStmt CallExpr(add)
214 WalkUpFromExpr ImplicitCastExpr
215   WalkUpFromStmt ImplicitCastExpr
216 WalkUpFromExpr DeclRefExpr(add)
217   WalkUpFromStmt DeclRefExpr(add)
218 WalkUpFromExpr IntegerLiteral(4)
219   WalkUpFromStmt IntegerLiteral(4)
220 WalkUpFromExpr IntegerLiteral(5)
221   WalkUpFromStmt IntegerLiteral(5)
222 )txt"));
223 
224   EXPECT_TRUE(visitorCallbackLogEqual(
225       RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
226       R"txt(
227 WalkUpFromExpr IntegerLiteral(1)
228   WalkUpFromStmt IntegerLiteral(1)
229 WalkUpFromExpr IntegerLiteral(2)
230   WalkUpFromStmt IntegerLiteral(2)
231 WalkUpFromExpr IntegerLiteral(3)
232   WalkUpFromStmt IntegerLiteral(3)
233 WalkUpFromExpr BinaryOperator(+)
234   WalkUpFromStmt BinaryOperator(+)
235 WalkUpFromExpr DeclRefExpr(add)
236   WalkUpFromStmt DeclRefExpr(add)
237 WalkUpFromExpr ImplicitCastExpr
238   WalkUpFromStmt ImplicitCastExpr
239 WalkUpFromExpr IntegerLiteral(4)
240   WalkUpFromStmt IntegerLiteral(4)
241 WalkUpFromExpr IntegerLiteral(5)
242   WalkUpFromStmt IntegerLiteral(5)
243 WalkUpFromCallExpr CallExpr(add)
244   WalkUpFromExpr CallExpr(add)
245     WalkUpFromStmt CallExpr(add)
246 WalkUpFromStmt CompoundStmt
247 )txt"));
248 }
249