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