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