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