Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Tooling / RecursiveASTVisitorTests / CallbacksBinaryOperator.cpp
blob4c5b2b66c068fddcb6d331f1d29e14ba34b303a4
1 //===- unittests/Tooling/RecursiveASTVisitorTests/CallbacksBinaryOperator.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 //===----------------------------------------------------------------------===//
9 #include "CallbacksCommon.h"
11 TEST(RecursiveASTVisitor, StmtCallbacks_TraverseBinaryOperator) {
12 class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
13 public:
14 RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
15 : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
17 bool TraverseBinaryOperator(BinaryOperator *BO) {
18 recordCallback(__func__, BO, [&]() {
19 RecordingVisitorBase::TraverseBinaryOperator(BO);
20 });
21 return true;
24 bool WalkUpFromStmt(Stmt *S) {
25 recordCallback(__func__, S,
26 [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
27 return true;
31 StringRef Code = R"cpp(
32 void test() {
34 2 + 3;
37 )cpp";
39 EXPECT_TRUE(visitorCallbackLogEqual(
40 RecordingVisitor(ShouldTraversePostOrder::No), Code,
41 R"txt(
42 WalkUpFromStmt CompoundStmt
43 WalkUpFromStmt IntegerLiteral(1)
44 TraverseBinaryOperator BinaryOperator(+)
45 WalkUpFromStmt BinaryOperator(+)
46 WalkUpFromStmt IntegerLiteral(2)
47 WalkUpFromStmt IntegerLiteral(3)
48 WalkUpFromStmt IntegerLiteral(4)
49 )txt"));
51 EXPECT_TRUE(visitorCallbackLogEqual(
52 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
53 R"txt(
54 WalkUpFromStmt IntegerLiteral(1)
55 TraverseBinaryOperator BinaryOperator(+)
56 WalkUpFromStmt IntegerLiteral(2)
57 WalkUpFromStmt IntegerLiteral(3)
58 WalkUpFromStmt BinaryOperator(+)
59 WalkUpFromStmt IntegerLiteral(4)
60 WalkUpFromStmt CompoundStmt
61 )txt"));
64 TEST(RecursiveASTVisitor,
65 StmtCallbacks_TraverseBinaryOperator_WalkUpFromBinaryOperator) {
66 class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
67 public:
68 RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
69 : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
71 bool TraverseBinaryOperator(BinaryOperator *BO) {
72 recordCallback(__func__, BO, [&]() {
73 RecordingVisitorBase::TraverseBinaryOperator(BO);
74 });
75 return true;
78 bool WalkUpFromStmt(Stmt *S) {
79 recordCallback(__func__, S,
80 [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
81 return true;
84 bool WalkUpFromExpr(Expr *E) {
85 recordCallback(__func__, E,
86 [&]() { RecordingVisitorBase::WalkUpFromExpr(E); });
87 return true;
90 bool WalkUpFromBinaryOperator(BinaryOperator *BO) {
91 recordCallback(__func__, BO, [&]() {
92 RecordingVisitorBase::WalkUpFromBinaryOperator(BO);
93 });
94 return true;
98 StringRef Code = R"cpp(
99 void test() {
101 2 + 3;
104 )cpp";
106 EXPECT_TRUE(visitorCallbackLogEqual(
107 RecordingVisitor(ShouldTraversePostOrder::No), Code,
108 R"txt(
109 WalkUpFromStmt CompoundStmt
110 WalkUpFromExpr IntegerLiteral(1)
111 WalkUpFromStmt IntegerLiteral(1)
112 TraverseBinaryOperator BinaryOperator(+)
113 WalkUpFromBinaryOperator BinaryOperator(+)
114 WalkUpFromExpr BinaryOperator(+)
115 WalkUpFromStmt BinaryOperator(+)
116 WalkUpFromExpr IntegerLiteral(2)
117 WalkUpFromStmt IntegerLiteral(2)
118 WalkUpFromExpr IntegerLiteral(3)
119 WalkUpFromStmt IntegerLiteral(3)
120 WalkUpFromExpr IntegerLiteral(4)
121 WalkUpFromStmt IntegerLiteral(4)
122 )txt"));
124 EXPECT_TRUE(visitorCallbackLogEqual(
125 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
126 R"txt(
127 WalkUpFromExpr IntegerLiteral(1)
128 WalkUpFromStmt IntegerLiteral(1)
129 TraverseBinaryOperator BinaryOperator(+)
130 WalkUpFromExpr IntegerLiteral(2)
131 WalkUpFromStmt IntegerLiteral(2)
132 WalkUpFromExpr IntegerLiteral(3)
133 WalkUpFromStmt IntegerLiteral(3)
134 WalkUpFromBinaryOperator BinaryOperator(+)
135 WalkUpFromExpr BinaryOperator(+)
136 WalkUpFromStmt BinaryOperator(+)
137 WalkUpFromExpr IntegerLiteral(4)
138 WalkUpFromStmt IntegerLiteral(4)
139 WalkUpFromStmt CompoundStmt
140 )txt"));
143 TEST(RecursiveASTVisitor, StmtCallbacks_WalkUpFromBinaryOperator) {
144 class RecordingVisitor : public RecordingVisitorBase<RecordingVisitor> {
145 public:
146 RecordingVisitor(ShouldTraversePostOrder ShouldTraversePostOrderValue)
147 : RecordingVisitorBase(ShouldTraversePostOrderValue) {}
149 bool WalkUpFromStmt(Stmt *S) {
150 recordCallback(__func__, S,
151 [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
152 return true;
155 bool WalkUpFromExpr(Expr *E) {
156 recordCallback(__func__, E,
157 [&]() { RecordingVisitorBase::WalkUpFromExpr(E); });
158 return true;
161 bool WalkUpFromBinaryOperator(BinaryOperator *BO) {
162 recordCallback(__func__, BO, [&]() {
163 RecordingVisitorBase::WalkUpFromBinaryOperator(BO);
165 return true;
169 StringRef Code = R"cpp(
170 void test() {
172 2 + 3;
175 )cpp";
177 EXPECT_TRUE(visitorCallbackLogEqual(
178 RecordingVisitor(ShouldTraversePostOrder::No), Code,
179 R"txt(
180 WalkUpFromStmt CompoundStmt
181 WalkUpFromExpr IntegerLiteral(1)
182 WalkUpFromStmt IntegerLiteral(1)
183 WalkUpFromBinaryOperator BinaryOperator(+)
184 WalkUpFromExpr BinaryOperator(+)
185 WalkUpFromStmt BinaryOperator(+)
186 WalkUpFromExpr IntegerLiteral(2)
187 WalkUpFromStmt IntegerLiteral(2)
188 WalkUpFromExpr IntegerLiteral(3)
189 WalkUpFromStmt IntegerLiteral(3)
190 WalkUpFromExpr IntegerLiteral(4)
191 WalkUpFromStmt IntegerLiteral(4)
192 )txt"));
194 EXPECT_TRUE(visitorCallbackLogEqual(
195 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
196 R"txt(
197 WalkUpFromExpr IntegerLiteral(1)
198 WalkUpFromStmt IntegerLiteral(1)
199 WalkUpFromExpr IntegerLiteral(2)
200 WalkUpFromStmt IntegerLiteral(2)
201 WalkUpFromExpr IntegerLiteral(3)
202 WalkUpFromStmt IntegerLiteral(3)
203 WalkUpFromBinaryOperator BinaryOperator(+)
204 WalkUpFromExpr BinaryOperator(+)
205 WalkUpFromStmt BinaryOperator(+)
206 WalkUpFromExpr IntegerLiteral(4)
207 WalkUpFromStmt IntegerLiteral(4)
208 WalkUpFromStmt CompoundStmt
209 )txt"));