1239313Sdim//===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===// 2239313Sdim// 3239313Sdim// The LLVM Compiler Infrastructure 4239313Sdim// 5239313Sdim// This file is distributed under the University of Illinois Open Source 6239313Sdim// License. See LICENSE.TXT for details. 7239313Sdim// 8239313Sdim//===----------------------------------------------------------------------===// 9239313Sdim// 10239313Sdim// This file implements matchers to be used together with the MatchFinder to 11239313Sdim// match AST nodes. 12239313Sdim// 13239313Sdim// Matchers are created by generator functions, which can be combined in 14239313Sdim// a functional in-language DSL to express queries over the C++ AST. 15239313Sdim// 16239313Sdim// For example, to match a class with a certain name, one would call: 17243830Sdim// recordDecl(hasName("MyClass")) 18239313Sdim// which returns a matcher that can be used to find all AST nodes that declare 19239313Sdim// a class named 'MyClass'. 20239313Sdim// 21239313Sdim// For more complicated match expressions we're often interested in accessing 22239313Sdim// multiple parts of the matched AST nodes once a match is found. In that case, 23239313Sdim// use the id(...) matcher around the match expressions that match the nodes 24239313Sdim// you want to access. 25239313Sdim// 26239313Sdim// For example, when we're interested in child classes of a certain class, we 27239313Sdim// would write: 28243830Sdim// recordDecl(hasName("MyClass"), hasChild(id("child", recordDecl()))) 29239313Sdim// When the match is found via the MatchFinder, a user provided callback will 30239313Sdim// be called with a BoundNodes instance that contains a mapping from the 31239313Sdim// strings that we provided for the id(...) calls to the nodes that were 32239313Sdim// matched. 33239313Sdim// In the given example, each time our matcher finds a match we get a callback 34239313Sdim// where "child" is bound to the CXXRecordDecl node of the matching child 35239313Sdim// class declaration. 36239313Sdim// 37239313Sdim// See ASTMatchersInternal.h for a more in-depth explanation of the 38239313Sdim// implementation details of the matcher framework. 39239313Sdim// 40239313Sdim// See ASTMatchFinder.h for how to use the generated matchers to run over 41239313Sdim// an AST. 42239313Sdim// 43239313Sdim//===----------------------------------------------------------------------===// 44239313Sdim 45239313Sdim#ifndef LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 46239313Sdim#define LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 47239313Sdim 48263508Sdim#include "clang/AST/DeclFriend.h" 49239313Sdim#include "clang/AST/DeclTemplate.h" 50239313Sdim#include "clang/ASTMatchers/ASTMatchersInternal.h" 51239313Sdim#include "clang/ASTMatchers/ASTMatchersMacros.h" 52239313Sdim#include "llvm/ADT/Twine.h" 53239313Sdim#include "llvm/Support/Regex.h" 54239462Sdim#include <iterator> 55239313Sdim 56239313Sdimnamespace clang { 57239313Sdimnamespace ast_matchers { 58239313Sdim 59239313Sdim/// \brief Maps string IDs to AST nodes matched by parts of a matcher. 60239313Sdim/// 61243830Sdim/// The bound nodes are generated by calling \c bind("id") on the node matchers 62243830Sdim/// of the nodes we want to access later. 63239313Sdim/// 64243830Sdim/// The instances of BoundNodes are created by \c MatchFinder when the user's 65239313Sdim/// callbacks are executed every time a match is found. 66239313Sdimclass BoundNodes { 67239313Sdimpublic: 68243830Sdim /// \brief Returns the AST node bound to \c ID. 69243830Sdim /// 70243830Sdim /// Returns NULL if there was no node bound to \c ID or if there is a node but 71239313Sdim /// it cannot be converted to the specified type. 72243830Sdim template <typename T> 73243830Sdim const T *getNodeAs(StringRef ID) const { 74243830Sdim return MyBoundNodes.getNodeAs<T>(ID); 75243830Sdim } 76243830Sdim 77243830Sdim /// \brief Deprecated. Please use \c getNodeAs instead. 78239313Sdim /// @{ 79239313Sdim template <typename T> 80239313Sdim const T *getDeclAs(StringRef ID) const { 81243830Sdim return getNodeAs<T>(ID); 82239313Sdim } 83239313Sdim template <typename T> 84239313Sdim const T *getStmtAs(StringRef ID) const { 85243830Sdim return getNodeAs<T>(ID); 86239313Sdim } 87239313Sdim /// @} 88239313Sdim 89263508Sdim /// \brief Type of mapping from binding identifiers to bound nodes. This type 90263508Sdim /// is an associative container with a key type of \c std::string and a value 91263508Sdim /// type of \c clang::ast_type_traits::DynTypedNode 92263508Sdim typedef internal::BoundNodesMap::IDToNodeMap IDToNodeMap; 93263508Sdim 94263508Sdim /// \brief Retrieve mapping from binding identifiers to bound nodes. 95263508Sdim const IDToNodeMap &getMap() const { 96263508Sdim return MyBoundNodes.getMap(); 97263508Sdim } 98263508Sdim 99239313Sdimprivate: 100239313Sdim /// \brief Create BoundNodes from a pre-filled map of bindings. 101243830Sdim BoundNodes(internal::BoundNodesMap &MyBoundNodes) 102243830Sdim : MyBoundNodes(MyBoundNodes) {} 103239313Sdim 104243830Sdim internal::BoundNodesMap MyBoundNodes; 105239313Sdim 106263508Sdim friend class internal::BoundNodesTreeBuilder; 107239313Sdim}; 108239313Sdim 109243830Sdim/// \brief If the provided matcher matches a node, binds the node to \c ID. 110239313Sdim/// 111243830Sdim/// FIXME: Do we want to support this now that we have bind()? 112239313Sdimtemplate <typename T> 113239313Sdiminternal::Matcher<T> id(const std::string &ID, 114239313Sdim const internal::BindableMatcher<T> &InnerMatcher) { 115239313Sdim return InnerMatcher.bind(ID); 116239313Sdim} 117239313Sdim 118239313Sdim/// \brief Types of matchers for the top-level classes in the AST class 119239313Sdim/// hierarchy. 120239313Sdim/// @{ 121239313Sdimtypedef internal::Matcher<Decl> DeclarationMatcher; 122243830Sdimtypedef internal::Matcher<Stmt> StatementMatcher; 123239313Sdimtypedef internal::Matcher<QualType> TypeMatcher; 124243830Sdimtypedef internal::Matcher<TypeLoc> TypeLocMatcher; 125243830Sdimtypedef internal::Matcher<NestedNameSpecifier> NestedNameSpecifierMatcher; 126243830Sdimtypedef internal::Matcher<NestedNameSpecifierLoc> NestedNameSpecifierLocMatcher; 127239313Sdim/// @} 128239313Sdim 129239313Sdim/// \brief Matches any node. 130239313Sdim/// 131239313Sdim/// Useful when another matcher requires a child matcher, but there's no 132239313Sdim/// additional constraint. This will often be used with an explicit conversion 133243830Sdim/// to an \c internal::Matcher<> type such as \c TypeMatcher. 134239313Sdim/// 135243830Sdim/// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g., 136243830Sdim/// \code 137239313Sdim/// "int* p" and "void f()" in 138239313Sdim/// int* p; 139239313Sdim/// void f(); 140243830Sdim/// \endcode 141243830Sdim/// 142243830Sdim/// Usable as: Any Matcher 143251662Sdiminline internal::PolymorphicMatcherWithParam0<internal::TrueMatcher> 144251662Sdimanything() { 145239313Sdim return internal::PolymorphicMatcherWithParam0<internal::TrueMatcher>(); 146239313Sdim} 147239313Sdim 148239313Sdim/// \brief Matches declarations. 149239313Sdim/// 150239313Sdim/// Examples matches \c X, \c C, and the friend declaration inside \c C; 151239313Sdim/// \code 152239313Sdim/// void X(); 153239313Sdim/// class C { 154239313Sdim/// friend X; 155239313Sdim/// }; 156239313Sdim/// \endcode 157249423Sdimconst internal::VariadicAllOfMatcher<Decl> decl; 158239313Sdim 159239313Sdim/// \brief Matches a declaration of anything that could have a name. 160239313Sdim/// 161243830Sdim/// Example matches \c X, \c S, the anonymous union type, \c i, and \c U; 162243830Sdim/// \code 163239313Sdim/// typedef int X; 164239313Sdim/// struct S { 165239313Sdim/// union { 166239313Sdim/// int i; 167239313Sdim/// } U; 168239313Sdim/// }; 169243830Sdim/// \endcode 170243830Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl; 171239313Sdim 172251662Sdim/// \brief Matches a declaration of a namespace. 173251662Sdim/// 174251662Sdim/// Given 175251662Sdim/// \code 176251662Sdim/// namespace {} 177251662Sdim/// namespace test {} 178251662Sdim/// \endcode 179251662Sdim/// namespaceDecl() 180251662Sdim/// matches "namespace {}" and "namespace test {}" 181251662Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> namespaceDecl; 182251662Sdim 183239313Sdim/// \brief Matches C++ class declarations. 184239313Sdim/// 185243830Sdim/// Example matches \c X, \c Z 186243830Sdim/// \code 187239313Sdim/// class X; 188239313Sdim/// template<class T> class Z {}; 189243830Sdim/// \endcode 190239313Sdimconst internal::VariadicDynCastAllOfMatcher< 191239313Sdim Decl, 192243830Sdim CXXRecordDecl> recordDecl; 193239313Sdim 194243830Sdim/// \brief Matches C++ class template declarations. 195243830Sdim/// 196243830Sdim/// Example matches \c Z 197243830Sdim/// \code 198243830Sdim/// template<class T> class Z {}; 199243830Sdim/// \endcode 200243830Sdimconst internal::VariadicDynCastAllOfMatcher< 201243830Sdim Decl, 202243830Sdim ClassTemplateDecl> classTemplateDecl; 203243830Sdim 204239313Sdim/// \brief Matches C++ class template specializations. 205239313Sdim/// 206239313Sdim/// Given 207243830Sdim/// \code 208239313Sdim/// template<typename T> class A {}; 209239313Sdim/// template<> class A<double> {}; 210239313Sdim/// A<int> a; 211243830Sdim/// \endcode 212243830Sdim/// classTemplateSpecializationDecl() 213239313Sdim/// matches the specializations \c A<int> and \c A<double> 214239313Sdimconst internal::VariadicDynCastAllOfMatcher< 215239313Sdim Decl, 216243830Sdim ClassTemplateSpecializationDecl> classTemplateSpecializationDecl; 217239313Sdim 218263508Sdim/// \brief Matches declarator declarations (field, variable, function 219263508Sdim/// and non-type template parameter declarations). 220263508Sdim/// 221263508Sdim/// Given 222263508Sdim/// \code 223263508Sdim/// class X { int y; }; 224263508Sdim/// \endcode 225263508Sdim/// declaratorDecl() 226263508Sdim/// matches \c int y. 227263508Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl> 228263508Sdim declaratorDecl; 229263508Sdim 230263508Sdim/// \brief Matches parameter variable declarations. 231263508Sdim/// 232263508Sdim/// Given 233263508Sdim/// \code 234263508Sdim/// void f(int x); 235263508Sdim/// \endcode 236263508Sdim/// parmVarDecl() 237263508Sdim/// matches \c int x. 238263508Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> parmVarDecl; 239263508Sdim 240249423Sdim/// \brief Matches C++ access specifier declarations. 241249423Sdim/// 242249423Sdim/// Given 243249423Sdim/// \code 244249423Sdim/// class C { 245249423Sdim/// public: 246249423Sdim/// int a; 247249423Sdim/// }; 248249423Sdim/// \endcode 249249423Sdim/// accessSpecDecl() 250249423Sdim/// matches 'public:' 251249423Sdimconst internal::VariadicDynCastAllOfMatcher< 252249423Sdim Decl, 253249423Sdim AccessSpecDecl> accessSpecDecl; 254249423Sdim 255263508Sdim/// \brief Matches constructor initializers. 256263508Sdim/// 257263508Sdim/// Examples matches \c i(42). 258263508Sdim/// \code 259263508Sdim/// class C { 260263508Sdim/// C() : i(42) {} 261263508Sdim/// int i; 262263508Sdim/// }; 263263508Sdim/// \endcode 264263508Sdimconst internal::VariadicAllOfMatcher<CXXCtorInitializer> ctorInitializer; 265263508Sdim 266249423Sdim/// \brief Matches public C++ declarations. 267249423Sdim/// 268249423Sdim/// Given 269249423Sdim/// \code 270249423Sdim/// class C { 271249423Sdim/// public: int a; 272249423Sdim/// protected: int b; 273249423Sdim/// private: int c; 274249423Sdim/// }; 275249423Sdim/// \endcode 276249423Sdim/// fieldDecl(isPublic()) 277249423Sdim/// matches 'int a;' 278249423SdimAST_MATCHER(Decl, isPublic) { 279249423Sdim return Node.getAccess() == AS_public; 280249423Sdim} 281249423Sdim 282249423Sdim/// \brief Matches protected C++ declarations. 283249423Sdim/// 284249423Sdim/// Given 285249423Sdim/// \code 286249423Sdim/// class C { 287249423Sdim/// public: int a; 288249423Sdim/// protected: int b; 289249423Sdim/// private: int c; 290249423Sdim/// }; 291249423Sdim/// \endcode 292249423Sdim/// fieldDecl(isProtected()) 293249423Sdim/// matches 'int b;' 294249423SdimAST_MATCHER(Decl, isProtected) { 295249423Sdim return Node.getAccess() == AS_protected; 296249423Sdim} 297249423Sdim 298249423Sdim/// \brief Matches private C++ declarations. 299249423Sdim/// 300249423Sdim/// Given 301249423Sdim/// \code 302249423Sdim/// class C { 303249423Sdim/// public: int a; 304249423Sdim/// protected: int b; 305249423Sdim/// private: int c; 306249423Sdim/// }; 307249423Sdim/// \endcode 308249423Sdim/// fieldDecl(isPrivate()) 309249423Sdim/// matches 'int c;' 310249423SdimAST_MATCHER(Decl, isPrivate) { 311249423Sdim return Node.getAccess() == AS_private; 312249423Sdim} 313249423Sdim 314239313Sdim/// \brief Matches classTemplateSpecializations that have at least one 315243830Sdim/// TemplateArgument matching the given InnerMatcher. 316239313Sdim/// 317239313Sdim/// Given 318243830Sdim/// \code 319239313Sdim/// template<typename T> class A {}; 320239313Sdim/// template<> class A<double> {}; 321239313Sdim/// A<int> a; 322243830Sdim/// \endcode 323243830Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 324239313Sdim/// refersToType(asString("int")))) 325239313Sdim/// matches the specialization \c A<int> 326239313SdimAST_MATCHER_P(ClassTemplateSpecializationDecl, hasAnyTemplateArgument, 327243830Sdim internal::Matcher<TemplateArgument>, InnerMatcher) { 328263508Sdim llvm::ArrayRef<TemplateArgument> List = Node.getTemplateArgs().asArray(); 329263508Sdim return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder, 330263508Sdim Builder); 331239313Sdim} 332239313Sdim 333239462Sdim/// \brief Matches expressions that match InnerMatcher after any implicit casts 334239462Sdim/// are stripped off. 335239462Sdim/// 336239462Sdim/// Parentheses and explicit casts are not discarded. 337239462Sdim/// Given 338243830Sdim/// \code 339239462Sdim/// int arr[5]; 340239462Sdim/// int a = 0; 341239462Sdim/// char b = 0; 342239462Sdim/// const int c = a; 343239462Sdim/// int *d = arr; 344239462Sdim/// long e = (long) 0l; 345243830Sdim/// \endcode 346239462Sdim/// The matchers 347243830Sdim/// \code 348243830Sdim/// varDecl(hasInitializer(ignoringImpCasts(integerLiteral()))) 349243830Sdim/// varDecl(hasInitializer(ignoringImpCasts(declRefExpr()))) 350243830Sdim/// \endcode 351239462Sdim/// would match the declarations for a, b, c, and d, but not e. 352243830Sdim/// While 353243830Sdim/// \code 354243830Sdim/// varDecl(hasInitializer(integerLiteral())) 355243830Sdim/// varDecl(hasInitializer(declRefExpr())) 356243830Sdim/// \endcode 357239462Sdim/// only match the declarations for b, c, and d. 358239462SdimAST_MATCHER_P(Expr, ignoringImpCasts, 359239462Sdim internal::Matcher<Expr>, InnerMatcher) { 360239462Sdim return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder); 361239462Sdim} 362239462Sdim 363239462Sdim/// \brief Matches expressions that match InnerMatcher after parentheses and 364239462Sdim/// casts are stripped off. 365239462Sdim/// 366239462Sdim/// Implicit and non-C Style casts are also discarded. 367239462Sdim/// Given 368243830Sdim/// \code 369239462Sdim/// int a = 0; 370239462Sdim/// char b = (0); 371239462Sdim/// void* c = reinterpret_cast<char*>(0); 372239462Sdim/// char d = char(0); 373243830Sdim/// \endcode 374239462Sdim/// The matcher 375243830Sdim/// varDecl(hasInitializer(ignoringParenCasts(integerLiteral()))) 376239462Sdim/// would match the declarations for a, b, c, and d. 377239462Sdim/// while 378243830Sdim/// varDecl(hasInitializer(integerLiteral())) 379239462Sdim/// only match the declaration for a. 380239462SdimAST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) { 381239462Sdim return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder); 382239462Sdim} 383239462Sdim 384239462Sdim/// \brief Matches expressions that match InnerMatcher after implicit casts and 385239462Sdim/// parentheses are stripped off. 386239462Sdim/// 387239462Sdim/// Explicit casts are not discarded. 388239462Sdim/// Given 389243830Sdim/// \code 390239462Sdim/// int arr[5]; 391239462Sdim/// int a = 0; 392239462Sdim/// char b = (0); 393239462Sdim/// const int c = a; 394239462Sdim/// int *d = (arr); 395239462Sdim/// long e = ((long) 0l); 396243830Sdim/// \endcode 397239462Sdim/// The matchers 398243830Sdim/// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral()))) 399243830Sdim/// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr()))) 400239462Sdim/// would match the declarations for a, b, c, and d, but not e. 401239462Sdim/// while 402243830Sdim/// varDecl(hasInitializer(integerLiteral())) 403243830Sdim/// varDecl(hasInitializer(declRefExpr())) 404239462Sdim/// would only match the declaration for a. 405239462SdimAST_MATCHER_P(Expr, ignoringParenImpCasts, 406239462Sdim internal::Matcher<Expr>, InnerMatcher) { 407239462Sdim return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder); 408239462Sdim} 409239462Sdim 410239313Sdim/// \brief Matches classTemplateSpecializations where the n'th TemplateArgument 411243830Sdim/// matches the given InnerMatcher. 412239313Sdim/// 413239313Sdim/// Given 414243830Sdim/// \code 415239313Sdim/// template<typename T, typename U> class A {}; 416239313Sdim/// A<bool, int> b; 417239313Sdim/// A<int, bool> c; 418243830Sdim/// \endcode 419243830Sdim/// classTemplateSpecializationDecl(hasTemplateArgument( 420239313Sdim/// 1, refersToType(asString("int")))) 421239313Sdim/// matches the specialization \c A<bool, int> 422239313SdimAST_MATCHER_P2(ClassTemplateSpecializationDecl, hasTemplateArgument, 423243830Sdim unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) { 424239313Sdim const TemplateArgumentList &List = Node.getTemplateArgs(); 425239313Sdim if (List.size() <= N) 426239313Sdim return false; 427243830Sdim return InnerMatcher.matches(List.get(N), Finder, Builder); 428239313Sdim} 429239313Sdim 430239313Sdim/// \brief Matches a TemplateArgument that refers to a certain type. 431239313Sdim/// 432239313Sdim/// Given 433243830Sdim/// \code 434239313Sdim/// struct X {}; 435239313Sdim/// template<typename T> struct A {}; 436239313Sdim/// A<X> a; 437243830Sdim/// \endcode 438243830Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 439239313Sdim/// refersToType(class(hasName("X"))))) 440239313Sdim/// matches the specialization \c A<X> 441239313SdimAST_MATCHER_P(TemplateArgument, refersToType, 442243830Sdim internal::Matcher<QualType>, InnerMatcher) { 443239313Sdim if (Node.getKind() != TemplateArgument::Type) 444239313Sdim return false; 445243830Sdim return InnerMatcher.matches(Node.getAsType(), Finder, Builder); 446239313Sdim} 447239313Sdim 448239313Sdim/// \brief Matches a TemplateArgument that refers to a certain declaration. 449239313Sdim/// 450239313Sdim/// Given 451243830Sdim/// \code 452239313Sdim/// template<typename T> struct A {}; 453239313Sdim/// struct B { B* next; }; 454239313Sdim/// A<&B::next> a; 455243830Sdim/// \endcode 456243830Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 457243830Sdim/// refersToDeclaration(fieldDecl(hasName("next")))) 458243830Sdim/// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 459239313Sdim/// \c B::next 460239313SdimAST_MATCHER_P(TemplateArgument, refersToDeclaration, 461243830Sdim internal::Matcher<Decl>, InnerMatcher) { 462243830Sdim if (Node.getKind() == TemplateArgument::Declaration) 463243830Sdim return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder); 464239313Sdim return false; 465239313Sdim} 466239313Sdim 467239313Sdim/// \brief Matches C++ constructor declarations. 468239313Sdim/// 469239313Sdim/// Example matches Foo::Foo() and Foo::Foo(int) 470243830Sdim/// \code 471239313Sdim/// class Foo { 472239313Sdim/// public: 473239313Sdim/// Foo(); 474239313Sdim/// Foo(int); 475239313Sdim/// int DoSomething(); 476239313Sdim/// }; 477243830Sdim/// \endcode 478239313Sdimconst internal::VariadicDynCastAllOfMatcher< 479239313Sdim Decl, 480243830Sdim CXXConstructorDecl> constructorDecl; 481239313Sdim 482239313Sdim/// \brief Matches explicit C++ destructor declarations. 483239313Sdim/// 484239313Sdim/// Example matches Foo::~Foo() 485243830Sdim/// \code 486239313Sdim/// class Foo { 487239313Sdim/// public: 488239313Sdim/// virtual ~Foo(); 489239313Sdim/// }; 490243830Sdim/// \endcode 491243830Sdimconst internal::VariadicDynCastAllOfMatcher< 492243830Sdim Decl, 493243830Sdim CXXDestructorDecl> destructorDecl; 494239313Sdim 495239313Sdim/// \brief Matches enum declarations. 496239313Sdim/// 497239313Sdim/// Example matches X 498243830Sdim/// \code 499239313Sdim/// enum X { 500239313Sdim/// A, B, C 501239313Sdim/// }; 502243830Sdim/// \endcode 503239313Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl; 504239313Sdim 505239313Sdim/// \brief Matches enum constants. 506239313Sdim/// 507239313Sdim/// Example matches A, B, C 508243830Sdim/// \code 509239313Sdim/// enum X { 510239313Sdim/// A, B, C 511239313Sdim/// }; 512243830Sdim/// \endcode 513239313Sdimconst internal::VariadicDynCastAllOfMatcher< 514239313Sdim Decl, 515243830Sdim EnumConstantDecl> enumConstantDecl; 516239313Sdim 517239313Sdim/// \brief Matches method declarations. 518239313Sdim/// 519239313Sdim/// Example matches y 520243830Sdim/// \code 521239313Sdim/// class X { void y() }; 522243830Sdim/// \endcode 523243830Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> methodDecl; 524239313Sdim 525239313Sdim/// \brief Matches variable declarations. 526239313Sdim/// 527239313Sdim/// Note: this does not match declarations of member variables, which are 528239313Sdim/// "field" declarations in Clang parlance. 529239313Sdim/// 530239313Sdim/// Example matches a 531243830Sdim/// \code 532239313Sdim/// int a; 533243830Sdim/// \endcode 534243830Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl; 535239313Sdim 536239313Sdim/// \brief Matches field declarations. 537239313Sdim/// 538239313Sdim/// Given 539243830Sdim/// \code 540239313Sdim/// class X { int m; }; 541243830Sdim/// \endcode 542243830Sdim/// fieldDecl() 543239313Sdim/// matches 'm'. 544243830Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl; 545239313Sdim 546239313Sdim/// \brief Matches function declarations. 547239313Sdim/// 548239313Sdim/// Example matches f 549243830Sdim/// \code 550239313Sdim/// void f(); 551243830Sdim/// \endcode 552243830Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl; 553239313Sdim 554243830Sdim/// \brief Matches C++ function template declarations. 555243830Sdim/// 556243830Sdim/// Example matches f 557243830Sdim/// \code 558243830Sdim/// template<class T> void f(T t) {} 559243830Sdim/// \endcode 560243830Sdimconst internal::VariadicDynCastAllOfMatcher< 561243830Sdim Decl, 562243830Sdim FunctionTemplateDecl> functionTemplateDecl; 563239313Sdim 564263508Sdim/// \brief Matches friend declarations. 565263508Sdim/// 566263508Sdim/// Given 567263508Sdim/// \code 568263508Sdim/// class X { friend void foo(); }; 569263508Sdim/// \endcode 570263508Sdim/// friendDecl() 571263508Sdim/// matches 'friend void foo()'. 572263508Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl; 573263508Sdim 574239313Sdim/// \brief Matches statements. 575239313Sdim/// 576239313Sdim/// Given 577243830Sdim/// \code 578239313Sdim/// { ++a; } 579243830Sdim/// \endcode 580243830Sdim/// stmt() 581239313Sdim/// matches both the compound statement '{ ++a; }' and '++a'. 582249423Sdimconst internal::VariadicAllOfMatcher<Stmt> stmt; 583239313Sdim 584239313Sdim/// \brief Matches declaration statements. 585239313Sdim/// 586239313Sdim/// Given 587243830Sdim/// \code 588239313Sdim/// int a; 589243830Sdim/// \endcode 590243830Sdim/// declStmt() 591239313Sdim/// matches 'int a'. 592239313Sdimconst internal::VariadicDynCastAllOfMatcher< 593239313Sdim Stmt, 594243830Sdim DeclStmt> declStmt; 595239313Sdim 596239313Sdim/// \brief Matches member expressions. 597239313Sdim/// 598239313Sdim/// Given 599243830Sdim/// \code 600239313Sdim/// class Y { 601239313Sdim/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 602239313Sdim/// int a; static int b; 603239313Sdim/// }; 604243830Sdim/// \endcode 605243830Sdim/// memberExpr() 606239313Sdim/// matches this->x, x, y.x, a, this->b 607243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr; 608239313Sdim 609239313Sdim/// \brief Matches call expressions. 610239313Sdim/// 611239313Sdim/// Example matches x.y() and y() 612243830Sdim/// \code 613239313Sdim/// X x; 614239313Sdim/// x.y(); 615239313Sdim/// y(); 616243830Sdim/// \endcode 617243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr; 618239313Sdim 619243830Sdim/// \brief Matches lambda expressions. 620243830Sdim/// 621243830Sdim/// Example matches [&](){return 5;} 622243830Sdim/// \code 623243830Sdim/// [&](){return 5;} 624243830Sdim/// \endcode 625243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr; 626243830Sdim 627239313Sdim/// \brief Matches member call expressions. 628239313Sdim/// 629239313Sdim/// Example matches x.y() 630243830Sdim/// \code 631239313Sdim/// X x; 632239313Sdim/// x.y(); 633243830Sdim/// \endcode 634243830Sdimconst internal::VariadicDynCastAllOfMatcher< 635243830Sdim Stmt, 636243830Sdim CXXMemberCallExpr> memberCallExpr; 637239313Sdim 638239313Sdim/// \brief Matches init list expressions. 639239313Sdim/// 640239313Sdim/// Given 641243830Sdim/// \code 642239313Sdim/// int a[] = { 1, 2 }; 643239313Sdim/// struct B { int x, y; }; 644239313Sdim/// B b = { 5, 6 }; 645243830Sdim/// \endcode 646239313Sdim/// initList() 647239313Sdim/// matches "{ 1, 2 }" and "{ 5, 6 }" 648239313Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr; 649239313Sdim 650239313Sdim/// \brief Matches using declarations. 651239313Sdim/// 652239313Sdim/// Given 653243830Sdim/// \code 654239313Sdim/// namespace X { int x; } 655239313Sdim/// using X::x; 656243830Sdim/// \endcode 657239313Sdim/// usingDecl() 658239313Sdim/// matches \code using X::x \endcode 659239313Sdimconst internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl; 660239313Sdim 661263508Sdim/// \brief Matches unresolved using value declarations. 662263508Sdim/// 663263508Sdim/// Given 664263508Sdim/// \code 665263508Sdim/// template<typename X> 666263508Sdim/// class C : private X { 667263508Sdim/// using X::x; 668263508Sdim/// }; 669263508Sdim/// \endcode 670263508Sdim/// unresolvedUsingValueDecl() 671263508Sdim/// matches \code using X::x \endcode 672263508Sdimconst internal::VariadicDynCastAllOfMatcher< 673263508Sdim Decl, 674263508Sdim UnresolvedUsingValueDecl> unresolvedUsingValueDecl; 675263508Sdim 676239313Sdim/// \brief Matches constructor call expressions (including implicit ones). 677239313Sdim/// 678239313Sdim/// Example matches string(ptr, n) and ptr within arguments of f 679243830Sdim/// (matcher = constructExpr()) 680243830Sdim/// \code 681239313Sdim/// void f(const string &a, const string &b); 682239313Sdim/// char *ptr; 683239313Sdim/// int n; 684239313Sdim/// f(string(ptr, n), ptr); 685243830Sdim/// \endcode 686239313Sdimconst internal::VariadicDynCastAllOfMatcher< 687239313Sdim Stmt, 688243830Sdim CXXConstructExpr> constructExpr; 689239313Sdim 690263508Sdim/// \brief Matches unresolved constructor call expressions. 691263508Sdim/// 692263508Sdim/// Example matches T(t) in return statement of f 693263508Sdim/// (matcher = unresolvedConstructExpr()) 694263508Sdim/// \code 695263508Sdim/// template <typename T> 696263508Sdim/// void f(const T& t) { return T(t); } 697263508Sdim/// \endcode 698263508Sdimconst internal::VariadicDynCastAllOfMatcher< 699263508Sdim Stmt, 700263508Sdim CXXUnresolvedConstructExpr> unresolvedConstructExpr; 701263508Sdim 702243830Sdim/// \brief Matches implicit and explicit this expressions. 703243830Sdim/// 704243830Sdim/// Example matches the implicit this expression in "return i". 705243830Sdim/// (matcher = thisExpr()) 706243830Sdim/// \code 707243830Sdim/// struct foo { 708243830Sdim/// int i; 709243830Sdim/// int f() { return i; } 710243830Sdim/// }; 711243830Sdim/// \endcode 712243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> thisExpr; 713243830Sdim 714239313Sdim/// \brief Matches nodes where temporaries are created. 715239313Sdim/// 716239313Sdim/// Example matches FunctionTakesString(GetStringByValue()) 717243830Sdim/// (matcher = bindTemporaryExpr()) 718243830Sdim/// \code 719239313Sdim/// FunctionTakesString(GetStringByValue()); 720239313Sdim/// FunctionTakesStringByPointer(GetStringPointer()); 721243830Sdim/// \endcode 722239313Sdimconst internal::VariadicDynCastAllOfMatcher< 723239313Sdim Stmt, 724243830Sdim CXXBindTemporaryExpr> bindTemporaryExpr; 725239313Sdim 726243830Sdim/// \brief Matches nodes where temporaries are materialized. 727243830Sdim/// 728243830Sdim/// Example: Given 729243830Sdim/// \code 730243830Sdim/// struct T {void func()}; 731243830Sdim/// T f(); 732243830Sdim/// void g(T); 733243830Sdim/// \endcode 734243830Sdim/// materializeTemporaryExpr() matches 'f()' in these statements 735243830Sdim/// \code 736243830Sdim/// T u(f()); 737243830Sdim/// g(f()); 738243830Sdim/// \endcode 739243830Sdim/// but does not match 740243830Sdim/// \code 741243830Sdim/// f(); 742243830Sdim/// f().func(); 743243830Sdim/// \endcode 744243830Sdimconst internal::VariadicDynCastAllOfMatcher< 745243830Sdim Stmt, 746243830Sdim MaterializeTemporaryExpr> materializeTemporaryExpr; 747243830Sdim 748239313Sdim/// \brief Matches new expressions. 749239313Sdim/// 750239313Sdim/// Given 751243830Sdim/// \code 752239313Sdim/// new X; 753243830Sdim/// \endcode 754243830Sdim/// newExpr() 755239313Sdim/// matches 'new X'. 756243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> newExpr; 757239313Sdim 758239313Sdim/// \brief Matches delete expressions. 759239313Sdim/// 760239313Sdim/// Given 761243830Sdim/// \code 762239313Sdim/// delete X; 763243830Sdim/// \endcode 764243830Sdim/// deleteExpr() 765239313Sdim/// matches 'delete X'. 766243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> deleteExpr; 767239313Sdim 768239313Sdim/// \brief Matches array subscript expressions. 769239313Sdim/// 770239313Sdim/// Given 771243830Sdim/// \code 772239313Sdim/// int i = a[1]; 773243830Sdim/// \endcode 774239313Sdim/// arraySubscriptExpr() 775239313Sdim/// matches "a[1]" 776239313Sdimconst internal::VariadicDynCastAllOfMatcher< 777239313Sdim Stmt, 778239313Sdim ArraySubscriptExpr> arraySubscriptExpr; 779239313Sdim 780239313Sdim/// \brief Matches the value of a default argument at the call site. 781239313Sdim/// 782239313Sdim/// Example matches the CXXDefaultArgExpr placeholder inserted for the 783239313Sdim/// default value of the second parameter in the call expression f(42) 784243830Sdim/// (matcher = defaultArgExpr()) 785243830Sdim/// \code 786239313Sdim/// void f(int x, int y = 0); 787239313Sdim/// f(42); 788243830Sdim/// \endcode 789239313Sdimconst internal::VariadicDynCastAllOfMatcher< 790239313Sdim Stmt, 791243830Sdim CXXDefaultArgExpr> defaultArgExpr; 792239313Sdim 793239313Sdim/// \brief Matches overloaded operator calls. 794239313Sdim/// 795239313Sdim/// Note that if an operator isn't overloaded, it won't match. Instead, use 796239313Sdim/// binaryOperator matcher. 797239313Sdim/// Currently it does not match operators such as new delete. 798239313Sdim/// FIXME: figure out why these do not match? 799239313Sdim/// 800239313Sdim/// Example matches both operator<<((o << b), c) and operator<<(o, b) 801243830Sdim/// (matcher = operatorCallExpr()) 802243830Sdim/// \code 803239313Sdim/// ostream &operator<< (ostream &out, int i) { }; 804239313Sdim/// ostream &o; int b = 1, c = 1; 805239313Sdim/// o << b << c; 806243830Sdim/// \endcode 807239313Sdimconst internal::VariadicDynCastAllOfMatcher< 808239313Sdim Stmt, 809243830Sdim CXXOperatorCallExpr> operatorCallExpr; 810239313Sdim 811239313Sdim/// \brief Matches expressions. 812239313Sdim/// 813239313Sdim/// Example matches x() 814243830Sdim/// \code 815239313Sdim/// void f() { x(); } 816243830Sdim/// \endcode 817243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr; 818239313Sdim 819239313Sdim/// \brief Matches expressions that refer to declarations. 820239313Sdim/// 821239313Sdim/// Example matches x in if (x) 822243830Sdim/// \code 823239313Sdim/// bool x; 824239313Sdim/// if (x) {} 825243830Sdim/// \endcode 826243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr; 827239313Sdim 828239313Sdim/// \brief Matches if statements. 829239313Sdim/// 830239313Sdim/// Example matches 'if (x) {}' 831243830Sdim/// \code 832239313Sdim/// if (x) {} 833243830Sdim/// \endcode 834239313Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt; 835239313Sdim 836239313Sdim/// \brief Matches for statements. 837239313Sdim/// 838239313Sdim/// Example matches 'for (;;) {}' 839243830Sdim/// \code 840239313Sdim/// for (;;) {} 841243830Sdim/// int i[] = {1, 2, 3}; for (auto a : i); 842243830Sdim/// \endcode 843243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt; 844239313Sdim 845243830Sdim/// \brief Matches range-based for statements. 846243830Sdim/// 847243830Sdim/// forRangeStmt() matches 'for (auto a : i)' 848243830Sdim/// \code 849243830Sdim/// int i[] = {1, 2, 3}; for (auto a : i); 850243830Sdim/// for(int j = 0; j < 5; ++j); 851243830Sdim/// \endcode 852243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt; 853243830Sdim 854239313Sdim/// \brief Matches the increment statement of a for loop. 855239313Sdim/// 856239313Sdim/// Example: 857239313Sdim/// forStmt(hasIncrement(unaryOperator(hasOperatorName("++")))) 858239313Sdim/// matches '++x' in 859243830Sdim/// \code 860239313Sdim/// for (x; x < N; ++x) { } 861243830Sdim/// \endcode 862239313SdimAST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>, 863239313Sdim InnerMatcher) { 864239313Sdim const Stmt *const Increment = Node.getInc(); 865239313Sdim return (Increment != NULL && 866239313Sdim InnerMatcher.matches(*Increment, Finder, Builder)); 867239313Sdim} 868239313Sdim 869239313Sdim/// \brief Matches the initialization statement of a for loop. 870239313Sdim/// 871239313Sdim/// Example: 872243830Sdim/// forStmt(hasLoopInit(declStmt())) 873239313Sdim/// matches 'int x = 0' in 874243830Sdim/// \code 875239313Sdim/// for (int x = 0; x < N; ++x) { } 876243830Sdim/// \endcode 877239313SdimAST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>, 878239313Sdim InnerMatcher) { 879239313Sdim const Stmt *const Init = Node.getInit(); 880239313Sdim return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder)); 881239313Sdim} 882239313Sdim 883239313Sdim/// \brief Matches while statements. 884239313Sdim/// 885239313Sdim/// Given 886243830Sdim/// \code 887239313Sdim/// while (true) {} 888243830Sdim/// \endcode 889239313Sdim/// whileStmt() 890239313Sdim/// matches 'while (true) {}'. 891243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt; 892239313Sdim 893239313Sdim/// \brief Matches do statements. 894239313Sdim/// 895239313Sdim/// Given 896243830Sdim/// \code 897239313Sdim/// do {} while (true); 898243830Sdim/// \endcode 899239313Sdim/// doStmt() 900239313Sdim/// matches 'do {} while(true)' 901239313Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt; 902239313Sdim 903243830Sdim/// \brief Matches break statements. 904243830Sdim/// 905243830Sdim/// Given 906243830Sdim/// \code 907243830Sdim/// while (true) { break; } 908243830Sdim/// \endcode 909243830Sdim/// breakStmt() 910243830Sdim/// matches 'break' 911243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt; 912243830Sdim 913243830Sdim/// \brief Matches continue statements. 914243830Sdim/// 915243830Sdim/// Given 916243830Sdim/// \code 917243830Sdim/// while (true) { continue; } 918243830Sdim/// \endcode 919243830Sdim/// continueStmt() 920243830Sdim/// matches 'continue' 921243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> continueStmt; 922243830Sdim 923243830Sdim/// \brief Matches return statements. 924243830Sdim/// 925243830Sdim/// Given 926243830Sdim/// \code 927243830Sdim/// return 1; 928243830Sdim/// \endcode 929243830Sdim/// returnStmt() 930243830Sdim/// matches 'return 1' 931243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt; 932243830Sdim 933243830Sdim/// \brief Matches goto statements. 934243830Sdim/// 935243830Sdim/// Given 936243830Sdim/// \code 937243830Sdim/// goto FOO; 938243830Sdim/// FOO: bar(); 939243830Sdim/// \endcode 940243830Sdim/// gotoStmt() 941243830Sdim/// matches 'goto FOO' 942243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt; 943243830Sdim 944243830Sdim/// \brief Matches label statements. 945243830Sdim/// 946243830Sdim/// Given 947243830Sdim/// \code 948243830Sdim/// goto FOO; 949243830Sdim/// FOO: bar(); 950243830Sdim/// \endcode 951243830Sdim/// labelStmt() 952243830Sdim/// matches 'FOO:' 953243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt; 954243830Sdim 955243830Sdim/// \brief Matches switch statements. 956243830Sdim/// 957243830Sdim/// Given 958243830Sdim/// \code 959243830Sdim/// switch(a) { case 42: break; default: break; } 960243830Sdim/// \endcode 961243830Sdim/// switchStmt() 962243830Sdim/// matches 'switch(a)'. 963243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt; 964243830Sdim 965239313Sdim/// \brief Matches case and default statements inside switch statements. 966239313Sdim/// 967239313Sdim/// Given 968243830Sdim/// \code 969239313Sdim/// switch(a) { case 42: break; default: break; } 970243830Sdim/// \endcode 971239313Sdim/// switchCase() 972239313Sdim/// matches 'case 42: break;' and 'default: break;'. 973243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase; 974239313Sdim 975263508Sdim/// \brief Matches case statements inside switch statements. 976263508Sdim/// 977263508Sdim/// Given 978263508Sdim/// \code 979263508Sdim/// switch(a) { case 42: break; default: break; } 980263508Sdim/// \endcode 981263508Sdim/// caseStmt() 982263508Sdim/// matches 'case 42: break;'. 983263508Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt; 984263508Sdim 985263508Sdim/// \brief Matches default statements inside switch statements. 986263508Sdim/// 987263508Sdim/// Given 988263508Sdim/// \code 989263508Sdim/// switch(a) { case 42: break; default: break; } 990263508Sdim/// \endcode 991263508Sdim/// defaultStmt() 992263508Sdim/// matches 'default: break;'. 993263508Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt; 994263508Sdim 995239313Sdim/// \brief Matches compound statements. 996239313Sdim/// 997239313Sdim/// Example matches '{}' and '{{}}'in 'for (;;) {{}}' 998243830Sdim/// \code 999239313Sdim/// for (;;) {{}} 1000243830Sdim/// \endcode 1001243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt; 1002239313Sdim 1003243830Sdim/// \brief Matches catch statements. 1004243830Sdim/// 1005243830Sdim/// \code 1006243830Sdim/// try {} catch(int i) {} 1007243830Sdim/// \endcode 1008243830Sdim/// catchStmt() 1009243830Sdim/// matches 'catch(int i)' 1010243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> catchStmt; 1011243830Sdim 1012243830Sdim/// \brief Matches try statements. 1013243830Sdim/// 1014243830Sdim/// \code 1015243830Sdim/// try {} catch(int i) {} 1016243830Sdim/// \endcode 1017243830Sdim/// tryStmt() 1018243830Sdim/// matches 'try {}' 1019243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> tryStmt; 1020243830Sdim 1021243830Sdim/// \brief Matches throw expressions. 1022243830Sdim/// 1023243830Sdim/// \code 1024243830Sdim/// try { throw 5; } catch(int i) {} 1025243830Sdim/// \endcode 1026243830Sdim/// throwExpr() 1027243830Sdim/// matches 'throw 5' 1028243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> throwExpr; 1029243830Sdim 1030243830Sdim/// \brief Matches null statements. 1031243830Sdim/// 1032243830Sdim/// \code 1033243830Sdim/// foo();; 1034243830Sdim/// \endcode 1035243830Sdim/// nullStmt() 1036243830Sdim/// matches the second ';' 1037243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt; 1038243830Sdim 1039243830Sdim/// \brief Matches asm statements. 1040243830Sdim/// 1041243830Sdim/// \code 1042243830Sdim/// int i = 100; 1043243830Sdim/// __asm("mov al, 2"); 1044243830Sdim/// \endcode 1045243830Sdim/// asmStmt() 1046243830Sdim/// matches '__asm("mov al, 2")' 1047243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt; 1048243830Sdim 1049239313Sdim/// \brief Matches bool literals. 1050239313Sdim/// 1051239313Sdim/// Example matches true 1052243830Sdim/// \code 1053239313Sdim/// true 1054243830Sdim/// \endcode 1055239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1056243830Sdim Stmt, 1057239313Sdim CXXBoolLiteralExpr> boolLiteral; 1058239313Sdim 1059239313Sdim/// \brief Matches string literals (also matches wide string literals). 1060239313Sdim/// 1061239313Sdim/// Example matches "abcd", L"abcd" 1062243830Sdim/// \code 1063239313Sdim/// char *s = "abcd"; wchar_t *ws = L"abcd" 1064243830Sdim/// \endcode 1065239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1066243830Sdim Stmt, 1067239313Sdim StringLiteral> stringLiteral; 1068239313Sdim 1069239313Sdim/// \brief Matches character literals (also matches wchar_t). 1070239313Sdim/// 1071239313Sdim/// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), 1072239313Sdim/// though. 1073239313Sdim/// 1074239313Sdim/// Example matches 'a', L'a' 1075243830Sdim/// \code 1076239313Sdim/// char ch = 'a'; wchar_t chw = L'a'; 1077243830Sdim/// \endcode 1078239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1079243830Sdim Stmt, 1080239313Sdim CharacterLiteral> characterLiteral; 1081239313Sdim 1082263508Sdim/// \brief Matches integer literals of all sizes / encodings, e.g. 1083263508Sdim/// 1, 1L, 0x1 and 1U. 1084239313Sdim/// 1085263508Sdim/// Does not match character-encoded integers such as L'a'. 1086239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1087243830Sdim Stmt, 1088239313Sdim IntegerLiteral> integerLiteral; 1089239313Sdim 1090263508Sdim/// \brief Matches float literals of all sizes / encodings, e.g. 1091263508Sdim/// 1.0, 1.0f, 1.0L and 1e10. 1092263508Sdim/// 1093263508Sdim/// Does not match implicit conversions such as 1094263508Sdim/// \code 1095263508Sdim/// float a = 10; 1096263508Sdim/// \endcode 1097263508Sdimconst internal::VariadicDynCastAllOfMatcher< 1098263508Sdim Stmt, 1099263508Sdim FloatingLiteral> floatLiteral; 1100263508Sdim 1101243830Sdim/// \brief Matches user defined literal operator call. 1102243830Sdim/// 1103243830Sdim/// Example match: "foo"_suffix 1104243830Sdimconst internal::VariadicDynCastAllOfMatcher< 1105243830Sdim Stmt, 1106243830Sdim UserDefinedLiteral> userDefinedLiteral; 1107243830Sdim 1108249423Sdim/// \brief Matches compound (i.e. non-scalar) literals 1109249423Sdim/// 1110249423Sdim/// Example match: {1}, (1, 2) 1111249423Sdim/// \code 1112249423Sdim/// int array[4] = {1}; vector int myvec = (vector int)(1, 2); 1113249423Sdim/// \endcode 1114249423Sdimconst internal::VariadicDynCastAllOfMatcher< 1115249423Sdim Stmt, 1116249423Sdim CompoundLiteralExpr> compoundLiteralExpr; 1117249423Sdim 1118243830Sdim/// \brief Matches nullptr literal. 1119243830Sdimconst internal::VariadicDynCastAllOfMatcher< 1120243830Sdim Stmt, 1121243830Sdim CXXNullPtrLiteralExpr> nullPtrLiteralExpr; 1122243830Sdim 1123239313Sdim/// \brief Matches binary operator expressions. 1124239313Sdim/// 1125239313Sdim/// Example matches a || b 1126243830Sdim/// \code 1127239313Sdim/// !(a || b) 1128243830Sdim/// \endcode 1129239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1130239313Sdim Stmt, 1131239313Sdim BinaryOperator> binaryOperator; 1132239313Sdim 1133239313Sdim/// \brief Matches unary operator expressions. 1134239313Sdim/// 1135239313Sdim/// Example matches !a 1136243830Sdim/// \code 1137239313Sdim/// !a || b 1138243830Sdim/// \endcode 1139239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1140239313Sdim Stmt, 1141239313Sdim UnaryOperator> unaryOperator; 1142239313Sdim 1143239313Sdim/// \brief Matches conditional operator expressions. 1144239313Sdim/// 1145239313Sdim/// Example matches a ? b : c 1146243830Sdim/// \code 1147239313Sdim/// (a ? b : c) + 42 1148243830Sdim/// \endcode 1149239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1150239313Sdim Stmt, 1151239313Sdim ConditionalOperator> conditionalOperator; 1152239313Sdim 1153239313Sdim/// \brief Matches a reinterpret_cast expression. 1154239313Sdim/// 1155239313Sdim/// Either the source expression or the destination type can be matched 1156239313Sdim/// using has(), but hasDestinationType() is more specific and can be 1157239313Sdim/// more readable. 1158239313Sdim/// 1159239313Sdim/// Example matches reinterpret_cast<char*>(&p) in 1160243830Sdim/// \code 1161239313Sdim/// void* p = reinterpret_cast<char*>(&p); 1162243830Sdim/// \endcode 1163239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1164243830Sdim Stmt, 1165243830Sdim CXXReinterpretCastExpr> reinterpretCastExpr; 1166239313Sdim 1167239313Sdim/// \brief Matches a C++ static_cast expression. 1168239313Sdim/// 1169239313Sdim/// \see hasDestinationType 1170239313Sdim/// \see reinterpretCast 1171239313Sdim/// 1172239313Sdim/// Example: 1173243830Sdim/// staticCastExpr() 1174239313Sdim/// matches 1175239313Sdim/// static_cast<long>(8) 1176239313Sdim/// in 1177243830Sdim/// \code 1178239313Sdim/// long eight(static_cast<long>(8)); 1179243830Sdim/// \endcode 1180239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1181243830Sdim Stmt, 1182243830Sdim CXXStaticCastExpr> staticCastExpr; 1183239313Sdim 1184239313Sdim/// \brief Matches a dynamic_cast expression. 1185239313Sdim/// 1186239313Sdim/// Example: 1187243830Sdim/// dynamicCastExpr() 1188239313Sdim/// matches 1189239313Sdim/// dynamic_cast<D*>(&b); 1190239313Sdim/// in 1191243830Sdim/// \code 1192239313Sdim/// struct B { virtual ~B() {} }; struct D : B {}; 1193239313Sdim/// B b; 1194239313Sdim/// D* p = dynamic_cast<D*>(&b); 1195243830Sdim/// \endcode 1196239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1197243830Sdim Stmt, 1198243830Sdim CXXDynamicCastExpr> dynamicCastExpr; 1199239313Sdim 1200239313Sdim/// \brief Matches a const_cast expression. 1201239313Sdim/// 1202239313Sdim/// Example: Matches const_cast<int*>(&r) in 1203243830Sdim/// \code 1204239313Sdim/// int n = 42; 1205243830Sdim/// const int &r(n); 1206239313Sdim/// int* p = const_cast<int*>(&r); 1207243830Sdim/// \endcode 1208239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1209243830Sdim Stmt, 1210243830Sdim CXXConstCastExpr> constCastExpr; 1211239313Sdim 1212243830Sdim/// \brief Matches a C-style cast expression. 1213243830Sdim/// 1214243830Sdim/// Example: Matches (int*) 2.2f in 1215243830Sdim/// \code 1216243830Sdim/// int i = (int) 2.2f; 1217243830Sdim/// \endcode 1218243830Sdimconst internal::VariadicDynCastAllOfMatcher< 1219243830Sdim Stmt, 1220243830Sdim CStyleCastExpr> cStyleCastExpr; 1221243830Sdim 1222239313Sdim/// \brief Matches explicit cast expressions. 1223239313Sdim/// 1224239313Sdim/// Matches any cast expression written in user code, whether it be a 1225239313Sdim/// C-style cast, a functional-style cast, or a keyword cast. 1226239313Sdim/// 1227239313Sdim/// Does not match implicit conversions. 1228239313Sdim/// 1229239313Sdim/// Note: the name "explicitCast" is chosen to match Clang's terminology, as 1230239313Sdim/// Clang uses the term "cast" to apply to implicit conversions as well as to 1231239313Sdim/// actual cast expressions. 1232239313Sdim/// 1233239313Sdim/// \see hasDestinationType. 1234239313Sdim/// 1235239313Sdim/// Example: matches all five of the casts in 1236243830Sdim/// \code 1237239313Sdim/// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 1238243830Sdim/// \endcode 1239239313Sdim/// but does not match the implicit conversion in 1240243830Sdim/// \code 1241239313Sdim/// long ell = 42; 1242243830Sdim/// \endcode 1243239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1244243830Sdim Stmt, 1245243830Sdim ExplicitCastExpr> explicitCastExpr; 1246239313Sdim 1247239313Sdim/// \brief Matches the implicit cast nodes of Clang's AST. 1248239313Sdim/// 1249239313Sdim/// This matches many different places, including function call return value 1250239313Sdim/// eliding, as well as any type conversions. 1251239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1252243830Sdim Stmt, 1253243830Sdim ImplicitCastExpr> implicitCastExpr; 1254239313Sdim 1255239462Sdim/// \brief Matches any cast nodes of Clang's AST. 1256239462Sdim/// 1257239462Sdim/// Example: castExpr() matches each of the following: 1258243830Sdim/// \code 1259239462Sdim/// (int) 3; 1260239462Sdim/// const_cast<Expr *>(SubExpr); 1261239462Sdim/// char c = 0; 1262243830Sdim/// \endcode 1263239462Sdim/// but does not match 1264243830Sdim/// \code 1265239462Sdim/// int i = (0); 1266239462Sdim/// int k = 0; 1267243830Sdim/// \endcode 1268243830Sdimconst internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr; 1269239462Sdim 1270239313Sdim/// \brief Matches functional cast expressions 1271239313Sdim/// 1272239313Sdim/// Example: Matches Foo(bar); 1273243830Sdim/// \code 1274239313Sdim/// Foo f = bar; 1275239313Sdim/// Foo g = (Foo) bar; 1276239313Sdim/// Foo h = Foo(bar); 1277243830Sdim/// \endcode 1278239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1279243830Sdim Stmt, 1280243830Sdim CXXFunctionalCastExpr> functionalCastExpr; 1281239313Sdim 1282263508Sdim/// \brief Matches functional cast expressions having N != 1 arguments 1283263508Sdim/// 1284263508Sdim/// Example: Matches Foo(bar, bar) 1285263508Sdim/// \code 1286263508Sdim/// Foo h = Foo(bar, bar); 1287263508Sdim/// \endcode 1288263508Sdimconst internal::VariadicDynCastAllOfMatcher< 1289263508Sdim Stmt, 1290263508Sdim CXXTemporaryObjectExpr> temporaryObjectExpr; 1291263508Sdim 1292243830Sdim/// \brief Matches \c QualTypes in the clang AST. 1293243830Sdimconst internal::VariadicAllOfMatcher<QualType> qualType; 1294243830Sdim 1295243830Sdim/// \brief Matches \c Types in the clang AST. 1296249423Sdimconst internal::VariadicAllOfMatcher<Type> type; 1297243830Sdim 1298243830Sdim/// \brief Matches \c TypeLocs in the clang AST. 1299249423Sdimconst internal::VariadicAllOfMatcher<TypeLoc> typeLoc; 1300243830Sdim 1301249423Sdim/// \brief Matches if any of the given matchers matches. 1302249423Sdim/// 1303249423Sdim/// Unlike \c anyOf, \c eachOf will generate a match result for each 1304249423Sdim/// matching submatcher. 1305249423Sdim/// 1306249423Sdim/// For example, in: 1307249423Sdim/// \code 1308249423Sdim/// class A { int a; int b; }; 1309249423Sdim/// \endcode 1310249423Sdim/// The matcher: 1311249423Sdim/// \code 1312249423Sdim/// recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), 1313249423Sdim/// has(fieldDecl(hasName("b")).bind("v")))) 1314249423Sdim/// \endcode 1315249423Sdim/// will generate two results binding "v", the first of which binds 1316249423Sdim/// the field declaration of \c a, the second the field declaration of 1317249423Sdim/// \c b. 1318249423Sdim/// 1319249423Sdim/// Usable as: Any Matcher 1320263508Sdimconst internal::VariadicOperatorMatcherFunc eachOf = { 1321263508Sdim internal::EachOfVariadicOperator 1322263508Sdim}; 1323249423Sdim 1324243830Sdim/// \brief Matches if any of the given matchers matches. 1325243830Sdim/// 1326243830Sdim/// Usable as: Any Matcher 1327263508Sdimconst internal::VariadicOperatorMatcherFunc anyOf = { 1328263508Sdim internal::AnyOfVariadicOperator 1329263508Sdim}; 1330243830Sdim 1331243830Sdim/// \brief Matches if all given matchers match. 1332243830Sdim/// 1333243830Sdim/// Usable as: Any Matcher 1334263508Sdimconst internal::VariadicOperatorMatcherFunc allOf = { 1335263508Sdim internal::AllOfVariadicOperator 1336263508Sdim}; 1337243830Sdim 1338239313Sdim/// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 1339239313Sdim/// 1340239313Sdim/// Given 1341243830Sdim/// \code 1342239313Sdim/// Foo x = bar; 1343239313Sdim/// int y = sizeof(x) + alignof(x); 1344243830Sdim/// \endcode 1345239313Sdim/// unaryExprOrTypeTraitExpr() 1346239313Sdim/// matches \c sizeof(x) and \c alignof(x) 1347239313Sdimconst internal::VariadicDynCastAllOfMatcher< 1348239313Sdim Stmt, 1349239313Sdim UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr; 1350239313Sdim 1351239313Sdim/// \brief Matches unary expressions that have a specific type of argument. 1352239313Sdim/// 1353239313Sdim/// Given 1354243830Sdim/// \code 1355239313Sdim/// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 1356243830Sdim/// \endcode 1357239313Sdim/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 1358239313Sdim/// matches \c sizeof(a) and \c alignof(c) 1359239313SdimAST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 1360243830Sdim internal::Matcher<QualType>, InnerMatcher) { 1361239313Sdim const QualType ArgumentType = Node.getTypeOfArgument(); 1362243830Sdim return InnerMatcher.matches(ArgumentType, Finder, Builder); 1363239313Sdim} 1364239313Sdim 1365239313Sdim/// \brief Matches unary expressions of a certain kind. 1366239313Sdim/// 1367239313Sdim/// Given 1368243830Sdim/// \code 1369239313Sdim/// int x; 1370239313Sdim/// int s = sizeof(x) + alignof(x) 1371243830Sdim/// \endcode 1372239313Sdim/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 1373239313Sdim/// matches \c sizeof(x) 1374239313SdimAST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 1375239313Sdim return Node.getKind() == Kind; 1376239313Sdim} 1377239313Sdim 1378239313Sdim/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1379239313Sdim/// alignof. 1380239313Sdiminline internal::Matcher<Stmt> alignOfExpr( 1381243830Sdim const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1382249423Sdim return stmt(unaryExprOrTypeTraitExpr(allOf( 1383243830Sdim ofKind(UETT_AlignOf), InnerMatcher))); 1384239313Sdim} 1385239313Sdim 1386239313Sdim/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1387239313Sdim/// sizeof. 1388239313Sdiminline internal::Matcher<Stmt> sizeOfExpr( 1389243830Sdim const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1390249423Sdim return stmt(unaryExprOrTypeTraitExpr( 1391249423Sdim allOf(ofKind(UETT_SizeOf), InnerMatcher))); 1392239313Sdim} 1393239313Sdim 1394239313Sdim/// \brief Matches NamedDecl nodes that have the specified name. 1395239313Sdim/// 1396239313Sdim/// Supports specifying enclosing namespaces or classes by prefixing the name 1397239313Sdim/// with '<enclosing>::'. 1398239313Sdim/// Does not match typedefs of an underlying type with the given name. 1399239313Sdim/// 1400239313Sdim/// Example matches X (Name == "X") 1401243830Sdim/// \code 1402239313Sdim/// class X; 1403243830Sdim/// \endcode 1404239313Sdim/// 1405239313Sdim/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 1406243830Sdim/// \code 1407243830Sdim/// namespace a { namespace b { class X; } } 1408243830Sdim/// \endcode 1409239313SdimAST_MATCHER_P(NamedDecl, hasName, std::string, Name) { 1410239313Sdim assert(!Name.empty()); 1411239313Sdim const std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1412249423Sdim const StringRef FullName = FullNameString; 1413249423Sdim const StringRef Pattern = Name; 1414239313Sdim if (Pattern.startswith("::")) { 1415239313Sdim return FullName == Pattern; 1416239313Sdim } else { 1417239313Sdim return FullName.endswith(("::" + Pattern).str()); 1418239313Sdim } 1419239313Sdim} 1420239313Sdim 1421249423Sdim/// \brief Matches NamedDecl nodes whose fully qualified names contain 1422249423Sdim/// a substring matched by the given RegExp. 1423239313Sdim/// 1424239313Sdim/// Supports specifying enclosing namespaces or classes by 1425239313Sdim/// prefixing the name with '<enclosing>::'. Does not match typedefs 1426239313Sdim/// of an underlying type with the given name. 1427239313Sdim/// 1428239313Sdim/// Example matches X (regexp == "::X") 1429243830Sdim/// \code 1430239313Sdim/// class X; 1431243830Sdim/// \endcode 1432239313Sdim/// 1433239313Sdim/// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 1434243830Sdim/// \code 1435243830Sdim/// namespace foo { namespace bar { class X; } } 1436243830Sdim/// \endcode 1437239313SdimAST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 1438239313Sdim assert(!RegExp.empty()); 1439239313Sdim std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1440239313Sdim llvm::Regex RE(RegExp); 1441239313Sdim return RE.match(FullNameString); 1442239313Sdim} 1443239313Sdim 1444239313Sdim/// \brief Matches overloaded operator names. 1445239313Sdim/// 1446239313Sdim/// Matches overloaded operator names specified in strings without the 1447249423Sdim/// "operator" prefix: e.g. "<<". 1448239313Sdim/// 1449249423Sdim/// Given: 1450243830Sdim/// \code 1451249423Sdim/// class A { int operator*(); }; 1452249423Sdim/// const A &operator<<(const A &a, const A &b); 1453249423Sdim/// A a; 1454249423Sdim/// a << a; // <-- This matches 1455243830Sdim/// \endcode 1456249423Sdim/// 1457249423Sdim/// \c operatorCallExpr(hasOverloadedOperatorName("<<"))) matches the specified 1458249423Sdim/// line and \c recordDecl(hasMethod(hasOverloadedOperatorName("*"))) matches 1459249423Sdim/// the declaration of \c A. 1460249423Sdim/// 1461249423Sdim/// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<CXXMethodDecl> 1462249423Sdiminline internal::PolymorphicMatcherWithParam1< 1463263508Sdim internal::HasOverloadedOperatorNameMatcher, StringRef, 1464263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)> 1465249423SdimhasOverloadedOperatorName(const StringRef Name) { 1466249423Sdim return internal::PolymorphicMatcherWithParam1< 1467263508Sdim internal::HasOverloadedOperatorNameMatcher, StringRef, 1468263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)>( 1469263508Sdim Name); 1470239313Sdim} 1471239313Sdim 1472239313Sdim/// \brief Matches C++ classes that are directly or indirectly derived from 1473239313Sdim/// a class matching \c Base. 1474239313Sdim/// 1475243830Sdim/// Note that a class is not considered to be derived from itself. 1476239313Sdim/// 1477243830Sdim/// Example matches Y, Z, C (Base == hasName("X")) 1478243830Sdim/// \code 1479243830Sdim/// class X; 1480239313Sdim/// class Y : public X {}; // directly derived 1481239313Sdim/// class Z : public Y {}; // indirectly derived 1482239313Sdim/// typedef X A; 1483239313Sdim/// typedef A B; 1484239313Sdim/// class C : public B {}; // derived from a typedef of X 1485243830Sdim/// \endcode 1486239313Sdim/// 1487239313Sdim/// In the following example, Bar matches isDerivedFrom(hasName("X")): 1488243830Sdim/// \code 1489239313Sdim/// class Foo; 1490239313Sdim/// typedef Foo X; 1491239313Sdim/// class Bar : public Foo {}; // derived from a type that X is a typedef of 1492243830Sdim/// \endcode 1493239313SdimAST_MATCHER_P(CXXRecordDecl, isDerivedFrom, 1494239313Sdim internal::Matcher<NamedDecl>, Base) { 1495239313Sdim return Finder->classIsDerivedFrom(&Node, Base, Builder); 1496239313Sdim} 1497239313Sdim 1498239313Sdim/// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 1499263508SdimAST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, StringRef, BaseName, 1) { 1500239313Sdim assert(!BaseName.empty()); 1501263508Sdim return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); 1502239313Sdim} 1503239313Sdim 1504243830Sdim/// \brief Similar to \c isDerivedFrom(), but also matches classes that directly 1505243830Sdim/// match \c Base. 1506263508SdimAST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, 1507263508Sdim internal::Matcher<NamedDecl>, Base, 0) { 1508263508Sdim return Matcher<CXXRecordDecl>(anyOf(Base, isDerivedFrom(Base))) 1509263508Sdim .matches(Node, Finder, Builder); 1510243830Sdim} 1511243830Sdim 1512243830Sdim/// \brief Overloaded method as shortcut for 1513243830Sdim/// \c isSameOrDerivedFrom(hasName(...)). 1514263508SdimAST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, StringRef, BaseName, 1515263508Sdim 1) { 1516243830Sdim assert(!BaseName.empty()); 1517263508Sdim return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); 1518243830Sdim} 1519243830Sdim 1520249423Sdim/// \brief Matches the first method of a class or struct that satisfies \c 1521249423Sdim/// InnerMatcher. 1522249423Sdim/// 1523249423Sdim/// Given: 1524249423Sdim/// \code 1525249423Sdim/// class A { void func(); }; 1526249423Sdim/// class B { void member(); }; 1527249423Sdim/// \code 1528249423Sdim/// 1529249423Sdim/// \c recordDecl(hasMethod(hasName("func"))) matches the declaration of \c A 1530249423Sdim/// but not \c B. 1531249423SdimAST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>, 1532249423Sdim InnerMatcher) { 1533263508Sdim return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(), 1534263508Sdim Node.method_end(), Finder, Builder); 1535249423Sdim} 1536249423Sdim 1537239313Sdim/// \brief Matches AST nodes that have child AST nodes that match the 1538239313Sdim/// provided matcher. 1539239313Sdim/// 1540243830Sdim/// Example matches X, Y (matcher = recordDecl(has(recordDecl(hasName("X"))) 1541243830Sdim/// \code 1542239313Sdim/// class X {}; // Matches X, because X::X is a class of name X inside X. 1543239313Sdim/// class Y { class X {}; }; 1544239313Sdim/// class Z { class Y { class X {}; }; }; // Does not match Z. 1545243830Sdim/// \endcode 1546239313Sdim/// 1547239313Sdim/// ChildT must be an AST base type. 1548243830Sdim/// 1549243830Sdim/// Usable as: Any Matcher 1550263508Sdimconst internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> 1551263508SdimLLVM_ATTRIBUTE_UNUSED has = {}; 1552239313Sdim 1553239313Sdim/// \brief Matches AST nodes that have descendant AST nodes that match the 1554239313Sdim/// provided matcher. 1555239313Sdim/// 1556239313Sdim/// Example matches X, Y, Z 1557243830Sdim/// (matcher = recordDecl(hasDescendant(recordDecl(hasName("X"))))) 1558243830Sdim/// \code 1559239313Sdim/// class X {}; // Matches X, because X::X is a class of name X inside X. 1560239313Sdim/// class Y { class X {}; }; 1561239313Sdim/// class Z { class Y { class X {}; }; }; 1562243830Sdim/// \endcode 1563239313Sdim/// 1564239313Sdim/// DescendantT must be an AST base type. 1565243830Sdim/// 1566243830Sdim/// Usable as: Any Matcher 1567263508Sdimconst internal::ArgumentAdaptingMatcherFunc<internal::HasDescendantMatcher> 1568263508SdimLLVM_ATTRIBUTE_UNUSED hasDescendant = {}; 1569239313Sdim 1570239313Sdim/// \brief Matches AST nodes that have child AST nodes that match the 1571239313Sdim/// provided matcher. 1572239313Sdim/// 1573243830Sdim/// Example matches X, Y (matcher = recordDecl(forEach(recordDecl(hasName("X"))) 1574243830Sdim/// \code 1575239313Sdim/// class X {}; // Matches X, because X::X is a class of name X inside X. 1576239313Sdim/// class Y { class X {}; }; 1577239313Sdim/// class Z { class Y { class X {}; }; }; // Does not match Z. 1578243830Sdim/// \endcode 1579239313Sdim/// 1580239313Sdim/// ChildT must be an AST base type. 1581239313Sdim/// 1582239313Sdim/// As opposed to 'has', 'forEach' will cause a match for each result that 1583239313Sdim/// matches instead of only on the first one. 1584243830Sdim/// 1585243830Sdim/// Usable as: Any Matcher 1586263508Sdimconst internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher> 1587263508SdimLLVM_ATTRIBUTE_UNUSED forEach = {}; 1588239313Sdim 1589239313Sdim/// \brief Matches AST nodes that have descendant AST nodes that match the 1590239313Sdim/// provided matcher. 1591239313Sdim/// 1592239313Sdim/// Example matches X, A, B, C 1593243830Sdim/// (matcher = recordDecl(forEachDescendant(recordDecl(hasName("X"))))) 1594243830Sdim/// \code 1595239313Sdim/// class X {}; // Matches X, because X::X is a class of name X inside X. 1596239313Sdim/// class A { class X {}; }; 1597239313Sdim/// class B { class C { class X {}; }; }; 1598243830Sdim/// \endcode 1599239313Sdim/// 1600239313Sdim/// DescendantT must be an AST base type. 1601239313Sdim/// 1602239313Sdim/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 1603239313Sdim/// each result that matches instead of only on the first one. 1604239313Sdim/// 1605239313Sdim/// Note: Recursively combined ForEachDescendant can cause many matches: 1606243830Sdim/// recordDecl(forEachDescendant(recordDecl(forEachDescendant(recordDecl())))) 1607239313Sdim/// will match 10 times (plus injected class name matches) on: 1608243830Sdim/// \code 1609239313Sdim/// class A { class B { class C { class D { class E {}; }; }; }; }; 1610243830Sdim/// \endcode 1611243830Sdim/// 1612243830Sdim/// Usable as: Any Matcher 1613263508Sdimconst internal::ArgumentAdaptingMatcherFunc<internal::ForEachDescendantMatcher> 1614263508SdimLLVM_ATTRIBUTE_UNUSED forEachDescendant = {}; 1615239313Sdim 1616249423Sdim/// \brief Matches if the node or any descendant matches. 1617249423Sdim/// 1618249423Sdim/// Generates results for each match. 1619249423Sdim/// 1620249423Sdim/// For example, in: 1621249423Sdim/// \code 1622249423Sdim/// class A { class B {}; class C {}; }; 1623249423Sdim/// \endcode 1624249423Sdim/// The matcher: 1625249423Sdim/// \code 1626249423Sdim/// recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("m"))) 1627249423Sdim/// \endcode 1628249423Sdim/// will generate results for \c A, \c B and \c C. 1629249423Sdim/// 1630249423Sdim/// Usable as: Any Matcher 1631249423Sdimtemplate <typename T> 1632263508Sdiminternal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) { 1633249423Sdim return eachOf(Matcher, forEachDescendant(Matcher)); 1634249423Sdim} 1635249423Sdim 1636243830Sdim/// \brief Matches AST nodes that have a parent that matches the provided 1637243830Sdim/// matcher. 1638243830Sdim/// 1639243830Sdim/// Given 1640243830Sdim/// \code 1641243830Sdim/// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } } 1642243830Sdim/// \endcode 1643243830Sdim/// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }". 1644243830Sdim/// 1645243830Sdim/// Usable as: Any Matcher 1646263508Sdimconst internal::ArgumentAdaptingMatcherFunc< 1647263508Sdim internal::HasParentMatcher, internal::TypeList<Decl, Stmt>, 1648263508Sdim internal::TypeList<Decl, Stmt> > LLVM_ATTRIBUTE_UNUSED hasParent = {}; 1649243830Sdim 1650243830Sdim/// \brief Matches AST nodes that have an ancestor that matches the provided 1651243830Sdim/// matcher. 1652243830Sdim/// 1653243830Sdim/// Given 1654243830Sdim/// \code 1655243830Sdim/// void f() { if (true) { int x = 42; } } 1656243830Sdim/// void g() { for (;;) { int x = 43; } } 1657243830Sdim/// \endcode 1658243830Sdim/// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43. 1659243830Sdim/// 1660243830Sdim/// Usable as: Any Matcher 1661263508Sdimconst internal::ArgumentAdaptingMatcherFunc< 1662263508Sdim internal::HasAncestorMatcher, internal::TypeList<Decl, Stmt>, 1663263508Sdim internal::TypeList<Decl, Stmt> > LLVM_ATTRIBUTE_UNUSED hasAncestor = {}; 1664243830Sdim 1665239313Sdim/// \brief Matches if the provided matcher does not match. 1666239313Sdim/// 1667243830Sdim/// Example matches Y (matcher = recordDecl(unless(hasName("X")))) 1668243830Sdim/// \code 1669239313Sdim/// class X {}; 1670239313Sdim/// class Y {}; 1671243830Sdim/// \endcode 1672243830Sdim/// 1673243830Sdim/// Usable as: Any Matcher 1674239313Sdimtemplate <typename M> 1675243830Sdiminternal::PolymorphicMatcherWithParam1<internal::NotMatcher, M> 1676243830Sdimunless(const M &InnerMatcher) { 1677239313Sdim return internal::PolymorphicMatcherWithParam1< 1678239313Sdim internal::NotMatcher, M>(InnerMatcher); 1679239313Sdim} 1680239313Sdim 1681263508Sdim/// \brief Matches a node if the declaration associated with that node 1682263508Sdim/// matches the given matcher. 1683239313Sdim/// 1684263508Sdim/// The associated declaration is: 1685263508Sdim/// - for type nodes, the declaration of the underlying type 1686263508Sdim/// - for CallExpr, the declaration of the callee 1687263508Sdim/// - for MemberExpr, the declaration of the referenced member 1688263508Sdim/// - for CXXConstructExpr, the declaration of the constructor 1689249423Sdim/// 1690263508Sdim/// Also usable as Matcher<T> for any T supporting the getDecl() member 1691263508Sdim/// function. e.g. various subtypes of clang::Type and various expressions. 1692263508Sdim/// 1693263508Sdim/// Usable as: Matcher<CallExpr>, Matcher<CXXConstructExpr>, 1694263508Sdim/// Matcher<DeclRefExpr>, Matcher<EnumType>, Matcher<InjectedClassNameType>, 1695263508Sdim/// Matcher<LabelStmt>, Matcher<MemberExpr>, Matcher<QualType>, 1696263508Sdim/// Matcher<RecordType>, Matcher<TagType>, 1697263508Sdim/// Matcher<TemplateSpecializationType>, Matcher<TemplateTypeParmType>, 1698263508Sdim/// Matcher<TypedefType>, Matcher<UnresolvedUsingType> 1699263508Sdiminline internal::PolymorphicMatcherWithParam1< 1700263508Sdim internal::HasDeclarationMatcher, internal::Matcher<Decl>, 1701263508Sdim void(internal::HasDeclarationSupportedTypes)> 1702263508SdimhasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 1703239313Sdim return internal::PolymorphicMatcherWithParam1< 1704263508Sdim internal::HasDeclarationMatcher, internal::Matcher<Decl>, 1705263508Sdim void(internal::HasDeclarationSupportedTypes)>(InnerMatcher); 1706239313Sdim} 1707239313Sdim 1708239313Sdim/// \brief Matches on the implicit object argument of a member call expression. 1709239313Sdim/// 1710243830Sdim/// Example matches y.x() (matcher = callExpr(on(hasType(recordDecl(hasName("Y")))))) 1711243830Sdim/// \code 1712239313Sdim/// class Y { public: void x(); }; 1713239313Sdim/// void z() { Y y; y.x(); }", 1714243830Sdim/// \endcode 1715239313Sdim/// 1716239313Sdim/// FIXME: Overload to allow directly matching types? 1717239313SdimAST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 1718239313Sdim InnerMatcher) { 1719249423Sdim const Expr *ExprNode = Node.getImplicitObjectArgument() 1720249423Sdim ->IgnoreParenImpCasts(); 1721239313Sdim return (ExprNode != NULL && 1722239313Sdim InnerMatcher.matches(*ExprNode, Finder, Builder)); 1723239313Sdim} 1724239313Sdim 1725239313Sdim/// \brief Matches if the call expression's callee expression matches. 1726239313Sdim/// 1727239313Sdim/// Given 1728243830Sdim/// \code 1729239313Sdim/// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 1730239313Sdim/// void f() { f(); } 1731243830Sdim/// \endcode 1732243830Sdim/// callExpr(callee(expr())) 1733239313Sdim/// matches this->x(), x(), y.x(), f() 1734239313Sdim/// with callee(...) 1735239313Sdim/// matching this->x, x, y.x, f respectively 1736239313Sdim/// 1737239313Sdim/// Note: Callee cannot take the more general internal::Matcher<Expr> 1738239313Sdim/// because this introduces ambiguous overloads with calls to Callee taking a 1739239313Sdim/// internal::Matcher<Decl>, as the matcher hierarchy is purely 1740239313Sdim/// implemented in terms of implicit casts. 1741239313SdimAST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 1742239313Sdim InnerMatcher) { 1743239313Sdim const Expr *ExprNode = Node.getCallee(); 1744239313Sdim return (ExprNode != NULL && 1745239313Sdim InnerMatcher.matches(*ExprNode, Finder, Builder)); 1746239313Sdim} 1747239313Sdim 1748239313Sdim/// \brief Matches if the call expression's callee's declaration matches the 1749239313Sdim/// given matcher. 1750239313Sdim/// 1751243830Sdim/// Example matches y.x() (matcher = callExpr(callee(methodDecl(hasName("x"))))) 1752243830Sdim/// \code 1753239313Sdim/// class Y { public: void x(); }; 1754239313Sdim/// void z() { Y y; y.x(); 1755243830Sdim/// \endcode 1756263508SdimAST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher, 1757263508Sdim 1) { 1758263508Sdim return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder); 1759239313Sdim} 1760239313Sdim 1761239313Sdim/// \brief Matches if the expression's or declaration's type matches a type 1762239313Sdim/// matcher. 1763239313Sdim/// 1764243830Sdim/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1765243830Sdim/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1766243830Sdim/// \code 1767239313Sdim/// class X {}; 1768239313Sdim/// void y(X &x) { x; X z; } 1769243830Sdim/// \endcode 1770263508SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 1771263508Sdim hasType, AST_POLYMORPHIC_SUPPORTED_TYPES_2(Expr, ValueDecl), 1772263508Sdim internal::Matcher<QualType>, InnerMatcher, 0) { 1773239313Sdim return InnerMatcher.matches(Node.getType(), Finder, Builder); 1774239313Sdim} 1775239313Sdim 1776239313Sdim/// \brief Overloaded to match the declaration of the expression's or value 1777239313Sdim/// declaration's type. 1778239313Sdim/// 1779239313Sdim/// In case of a value declaration (for example a variable declaration), 1780239313Sdim/// this resolves one layer of indirection. For example, in the value 1781243830Sdim/// declaration "X x;", recordDecl(hasName("X")) matches the declaration of X, 1782243830Sdim/// while varDecl(hasType(recordDecl(hasName("X")))) matches the declaration 1783239313Sdim/// of x." 1784239313Sdim/// 1785243830Sdim/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1786243830Sdim/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1787243830Sdim/// \code 1788239313Sdim/// class X {}; 1789239313Sdim/// void y(X &x) { x; X z; } 1790243830Sdim/// \endcode 1791239313Sdim/// 1792239313Sdim/// Usable as: Matcher<Expr>, Matcher<ValueDecl> 1793263508SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 1794263508Sdim hasType, AST_POLYMORPHIC_SUPPORTED_TYPES_2(Expr, ValueDecl), 1795263508Sdim internal::Matcher<Decl>, InnerMatcher, 1) { 1796263508Sdim return qualType(hasDeclaration(InnerMatcher)) 1797263508Sdim .matches(Node.getType(), Finder, Builder); 1798239313Sdim} 1799239313Sdim 1800263508Sdim/// \brief Matches if the type location of the declarator decl's type matches 1801263508Sdim/// the inner matcher. 1802263508Sdim/// 1803263508Sdim/// Given 1804263508Sdim/// \code 1805263508Sdim/// int x; 1806263508Sdim/// \endcode 1807263508Sdim/// declaratorDecl(hasTypeLoc(loc(asString("int")))) 1808263508Sdim/// matches int x 1809263508SdimAST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) { 1810263508Sdim if (!Node.getTypeSourceInfo()) 1811263508Sdim // This happens for example for implicit destructors. 1812263508Sdim return false; 1813263508Sdim return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder); 1814263508Sdim} 1815263508Sdim 1816239313Sdim/// \brief Matches if the matched type is represented by the given string. 1817239313Sdim/// 1818239313Sdim/// Given 1819243830Sdim/// \code 1820239313Sdim/// class Y { public: void x(); }; 1821239313Sdim/// void z() { Y* y; y->x(); } 1822243830Sdim/// \endcode 1823243830Sdim/// callExpr(on(hasType(asString("class Y *")))) 1824239313Sdim/// matches y->x() 1825239313SdimAST_MATCHER_P(QualType, asString, std::string, Name) { 1826239313Sdim return Name == Node.getAsString(); 1827239313Sdim} 1828239313Sdim 1829239313Sdim/// \brief Matches if the matched type is a pointer type and the pointee type 1830239313Sdim/// matches the specified matcher. 1831239313Sdim/// 1832239313Sdim/// Example matches y->x() 1833243830Sdim/// (matcher = callExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))))) 1834243830Sdim/// \code 1835239313Sdim/// class Y { public: void x(); }; 1836239313Sdim/// void z() { Y *y; y->x(); } 1837243830Sdim/// \endcode 1838239313SdimAST_MATCHER_P( 1839239313Sdim QualType, pointsTo, internal::Matcher<QualType>, 1840239313Sdim InnerMatcher) { 1841239313Sdim return (!Node.isNull() && Node->isPointerType() && 1842239313Sdim InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1843239313Sdim} 1844239313Sdim 1845239313Sdim/// \brief Overloaded to match the pointee type's declaration. 1846263508SdimAST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>, 1847263508Sdim InnerMatcher, 1) { 1848263508Sdim return pointsTo(qualType(hasDeclaration(InnerMatcher))) 1849263508Sdim .matches(Node, Finder, Builder); 1850239313Sdim} 1851239313Sdim 1852239313Sdim/// \brief Matches if the matched type is a reference type and the referenced 1853239313Sdim/// type matches the specified matcher. 1854239313Sdim/// 1855239313Sdim/// Example matches X &x and const X &y 1856243830Sdim/// (matcher = varDecl(hasType(references(recordDecl(hasName("X")))))) 1857243830Sdim/// \code 1858239313Sdim/// class X { 1859239313Sdim/// void a(X b) { 1860239313Sdim/// X &x = b; 1861239313Sdim/// const X &y = b; 1862239313Sdim/// }; 1863243830Sdim/// \endcode 1864239313SdimAST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 1865239313Sdim InnerMatcher) { 1866239313Sdim return (!Node.isNull() && Node->isReferenceType() && 1867239313Sdim InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1868239313Sdim} 1869239313Sdim 1870249423Sdim/// \brief Matches QualTypes whose canonical type matches InnerMatcher. 1871249423Sdim/// 1872249423Sdim/// Given: 1873249423Sdim/// \code 1874249423Sdim/// typedef int &int_ref; 1875249423Sdim/// int a; 1876249423Sdim/// int_ref b = a; 1877249423Sdim/// \code 1878249423Sdim/// 1879249423Sdim/// \c varDecl(hasType(qualType(referenceType()))))) will not match the 1880249423Sdim/// declaration of b but \c 1881249423Sdim/// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does. 1882249423SdimAST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>, 1883249423Sdim InnerMatcher) { 1884263508Sdim if (Node.isNull()) 1885263508Sdim return false; 1886249423Sdim return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder); 1887249423Sdim} 1888249423Sdim 1889239313Sdim/// \brief Overloaded to match the referenced type's declaration. 1890263508SdimAST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>, 1891263508Sdim InnerMatcher, 1) { 1892263508Sdim return references(qualType(hasDeclaration(InnerMatcher))) 1893263508Sdim .matches(Node, Finder, Builder); 1894239313Sdim} 1895239313Sdim 1896239313SdimAST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 1897239313Sdim internal::Matcher<Expr>, InnerMatcher) { 1898249423Sdim const Expr *ExprNode = Node.getImplicitObjectArgument(); 1899239313Sdim return (ExprNode != NULL && 1900239313Sdim InnerMatcher.matches(*ExprNode, Finder, Builder)); 1901239313Sdim} 1902239313Sdim 1903239313Sdim/// \brief Matches if the expression's type either matches the specified 1904239313Sdim/// matcher, or is a pointer to a type that matches the InnerMatcher. 1905263508SdimAST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 1906263508Sdim internal::Matcher<QualType>, InnerMatcher, 0) { 1907239313Sdim return onImplicitObjectArgument( 1908263508Sdim anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 1909263508Sdim .matches(Node, Finder, Builder); 1910239313Sdim} 1911239313Sdim 1912239313Sdim/// \brief Overloaded to match the type's declaration. 1913263508SdimAST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 1914263508Sdim internal::Matcher<Decl>, InnerMatcher, 1) { 1915239313Sdim return onImplicitObjectArgument( 1916263508Sdim anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 1917263508Sdim .matches(Node, Finder, Builder); 1918239313Sdim} 1919239313Sdim 1920239313Sdim/// \brief Matches a DeclRefExpr that refers to a declaration that matches the 1921239313Sdim/// specified matcher. 1922239313Sdim/// 1923239313Sdim/// Example matches x in if(x) 1924243830Sdim/// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 1925243830Sdim/// \code 1926239313Sdim/// bool x; 1927239313Sdim/// if (x) {} 1928243830Sdim/// \endcode 1929239313SdimAST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 1930239313Sdim InnerMatcher) { 1931239313Sdim const Decl *DeclNode = Node.getDecl(); 1932239313Sdim return (DeclNode != NULL && 1933239313Sdim InnerMatcher.matches(*DeclNode, Finder, Builder)); 1934239313Sdim} 1935239313Sdim 1936239313Sdim/// \brief Matches a \c DeclRefExpr that refers to a declaration through a 1937239313Sdim/// specific using shadow declaration. 1938239313Sdim/// 1939239313Sdim/// FIXME: This currently only works for functions. Fix. 1940239313Sdim/// 1941239313Sdim/// Given 1942243830Sdim/// \code 1943239313Sdim/// namespace a { void f() {} } 1944239313Sdim/// using a::f; 1945239313Sdim/// void g() { 1946239313Sdim/// f(); // Matches this .. 1947239313Sdim/// a::f(); // .. but not this. 1948239313Sdim/// } 1949243830Sdim/// \endcode 1950243830Sdim/// declRefExpr(throughUsingDeclaration(anything())) 1951239313Sdim/// matches \c f() 1952239313SdimAST_MATCHER_P(DeclRefExpr, throughUsingDecl, 1953243830Sdim internal::Matcher<UsingShadowDecl>, InnerMatcher) { 1954239313Sdim const NamedDecl *FoundDecl = Node.getFoundDecl(); 1955249423Sdim if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl)) 1956243830Sdim return InnerMatcher.matches(*UsingDecl, Finder, Builder); 1957239313Sdim return false; 1958239313Sdim} 1959239313Sdim 1960239462Sdim/// \brief Matches the Decl of a DeclStmt which has a single declaration. 1961239462Sdim/// 1962239462Sdim/// Given 1963243830Sdim/// \code 1964239462Sdim/// int a, b; 1965239462Sdim/// int c; 1966243830Sdim/// \endcode 1967243830Sdim/// declStmt(hasSingleDecl(anything())) 1968239462Sdim/// matches 'int c;' but not 'int a, b;'. 1969239462SdimAST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 1970239462Sdim if (Node.isSingleDecl()) { 1971239462Sdim const Decl *FoundDecl = Node.getSingleDecl(); 1972239462Sdim return InnerMatcher.matches(*FoundDecl, Finder, Builder); 1973239462Sdim } 1974239462Sdim return false; 1975239462Sdim} 1976239462Sdim 1977239313Sdim/// \brief Matches a variable declaration that has an initializer expression 1978239313Sdim/// that matches the given matcher. 1979239313Sdim/// 1980243830Sdim/// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 1981243830Sdim/// \code 1982239313Sdim/// bool y() { return true; } 1983239313Sdim/// bool x = y(); 1984243830Sdim/// \endcode 1985239313SdimAST_MATCHER_P( 1986239313Sdim VarDecl, hasInitializer, internal::Matcher<Expr>, 1987239313Sdim InnerMatcher) { 1988239313Sdim const Expr *Initializer = Node.getAnyInitializer(); 1989239313Sdim return (Initializer != NULL && 1990239313Sdim InnerMatcher.matches(*Initializer, Finder, Builder)); 1991239313Sdim} 1992239313Sdim 1993239313Sdim/// \brief Checks that a call expression or a constructor call expression has 1994239313Sdim/// a specific number of arguments (including absent default arguments). 1995239313Sdim/// 1996243830Sdim/// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 1997243830Sdim/// \code 1998239313Sdim/// void f(int x, int y); 1999239313Sdim/// f(0, 0); 2000243830Sdim/// \endcode 2001263508SdimAST_POLYMORPHIC_MATCHER_P(argumentCountIs, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 2002263508Sdim CallExpr, CXXConstructExpr), 2003263508Sdim unsigned, N) { 2004239313Sdim return Node.getNumArgs() == N; 2005239313Sdim} 2006239313Sdim 2007239313Sdim/// \brief Matches the n'th argument of a call expression or a constructor 2008239313Sdim/// call expression. 2009239313Sdim/// 2010239313Sdim/// Example matches y in x(y) 2011243830Sdim/// (matcher = callExpr(hasArgument(0, declRefExpr()))) 2012243830Sdim/// \code 2013239313Sdim/// void x(int) { int y; x(y); } 2014243830Sdim/// \endcode 2015239313SdimAST_POLYMORPHIC_MATCHER_P2( 2016263508Sdim hasArgument, 2017263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_2(CallExpr, CXXConstructExpr), 2018263508Sdim unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 2019239313Sdim return (N < Node.getNumArgs() && 2020239313Sdim InnerMatcher.matches( 2021239313Sdim *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 2022239313Sdim} 2023239313Sdim 2024239462Sdim/// \brief Matches declaration statements that contain a specific number of 2025239462Sdim/// declarations. 2026239462Sdim/// 2027239462Sdim/// Example: Given 2028243830Sdim/// \code 2029239462Sdim/// int a, b; 2030239462Sdim/// int c; 2031239462Sdim/// int d = 2, e; 2032243830Sdim/// \endcode 2033239462Sdim/// declCountIs(2) 2034239462Sdim/// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 2035239462SdimAST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 2036243830Sdim return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 2037239462Sdim} 2038239462Sdim 2039239462Sdim/// \brief Matches the n'th declaration of a declaration statement. 2040239462Sdim/// 2041239462Sdim/// Note that this does not work for global declarations because the AST 2042239462Sdim/// breaks up multiple-declaration DeclStmt's into multiple single-declaration 2043239462Sdim/// DeclStmt's. 2044239462Sdim/// Example: Given non-global declarations 2045243830Sdim/// \code 2046239462Sdim/// int a, b = 0; 2047239462Sdim/// int c; 2048239462Sdim/// int d = 2, e; 2049243830Sdim/// \endcode 2050243830Sdim/// declStmt(containsDeclaration( 2051243830Sdim/// 0, varDecl(hasInitializer(anything())))) 2052239462Sdim/// matches only 'int d = 2, e;', and 2053243830Sdim/// declStmt(containsDeclaration(1, varDecl())) 2054243830Sdim/// \code 2055239462Sdim/// matches 'int a, b = 0' as well as 'int d = 2, e;' 2056239462Sdim/// but 'int c;' is not matched. 2057243830Sdim/// \endcode 2058239462SdimAST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 2059239462Sdim internal::Matcher<Decl>, InnerMatcher) { 2060239462Sdim const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 2061239462Sdim if (N >= NumDecls) 2062239462Sdim return false; 2063239462Sdim DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 2064239462Sdim std::advance(Iterator, N); 2065239462Sdim return InnerMatcher.matches(**Iterator, Finder, Builder); 2066239462Sdim} 2067239462Sdim 2068239313Sdim/// \brief Matches a constructor initializer. 2069239313Sdim/// 2070239313Sdim/// Given 2071243830Sdim/// \code 2072239313Sdim/// struct Foo { 2073239313Sdim/// Foo() : foo_(1) { } 2074239313Sdim/// int foo_; 2075239313Sdim/// }; 2076243830Sdim/// \endcode 2077243830Sdim/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer(anything())))) 2078239313Sdim/// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 2079239313SdimAST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 2080239313Sdim internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 2081263508Sdim return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(), 2082263508Sdim Node.init_end(), Finder, Builder); 2083239313Sdim} 2084239313Sdim 2085239313Sdim/// \brief Matches the field declaration of a constructor initializer. 2086239313Sdim/// 2087239313Sdim/// Given 2088243830Sdim/// \code 2089239313Sdim/// struct Foo { 2090239313Sdim/// Foo() : foo_(1) { } 2091239313Sdim/// int foo_; 2092239313Sdim/// }; 2093243830Sdim/// \endcode 2094243830Sdim/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 2095239313Sdim/// forField(hasName("foo_")))))) 2096239313Sdim/// matches Foo 2097239313Sdim/// with forField matching foo_ 2098239313SdimAST_MATCHER_P(CXXCtorInitializer, forField, 2099239313Sdim internal::Matcher<FieldDecl>, InnerMatcher) { 2100239313Sdim const FieldDecl *NodeAsDecl = Node.getMember(); 2101239313Sdim return (NodeAsDecl != NULL && 2102239313Sdim InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 2103239313Sdim} 2104239313Sdim 2105239313Sdim/// \brief Matches the initializer expression of a constructor initializer. 2106239313Sdim/// 2107239313Sdim/// Given 2108243830Sdim/// \code 2109239313Sdim/// struct Foo { 2110239313Sdim/// Foo() : foo_(1) { } 2111239313Sdim/// int foo_; 2112239313Sdim/// }; 2113243830Sdim/// \endcode 2114243830Sdim/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 2115239313Sdim/// withInitializer(integerLiteral(equals(1))))))) 2116239313Sdim/// matches Foo 2117239313Sdim/// with withInitializer matching (1) 2118239313SdimAST_MATCHER_P(CXXCtorInitializer, withInitializer, 2119239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2120239313Sdim const Expr* NodeAsExpr = Node.getInit(); 2121239313Sdim return (NodeAsExpr != NULL && 2122239313Sdim InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 2123239313Sdim} 2124239313Sdim 2125263508Sdim/// \brief Matches a constructor initializer if it is explicitly written in 2126239313Sdim/// code (as opposed to implicitly added by the compiler). 2127239313Sdim/// 2128239313Sdim/// Given 2129243830Sdim/// \code 2130239313Sdim/// struct Foo { 2131239313Sdim/// Foo() { } 2132239313Sdim/// Foo(int) : foo_("A") { } 2133239313Sdim/// string foo_; 2134239313Sdim/// }; 2135243830Sdim/// \endcode 2136243830Sdim/// constructorDecl(hasAnyConstructorInitializer(isWritten())) 2137239313Sdim/// will match Foo(int), but not Foo() 2138239313SdimAST_MATCHER(CXXCtorInitializer, isWritten) { 2139239313Sdim return Node.isWritten(); 2140239313Sdim} 2141239313Sdim 2142239313Sdim/// \brief Matches a constructor declaration that has been implicitly added 2143239313Sdim/// by the compiler (eg. implicit default/copy constructors). 2144239313SdimAST_MATCHER(CXXConstructorDecl, isImplicit) { 2145239313Sdim return Node.isImplicit(); 2146239313Sdim} 2147239313Sdim 2148239313Sdim/// \brief Matches any argument of a call expression or a constructor call 2149239313Sdim/// expression. 2150239313Sdim/// 2151239313Sdim/// Given 2152243830Sdim/// \code 2153239313Sdim/// void x(int, int, int) { int y; x(1, y, 42); } 2154243830Sdim/// \endcode 2155243830Sdim/// callExpr(hasAnyArgument(declRefExpr())) 2156239313Sdim/// matches x(1, y, 42) 2157239313Sdim/// with hasAnyArgument(...) 2158239313Sdim/// matching y 2159263508Sdim/// 2160263508Sdim/// FIXME: Currently this will ignore parentheses and implicit casts on 2161263508Sdim/// the argument before applying the inner matcher. We'll want to remove 2162263508Sdim/// this to allow for greater control by the user once \c ignoreImplicit() 2163263508Sdim/// has been implemented. 2164263508SdimAST_POLYMORPHIC_MATCHER_P(hasAnyArgument, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 2165263508Sdim CallExpr, CXXConstructExpr), 2166263508Sdim internal::Matcher<Expr>, InnerMatcher) { 2167239313Sdim for (unsigned I = 0; I < Node.getNumArgs(); ++I) { 2168263508Sdim BoundNodesTreeBuilder Result(*Builder); 2169263508Sdim if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(), Finder, 2170263508Sdim &Result)) { 2171263508Sdim *Builder = Result; 2172239313Sdim return true; 2173239313Sdim } 2174239313Sdim } 2175239313Sdim return false; 2176239313Sdim} 2177239313Sdim 2178239313Sdim/// \brief Matches the n'th parameter of a function declaration. 2179239313Sdim/// 2180239313Sdim/// Given 2181243830Sdim/// \code 2182239313Sdim/// class X { void f(int x) {} }; 2183243830Sdim/// \endcode 2184243830Sdim/// methodDecl(hasParameter(0, hasType(varDecl()))) 2185239313Sdim/// matches f(int x) {} 2186239313Sdim/// with hasParameter(...) 2187239313Sdim/// matching int x 2188239313SdimAST_MATCHER_P2(FunctionDecl, hasParameter, 2189239313Sdim unsigned, N, internal::Matcher<ParmVarDecl>, 2190239313Sdim InnerMatcher) { 2191239313Sdim return (N < Node.getNumParams() && 2192239313Sdim InnerMatcher.matches( 2193239313Sdim *Node.getParamDecl(N), Finder, Builder)); 2194239313Sdim} 2195239313Sdim 2196239313Sdim/// \brief Matches any parameter of a function declaration. 2197239313Sdim/// 2198239313Sdim/// Does not match the 'this' parameter of a method. 2199239313Sdim/// 2200239313Sdim/// Given 2201243830Sdim/// \code 2202239313Sdim/// class X { void f(int x, int y, int z) {} }; 2203243830Sdim/// \endcode 2204243830Sdim/// methodDecl(hasAnyParameter(hasName("y"))) 2205239313Sdim/// matches f(int x, int y, int z) {} 2206239313Sdim/// with hasAnyParameter(...) 2207239313Sdim/// matching int y 2208239313SdimAST_MATCHER_P(FunctionDecl, hasAnyParameter, 2209239313Sdim internal::Matcher<ParmVarDecl>, InnerMatcher) { 2210263508Sdim return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(), 2211263508Sdim Node.param_end(), Finder, Builder); 2212239313Sdim} 2213239313Sdim 2214249423Sdim/// \brief Matches \c FunctionDecls that have a specific parameter count. 2215249423Sdim/// 2216249423Sdim/// Given 2217249423Sdim/// \code 2218249423Sdim/// void f(int i) {} 2219249423Sdim/// void g(int i, int j) {} 2220249423Sdim/// \endcode 2221249423Sdim/// functionDecl(parameterCountIs(2)) 2222249423Sdim/// matches g(int i, int j) {} 2223249423SdimAST_MATCHER_P(FunctionDecl, parameterCountIs, unsigned, N) { 2224249423Sdim return Node.getNumParams() == N; 2225249423Sdim} 2226249423Sdim 2227239313Sdim/// \brief Matches the return type of a function declaration. 2228239313Sdim/// 2229239313Sdim/// Given: 2230243830Sdim/// \code 2231239313Sdim/// class X { int f() { return 1; } }; 2232243830Sdim/// \endcode 2233243830Sdim/// methodDecl(returns(asString("int"))) 2234239313Sdim/// matches int f() { return 1; } 2235243830SdimAST_MATCHER_P(FunctionDecl, returns, 2236243830Sdim internal::Matcher<QualType>, InnerMatcher) { 2237243830Sdim return InnerMatcher.matches(Node.getResultType(), Finder, Builder); 2238239313Sdim} 2239239313Sdim 2240239462Sdim/// \brief Matches extern "C" function declarations. 2241239462Sdim/// 2242239462Sdim/// Given: 2243243830Sdim/// \code 2244239462Sdim/// extern "C" void f() {} 2245239462Sdim/// extern "C" { void g() {} } 2246239462Sdim/// void h() {} 2247243830Sdim/// \endcode 2248243830Sdim/// functionDecl(isExternC()) 2249239462Sdim/// matches the declaration of f and g, but not the declaration h 2250239462SdimAST_MATCHER(FunctionDecl, isExternC) { 2251239462Sdim return Node.isExternC(); 2252239462Sdim} 2253239462Sdim 2254239313Sdim/// \brief Matches the condition expression of an if statement, for loop, 2255239313Sdim/// or conditional operator. 2256239313Sdim/// 2257239313Sdim/// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 2258243830Sdim/// \code 2259239313Sdim/// if (true) {} 2260243830Sdim/// \endcode 2261263508SdimAST_POLYMORPHIC_MATCHER_P( 2262263508Sdim hasCondition, AST_POLYMORPHIC_SUPPORTED_TYPES_5( 2263263508Sdim IfStmt, ForStmt, WhileStmt, DoStmt, ConditionalOperator), 2264263508Sdim internal::Matcher<Expr>, InnerMatcher) { 2265239313Sdim const Expr *const Condition = Node.getCond(); 2266239313Sdim return (Condition != NULL && 2267239313Sdim InnerMatcher.matches(*Condition, Finder, Builder)); 2268239313Sdim} 2269239313Sdim 2270263508Sdimnamespace internal { 2271263508Sdimstruct NotEqualsBoundNodePredicate { 2272263508Sdim bool operator()(const internal::BoundNodesMap &Nodes) const { 2273263508Sdim return Nodes.getNode(ID) != Node; 2274263508Sdim } 2275263508Sdim std::string ID; 2276263508Sdim ast_type_traits::DynTypedNode Node; 2277263508Sdim}; 2278263508Sdim} // namespace internal 2279263508Sdim 2280263508Sdim/// \brief Matches if a node equals a previously bound node. 2281263508Sdim/// 2282263508Sdim/// Matches a node if it equals the node previously bound to \p ID. 2283263508Sdim/// 2284263508Sdim/// Given 2285263508Sdim/// \code 2286263508Sdim/// class X { int a; int b; }; 2287263508Sdim/// \endcode 2288263508Sdim/// recordDecl( 2289263508Sdim/// has(fieldDecl(hasName("a"), hasType(type().bind("t")))), 2290263508Sdim/// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))) 2291263508Sdim/// matches the class \c X, as \c a and \c b have the same type. 2292263508Sdim/// 2293263508Sdim/// Note that when multiple matches are involved via \c forEach* matchers, 2294263508Sdim/// \c equalsBoundNodes acts as a filter. 2295263508Sdim/// For example: 2296263508Sdim/// compoundStmt( 2297263508Sdim/// forEachDescendant(varDecl().bind("d")), 2298263508Sdim/// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))) 2299263508Sdim/// will trigger a match for each combination of variable declaration 2300263508Sdim/// and reference to that variable declaration within a compound statement. 2301263508SdimAST_POLYMORPHIC_MATCHER_P(equalsBoundNode, AST_POLYMORPHIC_SUPPORTED_TYPES_4( 2302263508Sdim Stmt, Decl, Type, QualType), 2303263508Sdim std::string, ID) { 2304263508Sdim // FIXME: Figure out whether it makes sense to allow this 2305263508Sdim // on any other node types. 2306263508Sdim // For *Loc it probably does not make sense, as those seem 2307263508Sdim // unique. For NestedNameSepcifier it might make sense, as 2308263508Sdim // those also have pointer identity, but I'm not sure whether 2309263508Sdim // they're ever reused. 2310263508Sdim internal::NotEqualsBoundNodePredicate Predicate; 2311263508Sdim Predicate.ID = ID; 2312263508Sdim Predicate.Node = ast_type_traits::DynTypedNode::create(Node); 2313263508Sdim return Builder->removeBindings(Predicate); 2314263508Sdim} 2315263508Sdim 2316239313Sdim/// \brief Matches the condition variable statement in an if statement. 2317239313Sdim/// 2318239313Sdim/// Given 2319243830Sdim/// \code 2320239313Sdim/// if (A* a = GetAPointer()) {} 2321243830Sdim/// \endcode 2322263508Sdim/// hasConditionVariableStatement(...) 2323239313Sdim/// matches 'A* a = GetAPointer()'. 2324239313SdimAST_MATCHER_P(IfStmt, hasConditionVariableStatement, 2325239313Sdim internal::Matcher<DeclStmt>, InnerMatcher) { 2326239313Sdim const DeclStmt* const DeclarationStatement = 2327239313Sdim Node.getConditionVariableDeclStmt(); 2328239313Sdim return DeclarationStatement != NULL && 2329239313Sdim InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 2330239313Sdim} 2331239313Sdim 2332239313Sdim/// \brief Matches the index expression of an array subscript expression. 2333239313Sdim/// 2334239313Sdim/// Given 2335243830Sdim/// \code 2336239313Sdim/// int i[5]; 2337239313Sdim/// void f() { i[1] = 42; } 2338243830Sdim/// \endcode 2339239313Sdim/// arraySubscriptExpression(hasIndex(integerLiteral())) 2340239313Sdim/// matches \c i[1] with the \c integerLiteral() matching \c 1 2341239313SdimAST_MATCHER_P(ArraySubscriptExpr, hasIndex, 2342243830Sdim internal::Matcher<Expr>, InnerMatcher) { 2343239313Sdim if (const Expr* Expression = Node.getIdx()) 2344243830Sdim return InnerMatcher.matches(*Expression, Finder, Builder); 2345239313Sdim return false; 2346239313Sdim} 2347239313Sdim 2348239313Sdim/// \brief Matches the base expression of an array subscript expression. 2349239313Sdim/// 2350239313Sdim/// Given 2351243830Sdim/// \code 2352239313Sdim/// int i[5]; 2353239313Sdim/// void f() { i[1] = 42; } 2354243830Sdim/// \endcode 2355243830Sdim/// arraySubscriptExpression(hasBase(implicitCastExpr( 2356243830Sdim/// hasSourceExpression(declRefExpr())))) 2357243830Sdim/// matches \c i[1] with the \c declRefExpr() matching \c i 2358239313SdimAST_MATCHER_P(ArraySubscriptExpr, hasBase, 2359243830Sdim internal::Matcher<Expr>, InnerMatcher) { 2360239313Sdim if (const Expr* Expression = Node.getBase()) 2361243830Sdim return InnerMatcher.matches(*Expression, Finder, Builder); 2362239313Sdim return false; 2363239313Sdim} 2364239313Sdim 2365239313Sdim/// \brief Matches a 'for', 'while', or 'do while' statement that has 2366239313Sdim/// a given body. 2367239313Sdim/// 2368239313Sdim/// Given 2369243830Sdim/// \code 2370239313Sdim/// for (;;) {} 2371243830Sdim/// \endcode 2372243830Sdim/// hasBody(compoundStmt()) 2373239313Sdim/// matches 'for (;;) {}' 2374243830Sdim/// with compoundStmt() 2375239313Sdim/// matching '{}' 2376263508SdimAST_POLYMORPHIC_MATCHER_P( 2377263508Sdim hasBody, AST_POLYMORPHIC_SUPPORTED_TYPES_3(DoStmt, ForStmt, WhileStmt), 2378263508Sdim internal::Matcher<Stmt>, InnerMatcher) { 2379239313Sdim const Stmt *const Statement = Node.getBody(); 2380239313Sdim return (Statement != NULL && 2381239313Sdim InnerMatcher.matches(*Statement, Finder, Builder)); 2382239313Sdim} 2383239313Sdim 2384239313Sdim/// \brief Matches compound statements where at least one substatement matches 2385239313Sdim/// a given matcher. 2386239313Sdim/// 2387239313Sdim/// Given 2388243830Sdim/// \code 2389239313Sdim/// { {}; 1+2; } 2390243830Sdim/// \endcode 2391243830Sdim/// hasAnySubstatement(compoundStmt()) 2392239313Sdim/// matches '{ {}; 1+2; }' 2393243830Sdim/// with compoundStmt() 2394239313Sdim/// matching '{}' 2395239313SdimAST_MATCHER_P(CompoundStmt, hasAnySubstatement, 2396239313Sdim internal::Matcher<Stmt>, InnerMatcher) { 2397263508Sdim return matchesFirstInPointerRange(InnerMatcher, Node.body_begin(), 2398263508Sdim Node.body_end(), Finder, Builder); 2399239313Sdim} 2400239313Sdim 2401239313Sdim/// \brief Checks that a compound statement contains a specific number of 2402239313Sdim/// child statements. 2403239313Sdim/// 2404239313Sdim/// Example: Given 2405243830Sdim/// \code 2406239313Sdim/// { for (;;) {} } 2407243830Sdim/// \endcode 2408243830Sdim/// compoundStmt(statementCountIs(0))) 2409239313Sdim/// matches '{}' 2410239313Sdim/// but does not match the outer compound statement. 2411239313SdimAST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 2412239313Sdim return Node.size() == N; 2413239313Sdim} 2414239313Sdim 2415239313Sdim/// \brief Matches literals that are equal to the given value. 2416239313Sdim/// 2417239313Sdim/// Example matches true (matcher = boolLiteral(equals(true))) 2418243830Sdim/// \code 2419239313Sdim/// true 2420243830Sdim/// \endcode 2421239313Sdim/// 2422239313Sdim/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 2423239313Sdim/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 2424239313Sdimtemplate <typename ValueT> 2425239313Sdiminternal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 2426239313Sdimequals(const ValueT &Value) { 2427239313Sdim return internal::PolymorphicMatcherWithParam1< 2428239313Sdim internal::ValueEqualsMatcher, 2429239313Sdim ValueT>(Value); 2430239313Sdim} 2431239313Sdim 2432239313Sdim/// \brief Matches the operator Name of operator expressions (binary or 2433239313Sdim/// unary). 2434239313Sdim/// 2435239313Sdim/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 2436243830Sdim/// \code 2437239313Sdim/// !(a || b) 2438243830Sdim/// \endcode 2439263508SdimAST_POLYMORPHIC_MATCHER_P(hasOperatorName, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 2440263508Sdim BinaryOperator, UnaryOperator), 2441263508Sdim std::string, Name) { 2442239313Sdim return Name == Node.getOpcodeStr(Node.getOpcode()); 2443239313Sdim} 2444239313Sdim 2445239313Sdim/// \brief Matches the left hand side of binary operator expressions. 2446239313Sdim/// 2447239313Sdim/// Example matches a (matcher = binaryOperator(hasLHS())) 2448243830Sdim/// \code 2449239313Sdim/// a || b 2450243830Sdim/// \endcode 2451239313SdimAST_MATCHER_P(BinaryOperator, hasLHS, 2452239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2453239313Sdim Expr *LeftHandSide = Node.getLHS(); 2454239313Sdim return (LeftHandSide != NULL && 2455239313Sdim InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 2456239313Sdim} 2457239313Sdim 2458239313Sdim/// \brief Matches the right hand side of binary operator expressions. 2459239313Sdim/// 2460239313Sdim/// Example matches b (matcher = binaryOperator(hasRHS())) 2461243830Sdim/// \code 2462239313Sdim/// a || b 2463243830Sdim/// \endcode 2464239313SdimAST_MATCHER_P(BinaryOperator, hasRHS, 2465239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2466239313Sdim Expr *RightHandSide = Node.getRHS(); 2467239313Sdim return (RightHandSide != NULL && 2468239313Sdim InnerMatcher.matches(*RightHandSide, Finder, Builder)); 2469239313Sdim} 2470239313Sdim 2471239313Sdim/// \brief Matches if either the left hand side or the right hand side of a 2472239313Sdim/// binary operator matches. 2473239313Sdiminline internal::Matcher<BinaryOperator> hasEitherOperand( 2474239313Sdim const internal::Matcher<Expr> &InnerMatcher) { 2475239313Sdim return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 2476239313Sdim} 2477239313Sdim 2478239313Sdim/// \brief Matches if the operand of a unary operator matches. 2479239313Sdim/// 2480243830Sdim/// Example matches true (matcher = hasUnaryOperand(boolLiteral(equals(true)))) 2481243830Sdim/// \code 2482239313Sdim/// !true 2483243830Sdim/// \endcode 2484239313SdimAST_MATCHER_P(UnaryOperator, hasUnaryOperand, 2485239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2486239313Sdim const Expr * const Operand = Node.getSubExpr(); 2487239313Sdim return (Operand != NULL && 2488239313Sdim InnerMatcher.matches(*Operand, Finder, Builder)); 2489239313Sdim} 2490239313Sdim 2491239313Sdim/// \brief Matches if the cast's source expression matches the given matcher. 2492239313Sdim/// 2493239313Sdim/// Example: matches "a string" (matcher = 2494243830Sdim/// hasSourceExpression(constructExpr())) 2495243830Sdim/// \code 2496239313Sdim/// class URL { URL(string); }; 2497239313Sdim/// URL url = "a string"; 2498239313SdimAST_MATCHER_P(CastExpr, hasSourceExpression, 2499239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2500239313Sdim const Expr* const SubExpression = Node.getSubExpr(); 2501239313Sdim return (SubExpression != NULL && 2502239313Sdim InnerMatcher.matches(*SubExpression, Finder, Builder)); 2503239313Sdim} 2504239313Sdim 2505239313Sdim/// \brief Matches casts whose destination type matches a given matcher. 2506239313Sdim/// 2507239313Sdim/// (Note: Clang's AST refers to other conversions as "casts" too, and calls 2508239313Sdim/// actual casts "explicit" casts.) 2509239313SdimAST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 2510239313Sdim internal::Matcher<QualType>, InnerMatcher) { 2511239313Sdim const QualType NodeType = Node.getTypeAsWritten(); 2512239313Sdim return InnerMatcher.matches(NodeType, Finder, Builder); 2513239313Sdim} 2514239313Sdim 2515239313Sdim/// \brief Matches implicit casts whose destination type matches a given 2516239313Sdim/// matcher. 2517239313Sdim/// 2518239313Sdim/// FIXME: Unit test this matcher 2519239313SdimAST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 2520239313Sdim internal::Matcher<QualType>, InnerMatcher) { 2521239313Sdim return InnerMatcher.matches(Node.getType(), Finder, Builder); 2522239313Sdim} 2523239313Sdim 2524239313Sdim/// \brief Matches the true branch expression of a conditional operator. 2525239313Sdim/// 2526239313Sdim/// Example matches a 2527243830Sdim/// \code 2528239313Sdim/// condition ? a : b 2529243830Sdim/// \endcode 2530239313SdimAST_MATCHER_P(ConditionalOperator, hasTrueExpression, 2531239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2532239313Sdim Expr *Expression = Node.getTrueExpr(); 2533239313Sdim return (Expression != NULL && 2534239313Sdim InnerMatcher.matches(*Expression, Finder, Builder)); 2535239313Sdim} 2536239313Sdim 2537239313Sdim/// \brief Matches the false branch expression of a conditional operator. 2538239313Sdim/// 2539239313Sdim/// Example matches b 2540243830Sdim/// \code 2541239313Sdim/// condition ? a : b 2542243830Sdim/// \endcode 2543239313SdimAST_MATCHER_P(ConditionalOperator, hasFalseExpression, 2544239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2545239313Sdim Expr *Expression = Node.getFalseExpr(); 2546239313Sdim return (Expression != NULL && 2547239313Sdim InnerMatcher.matches(*Expression, Finder, Builder)); 2548239313Sdim} 2549239313Sdim 2550239313Sdim/// \brief Matches if a declaration has a body attached. 2551239313Sdim/// 2552239313Sdim/// Example matches A, va, fa 2553243830Sdim/// \code 2554239313Sdim/// class A {}; 2555239313Sdim/// class B; // Doesn't match, as it has no body. 2556239313Sdim/// int va; 2557239313Sdim/// extern int vb; // Doesn't match, as it doesn't define the variable. 2558239313Sdim/// void fa() {} 2559239313Sdim/// void fb(); // Doesn't match, as it has no body. 2560243830Sdim/// \endcode 2561239313Sdim/// 2562239313Sdim/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 2563263508SdimAST_POLYMORPHIC_MATCHER(isDefinition, AST_POLYMORPHIC_SUPPORTED_TYPES_3( 2564263508Sdim TagDecl, VarDecl, FunctionDecl)) { 2565249423Sdim return Node.isThisDeclarationADefinition(); 2566239313Sdim} 2567239313Sdim 2568239313Sdim/// \brief Matches the class declaration that the given method declaration 2569239313Sdim/// belongs to. 2570239313Sdim/// 2571239313Sdim/// FIXME: Generalize this for other kinds of declarations. 2572239313Sdim/// FIXME: What other kind of declarations would we need to generalize 2573239313Sdim/// this to? 2574239313Sdim/// 2575239313Sdim/// Example matches A() in the last line 2576243830Sdim/// (matcher = constructExpr(hasDeclaration(methodDecl( 2577239313Sdim/// ofClass(hasName("A")))))) 2578243830Sdim/// \code 2579239313Sdim/// class A { 2580239313Sdim/// public: 2581239313Sdim/// A(); 2582239313Sdim/// }; 2583239313Sdim/// A a = A(); 2584243830Sdim/// \endcode 2585239313SdimAST_MATCHER_P(CXXMethodDecl, ofClass, 2586239313Sdim internal::Matcher<CXXRecordDecl>, InnerMatcher) { 2587239313Sdim const CXXRecordDecl *Parent = Node.getParent(); 2588239313Sdim return (Parent != NULL && 2589239313Sdim InnerMatcher.matches(*Parent, Finder, Builder)); 2590239313Sdim} 2591239313Sdim 2592251662Sdim/// \brief Matches if the given method declaration is virtual. 2593251662Sdim/// 2594251662Sdim/// Given 2595251662Sdim/// \code 2596251662Sdim/// class A { 2597251662Sdim/// public: 2598251662Sdim/// virtual void x(); 2599251662Sdim/// }; 2600251662Sdim/// \endcode 2601251662Sdim/// matches A::x 2602251662SdimAST_MATCHER(CXXMethodDecl, isVirtual) { 2603251662Sdim return Node.isVirtual(); 2604251662Sdim} 2605251662Sdim 2606263508Sdim/// \brief Matches if the given method declaration is const. 2607263508Sdim/// 2608263508Sdim/// Given 2609263508Sdim/// \code 2610263508Sdim/// struct A { 2611263508Sdim/// void foo() const; 2612263508Sdim/// void bar(); 2613263508Sdim/// }; 2614263508Sdim/// \endcode 2615263508Sdim/// 2616263508Sdim/// methodDecl(isConst()) matches A::foo() but not A::bar() 2617263508SdimAST_MATCHER(CXXMethodDecl, isConst) { 2618263508Sdim return Node.isConst(); 2619263508Sdim} 2620263508Sdim 2621251662Sdim/// \brief Matches if the given method declaration overrides another method. 2622251662Sdim/// 2623251662Sdim/// Given 2624251662Sdim/// \code 2625251662Sdim/// class A { 2626251662Sdim/// public: 2627251662Sdim/// virtual void x(); 2628251662Sdim/// }; 2629251662Sdim/// class B : public A { 2630251662Sdim/// public: 2631251662Sdim/// virtual void x(); 2632251662Sdim/// }; 2633251662Sdim/// \endcode 2634251662Sdim/// matches B::x 2635251662SdimAST_MATCHER(CXXMethodDecl, isOverride) { 2636251662Sdim return Node.size_overridden_methods() > 0; 2637251662Sdim} 2638251662Sdim 2639239313Sdim/// \brief Matches member expressions that are called with '->' as opposed 2640239313Sdim/// to '.'. 2641239313Sdim/// 2642239313Sdim/// Member calls on the implicit this pointer match as called with '->'. 2643239313Sdim/// 2644239313Sdim/// Given 2645243830Sdim/// \code 2646239313Sdim/// class Y { 2647239313Sdim/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 2648239313Sdim/// int a; 2649239313Sdim/// static int b; 2650239313Sdim/// }; 2651243830Sdim/// \endcode 2652243830Sdim/// memberExpr(isArrow()) 2653239313Sdim/// matches this->x, x, y.x, a, this->b 2654249423SdimAST_MATCHER(MemberExpr, isArrow) { 2655249423Sdim return Node.isArrow(); 2656239313Sdim} 2657239313Sdim 2658239313Sdim/// \brief Matches QualType nodes that are of integer type. 2659239313Sdim/// 2660239313Sdim/// Given 2661243830Sdim/// \code 2662239313Sdim/// void a(int); 2663239313Sdim/// void b(long); 2664239313Sdim/// void c(double); 2665243830Sdim/// \endcode 2666243830Sdim/// functionDecl(hasAnyParameter(hasType(isInteger()))) 2667239313Sdim/// matches "a(int)", "b(long)", but not "c(double)". 2668239313SdimAST_MATCHER(QualType, isInteger) { 2669239313Sdim return Node->isIntegerType(); 2670239313Sdim} 2671239313Sdim 2672239313Sdim/// \brief Matches QualType nodes that are const-qualified, i.e., that 2673239313Sdim/// include "top-level" const. 2674239313Sdim/// 2675239313Sdim/// Given 2676243830Sdim/// \code 2677239313Sdim/// void a(int); 2678239313Sdim/// void b(int const); 2679239313Sdim/// void c(const int); 2680239313Sdim/// void d(const int*); 2681239313Sdim/// void e(int const) {}; 2682243830Sdim/// \endcode 2683243830Sdim/// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 2684239313Sdim/// matches "void b(int const)", "void c(const int)" and 2685239313Sdim/// "void e(int const) {}". It does not match d as there 2686239313Sdim/// is no top-level const on the parameter type "const int *". 2687249423SdimAST_MATCHER(QualType, isConstQualified) { 2688249423Sdim return Node.isConstQualified(); 2689239313Sdim} 2690239313Sdim 2691249423Sdim/// \brief Matches QualType nodes that have local CV-qualifiers attached to 2692249423Sdim/// the node, not hidden within a typedef. 2693249423Sdim/// 2694249423Sdim/// Given 2695249423Sdim/// \code 2696249423Sdim/// typedef const int const_int; 2697249423Sdim/// const_int i; 2698249423Sdim/// int *const j; 2699249423Sdim/// int *volatile k; 2700249423Sdim/// int m; 2701249423Sdim/// \endcode 2702249423Sdim/// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k. 2703249423Sdim/// \c i is const-qualified but the qualifier is not local. 2704249423SdimAST_MATCHER(QualType, hasLocalQualifiers) { 2705249423Sdim return Node.hasLocalQualifiers(); 2706249423Sdim} 2707249423Sdim 2708239313Sdim/// \brief Matches a member expression where the member is matched by a 2709239313Sdim/// given matcher. 2710239313Sdim/// 2711239313Sdim/// Given 2712243830Sdim/// \code 2713239313Sdim/// struct { int first, second; } first, second; 2714239313Sdim/// int i(second.first); 2715239313Sdim/// int j(first.second); 2716243830Sdim/// \endcode 2717243830Sdim/// memberExpr(member(hasName("first"))) 2718239313Sdim/// matches second.first 2719239313Sdim/// but not first.second (because the member name there is "second"). 2720239313SdimAST_MATCHER_P(MemberExpr, member, 2721239313Sdim internal::Matcher<ValueDecl>, InnerMatcher) { 2722239313Sdim return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 2723239313Sdim} 2724239313Sdim 2725239313Sdim/// \brief Matches a member expression where the object expression is 2726239313Sdim/// matched by a given matcher. 2727239313Sdim/// 2728239313Sdim/// Given 2729243830Sdim/// \code 2730239313Sdim/// struct X { int m; }; 2731239313Sdim/// void f(X x) { x.m; m; } 2732243830Sdim/// \endcode 2733243830Sdim/// memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))) 2734239313Sdim/// matches "x.m" and "m" 2735239313Sdim/// with hasObjectExpression(...) 2736239313Sdim/// matching "x" and the implicit object expression of "m" which has type X*. 2737239313SdimAST_MATCHER_P(MemberExpr, hasObjectExpression, 2738239313Sdim internal::Matcher<Expr>, InnerMatcher) { 2739239313Sdim return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 2740239313Sdim} 2741239313Sdim 2742239313Sdim/// \brief Matches any using shadow declaration. 2743239313Sdim/// 2744239313Sdim/// Given 2745243830Sdim/// \code 2746239313Sdim/// namespace X { void b(); } 2747239313Sdim/// using X::b; 2748243830Sdim/// \endcode 2749239313Sdim/// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 2750239313Sdim/// matches \code using X::b \endcode 2751239313SdimAST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 2752243830Sdim internal::Matcher<UsingShadowDecl>, InnerMatcher) { 2753263508Sdim return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(), 2754263508Sdim Node.shadow_end(), Finder, Builder); 2755239313Sdim} 2756239313Sdim 2757239313Sdim/// \brief Matches a using shadow declaration where the target declaration is 2758239313Sdim/// matched by the given matcher. 2759239313Sdim/// 2760239313Sdim/// Given 2761243830Sdim/// \code 2762239313Sdim/// namespace X { int a; void b(); } 2763239313Sdim/// using X::a; 2764239313Sdim/// using X::b; 2765243830Sdim/// \endcode 2766243830Sdim/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 2767239313Sdim/// matches \code using X::b \endcode 2768239313Sdim/// but not \code using X::a \endcode 2769239313SdimAST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 2770243830Sdim internal::Matcher<NamedDecl>, InnerMatcher) { 2771243830Sdim return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 2772239313Sdim} 2773239313Sdim 2774239313Sdim/// \brief Matches template instantiations of function, class, or static 2775239313Sdim/// member variable template instantiations. 2776239313Sdim/// 2777239313Sdim/// Given 2778243830Sdim/// \code 2779239313Sdim/// template <typename T> class X {}; class A {}; X<A> x; 2780243830Sdim/// \endcode 2781239313Sdim/// or 2782243830Sdim/// \code 2783239313Sdim/// template <typename T> class X {}; class A {}; template class X<A>; 2784243830Sdim/// \endcode 2785243830Sdim/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2786239313Sdim/// matches the template instantiation of X<A>. 2787239313Sdim/// 2788239313Sdim/// But given 2789243830Sdim/// \code 2790243830Sdim/// template <typename T> class X {}; class A {}; 2791239313Sdim/// template <> class X<A> {}; X<A> x; 2792243830Sdim/// \endcode 2793243830Sdim/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2794239313Sdim/// does not match, as X<A> is an explicit template specialization. 2795239313Sdim/// 2796239313Sdim/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2797263508SdimAST_POLYMORPHIC_MATCHER( 2798263508Sdim isTemplateInstantiation, 2799263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_3(FunctionDecl, VarDecl, CXXRecordDecl)) { 2800249423Sdim return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation || 2801249423Sdim Node.getTemplateSpecializationKind() == 2802249423Sdim TSK_ExplicitInstantiationDefinition); 2803239313Sdim} 2804239313Sdim 2805243830Sdim/// \brief Matches explicit template specializations of function, class, or 2806243830Sdim/// static member variable template instantiations. 2807243830Sdim/// 2808243830Sdim/// Given 2809243830Sdim/// \code 2810243830Sdim/// template<typename T> void A(T t) { } 2811243830Sdim/// template<> void A(int N) { } 2812243830Sdim/// \endcode 2813243830Sdim/// functionDecl(isExplicitTemplateSpecialization()) 2814243830Sdim/// matches the specialization A<int>(). 2815243830Sdim/// 2816243830Sdim/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2817263508SdimAST_POLYMORPHIC_MATCHER( 2818263508Sdim isExplicitTemplateSpecialization, 2819263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_3(FunctionDecl, VarDecl, CXXRecordDecl)) { 2820249423Sdim return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization); 2821243830Sdim} 2822243830Sdim 2823243830Sdim/// \brief Matches \c TypeLocs for which the given inner 2824243830Sdim/// QualType-matcher matches. 2825243830Sdiminline internal::BindableMatcher<TypeLoc> loc( 2826243830Sdim const internal::Matcher<QualType> &InnerMatcher) { 2827243830Sdim return internal::BindableMatcher<TypeLoc>( 2828243830Sdim new internal::TypeLocTypeMatcher(InnerMatcher)); 2829243830Sdim} 2830243830Sdim 2831243830Sdim/// \brief Matches builtin Types. 2832243830Sdim/// 2833243830Sdim/// Given 2834243830Sdim/// \code 2835243830Sdim/// struct A {}; 2836243830Sdim/// A a; 2837243830Sdim/// int b; 2838243830Sdim/// float c; 2839243830Sdim/// bool d; 2840243830Sdim/// \endcode 2841243830Sdim/// builtinType() 2842243830Sdim/// matches "int b", "float c" and "bool d" 2843243830SdimAST_TYPE_MATCHER(BuiltinType, builtinType); 2844243830Sdim 2845243830Sdim/// \brief Matches all kinds of arrays. 2846243830Sdim/// 2847243830Sdim/// Given 2848243830Sdim/// \code 2849243830Sdim/// int a[] = { 2, 3 }; 2850243830Sdim/// int b[4]; 2851243830Sdim/// void f() { int c[a[0]]; } 2852243830Sdim/// \endcode 2853243830Sdim/// arrayType() 2854243830Sdim/// matches "int a[]", "int b[4]" and "int c[a[0]]"; 2855243830SdimAST_TYPE_MATCHER(ArrayType, arrayType); 2856243830Sdim 2857243830Sdim/// \brief Matches C99 complex types. 2858243830Sdim/// 2859243830Sdim/// Given 2860243830Sdim/// \code 2861243830Sdim/// _Complex float f; 2862243830Sdim/// \endcode 2863243830Sdim/// complexType() 2864243830Sdim/// matches "_Complex float f" 2865243830SdimAST_TYPE_MATCHER(ComplexType, complexType); 2866243830Sdim 2867243830Sdim/// \brief Matches arrays and C99 complex types that have a specific element 2868243830Sdim/// type. 2869243830Sdim/// 2870243830Sdim/// Given 2871243830Sdim/// \code 2872243830Sdim/// struct A {}; 2873243830Sdim/// A a[7]; 2874243830Sdim/// int b[7]; 2875243830Sdim/// \endcode 2876243830Sdim/// arrayType(hasElementType(builtinType())) 2877243830Sdim/// matches "int b[7]" 2878243830Sdim/// 2879243830Sdim/// Usable as: Matcher<ArrayType>, Matcher<ComplexType> 2880263508SdimAST_TYPELOC_TRAVERSE_MATCHER( 2881263508Sdim hasElementType, getElement, 2882263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_2(ArrayType, ComplexType)); 2883243830Sdim 2884243830Sdim/// \brief Matches C arrays with a specified constant size. 2885243830Sdim/// 2886243830Sdim/// Given 2887243830Sdim/// \code 2888243830Sdim/// void() { 2889243830Sdim/// int a[2]; 2890243830Sdim/// int b[] = { 2, 3 }; 2891243830Sdim/// int c[b[0]]; 2892243830Sdim/// } 2893243830Sdim/// \endcode 2894243830Sdim/// constantArrayType() 2895243830Sdim/// matches "int a[2]" 2896243830SdimAST_TYPE_MATCHER(ConstantArrayType, constantArrayType); 2897243830Sdim 2898243830Sdim/// \brief Matches \c ConstantArrayType nodes that have the specified size. 2899243830Sdim/// 2900243830Sdim/// Given 2901243830Sdim/// \code 2902243830Sdim/// int a[42]; 2903243830Sdim/// int b[2 * 21]; 2904243830Sdim/// int c[41], d[43]; 2905243830Sdim/// \endcode 2906243830Sdim/// constantArrayType(hasSize(42)) 2907243830Sdim/// matches "int a[42]" and "int b[2 * 21]" 2908243830SdimAST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) { 2909243830Sdim return Node.getSize() == N; 2910243830Sdim} 2911243830Sdim 2912243830Sdim/// \brief Matches C++ arrays whose size is a value-dependent expression. 2913243830Sdim/// 2914243830Sdim/// Given 2915243830Sdim/// \code 2916243830Sdim/// template<typename T, int Size> 2917243830Sdim/// class array { 2918243830Sdim/// T data[Size]; 2919243830Sdim/// }; 2920243830Sdim/// \endcode 2921243830Sdim/// dependentSizedArrayType 2922243830Sdim/// matches "T data[Size]" 2923243830SdimAST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType); 2924243830Sdim 2925243830Sdim/// \brief Matches C arrays with unspecified size. 2926243830Sdim/// 2927243830Sdim/// Given 2928243830Sdim/// \code 2929243830Sdim/// int a[] = { 2, 3 }; 2930243830Sdim/// int b[42]; 2931243830Sdim/// void f(int c[]) { int d[a[0]]; }; 2932243830Sdim/// \endcode 2933243830Sdim/// incompleteArrayType() 2934243830Sdim/// matches "int a[]" and "int c[]" 2935243830SdimAST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType); 2936243830Sdim 2937243830Sdim/// \brief Matches C arrays with a specified size that is not an 2938243830Sdim/// integer-constant-expression. 2939243830Sdim/// 2940243830Sdim/// Given 2941243830Sdim/// \code 2942243830Sdim/// void f() { 2943243830Sdim/// int a[] = { 2, 3 } 2944243830Sdim/// int b[42]; 2945243830Sdim/// int c[a[0]]; 2946243830Sdim/// \endcode 2947243830Sdim/// variableArrayType() 2948243830Sdim/// matches "int c[a[0]]" 2949243830SdimAST_TYPE_MATCHER(VariableArrayType, variableArrayType); 2950243830Sdim 2951243830Sdim/// \brief Matches \c VariableArrayType nodes that have a specific size 2952243830Sdim/// expression. 2953243830Sdim/// 2954243830Sdim/// Given 2955243830Sdim/// \code 2956243830Sdim/// void f(int b) { 2957243830Sdim/// int a[b]; 2958243830Sdim/// } 2959243830Sdim/// \endcode 2960243830Sdim/// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( 2961243830Sdim/// varDecl(hasName("b"))))))) 2962243830Sdim/// matches "int a[b]" 2963243830SdimAST_MATCHER_P(VariableArrayType, hasSizeExpr, 2964243830Sdim internal::Matcher<Expr>, InnerMatcher) { 2965243830Sdim return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder); 2966243830Sdim} 2967243830Sdim 2968243830Sdim/// \brief Matches atomic types. 2969243830Sdim/// 2970243830Sdim/// Given 2971243830Sdim/// \code 2972243830Sdim/// _Atomic(int) i; 2973243830Sdim/// \endcode 2974243830Sdim/// atomicType() 2975243830Sdim/// matches "_Atomic(int) i" 2976243830SdimAST_TYPE_MATCHER(AtomicType, atomicType); 2977243830Sdim 2978243830Sdim/// \brief Matches atomic types with a specific value type. 2979243830Sdim/// 2980243830Sdim/// Given 2981243830Sdim/// \code 2982243830Sdim/// _Atomic(int) i; 2983243830Sdim/// _Atomic(float) f; 2984243830Sdim/// \endcode 2985243830Sdim/// atomicType(hasValueType(isInteger())) 2986243830Sdim/// matches "_Atomic(int) i" 2987243830Sdim/// 2988243830Sdim/// Usable as: Matcher<AtomicType> 2989263508SdimAST_TYPELOC_TRAVERSE_MATCHER(hasValueType, getValue, 2990263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_1(AtomicType)); 2991243830Sdim 2992243830Sdim/// \brief Matches types nodes representing C++11 auto types. 2993243830Sdim/// 2994243830Sdim/// Given: 2995243830Sdim/// \code 2996243830Sdim/// auto n = 4; 2997243830Sdim/// int v[] = { 2, 3 } 2998243830Sdim/// for (auto i : v) { } 2999243830Sdim/// \endcode 3000243830Sdim/// autoType() 3001243830Sdim/// matches "auto n" and "auto i" 3002243830SdimAST_TYPE_MATCHER(AutoType, autoType); 3003243830Sdim 3004243830Sdim/// \brief Matches \c AutoType nodes where the deduced type is a specific type. 3005243830Sdim/// 3006243830Sdim/// Note: There is no \c TypeLoc for the deduced type and thus no 3007243830Sdim/// \c getDeducedLoc() matcher. 3008243830Sdim/// 3009243830Sdim/// Given 3010243830Sdim/// \code 3011243830Sdim/// auto a = 1; 3012243830Sdim/// auto b = 2.0; 3013243830Sdim/// \endcode 3014243830Sdim/// autoType(hasDeducedType(isInteger())) 3015243830Sdim/// matches "auto a" 3016243830Sdim/// 3017243830Sdim/// Usable as: Matcher<AutoType> 3018263508SdimAST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType, 3019263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_1(AutoType)); 3020243830Sdim 3021243830Sdim/// \brief Matches \c FunctionType nodes. 3022243830Sdim/// 3023243830Sdim/// Given 3024243830Sdim/// \code 3025243830Sdim/// int (*f)(int); 3026243830Sdim/// void g(); 3027243830Sdim/// \endcode 3028243830Sdim/// functionType() 3029243830Sdim/// matches "int (*f)(int)" and the type of "g". 3030243830SdimAST_TYPE_MATCHER(FunctionType, functionType); 3031243830Sdim 3032249423Sdim/// \brief Matches \c ParenType nodes. 3033249423Sdim/// 3034249423Sdim/// Given 3035249423Sdim/// \code 3036249423Sdim/// int (*ptr_to_array)[4]; 3037249423Sdim/// int *array_of_ptrs[4]; 3038249423Sdim/// \endcode 3039249423Sdim/// 3040249423Sdim/// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not 3041249423Sdim/// \c array_of_ptrs. 3042249423SdimAST_TYPE_MATCHER(ParenType, parenType); 3043249423Sdim 3044249423Sdim/// \brief Matches \c ParenType nodes where the inner type is a specific type. 3045249423Sdim/// 3046249423Sdim/// Given 3047249423Sdim/// \code 3048249423Sdim/// int (*ptr_to_array)[4]; 3049249423Sdim/// int (*ptr_to_func)(int); 3050249423Sdim/// \endcode 3051249423Sdim/// 3052249423Sdim/// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches 3053249423Sdim/// \c ptr_to_func but not \c ptr_to_array. 3054249423Sdim/// 3055249423Sdim/// Usable as: Matcher<ParenType> 3056263508SdimAST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType, 3057263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_1(ParenType)); 3058249423Sdim 3059243830Sdim/// \brief Matches block pointer types, i.e. types syntactically represented as 3060243830Sdim/// "void (^)(int)". 3061243830Sdim/// 3062243830Sdim/// The \c pointee is always required to be a \c FunctionType. 3063243830SdimAST_TYPE_MATCHER(BlockPointerType, blockPointerType); 3064243830Sdim 3065243830Sdim/// \brief Matches member pointer types. 3066243830Sdim/// Given 3067243830Sdim/// \code 3068243830Sdim/// struct A { int i; } 3069243830Sdim/// A::* ptr = A::i; 3070243830Sdim/// \endcode 3071243830Sdim/// memberPointerType() 3072243830Sdim/// matches "A::* ptr" 3073243830SdimAST_TYPE_MATCHER(MemberPointerType, memberPointerType); 3074243830Sdim 3075243830Sdim/// \brief Matches pointer types. 3076243830Sdim/// 3077243830Sdim/// Given 3078243830Sdim/// \code 3079243830Sdim/// int *a; 3080243830Sdim/// int &b = *a; 3081243830Sdim/// int c = 5; 3082243830Sdim/// \endcode 3083243830Sdim/// pointerType() 3084243830Sdim/// matches "int *a" 3085243830SdimAST_TYPE_MATCHER(PointerType, pointerType); 3086243830Sdim 3087249423Sdim/// \brief Matches both lvalue and rvalue reference types. 3088243830Sdim/// 3089243830Sdim/// Given 3090243830Sdim/// \code 3091243830Sdim/// int *a; 3092243830Sdim/// int &b = *a; 3093249423Sdim/// int &&c = 1; 3094249423Sdim/// auto &d = b; 3095249423Sdim/// auto &&e = c; 3096249423Sdim/// auto &&f = 2; 3097249423Sdim/// int g = 5; 3098243830Sdim/// \endcode 3099249423Sdim/// 3100249423Sdim/// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f. 3101243830SdimAST_TYPE_MATCHER(ReferenceType, referenceType); 3102243830Sdim 3103249423Sdim/// \brief Matches lvalue reference types. 3104249423Sdim/// 3105249423Sdim/// Given: 3106249423Sdim/// \code 3107249423Sdim/// int *a; 3108249423Sdim/// int &b = *a; 3109249423Sdim/// int &&c = 1; 3110249423Sdim/// auto &d = b; 3111249423Sdim/// auto &&e = c; 3112249423Sdim/// auto &&f = 2; 3113249423Sdim/// int g = 5; 3114249423Sdim/// \endcode 3115249423Sdim/// 3116249423Sdim/// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is 3117249423Sdim/// matched since the type is deduced as int& by reference collapsing rules. 3118249423SdimAST_TYPE_MATCHER(LValueReferenceType, lValueReferenceType); 3119249423Sdim 3120249423Sdim/// \brief Matches rvalue reference types. 3121249423Sdim/// 3122249423Sdim/// Given: 3123249423Sdim/// \code 3124249423Sdim/// int *a; 3125249423Sdim/// int &b = *a; 3126249423Sdim/// int &&c = 1; 3127249423Sdim/// auto &d = b; 3128249423Sdim/// auto &&e = c; 3129249423Sdim/// auto &&f = 2; 3130249423Sdim/// int g = 5; 3131249423Sdim/// \endcode 3132249423Sdim/// 3133249423Sdim/// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not 3134249423Sdim/// matched as it is deduced to int& by reference collapsing rules. 3135249423SdimAST_TYPE_MATCHER(RValueReferenceType, rValueReferenceType); 3136249423Sdim 3137243830Sdim/// \brief Narrows PointerType (and similar) matchers to those where the 3138243830Sdim/// \c pointee matches a given matcher. 3139243830Sdim/// 3140243830Sdim/// Given 3141243830Sdim/// \code 3142243830Sdim/// int *a; 3143243830Sdim/// int const *b; 3144243830Sdim/// float const *f; 3145243830Sdim/// \endcode 3146243830Sdim/// pointerType(pointee(isConstQualified(), isInteger())) 3147243830Sdim/// matches "int const *b" 3148243830Sdim/// 3149243830Sdim/// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>, 3150243830Sdim/// Matcher<PointerType>, Matcher<ReferenceType> 3151263508SdimAST_TYPELOC_TRAVERSE_MATCHER( 3152263508Sdim pointee, getPointee, 3153263508Sdim AST_POLYMORPHIC_SUPPORTED_TYPES_4(BlockPointerType, MemberPointerType, 3154263508Sdim PointerType, ReferenceType)); 3155243830Sdim 3156243830Sdim/// \brief Matches typedef types. 3157243830Sdim/// 3158243830Sdim/// Given 3159243830Sdim/// \code 3160243830Sdim/// typedef int X; 3161243830Sdim/// \endcode 3162243830Sdim/// typedefType() 3163243830Sdim/// matches "typedef int X" 3164243830SdimAST_TYPE_MATCHER(TypedefType, typedefType); 3165243830Sdim 3166249423Sdim/// \brief Matches template specialization types. 3167249423Sdim/// 3168249423Sdim/// Given 3169249423Sdim/// \code 3170249423Sdim/// template <typename T> 3171249423Sdim/// class C { }; 3172249423Sdim/// 3173249423Sdim/// template class C<int>; // A 3174249423Sdim/// C<char> var; // B 3175249423Sdim/// \code 3176249423Sdim/// 3177249423Sdim/// \c templateSpecializationType() matches the type of the explicit 3178249423Sdim/// instantiation in \c A and the type of the variable declaration in \c B. 3179249423SdimAST_TYPE_MATCHER(TemplateSpecializationType, templateSpecializationType); 3180249423Sdim 3181263508Sdim/// \brief Matches types nodes representing unary type transformations. 3182263508Sdim/// 3183263508Sdim/// Given: 3184263508Sdim/// \code 3185263508Sdim/// typedef __underlying_type(T) type; 3186263508Sdim/// \endcode 3187263508Sdim/// unaryTransformType() 3188263508Sdim/// matches "__underlying_type(T)" 3189263508SdimAST_TYPE_MATCHER(UnaryTransformType, unaryTransformType); 3190263508Sdim 3191249423Sdim/// \brief Matches record types (e.g. structs, classes). 3192249423Sdim/// 3193249423Sdim/// Given 3194249423Sdim/// \code 3195249423Sdim/// class C {}; 3196249423Sdim/// struct S {}; 3197249423Sdim/// 3198249423Sdim/// C c; 3199249423Sdim/// S s; 3200249423Sdim/// \code 3201249423Sdim/// 3202249423Sdim/// \c recordType() matches the type of the variable declarations of both \c c 3203249423Sdim/// and \c s. 3204249423SdimAST_TYPE_MATCHER(RecordType, recordType); 3205249423Sdim 3206249423Sdim/// \brief Matches types specified with an elaborated type keyword or with a 3207249423Sdim/// qualified name. 3208249423Sdim/// 3209249423Sdim/// Given 3210249423Sdim/// \code 3211249423Sdim/// namespace N { 3212249423Sdim/// namespace M { 3213249423Sdim/// class D {}; 3214249423Sdim/// } 3215249423Sdim/// } 3216249423Sdim/// class C {}; 3217249423Sdim/// 3218249423Sdim/// class C c; 3219249423Sdim/// N::M::D d; 3220249423Sdim/// \code 3221249423Sdim/// 3222249423Sdim/// \c elaboratedType() matches the type of the variable declarations of both 3223249423Sdim/// \c c and \c d. 3224249423SdimAST_TYPE_MATCHER(ElaboratedType, elaboratedType); 3225249423Sdim 3226249423Sdim/// \brief Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier, 3227249423Sdim/// matches \c InnerMatcher if the qualifier exists. 3228249423Sdim/// 3229249423Sdim/// Given 3230249423Sdim/// \code 3231249423Sdim/// namespace N { 3232249423Sdim/// namespace M { 3233249423Sdim/// class D {}; 3234249423Sdim/// } 3235249423Sdim/// } 3236249423Sdim/// N::M::D d; 3237249423Sdim/// \code 3238249423Sdim/// 3239249423Sdim/// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))) 3240249423Sdim/// matches the type of the variable declaration of \c d. 3241249423SdimAST_MATCHER_P(ElaboratedType, hasQualifier, 3242249423Sdim internal::Matcher<NestedNameSpecifier>, InnerMatcher) { 3243249423Sdim if (const NestedNameSpecifier *Qualifier = Node.getQualifier()) 3244249423Sdim return InnerMatcher.matches(*Qualifier, Finder, Builder); 3245249423Sdim 3246249423Sdim return false; 3247243830Sdim} 3248243830Sdim 3249249423Sdim/// \brief Matches ElaboratedTypes whose named type matches \c InnerMatcher. 3250249423Sdim/// 3251249423Sdim/// Given 3252249423Sdim/// \code 3253249423Sdim/// namespace N { 3254249423Sdim/// namespace M { 3255249423Sdim/// class D {}; 3256249423Sdim/// } 3257249423Sdim/// } 3258249423Sdim/// N::M::D d; 3259249423Sdim/// \code 3260249423Sdim/// 3261249423Sdim/// \c elaboratedType(namesType(recordType( 3262249423Sdim/// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable 3263249423Sdim/// declaration of \c d. 3264249423SdimAST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>, 3265249423Sdim InnerMatcher) { 3266249423Sdim return InnerMatcher.matches(Node.getNamedType(), Finder, Builder); 3267249423Sdim} 3268249423Sdim 3269249423Sdim/// \brief Matches declarations whose declaration context, interpreted as a 3270249423Sdim/// Decl, matches \c InnerMatcher. 3271249423Sdim/// 3272249423Sdim/// Given 3273249423Sdim/// \code 3274249423Sdim/// namespace N { 3275249423Sdim/// namespace M { 3276249423Sdim/// class D {}; 3277249423Sdim/// } 3278249423Sdim/// } 3279249423Sdim/// \code 3280249423Sdim/// 3281249423Sdim/// \c recordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the 3282249423Sdim/// declaration of \c class \c D. 3283249423SdimAST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) { 3284249423Sdim return InnerMatcher.matches(*Decl::castFromDeclContext(Node.getDeclContext()), 3285249423Sdim Finder, Builder); 3286249423Sdim} 3287249423Sdim 3288243830Sdim/// \brief Matches nested name specifiers. 3289243830Sdim/// 3290243830Sdim/// Given 3291243830Sdim/// \code 3292243830Sdim/// namespace ns { 3293243830Sdim/// struct A { static void f(); }; 3294243830Sdim/// void A::f() {} 3295243830Sdim/// void g() { A::f(); } 3296243830Sdim/// } 3297243830Sdim/// ns::A a; 3298243830Sdim/// \endcode 3299243830Sdim/// nestedNameSpecifier() 3300243830Sdim/// matches "ns::" and both "A::" 3301243830Sdimconst internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 3302243830Sdim 3303243830Sdim/// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 3304243830Sdimconst internal::VariadicAllOfMatcher< 3305243830Sdim NestedNameSpecifierLoc> nestedNameSpecifierLoc; 3306243830Sdim 3307243830Sdim/// \brief Matches \c NestedNameSpecifierLocs for which the given inner 3308243830Sdim/// NestedNameSpecifier-matcher matches. 3309243830Sdiminline internal::BindableMatcher<NestedNameSpecifierLoc> loc( 3310243830Sdim const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 3311243830Sdim return internal::BindableMatcher<NestedNameSpecifierLoc>( 3312243830Sdim new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 3313243830Sdim InnerMatcher)); 3314243830Sdim} 3315243830Sdim 3316243830Sdim/// \brief Matches nested name specifiers that specify a type matching the 3317243830Sdim/// given \c QualType matcher without qualifiers. 3318243830Sdim/// 3319243830Sdim/// Given 3320243830Sdim/// \code 3321243830Sdim/// struct A { struct B { struct C {}; }; }; 3322243830Sdim/// A::B::C c; 3323243830Sdim/// \endcode 3324243830Sdim/// nestedNameSpecifier(specifiesType(hasDeclaration(recordDecl(hasName("A"))))) 3325243830Sdim/// matches "A::" 3326243830SdimAST_MATCHER_P(NestedNameSpecifier, specifiesType, 3327243830Sdim internal::Matcher<QualType>, InnerMatcher) { 3328243830Sdim if (Node.getAsType() == NULL) 3329243830Sdim return false; 3330243830Sdim return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 3331243830Sdim} 3332243830Sdim 3333243830Sdim/// \brief Matches nested name specifier locs that specify a type matching the 3334243830Sdim/// given \c TypeLoc. 3335243830Sdim/// 3336243830Sdim/// Given 3337243830Sdim/// \code 3338243830Sdim/// struct A { struct B { struct C {}; }; }; 3339243830Sdim/// A::B::C c; 3340243830Sdim/// \endcode 3341243830Sdim/// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type( 3342243830Sdim/// hasDeclaration(recordDecl(hasName("A"))))))) 3343243830Sdim/// matches "A::" 3344243830SdimAST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, 3345243830Sdim internal::Matcher<TypeLoc>, InnerMatcher) { 3346243830Sdim return InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder); 3347243830Sdim} 3348243830Sdim 3349243830Sdim/// \brief Matches on the prefix of a \c NestedNameSpecifier. 3350243830Sdim/// 3351243830Sdim/// Given 3352243830Sdim/// \code 3353243830Sdim/// struct A { struct B { struct C {}; }; }; 3354243830Sdim/// A::B::C c; 3355243830Sdim/// \endcode 3356243830Sdim/// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 3357243830Sdim/// matches "A::" 3358249423SdimAST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix, 3359249423Sdim internal::Matcher<NestedNameSpecifier>, InnerMatcher, 3360249423Sdim 0) { 3361249423Sdim NestedNameSpecifier *NextNode = Node.getPrefix(); 3362249423Sdim if (NextNode == NULL) 3363249423Sdim return false; 3364249423Sdim return InnerMatcher.matches(*NextNode, Finder, Builder); 3365243830Sdim} 3366243830Sdim 3367243830Sdim/// \brief Matches on the prefix of a \c NestedNameSpecifierLoc. 3368243830Sdim/// 3369243830Sdim/// Given 3370243830Sdim/// \code 3371243830Sdim/// struct A { struct B { struct C {}; }; }; 3372243830Sdim/// A::B::C c; 3373243830Sdim/// \endcode 3374243830Sdim/// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 3375243830Sdim/// matches "A::" 3376249423SdimAST_MATCHER_P_OVERLOAD(NestedNameSpecifierLoc, hasPrefix, 3377249423Sdim internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher, 3378249423Sdim 1) { 3379249423Sdim NestedNameSpecifierLoc NextNode = Node.getPrefix(); 3380249423Sdim if (!NextNode) 3381249423Sdim return false; 3382249423Sdim return InnerMatcher.matches(NextNode, Finder, Builder); 3383243830Sdim} 3384243830Sdim 3385243830Sdim/// \brief Matches nested name specifiers that specify a namespace matching the 3386243830Sdim/// given namespace matcher. 3387243830Sdim/// 3388243830Sdim/// Given 3389243830Sdim/// \code 3390243830Sdim/// namespace ns { struct A {}; } 3391243830Sdim/// ns::A a; 3392243830Sdim/// \endcode 3393243830Sdim/// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 3394243830Sdim/// matches "ns::" 3395243830SdimAST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 3396243830Sdim internal::Matcher<NamespaceDecl>, InnerMatcher) { 3397243830Sdim if (Node.getAsNamespace() == NULL) 3398243830Sdim return false; 3399243830Sdim return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 3400243830Sdim} 3401243830Sdim 3402249423Sdim/// \brief Overloads for the \c equalsNode matcher. 3403249423Sdim/// FIXME: Implement for other node types. 3404249423Sdim/// @{ 3405249423Sdim 3406249423Sdim/// \brief Matches if a node equals another node. 3407249423Sdim/// 3408249423Sdim/// \c Decl has pointer identity in the AST. 3409249423SdimAST_MATCHER_P_OVERLOAD(Decl, equalsNode, Decl*, Other, 0) { 3410249423Sdim return &Node == Other; 3411249423Sdim} 3412249423Sdim/// \brief Matches if a node equals another node. 3413249423Sdim/// 3414249423Sdim/// \c Stmt has pointer identity in the AST. 3415249423Sdim/// 3416249423SdimAST_MATCHER_P_OVERLOAD(Stmt, equalsNode, Stmt*, Other, 1) { 3417249423Sdim return &Node == Other; 3418249423Sdim} 3419249423Sdim 3420249423Sdim/// @} 3421249423Sdim 3422263508Sdim/// \brief Matches each case or default statement belonging to the given switch 3423263508Sdim/// statement. This matcher may produce multiple matches. 3424263508Sdim/// 3425263508Sdim/// Given 3426263508Sdim/// \code 3427263508Sdim/// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } } 3428263508Sdim/// \endcode 3429263508Sdim/// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s") 3430263508Sdim/// matches four times, with "c" binding each of "case 1:", "case 2:", 3431263508Sdim/// "case 3:" and "case 4:", and "s" respectively binding "switch (1)", 3432263508Sdim/// "switch (1)", "switch (2)" and "switch (2)". 3433263508SdimAST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>, 3434263508Sdim InnerMatcher) { 3435263508Sdim BoundNodesTreeBuilder Result; 3436263508Sdim // FIXME: getSwitchCaseList() does not necessarily guarantee a stable 3437263508Sdim // iteration order. We should use the more general iterating matchers once 3438263508Sdim // they are capable of expressing this matcher (for example, it should ignore 3439263508Sdim // case statements belonging to nested switch statements). 3440263508Sdim bool Matched = false; 3441263508Sdim for (const SwitchCase *SC = Node.getSwitchCaseList(); SC; 3442263508Sdim SC = SC->getNextSwitchCase()) { 3443263508Sdim BoundNodesTreeBuilder CaseBuilder(*Builder); 3444263508Sdim bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder); 3445263508Sdim if (CaseMatched) { 3446263508Sdim Matched = true; 3447263508Sdim Result.addMatch(CaseBuilder); 3448263508Sdim } 3449263508Sdim } 3450263508Sdim *Builder = Result; 3451263508Sdim return Matched; 3452263508Sdim} 3453263508Sdim 3454263508Sdim/// \brief Matches each constructor initializer in a constructor definition. 3455263508Sdim/// 3456263508Sdim/// Given 3457263508Sdim/// \code 3458263508Sdim/// class A { A() : i(42), j(42) {} int i; int j; }; 3459263508Sdim/// \endcode 3460263508Sdim/// constructorDecl(forEachConstructorInitializer(forField(decl().bind("x")))) 3461263508Sdim/// will trigger two matches, binding for 'i' and 'j' respectively. 3462263508SdimAST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer, 3463263508Sdim internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 3464263508Sdim BoundNodesTreeBuilder Result; 3465263508Sdim bool Matched = false; 3466263508Sdim for (CXXConstructorDecl::init_const_iterator I = Node.init_begin(), 3467263508Sdim E = Node.init_end(); 3468263508Sdim I != E; ++I) { 3469263508Sdim BoundNodesTreeBuilder InitBuilder(*Builder); 3470263508Sdim if (InnerMatcher.matches(**I, Finder, &InitBuilder)) { 3471263508Sdim Matched = true; 3472263508Sdim Result.addMatch(InitBuilder); 3473263508Sdim } 3474263508Sdim } 3475263508Sdim *Builder = Result; 3476263508Sdim return Matched; 3477263508Sdim} 3478263508Sdim 3479263508Sdim/// \brief If the given case statement does not use the GNU case range 3480263508Sdim/// extension, matches the constant given in the statement. 3481263508Sdim/// 3482263508Sdim/// Given 3483263508Sdim/// \code 3484263508Sdim/// switch (1) { case 1: case 1+1: case 3 ... 4: ; } 3485263508Sdim/// \endcode 3486263508Sdim/// caseStmt(hasCaseConstant(integerLiteral())) 3487263508Sdim/// matches "case 1:" 3488263508SdimAST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>, 3489263508Sdim InnerMatcher) { 3490263508Sdim if (Node.getRHS()) 3491263508Sdim return false; 3492263508Sdim 3493263508Sdim return InnerMatcher.matches(*Node.getLHS(), Finder, Builder); 3494263508Sdim} 3495263508Sdim 3496239313Sdim} // end namespace ast_matchers 3497239313Sdim} // end namespace clang 3498239313Sdim 3499239313Sdim#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 3500