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