1//===- 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
11TEST(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(CallbackName: __func__, S,
26 CallDefaultFn: [&]() { RecordingVisitorBase::WalkUpFromStmt(S); });
27 return true;
28 }
29 };
30
31 StringRef Code = R"cpp(
32void test() {
33 1;
34 -2;
35 3;
36}
37)cpp";
38
39 EXPECT_TRUE(visitorCallbackLogEqual(
40 RecordingVisitor(ShouldTraversePostOrder::No), Code,
41 R"txt(
42WalkUpFromStmt CompoundStmt
43WalkUpFromStmt IntegerLiteral(1)
44TraverseUnaryOperator UnaryOperator(-)
45 WalkUpFromStmt UnaryOperator(-)
46 WalkUpFromStmt IntegerLiteral(2)
47WalkUpFromStmt IntegerLiteral(3)
48)txt"));
49
50 EXPECT_TRUE(visitorCallbackLogEqual(
51 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
52 R"txt(
53WalkUpFromStmt IntegerLiteral(1)
54TraverseUnaryOperator UnaryOperator(-)
55 WalkUpFromStmt IntegerLiteral(2)
56 WalkUpFromStmt UnaryOperator(-)
57WalkUpFromStmt IntegerLiteral(3)
58WalkUpFromStmt CompoundStmt
59)txt"));
60}
61
62TEST(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(CallbackName: __func__, S,
78 CallDefaultFn: [&]() { 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(
97void test() {
98 1;
99 -2;
100 3;
101}
102)cpp";
103
104 EXPECT_TRUE(visitorCallbackLogEqual(
105 RecordingVisitor(ShouldTraversePostOrder::No), Code,
106 R"txt(
107WalkUpFromStmt CompoundStmt
108WalkUpFromExpr IntegerLiteral(1)
109 WalkUpFromStmt IntegerLiteral(1)
110TraverseUnaryOperator UnaryOperator(-)
111 WalkUpFromUnaryOperator UnaryOperator(-)
112 WalkUpFromExpr UnaryOperator(-)
113 WalkUpFromStmt UnaryOperator(-)
114 WalkUpFromExpr IntegerLiteral(2)
115 WalkUpFromStmt IntegerLiteral(2)
116WalkUpFromExpr IntegerLiteral(3)
117 WalkUpFromStmt IntegerLiteral(3)
118)txt"));
119
120 EXPECT_TRUE(visitorCallbackLogEqual(
121 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
122 R"txt(
123WalkUpFromExpr IntegerLiteral(1)
124 WalkUpFromStmt IntegerLiteral(1)
125TraverseUnaryOperator UnaryOperator(-)
126 WalkUpFromExpr IntegerLiteral(2)
127 WalkUpFromStmt IntegerLiteral(2)
128 WalkUpFromUnaryOperator UnaryOperator(-)
129 WalkUpFromExpr UnaryOperator(-)
130 WalkUpFromStmt UnaryOperator(-)
131WalkUpFromExpr IntegerLiteral(3)
132 WalkUpFromStmt IntegerLiteral(3)
133WalkUpFromStmt CompoundStmt
134)txt"));
135}
136
137TEST(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(CallbackName: __func__, S,
145 CallDefaultFn: [&]() { 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(
164void test() {
165 1;
166 -2;
167 3;
168}
169)cpp";
170
171 EXPECT_TRUE(visitorCallbackLogEqual(
172 RecordingVisitor(ShouldTraversePostOrder::No), Code,
173 R"txt(
174WalkUpFromStmt CompoundStmt
175WalkUpFromExpr IntegerLiteral(1)
176 WalkUpFromStmt IntegerLiteral(1)
177WalkUpFromUnaryOperator UnaryOperator(-)
178 WalkUpFromExpr UnaryOperator(-)
179 WalkUpFromStmt UnaryOperator(-)
180WalkUpFromExpr IntegerLiteral(2)
181 WalkUpFromStmt IntegerLiteral(2)
182WalkUpFromExpr IntegerLiteral(3)
183 WalkUpFromStmt IntegerLiteral(3)
184)txt"));
185
186 EXPECT_TRUE(visitorCallbackLogEqual(
187 RecordingVisitor(ShouldTraversePostOrder::Yes), Code,
188 R"txt(
189WalkUpFromExpr IntegerLiteral(1)
190 WalkUpFromStmt IntegerLiteral(1)
191WalkUpFromExpr IntegerLiteral(2)
192 WalkUpFromStmt IntegerLiteral(2)
193WalkUpFromUnaryOperator UnaryOperator(-)
194 WalkUpFromExpr UnaryOperator(-)
195 WalkUpFromStmt UnaryOperator(-)
196WalkUpFromExpr IntegerLiteral(3)
197 WalkUpFromStmt IntegerLiteral(3)
198WalkUpFromStmt CompoundStmt
199)txt"));
200}
201

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