ASTMatchers.h revision afbcab8d10d4208c7ad8da79e948432117d4a326
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 compound (i.e. non-scalar) literals 926/// 927/// Example match: {1}, (1, 2) 928/// \code 929/// int array[4] = {1}; vector int myvec = (vector int)(1, 2); 930/// \endcode 931const internal::VariadicDynCastAllOfMatcher< 932 Stmt, 933 CompoundLiteralExpr> compoundLiteralExpr; 934 935/// \brief Matches nullptr literal. 936const internal::VariadicDynCastAllOfMatcher< 937 Stmt, 938 CXXNullPtrLiteralExpr> nullPtrLiteralExpr; 939 940/// \brief Matches binary operator expressions. 941/// 942/// Example matches a || b 943/// \code 944/// !(a || b) 945/// \endcode 946const internal::VariadicDynCastAllOfMatcher< 947 Stmt, 948 BinaryOperator> binaryOperator; 949 950/// \brief Matches unary operator expressions. 951/// 952/// Example matches !a 953/// \code 954/// !a || b 955/// \endcode 956const internal::VariadicDynCastAllOfMatcher< 957 Stmt, 958 UnaryOperator> unaryOperator; 959 960/// \brief Matches conditional operator expressions. 961/// 962/// Example matches a ? b : c 963/// \code 964/// (a ? b : c) + 42 965/// \endcode 966const internal::VariadicDynCastAllOfMatcher< 967 Stmt, 968 ConditionalOperator> conditionalOperator; 969 970/// \brief Matches a reinterpret_cast expression. 971/// 972/// Either the source expression or the destination type can be matched 973/// using has(), but hasDestinationType() is more specific and can be 974/// more readable. 975/// 976/// Example matches reinterpret_cast<char*>(&p) in 977/// \code 978/// void* p = reinterpret_cast<char*>(&p); 979/// \endcode 980const internal::VariadicDynCastAllOfMatcher< 981 Stmt, 982 CXXReinterpretCastExpr> reinterpretCastExpr; 983 984/// \brief Matches a C++ static_cast expression. 985/// 986/// \see hasDestinationType 987/// \see reinterpretCast 988/// 989/// Example: 990/// staticCastExpr() 991/// matches 992/// static_cast<long>(8) 993/// in 994/// \code 995/// long eight(static_cast<long>(8)); 996/// \endcode 997const internal::VariadicDynCastAllOfMatcher< 998 Stmt, 999 CXXStaticCastExpr> staticCastExpr; 1000 1001/// \brief Matches a dynamic_cast expression. 1002/// 1003/// Example: 1004/// dynamicCastExpr() 1005/// matches 1006/// dynamic_cast<D*>(&b); 1007/// in 1008/// \code 1009/// struct B { virtual ~B() {} }; struct D : B {}; 1010/// B b; 1011/// D* p = dynamic_cast<D*>(&b); 1012/// \endcode 1013const internal::VariadicDynCastAllOfMatcher< 1014 Stmt, 1015 CXXDynamicCastExpr> dynamicCastExpr; 1016 1017/// \brief Matches a const_cast expression. 1018/// 1019/// Example: Matches const_cast<int*>(&r) in 1020/// \code 1021/// int n = 42; 1022/// const int &r(n); 1023/// int* p = const_cast<int*>(&r); 1024/// \endcode 1025const internal::VariadicDynCastAllOfMatcher< 1026 Stmt, 1027 CXXConstCastExpr> constCastExpr; 1028 1029/// \brief Matches a C-style cast expression. 1030/// 1031/// Example: Matches (int*) 2.2f in 1032/// \code 1033/// int i = (int) 2.2f; 1034/// \endcode 1035const internal::VariadicDynCastAllOfMatcher< 1036 Stmt, 1037 CStyleCastExpr> cStyleCastExpr; 1038 1039/// \brief Matches explicit cast expressions. 1040/// 1041/// Matches any cast expression written in user code, whether it be a 1042/// C-style cast, a functional-style cast, or a keyword cast. 1043/// 1044/// Does not match implicit conversions. 1045/// 1046/// Note: the name "explicitCast" is chosen to match Clang's terminology, as 1047/// Clang uses the term "cast" to apply to implicit conversions as well as to 1048/// actual cast expressions. 1049/// 1050/// \see hasDestinationType. 1051/// 1052/// Example: matches all five of the casts in 1053/// \code 1054/// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 1055/// \endcode 1056/// but does not match the implicit conversion in 1057/// \code 1058/// long ell = 42; 1059/// \endcode 1060const internal::VariadicDynCastAllOfMatcher< 1061 Stmt, 1062 ExplicitCastExpr> explicitCastExpr; 1063 1064/// \brief Matches the implicit cast nodes of Clang's AST. 1065/// 1066/// This matches many different places, including function call return value 1067/// eliding, as well as any type conversions. 1068const internal::VariadicDynCastAllOfMatcher< 1069 Stmt, 1070 ImplicitCastExpr> implicitCastExpr; 1071 1072/// \brief Matches any cast nodes of Clang's AST. 1073/// 1074/// Example: castExpr() matches each of the following: 1075/// \code 1076/// (int) 3; 1077/// const_cast<Expr *>(SubExpr); 1078/// char c = 0; 1079/// \endcode 1080/// but does not match 1081/// \code 1082/// int i = (0); 1083/// int k = 0; 1084/// \endcode 1085const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr; 1086 1087/// \brief Matches functional cast expressions 1088/// 1089/// Example: Matches Foo(bar); 1090/// \code 1091/// Foo f = bar; 1092/// Foo g = (Foo) bar; 1093/// Foo h = Foo(bar); 1094/// \endcode 1095const internal::VariadicDynCastAllOfMatcher< 1096 Stmt, 1097 CXXFunctionalCastExpr> functionalCastExpr; 1098 1099/// \brief Matches \c QualTypes in the clang AST. 1100const internal::VariadicAllOfMatcher<QualType> qualType; 1101 1102/// \brief Matches \c Types in the clang AST. 1103const internal::VariadicDynCastAllOfMatcher<Type, Type> type; 1104 1105/// \brief Matches \c TypeLocs in the clang AST. 1106const internal::VariadicDynCastAllOfMatcher<TypeLoc, TypeLoc> typeLoc; 1107 1108/// \brief Matches if any of the given matchers matches. 1109/// 1110/// Unlike \c anyOf, \c eachOf will generate a match result for each 1111/// matching submatcher. 1112/// 1113/// For example, in: 1114/// \code 1115/// class A { int a; int b; }; 1116/// \endcode 1117/// The matcher: 1118/// \code 1119/// recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), 1120/// has(fieldDecl(hasName("b")).bind("v")))) 1121/// \endcode 1122/// will generate two results binding "v", the first of which binds 1123/// the field declaration of \c a, the second the field declaration of 1124/// \c b. 1125/// 1126/// Usable as: Any Matcher 1127template <typename M1, typename M2> 1128internal::PolymorphicMatcherWithParam2<internal::EachOfMatcher, M1, M2> 1129eachOf(const M1 &P1, const M2 &P2) { 1130 return internal::PolymorphicMatcherWithParam2<internal::EachOfMatcher, M1, 1131 M2>(P1, P2); 1132} 1133 1134/// \brief Various overloads for the anyOf matcher. 1135/// @{ 1136 1137/// \brief Matches if any of the given matchers matches. 1138/// 1139/// Usable as: Any Matcher 1140template<typename M1, typename M2> 1141internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, M2> 1142anyOf(const M1 &P1, const M2 &P2) { 1143 return internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 1144 M1, M2 >(P1, P2); 1145} 1146template<typename M1, typename M2, typename M3> 1147internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 1148 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, M3> > 1149anyOf(const M1 &P1, const M2 &P2, const M3 &P3) { 1150 return anyOf(P1, anyOf(P2, P3)); 1151} 1152template<typename M1, typename M2, typename M3, typename M4> 1153internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 1154 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, 1155 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 1156 M3, M4> > > 1157anyOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4) { 1158 return anyOf(P1, anyOf(P2, anyOf(P3, P4))); 1159} 1160template<typename M1, typename M2, typename M3, typename M4, typename M5> 1161internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 1162 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, 1163 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M3, 1164 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 1165 M4, M5> > > > 1166anyOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4, const M5 &P5) { 1167 return anyOf(P1, anyOf(P2, anyOf(P3, anyOf(P4, P5)))); 1168} 1169 1170/// @} 1171 1172/// \brief Various overloads for the allOf matcher. 1173/// @{ 1174 1175/// \brief Matches if all given matchers match. 1176/// 1177/// Usable as: Any Matcher 1178template<typename M1, typename M2> 1179internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M1, M2> 1180allOf(const M1 &P1, const M2 &P2) { 1181 return internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, 1182 M1, M2>(P1, P2); 1183} 1184template<typename M1, typename M2, typename M3> 1185internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M1, 1186 internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M2, M3> > 1187allOf(const M1 &P1, const M2 &P2, const M3 &P3) { 1188 return allOf(P1, allOf(P2, P3)); 1189} 1190 1191/// @} 1192 1193/// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 1194/// 1195/// Given 1196/// \code 1197/// Foo x = bar; 1198/// int y = sizeof(x) + alignof(x); 1199/// \endcode 1200/// unaryExprOrTypeTraitExpr() 1201/// matches \c sizeof(x) and \c alignof(x) 1202const internal::VariadicDynCastAllOfMatcher< 1203 Stmt, 1204 UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr; 1205 1206/// \brief Matches unary expressions that have a specific type of argument. 1207/// 1208/// Given 1209/// \code 1210/// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 1211/// \endcode 1212/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 1213/// matches \c sizeof(a) and \c alignof(c) 1214AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 1215 internal::Matcher<QualType>, InnerMatcher) { 1216 const QualType ArgumentType = Node.getTypeOfArgument(); 1217 return InnerMatcher.matches(ArgumentType, Finder, Builder); 1218} 1219 1220/// \brief Matches unary expressions of a certain kind. 1221/// 1222/// Given 1223/// \code 1224/// int x; 1225/// int s = sizeof(x) + alignof(x) 1226/// \endcode 1227/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 1228/// matches \c sizeof(x) 1229AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 1230 return Node.getKind() == Kind; 1231} 1232 1233/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1234/// alignof. 1235inline internal::Matcher<Stmt> alignOfExpr( 1236 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1237 return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf( 1238 ofKind(UETT_AlignOf), InnerMatcher))); 1239} 1240 1241/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1242/// sizeof. 1243inline internal::Matcher<Stmt> sizeOfExpr( 1244 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1245 return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf( 1246 ofKind(UETT_SizeOf), InnerMatcher))); 1247} 1248 1249/// \brief Matches NamedDecl nodes that have the specified name. 1250/// 1251/// Supports specifying enclosing namespaces or classes by prefixing the name 1252/// with '<enclosing>::'. 1253/// Does not match typedefs of an underlying type with the given name. 1254/// 1255/// Example matches X (Name == "X") 1256/// \code 1257/// class X; 1258/// \endcode 1259/// 1260/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 1261/// \code 1262/// namespace a { namespace b { class X; } } 1263/// \endcode 1264AST_MATCHER_P(NamedDecl, hasName, std::string, Name) { 1265 assert(!Name.empty()); 1266 const std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1267 const StringRef FullName = FullNameString; 1268 const StringRef Pattern = Name; 1269 if (Pattern.startswith("::")) { 1270 return FullName == Pattern; 1271 } else { 1272 return FullName.endswith(("::" + Pattern).str()); 1273 } 1274} 1275 1276/// \brief Matches NamedDecl nodes whose fully qualified names contain 1277/// a substring matched by the given RegExp. 1278/// 1279/// Supports specifying enclosing namespaces or classes by 1280/// prefixing the name with '<enclosing>::'. Does not match typedefs 1281/// of an underlying type with the given name. 1282/// 1283/// Example matches X (regexp == "::X") 1284/// \code 1285/// class X; 1286/// \endcode 1287/// 1288/// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 1289/// \code 1290/// namespace foo { namespace bar { class X; } } 1291/// \endcode 1292AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 1293 assert(!RegExp.empty()); 1294 std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1295 llvm::Regex RE(RegExp); 1296 return RE.match(FullNameString); 1297} 1298 1299/// \brief Matches overloaded operator names. 1300/// 1301/// Matches overloaded operator names specified in strings without the 1302/// "operator" prefix, such as "<<", for OverloadedOperatorCall's. 1303/// 1304/// Example matches a << b 1305/// (matcher == operatorCallExpr(hasOverloadedOperatorName("<<"))) 1306/// \code 1307/// a << b; 1308/// c && d; // assuming both operator<< 1309/// // and operator&& are overloaded somewhere. 1310/// \endcode 1311AST_MATCHER_P(CXXOperatorCallExpr, 1312 hasOverloadedOperatorName, std::string, Name) { 1313 return getOperatorSpelling(Node.getOperator()) == Name; 1314} 1315 1316/// \brief Matches C++ classes that are directly or indirectly derived from 1317/// a class matching \c Base. 1318/// 1319/// Note that a class is not considered to be derived from itself. 1320/// 1321/// Example matches Y, Z, C (Base == hasName("X")) 1322/// \code 1323/// class X; 1324/// class Y : public X {}; // directly derived 1325/// class Z : public Y {}; // indirectly derived 1326/// typedef X A; 1327/// typedef A B; 1328/// class C : public B {}; // derived from a typedef of X 1329/// \endcode 1330/// 1331/// In the following example, Bar matches isDerivedFrom(hasName("X")): 1332/// \code 1333/// class Foo; 1334/// typedef Foo X; 1335/// class Bar : public Foo {}; // derived from a type that X is a typedef of 1336/// \endcode 1337AST_MATCHER_P(CXXRecordDecl, isDerivedFrom, 1338 internal::Matcher<NamedDecl>, Base) { 1339 return Finder->classIsDerivedFrom(&Node, Base, Builder); 1340} 1341 1342/// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 1343inline internal::Matcher<CXXRecordDecl> isDerivedFrom(StringRef BaseName) { 1344 assert(!BaseName.empty()); 1345 return isDerivedFrom(hasName(BaseName)); 1346} 1347 1348/// \brief Similar to \c isDerivedFrom(), but also matches classes that directly 1349/// match \c Base. 1350inline internal::Matcher<CXXRecordDecl> isSameOrDerivedFrom( 1351 internal::Matcher<NamedDecl> Base) { 1352 return anyOf(Base, isDerivedFrom(Base)); 1353} 1354 1355/// \brief Overloaded method as shortcut for 1356/// \c isSameOrDerivedFrom(hasName(...)). 1357inline internal::Matcher<CXXRecordDecl> isSameOrDerivedFrom( 1358 StringRef BaseName) { 1359 assert(!BaseName.empty()); 1360 return isSameOrDerivedFrom(hasName(BaseName)); 1361} 1362 1363/// \brief Matches AST nodes that have child AST nodes that match the 1364/// provided matcher. 1365/// 1366/// Example matches X, Y (matcher = recordDecl(has(recordDecl(hasName("X"))) 1367/// \code 1368/// class X {}; // Matches X, because X::X is a class of name X inside X. 1369/// class Y { class X {}; }; 1370/// class Z { class Y { class X {}; }; }; // Does not match Z. 1371/// \endcode 1372/// 1373/// ChildT must be an AST base type. 1374/// 1375/// Usable as: Any Matcher 1376template <typename ChildT> 1377internal::ArgumentAdaptingMatcher<internal::HasMatcher, ChildT> has( 1378 const internal::Matcher<ChildT> &ChildMatcher) { 1379 return internal::ArgumentAdaptingMatcher<internal::HasMatcher, 1380 ChildT>(ChildMatcher); 1381} 1382 1383/// \brief Matches AST nodes that have descendant AST nodes that match the 1384/// provided matcher. 1385/// 1386/// Example matches X, Y, Z 1387/// (matcher = recordDecl(hasDescendant(recordDecl(hasName("X"))))) 1388/// \code 1389/// class X {}; // Matches X, because X::X is a class of name X inside X. 1390/// class Y { class X {}; }; 1391/// class Z { class Y { class X {}; }; }; 1392/// \endcode 1393/// 1394/// DescendantT must be an AST base type. 1395/// 1396/// Usable as: Any Matcher 1397template <typename DescendantT> 1398internal::ArgumentAdaptingMatcher<internal::HasDescendantMatcher, DescendantT> 1399hasDescendant(const internal::Matcher<DescendantT> &DescendantMatcher) { 1400 return internal::ArgumentAdaptingMatcher< 1401 internal::HasDescendantMatcher, 1402 DescendantT>(DescendantMatcher); 1403} 1404 1405/// \brief Matches AST nodes that have child AST nodes that match the 1406/// provided matcher. 1407/// 1408/// Example matches X, Y (matcher = recordDecl(forEach(recordDecl(hasName("X"))) 1409/// \code 1410/// class X {}; // Matches X, because X::X is a class of name X inside X. 1411/// class Y { class X {}; }; 1412/// class Z { class Y { class X {}; }; }; // Does not match Z. 1413/// \endcode 1414/// 1415/// ChildT must be an AST base type. 1416/// 1417/// As opposed to 'has', 'forEach' will cause a match for each result that 1418/// matches instead of only on the first one. 1419/// 1420/// Usable as: Any Matcher 1421template <typename ChildT> 1422internal::ArgumentAdaptingMatcher<internal::ForEachMatcher, ChildT> forEach( 1423 const internal::Matcher<ChildT> &ChildMatcher) { 1424 return internal::ArgumentAdaptingMatcher< 1425 internal::ForEachMatcher, 1426 ChildT>(ChildMatcher); 1427} 1428 1429/// \brief Matches AST nodes that have descendant AST nodes that match the 1430/// provided matcher. 1431/// 1432/// Example matches X, A, B, C 1433/// (matcher = recordDecl(forEachDescendant(recordDecl(hasName("X"))))) 1434/// \code 1435/// class X {}; // Matches X, because X::X is a class of name X inside X. 1436/// class A { class X {}; }; 1437/// class B { class C { class X {}; }; }; 1438/// \endcode 1439/// 1440/// DescendantT must be an AST base type. 1441/// 1442/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 1443/// each result that matches instead of only on the first one. 1444/// 1445/// Note: Recursively combined ForEachDescendant can cause many matches: 1446/// recordDecl(forEachDescendant(recordDecl(forEachDescendant(recordDecl())))) 1447/// will match 10 times (plus injected class name matches) on: 1448/// \code 1449/// class A { class B { class C { class D { class E {}; }; }; }; }; 1450/// \endcode 1451/// 1452/// Usable as: Any Matcher 1453template <typename DescendantT> 1454internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, 1455 DescendantT> 1456forEachDescendant( 1457 const internal::Matcher<DescendantT> &DescendantMatcher) { 1458 return internal::ArgumentAdaptingMatcher< 1459 internal::ForEachDescendantMatcher, 1460 DescendantT>(DescendantMatcher); 1461} 1462 1463/// \brief Matches if the node or any descendant matches. 1464/// 1465/// Generates results for each match. 1466/// 1467/// For example, in: 1468/// \code 1469/// class A { class B {}; class C {}; }; 1470/// \endcode 1471/// The matcher: 1472/// \code 1473/// recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("m"))) 1474/// \endcode 1475/// will generate results for \c A, \c B and \c C. 1476/// 1477/// Usable as: Any Matcher 1478template <typename T> 1479internal::PolymorphicMatcherWithParam2< 1480 internal::EachOfMatcher, internal::Matcher<T>, 1481 internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, T> > 1482findAll(const internal::Matcher<T> &Matcher) { 1483 return eachOf(Matcher, forEachDescendant(Matcher)); 1484} 1485 1486/// \brief Matches AST nodes that have a parent that matches the provided 1487/// matcher. 1488/// 1489/// Given 1490/// \code 1491/// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } } 1492/// \endcode 1493/// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }". 1494/// 1495/// Usable as: Any Matcher 1496template <typename ParentT> 1497internal::ArgumentAdaptingMatcher<internal::HasParentMatcher, ParentT> 1498hasParent(const internal::Matcher<ParentT> &ParentMatcher) { 1499 return internal::ArgumentAdaptingMatcher< 1500 internal::HasParentMatcher, 1501 ParentT>(ParentMatcher); 1502} 1503 1504/// \brief Matches AST nodes that have an ancestor that matches the provided 1505/// matcher. 1506/// 1507/// Given 1508/// \code 1509/// void f() { if (true) { int x = 42; } } 1510/// void g() { for (;;) { int x = 43; } } 1511/// \endcode 1512/// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43. 1513/// 1514/// Usable as: Any Matcher 1515template <typename AncestorT> 1516internal::ArgumentAdaptingMatcher<internal::HasAncestorMatcher, AncestorT> 1517hasAncestor(const internal::Matcher<AncestorT> &AncestorMatcher) { 1518 return internal::ArgumentAdaptingMatcher< 1519 internal::HasAncestorMatcher, 1520 AncestorT>(AncestorMatcher); 1521} 1522 1523/// \brief Matches if the provided matcher does not match. 1524/// 1525/// Example matches Y (matcher = recordDecl(unless(hasName("X")))) 1526/// \code 1527/// class X {}; 1528/// class Y {}; 1529/// \endcode 1530/// 1531/// Usable as: Any Matcher 1532template <typename M> 1533internal::PolymorphicMatcherWithParam1<internal::NotMatcher, M> 1534unless(const M &InnerMatcher) { 1535 return internal::PolymorphicMatcherWithParam1< 1536 internal::NotMatcher, M>(InnerMatcher); 1537} 1538 1539/// \brief Matches a type if the declaration of the type matches the given 1540/// matcher. 1541/// 1542/// Usable as: Matcher<QualType>, Matcher<CallExpr>, Matcher<CXXConstructExpr>, 1543/// Matcher<MemberExpr> 1544inline internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, 1545 internal::Matcher<Decl> > 1546 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 1547 return internal::PolymorphicMatcherWithParam1< 1548 internal::HasDeclarationMatcher, 1549 internal::Matcher<Decl> >(InnerMatcher); 1550} 1551 1552/// \brief Matches on the implicit object argument of a member call expression. 1553/// 1554/// Example matches y.x() (matcher = callExpr(on(hasType(recordDecl(hasName("Y")))))) 1555/// \code 1556/// class Y { public: void x(); }; 1557/// void z() { Y y; y.x(); }", 1558/// \endcode 1559/// 1560/// FIXME: Overload to allow directly matching types? 1561AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 1562 InnerMatcher) { 1563 const Expr *ExprNode = Node.getImplicitObjectArgument() 1564 ->IgnoreParenImpCasts(); 1565 return (ExprNode != NULL && 1566 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1567} 1568 1569/// \brief Matches if the call expression's callee expression matches. 1570/// 1571/// Given 1572/// \code 1573/// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 1574/// void f() { f(); } 1575/// \endcode 1576/// callExpr(callee(expr())) 1577/// matches this->x(), x(), y.x(), f() 1578/// with callee(...) 1579/// matching this->x, x, y.x, f respectively 1580/// 1581/// Note: Callee cannot take the more general internal::Matcher<Expr> 1582/// because this introduces ambiguous overloads with calls to Callee taking a 1583/// internal::Matcher<Decl>, as the matcher hierarchy is purely 1584/// implemented in terms of implicit casts. 1585AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 1586 InnerMatcher) { 1587 const Expr *ExprNode = Node.getCallee(); 1588 return (ExprNode != NULL && 1589 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1590} 1591 1592/// \brief Matches if the call expression's callee's declaration matches the 1593/// given matcher. 1594/// 1595/// Example matches y.x() (matcher = callExpr(callee(methodDecl(hasName("x"))))) 1596/// \code 1597/// class Y { public: void x(); }; 1598/// void z() { Y y; y.x(); 1599/// \endcode 1600inline internal::Matcher<CallExpr> callee( 1601 const internal::Matcher<Decl> &InnerMatcher) { 1602 return internal::Matcher<CallExpr>(hasDeclaration(InnerMatcher)); 1603} 1604 1605/// \brief Matches if the expression's or declaration's type matches a type 1606/// matcher. 1607/// 1608/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1609/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1610/// \code 1611/// class X {}; 1612/// void y(X &x) { x; X z; } 1613/// \endcode 1614AST_POLYMORPHIC_MATCHER_P(hasType, internal::Matcher<QualType>, 1615 InnerMatcher) { 1616 TOOLING_COMPILE_ASSERT((llvm::is_base_of<Expr, NodeType>::value || 1617 llvm::is_base_of<ValueDecl, NodeType>::value), 1618 instantiated_with_wrong_types); 1619 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1620} 1621 1622/// \brief Overloaded to match the declaration of the expression's or value 1623/// declaration's type. 1624/// 1625/// In case of a value declaration (for example a variable declaration), 1626/// this resolves one layer of indirection. For example, in the value 1627/// declaration "X x;", recordDecl(hasName("X")) matches the declaration of X, 1628/// while varDecl(hasType(recordDecl(hasName("X")))) matches the declaration 1629/// of x." 1630/// 1631/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1632/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1633/// \code 1634/// class X {}; 1635/// void y(X &x) { x; X z; } 1636/// \endcode 1637/// 1638/// Usable as: Matcher<Expr>, Matcher<ValueDecl> 1639inline internal::PolymorphicMatcherWithParam1< 1640 internal::matcher_hasTypeMatcher, 1641 internal::Matcher<QualType> > 1642hasType(const internal::Matcher<Decl> &InnerMatcher) { 1643 return hasType(internal::Matcher<QualType>( 1644 hasDeclaration(InnerMatcher))); 1645} 1646 1647/// \brief Matches if the matched type is represented by the given string. 1648/// 1649/// Given 1650/// \code 1651/// class Y { public: void x(); }; 1652/// void z() { Y* y; y->x(); } 1653/// \endcode 1654/// callExpr(on(hasType(asString("class Y *")))) 1655/// matches y->x() 1656AST_MATCHER_P(QualType, asString, std::string, Name) { 1657 return Name == Node.getAsString(); 1658} 1659 1660/// \brief Matches if the matched type is a pointer type and the pointee type 1661/// matches the specified matcher. 1662/// 1663/// Example matches y->x() 1664/// (matcher = callExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))))) 1665/// \code 1666/// class Y { public: void x(); }; 1667/// void z() { Y *y; y->x(); } 1668/// \endcode 1669AST_MATCHER_P( 1670 QualType, pointsTo, internal::Matcher<QualType>, 1671 InnerMatcher) { 1672 return (!Node.isNull() && Node->isPointerType() && 1673 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1674} 1675 1676/// \brief Overloaded to match the pointee type's declaration. 1677inline internal::Matcher<QualType> pointsTo( 1678 const internal::Matcher<Decl> &InnerMatcher) { 1679 return pointsTo(internal::Matcher<QualType>( 1680 hasDeclaration(InnerMatcher))); 1681} 1682 1683/// \brief Matches if the matched type is a reference type and the referenced 1684/// type matches the specified matcher. 1685/// 1686/// Example matches X &x and const X &y 1687/// (matcher = varDecl(hasType(references(recordDecl(hasName("X")))))) 1688/// \code 1689/// class X { 1690/// void a(X b) { 1691/// X &x = b; 1692/// const X &y = b; 1693/// }; 1694/// \endcode 1695AST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 1696 InnerMatcher) { 1697 return (!Node.isNull() && Node->isReferenceType() && 1698 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1699} 1700 1701/// \brief Overloaded to match the referenced type's declaration. 1702inline internal::Matcher<QualType> references( 1703 const internal::Matcher<Decl> &InnerMatcher) { 1704 return references(internal::Matcher<QualType>( 1705 hasDeclaration(InnerMatcher))); 1706} 1707 1708AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 1709 internal::Matcher<Expr>, InnerMatcher) { 1710 const Expr *ExprNode = Node.getImplicitObjectArgument(); 1711 return (ExprNode != NULL && 1712 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1713} 1714 1715/// \brief Matches if the expression's type either matches the specified 1716/// matcher, or is a pointer to a type that matches the InnerMatcher. 1717inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1718 const internal::Matcher<QualType> &InnerMatcher) { 1719 return onImplicitObjectArgument( 1720 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1721} 1722 1723/// \brief Overloaded to match the type's declaration. 1724inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1725 const internal::Matcher<Decl> &InnerMatcher) { 1726 return onImplicitObjectArgument( 1727 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1728} 1729 1730/// \brief Matches a DeclRefExpr that refers to a declaration that matches the 1731/// specified matcher. 1732/// 1733/// Example matches x in if(x) 1734/// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 1735/// \code 1736/// bool x; 1737/// if (x) {} 1738/// \endcode 1739AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 1740 InnerMatcher) { 1741 const Decl *DeclNode = Node.getDecl(); 1742 return (DeclNode != NULL && 1743 InnerMatcher.matches(*DeclNode, Finder, Builder)); 1744} 1745 1746/// \brief Matches a \c DeclRefExpr that refers to a declaration through a 1747/// specific using shadow declaration. 1748/// 1749/// FIXME: This currently only works for functions. Fix. 1750/// 1751/// Given 1752/// \code 1753/// namespace a { void f() {} } 1754/// using a::f; 1755/// void g() { 1756/// f(); // Matches this .. 1757/// a::f(); // .. but not this. 1758/// } 1759/// \endcode 1760/// declRefExpr(throughUsingDeclaration(anything())) 1761/// matches \c f() 1762AST_MATCHER_P(DeclRefExpr, throughUsingDecl, 1763 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 1764 const NamedDecl *FoundDecl = Node.getFoundDecl(); 1765 if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl)) 1766 return InnerMatcher.matches(*UsingDecl, Finder, Builder); 1767 return false; 1768} 1769 1770/// \brief Matches the Decl of a DeclStmt which has a single declaration. 1771/// 1772/// Given 1773/// \code 1774/// int a, b; 1775/// int c; 1776/// \endcode 1777/// declStmt(hasSingleDecl(anything())) 1778/// matches 'int c;' but not 'int a, b;'. 1779AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 1780 if (Node.isSingleDecl()) { 1781 const Decl *FoundDecl = Node.getSingleDecl(); 1782 return InnerMatcher.matches(*FoundDecl, Finder, Builder); 1783 } 1784 return false; 1785} 1786 1787/// \brief Matches a variable declaration that has an initializer expression 1788/// that matches the given matcher. 1789/// 1790/// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 1791/// \code 1792/// bool y() { return true; } 1793/// bool x = y(); 1794/// \endcode 1795AST_MATCHER_P( 1796 VarDecl, hasInitializer, internal::Matcher<Expr>, 1797 InnerMatcher) { 1798 const Expr *Initializer = Node.getAnyInitializer(); 1799 return (Initializer != NULL && 1800 InnerMatcher.matches(*Initializer, Finder, Builder)); 1801} 1802 1803/// \brief Checks that a call expression or a constructor call expression has 1804/// a specific number of arguments (including absent default arguments). 1805/// 1806/// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 1807/// \code 1808/// void f(int x, int y); 1809/// f(0, 0); 1810/// \endcode 1811AST_POLYMORPHIC_MATCHER_P(argumentCountIs, unsigned, N) { 1812 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1813 llvm::is_base_of<CXXConstructExpr, 1814 NodeType>::value), 1815 instantiated_with_wrong_types); 1816 return Node.getNumArgs() == N; 1817} 1818 1819/// \brief Matches the n'th argument of a call expression or a constructor 1820/// call expression. 1821/// 1822/// Example matches y in x(y) 1823/// (matcher = callExpr(hasArgument(0, declRefExpr()))) 1824/// \code 1825/// void x(int) { int y; x(y); } 1826/// \endcode 1827AST_POLYMORPHIC_MATCHER_P2( 1828 hasArgument, unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 1829 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1830 llvm::is_base_of<CXXConstructExpr, 1831 NodeType>::value), 1832 instantiated_with_wrong_types); 1833 return (N < Node.getNumArgs() && 1834 InnerMatcher.matches( 1835 *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 1836} 1837 1838/// \brief Matches declaration statements that contain a specific number of 1839/// declarations. 1840/// 1841/// Example: Given 1842/// \code 1843/// int a, b; 1844/// int c; 1845/// int d = 2, e; 1846/// \endcode 1847/// declCountIs(2) 1848/// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 1849AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 1850 return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 1851} 1852 1853/// \brief Matches the n'th declaration of a declaration statement. 1854/// 1855/// Note that this does not work for global declarations because the AST 1856/// breaks up multiple-declaration DeclStmt's into multiple single-declaration 1857/// DeclStmt's. 1858/// Example: Given non-global declarations 1859/// \code 1860/// int a, b = 0; 1861/// int c; 1862/// int d = 2, e; 1863/// \endcode 1864/// declStmt(containsDeclaration( 1865/// 0, varDecl(hasInitializer(anything())))) 1866/// matches only 'int d = 2, e;', and 1867/// declStmt(containsDeclaration(1, varDecl())) 1868/// \code 1869/// matches 'int a, b = 0' as well as 'int d = 2, e;' 1870/// but 'int c;' is not matched. 1871/// \endcode 1872AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 1873 internal::Matcher<Decl>, InnerMatcher) { 1874 const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 1875 if (N >= NumDecls) 1876 return false; 1877 DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 1878 std::advance(Iterator, N); 1879 return InnerMatcher.matches(**Iterator, Finder, Builder); 1880} 1881 1882/// \brief Matches a constructor initializer. 1883/// 1884/// Given 1885/// \code 1886/// struct Foo { 1887/// Foo() : foo_(1) { } 1888/// int foo_; 1889/// }; 1890/// \endcode 1891/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer(anything())))) 1892/// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 1893AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 1894 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 1895 for (CXXConstructorDecl::init_const_iterator I = Node.init_begin(); 1896 I != Node.init_end(); ++I) { 1897 if (InnerMatcher.matches(**I, Finder, Builder)) { 1898 return true; 1899 } 1900 } 1901 return false; 1902} 1903 1904/// \brief Matches the field declaration of a constructor initializer. 1905/// 1906/// Given 1907/// \code 1908/// struct Foo { 1909/// Foo() : foo_(1) { } 1910/// int foo_; 1911/// }; 1912/// \endcode 1913/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 1914/// forField(hasName("foo_")))))) 1915/// matches Foo 1916/// with forField matching foo_ 1917AST_MATCHER_P(CXXCtorInitializer, forField, 1918 internal::Matcher<FieldDecl>, InnerMatcher) { 1919 const FieldDecl *NodeAsDecl = Node.getMember(); 1920 return (NodeAsDecl != NULL && 1921 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 1922} 1923 1924/// \brief Matches the initializer expression of a constructor initializer. 1925/// 1926/// Given 1927/// \code 1928/// struct Foo { 1929/// Foo() : foo_(1) { } 1930/// int foo_; 1931/// }; 1932/// \endcode 1933/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 1934/// withInitializer(integerLiteral(equals(1))))))) 1935/// matches Foo 1936/// with withInitializer matching (1) 1937AST_MATCHER_P(CXXCtorInitializer, withInitializer, 1938 internal::Matcher<Expr>, InnerMatcher) { 1939 const Expr* NodeAsExpr = Node.getInit(); 1940 return (NodeAsExpr != NULL && 1941 InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 1942} 1943 1944/// \brief Matches a contructor initializer if it is explicitly written in 1945/// code (as opposed to implicitly added by the compiler). 1946/// 1947/// Given 1948/// \code 1949/// struct Foo { 1950/// Foo() { } 1951/// Foo(int) : foo_("A") { } 1952/// string foo_; 1953/// }; 1954/// \endcode 1955/// constructorDecl(hasAnyConstructorInitializer(isWritten())) 1956/// will match Foo(int), but not Foo() 1957AST_MATCHER(CXXCtorInitializer, isWritten) { 1958 return Node.isWritten(); 1959} 1960 1961/// \brief Matches a constructor declaration that has been implicitly added 1962/// by the compiler (eg. implicit default/copy constructors). 1963AST_MATCHER(CXXConstructorDecl, isImplicit) { 1964 return Node.isImplicit(); 1965} 1966 1967/// \brief Matches any argument of a call expression or a constructor call 1968/// expression. 1969/// 1970/// Given 1971/// \code 1972/// void x(int, int, int) { int y; x(1, y, 42); } 1973/// \endcode 1974/// callExpr(hasAnyArgument(declRefExpr())) 1975/// matches x(1, y, 42) 1976/// with hasAnyArgument(...) 1977/// matching y 1978AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, internal::Matcher<Expr>, 1979 InnerMatcher) { 1980 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1981 llvm::is_base_of<CXXConstructExpr, 1982 NodeType>::value), 1983 instantiated_with_wrong_types); 1984 for (unsigned I = 0; I < Node.getNumArgs(); ++I) { 1985 if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(), 1986 Finder, Builder)) { 1987 return true; 1988 } 1989 } 1990 return false; 1991} 1992 1993/// \brief Matches the n'th parameter of a function declaration. 1994/// 1995/// Given 1996/// \code 1997/// class X { void f(int x) {} }; 1998/// \endcode 1999/// methodDecl(hasParameter(0, hasType(varDecl()))) 2000/// matches f(int x) {} 2001/// with hasParameter(...) 2002/// matching int x 2003AST_MATCHER_P2(FunctionDecl, hasParameter, 2004 unsigned, N, internal::Matcher<ParmVarDecl>, 2005 InnerMatcher) { 2006 return (N < Node.getNumParams() && 2007 InnerMatcher.matches( 2008 *Node.getParamDecl(N), Finder, Builder)); 2009} 2010 2011/// \brief Matches any parameter of a function declaration. 2012/// 2013/// Does not match the 'this' parameter of a method. 2014/// 2015/// Given 2016/// \code 2017/// class X { void f(int x, int y, int z) {} }; 2018/// \endcode 2019/// methodDecl(hasAnyParameter(hasName("y"))) 2020/// matches f(int x, int y, int z) {} 2021/// with hasAnyParameter(...) 2022/// matching int y 2023AST_MATCHER_P(FunctionDecl, hasAnyParameter, 2024 internal::Matcher<ParmVarDecl>, InnerMatcher) { 2025 for (unsigned I = 0; I < Node.getNumParams(); ++I) { 2026 if (InnerMatcher.matches(*Node.getParamDecl(I), Finder, Builder)) { 2027 return true; 2028 } 2029 } 2030 return false; 2031} 2032 2033/// \brief Matches \c FunctionDecls that have a specific parameter count. 2034/// 2035/// Given 2036/// \code 2037/// void f(int i) {} 2038/// void g(int i, int j) {} 2039/// \endcode 2040/// functionDecl(parameterCountIs(2)) 2041/// matches g(int i, int j) {} 2042AST_MATCHER_P(FunctionDecl, parameterCountIs, unsigned, N) { 2043 return Node.getNumParams() == N; 2044} 2045 2046/// \brief Matches the return type of a function declaration. 2047/// 2048/// Given: 2049/// \code 2050/// class X { int f() { return 1; } }; 2051/// \endcode 2052/// methodDecl(returns(asString("int"))) 2053/// matches int f() { return 1; } 2054AST_MATCHER_P(FunctionDecl, returns, 2055 internal::Matcher<QualType>, InnerMatcher) { 2056 return InnerMatcher.matches(Node.getResultType(), Finder, Builder); 2057} 2058 2059/// \brief Matches extern "C" function declarations. 2060/// 2061/// Given: 2062/// \code 2063/// extern "C" void f() {} 2064/// extern "C" { void g() {} } 2065/// void h() {} 2066/// \endcode 2067/// functionDecl(isExternC()) 2068/// matches the declaration of f and g, but not the declaration h 2069AST_MATCHER(FunctionDecl, isExternC) { 2070 return Node.isExternC(); 2071} 2072 2073/// \brief Matches the condition expression of an if statement, for loop, 2074/// or conditional operator. 2075/// 2076/// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 2077/// \code 2078/// if (true) {} 2079/// \endcode 2080AST_POLYMORPHIC_MATCHER_P(hasCondition, internal::Matcher<Expr>, 2081 InnerMatcher) { 2082 TOOLING_COMPILE_ASSERT( 2083 (llvm::is_base_of<IfStmt, NodeType>::value) || 2084 (llvm::is_base_of<ForStmt, NodeType>::value) || 2085 (llvm::is_base_of<WhileStmt, NodeType>::value) || 2086 (llvm::is_base_of<DoStmt, NodeType>::value) || 2087 (llvm::is_base_of<ConditionalOperator, NodeType>::value), 2088 has_condition_requires_if_statement_conditional_operator_or_loop); 2089 const Expr *const Condition = Node.getCond(); 2090 return (Condition != NULL && 2091 InnerMatcher.matches(*Condition, Finder, Builder)); 2092} 2093 2094/// \brief Matches the condition variable statement in an if statement. 2095/// 2096/// Given 2097/// \code 2098/// if (A* a = GetAPointer()) {} 2099/// \endcode 2100/// hasConditionVariableStatment(...) 2101/// matches 'A* a = GetAPointer()'. 2102AST_MATCHER_P(IfStmt, hasConditionVariableStatement, 2103 internal::Matcher<DeclStmt>, InnerMatcher) { 2104 const DeclStmt* const DeclarationStatement = 2105 Node.getConditionVariableDeclStmt(); 2106 return DeclarationStatement != NULL && 2107 InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 2108} 2109 2110/// \brief Matches the index expression of an array subscript expression. 2111/// 2112/// Given 2113/// \code 2114/// int i[5]; 2115/// void f() { i[1] = 42; } 2116/// \endcode 2117/// arraySubscriptExpression(hasIndex(integerLiteral())) 2118/// matches \c i[1] with the \c integerLiteral() matching \c 1 2119AST_MATCHER_P(ArraySubscriptExpr, hasIndex, 2120 internal::Matcher<Expr>, InnerMatcher) { 2121 if (const Expr* Expression = Node.getIdx()) 2122 return InnerMatcher.matches(*Expression, Finder, Builder); 2123 return false; 2124} 2125 2126/// \brief Matches the base expression of an array subscript expression. 2127/// 2128/// Given 2129/// \code 2130/// int i[5]; 2131/// void f() { i[1] = 42; } 2132/// \endcode 2133/// arraySubscriptExpression(hasBase(implicitCastExpr( 2134/// hasSourceExpression(declRefExpr())))) 2135/// matches \c i[1] with the \c declRefExpr() matching \c i 2136AST_MATCHER_P(ArraySubscriptExpr, hasBase, 2137 internal::Matcher<Expr>, InnerMatcher) { 2138 if (const Expr* Expression = Node.getBase()) 2139 return InnerMatcher.matches(*Expression, Finder, Builder); 2140 return false; 2141} 2142 2143/// \brief Matches a 'for', 'while', or 'do while' statement that has 2144/// a given body. 2145/// 2146/// Given 2147/// \code 2148/// for (;;) {} 2149/// \endcode 2150/// hasBody(compoundStmt()) 2151/// matches 'for (;;) {}' 2152/// with compoundStmt() 2153/// matching '{}' 2154AST_POLYMORPHIC_MATCHER_P(hasBody, internal::Matcher<Stmt>, 2155 InnerMatcher) { 2156 TOOLING_COMPILE_ASSERT( 2157 (llvm::is_base_of<DoStmt, NodeType>::value) || 2158 (llvm::is_base_of<ForStmt, NodeType>::value) || 2159 (llvm::is_base_of<WhileStmt, NodeType>::value), 2160 has_body_requires_for_while_or_do_statement); 2161 const Stmt *const Statement = Node.getBody(); 2162 return (Statement != NULL && 2163 InnerMatcher.matches(*Statement, Finder, Builder)); 2164} 2165 2166/// \brief Matches compound statements where at least one substatement matches 2167/// a given matcher. 2168/// 2169/// Given 2170/// \code 2171/// { {}; 1+2; } 2172/// \endcode 2173/// hasAnySubstatement(compoundStmt()) 2174/// matches '{ {}; 1+2; }' 2175/// with compoundStmt() 2176/// matching '{}' 2177AST_MATCHER_P(CompoundStmt, hasAnySubstatement, 2178 internal::Matcher<Stmt>, InnerMatcher) { 2179 for (CompoundStmt::const_body_iterator It = Node.body_begin(); 2180 It != Node.body_end(); 2181 ++It) { 2182 if (InnerMatcher.matches(**It, Finder, Builder)) return true; 2183 } 2184 return false; 2185} 2186 2187/// \brief Checks that a compound statement contains a specific number of 2188/// child statements. 2189/// 2190/// Example: Given 2191/// \code 2192/// { for (;;) {} } 2193/// \endcode 2194/// compoundStmt(statementCountIs(0))) 2195/// matches '{}' 2196/// but does not match the outer compound statement. 2197AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 2198 return Node.size() == N; 2199} 2200 2201/// \brief Matches literals that are equal to the given value. 2202/// 2203/// Example matches true (matcher = boolLiteral(equals(true))) 2204/// \code 2205/// true 2206/// \endcode 2207/// 2208/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 2209/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 2210template <typename ValueT> 2211internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 2212equals(const ValueT &Value) { 2213 return internal::PolymorphicMatcherWithParam1< 2214 internal::ValueEqualsMatcher, 2215 ValueT>(Value); 2216} 2217 2218/// \brief Matches the operator Name of operator expressions (binary or 2219/// unary). 2220/// 2221/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 2222/// \code 2223/// !(a || b) 2224/// \endcode 2225AST_POLYMORPHIC_MATCHER_P(hasOperatorName, std::string, Name) { 2226 TOOLING_COMPILE_ASSERT( 2227 (llvm::is_base_of<BinaryOperator, NodeType>::value) || 2228 (llvm::is_base_of<UnaryOperator, NodeType>::value), 2229 has_condition_requires_if_statement_or_conditional_operator); 2230 return Name == Node.getOpcodeStr(Node.getOpcode()); 2231} 2232 2233/// \brief Matches the left hand side of binary operator expressions. 2234/// 2235/// Example matches a (matcher = binaryOperator(hasLHS())) 2236/// \code 2237/// a || b 2238/// \endcode 2239AST_MATCHER_P(BinaryOperator, hasLHS, 2240 internal::Matcher<Expr>, InnerMatcher) { 2241 Expr *LeftHandSide = Node.getLHS(); 2242 return (LeftHandSide != NULL && 2243 InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 2244} 2245 2246/// \brief Matches the right hand side of binary operator expressions. 2247/// 2248/// Example matches b (matcher = binaryOperator(hasRHS())) 2249/// \code 2250/// a || b 2251/// \endcode 2252AST_MATCHER_P(BinaryOperator, hasRHS, 2253 internal::Matcher<Expr>, InnerMatcher) { 2254 Expr *RightHandSide = Node.getRHS(); 2255 return (RightHandSide != NULL && 2256 InnerMatcher.matches(*RightHandSide, Finder, Builder)); 2257} 2258 2259/// \brief Matches if either the left hand side or the right hand side of a 2260/// binary operator matches. 2261inline internal::Matcher<BinaryOperator> hasEitherOperand( 2262 const internal::Matcher<Expr> &InnerMatcher) { 2263 return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 2264} 2265 2266/// \brief Matches if the operand of a unary operator matches. 2267/// 2268/// Example matches true (matcher = hasUnaryOperand(boolLiteral(equals(true)))) 2269/// \code 2270/// !true 2271/// \endcode 2272AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 2273 internal::Matcher<Expr>, InnerMatcher) { 2274 const Expr * const Operand = Node.getSubExpr(); 2275 return (Operand != NULL && 2276 InnerMatcher.matches(*Operand, Finder, Builder)); 2277} 2278 2279/// \brief Matches if the cast's source expression matches the given matcher. 2280/// 2281/// Example: matches "a string" (matcher = 2282/// hasSourceExpression(constructExpr())) 2283/// \code 2284/// class URL { URL(string); }; 2285/// URL url = "a string"; 2286AST_MATCHER_P(CastExpr, hasSourceExpression, 2287 internal::Matcher<Expr>, InnerMatcher) { 2288 const Expr* const SubExpression = Node.getSubExpr(); 2289 return (SubExpression != NULL && 2290 InnerMatcher.matches(*SubExpression, Finder, Builder)); 2291} 2292 2293/// \brief Matches casts whose destination type matches a given matcher. 2294/// 2295/// (Note: Clang's AST refers to other conversions as "casts" too, and calls 2296/// actual casts "explicit" casts.) 2297AST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 2298 internal::Matcher<QualType>, InnerMatcher) { 2299 const QualType NodeType = Node.getTypeAsWritten(); 2300 return InnerMatcher.matches(NodeType, Finder, Builder); 2301} 2302 2303/// \brief Matches implicit casts whose destination type matches a given 2304/// matcher. 2305/// 2306/// FIXME: Unit test this matcher 2307AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 2308 internal::Matcher<QualType>, InnerMatcher) { 2309 return InnerMatcher.matches(Node.getType(), Finder, Builder); 2310} 2311 2312/// \brief Matches the true branch expression of a conditional operator. 2313/// 2314/// Example matches a 2315/// \code 2316/// condition ? a : b 2317/// \endcode 2318AST_MATCHER_P(ConditionalOperator, hasTrueExpression, 2319 internal::Matcher<Expr>, InnerMatcher) { 2320 Expr *Expression = Node.getTrueExpr(); 2321 return (Expression != NULL && 2322 InnerMatcher.matches(*Expression, Finder, Builder)); 2323} 2324 2325/// \brief Matches the false branch expression of a conditional operator. 2326/// 2327/// Example matches b 2328/// \code 2329/// condition ? a : b 2330/// \endcode 2331AST_MATCHER_P(ConditionalOperator, hasFalseExpression, 2332 internal::Matcher<Expr>, InnerMatcher) { 2333 Expr *Expression = Node.getFalseExpr(); 2334 return (Expression != NULL && 2335 InnerMatcher.matches(*Expression, Finder, Builder)); 2336} 2337 2338/// \brief Matches if a declaration has a body attached. 2339/// 2340/// Example matches A, va, fa 2341/// \code 2342/// class A {}; 2343/// class B; // Doesn't match, as it has no body. 2344/// int va; 2345/// extern int vb; // Doesn't match, as it doesn't define the variable. 2346/// void fa() {} 2347/// void fb(); // Doesn't match, as it has no body. 2348/// \endcode 2349/// 2350/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 2351inline internal::PolymorphicMatcherWithParam0<internal::IsDefinitionMatcher> 2352isDefinition() { 2353 return internal::PolymorphicMatcherWithParam0< 2354 internal::IsDefinitionMatcher>(); 2355} 2356 2357/// \brief Matches the class declaration that the given method declaration 2358/// belongs to. 2359/// 2360/// FIXME: Generalize this for other kinds of declarations. 2361/// FIXME: What other kind of declarations would we need to generalize 2362/// this to? 2363/// 2364/// Example matches A() in the last line 2365/// (matcher = constructExpr(hasDeclaration(methodDecl( 2366/// ofClass(hasName("A")))))) 2367/// \code 2368/// class A { 2369/// public: 2370/// A(); 2371/// }; 2372/// A a = A(); 2373/// \endcode 2374AST_MATCHER_P(CXXMethodDecl, ofClass, 2375 internal::Matcher<CXXRecordDecl>, InnerMatcher) { 2376 const CXXRecordDecl *Parent = Node.getParent(); 2377 return (Parent != NULL && 2378 InnerMatcher.matches(*Parent, Finder, Builder)); 2379} 2380 2381/// \brief Matches member expressions that are called with '->' as opposed 2382/// to '.'. 2383/// 2384/// Member calls on the implicit this pointer match as called with '->'. 2385/// 2386/// Given 2387/// \code 2388/// class Y { 2389/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 2390/// int a; 2391/// static int b; 2392/// }; 2393/// \endcode 2394/// memberExpr(isArrow()) 2395/// matches this->x, x, y.x, a, this->b 2396inline internal::Matcher<MemberExpr> isArrow() { 2397 return makeMatcher(new internal::IsArrowMatcher()); 2398} 2399 2400/// \brief Matches QualType nodes that are of integer type. 2401/// 2402/// Given 2403/// \code 2404/// void a(int); 2405/// void b(long); 2406/// void c(double); 2407/// \endcode 2408/// functionDecl(hasAnyParameter(hasType(isInteger()))) 2409/// matches "a(int)", "b(long)", but not "c(double)". 2410AST_MATCHER(QualType, isInteger) { 2411 return Node->isIntegerType(); 2412} 2413 2414/// \brief Matches QualType nodes that are const-qualified, i.e., that 2415/// include "top-level" const. 2416/// 2417/// Given 2418/// \code 2419/// void a(int); 2420/// void b(int const); 2421/// void c(const int); 2422/// void d(const int*); 2423/// void e(int const) {}; 2424/// \endcode 2425/// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 2426/// matches "void b(int const)", "void c(const int)" and 2427/// "void e(int const) {}". It does not match d as there 2428/// is no top-level const on the parameter type "const int *". 2429inline internal::Matcher<QualType> isConstQualified() { 2430 return makeMatcher(new internal::IsConstQualifiedMatcher()); 2431} 2432 2433/// \brief Matches a member expression where the member is matched by a 2434/// given matcher. 2435/// 2436/// Given 2437/// \code 2438/// struct { int first, second; } first, second; 2439/// int i(second.first); 2440/// int j(first.second); 2441/// \endcode 2442/// memberExpr(member(hasName("first"))) 2443/// matches second.first 2444/// but not first.second (because the member name there is "second"). 2445AST_MATCHER_P(MemberExpr, member, 2446 internal::Matcher<ValueDecl>, InnerMatcher) { 2447 return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 2448} 2449 2450/// \brief Matches a member expression where the object expression is 2451/// matched by a given matcher. 2452/// 2453/// Given 2454/// \code 2455/// struct X { int m; }; 2456/// void f(X x) { x.m; m; } 2457/// \endcode 2458/// memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))) 2459/// matches "x.m" and "m" 2460/// with hasObjectExpression(...) 2461/// matching "x" and the implicit object expression of "m" which has type X*. 2462AST_MATCHER_P(MemberExpr, hasObjectExpression, 2463 internal::Matcher<Expr>, InnerMatcher) { 2464 return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 2465} 2466 2467/// \brief Matches any using shadow declaration. 2468/// 2469/// Given 2470/// \code 2471/// namespace X { void b(); } 2472/// using X::b; 2473/// \endcode 2474/// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 2475/// matches \code using X::b \endcode 2476AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 2477 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 2478 for (UsingDecl::shadow_iterator II = Node.shadow_begin(); 2479 II != Node.shadow_end(); ++II) { 2480 if (InnerMatcher.matches(**II, Finder, Builder)) 2481 return true; 2482 } 2483 return false; 2484} 2485 2486/// \brief Matches a using shadow declaration where the target declaration is 2487/// matched by the given matcher. 2488/// 2489/// Given 2490/// \code 2491/// namespace X { int a; void b(); } 2492/// using X::a; 2493/// using X::b; 2494/// \endcode 2495/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 2496/// matches \code using X::b \endcode 2497/// but not \code using X::a \endcode 2498AST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 2499 internal::Matcher<NamedDecl>, InnerMatcher) { 2500 return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 2501} 2502 2503/// \brief Matches template instantiations of function, class, or static 2504/// member variable template instantiations. 2505/// 2506/// Given 2507/// \code 2508/// template <typename T> class X {}; class A {}; X<A> x; 2509/// \endcode 2510/// or 2511/// \code 2512/// template <typename T> class X {}; class A {}; template class X<A>; 2513/// \endcode 2514/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2515/// matches the template instantiation of X<A>. 2516/// 2517/// But given 2518/// \code 2519/// template <typename T> class X {}; class A {}; 2520/// template <> class X<A> {}; X<A> x; 2521/// \endcode 2522/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2523/// does not match, as X<A> is an explicit template specialization. 2524/// 2525/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2526inline internal::PolymorphicMatcherWithParam0< 2527 internal::IsTemplateInstantiationMatcher> 2528isTemplateInstantiation() { 2529 return internal::PolymorphicMatcherWithParam0< 2530 internal::IsTemplateInstantiationMatcher>(); 2531} 2532 2533/// \brief Matches explicit template specializations of function, class, or 2534/// static member variable template instantiations. 2535/// 2536/// Given 2537/// \code 2538/// template<typename T> void A(T t) { } 2539/// template<> void A(int N) { } 2540/// \endcode 2541/// functionDecl(isExplicitTemplateSpecialization()) 2542/// matches the specialization A<int>(). 2543/// 2544/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2545inline internal::PolymorphicMatcherWithParam0< 2546 internal::IsExplicitTemplateSpecializationMatcher> 2547isExplicitTemplateSpecialization() { 2548 return internal::PolymorphicMatcherWithParam0< 2549 internal::IsExplicitTemplateSpecializationMatcher>(); 2550} 2551 2552/// \brief Matches \c TypeLocs for which the given inner 2553/// QualType-matcher matches. 2554inline internal::BindableMatcher<TypeLoc> loc( 2555 const internal::Matcher<QualType> &InnerMatcher) { 2556 return internal::BindableMatcher<TypeLoc>( 2557 new internal::TypeLocTypeMatcher(InnerMatcher)); 2558} 2559 2560/// \brief Matches builtin Types. 2561/// 2562/// Given 2563/// \code 2564/// struct A {}; 2565/// A a; 2566/// int b; 2567/// float c; 2568/// bool d; 2569/// \endcode 2570/// builtinType() 2571/// matches "int b", "float c" and "bool d" 2572AST_TYPE_MATCHER(BuiltinType, builtinType); 2573 2574/// \brief Matches all kinds of arrays. 2575/// 2576/// Given 2577/// \code 2578/// int a[] = { 2, 3 }; 2579/// int b[4]; 2580/// void f() { int c[a[0]]; } 2581/// \endcode 2582/// arrayType() 2583/// matches "int a[]", "int b[4]" and "int c[a[0]]"; 2584AST_TYPE_MATCHER(ArrayType, arrayType); 2585 2586/// \brief Matches C99 complex types. 2587/// 2588/// Given 2589/// \code 2590/// _Complex float f; 2591/// \endcode 2592/// complexType() 2593/// matches "_Complex float f" 2594AST_TYPE_MATCHER(ComplexType, complexType); 2595 2596/// \brief Matches arrays and C99 complex types that have a specific element 2597/// type. 2598/// 2599/// Given 2600/// \code 2601/// struct A {}; 2602/// A a[7]; 2603/// int b[7]; 2604/// \endcode 2605/// arrayType(hasElementType(builtinType())) 2606/// matches "int b[7]" 2607/// 2608/// Usable as: Matcher<ArrayType>, Matcher<ComplexType> 2609AST_TYPELOC_TRAVERSE_MATCHER(hasElementType, getElement); 2610 2611/// \brief Matches C arrays with a specified constant size. 2612/// 2613/// Given 2614/// \code 2615/// void() { 2616/// int a[2]; 2617/// int b[] = { 2, 3 }; 2618/// int c[b[0]]; 2619/// } 2620/// \endcode 2621/// constantArrayType() 2622/// matches "int a[2]" 2623AST_TYPE_MATCHER(ConstantArrayType, constantArrayType); 2624 2625/// \brief Matches \c ConstantArrayType nodes that have the specified size. 2626/// 2627/// Given 2628/// \code 2629/// int a[42]; 2630/// int b[2 * 21]; 2631/// int c[41], d[43]; 2632/// \endcode 2633/// constantArrayType(hasSize(42)) 2634/// matches "int a[42]" and "int b[2 * 21]" 2635AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) { 2636 return Node.getSize() == N; 2637} 2638 2639/// \brief Matches C++ arrays whose size is a value-dependent expression. 2640/// 2641/// Given 2642/// \code 2643/// template<typename T, int Size> 2644/// class array { 2645/// T data[Size]; 2646/// }; 2647/// \endcode 2648/// dependentSizedArrayType 2649/// matches "T data[Size]" 2650AST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType); 2651 2652/// \brief Matches C arrays with unspecified size. 2653/// 2654/// Given 2655/// \code 2656/// int a[] = { 2, 3 }; 2657/// int b[42]; 2658/// void f(int c[]) { int d[a[0]]; }; 2659/// \endcode 2660/// incompleteArrayType() 2661/// matches "int a[]" and "int c[]" 2662AST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType); 2663 2664/// \brief Matches C arrays with a specified size that is not an 2665/// integer-constant-expression. 2666/// 2667/// Given 2668/// \code 2669/// void f() { 2670/// int a[] = { 2, 3 } 2671/// int b[42]; 2672/// int c[a[0]]; 2673/// \endcode 2674/// variableArrayType() 2675/// matches "int c[a[0]]" 2676AST_TYPE_MATCHER(VariableArrayType, variableArrayType); 2677 2678/// \brief Matches \c VariableArrayType nodes that have a specific size 2679/// expression. 2680/// 2681/// Given 2682/// \code 2683/// void f(int b) { 2684/// int a[b]; 2685/// } 2686/// \endcode 2687/// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( 2688/// varDecl(hasName("b"))))))) 2689/// matches "int a[b]" 2690AST_MATCHER_P(VariableArrayType, hasSizeExpr, 2691 internal::Matcher<Expr>, InnerMatcher) { 2692 return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder); 2693} 2694 2695/// \brief Matches atomic types. 2696/// 2697/// Given 2698/// \code 2699/// _Atomic(int) i; 2700/// \endcode 2701/// atomicType() 2702/// matches "_Atomic(int) i" 2703AST_TYPE_MATCHER(AtomicType, atomicType); 2704 2705/// \brief Matches atomic types with a specific value type. 2706/// 2707/// Given 2708/// \code 2709/// _Atomic(int) i; 2710/// _Atomic(float) f; 2711/// \endcode 2712/// atomicType(hasValueType(isInteger())) 2713/// matches "_Atomic(int) i" 2714/// 2715/// Usable as: Matcher<AtomicType> 2716AST_TYPELOC_TRAVERSE_MATCHER(hasValueType, getValue); 2717 2718/// \brief Matches types nodes representing C++11 auto types. 2719/// 2720/// Given: 2721/// \code 2722/// auto n = 4; 2723/// int v[] = { 2, 3 } 2724/// for (auto i : v) { } 2725/// \endcode 2726/// autoType() 2727/// matches "auto n" and "auto i" 2728AST_TYPE_MATCHER(AutoType, autoType); 2729 2730/// \brief Matches \c AutoType nodes where the deduced type is a specific type. 2731/// 2732/// Note: There is no \c TypeLoc for the deduced type and thus no 2733/// \c getDeducedLoc() matcher. 2734/// 2735/// Given 2736/// \code 2737/// auto a = 1; 2738/// auto b = 2.0; 2739/// \endcode 2740/// autoType(hasDeducedType(isInteger())) 2741/// matches "auto a" 2742/// 2743/// Usable as: Matcher<AutoType> 2744AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType); 2745 2746/// \brief Matches \c FunctionType nodes. 2747/// 2748/// Given 2749/// \code 2750/// int (*f)(int); 2751/// void g(); 2752/// \endcode 2753/// functionType() 2754/// matches "int (*f)(int)" and the type of "g". 2755AST_TYPE_MATCHER(FunctionType, functionType); 2756 2757/// \brief Matches block pointer types, i.e. types syntactically represented as 2758/// "void (^)(int)". 2759/// 2760/// The \c pointee is always required to be a \c FunctionType. 2761AST_TYPE_MATCHER(BlockPointerType, blockPointerType); 2762 2763/// \brief Matches member pointer types. 2764/// Given 2765/// \code 2766/// struct A { int i; } 2767/// A::* ptr = A::i; 2768/// \endcode 2769/// memberPointerType() 2770/// matches "A::* ptr" 2771AST_TYPE_MATCHER(MemberPointerType, memberPointerType); 2772 2773/// \brief Matches pointer types. 2774/// 2775/// Given 2776/// \code 2777/// int *a; 2778/// int &b = *a; 2779/// int c = 5; 2780/// \endcode 2781/// pointerType() 2782/// matches "int *a" 2783AST_TYPE_MATCHER(PointerType, pointerType); 2784 2785/// \brief Matches reference types. 2786/// 2787/// Given 2788/// \code 2789/// int *a; 2790/// int &b = *a; 2791/// int c = 5; 2792/// \endcode 2793/// pointerType() 2794/// matches "int &b" 2795AST_TYPE_MATCHER(ReferenceType, referenceType); 2796 2797/// \brief Narrows PointerType (and similar) matchers to those where the 2798/// \c pointee matches a given matcher. 2799/// 2800/// Given 2801/// \code 2802/// int *a; 2803/// int const *b; 2804/// float const *f; 2805/// \endcode 2806/// pointerType(pointee(isConstQualified(), isInteger())) 2807/// matches "int const *b" 2808/// 2809/// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>, 2810/// Matcher<PointerType>, Matcher<ReferenceType> 2811AST_TYPELOC_TRAVERSE_MATCHER(pointee, getPointee); 2812 2813/// \brief Matches typedef types. 2814/// 2815/// Given 2816/// \code 2817/// typedef int X; 2818/// \endcode 2819/// typedefType() 2820/// matches "typedef int X" 2821AST_TYPE_MATCHER(TypedefType, typedefType); 2822 2823/// \brief Matches \c TypedefTypes referring to a specific 2824/// \c TypedefNameDecl. 2825AST_MATCHER_P(TypedefType, hasDecl, 2826 internal::Matcher<TypedefNameDecl>, InnerMatcher) { 2827 return InnerMatcher.matches(*Node.getDecl(), Finder, Builder); 2828} 2829 2830/// \brief Matches nested name specifiers. 2831/// 2832/// Given 2833/// \code 2834/// namespace ns { 2835/// struct A { static void f(); }; 2836/// void A::f() {} 2837/// void g() { A::f(); } 2838/// } 2839/// ns::A a; 2840/// \endcode 2841/// nestedNameSpecifier() 2842/// matches "ns::" and both "A::" 2843const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 2844 2845/// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 2846const internal::VariadicAllOfMatcher< 2847 NestedNameSpecifierLoc> nestedNameSpecifierLoc; 2848 2849/// \brief Matches \c NestedNameSpecifierLocs for which the given inner 2850/// NestedNameSpecifier-matcher matches. 2851inline internal::BindableMatcher<NestedNameSpecifierLoc> loc( 2852 const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 2853 return internal::BindableMatcher<NestedNameSpecifierLoc>( 2854 new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 2855 InnerMatcher)); 2856} 2857 2858/// \brief Matches nested name specifiers that specify a type matching the 2859/// given \c QualType matcher without qualifiers. 2860/// 2861/// Given 2862/// \code 2863/// struct A { struct B { struct C {}; }; }; 2864/// A::B::C c; 2865/// \endcode 2866/// nestedNameSpecifier(specifiesType(hasDeclaration(recordDecl(hasName("A"))))) 2867/// matches "A::" 2868AST_MATCHER_P(NestedNameSpecifier, specifiesType, 2869 internal::Matcher<QualType>, InnerMatcher) { 2870 if (Node.getAsType() == NULL) 2871 return false; 2872 return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 2873} 2874 2875/// \brief Matches nested name specifier locs that specify a type matching the 2876/// given \c TypeLoc. 2877/// 2878/// Given 2879/// \code 2880/// struct A { struct B { struct C {}; }; }; 2881/// A::B::C c; 2882/// \endcode 2883/// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type( 2884/// hasDeclaration(recordDecl(hasName("A"))))))) 2885/// matches "A::" 2886AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, 2887 internal::Matcher<TypeLoc>, InnerMatcher) { 2888 return InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder); 2889} 2890 2891/// \brief Matches on the prefix of a \c NestedNameSpecifier. 2892/// 2893/// Given 2894/// \code 2895/// struct A { struct B { struct C {}; }; }; 2896/// A::B::C c; 2897/// \endcode 2898/// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 2899/// matches "A::" 2900inline internal::Matcher<NestedNameSpecifier> hasPrefix( 2901 const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 2902 return internal::makeMatcher( 2903 new internal::NestedNameSpecifierPrefixMatcher(InnerMatcher)); 2904} 2905 2906/// \brief Matches on the prefix of a \c NestedNameSpecifierLoc. 2907/// 2908/// Given 2909/// \code 2910/// struct A { struct B { struct C {}; }; }; 2911/// A::B::C c; 2912/// \endcode 2913/// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 2914/// matches "A::" 2915inline internal::Matcher<NestedNameSpecifierLoc> hasPrefix( 2916 const internal::Matcher<NestedNameSpecifierLoc> &InnerMatcher) { 2917 return internal::makeMatcher( 2918 new internal::NestedNameSpecifierLocPrefixMatcher(InnerMatcher)); 2919} 2920 2921/// \brief Matches nested name specifiers that specify a namespace matching the 2922/// given namespace matcher. 2923/// 2924/// Given 2925/// \code 2926/// namespace ns { struct A {}; } 2927/// ns::A a; 2928/// \endcode 2929/// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 2930/// matches "ns::" 2931AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 2932 internal::Matcher<NamespaceDecl>, InnerMatcher) { 2933 if (Node.getAsNamespace() == NULL) 2934 return false; 2935 return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 2936} 2937 2938} // end namespace ast_matchers 2939} // end namespace clang 2940 2941#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 2942