| 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 |
|
| 11 |
TEST(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(__func__, S, |
| 26 |
[&]() { RecordingVisitorBase::WalkUpFromStmt(S); }); |
| 27 |
return true; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
StringRef Code = R"cpp( |
| 32 |
void add(int, int); |
| 33 |
void 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( |
| 43 |
WalkUpFromStmt CompoundStmt |
| 44 |
TraverseIntegerLiteral IntegerLiteral(1) |
| 45 |
WalkUpFromStmt IntegerLiteral(1) |
| 46 |
WalkUpFromStmt BinaryOperator(+) |
| 47 |
TraverseIntegerLiteral IntegerLiteral(2) |
| 48 |
WalkUpFromStmt IntegerLiteral(2) |
| 49 |
TraverseIntegerLiteral IntegerLiteral(3) |
| 50 |
WalkUpFromStmt IntegerLiteral(3) |
| 51 |
WalkUpFromStmt CallExpr(add) |
| 52 |
WalkUpFromStmt ImplicitCastExpr |
| 53 |
WalkUpFromStmt DeclRefExpr(add) |
| 54 |
TraverseIntegerLiteral IntegerLiteral(4) |
| 55 |
WalkUpFromStmt IntegerLiteral(4) |
| 56 |
TraverseIntegerLiteral IntegerLiteral(5) |
| 57 |
WalkUpFromStmt IntegerLiteral(5) |
| 58 |
)txt")); |
| 59 |
|
| 60 |
EXPECT_TRUE(visitorCallbackLogEqual( |
| 61 |
RecordingVisitor(ShouldTraversePostOrder::Yes), Code, |
| 62 |
R"txt( |
| 63 |
TraverseIntegerLiteral IntegerLiteral(1) |
| 64 |
WalkUpFromStmt IntegerLiteral(1) |
| 65 |
TraverseIntegerLiteral IntegerLiteral(2) |
| 66 |
WalkUpFromStmt IntegerLiteral(2) |
| 67 |
TraverseIntegerLiteral IntegerLiteral(3) |
| 68 |
WalkUpFromStmt IntegerLiteral(3) |
| 69 |
WalkUpFromStmt BinaryOperator(+) |
| 70 |
WalkUpFromStmt DeclRefExpr(add) |
| 71 |
WalkUpFromStmt ImplicitCastExpr |
| 72 |
TraverseIntegerLiteral IntegerLiteral(4) |
| 73 |
WalkUpFromStmt IntegerLiteral(4) |
| 74 |
TraverseIntegerLiteral IntegerLiteral(5) |
| 75 |
WalkUpFromStmt IntegerLiteral(5) |
| 76 |
WalkUpFromStmt CallExpr(add) |
| 77 |
WalkUpFromStmt CompoundStmt |
| 78 |
)txt")); |
| 79 |
} |
| 80 |
|
| 81 |
TEST(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(__func__, S, |
| 96 |
[&]() { 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( |
| 115 |
void add(int, int); |
| 116 |
void 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( |
| 126 |
WalkUpFromStmt CompoundStmt |
| 127 |
TraverseIntegerLiteral IntegerLiteral(1) |
| 128 |
WalkUpFromIntegerLiteral IntegerLiteral(1) |
| 129 |
WalkUpFromExpr IntegerLiteral(1) |
| 130 |
WalkUpFromStmt IntegerLiteral(1) |
| 131 |
WalkUpFromExpr BinaryOperator(+) |
| 132 |
WalkUpFromStmt BinaryOperator(+) |
| 133 |
TraverseIntegerLiteral IntegerLiteral(2) |
| 134 |
WalkUpFromIntegerLiteral IntegerLiteral(2) |
| 135 |
WalkUpFromExpr IntegerLiteral(2) |
| 136 |
WalkUpFromStmt IntegerLiteral(2) |
| 137 |
TraverseIntegerLiteral IntegerLiteral(3) |
| 138 |
WalkUpFromIntegerLiteral IntegerLiteral(3) |
| 139 |
WalkUpFromExpr IntegerLiteral(3) |
| 140 |
WalkUpFromStmt IntegerLiteral(3) |
| 141 |
WalkUpFromExpr CallExpr(add) |
| 142 |
WalkUpFromStmt CallExpr(add) |
| 143 |
WalkUpFromExpr ImplicitCastExpr |
| 144 |
WalkUpFromStmt ImplicitCastExpr |
| 145 |
WalkUpFromExpr DeclRefExpr(add) |
| 146 |
WalkUpFromStmt DeclRefExpr(add) |
| 147 |
TraverseIntegerLiteral IntegerLiteral(4) |
| 148 |
WalkUpFromIntegerLiteral IntegerLiteral(4) |
| 149 |
WalkUpFromExpr IntegerLiteral(4) |
| 150 |
WalkUpFromStmt IntegerLiteral(4) |
| 151 |
TraverseIntegerLiteral 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( |
| 160 |
TraverseIntegerLiteral IntegerLiteral(1) |
| 161 |
WalkUpFromIntegerLiteral IntegerLiteral(1) |
| 162 |
WalkUpFromExpr IntegerLiteral(1) |
| 163 |
WalkUpFromStmt IntegerLiteral(1) |
| 164 |
TraverseIntegerLiteral IntegerLiteral(2) |
| 165 |
WalkUpFromIntegerLiteral IntegerLiteral(2) |
| 166 |
WalkUpFromExpr IntegerLiteral(2) |
| 167 |
WalkUpFromStmt IntegerLiteral(2) |
| 168 |
TraverseIntegerLiteral IntegerLiteral(3) |
| 169 |
WalkUpFromIntegerLiteral IntegerLiteral(3) |
| 170 |
WalkUpFromExpr IntegerLiteral(3) |
| 171 |
WalkUpFromStmt IntegerLiteral(3) |
| 172 |
WalkUpFromExpr BinaryOperator(+) |
| 173 |
WalkUpFromStmt BinaryOperator(+) |
| 174 |
WalkUpFromExpr DeclRefExpr(add) |
| 175 |
WalkUpFromStmt DeclRefExpr(add) |
| 176 |
WalkUpFromExpr ImplicitCastExpr |
| 177 |
WalkUpFromStmt ImplicitCastExpr |
| 178 |
TraverseIntegerLiteral IntegerLiteral(4) |
| 179 |
WalkUpFromIntegerLiteral IntegerLiteral(4) |
| 180 |
WalkUpFromExpr IntegerLiteral(4) |
| 181 |
WalkUpFromStmt IntegerLiteral(4) |
| 182 |
TraverseIntegerLiteral IntegerLiteral(5) |
| 183 |
WalkUpFromIntegerLiteral IntegerLiteral(5) |
| 184 |
WalkUpFromExpr IntegerLiteral(5) |
| 185 |
WalkUpFromStmt IntegerLiteral(5) |
| 186 |
WalkUpFromExpr CallExpr(add) |
| 187 |
WalkUpFromStmt CallExpr(add) |
| 188 |
WalkUpFromStmt CompoundStmt |
| 189 |
)txt")); |
| 190 |
} |
| 191 |
|
| 192 |
TEST(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(__func__, S, |
| 200 |
[&]() { 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( |
| 219 |
void add(int, int); |
| 220 |
void 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( |
| 230 |
WalkUpFromStmt CompoundStmt |
| 231 |
WalkUpFromIntegerLiteral IntegerLiteral(1) |
| 232 |
WalkUpFromExpr IntegerLiteral(1) |
| 233 |
WalkUpFromStmt IntegerLiteral(1) |
| 234 |
WalkUpFromExpr BinaryOperator(+) |
| 235 |
WalkUpFromStmt BinaryOperator(+) |
| 236 |
WalkUpFromIntegerLiteral IntegerLiteral(2) |
| 237 |
WalkUpFromExpr IntegerLiteral(2) |
| 238 |
WalkUpFromStmt IntegerLiteral(2) |
| 239 |
WalkUpFromIntegerLiteral IntegerLiteral(3) |
| 240 |
WalkUpFromExpr IntegerLiteral(3) |
| 241 |
WalkUpFromStmt IntegerLiteral(3) |
| 242 |
WalkUpFromExpr CallExpr(add) |
| 243 |
WalkUpFromStmt CallExpr(add) |
| 244 |
WalkUpFromExpr ImplicitCastExpr |
| 245 |
WalkUpFromStmt ImplicitCastExpr |
| 246 |
WalkUpFromExpr DeclRefExpr(add) |
| 247 |
WalkUpFromStmt DeclRefExpr(add) |
| 248 |
WalkUpFromIntegerLiteral IntegerLiteral(4) |
| 249 |
WalkUpFromExpr IntegerLiteral(4) |
| 250 |
WalkUpFromStmt IntegerLiteral(4) |
| 251 |
WalkUpFromIntegerLiteral 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( |
| 259 |
WalkUpFromIntegerLiteral IntegerLiteral(1) |
| 260 |
WalkUpFromExpr IntegerLiteral(1) |
| 261 |
WalkUpFromStmt IntegerLiteral(1) |
| 262 |
WalkUpFromIntegerLiteral IntegerLiteral(2) |
| 263 |
WalkUpFromExpr IntegerLiteral(2) |
| 264 |
WalkUpFromStmt IntegerLiteral(2) |
| 265 |
WalkUpFromIntegerLiteral IntegerLiteral(3) |
| 266 |
WalkUpFromExpr IntegerLiteral(3) |
| 267 |
WalkUpFromStmt IntegerLiteral(3) |
| 268 |
WalkUpFromExpr BinaryOperator(+) |
| 269 |
WalkUpFromStmt BinaryOperator(+) |
| 270 |
WalkUpFromExpr DeclRefExpr(add) |
| 271 |
WalkUpFromStmt DeclRefExpr(add) |
| 272 |
WalkUpFromExpr ImplicitCastExpr |
| 273 |
WalkUpFromStmt ImplicitCastExpr |
| 274 |
WalkUpFromIntegerLiteral IntegerLiteral(4) |
| 275 |
WalkUpFromExpr IntegerLiteral(4) |
| 276 |
WalkUpFromStmt IntegerLiteral(4) |
| 277 |
WalkUpFromIntegerLiteral IntegerLiteral(5) |
| 278 |
WalkUpFromExpr IntegerLiteral(5) |
| 279 |
WalkUpFromStmt IntegerLiteral(5) |
| 280 |
WalkUpFromExpr CallExpr(add) |
| 281 |
WalkUpFromStmt CallExpr(add) |
| 282 |
WalkUpFromStmt CompoundStmt |
| 283 |
)txt")); |
| 284 |
} |