clang API Documentation
00001 //===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements matchers to be used together with the MatchFinder to 00011 // match AST nodes. 00012 // 00013 // Matchers are created by generator functions, which can be combined in 00014 // a functional in-language DSL to express queries over the C++ AST. 00015 // 00016 // For example, to match a class with a certain name, one would call: 00017 // recordDecl(hasName("MyClass")) 00018 // which returns a matcher that can be used to find all AST nodes that declare 00019 // a class named 'MyClass'. 00020 // 00021 // For more complicated match expressions we're often interested in accessing 00022 // multiple parts of the matched AST nodes once a match is found. In that case, 00023 // use the id(...) matcher around the match expressions that match the nodes 00024 // you want to access. 00025 // 00026 // For example, when we're interested in child classes of a certain class, we 00027 // would write: 00028 // recordDecl(hasName("MyClass"), hasChild(id("child", recordDecl()))) 00029 // When the match is found via the MatchFinder, a user provided callback will 00030 // be called with a BoundNodes instance that contains a mapping from the 00031 // strings that we provided for the id(...) calls to the nodes that were 00032 // matched. 00033 // In the given example, each time our matcher finds a match we get a callback 00034 // where "child" is bound to the CXXRecordDecl node of the matching child 00035 // class declaration. 00036 // 00037 // See ASTMatchersInternal.h for a more in-depth explanation of the 00038 // implementation details of the matcher framework. 00039 // 00040 // See ASTMatchFinder.h for how to use the generated matchers to run over 00041 // an AST. 00042 // 00043 //===----------------------------------------------------------------------===// 00044 00045 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H 00046 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H 00047 00048 #include "clang/AST/DeclFriend.h" 00049 #include "clang/AST/DeclTemplate.h" 00050 #include "clang/ASTMatchers/ASTMatchersInternal.h" 00051 #include "clang/ASTMatchers/ASTMatchersMacros.h" 00052 #include "llvm/ADT/Twine.h" 00053 #include "llvm/Support/Regex.h" 00054 #include <iterator> 00055 00056 namespace clang { 00057 namespace ast_matchers { 00058 00059 /// \brief Maps string IDs to AST nodes matched by parts of a matcher. 00060 /// 00061 /// The bound nodes are generated by calling \c bind("id") on the node matchers 00062 /// of the nodes we want to access later. 00063 /// 00064 /// The instances of BoundNodes are created by \c MatchFinder when the user's 00065 /// callbacks are executed every time a match is found. 00066 class BoundNodes { 00067 public: 00068 /// \brief Returns the AST node bound to \c ID. 00069 /// 00070 /// Returns NULL if there was no node bound to \c ID or if there is a node but 00071 /// it cannot be converted to the specified type. 00072 template <typename T> 00073 const T *getNodeAs(StringRef ID) const { 00074 return MyBoundNodes.getNodeAs<T>(ID); 00075 } 00076 00077 /// \brief Deprecated. Please use \c getNodeAs instead. 00078 /// @{ 00079 template <typename T> 00080 const T *getDeclAs(StringRef ID) const { 00081 return getNodeAs<T>(ID); 00082 } 00083 template <typename T> 00084 const T *getStmtAs(StringRef ID) const { 00085 return getNodeAs<T>(ID); 00086 } 00087 /// @} 00088 00089 /// \brief Type of mapping from binding identifiers to bound nodes. This type 00090 /// is an associative container with a key type of \c std::string and a value 00091 /// type of \c clang::ast_type_traits::DynTypedNode 00092 typedef internal::BoundNodesMap::IDToNodeMap IDToNodeMap; 00093 00094 /// \brief Retrieve mapping from binding identifiers to bound nodes. 00095 const IDToNodeMap &getMap() const { 00096 return MyBoundNodes.getMap(); 00097 } 00098 00099 private: 00100 /// \brief Create BoundNodes from a pre-filled map of bindings. 00101 BoundNodes(internal::BoundNodesMap &MyBoundNodes) 00102 : MyBoundNodes(MyBoundNodes) {} 00103 00104 internal::BoundNodesMap MyBoundNodes; 00105 00106 friend class internal::BoundNodesTreeBuilder; 00107 }; 00108 00109 /// \brief If the provided matcher matches a node, binds the node to \c ID. 00110 /// 00111 /// FIXME: Do we want to support this now that we have bind()? 00112 template <typename T> 00113 internal::Matcher<T> id(const std::string &ID, 00114 const internal::BindableMatcher<T> &InnerMatcher) { 00115 return InnerMatcher.bind(ID); 00116 } 00117 00118 /// \brief Types of matchers for the top-level classes in the AST class 00119 /// hierarchy. 00120 /// @{ 00121 typedef internal::Matcher<Decl> DeclarationMatcher; 00122 typedef internal::Matcher<Stmt> StatementMatcher; 00123 typedef internal::Matcher<QualType> TypeMatcher; 00124 typedef internal::Matcher<TypeLoc> TypeLocMatcher; 00125 typedef internal::Matcher<NestedNameSpecifier> NestedNameSpecifierMatcher; 00126 typedef internal::Matcher<NestedNameSpecifierLoc> NestedNameSpecifierLocMatcher; 00127 /// @} 00128 00129 /// \brief Matches any node. 00130 /// 00131 /// Useful when another matcher requires a child matcher, but there's no 00132 /// additional constraint. This will often be used with an explicit conversion 00133 /// to an \c internal::Matcher<> type such as \c TypeMatcher. 00134 /// 00135 /// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g., 00136 /// \code 00137 /// "int* p" and "void f()" in 00138 /// int* p; 00139 /// void f(); 00140 /// \endcode 00141 /// 00142 /// Usable as: Any Matcher 00143 inline internal::TrueMatcher anything() { return internal::TrueMatcher(); } 00144 00145 /// \brief Matches declarations. 00146 /// 00147 /// Examples matches \c X, \c C, and the friend declaration inside \c C; 00148 /// \code 00149 /// void X(); 00150 /// class C { 00151 /// friend X; 00152 /// }; 00153 /// \endcode 00154 const internal::VariadicAllOfMatcher<Decl> decl; 00155 00156 /// \brief Matches a declaration of a linkage specification. 00157 /// 00158 /// Given 00159 /// \code 00160 /// extern "C" {} 00161 /// \endcode 00162 /// linkageSpecDecl() 00163 /// matches "extern "C" {}" 00164 const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl> 00165 linkageSpecDecl; 00166 00167 /// \brief Matches a declaration of anything that could have a name. 00168 /// 00169 /// Example matches \c X, \c S, the anonymous union type, \c i, and \c U; 00170 /// \code 00171 /// typedef int X; 00172 /// struct S { 00173 /// union { 00174 /// int i; 00175 /// } U; 00176 /// }; 00177 /// \endcode 00178 const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl; 00179 00180 /// \brief Matches a declaration of a namespace. 00181 /// 00182 /// Given 00183 /// \code 00184 /// namespace {} 00185 /// namespace test {} 00186 /// \endcode 00187 /// namespaceDecl() 00188 /// matches "namespace {}" and "namespace test {}" 00189 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> namespaceDecl; 00190 00191 /// \brief Matches C++ class declarations. 00192 /// 00193 /// Example matches \c X, \c Z 00194 /// \code 00195 /// class X; 00196 /// template<class T> class Z {}; 00197 /// \endcode 00198 const internal::VariadicDynCastAllOfMatcher< 00199 Decl, 00200 CXXRecordDecl> recordDecl; 00201 00202 /// \brief Matches C++ class template declarations. 00203 /// 00204 /// Example matches \c Z 00205 /// \code 00206 /// template<class T> class Z {}; 00207 /// \endcode 00208 const internal::VariadicDynCastAllOfMatcher< 00209 Decl, 00210 ClassTemplateDecl> classTemplateDecl; 00211 00212 /// \brief Matches C++ class template specializations. 00213 /// 00214 /// Given 00215 /// \code 00216 /// template<typename T> class A {}; 00217 /// template<> class A<double> {}; 00218 /// A<int> a; 00219 /// \endcode 00220 /// classTemplateSpecializationDecl() 00221 /// matches the specializations \c A<int> and \c A<double> 00222 const internal::VariadicDynCastAllOfMatcher< 00223 Decl, 00224 ClassTemplateSpecializationDecl> classTemplateSpecializationDecl; 00225 00226 /// \brief Matches declarator declarations (field, variable, function 00227 /// and non-type template parameter declarations). 00228 /// 00229 /// Given 00230 /// \code 00231 /// class X { int y; }; 00232 /// \endcode 00233 /// declaratorDecl() 00234 /// matches \c int y. 00235 const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl> 00236 declaratorDecl; 00237 00238 /// \brief Matches parameter variable declarations. 00239 /// 00240 /// Given 00241 /// \code 00242 /// void f(int x); 00243 /// \endcode 00244 /// parmVarDecl() 00245 /// matches \c int x. 00246 const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> parmVarDecl; 00247 00248 /// \brief Matches C++ access specifier declarations. 00249 /// 00250 /// Given 00251 /// \code 00252 /// class C { 00253 /// public: 00254 /// int a; 00255 /// }; 00256 /// \endcode 00257 /// accessSpecDecl() 00258 /// matches 'public:' 00259 const internal::VariadicDynCastAllOfMatcher< 00260 Decl, 00261 AccessSpecDecl> accessSpecDecl; 00262 00263 /// \brief Matches constructor initializers. 00264 /// 00265 /// Examples matches \c i(42). 00266 /// \code 00267 /// class C { 00268 /// C() : i(42) {} 00269 /// int i; 00270 /// }; 00271 /// \endcode 00272 const internal::VariadicAllOfMatcher<CXXCtorInitializer> ctorInitializer; 00273 00274 /// \brief Matches template arguments. 00275 /// 00276 /// Given 00277 /// \code 00278 /// template <typename T> struct C {}; 00279 /// C<int> c; 00280 /// \endcode 00281 /// templateArgument() 00282 /// matches 'int' in C<int>. 00283 const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument; 00284 00285 /// \brief Matches public C++ declarations. 00286 /// 00287 /// Given 00288 /// \code 00289 /// class C { 00290 /// public: int a; 00291 /// protected: int b; 00292 /// private: int c; 00293 /// }; 00294 /// \endcode 00295 /// fieldDecl(isPublic()) 00296 /// matches 'int a;' 00297 AST_MATCHER(Decl, isPublic) { 00298 return Node.getAccess() == AS_public; 00299 } 00300 00301 /// \brief Matches protected C++ declarations. 00302 /// 00303 /// Given 00304 /// \code 00305 /// class C { 00306 /// public: int a; 00307 /// protected: int b; 00308 /// private: int c; 00309 /// }; 00310 /// \endcode 00311 /// fieldDecl(isProtected()) 00312 /// matches 'int b;' 00313 AST_MATCHER(Decl, isProtected) { 00314 return Node.getAccess() == AS_protected; 00315 } 00316 00317 /// \brief Matches private C++ declarations. 00318 /// 00319 /// Given 00320 /// \code 00321 /// class C { 00322 /// public: int a; 00323 /// protected: int b; 00324 /// private: int c; 00325 /// }; 00326 /// \endcode 00327 /// fieldDecl(isPrivate()) 00328 /// matches 'int c;' 00329 AST_MATCHER(Decl, isPrivate) { 00330 return Node.getAccess() == AS_private; 00331 } 00332 00333 /// \brief Matches a declaration that has been implicitly added 00334 /// by the compiler (eg. implicit default/copy constructors). 00335 AST_MATCHER(Decl, isImplicit) { 00336 return Node.isImplicit(); 00337 } 00338 00339 /// \brief Matches classTemplateSpecializations that have at least one 00340 /// TemplateArgument matching the given InnerMatcher. 00341 /// 00342 /// Given 00343 /// \code 00344 /// template<typename T> class A {}; 00345 /// template<> class A<double> {}; 00346 /// A<int> a; 00347 /// \endcode 00348 /// classTemplateSpecializationDecl(hasAnyTemplateArgument( 00349 /// refersToType(asString("int")))) 00350 /// matches the specialization \c A<int> 00351 AST_POLYMORPHIC_MATCHER_P( 00352 hasAnyTemplateArgument, 00353 AST_POLYMORPHIC_SUPPORTED_TYPES_2(ClassTemplateSpecializationDecl, 00354 TemplateSpecializationType), 00355 internal::Matcher<TemplateArgument>, InnerMatcher) { 00356 ArrayRef<TemplateArgument> List = 00357 internal::getTemplateSpecializationArgs(Node); 00358 return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder, 00359 Builder); 00360 } 00361 00362 /// \brief Matches expressions that match InnerMatcher after any implicit casts 00363 /// are stripped off. 00364 /// 00365 /// Parentheses and explicit casts are not discarded. 00366 /// Given 00367 /// \code 00368 /// int arr[5]; 00369 /// int a = 0; 00370 /// char b = 0; 00371 /// const int c = a; 00372 /// int *d = arr; 00373 /// long e = (long) 0l; 00374 /// \endcode 00375 /// The matchers 00376 /// \code 00377 /// varDecl(hasInitializer(ignoringImpCasts(integerLiteral()))) 00378 /// varDecl(hasInitializer(ignoringImpCasts(declRefExpr()))) 00379 /// \endcode 00380 /// would match the declarations for a, b, c, and d, but not e. 00381 /// While 00382 /// \code 00383 /// varDecl(hasInitializer(integerLiteral())) 00384 /// varDecl(hasInitializer(declRefExpr())) 00385 /// \endcode 00386 /// only match the declarations for b, c, and d. 00387 AST_MATCHER_P(Expr, ignoringImpCasts, 00388 internal::Matcher<Expr>, InnerMatcher) { 00389 return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder); 00390 } 00391 00392 /// \brief Matches expressions that match InnerMatcher after parentheses and 00393 /// casts are stripped off. 00394 /// 00395 /// Implicit and non-C Style casts are also discarded. 00396 /// Given 00397 /// \code 00398 /// int a = 0; 00399 /// char b = (0); 00400 /// void* c = reinterpret_cast<char*>(0); 00401 /// char d = char(0); 00402 /// \endcode 00403 /// The matcher 00404 /// varDecl(hasInitializer(ignoringParenCasts(integerLiteral()))) 00405 /// would match the declarations for a, b, c, and d. 00406 /// while 00407 /// varDecl(hasInitializer(integerLiteral())) 00408 /// only match the declaration for a. 00409 AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) { 00410 return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder); 00411 } 00412 00413 /// \brief Matches expressions that match InnerMatcher after implicit casts and 00414 /// parentheses are stripped off. 00415 /// 00416 /// Explicit casts are not discarded. 00417 /// Given 00418 /// \code 00419 /// int arr[5]; 00420 /// int a = 0; 00421 /// char b = (0); 00422 /// const int c = a; 00423 /// int *d = (arr); 00424 /// long e = ((long) 0l); 00425 /// \endcode 00426 /// The matchers 00427 /// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral()))) 00428 /// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr()))) 00429 /// would match the declarations for a, b, c, and d, but not e. 00430 /// while 00431 /// varDecl(hasInitializer(integerLiteral())) 00432 /// varDecl(hasInitializer(declRefExpr())) 00433 /// would only match the declaration for a. 00434 AST_MATCHER_P(Expr, ignoringParenImpCasts, 00435 internal::Matcher<Expr>, InnerMatcher) { 00436 return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder); 00437 } 00438 00439 /// \brief Matches classTemplateSpecializations where the n'th TemplateArgument 00440 /// matches the given InnerMatcher. 00441 /// 00442 /// Given 00443 /// \code 00444 /// template<typename T, typename U> class A {}; 00445 /// A<bool, int> b; 00446 /// A<int, bool> c; 00447 /// \endcode 00448 /// classTemplateSpecializationDecl(hasTemplateArgument( 00449 /// 1, refersToType(asString("int")))) 00450 /// matches the specialization \c A<bool, int> 00451 AST_POLYMORPHIC_MATCHER_P2( 00452 hasTemplateArgument, 00453 AST_POLYMORPHIC_SUPPORTED_TYPES_2(ClassTemplateSpecializationDecl, 00454 TemplateSpecializationType), 00455 unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) { 00456 ArrayRef<TemplateArgument> List = 00457 internal::getTemplateSpecializationArgs(Node); 00458 if (List.size() <= N) 00459 return false; 00460 return InnerMatcher.matches(List[N], Finder, Builder); 00461 } 00462 00463 /// \brief Matches if the number of template arguments equals \p N. 00464 /// 00465 /// Given 00466 /// \code 00467 /// template<typename T> struct C {}; 00468 /// C<int> c; 00469 /// \endcode 00470 /// classTemplateSpecializationDecl(templateArgumentCountIs(1)) 00471 /// matches C<int>. 00472 AST_POLYMORPHIC_MATCHER_P( 00473 templateArgumentCountIs, 00474 AST_POLYMORPHIC_SUPPORTED_TYPES_2(ClassTemplateSpecializationDecl, 00475 TemplateSpecializationType), 00476 unsigned, N) { 00477 return internal::getTemplateSpecializationArgs(Node).size() == N; 00478 } 00479 00480 /// \brief Matches a TemplateArgument that refers to a certain type. 00481 /// 00482 /// Given 00483 /// \code 00484 /// struct X {}; 00485 /// template<typename T> struct A {}; 00486 /// A<X> a; 00487 /// \endcode 00488 /// classTemplateSpecializationDecl(hasAnyTemplateArgument( 00489 /// refersToType(class(hasName("X"))))) 00490 /// matches the specialization \c A<X> 00491 AST_MATCHER_P(TemplateArgument, refersToType, 00492 internal::Matcher<QualType>, InnerMatcher) { 00493 if (Node.getKind() != TemplateArgument::Type) 00494 return false; 00495 return InnerMatcher.matches(Node.getAsType(), Finder, Builder); 00496 } 00497 00498 /// \brief Matches a canonical TemplateArgument that refers to a certain 00499 /// declaration. 00500 /// 00501 /// Given 00502 /// \code 00503 /// template<typename T> struct A {}; 00504 /// struct B { B* next; }; 00505 /// A<&B::next> a; 00506 /// \endcode 00507 /// classTemplateSpecializationDecl(hasAnyTemplateArgument( 00508 /// refersToDeclaration(fieldDecl(hasName("next")))) 00509 /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 00510 /// \c B::next 00511 AST_MATCHER_P(TemplateArgument, refersToDeclaration, 00512 internal::Matcher<Decl>, InnerMatcher) { 00513 if (Node.getKind() == TemplateArgument::Declaration) 00514 return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder); 00515 return false; 00516 } 00517 00518 /// \brief Matches a sugar TemplateArgument that refers to a certain expression. 00519 /// 00520 /// Given 00521 /// \code 00522 /// template<typename T> struct A {}; 00523 /// struct B { B* next; }; 00524 /// A<&B::next> a; 00525 /// \endcode 00526 /// templateSpecializationType(hasAnyTemplateArgument( 00527 /// isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))) 00528 /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 00529 /// \c B::next 00530 AST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher<Expr>, InnerMatcher) { 00531 if (Node.getKind() == TemplateArgument::Expression) 00532 return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder); 00533 return false; 00534 } 00535 00536 /// \brief Matches a TemplateArgument that is an integral value. 00537 /// 00538 /// Given 00539 /// \code 00540 /// template<int T> struct A {}; 00541 /// C<42> c; 00542 /// \endcode 00543 /// classTemplateSpecializationDecl( 00544 /// hasAnyTemplateArgument(isIntegral())) 00545 /// matches the implicit instantiation of C in C<42> 00546 /// with isIntegral() matching 42. 00547 AST_MATCHER(TemplateArgument, isIntegral) { 00548 return Node.getKind() == TemplateArgument::Integral; 00549 } 00550 00551 /// \brief Matches a TemplateArgument that referes to an integral type. 00552 /// 00553 /// Given 00554 /// \code 00555 /// template<int T> struct A {}; 00556 /// C<42> c; 00557 /// \endcode 00558 /// classTemplateSpecializationDecl( 00559 /// hasAnyTemplateArgument(refersToIntegralType(asString("int")))) 00560 /// matches the implicit instantiation of C in C<42>. 00561 AST_MATCHER_P(TemplateArgument, refersToIntegralType, 00562 internal::Matcher<QualType>, InnerMatcher) { 00563 if (Node.getKind() != TemplateArgument::Integral) 00564 return false; 00565 return InnerMatcher.matches(Node.getIntegralType(), Finder, Builder); 00566 } 00567 00568 /// \brief Matches a TemplateArgument of integral type with a given value. 00569 /// 00570 /// Note that 'Value' is a string as the template argument's value is 00571 /// an arbitrary precision integer. 'Value' must be euqal to the canonical 00572 /// representation of that integral value in base 10. 00573 /// 00574 /// Given 00575 /// \code 00576 /// template<int T> struct A {}; 00577 /// C<42> c; 00578 /// \endcode 00579 /// classTemplateSpecializationDecl( 00580 /// hasAnyTemplateArgument(equalsIntegralValue("42"))) 00581 /// matches the implicit instantiation of C in C<42>. 00582 AST_MATCHER_P(TemplateArgument, equalsIntegralValue, 00583 std::string, Value) { 00584 if (Node.getKind() != TemplateArgument::Integral) 00585 return false; 00586 return Node.getAsIntegral().toString(10) == Value; 00587 } 00588 00589 /// \brief Matches any value declaration. 00590 /// 00591 /// Example matches A, B, C and F 00592 /// \code 00593 /// enum X { A, B, C }; 00594 /// void F(); 00595 /// \endcode 00596 const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl; 00597 00598 /// \brief Matches C++ constructor declarations. 00599 /// 00600 /// Example matches Foo::Foo() and Foo::Foo(int) 00601 /// \code 00602 /// class Foo { 00603 /// public: 00604 /// Foo(); 00605 /// Foo(int); 00606 /// int DoSomething(); 00607 /// }; 00608 /// \endcode 00609 const internal::VariadicDynCastAllOfMatcher< 00610 Decl, 00611 CXXConstructorDecl> constructorDecl; 00612 00613 /// \brief Matches explicit C++ destructor declarations. 00614 /// 00615 /// Example matches Foo::~Foo() 00616 /// \code 00617 /// class Foo { 00618 /// public: 00619 /// virtual ~Foo(); 00620 /// }; 00621 /// \endcode 00622 const internal::VariadicDynCastAllOfMatcher< 00623 Decl, 00624 CXXDestructorDecl> destructorDecl; 00625 00626 /// \brief Matches enum declarations. 00627 /// 00628 /// Example matches X 00629 /// \code 00630 /// enum X { 00631 /// A, B, C 00632 /// }; 00633 /// \endcode 00634 const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl; 00635 00636 /// \brief Matches enum constants. 00637 /// 00638 /// Example matches A, B, C 00639 /// \code 00640 /// enum X { 00641 /// A, B, C 00642 /// }; 00643 /// \endcode 00644 const internal::VariadicDynCastAllOfMatcher< 00645 Decl, 00646 EnumConstantDecl> enumConstantDecl; 00647 00648 /// \brief Matches method declarations. 00649 /// 00650 /// Example matches y 00651 /// \code 00652 /// class X { void y(); }; 00653 /// \endcode 00654 const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> methodDecl; 00655 00656 /// \brief Matches variable declarations. 00657 /// 00658 /// Note: this does not match declarations of member variables, which are 00659 /// "field" declarations in Clang parlance. 00660 /// 00661 /// Example matches a 00662 /// \code 00663 /// int a; 00664 /// \endcode 00665 const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl; 00666 00667 /// \brief Matches field declarations. 00668 /// 00669 /// Given 00670 /// \code 00671 /// class X { int m; }; 00672 /// \endcode 00673 /// fieldDecl() 00674 /// matches 'm'. 00675 const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl; 00676 00677 /// \brief Matches function declarations. 00678 /// 00679 /// Example matches f 00680 /// \code 00681 /// void f(); 00682 /// \endcode 00683 const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl; 00684 00685 /// \brief Matches C++ function template declarations. 00686 /// 00687 /// Example matches f 00688 /// \code 00689 /// template<class T> void f(T t) {} 00690 /// \endcode 00691 const internal::VariadicDynCastAllOfMatcher< 00692 Decl, 00693 FunctionTemplateDecl> functionTemplateDecl; 00694 00695 /// \brief Matches friend declarations. 00696 /// 00697 /// Given 00698 /// \code 00699 /// class X { friend void foo(); }; 00700 /// \endcode 00701 /// friendDecl() 00702 /// matches 'friend void foo()'. 00703 const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl; 00704 00705 /// \brief Matches statements. 00706 /// 00707 /// Given 00708 /// \code 00709 /// { ++a; } 00710 /// \endcode 00711 /// stmt() 00712 /// matches both the compound statement '{ ++a; }' and '++a'. 00713 const internal::VariadicAllOfMatcher<Stmt> stmt; 00714 00715 /// \brief Matches declaration statements. 00716 /// 00717 /// Given 00718 /// \code 00719 /// int a; 00720 /// \endcode 00721 /// declStmt() 00722 /// matches 'int a'. 00723 const internal::VariadicDynCastAllOfMatcher< 00724 Stmt, 00725 DeclStmt> declStmt; 00726 00727 /// \brief Matches member expressions. 00728 /// 00729 /// Given 00730 /// \code 00731 /// class Y { 00732 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 00733 /// int a; static int b; 00734 /// }; 00735 /// \endcode 00736 /// memberExpr() 00737 /// matches this->x, x, y.x, a, this->b 00738 const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr; 00739 00740 /// \brief Matches call expressions. 00741 /// 00742 /// Example matches x.y() and y() 00743 /// \code 00744 /// X x; 00745 /// x.y(); 00746 /// y(); 00747 /// \endcode 00748 const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr; 00749 00750 /// \brief Matches lambda expressions. 00751 /// 00752 /// Example matches [&](){return 5;} 00753 /// \code 00754 /// [&](){return 5;} 00755 /// \endcode 00756 const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr; 00757 00758 /// \brief Matches member call expressions. 00759 /// 00760 /// Example matches x.y() 00761 /// \code 00762 /// X x; 00763 /// x.y(); 00764 /// \endcode 00765 const internal::VariadicDynCastAllOfMatcher< 00766 Stmt, 00767 CXXMemberCallExpr> memberCallExpr; 00768 00769 /// \brief Matches expressions that introduce cleanups to be run at the end 00770 /// of the sub-expression's evaluation. 00771 /// 00772 /// Example matches std::string() 00773 /// \code 00774 /// const std::string str = std::string(); 00775 /// \endcode 00776 const internal::VariadicDynCastAllOfMatcher<Stmt, ExprWithCleanups> 00777 exprWithCleanups; 00778 00779 /// \brief Matches init list expressions. 00780 /// 00781 /// Given 00782 /// \code 00783 /// int a[] = { 1, 2 }; 00784 /// struct B { int x, y; }; 00785 /// B b = { 5, 6 }; 00786 /// \endcode 00787 /// initListExpr() 00788 /// matches "{ 1, 2 }" and "{ 5, 6 }" 00789 const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr; 00790 00791 /// \brief Matches substitutions of non-type template parameters. 00792 /// 00793 /// Given 00794 /// \code 00795 /// template <int N> 00796 /// struct A { static const int n = N; }; 00797 /// struct B : public A<42> {}; 00798 /// \endcode 00799 /// substNonTypeTemplateParmExpr() 00800 /// matches "N" in the right-hand side of "static const int n = N;" 00801 const internal::VariadicDynCastAllOfMatcher<Stmt, SubstNonTypeTemplateParmExpr> 00802 substNonTypeTemplateParmExpr; 00803 00804 /// \brief Matches using declarations. 00805 /// 00806 /// Given 00807 /// \code 00808 /// namespace X { int x; } 00809 /// using X::x; 00810 /// \endcode 00811 /// usingDecl() 00812 /// matches \code using X::x \endcode 00813 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl; 00814 00815 /// \brief Matches using namespace declarations. 00816 /// 00817 /// Given 00818 /// \code 00819 /// namespace X { int x; } 00820 /// using namespace X; 00821 /// \endcode 00822 /// usingDirectiveDecl() 00823 /// matches \code using namespace X \endcode 00824 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDirectiveDecl> 00825 usingDirectiveDecl; 00826 00827 /// \brief Matches unresolved using value declarations. 00828 /// 00829 /// Given 00830 /// \code 00831 /// template<typename X> 00832 /// class C : private X { 00833 /// using X::x; 00834 /// }; 00835 /// \endcode 00836 /// unresolvedUsingValueDecl() 00837 /// matches \code using X::x \endcode 00838 const internal::VariadicDynCastAllOfMatcher< 00839 Decl, 00840 UnresolvedUsingValueDecl> unresolvedUsingValueDecl; 00841 00842 /// \brief Matches constructor call expressions (including implicit ones). 00843 /// 00844 /// Example matches string(ptr, n) and ptr within arguments of f 00845 /// (matcher = constructExpr()) 00846 /// \code 00847 /// void f(const string &a, const string &b); 00848 /// char *ptr; 00849 /// int n; 00850 /// f(string(ptr, n), ptr); 00851 /// \endcode 00852 const internal::VariadicDynCastAllOfMatcher< 00853 Stmt, 00854 CXXConstructExpr> constructExpr; 00855 00856 /// \brief Matches unresolved constructor call expressions. 00857 /// 00858 /// Example matches T(t) in return statement of f 00859 /// (matcher = unresolvedConstructExpr()) 00860 /// \code 00861 /// template <typename T> 00862 /// void f(const T& t) { return T(t); } 00863 /// \endcode 00864 const internal::VariadicDynCastAllOfMatcher< 00865 Stmt, 00866 CXXUnresolvedConstructExpr> unresolvedConstructExpr; 00867 00868 /// \brief Matches implicit and explicit this expressions. 00869 /// 00870 /// Example matches the implicit this expression in "return i". 00871 /// (matcher = thisExpr()) 00872 /// \code 00873 /// struct foo { 00874 /// int i; 00875 /// int f() { return i; } 00876 /// }; 00877 /// \endcode 00878 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> thisExpr; 00879 00880 /// \brief Matches nodes where temporaries are created. 00881 /// 00882 /// Example matches FunctionTakesString(GetStringByValue()) 00883 /// (matcher = bindTemporaryExpr()) 00884 /// \code 00885 /// FunctionTakesString(GetStringByValue()); 00886 /// FunctionTakesStringByPointer(GetStringPointer()); 00887 /// \endcode 00888 const internal::VariadicDynCastAllOfMatcher< 00889 Stmt, 00890 CXXBindTemporaryExpr> bindTemporaryExpr; 00891 00892 /// \brief Matches nodes where temporaries are materialized. 00893 /// 00894 /// Example: Given 00895 /// \code 00896 /// struct T {void func()}; 00897 /// T f(); 00898 /// void g(T); 00899 /// \endcode 00900 /// materializeTemporaryExpr() matches 'f()' in these statements 00901 /// \code 00902 /// T u(f()); 00903 /// g(f()); 00904 /// \endcode 00905 /// but does not match 00906 /// \code 00907 /// f(); 00908 /// f().func(); 00909 /// \endcode 00910 const internal::VariadicDynCastAllOfMatcher< 00911 Stmt, 00912 MaterializeTemporaryExpr> materializeTemporaryExpr; 00913 00914 /// \brief Matches new expressions. 00915 /// 00916 /// Given 00917 /// \code 00918 /// new X; 00919 /// \endcode 00920 /// newExpr() 00921 /// matches 'new X'. 00922 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> newExpr; 00923 00924 /// \brief Matches delete expressions. 00925 /// 00926 /// Given 00927 /// \code 00928 /// delete X; 00929 /// \endcode 00930 /// deleteExpr() 00931 /// matches 'delete X'. 00932 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> deleteExpr; 00933 00934 /// \brief Matches array subscript expressions. 00935 /// 00936 /// Given 00937 /// \code 00938 /// int i = a[1]; 00939 /// \endcode 00940 /// arraySubscriptExpr() 00941 /// matches "a[1]" 00942 const internal::VariadicDynCastAllOfMatcher< 00943 Stmt, 00944 ArraySubscriptExpr> arraySubscriptExpr; 00945 00946 /// \brief Matches the value of a default argument at the call site. 00947 /// 00948 /// Example matches the CXXDefaultArgExpr placeholder inserted for the 00949 /// default value of the second parameter in the call expression f(42) 00950 /// (matcher = defaultArgExpr()) 00951 /// \code 00952 /// void f(int x, int y = 0); 00953 /// f(42); 00954 /// \endcode 00955 const internal::VariadicDynCastAllOfMatcher< 00956 Stmt, 00957 CXXDefaultArgExpr> defaultArgExpr; 00958 00959 /// \brief Matches overloaded operator calls. 00960 /// 00961 /// Note that if an operator isn't overloaded, it won't match. Instead, use 00962 /// binaryOperator matcher. 00963 /// Currently it does not match operators such as new delete. 00964 /// FIXME: figure out why these do not match? 00965 /// 00966 /// Example matches both operator<<((o << b), c) and operator<<(o, b) 00967 /// (matcher = operatorCallExpr()) 00968 /// \code 00969 /// ostream &operator<< (ostream &out, int i) { }; 00970 /// ostream &o; int b = 1, c = 1; 00971 /// o << b << c; 00972 /// \endcode 00973 const internal::VariadicDynCastAllOfMatcher< 00974 Stmt, 00975 CXXOperatorCallExpr> operatorCallExpr; 00976 00977 /// \brief Matches expressions. 00978 /// 00979 /// Example matches x() 00980 /// \code 00981 /// void f() { x(); } 00982 /// \endcode 00983 const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr; 00984 00985 /// \brief Matches expressions that refer to declarations. 00986 /// 00987 /// Example matches x in if (x) 00988 /// \code 00989 /// bool x; 00990 /// if (x) {} 00991 /// \endcode 00992 const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr; 00993 00994 /// \brief Matches if statements. 00995 /// 00996 /// Example matches 'if (x) {}' 00997 /// \code 00998 /// if (x) {} 00999 /// \endcode 01000 const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt; 01001 01002 /// \brief Matches for statements. 01003 /// 01004 /// Example matches 'for (;;) {}' 01005 /// \code 01006 /// for (;;) {} 01007 /// int i[] = {1, 2, 3}; for (auto a : i); 01008 /// \endcode 01009 const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt; 01010 01011 /// \brief Matches the increment statement of a for loop. 01012 /// 01013 /// Example: 01014 /// forStmt(hasIncrement(unaryOperator(hasOperatorName("++")))) 01015 /// matches '++x' in 01016 /// \code 01017 /// for (x; x < N; ++x) { } 01018 /// \endcode 01019 AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>, 01020 InnerMatcher) { 01021 const Stmt *const Increment = Node.getInc(); 01022 return (Increment != nullptr && 01023 InnerMatcher.matches(*Increment, Finder, Builder)); 01024 } 01025 01026 /// \brief Matches the initialization statement of a for loop. 01027 /// 01028 /// Example: 01029 /// forStmt(hasLoopInit(declStmt())) 01030 /// matches 'int x = 0' in 01031 /// \code 01032 /// for (int x = 0; x < N; ++x) { } 01033 /// \endcode 01034 AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>, 01035 InnerMatcher) { 01036 const Stmt *const Init = Node.getInit(); 01037 return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder)); 01038 } 01039 01040 /// \brief Matches range-based for statements. 01041 /// 01042 /// forRangeStmt() matches 'for (auto a : i)' 01043 /// \code 01044 /// int i[] = {1, 2, 3}; for (auto a : i); 01045 /// for(int j = 0; j < 5; ++j); 01046 /// \endcode 01047 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt; 01048 01049 /// \brief Matches the initialization statement of a for loop. 01050 /// 01051 /// Example: 01052 /// forStmt(hasLoopVariable(anything())) 01053 /// matches 'int x' in 01054 /// \code 01055 /// for (int x : a) { } 01056 /// \endcode 01057 AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>, 01058 InnerMatcher) { 01059 const VarDecl *const Var = Node.getLoopVariable(); 01060 return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder)); 01061 } 01062 01063 /// \brief Matches the range initialization statement of a for loop. 01064 /// 01065 /// Example: 01066 /// forStmt(hasRangeInit(anything())) 01067 /// matches 'a' in 01068 /// \code 01069 /// for (int x : a) { } 01070 /// \endcode 01071 AST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>, 01072 InnerMatcher) { 01073 const Expr *const Init = Node.getRangeInit(); 01074 return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder)); 01075 } 01076 01077 /// \brief Matches while statements. 01078 /// 01079 /// Given 01080 /// \code 01081 /// while (true) {} 01082 /// \endcode 01083 /// whileStmt() 01084 /// matches 'while (true) {}'. 01085 const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt; 01086 01087 /// \brief Matches do statements. 01088 /// 01089 /// Given 01090 /// \code 01091 /// do {} while (true); 01092 /// \endcode 01093 /// doStmt() 01094 /// matches 'do {} while(true)' 01095 const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt; 01096 01097 /// \brief Matches break statements. 01098 /// 01099 /// Given 01100 /// \code 01101 /// while (true) { break; } 01102 /// \endcode 01103 /// breakStmt() 01104 /// matches 'break' 01105 const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt; 01106 01107 /// \brief Matches continue statements. 01108 /// 01109 /// Given 01110 /// \code 01111 /// while (true) { continue; } 01112 /// \endcode 01113 /// continueStmt() 01114 /// matches 'continue' 01115 const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> continueStmt; 01116 01117 /// \brief Matches return statements. 01118 /// 01119 /// Given 01120 /// \code 01121 /// return 1; 01122 /// \endcode 01123 /// returnStmt() 01124 /// matches 'return 1' 01125 const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt; 01126 01127 /// \brief Matches goto statements. 01128 /// 01129 /// Given 01130 /// \code 01131 /// goto FOO; 01132 /// FOO: bar(); 01133 /// \endcode 01134 /// gotoStmt() 01135 /// matches 'goto FOO' 01136 const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt; 01137 01138 /// \brief Matches label statements. 01139 /// 01140 /// Given 01141 /// \code 01142 /// goto FOO; 01143 /// FOO: bar(); 01144 /// \endcode 01145 /// labelStmt() 01146 /// matches 'FOO:' 01147 const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt; 01148 01149 /// \brief Matches switch statements. 01150 /// 01151 /// Given 01152 /// \code 01153 /// switch(a) { case 42: break; default: break; } 01154 /// \endcode 01155 /// switchStmt() 01156 /// matches 'switch(a)'. 01157 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt; 01158 01159 /// \brief Matches case and default statements inside switch statements. 01160 /// 01161 /// Given 01162 /// \code 01163 /// switch(a) { case 42: break; default: break; } 01164 /// \endcode 01165 /// switchCase() 01166 /// matches 'case 42: break;' and 'default: break;'. 01167 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase; 01168 01169 /// \brief Matches case statements inside switch statements. 01170 /// 01171 /// Given 01172 /// \code 01173 /// switch(a) { case 42: break; default: break; } 01174 /// \endcode 01175 /// caseStmt() 01176 /// matches 'case 42: break;'. 01177 const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt; 01178 01179 /// \brief Matches default statements inside switch statements. 01180 /// 01181 /// Given 01182 /// \code 01183 /// switch(a) { case 42: break; default: break; } 01184 /// \endcode 01185 /// defaultStmt() 01186 /// matches 'default: break;'. 01187 const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt; 01188 01189 /// \brief Matches compound statements. 01190 /// 01191 /// Example matches '{}' and '{{}}'in 'for (;;) {{}}' 01192 /// \code 01193 /// for (;;) {{}} 01194 /// \endcode 01195 const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt; 01196 01197 /// \brief Matches catch statements. 01198 /// 01199 /// \code 01200 /// try {} catch(int i) {} 01201 /// \endcode 01202 /// catchStmt() 01203 /// matches 'catch(int i)' 01204 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> catchStmt; 01205 01206 /// \brief Matches try statements. 01207 /// 01208 /// \code 01209 /// try {} catch(int i) {} 01210 /// \endcode 01211 /// tryStmt() 01212 /// matches 'try {}' 01213 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> tryStmt; 01214 01215 /// \brief Matches throw expressions. 01216 /// 01217 /// \code 01218 /// try { throw 5; } catch(int i) {} 01219 /// \endcode 01220 /// throwExpr() 01221 /// matches 'throw 5' 01222 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> throwExpr; 01223 01224 /// \brief Matches null statements. 01225 /// 01226 /// \code 01227 /// foo();; 01228 /// \endcode 01229 /// nullStmt() 01230 /// matches the second ';' 01231 const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt; 01232 01233 /// \brief Matches asm statements. 01234 /// 01235 /// \code 01236 /// int i = 100; 01237 /// __asm("mov al, 2"); 01238 /// \endcode 01239 /// asmStmt() 01240 /// matches '__asm("mov al, 2")' 01241 const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt; 01242 01243 /// \brief Matches bool literals. 01244 /// 01245 /// Example matches true 01246 /// \code 01247 /// true 01248 /// \endcode 01249 const internal::VariadicDynCastAllOfMatcher< 01250 Stmt, 01251 CXXBoolLiteralExpr> boolLiteral; 01252 01253 /// \brief Matches string literals (also matches wide string literals). 01254 /// 01255 /// Example matches "abcd", L"abcd" 01256 /// \code 01257 /// char *s = "abcd"; wchar_t *ws = L"abcd" 01258 /// \endcode 01259 const internal::VariadicDynCastAllOfMatcher< 01260 Stmt, 01261 StringLiteral> stringLiteral; 01262 01263 /// \brief Matches character literals (also matches wchar_t). 01264 /// 01265 /// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), 01266 /// though. 01267 /// 01268 /// Example matches 'a', L'a' 01269 /// \code 01270 /// char ch = 'a'; wchar_t chw = L'a'; 01271 /// \endcode 01272 const internal::VariadicDynCastAllOfMatcher< 01273 Stmt, 01274 CharacterLiteral> characterLiteral; 01275 01276 /// \brief Matches integer literals of all sizes / encodings, e.g. 01277 /// 1, 1L, 0x1 and 1U. 01278 /// 01279 /// Does not match character-encoded integers such as L'a'. 01280 const internal::VariadicDynCastAllOfMatcher< 01281 Stmt, 01282 IntegerLiteral> integerLiteral; 01283 01284 /// \brief Matches float literals of all sizes / encodings, e.g. 01285 /// 1.0, 1.0f, 1.0L and 1e10. 01286 /// 01287 /// Does not match implicit conversions such as 01288 /// \code 01289 /// float a = 10; 01290 /// \endcode 01291 const internal::VariadicDynCastAllOfMatcher< 01292 Stmt, 01293 FloatingLiteral> floatLiteral; 01294 01295 /// \brief Matches user defined literal operator call. 01296 /// 01297 /// Example match: "foo"_suffix 01298 const internal::VariadicDynCastAllOfMatcher< 01299 Stmt, 01300 UserDefinedLiteral> userDefinedLiteral; 01301 01302 /// \brief Matches compound (i.e. non-scalar) literals 01303 /// 01304 /// Example match: {1}, (1, 2) 01305 /// \code 01306 /// int array[4] = {1}; vector int myvec = (vector int)(1, 2); 01307 /// \endcode 01308 const internal::VariadicDynCastAllOfMatcher< 01309 Stmt, 01310 CompoundLiteralExpr> compoundLiteralExpr; 01311 01312 /// \brief Matches nullptr literal. 01313 const internal::VariadicDynCastAllOfMatcher< 01314 Stmt, 01315 CXXNullPtrLiteralExpr> nullPtrLiteralExpr; 01316 01317 /// \brief Matches binary operator expressions. 01318 /// 01319 /// Example matches a || b 01320 /// \code 01321 /// !(a || b) 01322 /// \endcode 01323 const internal::VariadicDynCastAllOfMatcher< 01324 Stmt, 01325 BinaryOperator> binaryOperator; 01326 01327 /// \brief Matches unary operator expressions. 01328 /// 01329 /// Example matches !a 01330 /// \code 01331 /// !a || b 01332 /// \endcode 01333 const internal::VariadicDynCastAllOfMatcher< 01334 Stmt, 01335 UnaryOperator> unaryOperator; 01336 01337 /// \brief Matches conditional operator expressions. 01338 /// 01339 /// Example matches a ? b : c 01340 /// \code 01341 /// (a ? b : c) + 42 01342 /// \endcode 01343 const internal::VariadicDynCastAllOfMatcher< 01344 Stmt, 01345 ConditionalOperator> conditionalOperator; 01346 01347 /// \brief Matches a reinterpret_cast expression. 01348 /// 01349 /// Either the source expression or the destination type can be matched 01350 /// using has(), but hasDestinationType() is more specific and can be 01351 /// more readable. 01352 /// 01353 /// Example matches reinterpret_cast<char*>(&p) in 01354 /// \code 01355 /// void* p = reinterpret_cast<char*>(&p); 01356 /// \endcode 01357 const internal::VariadicDynCastAllOfMatcher< 01358 Stmt, 01359 CXXReinterpretCastExpr> reinterpretCastExpr; 01360 01361 /// \brief Matches a C++ static_cast expression. 01362 /// 01363 /// \see hasDestinationType 01364 /// \see reinterpretCast 01365 /// 01366 /// Example: 01367 /// staticCastExpr() 01368 /// matches 01369 /// static_cast<long>(8) 01370 /// in 01371 /// \code 01372 /// long eight(static_cast<long>(8)); 01373 /// \endcode 01374 const internal::VariadicDynCastAllOfMatcher< 01375 Stmt, 01376 CXXStaticCastExpr> staticCastExpr; 01377 01378 /// \brief Matches a dynamic_cast expression. 01379 /// 01380 /// Example: 01381 /// dynamicCastExpr() 01382 /// matches 01383 /// dynamic_cast<D*>(&b); 01384 /// in 01385 /// \code 01386 /// struct B { virtual ~B() {} }; struct D : B {}; 01387 /// B b; 01388 /// D* p = dynamic_cast<D*>(&b); 01389 /// \endcode 01390 const internal::VariadicDynCastAllOfMatcher< 01391 Stmt, 01392 CXXDynamicCastExpr> dynamicCastExpr; 01393 01394 /// \brief Matches a const_cast expression. 01395 /// 01396 /// Example: Matches const_cast<int*>(&r) in 01397 /// \code 01398 /// int n = 42; 01399 /// const int &r(n); 01400 /// int* p = const_cast<int*>(&r); 01401 /// \endcode 01402 const internal::VariadicDynCastAllOfMatcher< 01403 Stmt, 01404 CXXConstCastExpr> constCastExpr; 01405 01406 /// \brief Matches a C-style cast expression. 01407 /// 01408 /// Example: Matches (int*) 2.2f in 01409 /// \code 01410 /// int i = (int) 2.2f; 01411 /// \endcode 01412 const internal::VariadicDynCastAllOfMatcher< 01413 Stmt, 01414 CStyleCastExpr> cStyleCastExpr; 01415 01416 /// \brief Matches explicit cast expressions. 01417 /// 01418 /// Matches any cast expression written in user code, whether it be a 01419 /// C-style cast, a functional-style cast, or a keyword cast. 01420 /// 01421 /// Does not match implicit conversions. 01422 /// 01423 /// Note: the name "explicitCast" is chosen to match Clang's terminology, as 01424 /// Clang uses the term "cast" to apply to implicit conversions as well as to 01425 /// actual cast expressions. 01426 /// 01427 /// \see hasDestinationType. 01428 /// 01429 /// Example: matches all five of the casts in 01430 /// \code 01431 /// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 01432 /// \endcode 01433 /// but does not match the implicit conversion in 01434 /// \code 01435 /// long ell = 42; 01436 /// \endcode 01437 const internal::VariadicDynCastAllOfMatcher< 01438 Stmt, 01439 ExplicitCastExpr> explicitCastExpr; 01440 01441 /// \brief Matches the implicit cast nodes of Clang's AST. 01442 /// 01443 /// This matches many different places, including function call return value 01444 /// eliding, as well as any type conversions. 01445 const internal::VariadicDynCastAllOfMatcher< 01446 Stmt, 01447 ImplicitCastExpr> implicitCastExpr; 01448 01449 /// \brief Matches any cast nodes of Clang's AST. 01450 /// 01451 /// Example: castExpr() matches each of the following: 01452 /// \code 01453 /// (int) 3; 01454 /// const_cast<Expr *>(SubExpr); 01455 /// char c = 0; 01456 /// \endcode 01457 /// but does not match 01458 /// \code 01459 /// int i = (0); 01460 /// int k = 0; 01461 /// \endcode 01462 const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr; 01463 01464 /// \brief Matches functional cast expressions 01465 /// 01466 /// Example: Matches Foo(bar); 01467 /// \code 01468 /// Foo f = bar; 01469 /// Foo g = (Foo) bar; 01470 /// Foo h = Foo(bar); 01471 /// \endcode 01472 const internal::VariadicDynCastAllOfMatcher< 01473 Stmt, 01474 CXXFunctionalCastExpr> functionalCastExpr; 01475 01476 /// \brief Matches functional cast expressions having N != 1 arguments 01477 /// 01478 /// Example: Matches Foo(bar, bar) 01479 /// \code 01480 /// Foo h = Foo(bar, bar); 01481 /// \endcode 01482 const internal::VariadicDynCastAllOfMatcher< 01483 Stmt, 01484 CXXTemporaryObjectExpr> temporaryObjectExpr; 01485 01486 /// \brief Matches \c QualTypes in the clang AST. 01487 const internal::VariadicAllOfMatcher<QualType> qualType; 01488 01489 /// \brief Matches \c Types in the clang AST. 01490 const internal::VariadicAllOfMatcher<Type> type; 01491 01492 /// \brief Matches \c TypeLocs in the clang AST. 01493 const internal::VariadicAllOfMatcher<TypeLoc> typeLoc; 01494 01495 /// \brief Matches if any of the given matchers matches. 01496 /// 01497 /// Unlike \c anyOf, \c eachOf will generate a match result for each 01498 /// matching submatcher. 01499 /// 01500 /// For example, in: 01501 /// \code 01502 /// class A { int a; int b; }; 01503 /// \endcode 01504 /// The matcher: 01505 /// \code 01506 /// recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), 01507 /// has(fieldDecl(hasName("b")).bind("v")))) 01508 /// \endcode 01509 /// will generate two results binding "v", the first of which binds 01510 /// the field declaration of \c a, the second the field declaration of 01511 /// \c b. 01512 /// 01513 /// Usable as: Any Matcher 01514 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> eachOf = { 01515 internal::EachOfVariadicOperator 01516 }; 01517 01518 /// \brief Matches if any of the given matchers matches. 01519 /// 01520 /// Usable as: Any Matcher 01521 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> anyOf = { 01522 internal::AnyOfVariadicOperator 01523 }; 01524 01525 /// \brief Matches if all given matchers match. 01526 /// 01527 /// Usable as: Any Matcher 01528 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> allOf = { 01529 internal::AllOfVariadicOperator 01530 }; 01531 01532 /// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 01533 /// 01534 /// Given 01535 /// \code 01536 /// Foo x = bar; 01537 /// int y = sizeof(x) + alignof(x); 01538 /// \endcode 01539 /// unaryExprOrTypeTraitExpr() 01540 /// matches \c sizeof(x) and \c alignof(x) 01541 const internal::VariadicDynCastAllOfMatcher< 01542 Stmt, 01543 UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr; 01544 01545 /// \brief Matches unary expressions that have a specific type of argument. 01546 /// 01547 /// Given 01548 /// \code 01549 /// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 01550 /// \endcode 01551 /// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 01552 /// matches \c sizeof(a) and \c alignof(c) 01553 AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 01554 internal::Matcher<QualType>, InnerMatcher) { 01555 const QualType ArgumentType = Node.getTypeOfArgument(); 01556 return InnerMatcher.matches(ArgumentType, Finder, Builder); 01557 } 01558 01559 /// \brief Matches unary expressions of a certain kind. 01560 /// 01561 /// Given 01562 /// \code 01563 /// int x; 01564 /// int s = sizeof(x) + alignof(x) 01565 /// \endcode 01566 /// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 01567 /// matches \c sizeof(x) 01568 AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 01569 return Node.getKind() == Kind; 01570 } 01571 01572 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching 01573 /// alignof. 01574 inline internal::Matcher<Stmt> alignOfExpr( 01575 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 01576 return stmt(unaryExprOrTypeTraitExpr(allOf( 01577 ofKind(UETT_AlignOf), InnerMatcher))); 01578 } 01579 01580 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching 01581 /// sizeof. 01582 inline internal::Matcher<Stmt> sizeOfExpr( 01583 const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 01584 return stmt(unaryExprOrTypeTraitExpr( 01585 allOf(ofKind(UETT_SizeOf), InnerMatcher))); 01586 } 01587 01588 /// \brief Matches NamedDecl nodes that have the specified name. 01589 /// 01590 /// Supports specifying enclosing namespaces or classes by prefixing the name 01591 /// with '<enclosing>::'. 01592 /// Does not match typedefs of an underlying type with the given name. 01593 /// 01594 /// Example matches X (Name == "X") 01595 /// \code 01596 /// class X; 01597 /// \endcode 01598 /// 01599 /// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 01600 /// \code 01601 /// namespace a { namespace b { class X; } } 01602 /// \endcode 01603 inline internal::Matcher<NamedDecl> hasName(const std::string &Name) { 01604 return internal::Matcher<NamedDecl>(new internal::HasNameMatcher(Name)); 01605 } 01606 01607 /// \brief Matches NamedDecl nodes whose fully qualified names contain 01608 /// a substring matched by the given RegExp. 01609 /// 01610 /// Supports specifying enclosing namespaces or classes by 01611 /// prefixing the name with '<enclosing>::'. Does not match typedefs 01612 /// of an underlying type with the given name. 01613 /// 01614 /// Example matches X (regexp == "::X") 01615 /// \code 01616 /// class X; 01617 /// \endcode 01618 /// 01619 /// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 01620 /// \code 01621 /// namespace foo { namespace bar { class X; } } 01622 /// \endcode 01623 AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 01624 assert(!RegExp.empty()); 01625 std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 01626 llvm::Regex RE(RegExp); 01627 return RE.match(FullNameString); 01628 } 01629 01630 /// \brief Matches overloaded operator names. 01631 /// 01632 /// Matches overloaded operator names specified in strings without the 01633 /// "operator" prefix: e.g. "<<". 01634 /// 01635 /// Given: 01636 /// \code 01637 /// class A { int operator*(); }; 01638 /// const A &operator<<(const A &a, const A &b); 01639 /// A a; 01640 /// a << a; // <-- This matches 01641 /// \endcode 01642 /// 01643 /// \c operatorCallExpr(hasOverloadedOperatorName("<<"))) matches the specified 01644 /// line and \c recordDecl(hasMethod(hasOverloadedOperatorName("*"))) matches 01645 /// the declaration of \c A. 01646 /// 01647 /// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl> 01648 inline internal::PolymorphicMatcherWithParam1< 01649 internal::HasOverloadedOperatorNameMatcher, StringRef, 01650 AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, FunctionDecl)> 01651 hasOverloadedOperatorName(StringRef Name) { 01652 return internal::PolymorphicMatcherWithParam1< 01653 internal::HasOverloadedOperatorNameMatcher, StringRef, 01654 AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, FunctionDecl)>( 01655 Name); 01656 } 01657 01658 /// \brief Matches C++ classes that are directly or indirectly derived from 01659 /// a class matching \c Base. 01660 /// 01661 /// Note that a class is not considered to be derived from itself. 01662 /// 01663 /// Example matches Y, Z, C (Base == hasName("X")) 01664 /// \code 01665 /// class X; 01666 /// class Y : public X {}; // directly derived 01667 /// class Z : public Y {}; // indirectly derived 01668 /// typedef X A; 01669 /// typedef A B; 01670 /// class C : public B {}; // derived from a typedef of X 01671 /// \endcode 01672 /// 01673 /// In the following example, Bar matches isDerivedFrom(hasName("X")): 01674 /// \code 01675 /// class Foo; 01676 /// typedef Foo X; 01677 /// class Bar : public Foo {}; // derived from a type that X is a typedef of 01678 /// \endcode 01679 AST_MATCHER_P(CXXRecordDecl, isDerivedFrom, 01680 internal::Matcher<NamedDecl>, Base) { 01681 return Finder->classIsDerivedFrom(&Node, Base, Builder); 01682 } 01683 01684 /// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 01685 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, StringRef, BaseName, 1) { 01686 assert(!BaseName.empty()); 01687 return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); 01688 } 01689 01690 /// \brief Similar to \c isDerivedFrom(), but also matches classes that directly 01691 /// match \c Base. 01692 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, 01693 internal::Matcher<NamedDecl>, Base, 0) { 01694 return Matcher<CXXRecordDecl>(anyOf(Base, isDerivedFrom(Base))) 01695 .matches(Node, Finder, Builder); 01696 } 01697 01698 /// \brief Overloaded method as shortcut for 01699 /// \c isSameOrDerivedFrom(hasName(...)). 01700 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, StringRef, BaseName, 01701 1) { 01702 assert(!BaseName.empty()); 01703 return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); 01704 } 01705 01706 /// \brief Matches the first method of a class or struct that satisfies \c 01707 /// InnerMatcher. 01708 /// 01709 /// Given: 01710 /// \code 01711 /// class A { void func(); }; 01712 /// class B { void member(); }; 01713 /// \code 01714 /// 01715 /// \c recordDecl(hasMethod(hasName("func"))) matches the declaration of \c A 01716 /// but not \c B. 01717 AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>, 01718 InnerMatcher) { 01719 return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(), 01720 Node.method_end(), Finder, Builder); 01721 } 01722 01723 /// \brief Matches AST nodes that have child AST nodes that match the 01724 /// provided matcher. 01725 /// 01726 /// Example matches X, Y (matcher = recordDecl(has(recordDecl(hasName("X"))) 01727 /// \code 01728 /// class X {}; // Matches X, because X::X is a class of name X inside X. 01729 /// class Y { class X {}; }; 01730 /// class Z { class Y { class X {}; }; }; // Does not match Z. 01731 /// \endcode 01732 /// 01733 /// ChildT must be an AST base type. 01734 /// 01735 /// Usable as: Any Matcher 01736 const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> 01737 LLVM_ATTRIBUTE_UNUSED has = {}; 01738 01739 /// \brief Matches AST nodes that have descendant AST nodes that match the 01740 /// provided matcher. 01741 /// 01742 /// Example matches X, Y, Z 01743 /// (matcher = recordDecl(hasDescendant(recordDecl(hasName("X"))))) 01744 /// \code 01745 /// class X {}; // Matches X, because X::X is a class of name X inside X. 01746 /// class Y { class X {}; }; 01747 /// class Z { class Y { class X {}; }; }; 01748 /// \endcode 01749 /// 01750 /// DescendantT must be an AST base type. 01751 /// 01752 /// Usable as: Any Matcher 01753 const internal::ArgumentAdaptingMatcherFunc<internal::HasDescendantMatcher> 01754 LLVM_ATTRIBUTE_UNUSED hasDescendant = {}; 01755 01756 /// \brief Matches AST nodes that have child AST nodes that match the 01757 /// provided matcher. 01758 /// 01759 /// Example matches X, Y (matcher = recordDecl(forEach(recordDecl(hasName("X"))) 01760 /// \code 01761 /// class X {}; // Matches X, because X::X is a class of name X inside X. 01762 /// class Y { class X {}; }; 01763 /// class Z { class Y { class X {}; }; }; // Does not match Z. 01764 /// \endcode 01765 /// 01766 /// ChildT must be an AST base type. 01767 /// 01768 /// As opposed to 'has', 'forEach' will cause a match for each result that 01769 /// matches instead of only on the first one. 01770 /// 01771 /// Usable as: Any Matcher 01772 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher> 01773 LLVM_ATTRIBUTE_UNUSED forEach = {}; 01774 01775 /// \brief Matches AST nodes that have descendant AST nodes that match the 01776 /// provided matcher. 01777 /// 01778 /// Example matches X, A, B, C 01779 /// (matcher = recordDecl(forEachDescendant(recordDecl(hasName("X"))))) 01780 /// \code 01781 /// class X {}; // Matches X, because X::X is a class of name X inside X. 01782 /// class A { class X {}; }; 01783 /// class B { class C { class X {}; }; }; 01784 /// \endcode 01785 /// 01786 /// DescendantT must be an AST base type. 01787 /// 01788 /// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 01789 /// each result that matches instead of only on the first one. 01790 /// 01791 /// Note: Recursively combined ForEachDescendant can cause many matches: 01792 /// recordDecl(forEachDescendant(recordDecl(forEachDescendant(recordDecl())))) 01793 /// will match 10 times (plus injected class name matches) on: 01794 /// \code 01795 /// class A { class B { class C { class D { class E {}; }; }; }; }; 01796 /// \endcode 01797 /// 01798 /// Usable as: Any Matcher 01799 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachDescendantMatcher> 01800 LLVM_ATTRIBUTE_UNUSED forEachDescendant = {}; 01801 01802 /// \brief Matches if the node or any descendant matches. 01803 /// 01804 /// Generates results for each match. 01805 /// 01806 /// For example, in: 01807 /// \code 01808 /// class A { class B {}; class C {}; }; 01809 /// \endcode 01810 /// The matcher: 01811 /// \code 01812 /// recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("m"))) 01813 /// \endcode 01814 /// will generate results for \c A, \c B and \c C. 01815 /// 01816 /// Usable as: Any Matcher 01817 template <typename T> 01818 internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) { 01819 return eachOf(Matcher, forEachDescendant(Matcher)); 01820 } 01821 01822 /// \brief Matches AST nodes that have a parent that matches the provided 01823 /// matcher. 01824 /// 01825 /// Given 01826 /// \code 01827 /// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } } 01828 /// \endcode 01829 /// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }". 01830 /// 01831 /// Usable as: Any Matcher 01832 const internal::ArgumentAdaptingMatcherFunc< 01833 internal::HasParentMatcher, internal::TypeList<Decl, Stmt>, 01834 internal::TypeList<Decl, Stmt> > LLVM_ATTRIBUTE_UNUSED hasParent = {}; 01835 01836 /// \brief Matches AST nodes that have an ancestor that matches the provided 01837 /// matcher. 01838 /// 01839 /// Given 01840 /// \code 01841 /// void f() { if (true) { int x = 42; } } 01842 /// void g() { for (;;) { int x = 43; } } 01843 /// \endcode 01844 /// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43. 01845 /// 01846 /// Usable as: Any Matcher 01847 const internal::ArgumentAdaptingMatcherFunc< 01848 internal::HasAncestorMatcher, internal::TypeList<Decl, Stmt>, 01849 internal::TypeList<Decl, Stmt> > LLVM_ATTRIBUTE_UNUSED hasAncestor = {}; 01850 01851 /// \brief Matches if the provided matcher does not match. 01852 /// 01853 /// Example matches Y (matcher = recordDecl(unless(hasName("X")))) 01854 /// \code 01855 /// class X {}; 01856 /// class Y {}; 01857 /// \endcode 01858 /// 01859 /// Usable as: Any Matcher 01860 const internal::VariadicOperatorMatcherFunc<1, 1> unless = { 01861 internal::NotUnaryOperator 01862 }; 01863 01864 /// \brief Matches a node if the declaration associated with that node 01865 /// matches the given matcher. 01866 /// 01867 /// The associated declaration is: 01868 /// - for type nodes, the declaration of the underlying type 01869 /// - for CallExpr, the declaration of the callee 01870 /// - for MemberExpr, the declaration of the referenced member 01871 /// - for CXXConstructExpr, the declaration of the constructor 01872 /// 01873 /// Also usable as Matcher<T> for any T supporting the getDecl() member 01874 /// function. e.g. various subtypes of clang::Type and various expressions. 01875 /// 01876 /// Usable as: Matcher<CallExpr>, Matcher<CXXConstructExpr>, 01877 /// Matcher<DeclRefExpr>, Matcher<EnumType>, Matcher<InjectedClassNameType>, 01878 /// Matcher<LabelStmt>, Matcher<MemberExpr>, Matcher<QualType>, 01879 /// Matcher<RecordType>, Matcher<TagType>, 01880 /// Matcher<TemplateSpecializationType>, Matcher<TemplateTypeParmType>, 01881 /// Matcher<TypedefType>, Matcher<UnresolvedUsingType> 01882 inline internal::PolymorphicMatcherWithParam1< 01883 internal::HasDeclarationMatcher, internal::Matcher<Decl>, 01884 void(internal::HasDeclarationSupportedTypes)> 01885 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 01886 return internal::PolymorphicMatcherWithParam1< 01887 internal::HasDeclarationMatcher, internal::Matcher<Decl>, 01888 void(internal::HasDeclarationSupportedTypes)>(InnerMatcher); 01889 } 01890 01891 /// \brief Matches on the implicit object argument of a member call expression. 01892 /// 01893 /// Example matches y.x() (matcher = callExpr(on(hasType(recordDecl(hasName("Y")))))) 01894 /// \code 01895 /// class Y { public: void x(); }; 01896 /// void z() { Y y; y.x(); }", 01897 /// \endcode 01898 /// 01899 /// FIXME: Overload to allow directly matching types? 01900 AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 01901 InnerMatcher) { 01902 const Expr *ExprNode = Node.getImplicitObjectArgument() 01903 ->IgnoreParenImpCasts(); 01904 return (ExprNode != nullptr && 01905 InnerMatcher.matches(*ExprNode, Finder, Builder)); 01906 } 01907 01908 /// \brief Matches if the call expression's callee expression matches. 01909 /// 01910 /// Given 01911 /// \code 01912 /// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 01913 /// void f() { f(); } 01914 /// \endcode 01915 /// callExpr(callee(expr())) 01916 /// matches this->x(), x(), y.x(), f() 01917 /// with callee(...) 01918 /// matching this->x, x, y.x, f respectively 01919 /// 01920 /// Note: Callee cannot take the more general internal::Matcher<Expr> 01921 /// because this introduces ambiguous overloads with calls to Callee taking a 01922 /// internal::Matcher<Decl>, as the matcher hierarchy is purely 01923 /// implemented in terms of implicit casts. 01924 AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 01925 InnerMatcher) { 01926 const Expr *ExprNode = Node.getCallee(); 01927 return (ExprNode != nullptr && 01928 InnerMatcher.matches(*ExprNode, Finder, Builder)); 01929 } 01930 01931 /// \brief Matches if the call expression's callee's declaration matches the 01932 /// given matcher. 01933 /// 01934 /// Example matches y.x() (matcher = callExpr(callee(methodDecl(hasName("x"))))) 01935 /// \code 01936 /// class Y { public: void x(); }; 01937 /// void z() { Y y; y.x(); } 01938 /// \endcode 01939 AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher, 01940 1) { 01941 return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder); 01942 } 01943 01944 /// \brief Matches if the expression's or declaration's type matches a type 01945 /// matcher. 01946 /// 01947 /// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 01948 /// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 01949 /// \code 01950 /// class X {}; 01951 /// void y(X &x) { x; X z; } 01952 /// \endcode 01953 AST_POLYMORPHIC_MATCHER_P_OVERLOAD( 01954 hasType, AST_POLYMORPHIC_SUPPORTED_TYPES_2(Expr, ValueDecl), 01955 internal::Matcher<QualType>, InnerMatcher, 0) { 01956 return InnerMatcher.matches(Node.getType(), Finder, Builder); 01957 } 01958 01959 /// \brief Overloaded to match the declaration of the expression's or value 01960 /// declaration's type. 01961 /// 01962 /// In case of a value declaration (for example a variable declaration), 01963 /// this resolves one layer of indirection. For example, in the value 01964 /// declaration "X x;", recordDecl(hasName("X")) matches the declaration of X, 01965 /// while varDecl(hasType(recordDecl(hasName("X")))) matches the declaration 01966 /// of x." 01967 /// 01968 /// Example matches x (matcher = expr(hasType(recordDecl(hasName("X"))))) 01969 /// and z (matcher = varDecl(hasType(recordDecl(hasName("X"))))) 01970 /// \code 01971 /// class X {}; 01972 /// void y(X &x) { x; X z; } 01973 /// \endcode 01974 /// 01975 /// Usable as: Matcher<Expr>, Matcher<ValueDecl> 01976 AST_POLYMORPHIC_MATCHER_P_OVERLOAD( 01977 hasType, AST_POLYMORPHIC_SUPPORTED_TYPES_2(Expr, ValueDecl), 01978 internal::Matcher<Decl>, InnerMatcher, 1) { 01979 return qualType(hasDeclaration(InnerMatcher)) 01980 .matches(Node.getType(), Finder, Builder); 01981 } 01982 01983 /// \brief Matches if the type location of the declarator decl's type matches 01984 /// the inner matcher. 01985 /// 01986 /// Given 01987 /// \code 01988 /// int x; 01989 /// \endcode 01990 /// declaratorDecl(hasTypeLoc(loc(asString("int")))) 01991 /// matches int x 01992 AST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) { 01993 if (!Node.getTypeSourceInfo()) 01994 // This happens for example for implicit destructors. 01995 return false; 01996 return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder); 01997 } 01998 01999 /// \brief Matches if the matched type is represented by the given string. 02000 /// 02001 /// Given 02002 /// \code 02003 /// class Y { public: void x(); }; 02004 /// void z() { Y* y; y->x(); } 02005 /// \endcode 02006 /// callExpr(on(hasType(asString("class Y *")))) 02007 /// matches y->x() 02008 AST_MATCHER_P(QualType, asString, std::string, Name) { 02009 return Name == Node.getAsString(); 02010 } 02011 02012 /// \brief Matches if the matched type is a pointer type and the pointee type 02013 /// matches the specified matcher. 02014 /// 02015 /// Example matches y->x() 02016 /// (matcher = callExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))))) 02017 /// \code 02018 /// class Y { public: void x(); }; 02019 /// void z() { Y *y; y->x(); } 02020 /// \endcode 02021 AST_MATCHER_P( 02022 QualType, pointsTo, internal::Matcher<QualType>, 02023 InnerMatcher) { 02024 return (!Node.isNull() && Node->isPointerType() && 02025 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 02026 } 02027 02028 /// \brief Overloaded to match the pointee type's declaration. 02029 AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>, 02030 InnerMatcher, 1) { 02031 return pointsTo(qualType(hasDeclaration(InnerMatcher))) 02032 .matches(Node, Finder, Builder); 02033 } 02034 02035 /// \brief Matches if the matched type is a reference type and the referenced 02036 /// type matches the specified matcher. 02037 /// 02038 /// Example matches X &x and const X &y 02039 /// (matcher = varDecl(hasType(references(recordDecl(hasName("X")))))) 02040 /// \code 02041 /// class X { 02042 /// void a(X b) { 02043 /// X &x = b; 02044 /// const X &y = b; 02045 /// } 02046 /// }; 02047 /// \endcode 02048 AST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 02049 InnerMatcher) { 02050 return (!Node.isNull() && Node->isReferenceType() && 02051 InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 02052 } 02053 02054 /// \brief Matches QualTypes whose canonical type matches InnerMatcher. 02055 /// 02056 /// Given: 02057 /// \code 02058 /// typedef int &int_ref; 02059 /// int a; 02060 /// int_ref b = a; 02061 /// \code 02062 /// 02063 /// \c varDecl(hasType(qualType(referenceType()))))) will not match the 02064 /// declaration of b but \c 02065 /// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does. 02066 AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>, 02067 InnerMatcher) { 02068 if (Node.isNull()) 02069 return false; 02070 return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder); 02071 } 02072 02073 /// \brief Overloaded to match the referenced type's declaration. 02074 AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>, 02075 InnerMatcher, 1) { 02076 return references(qualType(hasDeclaration(InnerMatcher))) 02077 .matches(Node, Finder, Builder); 02078 } 02079 02080 AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 02081 internal::Matcher<Expr>, InnerMatcher) { 02082 const Expr *ExprNode = Node.getImplicitObjectArgument(); 02083 return (ExprNode != nullptr && 02084 InnerMatcher.matches(*ExprNode, Finder, Builder)); 02085 } 02086 02087 /// \brief Matches if the expression's type either matches the specified 02088 /// matcher, or is a pointer to a type that matches the InnerMatcher. 02089 AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 02090 internal::Matcher<QualType>, InnerMatcher, 0) { 02091 return onImplicitObjectArgument( 02092 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 02093 .matches(Node, Finder, Builder); 02094 } 02095 02096 /// \brief Overloaded to match the type's declaration. 02097 AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 02098 internal::Matcher<Decl>, InnerMatcher, 1) { 02099 return onImplicitObjectArgument( 02100 anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 02101 .matches(Node, Finder, Builder); 02102 } 02103 02104 /// \brief Matches a DeclRefExpr that refers to a declaration that matches the 02105 /// specified matcher. 02106 /// 02107 /// Example matches x in if(x) 02108 /// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 02109 /// \code 02110 /// bool x; 02111 /// if (x) {} 02112 /// \endcode 02113 AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 02114 InnerMatcher) { 02115 const Decl *DeclNode = Node.getDecl(); 02116 return (DeclNode != nullptr && 02117 InnerMatcher.matches(*DeclNode, Finder, Builder)); 02118 } 02119 02120 /// \brief Matches a \c DeclRefExpr that refers to a declaration through a 02121 /// specific using shadow declaration. 02122 /// 02123 /// FIXME: This currently only works for functions. Fix. 02124 /// 02125 /// Given 02126 /// \code 02127 /// namespace a { void f() {} } 02128 /// using a::f; 02129 /// void g() { 02130 /// f(); // Matches this .. 02131 /// a::f(); // .. but not this. 02132 /// } 02133 /// \endcode 02134 /// declRefExpr(throughUsingDeclaration(anything())) 02135 /// matches \c f() 02136 AST_MATCHER_P(DeclRefExpr, throughUsingDecl, 02137 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 02138 const NamedDecl *FoundDecl = Node.getFoundDecl(); 02139 if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl)) 02140 return InnerMatcher.matches(*UsingDecl, Finder, Builder); 02141 return false; 02142 } 02143 02144 /// \brief Matches the Decl of a DeclStmt which has a single declaration. 02145 /// 02146 /// Given 02147 /// \code 02148 /// int a, b; 02149 /// int c; 02150 /// \endcode 02151 /// declStmt(hasSingleDecl(anything())) 02152 /// matches 'int c;' but not 'int a, b;'. 02153 AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 02154 if (Node.isSingleDecl()) { 02155 const Decl *FoundDecl = Node.getSingleDecl(); 02156 return InnerMatcher.matches(*FoundDecl, Finder, Builder); 02157 } 02158 return false; 02159 } 02160 02161 /// \brief Matches a variable declaration that has an initializer expression 02162 /// that matches the given matcher. 02163 /// 02164 /// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 02165 /// \code 02166 /// bool y() { return true; } 02167 /// bool x = y(); 02168 /// \endcode 02169 AST_MATCHER_P( 02170 VarDecl, hasInitializer, internal::Matcher<Expr>, 02171 InnerMatcher) { 02172 const Expr *Initializer = Node.getAnyInitializer(); 02173 return (Initializer != nullptr && 02174 InnerMatcher.matches(*Initializer, Finder, Builder)); 02175 } 02176 02177 /// \brief Matches a variable declaration that has function scope and is a 02178 /// non-static local variable. 02179 /// 02180 /// Example matches x (matcher = varDecl(hasLocalStorage()) 02181 /// \code 02182 /// void f() { 02183 /// int x; 02184 /// static int y; 02185 /// } 02186 /// int z; 02187 /// \endcode 02188 AST_MATCHER(VarDecl, hasLocalStorage) { 02189 return Node.hasLocalStorage(); 02190 } 02191 02192 /// \brief Matches a variable declaration that does not have local storage. 02193 /// 02194 /// Example matches y and z (matcher = varDecl(hasGlobalStorage()) 02195 /// \code 02196 /// void f() { 02197 /// int x; 02198 /// static int y; 02199 /// } 02200 /// int z; 02201 /// \endcode 02202 AST_MATCHER(VarDecl, hasGlobalStorage) { 02203 return Node.hasGlobalStorage(); 02204 } 02205 02206 /// \brief Checks that a call expression or a constructor call expression has 02207 /// a specific number of arguments (including absent default arguments). 02208 /// 02209 /// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 02210 /// \code 02211 /// void f(int x, int y); 02212 /// f(0, 0); 02213 /// \endcode 02214 AST_POLYMORPHIC_MATCHER_P(argumentCountIs, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 02215 CallExpr, CXXConstructExpr), 02216 unsigned, N) { 02217 return Node.getNumArgs() == N; 02218 } 02219 02220 /// \brief Matches the n'th argument of a call expression or a constructor 02221 /// call expression. 02222 /// 02223 /// Example matches y in x(y) 02224 /// (matcher = callExpr(hasArgument(0, declRefExpr()))) 02225 /// \code 02226 /// void x(int) { int y; x(y); } 02227 /// \endcode 02228 AST_POLYMORPHIC_MATCHER_P2( 02229 hasArgument, 02230 AST_POLYMORPHIC_SUPPORTED_TYPES_2(CallExpr, CXXConstructExpr), 02231 unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 02232 return (N < Node.getNumArgs() && 02233 InnerMatcher.matches( 02234 *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 02235 } 02236 02237 /// \brief Matches declaration statements that contain a specific number of 02238 /// declarations. 02239 /// 02240 /// Example: Given 02241 /// \code 02242 /// int a, b; 02243 /// int c; 02244 /// int d = 2, e; 02245 /// \endcode 02246 /// declCountIs(2) 02247 /// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 02248 AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 02249 return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 02250 } 02251 02252 /// \brief Matches the n'th declaration of a declaration statement. 02253 /// 02254 /// Note that this does not work for global declarations because the AST 02255 /// breaks up multiple-declaration DeclStmt's into multiple single-declaration 02256 /// DeclStmt's. 02257 /// Example: Given non-global declarations 02258 /// \code 02259 /// int a, b = 0; 02260 /// int c; 02261 /// int d = 2, e; 02262 /// \endcode 02263 /// declStmt(containsDeclaration( 02264 /// 0, varDecl(hasInitializer(anything())))) 02265 /// matches only 'int d = 2, e;', and 02266 /// declStmt(containsDeclaration(1, varDecl())) 02267 /// \code 02268 /// matches 'int a, b = 0' as well as 'int d = 2, e;' 02269 /// but 'int c;' is not matched. 02270 /// \endcode 02271 AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 02272 internal::Matcher<Decl>, InnerMatcher) { 02273 const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 02274 if (N >= NumDecls) 02275 return false; 02276 DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 02277 std::advance(Iterator, N); 02278 return InnerMatcher.matches(**Iterator, Finder, Builder); 02279 } 02280 02281 /// \brief Matches a constructor initializer. 02282 /// 02283 /// Given 02284 /// \code 02285 /// struct Foo { 02286 /// Foo() : foo_(1) { } 02287 /// int foo_; 02288 /// }; 02289 /// \endcode 02290 /// recordDecl(has(constructorDecl(hasAnyConstructorInitializer(anything())))) 02291 /// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 02292 AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 02293 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 02294 return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(), 02295 Node.init_end(), Finder, Builder); 02296 } 02297 02298 /// \brief Matches the field declaration of a constructor initializer. 02299 /// 02300 /// Given 02301 /// \code 02302 /// struct Foo { 02303 /// Foo() : foo_(1) { } 02304 /// int foo_; 02305 /// }; 02306 /// \endcode 02307 /// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 02308 /// forField(hasName("foo_")))))) 02309 /// matches Foo 02310 /// with forField matching foo_ 02311 AST_MATCHER_P(CXXCtorInitializer, forField, 02312 internal::Matcher<FieldDecl>, InnerMatcher) { 02313 const FieldDecl *NodeAsDecl = Node.getMember(); 02314 return (NodeAsDecl != nullptr && 02315 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 02316 } 02317 02318 /// \brief Matches the initializer expression of a constructor initializer. 02319 /// 02320 /// Given 02321 /// \code 02322 /// struct Foo { 02323 /// Foo() : foo_(1) { } 02324 /// int foo_; 02325 /// }; 02326 /// \endcode 02327 /// recordDecl(has(constructorDecl(hasAnyConstructorInitializer( 02328 /// withInitializer(integerLiteral(equals(1))))))) 02329 /// matches Foo 02330 /// with withInitializer matching (1) 02331 AST_MATCHER_P(CXXCtorInitializer, withInitializer, 02332 internal::Matcher<Expr>, InnerMatcher) { 02333 const Expr* NodeAsExpr = Node.getInit(); 02334 return (NodeAsExpr != nullptr && 02335 InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 02336 } 02337 02338 /// \brief Matches a constructor initializer if it is explicitly written in 02339 /// code (as opposed to implicitly added by the compiler). 02340 /// 02341 /// Given 02342 /// \code 02343 /// struct Foo { 02344 /// Foo() { } 02345 /// Foo(int) : foo_("A") { } 02346 /// string foo_; 02347 /// }; 02348 /// \endcode 02349 /// constructorDecl(hasAnyConstructorInitializer(isWritten())) 02350 /// will match Foo(int), but not Foo() 02351 AST_MATCHER(CXXCtorInitializer, isWritten) { 02352 return Node.isWritten(); 02353 } 02354 02355 /// \brief Matches any argument of a call expression or a constructor call 02356 /// expression. 02357 /// 02358 /// Given 02359 /// \code 02360 /// void x(int, int, int) { int y; x(1, y, 42); } 02361 /// \endcode 02362 /// callExpr(hasAnyArgument(declRefExpr())) 02363 /// matches x(1, y, 42) 02364 /// with hasAnyArgument(...) 02365 /// matching y 02366 /// 02367 /// FIXME: Currently this will ignore parentheses and implicit casts on 02368 /// the argument before applying the inner matcher. We'll want to remove 02369 /// this to allow for greater control by the user once \c ignoreImplicit() 02370 /// has been implemented. 02371 AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 02372 CallExpr, CXXConstructExpr), 02373 internal::Matcher<Expr>, InnerMatcher) { 02374 for (const Expr *Arg : Node.arguments()) { 02375 BoundNodesTreeBuilder Result(*Builder); 02376 if (InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, &Result)) { 02377 *Builder = std::move(Result); 02378 return true; 02379 } 02380 } 02381 return false; 02382 } 02383 02384 /// \brief Matches a constructor call expression which uses list initialization. 02385 AST_MATCHER(CXXConstructExpr, isListInitialization) { 02386 return Node.isListInitialization(); 02387 } 02388 02389 /// \brief Matches the n'th parameter of a function declaration. 02390 /// 02391 /// Given 02392 /// \code 02393 /// class X { void f(int x) {} }; 02394 /// \endcode 02395 /// methodDecl(hasParameter(0, hasType(varDecl()))) 02396 /// matches f(int x) {} 02397 /// with hasParameter(...) 02398 /// matching int x 02399 AST_MATCHER_P2(FunctionDecl, hasParameter, 02400 unsigned, N, internal::Matcher<ParmVarDecl>, 02401 InnerMatcher) { 02402 return (N < Node.getNumParams() && 02403 InnerMatcher.matches( 02404 *Node.getParamDecl(N), Finder, Builder)); 02405 } 02406 02407 /// \brief Matches any parameter of a function declaration. 02408 /// 02409 /// Does not match the 'this' parameter of a method. 02410 /// 02411 /// Given 02412 /// \code 02413 /// class X { void f(int x, int y, int z) {} }; 02414 /// \endcode 02415 /// methodDecl(hasAnyParameter(hasName("y"))) 02416 /// matches f(int x, int y, int z) {} 02417 /// with hasAnyParameter(...) 02418 /// matching int y 02419 AST_MATCHER_P(FunctionDecl, hasAnyParameter, 02420 internal::Matcher<ParmVarDecl>, InnerMatcher) { 02421 return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(), 02422 Node.param_end(), Finder, Builder); 02423 } 02424 02425 /// \brief Matches \c FunctionDecls that have a specific parameter count. 02426 /// 02427 /// Given 02428 /// \code 02429 /// void f(int i) {} 02430 /// void g(int i, int j) {} 02431 /// \endcode 02432 /// functionDecl(parameterCountIs(2)) 02433 /// matches g(int i, int j) {} 02434 AST_MATCHER_P(FunctionDecl, parameterCountIs, unsigned, N) { 02435 return Node.getNumParams() == N; 02436 } 02437 02438 /// \brief Matches the return type of a function declaration. 02439 /// 02440 /// Given: 02441 /// \code 02442 /// class X { int f() { return 1; } }; 02443 /// \endcode 02444 /// methodDecl(returns(asString("int"))) 02445 /// matches int f() { return 1; } 02446 AST_MATCHER_P(FunctionDecl, returns, 02447 internal::Matcher<QualType>, InnerMatcher) { 02448 return InnerMatcher.matches(Node.getReturnType(), Finder, Builder); 02449 } 02450 02451 /// \brief Matches extern "C" function declarations. 02452 /// 02453 /// Given: 02454 /// \code 02455 /// extern "C" void f() {} 02456 /// extern "C" { void g() {} } 02457 /// void h() {} 02458 /// \endcode 02459 /// functionDecl(isExternC()) 02460 /// matches the declaration of f and g, but not the declaration h 02461 AST_MATCHER(FunctionDecl, isExternC) { 02462 return Node.isExternC(); 02463 } 02464 02465 /// \brief Matches deleted function declarations. 02466 /// 02467 /// Given: 02468 /// \code 02469 /// void Func(); 02470 /// void DeletedFunc() = delete; 02471 /// \endcode 02472 /// functionDecl(isDeleted()) 02473 /// matches the declaration of DeletedFunc, but not Func. 02474 AST_MATCHER(FunctionDecl, isDeleted) { 02475 return Node.isDeleted(); 02476 } 02477 02478 /// \brief Matches the condition expression of an if statement, for loop, 02479 /// or conditional operator. 02480 /// 02481 /// Example matches true (matcher = hasCondition(boolLiteral(equals(true)))) 02482 /// \code 02483 /// if (true) {} 02484 /// \endcode 02485 AST_POLYMORPHIC_MATCHER_P( 02486 hasCondition, AST_POLYMORPHIC_SUPPORTED_TYPES_5( 02487 IfStmt, ForStmt, WhileStmt, DoStmt, ConditionalOperator), 02488 internal::Matcher<Expr>, InnerMatcher) { 02489 const Expr *const Condition = Node.getCond(); 02490 return (Condition != nullptr && 02491 InnerMatcher.matches(*Condition, Finder, Builder)); 02492 } 02493 02494 /// \brief Matches the then-statement of an if statement. 02495 /// 02496 /// Examples matches the if statement 02497 /// (matcher = ifStmt(hasThen(boolLiteral(equals(true))))) 02498 /// \code 02499 /// if (false) true; else false; 02500 /// \endcode 02501 AST_MATCHER_P(IfStmt, hasThen, internal::Matcher<Stmt>, InnerMatcher) { 02502 const Stmt *const Then = Node.getThen(); 02503 return (Then != nullptr && InnerMatcher.matches(*Then, Finder, Builder)); 02504 } 02505 02506 /// \brief Matches the else-statement of an if statement. 02507 /// 02508 /// Examples matches the if statement 02509 /// (matcher = ifStmt(hasElse(boolLiteral(equals(true))))) 02510 /// \code 02511 /// if (false) false; else true; 02512 /// \endcode 02513 AST_MATCHER_P(IfStmt, hasElse, internal::Matcher<Stmt>, InnerMatcher) { 02514 const Stmt *const Else = Node.getElse(); 02515 return (Else != nullptr && InnerMatcher.matches(*Else, Finder, Builder)); 02516 } 02517 02518 /// \brief Matches if a node equals a previously bound node. 02519 /// 02520 /// Matches a node if it equals the node previously bound to \p ID. 02521 /// 02522 /// Given 02523 /// \code 02524 /// class X { int a; int b; }; 02525 /// \endcode 02526 /// recordDecl( 02527 /// has(fieldDecl(hasName("a"), hasType(type().bind("t")))), 02528 /// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))) 02529 /// matches the class \c X, as \c a and \c b have the same type. 02530 /// 02531 /// Note that when multiple matches are involved via \c forEach* matchers, 02532 /// \c equalsBoundNodes acts as a filter. 02533 /// For example: 02534 /// compoundStmt( 02535 /// forEachDescendant(varDecl().bind("d")), 02536 /// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))) 02537 /// will trigger a match for each combination of variable declaration 02538 /// and reference to that variable declaration within a compound statement. 02539 AST_POLYMORPHIC_MATCHER_P(equalsBoundNode, AST_POLYMORPHIC_SUPPORTED_TYPES_4( 02540 Stmt, Decl, Type, QualType), 02541 std::string, ID) { 02542 // FIXME: Figure out whether it makes sense to allow this 02543 // on any other node types. 02544 // For *Loc it probably does not make sense, as those seem 02545 // unique. For NestedNameSepcifier it might make sense, as 02546 // those also have pointer identity, but I'm not sure whether 02547 // they're ever reused. 02548 internal::NotEqualsBoundNodePredicate Predicate; 02549 Predicate.ID = ID; 02550 Predicate.Node = ast_type_traits::DynTypedNode::create(Node); 02551 return Builder->removeBindings(Predicate); 02552 } 02553 02554 /// \brief Matches the condition variable statement in an if statement. 02555 /// 02556 /// Given 02557 /// \code 02558 /// if (A* a = GetAPointer()) {} 02559 /// \endcode 02560 /// hasConditionVariableStatement(...) 02561 /// matches 'A* a = GetAPointer()'. 02562 AST_MATCHER_P(IfStmt, hasConditionVariableStatement, 02563 internal::Matcher<DeclStmt>, InnerMatcher) { 02564 const DeclStmt* const DeclarationStatement = 02565 Node.getConditionVariableDeclStmt(); 02566 return DeclarationStatement != nullptr && 02567 InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 02568 } 02569 02570 /// \brief Matches the index expression of an array subscript expression. 02571 /// 02572 /// Given 02573 /// \code 02574 /// int i[5]; 02575 /// void f() { i[1] = 42; } 02576 /// \endcode 02577 /// arraySubscriptExpression(hasIndex(integerLiteral())) 02578 /// matches \c i[1] with the \c integerLiteral() matching \c 1 02579 AST_MATCHER_P(ArraySubscriptExpr, hasIndex, 02580 internal::Matcher<Expr>, InnerMatcher) { 02581 if (const Expr* Expression = Node.getIdx()) 02582 return InnerMatcher.matches(*Expression, Finder, Builder); 02583 return false; 02584 } 02585 02586 /// \brief Matches the base expression of an array subscript expression. 02587 /// 02588 /// Given 02589 /// \code 02590 /// int i[5]; 02591 /// void f() { i[1] = 42; } 02592 /// \endcode 02593 /// arraySubscriptExpression(hasBase(implicitCastExpr( 02594 /// hasSourceExpression(declRefExpr())))) 02595 /// matches \c i[1] with the \c declRefExpr() matching \c i 02596 AST_MATCHER_P(ArraySubscriptExpr, hasBase, 02597 internal::Matcher<Expr>, InnerMatcher) { 02598 if (const Expr* Expression = Node.getBase()) 02599 return InnerMatcher.matches(*Expression, Finder, Builder); 02600 return false; 02601 } 02602 02603 /// \brief Matches a 'for', 'while', or 'do while' statement that has 02604 /// a given body. 02605 /// 02606 /// Given 02607 /// \code 02608 /// for (;;) {} 02609 /// \endcode 02610 /// hasBody(compoundStmt()) 02611 /// matches 'for (;;) {}' 02612 /// with compoundStmt() 02613 /// matching '{}' 02614 AST_POLYMORPHIC_MATCHER_P(hasBody, 02615 AST_POLYMORPHIC_SUPPORTED_TYPES_4(DoStmt, ForStmt, 02616 WhileStmt, 02617 CXXForRangeStmt), 02618 internal::Matcher<Stmt>, InnerMatcher) { 02619 const Stmt *const Statement = Node.getBody(); 02620 return (Statement != nullptr && 02621 InnerMatcher.matches(*Statement, Finder, Builder)); 02622 } 02623 02624 /// \brief Matches compound statements where at least one substatement matches 02625 /// a given matcher. 02626 /// 02627 /// Given 02628 /// \code 02629 /// { {}; 1+2; } 02630 /// \endcode 02631 /// hasAnySubstatement(compoundStmt()) 02632 /// matches '{ {}; 1+2; }' 02633 /// with compoundStmt() 02634 /// matching '{}' 02635 AST_MATCHER_P(CompoundStmt, hasAnySubstatement, 02636 internal::Matcher<Stmt>, InnerMatcher) { 02637 return matchesFirstInPointerRange(InnerMatcher, Node.body_begin(), 02638 Node.body_end(), Finder, Builder); 02639 } 02640 02641 /// \brief Checks that a compound statement contains a specific number of 02642 /// child statements. 02643 /// 02644 /// Example: Given 02645 /// \code 02646 /// { for (;;) {} } 02647 /// \endcode 02648 /// compoundStmt(statementCountIs(0))) 02649 /// matches '{}' 02650 /// but does not match the outer compound statement. 02651 AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 02652 return Node.size() == N; 02653 } 02654 02655 /// \brief Matches literals that are equal to the given value. 02656 /// 02657 /// Example matches true (matcher = boolLiteral(equals(true))) 02658 /// \code 02659 /// true 02660 /// \endcode 02661 /// 02662 /// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>, 02663 /// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 02664 template <typename ValueT> 02665 internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 02666 equals(const ValueT &Value) { 02667 return internal::PolymorphicMatcherWithParam1< 02668 internal::ValueEqualsMatcher, 02669 ValueT>(Value); 02670 } 02671 02672 /// \brief Matches the operator Name of operator expressions (binary or 02673 /// unary). 02674 /// 02675 /// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 02676 /// \code 02677 /// !(a || b) 02678 /// \endcode 02679 AST_POLYMORPHIC_MATCHER_P(hasOperatorName, AST_POLYMORPHIC_SUPPORTED_TYPES_2( 02680 BinaryOperator, UnaryOperator), 02681 std::string, Name) { 02682 return Name == Node.getOpcodeStr(Node.getOpcode()); 02683 } 02684 02685 /// \brief Matches the left hand side of binary operator expressions. 02686 /// 02687 /// Example matches a (matcher = binaryOperator(hasLHS())) 02688 /// \code 02689 /// a || b 02690 /// \endcode 02691 AST_MATCHER_P(BinaryOperator, hasLHS, 02692 internal::Matcher<Expr>, InnerMatcher) { 02693 Expr *LeftHandSide = Node.getLHS(); 02694 return (LeftHandSide != nullptr && 02695 InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 02696 } 02697 02698 /// \brief Matches the right hand side of binary operator expressions. 02699 /// 02700 /// Example matches b (matcher = binaryOperator(hasRHS())) 02701 /// \code 02702 /// a || b 02703 /// \endcode 02704 AST_MATCHER_P(BinaryOperator, hasRHS, 02705 internal::Matcher<Expr>, InnerMatcher) { 02706 Expr *RightHandSide = Node.getRHS(); 02707 return (RightHandSide != nullptr && 02708 InnerMatcher.matches(*RightHandSide, Finder, Builder)); 02709 } 02710 02711 /// \brief Matches if either the left hand side or the right hand side of a 02712 /// binary operator matches. 02713 inline internal::Matcher<BinaryOperator> hasEitherOperand( 02714 const internal::Matcher<Expr> &InnerMatcher) { 02715 return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 02716 } 02717 02718 /// \brief Matches if the operand of a unary operator matches. 02719 /// 02720 /// Example matches true (matcher = hasUnaryOperand(boolLiteral(equals(true)))) 02721 /// \code 02722 /// !true 02723 /// \endcode 02724 AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 02725 internal::Matcher<Expr>, InnerMatcher) { 02726 const Expr * const Operand = Node.getSubExpr(); 02727 return (Operand != nullptr && 02728 InnerMatcher.matches(*Operand, Finder, Builder)); 02729 } 02730 02731 /// \brief Matches if the cast's source expression matches the given matcher. 02732 /// 02733 /// Example: matches "a string" (matcher = 02734 /// hasSourceExpression(constructExpr())) 02735 /// \code 02736 /// class URL { URL(string); }; 02737 /// URL url = "a string"; 02738 AST_MATCHER_P(CastExpr, hasSourceExpression, 02739 internal::Matcher<Expr>, InnerMatcher) { 02740 const Expr* const SubExpression = Node.getSubExpr(); 02741 return (SubExpression != nullptr && 02742 InnerMatcher.matches(*SubExpression, Finder, Builder)); 02743 } 02744 02745 /// \brief Matches casts whose destination type matches a given matcher. 02746 /// 02747 /// (Note: Clang's AST refers to other conversions as "casts" too, and calls 02748 /// actual casts "explicit" casts.) 02749 AST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 02750 internal::Matcher<QualType>, InnerMatcher) { 02751 const QualType NodeType = Node.getTypeAsWritten(); 02752 return InnerMatcher.matches(NodeType, Finder, Builder); 02753 } 02754 02755 /// \brief Matches implicit casts whose destination type matches a given 02756 /// matcher. 02757 /// 02758 /// FIXME: Unit test this matcher 02759 AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 02760 internal::Matcher<QualType>, InnerMatcher) { 02761 return InnerMatcher.matches(Node.getType(), Finder, Builder); 02762 } 02763 02764 /// \brief Matches the true branch expression of a conditional operator. 02765 /// 02766 /// Example matches a 02767 /// \code 02768 /// condition ? a : b 02769 /// \endcode 02770 AST_MATCHER_P(ConditionalOperator, hasTrueExpression, 02771 internal::Matcher<Expr>, InnerMatcher) { 02772 Expr *Expression = Node.getTrueExpr(); 02773 return (Expression != nullptr && 02774 InnerMatcher.matches(*Expression, Finder, Builder)); 02775 } 02776 02777 /// \brief Matches the false branch expression of a conditional operator. 02778 /// 02779 /// Example matches b 02780 /// \code 02781 /// condition ? a : b 02782 /// \endcode 02783 AST_MATCHER_P(ConditionalOperator, hasFalseExpression, 02784 internal::Matcher<Expr>, InnerMatcher) { 02785 Expr *Expression = Node.getFalseExpr(); 02786 return (Expression != nullptr && 02787 InnerMatcher.matches(*Expression, Finder, Builder)); 02788 } 02789 02790 /// \brief Matches if a declaration has a body attached. 02791 /// 02792 /// Example matches A, va, fa 02793 /// \code 02794 /// class A {}; 02795 /// class B; // Doesn't match, as it has no body. 02796 /// int va; 02797 /// extern int vb; // Doesn't match, as it doesn't define the variable. 02798 /// void fa() {} 02799 /// void fb(); // Doesn't match, as it has no body. 02800 /// \endcode 02801 /// 02802 /// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl> 02803 AST_POLYMORPHIC_MATCHER(isDefinition, AST_POLYMORPHIC_SUPPORTED_TYPES_3( 02804 TagDecl, VarDecl, FunctionDecl)) { 02805 return Node.isThisDeclarationADefinition(); 02806 } 02807 02808 /// \brief Matches the class declaration that the given method declaration 02809 /// belongs to. 02810 /// 02811 /// FIXME: Generalize this for other kinds of declarations. 02812 /// FIXME: What other kind of declarations would we need to generalize 02813 /// this to? 02814 /// 02815 /// Example matches A() in the last line 02816 /// (matcher = constructExpr(hasDeclaration(methodDecl( 02817 /// ofClass(hasName("A")))))) 02818 /// \code 02819 /// class A { 02820 /// public: 02821 /// A(); 02822 /// }; 02823 /// A a = A(); 02824 /// \endcode 02825 AST_MATCHER_P(CXXMethodDecl, ofClass, 02826 internal::Matcher<CXXRecordDecl>, InnerMatcher) { 02827 const CXXRecordDecl *Parent = Node.getParent(); 02828 return (Parent != nullptr && 02829 InnerMatcher.matches(*Parent, Finder, Builder)); 02830 } 02831 02832 /// \brief Matches if the given method declaration is virtual. 02833 /// 02834 /// Given 02835 /// \code 02836 /// class A { 02837 /// public: 02838 /// virtual void x(); 02839 /// }; 02840 /// \endcode 02841 /// matches A::x 02842 AST_MATCHER(CXXMethodDecl, isVirtual) { 02843 return Node.isVirtual(); 02844 } 02845 02846 /// \brief Matches if the given method declaration is pure. 02847 /// 02848 /// Given 02849 /// \code 02850 /// class A { 02851 /// public: 02852 /// virtual void x() = 0; 02853 /// }; 02854 /// \endcode 02855 /// matches A::x 02856 AST_MATCHER(CXXMethodDecl, isPure) { 02857 return Node.isPure(); 02858 } 02859 02860 /// \brief Matches if the given method declaration is const. 02861 /// 02862 /// Given 02863 /// \code 02864 /// struct A { 02865 /// void foo() const; 02866 /// void bar(); 02867 /// }; 02868 /// \endcode 02869 /// 02870 /// methodDecl(isConst()) matches A::foo() but not A::bar() 02871 AST_MATCHER(CXXMethodDecl, isConst) { 02872 return Node.isConst(); 02873 } 02874 02875 /// \brief Matches if the given method declaration overrides another method. 02876 /// 02877 /// Given 02878 /// \code 02879 /// class A { 02880 /// public: 02881 /// virtual void x(); 02882 /// }; 02883 /// class B : public A { 02884 /// public: 02885 /// virtual void x(); 02886 /// }; 02887 /// \endcode 02888 /// matches B::x 02889 AST_MATCHER(CXXMethodDecl, isOverride) { 02890 return Node.size_overridden_methods() > 0; 02891 } 02892 02893 /// \brief Matches member expressions that are called with '->' as opposed 02894 /// to '.'. 02895 /// 02896 /// Member calls on the implicit this pointer match as called with '->'. 02897 /// 02898 /// Given 02899 /// \code 02900 /// class Y { 02901 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 02902 /// int a; 02903 /// static int b; 02904 /// }; 02905 /// \endcode 02906 /// memberExpr(isArrow()) 02907 /// matches this->x, x, y.x, a, this->b 02908 AST_MATCHER(MemberExpr, isArrow) { 02909 return Node.isArrow(); 02910 } 02911 02912 /// \brief Matches QualType nodes that are of integer type. 02913 /// 02914 /// Given 02915 /// \code 02916 /// void a(int); 02917 /// void b(long); 02918 /// void c(double); 02919 /// \endcode 02920 /// functionDecl(hasAnyParameter(hasType(isInteger()))) 02921 /// matches "a(int)", "b(long)", but not "c(double)". 02922 AST_MATCHER(QualType, isInteger) { 02923 return Node->isIntegerType(); 02924 } 02925 02926 /// \brief Matches QualType nodes that are const-qualified, i.e., that 02927 /// include "top-level" const. 02928 /// 02929 /// Given 02930 /// \code 02931 /// void a(int); 02932 /// void b(int const); 02933 /// void c(const int); 02934 /// void d(const int*); 02935 /// void e(int const) {}; 02936 /// \endcode 02937 /// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 02938 /// matches "void b(int const)", "void c(const int)" and 02939 /// "void e(int const) {}". It does not match d as there 02940 /// is no top-level const on the parameter type "const int *". 02941 AST_MATCHER(QualType, isConstQualified) { 02942 return Node.isConstQualified(); 02943 } 02944 02945 /// \brief Matches QualType nodes that have local CV-qualifiers attached to 02946 /// the node, not hidden within a typedef. 02947 /// 02948 /// Given 02949 /// \code 02950 /// typedef const int const_int; 02951 /// const_int i; 02952 /// int *const j; 02953 /// int *volatile k; 02954 /// int m; 02955 /// \endcode 02956 /// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k. 02957 /// \c i is const-qualified but the qualifier is not local. 02958 AST_MATCHER(QualType, hasLocalQualifiers) { 02959 return Node.hasLocalQualifiers(); 02960 } 02961 02962 /// \brief Matches a member expression where the member is matched by a 02963 /// given matcher. 02964 /// 02965 /// Given 02966 /// \code 02967 /// struct { int first, second; } first, second; 02968 /// int i(second.first); 02969 /// int j(first.second); 02970 /// \endcode 02971 /// memberExpr(member(hasName("first"))) 02972 /// matches second.first 02973 /// but not first.second (because the member name there is "second"). 02974 AST_MATCHER_P(MemberExpr, member, 02975 internal::Matcher<ValueDecl>, InnerMatcher) { 02976 return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 02977 } 02978 02979 /// \brief Matches a member expression where the object expression is 02980 /// matched by a given matcher. 02981 /// 02982 /// Given 02983 /// \code 02984 /// struct X { int m; }; 02985 /// void f(X x) { x.m; m; } 02986 /// \endcode 02987 /// memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))) 02988 /// matches "x.m" and "m" 02989 /// with hasObjectExpression(...) 02990 /// matching "x" and the implicit object expression of "m" which has type X*. 02991 AST_MATCHER_P(MemberExpr, hasObjectExpression, 02992 internal::Matcher<Expr>, InnerMatcher) { 02993 return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 02994 } 02995 02996 /// \brief Matches any using shadow declaration. 02997 /// 02998 /// Given 02999 /// \code 03000 /// namespace X { void b(); } 03001 /// using X::b; 03002 /// \endcode 03003 /// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 03004 /// matches \code using X::b \endcode 03005 AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 03006 internal::Matcher<UsingShadowDecl>, InnerMatcher) { 03007 return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(), 03008 Node.shadow_end(), Finder, Builder); 03009 } 03010 03011 /// \brief Matches a using shadow declaration where the target declaration is 03012 /// matched by the given matcher. 03013 /// 03014 /// Given 03015 /// \code 03016 /// namespace X { int a; void b(); } 03017 /// using X::a; 03018 /// using X::b; 03019 /// \endcode 03020 /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 03021 /// matches \code using X::b \endcode 03022 /// but not \code using X::a \endcode 03023 AST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 03024 internal::Matcher<NamedDecl>, InnerMatcher) { 03025 return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 03026 } 03027 03028 /// \brief Matches template instantiations of function, class, or static 03029 /// member variable template instantiations. 03030 /// 03031 /// Given 03032 /// \code 03033 /// template <typename T> class X {}; class A {}; X<A> x; 03034 /// \endcode 03035 /// or 03036 /// \code 03037 /// template <typename T> class X {}; class A {}; template class X<A>; 03038 /// \endcode 03039 /// recordDecl(hasName("::X"), isTemplateInstantiation()) 03040 /// matches the template instantiation of X<A>. 03041 /// 03042 /// But given 03043 /// \code 03044 /// template <typename T> class X {}; class A {}; 03045 /// template <> class X<A> {}; X<A> x; 03046 /// \endcode 03047 /// recordDecl(hasName("::X"), isTemplateInstantiation()) 03048 /// does not match, as X<A> is an explicit template specialization. 03049 /// 03050 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 03051 AST_POLYMORPHIC_MATCHER( 03052 isTemplateInstantiation, 03053 AST_POLYMORPHIC_SUPPORTED_TYPES_3(FunctionDecl, VarDecl, CXXRecordDecl)) { 03054 return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation || 03055 Node.getTemplateSpecializationKind() == 03056 TSK_ExplicitInstantiationDefinition); 03057 } 03058 03059 /// \brief Matches declarations that are template instantiations or are inside 03060 /// template instantiations. 03061 /// 03062 /// Given 03063 /// \code 03064 /// template<typename T> void A(T t) { T i; } 03065 /// A(0); 03066 /// A(0U); 03067 /// \endcode 03068 /// functionDecl(isInstantiated()) 03069 /// matches 'A(int) {...};' and 'A(unsigned) {...}'. 03070 AST_MATCHER(Decl, isInstantiated) { 03071 auto IsInstantiation = decl(anyOf(recordDecl(isTemplateInstantiation()), 03072 functionDecl(isTemplateInstantiation()))); 03073 auto InnerMatcher = 03074 decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation))); 03075 return InnerMatcher.matches(Node, Finder, Builder); 03076 } 03077 03078 /// \brief Matches statements inside of a template instantiation. 03079 /// 03080 /// Given 03081 /// \code 03082 /// int j; 03083 /// template<typename T> void A(T t) { T i; j += 42;} 03084 /// A(0); 03085 /// A(0U); 03086 /// \endcode 03087 /// declStmt(isInTemplateInstantiation()) 03088 /// matches 'int i;' and 'unsigned i'. 03089 /// unless(stmt(isInTemplateInstantiation())) 03090 /// will NOT match j += 42; as it's shared between the template definition and 03091 /// instantiation. 03092 AST_MATCHER(Stmt, isInTemplateInstantiation) { 03093 auto InnerMatcher = 03094 stmt(hasAncestor(decl(anyOf(recordDecl(isTemplateInstantiation()), 03095 functionDecl(isTemplateInstantiation()))))); 03096 return InnerMatcher.matches(Node, Finder, Builder); 03097 } 03098 03099 /// \brief Matches explicit template specializations of function, class, or 03100 /// static member variable template instantiations. 03101 /// 03102 /// Given 03103 /// \code 03104 /// template<typename T> void A(T t) { } 03105 /// template<> void A(int N) { } 03106 /// \endcode 03107 /// functionDecl(isExplicitTemplateSpecialization()) 03108 /// matches the specialization A<int>(). 03109 /// 03110 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 03111 AST_POLYMORPHIC_MATCHER( 03112 isExplicitTemplateSpecialization, 03113 AST_POLYMORPHIC_SUPPORTED_TYPES_3(FunctionDecl, VarDecl, CXXRecordDecl)) { 03114 return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization); 03115 } 03116 03117 /// \brief Matches \c TypeLocs for which the given inner 03118 /// QualType-matcher matches. 03119 AST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher<TypeLoc>, loc, 03120 internal::Matcher<QualType>, InnerMatcher, 0) { 03121 return internal::BindableMatcher<TypeLoc>( 03122 new internal::TypeLocTypeMatcher(InnerMatcher)); 03123 } 03124 03125 /// \brief Matches builtin Types. 03126 /// 03127 /// Given 03128 /// \code 03129 /// struct A {}; 03130 /// A a; 03131 /// int b; 03132 /// float c; 03133 /// bool d; 03134 /// \endcode 03135 /// builtinType() 03136 /// matches "int b", "float c" and "bool d" 03137 AST_TYPE_MATCHER(BuiltinType, builtinType); 03138 03139 /// \brief Matches all kinds of arrays. 03140 /// 03141 /// Given 03142 /// \code 03143 /// int a[] = { 2, 3 }; 03144 /// int b[4]; 03145 /// void f() { int c[a[0]]; } 03146 /// \endcode 03147 /// arrayType() 03148 /// matches "int a[]", "int b[4]" and "int c[a[0]]"; 03149 AST_TYPE_MATCHER(ArrayType, arrayType); 03150 03151 /// \brief Matches C99 complex types. 03152 /// 03153 /// Given 03154 /// \code 03155 /// _Complex float f; 03156 /// \endcode 03157 /// complexType() 03158 /// matches "_Complex float f" 03159 AST_TYPE_MATCHER(ComplexType, complexType); 03160 03161 /// \brief Matches arrays and C99 complex types that have a specific element 03162 /// type. 03163 /// 03164 /// Given 03165 /// \code 03166 /// struct A {}; 03167 /// A a[7]; 03168 /// int b[7]; 03169 /// \endcode 03170 /// arrayType(hasElementType(builtinType())) 03171 /// matches "int b[7]" 03172 /// 03173 /// Usable as: Matcher<ArrayType>, Matcher<ComplexType> 03174 AST_TYPELOC_TRAVERSE_MATCHER( 03175 hasElementType, getElement, 03176 AST_POLYMORPHIC_SUPPORTED_TYPES_2(ArrayType, ComplexType)); 03177 03178 /// \brief Matches C arrays with a specified constant size. 03179 /// 03180 /// Given 03181 /// \code 03182 /// void() { 03183 /// int a[2]; 03184 /// int b[] = { 2, 3 }; 03185 /// int c[b[0]]; 03186 /// } 03187 /// \endcode 03188 /// constantArrayType() 03189 /// matches "int a[2]" 03190 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType); 03191 03192 /// \brief Matches \c ConstantArrayType nodes that have the specified size. 03193 /// 03194 /// Given 03195 /// \code 03196 /// int a[42]; 03197 /// int b[2 * 21]; 03198 /// int c[41], d[43]; 03199 /// \endcode 03200 /// constantArrayType(hasSize(42)) 03201 /// matches "int a[42]" and "int b[2 * 21]" 03202 AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) { 03203 return Node.getSize() == N; 03204 } 03205 03206 /// \brief Matches C++ arrays whose size is a value-dependent expression. 03207 /// 03208 /// Given 03209 /// \code 03210 /// template<typename T, int Size> 03211 /// class array { 03212 /// T data[Size]; 03213 /// }; 03214 /// \endcode 03215 /// dependentSizedArrayType 03216 /// matches "T data[Size]" 03217 AST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType); 03218 03219 /// \brief Matches C arrays with unspecified size. 03220 /// 03221 /// Given 03222 /// \code 03223 /// int a[] = { 2, 3 }; 03224 /// int b[42]; 03225 /// void f(int c[]) { int d[a[0]]; }; 03226 /// \endcode 03227 /// incompleteArrayType() 03228 /// matches "int a[]" and "int c[]" 03229 AST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType); 03230 03231 /// \brief Matches C arrays with a specified size that is not an 03232 /// integer-constant-expression. 03233 /// 03234 /// Given 03235 /// \code 03236 /// void f() { 03237 /// int a[] = { 2, 3 } 03238 /// int b[42]; 03239 /// int c[a[0]]; 03240 /// } 03241 /// \endcode 03242 /// variableArrayType() 03243 /// matches "int c[a[0]]" 03244 AST_TYPE_MATCHER(VariableArrayType, variableArrayType); 03245 03246 /// \brief Matches \c VariableArrayType nodes that have a specific size 03247 /// expression. 03248 /// 03249 /// Given 03250 /// \code 03251 /// void f(int b) { 03252 /// int a[b]; 03253 /// } 03254 /// \endcode 03255 /// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( 03256 /// varDecl(hasName("b"))))))) 03257 /// matches "int a[b]" 03258 AST_MATCHER_P(VariableArrayType, hasSizeExpr, 03259 internal::Matcher<Expr>, InnerMatcher) { 03260 return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder); 03261 } 03262 03263 /// \brief Matches atomic types. 03264 /// 03265 /// Given 03266 /// \code 03267 /// _Atomic(int) i; 03268 /// \endcode 03269 /// atomicType() 03270 /// matches "_Atomic(int) i" 03271 AST_TYPE_MATCHER(AtomicType, atomicType); 03272 03273 /// \brief Matches atomic types with a specific value type. 03274 /// 03275 /// Given 03276 /// \code 03277 /// _Atomic(int) i; 03278 /// _Atomic(float) f; 03279 /// \endcode 03280 /// atomicType(hasValueType(isInteger())) 03281 /// matches "_Atomic(int) i" 03282 /// 03283 /// Usable as: Matcher<AtomicType> 03284 AST_TYPELOC_TRAVERSE_MATCHER(hasValueType, getValue, 03285 AST_POLYMORPHIC_SUPPORTED_TYPES_1(AtomicType)); 03286 03287 /// \brief Matches types nodes representing C++11 auto types. 03288 /// 03289 /// Given: 03290 /// \code 03291 /// auto n = 4; 03292 /// int v[] = { 2, 3 } 03293 /// for (auto i : v) { } 03294 /// \endcode 03295 /// autoType() 03296 /// matches "auto n" and "auto i" 03297 AST_TYPE_MATCHER(AutoType, autoType); 03298 03299 /// \brief Matches \c AutoType nodes where the deduced type is a specific type. 03300 /// 03301 /// Note: There is no \c TypeLoc for the deduced type and thus no 03302 /// \c getDeducedLoc() matcher. 03303 /// 03304 /// Given 03305 /// \code 03306 /// auto a = 1; 03307 /// auto b = 2.0; 03308 /// \endcode 03309 /// autoType(hasDeducedType(isInteger())) 03310 /// matches "auto a" 03311 /// 03312 /// Usable as: Matcher<AutoType> 03313 AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType, 03314 AST_POLYMORPHIC_SUPPORTED_TYPES_1(AutoType)); 03315 03316 /// \brief Matches \c FunctionType nodes. 03317 /// 03318 /// Given 03319 /// \code 03320 /// int (*f)(int); 03321 /// void g(); 03322 /// \endcode 03323 /// functionType() 03324 /// matches "int (*f)(int)" and the type of "g". 03325 AST_TYPE_MATCHER(FunctionType, functionType); 03326 03327 /// \brief Matches \c ParenType nodes. 03328 /// 03329 /// Given 03330 /// \code 03331 /// int (*ptr_to_array)[4]; 03332 /// int *array_of_ptrs[4]; 03333 /// \endcode 03334 /// 03335 /// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not 03336 /// \c array_of_ptrs. 03337 AST_TYPE_MATCHER(ParenType, parenType); 03338 03339 /// \brief Matches \c ParenType nodes where the inner type is a specific type. 03340 /// 03341 /// Given 03342 /// \code 03343 /// int (*ptr_to_array)[4]; 03344 /// int (*ptr_to_func)(int); 03345 /// \endcode 03346 /// 03347 /// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches 03348 /// \c ptr_to_func but not \c ptr_to_array. 03349 /// 03350 /// Usable as: Matcher<ParenType> 03351 AST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType, 03352 AST_POLYMORPHIC_SUPPORTED_TYPES_1(ParenType)); 03353 03354 /// \brief Matches block pointer types, i.e. types syntactically represented as 03355 /// "void (^)(int)". 03356 /// 03357 /// The \c pointee is always required to be a \c FunctionType. 03358 AST_TYPE_MATCHER(BlockPointerType, blockPointerType); 03359 03360 /// \brief Matches member pointer types. 03361 /// Given 03362 /// \code 03363 /// struct A { int i; } 03364 /// A::* ptr = A::i; 03365 /// \endcode 03366 /// memberPointerType() 03367 /// matches "A::* ptr" 03368 AST_TYPE_MATCHER(MemberPointerType, memberPointerType); 03369 03370 /// \brief Matches pointer types. 03371 /// 03372 /// Given 03373 /// \code 03374 /// int *a; 03375 /// int &b = *a; 03376 /// int c = 5; 03377 /// \endcode 03378 /// pointerType() 03379 /// matches "int *a" 03380 AST_TYPE_MATCHER(PointerType, pointerType); 03381 03382 /// \brief Matches both lvalue and rvalue reference types. 03383 /// 03384 /// Given 03385 /// \code 03386 /// int *a; 03387 /// int &b = *a; 03388 /// int &&c = 1; 03389 /// auto &d = b; 03390 /// auto &&e = c; 03391 /// auto &&f = 2; 03392 /// int g = 5; 03393 /// \endcode 03394 /// 03395 /// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f. 03396 AST_TYPE_MATCHER(ReferenceType, referenceType); 03397 03398 /// \brief Matches lvalue reference types. 03399 /// 03400 /// Given: 03401 /// \code 03402 /// int *a; 03403 /// int &b = *a; 03404 /// int &&c = 1; 03405 /// auto &d = b; 03406 /// auto &&e = c; 03407 /// auto &&f = 2; 03408 /// int g = 5; 03409 /// \endcode 03410 /// 03411 /// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is 03412 /// matched since the type is deduced as int& by reference collapsing rules. 03413 AST_TYPE_MATCHER(LValueReferenceType, lValueReferenceType); 03414 03415 /// \brief Matches rvalue reference types. 03416 /// 03417 /// Given: 03418 /// \code 03419 /// int *a; 03420 /// int &b = *a; 03421 /// int &&c = 1; 03422 /// auto &d = b; 03423 /// auto &&e = c; 03424 /// auto &&f = 2; 03425 /// int g = 5; 03426 /// \endcode 03427 /// 03428 /// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not 03429 /// matched as it is deduced to int& by reference collapsing rules. 03430 AST_TYPE_MATCHER(RValueReferenceType, rValueReferenceType); 03431 03432 /// \brief Narrows PointerType (and similar) matchers to those where the 03433 /// \c pointee matches a given matcher. 03434 /// 03435 /// Given 03436 /// \code 03437 /// int *a; 03438 /// int const *b; 03439 /// float const *f; 03440 /// \endcode 03441 /// pointerType(pointee(isConstQualified(), isInteger())) 03442 /// matches "int const *b" 03443 /// 03444 /// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>, 03445 /// Matcher<PointerType>, Matcher<ReferenceType> 03446 AST_TYPELOC_TRAVERSE_MATCHER( 03447 pointee, getPointee, 03448 AST_POLYMORPHIC_SUPPORTED_TYPES_4(BlockPointerType, MemberPointerType, 03449 PointerType, ReferenceType)); 03450 03451 /// \brief Matches typedef types. 03452 /// 03453 /// Given 03454 /// \code 03455 /// typedef int X; 03456 /// \endcode 03457 /// typedefType() 03458 /// matches "typedef int X" 03459 AST_TYPE_MATCHER(TypedefType, typedefType); 03460 03461 /// \brief Matches template specialization types. 03462 /// 03463 /// Given 03464 /// \code 03465 /// template <typename T> 03466 /// class C { }; 03467 /// 03468 /// template class C<int>; // A 03469 /// C<char> var; // B 03470 /// \code 03471 /// 03472 /// \c templateSpecializationType() matches the type of the explicit 03473 /// instantiation in \c A and the type of the variable declaration in \c B. 03474 AST_TYPE_MATCHER(TemplateSpecializationType, templateSpecializationType); 03475 03476 /// \brief Matches types nodes representing unary type transformations. 03477 /// 03478 /// Given: 03479 /// \code 03480 /// typedef __underlying_type(T) type; 03481 /// \endcode 03482 /// unaryTransformType() 03483 /// matches "__underlying_type(T)" 03484 AST_TYPE_MATCHER(UnaryTransformType, unaryTransformType); 03485 03486 /// \brief Matches record types (e.g. structs, classes). 03487 /// 03488 /// Given 03489 /// \code 03490 /// class C {}; 03491 /// struct S {}; 03492 /// 03493 /// C c; 03494 /// S s; 03495 /// \code 03496 /// 03497 /// \c recordType() matches the type of the variable declarations of both \c c 03498 /// and \c s. 03499 AST_TYPE_MATCHER(RecordType, recordType); 03500 03501 /// \brief Matches types specified with an elaborated type keyword or with a 03502 /// qualified name. 03503 /// 03504 /// Given 03505 /// \code 03506 /// namespace N { 03507 /// namespace M { 03508 /// class D {}; 03509 /// } 03510 /// } 03511 /// class C {}; 03512 /// 03513 /// class C c; 03514 /// N::M::D d; 03515 /// \code 03516 /// 03517 /// \c elaboratedType() matches the type of the variable declarations of both 03518 /// \c c and \c d. 03519 AST_TYPE_MATCHER(ElaboratedType, elaboratedType); 03520 03521 /// \brief Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier, 03522 /// matches \c InnerMatcher if the qualifier exists. 03523 /// 03524 /// Given 03525 /// \code 03526 /// namespace N { 03527 /// namespace M { 03528 /// class D {}; 03529 /// } 03530 /// } 03531 /// N::M::D d; 03532 /// \code 03533 /// 03534 /// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))) 03535 /// matches the type of the variable declaration of \c d. 03536 AST_MATCHER_P(ElaboratedType, hasQualifier, 03537 internal::Matcher<NestedNameSpecifier>, InnerMatcher) { 03538 if (const NestedNameSpecifier *Qualifier = Node.getQualifier()) 03539 return InnerMatcher.matches(*Qualifier, Finder, Builder); 03540 03541 return false; 03542 } 03543 03544 /// \brief Matches ElaboratedTypes whose named type matches \c InnerMatcher. 03545 /// 03546 /// Given 03547 /// \code 03548 /// namespace N { 03549 /// namespace M { 03550 /// class D {}; 03551 /// } 03552 /// } 03553 /// N::M::D d; 03554 /// \code 03555 /// 03556 /// \c elaboratedType(namesType(recordType( 03557 /// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable 03558 /// declaration of \c d. 03559 AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>, 03560 InnerMatcher) { 03561 return InnerMatcher.matches(Node.getNamedType(), Finder, Builder); 03562 } 03563 03564 /// \brief Matches declarations whose declaration context, interpreted as a 03565 /// Decl, matches \c InnerMatcher. 03566 /// 03567 /// Given 03568 /// \code 03569 /// namespace N { 03570 /// namespace M { 03571 /// class D {}; 03572 /// } 03573 /// } 03574 /// \code 03575 /// 03576 /// \c recordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the 03577 /// declaration of \c class \c D. 03578 AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) { 03579 const DeclContext *DC = Node.getDeclContext(); 03580 if (!DC) return false; 03581 return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder); 03582 } 03583 03584 /// \brief Matches nested name specifiers. 03585 /// 03586 /// Given 03587 /// \code 03588 /// namespace ns { 03589 /// struct A { static void f(); }; 03590 /// void A::f() {} 03591 /// void g() { A::f(); } 03592 /// } 03593 /// ns::A a; 03594 /// \endcode 03595 /// nestedNameSpecifier() 03596 /// matches "ns::" and both "A::" 03597 const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 03598 03599 /// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 03600 const internal::VariadicAllOfMatcher< 03601 NestedNameSpecifierLoc> nestedNameSpecifierLoc; 03602 03603 /// \brief Matches \c NestedNameSpecifierLocs for which the given inner 03604 /// NestedNameSpecifier-matcher matches. 03605 AST_MATCHER_FUNCTION_P_OVERLOAD( 03606 internal::BindableMatcher<NestedNameSpecifierLoc>, loc, 03607 internal::Matcher<NestedNameSpecifier>, InnerMatcher, 1) { 03608 return internal::BindableMatcher<NestedNameSpecifierLoc>( 03609 new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 03610 InnerMatcher)); 03611 } 03612 03613 /// \brief Matches nested name specifiers that specify a type matching the 03614 /// given \c QualType matcher without qualifiers. 03615 /// 03616 /// Given 03617 /// \code 03618 /// struct A { struct B { struct C {}; }; }; 03619 /// A::B::C c; 03620 /// \endcode 03621 /// nestedNameSpecifier(specifiesType(hasDeclaration(recordDecl(hasName("A"))))) 03622 /// matches "A::" 03623 AST_MATCHER_P(NestedNameSpecifier, specifiesType, 03624 internal::Matcher<QualType>, InnerMatcher) { 03625 if (!Node.getAsType()) 03626 return false; 03627 return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 03628 } 03629 03630 /// \brief Matches nested name specifier locs that specify a type matching the 03631 /// given \c TypeLoc. 03632 /// 03633 /// Given 03634 /// \code 03635 /// struct A { struct B { struct C {}; }; }; 03636 /// A::B::C c; 03637 /// \endcode 03638 /// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type( 03639 /// hasDeclaration(recordDecl(hasName("A"))))))) 03640 /// matches "A::" 03641 AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, 03642 internal::Matcher<TypeLoc>, InnerMatcher) { 03643 return Node && InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder); 03644 } 03645 03646 /// \brief Matches on the prefix of a \c NestedNameSpecifier. 03647 /// 03648 /// Given 03649 /// \code 03650 /// struct A { struct B { struct C {}; }; }; 03651 /// A::B::C c; 03652 /// \endcode 03653 /// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 03654 /// matches "A::" 03655 AST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix, 03656 internal::Matcher<NestedNameSpecifier>, InnerMatcher, 03657 0) { 03658 NestedNameSpecifier *NextNode = Node.getPrefix(); 03659 if (!NextNode) 03660 return false; 03661 return InnerMatcher.matches(*NextNode, Finder, Builder); 03662 } 03663 03664 /// \brief Matches on the prefix of a \c NestedNameSpecifierLoc. 03665 /// 03666 /// Given 03667 /// \code 03668 /// struct A { struct B { struct C {}; }; }; 03669 /// A::B::C c; 03670 /// \endcode 03671 /// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 03672 /// matches "A::" 03673 AST_MATCHER_P_OVERLOAD(NestedNameSpecifierLoc, hasPrefix, 03674 internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher, 03675 1) { 03676 NestedNameSpecifierLoc NextNode = Node.getPrefix(); 03677 if (!NextNode) 03678 return false; 03679 return InnerMatcher.matches(NextNode, Finder, Builder); 03680 } 03681 03682 /// \brief Matches nested name specifiers that specify a namespace matching the 03683 /// given namespace matcher. 03684 /// 03685 /// Given 03686 /// \code 03687 /// namespace ns { struct A {}; } 03688 /// ns::A a; 03689 /// \endcode 03690 /// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 03691 /// matches "ns::" 03692 AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 03693 internal::Matcher<NamespaceDecl>, InnerMatcher) { 03694 if (!Node.getAsNamespace()) 03695 return false; 03696 return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 03697 } 03698 03699 /// \brief Overloads for the \c equalsNode matcher. 03700 /// FIXME: Implement for other node types. 03701 /// @{ 03702 03703 /// \brief Matches if a node equals another node. 03704 /// 03705 /// \c Decl has pointer identity in the AST. 03706 AST_MATCHER_P_OVERLOAD(Decl, equalsNode, const Decl*, Other, 0) { 03707 return &Node == Other; 03708 } 03709 /// \brief Matches if a node equals another node. 03710 /// 03711 /// \c Stmt has pointer identity in the AST. 03712 /// 03713 AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) { 03714 return &Node == Other; 03715 } 03716 03717 /// @} 03718 03719 /// \brief Matches each case or default statement belonging to the given switch 03720 /// statement. This matcher may produce multiple matches. 03721 /// 03722 /// Given 03723 /// \code 03724 /// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } } 03725 /// \endcode 03726 /// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s") 03727 /// matches four times, with "c" binding each of "case 1:", "case 2:", 03728 /// "case 3:" and "case 4:", and "s" respectively binding "switch (1)", 03729 /// "switch (1)", "switch (2)" and "switch (2)". 03730 AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>, 03731 InnerMatcher) { 03732 BoundNodesTreeBuilder Result; 03733 // FIXME: getSwitchCaseList() does not necessarily guarantee a stable 03734 // iteration order. We should use the more general iterating matchers once 03735 // they are capable of expressing this matcher (for example, it should ignore 03736 // case statements belonging to nested switch statements). 03737 bool Matched = false; 03738 for (const SwitchCase *SC = Node.getSwitchCaseList(); SC; 03739 SC = SC->getNextSwitchCase()) { 03740 BoundNodesTreeBuilder CaseBuilder(*Builder); 03741 bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder); 03742 if (CaseMatched) { 03743 Matched = true; 03744 Result.addMatch(CaseBuilder); 03745 } 03746 } 03747 *Builder = std::move(Result); 03748 return Matched; 03749 } 03750 03751 /// \brief Matches each constructor initializer in a constructor definition. 03752 /// 03753 /// Given 03754 /// \code 03755 /// class A { A() : i(42), j(42) {} int i; int j; }; 03756 /// \endcode 03757 /// constructorDecl(forEachConstructorInitializer(forField(decl().bind("x")))) 03758 /// will trigger two matches, binding for 'i' and 'j' respectively. 03759 AST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer, 03760 internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 03761 BoundNodesTreeBuilder Result; 03762 bool Matched = false; 03763 for (const auto *I : Node.inits()) { 03764 BoundNodesTreeBuilder InitBuilder(*Builder); 03765 if (InnerMatcher.matches(*I, Finder, &InitBuilder)) { 03766 Matched = true; 03767 Result.addMatch(InitBuilder); 03768 } 03769 } 03770 *Builder = std::move(Result); 03771 return Matched; 03772 } 03773 03774 /// \brief If the given case statement does not use the GNU case range 03775 /// extension, matches the constant given in the statement. 03776 /// 03777 /// Given 03778 /// \code 03779 /// switch (1) { case 1: case 1+1: case 3 ... 4: ; } 03780 /// \endcode 03781 /// caseStmt(hasCaseConstant(integerLiteral())) 03782 /// matches "case 1:" 03783 AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>, 03784 InnerMatcher) { 03785 if (Node.getRHS()) 03786 return false; 03787 03788 return InnerMatcher.matches(*Node.getLHS(), Finder, Builder); 03789 } 03790 03791 /// \brief Matches declaration that has a given attribute. 03792 /// 03793 /// Given 03794 /// \code 03795 /// __attribute__((device)) void f() { ... } 03796 /// \endcode 03797 /// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of 03798 /// f. 03799 AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) { 03800 for (const auto *Attr : Node.attrs()) { 03801 if (Attr->getKind() == AttrKind) 03802 return true; 03803 } 03804 return false; 03805 } 03806 03807 /// \brief Matches CUDA kernel call expression. 03808 /// 03809 /// Example matches, 03810 /// \code 03811 /// kernel<<<i,j>>>(); 03812 /// \endcode 03813 const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr> 03814 CUDAKernelCallExpr; 03815 03816 } // end namespace ast_matchers 03817 } // end namespace clang 03818 03819 #endif