1//===- CallbacksCompoundAssignOperator.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_TraverseCompoundAssignOperator) {
12 class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
13 public:
14 RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
15 : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
16
17 bool TraverseCompoundAssignOperator(CompoundAssignOperator *CAO) {
18 recordCallback(__func__, CAO, [&]() {
19 RecordingVisitorBase::TraverseCompoundAssignOperator(CAO);
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 test(int a) {
33 1;
34 a += 2;
35 3;
36}
37)cpp";
38
39 EXPECT_TRUE(visitorCallbackLogEqual(
40 RecordingVisitor(ShouldTraversePostOrder::No), Code,
41 R"txt(
42WalkUpFromStmt CompoundStmt
43WalkUpFromStmt IntegerLiteral(1)
44TraverseCompoundAssignOperator CompoundAssignOperator(+=)
45 WalkUpFromStmt CompoundAssignOperator(+=)
46 WalkUpFromStmt DeclRefExpr(a)
47 WalkUpFromStmt IntegerLiteral(2)
48WalkUpFromStmt IntegerLiteral(3)
49)txt"));
50
51 EXPECT_TRUE(visitorCallbackLogEqual(
52 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
53 R"txt(
54WalkUpFromStmt IntegerLiteral(1)
55TraverseCompoundAssignOperator CompoundAssignOperator(+=)
56 WalkUpFromStmt DeclRefExpr(a)
57 WalkUpFromStmt IntegerLiteral(2)
58 WalkUpFromStmt CompoundAssignOperator(+=)
59WalkUpFromStmt IntegerLiteral(3)
60WalkUpFromStmt CompoundStmt
61)txt"));
62}
63
64TEST(
65 RecursiveASTVisitor,
66 StmtCallbacks_TraverseCompoundAssignOperator_WalkUpFromCompoundAssignOperator) {
67 class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
68 public:
69 RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
70 : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
71
72 bool TraverseCompoundAssignOperator(CompoundAssignOperator *CAO) {
73 recordCallback(__func__, CAO, [&]() {
74 RecordingVisitorBase::TraverseCompoundAssignOperator(CAO);
75 });
76 return true;
77 }
78
79 bool WalkUpFromStmt(Stmt *S) {
80 recordCallback(CallbackName: __func__, S,
81 CallDefaultFn: [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
82 return true;
83 }
84
85 bool WalkUpFromExpr(Expr *E) {
86 recordCallback(__func__, E,
87 [&]() { RecordingVisitorBase::WalkUpFromExpr(E); });
88 return true;
89 }
90
91 bool WalkUpFromCompoundAssignOperator(CompoundAssignOperator *CAO) {
92 recordCallback(__func__, CAO, [&]() {
93 RecordingVisitorBase::WalkUpFromCompoundAssignOperator(CAO);
94 });
95 return true;
96 }
97 };
98
99 StringRef Code = R"cpp(
100void test(int a) {
101 1;
102 a += 2;
103 3;
104}
105)cpp";
106
107 EXPECT_TRUE(visitorCallbackLogEqual(
108 RecordingVisitor(ShouldTraversePostOrder::No), Code,
109 R"txt(
110WalkUpFromStmt CompoundStmt
111WalkUpFromExpr IntegerLiteral(1)
112 WalkUpFromStmt IntegerLiteral(1)
113TraverseCompoundAssignOperator CompoundAssignOperator(+=)
114 WalkUpFromCompoundAssignOperator CompoundAssignOperator(+=)
115 WalkUpFromExpr CompoundAssignOperator(+=)
116 WalkUpFromStmt CompoundAssignOperator(+=)
117 WalkUpFromExpr DeclRefExpr(a)
118 WalkUpFromStmt DeclRefExpr(a)
119 WalkUpFromExpr IntegerLiteral(2)
120 WalkUpFromStmt IntegerLiteral(2)
121WalkUpFromExpr IntegerLiteral(3)
122 WalkUpFromStmt IntegerLiteral(3)
123)txt"));
124
125 EXPECT_TRUE(visitorCallbackLogEqual(
126 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
127 R"txt(
128WalkUpFromExpr IntegerLiteral(1)
129 WalkUpFromStmt IntegerLiteral(1)
130TraverseCompoundAssignOperator CompoundAssignOperator(+=)
131 WalkUpFromExpr DeclRefExpr(a)
132 WalkUpFromStmt DeclRefExpr(a)
133 WalkUpFromExpr IntegerLiteral(2)
134 WalkUpFromStmt IntegerLiteral(2)
135 WalkUpFromCompoundAssignOperator CompoundAssignOperator(+=)
136 WalkUpFromExpr CompoundAssignOperator(+=)
137 WalkUpFromStmt CompoundAssignOperator(+=)
138WalkUpFromExpr IntegerLiteral(3)
139 WalkUpFromStmt IntegerLiteral(3)
140WalkUpFromStmt CompoundStmt
141)txt"));
142}
143
144TEST(RecursiveASTVisitor, StmtCallbacks_WalkUpFromCompoundAssignOperator) {
145 class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
146 public:
147 RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
148 : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
149
150 bool WalkUpFromStmt(Stmt *S) {
151 recordCallback(CallbackName: __func__, S,
152 CallDefaultFn: [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
153 return true;
154 }
155
156 bool WalkUpFromExpr(Expr *E) {
157 recordCallback(__func__, E,
158 [&]() { RecordingVisitorBase::WalkUpFromExpr(E); });
159 return true;
160 }
161
162 bool WalkUpFromCompoundAssignOperator(CompoundAssignOperator *CAO) {
163 recordCallback(__func__, CAO, [&]() {
164 RecordingVisitorBase::WalkUpFromCompoundAssignOperator(CAO);
165 });
166 return true;
167 }
168 };
169
170 StringRef Code = R"cpp(
171void test(int a) {
172 1;
173 a += 2;
174 3;
175}
176)cpp";
177
178 EXPECT_TRUE(visitorCallbackLogEqual(
179 RecordingVisitor(ShouldTraversePostOrder::No), Code,
180 R"txt(
181WalkUpFromStmt CompoundStmt
182WalkUpFromExpr IntegerLiteral(1)
183 WalkUpFromStmt IntegerLiteral(1)
184WalkUpFromCompoundAssignOperator CompoundAssignOperator(+=)
185 WalkUpFromExpr CompoundAssignOperator(+=)
186 WalkUpFromStmt CompoundAssignOperator(+=)
187WalkUpFromExpr DeclRefExpr(a)
188 WalkUpFromStmt DeclRefExpr(a)
189WalkUpFromExpr IntegerLiteral(2)
190 WalkUpFromStmt IntegerLiteral(2)
191WalkUpFromExpr IntegerLiteral(3)
192 WalkUpFromStmt IntegerLiteral(3)
193)txt"));
194
195 EXPECT_TRUE(visitorCallbackLogEqual(
196 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
197 R"txt(
198WalkUpFromExpr IntegerLiteral(1)
199 WalkUpFromStmt IntegerLiteral(1)
200WalkUpFromExpr DeclRefExpr(a)
201 WalkUpFromStmt DeclRefExpr(a)
202WalkUpFromExpr IntegerLiteral(2)
203 WalkUpFromStmt IntegerLiteral(2)
204WalkUpFromCompoundAssignOperator CompoundAssignOperator(+=)
205 WalkUpFromExpr CompoundAssignOperator(+=)
206 WalkUpFromStmt CompoundAssignOperator(+=)
207WalkUpFromExpr IntegerLiteral(3)
208 WalkUpFromStmt IntegerLiteral(3)
209WalkUpFromStmt CompoundStmt
210)txt"));
211}
212

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