ASTMatchers.h revision a7564433191601cb8851196b8dde39392c9c05ee
1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===// 2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// The LLVM Compiler Infrastructure 4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// This file is distributed under the University of Illinois Open Source 6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// License. See LICENSE.TXT for details. 7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//===----------------------------------------------------------------------===// 9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// This file implements matchers to be used together with the MatchFinder to 11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// match AST nodes. 12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Matchers are created by generator functions, which can be combined in 14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// a functional in-language DSL to express queries over the C++ AST. 1599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com// 16f65a55c8d0744b95be29a65d06b59b22b012f37bgabor@google.com// For example, to match a class with a certain name, one would call: 17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// recordDecl(hasName("MyClass")) 18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// which returns a matcher that can be used to find all AST nodes that declare 19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// a class named 'MyClass'. 20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// For more complicated match expressions we're often interested in accessing 22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// multiple parts of the matched AST nodes once a match is found. In that case, 23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// use the id(...) matcher around the match expressions that match the nodes 24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// you want to access. 25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 2607f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org// For example, when we're interested in child classes of a certain class, we 2751f892d349df9ed408f5a0e6e012667e5eae5f8bgabor@google.com// would write: 28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// recordDecl(hasName("MyClass"), hasChild(id("child", recordDecl()))) 29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// When the match is found via the MatchFinder, a user provided callback will 30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// be called with a BoundNodes instance that contains a mapping from the 31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// strings that we provided for the id(...) calls to the nodes that were 32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// matched. 33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// In the given example, each time our matcher finds a match we get a callback 34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// where "child" is bound to the CXXRecordDecl node of the matching child 35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// class declaration. 36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// See ASTMatchersInternal.h for a more in-depth explanation of the 38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// implementation details of the matcher framework. 39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// See ASTMatchFinder.h for how to use the generated matchers to run over 41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// an AST. 42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//===----------------------------------------------------------------------===// 44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/AST/DeclTemplate.h" 49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/ASTMatchers/ASTMatchersInternal.h" 50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/ASTMatchers/ASTMatchersMacros.h" 51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "llvm/ADT/Twine.h" 52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "llvm/Support/Regex.h" 53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <iterator> 54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace clang { 56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace ast_matchers { 57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Maps string IDs to AST nodes matched by parts of a matcher. 59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// The bound nodes are generated by calling \c bind("id") on the node matchers 61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// of the nodes we want to access later. 62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// The instances of BoundNodes are created by \c MatchFinder when the user's 64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// callbacks are executed every time a match is found. 65f65a55c8d0744b95be29a65d06b59b22b012f37bgabor@google.comclass BoundNodes { 66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgpublic: 67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org /// \brief Returns the AST node bound to \c ID. 68f65a55c8d0744b95be29a65d06b59b22b012f37bgabor@google.com /// 69179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org /// Returns NULL if there was no node bound to \c ID or if there is a node but 70179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org /// it cannot be converted to the specified type. 71179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org template <typename T> 72179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const T *getNodeAs(StringRef ID) const { 7395e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org return MyBoundNodes.getNodeAs<T>(ID); 7495e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org } 75179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 7695e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org /// \brief Deprecated. Please use \c getNodeAs instead. 7795e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org /// @{ 7895e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org template <typename T> 79917b88dd720b6e658c1fd7812bc61c605f315124gabor@google.com const T *getDeclAs(StringRef ID) const { 80917b88dd720b6e658c1fd7812bc61c605f315124gabor@google.com return getNodeAs<T>(ID); 81179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 8295e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org template <typename T> 83179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const T *getStmtAs(StringRef ID) const { 84179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return getNodeAs<T>(ID); 85179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 86179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org /// @} 87179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 88179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgprivate: 89179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org /// \brief Create BoundNodes from a pre-filled map of bindings. 90179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org BoundNodes(internal::BoundNodesMap &MyBoundNodes) 91179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org : MyBoundNodes(MyBoundNodes) {} 92179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 93179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org internal::BoundNodesMap MyBoundNodes; 94179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 9595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org friend class internal::BoundNodesTree; 9695e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org}; 97179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 98179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief If the provided matcher matches a node, binds the node to \c ID. 99179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 100179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// FIXME: Do we want to support this now that we have bind()? 101179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgtemplate <typename T> 102179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orginternal::Matcher<T> id(const std::string &ID, 103179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const internal::BindableMatcher<T> &InnerMatcher) { 104179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return InnerMatcher.bind(ID); 10595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org} 1061511be6edb54b6ade2bfad94256f76bc191e92ecdgrogan@chromium.org 107179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Types of matchers for the top-level classes in the AST class 108179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// hierarchy. 109179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// @{ 110179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgtypedef internal::Matcher<Decl> DeclarationMatcher; 111179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgtypedef internal::Matcher<QualType> TypeMatcher; 112179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgtypedef internal::Matcher<Stmt> StatementMatcher; 113179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgtypedef internal::Matcher<NestedNameSpecifier> NestedNameSpecifierMatcher; 114179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgtypedef internal::Matcher<NestedNameSpecifierLoc> NestedNameSpecifierLocMatcher; 115179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// @} 116179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 117179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Matches any node. 11807f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org/// 119179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// Useful when another matcher requires a child matcher, but there's no 120179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// additional constraint. This will often be used with an explicit conversion 12107f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org/// to an \c internal::Matcher<> type such as \c TypeMatcher. 122179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 123179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g., 124179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \code 125179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// "int* p" and "void f()" in 126179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// int* p; 12707f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org/// void f(); 128179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \endcode 129179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 130179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// Usable as: Any Matcher 13199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.cominline internal::PolymorphicMatcherWithParam0<internal::TrueMatcher> anything() { 13299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com return internal::PolymorphicMatcherWithParam0<internal::TrueMatcher>(); 13399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com} 13499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com 13599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com/// \brief Matches declarations. 13699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com/// 13799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com/// Examples matches \c X, \c C, and the friend declaration inside \c C; 138179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \code 139179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// void X(); 140179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// class C { 141179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// friend X; 142179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// }; 143179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \endcode 144179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgconst internal::VariadicDynCastAllOfMatcher<Decl, Decl> decl; 145179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 146179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Matches a declaration of anything that could have a name. 147179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 148179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// Example matches \c X, \c S, the anonymous union type, \c i, and \c U; 149179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \code 150179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// typedef int X; 151179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// struct S { 152179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// union { 153179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// int i; 154179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// } U; 155179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// }; 156179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \endcode 157179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgconst internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl; 158179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 159179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Matches C++ class declarations. 160179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 161179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// Example matches \c X, \c Z 162179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \code 163179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// class X; 164179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// template<class T> class Z {}; 165179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \endcode 166179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgconst internal::VariadicDynCastAllOfMatcher< 167179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Decl, 168179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org CXXRecordDecl> recordDecl; 169179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 170179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Matches C++ class template declarations. 171179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// 172179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// Example matches \c Z 173179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \code 174179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// template<class T> class Z {}; 175179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \endcode 176179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgconst internal::VariadicDynCastAllOfMatcher< 177179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Decl, 178179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ClassTemplateDecl> classTemplateDecl; 179179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 18095e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org/// \brief Matches C++ class template specializations. 18195e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org/// 18295e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org/// Given 18395e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org/// \code 18495e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org/// template<typename T> class A {}; 18595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org/// template<> class A<double> {}; 186179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// A<int> a; 187179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \endcode 188179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// classTemplateSpecializationDecl() 1895fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com/// matches the specializations \c A<int> and \c A<double> 190179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgconst internal::VariadicDynCastAllOfMatcher< 191179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Decl, 192179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ClassTemplateSpecializationDecl> classTemplateSpecializationDecl; 19345b9940be332834440bd5299419f396e38085ebehans@chromium.org 194179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// \brief Matches classTemplateSpecializations that have at least one 195179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org/// TemplateArgument matching the given InnerMatcher. 196/// 197/// Given 198/// \code 199/// template<typename T> class A {}; 200/// template<> class A<double> {}; 201/// A<int> a; 202/// \endcode 203/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 204/// refersToType(asString("int")))) 205/// matches the specialization \c A<int> 206AST_MATCHER_P(ClassTemplateSpecializationDecl, hasAnyTemplateArgument, 207 internal::Matcher<TemplateArgument>, InnerMatcher) { 208 const TemplateArgumentList &List = Node.getTemplateArgs(); 209 for (unsigned i = 0; i < List.size(); ++i) { 210 if (InnerMatcher.matches(List.get(i), Finder, Builder)) 211 return true; 212 } 213 return false; 214} 215 216/// \brief Matches expressions that match InnerMatcher after any implicit casts 217/// are stripped off. 218/// 219/// Parentheses and explicit casts are not discarded. 220/// Given 221/// \code 222/// int arr[5]; 223/// int a = 0; 224/// char b = 0; 225/// const int c = a; 226/// int *d = arr; 227/// long e = (long) 0l; 228/// \endcode 229/// The matchers 230/// \code 231/// varDecl(hasInitializer(ignoringImpCasts(integerLiteral()))) 232/// varDecl(hasInitializer(ignoringImpCasts(declRefExpr()))) 233/// \endcode 234/// would match the declarations for a, b, c, and d, but not e. 235/// While 236/// \code 237/// varDecl(hasInitializer(integerLiteral())) 238/// varDecl(hasInitializer(declRefExpr())) 239/// \endcode 240/// only match the declarations for b, c, and d. 241AST_MATCHER_P(Expr, ignoringImpCasts, 242 internal::Matcher<Expr>, InnerMatcher) { 243 return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder); 244} 245 246/// \brief Matches expressions that match InnerMatcher after parentheses and 247/// casts are stripped off. 248/// 249/// Implicit and non-C Style casts are also discarded. 250/// Given 251/// \code 252/// int a = 0; 253/// char b = (0); 254/// void* c = reinterpret_cast<char*>(0); 255/// char d = char(0); 256/// \endcode 257/// The matcher 258/// varDecl(hasInitializer(ignoringParenCasts(integerLiteral()))) 259/// would match the declarations for a, b, c, and d. 260/// while 261/// varDecl(hasInitializer(integerLiteral())) 262/// only match the declaration for a. 263AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) { 264 return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder); 265} 266 267/// \brief Matches expressions that match InnerMatcher after implicit casts and 268/// parentheses are stripped off. 269/// 270/// Explicit casts are not discarded. 271/// Given 272/// \code 273/// int arr[5]; 274/// int a = 0; 275/// char b = (0); 276/// const int c = a; 277/// int *d = (arr); 278/// long e = ((long) 0l); 279/// \endcode 280/// The matchers 281/// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral()))) 282/// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr()))) 283/// would match the declarations for a, b, c, and d, but not e. 284/// while 285/// varDecl(hasInitializer(integerLiteral())) 286/// varDecl(hasInitializer(declRefExpr())) 287/// would only match the declaration for a. 288AST_MATCHER_P(Expr, ignoringParenImpCasts, 289 internal::Matcher<Expr>, InnerMatcher) { 290 return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder); 291} 292 293/// \brief Matches classTemplateSpecializations where the n'th TemplateArgument 294/// matches the given InnerMatcher. 295/// 296/// Given 297/// \code 298/// template<typename T, typename U> class A {}; 299/// A<bool, int> b; 300/// A<int, bool> c; 301/// \endcode 302/// classTemplateSpecializationDecl(hasTemplateArgument( 303/// 1, refersToType(asString("int")))) 304/// matches the specialization \c A<bool, int> 305AST_MATCHER_P2(ClassTemplateSpecializationDecl, hasTemplateArgument, 306 unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) { 307 const TemplateArgumentList &List = Node.getTemplateArgs(); 308 if (List.size() <= N) 309 return false; 310 return InnerMatcher.matches(List.get(N), Finder, Builder); 311} 312 313/// \brief Matches a TemplateArgument that refers to a certain type. 314/// 315/// Given 316/// \code 317/// struct X {}; 318/// template<typename T> struct A {}; 319/// A<X> a; 320/// \endcode 321/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 322/// refersToType(class(hasName("X"))))) 323/// matches the specialization \c A<X> 324AST_MATCHER_P(TemplateArgument, refersToType, 325 internal::Matcher<QualType>, InnerMatcher) { 326 if (Node.getKind() != TemplateArgument::Type) 327 return false; 328 return InnerMatcher.matches(Node.getAsType(), Finder, Builder); 329} 330 331/// \brief Matches a TemplateArgument that refers to a certain declaration. 332/// 333/// Given 334/// \code 335/// template<typename T> struct A {}; 336/// struct B { B* next; }; 337/// A<&B::next> a; 338/// \endcode 339/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 340/// refersToDeclaration(fieldDecl(hasName("next")))) 341/// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 342/// \c B::next 343AST_MATCHER_P(TemplateArgument, refersToDeclaration, 344 internal::Matcher<Decl>, InnerMatcher) { 345 if (const Decl *Declaration = Node.getAsDecl()) 346 return InnerMatcher.matches(*Declaration, Finder, Builder); 347 return false; 348} 349 350/// \brief Matches C++ constructor declarations. 351/// 352/// Example matches Foo::Foo() and Foo::Foo(int) 353/// \code 354/// class Foo { 355/// public: 356/// Foo(); 357/// Foo(int); 358/// int DoSomething(); 359/// }; 360/// \endcode 361const internal::VariadicDynCastAllOfMatcher< 362 Decl, 363 CXXConstructorDecl> constructorDecl; 364 365/// \brief Matches explicit C++ destructor declarations. 366/// 367/// Example matches Foo::~Foo() 368/// \code 369/// class Foo { 370/// public: 371/// virtual ~Foo(); 372/// }; 373/// \endcode 374const internal::VariadicDynCastAllOfMatcher< 375 Decl, 376 CXXDestructorDecl> destructorDecl; 377 378/// \brief Matches enum declarations. 379/// 380/// Example matches X 381/// \code 382/// enum X { 383/// A, B, C 384/// }; 385/// \endcode 386const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl; 387 388/// \brief Matches enum constants. 389/// 390/// Example matches A, B, C 391/// \code 392/// enum X { 393/// A, B, C 394/// }; 395/// \endcode 396const internal::VariadicDynCastAllOfMatcher< 397 Decl, 398 EnumConstantDecl> enumConstantDecl; 399 400/// \brief Matches method declarations. 401/// 402/// Example matches y 403/// \code 404/// class X { void y() }; 405/// \endcode 406const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> methodDecl; 407 408/// \brief Matches variable declarations. 409/// 410/// Note: this does not match declarations of member variables, which are 411/// "field" declarations in Clang parlance. 412/// 413/// Example matches a 414/// \code 415/// int a; 416/// \endcode 417const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl; 418 419/// \brief Matches field declarations. 420/// 421/// Given 422/// \code 423/// class X { int m; }; 424/// \endcode 425/// fieldDecl() 426/// matches 'm'. 427const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl; 428 429/// \brief Matches function declarations. 430/// 431/// Example matches f 432/// \code 433/// void f(); 434/// \endcode 435const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl; 436 437/// \brief Matches C++ function template declarations. 438/// 439/// Example matches f 440/// \code 441/// template<class T> void f(T t) {} 442/// \endcode 443const internal::VariadicDynCastAllOfMatcher< 444 Decl, 445 FunctionTemplateDecl> functionTemplateDecl; 446 447/// \brief Matches statements. 448/// 449/// Given 450/// \code 451/// { ++a; } 452/// \endcode 453/// stmt() 454/// matches both the compound statement '{ ++a; }' and '++a'. 455const internal::VariadicDynCastAllOfMatcher<Stmt, Stmt> stmt; 456 457/// \brief Matches declaration statements. 458/// 459/// Given 460/// \code 461/// int a; 462/// \endcode 463/// declStmt() 464/// matches 'int a'. 465const internal::VariadicDynCastAllOfMatcher< 466 Stmt, 467 DeclStmt> declStmt; 468 469/// \brief Matches member expressions. 470/// 471/// Given 472/// \code 473/// class Y { 474/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 475/// int a; static int b; 476/// }; 477/// \endcode 478/// memberExpr() 479/// matches this->x, x, y.x, a, this->b 480const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr; 481 482/// \brief Matches call expressions. 483/// 484/// Example matches x.y() and y() 485/// \code 486/// X x; 487/// x.y(); 488/// y(); 489/// \endcode 490const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr; 491 492/// \brief Matches member call expressions. 493/// 494/// Example matches x.y() 495/// \code 496/// X x; 497/// x.y(); 498/// \endcode 499const internal::VariadicDynCastAllOfMatcher< 500 Stmt, 501 CXXMemberCallExpr> memberCallExpr; 502 503/// \brief Matches init list expressions. 504/// 505/// Given 506/// \code 507/// int a[] = { 1, 2 }; 508/// struct B { int x, y; }; 509/// B b = { 5, 6 }; 510/// \endcode 511/// initList() 512/// matches "{ 1, 2 }" and "{ 5, 6 }" 513const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr; 514 515/// \brief Matches using declarations. 516/// 517/// Given 518/// \code 519/// namespace X { int x; } 520/// using X::x; 521/// \endcode 522/// usingDecl() 523/// matches \code using X::x \endcode 524const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl; 525 526/// \brief Matches constructor call expressions (including implicit ones). 527/// 528/// Example matches string(ptr, n) and ptr within arguments of f 529/// (matcher = constructExpr()) 530/// \code 531/// void f(const string &a, const string &b); 532/// char *ptr; 533/// int n; 534/// f(string(ptr, n), ptr); 535/// \endcode 536const internal::VariadicDynCastAllOfMatcher< 537 Stmt, 538 CXXConstructExpr> constructExpr; 539 540/// \brief Matches nodes where temporaries are created. 541/// 542/// Example matches FunctionTakesString(GetStringByValue()) 543/// (matcher = bindTemporaryExpr()) 544/// \code 545/// FunctionTakesString(GetStringByValue()); 546/// FunctionTakesStringByPointer(GetStringPointer()); 547/// \endcode 548const internal::VariadicDynCastAllOfMatcher< 549 Stmt, 550 CXXBindTemporaryExpr> bindTemporaryExpr; 551 552/// \brief Matches nodes where temporaries are materialized. 553/// 554/// Example: Given 555/// \code 556/// struct T {void func()}; 557/// T f(); 558/// void g(T); 559/// \endcode 560/// materializeTemporaryExpr() matches 'f()' in these statements 561/// \code 562/// T u(f()); 563/// g(f()); 564/// \endcode 565/// but does not match 566/// \code 567/// f(); 568/// f().func(); 569/// \endcode 570const internal::VariadicDynCastAllOfMatcher< 571 Stmt, 572 MaterializeTemporaryExpr> materializeTemporaryExpr; 573 574/// \brief Matches new expressions. 575/// 576/// Given 577/// \code 578/// new X; 579/// \endcode 580/// newExpr() 581/// matches 'new X'. 582const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> newExpr; 583 584/// \brief Matches delete expressions. 585/// 586/// Given 587/// \code 588/// delete X; 589/// \endcode 590/// deleteExpr() 591/// matches 'delete X'. 592const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> deleteExpr; 593 594/// \brief Matches array subscript expressions. 595/// 596/// Given 597/// \code 598/// int i = a[1]; 599/// \endcode 600/// arraySubscriptExpr() 601/// matches "a[1]" 602const internal::VariadicDynCastAllOfMatcher< 603 Stmt, 604 ArraySubscriptExpr> arraySubscriptExpr; 605 606/// \brief Matches the value of a default argument at the call site. 607/// 608/// Example matches the CXXDefaultArgExpr placeholder inserted for the 609/// default value of the second parameter in the call expression f(42) 610/// (matcher = defaultArgExpr()) 611/// \code 612/// void f(int x, int y = 0); 613/// f(42); 614/// \endcode 615const internal::VariadicDynCastAllOfMatcher< 616 Stmt, 617 CXXDefaultArgExpr> defaultArgExpr; 618 619/// \brief Matches overloaded operator calls. 620/// 621/// Note that if an operator isn't overloaded, it won't match. Instead, use 622/// binaryOperator matcher. 623/// Currently it does not match operators such as new delete. 624/// FIXME: figure out why these do not match? 625/// 626/// Example matches both operator<<((o << b), c) and operator<<(o, b) 627/// (matcher = operatorCallExpr()) 628/// \code 629/// ostream &operator<< (ostream &out, int i) { }; 630/// ostream &o; int b = 1, c = 1; 631/// o << b << c; 632/// \endcode 633const internal::VariadicDynCastAllOfMatcher< 634 Stmt, 635 CXXOperatorCallExpr> operatorCallExpr; 636 637/// \brief Matches expressions. 638/// 639/// Example matches x() 640/// \code 641/// void f() { x(); } 642/// \endcode 643const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr; 644 645/// \brief Matches expressions that refer to declarations. 646/// 647/// Example matches x in if (x) 648/// \code 649/// bool x; 650/// if (x) {} 651/// \endcode 652const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr; 653 654/// \brief Matches if statements. 655/// 656/// Example matches 'if (x) {}' 657/// \code 658/// if (x) {} 659/// \endcode 660const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt; 661 662/// \brief Matches for statements. 663/// 664/// Example matches 'for (;;) {}' 665/// \code 666/// for (;;) {} 667/// \endcode 668const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt; 669 670/// \brief Matches the increment statement of a for loop. 671/// 672/// Example: 673/// forStmt(hasIncrement(unaryOperator(hasOperatorName("++")))) 674/// matches '++x' in 675/// \code 676/// for (x; x < N; ++x) { } 677/// \endcode 678AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>, 679 InnerMatcher) { 680 const Stmt *const Increment = Node.getInc(); 681 return (Increment != NULL && 682 InnerMatcher.matches(*Increment, Finder, Builder)); 683} 684 685/// \brief Matches the initialization statement of a for loop. 686/// 687/// Example: 688/// forStmt(hasLoopInit(declStmt())) 689/// matches 'int x = 0' in 690/// \code 691/// for (int x = 0; x < N; ++x) { } 692/// \endcode 693AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>, 694 InnerMatcher) { 695 const Stmt *const Init = Node.getInit(); 696 return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder)); 697} 698 699/// \brief Matches while statements. 700/// 701/// Given 702/// \code 703/// while (true) {} 704/// \endcode 705/// whileStmt() 706/// matches 'while (true) {}'. 707const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt; 708 709/// \brief Matches do statements. 710/// 711/// Given 712/// \code 713/// do {} while (true); 714/// \endcode 715/// doStmt() 716/// matches 'do {} while(true)' 717const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt; 718 719/// \brief Matches case and default statements inside switch statements. 720/// 721/// Given 722/// \code 723/// switch(a) { case 42: break; default: break; } 724/// \endcode 725/// switchCase() 726/// matches 'case 42: break;' and 'default: break;'. 727const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase; 728 729/// \brief Matches compound statements. 730/// 731/// Example matches '{}' and '{{}}'in 'for (;;) {{}}' 732/// \code 733/// for (;;) {{}} 734/// \endcode 735const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt; 736 737/// \brief Matches bool literals. 738/// 739/// Example matches true 740/// \code 741/// true 742/// \endcode 743const internal::VariadicDynCastAllOfMatcher< 744 Expr, 745 CXXBoolLiteralExpr> boolLiteral; 746 747/// \brief Matches string literals (also matches wide string literals). 748/// 749/// Example matches "abcd", L"abcd" 750/// \code 751/// char *s = "abcd"; wchar_t *ws = L"abcd" 752/// \endcode 753const internal::VariadicDynCastAllOfMatcher< 754 Expr, 755 StringLiteral> stringLiteral; 756 757/// \brief Matches character literals (also matches wchar_t). 758/// 759/// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), 760/// though. 761/// 762/// Example matches 'a', L'a' 763/// \code 764/// char ch = 'a'; wchar_t chw = L'a'; 765/// \endcode 766const internal::VariadicDynCastAllOfMatcher< 767 Expr, 768 CharacterLiteral> characterLiteral; 769 770/// \brief Matches integer literals of all sizes / encodings. 771/// 772/// Not matching character-encoded integers such as L'a'. 773/// 774/// Example matches 1, 1L, 0x1, 1U 775const internal::VariadicDynCastAllOfMatcher< 776 Expr, 777 IntegerLiteral> integerLiteral; 778 779/// \brief Matches binary operator expressions. 780/// 781/// Example matches a || b 782/// \code 783/// !(a || b) 784/// \endcode 785const internal::VariadicDynCastAllOfMatcher< 786 Stmt, 787 BinaryOperator> binaryOperator; 788 789/// \brief Matches unary operator expressions. 790/// 791/// Example matches !a 792/// \code 793/// !a || b 794/// \endcode 795const internal::VariadicDynCastAllOfMatcher< 796 Stmt, 797 UnaryOperator> unaryOperator; 798 799/// \brief Matches conditional operator expressions. 800/// 801/// Example matches a ? b : c 802/// \code 803/// (a ? b : c) + 42 804/// \endcode 805const internal::VariadicDynCastAllOfMatcher< 806 Stmt, 807 ConditionalOperator> conditionalOperator; 808 809/// \brief Matches a reinterpret_cast expression. 810/// 811/// Either the source expression or the destination type can be matched 812/// using has(), but hasDestinationType() is more specific and can be 813/// more readable. 814/// 815/// Example matches reinterpret_cast<char*>(&p) in 816/// \code 817/// void* p = reinterpret_cast<char*>(&p); 818/// \endcode 819const internal::VariadicDynCastAllOfMatcher< 820 Expr, 821 CXXReinterpretCastExpr> reinterpretCastExpr; 822 823/// \brief Matches a C++ static_cast expression. 824/// 825/// \see hasDestinationType 826/// \see reinterpretCast 827/// 828/// Example: 829/// staticCastExpr() 830/// matches 831/// static_cast<long>(8) 832/// in 833/// \code 834/// long eight(static_cast<long>(8)); 835/// \endcode 836const internal::VariadicDynCastAllOfMatcher< 837 Expr, 838 CXXStaticCastExpr> staticCastExpr; 839 840/// \brief Matches a dynamic_cast expression. 841/// 842/// Example: 843/// dynamicCastExpr() 844/// matches 845/// dynamic_cast<D*>(&b); 846/// in 847/// \code 848/// struct B { virtual ~B() {} }; struct D : B {}; 849/// B b; 850/// D* p = dynamic_cast<D*>(&b); 851/// \endcode 852const internal::VariadicDynCastAllOfMatcher< 853 Expr, 854 CXXDynamicCastExpr> dynamicCastExpr; 855 856/// \brief Matches a const_cast expression. 857/// 858/// Example: Matches const_cast<int*>(&r) in 859/// \code 860/// int n = 42; 861/// const int &r(n); 862/// int* p = const_cast<int*>(&r); 863/// \endcode 864const internal::VariadicDynCastAllOfMatcher< 865 Expr, 866 CXXConstCastExpr> constCastExpr; 867 868/// \brief Matches explicit cast expressions. 869/// 870/// Matches any cast expression written in user code, whether it be a 871/// C-style cast, a functional-style cast, or a keyword cast. 872/// 873/// Does not match implicit conversions. 874/// 875/// Note: the name "explicitCast" is chosen to match Clang's terminology, as 876/// Clang uses the term "cast" to apply to implicit conversions as well as to 877/// actual cast expressions. 878/// 879/// \see hasDestinationType. 880/// 881/// Example: matches all five of the casts in 882/// \code 883/// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 884/// \endcode 885/// but does not match the implicit conversion in 886/// \code 887/// long ell = 42; 888/// \endcode 889const internal::VariadicDynCastAllOfMatcher< 890 Expr, 891 ExplicitCastExpr> explicitCastExpr; 892 893/// \brief Matches the implicit cast nodes of Clang's AST. 894/// 895/// This matches many different places, including function call return value 896/// eliding, as well as any type conversions. 897const internal::VariadicDynCastAllOfMatcher< 898 Expr, 899 ImplicitCastExpr> implicitCastExpr; 900 901/// \brief Matches any cast nodes of Clang's AST. 902/// 903/// Example: castExpr() matches each of the following: 904/// \code 905/// (int) 3; 906/// const_cast<Expr *>(SubExpr); 907/// char c = 0; 908/// \endcode 909/// but does not match 910/// \code 911/// int i = (0); 912/// int k = 0; 913/// \endcode 914const internal::VariadicDynCastAllOfMatcher<Expr, CastExpr> castExpr; 915 916/// \brief Matches functional cast expressions 917/// 918/// Example: Matches Foo(bar); 919/// \code 920/// Foo f = bar; 921/// Foo g = (Foo) bar; 922/// Foo h = Foo(bar); 923/// \endcode 924const internal::VariadicDynCastAllOfMatcher< 925 Expr, 926 CXXFunctionalCastExpr> functionalCastExpr; 927 928/// \brief Various overloads for the anyOf matcher. 929/// @{ 930 931/// \brief Matches if any of the given matchers matches. 932/// 933/// Usable as: Any Matcher 934template<typename M1, typename M2> 935internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, M2> 936anyOf(const M1 &P1, const M2 &P2) { 937 return internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 938 M1, M2 >(P1, P2); 939} 940template<typename M1, typename M2, typename M3> 941internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 942 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, M3> > 943anyOf(const M1 &P1, const M2 &P2, const M3 &P3) { 944 return anyOf(P1, anyOf(P2, P3)); 945} 946template<typename M1, typename M2, typename M3, typename M4> 947internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 948 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, 949 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 950 M3, M4> > > 951anyOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4) { 952 return anyOf(P1, anyOf(P2, anyOf(P3, P4))); 953} 954template<typename M1, typename M2, typename M3, typename M4, typename M5> 955internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M1, 956 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M2, 957 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, M3, 958 internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, 959 M4, M5> > > > 960anyOf(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4, const M5 &P5) { 961 return anyOf(P1, anyOf(P2, anyOf(P3, anyOf(P4, P5)))); 962} 963 964/// @} 965 966/// \brief Various overloads for the allOf matcher. 967/// @{ 968 969/// \brief Matches if all given matchers match. 970/// 971/// Usable as: Any Matcher 972template<typename M1, typename M2> 973internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M1, M2> 974allOf(const M1 &P1, const M2 &P2) { 975 return internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, 976 M1, M2>(P1, P2); 977} 978template<typename M1, typename M2, typename M3> 979internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M1, 980 internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, M2, M3> > 981allOf(const M1 &P1, const M2 &P2, const M3 &P3) { 982 return allOf(P1, allOf(P2, P3)); 983} 984 985/// @} 986 987/// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 988/// 989/// Given 990/// \code 991/// Foo x = bar; 992/// int y = sizeof(x) + alignof(x); 993/// \endcode 994/// unaryExprOrTypeTraitExpr() 995/// matches \c sizeof(x) and \c alignof(x) 996const internal::VariadicDynCastAllOfMatcher< 997 Stmt, 998 UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr; 999 1000/// \brief Matches unary expressions that have a specific type of argument. 1001/// 1002/// Given 1003/// \code 1004/// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 1005/// \endcode 1006/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 1007/// matches \c sizeof(a) and \c alignof(c) 1008AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 1009 internal::Matcher<QualType>, InnerMatcher) { 1010 const QualType ArgumentType = Node.getTypeOfArgument(); 1011 return InnerMatcher.matches(ArgumentType, Finder, Builder); 1012} 1013 1014/// \brief Matches unary expressions of a certain kind. 1015/// 1016/// Given 1017/// \code 1018/// int x; 1019/// int s = sizeof(x) + alignof(x) 1020/// \endcode 1021/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 1022/// matches \c sizeof(x) 1023AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 1024 return Node.getKind() == Kind; 1025} 1026 1027/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1028/// alignof. 1029inline internal::Matcher<Stmt> alignOfExpr( 1030 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1031 return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf( 1032 ofKind(UETT_AlignOf), InnerMatcher))); 1033} 1034 1035/// \brief Same as unaryExprOrTypeTraitExpr, but only matching 1036/// sizeof. 1037inline internal::Matcher<Stmt> sizeOfExpr( 1038 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 1039 return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf( 1040 ofKind(UETT_SizeOf), InnerMatcher))); 1041} 1042 1043/// \brief Matches NamedDecl nodes that have the specified name. 1044/// 1045/// Supports specifying enclosing namespaces or classes by prefixing the name 1046/// with '<enclosing>::'. 1047/// Does not match typedefs of an underlying type with the given name. 1048/// 1049/// Example matches X (Name == "X") 1050/// \code 1051/// class X; 1052/// \endcode 1053/// 1054/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 1055/// \code 1056/// namespace a { namespace b { class X; } } 1057/// \endcode 1058AST_MATCHER_P(NamedDecl, hasName, std::string, Name) { 1059 assert(!Name.empty()); 1060 const std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1061 const llvm::StringRef FullName = FullNameString; 1062 const llvm::StringRef Pattern = Name; 1063 if (Pattern.startswith("::")) { 1064 return FullName == Pattern; 1065 } else { 1066 return FullName.endswith(("::" + Pattern).str()); 1067 } 1068} 1069 1070/// \brief Matches NamedDecl nodes whose full names partially match the 1071/// given RegExp. 1072/// 1073/// Supports specifying enclosing namespaces or classes by 1074/// prefixing the name with '<enclosing>::'. Does not match typedefs 1075/// of an underlying type with the given name. 1076/// 1077/// Example matches X (regexp == "::X") 1078/// \code 1079/// class X; 1080/// \endcode 1081/// 1082/// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 1083/// \code 1084/// namespace foo { namespace bar { class X; } } 1085/// \endcode 1086AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 1087 assert(!RegExp.empty()); 1088 std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 1089 llvm::Regex RE(RegExp); 1090 return RE.match(FullNameString); 1091} 1092 1093/// \brief Matches overloaded operator names. 1094/// 1095/// Matches overloaded operator names specified in strings without the 1096/// "operator" prefix, such as "<<", for OverloadedOperatorCall's. 1097/// 1098/// Example matches a << b 1099/// (matcher == operatorCallExpr(hasOverloadedOperatorName("<<"))) 1100/// \code 1101/// a << b; 1102/// c && d; // assuming both operator<< 1103/// // and operator&& are overloaded somewhere. 1104/// \endcode 1105AST_MATCHER_P(CXXOperatorCallExpr, 1106 hasOverloadedOperatorName, std::string, Name) { 1107 return getOperatorSpelling(Node.getOperator()) == Name; 1108} 1109 1110/// \brief Matches C++ classes that are directly or indirectly derived from 1111/// a class matching \c Base. 1112/// 1113/// Note that a class is not considered to be derived from itself. 1114/// 1115/// Example matches Y, Z, C (Base == hasName("X")) 1116/// \code 1117/// class X; 1118/// class Y : public X {}; // directly derived 1119/// class Z : public Y {}; // indirectly derived 1120/// typedef X A; 1121/// typedef A B; 1122/// class C : public B {}; // derived from a typedef of X 1123/// \endcode 1124/// 1125/// In the following example, Bar matches isDerivedFrom(hasName("X")): 1126/// \code 1127/// class Foo; 1128/// typedef Foo X; 1129/// class Bar : public Foo {}; // derived from a type that X is a typedef of 1130/// \endcode 1131AST_MATCHER_P(CXXRecordDecl, isDerivedFrom, 1132 internal::Matcher<NamedDecl>, Base) { 1133 return Finder->classIsDerivedFrom(&Node, Base, Builder); 1134} 1135 1136/// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 1137inline internal::Matcher<CXXRecordDecl> isDerivedFrom(StringRef BaseName) { 1138 assert(!BaseName.empty()); 1139 return isDerivedFrom(hasName(BaseName)); 1140} 1141 1142/// \brief Similar to \c isDerivedFrom(), but also matches classes that directly 1143/// match \c Base. 1144inline internal::Matcher<CXXRecordDecl> isSameOrDerivedFrom( 1145 internal::Matcher<NamedDecl> Base) { 1146 return anyOf(Base, isDerivedFrom(Base)); 1147} 1148 1149/// \brief Overloaded method as shortcut for 1150/// \c isSameOrDerivedFrom(hasName(...)). 1151inline internal::Matcher<CXXRecordDecl> isSameOrDerivedFrom( 1152 StringRef BaseName) { 1153 assert(!BaseName.empty()); 1154 return isSameOrDerivedFrom(hasName(BaseName)); 1155} 1156 1157/// \brief Matches AST nodes that have child AST nodes that match the 1158/// provided matcher. 1159/// 1160/// Example matches X, Y (matcher = recordDecl(has(recordDecl(hasName("X"))) 1161/// \code 1162/// class X {}; // Matches X, because X::X is a class of name X inside X. 1163/// class Y { class X {}; }; 1164/// class Z { class Y { class X {}; }; }; // Does not match Z. 1165/// \endcode 1166/// 1167/// ChildT must be an AST base type. 1168/// 1169/// Usable as: Any Matcher 1170template <typename ChildT> 1171internal::ArgumentAdaptingMatcher<internal::HasMatcher, ChildT> has( 1172 const internal::Matcher<ChildT> &ChildMatcher) { 1173 return internal::ArgumentAdaptingMatcher<internal::HasMatcher, 1174 ChildT>(ChildMatcher); 1175} 1176 1177/// \brief Matches AST nodes that have descendant AST nodes that match the 1178/// provided matcher. 1179/// 1180/// Example matches X, Y, Z 1181/// (matcher = recordDecl(hasDescendant(recordDecl(hasName("X"))))) 1182/// \code 1183/// class X {}; // Matches X, because X::X is a class of name X inside X. 1184/// class Y { class X {}; }; 1185/// class Z { class Y { class X {}; }; }; 1186/// \endcode 1187/// 1188/// DescendantT must be an AST base type. 1189/// 1190/// Usable as: Any Matcher 1191template <typename DescendantT> 1192internal::ArgumentAdaptingMatcher<internal::HasDescendantMatcher, DescendantT> 1193hasDescendant(const internal::Matcher<DescendantT> &DescendantMatcher) { 1194 return internal::ArgumentAdaptingMatcher< 1195 internal::HasDescendantMatcher, 1196 DescendantT>(DescendantMatcher); 1197} 1198 1199/// \brief Matches AST nodes that have child AST nodes that match the 1200/// provided matcher. 1201/// 1202/// Example matches X, Y (matcher = recordDecl(forEach(recordDecl(hasName("X"))) 1203/// \code 1204/// class X {}; // Matches X, because X::X is a class of name X inside X. 1205/// class Y { class X {}; }; 1206/// class Z { class Y { class X {}; }; }; // Does not match Z. 1207/// \endcode 1208/// 1209/// ChildT must be an AST base type. 1210/// 1211/// As opposed to 'has', 'forEach' will cause a match for each result that 1212/// matches instead of only on the first one. 1213/// 1214/// Usable as: Any Matcher 1215template <typename ChildT> 1216internal::ArgumentAdaptingMatcher<internal::ForEachMatcher, ChildT> forEach( 1217 const internal::Matcher<ChildT> &ChildMatcher) { 1218 return internal::ArgumentAdaptingMatcher< 1219 internal::ForEachMatcher, 1220 ChildT>(ChildMatcher); 1221} 1222 1223/// \brief Matches AST nodes that have descendant AST nodes that match the 1224/// provided matcher. 1225/// 1226/// Example matches X, A, B, C 1227/// (matcher = recordDecl(forEachDescendant(recordDecl(hasName("X"))))) 1228/// \code 1229/// class X {}; // Matches X, because X::X is a class of name X inside X. 1230/// class A { class X {}; }; 1231/// class B { class C { class X {}; }; }; 1232/// \endcode 1233/// 1234/// DescendantT must be an AST base type. 1235/// 1236/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 1237/// each result that matches instead of only on the first one. 1238/// 1239/// Note: Recursively combined ForEachDescendant can cause many matches: 1240/// recordDecl(forEachDescendant(recordDecl(forEachDescendant(recordDecl())))) 1241/// will match 10 times (plus injected class name matches) on: 1242/// \code 1243/// class A { class B { class C { class D { class E {}; }; }; }; }; 1244/// \endcode 1245/// 1246/// Usable as: Any Matcher 1247template <typename DescendantT> 1248internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, DescendantT> 1249forEachDescendant( 1250 const internal::Matcher<DescendantT> &DescendantMatcher) { 1251 return internal::ArgumentAdaptingMatcher< 1252 internal::ForEachDescendantMatcher, 1253 DescendantT>(DescendantMatcher); 1254} 1255 1256/// \brief Matches AST nodes that have an ancestor that matches the provided 1257/// matcher. 1258/// 1259/// Given 1260/// \code 1261/// void f() { if (true) { int x = 42; } } 1262/// void g() { for (;;) { int x = 43; } } 1263/// \endcode 1264/// \c expr(integerLiteral(hasAncsestor(ifStmt()))) matches \c 42, but not 43. 1265/// 1266/// Usable as: Any Matcher 1267template <typename AncestorT> 1268internal::ArgumentAdaptingMatcher<internal::HasAncestorMatcher, AncestorT> 1269hasAncestor(const internal::Matcher<AncestorT> &AncestorMatcher) { 1270 return internal::ArgumentAdaptingMatcher< 1271 internal::HasAncestorMatcher, 1272 AncestorT>(AncestorMatcher); 1273} 1274 1275/// \brief Matches if the provided matcher does not match. 1276/// 1277/// Example matches Y (matcher = recordDecl(unless(hasName("X")))) 1278/// \code 1279/// class X {}; 1280/// class Y {}; 1281/// \endcode 1282/// 1283/// Usable as: Any Matcher 1284template <typename M> 1285internal::PolymorphicMatcherWithParam1<internal::NotMatcher, M> 1286unless(const M &InnerMatcher) { 1287 return internal::PolymorphicMatcherWithParam1< 1288 internal::NotMatcher, M>(InnerMatcher); 1289} 1290 1291/// \brief Matches a type if the declaration of the type matches the given 1292/// matcher. 1293/// 1294/// Usable as: Matcher<QualType>, Matcher<CallExpr>, Matcher<CXXConstructExpr> 1295inline internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, 1296 internal::Matcher<Decl> > 1297 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 1298 return internal::PolymorphicMatcherWithParam1< 1299 internal::HasDeclarationMatcher, 1300 internal::Matcher<Decl> >(InnerMatcher); 1301} 1302 1303/// \brief Matches on the implicit object argument of a member call expression. 1304/// 1305/// Example matches y.x() (matcher = callExpr(on(hasType(recordDecl(hasName("Y")))))) 1306/// \code 1307/// class Y { public: void x(); }; 1308/// void z() { Y y; y.x(); }", 1309/// \endcode 1310/// 1311/// FIXME: Overload to allow directly matching types? 1312AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 1313 InnerMatcher) { 1314 const Expr *ExprNode = const_cast<CXXMemberCallExpr&>(Node) 1315 .getImplicitObjectArgument() 1316 ->IgnoreParenImpCasts(); 1317 return (ExprNode != NULL && 1318 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1319} 1320 1321/// \brief Matches if the call expression's callee expression matches. 1322/// 1323/// Given 1324/// \code 1325/// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 1326/// void f() { f(); } 1327/// \endcode 1328/// callExpr(callee(expr())) 1329/// matches this->x(), x(), y.x(), f() 1330/// with callee(...) 1331/// matching this->x, x, y.x, f respectively 1332/// 1333/// Note: Callee cannot take the more general internal::Matcher<Expr> 1334/// because this introduces ambiguous overloads with calls to Callee taking a 1335/// internal::Matcher<Decl>, as the matcher hierarchy is purely 1336/// implemented in terms of implicit casts. 1337AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 1338 InnerMatcher) { 1339 const Expr *ExprNode = Node.getCallee(); 1340 return (ExprNode != NULL && 1341 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1342} 1343 1344/// \brief Matches if the call expression's callee's declaration matches the 1345/// given matcher. 1346/// 1347/// Example matches y.x() (matcher = callExpr(callee(methodDecl(hasName("x"))))) 1348/// \code 1349/// class Y { public: void x(); }; 1350/// void z() { Y y; y.x(); 1351/// \endcode 1352inline internal::Matcher<CallExpr> callee( 1353 const internal::Matcher<Decl> &InnerMatcher) { 1354 return internal::Matcher<CallExpr>(hasDeclaration(InnerMatcher)); 1355} 1356 1357/// \brief Matches if the expression's or declaration's type matches a type 1358/// matcher. 1359/// 1360/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1361/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1362/// \code 1363/// class X {}; 1364/// void y(X &x) { x; X z; } 1365/// \endcode 1366AST_POLYMORPHIC_MATCHER_P(hasType, internal::Matcher<QualType>, 1367 InnerMatcher) { 1368 TOOLING_COMPILE_ASSERT((llvm::is_base_of<Expr, NodeType>::value || 1369 llvm::is_base_of<ValueDecl, NodeType>::value), 1370 instantiated_with_wrong_types); 1371 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1372} 1373 1374/// \brief Overloaded to match the declaration of the expression's or value 1375/// declaration's type. 1376/// 1377/// In case of a value declaration (for example a variable declaration), 1378/// this resolves one layer of indirection. For example, in the value 1379/// declaration "X x;", recordDecl(hasName("X")) matches the declaration of X, 1380/// while varDecl(hasType(recordDecl(hasName("X")))) matches the declaration 1381/// of x." 1382/// 1383/// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 1384/// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 1385/// \code 1386/// class X {}; 1387/// void y(X &x) { x; X z; } 1388/// \endcode 1389/// 1390/// Usable as: Matcher<Expr>, Matcher<ValueDecl> 1391inline internal::PolymorphicMatcherWithParam1< 1392 internal::matcher_hasTypeMatcher, 1393 internal::Matcher<QualType> > 1394hasType(const internal::Matcher<Decl> &InnerMatcher) { 1395 return hasType(internal::Matcher<QualType>( 1396 hasDeclaration(InnerMatcher))); 1397} 1398 1399/// \brief Matches if the matched type is represented by the given string. 1400/// 1401/// Given 1402/// \code 1403/// class Y { public: void x(); }; 1404/// void z() { Y* y; y->x(); } 1405/// \endcode 1406/// callExpr(on(hasType(asString("class Y *")))) 1407/// matches y->x() 1408AST_MATCHER_P(QualType, asString, std::string, Name) { 1409 return Name == Node.getAsString(); 1410} 1411 1412/// \brief Matches if the matched type is a pointer type and the pointee type 1413/// matches the specified matcher. 1414/// 1415/// Example matches y->x() 1416/// (matcher = callExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))))) 1417/// \code 1418/// class Y { public: void x(); }; 1419/// void z() { Y *y; y->x(); } 1420/// \endcode 1421AST_MATCHER_P( 1422 QualType, pointsTo, internal::Matcher<QualType>, 1423 InnerMatcher) { 1424 return (!Node.isNull() && Node->isPointerType() && 1425 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1426} 1427 1428/// \brief Overloaded to match the pointee type's declaration. 1429inline internal::Matcher<QualType> pointsTo( 1430 const internal::Matcher<Decl> &InnerMatcher) { 1431 return pointsTo(internal::Matcher<QualType>( 1432 hasDeclaration(InnerMatcher))); 1433} 1434 1435/// \brief Matches if the matched type is a reference type and the referenced 1436/// type matches the specified matcher. 1437/// 1438/// Example matches X &x and const X &y 1439/// (matcher = varDecl(hasType(references(recordDecl(hasName("X")))))) 1440/// \code 1441/// class X { 1442/// void a(X b) { 1443/// X &x = b; 1444/// const X &y = b; 1445/// }; 1446/// \endcode 1447AST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 1448 InnerMatcher) { 1449 return (!Node.isNull() && Node->isReferenceType() && 1450 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 1451} 1452 1453/// \brief Overloaded to match the referenced type's declaration. 1454inline internal::Matcher<QualType> references( 1455 const internal::Matcher<Decl> &InnerMatcher) { 1456 return references(internal::Matcher<QualType>( 1457 hasDeclaration(InnerMatcher))); 1458} 1459 1460AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 1461 internal::Matcher<Expr>, InnerMatcher) { 1462 const Expr *ExprNode = 1463 const_cast<CXXMemberCallExpr&>(Node).getImplicitObjectArgument(); 1464 return (ExprNode != NULL && 1465 InnerMatcher.matches(*ExprNode, Finder, Builder)); 1466} 1467 1468/// \brief Matches if the expression's type either matches the specified 1469/// matcher, or is a pointer to a type that matches the InnerMatcher. 1470inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1471 const internal::Matcher<QualType> &InnerMatcher) { 1472 return onImplicitObjectArgument( 1473 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1474} 1475 1476/// \brief Overloaded to match the type's declaration. 1477inline internal::Matcher<CXXMemberCallExpr> thisPointerType( 1478 const internal::Matcher<Decl> &InnerMatcher) { 1479 return onImplicitObjectArgument( 1480 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))); 1481} 1482 1483/// \brief Matches a DeclRefExpr that refers to a declaration that matches the 1484/// specified matcher. 1485/// 1486/// Example matches x in if(x) 1487/// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 1488/// \code 1489/// bool x; 1490/// if (x) {} 1491/// \endcode 1492AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 1493 InnerMatcher) { 1494 const Decl *DeclNode = Node.getDecl(); 1495 return (DeclNode != NULL && 1496 InnerMatcher.matches(*DeclNode, Finder, Builder)); 1497} 1498 1499/// \brief Matches a \c DeclRefExpr that refers to a declaration through a 1500/// specific using shadow declaration. 1501/// 1502/// FIXME: This currently only works for functions. Fix. 1503/// 1504/// Given 1505/// \code 1506/// namespace a { void f() {} } 1507/// using a::f; 1508/// void g() { 1509/// f(); // Matches this .. 1510/// a::f(); // .. but not this. 1511/// } 1512/// \endcode 1513/// declRefExpr(throughUsingDeclaration(anything())) 1514/// matches \c f() 1515AST_MATCHER_P(DeclRefExpr, throughUsingDecl, 1516 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 1517 const NamedDecl *FoundDecl = Node.getFoundDecl(); 1518 if (const UsingShadowDecl *UsingDecl = 1519 llvm::dyn_cast<UsingShadowDecl>(FoundDecl)) 1520 return InnerMatcher.matches(*UsingDecl, Finder, Builder); 1521 return false; 1522} 1523 1524/// \brief Matches the Decl of a DeclStmt which has a single declaration. 1525/// 1526/// Given 1527/// \code 1528/// int a, b; 1529/// int c; 1530/// \endcode 1531/// declStmt(hasSingleDecl(anything())) 1532/// matches 'int c;' but not 'int a, b;'. 1533AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 1534 if (Node.isSingleDecl()) { 1535 const Decl *FoundDecl = Node.getSingleDecl(); 1536 return InnerMatcher.matches(*FoundDecl, Finder, Builder); 1537 } 1538 return false; 1539} 1540 1541/// \brief Matches a variable declaration that has an initializer expression 1542/// that matches the given matcher. 1543/// 1544/// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 1545/// \code 1546/// bool y() { return true; } 1547/// bool x = y(); 1548/// \endcode 1549AST_MATCHER_P( 1550 VarDecl, hasInitializer, internal::Matcher<Expr>, 1551 InnerMatcher) { 1552 const Expr *Initializer = Node.getAnyInitializer(); 1553 return (Initializer != NULL && 1554 InnerMatcher.matches(*Initializer, Finder, Builder)); 1555} 1556 1557/// \brief Checks that a call expression or a constructor call expression has 1558/// a specific number of arguments (including absent default arguments). 1559/// 1560/// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 1561/// \code 1562/// void f(int x, int y); 1563/// f(0, 0); 1564/// \endcode 1565AST_POLYMORPHIC_MATCHER_P(argumentCountIs, unsigned, N) { 1566 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1567 llvm::is_base_of<CXXConstructExpr, 1568 NodeType>::value), 1569 instantiated_with_wrong_types); 1570 return Node.getNumArgs() == N; 1571} 1572 1573/// \brief Matches the n'th argument of a call expression or a constructor 1574/// call expression. 1575/// 1576/// Example matches y in x(y) 1577/// (matcher = callExpr(hasArgument(0, declRefExpr()))) 1578/// \code 1579/// void x(int) { int y; x(y); } 1580/// \endcode 1581AST_POLYMORPHIC_MATCHER_P2( 1582 hasArgument, unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 1583 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1584 llvm::is_base_of<CXXConstructExpr, 1585 NodeType>::value), 1586 instantiated_with_wrong_types); 1587 return (N < Node.getNumArgs() && 1588 InnerMatcher.matches( 1589 *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 1590} 1591 1592/// \brief Matches declaration statements that contain a specific number of 1593/// declarations. 1594/// 1595/// Example: Given 1596/// \code 1597/// int a, b; 1598/// int c; 1599/// int d = 2, e; 1600/// \endcode 1601/// declCountIs(2) 1602/// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 1603AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 1604 return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 1605} 1606 1607/// \brief Matches the n'th declaration of a declaration statement. 1608/// 1609/// Note that this does not work for global declarations because the AST 1610/// breaks up multiple-declaration DeclStmt's into multiple single-declaration 1611/// DeclStmt's. 1612/// Example: Given non-global declarations 1613/// \code 1614/// int a, b = 0; 1615/// int c; 1616/// int d = 2, e; 1617/// \endcode 1618/// declStmt(containsDeclaration( 1619/// 0, varDecl(hasInitializer(anything())))) 1620/// matches only 'int d = 2, e;', and 1621/// declStmt(containsDeclaration(1, varDecl())) 1622/// \code 1623/// matches 'int a, b = 0' as well as 'int d = 2, e;' 1624/// but 'int c;' is not matched. 1625/// \endcode 1626AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 1627 internal::Matcher<Decl>, InnerMatcher) { 1628 const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 1629 if (N >= NumDecls) 1630 return false; 1631 DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 1632 std::advance(Iterator, N); 1633 return InnerMatcher.matches(**Iterator, Finder, Builder); 1634} 1635 1636/// \brief Matches a constructor initializer. 1637/// 1638/// Given 1639/// \code 1640/// struct Foo { 1641/// Foo() : foo_(1) { } 1642/// int foo_; 1643/// }; 1644/// \endcode 1645/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer(anything())))) 1646/// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 1647AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 1648 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 1649 for (CXXConstructorDecl::init_const_iterator I = Node.init_begin(); 1650 I != Node.init_end(); ++I) { 1651 if (InnerMatcher.matches(**I, Finder, Builder)) { 1652 return true; 1653 } 1654 } 1655 return false; 1656} 1657 1658/// \brief Matches the field declaration of a constructor initializer. 1659/// 1660/// Given 1661/// \code 1662/// struct Foo { 1663/// Foo() : foo_(1) { } 1664/// int foo_; 1665/// }; 1666/// \endcode 1667/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 1668/// forField(hasName("foo_")))))) 1669/// matches Foo 1670/// with forField matching foo_ 1671AST_MATCHER_P(CXXCtorInitializer, forField, 1672 internal::Matcher<FieldDecl>, InnerMatcher) { 1673 const FieldDecl *NodeAsDecl = Node.getMember(); 1674 return (NodeAsDecl != NULL && 1675 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 1676} 1677 1678/// \brief Matches the initializer expression of a constructor initializer. 1679/// 1680/// Given 1681/// \code 1682/// struct Foo { 1683/// Foo() : foo_(1) { } 1684/// int foo_; 1685/// }; 1686/// \endcode 1687/// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 1688/// withInitializer(integerLiteral(equals(1))))))) 1689/// matches Foo 1690/// with withInitializer matching (1) 1691AST_MATCHER_P(CXXCtorInitializer, withInitializer, 1692 internal::Matcher<Expr>, InnerMatcher) { 1693 const Expr* NodeAsExpr = Node.getInit(); 1694 return (NodeAsExpr != NULL && 1695 InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 1696} 1697 1698/// \brief Matches a contructor initializer if it is explicitly written in 1699/// code (as opposed to implicitly added by the compiler). 1700/// 1701/// Given 1702/// \code 1703/// struct Foo { 1704/// Foo() { } 1705/// Foo(int) : foo_("A") { } 1706/// string foo_; 1707/// }; 1708/// \endcode 1709/// constructorDecl(hasAnyConstructorInitializer(isWritten())) 1710/// will match Foo(int), but not Foo() 1711AST_MATCHER(CXXCtorInitializer, isWritten) { 1712 return Node.isWritten(); 1713} 1714 1715/// \brief Matches a constructor declaration that has been implicitly added 1716/// by the compiler (eg. implicit default/copy constructors). 1717AST_MATCHER(CXXConstructorDecl, isImplicit) { 1718 return Node.isImplicit(); 1719} 1720 1721/// \brief Matches any argument of a call expression or a constructor call 1722/// expression. 1723/// 1724/// Given 1725/// \code 1726/// void x(int, int, int) { int y; x(1, y, 42); } 1727/// \endcode 1728/// callExpr(hasAnyArgument(declRefExpr())) 1729/// matches x(1, y, 42) 1730/// with hasAnyArgument(...) 1731/// matching y 1732AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, internal::Matcher<Expr>, 1733 InnerMatcher) { 1734 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value || 1735 llvm::is_base_of<CXXConstructExpr, 1736 NodeType>::value), 1737 instantiated_with_wrong_types); 1738 for (unsigned I = 0; I < Node.getNumArgs(); ++I) { 1739 if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(), 1740 Finder, Builder)) { 1741 return true; 1742 } 1743 } 1744 return false; 1745} 1746 1747/// \brief Matches the n'th parameter of a function declaration. 1748/// 1749/// Given 1750/// \code 1751/// class X { void f(int x) {} }; 1752/// \endcode 1753/// methodDecl(hasParameter(0, hasType(varDecl()))) 1754/// matches f(int x) {} 1755/// with hasParameter(...) 1756/// matching int x 1757AST_MATCHER_P2(FunctionDecl, hasParameter, 1758 unsigned, N, internal::Matcher<ParmVarDecl>, 1759 InnerMatcher) { 1760 return (N < Node.getNumParams() && 1761 InnerMatcher.matches( 1762 *Node.getParamDecl(N), Finder, Builder)); 1763} 1764 1765/// \brief Matches any parameter of a function declaration. 1766/// 1767/// Does not match the 'this' parameter of a method. 1768/// 1769/// Given 1770/// \code 1771/// class X { void f(int x, int y, int z) {} }; 1772/// \endcode 1773/// methodDecl(hasAnyParameter(hasName("y"))) 1774/// matches f(int x, int y, int z) {} 1775/// with hasAnyParameter(...) 1776/// matching int y 1777AST_MATCHER_P(FunctionDecl, hasAnyParameter, 1778 internal::Matcher<ParmVarDecl>, InnerMatcher) { 1779 for (unsigned I = 0; I < Node.getNumParams(); ++I) { 1780 if (InnerMatcher.matches(*Node.getParamDecl(I), Finder, Builder)) { 1781 return true; 1782 } 1783 } 1784 return false; 1785} 1786 1787/// \brief Matches the return type of a function declaration. 1788/// 1789/// Given: 1790/// \code 1791/// class X { int f() { return 1; } }; 1792/// \endcode 1793/// methodDecl(returns(asString("int"))) 1794/// matches int f() { return 1; } 1795AST_MATCHER_P(FunctionDecl, returns, 1796 internal::Matcher<QualType>, InnerMatcher) { 1797 return InnerMatcher.matches(Node.getResultType(), Finder, Builder); 1798} 1799 1800/// \brief Matches extern "C" function declarations. 1801/// 1802/// Given: 1803/// \code 1804/// extern "C" void f() {} 1805/// extern "C" { void g() {} } 1806/// void h() {} 1807/// \endcode 1808/// functionDecl(isExternC()) 1809/// matches the declaration of f and g, but not the declaration h 1810AST_MATCHER(FunctionDecl, isExternC) { 1811 return Node.isExternC(); 1812} 1813 1814/// \brief Matches the condition expression of an if statement, for loop, 1815/// or conditional operator. 1816/// 1817/// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 1818/// \code 1819/// if (true) {} 1820/// \endcode 1821AST_POLYMORPHIC_MATCHER_P(hasCondition, internal::Matcher<Expr>, 1822 InnerMatcher) { 1823 TOOLING_COMPILE_ASSERT( 1824 (llvm::is_base_of<IfStmt, NodeType>::value) || 1825 (llvm::is_base_of<ForStmt, NodeType>::value) || 1826 (llvm::is_base_of<WhileStmt, NodeType>::value) || 1827 (llvm::is_base_of<DoStmt, NodeType>::value) || 1828 (llvm::is_base_of<ConditionalOperator, NodeType>::value), 1829 has_condition_requires_if_statement_conditional_operator_or_loop); 1830 const Expr *const Condition = Node.getCond(); 1831 return (Condition != NULL && 1832 InnerMatcher.matches(*Condition, Finder, Builder)); 1833} 1834 1835/// \brief Matches the condition variable statement in an if statement. 1836/// 1837/// Given 1838/// \code 1839/// if (A* a = GetAPointer()) {} 1840/// \endcode 1841/// hasConditionVariableStatment(...) 1842/// matches 'A* a = GetAPointer()'. 1843AST_MATCHER_P(IfStmt, hasConditionVariableStatement, 1844 internal::Matcher<DeclStmt>, InnerMatcher) { 1845 const DeclStmt* const DeclarationStatement = 1846 Node.getConditionVariableDeclStmt(); 1847 return DeclarationStatement != NULL && 1848 InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 1849} 1850 1851/// \brief Matches the index expression of an array subscript expression. 1852/// 1853/// Given 1854/// \code 1855/// int i[5]; 1856/// void f() { i[1] = 42; } 1857/// \endcode 1858/// arraySubscriptExpression(hasIndex(integerLiteral())) 1859/// matches \c i[1] with the \c integerLiteral() matching \c 1 1860AST_MATCHER_P(ArraySubscriptExpr, hasIndex, 1861 internal::Matcher<Expr>, InnerMatcher) { 1862 if (const Expr* Expression = Node.getIdx()) 1863 return InnerMatcher.matches(*Expression, Finder, Builder); 1864 return false; 1865} 1866 1867/// \brief Matches the base expression of an array subscript expression. 1868/// 1869/// Given 1870/// \code 1871/// int i[5]; 1872/// void f() { i[1] = 42; } 1873/// \endcode 1874/// arraySubscriptExpression(hasBase(implicitCastExpr( 1875/// hasSourceExpression(declRefExpr())))) 1876/// matches \c i[1] with the \c declRefExpr() matching \c i 1877AST_MATCHER_P(ArraySubscriptExpr, hasBase, 1878 internal::Matcher<Expr>, InnerMatcher) { 1879 if (const Expr* Expression = Node.getBase()) 1880 return InnerMatcher.matches(*Expression, Finder, Builder); 1881 return false; 1882} 1883 1884/// \brief Matches a 'for', 'while', or 'do while' statement that has 1885/// a given body. 1886/// 1887/// Given 1888/// \code 1889/// for (;;) {} 1890/// \endcode 1891/// hasBody(compoundStmt()) 1892/// matches 'for (;;) {}' 1893/// with compoundStmt() 1894/// matching '{}' 1895AST_POLYMORPHIC_MATCHER_P(hasBody, internal::Matcher<Stmt>, 1896 InnerMatcher) { 1897 TOOLING_COMPILE_ASSERT( 1898 (llvm::is_base_of<DoStmt, NodeType>::value) || 1899 (llvm::is_base_of<ForStmt, NodeType>::value) || 1900 (llvm::is_base_of<WhileStmt, NodeType>::value), 1901 has_body_requires_for_while_or_do_statement); 1902 const Stmt *const Statement = Node.getBody(); 1903 return (Statement != NULL && 1904 InnerMatcher.matches(*Statement, Finder, Builder)); 1905} 1906 1907/// \brief Matches compound statements where at least one substatement matches 1908/// a given matcher. 1909/// 1910/// Given 1911/// \code 1912/// { {}; 1+2; } 1913/// \endcode 1914/// hasAnySubstatement(compoundStmt()) 1915/// matches '{ {}; 1+2; }' 1916/// with compoundStmt() 1917/// matching '{}' 1918AST_MATCHER_P(CompoundStmt, hasAnySubstatement, 1919 internal::Matcher<Stmt>, InnerMatcher) { 1920 for (CompoundStmt::const_body_iterator It = Node.body_begin(); 1921 It != Node.body_end(); 1922 ++It) { 1923 if (InnerMatcher.matches(**It, Finder, Builder)) return true; 1924 } 1925 return false; 1926} 1927 1928/// \brief Checks that a compound statement contains a specific number of 1929/// child statements. 1930/// 1931/// Example: Given 1932/// \code 1933/// { for (;;) {} } 1934/// \endcode 1935/// compoundStmt(statementCountIs(0))) 1936/// matches '{}' 1937/// but does not match the outer compound statement. 1938AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 1939 return Node.size() == N; 1940} 1941 1942/// \brief Matches literals that are equal to the given value. 1943/// 1944/// Example matches true (matcher = boolLiteral(equals(true))) 1945/// \code 1946/// true 1947/// \endcode 1948/// 1949/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 1950/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 1951template <typename ValueT> 1952internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 1953equals(const ValueT &Value) { 1954 return internal::PolymorphicMatcherWithParam1< 1955 internal::ValueEqualsMatcher, 1956 ValueT>(Value); 1957} 1958 1959/// \brief Matches the operator Name of operator expressions (binary or 1960/// unary). 1961/// 1962/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 1963/// \code 1964/// !(a || b) 1965/// \endcode 1966AST_POLYMORPHIC_MATCHER_P(hasOperatorName, std::string, Name) { 1967 TOOLING_COMPILE_ASSERT( 1968 (llvm::is_base_of<BinaryOperator, NodeType>::value) || 1969 (llvm::is_base_of<UnaryOperator, NodeType>::value), 1970 has_condition_requires_if_statement_or_conditional_operator); 1971 return Name == Node.getOpcodeStr(Node.getOpcode()); 1972} 1973 1974/// \brief Matches the left hand side of binary operator expressions. 1975/// 1976/// Example matches a (matcher = binaryOperator(hasLHS())) 1977/// \code 1978/// a || b 1979/// \endcode 1980AST_MATCHER_P(BinaryOperator, hasLHS, 1981 internal::Matcher<Expr>, InnerMatcher) { 1982 Expr *LeftHandSide = Node.getLHS(); 1983 return (LeftHandSide != NULL && 1984 InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 1985} 1986 1987/// \brief Matches the right hand side of binary operator expressions. 1988/// 1989/// Example matches b (matcher = binaryOperator(hasRHS())) 1990/// \code 1991/// a || b 1992/// \endcode 1993AST_MATCHER_P(BinaryOperator, hasRHS, 1994 internal::Matcher<Expr>, InnerMatcher) { 1995 Expr *RightHandSide = Node.getRHS(); 1996 return (RightHandSide != NULL && 1997 InnerMatcher.matches(*RightHandSide, Finder, Builder)); 1998} 1999 2000/// \brief Matches if either the left hand side or the right hand side of a 2001/// binary operator matches. 2002inline internal::Matcher<BinaryOperator> hasEitherOperand( 2003 const internal::Matcher<Expr> &InnerMatcher) { 2004 return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 2005} 2006 2007/// \brief Matches if the operand of a unary operator matches. 2008/// 2009/// Example matches true (matcher = hasOperand(boolLiteral(equals(true)))) 2010/// \code 2011/// !true 2012/// \endcode 2013AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 2014 internal::Matcher<Expr>, InnerMatcher) { 2015 const Expr * const Operand = Node.getSubExpr(); 2016 return (Operand != NULL && 2017 InnerMatcher.matches(*Operand, Finder, Builder)); 2018} 2019 2020/// \brief Matches if the cast's source expression matches the given matcher. 2021/// 2022/// Example: matches "a string" (matcher = 2023/// hasSourceExpression(constructExpr())) 2024/// \code 2025/// class URL { URL(string); }; 2026/// URL url = "a string"; 2027AST_MATCHER_P(CastExpr, hasSourceExpression, 2028 internal::Matcher<Expr>, InnerMatcher) { 2029 const Expr* const SubExpression = Node.getSubExpr(); 2030 return (SubExpression != NULL && 2031 InnerMatcher.matches(*SubExpression, Finder, Builder)); 2032} 2033 2034/// \brief Matches casts whose destination type matches a given matcher. 2035/// 2036/// (Note: Clang's AST refers to other conversions as "casts" too, and calls 2037/// actual casts "explicit" casts.) 2038AST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 2039 internal::Matcher<QualType>, InnerMatcher) { 2040 const QualType NodeType = Node.getTypeAsWritten(); 2041 return InnerMatcher.matches(NodeType, Finder, Builder); 2042} 2043 2044/// \brief Matches implicit casts whose destination type matches a given 2045/// matcher. 2046/// 2047/// FIXME: Unit test this matcher 2048AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 2049 internal::Matcher<QualType>, InnerMatcher) { 2050 return InnerMatcher.matches(Node.getType(), Finder, Builder); 2051} 2052 2053/// \brief Matches the true branch expression of a conditional operator. 2054/// 2055/// Example matches a 2056/// \code 2057/// condition ? a : b 2058/// \endcode 2059AST_MATCHER_P(ConditionalOperator, hasTrueExpression, 2060 internal::Matcher<Expr>, InnerMatcher) { 2061 Expr *Expression = Node.getTrueExpr(); 2062 return (Expression != NULL && 2063 InnerMatcher.matches(*Expression, Finder, Builder)); 2064} 2065 2066/// \brief Matches the false branch expression of a conditional operator. 2067/// 2068/// Example matches b 2069/// \code 2070/// condition ? a : b 2071/// \endcode 2072AST_MATCHER_P(ConditionalOperator, hasFalseExpression, 2073 internal::Matcher<Expr>, InnerMatcher) { 2074 Expr *Expression = Node.getFalseExpr(); 2075 return (Expression != NULL && 2076 InnerMatcher.matches(*Expression, Finder, Builder)); 2077} 2078 2079/// \brief Matches if a declaration has a body attached. 2080/// 2081/// Example matches A, va, fa 2082/// \code 2083/// class A {}; 2084/// class B; // Doesn't match, as it has no body. 2085/// int va; 2086/// extern int vb; // Doesn't match, as it doesn't define the variable. 2087/// void fa() {} 2088/// void fb(); // Doesn't match, as it has no body. 2089/// \endcode 2090/// 2091/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 2092inline internal::PolymorphicMatcherWithParam0<internal::IsDefinitionMatcher> 2093isDefinition() { 2094 return internal::PolymorphicMatcherWithParam0< 2095 internal::IsDefinitionMatcher>(); 2096} 2097 2098/// \brief Matches the class declaration that the given method declaration 2099/// belongs to. 2100/// 2101/// FIXME: Generalize this for other kinds of declarations. 2102/// FIXME: What other kind of declarations would we need to generalize 2103/// this to? 2104/// 2105/// Example matches A() in the last line 2106/// (matcher = constructExpr(hasDeclaration(methodDecl( 2107/// ofClass(hasName("A")))))) 2108/// \code 2109/// class A { 2110/// public: 2111/// A(); 2112/// }; 2113/// A a = A(); 2114/// \endcode 2115AST_MATCHER_P(CXXMethodDecl, ofClass, 2116 internal::Matcher<CXXRecordDecl>, InnerMatcher) { 2117 const CXXRecordDecl *Parent = Node.getParent(); 2118 return (Parent != NULL && 2119 InnerMatcher.matches(*Parent, Finder, Builder)); 2120} 2121 2122/// \brief Matches member expressions that are called with '->' as opposed 2123/// to '.'. 2124/// 2125/// Member calls on the implicit this pointer match as called with '->'. 2126/// 2127/// Given 2128/// \code 2129/// class Y { 2130/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 2131/// int a; 2132/// static int b; 2133/// }; 2134/// \endcode 2135/// memberExpr(isArrow()) 2136/// matches this->x, x, y.x, a, this->b 2137inline internal::Matcher<MemberExpr> isArrow() { 2138 return makeMatcher(new internal::IsArrowMatcher()); 2139} 2140 2141/// \brief Matches QualType nodes that are of integer type. 2142/// 2143/// Given 2144/// \code 2145/// void a(int); 2146/// void b(long); 2147/// void c(double); 2148/// \endcode 2149/// functionDecl(hasAnyParameter(hasType(isInteger()))) 2150/// matches "a(int)", "b(long)", but not "c(double)". 2151AST_MATCHER(QualType, isInteger) { 2152 return Node->isIntegerType(); 2153} 2154 2155/// \brief Matches QualType nodes that are const-qualified, i.e., that 2156/// include "top-level" const. 2157/// 2158/// Given 2159/// \code 2160/// void a(int); 2161/// void b(int const); 2162/// void c(const int); 2163/// void d(const int*); 2164/// void e(int const) {}; 2165/// \endcode 2166/// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 2167/// matches "void b(int const)", "void c(const int)" and 2168/// "void e(int const) {}". It does not match d as there 2169/// is no top-level const on the parameter type "const int *". 2170inline internal::Matcher<QualType> isConstQualified() { 2171 return makeMatcher(new internal::IsConstQualifiedMatcher()); 2172} 2173 2174/// \brief Matches a member expression where the member is matched by a 2175/// given matcher. 2176/// 2177/// Given 2178/// \code 2179/// struct { int first, second; } first, second; 2180/// int i(second.first); 2181/// int j(first.second); 2182/// \endcode 2183/// memberExpr(member(hasName("first"))) 2184/// matches second.first 2185/// but not first.second (because the member name there is "second"). 2186AST_MATCHER_P(MemberExpr, member, 2187 internal::Matcher<ValueDecl>, InnerMatcher) { 2188 return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 2189} 2190 2191/// \brief Matches a member expression where the object expression is 2192/// matched by a given matcher. 2193/// 2194/// Given 2195/// \code 2196/// struct X { int m; }; 2197/// void f(X x) { x.m; m; } 2198/// \endcode 2199/// memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))) 2200/// matches "x.m" and "m" 2201/// with hasObjectExpression(...) 2202/// matching "x" and the implicit object expression of "m" which has type X*. 2203AST_MATCHER_P(MemberExpr, hasObjectExpression, 2204 internal::Matcher<Expr>, InnerMatcher) { 2205 return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 2206} 2207 2208/// \brief Matches any using shadow declaration. 2209/// 2210/// Given 2211/// \code 2212/// namespace X { void b(); } 2213/// using X::b; 2214/// \endcode 2215/// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 2216/// matches \code using X::b \endcode 2217AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 2218 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 2219 for (UsingDecl::shadow_iterator II = Node.shadow_begin(); 2220 II != Node.shadow_end(); ++II) { 2221 if (InnerMatcher.matches(**II, Finder, Builder)) 2222 return true; 2223 } 2224 return false; 2225} 2226 2227/// \brief Matches a using shadow declaration where the target declaration is 2228/// matched by the given matcher. 2229/// 2230/// Given 2231/// \code 2232/// namespace X { int a; void b(); } 2233/// using X::a; 2234/// using X::b; 2235/// \endcode 2236/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 2237/// matches \code using X::b \endcode 2238/// but not \code using X::a \endcode 2239AST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 2240 internal::Matcher<NamedDecl>, InnerMatcher) { 2241 return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 2242} 2243 2244/// \brief Matches template instantiations of function, class, or static 2245/// member variable template instantiations. 2246/// 2247/// Given 2248/// \code 2249/// template <typename T> class X {}; class A {}; X<A> x; 2250/// \endcode 2251/// or 2252/// \code 2253/// template <typename T> class X {}; class A {}; template class X<A>; 2254/// \endcode 2255/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2256/// matches the template instantiation of X<A>. 2257/// 2258/// But given 2259/// \code 2260/// template <typename T> class X {}; class A {}; 2261/// template <> class X<A> {}; X<A> x; 2262/// \endcode 2263/// recordDecl(hasName("::X"), isTemplateInstantiation()) 2264/// does not match, as X<A> is an explicit template specialization. 2265/// 2266/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2267inline internal::PolymorphicMatcherWithParam0< 2268 internal::IsTemplateInstantiationMatcher> 2269isTemplateInstantiation() { 2270 return internal::PolymorphicMatcherWithParam0< 2271 internal::IsTemplateInstantiationMatcher>(); 2272} 2273 2274/// \brief Matches explicit template specializations of function, class, or 2275/// static member variable template instantiations. 2276/// 2277/// Given 2278/// \code 2279/// template<typename T> void A(T t) { } 2280/// template<> void A(int N) { } 2281/// \endcode 2282/// functionDecl(isExplicitTemplateSpecialization()) 2283/// matches the specialization A<int>(). 2284/// 2285/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 2286inline internal::PolymorphicMatcherWithParam0< 2287 internal::IsExplicitTemplateSpecializationMatcher> 2288isExplicitTemplateSpecialization() { 2289 return internal::PolymorphicMatcherWithParam0< 2290 internal::IsExplicitTemplateSpecializationMatcher>(); 2291} 2292 2293/// \brief Matches nested name specifiers. 2294/// 2295/// Given 2296/// \code 2297/// namespace ns { 2298/// struct A { static void f(); }; 2299/// void A::f() {} 2300/// void g() { A::f(); } 2301/// } 2302/// ns::A a; 2303/// \endcode 2304/// nestedNameSpecifier() 2305/// matches "ns::" and both "A::" 2306const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 2307 2308/// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 2309const internal::VariadicAllOfMatcher< 2310 NestedNameSpecifierLoc> nestedNameSpecifierLoc; 2311 2312/// \brief Matches \c NestedNameSpecifierLocs for which the given inner 2313/// NestedNameSpecifier-matcher matches. 2314inline internal::BindableMatcher<NestedNameSpecifierLoc> loc( 2315 const internal::Matcher<NestedNameSpecifier> &InnerMatcher) { 2316 return internal::BindableMatcher<NestedNameSpecifierLoc>( 2317 new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 2318 InnerMatcher)); 2319} 2320 2321/// \brief Matches nested name specifiers that specify a type matching the 2322/// given \c QualType matcher without qualifiers. 2323/// FIXME: This is a temporary solution. Switch to using Type-matchers as soon 2324/// as we have those. 2325/// 2326/// Given 2327/// \code 2328/// struct A { struct B { struct C {}; }; }; 2329/// A::B::C c; 2330/// \endcode 2331/// nestedNameSpecifier(specifiesType(hasDeclaration(recordDecl(hasName("A"))))) 2332/// matches "A::" 2333AST_MATCHER_P(NestedNameSpecifier, specifiesType, 2334 internal::Matcher<QualType>, InnerMatcher) { 2335 if (Node.getAsType() == NULL) 2336 return false; 2337 return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 2338} 2339 2340/// \brief Matches on the prefix of a \c NestedNameSpecifier or 2341/// \c NestedNameSpecifierLoc. 2342/// 2343/// Given 2344/// \code 2345/// struct A { struct B { struct C {}; }; }; 2346/// A::B::C c; 2347/// \endcode 2348/// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 2349/// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 2350/// both match "A::" 2351LOC_TRAVERSE_MATCHER(hasPrefix, NestedNameSpecifier, getPrefix) 2352 2353/// \brief Matches nested name specifiers that specify a namespace matching the 2354/// given namespace matcher. 2355/// 2356/// Given 2357/// \code 2358/// namespace ns { struct A {}; } 2359/// ns::A a; 2360/// \endcode 2361/// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 2362/// matches "ns::" 2363AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 2364 internal::Matcher<NamespaceDecl>, InnerMatcher) { 2365 if (Node.getAsNamespace() == NULL) 2366 return false; 2367 return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 2368} 2369 2370} // end namespace ast_matchers 2371} // end namespace clang 2372 2373#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H 2374