1 | //== unittests/ASTMatchers/ASTMatchersNodeTest.cpp - AST matcher unit tests ==// |
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 "ASTMatchersTest.h" |
10 | #include "clang/AST/PrettyPrinter.h" |
11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
12 | #include "clang/ASTMatchers/ASTMatchers.h" |
13 | #include "clang/Tooling/Tooling.h" |
14 | #include "llvm/TargetParser/Host.h" |
15 | #include "llvm/TargetParser/Triple.h" |
16 | #include "gtest/gtest.h" |
17 | |
18 | namespace clang { |
19 | namespace ast_matchers { |
20 | |
21 | TEST_P(ASTMatchersTest, Decl_CXX) { |
22 | if (!GetParam().isCXX()) { |
23 | // FIXME: Add a test for `decl()` that does not depend on C++. |
24 | return; |
25 | } |
26 | EXPECT_TRUE(notMatches("" , decl(usingDecl()))); |
27 | EXPECT_TRUE( |
28 | matches("namespace x { class X {}; } using x::X;" , decl(usingDecl()))); |
29 | } |
30 | |
31 | TEST_P(ASTMatchersTest, NameableDeclaration_MatchesVariousDecls) { |
32 | DeclarationMatcher NamedX = namedDecl(hasName(Name: "X" )); |
33 | EXPECT_TRUE(matches("typedef int X;" , NamedX)); |
34 | EXPECT_TRUE(matches("int X;" , NamedX)); |
35 | EXPECT_TRUE(matches("void foo() { int X; }" , NamedX)); |
36 | EXPECT_TRUE(matches("enum X { A, B, C };" , NamedX)); |
37 | |
38 | EXPECT_TRUE(notMatches("#define X 1" , NamedX)); |
39 | } |
40 | |
41 | TEST_P(ASTMatchersTest, NamedDecl_CXX) { |
42 | if (!GetParam().isCXX()) { |
43 | return; |
44 | } |
45 | DeclarationMatcher NamedX = namedDecl(hasName(Name: "X" )); |
46 | EXPECT_TRUE(matches("class foo { virtual void X(); };" , NamedX)); |
47 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }" , NamedX)); |
48 | EXPECT_TRUE(matches("namespace X { }" , NamedX)); |
49 | } |
50 | |
51 | TEST_P(ASTMatchersTest, MatchesNameRE) { |
52 | DeclarationMatcher NamedX = namedDecl(matchesName(RegExp: "::X" )); |
53 | EXPECT_TRUE(matches("typedef int Xa;" , NamedX)); |
54 | EXPECT_TRUE(matches("int Xb;" , NamedX)); |
55 | EXPECT_TRUE(matches("void foo() { int Xgh; }" , NamedX)); |
56 | EXPECT_TRUE(matches("enum X { A, B, C };" , NamedX)); |
57 | |
58 | EXPECT_TRUE(notMatches("#define Xkl 1" , NamedX)); |
59 | |
60 | DeclarationMatcher StartsWithNo = namedDecl(matchesName(RegExp: "::no" )); |
61 | EXPECT_TRUE(matches("int no_foo;" , StartsWithNo)); |
62 | |
63 | DeclarationMatcher Abc = namedDecl(matchesName(RegExp: "a.*b.*c" )); |
64 | EXPECT_TRUE(matches("int abc;" , Abc)); |
65 | EXPECT_TRUE(matches("int aFOObBARc;" , Abc)); |
66 | EXPECT_TRUE(notMatches("int cab;" , Abc)); |
67 | EXPECT_TRUE(matches("int cabc;" , Abc)); |
68 | |
69 | DeclarationMatcher StartsWithK = namedDecl(matchesName(RegExp: ":k[^:]*$" )); |
70 | EXPECT_TRUE(matches("int k;" , StartsWithK)); |
71 | EXPECT_TRUE(matches("int kAbc;" , StartsWithK)); |
72 | } |
73 | |
74 | TEST_P(ASTMatchersTest, MatchesNameRE_CXX) { |
75 | if (!GetParam().isCXX()) { |
76 | return; |
77 | } |
78 | DeclarationMatcher NamedX = namedDecl(matchesName(RegExp: "::X" )); |
79 | EXPECT_TRUE(matches("class foo { virtual void Xc(); };" , NamedX)); |
80 | EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }" , NamedX)); |
81 | EXPECT_TRUE(matches("namespace Xij { }" , NamedX)); |
82 | |
83 | DeclarationMatcher StartsWithNo = namedDecl(matchesName(RegExp: "::no" )); |
84 | EXPECT_TRUE(matches("class foo { virtual void nobody(); };" , StartsWithNo)); |
85 | |
86 | DeclarationMatcher StartsWithK = namedDecl(matchesName(RegExp: ":k[^:]*$" )); |
87 | EXPECT_TRUE(matches("namespace x { int kTest; }" , StartsWithK)); |
88 | EXPECT_TRUE(matches("class C { int k; };" , StartsWithK)); |
89 | EXPECT_TRUE(notMatches("class C { int ckc; };" , StartsWithK)); |
90 | EXPECT_TRUE(notMatches("int K;" , StartsWithK)); |
91 | |
92 | DeclarationMatcher StartsWithKIgnoreCase = |
93 | namedDecl(matchesName(RegExp: ":k[^:]*$" , RegexFlags: llvm::Regex::IgnoreCase)); |
94 | EXPECT_TRUE(matches("int k;" , StartsWithKIgnoreCase)); |
95 | EXPECT_TRUE(matches("int K;" , StartsWithKIgnoreCase)); |
96 | } |
97 | |
98 | TEST_P(ASTMatchersTest, DeclarationMatcher_MatchClass) { |
99 | if (!GetParam().isCXX()) { |
100 | return; |
101 | } |
102 | |
103 | DeclarationMatcher ClassX = recordDecl(recordDecl(hasName(Name: "X" ))); |
104 | EXPECT_TRUE(matches("class X;" , ClassX)); |
105 | EXPECT_TRUE(matches("class X {};" , ClassX)); |
106 | EXPECT_TRUE(matches("template<class T> class X {};" , ClassX)); |
107 | EXPECT_TRUE(notMatches("" , ClassX)); |
108 | } |
109 | |
110 | TEST_P(ASTMatchersTest, TranslationUnitDecl) { |
111 | if (!GetParam().isCXX()) { |
112 | // FIXME: Add a test for `translationUnitDecl()` that does not depend on |
113 | // C++. |
114 | return; |
115 | } |
116 | StringRef Code = "int MyVar1;\n" |
117 | "namespace NameSpace {\n" |
118 | "int MyVar2;\n" |
119 | "} // namespace NameSpace\n" ; |
120 | EXPECT_TRUE(matches( |
121 | Code, varDecl(hasName("MyVar1" ), hasDeclContext(translationUnitDecl())))); |
122 | EXPECT_FALSE(matches( |
123 | Code, varDecl(hasName("MyVar2" ), hasDeclContext(translationUnitDecl())))); |
124 | EXPECT_TRUE(matches( |
125 | Code, |
126 | varDecl(hasName("MyVar2" ), |
127 | hasDeclContext(decl(hasDeclContext(translationUnitDecl())))))); |
128 | } |
129 | |
130 | TEST_P(ASTMatchersTest, LinkageSpecDecl) { |
131 | if (!GetParam().isCXX()) { |
132 | return; |
133 | } |
134 | EXPECT_TRUE(matches("extern \"C\" { void foo() {}; }" , linkageSpecDecl())); |
135 | EXPECT_TRUE(notMatches("void foo() {};" , linkageSpecDecl())); |
136 | } |
137 | |
138 | TEST_P(ASTMatchersTest, ClassTemplateDecl_DoesNotMatchClass) { |
139 | if (!GetParam().isCXX()) { |
140 | return; |
141 | } |
142 | DeclarationMatcher ClassX = classTemplateDecl(hasName(Name: "X" )); |
143 | EXPECT_TRUE(notMatches("class X;" , ClassX)); |
144 | EXPECT_TRUE(notMatches("class X {};" , ClassX)); |
145 | } |
146 | |
147 | TEST_P(ASTMatchersTest, ClassTemplateDecl_MatchesClassTemplate) { |
148 | if (!GetParam().isCXX()) { |
149 | return; |
150 | } |
151 | DeclarationMatcher ClassX = classTemplateDecl(hasName(Name: "X" )); |
152 | EXPECT_TRUE(matches("template<typename T> class X {};" , ClassX)); |
153 | EXPECT_TRUE(matches("class Z { template<class T> class X {}; };" , ClassX)); |
154 | } |
155 | |
156 | TEST_P(ASTMatchersTest, |
157 | ClassTemplateDecl_DoesNotMatchClassTemplateExplicitSpecialization) { |
158 | if (!GetParam().isCXX()) { |
159 | return; |
160 | } |
161 | EXPECT_TRUE(notMatches( |
162 | "template<typename T> class X { };" |
163 | "template<> class X<int> { int a; };" , |
164 | classTemplateDecl(hasName("X" ), hasDescendant(fieldDecl(hasName("a" )))))); |
165 | } |
166 | |
167 | TEST_P(ASTMatchersTest, |
168 | ClassTemplateDecl_DoesNotMatchClassTemplatePartialSpecialization) { |
169 | if (!GetParam().isCXX()) { |
170 | return; |
171 | } |
172 | EXPECT_TRUE(notMatches( |
173 | "template<typename T, typename U> class X { };" |
174 | "template<typename T> class X<T, int> { int a; };" , |
175 | classTemplateDecl(hasName("X" ), hasDescendant(fieldDecl(hasName("a" )))))); |
176 | } |
177 | |
178 | TEST(ASTMatchersTestCUDA, CUDAKernelCallExpr) { |
179 | EXPECT_TRUE(matchesWithCuda("__global__ void f() { }" |
180 | "void g() { f<<<1, 2>>>(); }" , |
181 | cudaKernelCallExpr())); |
182 | EXPECT_TRUE(notMatchesWithCuda("void f() {}" , cudaKernelCallExpr())); |
183 | } |
184 | |
185 | TEST(ASTMatchersTestCUDA, HasAttrCUDA) { |
186 | EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}" , |
187 | hasAttr(clang::attr::CUDADevice))); |
188 | EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}" , |
189 | hasAttr(clang::attr::CUDAGlobal))); |
190 | } |
191 | |
192 | TEST_P(ASTMatchersTest, ExportDecl) { |
193 | if (!GetParam().isCXX20OrLater()) { |
194 | return; |
195 | } |
196 | const std::string = "module;export module ast_matcher_test;" ; |
197 | EXPECT_TRUE(matches(moduleHeader + "export void foo();" , |
198 | exportDecl(has(functionDecl())))); |
199 | EXPECT_TRUE(matches(moduleHeader + "export { void foo(); int v; }" , |
200 | exportDecl(has(functionDecl())))); |
201 | EXPECT_TRUE(matches(moduleHeader + "export { void foo(); int v; }" , |
202 | exportDecl(has(varDecl())))); |
203 | EXPECT_TRUE(matches(moduleHeader + "export namespace aa { void foo(); }" , |
204 | exportDecl(has(namespaceDecl())))); |
205 | } |
206 | |
207 | TEST_P(ASTMatchersTest, ValueDecl) { |
208 | if (!GetParam().isCXX()) { |
209 | // FIXME: Fix this test in non-C++ language modes. |
210 | return; |
211 | } |
212 | EXPECT_TRUE(matches("enum EnumType { EnumValue };" , |
213 | valueDecl(hasType(asString("enum EnumType" ))))); |
214 | EXPECT_TRUE(matches("void FunctionDecl();" , |
215 | valueDecl(hasType(asString("void (void)" ))))); |
216 | } |
217 | |
218 | TEST_P(ASTMatchersTest, FriendDecl) { |
219 | if (!GetParam().isCXX()) { |
220 | return; |
221 | } |
222 | EXPECT_TRUE(matches("class Y { friend class X; };" , |
223 | friendDecl(hasType(asString("class X" ))))); |
224 | EXPECT_TRUE(matches("class Y { friend class X; };" , |
225 | friendDecl(hasType(recordDecl(hasName("X" )))))); |
226 | |
227 | EXPECT_TRUE(matches("class Y { friend void f(); };" , |
228 | functionDecl(hasName("f" ), hasParent(friendDecl())))); |
229 | } |
230 | |
231 | TEST_P(ASTMatchersTest, EnumDecl_DoesNotMatchClasses) { |
232 | if (!GetParam().isCXX()) { |
233 | return; |
234 | } |
235 | EXPECT_TRUE(notMatches("class X {};" , enumDecl(hasName("X" )))); |
236 | } |
237 | |
238 | TEST_P(ASTMatchersTest, EnumDecl_MatchesEnums) { |
239 | if (!GetParam().isCXX()) { |
240 | // FIXME: Fix this test in non-C++ language modes. |
241 | return; |
242 | } |
243 | EXPECT_TRUE(matches("enum X {};" , enumDecl(hasName("X" )))); |
244 | } |
245 | |
246 | TEST_P(ASTMatchersTest, EnumConstantDecl) { |
247 | if (!GetParam().isCXX()) { |
248 | // FIXME: Fix this test in non-C++ language modes. |
249 | return; |
250 | } |
251 | DeclarationMatcher Matcher = enumConstantDecl(hasName(Name: "A" )); |
252 | EXPECT_TRUE(matches("enum X{ A };" , Matcher)); |
253 | EXPECT_TRUE(notMatches("enum X{ B };" , Matcher)); |
254 | EXPECT_TRUE(notMatches("enum X {};" , Matcher)); |
255 | } |
256 | |
257 | TEST_P(ASTMatchersTest, TagDecl) { |
258 | if (!GetParam().isCXX()) { |
259 | // FIXME: Fix this test in non-C++ language modes. |
260 | return; |
261 | } |
262 | EXPECT_TRUE(matches("struct X {};" , tagDecl(hasName("X" )))); |
263 | EXPECT_TRUE(matches("union U {};" , tagDecl(hasName("U" )))); |
264 | EXPECT_TRUE(matches("enum E {};" , tagDecl(hasName("E" )))); |
265 | } |
266 | |
267 | TEST_P(ASTMatchersTest, TagDecl_CXX) { |
268 | if (!GetParam().isCXX()) { |
269 | return; |
270 | } |
271 | EXPECT_TRUE(matches("class C {};" , tagDecl(hasName("C" )))); |
272 | } |
273 | |
274 | TEST_P(ASTMatchersTest, UnresolvedLookupExpr) { |
275 | if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
276 | // FIXME: Fix this test to work with delayed template parsing. |
277 | return; |
278 | } |
279 | |
280 | EXPECT_TRUE(matches("template<typename T>" |
281 | "T foo() { T a; return a; }" |
282 | "template<typename T>" |
283 | "void bar() {" |
284 | " foo<T>();" |
285 | "}" , |
286 | unresolvedLookupExpr())); |
287 | } |
288 | |
289 | TEST_P(ASTMatchersTest, UsesADL) { |
290 | if (!GetParam().isCXX()) { |
291 | return; |
292 | } |
293 | |
294 | StatementMatcher ADLMatch = callExpr(usesADL()); |
295 | StatementMatcher ADLMatchOper = cxxOperatorCallExpr(usesADL()); |
296 | StringRef NS_Str = R"cpp( |
297 | namespace NS { |
298 | struct X {}; |
299 | void f(X); |
300 | void operator+(X, X); |
301 | } |
302 | struct MyX {}; |
303 | void f(...); |
304 | void operator+(MyX, MyX); |
305 | )cpp" ; |
306 | |
307 | auto MkStr = [&](StringRef Body) { |
308 | return (NS_Str + "void test_fn() { " + Body + " }" ).str(); |
309 | }; |
310 | |
311 | EXPECT_TRUE(matches(MkStr("NS::X x; f(x);" ), ADLMatch)); |
312 | EXPECT_TRUE(notMatches(MkStr("NS::X x; NS::f(x);" ), ADLMatch)); |
313 | EXPECT_TRUE(notMatches(MkStr("MyX x; f(x);" ), ADLMatch)); |
314 | EXPECT_TRUE(notMatches(MkStr("NS::X x; using NS::f; f(x);" ), ADLMatch)); |
315 | |
316 | // Operator call expressions |
317 | EXPECT_TRUE(matches(MkStr("NS::X x; x + x;" ), ADLMatch)); |
318 | EXPECT_TRUE(matches(MkStr("NS::X x; x + x;" ), ADLMatchOper)); |
319 | EXPECT_TRUE(notMatches(MkStr("MyX x; x + x;" ), ADLMatch)); |
320 | EXPECT_TRUE(notMatches(MkStr("MyX x; x + x;" ), ADLMatchOper)); |
321 | EXPECT_TRUE(matches(MkStr("NS::X x; operator+(x, x);" ), ADLMatch)); |
322 | EXPECT_TRUE(notMatches(MkStr("NS::X x; NS::operator+(x, x);" ), ADLMatch)); |
323 | } |
324 | |
325 | TEST_P(ASTMatchersTest, CallExpr_CXX) { |
326 | if (!GetParam().isCXX()) { |
327 | // FIXME: Add a test for `callExpr()` that does not depend on C++. |
328 | return; |
329 | } |
330 | // FIXME: Do we want to overload Call() to directly take |
331 | // Matcher<Decl>, too? |
332 | StatementMatcher MethodX = |
333 | callExpr(hasDeclaration(InnerMatcher: cxxMethodDecl(hasName(Name: "x" )))); |
334 | |
335 | EXPECT_TRUE(matches("class Y { void x() { x(); } };" , MethodX)); |
336 | EXPECT_TRUE(notMatches("class Y { void x() {} };" , MethodX)); |
337 | |
338 | StatementMatcher MethodOnY = |
339 | cxxMemberCallExpr(on(InnerMatcher: hasType(InnerMatcher: recordDecl(hasName(Name: "Y" ))))); |
340 | |
341 | EXPECT_TRUE(matches("class Y { public: void x(); }; void z() { Y y; y.x(); }" , |
342 | MethodOnY)); |
343 | EXPECT_TRUE(matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }" , |
344 | MethodOnY)); |
345 | EXPECT_TRUE(notMatches( |
346 | "class Y { public: void x(); }; void z(Y *&y) { y->x(); }" , MethodOnY)); |
347 | EXPECT_TRUE(notMatches( |
348 | "class Y { public: void x(); }; void z(Y y[]) { y->x(); }" , MethodOnY)); |
349 | EXPECT_TRUE(notMatches( |
350 | "class Y { public: void x(); }; void z() { Y *y; y->x(); }" , MethodOnY)); |
351 | |
352 | StatementMatcher MethodOnYPointer = |
353 | cxxMemberCallExpr(on(InnerMatcher: hasType(InnerMatcher: pointsTo(InnerMatcher: recordDecl(hasName(Name: "Y" )))))); |
354 | |
355 | EXPECT_TRUE( |
356 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }" , |
357 | MethodOnYPointer)); |
358 | EXPECT_TRUE( |
359 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }" , |
360 | MethodOnYPointer)); |
361 | EXPECT_TRUE( |
362 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }" , |
363 | MethodOnYPointer)); |
364 | EXPECT_TRUE( |
365 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }" , |
366 | MethodOnYPointer)); |
367 | EXPECT_TRUE( |
368 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }" , |
369 | MethodOnYPointer)); |
370 | } |
371 | |
372 | TEST_P(ASTMatchersTest, LambdaExpr) { |
373 | if (!GetParam().isCXX11OrLater()) { |
374 | return; |
375 | } |
376 | EXPECT_TRUE(matches("auto f = [] (int i) { return i; };" , lambdaExpr())); |
377 | } |
378 | |
379 | TEST_P(ASTMatchersTest, CXXForRangeStmt) { |
380 | EXPECT_TRUE( |
381 | notMatches("void f() { for (int i; i<5; ++i); }" , cxxForRangeStmt())); |
382 | } |
383 | |
384 | TEST_P(ASTMatchersTest, CXXForRangeStmt_CXX11) { |
385 | if (!GetParam().isCXX11OrLater()) { |
386 | return; |
387 | } |
388 | EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };" |
389 | "void f() { for (auto &a : as); }" , |
390 | cxxForRangeStmt())); |
391 | } |
392 | |
393 | TEST_P(ASTMatchersTest, SubstNonTypeTemplateParmExpr) { |
394 | if (!GetParam().isCXX()) { |
395 | return; |
396 | } |
397 | EXPECT_FALSE(matches("template<int N>\n" |
398 | "struct A { static const int n = 0; };\n" |
399 | "struct B : public A<42> {};" , |
400 | traverse(TK_AsIs, substNonTypeTemplateParmExpr()))); |
401 | EXPECT_TRUE(matches("template<int N>\n" |
402 | "struct A { static const int n = N; };\n" |
403 | "struct B : public A<42> {};" , |
404 | traverse(TK_AsIs, substNonTypeTemplateParmExpr()))); |
405 | } |
406 | |
407 | TEST_P(ASTMatchersTest, NonTypeTemplateParmDecl) { |
408 | if (!GetParam().isCXX()) { |
409 | return; |
410 | } |
411 | EXPECT_TRUE(matches("template <int N> void f();" , |
412 | nonTypeTemplateParmDecl(hasName("N" )))); |
413 | EXPECT_TRUE( |
414 | notMatches("template <typename T> void f();" , nonTypeTemplateParmDecl())); |
415 | } |
416 | |
417 | TEST_P(ASTMatchersTest, TemplateTypeParmDecl) { |
418 | if (!GetParam().isCXX()) { |
419 | return; |
420 | } |
421 | EXPECT_TRUE(matches("template <typename T> void f();" , |
422 | templateTypeParmDecl(hasName("T" )))); |
423 | EXPECT_TRUE(notMatches("template <int N> void f();" , templateTypeParmDecl())); |
424 | } |
425 | |
426 | TEST_P(ASTMatchersTest, TemplateTemplateParmDecl) { |
427 | if (!GetParam().isCXX()) |
428 | return; |
429 | EXPECT_TRUE(matches("template <template <typename> class Z> void f();" , |
430 | templateTemplateParmDecl(hasName("Z" )))); |
431 | EXPECT_TRUE(notMatches("template <typename, int> void f();" , |
432 | templateTemplateParmDecl())); |
433 | } |
434 | |
435 | TEST_P(ASTMatchersTest, UserDefinedLiteral) { |
436 | if (!GetParam().isCXX11OrLater()) { |
437 | return; |
438 | } |
439 | EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {" |
440 | " return i + 1;" |
441 | "}" |
442 | "char c = 'a'_inc;" , |
443 | userDefinedLiteral())); |
444 | } |
445 | |
446 | TEST_P(ASTMatchersTest, FlowControl) { |
447 | EXPECT_TRUE(matches("void f() { while(1) { break; } }" , breakStmt())); |
448 | EXPECT_TRUE(matches("void f() { while(1) { continue; } }" , continueStmt())); |
449 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}" , gotoStmt())); |
450 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}" , |
451 | labelStmt(hasDeclaration(labelDecl(hasName("FOO" )))))); |
452 | EXPECT_TRUE(matches("void f() { FOO: ; void *ptr = &&FOO; goto *ptr; }" , |
453 | addrLabelExpr())); |
454 | EXPECT_TRUE(matches("void f() { return; }" , returnStmt())); |
455 | } |
456 | |
457 | TEST_P(ASTMatchersTest, CXXOperatorCallExpr) { |
458 | if (!GetParam().isCXX()) { |
459 | return; |
460 | } |
461 | |
462 | StatementMatcher OpCall = cxxOperatorCallExpr(); |
463 | // Unary operator |
464 | EXPECT_TRUE(matches("class Y { }; " |
465 | "bool operator!(Y x) { return false; }; " |
466 | "Y y; bool c = !y;" , |
467 | OpCall)); |
468 | // No match -- special operators like "new", "delete" |
469 | // FIXME: operator new takes size_t, for which we need stddef.h, for which |
470 | // we need to figure out include paths in the test. |
471 | // EXPECT_TRUE(NotMatches("#include <stddef.h>\n" |
472 | // "class Y { }; " |
473 | // "void *operator new(size_t size) { return 0; } " |
474 | // "Y *y = new Y;", OpCall)); |
475 | EXPECT_TRUE(notMatches("class Y { }; " |
476 | "void operator delete(void *p) { } " |
477 | "void a() {Y *y = new Y; delete y;}" , |
478 | OpCall)); |
479 | // Binary operator |
480 | EXPECT_TRUE(matches("class Y { }; " |
481 | "bool operator&&(Y x, Y y) { return true; }; " |
482 | "Y a; Y b; bool c = a && b;" , |
483 | OpCall)); |
484 | // No match -- normal operator, not an overloaded one. |
485 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;" , OpCall)); |
486 | EXPECT_TRUE(notMatches("int t = 5 << 2;" , OpCall)); |
487 | } |
488 | |
489 | TEST_P(ASTMatchersTest, FoldExpr) { |
490 | if (!GetParam().isCXX() || !GetParam().isCXX17OrLater()) { |
491 | return; |
492 | } |
493 | |
494 | EXPECT_TRUE(matches("template <typename... Args> auto sum(Args... args) { " |
495 | "return (0 + ... + args); }" , |
496 | cxxFoldExpr())); |
497 | EXPECT_TRUE(matches("template <typename... Args> auto sum(Args... args) { " |
498 | "return (args + ...); }" , |
499 | cxxFoldExpr())); |
500 | } |
501 | |
502 | TEST_P(ASTMatchersTest, ThisPointerType) { |
503 | if (!GetParam().isCXX()) { |
504 | return; |
505 | } |
506 | |
507 | StatementMatcher MethodOnY = traverse( |
508 | TK: TK_AsIs, InnerMatcher: cxxMemberCallExpr(thisPointerType(InnerMatcher: recordDecl(hasName(Name: "Y" ))))); |
509 | |
510 | EXPECT_TRUE(matches("class Y { public: void x(); }; void z() { Y y; y.x(); }" , |
511 | MethodOnY)); |
512 | EXPECT_TRUE(matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }" , |
513 | MethodOnY)); |
514 | EXPECT_TRUE(matches( |
515 | "class Y { public: void x(); }; void z(Y *&y) { y->x(); }" , MethodOnY)); |
516 | EXPECT_TRUE(matches( |
517 | "class Y { public: void x(); }; void z(Y y[]) { y->x(); }" , MethodOnY)); |
518 | EXPECT_TRUE(matches( |
519 | "class Y { public: void x(); }; void z() { Y *y; y->x(); }" , MethodOnY)); |
520 | |
521 | EXPECT_TRUE(matches("class Y {" |
522 | " public: virtual void x();" |
523 | "};" |
524 | "class X : public Y {" |
525 | " public: virtual void x();" |
526 | "};" |
527 | "void z() { X *x; x->Y::x(); }" , |
528 | MethodOnY)); |
529 | } |
530 | |
531 | TEST_P(ASTMatchersTest, DeclRefExpr) { |
532 | if (!GetParam().isCXX()) { |
533 | // FIXME: Add a test for `declRefExpr()` that does not depend on C++. |
534 | return; |
535 | } |
536 | StatementMatcher Reference = declRefExpr(to(InnerMatcher: varDecl(hasInitializer( |
537 | InnerMatcher: cxxMemberCallExpr(thisPointerType(InnerMatcher: recordDecl(hasName(Name: "Y" )))))))); |
538 | |
539 | EXPECT_TRUE(matches("class Y {" |
540 | " public:" |
541 | " bool x() const;" |
542 | "};" |
543 | "void z(const Y &y) {" |
544 | " bool b = y.x();" |
545 | " if (b) {}" |
546 | "}" , |
547 | Reference)); |
548 | |
549 | EXPECT_TRUE(notMatches("class Y {" |
550 | " public:" |
551 | " bool x() const;" |
552 | "};" |
553 | "void z(const Y &y) {" |
554 | " bool b = y.x();" |
555 | "}" , |
556 | Reference)); |
557 | } |
558 | |
559 | TEST_P(ASTMatchersTest, DependentScopeDeclRefExpr) { |
560 | if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
561 | // FIXME: Fix this test to work with delayed template parsing. |
562 | return; |
563 | } |
564 | |
565 | EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };" , |
566 | dependentScopeDeclRefExpr())); |
567 | |
568 | EXPECT_TRUE( |
569 | matches("template <typename T> struct S { static T Foo; };" |
570 | "template <typename T> void declToImport() { (void)S<T>::Foo; }" , |
571 | dependentScopeDeclRefExpr())); |
572 | } |
573 | |
574 | TEST_P(ASTMatchersTest, CXXMemberCallExpr) { |
575 | if (!GetParam().isCXX()) { |
576 | return; |
577 | } |
578 | StatementMatcher CallOnVariableY = |
579 | cxxMemberCallExpr(on(InnerMatcher: declRefExpr(to(InnerMatcher: varDecl(hasName(Name: "y" )))))); |
580 | |
581 | EXPECT_TRUE(matches("class Y { public: void x() { Y y; y.x(); } };" , |
582 | CallOnVariableY)); |
583 | EXPECT_TRUE(matches("class Y { public: void x() const { Y y; y.x(); } };" , |
584 | CallOnVariableY)); |
585 | EXPECT_TRUE(matches("class Y { public: void x(); };" |
586 | "class X : public Y { void z() { X y; y.x(); } };" , |
587 | CallOnVariableY)); |
588 | EXPECT_TRUE(matches("class Y { public: void x(); };" |
589 | "class X : public Y { void z() { X *y; y->x(); } };" , |
590 | CallOnVariableY)); |
591 | EXPECT_TRUE(notMatches( |
592 | "class Y { public: void x(); };" |
593 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };" , |
594 | CallOnVariableY)); |
595 | } |
596 | |
597 | TEST_P(ASTMatchersTest, UnaryExprOrTypeTraitExpr) { |
598 | EXPECT_TRUE( |
599 | matches("void x() { int a = sizeof(a); }" , unaryExprOrTypeTraitExpr())); |
600 | } |
601 | |
602 | TEST_P(ASTMatchersTest, AlignOfExpr) { |
603 | EXPECT_TRUE( |
604 | notMatches("void x() { int a = sizeof(a); }" , alignOfExpr(anything()))); |
605 | // FIXME: Uncomment once alignof is enabled. |
606 | // EXPECT_TRUE(matches("void x() { int a = alignof(a); }", |
607 | // unaryExprOrTypeTraitExpr())); |
608 | // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }", |
609 | // sizeOfExpr())); |
610 | } |
611 | |
612 | TEST_P(ASTMatchersTest, MemberExpr_DoesNotMatchClasses) { |
613 | if (!GetParam().isCXX()) { |
614 | return; |
615 | } |
616 | EXPECT_TRUE(notMatches("class Y { void x() {} };" , memberExpr())); |
617 | EXPECT_TRUE(notMatches("class Y { void x() {} };" , unresolvedMemberExpr())); |
618 | EXPECT_TRUE( |
619 | notMatches("class Y { void x() {} };" , cxxDependentScopeMemberExpr())); |
620 | } |
621 | |
622 | TEST_P(ASTMatchersTest, MemberExpr_MatchesMemberFunctionCall) { |
623 | if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
624 | // FIXME: Fix this test to work with delayed template parsing. |
625 | return; |
626 | } |
627 | EXPECT_TRUE(matches("class Y { void x() { x(); } };" , memberExpr())); |
628 | EXPECT_TRUE(matches("class Y { template <class T> void x() { x<T>(); } };" , |
629 | unresolvedMemberExpr())); |
630 | EXPECT_TRUE(matches("template <class T> void x() { T t; t.f(); }" , |
631 | cxxDependentScopeMemberExpr())); |
632 | } |
633 | |
634 | TEST_P(ASTMatchersTest, MemberExpr_MatchesVariable) { |
635 | if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
636 | // FIXME: Fix this test to work with delayed template parsing. |
637 | return; |
638 | } |
639 | EXPECT_TRUE( |
640 | matches("class Y { void x() { this->y; } int y; };" , memberExpr())); |
641 | EXPECT_TRUE(matches("class Y { void x() { y; } int y; };" , memberExpr())); |
642 | EXPECT_TRUE( |
643 | matches("class Y { void x() { Y y; y.y; } int y; };" , memberExpr())); |
644 | EXPECT_TRUE(matches("template <class T>" |
645 | "class X : T { void f() { this->T::v; } };" , |
646 | cxxDependentScopeMemberExpr())); |
647 | EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };" , |
648 | dependentScopeDeclRefExpr())); |
649 | EXPECT_TRUE(matches("template <class T> void x() { T t; t.v; }" , |
650 | cxxDependentScopeMemberExpr())); |
651 | } |
652 | |
653 | TEST_P(ASTMatchersTest, MemberExpr_MatchesStaticVariable) { |
654 | if (!GetParam().isCXX()) { |
655 | return; |
656 | } |
657 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };" , |
658 | memberExpr())); |
659 | EXPECT_TRUE( |
660 | notMatches("class Y { void x() { y; } static int y; };" , memberExpr())); |
661 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };" , |
662 | memberExpr())); |
663 | } |
664 | |
665 | TEST_P(ASTMatchersTest, FunctionDecl) { |
666 | StatementMatcher CallFunctionF = callExpr(callee(InnerMatcher: functionDecl(hasName(Name: "f" )))); |
667 | |
668 | EXPECT_TRUE(matches("void f() { f(); }" , CallFunctionF)); |
669 | EXPECT_TRUE(notMatches("void f() { }" , CallFunctionF)); |
670 | |
671 | EXPECT_TRUE(notMatches("void f(int);" , functionDecl(isVariadic()))); |
672 | EXPECT_TRUE(notMatches("void f();" , functionDecl(isVariadic()))); |
673 | EXPECT_TRUE(matches("void f(int, ...);" , functionDecl(parameterCountIs(1)))); |
674 | } |
675 | |
676 | TEST_P(ASTMatchersTest, FunctionDecl_C) { |
677 | if (!GetParam().isC()) { |
678 | return; |
679 | } |
680 | EXPECT_TRUE(notMatches("void f();" , functionDecl(isVariadic()))); |
681 | EXPECT_TRUE(matches("void f();" , functionDecl(parameterCountIs(0)))); |
682 | } |
683 | |
684 | TEST_P(ASTMatchersTest, FunctionDecl_CXX) { |
685 | if (!GetParam().isCXX()) { |
686 | return; |
687 | } |
688 | |
689 | StatementMatcher CallFunctionF = callExpr(callee(InnerMatcher: functionDecl(hasName(Name: "f" )))); |
690 | |
691 | if (!GetParam().hasDelayedTemplateParsing()) { |
692 | // FIXME: Fix this test to work with delayed template parsing. |
693 | // Dependent contexts, but a non-dependent call. |
694 | EXPECT_TRUE( |
695 | matches("void f(); template <int N> void g() { f(); }" , CallFunctionF)); |
696 | EXPECT_TRUE( |
697 | matches("void f(); template <int N> struct S { void g() { f(); } };" , |
698 | CallFunctionF)); |
699 | } |
700 | |
701 | // Dependent calls don't match. |
702 | EXPECT_TRUE( |
703 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }" , |
704 | CallFunctionF)); |
705 | EXPECT_TRUE( |
706 | notMatches("void f(int);" |
707 | "template <typename T> struct S { void g(T t) { f(t); } };" , |
708 | CallFunctionF)); |
709 | |
710 | EXPECT_TRUE(matches("void f(...);" , functionDecl(isVariadic()))); |
711 | EXPECT_TRUE(matches("void f(...);" , functionDecl(parameterCountIs(0)))); |
712 | } |
713 | |
714 | TEST_P(ASTMatchersTest, FunctionDecl_CXX11) { |
715 | if (!GetParam().isCXX11OrLater()) { |
716 | return; |
717 | } |
718 | |
719 | EXPECT_TRUE(notMatches("template <typename... Ts> void f(Ts...);" , |
720 | functionDecl(isVariadic()))); |
721 | } |
722 | |
723 | TEST_P(ASTMatchersTest, |
724 | FunctionTemplateDecl_MatchesFunctionTemplateDeclarations) { |
725 | if (!GetParam().isCXX()) { |
726 | return; |
727 | } |
728 | EXPECT_TRUE(matches("template <typename T> void f(T t) {}" , |
729 | functionTemplateDecl(hasName("f" )))); |
730 | } |
731 | |
732 | TEST_P(ASTMatchersTest, FunctionTemplate_DoesNotMatchFunctionDeclarations) { |
733 | EXPECT_TRUE( |
734 | notMatches("void f(double d);" , functionTemplateDecl(hasName("f" )))); |
735 | EXPECT_TRUE( |
736 | notMatches("void f(int t) {}" , functionTemplateDecl(hasName("f" )))); |
737 | } |
738 | |
739 | TEST_P(ASTMatchersTest, |
740 | FunctionTemplateDecl_DoesNotMatchFunctionTemplateSpecializations) { |
741 | if (!GetParam().isCXX()) { |
742 | return; |
743 | } |
744 | EXPECT_TRUE(notMatches( |
745 | "void g(); template <typename T> void f(T t) {}" |
746 | "template <> void f(int t) { g(); }" , |
747 | functionTemplateDecl(hasName("f" ), hasDescendant(declRefExpr(to( |
748 | functionDecl(hasName("g" )))))))); |
749 | } |
750 | |
751 | TEST_P(ASTMatchersTest, ClassTemplateSpecializationDecl) { |
752 | if (!GetParam().isCXX()) { |
753 | return; |
754 | } |
755 | EXPECT_TRUE(matches("template<typename T> struct A {};" |
756 | "template<> struct A<int> {};" , |
757 | classTemplateSpecializationDecl())); |
758 | EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;" , |
759 | classTemplateSpecializationDecl())); |
760 | EXPECT_TRUE(notMatches("template<typename T> struct A {};" , |
761 | classTemplateSpecializationDecl())); |
762 | } |
763 | |
764 | TEST_P(ASTMatchersTest, DeclaratorDecl) { |
765 | EXPECT_TRUE(matches("int x;" , declaratorDecl())); |
766 | EXPECT_TRUE(notMatches("struct A {};" , declaratorDecl())); |
767 | } |
768 | |
769 | TEST_P(ASTMatchersTest, DeclaratorDecl_CXX) { |
770 | if (!GetParam().isCXX()) { |
771 | return; |
772 | } |
773 | EXPECT_TRUE(notMatches("class A {};" , declaratorDecl())); |
774 | } |
775 | |
776 | TEST_P(ASTMatchersTest, ParmVarDecl) { |
777 | EXPECT_TRUE(matches("void f(int x);" , parmVarDecl())); |
778 | EXPECT_TRUE(notMatches("void f();" , parmVarDecl())); |
779 | } |
780 | |
781 | TEST_P(ASTMatchersTest, StaticAssertDecl) { |
782 | if (!GetParam().isCXX11OrLater()) |
783 | return; |
784 | |
785 | EXPECT_TRUE(matches("static_assert(true, \"\");" , staticAssertDecl())); |
786 | EXPECT_TRUE( |
787 | notMatches("constexpr bool staticassert(bool B, const char *M) " |
788 | "{ return true; };\n void f() { staticassert(true, \"\"); }" , |
789 | staticAssertDecl())); |
790 | } |
791 | |
792 | TEST_P(ASTMatchersTest, Matcher_ConstructorCall) { |
793 | if (!GetParam().isCXX()) { |
794 | return; |
795 | } |
796 | |
797 | StatementMatcher Constructor = traverse(TK: TK_AsIs, InnerMatcher: cxxConstructExpr()); |
798 | |
799 | EXPECT_TRUE( |
800 | matches("class X { public: X(); }; void x() { X x; }" , Constructor)); |
801 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { X x = X(); }" , |
802 | Constructor)); |
803 | EXPECT_TRUE(matches("class X { public: X(int); }; void x() { X x = 0; }" , |
804 | Constructor)); |
805 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }" , Constructor)); |
806 | } |
807 | |
808 | TEST_P(ASTMatchersTest, Match_ConstructorInitializers) { |
809 | if (!GetParam().isCXX()) { |
810 | return; |
811 | } |
812 | EXPECT_TRUE(matches("class C { int i; public: C(int ii) : i(ii) {} };" , |
813 | cxxCtorInitializer(forField(hasName("i" ))))); |
814 | } |
815 | |
816 | TEST_P(ASTMatchersTest, Matcher_ThisExpr) { |
817 | if (!GetParam().isCXX()) { |
818 | return; |
819 | } |
820 | EXPECT_TRUE( |
821 | matches("struct X { int a; int f () { return a; } };" , cxxThisExpr())); |
822 | EXPECT_TRUE( |
823 | notMatches("struct X { int f () { int a; return a; } };" , cxxThisExpr())); |
824 | } |
825 | |
826 | TEST_P(ASTMatchersTest, Matcher_BindTemporaryExpression) { |
827 | if (!GetParam().isCXX()) { |
828 | return; |
829 | } |
830 | |
831 | StatementMatcher TempExpression = traverse(TK: TK_AsIs, InnerMatcher: cxxBindTemporaryExpr()); |
832 | |
833 | StringRef ClassString = "class string { public: string(); ~string(); }; " ; |
834 | |
835 | EXPECT_TRUE(matches( |
836 | ClassString + "string GetStringByValue();" |
837 | "void FunctionTakesString(string s);" |
838 | "void run() { FunctionTakesString(GetStringByValue()); }" , |
839 | TempExpression)); |
840 | |
841 | EXPECT_TRUE(notMatches(ClassString + |
842 | "string* GetStringPointer(); " |
843 | "void FunctionTakesStringPtr(string* s);" |
844 | "void run() {" |
845 | " string* s = GetStringPointer();" |
846 | " FunctionTakesStringPtr(GetStringPointer());" |
847 | " FunctionTakesStringPtr(s);" |
848 | "}" , |
849 | TempExpression)); |
850 | |
851 | EXPECT_TRUE(notMatches("class no_dtor {};" |
852 | "no_dtor GetObjByValue();" |
853 | "void ConsumeObj(no_dtor param);" |
854 | "void run() { ConsumeObj(GetObjByValue()); }" , |
855 | TempExpression)); |
856 | } |
857 | |
858 | TEST_P(ASTMatchersTest, MaterializeTemporaryExpr_MatchesTemporaryCXX11CXX14) { |
859 | if (GetParam().Language != Lang_CXX11 && GetParam().Language != Lang_CXX14) { |
860 | return; |
861 | } |
862 | |
863 | StatementMatcher TempExpression = |
864 | traverse(TK: TK_AsIs, InnerMatcher: materializeTemporaryExpr()); |
865 | |
866 | EXPECT_TRUE(matches("class string { public: string(); }; " |
867 | "string GetStringByValue();" |
868 | "void FunctionTakesString(string s);" |
869 | "void run() { FunctionTakesString(GetStringByValue()); }" , |
870 | TempExpression)); |
871 | } |
872 | |
873 | TEST_P(ASTMatchersTest, MaterializeTemporaryExpr_MatchesTemporary) { |
874 | if (!GetParam().isCXX()) { |
875 | return; |
876 | } |
877 | |
878 | StringRef ClassString = "class string { public: string(); int length(); }; " ; |
879 | StatementMatcher TempExpression = |
880 | traverse(TK: TK_AsIs, InnerMatcher: materializeTemporaryExpr()); |
881 | |
882 | EXPECT_TRUE(notMatches(ClassString + |
883 | "string* GetStringPointer(); " |
884 | "void FunctionTakesStringPtr(string* s);" |
885 | "void run() {" |
886 | " string* s = GetStringPointer();" |
887 | " FunctionTakesStringPtr(GetStringPointer());" |
888 | " FunctionTakesStringPtr(s);" |
889 | "}" , |
890 | TempExpression)); |
891 | |
892 | EXPECT_TRUE(matches(ClassString + |
893 | "string GetStringByValue();" |
894 | "void run() { int k = GetStringByValue().length(); }" , |
895 | TempExpression)); |
896 | |
897 | EXPECT_TRUE(notMatches(ClassString + "string GetStringByValue();" |
898 | "void run() { GetStringByValue(); }" , |
899 | TempExpression)); |
900 | } |
901 | |
902 | TEST_P(ASTMatchersTest, Matcher_NewExpression) { |
903 | if (!GetParam().isCXX()) { |
904 | return; |
905 | } |
906 | |
907 | StatementMatcher New = cxxNewExpr(); |
908 | |
909 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }" , New)); |
910 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X(); }" , New)); |
911 | EXPECT_TRUE( |
912 | matches("class X { public: X(int); }; void x() { new X(0); }" , New)); |
913 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }" , New)); |
914 | } |
915 | |
916 | TEST_P(ASTMatchersTest, Matcher_DeleteExpression) { |
917 | if (!GetParam().isCXX()) { |
918 | return; |
919 | } |
920 | EXPECT_TRUE( |
921 | matches("struct A {}; void f(A* a) { delete a; }" , cxxDeleteExpr())); |
922 | } |
923 | |
924 | TEST_P(ASTMatchersTest, Matcher_NoexceptExpression) { |
925 | if (!GetParam().isCXX11OrLater()) { |
926 | return; |
927 | } |
928 | StatementMatcher NoExcept = cxxNoexceptExpr(); |
929 | EXPECT_TRUE(matches("void foo(); bool bar = noexcept(foo());" , NoExcept)); |
930 | EXPECT_TRUE( |
931 | matches("void foo() noexcept; bool bar = noexcept(foo());" , NoExcept)); |
932 | EXPECT_TRUE(notMatches("void foo() noexcept;" , NoExcept)); |
933 | EXPECT_TRUE(notMatches("void foo() noexcept(0+1);" , NoExcept)); |
934 | EXPECT_TRUE(matches("void foo() noexcept(noexcept(1+1));" , NoExcept)); |
935 | } |
936 | |
937 | TEST_P(ASTMatchersTest, Matcher_DefaultArgument) { |
938 | if (!GetParam().isCXX()) { |
939 | return; |
940 | } |
941 | StatementMatcher Arg = cxxDefaultArgExpr(); |
942 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }" , Arg)); |
943 | EXPECT_TRUE( |
944 | matches("class X { void x(int, int = 0) { int y; x(y); } };" , Arg)); |
945 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }" , Arg)); |
946 | } |
947 | |
948 | TEST_P(ASTMatchersTest, StringLiteral) { |
949 | StatementMatcher Literal = stringLiteral(); |
950 | EXPECT_TRUE(matches("const char *s = \"string\";" , Literal)); |
951 | // with escaped characters |
952 | EXPECT_TRUE(matches("const char *s = \"\x05five\";" , Literal)); |
953 | // no matching -- though the data type is the same, there is no string literal |
954 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};" , Literal)); |
955 | } |
956 | |
957 | TEST_P(ASTMatchersTest, StringLiteral_CXX) { |
958 | if (!GetParam().isCXX()) { |
959 | return; |
960 | } |
961 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";" , stringLiteral())); |
962 | } |
963 | |
964 | TEST_P(ASTMatchersTest, CharacterLiteral) { |
965 | EXPECT_TRUE(matches("const char c = 'c';" , characterLiteral())); |
966 | EXPECT_TRUE(notMatches("const char c = 0x1;" , characterLiteral())); |
967 | } |
968 | |
969 | TEST_P(ASTMatchersTest, CharacterLiteral_CXX) { |
970 | if (!GetParam().isCXX()) { |
971 | return; |
972 | } |
973 | // wide character |
974 | EXPECT_TRUE(matches("const char c = L'c';" , characterLiteral())); |
975 | // wide character, Hex encoded, NOT MATCHED! |
976 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;" , characterLiteral())); |
977 | } |
978 | |
979 | TEST_P(ASTMatchersTest, IntegerLiteral) { |
980 | StatementMatcher HasIntLiteral = integerLiteral(); |
981 | EXPECT_TRUE(matches("int i = 10;" , HasIntLiteral)); |
982 | EXPECT_TRUE(matches("int i = 0x1AB;" , HasIntLiteral)); |
983 | EXPECT_TRUE(matches("int i = 10L;" , HasIntLiteral)); |
984 | EXPECT_TRUE(matches("int i = 10U;" , HasIntLiteral)); |
985 | |
986 | // Non-matching cases (character literals, float and double) |
987 | EXPECT_TRUE(notMatches("int i = L'a';" , |
988 | HasIntLiteral)); // this is actually a character |
989 | // literal cast to int |
990 | EXPECT_TRUE(notMatches("int i = 'a';" , HasIntLiteral)); |
991 | EXPECT_TRUE(notMatches("int i = 1e10;" , HasIntLiteral)); |
992 | EXPECT_TRUE(notMatches("int i = 10.0;" , HasIntLiteral)); |
993 | |
994 | // Negative integers. |
995 | EXPECT_TRUE( |
996 | matches("int i = -10;" , |
997 | unaryOperator(hasOperatorName("-" ), |
998 | hasUnaryOperand(integerLiteral(equals(10)))))); |
999 | } |
1000 | |
1001 | TEST_P(ASTMatchersTest, FloatLiteral) { |
1002 | StatementMatcher HasFloatLiteral = floatLiteral(); |
1003 | EXPECT_TRUE(matches("float i = 10.0;" , HasFloatLiteral)); |
1004 | EXPECT_TRUE(matches("float i = 10.0f;" , HasFloatLiteral)); |
1005 | EXPECT_TRUE(matches("double i = 10.0;" , HasFloatLiteral)); |
1006 | EXPECT_TRUE(matches("double i = 10.0L;" , HasFloatLiteral)); |
1007 | EXPECT_TRUE(matches("double i = 1e10;" , HasFloatLiteral)); |
1008 | EXPECT_TRUE(matches("double i = 5.0;" , floatLiteral(equals(5.0)))); |
1009 | EXPECT_TRUE(matches("double i = 5.0;" , floatLiteral(equals(5.0f)))); |
1010 | EXPECT_TRUE( |
1011 | matches("double i = 5.0;" , floatLiteral(equals(llvm::APFloat(5.0))))); |
1012 | |
1013 | EXPECT_TRUE(notMatches("float i = 10;" , HasFloatLiteral)); |
1014 | EXPECT_TRUE(notMatches("double i = 5.0;" , floatLiteral(equals(6.0)))); |
1015 | EXPECT_TRUE(notMatches("double i = 5.0;" , floatLiteral(equals(6.0f)))); |
1016 | EXPECT_TRUE( |
1017 | notMatches("double i = 5.0;" , floatLiteral(equals(llvm::APFloat(6.0))))); |
1018 | } |
1019 | |
1020 | TEST_P(ASTMatchersTest, FixedPointLiterals) { |
1021 | StatementMatcher HasFixedPointLiteral = fixedPointLiteral(); |
1022 | EXPECT_TRUE(matchesWithFixedpoint("_Fract i = 0.25r;" , HasFixedPointLiteral)); |
1023 | EXPECT_TRUE( |
1024 | matchesWithFixedpoint("_Fract i = 0.25hr;" , HasFixedPointLiteral)); |
1025 | EXPECT_TRUE( |
1026 | matchesWithFixedpoint("_Fract i = 0.25uhr;" , HasFixedPointLiteral)); |
1027 | EXPECT_TRUE( |
1028 | matchesWithFixedpoint("_Fract i = 0.25ur;" , HasFixedPointLiteral)); |
1029 | EXPECT_TRUE( |
1030 | matchesWithFixedpoint("_Fract i = 0.25lr;" , HasFixedPointLiteral)); |
1031 | EXPECT_TRUE( |
1032 | matchesWithFixedpoint("_Fract i = 0.25ulr;" , HasFixedPointLiteral)); |
1033 | EXPECT_TRUE(matchesWithFixedpoint("_Accum i = 1.25k;" , HasFixedPointLiteral)); |
1034 | EXPECT_TRUE( |
1035 | matchesWithFixedpoint("_Accum i = 1.25hk;" , HasFixedPointLiteral)); |
1036 | EXPECT_TRUE( |
1037 | matchesWithFixedpoint("_Accum i = 1.25uhk;" , HasFixedPointLiteral)); |
1038 | EXPECT_TRUE( |
1039 | matchesWithFixedpoint("_Accum i = 1.25uk;" , HasFixedPointLiteral)); |
1040 | EXPECT_TRUE( |
1041 | matchesWithFixedpoint("_Accum i = 1.25lk;" , HasFixedPointLiteral)); |
1042 | EXPECT_TRUE( |
1043 | matchesWithFixedpoint("_Accum i = 1.25ulk;" , HasFixedPointLiteral)); |
1044 | EXPECT_TRUE(matchesWithFixedpoint("_Accum decexp1 = 1.575e1k;" , |
1045 | HasFixedPointLiteral)); |
1046 | EXPECT_TRUE( |
1047 | matchesWithFixedpoint("_Accum hex = 0x1.25fp2k;" , HasFixedPointLiteral)); |
1048 | EXPECT_TRUE(matchesWithFixedpoint("_Sat long _Fract i = 0.25r;" , |
1049 | HasFixedPointLiteral)); |
1050 | EXPECT_TRUE(matchesWithFixedpoint("_Sat short _Accum i = 256.0k;" , |
1051 | HasFixedPointLiteral)); |
1052 | EXPECT_TRUE(matchesWithFixedpoint( |
1053 | "_Accum i = 256.0k;" , |
1054 | fixedPointLiteral(equals(llvm::APInt(32, 0x800000, true))))); |
1055 | EXPECT_TRUE(matchesWithFixedpoint( |
1056 | "_Fract i = 0.25ulr;" , |
1057 | fixedPointLiteral(equals(llvm::APInt(32, 0x40000000, false))))); |
1058 | EXPECT_TRUE(matchesWithFixedpoint( |
1059 | "_Fract i = 0.5hr;" , |
1060 | fixedPointLiteral(equals(llvm::APInt(8, 0x40, true))))); |
1061 | |
1062 | EXPECT_TRUE( |
1063 | notMatchesWithFixedpoint("short _Accum i = 2u;" , HasFixedPointLiteral)); |
1064 | EXPECT_TRUE( |
1065 | notMatchesWithFixedpoint("short _Accum i = 2;" , HasFixedPointLiteral)); |
1066 | EXPECT_TRUE( |
1067 | notMatchesWithFixedpoint("_Accum i = 1.25;" , HasFixedPointLiteral)); |
1068 | EXPECT_TRUE(notMatchesWithFixedpoint("_Accum i = (double)(1.25 * 4.5i);" , |
1069 | HasFixedPointLiteral)); |
1070 | EXPECT_TRUE(notMatchesWithFixedpoint( |
1071 | "_Accum i = 256.0k;" , |
1072 | fixedPointLiteral(equals(llvm::APInt(32, 0x800001, true))))); |
1073 | EXPECT_TRUE(notMatchesWithFixedpoint( |
1074 | "_Fract i = 0.25ulr;" , |
1075 | fixedPointLiteral(equals(llvm::APInt(32, 0x40000001, false))))); |
1076 | EXPECT_TRUE(notMatchesWithFixedpoint( |
1077 | "_Fract i = 0.5hr;" , |
1078 | fixedPointLiteral(equals(llvm::APInt(8, 0x41, true))))); |
1079 | } |
1080 | |
1081 | TEST_P(ASTMatchersTest, CXXNullPtrLiteralExpr) { |
1082 | if (!GetParam().isCXX11OrLater()) { |
1083 | return; |
1084 | } |
1085 | EXPECT_TRUE(matches("int* i = nullptr;" , cxxNullPtrLiteralExpr())); |
1086 | } |
1087 | |
1088 | TEST_P(ASTMatchersTest, ChooseExpr) { |
1089 | EXPECT_TRUE(matches("void f() { (void)__builtin_choose_expr(1, 2, 3); }" , |
1090 | chooseExpr())); |
1091 | } |
1092 | |
1093 | TEST_P(ASTMatchersTest, ConvertVectorExpr) { |
1094 | EXPECT_TRUE(matches( |
1095 | "typedef double vector4double __attribute__((__vector_size__(32)));" |
1096 | "typedef float vector4float __attribute__((__vector_size__(16)));" |
1097 | "vector4float vf;" |
1098 | "void f() { (void)__builtin_convertvector(vf, vector4double); }" , |
1099 | convertVectorExpr())); |
1100 | EXPECT_TRUE(notMatches("void f() { (void)__builtin_choose_expr(1, 2, 3); }" , |
1101 | convertVectorExpr())); |
1102 | } |
1103 | |
1104 | TEST_P(ASTMatchersTest, GNUNullExpr) { |
1105 | if (!GetParam().isCXX()) { |
1106 | return; |
1107 | } |
1108 | EXPECT_TRUE(matches("int* i = __null;" , gnuNullExpr())); |
1109 | } |
1110 | |
1111 | TEST_P(ASTMatchersTest, GenericSelectionExpr) { |
1112 | EXPECT_TRUE(matches("void f() { (void)_Generic(1, int: 1, float: 2.0); }" , |
1113 | genericSelectionExpr())); |
1114 | } |
1115 | |
1116 | TEST_P(ASTMatchersTest, AtomicExpr) { |
1117 | EXPECT_TRUE(matches("void foo() { int *ptr; __atomic_load_n(ptr, 1); }" , |
1118 | atomicExpr())); |
1119 | } |
1120 | |
1121 | TEST_P(ASTMatchersTest, Initializers_C99) { |
1122 | if (!GetParam().isC99OrLater()) { |
1123 | return; |
1124 | } |
1125 | EXPECT_TRUE(matches( |
1126 | "void foo() { struct point { double x; double y; };" |
1127 | " struct point ptarray[10] = " |
1128 | " { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }" , |
1129 | initListExpr(hasSyntacticForm(initListExpr( |
1130 | has(designatedInitExpr(designatorCountIs(2), |
1131 | hasDescendant(floatLiteral(equals(1.0))), |
1132 | hasDescendant(integerLiteral(equals(2))))), |
1133 | has(designatedInitExpr(designatorCountIs(2), |
1134 | hasDescendant(floatLiteral(equals(2.0))), |
1135 | hasDescendant(integerLiteral(equals(2))))), |
1136 | has(designatedInitExpr( |
1137 | designatorCountIs(2), hasDescendant(floatLiteral(equals(1.0))), |
1138 | hasDescendant(integerLiteral(equals(0)))))))))); |
1139 | } |
1140 | |
1141 | TEST_P(ASTMatchersTest, Initializers_CXX) { |
1142 | if (GetParam().Language != Lang_CXX03) { |
1143 | // FIXME: Make this test pass with other C++ standard versions. |
1144 | return; |
1145 | } |
1146 | EXPECT_TRUE(matches( |
1147 | "void foo() { struct point { double x; double y; };" |
1148 | " struct point ptarray[10] = " |
1149 | " { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }" , |
1150 | initListExpr( |
1151 | has(cxxConstructExpr(requiresZeroInitialization())), |
1152 | has(initListExpr( |
1153 | hasType(asString("struct point" )), has(floatLiteral(equals(1.0))), |
1154 | has(implicitValueInitExpr(hasType(asString("double" )))))), |
1155 | has(initListExpr(hasType(asString("struct point" )), |
1156 | has(floatLiteral(equals(2.0))), |
1157 | has(floatLiteral(equals(1.0)))))))); |
1158 | } |
1159 | |
1160 | TEST_P(ASTMatchersTest, ParenListExpr) { |
1161 | if (!GetParam().isCXX()) { |
1162 | return; |
1163 | } |
1164 | EXPECT_TRUE( |
1165 | matches("template<typename T> class foo { void bar() { foo X(*this); } };" |
1166 | "template class foo<int>;" , |
1167 | varDecl(hasInitializer(parenListExpr(has(unaryOperator())))))); |
1168 | } |
1169 | |
1170 | TEST_P(ASTMatchersTest, StmtExpr) { |
1171 | EXPECT_TRUE(matches("void declToImport() { int C = ({int X=4; X;}); }" , |
1172 | varDecl(hasInitializer(stmtExpr())))); |
1173 | } |
1174 | |
1175 | TEST_P(ASTMatchersTest, PredefinedExpr) { |
1176 | // __func__ expands as StringLiteral("foo") |
1177 | EXPECT_TRUE(matches("void foo() { __func__; }" , |
1178 | predefinedExpr(hasType(asString("const char[4]" )), |
1179 | has(stringLiteral())))); |
1180 | } |
1181 | |
1182 | TEST_P(ASTMatchersTest, AsmStatement) { |
1183 | EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }" , asmStmt())); |
1184 | } |
1185 | |
1186 | TEST_P(ASTMatchersTest, HasCondition) { |
1187 | if (!GetParam().isCXX()) { |
1188 | // FIXME: Add a test for `hasCondition()` that does not depend on C++. |
1189 | return; |
1190 | } |
1191 | |
1192 | StatementMatcher Condition = |
1193 | ifStmt(hasCondition(InnerMatcher: cxxBoolLiteral(equals(Value: true)))); |
1194 | |
1195 | EXPECT_TRUE(matches("void x() { if (true) {} }" , Condition)); |
1196 | EXPECT_TRUE(notMatches("void x() { if (false) {} }" , Condition)); |
1197 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }" , Condition)); |
1198 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }" , Condition)); |
1199 | EXPECT_TRUE(notMatches("void x() { if (1) {} }" , Condition)); |
1200 | } |
1201 | |
1202 | TEST_P(ASTMatchersTest, ConditionalOperator) { |
1203 | if (!GetParam().isCXX()) { |
1204 | // FIXME: Add a test for `conditionalOperator()` that does not depend on |
1205 | // C++. |
1206 | return; |
1207 | } |
1208 | |
1209 | StatementMatcher Conditional = |
1210 | conditionalOperator(hasCondition(InnerMatcher: cxxBoolLiteral(equals(Value: true))), |
1211 | hasTrueExpression(InnerMatcher: cxxBoolLiteral(equals(Value: false)))); |
1212 | |
1213 | EXPECT_TRUE(matches("void x() { true ? false : true; }" , Conditional)); |
1214 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }" , Conditional)); |
1215 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }" , Conditional)); |
1216 | |
1217 | StatementMatcher ConditionalFalse = |
1218 | conditionalOperator(hasFalseExpression(InnerMatcher: cxxBoolLiteral(equals(Value: false)))); |
1219 | |
1220 | EXPECT_TRUE(matches("void x() { true ? true : false; }" , ConditionalFalse)); |
1221 | EXPECT_TRUE( |
1222 | notMatches("void x() { true ? false : true; }" , ConditionalFalse)); |
1223 | |
1224 | EXPECT_TRUE(matches("void x() { true ? true : false; }" , ConditionalFalse)); |
1225 | EXPECT_TRUE( |
1226 | notMatches("void x() { true ? false : true; }" , ConditionalFalse)); |
1227 | } |
1228 | |
1229 | TEST_P(ASTMatchersTest, BinaryConditionalOperator) { |
1230 | if (!GetParam().isCXX()) { |
1231 | // FIXME: This test should work in non-C++ language modes. |
1232 | return; |
1233 | } |
1234 | |
1235 | StatementMatcher AlwaysOne = traverse( |
1236 | TK: TK_AsIs, InnerMatcher: binaryConditionalOperator( |
1237 | hasCondition(InnerMatcher: implicitCastExpr(has(opaqueValueExpr( |
1238 | hasSourceExpression(InnerMatcher: (integerLiteral(equals(Value: 1)))))))), |
1239 | hasFalseExpression(InnerMatcher: integerLiteral(equals(Value: 0))))); |
1240 | |
1241 | EXPECT_TRUE(matches("void x() { 1 ?: 0; }" , AlwaysOne)); |
1242 | |
1243 | StatementMatcher FourNotFive = binaryConditionalOperator( |
1244 | hasTrueExpression( |
1245 | InnerMatcher: opaqueValueExpr(hasSourceExpression(InnerMatcher: (integerLiteral(equals(Value: 4)))))), |
1246 | hasFalseExpression(InnerMatcher: integerLiteral(equals(Value: 5)))); |
1247 | |
1248 | EXPECT_TRUE(matches("void x() { 4 ?: 5; }" , FourNotFive)); |
1249 | } |
1250 | |
1251 | TEST_P(ASTMatchersTest, ArraySubscriptExpr) { |
1252 | EXPECT_TRUE( |
1253 | matches("int i[2]; void f() { i[1] = 1; }" , arraySubscriptExpr())); |
1254 | EXPECT_TRUE(notMatches("int i; void f() { i = 1; }" , arraySubscriptExpr())); |
1255 | } |
1256 | |
1257 | TEST_P(ASTMatchersTest, ForStmt) { |
1258 | EXPECT_TRUE(matches("void f() { for(;;); }" , forStmt())); |
1259 | EXPECT_TRUE(matches("void f() { if(1) for(;;); }" , forStmt())); |
1260 | } |
1261 | |
1262 | TEST_P(ASTMatchersTest, ForStmt_CXX11) { |
1263 | if (!GetParam().isCXX11OrLater()) { |
1264 | return; |
1265 | } |
1266 | EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };" |
1267 | "void f() { for (auto &a : as); }" , |
1268 | forStmt())); |
1269 | } |
1270 | |
1271 | TEST_P(ASTMatchersTest, ForStmt_NoFalsePositives) { |
1272 | EXPECT_TRUE(notMatches("void f() { ; }" , forStmt())); |
1273 | EXPECT_TRUE(notMatches("void f() { if(1); }" , forStmt())); |
1274 | } |
1275 | |
1276 | TEST_P(ASTMatchersTest, CompoundStatement) { |
1277 | EXPECT_TRUE(notMatches("void f();" , compoundStmt())); |
1278 | EXPECT_TRUE(matches("void f() {}" , compoundStmt())); |
1279 | EXPECT_TRUE(matches("void f() {{}}" , compoundStmt())); |
1280 | } |
1281 | |
1282 | TEST_P(ASTMatchersTest, CompoundStatement_DoesNotMatchEmptyStruct) { |
1283 | if (!GetParam().isCXX()) { |
1284 | // FIXME: Add a similar test that does not depend on C++. |
1285 | return; |
1286 | } |
1287 | // It's not a compound statement just because there's "{}" in the source |
1288 | // text. This is an AST search, not grep. |
1289 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }" , compoundStmt())); |
1290 | EXPECT_TRUE( |
1291 | matches("namespace n { struct S { void f() {{}} }; }" , compoundStmt())); |
1292 | } |
1293 | |
1294 | TEST_P(ASTMatchersTest, CastExpr_MatchesExplicitCasts) { |
1295 | EXPECT_TRUE(matches("void *p = (void *)(&p);" , castExpr())); |
1296 | } |
1297 | |
1298 | TEST_P(ASTMatchersTest, CastExpr_MatchesExplicitCasts_CXX) { |
1299 | if (!GetParam().isCXX()) { |
1300 | return; |
1301 | } |
1302 | EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);" , castExpr())); |
1303 | EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);" , castExpr())); |
1304 | EXPECT_TRUE(matches("char c = char(0);" , castExpr())); |
1305 | } |
1306 | |
1307 | TEST_P(ASTMatchersTest, CastExpression_MatchesImplicitCasts) { |
1308 | // This test creates an implicit cast from int to char. |
1309 | EXPECT_TRUE(matches("char c = 0;" , traverse(TK_AsIs, castExpr()))); |
1310 | // This test creates an implicit cast from lvalue to rvalue. |
1311 | EXPECT_TRUE(matches("void f() { char c = 0, d = c; }" , |
1312 | traverse(TK_AsIs, castExpr()))); |
1313 | } |
1314 | |
1315 | TEST_P(ASTMatchersTest, CastExpr_DoesNotMatchNonCasts) { |
1316 | if (GetParam().isC()) { |
1317 | // This does have a cast in C |
1318 | EXPECT_TRUE(matches("char c = '0';" , implicitCastExpr())); |
1319 | } else { |
1320 | EXPECT_TRUE(notMatches("char c = '0';" , castExpr())); |
1321 | } |
1322 | EXPECT_TRUE(notMatches("int i = (0);" , castExpr())); |
1323 | EXPECT_TRUE(notMatches("int i = 0;" , castExpr())); |
1324 | } |
1325 | |
1326 | TEST_P(ASTMatchersTest, CastExpr_DoesNotMatchNonCasts_CXX) { |
1327 | if (!GetParam().isCXX()) { |
1328 | return; |
1329 | } |
1330 | EXPECT_TRUE(notMatches("char c, &q = c;" , castExpr())); |
1331 | } |
1332 | |
1333 | TEST_P(ASTMatchersTest, CXXReinterpretCastExpr) { |
1334 | if (!GetParam().isCXX()) { |
1335 | return; |
1336 | } |
1337 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);" , |
1338 | cxxReinterpretCastExpr())); |
1339 | } |
1340 | |
1341 | TEST_P(ASTMatchersTest, CXXReinterpretCastExpr_DoesNotMatchOtherCasts) { |
1342 | if (!GetParam().isCXX()) { |
1343 | return; |
1344 | } |
1345 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);" , cxxReinterpretCastExpr())); |
1346 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);" , |
1347 | cxxReinterpretCastExpr())); |
1348 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);" , |
1349 | cxxReinterpretCastExpr())); |
1350 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
1351 | "B b;" |
1352 | "D* p = dynamic_cast<D*>(&b);" , |
1353 | cxxReinterpretCastExpr())); |
1354 | } |
1355 | |
1356 | TEST_P(ASTMatchersTest, CXXFunctionalCastExpr_MatchesSimpleCase) { |
1357 | if (!GetParam().isCXX()) { |
1358 | return; |
1359 | } |
1360 | StringRef foo_class = "class Foo { public: Foo(const char*); };" ; |
1361 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }" , |
1362 | cxxFunctionalCastExpr())); |
1363 | } |
1364 | |
1365 | TEST_P(ASTMatchersTest, CXXFunctionalCastExpr_DoesNotMatchOtherCasts) { |
1366 | if (!GetParam().isCXX()) { |
1367 | return; |
1368 | } |
1369 | StringRef FooClass = "class Foo { public: Foo(const char*); };" ; |
1370 | EXPECT_TRUE( |
1371 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }" , |
1372 | cxxFunctionalCastExpr())); |
1373 | EXPECT_TRUE(notMatches(FooClass + "void r() { Foo f = \"hello world\"; }" , |
1374 | cxxFunctionalCastExpr())); |
1375 | } |
1376 | |
1377 | TEST_P(ASTMatchersTest, CXXDynamicCastExpr) { |
1378 | if (!GetParam().isCXX()) { |
1379 | return; |
1380 | } |
1381 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
1382 | "B b;" |
1383 | "D* p = dynamic_cast<D*>(&b);" , |
1384 | cxxDynamicCastExpr())); |
1385 | } |
1386 | |
1387 | TEST_P(ASTMatchersTest, CXXStaticCastExpr_MatchesSimpleCase) { |
1388 | if (!GetParam().isCXX()) { |
1389 | return; |
1390 | } |
1391 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));" , cxxStaticCastExpr())); |
1392 | } |
1393 | |
1394 | TEST_P(ASTMatchersTest, CXXStaticCastExpr_DoesNotMatchOtherCasts) { |
1395 | if (!GetParam().isCXX()) { |
1396 | return; |
1397 | } |
1398 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);" , cxxStaticCastExpr())); |
1399 | EXPECT_TRUE( |
1400 | notMatches("char q, *p = const_cast<char*>(&q);" , cxxStaticCastExpr())); |
1401 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);" , |
1402 | cxxStaticCastExpr())); |
1403 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
1404 | "B b;" |
1405 | "D* p = dynamic_cast<D*>(&b);" , |
1406 | cxxStaticCastExpr())); |
1407 | } |
1408 | |
1409 | TEST_P(ASTMatchersTest, CStyleCastExpr_MatchesSimpleCase) { |
1410 | EXPECT_TRUE(matches("int i = (int) 2.2f;" , cStyleCastExpr())); |
1411 | } |
1412 | |
1413 | TEST_P(ASTMatchersTest, CStyleCastExpr_DoesNotMatchOtherCasts) { |
1414 | if (!GetParam().isCXX()) { |
1415 | return; |
1416 | } |
1417 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);" |
1418 | "char q, *r = const_cast<char*>(&q);" |
1419 | "void* s = reinterpret_cast<char*>(&s);" |
1420 | "struct B { virtual ~B() {} }; struct D : B {};" |
1421 | "B b;" |
1422 | "D* t = dynamic_cast<D*>(&b);" , |
1423 | cStyleCastExpr())); |
1424 | } |
1425 | |
1426 | TEST_P(ASTMatchersTest, ImplicitCastExpr_MatchesSimpleCase) { |
1427 | // This test creates an implicit const cast. |
1428 | EXPECT_TRUE( |
1429 | matches("void f() { int x = 0; const int y = x; }" , |
1430 | traverse(TK_AsIs, varDecl(hasInitializer(implicitCastExpr()))))); |
1431 | // This test creates an implicit cast from int to char. |
1432 | EXPECT_TRUE( |
1433 | matches("char c = 0;" , |
1434 | traverse(TK_AsIs, varDecl(hasInitializer(implicitCastExpr()))))); |
1435 | // This test creates an implicit array-to-pointer cast. |
1436 | EXPECT_TRUE( |
1437 | matches("int arr[6]; int *p = arr;" , |
1438 | traverse(TK_AsIs, varDecl(hasInitializer(implicitCastExpr()))))); |
1439 | } |
1440 | |
1441 | TEST_P(ASTMatchersTest, ImplicitCastExpr_DoesNotMatchIncorrectly) { |
1442 | // This test verifies that implicitCastExpr() matches exactly when implicit |
1443 | // casts are present, and that it ignores explicit and paren casts. |
1444 | |
1445 | // These two test cases have no casts. |
1446 | EXPECT_TRUE( |
1447 | notMatches("int x = 0;" , varDecl(hasInitializer(implicitCastExpr())))); |
1448 | EXPECT_TRUE( |
1449 | notMatches("int x = (0);" , varDecl(hasInitializer(implicitCastExpr())))); |
1450 | EXPECT_TRUE(notMatches("void f() { int x = 0; double d = (double) x; }" , |
1451 | varDecl(hasInitializer(implicitCastExpr())))); |
1452 | } |
1453 | |
1454 | TEST_P(ASTMatchersTest, ImplicitCastExpr_DoesNotMatchIncorrectly_CXX) { |
1455 | if (!GetParam().isCXX()) { |
1456 | return; |
1457 | } |
1458 | EXPECT_TRUE(notMatches("int x = 0, &y = x;" , |
1459 | varDecl(hasInitializer(implicitCastExpr())))); |
1460 | EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);" , |
1461 | varDecl(hasInitializer(implicitCastExpr())))); |
1462 | } |
1463 | |
1464 | TEST_P(ASTMatchersTest, Stmt_DoesNotMatchDeclarations) { |
1465 | EXPECT_TRUE(notMatches("struct X {};" , stmt())); |
1466 | } |
1467 | |
1468 | TEST_P(ASTMatchersTest, Stmt_MatchesCompoundStatments) { |
1469 | EXPECT_TRUE(matches("void x() {}" , stmt())); |
1470 | } |
1471 | |
1472 | TEST_P(ASTMatchersTest, DeclStmt_DoesNotMatchCompoundStatements) { |
1473 | EXPECT_TRUE(notMatches("void x() {}" , declStmt())); |
1474 | } |
1475 | |
1476 | TEST_P(ASTMatchersTest, DeclStmt_MatchesVariableDeclarationStatements) { |
1477 | EXPECT_TRUE(matches("void x() { int a; }" , declStmt())); |
1478 | } |
1479 | |
1480 | TEST_P(ASTMatchersTest, ExprWithCleanups_MatchesExprWithCleanups) { |
1481 | if (!GetParam().isCXX()) { |
1482 | return; |
1483 | } |
1484 | EXPECT_TRUE( |
1485 | matches("struct Foo { ~Foo(); };" |
1486 | "const Foo f = Foo();" , |
1487 | traverse(TK_AsIs, varDecl(hasInitializer(exprWithCleanups()))))); |
1488 | EXPECT_FALSE( |
1489 | matches("struct Foo { }; Foo a;" |
1490 | "const Foo f = a;" , |
1491 | traverse(TK_AsIs, varDecl(hasInitializer(exprWithCleanups()))))); |
1492 | } |
1493 | |
1494 | TEST_P(ASTMatchersTest, InitListExpr) { |
1495 | EXPECT_TRUE(matches("int a[] = { 1, 2 };" , |
1496 | initListExpr(hasType(asString("int[2]" ))))); |
1497 | EXPECT_TRUE(matches("struct B { int x, y; }; struct B b = { 5, 6 };" , |
1498 | initListExpr(hasType(recordDecl(hasName("B" )))))); |
1499 | EXPECT_TRUE( |
1500 | matches("int i[1] = {42, [0] = 43};" , integerLiteral(equals(42)))); |
1501 | } |
1502 | |
1503 | TEST_P(ASTMatchersTest, InitListExpr_CXX) { |
1504 | if (!GetParam().isCXX()) { |
1505 | return; |
1506 | } |
1507 | EXPECT_TRUE(matches("struct S { S(void (*a)()); };" |
1508 | "void f();" |
1509 | "S s[1] = { &f };" , |
1510 | declRefExpr(to(functionDecl(hasName("f" )))))); |
1511 | } |
1512 | |
1513 | TEST_P(ASTMatchersTest, |
1514 | CXXStdInitializerListExpression_MatchesCXXStdInitializerListExpression) { |
1515 | if (!GetParam().isCXX11OrLater()) { |
1516 | return; |
1517 | } |
1518 | StringRef code = "namespace std {" |
1519 | "template <typename E> class initializer_list {" |
1520 | " public: const E *a, *b;" |
1521 | "};" |
1522 | "}" |
1523 | "struct A {" |
1524 | " A(std::initializer_list<int>) {}" |
1525 | "};" ; |
1526 | EXPECT_TRUE(matches( |
1527 | code + "A a{0};" , |
1528 | traverse(TK_AsIs, cxxConstructExpr(has(cxxStdInitializerListExpr()), |
1529 | hasDeclaration(cxxConstructorDecl( |
1530 | ofClass(hasName("A" )))))))); |
1531 | EXPECT_TRUE(matches( |
1532 | code + "A a = {0};" , |
1533 | traverse(TK_AsIs, cxxConstructExpr(has(cxxStdInitializerListExpr()), |
1534 | hasDeclaration(cxxConstructorDecl( |
1535 | ofClass(hasName("A" )))))))); |
1536 | |
1537 | EXPECT_TRUE(notMatches("int a[] = { 1, 2 };" , cxxStdInitializerListExpr())); |
1538 | EXPECT_TRUE(notMatches("struct B { int x, y; }; B b = { 5, 6 };" , |
1539 | cxxStdInitializerListExpr())); |
1540 | } |
1541 | |
1542 | TEST_P(ASTMatchersTest, UsingDecl_MatchesUsingDeclarations) { |
1543 | if (!GetParam().isCXX()) { |
1544 | return; |
1545 | } |
1546 | EXPECT_TRUE(matches("namespace X { int x; } using X::x;" , usingDecl())); |
1547 | } |
1548 | |
1549 | TEST_P(ASTMatchersTest, UsingDecl_MatchesShadowUsingDelcarations) { |
1550 | if (!GetParam().isCXX()) { |
1551 | return; |
1552 | } |
1553 | EXPECT_TRUE(matches("namespace f { int a; } using f::a;" , |
1554 | usingDecl(hasAnyUsingShadowDecl(hasName("a" ))))); |
1555 | } |
1556 | |
1557 | TEST_P(ASTMatchersTest, UsingEnumDecl_MatchesUsingEnumDeclarations) { |
1558 | if (!GetParam().isCXX20OrLater()) { |
1559 | return; |
1560 | } |
1561 | EXPECT_TRUE( |
1562 | matches("namespace X { enum x {}; } using enum X::x;" , usingEnumDecl())); |
1563 | } |
1564 | |
1565 | TEST_P(ASTMatchersTest, UsingEnumDecl_MatchesShadowUsingDeclarations) { |
1566 | if (!GetParam().isCXX20OrLater()) { |
1567 | return; |
1568 | } |
1569 | EXPECT_TRUE(matches("namespace f { enum a {b}; } using enum f::a;" , |
1570 | usingEnumDecl(hasAnyUsingShadowDecl(hasName("b" ))))); |
1571 | } |
1572 | |
1573 | TEST_P(ASTMatchersTest, UsingDirectiveDecl_MatchesUsingNamespace) { |
1574 | if (!GetParam().isCXX()) { |
1575 | return; |
1576 | } |
1577 | EXPECT_TRUE(matches("namespace X { int x; } using namespace X;" , |
1578 | usingDirectiveDecl())); |
1579 | EXPECT_FALSE( |
1580 | matches("namespace X { int x; } using X::x;" , usingDirectiveDecl())); |
1581 | } |
1582 | |
1583 | TEST_P(ASTMatchersTest, WhileStmt) { |
1584 | EXPECT_TRUE(notMatches("void x() {}" , whileStmt())); |
1585 | EXPECT_TRUE(matches("void x() { while(1); }" , whileStmt())); |
1586 | EXPECT_TRUE(notMatches("void x() { do {} while(1); }" , whileStmt())); |
1587 | } |
1588 | |
1589 | TEST_P(ASTMatchersTest, DoStmt_MatchesDoLoops) { |
1590 | EXPECT_TRUE(matches("void x() { do {} while(1); }" , doStmt())); |
1591 | EXPECT_TRUE(matches("void x() { do ; while(0); }" , doStmt())); |
1592 | } |
1593 | |
1594 | TEST_P(ASTMatchersTest, DoStmt_DoesNotMatchWhileLoops) { |
1595 | EXPECT_TRUE(notMatches("void x() { while(1) {} }" , doStmt())); |
1596 | } |
1597 | |
1598 | TEST_P(ASTMatchersTest, SwitchCase_MatchesCase) { |
1599 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }" , switchCase())); |
1600 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }" , switchCase())); |
1601 | EXPECT_TRUE(matches("void x() { switch(42) default:; }" , switchCase())); |
1602 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }" , switchCase())); |
1603 | } |
1604 | |
1605 | TEST_P(ASTMatchersTest, SwitchCase_MatchesSwitch) { |
1606 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }" , switchStmt())); |
1607 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }" , switchStmt())); |
1608 | EXPECT_TRUE(matches("void x() { switch(42) default:; }" , switchStmt())); |
1609 | EXPECT_TRUE(notMatches("void x() {}" , switchStmt())); |
1610 | } |
1611 | |
1612 | TEST_P(ASTMatchersTest, CxxExceptionHandling_SimpleCases) { |
1613 | if (!GetParam().isCXX()) { |
1614 | return; |
1615 | } |
1616 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }" , cxxCatchStmt())); |
1617 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }" , cxxTryStmt())); |
1618 | EXPECT_TRUE( |
1619 | notMatches("void foo() try { } catch(int X) { }" , cxxThrowExpr())); |
1620 | EXPECT_TRUE( |
1621 | matches("void foo() try { throw; } catch(int X) { }" , cxxThrowExpr())); |
1622 | EXPECT_TRUE( |
1623 | matches("void foo() try { throw 5;} catch(int X) { }" , cxxThrowExpr())); |
1624 | EXPECT_TRUE(matches("void foo() try { throw; } catch(...) { }" , |
1625 | cxxCatchStmt(isCatchAll()))); |
1626 | EXPECT_TRUE(notMatches("void foo() try { throw; } catch(int) { }" , |
1627 | cxxCatchStmt(isCatchAll()))); |
1628 | EXPECT_TRUE(matches("void foo() try {} catch(int X) { }" , |
1629 | varDecl(isExceptionVariable()))); |
1630 | EXPECT_TRUE(notMatches("void foo() try { int X; } catch (...) { }" , |
1631 | varDecl(isExceptionVariable()))); |
1632 | } |
1633 | |
1634 | TEST_P(ASTMatchersTest, ParenExpr_SimpleCases) { |
1635 | EXPECT_TRUE(matches("int i = (3);" , traverse(TK_AsIs, parenExpr()))); |
1636 | EXPECT_TRUE(matches("int i = (3 + 7);" , traverse(TK_AsIs, parenExpr()))); |
1637 | EXPECT_TRUE(notMatches("int i = 3;" , traverse(TK_AsIs, parenExpr()))); |
1638 | EXPECT_TRUE(notMatches("int f() { return 1; }; void g() { int a = f(); }" , |
1639 | traverse(TK_AsIs, parenExpr()))); |
1640 | } |
1641 | |
1642 | TEST_P(ASTMatchersTest, IgnoringParens) { |
1643 | EXPECT_FALSE(matches("const char* str = (\"my-string\");" , |
1644 | traverse(TK_AsIs, implicitCastExpr(hasSourceExpression( |
1645 | stringLiteral()))))); |
1646 | EXPECT_TRUE( |
1647 | matches("const char* str = (\"my-string\");" , |
1648 | traverse(TK_AsIs, implicitCastExpr(hasSourceExpression( |
1649 | ignoringParens(stringLiteral())))))); |
1650 | } |
1651 | |
1652 | TEST_P(ASTMatchersTest, QualType) { |
1653 | EXPECT_TRUE(matches("struct S {};" , qualType().bind("loc" ))); |
1654 | } |
1655 | |
1656 | TEST_P(ASTMatchersTest, ConstantArrayType) { |
1657 | EXPECT_TRUE(matches("int a[2];" , constantArrayType())); |
1658 | EXPECT_TRUE(notMatches("void f() { int a[] = { 2, 3 }; int b[a[0]]; }" , |
1659 | constantArrayType(hasElementType(builtinType())))); |
1660 | |
1661 | EXPECT_TRUE(matches("int a[42];" , constantArrayType(hasSize(42)))); |
1662 | EXPECT_TRUE(matches("int b[2*21];" , constantArrayType(hasSize(42)))); |
1663 | EXPECT_TRUE(notMatches("int c[41], d[43];" , constantArrayType(hasSize(42)))); |
1664 | } |
1665 | |
1666 | TEST_P(ASTMatchersTest, DependentSizedArrayType) { |
1667 | if (!GetParam().isCXX()) { |
1668 | return; |
1669 | } |
1670 | EXPECT_TRUE( |
1671 | matches("template <typename T, int Size> class array { T data[Size]; };" , |
1672 | dependentSizedArrayType())); |
1673 | EXPECT_TRUE( |
1674 | notMatches("int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }" , |
1675 | dependentSizedArrayType())); |
1676 | } |
1677 | |
1678 | TEST_P(ASTMatchersTest, DependentSizedExtVectorType) { |
1679 | if (!GetParam().isCXX()) { |
1680 | return; |
1681 | } |
1682 | EXPECT_TRUE(matches("template<typename T, int Size>" |
1683 | "class vector {" |
1684 | " typedef T __attribute__((ext_vector_type(Size))) type;" |
1685 | "};" , |
1686 | dependentSizedExtVectorType())); |
1687 | EXPECT_TRUE( |
1688 | notMatches("int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }" , |
1689 | dependentSizedExtVectorType())); |
1690 | } |
1691 | |
1692 | TEST_P(ASTMatchersTest, IncompleteArrayType) { |
1693 | EXPECT_TRUE(matches("int a[] = { 2, 3 };" , incompleteArrayType())); |
1694 | EXPECT_TRUE(matches("void f(int a[]) {}" , incompleteArrayType())); |
1695 | |
1696 | EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }" , |
1697 | incompleteArrayType())); |
1698 | } |
1699 | |
1700 | TEST_P(ASTMatchersTest, VariableArrayType) { |
1701 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }" , variableArrayType())); |
1702 | EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];" , variableArrayType())); |
1703 | |
1704 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }" , |
1705 | variableArrayType(hasSizeExpr(ignoringImpCasts( |
1706 | declRefExpr(to(varDecl(hasName("b" ))))))))); |
1707 | } |
1708 | |
1709 | TEST_P(ASTMatchersTest, AtomicType) { |
1710 | if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() != |
1711 | llvm::Triple::Win32) { |
1712 | // FIXME: Make this work for MSVC. |
1713 | EXPECT_TRUE(matches("_Atomic(int) i;" , atomicType())); |
1714 | |
1715 | EXPECT_TRUE( |
1716 | matches("_Atomic(int) i;" , atomicType(hasValueType(isInteger())))); |
1717 | EXPECT_TRUE( |
1718 | notMatches("_Atomic(float) f;" , atomicType(hasValueType(isInteger())))); |
1719 | } |
1720 | } |
1721 | |
1722 | TEST_P(ASTMatchersTest, AutoType) { |
1723 | if (!GetParam().isCXX11OrLater()) { |
1724 | return; |
1725 | } |
1726 | EXPECT_TRUE(matches("auto i = 2;" , autoType())); |
1727 | EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }" , |
1728 | autoType())); |
1729 | |
1730 | EXPECT_TRUE(matches("auto i = 2;" , varDecl(hasType(isInteger())))); |
1731 | EXPECT_TRUE(matches("struct X{}; auto x = X{};" , |
1732 | varDecl(hasType(recordDecl(hasName("X" )))))); |
1733 | |
1734 | // FIXME: Matching against the type-as-written can't work here, because the |
1735 | // type as written was not deduced. |
1736 | // EXPECT_TRUE(matches("auto a = 1;", |
1737 | // autoType(hasDeducedType(isInteger())))); |
1738 | // EXPECT_TRUE(notMatches("auto b = 2.0;", |
1739 | // autoType(hasDeducedType(isInteger())))); |
1740 | } |
1741 | |
1742 | TEST_P(ASTMatchersTest, DecltypeType) { |
1743 | if (!GetParam().isCXX11OrLater()) { |
1744 | return; |
1745 | } |
1746 | EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;" , decltypeType())); |
1747 | EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;" , |
1748 | decltypeType(hasUnderlyingType(isInteger())))); |
1749 | } |
1750 | |
1751 | TEST_P(ASTMatchersTest, FunctionType) { |
1752 | EXPECT_TRUE(matches("int (*f)(int);" , functionType())); |
1753 | EXPECT_TRUE(matches("void f(int i) {}" , functionType())); |
1754 | } |
1755 | |
1756 | TEST_P(ASTMatchersTest, IgnoringParens_Type) { |
1757 | EXPECT_TRUE( |
1758 | notMatches("void (*fp)(void);" , pointerType(pointee(functionType())))); |
1759 | EXPECT_TRUE(matches("void (*fp)(void);" , |
1760 | pointerType(pointee(ignoringParens(functionType()))))); |
1761 | } |
1762 | |
1763 | TEST_P(ASTMatchersTest, FunctionProtoType) { |
1764 | EXPECT_TRUE(matches("int (*f)(int);" , functionProtoType())); |
1765 | EXPECT_TRUE(matches("void f(int i);" , functionProtoType())); |
1766 | EXPECT_TRUE(matches("void f(void);" , functionProtoType(parameterCountIs(0)))); |
1767 | } |
1768 | |
1769 | TEST_P(ASTMatchersTest, FunctionProtoType_C) { |
1770 | if (!GetParam().isCOrEarlier(MaximumStdVersion: 17)) { |
1771 | return; |
1772 | } |
1773 | EXPECT_TRUE(notMatches("void f();" , functionProtoType())); |
1774 | } |
1775 | |
1776 | TEST_P(ASTMatchersTest, FunctionProtoType_CXX) { |
1777 | if (!GetParam().isCXX()) { |
1778 | return; |
1779 | } |
1780 | EXPECT_TRUE(matches("void f();" , functionProtoType(parameterCountIs(0)))); |
1781 | } |
1782 | |
1783 | TEST_P(ASTMatchersTest, ParenType) { |
1784 | EXPECT_TRUE( |
1785 | matches("int (*array)[4];" , varDecl(hasType(pointsTo(parenType()))))); |
1786 | EXPECT_TRUE(notMatches("int *array[4];" , varDecl(hasType(parenType())))); |
1787 | |
1788 | EXPECT_TRUE(matches( |
1789 | "int (*ptr_to_func)(int);" , |
1790 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
1791 | EXPECT_TRUE(notMatches( |
1792 | "int (*ptr_to_array)[4];" , |
1793 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
1794 | } |
1795 | |
1796 | TEST_P(ASTMatchersTest, PointerType) { |
1797 | // FIXME: Reactive when these tests can be more specific (not matching |
1798 | // implicit code on certain platforms), likely when we have hasDescendant for |
1799 | // Types/TypeLocs. |
1800 | // EXPECT_TRUE(matchAndVerifyResultTrue( |
1801 | // "int* a;", |
1802 | // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))), |
1803 | // std::make_unique<VerifyIdIsBoundTo<TypeLoc>>("loc", 1))); |
1804 | // EXPECT_TRUE(matchAndVerifyResultTrue( |
1805 | // "int* a;", |
1806 | // pointerTypeLoc().bind("loc"), |
1807 | // std::make_unique<VerifyIdIsBoundTo<TypeLoc>>("loc", 1))); |
1808 | EXPECT_TRUE(matches("int** a;" , loc(pointerType(pointee(qualType()))))); |
1809 | EXPECT_TRUE(matches("int** a;" , loc(pointerType(pointee(pointerType()))))); |
1810 | EXPECT_TRUE(matches("int* b; int* * const a = &b;" , |
1811 | loc(qualType(isConstQualified(), pointerType())))); |
1812 | |
1813 | StringRef Fragment = "int *ptr;" ; |
1814 | EXPECT_TRUE(notMatches(Fragment, |
1815 | varDecl(hasName("ptr" ), hasType(blockPointerType())))); |
1816 | EXPECT_TRUE(notMatches( |
1817 | Fragment, varDecl(hasName("ptr" ), hasType(memberPointerType())))); |
1818 | EXPECT_TRUE( |
1819 | matches(Fragment, varDecl(hasName("ptr" ), hasType(pointerType())))); |
1820 | EXPECT_TRUE( |
1821 | notMatches(Fragment, varDecl(hasName("ptr" ), hasType(referenceType())))); |
1822 | } |
1823 | |
1824 | TEST_P(ASTMatchersTest, PointerType_CXX) { |
1825 | if (!GetParam().isCXX()) { |
1826 | return; |
1827 | } |
1828 | StringRef Fragment = "struct A { int i; }; int A::* ptr = &A::i;" ; |
1829 | EXPECT_TRUE(notMatches(Fragment, |
1830 | varDecl(hasName("ptr" ), hasType(blockPointerType())))); |
1831 | EXPECT_TRUE( |
1832 | matches(Fragment, varDecl(hasName("ptr" ), hasType(memberPointerType())))); |
1833 | EXPECT_TRUE( |
1834 | notMatches(Fragment, varDecl(hasName("ptr" ), hasType(pointerType())))); |
1835 | EXPECT_TRUE( |
1836 | notMatches(Fragment, varDecl(hasName("ptr" ), hasType(referenceType())))); |
1837 | EXPECT_TRUE(notMatches( |
1838 | Fragment, varDecl(hasName("ptr" ), hasType(lValueReferenceType())))); |
1839 | EXPECT_TRUE(notMatches( |
1840 | Fragment, varDecl(hasName("ptr" ), hasType(rValueReferenceType())))); |
1841 | |
1842 | Fragment = "int a; int &ref = a;" ; |
1843 | EXPECT_TRUE(notMatches(Fragment, |
1844 | varDecl(hasName("ref" ), hasType(blockPointerType())))); |
1845 | EXPECT_TRUE(notMatches( |
1846 | Fragment, varDecl(hasName("ref" ), hasType(memberPointerType())))); |
1847 | EXPECT_TRUE( |
1848 | notMatches(Fragment, varDecl(hasName("ref" ), hasType(pointerType())))); |
1849 | EXPECT_TRUE( |
1850 | matches(Fragment, varDecl(hasName("ref" ), hasType(referenceType())))); |
1851 | EXPECT_TRUE(matches(Fragment, |
1852 | varDecl(hasName("ref" ), hasType(lValueReferenceType())))); |
1853 | EXPECT_TRUE(notMatches( |
1854 | Fragment, varDecl(hasName("ref" ), hasType(rValueReferenceType())))); |
1855 | } |
1856 | |
1857 | TEST_P(ASTMatchersTest, PointerType_CXX11) { |
1858 | if (!GetParam().isCXX11OrLater()) { |
1859 | return; |
1860 | } |
1861 | StringRef Fragment = "int &&ref = 2;" ; |
1862 | EXPECT_TRUE(notMatches(Fragment, |
1863 | varDecl(hasName("ref" ), hasType(blockPointerType())))); |
1864 | EXPECT_TRUE(notMatches( |
1865 | Fragment, varDecl(hasName("ref" ), hasType(memberPointerType())))); |
1866 | EXPECT_TRUE( |
1867 | notMatches(Fragment, varDecl(hasName("ref" ), hasType(pointerType())))); |
1868 | EXPECT_TRUE( |
1869 | matches(Fragment, varDecl(hasName("ref" ), hasType(referenceType())))); |
1870 | EXPECT_TRUE(notMatches( |
1871 | Fragment, varDecl(hasName("ref" ), hasType(lValueReferenceType())))); |
1872 | EXPECT_TRUE(matches(Fragment, |
1873 | varDecl(hasName("ref" ), hasType(rValueReferenceType())))); |
1874 | } |
1875 | |
1876 | TEST_P(ASTMatchersTest, AutoRefTypes) { |
1877 | if (!GetParam().isCXX11OrLater()) { |
1878 | return; |
1879 | } |
1880 | |
1881 | StringRef Fragment = "auto a = 1;" |
1882 | "auto b = a;" |
1883 | "auto &c = a;" |
1884 | "auto &&d = c;" |
1885 | "auto &&e = 2;" ; |
1886 | EXPECT_TRUE( |
1887 | notMatches(Fragment, varDecl(hasName("a" ), hasType(referenceType())))); |
1888 | EXPECT_TRUE( |
1889 | notMatches(Fragment, varDecl(hasName("b" ), hasType(referenceType())))); |
1890 | EXPECT_TRUE( |
1891 | matches(Fragment, varDecl(hasName("c" ), hasType(referenceType())))); |
1892 | EXPECT_TRUE( |
1893 | matches(Fragment, varDecl(hasName("c" ), hasType(lValueReferenceType())))); |
1894 | EXPECT_TRUE(notMatches( |
1895 | Fragment, varDecl(hasName("c" ), hasType(rValueReferenceType())))); |
1896 | EXPECT_TRUE( |
1897 | matches(Fragment, varDecl(hasName("d" ), hasType(referenceType())))); |
1898 | EXPECT_TRUE( |
1899 | matches(Fragment, varDecl(hasName("d" ), hasType(lValueReferenceType())))); |
1900 | EXPECT_TRUE(notMatches( |
1901 | Fragment, varDecl(hasName("d" ), hasType(rValueReferenceType())))); |
1902 | EXPECT_TRUE( |
1903 | matches(Fragment, varDecl(hasName("e" ), hasType(referenceType())))); |
1904 | EXPECT_TRUE(notMatches( |
1905 | Fragment, varDecl(hasName("e" ), hasType(lValueReferenceType())))); |
1906 | EXPECT_TRUE( |
1907 | matches(Fragment, varDecl(hasName("e" ), hasType(rValueReferenceType())))); |
1908 | } |
1909 | |
1910 | TEST_P(ASTMatchersTest, EnumType) { |
1911 | EXPECT_TRUE( |
1912 | matches("enum Color { Green }; enum Color color;" , loc(enumType()))); |
1913 | } |
1914 | |
1915 | TEST_P(ASTMatchersTest, EnumType_CXX) { |
1916 | if (!GetParam().isCXX()) { |
1917 | return; |
1918 | } |
1919 | EXPECT_TRUE(matches("enum Color { Green }; Color color;" , loc(enumType()))); |
1920 | } |
1921 | |
1922 | TEST_P(ASTMatchersTest, EnumType_CXX11) { |
1923 | if (!GetParam().isCXX11OrLater()) { |
1924 | return; |
1925 | } |
1926 | EXPECT_TRUE( |
1927 | matches("enum class Color { Green }; Color color;" , loc(enumType()))); |
1928 | } |
1929 | |
1930 | TEST_P(ASTMatchersTest, PointerType_MatchesPointersToConstTypes) { |
1931 | EXPECT_TRUE(matches("int b; int * const a = &b;" , loc(pointerType()))); |
1932 | EXPECT_TRUE(matches("int b; int * const a = &b;" , loc(pointerType()))); |
1933 | EXPECT_TRUE(matches("int b; const int * a = &b;" , |
1934 | loc(pointerType(pointee(builtinType()))))); |
1935 | EXPECT_TRUE(matches("int b; const int * a = &b;" , |
1936 | pointerType(pointee(builtinType())))); |
1937 | } |
1938 | |
1939 | TEST_P(ASTMatchersTest, TypedefType) { |
1940 | EXPECT_TRUE(matches("typedef int X; X a;" , |
1941 | varDecl(hasName("a" ), hasType(elaboratedType( |
1942 | namesType(typedefType())))))); |
1943 | } |
1944 | |
1945 | TEST_P(ASTMatchersTest, MacroQualifiedType) { |
1946 | EXPECT_TRUE(matches( |
1947 | R"( |
1948 | #define CDECL __attribute__((cdecl)) |
1949 | typedef void (CDECL *X)(); |
1950 | )" , |
1951 | typedefDecl(hasType(pointerType(pointee(macroQualifiedType())))))); |
1952 | EXPECT_TRUE(notMatches( |
1953 | R"( |
1954 | typedef void (__attribute__((cdecl)) *Y)(); |
1955 | )" , |
1956 | typedefDecl(hasType(pointerType(pointee(macroQualifiedType())))))); |
1957 | } |
1958 | |
1959 | TEST_P(ASTMatchersTest, TemplateSpecializationType) { |
1960 | if (!GetParam().isCXX()) { |
1961 | return; |
1962 | } |
1963 | EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;" , |
1964 | templateSpecializationType())); |
1965 | } |
1966 | |
1967 | TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) { |
1968 | if (!GetParam().isCXX17OrLater()) { |
1969 | return; |
1970 | } |
1971 | EXPECT_TRUE( |
1972 | matches("template <typename T> class A{ public: A(T) {} }; A a(1);" , |
1973 | deducedTemplateSpecializationType())); |
1974 | } |
1975 | |
1976 | TEST_P(ASTMatchersTest, DependentNameType) { |
1977 | if (!GetParam().isCXX()) { |
1978 | return; |
1979 | } |
1980 | |
1981 | EXPECT_TRUE(matches( |
1982 | R"( |
1983 | template <typename T> struct declToImport { |
1984 | typedef typename T::type dependent_name; |
1985 | }; |
1986 | )" , |
1987 | dependentNameType())); |
1988 | } |
1989 | |
1990 | TEST_P(ASTMatchersTest, DependentTemplateSpecializationType) { |
1991 | if (!GetParam().isCXX()) { |
1992 | return; |
1993 | } |
1994 | |
1995 | EXPECT_TRUE(matches( |
1996 | R"( |
1997 | template<typename T> struct A; |
1998 | template<typename T> struct declToImport { |
1999 | typename A<T>::template B<T> a; |
2000 | }; |
2001 | )" , |
2002 | dependentTemplateSpecializationType())); |
2003 | } |
2004 | |
2005 | TEST_P(ASTMatchersTest, RecordType) { |
2006 | EXPECT_TRUE(matches("struct S {}; struct S s;" , |
2007 | recordType(hasDeclaration(recordDecl(hasName("S" )))))); |
2008 | EXPECT_TRUE(notMatches("int i;" , |
2009 | recordType(hasDeclaration(recordDecl(hasName("S" )))))); |
2010 | } |
2011 | |
2012 | TEST_P(ASTMatchersTest, RecordType_CXX) { |
2013 | if (!GetParam().isCXX()) { |
2014 | return; |
2015 | } |
2016 | EXPECT_TRUE(matches("class C {}; C c;" , recordType())); |
2017 | EXPECT_TRUE(matches("struct S {}; S s;" , |
2018 | recordType(hasDeclaration(recordDecl(hasName("S" )))))); |
2019 | } |
2020 | |
2021 | TEST_P(ASTMatchersTest, ElaboratedType) { |
2022 | if (!GetParam().isCXX()) { |
2023 | // FIXME: Add a test for `elaboratedType()` that does not depend on C++. |
2024 | return; |
2025 | } |
2026 | EXPECT_TRUE(matches("namespace N {" |
2027 | " namespace M {" |
2028 | " class D {};" |
2029 | " }" |
2030 | "}" |
2031 | "N::M::D d;" , |
2032 | elaboratedType())); |
2033 | EXPECT_TRUE(matches("class C {} c;" , elaboratedType())); |
2034 | EXPECT_TRUE(matches("class C {}; C c;" , elaboratedType())); |
2035 | } |
2036 | |
2037 | TEST_P(ASTMatchersTest, SubstTemplateTypeParmType) { |
2038 | if (!GetParam().isCXX()) { |
2039 | return; |
2040 | } |
2041 | StringRef code = "template <typename T>" |
2042 | "int F() {" |
2043 | " return 1 + T();" |
2044 | "}" |
2045 | "int i = F<int>();" ; |
2046 | EXPECT_FALSE(matches(code, binaryOperator(hasLHS( |
2047 | expr(hasType(substTemplateTypeParmType())))))); |
2048 | EXPECT_TRUE(matches(code, binaryOperator(hasRHS( |
2049 | expr(hasType(substTemplateTypeParmType())))))); |
2050 | } |
2051 | |
2052 | TEST_P(ASTMatchersTest, NestedNameSpecifier) { |
2053 | if (!GetParam().isCXX()) { |
2054 | return; |
2055 | } |
2056 | EXPECT_TRUE( |
2057 | matches("namespace ns { struct A {}; } ns::A a;" , nestedNameSpecifier())); |
2058 | EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };" , |
2059 | nestedNameSpecifier())); |
2060 | EXPECT_TRUE( |
2061 | matches("struct A { void f(); }; void A::f() {}" , nestedNameSpecifier())); |
2062 | EXPECT_TRUE(matches("namespace a { namespace b {} } namespace ab = a::b;" , |
2063 | nestedNameSpecifier())); |
2064 | |
2065 | EXPECT_TRUE(matches("struct A { static void f() {} }; void g() { A::f(); }" , |
2066 | nestedNameSpecifier())); |
2067 | EXPECT_TRUE( |
2068 | notMatches("struct A { static void f() {} }; void g(A* a) { a->f(); }" , |
2069 | nestedNameSpecifier())); |
2070 | } |
2071 | |
2072 | TEST_P(ASTMatchersTest, Attr) { |
2073 | // Windows adds some implicit attributes. |
2074 | bool AutomaticAttributes = StringRef(GetParam().Target).contains(Other: "win32" ); |
2075 | if (GetParam().isCXX11OrLater()) { |
2076 | EXPECT_TRUE(matches("struct [[clang::warn_unused_result]] F{};" , attr())); |
2077 | |
2078 | // Unknown attributes are not parsed into an AST node. |
2079 | if (!AutomaticAttributes) { |
2080 | EXPECT_TRUE(notMatches("int x [[unknownattr]];" , attr())); |
2081 | } |
2082 | } |
2083 | if (GetParam().isCXX17OrLater()) { |
2084 | EXPECT_TRUE(matches("struct [[nodiscard]] F{};" , attr())); |
2085 | } |
2086 | EXPECT_TRUE(matches("int x(int * __attribute__((nonnull)) );" , attr())); |
2087 | if (!AutomaticAttributes) { |
2088 | EXPECT_TRUE(notMatches("struct F{}; int x(int *);" , attr())); |
2089 | // Some known attributes are not parsed into an AST node. |
2090 | EXPECT_TRUE(notMatches("typedef int x __attribute__((ext_vector_type(1)));" , |
2091 | attr())); |
2092 | } |
2093 | } |
2094 | |
2095 | TEST_P(ASTMatchersTest, NullStmt) { |
2096 | EXPECT_TRUE(matches("void f() {int i;;}" , nullStmt())); |
2097 | EXPECT_TRUE(notMatches("void f() {int i;}" , nullStmt())); |
2098 | } |
2099 | |
2100 | TEST_P(ASTMatchersTest, NamespaceAliasDecl) { |
2101 | if (!GetParam().isCXX()) { |
2102 | return; |
2103 | } |
2104 | EXPECT_TRUE(matches("namespace test {} namespace alias = ::test;" , |
2105 | namespaceAliasDecl(hasName("alias" )))); |
2106 | } |
2107 | |
2108 | TEST_P(ASTMatchersTest, NestedNameSpecifier_MatchesTypes) { |
2109 | if (!GetParam().isCXX()) { |
2110 | return; |
2111 | } |
2112 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
2113 | specifiesType(InnerMatcher: hasDeclaration(InnerMatcher: recordDecl(hasName(Name: "A" ))))); |
2114 | EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;" , Matcher)); |
2115 | EXPECT_TRUE( |
2116 | matches("struct A { struct B { struct C {}; }; }; A::B::C c;" , Matcher)); |
2117 | EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;" , Matcher)); |
2118 | } |
2119 | |
2120 | TEST_P(ASTMatchersTest, NestedNameSpecifier_MatchesNamespaceDecls) { |
2121 | if (!GetParam().isCXX()) { |
2122 | return; |
2123 | } |
2124 | NestedNameSpecifierMatcher Matcher = |
2125 | nestedNameSpecifier(specifiesNamespace(InnerMatcher: hasName(Name: "ns" ))); |
2126 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;" , Matcher)); |
2127 | EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;" , Matcher)); |
2128 | EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;" , Matcher)); |
2129 | } |
2130 | |
2131 | TEST_P(ASTMatchersTest, |
2132 | NestedNameSpecifier_MatchesNestedNameSpecifierPrefixes) { |
2133 | if (!GetParam().isCXX()) { |
2134 | return; |
2135 | } |
2136 | EXPECT_TRUE(matches( |
2137 | "struct A { struct B { struct C {}; }; }; A::B::C c;" , |
2138 | nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A" )))))); |
2139 | EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;" , |
2140 | nestedNameSpecifierLoc(hasPrefix(specifiesTypeLoc( |
2141 | loc(qualType(asString("struct A" )))))))); |
2142 | EXPECT_TRUE(matches( |
2143 | "namespace N { struct A { struct B { struct C {}; }; }; } N::A::B::C c;" , |
2144 | nestedNameSpecifierLoc(hasPrefix( |
2145 | specifiesTypeLoc(loc(qualType(asString("struct N::A" )))))))); |
2146 | } |
2147 | |
2148 | template <typename T> |
2149 | class VerifyAncestorHasChildIsEqual : public BoundNodesCallback { |
2150 | public: |
2151 | bool run(const BoundNodes *Nodes, ASTContext *Context) override { |
2152 | const T *Node = Nodes->getNodeAs<T>("" ); |
2153 | return verify(*Nodes, *Context, Node); |
2154 | } |
2155 | |
2156 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) { |
2157 | // Use the original typed pointer to verify we can pass pointers to subtypes |
2158 | // to equalsNode. |
2159 | const T *TypedNode = cast<T>(Node); |
2160 | return selectFirst<T>( |
2161 | "" , match(stmt(hasParent( |
2162 | stmt(has(stmt(equalsNode(TypedNode)))).bind("" ))), |
2163 | *Node, Context)) != nullptr; |
2164 | } |
2165 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) { |
2166 | // Use the original typed pointer to verify we can pass pointers to subtypes |
2167 | // to equalsNode. |
2168 | const T *TypedNode = cast<T>(Node); |
2169 | return selectFirst<T>( |
2170 | "" , match(decl(hasParent( |
2171 | decl(has(decl(equalsNode(TypedNode)))).bind("" ))), |
2172 | *Node, Context)) != nullptr; |
2173 | } |
2174 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Type *Node) { |
2175 | // Use the original typed pointer to verify we can pass pointers to subtypes |
2176 | // to equalsNode. |
2177 | const T *TypedNode = cast<T>(Node); |
2178 | const auto *Dec = Nodes.getNodeAs<FieldDecl>(ID: "decl" ); |
2179 | return selectFirst<T>( |
2180 | "" , match(fieldDecl(hasParent(decl(has(fieldDecl( |
2181 | hasType(type(equalsNode(TypedNode)).bind("" ))))))), |
2182 | *Dec, Context)) != nullptr; |
2183 | } |
2184 | }; |
2185 | |
2186 | TEST_P(ASTMatchersTest, IsEqualTo_MatchesNodesByIdentity) { |
2187 | EXPECT_TRUE(matchAndVerifyResultTrue( |
2188 | "void f() { if (1) if(1) {} }" , ifStmt().bind("" ), |
2189 | std::make_unique<VerifyAncestorHasChildIsEqual<IfStmt>>())); |
2190 | } |
2191 | |
2192 | TEST_P(ASTMatchersTest, IsEqualTo_MatchesNodesByIdentity_Cxx) { |
2193 | if (!GetParam().isCXX()) { |
2194 | return; |
2195 | } |
2196 | EXPECT_TRUE(matchAndVerifyResultTrue( |
2197 | "class X { class Y {}; };" , recordDecl(hasName("::X::Y" )).bind("" ), |
2198 | std::make_unique<VerifyAncestorHasChildIsEqual<CXXRecordDecl>>())); |
2199 | EXPECT_TRUE(matchAndVerifyResultTrue( |
2200 | "class X { class Y {} y; };" , |
2201 | fieldDecl(hasName("y" ), hasType(type().bind("" ))).bind("decl" ), |
2202 | std::make_unique<VerifyAncestorHasChildIsEqual<Type>>())); |
2203 | } |
2204 | |
2205 | TEST_P(ASTMatchersTest, TypedefDecl) { |
2206 | EXPECT_TRUE(matches("typedef int typedefDeclTest;" , |
2207 | typedefDecl(hasName("typedefDeclTest" )))); |
2208 | } |
2209 | |
2210 | TEST_P(ASTMatchersTest, TypedefDecl_Cxx) { |
2211 | if (!GetParam().isCXX11OrLater()) { |
2212 | return; |
2213 | } |
2214 | EXPECT_TRUE(notMatches("using typedefDeclTest = int;" , |
2215 | typedefDecl(hasName("typedefDeclTest" )))); |
2216 | } |
2217 | |
2218 | TEST_P(ASTMatchersTest, TypeAliasDecl) { |
2219 | EXPECT_TRUE(notMatches("typedef int typeAliasTest;" , |
2220 | typeAliasDecl(hasName("typeAliasTest" )))); |
2221 | } |
2222 | |
2223 | TEST_P(ASTMatchersTest, TypeAliasDecl_CXX) { |
2224 | if (!GetParam().isCXX11OrLater()) { |
2225 | return; |
2226 | } |
2227 | EXPECT_TRUE(matches("using typeAliasTest = int;" , |
2228 | typeAliasDecl(hasName("typeAliasTest" )))); |
2229 | } |
2230 | |
2231 | TEST_P(ASTMatchersTest, TypedefNameDecl) { |
2232 | EXPECT_TRUE(matches("typedef int typedefNameDeclTest1;" , |
2233 | typedefNameDecl(hasName("typedefNameDeclTest1" )))); |
2234 | } |
2235 | |
2236 | TEST_P(ASTMatchersTest, TypedefNameDecl_CXX) { |
2237 | if (!GetParam().isCXX11OrLater()) { |
2238 | return; |
2239 | } |
2240 | EXPECT_TRUE(matches("using typedefNameDeclTest = int;" , |
2241 | typedefNameDecl(hasName("typedefNameDeclTest" )))); |
2242 | } |
2243 | |
2244 | TEST_P(ASTMatchersTest, TypeAliasTemplateDecl) { |
2245 | if (!GetParam().isCXX11OrLater()) { |
2246 | return; |
2247 | } |
2248 | StringRef Code = R"( |
2249 | template <typename T> |
2250 | class X { T t; }; |
2251 | |
2252 | template <typename T> |
2253 | using typeAliasTemplateDecl = X<T>; |
2254 | |
2255 | using typeAliasDecl = X<int>; |
2256 | )" ; |
2257 | EXPECT_TRUE( |
2258 | matches(Code, typeAliasTemplateDecl(hasName("typeAliasTemplateDecl" )))); |
2259 | EXPECT_TRUE( |
2260 | notMatches(Code, typeAliasTemplateDecl(hasName("typeAliasDecl" )))); |
2261 | } |
2262 | |
2263 | TEST_P(ASTMatchersTest, QualifiedTypeLocTest_BindsToConstIntVarDecl) { |
2264 | EXPECT_TRUE(matches("const int x = 0;" , |
2265 | qualifiedTypeLoc(loc(asString("const int" ))))); |
2266 | } |
2267 | |
2268 | TEST_P(ASTMatchersTest, QualifiedTypeLocTest_BindsToConstIntFunctionDecl) { |
2269 | EXPECT_TRUE(matches("const int f() { return 5; }" , |
2270 | qualifiedTypeLoc(loc(asString("const int" ))))); |
2271 | } |
2272 | |
2273 | TEST_P(ASTMatchersTest, QualifiedTypeLocTest_DoesNotBindToUnqualifiedVarDecl) { |
2274 | EXPECT_TRUE(notMatches("int x = 0;" , qualifiedTypeLoc(loc(asString("int" ))))); |
2275 | } |
2276 | |
2277 | TEST_P(ASTMatchersTest, QualifiedTypeLocTest_IntDoesNotBindToConstIntDecl) { |
2278 | EXPECT_TRUE( |
2279 | notMatches("const int x = 0;" , qualifiedTypeLoc(loc(asString("int" ))))); |
2280 | } |
2281 | |
2282 | TEST_P(ASTMatchersTest, QualifiedTypeLocTest_IntDoesNotBindToConstFloatDecl) { |
2283 | EXPECT_TRUE( |
2284 | notMatches("const float x = 0;" , qualifiedTypeLoc(loc(asString("int" ))))); |
2285 | } |
2286 | |
2287 | TEST_P(ASTMatchersTest, PointerTypeLocTest_BindsToAnyPointerTypeLoc) { |
2288 | auto matcher = varDecl(hasName(Name: "x" ), hasTypeLoc(Inner: pointerTypeLoc())); |
2289 | EXPECT_TRUE(matches("int* x;" , matcher)); |
2290 | EXPECT_TRUE(matches("float* x;" , matcher)); |
2291 | EXPECT_TRUE(matches("char* x;" , matcher)); |
2292 | EXPECT_TRUE(matches("void* x;" , matcher)); |
2293 | } |
2294 | |
2295 | TEST_P(ASTMatchersTest, PointerTypeLocTest_DoesNotBindToNonPointerTypeLoc) { |
2296 | auto matcher = varDecl(hasName(Name: "x" ), hasTypeLoc(Inner: pointerTypeLoc())); |
2297 | EXPECT_TRUE(notMatches("int x;" , matcher)); |
2298 | EXPECT_TRUE(notMatches("float x;" , matcher)); |
2299 | EXPECT_TRUE(notMatches("char x;" , matcher)); |
2300 | } |
2301 | |
2302 | TEST_P(ASTMatchersTest, ReferenceTypeLocTest_BindsToAnyReferenceTypeLoc) { |
2303 | if (!GetParam().isCXX()) { |
2304 | return; |
2305 | } |
2306 | auto matcher = varDecl(hasName(Name: "r" ), hasTypeLoc(Inner: referenceTypeLoc())); |
2307 | EXPECT_TRUE(matches("int rr = 3; int& r = rr;" , matcher)); |
2308 | EXPECT_TRUE(matches("int rr = 3; auto& r = rr;" , matcher)); |
2309 | EXPECT_TRUE(matches("int rr = 3; const int& r = rr;" , matcher)); |
2310 | EXPECT_TRUE(matches("float rr = 3.0; float& r = rr;" , matcher)); |
2311 | EXPECT_TRUE(matches("char rr = 'a'; char& r = rr;" , matcher)); |
2312 | } |
2313 | |
2314 | TEST_P(ASTMatchersTest, ReferenceTypeLocTest_DoesNotBindToNonReferenceTypeLoc) { |
2315 | auto matcher = varDecl(hasName(Name: "r" ), hasTypeLoc(Inner: referenceTypeLoc())); |
2316 | EXPECT_TRUE(notMatches("int r;" , matcher)); |
2317 | EXPECT_TRUE(notMatches("int r = 3;" , matcher)); |
2318 | EXPECT_TRUE(notMatches("const int r = 3;" , matcher)); |
2319 | EXPECT_TRUE(notMatches("int* r;" , matcher)); |
2320 | EXPECT_TRUE(notMatches("float r;" , matcher)); |
2321 | EXPECT_TRUE(notMatches("char r;" , matcher)); |
2322 | } |
2323 | |
2324 | TEST_P(ASTMatchersTest, ReferenceTypeLocTest_BindsToAnyRvalueReferenceTypeLoc) { |
2325 | if (!GetParam().isCXX()) { |
2326 | return; |
2327 | } |
2328 | auto matcher = varDecl(hasName(Name: "r" ), hasTypeLoc(Inner: referenceTypeLoc())); |
2329 | EXPECT_TRUE(matches("int&& r = 3;" , matcher)); |
2330 | EXPECT_TRUE(matches("auto&& r = 3;" , matcher)); |
2331 | EXPECT_TRUE(matches("float&& r = 3.0;" , matcher)); |
2332 | } |
2333 | |
2334 | TEST_P(ASTMatchersTest, |
2335 | TemplateSpecializationTypeLocTest_BindsToVarDeclTemplateSpecialization) { |
2336 | if (!GetParam().isCXX()) { |
2337 | return; |
2338 | } |
2339 | EXPECT_TRUE(matches( |
2340 | "template <typename T> class C {}; C<char> var;" , |
2341 | varDecl(hasName("var" ), hasTypeLoc(elaboratedTypeLoc(hasNamedTypeLoc( |
2342 | templateSpecializationTypeLoc())))))); |
2343 | } |
2344 | |
2345 | TEST_P( |
2346 | ASTMatchersTest, |
2347 | TemplateSpecializationTypeLocTest_DoesNotBindToNonTemplateSpecialization) { |
2348 | if (!GetParam().isCXX()) { |
2349 | return; |
2350 | } |
2351 | EXPECT_TRUE(notMatches( |
2352 | "class C {}; C var;" , |
2353 | varDecl(hasName("var" ), hasTypeLoc(templateSpecializationTypeLoc())))); |
2354 | } |
2355 | |
2356 | TEST_P(ASTMatchersTest, |
2357 | ElaboratedTypeLocTest_BindsToElaboratedObjectDeclaration) { |
2358 | if (!GetParam().isCXX()) { |
2359 | return; |
2360 | } |
2361 | EXPECT_TRUE(matches("class C {}; class C c;" , |
2362 | varDecl(hasName("c" ), hasTypeLoc(elaboratedTypeLoc())))); |
2363 | } |
2364 | |
2365 | TEST_P(ASTMatchersTest, |
2366 | ElaboratedTypeLocTest_BindsToNamespaceElaboratedObjectDeclaration) { |
2367 | if (!GetParam().isCXX()) { |
2368 | return; |
2369 | } |
2370 | EXPECT_TRUE(matches("namespace N { class D {}; } N::D d;" , |
2371 | varDecl(hasName("d" ), hasTypeLoc(elaboratedTypeLoc())))); |
2372 | } |
2373 | |
2374 | TEST_P(ASTMatchersTest, |
2375 | ElaboratedTypeLocTest_BindsToElaboratedStructDeclaration) { |
2376 | EXPECT_TRUE(matches("struct s {}; struct s ss;" , |
2377 | varDecl(hasName("ss" ), hasTypeLoc(elaboratedTypeLoc())))); |
2378 | } |
2379 | |
2380 | TEST_P(ASTMatchersTest, |
2381 | ElaboratedTypeLocTest_BindsToBareElaboratedObjectDeclaration) { |
2382 | if (!GetParam().isCXX()) { |
2383 | return; |
2384 | } |
2385 | EXPECT_TRUE(matches("class C {}; C c;" , |
2386 | varDecl(hasName("c" ), hasTypeLoc(elaboratedTypeLoc())))); |
2387 | } |
2388 | |
2389 | TEST_P( |
2390 | ASTMatchersTest, |
2391 | ElaboratedTypeLocTest_DoesNotBindToNamespaceNonElaboratedObjectDeclaration) { |
2392 | if (!GetParam().isCXX()) { |
2393 | return; |
2394 | } |
2395 | EXPECT_TRUE(matches("namespace N { class D {}; } using N::D; D d;" , |
2396 | varDecl(hasName("d" ), hasTypeLoc(elaboratedTypeLoc())))); |
2397 | } |
2398 | |
2399 | TEST_P(ASTMatchersTest, |
2400 | ElaboratedTypeLocTest_BindsToBareElaboratedStructDeclaration) { |
2401 | if (!GetParam().isCXX()) { |
2402 | return; |
2403 | } |
2404 | EXPECT_TRUE(matches("struct s {}; s ss;" , |
2405 | varDecl(hasName("ss" ), hasTypeLoc(elaboratedTypeLoc())))); |
2406 | } |
2407 | |
2408 | TEST_P(ASTMatchersTest, LambdaCaptureTest) { |
2409 | if (!GetParam().isCXX11OrLater()) { |
2410 | return; |
2411 | } |
2412 | EXPECT_TRUE(matches("int main() { int cc; auto f = [cc](){ return cc; }; }" , |
2413 | lambdaExpr(hasAnyCapture(lambdaCapture())))); |
2414 | } |
2415 | |
2416 | TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureOfVarDecl) { |
2417 | if (!GetParam().isCXX11OrLater()) { |
2418 | return; |
2419 | } |
2420 | auto matcher = lambdaExpr( |
2421 | hasAnyCapture(InnerMatcher: lambdaCapture(capturesVar(InnerMatcher: varDecl(hasName(Name: "cc" )))))); |
2422 | EXPECT_TRUE(matches("int main() { int cc; auto f = [cc](){ return cc; }; }" , |
2423 | matcher)); |
2424 | EXPECT_TRUE(matches("int main() { int cc; auto f = [&cc](){ return cc; }; }" , |
2425 | matcher)); |
2426 | EXPECT_TRUE( |
2427 | matches("int main() { int cc; auto f = [=](){ return cc; }; }" , matcher)); |
2428 | EXPECT_TRUE( |
2429 | matches("int main() { int cc; auto f = [&](){ return cc; }; }" , matcher)); |
2430 | EXPECT_TRUE(matches( |
2431 | "void f(int a) { int cc[a]; auto f = [&](){ return cc;}; }" , matcher)); |
2432 | } |
2433 | |
2434 | TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureWithInitializer) { |
2435 | if (!GetParam().isCXX14OrLater()) { |
2436 | return; |
2437 | } |
2438 | auto matcher = lambdaExpr(hasAnyCapture(InnerMatcher: lambdaCapture(capturesVar( |
2439 | InnerMatcher: varDecl(hasName(Name: "cc" ), hasInitializer(InnerMatcher: integerLiteral(equals(Value: 1)))))))); |
2440 | EXPECT_TRUE( |
2441 | matches("int main() { auto lambda = [cc = 1] {return cc;}; }" , matcher)); |
2442 | EXPECT_TRUE( |
2443 | matches("int main() { int cc = 2; auto lambda = [cc = 1] {return cc;}; }" , |
2444 | matcher)); |
2445 | } |
2446 | |
2447 | TEST_P(ASTMatchersTest, LambdaCaptureTest_DoesNotBindToCaptureOfVarDecl) { |
2448 | if (!GetParam().isCXX11OrLater()) { |
2449 | return; |
2450 | } |
2451 | auto matcher = lambdaExpr( |
2452 | hasAnyCapture(InnerMatcher: lambdaCapture(capturesVar(InnerMatcher: varDecl(hasName(Name: "cc" )))))); |
2453 | EXPECT_FALSE(matches("int main() { auto f = [](){ return 5; }; }" , matcher)); |
2454 | EXPECT_FALSE(matches("int main() { int xx; auto f = [xx](){ return xx; }; }" , |
2455 | matcher)); |
2456 | } |
2457 | |
2458 | TEST_P(ASTMatchersTest, |
2459 | LambdaCaptureTest_DoesNotBindToCaptureWithInitializerAndDifferentName) { |
2460 | if (!GetParam().isCXX14OrLater()) { |
2461 | return; |
2462 | } |
2463 | EXPECT_FALSE(matches( |
2464 | "int main() { auto lambda = [xx = 1] {return xx;}; }" , |
2465 | lambdaExpr(hasAnyCapture(lambdaCapture(capturesVar(varDecl( |
2466 | hasName("cc" ), hasInitializer(integerLiteral(equals(1)))))))))); |
2467 | } |
2468 | |
2469 | TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureOfReferenceType) { |
2470 | if (!GetParam().isCXX20OrLater()) { |
2471 | return; |
2472 | } |
2473 | auto matcher = lambdaExpr(hasAnyCapture( |
2474 | InnerMatcher: lambdaCapture(capturesVar(InnerMatcher: varDecl(hasType(InnerMatcher: referenceType())))))); |
2475 | EXPECT_TRUE(matches("template <class ...T> void f(T &...args) {" |
2476 | " [&...args = args] () mutable {" |
2477 | " }();" |
2478 | "}" |
2479 | "int main() {" |
2480 | " int a;" |
2481 | " f(a);" |
2482 | "}" , matcher)); |
2483 | EXPECT_FALSE(matches("template <class ...T> void f(T &...args) {" |
2484 | " [...args = args] () mutable {" |
2485 | " }();" |
2486 | "}" |
2487 | "int main() {" |
2488 | " int a;" |
2489 | " f(a);" |
2490 | "}" , matcher)); |
2491 | } |
2492 | |
2493 | TEST_P(ASTMatchersTest, IsDerivedFromRecursion) { |
2494 | if (!GetParam().isCXX11OrLater()) |
2495 | return; |
2496 | |
2497 | // Check we don't crash on cycles in the traversal and inheritance hierarchy. |
2498 | // Clang will normally enforce there are no cycles, but matchers opted to |
2499 | // traverse primary template for dependent specializations, spuriously |
2500 | // creating the cycles. |
2501 | DeclarationMatcher matcher = cxxRecordDecl(isDerivedFrom(BaseName: "X" )); |
2502 | EXPECT_TRUE(notMatches(R"cpp( |
2503 | template <typename T1, typename T2> |
2504 | struct M; |
2505 | |
2506 | template <typename T1> |
2507 | struct M<T1, void> {}; |
2508 | |
2509 | template <typename T1, typename T2> |
2510 | struct L : M<T1, T2> {}; |
2511 | |
2512 | template <typename T1, typename T2> |
2513 | struct M : L<M<T1, T2>, M<T1, T2>> {}; |
2514 | )cpp" , |
2515 | matcher)); |
2516 | |
2517 | // Check the running time is not exponential. The number of subojects to |
2518 | // traverse grows as fibonacci numbers even though the number of bases to |
2519 | // traverse is quadratic. |
2520 | // The test will hang if implementation of matchers traverses all subojects. |
2521 | EXPECT_TRUE(notMatches(R"cpp( |
2522 | template <class T> struct A0 {}; |
2523 | template <class T> struct A1 : A0<T> {}; |
2524 | template <class T> struct A2 : A1<T>, A0<T> {}; |
2525 | template <class T> struct A3 : A2<T>, A1<T> {}; |
2526 | template <class T> struct A4 : A3<T>, A2<T> {}; |
2527 | template <class T> struct A5 : A4<T>, A3<T> {}; |
2528 | template <class T> struct A6 : A5<T>, A4<T> {}; |
2529 | template <class T> struct A7 : A6<T>, A5<T> {}; |
2530 | template <class T> struct A8 : A7<T>, A6<T> {}; |
2531 | template <class T> struct A9 : A8<T>, A7<T> {}; |
2532 | template <class T> struct A10 : A9<T>, A8<T> {}; |
2533 | template <class T> struct A11 : A10<T>, A9<T> {}; |
2534 | template <class T> struct A12 : A11<T>, A10<T> {}; |
2535 | template <class T> struct A13 : A12<T>, A11<T> {}; |
2536 | template <class T> struct A14 : A13<T>, A12<T> {}; |
2537 | template <class T> struct A15 : A14<T>, A13<T> {}; |
2538 | template <class T> struct A16 : A15<T>, A14<T> {}; |
2539 | template <class T> struct A17 : A16<T>, A15<T> {}; |
2540 | template <class T> struct A18 : A17<T>, A16<T> {}; |
2541 | template <class T> struct A19 : A18<T>, A17<T> {}; |
2542 | template <class T> struct A20 : A19<T>, A18<T> {}; |
2543 | template <class T> struct A21 : A20<T>, A19<T> {}; |
2544 | template <class T> struct A22 : A21<T>, A20<T> {}; |
2545 | template <class T> struct A23 : A22<T>, A21<T> {}; |
2546 | template <class T> struct A24 : A23<T>, A22<T> {}; |
2547 | template <class T> struct A25 : A24<T>, A23<T> {}; |
2548 | template <class T> struct A26 : A25<T>, A24<T> {}; |
2549 | template <class T> struct A27 : A26<T>, A25<T> {}; |
2550 | template <class T> struct A28 : A27<T>, A26<T> {}; |
2551 | template <class T> struct A29 : A28<T>, A27<T> {}; |
2552 | template <class T> struct A30 : A29<T>, A28<T> {}; |
2553 | template <class T> struct A31 : A30<T>, A29<T> {}; |
2554 | template <class T> struct A32 : A31<T>, A30<T> {}; |
2555 | template <class T> struct A33 : A32<T>, A31<T> {}; |
2556 | template <class T> struct A34 : A33<T>, A32<T> {}; |
2557 | template <class T> struct A35 : A34<T>, A33<T> {}; |
2558 | template <class T> struct A36 : A35<T>, A34<T> {}; |
2559 | template <class T> struct A37 : A36<T>, A35<T> {}; |
2560 | template <class T> struct A38 : A37<T>, A36<T> {}; |
2561 | template <class T> struct A39 : A38<T>, A37<T> {}; |
2562 | template <class T> struct A40 : A39<T>, A38<T> {}; |
2563 | )cpp" , |
2564 | matcher)); |
2565 | } |
2566 | |
2567 | TEST(ASTMatchersTestObjC, ObjCMessageCalees) { |
2568 | StatementMatcher MessagingFoo = |
2569 | objcMessageExpr(callee(InnerMatcher: objcMethodDecl(hasName(Name: "foo" )))); |
2570 | |
2571 | EXPECT_TRUE(matchesObjC("@interface I" |
2572 | "+ (void)foo;" |
2573 | "@end\n" |
2574 | "int main() {" |
2575 | " [I foo];" |
2576 | "}" , |
2577 | MessagingFoo)); |
2578 | EXPECT_TRUE(notMatchesObjC("@interface I" |
2579 | "+ (void)foo;" |
2580 | "+ (void)bar;" |
2581 | "@end\n" |
2582 | "int main() {" |
2583 | " [I bar];" |
2584 | "}" , |
2585 | MessagingFoo)); |
2586 | EXPECT_TRUE(matchesObjC("@interface I" |
2587 | "- (void)foo;" |
2588 | "- (void)bar;" |
2589 | "@end\n" |
2590 | "int main() {" |
2591 | " I *i;" |
2592 | " [i foo];" |
2593 | "}" , |
2594 | MessagingFoo)); |
2595 | EXPECT_TRUE(notMatchesObjC("@interface I" |
2596 | "- (void)foo;" |
2597 | "- (void)bar;" |
2598 | "@end\n" |
2599 | "int main() {" |
2600 | " I *i;" |
2601 | " [i bar];" |
2602 | "}" , |
2603 | MessagingFoo)); |
2604 | } |
2605 | |
2606 | TEST(ASTMatchersTestObjC, ObjCMessageExpr) { |
2607 | // Don't find ObjCMessageExpr where none are present. |
2608 | EXPECT_TRUE(notMatchesObjC("" , objcMessageExpr(anything()))); |
2609 | |
2610 | StringRef Objc1String = "@interface Str " |
2611 | " - (Str *)uppercaseString;" |
2612 | "@end " |
2613 | "@interface foo " |
2614 | "- (void)contents;" |
2615 | "- (void)meth:(Str *)text;" |
2616 | "@end " |
2617 | " " |
2618 | "@implementation foo " |
2619 | "- (void) meth:(Str *)text { " |
2620 | " [self contents];" |
2621 | " Str *up = [text uppercaseString];" |
2622 | "} " |
2623 | "@end " ; |
2624 | EXPECT_TRUE(matchesObjC(Objc1String, objcMessageExpr(anything()))); |
2625 | EXPECT_TRUE(matchesObjC(Objc1String, |
2626 | objcMessageExpr(hasAnySelector({"contents" , "meth:" })) |
2627 | |
2628 | )); |
2629 | EXPECT_TRUE( |
2630 | matchesObjC(Objc1String, objcMessageExpr(hasSelector("contents" )))); |
2631 | EXPECT_TRUE(matchesObjC( |
2632 | Objc1String, objcMessageExpr(hasAnySelector("contents" , "contentsA" )))); |
2633 | EXPECT_FALSE(matchesObjC( |
2634 | Objc1String, objcMessageExpr(hasAnySelector("contentsB" , "contentsC" )))); |
2635 | EXPECT_TRUE( |
2636 | matchesObjC(Objc1String, objcMessageExpr(matchesSelector("cont*" )))); |
2637 | EXPECT_FALSE( |
2638 | matchesObjC(Objc1String, objcMessageExpr(matchesSelector("?cont*" )))); |
2639 | EXPECT_TRUE( |
2640 | notMatchesObjC(Objc1String, objcMessageExpr(hasSelector("contents" ), |
2641 | hasNullSelector()))); |
2642 | EXPECT_TRUE(matchesObjC(Objc1String, objcMessageExpr(hasSelector("contents" ), |
2643 | hasUnarySelector()))); |
2644 | EXPECT_TRUE(matchesObjC(Objc1String, objcMessageExpr(hasSelector("contents" ), |
2645 | numSelectorArgs(0)))); |
2646 | EXPECT_TRUE( |
2647 | matchesObjC(Objc1String, objcMessageExpr(matchesSelector("uppercase*" ), |
2648 | argumentCountIs(0)))); |
2649 | } |
2650 | |
2651 | TEST(ASTMatchersTestObjC, ObjCStringLiteral) { |
2652 | |
2653 | StringRef Objc1String = "@interface NSObject " |
2654 | "@end " |
2655 | "@interface NSString " |
2656 | "@end " |
2657 | "@interface Test : NSObject " |
2658 | "+ (void)someFunction:(NSString *)Desc; " |
2659 | "@end " |
2660 | "@implementation Test " |
2661 | "+ (void)someFunction:(NSString *)Desc { " |
2662 | " return; " |
2663 | "} " |
2664 | "- (void) foo { " |
2665 | " [Test someFunction:@\"Ola!\"]; " |
2666 | "}\n" |
2667 | "@end " ; |
2668 | EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral())); |
2669 | } |
2670 | |
2671 | TEST(ASTMatchersTestObjC, ObjCDecls) { |
2672 | StringRef ObjCString = "@protocol Proto " |
2673 | "- (void)protoDidThing; " |
2674 | "@end " |
2675 | "@interface Thing " |
2676 | "@property int enabled; " |
2677 | "@end " |
2678 | "@interface Thing (ABC) " |
2679 | "- (void)abc_doThing; " |
2680 | "@end " |
2681 | "@implementation Thing " |
2682 | "{ id _ivar; } " |
2683 | "- (void)anything {} " |
2684 | "@end " |
2685 | "@implementation Thing (ABC) " |
2686 | "- (void)abc_doThing {} " |
2687 | "@end " ; |
2688 | |
2689 | EXPECT_TRUE(matchesObjC(ObjCString, objcProtocolDecl(hasName("Proto" )))); |
2690 | EXPECT_TRUE( |
2691 | matchesObjC(ObjCString, objcImplementationDecl(hasName("Thing" )))); |
2692 | EXPECT_TRUE(matchesObjC(ObjCString, objcCategoryDecl(hasName("ABC" )))); |
2693 | EXPECT_TRUE(matchesObjC(ObjCString, objcCategoryImplDecl(hasName("ABC" )))); |
2694 | EXPECT_TRUE( |
2695 | matchesObjC(ObjCString, objcMethodDecl(hasName("protoDidThing" )))); |
2696 | EXPECT_TRUE(matchesObjC(ObjCString, objcMethodDecl(hasName("abc_doThing" )))); |
2697 | EXPECT_TRUE(matchesObjC(ObjCString, objcMethodDecl(hasName("anything" )))); |
2698 | EXPECT_TRUE(matchesObjC(ObjCString, objcIvarDecl(hasName("_ivar" )))); |
2699 | EXPECT_TRUE(matchesObjC(ObjCString, objcPropertyDecl(hasName("enabled" )))); |
2700 | } |
2701 | |
2702 | TEST(ASTMatchersTestObjC, ObjCExceptionStmts) { |
2703 | StringRef ObjCString = "void f(id obj) {" |
2704 | " @try {" |
2705 | " @throw obj;" |
2706 | " } @catch (...) {" |
2707 | " } @finally {}" |
2708 | "}" ; |
2709 | |
2710 | EXPECT_TRUE(matchesObjC(ObjCString, objcTryStmt())); |
2711 | EXPECT_TRUE(matchesObjC(ObjCString, objcThrowStmt())); |
2712 | EXPECT_TRUE(matchesObjC(ObjCString, objcCatchStmt())); |
2713 | EXPECT_TRUE(matchesObjC(ObjCString, objcFinallyStmt())); |
2714 | } |
2715 | |
2716 | TEST(ASTMatchersTest, DecompositionDecl) { |
2717 | StringRef Code = R"cpp( |
2718 | void foo() |
2719 | { |
2720 | int arr[3]; |
2721 | auto &[f, s, t] = arr; |
2722 | |
2723 | f = 42; |
2724 | } |
2725 | )cpp" ; |
2726 | EXPECT_TRUE(matchesConditionally( |
2727 | Code, decompositionDecl(hasBinding(0, bindingDecl(hasName("f" )))), true, |
2728 | {"-std=c++17" })); |
2729 | EXPECT_FALSE(matchesConditionally( |
2730 | Code, decompositionDecl(hasBinding(42, bindingDecl(hasName("f" )))), true, |
2731 | {"-std=c++17" })); |
2732 | EXPECT_FALSE(matchesConditionally( |
2733 | Code, decompositionDecl(hasBinding(0, bindingDecl(hasName("s" )))), true, |
2734 | {"-std=c++17" })); |
2735 | EXPECT_TRUE(matchesConditionally( |
2736 | Code, decompositionDecl(hasBinding(1, bindingDecl(hasName("s" )))), true, |
2737 | {"-std=c++17" })); |
2738 | |
2739 | EXPECT_TRUE(matchesConditionally( |
2740 | Code, |
2741 | bindingDecl(decl().bind("self" ), hasName("f" ), |
2742 | forDecomposition(decompositionDecl( |
2743 | hasAnyBinding(bindingDecl(equalsBoundNode("self" )))))), |
2744 | true, {"-std=c++17" })); |
2745 | } |
2746 | |
2747 | TEST(ASTMatchersTestObjC, ObjCAutoreleasePoolStmt) { |
2748 | StringRef ObjCString = "void f() {" |
2749 | "@autoreleasepool {" |
2750 | " int x = 1;" |
2751 | "}" |
2752 | "}" ; |
2753 | EXPECT_TRUE(matchesObjC(ObjCString, autoreleasePoolStmt())); |
2754 | StringRef ObjCStringNoPool = "void f() { int x = 1; }" ; |
2755 | EXPECT_FALSE(matchesObjC(ObjCStringNoPool, autoreleasePoolStmt())); |
2756 | } |
2757 | |
2758 | TEST(ASTMatchersTestOpenMP, OMPExecutableDirective) { |
2759 | auto Matcher = stmt(ompExecutableDirective()); |
2760 | |
2761 | StringRef Source0 = R"( |
2762 | void x() { |
2763 | #pragma omp parallel |
2764 | ; |
2765 | })" ; |
2766 | EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher)); |
2767 | |
2768 | StringRef Source1 = R"( |
2769 | void x() { |
2770 | #pragma omp taskyield |
2771 | ; |
2772 | })" ; |
2773 | EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); |
2774 | |
2775 | StringRef Source2 = R"( |
2776 | void x() { |
2777 | ; |
2778 | })" ; |
2779 | EXPECT_TRUE(notMatchesWithOpenMP(Source2, Matcher)); |
2780 | } |
2781 | |
2782 | TEST(ASTMatchersTestOpenMP, OMPDefaultClause) { |
2783 | auto Matcher = ompExecutableDirective(hasAnyClause(InnerMatcher: ompDefaultClause())); |
2784 | |
2785 | StringRef Source0 = R"( |
2786 | void x() { |
2787 | ; |
2788 | })" ; |
2789 | EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
2790 | |
2791 | StringRef Source1 = R"( |
2792 | void x() { |
2793 | #pragma omp parallel |
2794 | ; |
2795 | })" ; |
2796 | EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
2797 | |
2798 | StringRef Source2 = R"( |
2799 | void x() { |
2800 | #pragma omp parallel default(none) |
2801 | ; |
2802 | })" ; |
2803 | EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); |
2804 | |
2805 | StringRef Source3 = R"( |
2806 | void x() { |
2807 | #pragma omp parallel default(shared) |
2808 | ; |
2809 | })" ; |
2810 | EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); |
2811 | |
2812 | StringRef Source4 = R"( |
2813 | void x() { |
2814 | #pragma omp parallel default(firstprivate) |
2815 | ; |
2816 | })" ; |
2817 | EXPECT_TRUE(matchesWithOpenMP51(Source4, Matcher)); |
2818 | |
2819 | StringRef Source5 = R"( |
2820 | void x(int x) { |
2821 | #pragma omp parallel num_threads(x) |
2822 | ; |
2823 | })" ; |
2824 | EXPECT_TRUE(notMatchesWithOpenMP(Source5, Matcher)); |
2825 | } |
2826 | |
2827 | TEST(ASTMatchersTest, Finder_DynamicOnlyAcceptsSomeMatchers) { |
2828 | MatchFinder Finder; |
2829 | EXPECT_TRUE(Finder.addDynamicMatcher(decl(), nullptr)); |
2830 | EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), nullptr)); |
2831 | EXPECT_TRUE( |
2832 | Finder.addDynamicMatcher(constantArrayType(hasSize(42)), nullptr)); |
2833 | |
2834 | // Do not accept non-toplevel matchers. |
2835 | EXPECT_FALSE(Finder.addDynamicMatcher(isMain(), nullptr)); |
2836 | EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x" ), nullptr)); |
2837 | } |
2838 | |
2839 | TEST(MatchFinderAPI, MatchesDynamic) { |
2840 | StringRef SourceCode = "struct A { void f() {} };" ; |
2841 | auto Matcher = functionDecl(isDefinition()).bind(ID: "method" ); |
2842 | |
2843 | auto astUnit = tooling::buildASTFromCode(Code: SourceCode); |
2844 | |
2845 | auto GlobalBoundNodes = matchDynamic(Matcher, Context&: astUnit->getASTContext()); |
2846 | |
2847 | EXPECT_EQ(GlobalBoundNodes.size(), 1u); |
2848 | EXPECT_EQ(GlobalBoundNodes[0].getMap().size(), 1u); |
2849 | |
2850 | auto GlobalMethodNode = GlobalBoundNodes[0].getNodeAs<FunctionDecl>(ID: "method" ); |
2851 | EXPECT_TRUE(GlobalMethodNode != nullptr); |
2852 | |
2853 | auto MethodBoundNodes = |
2854 | matchDynamic(Matcher, Node: *GlobalMethodNode, Context&: astUnit->getASTContext()); |
2855 | EXPECT_EQ(MethodBoundNodes.size(), 1u); |
2856 | EXPECT_EQ(MethodBoundNodes[0].getMap().size(), 1u); |
2857 | |
2858 | auto MethodNode = MethodBoundNodes[0].getNodeAs<FunctionDecl>(ID: "method" ); |
2859 | EXPECT_EQ(MethodNode, GlobalMethodNode); |
2860 | } |
2861 | |
2862 | static std::vector<TestClangConfig> allTestClangConfigs() { |
2863 | std::vector<TestClangConfig> all_configs; |
2864 | for (TestLanguage lang : { |
2865 | #define TESTLANGUAGE(lang, version, std_flag, version_index) \ |
2866 | Lang_##lang##version, |
2867 | #include "clang/Testing/TestLanguage.def" |
2868 | }) { |
2869 | TestClangConfig config; |
2870 | config.Language = lang; |
2871 | |
2872 | // Use an unknown-unknown triple so we don't instantiate the full system |
2873 | // toolchain. On Linux, instantiating the toolchain involves stat'ing |
2874 | // large portions of /usr/lib, and this slows down not only this test, but |
2875 | // all other tests, via contention in the kernel. |
2876 | // |
2877 | // FIXME: This is a hack to work around the fact that there's no way to do |
2878 | // the equivalent of runToolOnCodeWithArgs without instantiating a full |
2879 | // Driver. We should consider having a function, at least for tests, that |
2880 | // invokes cc1. |
2881 | config.Target = "i386-unknown-unknown" ; |
2882 | all_configs.push_back(x: config); |
2883 | |
2884 | // Windows target is interesting to test because it enables |
2885 | // `-fdelayed-template-parsing`. |
2886 | config.Target = "x86_64-pc-win32-msvc" ; |
2887 | all_configs.push_back(x: config); |
2888 | } |
2889 | return all_configs; |
2890 | } |
2891 | |
2892 | INSTANTIATE_TEST_SUITE_P( |
2893 | ASTMatchersTests, ASTMatchersTest, testing::ValuesIn(allTestClangConfigs()), |
2894 | [](const testing::TestParamInfo<TestClangConfig> &Info) { |
2895 | return Info.param.toShortString(); |
2896 | }); |
2897 | |
2898 | } // namespace ast_matchers |
2899 | } // namespace clang |
2900 | |