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