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