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