ASTMatchers.h revision c711af2ddbbb1d46223cc9379d32a203fb5bc7f4
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>, 1474/// Matcher<MemberExpr> 1475inline internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, 1476 internal::Matcher<Decl> > 1477 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 1478 return internal::PolymorphicMatcherWithParam1< 1479 internal::HasDeclarationMatcher, 1480 internal::Matcher<Decl> >(InnerMatcher); 1481} 1482 1483/// \brief Matches on the implicit object argument of a member call expression. 1484/// 1485/// Example matches y.x() (matcher = callExpr(on(hasType(recordDecl(hasName("Y")))))) 1486/// \code 1487/// class Y { public: void x(); }; 1488/// void z() { Y y; y.x(); }", 1489/// \endcode 1490/// 1491/// FIXME: Overload to allow directly matching types? 1492AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 1493 InnerMatcher) { 1494 const Expr *ExprNode = const_cast<CXXMemberCallExpr&>(Node) 1495 .getImplicitObjectArgument() 1496 ->IgnoreParenImpCasts(); 1497 return (ExprNode != NULL && 1498 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1499} 1500 1501/// \brief Matches if the call expression's callee expression matches. 1502/// 1503/// Given 1504/// \code 1505/// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 1506/// void f() { f(); } 1507/// \endcode 1508/// callExpr(callee(expr())) 1509/// matches this->x(), x(), y.x(), f() 1510/// with callee(...) 1511/// matching this->x, x, y.x, f respectively 1512/// 1513/// Note: Callee cannot take the more general internal::Matcher<Expr> 1514/// because this introduces ambiguous overloads with calls to Callee taking a 1515/// internal::Matcher<Decl>, as the matcher hierarchy is purely 1516/// implemented in terms of implicit casts. 1517AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 1518 InnerMatcher) { 1519 const Expr *ExprNode = Node.getCallee(); 1520 return (ExprNode != NULL && 1521 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1522} 1523 1524/// \brief Matches if the call expression's callee's declaration matches the 1525/// given matcher. 1526/// 1527/// Example matches y.x() (matcher = callExpr(callee(methodDecl(hasName("x"))))) 1528/// \code 1529/// class Y { public: void x(); }; 1530/// void z() { Y y; y.x(); 1531/// \endcode 1532inline internal::Matcher<CallExpr> callee( 1533 const internal::Matcher<Decl> &InnerMatcher) { 1534 return internal::Matcher<CallExpr>(hasDeclaration(InnerMatcher)); 1535} 1536 1537/// \brief Matches if the expression's or declaration's type matches a type 1538/// matcher. 1539/// 1540/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1541/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1542/// \code 1543/// class X {}; 1544/// void y(X &x) { x; X z; } 1545/// \endcode 1546AST_POLYMORPHIC_MATCHER_P(hasType, internal::Matcher<QualType>, 1547 InnerMatcher) { 1548 TOOLING_COMPILE_ASSERT((llvm::is_base_of<Expr, NodeType>::value || 1549 llvm::is_base_of<ValueDecl, NodeType>::value), 1550 instantiated_with_wrong_types); 1551 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1552} 1553 1554/// \brief Overloaded to match the declaration of the expression's or value 1555/// declaration's type. 1556/// 1557/// In case of a value declaration (for example a variable declaration), 1558/// this resolves one layer of indirection. For example, in the value 1559/// declaration "X x;", recordDecl(hasName("X")) matches the declaration of X, 1560/// while varDecl(hasType(recordDecl(hasName("X")))) matches the declaration 1561/// of x." 1562/// 1563/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1564/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1565/// \code 1566/// class X {}; 1567/// void y(X &x) { x; X z; } 1568/// \endcode 1569/// 1570/// Usable as: Matcher<Expr>, Matcher<ValueDecl> 1571inline internal::PolymorphicMatcherWithParam1< 1572 internal::matcher_hasTypeMatcher, 1573 internal::Matcher<QualType> > 1574hasType(const internal::Matcher<Decl> &InnerMatcher) { 1575 return hasType(internal::Matcher<QualType>( 1576 hasDeclaration(InnerMatcher))); 1577} 1578 1579/// \brief Matches if the matched type is represented by the given string. 1580/// 1581/// Given 1582/// \code 1583/// class Y { public: void x(); }; 1584/// void z() { Y* y; y->x(); } 1585/// \endcode 1586/// callExpr(on(hasType(asString("class Y *")))) 1587/// matches y->x() 1588AST_MATCHER_P(QualType, asString, std::string, Name) { 1589 return Name == Node.getAsString(); 1590} 1591 1592/// \brief Matches if the matched type is a pointer type and the pointee type 1593/// matches the specified matcher. 1594/// 1595/// Example matches y->x() 1596/// (matcher = callExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))))) 1597/// \code 1598/// class Y { public: void x(); }; 1599/// void z() { Y *y; y->x(); } 1600/// \endcode 1601AST_MATCHER_P( 1602 QualType, pointsTo, internal::Matcher<QualType>, 1603 InnerMatcher) { 1604 return (!Node.isNull() && Node->isPointerType() && 1605 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1606} 1607 1608/// \brief Overloaded to match the pointee type's declaration. 1609inline internal::Matcher<QualType> pointsTo( 1610 const internal::Matcher<Decl> &InnerMatcher) { 1611 return pointsTo(internal::Matcher<QualType>( 1612 hasDeclaration(InnerMatcher))); 1613} 1614 1615/// \brief Matches if the matched type is a reference type and the referenced 1616/// type matches the specified matcher. 1617/// 1618/// Example matches X &x and const X &y 1619/// (matcher = varDecl(hasType(references(recordDecl(hasName("X")))))) 1620/// \code 1621/// class X { 1622/// void a(X b) { 1623/// X &x = b; 1624/// const X &y = b; 1625/// }; 1626/// \endcode 1627AST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 1628 InnerMatcher) { 1629 return (!Node.isNull() && Node->isReferenceType() && 1630 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1631} 1632 1633/// \brief Overloaded to match the referenced type's declaration. 1634inline internal::Matcher<QualType> references( 1635 const internal::Matcher<Decl> &InnerMatcher) { 1636 return references(internal::Matcher<QualType>( 1637 hasDeclaration(InnerMatcher))); 1638} 1639 1640AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 1641 internal::Matcher<Expr>, InnerMatcher) { 1642 const Expr *ExprNode = 1643 const_cast<CXXMemberCallExpr&>(Node).getImplicitObjectArgument(); 1644 return (ExprNode != NULL && 1645 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1646} 1647 1648/// \brief Matches if the expression's type either matches the specified 1649/// matcher, or is a pointer to a type that matches the InnerMatcher. 1650inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1651 const internal::Matcher<QualType> &InnerMatcher) { 1652 return onImplicitObjectArgument( 1653 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1654} 1655 1656/// \brief Overloaded to match the type's declaration. 1657inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1658 const internal::Matcher<Decl> &InnerMatcher) { 1659 return onImplicitObjectArgument( 1660 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1661} 1662 1663/// \brief Matches a DeclRefExpr that refers to a declaration that matches the 1664/// specified matcher. 1665/// 1666/// Example matches x in if(x) 1667/// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 1668/// \code 1669/// bool x; 1670/// if (x) {} 1671/// \endcode 1672AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 1673 InnerMatcher) { 1674 const Decl *DeclNode = Node.getDecl(); 1675 return (DeclNode != NULL && 1676 InnerMatcher.matches(*DeclNode, Finder, Builder)); 1677} 1678 1679/// \brief Matches a \c DeclRefExpr that refers to a declaration through a 1680/// specific using shadow declaration. 1681/// 1682/// FIXME: This currently only works for functions. Fix. 1683/// 1684/// Given 1685/// \code 1686/// namespace a { void f() {} } 1687/// using a::f; 1688/// void g() { 1689/// f(); // Matches this .. 1690/// a::f(); // .. but not this. 1691/// } 1692/// \endcode 1693/// declRefExpr(throughUsingDeclaration(anything())) 1694/// matches \c f() 1695AST_MATCHER_P(DeclRefExpr, throughUsingDecl, 1696 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 1697 const NamedDecl *FoundDecl = Node.getFoundDecl(); 1698 if (const UsingShadowDecl *UsingDecl = 1699 llvm::dyn_cast<UsingShadowDecl>(FoundDecl)) 1700 return InnerMatcher.matches(*UsingDecl, Finder, Builder); 1701 return false; 1702} 1703 1704/// \brief Matches the Decl of a DeclStmt which has a single declaration. 1705/// 1706/// Given 1707/// \code 1708/// int a, b; 1709/// int c; 1710/// \endcode 1711/// declStmt(hasSingleDecl(anything())) 1712/// matches 'int c;' but not 'int a, b;'. 1713AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 1714 if (Node.isSingleDecl()) { 1715 const Decl *FoundDecl = Node.getSingleDecl(); 1716 return InnerMatcher.matches(*FoundDecl, Finder, Builder); 1717 } 1718 return false; 1719} 1720 1721/// \brief Matches a variable declaration that has an initializer expression 1722/// that matches the given matcher. 1723/// 1724/// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 1725/// \code 1726/// bool y() { return true; } 1727/// bool x = y(); 1728/// \endcode 1729AST_MATCHER_P( 1730 VarDecl, hasInitializer, internal::Matcher<Expr>, 1731 InnerMatcher) { 1732 const Expr *Initializer = Node.getAnyInitializer(); 1733 return (Initializer != NULL && 1734 InnerMatcher.matches(*Initializer, Finder, Builder)); 1735} 1736 1737/// \brief Checks that a call expression or a constructor call expression has 1738/// a specific number of arguments (including absent default arguments). 1739/// 1740/// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 1741/// \code 1742/// void f(int x, int y); 1743/// f(0, 0); 1744/// \endcode 1745AST_POLYMORPHIC_MATCHER_P(argumentCountIs, unsigned, N) { 1746 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1747 llvm::is_base_of<CXXConstructExpr, 1748 NodeType>::value), 1749 instantiated_with_wrong_types); 1750 return Node.getNumArgs() == N; 1751} 1752 1753/// \brief Matches the n'th argument of a call expression or a constructor 1754/// call expression. 1755/// 1756/// Example matches y in x(y) 1757/// (matcher = callExpr(hasArgument(0, declRefExpr()))) 1758/// \code 1759/// void x(int) { int y; x(y); } 1760/// \endcode 1761AST_POLYMORPHIC_MATCHER_P2( 1762 hasArgument, unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 1763 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1764 llvm::is_base_of<CXXConstructExpr, 1765 NodeType>::value), 1766 instantiated_with_wrong_types); 1767 return (N < Node.getNumArgs() && 1768 InnerMatcher.matches( 1769 *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 1770} 1771 1772/// \brief Matches declaration statements that contain a specific number of 1773/// declarations. 1774/// 1775/// Example: Given 1776/// \code 1777/// int a, b; 1778/// int c; 1779/// int d = 2, e; 1780/// \endcode 1781/// declCountIs(2) 1782/// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 1783AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 1784 return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 1785} 1786 1787/// \brief Matches the n'th declaration of a declaration statement. 1788/// 1789/// Note that this does not work for global declarations because the AST 1790/// breaks up multiple-declaration DeclStmt's into multiple single-declaration 1791/// DeclStmt's. 1792/// Example: Given non-global declarations 1793/// \code 1794/// int a, b = 0; 1795/// int c; 1796/// int d = 2, e; 1797/// \endcode 1798/// declStmt(containsDeclaration( 1799/// 0, varDecl(hasInitializer(anything())))) 1800/// matches only 'int d = 2, e;', and 1801/// declStmt(containsDeclaration(1, varDecl())) 1802/// \code 1803/// matches 'int a, b = 0' as well as 'int d = 2, e;' 1804/// but 'int c;' is not matched. 1805/// \endcode 1806AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 1807 internal::Matcher<Decl>, InnerMatcher) { 1808 const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 1809 if (N >= NumDecls) 1810 return false; 1811 DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 1812 std::advance(Iterator, N); 1813 return InnerMatcher.matches(**Iterator, Finder, Builder); 1814} 1815 1816/// \brief Matches a constructor initializer. 1817/// 1818/// Given 1819/// \code 1820/// struct Foo { 1821/// Foo() : foo_(1) { } 1822/// int foo_; 1823/// }; 1824/// \endcode 1825/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer(anything())))) 1826/// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 1827AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 1828 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 1829 for (CXXConstructorDecl::init_const_iterator I = Node.init_begin(); 1830 I != Node.init_end(); ++I) { 1831 if (InnerMatcher.matches(**I, Finder, Builder)) { 1832 return true; 1833 } 1834 } 1835 return false; 1836} 1837 1838/// \brief Matches the field declaration of a constructor initializer. 1839/// 1840/// Given 1841/// \code 1842/// struct Foo { 1843/// Foo() : foo_(1) { } 1844/// int foo_; 1845/// }; 1846/// \endcode 1847/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 1848/// forField(hasName("foo_")))))) 1849/// matches Foo 1850/// with forField matching foo_ 1851AST_MATCHER_P(CXXCtorInitializer, forField, 1852 internal::Matcher<FieldDecl>, InnerMatcher) { 1853 const FieldDecl *NodeAsDecl = Node.getMember(); 1854 return (NodeAsDecl != NULL && 1855 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 1856} 1857 1858/// \brief Matches the initializer expression of a constructor initializer. 1859/// 1860/// Given 1861/// \code 1862/// struct Foo { 1863/// Foo() : foo_(1) { } 1864/// int foo_; 1865/// }; 1866/// \endcode 1867/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 1868/// withInitializer(integerLiteral(equals(1))))))) 1869/// matches Foo 1870/// with withInitializer matching (1) 1871AST_MATCHER_P(CXXCtorInitializer, withInitializer, 1872 internal::Matcher<Expr>, InnerMatcher) { 1873 const Expr* NodeAsExpr = Node.getInit(); 1874 return (NodeAsExpr != NULL && 1875 InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 1876} 1877 1878/// \brief Matches a contructor initializer if it is explicitly written in 1879/// code (as opposed to implicitly added by the compiler). 1880/// 1881/// Given 1882/// \code 1883/// struct Foo { 1884/// Foo() { } 1885/// Foo(int) : foo_("A") { } 1886/// string foo_; 1887/// }; 1888/// \endcode 1889/// constructorDecl(hasAnyConstructorInitializer(isWritten())) 1890/// will match Foo(int), but not Foo() 1891AST_MATCHER(CXXCtorInitializer, isWritten) { 1892 return Node.isWritten(); 1893} 1894 1895/// \brief Matches a constructor declaration that has been implicitly added 1896/// by the compiler (eg. implicit default/copy constructors). 1897AST_MATCHER(CXXConstructorDecl, isImplicit) { 1898 return Node.isImplicit(); 1899} 1900 1901/// \brief Matches any argument of a call expression or a constructor call 1902/// expression. 1903/// 1904/// Given 1905/// \code 1906/// void x(int, int, int) { int y; x(1, y, 42); } 1907/// \endcode 1908/// callExpr(hasAnyArgument(declRefExpr())) 1909/// matches x(1, y, 42) 1910/// with hasAnyArgument(...) 1911/// matching y 1912AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, internal::Matcher<Expr>, 1913 InnerMatcher) { 1914 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1915 llvm::is_base_of<CXXConstructExpr, 1916 NodeType>::value), 1917 instantiated_with_wrong_types); 1918 for (unsigned I = 0; I < Node.getNumArgs(); ++I) { 1919 if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(), 1920 Finder, Builder)) { 1921 return true; 1922 } 1923 } 1924 return false; 1925} 1926 1927/// \brief Matches the n'th parameter of a function declaration. 1928/// 1929/// Given 1930/// \code 1931/// class X { void f(int x) {} }; 1932/// \endcode 1933/// methodDecl(hasParameter(0, hasType(varDecl()))) 1934/// matches f(int x) {} 1935/// with hasParameter(...) 1936/// matching int x 1937AST_MATCHER_P2(FunctionDecl, hasParameter, 1938 unsigned, N, internal::Matcher<ParmVarDecl>, 1939 InnerMatcher) { 1940 return (N < Node.getNumParams() && 1941 InnerMatcher.matches( 1942 *Node.getParamDecl(N), Finder, Builder)); 1943} 1944 1945/// \brief Matches any parameter of a function declaration. 1946/// 1947/// Does not match the 'this' parameter of a method. 1948/// 1949/// Given 1950/// \code 1951/// class X { void f(int x, int y, int z) {} }; 1952/// \endcode 1953/// methodDecl(hasAnyParameter(hasName("y"))) 1954/// matches f(int x, int y, int z) {} 1955/// with hasAnyParameter(...) 1956/// matching int y 1957AST_MATCHER_P(FunctionDecl, hasAnyParameter, 1958 internal::Matcher<ParmVarDecl>, InnerMatcher) { 1959 for (unsigned I = 0; I < Node.getNumParams(); ++I) { 1960 if (InnerMatcher.matches(*Node.getParamDecl(I), Finder, Builder)) { 1961 return true; 1962 } 1963 } 1964 return false; 1965} 1966 1967/// \brief Matches the return type of a function declaration. 1968/// 1969/// Given: 1970/// \code 1971/// class X { int f() { return 1; } }; 1972/// \endcode 1973/// methodDecl(returns(asString("int"))) 1974/// matches int f() { return 1; } 1975AST_MATCHER_P(FunctionDecl, returns, 1976 internal::Matcher<QualType>, InnerMatcher) { 1977 return InnerMatcher.matches(Node.getResultType(), Finder, Builder); 1978} 1979 1980/// \brief Matches extern "C" function declarations. 1981/// 1982/// Given: 1983/// \code 1984/// extern "C" void f() {} 1985/// extern "C" { void g() {} } 1986/// void h() {} 1987/// \endcode 1988/// functionDecl(isExternC()) 1989/// matches the declaration of f and g, but not the declaration h 1990AST_MATCHER(FunctionDecl, isExternC) { 1991 return Node.isExternC(); 1992} 1993 1994/// \brief Matches the condition expression of an if statement, for loop, 1995/// or conditional operator. 1996/// 1997/// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 1998/// \code 1999/// if (true) {} 2000/// \endcode 2001AST_POLYMORPHIC_MATCHER_P(hasCondition, internal::Matcher<Expr>, 2002 InnerMatcher) { 2003 TOOLING_COMPILE_ASSERT( 2004 (llvm::is_base_of<IfStmt, NodeType>::value) || 2005 (llvm::is_base_of<ForStmt, NodeType>::value) || 2006 (llvm::is_base_of<WhileStmt, NodeType>::value) || 2007 (llvm::is_base_of<DoStmt, NodeType>::value) || 2008 (llvm::is_base_of<ConditionalOperator, NodeType>::value), 2009 has_condition_requires_if_statement_conditional_operator_or_loop); 2010 const Expr *const Condition = Node.getCond(); 2011 return (Condition != NULL && 2012 InnerMatcher.matches(*Condition, Finder, Builder)); 2013} 2014 2015/// \brief Matches the condition variable statement in an if statement. 2016/// 2017/// Given 2018/// \code 2019/// if (A* a = GetAPointer()) {} 2020/// \endcode 2021/// hasConditionVariableStatment(...) 2022/// matches 'A* a = GetAPointer()'. 2023AST_MATCHER_P(IfStmt, hasConditionVariableStatement, 2024 internal::Matcher<DeclStmt>, InnerMatcher) { 2025 const DeclStmt* const DeclarationStatement = 2026 Node.getConditionVariableDeclStmt(); 2027 return DeclarationStatement != NULL && 2028 InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 2029} 2030 2031/// \brief Matches the index expression of an array subscript expression. 2032/// 2033/// Given 2034/// \code 2035/// int i[5]; 2036/// void f() { i[1] = 42; } 2037/// \endcode 2038/// arraySubscriptExpression(hasIndex(integerLiteral())) 2039/// matches \c i[1] with the \c integerLiteral() matching \c 1 2040AST_MATCHER_P(ArraySubscriptExpr, hasIndex, 2041 internal::Matcher<Expr>, InnerMatcher) { 2042 if (const Expr* Expression = Node.getIdx()) 2043 return InnerMatcher.matches(*Expression, Finder, Builder); 2044 return false; 2045} 2046 2047/// \brief Matches the base expression of an array subscript expression. 2048/// 2049/// Given 2050/// \code 2051/// int i[5]; 2052/// void f() { i[1] = 42; } 2053/// \endcode 2054/// arraySubscriptExpression(hasBase(implicitCastExpr( 2055/// hasSourceExpression(declRefExpr())))) 2056/// matches \c i[1] with the \c declRefExpr() matching \c i 2057AST_MATCHER_P(ArraySubscriptExpr, hasBase, 2058 internal::Matcher<Expr>, InnerMatcher) { 2059 if (const Expr* Expression = Node.getBase()) 2060 return InnerMatcher.matches(*Expression, Finder, Builder); 2061 return false; 2062} 2063 2064/// \brief Matches a 'for', 'while', or 'do while' statement that has 2065/// a given body. 2066/// 2067/// Given 2068/// \code 2069/// for (;;) {} 2070/// \endcode 2071/// hasBody(compoundStmt()) 2072/// matches 'for (;;) {}' 2073/// with compoundStmt() 2074/// matching '{}' 2075AST_POLYMORPHIC_MATCHER_P(hasBody, internal::Matcher<Stmt>, 2076 InnerMatcher) { 2077 TOOLING_COMPILE_ASSERT( 2078 (llvm::is_base_of<DoStmt, NodeType>::value) || 2079 (llvm::is_base_of<ForStmt, NodeType>::value) || 2080 (llvm::is_base_of<WhileStmt, NodeType>::value), 2081 has_body_requires_for_while_or_do_statement); 2082 const Stmt *const Statement = Node.getBody(); 2083 return (Statement != NULL && 2084 InnerMatcher.matches(*Statement, Finder, Builder)); 2085} 2086 2087/// \brief Matches compound statements where at least one substatement matches 2088/// a given matcher. 2089/// 2090/// Given 2091/// \code 2092/// { {}; 1+2; } 2093/// \endcode 2094/// hasAnySubstatement(compoundStmt()) 2095/// matches '{ {}; 1+2; }' 2096/// with compoundStmt() 2097/// matching '{}' 2098AST_MATCHER_P(CompoundStmt, hasAnySubstatement, 2099 internal::Matcher<Stmt>, InnerMatcher) { 2100 for (CompoundStmt::const_body_iterator It = Node.body_begin(); 2101 It != Node.body_end(); 2102 ++It) { 2103 if (InnerMatcher.matches(**It, Finder, Builder)) return true; 2104 } 2105 return false; 2106} 2107 2108/// \brief Checks that a compound statement contains a specific number of 2109/// child statements. 2110/// 2111/// Example: Given 2112/// \code 2113/// { for (;;) {} } 2114/// \endcode 2115/// compoundStmt(statementCountIs(0))) 2116/// matches '{}' 2117/// but does not match the outer compound statement. 2118AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 2119 return Node.size() == N; 2120} 2121 2122/// \brief Matches literals that are equal to the given value. 2123/// 2124/// Example matches true (matcher = boolLiteral(equals(true))) 2125/// \code 2126/// true 2127/// \endcode 2128/// 2129/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 2130/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 2131template <typename ValueT> 2132internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 2133equals(const ValueT &Value) { 2134 return internal::PolymorphicMatcherWithParam1< 2135 internal::ValueEqualsMatcher, 2136 ValueT>(Value); 2137} 2138 2139/// \brief Matches the operator Name of operator expressions (binary or 2140/// unary). 2141/// 2142/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 2143/// \code 2144/// !(a || b) 2145/// \endcode 2146AST_POLYMORPHIC_MATCHER_P(hasOperatorName, std::string, Name) { 2147 TOOLING_COMPILE_ASSERT( 2148 (llvm::is_base_of<BinaryOperator, NodeType>::value) || 2149 (llvm::is_base_of<UnaryOperator, NodeType>::value), 2150 has_condition_requires_if_statement_or_conditional_operator); 2151 return Name == Node.getOpcodeStr(Node.getOpcode()); 2152} 2153 2154/// \brief Matches the left hand side of binary operator expressions. 2155/// 2156/// Example matches a (matcher = binaryOperator(hasLHS())) 2157/// \code 2158/// a || b 2159/// \endcode 2160AST_MATCHER_P(BinaryOperator, hasLHS, 2161 internal::Matcher<Expr>, InnerMatcher) { 2162 Expr *LeftHandSide = Node.getLHS(); 2163 return (LeftHandSide != NULL && 2164 InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 2165} 2166 2167/// \brief Matches the right hand side of binary operator expressions. 2168/// 2169/// Example matches b (matcher = binaryOperator(hasRHS())) 2170/// \code 2171/// a || b 2172/// \endcode 2173AST_MATCHER_P(BinaryOperator, hasRHS, 2174 internal::Matcher<Expr>, InnerMatcher) { 2175 Expr *RightHandSide = Node.getRHS(); 2176 return (RightHandSide != NULL && 2177 InnerMatcher.matches(*RightHandSide, Finder, Builder)); 2178} 2179 2180/// \brief Matches if either the left hand side or the right hand side of a 2181/// binary operator matches. 2182inline internal::Matcher<BinaryOperator> hasEitherOperand( 2183 const internal::Matcher<Expr> &InnerMatcher) { 2184 return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 2185} 2186 2187/// \brief Matches if the operand of a unary operator matches. 2188/// 2189/// Example matches true (matcher = hasUnaryOperand(boolLiteral(equals(true)))) 2190/// \code 2191/// !true 2192/// \endcode 2193AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 2194 internal::Matcher<Expr>, InnerMatcher) { 2195 const Expr * const Operand = Node.getSubExpr(); 2196 return (Operand != NULL && 2197 InnerMatcher.matches(*Operand, Finder, Builder)); 2198} 2199 2200/// \brief Matches if the cast's source expression matches the given matcher. 2201/// 2202/// Example: matches "a string" (matcher = 2203/// hasSourceExpression(constructExpr())) 2204/// \code 2205/// class URL { URL(string); }; 2206/// URL url = "a string"; 2207AST_MATCHER_P(CastExpr, hasSourceExpression, 2208 internal::Matcher<Expr>, InnerMatcher) { 2209 const Expr* const SubExpression = Node.getSubExpr(); 2210 return (SubExpression != NULL && 2211 InnerMatcher.matches(*SubExpression, Finder, Builder)); 2212} 2213 2214/// \brief Matches casts whose destination type matches a given matcher. 2215/// 2216/// (Note: Clang's AST refers to other conversions as "casts" too, and calls 2217/// actual casts "explicit" casts.) 2218AST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 2219 internal::Matcher<QualType>, InnerMatcher) { 2220 const QualType NodeType = Node.getTypeAsWritten(); 2221 return InnerMatcher.matches(NodeType, Finder, Builder); 2222} 2223 2224/// \brief Matches implicit casts whose destination type matches a given 2225/// matcher. 2226/// 2227/// FIXME: Unit test this matcher 2228AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 2229 internal::Matcher<QualType>, InnerMatcher) { 2230 return InnerMatcher.matches(Node.getType(), Finder, Builder); 2231} 2232 2233/// \brief Matches the true branch expression of a conditional operator. 2234/// 2235/// Example matches a 2236/// \code 2237/// condition ? a : b 2238/// \endcode 2239AST_MATCHER_P(ConditionalOperator, hasTrueExpression, 2240 internal::Matcher<Expr>, InnerMatcher) { 2241 Expr *Expression = Node.getTrueExpr(); 2242 return (Expression != NULL && 2243 InnerMatcher.matches(*Expression, Finder, Builder)); 2244} 2245 2246/// \brief Matches the false branch expression of a conditional operator. 2247/// 2248/// Example matches b 2249/// \code 2250/// condition ? a : b 2251/// \endcode 2252AST_MATCHER_P(ConditionalOperator, hasFalseExpression, 2253 internal::Matcher<Expr>, InnerMatcher) { 2254 Expr *Expression = Node.getFalseExpr(); 2255 return (Expression != NULL && 2256 InnerMatcher.matches(*Expression, Finder, Builder)); 2257} 2258 2259/// \brief Matches if a declaration has a body attached. 2260/// 2261/// Example matches A, va, fa 2262/// \code 2263/// class A {}; 2264/// class B; // Doesn't match, as it has no body. 2265/// int va; 2266/// extern int vb; // Doesn't match, as it doesn't define the variable. 2267/// void fa() {} 2268/// void fb(); // Doesn't match, as it has no body. 2269/// \endcode 2270/// 2271/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 2272inline internal::PolymorphicMatcherWithParam0<internal::IsDefinitionMatcher> 2273isDefinition() { 2274 return internal::PolymorphicMatcherWithParam0< 2275 internal::IsDefinitionMatcher>(); 2276} 2277 2278/// \brief Matches the class declaration that the given method declaration 2279/// belongs to. 2280/// 2281/// FIXME: Generalize this for other kinds of declarations. 2282/// FIXME: What other kind of declarations would we need to generalize 2283/// this to? 2284/// 2285/// Example matches A() in the last line 2286/// (matcher = constructExpr(hasDeclaration(methodDecl( 2287/// ofClass(hasName("A")))))) 2288/// \code 2289/// class A { 2290/// public: 2291/// A(); 2292/// }; 2293/// A a = A(); 2294/// \endcode 2295AST_MATCHER_P(CXXMethodDecl, ofClass, 2296 internal::Matcher<CXXRecordDecl>, InnerMatcher) { 2297 const CXXRecordDecl *Parent = Node.getParent(); 2298 return (Parent != NULL && 2299 InnerMatcher.matches(*Parent, Finder, Builder)); 2300} 2301 2302/// \brief Matches member expressions that are called with '->' as opposed 2303/// to '.'. 2304/// 2305/// Member calls on the implicit this pointer match as called with '->'. 2306/// 2307/// Given 2308/// \code 2309/// class Y { 2310/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 2311/// int a; 2312/// static int b; 2313/// }; 2314/// \endcode 2315/// memberExpr(isArrow()) 2316/// matches this->x, x, y.x, a, this->b 2317inline internal::Matcher<MemberExpr> isArrow() { 2318 return makeMatcher(new internal::IsArrowMatcher()); 2319} 2320 2321/// \brief Matches QualType nodes that are of integer type. 2322/// 2323/// Given 2324/// \code 2325/// void a(int); 2326/// void b(long); 2327/// void c(double); 2328/// \endcode 2329/// functionDecl(hasAnyParameter(hasType(isInteger()))) 2330/// matches "a(int)", "b(long)", but not "c(double)". 2331AST_MATCHER(QualType, isInteger) { 2332 return Node->isIntegerType(); 2333} 2334 2335/// \brief Matches QualType nodes that are const-qualified, i.e., that 2336/// include "top-level" const. 2337/// 2338/// Given 2339/// \code 2340/// void a(int); 2341/// void b(int const); 2342/// void c(const int); 2343/// void d(const int*); 2344/// void e(int const) {}; 2345/// \endcode 2346/// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 2347/// matches "void b(int const)", "void c(const int)" and 2348/// "void e(int const) {}". It does not match d as there 2349/// is no top-level const on the parameter type "const int *". 2350inline internal::Matcher<QualType> isConstQualified() { 2351 return makeMatcher(new internal::IsConstQualifiedMatcher()); 2352} 2353 2354/// \brief Matches a member expression where the member is matched by a 2355/// given matcher. 2356/// 2357/// Given 2358/// \code 2359/// struct { int first, second; } first, second; 2360/// int i(second.first); 2361/// int j(first.second); 2362/// \endcode 2363/// memberExpr(member(hasName("first"))) 2364/// matches second.first 2365/// but not first.second (because the member name there is "second"). 2366AST_MATCHER_P(MemberExpr, member, 2367 internal::Matcher<ValueDecl>, InnerMatcher) { 2368 return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 2369} 2370 2371/// \brief Matches a member expression where the object expression is 2372/// matched by a given matcher. 2373/// 2374/// Given 2375/// \code 2376/// struct X { int m; }; 2377/// void f(X x) { x.m; m; } 2378/// \endcode 2379/// memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))) 2380/// matches "x.m" and "m" 2381/// with hasObjectExpression(...) 2382/// matching "x" and the implicit object expression of "m" which has type X*. 2383AST_MATCHER_P(MemberExpr, hasObjectExpression, 2384 internal::Matcher<Expr>, InnerMatcher) { 2385 return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 2386} 2387 2388/// \brief Matches any using shadow declaration. 2389/// 2390/// Given 2391/// \code 2392/// namespace X { void b(); } 2393/// using X::b; 2394/// \endcode 2395/// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 2396/// matches \code using X::b \endcode 2397AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 2398 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 2399 for (UsingDecl::shadow_iterator II = Node.shadow_begin(); 2400 II != Node.shadow_end(); ++II) { 2401 if (InnerMatcher.matches(**II, Finder, Builder)) 2402 return true; 2403 } 2404 return false; 2405} 2406 2407/// \brief Matches a using shadow declaration where the target declaration is 2408/// matched by the given matcher. 2409/// 2410/// Given 2411/// \code 2412/// namespace X { int a; void b(); } 2413/// using X::a; 2414/// using X::b; 2415/// \endcode 2416/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 2417/// matches \code using X::b \endcode 2418/// but not \code using X::a \endcode 2419AST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 2420 internal::Matcher<NamedDecl>, InnerMatcher) { 2421 return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 2422} 2423 2424/// \brief Matches template instantiations of function, class, or static 2425/// member variable template instantiations. 2426/// 2427/// Given 2428/// \code 2429/// template <typename T> class X {}; class A {}; X<A> x; 2430/// \endcode 2431/// or 2432/// \code 2433/// template <typename T> class X {}; class A {}; template class X<A>; 2434/// \endcode 2435/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2436/// matches the template instantiation of X<A>. 2437/// 2438/// But given 2439/// \code 2440/// template <typename T> class X {}; class A {}; 2441/// template <> class X<A> {}; X<A> x; 2442/// \endcode 2443/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2444/// does not match, as X<A> is an explicit template specialization. 2445/// 2446/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2447inline internal::PolymorphicMatcherWithParam0< 2448 internal::IsTemplateInstantiationMatcher> 2449isTemplateInstantiation() { 2450 return internal::PolymorphicMatcherWithParam0< 2451 internal::IsTemplateInstantiationMatcher>(); 2452} 2453 2454/// \brief Matches explicit template specializations of function, class, or 2455/// static member variable template instantiations. 2456/// 2457/// Given 2458/// \code 2459/// template<typename T> void A(T t) { } 2460/// template<> void A(int N) { } 2461/// \endcode 2462/// functionDecl(isExplicitTemplateSpecialization()) 2463/// matches the specialization A<int>(). 2464/// 2465/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2466inline internal::PolymorphicMatcherWithParam0< 2467 internal::IsExplicitTemplateSpecializationMatcher> 2468isExplicitTemplateSpecialization() { 2469 return internal::PolymorphicMatcherWithParam0< 2470 internal::IsExplicitTemplateSpecializationMatcher>(); 2471} 2472 2473/// \brief Matches \c QualTypes in the clang AST. 2474const internal::VariadicAllOfMatcher<QualType> qualType; 2475 2476/// \brief Matches \c Types in the clang AST. 2477const internal::VariadicDynCastAllOfMatcher<Type, Type> type; 2478 2479/// \brief Matches \c TypeLocs in the clang AST. 2480const internal::VariadicDynCastAllOfMatcher<TypeLoc, TypeLoc> typeLoc; 2481 2482/// \brief Matches \c TypeLocs for which the given inner 2483/// QualType-matcher matches. 2484inline internal::BindableMatcher<TypeLoc> loc( 2485 const internal::Matcher<QualType> &InnerMatcher) { 2486 return internal::BindableMatcher<TypeLoc>( 2487 new internal::TypeLocTypeMatcher(InnerMatcher)); 2488} 2489 2490/// \brief Matches builtin Types. 2491/// 2492/// Given 2493/// \code 2494/// struct A {}; 2495/// A a; 2496/// int b; 2497/// float c; 2498/// bool d; 2499/// \endcode 2500/// builtinType() 2501/// matches "int b", "float c" and "bool d" 2502AST_TYPE_MATCHER(BuiltinType, builtinType); 2503 2504/// \brief Matches all kinds of arrays. 2505/// 2506/// Given 2507/// \code 2508/// int a[] = { 2, 3 }; 2509/// int b[4]; 2510/// void f() { int c[a[0]]; } 2511/// \endcode 2512/// arrayType() 2513/// matches "int a[]", "int b[4]" and "int c[a[0]]"; 2514AST_TYPE_MATCHER(ArrayType, arrayType); 2515 2516/// \brief Matches C99 complex types. 2517/// 2518/// Given 2519/// \code 2520/// _Complex float f; 2521/// \endcode 2522/// complexType() 2523/// matches "_Complex float f" 2524AST_TYPE_MATCHER(ComplexType, complexType); 2525 2526/// \brief Matches arrays and C99 complex types that have a specific element 2527/// type. 2528/// 2529/// Given 2530/// \code 2531/// struct A {}; 2532/// A a[7]; 2533/// int b[7]; 2534/// \endcode 2535/// arrayType(hasElementType(builtinType())) 2536/// matches "int b[7]" 2537/// 2538/// Usable as: Matcher<ArrayType>, Matcher<ComplexType> 2539AST_TYPELOC_TRAVERSE_MATCHER(hasElementType, getElement); 2540 2541/// \brief Matches C arrays with a specified constant size. 2542/// 2543/// Given 2544/// \code 2545/// void() { 2546/// int a[2]; 2547/// int b[] = { 2, 3 }; 2548/// int c[b[0]]; 2549/// } 2550/// \endcode 2551/// constantArrayType() 2552/// matches "int a[2]" 2553AST_TYPE_MATCHER(ConstantArrayType, constantArrayType); 2554 2555/// \brief Matches \c ConstantArrayType nodes that have the specified size. 2556/// 2557/// Given 2558/// \code 2559/// int a[42]; 2560/// int b[2 * 21]; 2561/// int c[41], d[43]; 2562/// \endcode 2563/// constantArrayType(hasSize(42)) 2564/// matches "int a[42]" and "int b[2 * 21]" 2565AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) { 2566 return Node.getSize() == N; 2567} 2568 2569/// \brief Matches C++ arrays whose size is a value-dependent expression. 2570/// 2571/// Given 2572/// \code 2573/// template<typename T, int Size> 2574/// class array { 2575/// T data[Size]; 2576/// }; 2577/// \endcode 2578/// dependentSizedArrayType 2579/// matches "T data[Size]" 2580AST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType); 2581 2582/// \brief Matches C arrays with unspecified size. 2583/// 2584/// Given 2585/// \code 2586/// int a[] = { 2, 3 }; 2587/// int b[42]; 2588/// void f(int c[]) { int d[a[0]]; }; 2589/// \endcode 2590/// incompleteArrayType() 2591/// matches "int a[]" and "int c[]" 2592AST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType); 2593 2594/// \brief Matches C arrays with a specified size that is not an 2595/// integer-constant-expression. 2596/// 2597/// Given 2598/// \code 2599/// void f() { 2600/// int a[] = { 2, 3 } 2601/// int b[42]; 2602/// int c[a[0]]; 2603/// \endcode 2604/// variableArrayType() 2605/// matches "int c[a[0]]" 2606AST_TYPE_MATCHER(VariableArrayType, variableArrayType); 2607 2608/// \brief Matches \c VariableArrayType nodes that have a specific size 2609/// expression. 2610/// 2611/// Given 2612/// \code 2613/// void f(int b) { 2614/// int a[b]; 2615/// } 2616/// \endcode 2617/// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( 2618/// varDecl(hasName("b"))))))) 2619/// matches "int a[b]" 2620AST_MATCHER_P(VariableArrayType, hasSizeExpr, 2621 internal::Matcher<Expr>, InnerMatcher) { 2622 return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder); 2623} 2624 2625/// \brief Matches atomic types. 2626/// 2627/// Given 2628/// \code 2629/// _Atomic(int) i; 2630/// \endcode 2631/// atomicType() 2632/// matches "_Atomic(int) i" 2633AST_TYPE_MATCHER(AtomicType, atomicType); 2634 2635/// \brief Matches atomic types with a specific value type. 2636/// 2637/// Given 2638/// \code 2639/// _Atomic(int) i; 2640/// _Atomic(float) f; 2641/// \endcode 2642/// atomicType(hasValueType(isInteger())) 2643/// matches "_Atomic(int) i" 2644/// 2645/// Usable as: Matcher<AtomicType> 2646AST_TYPELOC_TRAVERSE_MATCHER(hasValueType, getValue); 2647 2648/// \brief Matches types nodes representing C++11 auto types. 2649/// 2650/// Given: 2651/// \code 2652/// auto n = 4; 2653/// int v[] = { 2, 3 } 2654/// for (auto i : v) { } 2655/// \endcode 2656/// autoType() 2657/// matches "auto n" and "auto i" 2658AST_TYPE_MATCHER(AutoType, autoType); 2659 2660/// \brief Matches \c AutoType nodes where the deduced type is a specific type. 2661/// 2662/// Note: There is no \c TypeLoc for the deduced type and thus no 2663/// \c getDeducedLoc() matcher. 2664/// 2665/// Given 2666/// \code 2667/// auto a = 1; 2668/// auto b = 2.0; 2669/// \endcode 2670/// autoType(hasDeducedType(isInteger())) 2671/// matches "auto a" 2672/// 2673/// Usable as: Matcher<AutoType> 2674AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType); 2675 2676/// \brief Matches block pointer types, i.e. types syntactically represented as 2677/// "void (^)(int)". 2678/// 2679/// The \c pointee is always required to be a \c FunctionType. 2680AST_TYPE_MATCHER(BlockPointerType, blockPointerType); 2681 2682/// \brief Matches member pointer types. 2683/// Given 2684/// \code 2685/// struct A { int i; } 2686/// A::* ptr = A::i; 2687/// \endcode 2688/// memberPointerType() 2689/// matches "A::* ptr" 2690AST_TYPE_MATCHER(MemberPointerType, memberPointerType); 2691 2692/// \brief Matches pointer types. 2693/// 2694/// Given 2695/// \code 2696/// int *a; 2697/// int &b = *a; 2698/// int c = 5; 2699/// \endcode 2700/// pointerType() 2701/// matches "int *a" 2702AST_TYPE_MATCHER(PointerType, pointerType); 2703 2704/// \brief Matches reference types. 2705/// 2706/// Given 2707/// \code 2708/// int *a; 2709/// int &b = *a; 2710/// int c = 5; 2711/// \endcode 2712/// pointerType() 2713/// matches "int &b" 2714AST_TYPE_MATCHER(ReferenceType, referenceType); 2715 2716/// \brief Narrows PointerType (and similar) matchers to those where the 2717/// \c pointee matches a given matcher. 2718/// 2719/// Given 2720/// \code 2721/// int *a; 2722/// int const *b; 2723/// float const *f; 2724/// \endcode 2725/// pointerType(pointee(isConstQualified(), isInteger())) 2726/// matches "int const *b" 2727/// 2728/// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>, 2729/// Matcher<PointerType>, Matcher<ReferenceType> 2730AST_TYPELOC_TRAVERSE_MATCHER(pointee, getPointee); 2731 2732/// \brief Matches typedef types. 2733/// 2734/// Given 2735/// \code 2736/// typedef int X; 2737/// \endcode 2738/// typedefType() 2739/// matches "typedef int X" 2740AST_TYPE_MATCHER(TypedefType, typedefType); 2741 2742/// \brief Matches \c TypedefTypes referring to a specific 2743/// \c TypedefNameDecl. 2744AST_MATCHER_P(TypedefType, hasDecl, 2745 internal::Matcher<TypedefNameDecl>, InnerMatcher) { 2746 return InnerMatcher.matches(*Node.getDecl(), Finder, Builder); 2747} 2748 2749/// \brief Matches nested name specifiers. 2750/// 2751/// Given 2752/// \code 2753/// namespace ns { 2754/// struct A { static void f(); }; 2755/// void A::f() {} 2756/// void g() { A::f(); } 2757/// } 2758/// ns::A a; 2759/// \endcode 2760/// nestedNameSpecifier() 2761/// matches "ns::" and both "A::" 2762const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 2763 2764/// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 2765const internal::VariadicAllOfMatcher< 2766 NestedNameSpecifierLoc> nestedNameSpecifierLoc; 2767 2768/// \brief Matches \c NestedNameSpecifierLocs for which the given inner 2769/// NestedNameSpecifier-matcher matches. 2770inline internal::BindableMatcher<NestedNameSpecifierLoc> loc( 2771 const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 2772 return internal::BindableMatcher<NestedNameSpecifierLoc>( 2773 new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 2774 InnerMatcher)); 2775} 2776 2777/// \brief Matches nested name specifiers that specify a type matching the 2778/// given \c QualType matcher without qualifiers. 2779/// 2780/// Given 2781/// \code 2782/// struct A { struct B { struct C {}; }; }; 2783/// A::B::C c; 2784/// \endcode 2785/// nestedNameSpecifier(specifiesType(hasDeclaration(recordDecl(hasName("A"))))) 2786/// matches "A::" 2787AST_MATCHER_P(NestedNameSpecifier, specifiesType, 2788 internal::Matcher<QualType>, InnerMatcher) { 2789 if (Node.getAsType() == NULL) 2790 return false; 2791 return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 2792} 2793 2794/// \brief Matches nested name specifier locs that specify a type matching the 2795/// given \c TypeLoc. 2796/// 2797/// Given 2798/// \code 2799/// struct A { struct B { struct C {}; }; }; 2800/// A::B::C c; 2801/// \endcode 2802/// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type( 2803/// hasDeclaration(recordDecl(hasName("A"))))))) 2804/// matches "A::" 2805AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, 2806 internal::Matcher<TypeLoc>, InnerMatcher) { 2807 return InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder); 2808} 2809 2810/// \brief Matches on the prefix of a \c NestedNameSpecifier. 2811/// 2812/// Given 2813/// \code 2814/// struct A { struct B { struct C {}; }; }; 2815/// A::B::C c; 2816/// \endcode 2817/// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 2818/// matches "A::" 2819inline internal::Matcher<NestedNameSpecifier> hasPrefix( 2820 const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 2821 return internal::makeMatcher( 2822 new internal::NestedNameSpecifierPrefixMatcher(InnerMatcher)); 2823} 2824 2825/// \brief Matches on the prefix of a \c NestedNameSpecifierLoc. 2826/// 2827/// Given 2828/// \code 2829/// struct A { struct B { struct C {}; }; }; 2830/// A::B::C c; 2831/// \endcode 2832/// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 2833/// matches "A::" 2834inline internal::Matcher<NestedNameSpecifierLoc> hasPrefix( 2835 const internal::Matcher<NestedNameSpecifierLoc> &InnerMatcher) { 2836 return internal::makeMatcher( 2837 new internal::NestedNameSpecifierLocPrefixMatcher(InnerMatcher)); 2838} 2839 2840/// \brief Matches nested name specifiers that specify a namespace matching the 2841/// given namespace matcher. 2842/// 2843/// Given 2844/// \code 2845/// namespace ns { struct A {}; } 2846/// ns::A a; 2847/// \endcode 2848/// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 2849/// matches "ns::" 2850AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 2851 internal::Matcher<NamespaceDecl>, InnerMatcher) { 2852 if (Node.getAsNamespace() == NULL) 2853 return false; 2854 return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 2855} 2856 2857} // end namespace ast_matchers 2858} // end namespace clang 2859 2860#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 2861