1 //===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements matchers to be used together with the MatchFinder to 11 // match AST nodes. 12 // 13 // Matchers are created by generator functions, which can be combined in 14 // a functional in-language DSL to express queries over the C++ AST. 15 // 16 // For example, to match a class with a certain name, one would call: 17 // recordDecl(hasName("MyClass")) 18 // which returns a matcher that can be used to find all AST nodes that declare 19 // a class named 'MyClass'. 20 // 21 // For more complicated match expressions we're often interested in accessing 22 // multiple parts of the matched AST nodes once a match is found. In that case, 23 // use the id(...) matcher around the match expressions that match the nodes 24 // you want to access. 25 // 26 // For example, when we're interested in child classes of a certain class, we 27 // would write: 28 // recordDecl(hasName("MyClass"), hasChild(id("child", recordDecl()))) 29 // When the match is found via the MatchFinder, a user provided callback will 30 // be called with a BoundNodes instance that contains a mapping from the 31 // strings that we provided for the id(...) calls to the nodes that were 32 // matched. 33 // In the given example, each time our matcher finds a match we get a callback 34 // where "child" is bound to the CXXRecordDecl node of the matching child 35 // class declaration. 36 // 37 // See ASTMatchersInternal.h for a more in-depth explanation of the 38 // implementation details of the matcher framework. 39 // 40 // See ASTMatchFinder.h for how to use the generated matchers to run over 41 // an AST. 42 // 43 //===----------------------------------------------------------------------===// 44 45 #ifndef LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 46 #define LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 47 48 #include "clang/AST/DeclFriend.h" 49 #include "clang/AST/DeclTemplate.h" 50 #include "clang/ASTMatchers/ASTMatchersInternal.h" 51 #include "clang/ASTMatchers/ASTMatchersMacros.h" 52 #include "llvm/ADT/Twine.h" 53 #include "llvm/Support/Regex.h" 54 #include <iterator> 55 56 namespace clang { 57 namespace ast_matchers { 58 59 /// \brief Maps string IDs to AST nodes matched by parts of a matcher. 60 /// 61 /// The bound nodes are generated by calling \c bind("id") on the node matchers 62 /// of the nodes we want to access later. 63 /// 64 /// The instances of BoundNodes are created by \c MatchFinder when the user's 65 /// callbacks are executed every time a match is found. 66 class BoundNodes { 67 public: 68 /// \brief Returns the AST node bound to \c ID. 69 /// 70 /// Returns NULL if there was no node bound to \c ID or if there is a node but 71 /// it cannot be converted to the specified type. 72 template <typename T> 73 const T *getNodeAs(StringRef ID) const { 74 return MyBoundNodes.getNodeAs<T>(ID); 75 } 76 77 /// \brief Deprecated. Please use \c getNodeAs instead. 78 /// @{ 79 template <typename T> 80 const T *getDeclAs(StringRef ID) const { 81 return getNodeAs<T>(ID); 82 } 83 template <typename T> 84 const T *getStmtAs(StringRef ID) const { 85 return getNodeAs<T>(ID); 86 } 87 /// @} 88 89 private: 90 /// \brief Create BoundNodes from a pre-filled map of bindings. 91 BoundNodes(internal::BoundNodesMap &MyBoundNodes) 92 : MyBoundNodes(MyBoundNodes) {} 93 94 internal::BoundNodesMap MyBoundNodes; 95 96 friend class internal::BoundNodesTreeBuilder; 97 }; 98 99 /// \brief If the provided matcher matches a node, binds the node to \c ID. 100 /// 101 /// FIXME: Do we want to support this now that we have bind()? 102 template <typename T> 103 internal::Matcher<T> id(const std::string &ID, 104 const internal::BindableMatcher<T> &InnerMatcher) { 105 return InnerMatcher.bind(ID); 106 } 107 108 /// \brief Types of matchers for the top-level classes in the AST class 109 /// hierarchy. 110 /// @{ 111 typedef internal::Matcher<Decl> DeclarationMatcher; 112 typedef internal::Matcher<Stmt> StatementMatcher; 113 typedef internal::Matcher<QualType> TypeMatcher; 114 typedef internal::Matcher<TypeLoc> TypeLocMatcher; 115 typedef internal::Matcher<NestedNameSpecifier> NestedNameSpecifierMatcher; 116 typedef internal::Matcher<NestedNameSpecifierLoc> NestedNameSpecifierLocMatcher; 117 /// @} 118 119 /// \brief Matches any node. 120 /// 121 /// Useful when another matcher requires a child matcher, but there's no 122 /// additional constraint. This will often be used with an explicit conversion 123 /// to an \c internal::Matcher<> type such as \c TypeMatcher. 124 /// 125 /// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g., 126 /// \code 127 /// "int* p" and "void f()" in 128 /// int* p; 129 /// void f(); 130 /// \endcode 131 /// 132 /// Usable as: Any Matcher 133 inline internal::PolymorphicMatcherWithParam0<internal::TrueMatcher> 134 anything() { 135 return internal::PolymorphicMatcherWithParam0<internal::TrueMatcher>(); 136 } 137 138 /// \brief Matches declarations. 139 /// 140 /// Examples matches \c X, \c C, and the friend declaration inside \c C; 141 /// \code 142 /// void X(); 143 /// class C { 144 /// friend X; 145 /// }; 146 /// \endcode 147 const internal::VariadicAllOfMatcher<Decl> decl; 148 149 /// \brief Matches a declaration of anything that could have a name. 150 /// 151 /// Example matches \c X, \c S, the anonymous union type, \c i, and \c U; 152 /// \code 153 /// typedef int X; 154 /// struct S { 155 /// union { 156 /// int i; 157 /// } U; 158 /// }; 159 /// \endcode 160 const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl; 161 162 /// \brief Matches a declaration of a namespace. 163 /// 164 /// Given 165 /// \code 166 /// namespace {} 167 /// namespace test {} 168 /// \endcode 169 /// namespaceDecl() 170 /// matches "namespace {}" and "namespace test {}" 171 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> namespaceDecl; 172 173 /// \brief Matches C++ class declarations. 174 /// 175 /// Example matches \c X, \c Z 176 /// \code 177 /// class X; 178 /// template<class T> class Z {}; 179 /// \endcode 180 const internal::VariadicDynCastAllOfMatcher< 181 Decl, 182 CXXRecordDecl> recordDecl; 183 184 /// \brief Matches C++ class template declarations. 185 /// 186 /// Example matches \c Z 187 /// \code 188 /// template<class T> class Z {}; 189 /// \endcode 190 const internal::VariadicDynCastAllOfMatcher< 191 Decl, 192 ClassTemplateDecl> classTemplateDecl; 193 194 /// \brief Matches C++ class template specializations. 195 /// 196 /// Given 197 /// \code 198 /// template<typename T> class A {}; 199 /// template<> class A<double> {}; 200 /// A<int> a; 201 /// \endcode 202 /// classTemplateSpecializationDecl() 203 /// matches the specializations \c A<int> and \c A<double> 204 const internal::VariadicDynCastAllOfMatcher< 205 Decl, 206 ClassTemplateSpecializationDecl> classTemplateSpecializationDecl; 207 208 /// \brief Matches declarator declarations (field, variable, function 209 /// and non-type template parameter declarations). 210 /// 211 /// Given 212 /// \code 213 /// class X { int y; }; 214 /// \endcode 215 /// declaratorDecl() 216 /// matches \c int y. 217 const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl> 218 declaratorDecl; 219 220 /// \brief Matches parameter variable declarations. 221 /// 222 /// Given 223 /// \code 224 /// void f(int x); 225 /// \endcode 226 /// parmVarDecl() 227 /// matches \c int x. 228 const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> parmVarDecl; 229 230 /// \brief Matches C++ access specifier declarations. 231 /// 232 /// Given 233 /// \code 234 /// class C { 235 /// public: 236 /// int a; 237 /// }; 238 /// \endcode 239 /// accessSpecDecl() 240 /// matches 'public:' 241 const internal::VariadicDynCastAllOfMatcher< 242 Decl, 243 AccessSpecDecl> accessSpecDecl; 244 245 /// \brief Matches constructor initializers. 246 /// 247 /// Examples matches \c i(42). 248 /// \code 249 /// class C { 250 /// C() : i(42) {} 251 /// int i; 252 /// }; 253 /// \endcode 254 const internal::VariadicAllOfMatcher<CXXCtorInitializer> ctorInitializer; 255 256 /// \brief Matches public C++ declarations. 257 /// 258 /// Given 259 /// \code 260 /// class C { 261 /// public: int a; 262 /// protected: int b; 263 /// private: int c; 264 /// }; 265 /// \endcode 266 /// fieldDecl(isPublic()) 267 /// matches 'int a;' 268 AST_MATCHER(Decl, isPublic) { 269 return Node.getAccess() == AS_public; 270 } 271 272 /// \brief Matches protected C++ declarations. 273 /// 274 /// Given 275 /// \code 276 /// class C { 277 /// public: int a; 278 /// protected: int b; 279 /// private: int c; 280 /// }; 281 /// \endcode 282 /// fieldDecl(isProtected()) 283 /// matches 'int b;' 284 AST_MATCHER(Decl, isProtected) { 285 return Node.getAccess() == AS_protected; 286 } 287 288 /// \brief Matches private C++ declarations. 289 /// 290 /// Given 291 /// \code 292 /// class C { 293 /// public: int a; 294 /// protected: int b; 295 /// private: int c; 296 /// }; 297 /// \endcode 298 /// fieldDecl(isPrivate()) 299 /// matches 'int c;' 300 AST_MATCHER(Decl, isPrivate) { 301 return Node.getAccess() == AS_private; 302 } 303 304 /// \brief Matches classTemplateSpecializations that have at least one 305 /// TemplateArgument matching the given InnerMatcher. 306 /// 307 /// Given 308 /// \code 309 /// template<typename T> class A {}; 310 /// template<> class A<double> {}; 311 /// A<int> a; 312 /// \endcode 313 /// classTemplateSpecializationDecl(hasAnyTemplateArgument( 314 /// refersToType(asString("int")))) 315 /// matches the specialization \c A<int> 316 AST_MATCHER_P(ClassTemplateSpecializationDecl, hasAnyTemplateArgument, 317 internal::Matcher<TemplateArgument>, InnerMatcher) { 318 llvm::ArrayRef<TemplateArgument> List = Node.getTemplateArgs().asArray(); 319 return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder, 320 Builder); 321 } 322 323 /// \brief Matches expressions that match InnerMatcher after any implicit casts 324 /// are stripped off. 325 /// 326 /// Parentheses and explicit casts are not discarded. 327 /// Given 328 /// \code 329 /// int arr[5]; 330 /// int a = 0; 331 /// char b = 0; 332 /// const int c = a; 333 /// int *d = arr; 334 /// long e = (long) 0l; 335 /// \endcode 336 /// The matchers 337 /// \code 338 /// varDecl(hasInitializer(ignoringImpCasts(integerLiteral()))) 339 /// varDecl(hasInitializer(ignoringImpCasts(declRefExpr()))) 340 /// \endcode 341 /// would match the declarations for a, b, c, and d, but not e. 342 /// While 343 /// \code 344 /// varDecl(hasInitializer(integerLiteral())) 345 /// varDecl(hasInitializer(declRefExpr())) 346 /// \endcode 347 /// only match the declarations for b, c, and d. 348 AST_MATCHER_P(Expr, ignoringImpCasts, 349 internal::Matcher<Expr>, InnerMatcher) { 350 return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder); 351 } 352 353 /// \brief Matches expressions that match InnerMatcher after parentheses and 354 /// casts are stripped off. 355 /// 356 /// Implicit and non-C Style casts are also discarded. 357 /// Given 358 /// \code 359 /// int a = 0; 360 /// char b = (0); 361 /// void* c = reinterpret_cast<char*>(0); 362 /// char d = char(0); 363 /// \endcode 364 /// The matcher 365 /// varDecl(hasInitializer(ignoringParenCasts(integerLiteral()))) 366 /// would match the declarations for a, b, c, and d. 367 /// while 368 /// varDecl(hasInitializer(integerLiteral())) 369 /// only match the declaration for a. 370 AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) { 371 return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder); 372 } 373 374 /// \brief Matches expressions that match InnerMatcher after implicit casts and 375 /// parentheses are stripped off. 376 /// 377 /// Explicit casts are not discarded. 378 /// Given 379 /// \code 380 /// int arr[5]; 381 /// int a = 0; 382 /// char b = (0); 383 /// const int c = a; 384 /// int *d = (arr); 385 /// long e = ((long) 0l); 386 /// \endcode 387 /// The matchers 388 /// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral()))) 389 /// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr()))) 390 /// would match the declarations for a, b, c, and d, but not e. 391 /// while 392 /// varDecl(hasInitializer(integerLiteral())) 393 /// varDecl(hasInitializer(declRefExpr())) 394 /// would only match the declaration for a. 395 AST_MATCHER_P(Expr, ignoringParenImpCasts, 396 internal::Matcher<Expr>, InnerMatcher) { 397 return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder); 398 } 399 400 /// \brief Matches classTemplateSpecializations where the n'th TemplateArgument 401 /// matches the given InnerMatcher. 402 /// 403 /// Given 404 /// \code 405 /// template<typename T, typename U> class A {}; 406 /// A<bool, int> b; 407 /// A<int, bool> c; 408 /// \endcode 409 /// classTemplateSpecializationDecl(hasTemplateArgument( 410 /// 1, refersToType(asString("int")))) 411 /// matches the specialization \c A<bool, int> 412 AST_MATCHER_P2(ClassTemplateSpecializationDecl, hasTemplateArgument, 413 unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) { 414 const TemplateArgumentList &List = Node.getTemplateArgs(); 415 if (List.size() <= N) 416 return false; 417 return InnerMatcher.matches(List.get(N), Finder, Builder); 418 } 419 420 /// \brief Matches a TemplateArgument that refers to a certain type. 421 /// 422 /// Given 423 /// \code 424 /// struct X {}; 425 /// template<typename T> struct A {}; 426 /// A<X> a; 427 /// \endcode 428 /// classTemplateSpecializationDecl(hasAnyTemplateArgument( 429 /// refersToType(class(hasName("X"))))) 430 /// matches the specialization \c A<X> 431 AST_MATCHER_P(TemplateArgument, refersToType, 432 internal::Matcher<QualType>, InnerMatcher) { 433 if (Node.getKind() != TemplateArgument::Type) 434 return false; 435 return InnerMatcher.matches(Node.getAsType(), Finder, Builder); 436 } 437 438 /// \brief Matches a TemplateArgument that refers to a certain declaration. 439 /// 440 /// Given 441 /// \code 442 /// template<typename T> struct A {}; 443 /// struct B { B* next; }; 444 /// A<&B::next> a; 445 /// \endcode 446 /// classTemplateSpecializationDecl(hasAnyTemplateArgument( 447 /// refersToDeclaration(fieldDecl(hasName("next")))) 448 /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 449 /// \c B::next 450 AST_MATCHER_P(TemplateArgument, refersToDeclaration, 451 internal::Matcher<Decl>, InnerMatcher) { 452 if (Node.getKind() == TemplateArgument::Declaration) 453 return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder); 454 return false; 455 } 456 457 /// \brief Matches C++ constructor declarations. 458 /// 459 /// Example matches Foo::Foo() and Foo::Foo(int) 460 /// \code 461 /// class Foo { 462 /// public: 463 /// Foo(); 464 /// Foo(int); 465 /// int DoSomething(); 466 /// }; 467 /// \endcode 468 const internal::VariadicDynCastAllOfMatcher< 469 Decl, 470 CXXConstructorDecl> constructorDecl; 471 472 /// \brief Matches explicit C++ destructor declarations. 473 /// 474 /// Example matches Foo::~Foo() 475 /// \code 476 /// class Foo { 477 /// public: 478 /// virtual ~Foo(); 479 /// }; 480 /// \endcode 481 const internal::VariadicDynCastAllOfMatcher< 482 Decl, 483 CXXDestructorDecl> destructorDecl; 484 485 /// \brief Matches enum declarations. 486 /// 487 /// Example matches X 488 /// \code 489 /// enum X { 490 /// A, B, C 491 /// }; 492 /// \endcode 493 const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl; 494 495 /// \brief Matches enum constants. 496 /// 497 /// Example matches A, B, C 498 /// \code 499 /// enum X { 500 /// A, B, C 501 /// }; 502 /// \endcode 503 const internal::VariadicDynCastAllOfMatcher< 504 Decl, 505 EnumConstantDecl> enumConstantDecl; 506 507 /// \brief Matches method declarations. 508 /// 509 /// Example matches y 510 /// \code 511 /// class X { void y() }; 512 /// \endcode 513 const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> methodDecl; 514 515 /// \brief Matches variable declarations. 516 /// 517 /// Note: this does not match declarations of member variables, which are 518 /// "field" declarations in Clang parlance. 519 /// 520 /// Example matches a 521 /// \code 522 /// int a; 523 /// \endcode 524 const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl; 525 526 /// \brief Matches field declarations. 527 /// 528 /// Given 529 /// \code 530 /// class X { int m; }; 531 /// \endcode 532 /// fieldDecl() 533 /// matches 'm'. 534 const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl; 535 536 /// \brief Matches function declarations. 537 /// 538 /// Example matches f 539 /// \code 540 /// void f(); 541 /// \endcode 542 const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl; 543 544 /// \brief Matches C++ function template declarations. 545 /// 546 /// Example matches f 547 /// \code 548 /// template<class T> void f(T t) {} 549 /// \endcode 550 const internal::VariadicDynCastAllOfMatcher< 551 Decl, 552 FunctionTemplateDecl> functionTemplateDecl; 553 554 /// \brief Matches friend declarations. 555 /// 556 /// Given 557 /// \code 558 /// class X { friend void foo(); }; 559 /// \endcode 560 /// friendDecl() 561 /// matches 'friend void foo()'. 562 const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl; 563 564 /// \brief Matches statements. 565 /// 566 /// Given 567 /// \code 568 /// { ++a; } 569 /// \endcode 570 /// stmt() 571 /// matches both the compound statement '{ ++a; }' and '++a'. 572 const internal::VariadicAllOfMatcher<Stmt> stmt; 573 574 /// \brief Matches declaration statements. 575 /// 576 /// Given 577 /// \code 578 /// int a; 579 /// \endcode 580 /// declStmt() 581 /// matches 'int a'. 582 const internal::VariadicDynCastAllOfMatcher< 583 Stmt, 584 DeclStmt> declStmt; 585 586 /// \brief Matches member expressions. 587 /// 588 /// Given 589 /// \code 590 /// class Y { 591 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 592 /// int a; static int b; 593 /// }; 594 /// \endcode 595 /// memberExpr() 596 /// matches this->x, x, y.x, a, this->b 597 const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr; 598 599 /// \brief Matches call expressions. 600 /// 601 /// Example matches x.y() and y() 602 /// \code 603 /// X x; 604 /// x.y(); 605 /// y(); 606 /// \endcode 607 const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr; 608 609 /// \brief Matches lambda expressions. 610 /// 611 /// Example matches [&](){return 5;} 612 /// \code 613 /// [&](){return 5;} 614 /// \endcode 615 const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr; 616 617 /// \brief Matches member call expressions. 618 /// 619 /// Example matches x.y() 620 /// \code 621 /// X x; 622 /// x.y(); 623 /// \endcode 624 const internal::VariadicDynCastAllOfMatcher< 625 Stmt, 626 CXXMemberCallExpr> memberCallExpr; 627 628 /// \brief Matches init list expressions. 629 /// 630 /// Given 631 /// \code 632 /// int a[] = { 1, 2 }; 633 /// struct B { int x, y; }; 634 /// B b = { 5, 6 }; 635 /// \endcode 636 /// initList() 637 /// matches "{ 1, 2 }" and "{ 5, 6 }" 638 const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr; 639 640 /// \brief Matches using declarations. 641 /// 642 /// Given 643 /// \code 644 /// namespace X { int x; } 645 /// using X::x; 646 /// \endcode 647 /// usingDecl() 648 /// matches \code using X::x \endcode 649 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl; 650 651 /// \brief Matches unresolved using value declarations. 652 /// 653 /// Given 654 /// \code 655 /// template<typename X> 656 /// class C : private X { 657 /// using X::x; 658 /// }; 659 /// \endcode 660 /// unresolvedUsingValueDecl() 661 /// matches \code using X::x \endcode 662 const internal::VariadicDynCastAllOfMatcher< 663 Decl, 664 UnresolvedUsingValueDecl> unresolvedUsingValueDecl; 665 666 /// \brief Matches constructor call expressions (including implicit ones). 667 /// 668 /// Example matches string(ptr, n) and ptr within arguments of f 669 /// (matcher = constructExpr()) 670 /// \code 671 /// void f(const string &a, const string &b); 672 /// char *ptr; 673 /// int n; 674 /// f(string(ptr, n), ptr); 675 /// \endcode 676 const internal::VariadicDynCastAllOfMatcher< 677 Stmt, 678 CXXConstructExpr> constructExpr; 679 680 /// \brief Matches unresolved constructor call expressions. 681 /// 682 /// Example matches T(t) in return statement of f 683 /// (matcher = unresolvedConstructExpr()) 684 /// \code 685 /// template <typename T> 686 /// void f(const T& t) { return T(t); } 687 /// \endcode 688 const internal::VariadicDynCastAllOfMatcher< 689 Stmt, 690 CXXUnresolvedConstructExpr> unresolvedConstructExpr; 691 692 /// \brief Matches implicit and explicit this expressions. 693 /// 694 /// Example matches the implicit this expression in "return i". 695 /// (matcher = thisExpr()) 696 /// \code 697 /// struct foo { 698 /// int i; 699 /// int f() { return i; } 700 /// }; 701 /// \endcode 702 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> thisExpr; 703 704 /// \brief Matches nodes where temporaries are created. 705 /// 706 /// Example matches FunctionTakesString(GetStringByValue()) 707 /// (matcher = bindTemporaryExpr()) 708 /// \code 709 /// FunctionTakesString(GetStringByValue()); 710 /// FunctionTakesStringByPointer(GetStringPointer()); 711 /// \endcode 712 const internal::VariadicDynCastAllOfMatcher< 713 Stmt, 714 CXXBindTemporaryExpr> bindTemporaryExpr; 715 716 /// \brief Matches nodes where temporaries are materialized. 717 /// 718 /// Example: Given 719 /// \code 720 /// struct T {void func()}; 721 /// T f(); 722 /// void g(T); 723 /// \endcode 724 /// materializeTemporaryExpr() matches 'f()' in these statements 725 /// \code 726 /// T u(f()); 727 /// g(f()); 728 /// \endcode 729 /// but does not match 730 /// \code 731 /// f(); 732 /// f().func(); 733 /// \endcode 734 const internal::VariadicDynCastAllOfMatcher< 735 Stmt, 736 MaterializeTemporaryExpr> materializeTemporaryExpr; 737 738 /// \brief Matches new expressions. 739 /// 740 /// Given 741 /// \code 742 /// new X; 743 /// \endcode 744 /// newExpr() 745 /// matches 'new X'. 746 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> newExpr; 747 748 /// \brief Matches delete expressions. 749 /// 750 /// Given 751 /// \code 752 /// delete X; 753 /// \endcode 754 /// deleteExpr() 755 /// matches 'delete X'. 756 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> deleteExpr; 757 758 /// \brief Matches array subscript expressions. 759 /// 760 /// Given 761 /// \code 762 /// int i = a[1]; 763 /// \endcode 764 /// arraySubscriptExpr() 765 /// matches "a[1]" 766 const internal::VariadicDynCastAllOfMatcher< 767 Stmt, 768 ArraySubscriptExpr> arraySubscriptExpr; 769 770 /// \brief Matches the value of a default argument at the call site. 771 /// 772 /// Example matches the CXXDefaultArgExpr placeholder inserted for the 773 /// default value of the second parameter in the call expression f(42) 774 /// (matcher = defaultArgExpr()) 775 /// \code 776 /// void f(int x, int y = 0); 777 /// f(42); 778 /// \endcode 779 const internal::VariadicDynCastAllOfMatcher< 780 Stmt, 781 CXXDefaultArgExpr> defaultArgExpr; 782 783 /// \brief Matches overloaded operator calls. 784 /// 785 /// Note that if an operator isn't overloaded, it won't match. Instead, use 786 /// binaryOperator matcher. 787 /// Currently it does not match operators such as new delete. 788 /// FIXME: figure out why these do not match? 789 /// 790 /// Example matches both operator<<((o << b), c) and operator<<(o, b) 791 /// (matcher = operatorCallExpr()) 792 /// \code 793 /// ostream &operator<< (ostream &out, int i) { }; 794 /// ostream &o; int b = 1, c = 1; 795 /// o << b << c; 796 /// \endcode 797 const internal::VariadicDynCastAllOfMatcher< 798 Stmt, 799 CXXOperatorCallExpr> operatorCallExpr; 800 801 /// \brief Matches expressions. 802 /// 803 /// Example matches x() 804 /// \code 805 /// void f() { x(); } 806 /// \endcode 807 const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr; 808 809 /// \brief Matches expressions that refer to declarations. 810 /// 811 /// Example matches x in if (x) 812 /// \code 813 /// bool x; 814 /// if (x) {} 815 /// \endcode 816 const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr; 817 818 /// \brief Matches if statements. 819 /// 820 /// Example matches 'if (x) {}' 821 /// \code 822 /// if (x) {} 823 /// \endcode 824 const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt; 825 826 /// \brief Matches for statements. 827 /// 828 /// Example matches 'for (;;) {}' 829 /// \code 830 /// for (;;) {} 831 /// int i[] = {1, 2, 3}; for (auto a : i); 832 /// \endcode 833 const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt; 834 835 /// \brief Matches range-based for statements. 836 /// 837 /// forRangeStmt() matches 'for (auto a : i)' 838 /// \code 839 /// int i[] = {1, 2, 3}; for (auto a : i); 840 /// for(int j = 0; j < 5; ++j); 841 /// \endcode 842 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt; 843 844 /// \brief Matches the increment statement of a for loop. 845 /// 846 /// Example: 847 /// forStmt(hasIncrement(unaryOperator(hasOperatorName("++")))) 848 /// matches '++x' in 849 /// \code 850 /// for (x; x < N; ++x) { } 851 /// \endcode 852 AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>, 853 InnerMatcher) { 854 const Stmt *const Increment = Node.getInc(); 855 return (Increment != NULL && 856 InnerMatcher.matches(*Increment, Finder, Builder)); 857 } 858 859 /// \brief Matches the initialization statement of a for loop. 860 /// 861 /// Example: 862 /// forStmt(hasLoopInit(declStmt())) 863 /// matches 'int x = 0' in 864 /// \code 865 /// for (int x = 0; x < N; ++x) { } 866 /// \endcode 867 AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>, 868 InnerMatcher) { 869 const Stmt *const Init = Node.getInit(); 870 return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder)); 871 } 872 873 /// \brief Matches while statements. 874 /// 875 /// Given 876 /// \code 877 /// while (true) {} 878 /// \endcode 879 /// whileStmt() 880 /// matches 'while (true) {}'. 881 const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt; 882 883 /// \brief Matches do statements. 884 /// 885 /// Given 886 /// \code 887 /// do {} while (true); 888 /// \endcode 889 /// doStmt() 890 /// matches 'do {} while(true)' 891 const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt; 892 893 /// \brief Matches break statements. 894 /// 895 /// Given 896 /// \code 897 /// while (true) { break; } 898 /// \endcode 899 /// breakStmt() 900 /// matches 'break' 901 const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt; 902 903 /// \brief Matches continue statements. 904 /// 905 /// Given 906 /// \code 907 /// while (true) { continue; } 908 /// \endcode 909 /// continueStmt() 910 /// matches 'continue' 911 const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> continueStmt; 912 913 /// \brief Matches return statements. 914 /// 915 /// Given 916 /// \code 917 /// return 1; 918 /// \endcode 919 /// returnStmt() 920 /// matches 'return 1' 921 const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt; 922 923 /// \brief Matches goto statements. 924 /// 925 /// Given 926 /// \code 927 /// goto FOO; 928 /// FOO: bar(); 929 /// \endcode 930 /// gotoStmt() 931 /// matches 'goto FOO' 932 const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt; 933 934 /// \brief Matches label statements. 935 /// 936 /// Given 937 /// \code 938 /// goto FOO; 939 /// FOO: bar(); 940 /// \endcode 941 /// labelStmt() 942 /// matches 'FOO:' 943 const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt; 944 945 /// \brief Matches switch statements. 946 /// 947 /// Given 948 /// \code 949 /// switch(a) { case 42: break; default: break; } 950 /// \endcode 951 /// switchStmt() 952 /// matches 'switch(a)'. 953 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt; 954 955 /// \brief Matches case and default statements inside switch statements. 956 /// 957 /// Given 958 /// \code 959 /// switch(a) { case 42: break; default: break; } 960 /// \endcode 961 /// switchCase() 962 /// matches 'case 42: break;' and 'default: break;'. 963 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase; 964 965 /// \brief Matches case statements inside switch statements. 966 /// 967 /// Given 968 /// \code 969 /// switch(a) { case 42: break; default: break; } 970 /// \endcode 971 /// caseStmt() 972 /// matches 'case 42: break;'. 973 const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt; 974 975 /// \brief Matches default statements inside switch statements. 976 /// 977 /// Given 978 /// \code 979 /// switch(a) { case 42: break; default: break; } 980 /// \endcode 981 /// defaultStmt() 982 /// matches 'default: break;'. 983 const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt; 984 985 /// \brief Matches compound statements. 986 /// 987 /// Example matches '{}' and '{{}}'in 'for (;;) {{}}' 988 /// \code 989 /// for (;;) {{}} 990 /// \endcode 991 const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt; 992 993 /// \brief Matches catch statements. 994 /// 995 /// \code 996 /// try {} catch(int i) {} 997 /// \endcode 998 /// catchStmt() 999 /// matches 'catch(int i)' 1000 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> catchStmt; 1001 1002 /// \brief Matches try statements. 1003 /// 1004 /// \code 1005 /// try {} catch(int i) {} 1006 /// \endcode 1007 /// tryStmt() 1008 /// matches 'try {}' 1009 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> tryStmt; 1010 1011 /// \brief Matches throw expressions. 1012 /// 1013 /// \code 1014 /// try { throw 5; } catch(int i) {} 1015 /// \endcode 1016 /// throwExpr() 1017 /// matches 'throw 5' 1018 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> throwExpr; 1019 1020 /// \brief Matches null statements. 1021 /// 1022 /// \code 1023 /// foo();; 1024 /// \endcode 1025 /// nullStmt() 1026 /// matches the second ';' 1027 const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt; 1028 1029 /// \brief Matches asm statements. 1030 /// 1031 /// \code 1032 /// int i = 100; 1033 /// __asm("mov al, 2"); 1034 /// \endcode 1035 /// asmStmt() 1036 /// matches '__asm("mov al, 2")' 1037 const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt; 1038 1039 /// \brief Matches bool literals. 1040 /// 1041 /// Example matches true 1042 /// \code 1043 /// true 1044 /// \endcode 1045 const internal::VariadicDynCastAllOfMatcher< 1046 Stmt, 1047 CXXBoolLiteralExpr> boolLiteral; 1048 1049 /// \brief Matches string literals (also matches wide string literals). 1050 /// 1051 /// Example matches "abcd", L"abcd" 1052 /// \code 1053 /// char *s = "abcd"; wchar_t *ws = L"abcd" 1054 /// \endcode 1055 const internal::VariadicDynCastAllOfMatcher< 1056 Stmt, 1057 StringLiteral> stringLiteral; 1058 1059 /// \brief Matches character literals (also matches wchar_t). 1060 /// 1061 /// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), 1062 /// though. 1063 /// 1064 /// Example matches 'a', L'a' 1065 /// \code 1066 /// char ch = 'a'; wchar_t chw = L'a'; 1067 /// \endcode 1068 const internal::VariadicDynCastAllOfMatcher< 1069 Stmt, 1070 CharacterLiteral> characterLiteral; 1071 1072 /// \brief Matches integer literals of all sizes / encodings, e.g. 1073 /// 1, 1L, 0x1 and 1U. 1074 /// 1075 /// Does not match character-encoded integers such as L'a'. 1076 const internal::VariadicDynCastAllOfMatcher< 1077 Stmt, 1078 IntegerLiteral> integerLiteral; 1079 1080 /// \brief Matches float literals of all sizes / encodings, e.g. 1081 /// 1.0, 1.0f, 1.0L and 1e10. 1082 /// 1083 /// Does not match implicit conversions such as 1084 /// \code 1085 /// float a = 10; 1086 /// \endcode 1087 const internal::VariadicDynCastAllOfMatcher< 1088 Stmt, 1089 FloatingLiteral> floatLiteral; 1090 1091 /// \brief Matches user defined literal operator call. 1092 /// 1093 /// Example match: "foo"_suffix 1094 const internal::VariadicDynCastAllOfMatcher< 1095 Stmt, 1096 UserDefinedLiteral> userDefinedLiteral; 1097 1098 /// \brief Matches compound (i.e. non-scalar) literals 1099 /// 1100 /// Example match: {1}, (1, 2) 1101 /// \code 1102 /// int array[4] = {1}; vector int myvec = (vector int)(1, 2); 1103 /// \endcode 1104 const internal::VariadicDynCastAllOfMatcher< 1105 Stmt, 1106 CompoundLiteralExpr> compoundLiteralExpr; 1107 1108 /// \brief Matches nullptr literal. 1109 const internal::VariadicDynCastAllOfMatcher< 1110 Stmt, 1111 CXXNullPtrLiteralExpr> nullPtrLiteralExpr; 1112 1113 /// \brief Matches binary operator expressions. 1114 /// 1115 /// Example matches a || b 1116 /// \code 1117 /// !(a || b) 1118 /// \endcode 1119 const internal::VariadicDynCastAllOfMatcher< 1120 Stmt, 1121 BinaryOperator> binaryOperator; 1122 1123 /// \brief Matches unary operator expressions. 1124 /// 1125 /// Example matches !a 1126 /// \code 1127 /// !a || b 1128 /// \endcode 1129 const internal::VariadicDynCastAllOfMatcher< 1130 Stmt, 1131 UnaryOperator> unaryOperator; 1132 1133 /// \brief Matches conditional operator expressions. 1134 /// 1135 /// Example matches a ? b : c 1136 /// \code 1137 /// (a ? b : c) + 42 1138 /// \endcode 1139 const internal::VariadicDynCastAllOfMatcher< 1140 Stmt, 1141 ConditionalOperator> conditionalOperator; 1142 1143 /// \brief Matches a reinterpret_cast expression. 1144 /// 1145 /// Either the source expression or the destination type can be matched 1146 /// using has(), but hasDestinationType() is more specific and can be 1147 /// more readable. 1148 /// 1149 /// Example matches reinterpret_cast<char*>(&p) in 1150 /// \code 1151 /// void* p = reinterpret_cast<char*>(&p); 1152 /// \endcode 1153 const internal::VariadicDynCastAllOfMatcher< 1154 Stmt, 1155 CXXReinterpretCastExpr> reinterpretCastExpr; 1156 1157 /// \brief Matches a C++ static_cast expression. 1158 /// 1159 /// \see hasDestinationType 1160 /// \see reinterpretCast 1161 /// 1162 /// Example: 1163 /// staticCastExpr() 1164 /// matches 1165 /// static_cast<long>(8) 1166 /// in 1167 /// \code 1168 /// long eight(static_cast<long>(8)); 1169 /// \endcode 1170 const internal::VariadicDynCastAllOfMatcher< 1171 Stmt, 1172 CXXStaticCastExpr> staticCastExpr; 1173 1174 /// \brief Matches a dynamic_cast expression. 1175 /// 1176 /// Example: 1177 /// dynamicCastExpr() 1178 /// matches 1179 /// dynamic_cast<D*>(&b); 1180 /// in 1181 /// \code 1182 /// struct B { virtual ~B() {} }; struct D : B {}; 1183 /// B b; 1184 /// D* p = dynamic_cast<D*>(&b); 1185 /// \endcode 1186 const internal::VariadicDynCastAllOfMatcher< 1187 Stmt, 1188 CXXDynamicCastExpr> dynamicCastExpr; 1189 1190 /// \brief Matches a const_cast expression. 1191 /// 1192 /// Example: Matches const_cast<int*>(&r) in 1193 /// \code 1194 /// int n = 42; 1195 /// const int &r(n); 1196 /// int* p = const_cast<int*>(&r); 1197 /// \endcode 1198 const internal::VariadicDynCastAllOfMatcher< 1199 Stmt, 1200 CXXConstCastExpr> constCastExpr; 1201 1202 /// \brief Matches a C-style cast expression. 1203 /// 1204 /// Example: Matches (int*) 2.2f in 1205 /// \code 1206 /// int i = (int) 2.2f; 1207 /// \endcode 1208 const internal::VariadicDynCastAllOfMatcher< 1209 Stmt, 1210 CStyleCastExpr> cStyleCastExpr; 1211 1212 /// \brief Matches explicit cast expressions. 1213 /// 1214 /// Matches any cast expression written in user code, whether it be a 1215 /// C-style cast, a functional-style cast, or a keyword cast. 1216 /// 1217 /// Does not match implicit conversions. 1218 /// 1219 /// Note: the name "explicitCast" is chosen to match Clang's terminology, as 1220 /// Clang uses the term "cast" to apply to implicit conversions as well as to 1221 /// actual cast expressions. 1222 /// 1223 /// \see hasDestinationType. 1224 /// 1225 /// Example: matches all five of the casts in 1226 /// \code 1227 /// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 1228 /// \endcode 1229 /// but does not match the implicit conversion in 1230 /// \code 1231 /// long ell = 42; 1232 /// \endcode 1233 const internal::VariadicDynCastAllOfMatcher< 1234 Stmt, 1235 ExplicitCastExpr> explicitCastExpr; 1236 1237 /// \brief Matches the implicit cast nodes of Clang's AST. 1238 /// 1239 /// This matches many different places, including function call return value 1240 /// eliding, as well as any type conversions. 1241 const internal::VariadicDynCastAllOfMatcher< 1242 Stmt, 1243 ImplicitCastExpr> implicitCastExpr; 1244 1245 /// \brief Matches any cast nodes of Clang's AST. 1246 /// 1247 /// Example: castExpr() matches each of the following: 1248 /// \code 1249 /// (int) 3; 1250 /// const_cast<Expr *>(SubExpr); 1251 /// char c = 0; 1252 /// \endcode 1253 /// but does not match 1254 /// \code 1255 /// int i = (0); 1256 /// int k = 0; 1257 /// \endcode 1258 const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr; 1259 1260 /// \brief Matches functional cast expressions 1261 /// 1262 /// Example: Matches Foo(bar); 1263 /// \code 1264 /// Foo f = bar; 1265 /// Foo g = (Foo) bar; 1266 /// Foo h = Foo(bar); 1267 /// \endcode 1268 const internal::VariadicDynCastAllOfMatcher< 1269 Stmt, 1270 CXXFunctionalCastExpr> functionalCastExpr; 1271 1272 /// \brief Matches \c QualTypes in the clang AST. 1273 const internal::VariadicAllOfMatcher<QualType> qualType; 1274 1275 /// \brief Matches \c Types in the clang AST. 1276 const internal::VariadicAllOfMatcher<Type> type; 1277 1278 /// \brief Matches \c TypeLocs in the clang AST. 1279 const internal::VariadicAllOfMatcher<TypeLoc> typeLoc; 1280 1281 /// \brief Matches if any of the given matchers matches. 1282 /// 1283 /// Unlike \c anyOf, \c eachOf will generate a match result for each 1284 /// matching submatcher. 1285 /// 1286 /// For example, in: 1287 /// \code 1288 /// class A { int a; int b; }; 1289 /// \endcode 1290 /// The matcher: 1291 /// \code 1292 /// recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), 1293 /// has(fieldDecl(hasName("b")).bind("v")))) 1294 /// \endcode 1295 /// will generate two results binding "v", the first of which binds 1296 /// the field declaration of \c a, the second the field declaration of 1297 /// \c b. 1298 /// 1299 /// Usable as: Any Matcher 1300 template <typename M1, typename M2> 1301 internal::PolymorphicMatcherWithParam2<internal::EachOfMatcher, M1, M2> 1302 eachOf(const M1 &P1, const M2 &P2) { 1303 return internal::PolymorphicMatcherWithParam2<internal::EachOfMatcher, M1, 1304 M2>(P1, P2); 1305 } 1306 1307 /// \brief Various overloads for the anyOf matcher. 1308 /// @{ 1309 1310 /// \brief Matches if any of the given matchers matches. 1311 /// 1312 /// Usable as: Any Matcher 1313 template<typename M1, typename M2> 1314 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, M2> 1315 anyOf(const M1 &P1, const M2 &P2) { 1316 return internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 1317 M1, M2 >(P1, P2); 1318 } 1319 template<typename M1, typename M2, typename M3> 1320 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 1321 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, M3> > 1322 anyOf(const M1 &P1, const M2 &P2, const M3 &P3) { 1323 return anyOf(P1, anyOf(P2, P3)); 1324 } 1325 template<typename M1, typename M2, typename M3, typename M4> 1326 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 1327 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, 1328 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 1329 M3, M4> > > 1330 anyOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4) { 1331 return anyOf(P1, anyOf(P2, anyOf(P3, P4))); 1332 } 1333 template<typename M1, typename M2, typename M3, typename M4, typename M5> 1334 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 1335 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, 1336 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M3, 1337 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 1338 M4, M5> > > > 1339 anyOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4, const M5 &P5) { 1340 return anyOf(P1, anyOf(P2, anyOf(P3, anyOf(P4, P5)))); 1341 } 1342 1343 /// @} 1344 1345 /// \brief Various overloads for the allOf matcher. 1346 /// @{ 1347 1348 /// \brief Matches if all given matchers match. 1349 /// 1350 /// Usable as: Any Matcher 1351 template <typename M1, typename M2> 1352 internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M1, M2> 1353 allOf(const M1 &P1, const M2 &P2) { 1354 return internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M1, M2>( 1355 P1, P2); 1356 } 1357 template <typename M1, typename M2, typename M3> 1358 internal::PolymorphicMatcherWithParam2< 1359 internal::AllOfMatcher, M1, 1360 internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M2, M3> > 1361 allOf(const M1 &P1, const M2 &P2, const M3 &P3) { 1362 return allOf(P1, allOf(P2, P3)); 1363 } 1364 template <typename M1, typename M2, typename M3, typename M4> 1365 internal::PolymorphicMatcherWithParam2< 1366 internal::AllOfMatcher, M1, 1367 internal::PolymorphicMatcherWithParam2< 1368 internal::AllOfMatcher, M2, internal::PolymorphicMatcherWithParam2< 1369 internal::AllOfMatcher, M3, M4> > > 1370 allOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4) { 1371 return allOf(P1, allOf(P2, P3, P4)); 1372 } 1373 template <typename M1, typename M2, typename M3, typename M4, typename M5> 1374 internal::PolymorphicMatcherWithParam2< 1375 internal::AllOfMatcher, M1, 1376 internal::PolymorphicMatcherWithParam2< 1377 internal::AllOfMatcher, M2, 1378 internal::PolymorphicMatcherWithParam2< 1379 internal::AllOfMatcher, M3, 1380 internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M4, 1381 M5> > > > 1382 allOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4, const M5 &P5) { 1383 return allOf(P1, allOf(P2, P3, P4, P5)); 1384 } 1385 1386 /// @} 1387 1388 /// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 1389 /// 1390 /// Given 1391 /// \code 1392 /// Foo x = bar; 1393 /// int y = sizeof(x) + alignof(x); 1394 /// \endcode 1395 /// unaryExprOrTypeTraitExpr() 1396 /// matches \c sizeof(x) and \c alignof(x) 1397 const internal::VariadicDynCastAllOfMatcher< 1398 Stmt, 1399 UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr; 1400 1401 /// \brief Matches unary expressions that have a specific type of argument. 1402 /// 1403 /// Given 1404 /// \code 1405 /// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 1406 /// \endcode 1407 /// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 1408 /// matches \c sizeof(a) and \c alignof(c) 1409 AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 1410 internal::Matcher<QualType>, InnerMatcher) { 1411 const QualType ArgumentType = Node.getTypeOfArgument(); 1412 return InnerMatcher.matches(ArgumentType, Finder, Builder); 1413 } 1414 1415 /// \brief Matches unary expressions of a certain kind. 1416 /// 1417 /// Given 1418 /// \code 1419 /// int x; 1420 /// int s = sizeof(x) + alignof(x) 1421 /// \endcode 1422 /// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 1423 /// matches \c sizeof(x) 1424 AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 1425 return Node.getKind() == Kind; 1426 } 1427 1428 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1429 /// alignof. 1430 inline internal::Matcher<Stmt> alignOfExpr( 1431 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1432 return stmt(unaryExprOrTypeTraitExpr(allOf( 1433 ofKind(UETT_AlignOf), InnerMatcher))); 1434 } 1435 1436 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1437 /// sizeof. 1438 inline internal::Matcher<Stmt> sizeOfExpr( 1439 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1440 return stmt(unaryExprOrTypeTraitExpr( 1441 allOf(ofKind(UETT_SizeOf), InnerMatcher))); 1442 } 1443 1444 /// \brief Matches NamedDecl nodes that have the specified name. 1445 /// 1446 /// Supports specifying enclosing namespaces or classes by prefixing the name 1447 /// with '<enclosing>::'. 1448 /// Does not match typedefs of an underlying type with the given name. 1449 /// 1450 /// Example matches X (Name == "X") 1451 /// \code 1452 /// class X; 1453 /// \endcode 1454 /// 1455 /// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 1456 /// \code 1457 /// namespace a { namespace b { class X; } } 1458 /// \endcode 1459 AST_MATCHER_P(NamedDecl, hasName, std::string, Name) { 1460 assert(!Name.empty()); 1461 const std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1462 const StringRef FullName = FullNameString; 1463 const StringRef Pattern = Name; 1464 if (Pattern.startswith("::")) { 1465 return FullName == Pattern; 1466 } else { 1467 return FullName.endswith(("::" + Pattern).str()); 1468 } 1469 } 1470 1471 /// \brief Matches NamedDecl nodes whose fully qualified names contain 1472 /// a substring matched by the given RegExp. 1473 /// 1474 /// Supports specifying enclosing namespaces or classes by 1475 /// prefixing the name with '<enclosing>::'. Does not match typedefs 1476 /// of an underlying type with the given name. 1477 /// 1478 /// Example matches X (regexp == "::X") 1479 /// \code 1480 /// class X; 1481 /// \endcode 1482 /// 1483 /// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 1484 /// \code 1485 /// namespace foo { namespace bar { class X; } } 1486 /// \endcode 1487 AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 1488 assert(!RegExp.empty()); 1489 std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1490 llvm::Regex RE(RegExp); 1491 return RE.match(FullNameString); 1492 } 1493 1494 /// \brief Matches overloaded operator names. 1495 /// 1496 /// Matches overloaded operator names specified in strings without the 1497 /// "operator" prefix: e.g. "<<". 1498 /// 1499 /// Given: 1500 /// \code 1501 /// class A { int operator*(); }; 1502 /// const A &operator<<(const A &a, const A &b); 1503 /// A a; 1504 /// a << a; // <-- This matches 1505 /// \endcode 1506 /// 1507 /// \c operatorCallExpr(hasOverloadedOperatorName("<<"))) matches the specified 1508 /// line and \c recordDecl(hasMethod(hasOverloadedOperatorName("*"))) matches 1509 /// the declaration of \c A. 1510 /// 1511 /// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<CXXMethodDecl> 1512 inline internal::PolymorphicMatcherWithParam1< 1513 internal::HasOverloadedOperatorNameMatcher, StringRef, 1514 AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)> 1515 hasOverloadedOperatorName(const StringRef Name) { 1516 return internal::PolymorphicMatcherWithParam1< 1517 internal::HasOverloadedOperatorNameMatcher, StringRef, 1518 AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)>( 1519 Name); 1520 } 1521 1522 /// \brief Matches C++ classes that are directly or indirectly derived from 1523 /// a class matching \c Base. 1524 /// 1525 /// Note that a class is not considered to be derived from itself. 1526 /// 1527 /// Example matches Y, Z, C (Base == hasName("X")) 1528 /// \code 1529 /// class X; 1530 /// class Y : public X {}; // directly derived 1531 /// class Z : public Y {}; // indirectly derived 1532 /// typedef X A; 1533 /// typedef A B; 1534 /// class C : public B {}; // derived from a typedef of X 1535 /// \endcode 1536 /// 1537 /// In the following example, Bar matches isDerivedFrom(hasName("X")): 1538 /// \code 1539 /// class Foo; 1540 /// typedef Foo X; 1541 /// class Bar : public Foo {}; // derived from a type that X is a typedef of 1542 /// \endcode 1543 AST_MATCHER_P(CXXRecordDecl, isDerivedFrom, 1544 internal::Matcher<NamedDecl>, Base) { 1545 return Finder->classIsDerivedFrom(&Node, Base, Builder); 1546 } 1547 1548 /// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 1549 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, StringRef, BaseName, 1) { 1550 assert(!BaseName.empty()); 1551 return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); 1552 } 1553 1554 /// \brief Similar to \c isDerivedFrom(), but also matches classes that directly 1555 /// match \c Base. 1556 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, 1557 internal::Matcher<NamedDecl>, Base, 0) { 1558 return Matcher<CXXRecordDecl>(anyOf(Base, isDerivedFrom(Base))) 1559 .matches(Node, Finder, Builder); 1560 } 1561 1562 /// \brief Overloaded method as shortcut for 1563 /// \c isSameOrDerivedFrom(hasName(...)). 1564 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, StringRef, BaseName, 1565 1) { 1566 assert(!BaseName.empty()); 1567 return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); 1568 } 1569 1570 /// \brief Matches the first method of a class or struct that satisfies \c 1571 /// InnerMatcher. 1572 /// 1573 /// Given: 1574 /// \code 1575 /// class A { void func(); }; 1576 /// class B { void member(); }; 1577 /// \code 1578 /// 1579 /// \c recordDecl(hasMethod(hasName("func"))) matches the declaration of \c A 1580 /// but not \c B. 1581 AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>, 1582 InnerMatcher) { 1583 return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(), 1584 Node.method_end(), Finder, Builder); 1585 } 1586 1587 /// \brief Matches AST nodes that have child AST nodes that match the 1588 /// provided matcher. 1589 /// 1590 /// Example matches X, Y (matcher = recordDecl(has(recordDecl(hasName("X"))) 1591 /// \code 1592 /// class X {}; // Matches X, because X::X is a class of name X inside X. 1593 /// class Y { class X {}; }; 1594 /// class Z { class Y { class X {}; }; }; // Does not match Z. 1595 /// \endcode 1596 /// 1597 /// ChildT must be an AST base type. 1598 /// 1599 /// Usable as: Any Matcher 1600 template <typename ChildT> 1601 internal::ArgumentAdaptingMatcher<internal::HasMatcher, ChildT> has( 1602 const internal::Matcher<ChildT> &ChildMatcher) { 1603 return internal::ArgumentAdaptingMatcher<internal::HasMatcher, 1604 ChildT>(ChildMatcher); 1605 } 1606 1607 /// \brief Matches AST nodes that have descendant AST nodes that match the 1608 /// provided matcher. 1609 /// 1610 /// Example matches X, Y, Z 1611 /// (matcher = recordDecl(hasDescendant(recordDecl(hasName("X"))))) 1612 /// \code 1613 /// class X {}; // Matches X, because X::X is a class of name X inside X. 1614 /// class Y { class X {}; }; 1615 /// class Z { class Y { class X {}; }; }; 1616 /// \endcode 1617 /// 1618 /// DescendantT must be an AST base type. 1619 /// 1620 /// Usable as: Any Matcher 1621 template <typename DescendantT> 1622 internal::ArgumentAdaptingMatcher<internal::HasDescendantMatcher, DescendantT> 1623 hasDescendant(const internal::Matcher<DescendantT> &DescendantMatcher) { 1624 return internal::ArgumentAdaptingMatcher< 1625 internal::HasDescendantMatcher, 1626 DescendantT>(DescendantMatcher); 1627 } 1628 1629 /// \brief Matches AST nodes that have child AST nodes that match the 1630 /// provided matcher. 1631 /// 1632 /// Example matches X, Y (matcher = recordDecl(forEach(recordDecl(hasName("X"))) 1633 /// \code 1634 /// class X {}; // Matches X, because X::X is a class of name X inside X. 1635 /// class Y { class X {}; }; 1636 /// class Z { class Y { class X {}; }; }; // Does not match Z. 1637 /// \endcode 1638 /// 1639 /// ChildT must be an AST base type. 1640 /// 1641 /// As opposed to 'has', 'forEach' will cause a match for each result that 1642 /// matches instead of only on the first one. 1643 /// 1644 /// Usable as: Any Matcher 1645 template <typename ChildT> 1646 internal::ArgumentAdaptingMatcher<internal::ForEachMatcher, ChildT> forEach( 1647 const internal::Matcher<ChildT> &ChildMatcher) { 1648 return internal::ArgumentAdaptingMatcher< 1649 internal::ForEachMatcher, 1650 ChildT>(ChildMatcher); 1651 } 1652 1653 /// \brief Matches AST nodes that have descendant AST nodes that match the 1654 /// provided matcher. 1655 /// 1656 /// Example matches X, A, B, C 1657 /// (matcher = recordDecl(forEachDescendant(recordDecl(hasName("X"))))) 1658 /// \code 1659 /// class X {}; // Matches X, because X::X is a class of name X inside X. 1660 /// class A { class X {}; }; 1661 /// class B { class C { class X {}; }; }; 1662 /// \endcode 1663 /// 1664 /// DescendantT must be an AST base type. 1665 /// 1666 /// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 1667 /// each result that matches instead of only on the first one. 1668 /// 1669 /// Note: Recursively combined ForEachDescendant can cause many matches: 1670 /// recordDecl(forEachDescendant(recordDecl(forEachDescendant(recordDecl())))) 1671 /// will match 10 times (plus injected class name matches) on: 1672 /// \code 1673 /// class A { class B { class C { class D { class E {}; }; }; }; }; 1674 /// \endcode 1675 /// 1676 /// Usable as: Any Matcher 1677 template <typename DescendantT> 1678 internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, 1679 DescendantT> 1680 forEachDescendant( 1681 const internal::Matcher<DescendantT> &DescendantMatcher) { 1682 return internal::ArgumentAdaptingMatcher< 1683 internal::ForEachDescendantMatcher, 1684 DescendantT>(DescendantMatcher); 1685 } 1686 1687 /// \brief Matches if the node or any descendant matches. 1688 /// 1689 /// Generates results for each match. 1690 /// 1691 /// For example, in: 1692 /// \code 1693 /// class A { class B {}; class C {}; }; 1694 /// \endcode 1695 /// The matcher: 1696 /// \code 1697 /// recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("m"))) 1698 /// \endcode 1699 /// will generate results for \c A, \c B and \c C. 1700 /// 1701 /// Usable as: Any Matcher 1702 template <typename T> 1703 internal::PolymorphicMatcherWithParam2< 1704 internal::EachOfMatcher, internal::Matcher<T>, 1705 internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, T> > 1706 findAll(const internal::Matcher<T> &Matcher) { 1707 return eachOf(Matcher, forEachDescendant(Matcher)); 1708 } 1709 1710 /// \brief Matches AST nodes that have a parent that matches the provided 1711 /// matcher. 1712 /// 1713 /// Given 1714 /// \code 1715 /// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } } 1716 /// \endcode 1717 /// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }". 1718 /// 1719 /// Usable as: Any Matcher 1720 template <typename ParentT> 1721 internal::ArgumentAdaptingMatcher<internal::HasParentMatcher, ParentT, 1722 internal::TypeList<Decl, Stmt>, 1723 internal::TypeList<Decl, Stmt> > 1724 hasParent(const internal::Matcher<ParentT> &ParentMatcher) { 1725 return internal::ArgumentAdaptingMatcher< 1726 internal::HasParentMatcher, ParentT, internal::TypeList<Decl, Stmt>, 1727 internal::TypeList<Decl, Stmt> >(ParentMatcher); 1728 } 1729 1730 /// \brief Matches AST nodes that have an ancestor that matches the provided 1731 /// matcher. 1732 /// 1733 /// Given 1734 /// \code 1735 /// void f() { if (true) { int x = 42; } } 1736 /// void g() { for (;;) { int x = 43; } } 1737 /// \endcode 1738 /// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43. 1739 /// 1740 /// Usable as: Any Matcher 1741 template <typename AncestorT> 1742 internal::ArgumentAdaptingMatcher<internal::HasAncestorMatcher, AncestorT, 1743 internal::TypeList<Decl, Stmt>, 1744 internal::TypeList<Decl, Stmt> > 1745 hasAncestor(const internal::Matcher<AncestorT> &AncestorMatcher) { 1746 return internal::ArgumentAdaptingMatcher< 1747 internal::HasAncestorMatcher, AncestorT, internal::TypeList<Decl, Stmt>, 1748 internal::TypeList<Decl, Stmt> >(AncestorMatcher); 1749 } 1750 1751 /// \brief Matches if the provided matcher does not match. 1752 /// 1753 /// Example matches Y (matcher = recordDecl(unless(hasName("X")))) 1754 /// \code 1755 /// class X {}; 1756 /// class Y {}; 1757 /// \endcode 1758 /// 1759 /// Usable as: Any Matcher 1760 template <typename M> 1761 internal::PolymorphicMatcherWithParam1<internal::NotMatcher, M> 1762 unless(const M &InnerMatcher) { 1763 return internal::PolymorphicMatcherWithParam1< 1764 internal::NotMatcher, M>(InnerMatcher); 1765 } 1766 1767 /// \brief Matches a node if the declaration associated with that node 1768 /// matches the given matcher. 1769 /// 1770 /// The associated declaration is: 1771 /// - for type nodes, the declaration of the underlying type 1772 /// - for CallExpr, the declaration of the callee 1773 /// - for MemberExpr, the declaration of the referenced member 1774 /// - for CXXConstructExpr, the declaration of the constructor 1775 /// 1776 /// Also usable as Matcher<T> for any T supporting the getDecl() member 1777 /// function. e.g. various subtypes of clang::Type and various expressions. 1778 /// FIXME: Add all node types for which this is matcher is usable due to 1779 /// getDecl(). 1780 /// 1781 /// Usable as: Matcher<QualType>, Matcher<CallExpr>, Matcher<CXXConstructExpr>, 1782 /// Matcher<MemberExpr>, Matcher<TypedefType>, 1783 /// Matcher<TemplateSpecializationType> 1784 inline internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, 1785 internal::Matcher<Decl> > 1786 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 1787 return internal::PolymorphicMatcherWithParam1< 1788 internal::HasDeclarationMatcher, 1789 internal::Matcher<Decl> >(InnerMatcher); 1790 } 1791 1792 /// \brief Matches on the implicit object argument of a member call expression. 1793 /// 1794 /// Example matches y.x() (matcher = callExpr(on(hasType(recordDecl(hasName("Y")))))) 1795 /// \code 1796 /// class Y { public: void x(); }; 1797 /// void z() { Y y; y.x(); }", 1798 /// \endcode 1799 /// 1800 /// FIXME: Overload to allow directly matching types? 1801 AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 1802 InnerMatcher) { 1803 const Expr *ExprNode = Node.getImplicitObjectArgument() 1804 ->IgnoreParenImpCasts(); 1805 return (ExprNode != NULL && 1806 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1807 } 1808 1809 /// \brief Matches if the call expression's callee expression matches. 1810 /// 1811 /// Given 1812 /// \code 1813 /// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 1814 /// void f() { f(); } 1815 /// \endcode 1816 /// callExpr(callee(expr())) 1817 /// matches this->x(), x(), y.x(), f() 1818 /// with callee(...) 1819 /// matching this->x, x, y.x, f respectively 1820 /// 1821 /// Note: Callee cannot take the more general internal::Matcher<Expr> 1822 /// because this introduces ambiguous overloads with calls to Callee taking a 1823 /// internal::Matcher<Decl>, as the matcher hierarchy is purely 1824 /// implemented in terms of implicit casts. 1825 AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 1826 InnerMatcher) { 1827 const Expr *ExprNode = Node.getCallee(); 1828 return (ExprNode != NULL && 1829 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1830 } 1831 1832 /// \brief Matches if the call expression's callee's declaration matches the 1833 /// given matcher. 1834 /// 1835 /// Example matches y.x() (matcher = callExpr(callee(methodDecl(hasName("x"))))) 1836 /// \code 1837 /// class Y { public: void x(); }; 1838 /// void z() { Y y; y.x(); 1839 /// \endcode 1840 AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher, 1841 1) { 1842 return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder); 1843 } 1844 1845 /// \brief Matches if the expression's or declaration's type matches a type 1846 /// matcher. 1847 /// 1848 /// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1849 /// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1850 /// \code 1851 /// class X {}; 1852 /// void y(X &x) { x; X z; } 1853 /// \endcode 1854 AST_POLYMORPHIC_MATCHER_P_OVERLOAD( 1855 hasType, AST_POLYMORPHIC_SUPPORTED_TYPES_2(Expr, ValueDecl), 1856 internal::Matcher<QualType>, InnerMatcher, 0) { 1857 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1858 } 1859 1860 /// \brief Overloaded to match the declaration of the expression's or value 1861 /// declaration's type. 1862 /// 1863 /// In case of a value declaration (for example a variable declaration), 1864 /// this resolves one layer of indirection. For example, in the value 1865 /// declaration "X x;", recordDecl(hasName("X")) matches the declaration of X, 1866 /// while varDecl(hasType(recordDecl(hasName("X")))) matches the declaration 1867 /// of x." 1868 /// 1869 /// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1870 /// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1871 /// \code 1872 /// class X {}; 1873 /// void y(X &x) { x; X z; } 1874 /// \endcode 1875 /// 1876 /// Usable as: Matcher<Expr>, Matcher<ValueDecl> 1877 AST_POLYMORPHIC_MATCHER_P_OVERLOAD( 1878 hasType, AST_POLYMORPHIC_SUPPORTED_TYPES_2(Expr, ValueDecl), 1879 internal::Matcher<Decl>, InnerMatcher, 1) { 1880 return qualType(hasDeclaration(InnerMatcher)) 1881 .matches(Node.getType(), Finder, Builder); 1882 } 1883 1884 /// \brief Matches if the type location of the declarator decl's type matches 1885 /// the inner matcher. 1886 /// 1887 /// Given 1888 /// \code 1889 /// int x; 1890 /// \endcode 1891 /// declaratorDecl(hasTypeLoc(loc(asString("int")))) 1892 /// matches int x 1893 AST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) { 1894 if (!Node.getTypeSourceInfo()) 1895 // This happens for example for implicit destructors. 1896 return false; 1897 return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder); 1898 } 1899 1900 /// \brief Matches if the matched type is represented by the given string. 1901 /// 1902 /// Given 1903 /// \code 1904 /// class Y { public: void x(); }; 1905 /// void z() { Y* y; y->x(); } 1906 /// \endcode 1907 /// callExpr(on(hasType(asString("class Y *")))) 1908 /// matches y->x() 1909 AST_MATCHER_P(QualType, asString, std::string, Name) { 1910 return Name == Node.getAsString(); 1911 } 1912 1913 /// \brief Matches if the matched type is a pointer type and the pointee type 1914 /// matches the specified matcher. 1915 /// 1916 /// Example matches y->x() 1917 /// (matcher = callExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))))) 1918 /// \code 1919 /// class Y { public: void x(); }; 1920 /// void z() { Y *y; y->x(); } 1921 /// \endcode 1922 AST_MATCHER_P( 1923 QualType, pointsTo, internal::Matcher<QualType>, 1924 InnerMatcher) { 1925 return (!Node.isNull() && Node->isPointerType() && 1926 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1927 } 1928 1929 /// \brief Overloaded to match the pointee type's declaration. 1930 AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>, 1931 InnerMatcher, 1) { 1932 return pointsTo(qualType(hasDeclaration(InnerMatcher))) 1933 .matches(Node, Finder, Builder); 1934 } 1935 1936 /// \brief Matches if the matched type is a reference type and the referenced 1937 /// type matches the specified matcher. 1938 /// 1939 /// Example matches X &x and const X &y 1940 /// (matcher = varDecl(hasType(references(recordDecl(hasName("X")))))) 1941 /// \code 1942 /// class X { 1943 /// void a(X b) { 1944 /// X &x = b; 1945 /// const X &y = b; 1946 /// }; 1947 /// \endcode 1948 AST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 1949 InnerMatcher) { 1950 return (!Node.isNull() && Node->isReferenceType() && 1951 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1952 } 1953 1954 /// \brief Matches QualTypes whose canonical type matches InnerMatcher. 1955 /// 1956 /// Given: 1957 /// \code 1958 /// typedef int &int_ref; 1959 /// int a; 1960 /// int_ref b = a; 1961 /// \code 1962 /// 1963 /// \c varDecl(hasType(qualType(referenceType()))))) will not match the 1964 /// declaration of b but \c 1965 /// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does. 1966 AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>, 1967 InnerMatcher) { 1968 if (Node.isNull()) 1969 return false; 1970 return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder); 1971 } 1972 1973 /// \brief Overloaded to match the referenced type's declaration. 1974 AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>, 1975 InnerMatcher, 1) { 1976 return references(qualType(hasDeclaration(InnerMatcher))) 1977 .matches(Node, Finder, Builder); 1978 } 1979 1980 AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 1981 internal::Matcher<Expr>, InnerMatcher) { 1982 const Expr *ExprNode = Node.getImplicitObjectArgument(); 1983 return (ExprNode != NULL && 1984 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1985 } 1986 1987 /// \brief Matches if the expression's type either matches the specified 1988 /// matcher, or is a pointer to a type that matches the InnerMatcher. 1989 AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 1990 internal::Matcher<QualType>, InnerMatcher, 0) { 1991 return onImplicitObjectArgument( 1992 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 1993 .matches(Node, Finder, Builder); 1994 } 1995 1996 /// \brief Overloaded to match the type's declaration. 1997 AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 1998 internal::Matcher<Decl>, InnerMatcher, 1) { 1999 return onImplicitObjectArgument( 2000 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 2001 .matches(Node, Finder, Builder); 2002 } 2003 2004 /// \brief Matches a DeclRefExpr that refers to a declaration that matches the 2005 /// specified matcher. 2006 /// 2007 /// Example matches x in if(x) 2008 /// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 2009 /// \code 2010 /// bool x; 2011 /// if (x) {} 2012 /// \endcode 2013 AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 2014 InnerMatcher) { 2015 const Decl *DeclNode = Node.getDecl(); 2016 return (DeclNode != NULL && 2017 InnerMatcher.matches(*DeclNode, Finder, Builder)); 2018 } 2019 2020 /// \brief Matches a \c DeclRefExpr that refers to a declaration through a 2021 /// specific using shadow declaration. 2022 /// 2023 /// FIXME: This currently only works for functions. Fix. 2024 /// 2025 /// Given 2026 /// \code 2027 /// namespace a { void f() {} } 2028 /// using a::f; 2029 /// void g() { 2030 /// f(); // Matches this .. 2031 /// a::f(); // .. but not this. 2032 /// } 2033 /// \endcode 2034 /// declRefExpr(throughUsingDeclaration(anything())) 2035 /// matches \c f() 2036 AST_MATCHER_P(DeclRefExpr, throughUsingDecl, 2037 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 2038 const NamedDecl *FoundDecl = Node.getFoundDecl(); 2039 if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl)) 2040 return InnerMatcher.matches(*UsingDecl, Finder, Builder); 2041 return false; 2042 } 2043 2044 /// \brief Matches the Decl of a DeclStmt which has a single declaration. 2045 /// 2046 /// Given 2047 /// \code 2048 /// int a, b; 2049 /// int c; 2050 /// \endcode 2051 /// declStmt(hasSingleDecl(anything())) 2052 /// matches 'int c;' but not 'int a, b;'. 2053 AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 2054 if (Node.isSingleDecl()) { 2055 const Decl *FoundDecl = Node.getSingleDecl(); 2056 return InnerMatcher.matches(*FoundDecl, Finder, Builder); 2057 } 2058 return false; 2059 } 2060 2061 /// \brief Matches a variable declaration that has an initializer expression 2062 /// that matches the given matcher. 2063 /// 2064 /// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 2065 /// \code 2066 /// bool y() { return true; } 2067 /// bool x = y(); 2068 /// \endcode 2069 AST_MATCHER_P( 2070 VarDecl, hasInitializer, internal::Matcher<Expr>, 2071 InnerMatcher) { 2072 const Expr *Initializer = Node.getAnyInitializer(); 2073 return (Initializer != NULL && 2074 InnerMatcher.matches(*Initializer, Finder, Builder)); 2075 } 2076 2077 /// \brief Checks that a call expression or a constructor call expression has 2078 /// a specific number of arguments (including absent default arguments). 2079 /// 2080 /// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 2081 /// \code 2082 /// void f(int x, int y); 2083 /// f(0, 0); 2084 /// \endcode 2085 AST_POLYMORPHIC_MATCHER_P(argumentCountIs, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 2086 CallExpr, CXXConstructExpr), 2087 unsigned, N) { 2088 return Node.getNumArgs() == N; 2089 } 2090 2091 /// \brief Matches the n'th argument of a call expression or a constructor 2092 /// call expression. 2093 /// 2094 /// Example matches y in x(y) 2095 /// (matcher = callExpr(hasArgument(0, declRefExpr()))) 2096 /// \code 2097 /// void x(int) { int y; x(y); } 2098 /// \endcode 2099 AST_POLYMORPHIC_MATCHER_P2( 2100 hasArgument, 2101 AST_POLYMORPHIC_SUPPORTED_TYPES_2(CallExpr, CXXConstructExpr), 2102 unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 2103 return (N < Node.getNumArgs() && 2104 InnerMatcher.matches( 2105 *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 2106 } 2107 2108 /// \brief Matches declaration statements that contain a specific number of 2109 /// declarations. 2110 /// 2111 /// Example: Given 2112 /// \code 2113 /// int a, b; 2114 /// int c; 2115 /// int d = 2, e; 2116 /// \endcode 2117 /// declCountIs(2) 2118 /// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 2119 AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 2120 return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 2121 } 2122 2123 /// \brief Matches the n'th declaration of a declaration statement. 2124 /// 2125 /// Note that this does not work for global declarations because the AST 2126 /// breaks up multiple-declaration DeclStmt's into multiple single-declaration 2127 /// DeclStmt's. 2128 /// Example: Given non-global declarations 2129 /// \code 2130 /// int a, b = 0; 2131 /// int c; 2132 /// int d = 2, e; 2133 /// \endcode 2134 /// declStmt(containsDeclaration( 2135 /// 0, varDecl(hasInitializer(anything())))) 2136 /// matches only 'int d = 2, e;', and 2137 /// declStmt(containsDeclaration(1, varDecl())) 2138 /// \code 2139 /// matches 'int a, b = 0' as well as 'int d = 2, e;' 2140 /// but 'int c;' is not matched. 2141 /// \endcode 2142 AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 2143 internal::Matcher<Decl>, InnerMatcher) { 2144 const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 2145 if (N >= NumDecls) 2146 return false; 2147 DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 2148 std::advance(Iterator, N); 2149 return InnerMatcher.matches(**Iterator, Finder, Builder); 2150 } 2151 2152 /// \brief Matches a constructor initializer. 2153 /// 2154 /// Given 2155 /// \code 2156 /// struct Foo { 2157 /// Foo() : foo_(1) { } 2158 /// int foo_; 2159 /// }; 2160 /// \endcode 2161 /// recordDecl(has(constructorDecl(hasAnyConstructorInitializer(anything())))) 2162 /// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 2163 AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 2164 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 2165 return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(), 2166 Node.init_end(), Finder, Builder); 2167 } 2168 2169 /// \brief Matches the field declaration of a constructor initializer. 2170 /// 2171 /// Given 2172 /// \code 2173 /// struct Foo { 2174 /// Foo() : foo_(1) { } 2175 /// int foo_; 2176 /// }; 2177 /// \endcode 2178 /// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 2179 /// forField(hasName("foo_")))))) 2180 /// matches Foo 2181 /// with forField matching foo_ 2182 AST_MATCHER_P(CXXCtorInitializer, forField, 2183 internal::Matcher<FieldDecl>, InnerMatcher) { 2184 const FieldDecl *NodeAsDecl = Node.getMember(); 2185 return (NodeAsDecl != NULL && 2186 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 2187 } 2188 2189 /// \brief Matches the initializer expression of a constructor initializer. 2190 /// 2191 /// Given 2192 /// \code 2193 /// struct Foo { 2194 /// Foo() : foo_(1) { } 2195 /// int foo_; 2196 /// }; 2197 /// \endcode 2198 /// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 2199 /// withInitializer(integerLiteral(equals(1))))))) 2200 /// matches Foo 2201 /// with withInitializer matching (1) 2202 AST_MATCHER_P(CXXCtorInitializer, withInitializer, 2203 internal::Matcher<Expr>, InnerMatcher) { 2204 const Expr* NodeAsExpr = Node.getInit(); 2205 return (NodeAsExpr != NULL && 2206 InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 2207 } 2208 2209 /// \brief Matches a contructor initializer if it is explicitly written in 2210 /// code (as opposed to implicitly added by the compiler). 2211 /// 2212 /// Given 2213 /// \code 2214 /// struct Foo { 2215 /// Foo() { } 2216 /// Foo(int) : foo_("A") { } 2217 /// string foo_; 2218 /// }; 2219 /// \endcode 2220 /// constructorDecl(hasAnyConstructorInitializer(isWritten())) 2221 /// will match Foo(int), but not Foo() 2222 AST_MATCHER(CXXCtorInitializer, isWritten) { 2223 return Node.isWritten(); 2224 } 2225 2226 /// \brief Matches a constructor declaration that has been implicitly added 2227 /// by the compiler (eg. implicit default/copy constructors). 2228 AST_MATCHER(CXXConstructorDecl, isImplicit) { 2229 return Node.isImplicit(); 2230 } 2231 2232 /// \brief Matches any argument of a call expression or a constructor call 2233 /// expression. 2234 /// 2235 /// Given 2236 /// \code 2237 /// void x(int, int, int) { int y; x(1, y, 42); } 2238 /// \endcode 2239 /// callExpr(hasAnyArgument(declRefExpr())) 2240 /// matches x(1, y, 42) 2241 /// with hasAnyArgument(...) 2242 /// matching y 2243 /// 2244 /// FIXME: Currently this will ignore parentheses and implicit casts on 2245 /// the argument before applying the inner matcher. We'll want to remove 2246 /// this to allow for greater control by the user once \c ignoreImplicit() 2247 /// has been implemented. 2248 AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 2249 CallExpr, CXXConstructExpr), 2250 internal::Matcher<Expr>, InnerMatcher) { 2251 for (unsigned I = 0; I < Node.getNumArgs(); ++I) { 2252 BoundNodesTreeBuilder Result(*Builder); 2253 if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(), Finder, 2254 &Result)) { 2255 *Builder = Result; 2256 return true; 2257 } 2258 } 2259 return false; 2260 } 2261 2262 /// \brief Matches the n'th parameter of a function declaration. 2263 /// 2264 /// Given 2265 /// \code 2266 /// class X { void f(int x) {} }; 2267 /// \endcode 2268 /// methodDecl(hasParameter(0, hasType(varDecl()))) 2269 /// matches f(int x) {} 2270 /// with hasParameter(...) 2271 /// matching int x 2272 AST_MATCHER_P2(FunctionDecl, hasParameter, 2273 unsigned, N, internal::Matcher<ParmVarDecl>, 2274 InnerMatcher) { 2275 return (N < Node.getNumParams() && 2276 InnerMatcher.matches( 2277 *Node.getParamDecl(N), Finder, Builder)); 2278 } 2279 2280 /// \brief Matches any parameter of a function declaration. 2281 /// 2282 /// Does not match the 'this' parameter of a method. 2283 /// 2284 /// Given 2285 /// \code 2286 /// class X { void f(int x, int y, int z) {} }; 2287 /// \endcode 2288 /// methodDecl(hasAnyParameter(hasName("y"))) 2289 /// matches f(int x, int y, int z) {} 2290 /// with hasAnyParameter(...) 2291 /// matching int y 2292 AST_MATCHER_P(FunctionDecl, hasAnyParameter, 2293 internal::Matcher<ParmVarDecl>, InnerMatcher) { 2294 return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(), 2295 Node.param_end(), Finder, Builder); 2296 } 2297 2298 /// \brief Matches \c FunctionDecls that have a specific parameter count. 2299 /// 2300 /// Given 2301 /// \code 2302 /// void f(int i) {} 2303 /// void g(int i, int j) {} 2304 /// \endcode 2305 /// functionDecl(parameterCountIs(2)) 2306 /// matches g(int i, int j) {} 2307 AST_MATCHER_P(FunctionDecl, parameterCountIs, unsigned, N) { 2308 return Node.getNumParams() == N; 2309 } 2310 2311 /// \brief Matches the return type of a function declaration. 2312 /// 2313 /// Given: 2314 /// \code 2315 /// class X { int f() { return 1; } }; 2316 /// \endcode 2317 /// methodDecl(returns(asString("int"))) 2318 /// matches int f() { return 1; } 2319 AST_MATCHER_P(FunctionDecl, returns, 2320 internal::Matcher<QualType>, InnerMatcher) { 2321 return InnerMatcher.matches(Node.getResultType(), Finder, Builder); 2322 } 2323 2324 /// \brief Matches extern "C" function declarations. 2325 /// 2326 /// Given: 2327 /// \code 2328 /// extern "C" void f() {} 2329 /// extern "C" { void g() {} } 2330 /// void h() {} 2331 /// \endcode 2332 /// functionDecl(isExternC()) 2333 /// matches the declaration of f and g, but not the declaration h 2334 AST_MATCHER(FunctionDecl, isExternC) { 2335 return Node.isExternC(); 2336 } 2337 2338 /// \brief Matches the condition expression of an if statement, for loop, 2339 /// or conditional operator. 2340 /// 2341 /// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 2342 /// \code 2343 /// if (true) {} 2344 /// \endcode 2345 AST_POLYMORPHIC_MATCHER_P( 2346 hasCondition, AST_POLYMORPHIC_SUPPORTED_TYPES_5( 2347 IfStmt, ForStmt, WhileStmt, DoStmt, ConditionalOperator), 2348 internal::Matcher<Expr>, InnerMatcher) { 2349 const Expr *const Condition = Node.getCond(); 2350 return (Condition != NULL && 2351 InnerMatcher.matches(*Condition, Finder, Builder)); 2352 } 2353 2354 namespace internal { 2355 struct NotEqualsBoundNodePredicate { 2356 bool operator()(const internal::BoundNodesMap &Nodes) const { 2357 return Nodes.getNode(ID) != Node; 2358 } 2359 std::string ID; 2360 ast_type_traits::DynTypedNode Node; 2361 }; 2362 } // namespace internal 2363 2364 /// \brief Matches if a node equals a previously bound node. 2365 /// 2366 /// Matches a node if it equals the node previously bound to \p ID. 2367 /// 2368 /// Given 2369 /// \code 2370 /// class X { int a; int b; }; 2371 /// \endcode 2372 /// recordDecl( 2373 /// has(fieldDecl(hasName("a"), hasType(type().bind("t")))), 2374 /// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))) 2375 /// matches the class \c X, as \c a and \c b have the same type. 2376 /// 2377 /// Note that when multiple matches are involved via \c forEach* matchers, 2378 /// \c equalsBoundNodes acts as a filter. 2379 /// For example: 2380 /// compoundStmt( 2381 /// forEachDescendant(varDecl().bind("d")), 2382 /// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))) 2383 /// will trigger a match for each combination of variable declaration 2384 /// and reference to that variable declaration within a compound statement. 2385 AST_POLYMORPHIC_MATCHER_P(equalsBoundNode, AST_POLYMORPHIC_SUPPORTED_TYPES_4( 2386 Stmt, Decl, Type, QualType), 2387 std::string, ID) { 2388 // FIXME: Figure out whether it makes sense to allow this 2389 // on any other node types. 2390 // For *Loc it probably does not make sense, as those seem 2391 // unique. For NestedNameSepcifier it might make sense, as 2392 // those also have pointer identity, but I'm not sure whether 2393 // they're ever reused. 2394 internal::NotEqualsBoundNodePredicate Predicate; 2395 Predicate.ID = ID; 2396 Predicate.Node = ast_type_traits::DynTypedNode::create(Node); 2397 return Builder->removeBindings(Predicate); 2398 } 2399 2400 /// \brief Matches the condition variable statement in an if statement. 2401 /// 2402 /// Given 2403 /// \code 2404 /// if (A* a = GetAPointer()) {} 2405 /// \endcode 2406 /// hasConditionVariableStatment(...) 2407 /// matches 'A* a = GetAPointer()'. 2408 AST_MATCHER_P(IfStmt, hasConditionVariableStatement, 2409 internal::Matcher<DeclStmt>, InnerMatcher) { 2410 const DeclStmt* const DeclarationStatement = 2411 Node.getConditionVariableDeclStmt(); 2412 return DeclarationStatement != NULL && 2413 InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 2414 } 2415 2416 /// \brief Matches the index expression of an array subscript expression. 2417 /// 2418 /// Given 2419 /// \code 2420 /// int i[5]; 2421 /// void f() { i[1] = 42; } 2422 /// \endcode 2423 /// arraySubscriptExpression(hasIndex(integerLiteral())) 2424 /// matches \c i[1] with the \c integerLiteral() matching \c 1 2425 AST_MATCHER_P(ArraySubscriptExpr, hasIndex, 2426 internal::Matcher<Expr>, InnerMatcher) { 2427 if (const Expr* Expression = Node.getIdx()) 2428 return InnerMatcher.matches(*Expression, Finder, Builder); 2429 return false; 2430 } 2431 2432 /// \brief Matches the base expression of an array subscript expression. 2433 /// 2434 /// Given 2435 /// \code 2436 /// int i[5]; 2437 /// void f() { i[1] = 42; } 2438 /// \endcode 2439 /// arraySubscriptExpression(hasBase(implicitCastExpr( 2440 /// hasSourceExpression(declRefExpr())))) 2441 /// matches \c i[1] with the \c declRefExpr() matching \c i 2442 AST_MATCHER_P(ArraySubscriptExpr, hasBase, 2443 internal::Matcher<Expr>, InnerMatcher) { 2444 if (const Expr* Expression = Node.getBase()) 2445 return InnerMatcher.matches(*Expression, Finder, Builder); 2446 return false; 2447 } 2448 2449 /// \brief Matches a 'for', 'while', or 'do while' statement that has 2450 /// a given body. 2451 /// 2452 /// Given 2453 /// \code 2454 /// for (;;) {} 2455 /// \endcode 2456 /// hasBody(compoundStmt()) 2457 /// matches 'for (;;) {}' 2458 /// with compoundStmt() 2459 /// matching '{}' 2460 AST_POLYMORPHIC_MATCHER_P( 2461 hasBody, AST_POLYMORPHIC_SUPPORTED_TYPES_3(DoStmt, ForStmt, WhileStmt), 2462 internal::Matcher<Stmt>, InnerMatcher) { 2463 const Stmt *const Statement = Node.getBody(); 2464 return (Statement != NULL && 2465 InnerMatcher.matches(*Statement, Finder, Builder)); 2466 } 2467 2468 /// \brief Matches compound statements where at least one substatement matches 2469 /// a given matcher. 2470 /// 2471 /// Given 2472 /// \code 2473 /// { {}; 1+2; } 2474 /// \endcode 2475 /// hasAnySubstatement(compoundStmt()) 2476 /// matches '{ {}; 1+2; }' 2477 /// with compoundStmt() 2478 /// matching '{}' 2479 AST_MATCHER_P(CompoundStmt, hasAnySubstatement, 2480 internal::Matcher<Stmt>, InnerMatcher) { 2481 return matchesFirstInPointerRange(InnerMatcher, Node.body_begin(), 2482 Node.body_end(), Finder, Builder); 2483 } 2484 2485 /// \brief Checks that a compound statement contains a specific number of 2486 /// child statements. 2487 /// 2488 /// Example: Given 2489 /// \code 2490 /// { for (;;) {} } 2491 /// \endcode 2492 /// compoundStmt(statementCountIs(0))) 2493 /// matches '{}' 2494 /// but does not match the outer compound statement. 2495 AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 2496 return Node.size() == N; 2497 } 2498 2499 /// \brief Matches literals that are equal to the given value. 2500 /// 2501 /// Example matches true (matcher = boolLiteral(equals(true))) 2502 /// \code 2503 /// true 2504 /// \endcode 2505 /// 2506 /// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 2507 /// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 2508 template <typename ValueT> 2509 internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 2510 equals(const ValueT &Value) { 2511 return internal::PolymorphicMatcherWithParam1< 2512 internal::ValueEqualsMatcher, 2513 ValueT>(Value); 2514 } 2515 2516 /// \brief Matches the operator Name of operator expressions (binary or 2517 /// unary). 2518 /// 2519 /// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 2520 /// \code 2521 /// !(a || b) 2522 /// \endcode 2523 AST_POLYMORPHIC_MATCHER_P(hasOperatorName, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 2524 BinaryOperator, UnaryOperator), 2525 std::string, Name) { 2526 return Name == Node.getOpcodeStr(Node.getOpcode()); 2527 } 2528 2529 /// \brief Matches the left hand side of binary operator expressions. 2530 /// 2531 /// Example matches a (matcher = binaryOperator(hasLHS())) 2532 /// \code 2533 /// a || b 2534 /// \endcode 2535 AST_MATCHER_P(BinaryOperator, hasLHS, 2536 internal::Matcher<Expr>, InnerMatcher) { 2537 Expr *LeftHandSide = Node.getLHS(); 2538 return (LeftHandSide != NULL && 2539 InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 2540 } 2541 2542 /// \brief Matches the right hand side of binary operator expressions. 2543 /// 2544 /// Example matches b (matcher = binaryOperator(hasRHS())) 2545 /// \code 2546 /// a || b 2547 /// \endcode 2548 AST_MATCHER_P(BinaryOperator, hasRHS, 2549 internal::Matcher<Expr>, InnerMatcher) { 2550 Expr *RightHandSide = Node.getRHS(); 2551 return (RightHandSide != NULL && 2552 InnerMatcher.matches(*RightHandSide, Finder, Builder)); 2553 } 2554 2555 /// \brief Matches if either the left hand side or the right hand side of a 2556 /// binary operator matches. 2557 inline internal::Matcher<BinaryOperator> hasEitherOperand( 2558 const internal::Matcher<Expr> &InnerMatcher) { 2559 return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 2560 } 2561 2562 /// \brief Matches if the operand of a unary operator matches. 2563 /// 2564 /// Example matches true (matcher = hasUnaryOperand(boolLiteral(equals(true)))) 2565 /// \code 2566 /// !true 2567 /// \endcode 2568 AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 2569 internal::Matcher<Expr>, InnerMatcher) { 2570 const Expr * const Operand = Node.getSubExpr(); 2571 return (Operand != NULL && 2572 InnerMatcher.matches(*Operand, Finder, Builder)); 2573 } 2574 2575 /// \brief Matches if the cast's source expression matches the given matcher. 2576 /// 2577 /// Example: matches "a string" (matcher = 2578 /// hasSourceExpression(constructExpr())) 2579 /// \code 2580 /// class URL { URL(string); }; 2581 /// URL url = "a string"; 2582 AST_MATCHER_P(CastExpr, hasSourceExpression, 2583 internal::Matcher<Expr>, InnerMatcher) { 2584 const Expr* const SubExpression = Node.getSubExpr(); 2585 return (SubExpression != NULL && 2586 InnerMatcher.matches(*SubExpression, Finder, Builder)); 2587 } 2588 2589 /// \brief Matches casts whose destination type matches a given matcher. 2590 /// 2591 /// (Note: Clang's AST refers to other conversions as "casts" too, and calls 2592 /// actual casts "explicit" casts.) 2593 AST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 2594 internal::Matcher<QualType>, InnerMatcher) { 2595 const QualType NodeType = Node.getTypeAsWritten(); 2596 return InnerMatcher.matches(NodeType, Finder, Builder); 2597 } 2598 2599 /// \brief Matches implicit casts whose destination type matches a given 2600 /// matcher. 2601 /// 2602 /// FIXME: Unit test this matcher 2603 AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 2604 internal::Matcher<QualType>, InnerMatcher) { 2605 return InnerMatcher.matches(Node.getType(), Finder, Builder); 2606 } 2607 2608 /// \brief Matches the true branch expression of a conditional operator. 2609 /// 2610 /// Example matches a 2611 /// \code 2612 /// condition ? a : b 2613 /// \endcode 2614 AST_MATCHER_P(ConditionalOperator, hasTrueExpression, 2615 internal::Matcher<Expr>, InnerMatcher) { 2616 Expr *Expression = Node.getTrueExpr(); 2617 return (Expression != NULL && 2618 InnerMatcher.matches(*Expression, Finder, Builder)); 2619 } 2620 2621 /// \brief Matches the false branch expression of a conditional operator. 2622 /// 2623 /// Example matches b 2624 /// \code 2625 /// condition ? a : b 2626 /// \endcode 2627 AST_MATCHER_P(ConditionalOperator, hasFalseExpression, 2628 internal::Matcher<Expr>, InnerMatcher) { 2629 Expr *Expression = Node.getFalseExpr(); 2630 return (Expression != NULL && 2631 InnerMatcher.matches(*Expression, Finder, Builder)); 2632 } 2633 2634 /// \brief Matches if a declaration has a body attached. 2635 /// 2636 /// Example matches A, va, fa 2637 /// \code 2638 /// class A {}; 2639 /// class B; // Doesn't match, as it has no body. 2640 /// int va; 2641 /// extern int vb; // Doesn't match, as it doesn't define the variable. 2642 /// void fa() {} 2643 /// void fb(); // Doesn't match, as it has no body. 2644 /// \endcode 2645 /// 2646 /// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 2647 AST_POLYMORPHIC_MATCHER(isDefinition, AST_POLYMORPHIC_SUPPORTED_TYPES_3( 2648 TagDecl, VarDecl, FunctionDecl)) { 2649 return Node.isThisDeclarationADefinition(); 2650 } 2651 2652 /// \brief Matches the class declaration that the given method declaration 2653 /// belongs to. 2654 /// 2655 /// FIXME: Generalize this for other kinds of declarations. 2656 /// FIXME: What other kind of declarations would we need to generalize 2657 /// this to? 2658 /// 2659 /// Example matches A() in the last line 2660 /// (matcher = constructExpr(hasDeclaration(methodDecl( 2661 /// ofClass(hasName("A")))))) 2662 /// \code 2663 /// class A { 2664 /// public: 2665 /// A(); 2666 /// }; 2667 /// A a = A(); 2668 /// \endcode 2669 AST_MATCHER_P(CXXMethodDecl, ofClass, 2670 internal::Matcher<CXXRecordDecl>, InnerMatcher) { 2671 const CXXRecordDecl *Parent = Node.getParent(); 2672 return (Parent != NULL && 2673 InnerMatcher.matches(*Parent, Finder, Builder)); 2674 } 2675 2676 /// \brief Matches if the given method declaration is virtual. 2677 /// 2678 /// Given 2679 /// \code 2680 /// class A { 2681 /// public: 2682 /// virtual void x(); 2683 /// }; 2684 /// \endcode 2685 /// matches A::x 2686 AST_MATCHER(CXXMethodDecl, isVirtual) { 2687 return Node.isVirtual(); 2688 } 2689 2690 /// \brief Matches if the given method declaration is const. 2691 /// 2692 /// Given 2693 /// \code 2694 /// struct A { 2695 /// void foo() const; 2696 /// void bar(); 2697 /// }; 2698 /// \endcode 2699 /// 2700 /// methodDecl(isConst()) matches A::foo() but not A::bar() 2701 AST_MATCHER(CXXMethodDecl, isConst) { 2702 return Node.isConst(); 2703 } 2704 2705 /// \brief Matches if the given method declaration overrides another method. 2706 /// 2707 /// Given 2708 /// \code 2709 /// class A { 2710 /// public: 2711 /// virtual void x(); 2712 /// }; 2713 /// class B : public A { 2714 /// public: 2715 /// virtual void x(); 2716 /// }; 2717 /// \endcode 2718 /// matches B::x 2719 AST_MATCHER(CXXMethodDecl, isOverride) { 2720 return Node.size_overridden_methods() > 0; 2721 } 2722 2723 /// \brief Matches member expressions that are called with '->' as opposed 2724 /// to '.'. 2725 /// 2726 /// Member calls on the implicit this pointer match as called with '->'. 2727 /// 2728 /// Given 2729 /// \code 2730 /// class Y { 2731 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 2732 /// int a; 2733 /// static int b; 2734 /// }; 2735 /// \endcode 2736 /// memberExpr(isArrow()) 2737 /// matches this->x, x, y.x, a, this->b 2738 AST_MATCHER(MemberExpr, isArrow) { 2739 return Node.isArrow(); 2740 } 2741 2742 /// \brief Matches QualType nodes that are of integer type. 2743 /// 2744 /// Given 2745 /// \code 2746 /// void a(int); 2747 /// void b(long); 2748 /// void c(double); 2749 /// \endcode 2750 /// functionDecl(hasAnyParameter(hasType(isInteger()))) 2751 /// matches "a(int)", "b(long)", but not "c(double)". 2752 AST_MATCHER(QualType, isInteger) { 2753 return Node->isIntegerType(); 2754 } 2755 2756 /// \brief Matches QualType nodes that are const-qualified, i.e., that 2757 /// include "top-level" const. 2758 /// 2759 /// Given 2760 /// \code 2761 /// void a(int); 2762 /// void b(int const); 2763 /// void c(const int); 2764 /// void d(const int*); 2765 /// void e(int const) {}; 2766 /// \endcode 2767 /// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 2768 /// matches "void b(int const)", "void c(const int)" and 2769 /// "void e(int const) {}". It does not match d as there 2770 /// is no top-level const on the parameter type "const int *". 2771 AST_MATCHER(QualType, isConstQualified) { 2772 return Node.isConstQualified(); 2773 } 2774 2775 /// \brief Matches QualType nodes that have local CV-qualifiers attached to 2776 /// the node, not hidden within a typedef. 2777 /// 2778 /// Given 2779 /// \code 2780 /// typedef const int const_int; 2781 /// const_int i; 2782 /// int *const j; 2783 /// int *volatile k; 2784 /// int m; 2785 /// \endcode 2786 /// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k. 2787 /// \c i is const-qualified but the qualifier is not local. 2788 AST_MATCHER(QualType, hasLocalQualifiers) { 2789 return Node.hasLocalQualifiers(); 2790 } 2791 2792 /// \brief Matches a member expression where the member is matched by a 2793 /// given matcher. 2794 /// 2795 /// Given 2796 /// \code 2797 /// struct { int first, second; } first, second; 2798 /// int i(second.first); 2799 /// int j(first.second); 2800 /// \endcode 2801 /// memberExpr(member(hasName("first"))) 2802 /// matches second.first 2803 /// but not first.second (because the member name there is "second"). 2804 AST_MATCHER_P(MemberExpr, member, 2805 internal::Matcher<ValueDecl>, InnerMatcher) { 2806 return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 2807 } 2808 2809 /// \brief Matches a member expression where the object expression is 2810 /// matched by a given matcher. 2811 /// 2812 /// Given 2813 /// \code 2814 /// struct X { int m; }; 2815 /// void f(X x) { x.m; m; } 2816 /// \endcode 2817 /// memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))) 2818 /// matches "x.m" and "m" 2819 /// with hasObjectExpression(...) 2820 /// matching "x" and the implicit object expression of "m" which has type X*. 2821 AST_MATCHER_P(MemberExpr, hasObjectExpression, 2822 internal::Matcher<Expr>, InnerMatcher) { 2823 return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 2824 } 2825 2826 /// \brief Matches any using shadow declaration. 2827 /// 2828 /// Given 2829 /// \code 2830 /// namespace X { void b(); } 2831 /// using X::b; 2832 /// \endcode 2833 /// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 2834 /// matches \code using X::b \endcode 2835 AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 2836 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 2837 return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(), 2838 Node.shadow_end(), Finder, Builder); 2839 } 2840 2841 /// \brief Matches a using shadow declaration where the target declaration is 2842 /// matched by the given matcher. 2843 /// 2844 /// Given 2845 /// \code 2846 /// namespace X { int a; void b(); } 2847 /// using X::a; 2848 /// using X::b; 2849 /// \endcode 2850 /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 2851 /// matches \code using X::b \endcode 2852 /// but not \code using X::a \endcode 2853 AST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 2854 internal::Matcher<NamedDecl>, InnerMatcher) { 2855 return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 2856 } 2857 2858 /// \brief Matches template instantiations of function, class, or static 2859 /// member variable template instantiations. 2860 /// 2861 /// Given 2862 /// \code 2863 /// template <typename T> class X {}; class A {}; X<A> x; 2864 /// \endcode 2865 /// or 2866 /// \code 2867 /// template <typename T> class X {}; class A {}; template class X<A>; 2868 /// \endcode 2869 /// recordDecl(hasName("::X"), isTemplateInstantiation()) 2870 /// matches the template instantiation of X<A>. 2871 /// 2872 /// But given 2873 /// \code 2874 /// template <typename T> class X {}; class A {}; 2875 /// template <> class X<A> {}; X<A> x; 2876 /// \endcode 2877 /// recordDecl(hasName("::X"), isTemplateInstantiation()) 2878 /// does not match, as X<A> is an explicit template specialization. 2879 /// 2880 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2881 AST_POLYMORPHIC_MATCHER( 2882 isTemplateInstantiation, 2883 AST_POLYMORPHIC_SUPPORTED_TYPES_3(FunctionDecl, VarDecl, CXXRecordDecl)) { 2884 return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation || 2885 Node.getTemplateSpecializationKind() == 2886 TSK_ExplicitInstantiationDefinition); 2887 } 2888 2889 /// \brief Matches explicit template specializations of function, class, or 2890 /// static member variable template instantiations. 2891 /// 2892 /// Given 2893 /// \code 2894 /// template<typename T> void A(T t) { } 2895 /// template<> void A(int N) { } 2896 /// \endcode 2897 /// functionDecl(isExplicitTemplateSpecialization()) 2898 /// matches the specialization A<int>(). 2899 /// 2900 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2901 AST_POLYMORPHIC_MATCHER( 2902 isExplicitTemplateSpecialization, 2903 AST_POLYMORPHIC_SUPPORTED_TYPES_3(FunctionDecl, VarDecl, CXXRecordDecl)) { 2904 return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization); 2905 } 2906 2907 /// \brief Matches \c TypeLocs for which the given inner 2908 /// QualType-matcher matches. 2909 inline internal::BindableMatcher<TypeLoc> loc( 2910 const internal::Matcher<QualType> &InnerMatcher) { 2911 return internal::BindableMatcher<TypeLoc>( 2912 new internal::TypeLocTypeMatcher(InnerMatcher)); 2913 } 2914 2915 /// \brief Matches builtin Types. 2916 /// 2917 /// Given 2918 /// \code 2919 /// struct A {}; 2920 /// A a; 2921 /// int b; 2922 /// float c; 2923 /// bool d; 2924 /// \endcode 2925 /// builtinType() 2926 /// matches "int b", "float c" and "bool d" 2927 AST_TYPE_MATCHER(BuiltinType, builtinType); 2928 2929 /// \brief Matches all kinds of arrays. 2930 /// 2931 /// Given 2932 /// \code 2933 /// int a[] = { 2, 3 }; 2934 /// int b[4]; 2935 /// void f() { int c[a[0]]; } 2936 /// \endcode 2937 /// arrayType() 2938 /// matches "int a[]", "int b[4]" and "int c[a[0]]"; 2939 AST_TYPE_MATCHER(ArrayType, arrayType); 2940 2941 /// \brief Matches C99 complex types. 2942 /// 2943 /// Given 2944 /// \code 2945 /// _Complex float f; 2946 /// \endcode 2947 /// complexType() 2948 /// matches "_Complex float f" 2949 AST_TYPE_MATCHER(ComplexType, complexType); 2950 2951 /// \brief Matches arrays and C99 complex types that have a specific element 2952 /// type. 2953 /// 2954 /// Given 2955 /// \code 2956 /// struct A {}; 2957 /// A a[7]; 2958 /// int b[7]; 2959 /// \endcode 2960 /// arrayType(hasElementType(builtinType())) 2961 /// matches "int b[7]" 2962 /// 2963 /// Usable as: Matcher<ArrayType>, Matcher<ComplexType> 2964 AST_TYPELOC_TRAVERSE_MATCHER( 2965 hasElementType, getElement, 2966 AST_POLYMORPHIC_SUPPORTED_TYPES_2(ArrayType, ComplexType)); 2967 2968 /// \brief Matches C arrays with a specified constant size. 2969 /// 2970 /// Given 2971 /// \code 2972 /// void() { 2973 /// int a[2]; 2974 /// int b[] = { 2, 3 }; 2975 /// int c[b[0]]; 2976 /// } 2977 /// \endcode 2978 /// constantArrayType() 2979 /// matches "int a[2]" 2980 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType); 2981 2982 /// \brief Matches \c ConstantArrayType nodes that have the specified size. 2983 /// 2984 /// Given 2985 /// \code 2986 /// int a[42]; 2987 /// int b[2 * 21]; 2988 /// int c[41], d[43]; 2989 /// \endcode 2990 /// constantArrayType(hasSize(42)) 2991 /// matches "int a[42]" and "int b[2 * 21]" 2992 AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) { 2993 return Node.getSize() == N; 2994 } 2995 2996 /// \brief Matches C++ arrays whose size is a value-dependent expression. 2997 /// 2998 /// Given 2999 /// \code 3000 /// template<typename T, int Size> 3001 /// class array { 3002 /// T data[Size]; 3003 /// }; 3004 /// \endcode 3005 /// dependentSizedArrayType 3006 /// matches "T data[Size]" 3007 AST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType); 3008 3009 /// \brief Matches C arrays with unspecified size. 3010 /// 3011 /// Given 3012 /// \code 3013 /// int a[] = { 2, 3 }; 3014 /// int b[42]; 3015 /// void f(int c[]) { int d[a[0]]; }; 3016 /// \endcode 3017 /// incompleteArrayType() 3018 /// matches "int a[]" and "int c[]" 3019 AST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType); 3020 3021 /// \brief Matches C arrays with a specified size that is not an 3022 /// integer-constant-expression. 3023 /// 3024 /// Given 3025 /// \code 3026 /// void f() { 3027 /// int a[] = { 2, 3 } 3028 /// int b[42]; 3029 /// int c[a[0]]; 3030 /// \endcode 3031 /// variableArrayType() 3032 /// matches "int c[a[0]]" 3033 AST_TYPE_MATCHER(VariableArrayType, variableArrayType); 3034 3035 /// \brief Matches \c VariableArrayType nodes that have a specific size 3036 /// expression. 3037 /// 3038 /// Given 3039 /// \code 3040 /// void f(int b) { 3041 /// int a[b]; 3042 /// } 3043 /// \endcode 3044 /// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( 3045 /// varDecl(hasName("b"))))))) 3046 /// matches "int a[b]" 3047 AST_MATCHER_P(VariableArrayType, hasSizeExpr, 3048 internal::Matcher<Expr>, InnerMatcher) { 3049 return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder); 3050 } 3051 3052 /// \brief Matches atomic types. 3053 /// 3054 /// Given 3055 /// \code 3056 /// _Atomic(int) i; 3057 /// \endcode 3058 /// atomicType() 3059 /// matches "_Atomic(int) i" 3060 AST_TYPE_MATCHER(AtomicType, atomicType); 3061 3062 /// \brief Matches atomic types with a specific value type. 3063 /// 3064 /// Given 3065 /// \code 3066 /// _Atomic(int) i; 3067 /// _Atomic(float) f; 3068 /// \endcode 3069 /// atomicType(hasValueType(isInteger())) 3070 /// matches "_Atomic(int) i" 3071 /// 3072 /// Usable as: Matcher<AtomicType> 3073 AST_TYPELOC_TRAVERSE_MATCHER(hasValueType, getValue, 3074 AST_POLYMORPHIC_SUPPORTED_TYPES_1(AtomicType)); 3075 3076 /// \brief Matches types nodes representing C++11 auto types. 3077 /// 3078 /// Given: 3079 /// \code 3080 /// auto n = 4; 3081 /// int v[] = { 2, 3 } 3082 /// for (auto i : v) { } 3083 /// \endcode 3084 /// autoType() 3085 /// matches "auto n" and "auto i" 3086 AST_TYPE_MATCHER(AutoType, autoType); 3087 3088 /// \brief Matches \c AutoType nodes where the deduced type is a specific type. 3089 /// 3090 /// Note: There is no \c TypeLoc for the deduced type and thus no 3091 /// \c getDeducedLoc() matcher. 3092 /// 3093 /// Given 3094 /// \code 3095 /// auto a = 1; 3096 /// auto b = 2.0; 3097 /// \endcode 3098 /// autoType(hasDeducedType(isInteger())) 3099 /// matches "auto a" 3100 /// 3101 /// Usable as: Matcher<AutoType> 3102 AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType, 3103 AST_POLYMORPHIC_SUPPORTED_TYPES_1(AutoType)); 3104 3105 /// \brief Matches \c FunctionType nodes. 3106 /// 3107 /// Given 3108 /// \code 3109 /// int (*f)(int); 3110 /// void g(); 3111 /// \endcode 3112 /// functionType() 3113 /// matches "int (*f)(int)" and the type of "g". 3114 AST_TYPE_MATCHER(FunctionType, functionType); 3115 3116 /// \brief Matches \c ParenType nodes. 3117 /// 3118 /// Given 3119 /// \code 3120 /// int (*ptr_to_array)[4]; 3121 /// int *array_of_ptrs[4]; 3122 /// \endcode 3123 /// 3124 /// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not 3125 /// \c array_of_ptrs. 3126 AST_TYPE_MATCHER(ParenType, parenType); 3127 3128 /// \brief Matches \c ParenType nodes where the inner type is a specific type. 3129 /// 3130 /// Given 3131 /// \code 3132 /// int (*ptr_to_array)[4]; 3133 /// int (*ptr_to_func)(int); 3134 /// \endcode 3135 /// 3136 /// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches 3137 /// \c ptr_to_func but not \c ptr_to_array. 3138 /// 3139 /// Usable as: Matcher<ParenType> 3140 AST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType, 3141 AST_POLYMORPHIC_SUPPORTED_TYPES_1(ParenType)); 3142 3143 /// \brief Matches block pointer types, i.e. types syntactically represented as 3144 /// "void (^)(int)". 3145 /// 3146 /// The \c pointee is always required to be a \c FunctionType. 3147 AST_TYPE_MATCHER(BlockPointerType, blockPointerType); 3148 3149 /// \brief Matches member pointer types. 3150 /// Given 3151 /// \code 3152 /// struct A { int i; } 3153 /// A::* ptr = A::i; 3154 /// \endcode 3155 /// memberPointerType() 3156 /// matches "A::* ptr" 3157 AST_TYPE_MATCHER(MemberPointerType, memberPointerType); 3158 3159 /// \brief Matches pointer types. 3160 /// 3161 /// Given 3162 /// \code 3163 /// int *a; 3164 /// int &b = *a; 3165 /// int c = 5; 3166 /// \endcode 3167 /// pointerType() 3168 /// matches "int *a" 3169 AST_TYPE_MATCHER(PointerType, pointerType); 3170 3171 /// \brief Matches both lvalue and rvalue reference types. 3172 /// 3173 /// Given 3174 /// \code 3175 /// int *a; 3176 /// int &b = *a; 3177 /// int &&c = 1; 3178 /// auto &d = b; 3179 /// auto &&e = c; 3180 /// auto &&f = 2; 3181 /// int g = 5; 3182 /// \endcode 3183 /// 3184 /// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f. 3185 AST_TYPE_MATCHER(ReferenceType, referenceType); 3186 3187 /// \brief Matches lvalue reference types. 3188 /// 3189 /// Given: 3190 /// \code 3191 /// int *a; 3192 /// int &b = *a; 3193 /// int &&c = 1; 3194 /// auto &d = b; 3195 /// auto &&e = c; 3196 /// auto &&f = 2; 3197 /// int g = 5; 3198 /// \endcode 3199 /// 3200 /// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is 3201 /// matched since the type is deduced as int& by reference collapsing rules. 3202 AST_TYPE_MATCHER(LValueReferenceType, lValueReferenceType); 3203 3204 /// \brief Matches rvalue reference types. 3205 /// 3206 /// Given: 3207 /// \code 3208 /// int *a; 3209 /// int &b = *a; 3210 /// int &&c = 1; 3211 /// auto &d = b; 3212 /// auto &&e = c; 3213 /// auto &&f = 2; 3214 /// int g = 5; 3215 /// \endcode 3216 /// 3217 /// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not 3218 /// matched as it is deduced to int& by reference collapsing rules. 3219 AST_TYPE_MATCHER(RValueReferenceType, rValueReferenceType); 3220 3221 /// \brief Narrows PointerType (and similar) matchers to those where the 3222 /// \c pointee matches a given matcher. 3223 /// 3224 /// Given 3225 /// \code 3226 /// int *a; 3227 /// int const *b; 3228 /// float const *f; 3229 /// \endcode 3230 /// pointerType(pointee(isConstQualified(), isInteger())) 3231 /// matches "int const *b" 3232 /// 3233 /// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>, 3234 /// Matcher<PointerType>, Matcher<ReferenceType> 3235 AST_TYPELOC_TRAVERSE_MATCHER( 3236 pointee, getPointee, 3237 AST_POLYMORPHIC_SUPPORTED_TYPES_4(BlockPointerType, MemberPointerType, 3238 PointerType, ReferenceType)); 3239 3240 /// \brief Matches typedef types. 3241 /// 3242 /// Given 3243 /// \code 3244 /// typedef int X; 3245 /// \endcode 3246 /// typedefType() 3247 /// matches "typedef int X" 3248 AST_TYPE_MATCHER(TypedefType, typedefType); 3249 3250 /// \brief Matches template specialization types. 3251 /// 3252 /// Given 3253 /// \code 3254 /// template <typename T> 3255 /// class C { }; 3256 /// 3257 /// template class C<int>; // A 3258 /// C<char> var; // B 3259 /// \code 3260 /// 3261 /// \c templateSpecializationType() matches the type of the explicit 3262 /// instantiation in \c A and the type of the variable declaration in \c B. 3263 AST_TYPE_MATCHER(TemplateSpecializationType, templateSpecializationType); 3264 3265 /// \brief Matches types nodes representing unary type transformations. 3266 /// 3267 /// Given: 3268 /// \code 3269 /// typedef __underlying_type(T) type; 3270 /// \endcode 3271 /// unaryTransformType() 3272 /// matches "__underlying_type(T)" 3273 AST_TYPE_MATCHER(UnaryTransformType, unaryTransformType); 3274 3275 /// \brief Matches record types (e.g. structs, classes). 3276 /// 3277 /// Given 3278 /// \code 3279 /// class C {}; 3280 /// struct S {}; 3281 /// 3282 /// C c; 3283 /// S s; 3284 /// \code 3285 /// 3286 /// \c recordType() matches the type of the variable declarations of both \c c 3287 /// and \c s. 3288 AST_TYPE_MATCHER(RecordType, recordType); 3289 3290 /// \brief Matches types specified with an elaborated type keyword or with a 3291 /// qualified name. 3292 /// 3293 /// Given 3294 /// \code 3295 /// namespace N { 3296 /// namespace M { 3297 /// class D {}; 3298 /// } 3299 /// } 3300 /// class C {}; 3301 /// 3302 /// class C c; 3303 /// N::M::D d; 3304 /// \code 3305 /// 3306 /// \c elaboratedType() matches the type of the variable declarations of both 3307 /// \c c and \c d. 3308 AST_TYPE_MATCHER(ElaboratedType, elaboratedType); 3309 3310 /// \brief Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier, 3311 /// matches \c InnerMatcher if the qualifier exists. 3312 /// 3313 /// Given 3314 /// \code 3315 /// namespace N { 3316 /// namespace M { 3317 /// class D {}; 3318 /// } 3319 /// } 3320 /// N::M::D d; 3321 /// \code 3322 /// 3323 /// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))) 3324 /// matches the type of the variable declaration of \c d. 3325 AST_MATCHER_P(ElaboratedType, hasQualifier, 3326 internal::Matcher<NestedNameSpecifier>, InnerMatcher) { 3327 if (const NestedNameSpecifier *Qualifier = Node.getQualifier()) 3328 return InnerMatcher.matches(*Qualifier, Finder, Builder); 3329 3330 return false; 3331 } 3332 3333 /// \brief Matches ElaboratedTypes whose named type matches \c InnerMatcher. 3334 /// 3335 /// Given 3336 /// \code 3337 /// namespace N { 3338 /// namespace M { 3339 /// class D {}; 3340 /// } 3341 /// } 3342 /// N::M::D d; 3343 /// \code 3344 /// 3345 /// \c elaboratedType(namesType(recordType( 3346 /// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable 3347 /// declaration of \c d. 3348 AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>, 3349 InnerMatcher) { 3350 return InnerMatcher.matches(Node.getNamedType(), Finder, Builder); 3351 } 3352 3353 /// \brief Matches declarations whose declaration context, interpreted as a 3354 /// Decl, matches \c InnerMatcher. 3355 /// 3356 /// Given 3357 /// \code 3358 /// namespace N { 3359 /// namespace M { 3360 /// class D {}; 3361 /// } 3362 /// } 3363 /// \code 3364 /// 3365 /// \c recordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the 3366 /// declaration of \c class \c D. 3367 AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) { 3368 return InnerMatcher.matches(*Decl::castFromDeclContext(Node.getDeclContext()), 3369 Finder, Builder); 3370 } 3371 3372 /// \brief Matches nested name specifiers. 3373 /// 3374 /// Given 3375 /// \code 3376 /// namespace ns { 3377 /// struct A { static void f(); }; 3378 /// void A::f() {} 3379 /// void g() { A::f(); } 3380 /// } 3381 /// ns::A a; 3382 /// \endcode 3383 /// nestedNameSpecifier() 3384 /// matches "ns::" and both "A::" 3385 const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 3386 3387 /// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 3388 const internal::VariadicAllOfMatcher< 3389 NestedNameSpecifierLoc> nestedNameSpecifierLoc; 3390 3391 /// \brief Matches \c NestedNameSpecifierLocs for which the given inner 3392 /// NestedNameSpecifier-matcher matches. 3393 inline internal::BindableMatcher<NestedNameSpecifierLoc> loc( 3394 const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 3395 return internal::BindableMatcher<NestedNameSpecifierLoc>( 3396 new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 3397 InnerMatcher)); 3398 } 3399 3400 /// \brief Matches nested name specifiers that specify a type matching the 3401 /// given \c QualType matcher without qualifiers. 3402 /// 3403 /// Given 3404 /// \code 3405 /// struct A { struct B { struct C {}; }; }; 3406 /// A::B::C c; 3407 /// \endcode 3408 /// nestedNameSpecifier(specifiesType(hasDeclaration(recordDecl(hasName("A"))))) 3409 /// matches "A::" 3410 AST_MATCHER_P(NestedNameSpecifier, specifiesType, 3411 internal::Matcher<QualType>, InnerMatcher) { 3412 if (Node.getAsType() == NULL) 3413 return false; 3414 return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 3415 } 3416 3417 /// \brief Matches nested name specifier locs that specify a type matching the 3418 /// given \c TypeLoc. 3419 /// 3420 /// Given 3421 /// \code 3422 /// struct A { struct B { struct C {}; }; }; 3423 /// A::B::C c; 3424 /// \endcode 3425 /// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type( 3426 /// hasDeclaration(recordDecl(hasName("A"))))))) 3427 /// matches "A::" 3428 AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, 3429 internal::Matcher<TypeLoc>, InnerMatcher) { 3430 return InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder); 3431 } 3432 3433 /// \brief Matches on the prefix of a \c NestedNameSpecifier. 3434 /// 3435 /// Given 3436 /// \code 3437 /// struct A { struct B { struct C {}; }; }; 3438 /// A::B::C c; 3439 /// \endcode 3440 /// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 3441 /// matches "A::" 3442 AST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix, 3443 internal::Matcher<NestedNameSpecifier>, InnerMatcher, 3444 0) { 3445 NestedNameSpecifier *NextNode = Node.getPrefix(); 3446 if (NextNode == NULL) 3447 return false; 3448 return InnerMatcher.matches(*NextNode, Finder, Builder); 3449 } 3450 3451 /// \brief Matches on the prefix of a \c NestedNameSpecifierLoc. 3452 /// 3453 /// Given 3454 /// \code 3455 /// struct A { struct B { struct C {}; }; }; 3456 /// A::B::C c; 3457 /// \endcode 3458 /// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 3459 /// matches "A::" 3460 AST_MATCHER_P_OVERLOAD(NestedNameSpecifierLoc, hasPrefix, 3461 internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher, 3462 1) { 3463 NestedNameSpecifierLoc NextNode = Node.getPrefix(); 3464 if (!NextNode) 3465 return false; 3466 return InnerMatcher.matches(NextNode, Finder, Builder); 3467 } 3468 3469 /// \brief Matches nested name specifiers that specify a namespace matching the 3470 /// given namespace matcher. 3471 /// 3472 /// Given 3473 /// \code 3474 /// namespace ns { struct A {}; } 3475 /// ns::A a; 3476 /// \endcode 3477 /// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 3478 /// matches "ns::" 3479 AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 3480 internal::Matcher<NamespaceDecl>, InnerMatcher) { 3481 if (Node.getAsNamespace() == NULL) 3482 return false; 3483 return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 3484 } 3485 3486 /// \brief Overloads for the \c equalsNode matcher. 3487 /// FIXME: Implement for other node types. 3488 /// @{ 3489 3490 /// \brief Matches if a node equals another node. 3491 /// 3492 /// \c Decl has pointer identity in the AST. 3493 AST_MATCHER_P_OVERLOAD(Decl, equalsNode, Decl*, Other, 0) { 3494 return &Node == Other; 3495 } 3496 /// \brief Matches if a node equals another node. 3497 /// 3498 /// \c Stmt has pointer identity in the AST. 3499 /// 3500 AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, Stmt*, Other, 1) { 3501 return &Node == Other; 3502 } 3503 3504 /// @} 3505 3506 /// \brief Matches each case or default statement belonging to the given switch 3507 /// statement. This matcher may produce multiple matches. 3508 /// 3509 /// Given 3510 /// \code 3511 /// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } } 3512 /// \endcode 3513 /// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s") 3514 /// matches four times, with "c" binding each of "case 1:", "case 2:", 3515 /// "case 3:" and "case 4:", and "s" respectively binding "switch (1)", 3516 /// "switch (1)", "switch (2)" and "switch (2)". 3517 AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>, 3518 InnerMatcher) { 3519 BoundNodesTreeBuilder Result; 3520 // FIXME: getSwitchCaseList() does not necessarily guarantee a stable 3521 // iteration order. We should use the more general iterating matchers once 3522 // they are capable of expressing this matcher (for example, it should ignore 3523 // case statements belonging to nested switch statements). 3524 bool Matched = false; 3525 for (const SwitchCase *SC = Node.getSwitchCaseList(); SC; 3526 SC = SC->getNextSwitchCase()) { 3527 BoundNodesTreeBuilder CaseBuilder(*Builder); 3528 bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder); 3529 if (CaseMatched) { 3530 Matched = true; 3531 Result.addMatch(CaseBuilder); 3532 } 3533 } 3534 *Builder = Result; 3535 return Matched; 3536 } 3537 3538 /// \brief Matches each constructor initializer in a constructor definition. 3539 /// 3540 /// Given 3541 /// \code 3542 /// class A { A() : i(42), j(42) {} int i; int j; }; 3543 /// \endcode 3544 /// constructorDecl(forEachConstructorInitializer(forField(decl().bind("x")))) 3545 /// will trigger two matches, binding for 'i' and 'j' respectively. 3546 AST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer, 3547 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 3548 BoundNodesTreeBuilder Result; 3549 bool Matched = false; 3550 for (CXXConstructorDecl::init_const_iterator I = Node.init_begin(), 3551 E = Node.init_end(); 3552 I != E; ++I) { 3553 BoundNodesTreeBuilder InitBuilder(*Builder); 3554 if (InnerMatcher.matches(**I, Finder, &InitBuilder)) { 3555 Matched = true; 3556 Result.addMatch(InitBuilder); 3557 } 3558 } 3559 *Builder = Result; 3560 return Matched; 3561 } 3562 3563 /// \brief If the given case statement does not use the GNU case range 3564 /// extension, matches the constant given in the statement. 3565 /// 3566 /// Given 3567 /// \code 3568 /// switch (1) { case 1: case 1+1: case 3 ... 4: ; } 3569 /// \endcode 3570 /// caseStmt(hasCaseConstant(integerLiteral())) 3571 /// matches "case 1:" 3572 AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>, 3573 InnerMatcher) { 3574 if (Node.getRHS()) 3575 return false; 3576 3577 return InnerMatcher.matches(*Node.getLHS(), Finder, Builder); 3578 } 3579 3580 } // end namespace ast_matchers 3581 } // end namespace clang 3582 3583 #endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 3584