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