ASTMatchers.h revision 089e5b3bf2e57da0173b170ad501867b43395385
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// record(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// record(hasName("MyClass"), hasChild(id("child", record()))) 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 54namespace clang { 55namespace ast_matchers { 56 57/// \brief Maps string IDs to AST nodes matched by parts of a matcher. 58/// 59/// The bound nodes are generated by adding id(...) matchers into the 60/// match expression around the matchers for the nodes we want to access later. 61/// 62/// The instances of BoundNodes are created by MatchFinder when the user's 63/// callbacks are executed every time a match is found. 64class BoundNodes { 65public: 66 /// \brief Returns the AST node bound to 'ID'. 67 /// Returns NULL if there was no node bound to 'ID' or if there is a node but 68 /// it cannot be converted to the specified type. 69 /// FIXME: We'll need one of those for every base type. 70 /// @{ 71 template <typename T> 72 const T *getDeclAs(StringRef ID) const { 73 return getNodeAs<T>(DeclBindings, ID); 74 } 75 template <typename T> 76 const T *getStmtAs(StringRef ID) const { 77 return getNodeAs<T>(StmtBindings, ID); 78 } 79 /// @} 80 81private: 82 /// \brief Create BoundNodes from a pre-filled map of bindings. 83 BoundNodes(const std::map<std::string, const Decl*> &DeclBindings, 84 const std::map<std::string, const Stmt*> &StmtBindings) 85 : DeclBindings(DeclBindings), StmtBindings(StmtBindings) {} 86 87 template <typename T, typename MapT> 88 const T *getNodeAs(const MapT &Bindings, StringRef ID) const { 89 typename MapT::const_iterator It = Bindings.find(ID); 90 if (It == Bindings.end()) { 91 return NULL; 92 } 93 return llvm::dyn_cast<T>(It->second); 94 } 95 96 std::map<std::string, const Decl*> DeclBindings; 97 std::map<std::string, const Stmt*> StmtBindings; 98 99 friend class internal::BoundNodesTree; 100}; 101 102/// \brief If the provided matcher matches a node, binds the node to 'ID'. 103/// 104/// FIXME: Add example for accessing it. 105template <typename T> 106internal::Matcher<T> id(const std::string &ID, 107 const internal::BindableMatcher<T> &InnerMatcher) { 108 return InnerMatcher.bind(ID); 109} 110 111/// \brief Types of matchers for the top-level classes in the AST class 112/// hierarchy. 113/// @{ 114typedef internal::Matcher<Decl> DeclarationMatcher; 115typedef internal::Matcher<QualType> TypeMatcher; 116typedef internal::Matcher<Stmt> StatementMatcher; 117/// @} 118 119/// \brief Matches any node. 120/// 121/// Useful when another matcher requires a child matcher, but there's no 122/// additional constraint. This will often be used with an explicit conversion 123/// to a internal::Matcher<> type such as TypeMatcher. 124/// 125/// Example: DeclarationMatcher(anything()) matches all declarations, e.g., 126/// "int* p" and "void f()" in 127/// int* p; 128/// void f(); 129inline internal::PolymorphicMatcherWithParam0<internal::TrueMatcher> anything() { 130 return internal::PolymorphicMatcherWithParam0<internal::TrueMatcher>(); 131} 132 133/// \brief Matches declarations. 134/// 135/// Examples matches \c X, \c C, and the friend declaration inside \c C; 136/// \code 137/// void X(); 138/// class C { 139/// friend X; 140/// }; 141/// \endcode 142const internal::VariadicDynCastAllOfMatcher<Decl, Decl> decl; 143 144/// \brief Matches a declaration of anything that could have a name. 145/// 146/// Example matches X, S, the anonymous union type, i, and U; 147/// typedef int X; 148/// struct S { 149/// union { 150/// int i; 151/// } U; 152/// }; 153const internal::VariadicDynCastAllOfMatcher< 154 Decl, 155 NamedDecl> nameableDeclaration; 156 157/// \brief Matches C++ class declarations. 158/// 159/// Example matches X, Z 160/// class X; 161/// template<class T> class Z {}; 162const internal::VariadicDynCastAllOfMatcher< 163 Decl, 164 CXXRecordDecl> record; 165 166/// \brief Matches C++ class template specializations. 167/// 168/// Given 169/// template<typename T> class A {}; 170/// template<> class A<double> {}; 171/// A<int> a; 172/// classTemplateSpecialization() 173/// matches the specializations \c A<int> and \c A<double> 174const internal::VariadicDynCastAllOfMatcher< 175 Decl, 176 ClassTemplateSpecializationDecl> classTemplateSpecialization; 177 178/// \brief Matches classTemplateSpecializations that have at least one 179/// TemplateArgument matching the given Matcher. 180/// 181/// Given 182/// template<typename T> class A {}; 183/// template<> class A<double> {}; 184/// A<int> a; 185/// classTemplateSpecialization(hasAnyTemplateArgument( 186/// refersToType(asString("int")))) 187/// matches the specialization \c A<int> 188AST_MATCHER_P(ClassTemplateSpecializationDecl, hasAnyTemplateArgument, 189 internal::Matcher<TemplateArgument>, Matcher) { 190 const TemplateArgumentList &List = Node.getTemplateArgs(); 191 for (unsigned i = 0; i < List.size(); ++i) { 192 if (Matcher.matches(List.get(i), Finder, Builder)) 193 return true; 194 } 195 return false; 196} 197 198/// \brief Matches expressions that match InnerMatcher after any implicit casts 199/// are stripped off. 200/// 201/// Parentheses and explicit casts are not discarded. 202/// Given 203/// int arr[5]; 204/// int a = 0; 205/// char b = 0; 206/// const int c = a; 207/// int *d = arr; 208/// long e = (long) 0l; 209/// The matchers 210/// variable(hasInitializer(ignoringImpCasts(integerLiteral()))) 211/// variable(hasInitializer(ignoringImpCasts(declarationReference()))) 212/// would match the declarations for a, b, c, and d, but not e. 213/// while 214/// variable(hasInitializer(integerLiteral())) 215/// variable(hasInitializer(declarationReference())) 216/// only match the declarations for b, c, and d. 217AST_MATCHER_P(Expr, ignoringImpCasts, 218 internal::Matcher<Expr>, InnerMatcher) { 219 return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder); 220} 221 222/// \brief Matches expressions that match InnerMatcher after parentheses and 223/// casts are stripped off. 224/// 225/// Implicit and non-C Style casts are also discarded. 226/// Given 227/// int a = 0; 228/// char b = (0); 229/// void* c = reinterpret_cast<char*>(0); 230/// char d = char(0); 231/// The matcher 232/// variable(hasInitializer(ignoringParenCasts(integerLiteral()))) 233/// would match the declarations for a, b, c, and d. 234/// while 235/// variable(hasInitializer(integerLiteral())) 236/// only match the declaration for a. 237AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) { 238 return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder); 239} 240 241/// \brief Matches expressions that match InnerMatcher after implicit casts and 242/// parentheses are stripped off. 243/// 244/// Explicit casts are not discarded. 245/// Given 246/// int arr[5]; 247/// int a = 0; 248/// char b = (0); 249/// const int c = a; 250/// int *d = (arr); 251/// long e = ((long) 0l); 252/// The matchers 253/// variable(hasInitializer(ignoringParenImpCasts( 254/// integerLiteral()))) 255/// variable(hasInitializer(ignoringParenImpCasts( 256/// declarationReference()))) 257/// would match the declarations for a, b, c, and d, but not e. 258/// while 259/// variable(hasInitializer(integerLiteral())) 260/// variable(hasInitializer(declarationReference())) 261/// would only match the declaration for a. 262AST_MATCHER_P(Expr, ignoringParenImpCasts, 263 internal::Matcher<Expr>, InnerMatcher) { 264 return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder); 265} 266 267/// \brief Matches classTemplateSpecializations where the n'th TemplateArgument 268/// matches the given Matcher. 269/// 270/// Given 271/// template<typename T, typename U> class A {}; 272/// A<bool, int> b; 273/// A<int, bool> c; 274/// classTemplateSpecialization(hasTemplateArgument( 275/// 1, refersToType(asString("int")))) 276/// matches the specialization \c A<bool, int> 277AST_MATCHER_P2(ClassTemplateSpecializationDecl, hasTemplateArgument, 278 unsigned, N, internal::Matcher<TemplateArgument>, Matcher) { 279 const TemplateArgumentList &List = Node.getTemplateArgs(); 280 if (List.size() <= N) 281 return false; 282 return Matcher.matches(List.get(N), Finder, Builder); 283} 284 285/// \brief Matches a TemplateArgument that refers to a certain type. 286/// 287/// Given 288/// struct X {}; 289/// template<typename T> struct A {}; 290/// A<X> a; 291/// classTemplateSpecialization(hasAnyTemplateArgument( 292/// refersToType(class(hasName("X"))))) 293/// matches the specialization \c A<X> 294AST_MATCHER_P(TemplateArgument, refersToType, 295 internal::Matcher<QualType>, Matcher) { 296 if (Node.getKind() != TemplateArgument::Type) 297 return false; 298 return Matcher.matches(Node.getAsType(), Finder, Builder); 299} 300 301/// \brief Matches a TemplateArgument that refers to a certain declaration. 302/// 303/// Given 304/// template<typename T> struct A {}; 305/// struct B { B* next; }; 306/// A<&B::next> a; 307/// classTemplateSpecialization(hasAnyTemplateArgument( 308/// refersToDeclaration(field(hasName("next")))) 309/// matches the specialization \c A<&B::next> with \c field(...) matching 310/// \c B::next 311AST_MATCHER_P(TemplateArgument, refersToDeclaration, 312 internal::Matcher<Decl>, Matcher) { 313 if (const Decl *Declaration = Node.getAsDecl()) 314 return Matcher.matches(*Declaration, Finder, Builder); 315 return false; 316} 317 318/// \brief Matches C++ constructor declarations. 319/// 320/// Example matches Foo::Foo() and Foo::Foo(int) 321/// class Foo { 322/// public: 323/// Foo(); 324/// Foo(int); 325/// int DoSomething(); 326/// }; 327const internal::VariadicDynCastAllOfMatcher< 328 Decl, 329 CXXConstructorDecl> constructor; 330 331/// \brief Matches explicit C++ destructor declarations. 332/// 333/// Example matches Foo::~Foo() 334/// class Foo { 335/// public: 336/// virtual ~Foo(); 337/// }; 338const internal::VariadicDynCastAllOfMatcher<Decl, CXXDestructorDecl> destructor; 339 340/// \brief Matches enum declarations. 341/// 342/// Example matches X 343/// enum X { 344/// A, B, C 345/// }; 346const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl; 347 348/// \brief Matches enum constants. 349/// 350/// Example matches A, B, C 351/// enum X { 352/// A, B, C 353/// }; 354const internal::VariadicDynCastAllOfMatcher< 355 Decl, 356 EnumConstantDecl> enumConstant; 357 358/// \brief Matches method declarations. 359/// 360/// Example matches y 361/// class X { void y() }; 362const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> method; 363 364/// \brief Matches variable declarations. 365/// 366/// Note: this does not match declarations of member variables, which are 367/// "field" declarations in Clang parlance. 368/// 369/// Example matches a 370/// int a; 371const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> variable; 372 373/// \brief Matches field declarations. 374/// 375/// Given 376/// class X { int m; }; 377/// field() 378/// matches 'm'. 379const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> field; 380 381/// \brief Matches function declarations. 382/// 383/// Example matches f 384/// void f(); 385const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> function; 386 387 388/// \brief Matches statements. 389/// 390/// Given 391/// { ++a; } 392/// statement() 393/// matches both the compound statement '{ ++a; }' and '++a'. 394const internal::VariadicDynCastAllOfMatcher<Stmt, Stmt> statement; 395 396/// \brief Matches declaration statements. 397/// 398/// Given 399/// int a; 400/// declarationStatement() 401/// matches 'int a'. 402const internal::VariadicDynCastAllOfMatcher< 403 Stmt, 404 DeclStmt> declarationStatement; 405 406/// \brief Matches member expressions. 407/// 408/// Given 409/// class Y { 410/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 411/// int a; static int b; 412/// }; 413/// memberExpression() 414/// matches this->x, x, y.x, a, this->b 415const internal::VariadicDynCastAllOfMatcher< 416 Stmt, 417 MemberExpr> memberExpression; 418 419/// \brief Matches call expressions. 420/// 421/// Example matches x.y() and y() 422/// X x; 423/// x.y(); 424/// y(); 425const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> call; 426 427/// \brief Matches member call expressions. 428/// 429/// Example matches x.y() 430/// X x; 431/// x.y(); 432const internal::VariadicDynCastAllOfMatcher<Stmt, CXXMemberCallExpr> memberCall; 433 434/// \brief Matches init list expressions. 435/// 436/// Given 437/// int a[] = { 1, 2 }; 438/// struct B { int x, y; }; 439/// B b = { 5, 6 }; 440/// initList() 441/// matches "{ 1, 2 }" and "{ 5, 6 }" 442const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr; 443 444/// \brief Matches using declarations. 445/// 446/// Given 447/// namespace X { int x; } 448/// using X::x; 449/// usingDecl() 450/// matches \code using X::x \endcode 451const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl; 452 453/// \brief Matches constructor call expressions (including implicit ones). 454/// 455/// Example matches string(ptr, n) and ptr within arguments of f 456/// (matcher = constructorCall()) 457/// void f(const string &a, const string &b); 458/// char *ptr; 459/// int n; 460/// f(string(ptr, n), ptr); 461const internal::VariadicDynCastAllOfMatcher< 462 Stmt, 463 CXXConstructExpr> constructorCall; 464 465/// \brief Matches nodes where temporaries are created. 466/// 467/// Example matches FunctionTakesString(GetStringByValue()) 468/// (matcher = bindTemporaryExpression()) 469/// FunctionTakesString(GetStringByValue()); 470/// FunctionTakesStringByPointer(GetStringPointer()); 471const internal::VariadicDynCastAllOfMatcher< 472 Stmt, 473 CXXBindTemporaryExpr> bindTemporaryExpression; 474 475/// \brief Matches new expressions. 476/// 477/// Given 478/// new X; 479/// newExpression() 480/// matches 'new X'. 481const internal::VariadicDynCastAllOfMatcher< 482 Stmt, 483 CXXNewExpr> newExpression; 484 485/// \brief Matches delete expressions. 486/// 487/// Given 488/// delete X; 489/// deleteExpression() 490/// matches 'delete X'. 491const internal::VariadicDynCastAllOfMatcher< 492 Stmt, 493 CXXDeleteExpr> deleteExpression; 494 495/// \brief Matches array subscript expressions. 496/// 497/// Given 498/// int i = a[1]; 499/// arraySubscriptExpr() 500/// matches "a[1]" 501const internal::VariadicDynCastAllOfMatcher< 502 Stmt, 503 ArraySubscriptExpr> arraySubscriptExpr; 504 505/// \brief Matches the value of a default argument at the call site. 506/// 507/// Example matches the CXXDefaultArgExpr placeholder inserted for the 508/// default value of the second parameter in the call expression f(42) 509/// (matcher = defaultArgument()) 510/// void f(int x, int y = 0); 511/// f(42); 512const internal::VariadicDynCastAllOfMatcher< 513 Stmt, 514 CXXDefaultArgExpr> defaultArgument; 515 516/// \brief Matches overloaded operator calls. 517/// 518/// Note that if an operator isn't overloaded, it won't match. Instead, use 519/// binaryOperator matcher. 520/// Currently it does not match operators such as new delete. 521/// FIXME: figure out why these do not match? 522/// 523/// Example matches both operator<<((o << b), c) and operator<<(o, b) 524/// (matcher = overloadedOperatorCall()) 525/// ostream &operator<< (ostream &out, int i) { }; 526/// ostream &o; int b = 1, c = 1; 527/// o << b << c; 528const internal::VariadicDynCastAllOfMatcher< 529 Stmt, 530 CXXOperatorCallExpr> overloadedOperatorCall; 531 532/// \brief Matches expressions. 533/// 534/// Example matches x() 535/// void f() { x(); } 536const internal::VariadicDynCastAllOfMatcher< 537 Stmt, 538 Expr> expression; 539 540/// \brief Matches expressions that refer to declarations. 541/// 542/// Example matches x in if (x) 543/// bool x; 544/// if (x) {} 545const internal::VariadicDynCastAllOfMatcher< 546 Stmt, 547 DeclRefExpr> declarationReference; 548 549/// \brief Matches if statements. 550/// 551/// Example matches 'if (x) {}' 552/// if (x) {} 553const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt; 554 555/// \brief Matches for statements. 556/// 557/// Example matches 'for (;;) {}' 558/// for (;;) {} 559const internal::VariadicDynCastAllOfMatcher< 560 Stmt, ForStmt> forStmt; 561 562/// \brief Matches the increment statement of a for loop. 563/// 564/// Example: 565/// forStmt(hasIncrement(unaryOperator(hasOperatorName("++")))) 566/// matches '++x' in 567/// for (x; x < N; ++x) { } 568AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>, 569 InnerMatcher) { 570 const Stmt *const Increment = Node.getInc(); 571 return (Increment != NULL && 572 InnerMatcher.matches(*Increment, Finder, Builder)); 573} 574 575/// \brief Matches the initialization statement of a for loop. 576/// 577/// Example: 578/// forStmt(hasLoopInit(declarationStatement())) 579/// matches 'int x = 0' in 580/// for (int x = 0; x < N; ++x) { } 581AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>, 582 InnerMatcher) { 583 const Stmt *const Init = Node.getInit(); 584 return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder)); 585} 586 587/// \brief Matches while statements. 588/// 589/// Given 590/// while (true) {} 591/// whileStmt() 592/// matches 'while (true) {}'. 593const internal::VariadicDynCastAllOfMatcher< 594 Stmt, 595 WhileStmt> whileStmt; 596 597/// \brief Matches do statements. 598/// 599/// Given 600/// do {} while (true); 601/// doStmt() 602/// matches 'do {} while(true)' 603const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt; 604 605/// \brief Matches case and default statements inside switch statements. 606/// 607/// Given 608/// switch(a) { case 42: break; default: break; } 609/// switchCase() 610/// matches 'case 42: break;' and 'default: break;'. 611const internal::VariadicDynCastAllOfMatcher< 612 Stmt, 613 SwitchCase> switchCase; 614 615/// \brief Matches compound statements. 616/// 617/// Example matches '{}' and '{{}}'in 'for (;;) {{}}' 618/// for (;;) {{}} 619const internal::VariadicDynCastAllOfMatcher< 620 Stmt, 621 CompoundStmt> compoundStatement; 622 623/// \brief Matches bool literals. 624/// 625/// Example matches true 626/// true 627const internal::VariadicDynCastAllOfMatcher< 628 Expr, 629 CXXBoolLiteralExpr> boolLiteral; 630 631/// \brief Matches string literals (also matches wide string literals). 632/// 633/// Example matches "abcd", L"abcd" 634/// char *s = "abcd"; wchar_t *ws = L"abcd" 635const internal::VariadicDynCastAllOfMatcher< 636 Expr, 637 StringLiteral> stringLiteral; 638 639/// \brief Matches character literals (also matches wchar_t). 640/// 641/// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), 642/// though. 643/// 644/// Example matches 'a', L'a' 645/// char ch = 'a'; wchar_t chw = L'a'; 646const internal::VariadicDynCastAllOfMatcher< 647 Expr, 648 CharacterLiteral> characterLiteral; 649 650/// \brief Matches integer literals of all sizes / encodings. 651/// 652/// Not matching character-encoded integers such as L'a'. 653/// 654/// Example matches 1, 1L, 0x1, 1U 655const internal::VariadicDynCastAllOfMatcher< 656 Expr, 657 IntegerLiteral> integerLiteral; 658 659/// \brief Matches binary operator expressions. 660/// 661/// Example matches a || b 662/// !(a || b) 663const internal::VariadicDynCastAllOfMatcher< 664 Stmt, 665 BinaryOperator> binaryOperator; 666 667/// \brief Matches unary operator expressions. 668/// 669/// Example matches !a 670/// !a || b 671const internal::VariadicDynCastAllOfMatcher< 672 Stmt, 673 UnaryOperator> unaryOperator; 674 675/// \brief Matches conditional operator expressions. 676/// 677/// Example matches a ? b : c 678/// (a ? b : c) + 42 679const internal::VariadicDynCastAllOfMatcher< 680 Stmt, 681 ConditionalOperator> conditionalOperator; 682 683/// \brief Matches a reinterpret_cast expression. 684/// 685/// Either the source expression or the destination type can be matched 686/// using has(), but hasDestinationType() is more specific and can be 687/// more readable. 688/// 689/// Example matches reinterpret_cast<char*>(&p) in 690/// void* p = reinterpret_cast<char*>(&p); 691const internal::VariadicDynCastAllOfMatcher< 692 Expr, 693 CXXReinterpretCastExpr> reinterpretCast; 694 695/// \brief Matches a C++ static_cast expression. 696/// 697/// \see hasDestinationType 698/// \see reinterpretCast 699/// 700/// Example: 701/// staticCast() 702/// matches 703/// static_cast<long>(8) 704/// in 705/// long eight(static_cast<long>(8)); 706const internal::VariadicDynCastAllOfMatcher< 707 Expr, 708 CXXStaticCastExpr> staticCast; 709 710/// \brief Matches a dynamic_cast expression. 711/// 712/// Example: 713/// dynamicCast() 714/// matches 715/// dynamic_cast<D*>(&b); 716/// in 717/// struct B { virtual ~B() {} }; struct D : B {}; 718/// B b; 719/// D* p = dynamic_cast<D*>(&b); 720const internal::VariadicDynCastAllOfMatcher< 721 Expr, 722 CXXDynamicCastExpr> dynamicCast; 723 724/// \brief Matches a const_cast expression. 725/// 726/// Example: Matches const_cast<int*>(&r) in 727/// int n = 42; 728/// const int& r(n); 729/// int* p = const_cast<int*>(&r); 730const internal::VariadicDynCastAllOfMatcher< 731 Expr, 732 CXXConstCastExpr> constCast; 733 734/// \brief Matches explicit cast expressions. 735/// 736/// Matches any cast expression written in user code, whether it be a 737/// C-style cast, a functional-style cast, or a keyword cast. 738/// 739/// Does not match implicit conversions. 740/// 741/// Note: the name "explicitCast" is chosen to match Clang's terminology, as 742/// Clang uses the term "cast" to apply to implicit conversions as well as to 743/// actual cast expressions. 744/// 745/// \see hasDestinationType. 746/// 747/// Example: matches all five of the casts in 748/// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 749/// but does not match the implicit conversion in 750/// long ell = 42; 751const internal::VariadicDynCastAllOfMatcher< 752 Expr, 753 ExplicitCastExpr> explicitCast; 754 755/// \brief Matches the implicit cast nodes of Clang's AST. 756/// 757/// This matches many different places, including function call return value 758/// eliding, as well as any type conversions. 759const internal::VariadicDynCastAllOfMatcher< 760 Expr, 761 ImplicitCastExpr> implicitCast; 762 763/// \brief Matches any cast nodes of Clang's AST. 764/// 765/// Example: castExpr() matches each of the following: 766/// (int) 3; 767/// const_cast<Expr *>(SubExpr); 768/// char c = 0; 769/// but does not match 770/// int i = (0); 771/// int k = 0; 772const internal::VariadicDynCastAllOfMatcher< 773 Expr, 774 CastExpr> castExpr; 775 776/// \brief Matches functional cast expressions 777/// 778/// Example: Matches Foo(bar); 779/// Foo f = bar; 780/// Foo g = (Foo) bar; 781/// Foo h = Foo(bar); 782const internal::VariadicDynCastAllOfMatcher< 783 Expr, 784 CXXFunctionalCastExpr> functionalCast; 785 786/// \brief Various overloads for the anyOf matcher. 787/// @{ 788template<typename C1, typename C2> 789internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1, C2> 790anyOf(const C1 &P1, const C2 &P2) { 791 return internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 792 C1, C2 >(P1, P2); 793} 794template<typename C1, typename C2, typename C3> 795internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1, 796 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C2, C3> > 797anyOf(const C1 &P1, const C2 &P2, const C3 &P3) { 798 return anyOf(P1, anyOf(P2, P3)); 799} 800template<typename C1, typename C2, typename C3, typename C4> 801internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1, 802 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C2, 803 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 804 C3, C4> > > 805anyOf(const C1 &P1, const C2 &P2, const C3 &P3, const C4 &P4) { 806 return anyOf(P1, anyOf(P2, anyOf(P3, P4))); 807} 808template<typename C1, typename C2, typename C3, typename C4, typename C5> 809internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1, 810 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C2, 811 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C3, 812 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 813 C4, C5> > > > 814anyOf(const C1& P1, const C2& P2, const C3& P3, const C4& P4, const C5& P5) { 815 return anyOf(P1, anyOf(P2, anyOf(P3, anyOf(P4, P5)))); 816} 817/// @} 818 819/// \brief Various overloads for the allOf matcher. 820/// @{ 821template<typename C1, typename C2> 822internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, C1, C2> 823allOf(const C1 &P1, const C2 &P2) { 824 return internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, 825 C1, C2>(P1, P2); 826} 827template<typename C1, typename C2, typename C3> 828internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, C1, 829 internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, C2, C3> > 830allOf(const C1& P1, const C2& P2, const C3& P3) { 831 return allOf(P1, allOf(P2, P3)); 832} 833/// @} 834 835/// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 836/// 837/// Given 838/// Foo x = bar; 839/// int y = sizeof(x) + alignof(x); 840/// unaryExprOrTypeTraitExpr() 841/// matches \c sizeof(x) and \c alignof(x) 842const internal::VariadicDynCastAllOfMatcher< 843 Stmt, 844 UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr; 845 846/// \brief Matches unary expressions that have a specific type of argument. 847/// 848/// Given 849/// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 850/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 851/// matches \c sizeof(a) and \c alignof(c) 852AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 853 internal::Matcher<QualType>, Matcher) { 854 const QualType ArgumentType = Node.getTypeOfArgument(); 855 return Matcher.matches(ArgumentType, Finder, Builder); 856} 857 858/// \brief Matches unary expressions of a certain kind. 859/// 860/// Given 861/// int x; 862/// int s = sizeof(x) + alignof(x) 863/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 864/// matches \c sizeof(x) 865AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 866 return Node.getKind() == Kind; 867} 868 869/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 870/// alignof. 871inline internal::Matcher<Stmt> alignOfExpr( 872 const internal::Matcher<UnaryExprOrTypeTraitExpr> &Matcher) { 873 return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf( 874 ofKind(UETT_AlignOf), Matcher))); 875} 876 877/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 878/// sizeof. 879inline internal::Matcher<Stmt> sizeOfExpr( 880 const internal::Matcher<UnaryExprOrTypeTraitExpr> &Matcher) { 881 return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf( 882 ofKind(UETT_SizeOf), Matcher))); 883} 884 885/// \brief Matches NamedDecl nodes that have the specified name. 886/// 887/// Supports specifying enclosing namespaces or classes by prefixing the name 888/// with '<enclosing>::'. 889/// Does not match typedefs of an underlying type with the given name. 890/// 891/// Example matches X (Name == "X") 892/// class X; 893/// 894/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 895/// namespace a { namespace b { class X; } } 896AST_MATCHER_P(NamedDecl, hasName, std::string, Name) { 897 assert(!Name.empty()); 898 const std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 899 const llvm::StringRef FullName = FullNameString; 900 const llvm::StringRef Pattern = Name; 901 if (Pattern.startswith("::")) { 902 return FullName == Pattern; 903 } else { 904 return FullName.endswith(("::" + Pattern).str()); 905 } 906} 907 908/// \brief Matches NamedDecl nodes whose full names partially match the 909/// given RegExp. 910/// 911/// Supports specifying enclosing namespaces or classes by 912/// prefixing the name with '<enclosing>::'. Does not match typedefs 913/// of an underlying type with the given name. 914/// 915/// Example matches X (regexp == "::X") 916/// class X; 917/// 918/// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 919/// namespace foo { namespace bar { class X; } } 920AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 921 assert(!RegExp.empty()); 922 std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 923 llvm::Regex RE(RegExp); 924 return RE.match(FullNameString); 925} 926 927/// \brief Matches overloaded operator names. 928/// 929/// Matches overloaded operator names specified in strings without the 930/// "operator" prefix, such as "<<", for OverloadedOperatorCall's. 931/// 932/// Example matches a << b 933/// (matcher == overloadedOperatorCall(hasOverloadedOperatorName("<<"))) 934/// a << b; 935/// c && d; // assuming both operator<< 936/// // and operator&& are overloaded somewhere. 937AST_MATCHER_P(CXXOperatorCallExpr, 938 hasOverloadedOperatorName, std::string, Name) { 939 return getOperatorSpelling(Node.getOperator()) == Name; 940} 941 942/// \brief Matches C++ classes that are directly or indirectly derived from 943/// a class matching \c Base. 944/// 945/// Note that a class is considered to be also derived from itself. 946/// 947/// Example matches X, Y, Z, C (Base == hasName("X")) 948/// class X; // A class is considered to be derived from itself 949/// class Y : public X {}; // directly derived 950/// class Z : public Y {}; // indirectly derived 951/// typedef X A; 952/// typedef A B; 953/// class C : public B {}; // derived from a typedef of X 954/// 955/// In the following example, Bar matches isDerivedFrom(hasName("X")): 956/// class Foo; 957/// typedef Foo X; 958/// class Bar : public Foo {}; // derived from a type that X is a typedef of 959AST_MATCHER_P(CXXRecordDecl, isDerivedFrom, 960 internal::Matcher<NamedDecl>, Base) { 961 return Finder->classIsDerivedFrom(&Node, Base, Builder); 962} 963 964/// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 965inline internal::Matcher<CXXRecordDecl> isDerivedFrom(StringRef BaseName) { 966 assert(!BaseName.empty()); 967 return isDerivedFrom(hasName(BaseName)); 968} 969 970/// \brief Matches AST nodes that have child AST nodes that match the 971/// provided matcher. 972/// 973/// Example matches X, Y (matcher = record(has(record(hasName("X"))) 974/// class X {}; // Matches X, because X::X is a class of name X inside X. 975/// class Y { class X {}; }; 976/// class Z { class Y { class X {}; }; }; // Does not match Z. 977/// 978/// ChildT must be an AST base type. 979template <typename ChildT> 980internal::ArgumentAdaptingMatcher<internal::HasMatcher, ChildT> has( 981 const internal::Matcher<ChildT> &ChildMatcher) { 982 return internal::ArgumentAdaptingMatcher<internal::HasMatcher, 983 ChildT>(ChildMatcher); 984} 985 986/// \brief Matches AST nodes that have descendant AST nodes that match the 987/// provided matcher. 988/// 989/// Example matches X, Y, Z 990/// (matcher = record(hasDescendant(record(hasName("X"))))) 991/// class X {}; // Matches X, because X::X is a class of name X inside X. 992/// class Y { class X {}; }; 993/// class Z { class Y { class X {}; }; }; 994/// 995/// DescendantT must be an AST base type. 996template <typename DescendantT> 997internal::ArgumentAdaptingMatcher<internal::HasDescendantMatcher, DescendantT> 998hasDescendant(const internal::Matcher<DescendantT> &DescendantMatcher) { 999 return internal::ArgumentAdaptingMatcher< 1000 internal::HasDescendantMatcher, 1001 DescendantT>(DescendantMatcher); 1002} 1003 1004 1005/// \brief Matches AST nodes that have child AST nodes that match the 1006/// provided matcher. 1007/// 1008/// Example matches X, Y (matcher = record(forEach(record(hasName("X"))) 1009/// class X {}; // Matches X, because X::X is a class of name X inside X. 1010/// class Y { class X {}; }; 1011/// class Z { class Y { class X {}; }; }; // Does not match Z. 1012/// 1013/// ChildT must be an AST base type. 1014/// 1015/// As opposed to 'has', 'forEach' will cause a match for each result that 1016/// matches instead of only on the first one. 1017template <typename ChildT> 1018internal::ArgumentAdaptingMatcher<internal::ForEachMatcher, ChildT> forEach( 1019 const internal::Matcher<ChildT>& ChildMatcher) { 1020 return internal::ArgumentAdaptingMatcher< 1021 internal::ForEachMatcher, 1022 ChildT>(ChildMatcher); 1023} 1024 1025/// \brief Matches AST nodes that have descendant AST nodes that match the 1026/// provided matcher. 1027/// 1028/// Example matches X, A, B, C 1029/// (matcher = record(forEachDescendant(record(hasName("X"))))) 1030/// class X {}; // Matches X, because X::X is a class of name X inside X. 1031/// class A { class X {}; }; 1032/// class B { class C { class X {}; }; }; 1033/// 1034/// DescendantT must be an AST base type. 1035/// 1036/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 1037/// each result that matches instead of only on the first one. 1038/// 1039/// Note: Recursively combined ForEachDescendant can cause many matches: 1040/// record(forEachDescendant(record(forEachDescendant(record())))) 1041/// will match 10 times (plus injected class name matches) on: 1042/// class A { class B { class C { class D { class E {}; }; }; }; }; 1043template <typename DescendantT> 1044internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, DescendantT> 1045forEachDescendant( 1046 const internal::Matcher<DescendantT>& DescendantMatcher) { 1047 return internal::ArgumentAdaptingMatcher< 1048 internal::ForEachDescendantMatcher, 1049 DescendantT>(DescendantMatcher); 1050} 1051 1052/// \brief Matches if the provided matcher does not match. 1053/// 1054/// Example matches Y (matcher = record(unless(hasName("X")))) 1055/// class X {}; 1056/// class Y {}; 1057template <typename M> 1058internal::PolymorphicMatcherWithParam1<internal::NotMatcher, M> unless(const M &InnerMatcher) { 1059 return internal::PolymorphicMatcherWithParam1< 1060 internal::NotMatcher, M>(InnerMatcher); 1061} 1062 1063/// \brief Matches a type if the declaration of the type matches the given 1064/// matcher. 1065/// 1066/// Usable as: Matcher<QualType>, Matcher<CallExpr>, Matcher<CXXConstructExpr> 1067inline internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, 1068 internal::Matcher<Decl> > 1069 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 1070 return internal::PolymorphicMatcherWithParam1< 1071 internal::HasDeclarationMatcher, 1072 internal::Matcher<Decl> >(InnerMatcher); 1073} 1074 1075/// \brief Matches on the implicit object argument of a member call expression. 1076/// 1077/// Example matches y.x() (matcher = call(on(hasType(record(hasName("Y")))))) 1078/// class Y { public: void x(); }; 1079/// void z() { Y y; y.x(); }", 1080/// 1081/// FIXME: Overload to allow directly matching types? 1082AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 1083 InnerMatcher) { 1084 const Expr *ExprNode = const_cast<CXXMemberCallExpr&>(Node) 1085 .getImplicitObjectArgument() 1086 ->IgnoreParenImpCasts(); 1087 return (ExprNode != NULL && 1088 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1089} 1090 1091/// \brief Matches if the call expression's callee expression matches. 1092/// 1093/// Given 1094/// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 1095/// void f() { f(); } 1096/// call(callee(expression())) 1097/// matches this->x(), x(), y.x(), f() 1098/// with callee(...) 1099/// matching this->x, x, y.x, f respectively 1100/// 1101/// Note: Callee cannot take the more general internal::Matcher<Expr> 1102/// because this introduces ambiguous overloads with calls to Callee taking a 1103/// internal::Matcher<Decl>, as the matcher hierarchy is purely 1104/// implemented in terms of implicit casts. 1105AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 1106 InnerMatcher) { 1107 const Expr *ExprNode = Node.getCallee(); 1108 return (ExprNode != NULL && 1109 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1110} 1111 1112/// \brief Matches if the call expression's callee's declaration matches the 1113/// given matcher. 1114/// 1115/// Example matches y.x() (matcher = call(callee(method(hasName("x"))))) 1116/// class Y { public: void x(); }; 1117/// void z() { Y y; y.x(); 1118inline internal::Matcher<CallExpr> callee( 1119 const internal::Matcher<Decl> &InnerMatcher) { 1120 return internal::Matcher<CallExpr>(hasDeclaration(InnerMatcher)); 1121} 1122 1123/// \brief Matches if the expression's or declaration's type matches a type 1124/// matcher. 1125/// 1126/// Example matches x (matcher = expression(hasType( 1127/// hasDeclaration(record(hasName("X")))))) 1128/// and z (matcher = variable(hasType( 1129/// hasDeclaration(record(hasName("X")))))) 1130/// class X {}; 1131/// void y(X &x) { x; X z; } 1132AST_POLYMORPHIC_MATCHER_P(hasType, internal::Matcher<QualType>, 1133 InnerMatcher) { 1134 TOOLING_COMPILE_ASSERT((llvm::is_base_of<Expr, NodeType>::value || 1135 llvm::is_base_of<ValueDecl, NodeType>::value), 1136 instantiated_with_wrong_types); 1137 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1138} 1139 1140/// \brief Overloaded to match the declaration of the expression's or value 1141/// declaration's type. 1142/// 1143/// In case of a value declaration (for example a variable declaration), 1144/// this resolves one layer of indirection. For example, in the value 1145/// declaration "X x;", record(hasName("X")) matches the declaration of X, 1146/// while variable(hasType(record(hasName("X")))) matches the declaration 1147/// of x." 1148/// 1149/// Example matches x (matcher = expression(hasType(record(hasName("X"))))) 1150/// and z (matcher = variable(hasType(record(hasName("X"))))) 1151/// class X {}; 1152/// void y(X &x) { x; X z; } 1153/// 1154/// Usable as: Matcher<Expr>, Matcher<ValueDecl> 1155inline internal::PolymorphicMatcherWithParam1< 1156 internal::matcher_hasTypeMatcher, 1157 internal::Matcher<QualType> > 1158hasType(const internal::Matcher<Decl> &InnerMatcher) { 1159 return hasType(internal::Matcher<QualType>( 1160 hasDeclaration(InnerMatcher))); 1161} 1162 1163/// \brief Matches if the matched type is represented by the given string. 1164/// 1165/// Given 1166/// class Y { public: void x(); }; 1167/// void z() { Y* y; y->x(); } 1168/// call(on(hasType(asString("class Y *")))) 1169/// matches y->x() 1170AST_MATCHER_P(QualType, asString, std::string, Name) { 1171 return Name == Node.getAsString(); 1172} 1173 1174/// \brief Matches if the matched type is a pointer type and the pointee type 1175/// matches the specified matcher. 1176/// 1177/// Example matches y->x() 1178/// (matcher = call(on(hasType(pointsTo(record(hasName("Y"))))))) 1179/// class Y { public: void x(); }; 1180/// void z() { Y *y; y->x(); } 1181AST_MATCHER_P( 1182 QualType, pointsTo, internal::Matcher<QualType>, 1183 InnerMatcher) { 1184 return (!Node.isNull() && Node->isPointerType() && 1185 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1186} 1187 1188/// \brief Overloaded to match the pointee type's declaration. 1189inline internal::Matcher<QualType> pointsTo( 1190 const internal::Matcher<Decl> &InnerMatcher) { 1191 return pointsTo(internal::Matcher<QualType>( 1192 hasDeclaration(InnerMatcher))); 1193} 1194 1195/// \brief Matches if the matched type is a reference type and the referenced 1196/// type matches the specified matcher. 1197/// 1198/// Example matches X &x and const X &y 1199/// (matcher = variable(hasType(references(record(hasName("X")))))) 1200/// class X { 1201/// void a(X b) { 1202/// X &x = b; 1203/// const X &y = b; 1204/// }; 1205AST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 1206 InnerMatcher) { 1207 return (!Node.isNull() && Node->isReferenceType() && 1208 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1209} 1210 1211/// \brief Overloaded to match the referenced type's declaration. 1212inline internal::Matcher<QualType> references( 1213 const internal::Matcher<Decl> &InnerMatcher) { 1214 return references(internal::Matcher<QualType>( 1215 hasDeclaration(InnerMatcher))); 1216} 1217 1218AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 1219 internal::Matcher<Expr>, InnerMatcher) { 1220 const Expr *ExprNode = 1221 const_cast<CXXMemberCallExpr&>(Node).getImplicitObjectArgument(); 1222 return (ExprNode != NULL && 1223 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1224} 1225 1226/// \brief Matches if the expression's type either matches the specified 1227/// matcher, or is a pointer to a type that matches the InnerMatcher. 1228inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1229 const internal::Matcher<QualType> &InnerMatcher) { 1230 return onImplicitObjectArgument( 1231 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1232} 1233 1234/// \brief Overloaded to match the type's declaration. 1235inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1236 const internal::Matcher<Decl> &InnerMatcher) { 1237 return onImplicitObjectArgument( 1238 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1239} 1240 1241/// \brief Matches a DeclRefExpr that refers to a declaration that matches the 1242/// specified matcher. 1243/// 1244/// Example matches x in if(x) 1245/// (matcher = declarationReference(to(variable(hasName("x"))))) 1246/// bool x; 1247/// if (x) {} 1248AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 1249 InnerMatcher) { 1250 const Decl *DeclNode = Node.getDecl(); 1251 return (DeclNode != NULL && 1252 InnerMatcher.matches(*DeclNode, Finder, Builder)); 1253} 1254 1255/// \brief Matches a \c DeclRefExpr that refers to a declaration through a 1256/// specific using shadow declaration. 1257/// 1258/// FIXME: This currently only works for functions. Fix. 1259/// 1260/// Given 1261/// namespace a { void f() {} } 1262/// using a::f; 1263/// void g() { 1264/// f(); // Matches this .. 1265/// a::f(); // .. but not this. 1266/// } 1267/// declarationReference(throughUsingDeclaration(anything())) 1268/// matches \c f() 1269AST_MATCHER_P(DeclRefExpr, throughUsingDecl, 1270 internal::Matcher<UsingShadowDecl>, Matcher) { 1271 const NamedDecl *FoundDecl = Node.getFoundDecl(); 1272 if (const UsingShadowDecl *UsingDecl = 1273 llvm::dyn_cast<UsingShadowDecl>(FoundDecl)) 1274 return Matcher.matches(*UsingDecl, Finder, Builder); 1275 return false; 1276} 1277 1278/// \brief Matches a variable declaration that has an initializer expression 1279/// that matches the given matcher. 1280/// 1281/// Example matches x (matcher = variable(hasInitializer(call()))) 1282/// bool y() { return true; } 1283/// bool x = y(); 1284AST_MATCHER_P( 1285 VarDecl, hasInitializer, internal::Matcher<Expr>, 1286 InnerMatcher) { 1287 const Expr *Initializer = Node.getAnyInitializer(); 1288 return (Initializer != NULL && 1289 InnerMatcher.matches(*Initializer, Finder, Builder)); 1290} 1291 1292/// \brief Checks that a call expression or a constructor call expression has 1293/// a specific number of arguments (including absent default arguments). 1294/// 1295/// Example matches f(0, 0) (matcher = call(argumentCountIs(2))) 1296/// void f(int x, int y); 1297/// f(0, 0); 1298AST_POLYMORPHIC_MATCHER_P(argumentCountIs, unsigned, N) { 1299 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1300 llvm::is_base_of<CXXConstructExpr, 1301 NodeType>::value), 1302 instantiated_with_wrong_types); 1303 return Node.getNumArgs() == N; 1304} 1305 1306/// \brief Matches the n'th argument of a call expression or a constructor 1307/// call expression. 1308/// 1309/// Example matches y in x(y) 1310/// (matcher = call(hasArgument(0, declarationReference()))) 1311/// void x(int) { int y; x(y); } 1312AST_POLYMORPHIC_MATCHER_P2( 1313 hasArgument, unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 1314 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1315 llvm::is_base_of<CXXConstructExpr, 1316 NodeType>::value), 1317 instantiated_with_wrong_types); 1318 return (N < Node.getNumArgs() && 1319 InnerMatcher.matches( 1320 *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 1321} 1322 1323/// \brief Matches a constructor initializer. 1324/// 1325/// Given 1326/// struct Foo { 1327/// Foo() : foo_(1) { } 1328/// int foo_; 1329/// }; 1330/// record(has(constructor(hasAnyConstructorInitializer(anything())))) 1331/// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 1332AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 1333 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 1334 for (CXXConstructorDecl::init_const_iterator I = Node.init_begin(); 1335 I != Node.init_end(); ++I) { 1336 if (InnerMatcher.matches(**I, Finder, Builder)) { 1337 return true; 1338 } 1339 } 1340 return false; 1341} 1342 1343/// \brief Matches the field declaration of a constructor initializer. 1344/// 1345/// Given 1346/// struct Foo { 1347/// Foo() : foo_(1) { } 1348/// int foo_; 1349/// }; 1350/// record(has(constructor(hasAnyConstructorInitializer( 1351/// forField(hasName("foo_")))))) 1352/// matches Foo 1353/// with forField matching foo_ 1354AST_MATCHER_P(CXXCtorInitializer, forField, 1355 internal::Matcher<FieldDecl>, InnerMatcher) { 1356 const FieldDecl *NodeAsDecl = Node.getMember(); 1357 return (NodeAsDecl != NULL && 1358 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 1359} 1360 1361/// \brief Matches the initializer expression of a constructor initializer. 1362/// 1363/// Given 1364/// struct Foo { 1365/// Foo() : foo_(1) { } 1366/// int foo_; 1367/// }; 1368/// record(has(constructor(hasAnyConstructorInitializer( 1369/// withInitializer(integerLiteral(equals(1))))))) 1370/// matches Foo 1371/// with withInitializer matching (1) 1372AST_MATCHER_P(CXXCtorInitializer, withInitializer, 1373 internal::Matcher<Expr>, InnerMatcher) { 1374 const Expr* NodeAsExpr = Node.getInit(); 1375 return (NodeAsExpr != NULL && 1376 InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 1377} 1378 1379/// \brief Matches a contructor initializer if it is explicitly written in 1380/// code (as opposed to implicitly added by the compiler). 1381/// 1382/// Given 1383/// struct Foo { 1384/// Foo() { } 1385/// Foo(int) : foo_("A") { } 1386/// string foo_; 1387/// }; 1388/// constructor(hasAnyConstructorInitializer(isWritten())) 1389/// will match Foo(int), but not Foo() 1390AST_MATCHER(CXXCtorInitializer, isWritten) { 1391 return Node.isWritten(); 1392} 1393 1394/// \brief Matches a constructor declaration that has been implicitly added 1395/// by the compiler (eg. implicit default/copy constructors). 1396AST_MATCHER(CXXConstructorDecl, isImplicit) { 1397 return Node.isImplicit(); 1398} 1399 1400/// \brief Matches any argument of a call expression or a constructor call 1401/// expression. 1402/// 1403/// Given 1404/// void x(int, int, int) { int y; x(1, y, 42); } 1405/// call(hasAnyArgument(declarationReference())) 1406/// matches x(1, y, 42) 1407/// with hasAnyArgument(...) 1408/// matching y 1409AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, internal::Matcher<Expr>, 1410 InnerMatcher) { 1411 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1412 llvm::is_base_of<CXXConstructExpr, 1413 NodeType>::value), 1414 instantiated_with_wrong_types); 1415 for (unsigned I = 0; I < Node.getNumArgs(); ++I) { 1416 if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(), 1417 Finder, Builder)) { 1418 return true; 1419 } 1420 } 1421 return false; 1422} 1423 1424/// \brief Matches the n'th parameter of a function declaration. 1425/// 1426/// Given 1427/// class X { void f(int x) {} }; 1428/// method(hasParameter(0, hasType(variable()))) 1429/// matches f(int x) {} 1430/// with hasParameter(...) 1431/// matching int x 1432AST_MATCHER_P2(FunctionDecl, hasParameter, 1433 unsigned, N, internal::Matcher<ParmVarDecl>, 1434 InnerMatcher) { 1435 return (N < Node.getNumParams() && 1436 InnerMatcher.matches( 1437 *Node.getParamDecl(N), Finder, Builder)); 1438} 1439 1440/// \brief Matches any parameter of a function declaration. 1441/// 1442/// Does not match the 'this' parameter of a method. 1443/// 1444/// Given 1445/// class X { void f(int x, int y, int z) {} }; 1446/// method(hasAnyParameter(hasName("y"))) 1447/// matches f(int x, int y, int z) {} 1448/// with hasAnyParameter(...) 1449/// matching int y 1450AST_MATCHER_P(FunctionDecl, hasAnyParameter, 1451 internal::Matcher<ParmVarDecl>, InnerMatcher) { 1452 for (unsigned I = 0; I < Node.getNumParams(); ++I) { 1453 if (InnerMatcher.matches(*Node.getParamDecl(I), Finder, Builder)) { 1454 return true; 1455 } 1456 } 1457 return false; 1458} 1459 1460/// \brief Matches the return type of a function declaration. 1461/// 1462/// Given: 1463/// class X { int f() { return 1; } }; 1464/// method(returns(asString("int"))) 1465/// matches int f() { return 1; } 1466AST_MATCHER_P(FunctionDecl, returns, internal::Matcher<QualType>, Matcher) { 1467 return Matcher.matches(Node.getResultType(), Finder, Builder); 1468} 1469 1470/// \brief Matches extern "C" function declarations. 1471/// 1472/// Given: 1473/// extern "C" void f() {} 1474/// extern "C" { void g() {} } 1475/// void h() {} 1476/// function(isExternC()) 1477/// matches the declaration of f and g, but not the declaration h 1478AST_MATCHER(FunctionDecl, isExternC) { 1479 return Node.isExternC(); 1480} 1481 1482/// \brief Matches the condition expression of an if statement, for loop, 1483/// or conditional operator. 1484/// 1485/// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 1486/// if (true) {} 1487AST_POLYMORPHIC_MATCHER_P(hasCondition, internal::Matcher<Expr>, 1488 InnerMatcher) { 1489 TOOLING_COMPILE_ASSERT( 1490 (llvm::is_base_of<IfStmt, NodeType>::value) || 1491 (llvm::is_base_of<ForStmt, NodeType>::value) || 1492 (llvm::is_base_of<WhileStmt, NodeType>::value) || 1493 (llvm::is_base_of<DoStmt, NodeType>::value) || 1494 (llvm::is_base_of<ConditionalOperator, NodeType>::value), 1495 has_condition_requires_if_statement_conditional_operator_or_loop); 1496 const Expr *const Condition = Node.getCond(); 1497 return (Condition != NULL && 1498 InnerMatcher.matches(*Condition, Finder, Builder)); 1499} 1500 1501/// \brief Matches the condition variable statement in an if statement. 1502/// 1503/// Given 1504/// if (A* a = GetAPointer()) {} 1505/// hasConditionVariableStatment(...) 1506/// matches 'A* a = GetAPointer()'. 1507AST_MATCHER_P(IfStmt, hasConditionVariableStatement, 1508 internal::Matcher<DeclStmt>, InnerMatcher) { 1509 const DeclStmt* const DeclarationStatement = 1510 Node.getConditionVariableDeclStmt(); 1511 return DeclarationStatement != NULL && 1512 InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 1513} 1514 1515/// \brief Matches the index expression of an array subscript expression. 1516/// 1517/// Given 1518/// int i[5]; 1519/// void f() { i[1] = 42; } 1520/// arraySubscriptExpression(hasIndex(integerLiteral())) 1521/// matches \c i[1] with the \c integerLiteral() matching \c 1 1522AST_MATCHER_P(ArraySubscriptExpr, hasIndex, 1523 internal::Matcher<Expr>, matcher) { 1524 if (const Expr* Expression = Node.getIdx()) 1525 return matcher.matches(*Expression, Finder, Builder); 1526 return false; 1527} 1528 1529/// \brief Matches the base expression of an array subscript expression. 1530/// 1531/// Given 1532/// int i[5]; 1533/// void f() { i[1] = 42; } 1534/// arraySubscriptExpression(hasBase(implicitCast( 1535/// hasSourceExpression(declarationReference())))) 1536/// matches \c i[1] with the \c declarationReference() matching \c i 1537AST_MATCHER_P(ArraySubscriptExpr, hasBase, 1538 internal::Matcher<Expr>, matcher) { 1539 if (const Expr* Expression = Node.getBase()) 1540 return matcher.matches(*Expression, Finder, Builder); 1541 return false; 1542} 1543 1544/// \brief Matches a 'for', 'while', or 'do while' statement that has 1545/// a given body. 1546/// 1547/// Given 1548/// for (;;) {} 1549/// hasBody(compoundStatement()) 1550/// matches 'for (;;) {}' 1551/// with compoundStatement() 1552/// matching '{}' 1553AST_POLYMORPHIC_MATCHER_P(hasBody, internal::Matcher<Stmt>, 1554 InnerMatcher) { 1555 TOOLING_COMPILE_ASSERT( 1556 (llvm::is_base_of<DoStmt, NodeType>::value) || 1557 (llvm::is_base_of<ForStmt, NodeType>::value) || 1558 (llvm::is_base_of<WhileStmt, NodeType>::value), 1559 has_body_requires_for_while_or_do_statement); 1560 const Stmt *const Statement = Node.getBody(); 1561 return (Statement != NULL && 1562 InnerMatcher.matches(*Statement, Finder, Builder)); 1563} 1564 1565/// \brief Matches compound statements where at least one substatement matches 1566/// a given matcher. 1567/// 1568/// Given 1569/// { {}; 1+2; } 1570/// hasAnySubstatement(compoundStatement()) 1571/// matches '{ {}; 1+2; }' 1572/// with compoundStatement() 1573/// matching '{}' 1574AST_MATCHER_P(CompoundStmt, hasAnySubstatement, 1575 internal::Matcher<Stmt>, InnerMatcher) { 1576 for (CompoundStmt::const_body_iterator It = Node.body_begin(); 1577 It != Node.body_end(); 1578 ++It) { 1579 if (InnerMatcher.matches(**It, Finder, Builder)) return true; 1580 } 1581 return false; 1582} 1583 1584/// \brief Checks that a compound statement contains a specific number of 1585/// child statements. 1586/// 1587/// Example: Given 1588/// { for (;;) {} } 1589/// compoundStatement(statementCountIs(0))) 1590/// matches '{}' 1591/// but does not match the outer compound statement. 1592AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 1593 return Node.size() == N; 1594} 1595 1596/// \brief Matches literals that are equal to the given value. 1597/// 1598/// Example matches true (matcher = boolLiteral(equals(true))) 1599/// true 1600/// 1601/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 1602/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 1603template <typename ValueT> 1604internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 1605equals(const ValueT &Value) { 1606 return internal::PolymorphicMatcherWithParam1< 1607 internal::ValueEqualsMatcher, 1608 ValueT>(Value); 1609} 1610 1611/// \brief Matches the operator Name of operator expressions (binary or 1612/// unary). 1613/// 1614/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 1615/// !(a || b) 1616AST_POLYMORPHIC_MATCHER_P(hasOperatorName, std::string, Name) { 1617 TOOLING_COMPILE_ASSERT( 1618 (llvm::is_base_of<BinaryOperator, NodeType>::value) || 1619 (llvm::is_base_of<UnaryOperator, NodeType>::value), 1620 has_condition_requires_if_statement_or_conditional_operator); 1621 return Name == Node.getOpcodeStr(Node.getOpcode()); 1622} 1623 1624/// \brief Matches the left hand side of binary operator expressions. 1625/// 1626/// Example matches a (matcher = binaryOperator(hasLHS())) 1627/// a || b 1628AST_MATCHER_P(BinaryOperator, hasLHS, 1629 internal::Matcher<Expr>, InnerMatcher) { 1630 Expr *LeftHandSide = Node.getLHS(); 1631 return (LeftHandSide != NULL && 1632 InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 1633} 1634 1635/// \brief Matches the right hand side of binary operator expressions. 1636/// 1637/// Example matches b (matcher = binaryOperator(hasRHS())) 1638/// a || b 1639AST_MATCHER_P(BinaryOperator, hasRHS, 1640 internal::Matcher<Expr>, InnerMatcher) { 1641 Expr *RightHandSide = Node.getRHS(); 1642 return (RightHandSide != NULL && 1643 InnerMatcher.matches(*RightHandSide, Finder, Builder)); 1644} 1645 1646/// \brief Matches if either the left hand side or the right hand side of a 1647/// binary operator matches. 1648inline internal::Matcher<BinaryOperator> hasEitherOperand( 1649 const internal::Matcher<Expr> &InnerMatcher) { 1650 return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 1651} 1652 1653/// \brief Matches if the operand of a unary operator matches. 1654/// 1655/// Example matches true (matcher = hasOperand(boolLiteral(equals(true)))) 1656/// !true 1657AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 1658 internal::Matcher<Expr>, InnerMatcher) { 1659 const Expr * const Operand = Node.getSubExpr(); 1660 return (Operand != NULL && 1661 InnerMatcher.matches(*Operand, Finder, Builder)); 1662} 1663 1664/// \brief Matches if the cast's source expression matches the given matcher. 1665/// 1666/// Example: matches "a string" (matcher = 1667/// hasSourceExpression(constructorCall())) 1668/// 1669/// class URL { URL(string); }; 1670/// URL url = "a string"; 1671AST_MATCHER_P(CastExpr, hasSourceExpression, 1672 internal::Matcher<Expr>, InnerMatcher) { 1673 const Expr* const SubExpression = Node.getSubExpr(); 1674 return (SubExpression != NULL && 1675 InnerMatcher.matches(*SubExpression, Finder, Builder)); 1676} 1677 1678/// \brief Matches casts whose destination type matches a given matcher. 1679/// 1680/// (Note: Clang's AST refers to other conversions as "casts" too, and calls 1681/// actual casts "explicit" casts.) 1682AST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 1683 internal::Matcher<QualType>, InnerMatcher) { 1684 const QualType NodeType = Node.getTypeAsWritten(); 1685 return InnerMatcher.matches(NodeType, Finder, Builder); 1686} 1687 1688/// \brief Matches implicit casts whose destination type matches a given 1689/// matcher. 1690/// 1691/// FIXME: Unit test this matcher 1692AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 1693 internal::Matcher<QualType>, InnerMatcher) { 1694 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1695} 1696 1697/// \brief Matches the true branch expression of a conditional operator. 1698/// 1699/// Example matches a 1700/// condition ? a : b 1701AST_MATCHER_P(ConditionalOperator, hasTrueExpression, 1702 internal::Matcher<Expr>, InnerMatcher) { 1703 Expr *Expression = Node.getTrueExpr(); 1704 return (Expression != NULL && 1705 InnerMatcher.matches(*Expression, Finder, Builder)); 1706} 1707 1708/// \brief Matches the false branch expression of a conditional operator. 1709/// 1710/// Example matches b 1711/// condition ? a : b 1712AST_MATCHER_P(ConditionalOperator, hasFalseExpression, 1713 internal::Matcher<Expr>, InnerMatcher) { 1714 Expr *Expression = Node.getFalseExpr(); 1715 return (Expression != NULL && 1716 InnerMatcher.matches(*Expression, Finder, Builder)); 1717} 1718 1719/// \brief Matches if a declaration has a body attached. 1720/// 1721/// Example matches A, va, fa 1722/// class A {}; 1723/// class B; // Doesn't match, as it has no body. 1724/// int va; 1725/// extern int vb; // Doesn't match, as it doesn't define the variable. 1726/// void fa() {} 1727/// void fb(); // Doesn't match, as it has no body. 1728/// 1729/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 1730inline internal::PolymorphicMatcherWithParam0<internal::IsDefinitionMatcher> 1731isDefinition() { 1732 return internal::PolymorphicMatcherWithParam0< 1733 internal::IsDefinitionMatcher>(); 1734} 1735 1736/// \brief Matches the class declaration that the given method declaration 1737/// belongs to. 1738/// 1739/// FIXME: Generalize this for other kinds of declarations. 1740/// FIXME: What other kind of declarations would we need to generalize 1741/// this to? 1742/// 1743/// Example matches A() in the last line 1744/// (matcher = constructorCall(hasDeclaration(method( 1745/// ofClass(hasName("A")))))) 1746/// class A { 1747/// public: 1748/// A(); 1749/// }; 1750/// A a = A(); 1751AST_MATCHER_P(CXXMethodDecl, ofClass, 1752 internal::Matcher<CXXRecordDecl>, InnerMatcher) { 1753 const CXXRecordDecl *Parent = Node.getParent(); 1754 return (Parent != NULL && 1755 InnerMatcher.matches(*Parent, Finder, Builder)); 1756} 1757 1758/// \brief Matches member expressions that are called with '->' as opposed 1759/// to '.'. 1760/// 1761/// Member calls on the implicit this pointer match as called with '->'. 1762/// 1763/// Given 1764/// class Y { 1765/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 1766/// int a; 1767/// static int b; 1768/// }; 1769/// memberExpression(isArrow()) 1770/// matches this->x, x, y.x, a, this->b 1771inline internal::Matcher<MemberExpr> isArrow() { 1772 return makeMatcher(new internal::IsArrowMatcher()); 1773} 1774 1775/// \brief Matches QualType nodes that are of integer type. 1776/// 1777/// Given 1778/// void a(int); 1779/// void b(long); 1780/// void c(double); 1781/// function(hasAnyParameter(hasType(isInteger()))) 1782/// matches "a(int)", "b(long)", but not "c(double)". 1783AST_MATCHER(QualType, isInteger) { 1784 return Node->isIntegerType(); 1785} 1786 1787/// \brief Matches QualType nodes that are const-qualified, i.e., that 1788/// include "top-level" const. 1789/// 1790/// Given 1791/// void a(int); 1792/// void b(int const); 1793/// void c(const int); 1794/// void d(const int*); 1795/// void e(int const) {}; 1796/// function(hasAnyParameter(hasType(isConstQualified()))) 1797/// matches "void b(int const)", "void c(const int)" and 1798/// "void e(int const) {}". It does not match d as there 1799/// is no top-level const on the parameter type "const int *". 1800inline internal::Matcher<QualType> isConstQualified() { 1801 return makeMatcher(new internal::IsConstQualifiedMatcher()); 1802} 1803 1804/// \brief Matches a member expression where the member is matched by a 1805/// given matcher. 1806/// 1807/// Given 1808/// struct { int first, second; } first, second; 1809/// int i(second.first); 1810/// int j(first.second); 1811/// memberExpression(member(hasName("first"))) 1812/// matches second.first 1813/// but not first.second (because the member name there is "second"). 1814AST_MATCHER_P(MemberExpr, member, 1815 internal::Matcher<ValueDecl>, InnerMatcher) { 1816 return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 1817} 1818 1819/// \brief Matches a member expression where the object expression is 1820/// matched by a given matcher. 1821/// 1822/// Given 1823/// struct X { int m; }; 1824/// void f(X x) { x.m; m; } 1825/// memberExpression(hasObjectExpression(hasType(record(hasName("X"))))))) 1826/// matches "x.m" and "m" 1827/// with hasObjectExpression(...) 1828/// matching "x" and the implicit object expression of "m" which has type X*. 1829AST_MATCHER_P(MemberExpr, hasObjectExpression, 1830 internal::Matcher<Expr>, InnerMatcher) { 1831 return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 1832} 1833 1834/// \brief Matches any using shadow declaration. 1835/// 1836/// Given 1837/// namespace X { void b(); } 1838/// using X::b; 1839/// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 1840/// matches \code using X::b \endcode 1841AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 1842 internal::Matcher<UsingShadowDecl>, Matcher) { 1843 for (UsingDecl::shadow_iterator II = Node.shadow_begin(); 1844 II != Node.shadow_end(); ++II) { 1845 if (Matcher.matches(**II, Finder, Builder)) 1846 return true; 1847 } 1848 return false; 1849} 1850 1851/// \brief Matches a using shadow declaration where the target declaration is 1852/// matched by the given matcher. 1853/// 1854/// Given 1855/// namespace X { int a; void b(); } 1856/// using X::a; 1857/// using X::b; 1858/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(function()))) 1859/// matches \code using X::b \endcode 1860/// but not \code using X::a \endcode 1861AST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 1862 internal::Matcher<NamedDecl>, Matcher) { 1863 return Matcher.matches(*Node.getTargetDecl(), Finder, Builder); 1864} 1865 1866/// \brief Matches template instantiations of function, class, or static 1867/// member variable template instantiations. 1868/// 1869/// Given 1870/// template <typename T> class X {}; class A {}; X<A> x; 1871/// or 1872/// template <typename T> class X {}; class A {}; template class X<A>; 1873/// record(hasName("::X"), isTemplateInstantiation()) 1874/// matches the template instantiation of X<A>. 1875/// 1876/// But given 1877/// template <typename T> class X {}; class A {}; 1878/// template <> class X<A> {}; X<A> x; 1879/// record(hasName("::X"), isTemplateInstantiation()) 1880/// does not match, as X<A> is an explicit template specialization. 1881/// 1882/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 1883inline internal::PolymorphicMatcherWithParam0< 1884 internal::IsTemplateInstantiationMatcher> 1885isTemplateInstantiation() { 1886 return internal::PolymorphicMatcherWithParam0< 1887 internal::IsTemplateInstantiationMatcher>(); 1888} 1889 1890} // end namespace ast_matchers 1891} // end namespace clang 1892 1893#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 1894