1327952Sdim//===- ASTMatchers.h - Structural query framework ---------------*- C++ -*-===// 2239313Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6239313Sdim// 7239313Sdim//===----------------------------------------------------------------------===// 8239313Sdim// 9239313Sdim// This file implements matchers to be used together with the MatchFinder to 10239313Sdim// match AST nodes. 11239313Sdim// 12239313Sdim// Matchers are created by generator functions, which can be combined in 13239313Sdim// a functional in-language DSL to express queries over the C++ AST. 14239313Sdim// 15239313Sdim// For example, to match a class with a certain name, one would call: 16296417Sdim// cxxRecordDecl(hasName("MyClass")) 17239313Sdim// which returns a matcher that can be used to find all AST nodes that declare 18239313Sdim// a class named 'MyClass'. 19239313Sdim// 20239313Sdim// For more complicated match expressions we're often interested in accessing 21239313Sdim// multiple parts of the matched AST nodes once a match is found. In that case, 22360784Sdim// call `.bind("name")` on match expressions that match the nodes you want to 23360784Sdim// access. 24239313Sdim// 25239313Sdim// For example, when we're interested in child classes of a certain class, we 26239313Sdim// would write: 27360784Sdim// cxxRecordDecl(hasName("MyClass"), has(recordDecl().bind("child"))) 28239313Sdim// When the match is found via the MatchFinder, a user provided callback will 29239313Sdim// be called with a BoundNodes instance that contains a mapping from the 30360784Sdim// strings that we provided for the `.bind()` calls to the nodes that were 31239313Sdim// matched. 32239313Sdim// In the given example, each time our matcher finds a match we get a callback 33296417Sdim// where "child" is bound to the RecordDecl node of the matching child 34239313Sdim// class declaration. 35239313Sdim// 36239313Sdim// See ASTMatchersInternal.h for a more in-depth explanation of the 37239313Sdim// implementation details of the matcher framework. 38239313Sdim// 39239313Sdim// See ASTMatchFinder.h for how to use the generated matchers to run over 40239313Sdim// an AST. 41239313Sdim// 42239313Sdim//===----------------------------------------------------------------------===// 43239313Sdim 44280031Sdim#ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H 45280031Sdim#define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H 46239313Sdim 47280031Sdim#include "clang/AST/ASTContext.h" 48327952Sdim#include "clang/AST/ASTTypeTraits.h" 49327952Sdim#include "clang/AST/Attr.h" 50327952Sdim#include "clang/AST/Decl.h" 51327952Sdim#include "clang/AST/DeclCXX.h" 52261991Sdim#include "clang/AST/DeclFriend.h" 53288943Sdim#include "clang/AST/DeclObjC.h" 54239313Sdim#include "clang/AST/DeclTemplate.h" 55327952Sdim#include "clang/AST/Expr.h" 56327952Sdim#include "clang/AST/ExprCXX.h" 57327952Sdim#include "clang/AST/ExprObjC.h" 58360784Sdim#include "clang/AST/LambdaCapture.h" 59327952Sdim#include "clang/AST/NestedNameSpecifier.h" 60353358Sdim#include "clang/AST/OpenMPClause.h" 61327952Sdim#include "clang/AST/OperationKinds.h" 62327952Sdim#include "clang/AST/Stmt.h" 63327952Sdim#include "clang/AST/StmtCXX.h" 64327952Sdim#include "clang/AST/StmtObjC.h" 65353358Sdim#include "clang/AST/StmtOpenMP.h" 66327952Sdim#include "clang/AST/TemplateBase.h" 67327952Sdim#include "clang/AST/TemplateName.h" 68327952Sdim#include "clang/AST/Type.h" 69327952Sdim#include "clang/AST/TypeLoc.h" 70239313Sdim#include "clang/ASTMatchers/ASTMatchersInternal.h" 71239313Sdim#include "clang/ASTMatchers/ASTMatchersMacros.h" 72327952Sdim#include "clang/Basic/AttrKinds.h" 73327952Sdim#include "clang/Basic/ExceptionSpecificationType.h" 74327952Sdim#include "clang/Basic/IdentifierTable.h" 75327952Sdim#include "clang/Basic/LLVM.h" 76327952Sdim#include "clang/Basic/SourceManager.h" 77327952Sdim#include "clang/Basic/Specifiers.h" 78327952Sdim#include "clang/Basic/TypeTraits.h" 79327952Sdim#include "llvm/ADT/ArrayRef.h" 80327952Sdim#include "llvm/ADT/SmallVector.h" 81327952Sdim#include "llvm/ADT/StringRef.h" 82327952Sdim#include "llvm/Support/Casting.h" 83327952Sdim#include "llvm/Support/Compiler.h" 84327952Sdim#include "llvm/Support/ErrorHandling.h" 85239313Sdim#include "llvm/Support/Regex.h" 86327952Sdim#include <cassert> 87327952Sdim#include <cstddef> 88239462Sdim#include <iterator> 89327952Sdim#include <limits> 90327952Sdim#include <string> 91327952Sdim#include <utility> 92327952Sdim#include <vector> 93239313Sdim 94239313Sdimnamespace clang { 95239313Sdimnamespace ast_matchers { 96239313Sdim 97341825Sdim/// Maps string IDs to AST nodes matched by parts of a matcher. 98239313Sdim/// 99243830Sdim/// The bound nodes are generated by calling \c bind("id") on the node matchers 100243830Sdim/// of the nodes we want to access later. 101239313Sdim/// 102243830Sdim/// The instances of BoundNodes are created by \c MatchFinder when the user's 103239313Sdim/// callbacks are executed every time a match is found. 104239313Sdimclass BoundNodes { 105239313Sdimpublic: 106341825Sdim /// Returns the AST node bound to \c ID. 107243830Sdim /// 108243830Sdim /// Returns NULL if there was no node bound to \c ID or if there is a node but 109239313Sdim /// it cannot be converted to the specified type. 110243830Sdim template <typename T> 111243830Sdim const T *getNodeAs(StringRef ID) const { 112243830Sdim return MyBoundNodes.getNodeAs<T>(ID); 113243830Sdim } 114243830Sdim 115341825Sdim /// Type of mapping from binding identifiers to bound nodes. This type 116261991Sdim /// is an associative container with a key type of \c std::string and a value 117261991Sdim /// type of \c clang::ast_type_traits::DynTypedNode 118327952Sdim using IDToNodeMap = internal::BoundNodesMap::IDToNodeMap; 119261991Sdim 120341825Sdim /// Retrieve mapping from binding identifiers to bound nodes. 121261991Sdim const IDToNodeMap &getMap() const { 122261991Sdim return MyBoundNodes.getMap(); 123261991Sdim } 124261991Sdim 125239313Sdimprivate: 126327952Sdim friend class internal::BoundNodesTreeBuilder; 127327952Sdim 128341825Sdim /// Create BoundNodes from a pre-filled map of bindings. 129243830Sdim BoundNodes(internal::BoundNodesMap &MyBoundNodes) 130243830Sdim : MyBoundNodes(MyBoundNodes) {} 131239313Sdim 132243830Sdim internal::BoundNodesMap MyBoundNodes; 133239313Sdim}; 134239313Sdim 135341825Sdim/// Types of matchers for the top-level classes in the AST class 136239313Sdim/// hierarchy. 137239313Sdim/// @{ 138327952Sdimusing DeclarationMatcher = internal::Matcher<Decl>; 139327952Sdimusing StatementMatcher = internal::Matcher<Stmt>; 140327952Sdimusing TypeMatcher = internal::Matcher<QualType>; 141327952Sdimusing TypeLocMatcher = internal::Matcher<TypeLoc>; 142327952Sdimusing NestedNameSpecifierMatcher = internal::Matcher<NestedNameSpecifier>; 143327952Sdimusing NestedNameSpecifierLocMatcher = internal::Matcher<NestedNameSpecifierLoc>; 144327952Sdimusing CXXCtorInitializerMatcher = internal::Matcher<CXXCtorInitializer>; 145239313Sdim/// @} 146239313Sdim 147341825Sdim/// Matches any node. 148239313Sdim/// 149239313Sdim/// Useful when another matcher requires a child matcher, but there's no 150239313Sdim/// additional constraint. This will often be used with an explicit conversion 151243830Sdim/// to an \c internal::Matcher<> type such as \c TypeMatcher. 152239313Sdim/// 153243830Sdim/// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g., 154243830Sdim/// \code 155239313Sdim/// "int* p" and "void f()" in 156239313Sdim/// int* p; 157239313Sdim/// void f(); 158243830Sdim/// \endcode 159243830Sdim/// 160243830Sdim/// Usable as: Any Matcher 161280031Sdiminline internal::TrueMatcher anything() { return internal::TrueMatcher(); } 162280031Sdim 163341825Sdim/// Matches the top declaration context. 164288943Sdim/// 165288943Sdim/// Given 166288943Sdim/// \code 167288943Sdim/// int X; 168288943Sdim/// namespace NS { 169288943Sdim/// int Y; 170288943Sdim/// } // namespace NS 171288943Sdim/// \endcode 172288943Sdim/// decl(hasDeclContext(translationUnitDecl())) 173288943Sdim/// matches "int X", but not "int Y". 174327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl> 175288943Sdim translationUnitDecl; 176288943Sdim 177341825Sdim/// Matches typedef declarations. 178280031Sdim/// 179280031Sdim/// Given 180280031Sdim/// \code 181280031Sdim/// typedef int X; 182309124Sdim/// using Y = int; 183280031Sdim/// \endcode 184280031Sdim/// typedefDecl() 185309124Sdim/// matches "typedef int X", but not "using Y = int" 186327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl> 187327952Sdim typedefDecl; 188280031Sdim 189341825Sdim/// Matches typedef name declarations. 190309124Sdim/// 191309124Sdim/// Given 192309124Sdim/// \code 193309124Sdim/// typedef int X; 194309124Sdim/// using Y = int; 195309124Sdim/// \endcode 196309124Sdim/// typedefNameDecl() 197309124Sdim/// matches "typedef int X" and "using Y = int" 198327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl> 199309124Sdim typedefNameDecl; 200309124Sdim 201341825Sdim/// Matches type alias declarations. 202309124Sdim/// 203309124Sdim/// Given 204309124Sdim/// \code 205309124Sdim/// typedef int X; 206309124Sdim/// using Y = int; 207309124Sdim/// \endcode 208309124Sdim/// typeAliasDecl() 209309124Sdim/// matches "using Y = int", but not "typedef int X" 210327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> 211327952Sdim typeAliasDecl; 212309124Sdim 213341825Sdim/// Matches type alias template declarations. 214321369Sdim/// 215321369Sdim/// typeAliasTemplateDecl() matches 216321369Sdim/// \code 217321369Sdim/// template <typename T> 218321369Sdim/// using Y = X<T>; 219321369Sdim/// \endcode 220327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasTemplateDecl> 221321369Sdim typeAliasTemplateDecl; 222321369Sdim 223341825Sdim/// Matches AST nodes that were expanded within the main-file. 224280031Sdim/// 225296417Sdim/// Example matches X but not Y 226296417Sdim/// (matcher = cxxRecordDecl(isExpansionInMainFile()) 227280031Sdim/// \code 228280031Sdim/// #include <Y.h> 229280031Sdim/// class X {}; 230280031Sdim/// \endcode 231280031Sdim/// Y.h: 232280031Sdim/// \code 233280031Sdim/// class Y {}; 234280031Sdim/// \endcode 235280031Sdim/// 236280031Sdim/// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc> 237280031SdimAST_POLYMORPHIC_MATCHER(isExpansionInMainFile, 238288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) { 239280031Sdim auto &SourceManager = Finder->getASTContext().getSourceManager(); 240280031Sdim return SourceManager.isInMainFile( 241344779Sdim SourceManager.getExpansionLoc(Node.getBeginLoc())); 242239313Sdim} 243239313Sdim 244341825Sdim/// Matches AST nodes that were expanded within system-header-files. 245280031Sdim/// 246280031Sdim/// Example matches Y but not X 247296417Sdim/// (matcher = cxxRecordDecl(isExpansionInSystemHeader()) 248280031Sdim/// \code 249280031Sdim/// #include <SystemHeader.h> 250280031Sdim/// class X {}; 251280031Sdim/// \endcode 252280031Sdim/// SystemHeader.h: 253280031Sdim/// \code 254280031Sdim/// class Y {}; 255280031Sdim/// \endcode 256280031Sdim/// 257280031Sdim/// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc> 258280031SdimAST_POLYMORPHIC_MATCHER(isExpansionInSystemHeader, 259288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) { 260280031Sdim auto &SourceManager = Finder->getASTContext().getSourceManager(); 261344779Sdim auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc()); 262280031Sdim if (ExpansionLoc.isInvalid()) { 263280031Sdim return false; 264280031Sdim } 265280031Sdim return SourceManager.isInSystemHeader(ExpansionLoc); 266280031Sdim} 267280031Sdim 268341825Sdim/// Matches AST nodes that were expanded within files whose name is 269280031Sdim/// partially matching a given regex. 270280031Sdim/// 271280031Sdim/// Example matches Y but not X 272296417Sdim/// (matcher = cxxRecordDecl(isExpansionInFileMatching("AST.*")) 273280031Sdim/// \code 274280031Sdim/// #include "ASTMatcher.h" 275280031Sdim/// class X {}; 276280031Sdim/// \endcode 277280031Sdim/// ASTMatcher.h: 278280031Sdim/// \code 279280031Sdim/// class Y {}; 280280031Sdim/// \endcode 281280031Sdim/// 282280031Sdim/// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc> 283280031SdimAST_POLYMORPHIC_MATCHER_P(isExpansionInFileMatching, 284288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc), 285280031Sdim std::string, RegExp) { 286280031Sdim auto &SourceManager = Finder->getASTContext().getSourceManager(); 287344779Sdim auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc()); 288280031Sdim if (ExpansionLoc.isInvalid()) { 289280031Sdim return false; 290280031Sdim } 291280031Sdim auto FileEntry = 292280031Sdim SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc)); 293280031Sdim if (!FileEntry) { 294280031Sdim return false; 295280031Sdim } 296280031Sdim 297280031Sdim auto Filename = FileEntry->getName(); 298280031Sdim llvm::Regex RE(RegExp); 299280031Sdim return RE.match(Filename); 300280031Sdim} 301280031Sdim 302341825Sdim/// Matches declarations. 303239313Sdim/// 304239313Sdim/// Examples matches \c X, \c C, and the friend declaration inside \c C; 305239313Sdim/// \code 306239313Sdim/// void X(); 307239313Sdim/// class C { 308239313Sdim/// friend X; 309239313Sdim/// }; 310239313Sdim/// \endcode 311327952Sdimextern const internal::VariadicAllOfMatcher<Decl> decl; 312239313Sdim 313341825Sdim/// Matches a declaration of a linkage specification. 314280031Sdim/// 315280031Sdim/// Given 316280031Sdim/// \code 317280031Sdim/// extern "C" {} 318280031Sdim/// \endcode 319280031Sdim/// linkageSpecDecl() 320280031Sdim/// matches "extern "C" {}" 321327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl> 322280031Sdim linkageSpecDecl; 323280031Sdim 324341825Sdim/// Matches a declaration of anything that could have a name. 325239313Sdim/// 326243830Sdim/// Example matches \c X, \c S, the anonymous union type, \c i, and \c U; 327243830Sdim/// \code 328239313Sdim/// typedef int X; 329239313Sdim/// struct S { 330239313Sdim/// union { 331239313Sdim/// int i; 332239313Sdim/// } U; 333239313Sdim/// }; 334243830Sdim/// \endcode 335327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl; 336239313Sdim 337341825Sdim/// Matches a declaration of label. 338309124Sdim/// 339309124Sdim/// Given 340309124Sdim/// \code 341309124Sdim/// goto FOO; 342309124Sdim/// FOO: bar(); 343309124Sdim/// \endcode 344309124Sdim/// labelDecl() 345309124Sdim/// matches 'FOO:' 346327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, LabelDecl> labelDecl; 347309124Sdim 348341825Sdim/// Matches a declaration of a namespace. 349251662Sdim/// 350251662Sdim/// Given 351251662Sdim/// \code 352251662Sdim/// namespace {} 353251662Sdim/// namespace test {} 354251662Sdim/// \endcode 355251662Sdim/// namespaceDecl() 356251662Sdim/// matches "namespace {}" and "namespace test {}" 357327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> 358327952Sdim namespaceDecl; 359251662Sdim 360341825Sdim/// Matches a declaration of a namespace alias. 361296417Sdim/// 362296417Sdim/// Given 363296417Sdim/// \code 364296417Sdim/// namespace test {} 365296417Sdim/// namespace alias = ::test; 366296417Sdim/// \endcode 367296417Sdim/// namespaceAliasDecl() 368296417Sdim/// matches "namespace alias" but not "namespace test" 369327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceAliasDecl> 370296417Sdim namespaceAliasDecl; 371296417Sdim 372341825Sdim/// Matches class, struct, and union declarations. 373296417Sdim/// 374296417Sdim/// Example matches \c X, \c Z, \c U, and \c S 375296417Sdim/// \code 376296417Sdim/// class X; 377296417Sdim/// template<class T> class Z {}; 378296417Sdim/// struct S {}; 379296417Sdim/// union U {}; 380296417Sdim/// \endcode 381327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, RecordDecl> recordDecl; 382296417Sdim 383341825Sdim/// Matches C++ class declarations. 384239313Sdim/// 385243830Sdim/// Example matches \c X, \c Z 386243830Sdim/// \code 387239313Sdim/// class X; 388239313Sdim/// template<class T> class Z {}; 389243830Sdim/// \endcode 390327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, CXXRecordDecl> 391327952Sdim cxxRecordDecl; 392239313Sdim 393341825Sdim/// Matches C++ class template declarations. 394243830Sdim/// 395243830Sdim/// Example matches \c Z 396243830Sdim/// \code 397243830Sdim/// template<class T> class Z {}; 398243830Sdim/// \endcode 399327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ClassTemplateDecl> 400327952Sdim classTemplateDecl; 401243830Sdim 402341825Sdim/// Matches C++ class template specializations. 403239313Sdim/// 404239313Sdim/// Given 405243830Sdim/// \code 406239313Sdim/// template<typename T> class A {}; 407239313Sdim/// template<> class A<double> {}; 408239313Sdim/// A<int> a; 409243830Sdim/// \endcode 410243830Sdim/// classTemplateSpecializationDecl() 411239313Sdim/// matches the specializations \c A<int> and \c A<double> 412327952Sdimextern const internal::VariadicDynCastAllOfMatcher< 413327952Sdim Decl, ClassTemplateSpecializationDecl> 414327952Sdim classTemplateSpecializationDecl; 415239313Sdim 416344779Sdim/// Matches C++ class template partial specializations. 417344779Sdim/// 418344779Sdim/// Given 419344779Sdim/// \code 420344779Sdim/// template<class T1, class T2, int I> 421344779Sdim/// class A {}; 422344779Sdim/// 423344779Sdim/// template<class T, int I> 424344779Sdim/// class A<T, T*, I> {}; 425344779Sdim/// 426344779Sdim/// template<> 427344779Sdim/// class A<int, int, 1> {}; 428344779Sdim/// \endcode 429344779Sdim/// classTemplatePartialSpecializationDecl() 430344779Sdim/// matches the specialization \c A<T,T*,I> but not \c A<int,int,1> 431344779Sdimextern const internal::VariadicDynCastAllOfMatcher< 432344779Sdim Decl, ClassTemplatePartialSpecializationDecl> 433344779Sdim classTemplatePartialSpecializationDecl; 434344779Sdim 435341825Sdim/// Matches declarator declarations (field, variable, function 436261991Sdim/// and non-type template parameter declarations). 437261991Sdim/// 438261991Sdim/// Given 439261991Sdim/// \code 440261991Sdim/// class X { int y; }; 441261991Sdim/// \endcode 442261991Sdim/// declaratorDecl() 443261991Sdim/// matches \c int y. 444327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl> 445261991Sdim declaratorDecl; 446261991Sdim 447341825Sdim/// Matches parameter variable declarations. 448261991Sdim/// 449261991Sdim/// Given 450261991Sdim/// \code 451261991Sdim/// void f(int x); 452261991Sdim/// \endcode 453261991Sdim/// parmVarDecl() 454261991Sdim/// matches \c int x. 455327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> 456327952Sdim parmVarDecl; 457261991Sdim 458341825Sdim/// Matches C++ access specifier declarations. 459249423Sdim/// 460249423Sdim/// Given 461249423Sdim/// \code 462249423Sdim/// class C { 463249423Sdim/// public: 464249423Sdim/// int a; 465249423Sdim/// }; 466249423Sdim/// \endcode 467249423Sdim/// accessSpecDecl() 468249423Sdim/// matches 'public:' 469327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, AccessSpecDecl> 470327952Sdim accessSpecDecl; 471249423Sdim 472341825Sdim/// Matches constructor initializers. 473261991Sdim/// 474261991Sdim/// Examples matches \c i(42). 475261991Sdim/// \code 476261991Sdim/// class C { 477261991Sdim/// C() : i(42) {} 478261991Sdim/// int i; 479261991Sdim/// }; 480261991Sdim/// \endcode 481327952Sdimextern const internal::VariadicAllOfMatcher<CXXCtorInitializer> 482327952Sdim cxxCtorInitializer; 483261991Sdim 484341825Sdim/// Matches template arguments. 485280031Sdim/// 486280031Sdim/// Given 487280031Sdim/// \code 488280031Sdim/// template <typename T> struct C {}; 489280031Sdim/// C<int> c; 490280031Sdim/// \endcode 491280031Sdim/// templateArgument() 492280031Sdim/// matches 'int' in C<int>. 493327952Sdimextern const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument; 494280031Sdim 495341825Sdim/// Matches template name. 496314564Sdim/// 497314564Sdim/// Given 498314564Sdim/// \code 499314564Sdim/// template <typename T> class X { }; 500314564Sdim/// X<int> xi; 501314564Sdim/// \endcode 502314564Sdim/// templateName() 503314564Sdim/// matches 'X' in X<int>. 504327952Sdimextern const internal::VariadicAllOfMatcher<TemplateName> templateName; 505314564Sdim 506341825Sdim/// Matches non-type template parameter declarations. 507296417Sdim/// 508296417Sdim/// Given 509296417Sdim/// \code 510296417Sdim/// template <typename T, int N> struct C {}; 511296417Sdim/// \endcode 512296417Sdim/// nonTypeTemplateParmDecl() 513296417Sdim/// matches 'N', but not 'T'. 514327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, 515327952Sdim NonTypeTemplateParmDecl> 516327952Sdim nonTypeTemplateParmDecl; 517296417Sdim 518341825Sdim/// Matches template type parameter declarations. 519296417Sdim/// 520296417Sdim/// Given 521296417Sdim/// \code 522296417Sdim/// template <typename T, int N> struct C {}; 523296417Sdim/// \endcode 524296417Sdim/// templateTypeParmDecl() 525296417Sdim/// matches 'T', but not 'N'. 526327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, TemplateTypeParmDecl> 527327952Sdim templateTypeParmDecl; 528296417Sdim 529341825Sdim/// Matches public C++ declarations. 530249423Sdim/// 531249423Sdim/// Given 532249423Sdim/// \code 533249423Sdim/// class C { 534249423Sdim/// public: int a; 535249423Sdim/// protected: int b; 536249423Sdim/// private: int c; 537249423Sdim/// }; 538249423Sdim/// \endcode 539249423Sdim/// fieldDecl(isPublic()) 540309124Sdim/// matches 'int a;' 541249423SdimAST_MATCHER(Decl, isPublic) { 542249423Sdim return Node.getAccess() == AS_public; 543249423Sdim} 544249423Sdim 545341825Sdim/// Matches protected C++ declarations. 546249423Sdim/// 547249423Sdim/// Given 548249423Sdim/// \code 549249423Sdim/// class C { 550249423Sdim/// public: int a; 551249423Sdim/// protected: int b; 552249423Sdim/// private: int c; 553249423Sdim/// }; 554249423Sdim/// \endcode 555249423Sdim/// fieldDecl(isProtected()) 556309124Sdim/// matches 'int b;' 557249423SdimAST_MATCHER(Decl, isProtected) { 558249423Sdim return Node.getAccess() == AS_protected; 559249423Sdim} 560249423Sdim 561341825Sdim/// Matches private C++ declarations. 562249423Sdim/// 563249423Sdim/// Given 564249423Sdim/// \code 565249423Sdim/// class C { 566249423Sdim/// public: int a; 567249423Sdim/// protected: int b; 568249423Sdim/// private: int c; 569249423Sdim/// }; 570249423Sdim/// \endcode 571249423Sdim/// fieldDecl(isPrivate()) 572309124Sdim/// matches 'int c;' 573249423SdimAST_MATCHER(Decl, isPrivate) { 574249423Sdim return Node.getAccess() == AS_private; 575249423Sdim} 576249423Sdim 577341825Sdim/// Matches non-static data members that are bit-fields. 578309124Sdim/// 579309124Sdim/// Given 580309124Sdim/// \code 581309124Sdim/// class C { 582309124Sdim/// int a : 2; 583309124Sdim/// int b; 584309124Sdim/// }; 585309124Sdim/// \endcode 586309124Sdim/// fieldDecl(isBitField()) 587309124Sdim/// matches 'int a;' but not 'int b;'. 588309124SdimAST_MATCHER(FieldDecl, isBitField) { 589309124Sdim return Node.isBitField(); 590309124Sdim} 591309124Sdim 592341825Sdim/// Matches non-static data members that are bit-fields of the specified 593314564Sdim/// bit width. 594309124Sdim/// 595309124Sdim/// Given 596309124Sdim/// \code 597309124Sdim/// class C { 598309124Sdim/// int a : 2; 599309124Sdim/// int b : 4; 600309124Sdim/// int c : 2; 601309124Sdim/// }; 602309124Sdim/// \endcode 603314564Sdim/// fieldDecl(hasBitWidth(2)) 604309124Sdim/// matches 'int a;' and 'int c;' but not 'int b;'. 605309124SdimAST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) { 606309124Sdim return Node.isBitField() && 607309124Sdim Node.getBitWidthValue(Finder->getASTContext()) == Width; 608309124Sdim} 609309124Sdim 610341825Sdim/// Matches non-static data members that have an in-class initializer. 611314564Sdim/// 612314564Sdim/// Given 613314564Sdim/// \code 614314564Sdim/// class C { 615314564Sdim/// int a = 2; 616314564Sdim/// int b = 3; 617314564Sdim/// int c; 618314564Sdim/// }; 619314564Sdim/// \endcode 620314564Sdim/// fieldDecl(hasInClassInitializer(integerLiteral(equals(2)))) 621314564Sdim/// matches 'int a;' but not 'int b;'. 622314564Sdim/// fieldDecl(hasInClassInitializer(anything())) 623314564Sdim/// matches 'int a;' and 'int b;' but not 'int c;'. 624314564SdimAST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>, 625314564Sdim InnerMatcher) { 626314564Sdim const Expr *Initializer = Node.getInClassInitializer(); 627314564Sdim return (Initializer != nullptr && 628314564Sdim InnerMatcher.matches(*Initializer, Finder, Builder)); 629314564Sdim} 630314564Sdim 631341825Sdim/// Determines whether the function is "main", which is the entry point 632341825Sdim/// into an executable program. 633341825SdimAST_MATCHER(FunctionDecl, isMain) { 634341825Sdim return Node.isMain(); 635341825Sdim} 636341825Sdim 637341825Sdim/// Matches the specialized template of a specialization declaration. 638327952Sdim/// 639327952Sdim/// Given 640327952Sdim/// \code 641344779Sdim/// template<typename T> class A {}; #1 642344779Sdim/// template<> class A<int> {}; #2 643327952Sdim/// \endcode 644327952Sdim/// classTemplateSpecializationDecl(hasSpecializedTemplate(classTemplateDecl())) 645344779Sdim/// matches '#2' with classTemplateDecl() matching the class template 646344779Sdim/// declaration of 'A' at #1. 647327952SdimAST_MATCHER_P(ClassTemplateSpecializationDecl, hasSpecializedTemplate, 648327952Sdim internal::Matcher<ClassTemplateDecl>, InnerMatcher) { 649327952Sdim const ClassTemplateDecl* Decl = Node.getSpecializedTemplate(); 650327952Sdim return (Decl != nullptr && 651327952Sdim InnerMatcher.matches(*Decl, Finder, Builder)); 652327952Sdim} 653327952Sdim 654341825Sdim/// Matches a declaration that has been implicitly added 655276479Sdim/// by the compiler (eg. implicit default/copy constructors). 656276479SdimAST_MATCHER(Decl, isImplicit) { 657276479Sdim return Node.isImplicit(); 658276479Sdim} 659276479Sdim 660341825Sdim/// Matches classTemplateSpecializations, templateSpecializationType and 661314564Sdim/// functionDecl that have at least one TemplateArgument matching the given 662314564Sdim/// InnerMatcher. 663239313Sdim/// 664239313Sdim/// Given 665243830Sdim/// \code 666239313Sdim/// template<typename T> class A {}; 667239313Sdim/// template<> class A<double> {}; 668239313Sdim/// A<int> a; 669314564Sdim/// 670314564Sdim/// template<typename T> f() {}; 671314564Sdim/// void func() { f<int>(); }; 672243830Sdim/// \endcode 673314564Sdim/// 674314564Sdim/// \endcode 675243830Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 676239313Sdim/// refersToType(asString("int")))) 677239313Sdim/// matches the specialization \c A<int> 678314564Sdim/// 679314564Sdim/// functionDecl(hasAnyTemplateArgument(refersToType(asString("int")))) 680314564Sdim/// matches the specialization \c f<int> 681276479SdimAST_POLYMORPHIC_MATCHER_P( 682276479Sdim hasAnyTemplateArgument, 683288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, 684314564Sdim TemplateSpecializationType, 685314564Sdim FunctionDecl), 686276479Sdim internal::Matcher<TemplateArgument>, InnerMatcher) { 687276479Sdim ArrayRef<TemplateArgument> List = 688276479Sdim internal::getTemplateSpecializationArgs(Node); 689261991Sdim return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder, 690261991Sdim Builder); 691239313Sdim} 692239313Sdim 693360784Sdim/// Causes all nested matchers to be matched with the specified traversal kind. 694360784Sdim/// 695360784Sdim/// Given 696360784Sdim/// \code 697360784Sdim/// void foo() 698360784Sdim/// { 699360784Sdim/// int i = 3.0; 700360784Sdim/// } 701360784Sdim/// \endcode 702360784Sdim/// The matcher 703360784Sdim/// \code 704360784Sdim/// traverse(ast_type_traits::TK_IgnoreImplicitCastsAndParentheses, 705360784Sdim/// varDecl(hasInitializer(floatLiteral().bind("init"))) 706360784Sdim/// ) 707360784Sdim/// \endcode 708360784Sdim/// matches the variable declaration with "init" bound to the "3.0". 709360784Sdimtemplate <typename T> 710360784Sdiminternal::Matcher<T> traverse(ast_type_traits::TraversalKind TK, 711360784Sdim const internal::Matcher<T> &InnerMatcher) { 712360784Sdim return internal::DynTypedMatcher::constructRestrictedWrapper( 713360784Sdim new internal::TraversalMatcher<T>(TK, InnerMatcher), 714360784Sdim InnerMatcher.getID().first) 715360784Sdim .template unconditionalConvertTo<T>(); 716360784Sdim} 717360784Sdim 718360784Sdimtemplate <typename T> 719360784Sdiminternal::BindableMatcher<T> 720360784Sdimtraverse(ast_type_traits::TraversalKind TK, 721360784Sdim const internal::BindableMatcher<T> &InnerMatcher) { 722360784Sdim return internal::BindableMatcher<T>( 723360784Sdim internal::DynTypedMatcher::constructRestrictedWrapper( 724360784Sdim new internal::TraversalMatcher<T>(TK, InnerMatcher), 725360784Sdim InnerMatcher.getID().first) 726360784Sdim .template unconditionalConvertTo<T>()); 727360784Sdim} 728360784Sdim 729360784Sdimtemplate <typename... T> 730360784Sdiminternal::TraversalWrapper<internal::VariadicOperatorMatcher<T...>> 731360784Sdimtraverse(ast_type_traits::TraversalKind TK, 732360784Sdim const internal::VariadicOperatorMatcher<T...> &InnerMatcher) { 733360784Sdim return internal::TraversalWrapper<internal::VariadicOperatorMatcher<T...>>( 734360784Sdim TK, InnerMatcher); 735360784Sdim} 736360784Sdim 737360784Sdimtemplate <template <typename ToArg, typename FromArg> class ArgumentAdapterT, 738360784Sdim typename T, typename ToTypes> 739360784Sdiminternal::TraversalWrapper< 740360784Sdim internal::ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT, T, ToTypes>> 741360784Sdimtraverse(ast_type_traits::TraversalKind TK, 742360784Sdim const internal::ArgumentAdaptingMatcherFuncAdaptor< 743360784Sdim ArgumentAdapterT, T, ToTypes> &InnerMatcher) { 744360784Sdim return internal::TraversalWrapper< 745360784Sdim internal::ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT, T, 746360784Sdim ToTypes>>(TK, InnerMatcher); 747360784Sdim} 748360784Sdim 749360784Sdimtemplate <template <typename T, typename P1> class MatcherT, typename P1, 750360784Sdim typename ReturnTypesF> 751360784Sdiminternal::TraversalWrapper< 752360784Sdim internal::PolymorphicMatcherWithParam1<MatcherT, P1, ReturnTypesF>> 753360784Sdimtraverse( 754360784Sdim ast_type_traits::TraversalKind TK, 755360784Sdim const internal::PolymorphicMatcherWithParam1<MatcherT, P1, ReturnTypesF> 756360784Sdim &InnerMatcher) { 757360784Sdim return internal::TraversalWrapper< 758360784Sdim internal::PolymorphicMatcherWithParam1<MatcherT, P1, ReturnTypesF>>( 759360784Sdim TK, InnerMatcher); 760360784Sdim} 761360784Sdim 762360784Sdimtemplate <template <typename T, typename P1, typename P2> class MatcherT, 763360784Sdim typename P1, typename P2, typename ReturnTypesF> 764360784Sdiminternal::TraversalWrapper< 765360784Sdim internal::PolymorphicMatcherWithParam2<MatcherT, P1, P2, ReturnTypesF>> 766360784Sdimtraverse( 767360784Sdim ast_type_traits::TraversalKind TK, 768360784Sdim const internal::PolymorphicMatcherWithParam2<MatcherT, P1, P2, ReturnTypesF> 769360784Sdim &InnerMatcher) { 770360784Sdim return internal::TraversalWrapper< 771360784Sdim internal::PolymorphicMatcherWithParam2<MatcherT, P1, P2, ReturnTypesF>>( 772360784Sdim TK, InnerMatcher); 773360784Sdim} 774360784Sdim 775341825Sdim/// Matches expressions that match InnerMatcher after any implicit AST 776309124Sdim/// nodes are stripped off. 777309124Sdim/// 778309124Sdim/// Parentheses and explicit casts are not discarded. 779309124Sdim/// Given 780309124Sdim/// \code 781309124Sdim/// class C {}; 782309124Sdim/// C a = C(); 783309124Sdim/// C b; 784309124Sdim/// C c = b; 785309124Sdim/// \endcode 786309124Sdim/// The matchers 787309124Sdim/// \code 788309124Sdim/// varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr()))) 789309124Sdim/// \endcode 790309124Sdim/// would match the declarations for a, b, and c. 791309124Sdim/// While 792309124Sdim/// \code 793309124Sdim/// varDecl(hasInitializer(cxxConstructExpr())) 794309124Sdim/// \endcode 795309124Sdim/// only match the declarations for b and c. 796341825SdimAST_MATCHER_P(Expr, ignoringImplicit, internal::Matcher<Expr>, 797309124Sdim InnerMatcher) { 798309124Sdim return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder); 799309124Sdim} 800309124Sdim 801341825Sdim/// Matches expressions that match InnerMatcher after any implicit casts 802239462Sdim/// are stripped off. 803239462Sdim/// 804239462Sdim/// Parentheses and explicit casts are not discarded. 805239462Sdim/// Given 806243830Sdim/// \code 807239462Sdim/// int arr[5]; 808239462Sdim/// int a = 0; 809239462Sdim/// char b = 0; 810239462Sdim/// const int c = a; 811239462Sdim/// int *d = arr; 812239462Sdim/// long e = (long) 0l; 813243830Sdim/// \endcode 814239462Sdim/// The matchers 815243830Sdim/// \code 816243830Sdim/// varDecl(hasInitializer(ignoringImpCasts(integerLiteral()))) 817243830Sdim/// varDecl(hasInitializer(ignoringImpCasts(declRefExpr()))) 818243830Sdim/// \endcode 819239462Sdim/// would match the declarations for a, b, c, and d, but not e. 820243830Sdim/// While 821243830Sdim/// \code 822243830Sdim/// varDecl(hasInitializer(integerLiteral())) 823243830Sdim/// varDecl(hasInitializer(declRefExpr())) 824243830Sdim/// \endcode 825239462Sdim/// only match the declarations for b, c, and d. 826239462SdimAST_MATCHER_P(Expr, ignoringImpCasts, 827239462Sdim internal::Matcher<Expr>, InnerMatcher) { 828239462Sdim return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder); 829239462Sdim} 830239462Sdim 831341825Sdim/// Matches expressions that match InnerMatcher after parentheses and 832239462Sdim/// casts are stripped off. 833239462Sdim/// 834239462Sdim/// Implicit and non-C Style casts are also discarded. 835239462Sdim/// Given 836243830Sdim/// \code 837239462Sdim/// int a = 0; 838239462Sdim/// char b = (0); 839239462Sdim/// void* c = reinterpret_cast<char*>(0); 840239462Sdim/// char d = char(0); 841243830Sdim/// \endcode 842239462Sdim/// The matcher 843243830Sdim/// varDecl(hasInitializer(ignoringParenCasts(integerLiteral()))) 844239462Sdim/// would match the declarations for a, b, c, and d. 845239462Sdim/// while 846243830Sdim/// varDecl(hasInitializer(integerLiteral())) 847239462Sdim/// only match the declaration for a. 848239462SdimAST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) { 849239462Sdim return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder); 850239462Sdim} 851239462Sdim 852341825Sdim/// Matches expressions that match InnerMatcher after implicit casts and 853239462Sdim/// parentheses are stripped off. 854239462Sdim/// 855239462Sdim/// Explicit casts are not discarded. 856239462Sdim/// Given 857243830Sdim/// \code 858239462Sdim/// int arr[5]; 859239462Sdim/// int a = 0; 860239462Sdim/// char b = (0); 861239462Sdim/// const int c = a; 862239462Sdim/// int *d = (arr); 863239462Sdim/// long e = ((long) 0l); 864243830Sdim/// \endcode 865239462Sdim/// The matchers 866243830Sdim/// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral()))) 867243830Sdim/// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr()))) 868239462Sdim/// would match the declarations for a, b, c, and d, but not e. 869239462Sdim/// while 870243830Sdim/// varDecl(hasInitializer(integerLiteral())) 871243830Sdim/// varDecl(hasInitializer(declRefExpr())) 872239462Sdim/// would only match the declaration for a. 873239462SdimAST_MATCHER_P(Expr, ignoringParenImpCasts, 874239462Sdim internal::Matcher<Expr>, InnerMatcher) { 875239462Sdim return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder); 876239462Sdim} 877239462Sdim 878341825Sdim/// Matches types that match InnerMatcher after any parens are stripped. 879309124Sdim/// 880309124Sdim/// Given 881309124Sdim/// \code 882309124Sdim/// void (*fp)(void); 883309124Sdim/// \endcode 884309124Sdim/// The matcher 885309124Sdim/// \code 886309124Sdim/// varDecl(hasType(pointerType(pointee(ignoringParens(functionType()))))) 887309124Sdim/// \endcode 888309124Sdim/// would match the declaration for fp. 889344779SdimAST_MATCHER_P_OVERLOAD(QualType, ignoringParens, internal::Matcher<QualType>, 890344779Sdim InnerMatcher, 0) { 891309124Sdim return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder); 892309124Sdim} 893309124Sdim 894344779Sdim/// Overload \c ignoringParens for \c Expr. 895344779Sdim/// 896344779Sdim/// Given 897344779Sdim/// \code 898344779Sdim/// const char* str = ("my-string"); 899344779Sdim/// \endcode 900344779Sdim/// The matcher 901344779Sdim/// \code 902344779Sdim/// implicitCastExpr(hasSourceExpression(ignoringParens(stringLiteral()))) 903344779Sdim/// \endcode 904344779Sdim/// would match the implicit cast resulting from the assignment. 905344779SdimAST_MATCHER_P_OVERLOAD(Expr, ignoringParens, internal::Matcher<Expr>, 906344779Sdim InnerMatcher, 1) { 907344779Sdim const Expr *E = Node.IgnoreParens(); 908344779Sdim return InnerMatcher.matches(*E, Finder, Builder); 909344779Sdim} 910344779Sdim 911344779Sdim/// Matches expressions that are instantiation-dependent even if it is 912344779Sdim/// neither type- nor value-dependent. 913344779Sdim/// 914344779Sdim/// In the following example, the expression sizeof(sizeof(T() + T())) 915344779Sdim/// is instantiation-dependent (since it involves a template parameter T), 916344779Sdim/// but is neither type- nor value-dependent, since the type of the inner 917344779Sdim/// sizeof is known (std::size_t) and therefore the size of the outer 918344779Sdim/// sizeof is known. 919344779Sdim/// \code 920344779Sdim/// template<typename T> 921344779Sdim/// void f(T x, T y) { sizeof(sizeof(T() + T()); } 922344779Sdim/// \endcode 923344779Sdim/// expr(isInstantiationDependent()) matches sizeof(sizeof(T() + T()) 924344779SdimAST_MATCHER(Expr, isInstantiationDependent) { 925344779Sdim return Node.isInstantiationDependent(); 926344779Sdim} 927344779Sdim 928344779Sdim/// Matches expressions that are type-dependent because the template type 929344779Sdim/// is not yet instantiated. 930344779Sdim/// 931344779Sdim/// For example, the expressions "x" and "x + y" are type-dependent in 932344779Sdim/// the following code, but "y" is not type-dependent: 933344779Sdim/// \code 934344779Sdim/// template<typename T> 935344779Sdim/// void add(T x, int y) { 936344779Sdim/// x + y; 937344779Sdim/// } 938344779Sdim/// \endcode 939344779Sdim/// expr(isTypeDependent()) matches x + y 940344779SdimAST_MATCHER(Expr, isTypeDependent) { return Node.isTypeDependent(); } 941344779Sdim 942344779Sdim/// Matches expression that are value-dependent because they contain a 943344779Sdim/// non-type template parameter. 944344779Sdim/// 945344779Sdim/// For example, the array bound of "Chars" in the following example is 946344779Sdim/// value-dependent. 947344779Sdim/// \code 948344779Sdim/// template<int Size> int f() { return Size; } 949344779Sdim/// \endcode 950344779Sdim/// expr(isValueDependent()) matches return Size 951344779SdimAST_MATCHER(Expr, isValueDependent) { return Node.isValueDependent(); } 952344779Sdim 953341825Sdim/// Matches classTemplateSpecializations, templateSpecializationType and 954314564Sdim/// functionDecl where the n'th TemplateArgument matches the given InnerMatcher. 955239313Sdim/// 956239313Sdim/// Given 957243830Sdim/// \code 958239313Sdim/// template<typename T, typename U> class A {}; 959239313Sdim/// A<bool, int> b; 960239313Sdim/// A<int, bool> c; 961314564Sdim/// 962341825Sdim/// template<typename T> void f() {} 963314564Sdim/// void func() { f<int>(); }; 964243830Sdim/// \endcode 965243830Sdim/// classTemplateSpecializationDecl(hasTemplateArgument( 966239313Sdim/// 1, refersToType(asString("int")))) 967239313Sdim/// matches the specialization \c A<bool, int> 968314564Sdim/// 969314564Sdim/// functionDecl(hasTemplateArgument(0, refersToType(asString("int")))) 970314564Sdim/// matches the specialization \c f<int> 971276479SdimAST_POLYMORPHIC_MATCHER_P2( 972276479Sdim hasTemplateArgument, 973288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, 974314564Sdim TemplateSpecializationType, 975314564Sdim FunctionDecl), 976276479Sdim unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) { 977276479Sdim ArrayRef<TemplateArgument> List = 978276479Sdim internal::getTemplateSpecializationArgs(Node); 979239313Sdim if (List.size() <= N) 980239313Sdim return false; 981276479Sdim return InnerMatcher.matches(List[N], Finder, Builder); 982239313Sdim} 983239313Sdim 984341825Sdim/// Matches if the number of template arguments equals \p N. 985280031Sdim/// 986280031Sdim/// Given 987280031Sdim/// \code 988280031Sdim/// template<typename T> struct C {}; 989280031Sdim/// C<int> c; 990280031Sdim/// \endcode 991280031Sdim/// classTemplateSpecializationDecl(templateArgumentCountIs(1)) 992280031Sdim/// matches C<int>. 993280031SdimAST_POLYMORPHIC_MATCHER_P( 994280031Sdim templateArgumentCountIs, 995288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, 996288943Sdim TemplateSpecializationType), 997280031Sdim unsigned, N) { 998280031Sdim return internal::getTemplateSpecializationArgs(Node).size() == N; 999280031Sdim} 1000280031Sdim 1001341825Sdim/// Matches a TemplateArgument that refers to a certain type. 1002239313Sdim/// 1003239313Sdim/// Given 1004243830Sdim/// \code 1005239313Sdim/// struct X {}; 1006239313Sdim/// template<typename T> struct A {}; 1007239313Sdim/// A<X> a; 1008243830Sdim/// \endcode 1009243830Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 1010239313Sdim/// refersToType(class(hasName("X"))))) 1011239313Sdim/// matches the specialization \c A<X> 1012239313SdimAST_MATCHER_P(TemplateArgument, refersToType, 1013243830Sdim internal::Matcher<QualType>, InnerMatcher) { 1014239313Sdim if (Node.getKind() != TemplateArgument::Type) 1015239313Sdim return false; 1016243830Sdim return InnerMatcher.matches(Node.getAsType(), Finder, Builder); 1017239313Sdim} 1018239313Sdim 1019341825Sdim/// Matches a TemplateArgument that refers to a certain template. 1020314564Sdim/// 1021314564Sdim/// Given 1022314564Sdim/// \code 1023314564Sdim/// template<template <typename> class S> class X {}; 1024344779Sdim/// template<typename T> class Y {}; 1025314564Sdim/// X<Y> xi; 1026314564Sdim/// \endcode 1027314564Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 1028314564Sdim/// refersToTemplate(templateName()))) 1029314564Sdim/// matches the specialization \c X<Y> 1030314564SdimAST_MATCHER_P(TemplateArgument, refersToTemplate, 1031314564Sdim internal::Matcher<TemplateName>, InnerMatcher) { 1032314564Sdim if (Node.getKind() != TemplateArgument::Template) 1033314564Sdim return false; 1034314564Sdim return InnerMatcher.matches(Node.getAsTemplate(), Finder, Builder); 1035314564Sdim} 1036314564Sdim 1037341825Sdim/// Matches a canonical TemplateArgument that refers to a certain 1038276479Sdim/// declaration. 1039239313Sdim/// 1040239313Sdim/// Given 1041243830Sdim/// \code 1042341825Sdim/// struct B { int next; }; 1043341825Sdim/// template<int(B::*next_ptr)> struct A {}; 1044239313Sdim/// A<&B::next> a; 1045243830Sdim/// \endcode 1046243830Sdim/// classTemplateSpecializationDecl(hasAnyTemplateArgument( 1047341825Sdim/// refersToDeclaration(fieldDecl(hasName("next"))))) 1048243830Sdim/// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 1049239313Sdim/// \c B::next 1050239313SdimAST_MATCHER_P(TemplateArgument, refersToDeclaration, 1051243830Sdim internal::Matcher<Decl>, InnerMatcher) { 1052243830Sdim if (Node.getKind() == TemplateArgument::Declaration) 1053243830Sdim return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder); 1054239313Sdim return false; 1055239313Sdim} 1056239313Sdim 1057341825Sdim/// Matches a sugar TemplateArgument that refers to a certain expression. 1058276479Sdim/// 1059276479Sdim/// Given 1060276479Sdim/// \code 1061341825Sdim/// struct B { int next; }; 1062341825Sdim/// template<int(B::*next_ptr)> struct A {}; 1063276479Sdim/// A<&B::next> a; 1064276479Sdim/// \endcode 1065276479Sdim/// templateSpecializationType(hasAnyTemplateArgument( 1066276479Sdim/// isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))) 1067276479Sdim/// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching 1068276479Sdim/// \c B::next 1069276479SdimAST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher<Expr>, InnerMatcher) { 1070276479Sdim if (Node.getKind() == TemplateArgument::Expression) 1071276479Sdim return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder); 1072276479Sdim return false; 1073276479Sdim} 1074276479Sdim 1075341825Sdim/// Matches a TemplateArgument that is an integral value. 1076280031Sdim/// 1077280031Sdim/// Given 1078280031Sdim/// \code 1079341825Sdim/// template<int T> struct C {}; 1080280031Sdim/// C<42> c; 1081280031Sdim/// \endcode 1082280031Sdim/// classTemplateSpecializationDecl( 1083280031Sdim/// hasAnyTemplateArgument(isIntegral())) 1084280031Sdim/// matches the implicit instantiation of C in C<42> 1085280031Sdim/// with isIntegral() matching 42. 1086280031SdimAST_MATCHER(TemplateArgument, isIntegral) { 1087280031Sdim return Node.getKind() == TemplateArgument::Integral; 1088280031Sdim} 1089280031Sdim 1090341825Sdim/// Matches a TemplateArgument that referes to an integral type. 1091280031Sdim/// 1092280031Sdim/// Given 1093280031Sdim/// \code 1094341825Sdim/// template<int T> struct C {}; 1095280031Sdim/// C<42> c; 1096280031Sdim/// \endcode 1097280031Sdim/// classTemplateSpecializationDecl( 1098280031Sdim/// hasAnyTemplateArgument(refersToIntegralType(asString("int")))) 1099280031Sdim/// matches the implicit instantiation of C in C<42>. 1100280031SdimAST_MATCHER_P(TemplateArgument, refersToIntegralType, 1101280031Sdim internal::Matcher<QualType>, InnerMatcher) { 1102280031Sdim if (Node.getKind() != TemplateArgument::Integral) 1103280031Sdim return false; 1104280031Sdim return InnerMatcher.matches(Node.getIntegralType(), Finder, Builder); 1105280031Sdim} 1106280031Sdim 1107341825Sdim/// Matches a TemplateArgument of integral type with a given value. 1108280031Sdim/// 1109280031Sdim/// Note that 'Value' is a string as the template argument's value is 1110280031Sdim/// an arbitrary precision integer. 'Value' must be euqal to the canonical 1111280031Sdim/// representation of that integral value in base 10. 1112280031Sdim/// 1113280031Sdim/// Given 1114280031Sdim/// \code 1115341825Sdim/// template<int T> struct C {}; 1116280031Sdim/// C<42> c; 1117280031Sdim/// \endcode 1118280031Sdim/// classTemplateSpecializationDecl( 1119280031Sdim/// hasAnyTemplateArgument(equalsIntegralValue("42"))) 1120280031Sdim/// matches the implicit instantiation of C in C<42>. 1121280031SdimAST_MATCHER_P(TemplateArgument, equalsIntegralValue, 1122280031Sdim std::string, Value) { 1123280031Sdim if (Node.getKind() != TemplateArgument::Integral) 1124280031Sdim return false; 1125280031Sdim return Node.getAsIntegral().toString(10) == Value; 1126280031Sdim} 1127280031Sdim 1128341825Sdim/// Matches an Objective-C autorelease pool statement. 1129280031Sdim/// 1130341825Sdim/// Given 1131341825Sdim/// \code 1132341825Sdim/// @autoreleasepool { 1133341825Sdim/// int x = 0; 1134341825Sdim/// } 1135341825Sdim/// \endcode 1136341825Sdim/// autoreleasePoolStmt(stmt()) matches the declaration of "x" 1137341825Sdim/// inside the autorelease pool. 1138341825Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 1139341825Sdim ObjCAutoreleasePoolStmt> autoreleasePoolStmt; 1140341825Sdim 1141341825Sdim/// Matches any value declaration. 1142341825Sdim/// 1143280031Sdim/// Example matches A, B, C and F 1144280031Sdim/// \code 1145280031Sdim/// enum X { A, B, C }; 1146280031Sdim/// void F(); 1147280031Sdim/// \endcode 1148327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl; 1149280031Sdim 1150341825Sdim/// Matches C++ constructor declarations. 1151239313Sdim/// 1152239313Sdim/// Example matches Foo::Foo() and Foo::Foo(int) 1153243830Sdim/// \code 1154239313Sdim/// class Foo { 1155239313Sdim/// public: 1156239313Sdim/// Foo(); 1157239313Sdim/// Foo(int); 1158239313Sdim/// int DoSomething(); 1159239313Sdim/// }; 1160243830Sdim/// \endcode 1161327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, CXXConstructorDecl> 1162327952Sdim cxxConstructorDecl; 1163239313Sdim 1164341825Sdim/// Matches explicit C++ destructor declarations. 1165239313Sdim/// 1166239313Sdim/// Example matches Foo::~Foo() 1167243830Sdim/// \code 1168239313Sdim/// class Foo { 1169239313Sdim/// public: 1170239313Sdim/// virtual ~Foo(); 1171239313Sdim/// }; 1172243830Sdim/// \endcode 1173327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, CXXDestructorDecl> 1174327952Sdim cxxDestructorDecl; 1175239313Sdim 1176341825Sdim/// Matches enum declarations. 1177239313Sdim/// 1178239313Sdim/// Example matches X 1179243830Sdim/// \code 1180239313Sdim/// enum X { 1181239313Sdim/// A, B, C 1182239313Sdim/// }; 1183243830Sdim/// \endcode 1184327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl; 1185239313Sdim 1186341825Sdim/// Matches enum constants. 1187239313Sdim/// 1188239313Sdim/// Example matches A, B, C 1189243830Sdim/// \code 1190239313Sdim/// enum X { 1191239313Sdim/// A, B, C 1192239313Sdim/// }; 1193243830Sdim/// \endcode 1194327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, EnumConstantDecl> 1195327952Sdim enumConstantDecl; 1196239313Sdim 1197341825Sdim/// Matches method declarations. 1198239313Sdim/// 1199239313Sdim/// Example matches y 1200243830Sdim/// \code 1201276479Sdim/// class X { void y(); }; 1202243830Sdim/// \endcode 1203327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> 1204327952Sdim cxxMethodDecl; 1205239313Sdim 1206341825Sdim/// Matches conversion operator declarations. 1207288943Sdim/// 1208288943Sdim/// Example matches the operator. 1209288943Sdim/// \code 1210288943Sdim/// class X { operator int() const; }; 1211288943Sdim/// \endcode 1212327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, CXXConversionDecl> 1213296417Sdim cxxConversionDecl; 1214288943Sdim 1215353358Sdim/// Matches user-defined and implicitly generated deduction guide. 1216353358Sdim/// 1217353358Sdim/// Example matches the deduction guide. 1218353358Sdim/// \code 1219353358Sdim/// template<typename T> 1220353358Sdim/// class X { X(int) }; 1221353358Sdim/// X(int) -> X<int>; 1222353358Sdim/// \endcode 1223353358Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl> 1224353358Sdim cxxDeductionGuideDecl; 1225353358Sdim 1226341825Sdim/// Matches variable declarations. 1227239313Sdim/// 1228239313Sdim/// Note: this does not match declarations of member variables, which are 1229239313Sdim/// "field" declarations in Clang parlance. 1230239313Sdim/// 1231239313Sdim/// Example matches a 1232243830Sdim/// \code 1233239313Sdim/// int a; 1234243830Sdim/// \endcode 1235327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl; 1236239313Sdim 1237341825Sdim/// Matches field declarations. 1238239313Sdim/// 1239239313Sdim/// Given 1240243830Sdim/// \code 1241239313Sdim/// class X { int m; }; 1242243830Sdim/// \endcode 1243243830Sdim/// fieldDecl() 1244239313Sdim/// matches 'm'. 1245327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl; 1246239313Sdim 1247344779Sdim/// Matches indirect field declarations. 1248344779Sdim/// 1249344779Sdim/// Given 1250344779Sdim/// \code 1251344779Sdim/// struct X { struct { int a; }; }; 1252344779Sdim/// \endcode 1253344779Sdim/// indirectFieldDecl() 1254344779Sdim/// matches 'a'. 1255344779Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, IndirectFieldDecl> 1256344779Sdim indirectFieldDecl; 1257344779Sdim 1258341825Sdim/// Matches function declarations. 1259239313Sdim/// 1260239313Sdim/// Example matches f 1261243830Sdim/// \code 1262239313Sdim/// void f(); 1263243830Sdim/// \endcode 1264327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> 1265327952Sdim functionDecl; 1266239313Sdim 1267341825Sdim/// Matches C++ function template declarations. 1268243830Sdim/// 1269243830Sdim/// Example matches f 1270243830Sdim/// \code 1271243830Sdim/// template<class T> void f(T t) {} 1272243830Sdim/// \endcode 1273327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, FunctionTemplateDecl> 1274327952Sdim functionTemplateDecl; 1275239313Sdim 1276341825Sdim/// Matches friend declarations. 1277261991Sdim/// 1278261991Sdim/// Given 1279261991Sdim/// \code 1280261991Sdim/// class X { friend void foo(); }; 1281261991Sdim/// \endcode 1282261991Sdim/// friendDecl() 1283261991Sdim/// matches 'friend void foo()'. 1284327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl; 1285261991Sdim 1286341825Sdim/// Matches statements. 1287239313Sdim/// 1288239313Sdim/// Given 1289243830Sdim/// \code 1290239313Sdim/// { ++a; } 1291243830Sdim/// \endcode 1292243830Sdim/// stmt() 1293239313Sdim/// matches both the compound statement '{ ++a; }' and '++a'. 1294327952Sdimextern const internal::VariadicAllOfMatcher<Stmt> stmt; 1295239313Sdim 1296341825Sdim/// Matches declaration statements. 1297239313Sdim/// 1298239313Sdim/// Given 1299243830Sdim/// \code 1300239313Sdim/// int a; 1301243830Sdim/// \endcode 1302243830Sdim/// declStmt() 1303239313Sdim/// matches 'int a'. 1304327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclStmt> declStmt; 1305239313Sdim 1306341825Sdim/// Matches member expressions. 1307239313Sdim/// 1308239313Sdim/// Given 1309243830Sdim/// \code 1310239313Sdim/// class Y { 1311239313Sdim/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 1312239313Sdim/// int a; static int b; 1313239313Sdim/// }; 1314243830Sdim/// \endcode 1315243830Sdim/// memberExpr() 1316239313Sdim/// matches this->x, x, y.x, a, this->b 1317327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr; 1318239313Sdim 1319344779Sdim/// Matches unresolved member expressions. 1320344779Sdim/// 1321344779Sdim/// Given 1322344779Sdim/// \code 1323344779Sdim/// struct X { 1324344779Sdim/// template <class T> void f(); 1325344779Sdim/// void g(); 1326344779Sdim/// }; 1327344779Sdim/// template <class T> void h() { X x; x.f<T>(); x.g(); } 1328344779Sdim/// \endcode 1329344779Sdim/// unresolvedMemberExpr() 1330344779Sdim/// matches x.f<T> 1331344779Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedMemberExpr> 1332344779Sdim unresolvedMemberExpr; 1333344779Sdim 1334344779Sdim/// Matches member expressions where the actual member referenced could not be 1335344779Sdim/// resolved because the base expression or the member name was dependent. 1336344779Sdim/// 1337344779Sdim/// Given 1338344779Sdim/// \code 1339344779Sdim/// template <class T> void f() { T t; t.g(); } 1340344779Sdim/// \endcode 1341344779Sdim/// cxxDependentScopeMemberExpr() 1342344779Sdim/// matches t.g 1343344779Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 1344344779Sdim CXXDependentScopeMemberExpr> 1345344779Sdim cxxDependentScopeMemberExpr; 1346344779Sdim 1347341825Sdim/// Matches call expressions. 1348239313Sdim/// 1349239313Sdim/// Example matches x.y() and y() 1350243830Sdim/// \code 1351239313Sdim/// X x; 1352239313Sdim/// x.y(); 1353239313Sdim/// y(); 1354243830Sdim/// \endcode 1355327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr; 1356239313Sdim 1357344779Sdim/// Matches call expressions which were resolved using ADL. 1358344779Sdim/// 1359344779Sdim/// Example matches y(x) but not y(42) or NS::y(x). 1360344779Sdim/// \code 1361344779Sdim/// namespace NS { 1362344779Sdim/// struct X {}; 1363344779Sdim/// void y(X); 1364344779Sdim/// } 1365344779Sdim/// 1366344779Sdim/// void y(...); 1367344779Sdim/// 1368344779Sdim/// void test() { 1369344779Sdim/// NS::X x; 1370344779Sdim/// y(x); // Matches 1371344779Sdim/// NS::y(x); // Doesn't match 1372344779Sdim/// y(42); // Doesn't match 1373344779Sdim/// using NS::y; 1374344779Sdim/// y(x); // Found by both unqualified lookup and ADL, doesn't match 1375344779Sdim// } 1376344779Sdim/// \endcode 1377344779SdimAST_MATCHER(CallExpr, usesADL) { return Node.usesADL(); } 1378344779Sdim 1379341825Sdim/// Matches lambda expressions. 1380243830Sdim/// 1381243830Sdim/// Example matches [&](){return 5;} 1382243830Sdim/// \code 1383243830Sdim/// [&](){return 5;} 1384243830Sdim/// \endcode 1385327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr; 1386243830Sdim 1387341825Sdim/// Matches member call expressions. 1388239313Sdim/// 1389239313Sdim/// Example matches x.y() 1390243830Sdim/// \code 1391239313Sdim/// X x; 1392239313Sdim/// x.y(); 1393243830Sdim/// \endcode 1394327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXMemberCallExpr> 1395327952Sdim cxxMemberCallExpr; 1396239313Sdim 1397341825Sdim/// Matches ObjectiveC Message invocation expressions. 1398288943Sdim/// 1399288943Sdim/// The innermost message send invokes the "alloc" class method on the 1400288943Sdim/// NSString class, while the outermost message send invokes the 1401288943Sdim/// "initWithString" instance method on the object returned from 1402288943Sdim/// NSString's "alloc". This matcher should match both message sends. 1403288943Sdim/// \code 1404288943Sdim/// [[NSString alloc] initWithString:@"Hello"] 1405288943Sdim/// \endcode 1406327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCMessageExpr> 1407327952Sdim objcMessageExpr; 1408288943Sdim 1409341825Sdim/// Matches Objective-C interface declarations. 1410296417Sdim/// 1411296417Sdim/// Example matches Foo 1412296417Sdim/// \code 1413296417Sdim/// @interface Foo 1414296417Sdim/// @end 1415296417Sdim/// \endcode 1416327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCInterfaceDecl> 1417327952Sdim objcInterfaceDecl; 1418288943Sdim 1419341825Sdim/// Matches Objective-C implementation declarations. 1420327952Sdim/// 1421327952Sdim/// Example matches Foo 1422327952Sdim/// \code 1423327952Sdim/// @implementation Foo 1424327952Sdim/// @end 1425327952Sdim/// \endcode 1426327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCImplementationDecl> 1427327952Sdim objcImplementationDecl; 1428327952Sdim 1429341825Sdim/// Matches Objective-C protocol declarations. 1430321369Sdim/// 1431321369Sdim/// Example matches FooDelegate 1432321369Sdim/// \code 1433321369Sdim/// @protocol FooDelegate 1434321369Sdim/// @end 1435321369Sdim/// \endcode 1436327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCProtocolDecl> 1437327952Sdim objcProtocolDecl; 1438321369Sdim 1439341825Sdim/// Matches Objective-C category declarations. 1440321369Sdim/// 1441321369Sdim/// Example matches Foo (Additions) 1442321369Sdim/// \code 1443321369Sdim/// @interface Foo (Additions) 1444321369Sdim/// @end 1445321369Sdim/// \endcode 1446327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryDecl> 1447327952Sdim objcCategoryDecl; 1448321369Sdim 1449341825Sdim/// Matches Objective-C category definitions. 1450327952Sdim/// 1451327952Sdim/// Example matches Foo (Additions) 1452327952Sdim/// \code 1453327952Sdim/// @implementation Foo (Additions) 1454327952Sdim/// @end 1455327952Sdim/// \endcode 1456327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl> 1457327952Sdim objcCategoryImplDecl; 1458327952Sdim 1459341825Sdim/// Matches Objective-C method declarations. 1460321369Sdim/// 1461321369Sdim/// Example matches both declaration and definition of -[Foo method] 1462321369Sdim/// \code 1463321369Sdim/// @interface Foo 1464321369Sdim/// - (void)method; 1465321369Sdim/// @end 1466321369Sdim/// 1467321369Sdim/// @implementation Foo 1468321369Sdim/// - (void)method {} 1469321369Sdim/// @end 1470321369Sdim/// \endcode 1471327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl> 1472327952Sdim objcMethodDecl; 1473321369Sdim 1474341825Sdim/// Matches block declarations. 1475321369Sdim/// 1476341825Sdim/// Example matches the declaration of the nameless block printing an input 1477341825Sdim/// integer. 1478341825Sdim/// 1479341825Sdim/// \code 1480341825Sdim/// myFunc(^(int p) { 1481341825Sdim/// printf("%d", p); 1482341825Sdim/// }) 1483341825Sdim/// \endcode 1484341825Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl> 1485341825Sdim blockDecl; 1486341825Sdim 1487341825Sdim/// Matches Objective-C instance variable declarations. 1488341825Sdim/// 1489321369Sdim/// Example matches _enabled 1490321369Sdim/// \code 1491321369Sdim/// @implementation Foo { 1492321369Sdim/// BOOL _enabled; 1493321369Sdim/// } 1494321369Sdim/// @end 1495321369Sdim/// \endcode 1496327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCIvarDecl> 1497327952Sdim objcIvarDecl; 1498321369Sdim 1499341825Sdim/// Matches Objective-C property declarations. 1500321369Sdim/// 1501321369Sdim/// Example matches enabled 1502321369Sdim/// \code 1503321369Sdim/// @interface Foo 1504321369Sdim/// @property BOOL enabled; 1505321369Sdim/// @end 1506321369Sdim/// \endcode 1507327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCPropertyDecl> 1508327952Sdim objcPropertyDecl; 1509321369Sdim 1510341825Sdim/// Matches Objective-C \@throw statements. 1511327952Sdim/// 1512327952Sdim/// Example matches \@throw 1513327952Sdim/// \code 1514327952Sdim/// @throw obj; 1515327952Sdim/// \endcode 1516327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtThrowStmt> 1517327952Sdim objcThrowStmt; 1518327952Sdim 1519341825Sdim/// Matches Objective-C @try statements. 1520327952Sdim/// 1521327952Sdim/// Example matches @try 1522327952Sdim/// \code 1523327952Sdim/// @try {} 1524327952Sdim/// @catch (...) {} 1525327952Sdim/// \endcode 1526327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtTryStmt> 1527327952Sdim objcTryStmt; 1528327952Sdim 1529341825Sdim/// Matches Objective-C @catch statements. 1530327952Sdim/// 1531327952Sdim/// Example matches @catch 1532327952Sdim/// \code 1533327952Sdim/// @try {} 1534327952Sdim/// @catch (...) {} 1535327952Sdim/// \endcode 1536327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtCatchStmt> 1537327952Sdim objcCatchStmt; 1538327952Sdim 1539341825Sdim/// Matches Objective-C @finally statements. 1540327952Sdim/// 1541327952Sdim/// Example matches @finally 1542327952Sdim/// \code 1543327952Sdim/// @try {} 1544327952Sdim/// @finally {} 1545327952Sdim/// \endcode 1546327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtFinallyStmt> 1547327952Sdim objcFinallyStmt; 1548327952Sdim 1549341825Sdim/// Matches expressions that introduce cleanups to be run at the end 1550276479Sdim/// of the sub-expression's evaluation. 1551276479Sdim/// 1552276479Sdim/// Example matches std::string() 1553276479Sdim/// \code 1554276479Sdim/// const std::string str = std::string(); 1555276479Sdim/// \endcode 1556327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ExprWithCleanups> 1557327952Sdim exprWithCleanups; 1558276479Sdim 1559341825Sdim/// Matches init list expressions. 1560239313Sdim/// 1561239313Sdim/// Given 1562243830Sdim/// \code 1563239313Sdim/// int a[] = { 1, 2 }; 1564239313Sdim/// struct B { int x, y; }; 1565239313Sdim/// B b = { 5, 6 }; 1566243830Sdim/// \endcode 1567276479Sdim/// initListExpr() 1568239313Sdim/// matches "{ 1, 2 }" and "{ 5, 6 }" 1569327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> 1570327952Sdim initListExpr; 1571239313Sdim 1572341825Sdim/// Matches the syntactic form of init list expressions 1573309124Sdim/// (if expression have it). 1574309124SdimAST_MATCHER_P(InitListExpr, hasSyntacticForm, 1575309124Sdim internal::Matcher<Expr>, InnerMatcher) { 1576309124Sdim const Expr *SyntForm = Node.getSyntacticForm(); 1577309124Sdim return (SyntForm != nullptr && 1578309124Sdim InnerMatcher.matches(*SyntForm, Finder, Builder)); 1579309124Sdim} 1580309124Sdim 1581341825Sdim/// Matches C++ initializer list expressions. 1582321369Sdim/// 1583321369Sdim/// Given 1584321369Sdim/// \code 1585321369Sdim/// std::vector<int> a({ 1, 2, 3 }); 1586321369Sdim/// std::vector<int> b = { 4, 5 }; 1587321369Sdim/// int c[] = { 6, 7 }; 1588321369Sdim/// std::pair<int, int> d = { 8, 9 }; 1589321369Sdim/// \endcode 1590321369Sdim/// cxxStdInitializerListExpr() 1591321369Sdim/// matches "{ 1, 2, 3 }" and "{ 4, 5 }" 1592327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 1593327952Sdim CXXStdInitializerListExpr> 1594327952Sdim cxxStdInitializerListExpr; 1595321369Sdim 1596341825Sdim/// Matches implicit initializers of init list expressions. 1597309124Sdim/// 1598309124Sdim/// Given 1599309124Sdim/// \code 1600309124Sdim/// point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 1601309124Sdim/// \endcode 1602309124Sdim/// implicitValueInitExpr() 1603309124Sdim/// matches "[0].y" (implicitly) 1604327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitValueInitExpr> 1605327952Sdim implicitValueInitExpr; 1606309124Sdim 1607341825Sdim/// Matches paren list expressions. 1608309124Sdim/// ParenListExprs don't have a predefined type and are used for late parsing. 1609309124Sdim/// In the final AST, they can be met in template declarations. 1610309124Sdim/// 1611309124Sdim/// Given 1612309124Sdim/// \code 1613309124Sdim/// template<typename T> class X { 1614309124Sdim/// void f() { 1615309124Sdim/// X x(*this); 1616309124Sdim/// int a = 0, b = 1; int i = (a, b); 1617309124Sdim/// } 1618309124Sdim/// }; 1619309124Sdim/// \endcode 1620309124Sdim/// parenListExpr() matches "*this" but NOT matches (a, b) because (a, b) 1621309124Sdim/// has a predefined type and is a ParenExpr, not a ParenListExpr. 1622327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ParenListExpr> 1623327952Sdim parenListExpr; 1624309124Sdim 1625341825Sdim/// Matches substitutions of non-type template parameters. 1626276479Sdim/// 1627276479Sdim/// Given 1628276479Sdim/// \code 1629276479Sdim/// template <int N> 1630276479Sdim/// struct A { static const int n = N; }; 1631276479Sdim/// struct B : public A<42> {}; 1632276479Sdim/// \endcode 1633276479Sdim/// substNonTypeTemplateParmExpr() 1634276479Sdim/// matches "N" in the right-hand side of "static const int n = N;" 1635327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 1636327952Sdim SubstNonTypeTemplateParmExpr> 1637327952Sdim substNonTypeTemplateParmExpr; 1638276479Sdim 1639341825Sdim/// Matches using declarations. 1640239313Sdim/// 1641239313Sdim/// Given 1642243830Sdim/// \code 1643239313Sdim/// namespace X { int x; } 1644239313Sdim/// using X::x; 1645243830Sdim/// \endcode 1646239313Sdim/// usingDecl() 1647239313Sdim/// matches \code using X::x \endcode 1648327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl; 1649239313Sdim 1650341825Sdim/// Matches using namespace declarations. 1651276479Sdim/// 1652276479Sdim/// Given 1653276479Sdim/// \code 1654276479Sdim/// namespace X { int x; } 1655276479Sdim/// using namespace X; 1656276479Sdim/// \endcode 1657276479Sdim/// usingDirectiveDecl() 1658276479Sdim/// matches \code using namespace X \endcode 1659327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, UsingDirectiveDecl> 1660327952Sdim usingDirectiveDecl; 1661276479Sdim 1662341825Sdim/// Matches reference to a name that can be looked up during parsing 1663309124Sdim/// but could not be resolved to a specific declaration. 1664309124Sdim/// 1665309124Sdim/// Given 1666309124Sdim/// \code 1667309124Sdim/// template<typename T> 1668309124Sdim/// T foo() { T a; return a; } 1669309124Sdim/// template<typename T> 1670309124Sdim/// void bar() { 1671309124Sdim/// foo<T>(); 1672309124Sdim/// } 1673309124Sdim/// \endcode 1674309124Sdim/// unresolvedLookupExpr() 1675309124Sdim/// matches \code foo<T>() \endcode 1676327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedLookupExpr> 1677327952Sdim unresolvedLookupExpr; 1678309124Sdim 1679341825Sdim/// Matches unresolved using value declarations. 1680261991Sdim/// 1681261991Sdim/// Given 1682261991Sdim/// \code 1683261991Sdim/// template<typename X> 1684261991Sdim/// class C : private X { 1685261991Sdim/// using X::x; 1686261991Sdim/// }; 1687261991Sdim/// \endcode 1688261991Sdim/// unresolvedUsingValueDecl() 1689261991Sdim/// matches \code using X::x \endcode 1690327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, 1691327952Sdim UnresolvedUsingValueDecl> 1692327952Sdim unresolvedUsingValueDecl; 1693261991Sdim 1694341825Sdim/// Matches unresolved using value declarations that involve the 1695296417Sdim/// typename. 1696296417Sdim/// 1697296417Sdim/// Given 1698296417Sdim/// \code 1699296417Sdim/// template <typename T> 1700296417Sdim/// struct Base { typedef T Foo; }; 1701296417Sdim/// 1702296417Sdim/// template<typename T> 1703296417Sdim/// struct S : private Base<T> { 1704296417Sdim/// using typename Base<T>::Foo; 1705296417Sdim/// }; 1706296417Sdim/// \endcode 1707296417Sdim/// unresolvedUsingTypenameDecl() 1708296417Sdim/// matches \code using Base<T>::Foo \endcode 1709327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, 1710327952Sdim UnresolvedUsingTypenameDecl> 1711327952Sdim unresolvedUsingTypenameDecl; 1712296417Sdim 1713344779Sdim/// Matches a constant expression wrapper. 1714344779Sdim/// 1715344779Sdim/// Example matches the constant in the case statement: 1716344779Sdim/// (matcher = constantExpr()) 1717344779Sdim/// \code 1718344779Sdim/// switch (a) { 1719344779Sdim/// case 37: break; 1720344779Sdim/// } 1721344779Sdim/// \endcode 1722344779Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ConstantExpr> 1723344779Sdim constantExpr; 1724344779Sdim 1725341825Sdim/// Matches parentheses used in expressions. 1726309124Sdim/// 1727309124Sdim/// Example matches (foo() + 1) 1728309124Sdim/// \code 1729309124Sdim/// int foo() { return 1; } 1730309124Sdim/// int a = (foo() + 1); 1731309124Sdim/// \endcode 1732327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ParenExpr> parenExpr; 1733309124Sdim 1734341825Sdim/// Matches constructor call expressions (including implicit ones). 1735239313Sdim/// 1736239313Sdim/// Example matches string(ptr, n) and ptr within arguments of f 1737296417Sdim/// (matcher = cxxConstructExpr()) 1738243830Sdim/// \code 1739239313Sdim/// void f(const string &a, const string &b); 1740239313Sdim/// char *ptr; 1741239313Sdim/// int n; 1742239313Sdim/// f(string(ptr, n), ptr); 1743243830Sdim/// \endcode 1744327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstructExpr> 1745327952Sdim cxxConstructExpr; 1746239313Sdim 1747341825Sdim/// Matches unresolved constructor call expressions. 1748261991Sdim/// 1749261991Sdim/// Example matches T(t) in return statement of f 1750296417Sdim/// (matcher = cxxUnresolvedConstructExpr()) 1751261991Sdim/// \code 1752261991Sdim/// template <typename T> 1753261991Sdim/// void f(const T& t) { return T(t); } 1754261991Sdim/// \endcode 1755327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 1756327952Sdim CXXUnresolvedConstructExpr> 1757327952Sdim cxxUnresolvedConstructExpr; 1758261991Sdim 1759341825Sdim/// Matches implicit and explicit this expressions. 1760243830Sdim/// 1761243830Sdim/// Example matches the implicit this expression in "return i". 1762296417Sdim/// (matcher = cxxThisExpr()) 1763243830Sdim/// \code 1764243830Sdim/// struct foo { 1765243830Sdim/// int i; 1766243830Sdim/// int f() { return i; } 1767243830Sdim/// }; 1768243830Sdim/// \endcode 1769327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> 1770327952Sdim cxxThisExpr; 1771243830Sdim 1772341825Sdim/// Matches nodes where temporaries are created. 1773239313Sdim/// 1774239313Sdim/// Example matches FunctionTakesString(GetStringByValue()) 1775296417Sdim/// (matcher = cxxBindTemporaryExpr()) 1776243830Sdim/// \code 1777239313Sdim/// FunctionTakesString(GetStringByValue()); 1778239313Sdim/// FunctionTakesStringByPointer(GetStringPointer()); 1779243830Sdim/// \endcode 1780327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBindTemporaryExpr> 1781327952Sdim cxxBindTemporaryExpr; 1782239313Sdim 1783341825Sdim/// Matches nodes where temporaries are materialized. 1784243830Sdim/// 1785243830Sdim/// Example: Given 1786243830Sdim/// \code 1787321369Sdim/// struct T {void func();}; 1788243830Sdim/// T f(); 1789243830Sdim/// void g(T); 1790243830Sdim/// \endcode 1791243830Sdim/// materializeTemporaryExpr() matches 'f()' in these statements 1792243830Sdim/// \code 1793243830Sdim/// T u(f()); 1794243830Sdim/// g(f()); 1795341825Sdim/// f().func(); 1796243830Sdim/// \endcode 1797243830Sdim/// but does not match 1798243830Sdim/// \code 1799243830Sdim/// f(); 1800243830Sdim/// \endcode 1801327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 1802327952Sdim MaterializeTemporaryExpr> 1803327952Sdim materializeTemporaryExpr; 1804243830Sdim 1805341825Sdim/// Matches new expressions. 1806239313Sdim/// 1807239313Sdim/// Given 1808243830Sdim/// \code 1809239313Sdim/// new X; 1810243830Sdim/// \endcode 1811296417Sdim/// cxxNewExpr() 1812239313Sdim/// matches 'new X'. 1813327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> cxxNewExpr; 1814239313Sdim 1815341825Sdim/// Matches delete expressions. 1816239313Sdim/// 1817239313Sdim/// Given 1818243830Sdim/// \code 1819239313Sdim/// delete X; 1820243830Sdim/// \endcode 1821296417Sdim/// cxxDeleteExpr() 1822239313Sdim/// matches 'delete X'. 1823327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> 1824327952Sdim cxxDeleteExpr; 1825239313Sdim 1826341825Sdim/// Matches array subscript expressions. 1827239313Sdim/// 1828239313Sdim/// Given 1829243830Sdim/// \code 1830239313Sdim/// int i = a[1]; 1831243830Sdim/// \endcode 1832239313Sdim/// arraySubscriptExpr() 1833239313Sdim/// matches "a[1]" 1834327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ArraySubscriptExpr> 1835327952Sdim arraySubscriptExpr; 1836239313Sdim 1837341825Sdim/// Matches the value of a default argument at the call site. 1838239313Sdim/// 1839239313Sdim/// Example matches the CXXDefaultArgExpr placeholder inserted for the 1840239313Sdim/// default value of the second parameter in the call expression f(42) 1841296417Sdim/// (matcher = cxxDefaultArgExpr()) 1842243830Sdim/// \code 1843239313Sdim/// void f(int x, int y = 0); 1844239313Sdim/// f(42); 1845243830Sdim/// \endcode 1846327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDefaultArgExpr> 1847327952Sdim cxxDefaultArgExpr; 1848239313Sdim 1849341825Sdim/// Matches overloaded operator calls. 1850239313Sdim/// 1851239313Sdim/// Note that if an operator isn't overloaded, it won't match. Instead, use 1852239313Sdim/// binaryOperator matcher. 1853239313Sdim/// Currently it does not match operators such as new delete. 1854239313Sdim/// FIXME: figure out why these do not match? 1855239313Sdim/// 1856239313Sdim/// Example matches both operator<<((o << b), c) and operator<<(o, b) 1857296417Sdim/// (matcher = cxxOperatorCallExpr()) 1858243830Sdim/// \code 1859239313Sdim/// ostream &operator<< (ostream &out, int i) { }; 1860239313Sdim/// ostream &o; int b = 1, c = 1; 1861239313Sdim/// o << b << c; 1862243830Sdim/// \endcode 1863327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXOperatorCallExpr> 1864327952Sdim cxxOperatorCallExpr; 1865239313Sdim 1866341825Sdim/// Matches expressions. 1867239313Sdim/// 1868239313Sdim/// Example matches x() 1869243830Sdim/// \code 1870239313Sdim/// void f() { x(); } 1871243830Sdim/// \endcode 1872327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr; 1873239313Sdim 1874341825Sdim/// Matches expressions that refer to declarations. 1875239313Sdim/// 1876239313Sdim/// Example matches x in if (x) 1877243830Sdim/// \code 1878239313Sdim/// bool x; 1879239313Sdim/// if (x) {} 1880243830Sdim/// \endcode 1881327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> 1882327952Sdim declRefExpr; 1883239313Sdim 1884341825Sdim/// Matches a reference to an ObjCIvar. 1885239313Sdim/// 1886341825Sdim/// Example: matches "a" in "init" method: 1887341825Sdim/// \code 1888341825Sdim/// @implementation A { 1889341825Sdim/// NSString *a; 1890341825Sdim/// } 1891341825Sdim/// - (void) init { 1892341825Sdim/// a = @"hello"; 1893341825Sdim/// } 1894341825Sdim/// \endcode 1895341825Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr> 1896341825Sdim objcIvarRefExpr; 1897341825Sdim 1898344779Sdim/// Matches a reference to a block. 1899344779Sdim/// 1900344779Sdim/// Example: matches "^{}": 1901344779Sdim/// \code 1902344779Sdim/// void f() { ^{}(); } 1903344779Sdim/// \endcode 1904344779Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr; 1905344779Sdim 1906341825Sdim/// Matches if statements. 1907341825Sdim/// 1908239313Sdim/// Example matches 'if (x) {}' 1909243830Sdim/// \code 1910239313Sdim/// if (x) {} 1911243830Sdim/// \endcode 1912327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt; 1913239313Sdim 1914341825Sdim/// Matches for statements. 1915239313Sdim/// 1916239313Sdim/// Example matches 'for (;;) {}' 1917243830Sdim/// \code 1918239313Sdim/// for (;;) {} 1919243830Sdim/// int i[] = {1, 2, 3}; for (auto a : i); 1920243830Sdim/// \endcode 1921327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt; 1922239313Sdim 1923341825Sdim/// Matches the increment statement of a for loop. 1924239313Sdim/// 1925239313Sdim/// Example: 1926239313Sdim/// forStmt(hasIncrement(unaryOperator(hasOperatorName("++")))) 1927239313Sdim/// matches '++x' in 1928243830Sdim/// \code 1929239313Sdim/// for (x; x < N; ++x) { } 1930243830Sdim/// \endcode 1931239313SdimAST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>, 1932239313Sdim InnerMatcher) { 1933239313Sdim const Stmt *const Increment = Node.getInc(); 1934276479Sdim return (Increment != nullptr && 1935239313Sdim InnerMatcher.matches(*Increment, Finder, Builder)); 1936239313Sdim} 1937239313Sdim 1938341825Sdim/// Matches the initialization statement of a for loop. 1939239313Sdim/// 1940239313Sdim/// Example: 1941243830Sdim/// forStmt(hasLoopInit(declStmt())) 1942239313Sdim/// matches 'int x = 0' in 1943243830Sdim/// \code 1944239313Sdim/// for (int x = 0; x < N; ++x) { } 1945243830Sdim/// \endcode 1946239313SdimAST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>, 1947239313Sdim InnerMatcher) { 1948239313Sdim const Stmt *const Init = Node.getInit(); 1949276479Sdim return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder)); 1950239313Sdim} 1951239313Sdim 1952341825Sdim/// Matches range-based for statements. 1953276479Sdim/// 1954296417Sdim/// cxxForRangeStmt() matches 'for (auto a : i)' 1955276479Sdim/// \code 1956276479Sdim/// int i[] = {1, 2, 3}; for (auto a : i); 1957276479Sdim/// for(int j = 0; j < 5; ++j); 1958276479Sdim/// \endcode 1959327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> 1960327952Sdim cxxForRangeStmt; 1961276479Sdim 1962341825Sdim/// Matches the initialization statement of a for loop. 1963276479Sdim/// 1964276479Sdim/// Example: 1965276479Sdim/// forStmt(hasLoopVariable(anything())) 1966276479Sdim/// matches 'int x' in 1967276479Sdim/// \code 1968276479Sdim/// for (int x : a) { } 1969276479Sdim/// \endcode 1970276479SdimAST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>, 1971276479Sdim InnerMatcher) { 1972276479Sdim const VarDecl *const Var = Node.getLoopVariable(); 1973276479Sdim return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder)); 1974276479Sdim} 1975276479Sdim 1976341825Sdim/// Matches the range initialization statement of a for loop. 1977276479Sdim/// 1978276479Sdim/// Example: 1979276479Sdim/// forStmt(hasRangeInit(anything())) 1980276479Sdim/// matches 'a' in 1981276479Sdim/// \code 1982276479Sdim/// for (int x : a) { } 1983276479Sdim/// \endcode 1984276479SdimAST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>, 1985276479Sdim InnerMatcher) { 1986276479Sdim const Expr *const Init = Node.getRangeInit(); 1987276479Sdim return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder)); 1988276479Sdim} 1989276479Sdim 1990341825Sdim/// Matches while statements. 1991239313Sdim/// 1992239313Sdim/// Given 1993243830Sdim/// \code 1994239313Sdim/// while (true) {} 1995243830Sdim/// \endcode 1996239313Sdim/// whileStmt() 1997239313Sdim/// matches 'while (true) {}'. 1998327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt; 1999239313Sdim 2000341825Sdim/// Matches do statements. 2001239313Sdim/// 2002239313Sdim/// Given 2003243830Sdim/// \code 2004239313Sdim/// do {} while (true); 2005243830Sdim/// \endcode 2006239313Sdim/// doStmt() 2007239313Sdim/// matches 'do {} while(true)' 2008327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt; 2009239313Sdim 2010341825Sdim/// Matches break statements. 2011243830Sdim/// 2012243830Sdim/// Given 2013243830Sdim/// \code 2014243830Sdim/// while (true) { break; } 2015243830Sdim/// \endcode 2016243830Sdim/// breakStmt() 2017243830Sdim/// matches 'break' 2018327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt; 2019243830Sdim 2020341825Sdim/// Matches continue statements. 2021243830Sdim/// 2022243830Sdim/// Given 2023243830Sdim/// \code 2024243830Sdim/// while (true) { continue; } 2025243830Sdim/// \endcode 2026243830Sdim/// continueStmt() 2027243830Sdim/// matches 'continue' 2028327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> 2029327952Sdim continueStmt; 2030243830Sdim 2031341825Sdim/// Matches return statements. 2032243830Sdim/// 2033243830Sdim/// Given 2034243830Sdim/// \code 2035243830Sdim/// return 1; 2036243830Sdim/// \endcode 2037243830Sdim/// returnStmt() 2038243830Sdim/// matches 'return 1' 2039327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt; 2040243830Sdim 2041341825Sdim/// Matches goto statements. 2042243830Sdim/// 2043243830Sdim/// Given 2044243830Sdim/// \code 2045243830Sdim/// goto FOO; 2046243830Sdim/// FOO: bar(); 2047243830Sdim/// \endcode 2048243830Sdim/// gotoStmt() 2049243830Sdim/// matches 'goto FOO' 2050327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt; 2051243830Sdim 2052341825Sdim/// Matches label statements. 2053243830Sdim/// 2054243830Sdim/// Given 2055243830Sdim/// \code 2056243830Sdim/// goto FOO; 2057243830Sdim/// FOO: bar(); 2058243830Sdim/// \endcode 2059243830Sdim/// labelStmt() 2060243830Sdim/// matches 'FOO:' 2061327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt; 2062243830Sdim 2063341825Sdim/// Matches address of label statements (GNU extension). 2064309124Sdim/// 2065309124Sdim/// Given 2066309124Sdim/// \code 2067309124Sdim/// FOO: bar(); 2068309124Sdim/// void *ptr = &&FOO; 2069309124Sdim/// goto *bar; 2070309124Sdim/// \endcode 2071309124Sdim/// addrLabelExpr() 2072309124Sdim/// matches '&&FOO' 2073327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, AddrLabelExpr> 2074327952Sdim addrLabelExpr; 2075309124Sdim 2076341825Sdim/// Matches switch statements. 2077243830Sdim/// 2078243830Sdim/// Given 2079243830Sdim/// \code 2080243830Sdim/// switch(a) { case 42: break; default: break; } 2081243830Sdim/// \endcode 2082243830Sdim/// switchStmt() 2083243830Sdim/// matches 'switch(a)'. 2084327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt; 2085243830Sdim 2086341825Sdim/// Matches case and default statements inside switch statements. 2087239313Sdim/// 2088239313Sdim/// Given 2089243830Sdim/// \code 2090239313Sdim/// switch(a) { case 42: break; default: break; } 2091243830Sdim/// \endcode 2092239313Sdim/// switchCase() 2093341825Sdim/// matches 'case 42:' and 'default:'. 2094327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase; 2095239313Sdim 2096341825Sdim/// Matches case statements inside switch statements. 2097261991Sdim/// 2098261991Sdim/// Given 2099261991Sdim/// \code 2100261991Sdim/// switch(a) { case 42: break; default: break; } 2101261991Sdim/// \endcode 2102261991Sdim/// caseStmt() 2103341825Sdim/// matches 'case 42:'. 2104327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt; 2105261991Sdim 2106341825Sdim/// Matches default statements inside switch statements. 2107261991Sdim/// 2108261991Sdim/// Given 2109261991Sdim/// \code 2110261991Sdim/// switch(a) { case 42: break; default: break; } 2111261991Sdim/// \endcode 2112261991Sdim/// defaultStmt() 2113341825Sdim/// matches 'default:'. 2114327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> 2115327952Sdim defaultStmt; 2116261991Sdim 2117341825Sdim/// Matches compound statements. 2118239313Sdim/// 2119341825Sdim/// Example matches '{}' and '{{}}' in 'for (;;) {{}}' 2120243830Sdim/// \code 2121239313Sdim/// for (;;) {{}} 2122243830Sdim/// \endcode 2123327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> 2124327952Sdim compoundStmt; 2125239313Sdim 2126341825Sdim/// Matches catch statements. 2127243830Sdim/// 2128243830Sdim/// \code 2129243830Sdim/// try {} catch(int i) {} 2130243830Sdim/// \endcode 2131296417Sdim/// cxxCatchStmt() 2132243830Sdim/// matches 'catch(int i)' 2133327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> 2134327952Sdim cxxCatchStmt; 2135243830Sdim 2136341825Sdim/// Matches try statements. 2137243830Sdim/// 2138243830Sdim/// \code 2139243830Sdim/// try {} catch(int i) {} 2140243830Sdim/// \endcode 2141296417Sdim/// cxxTryStmt() 2142243830Sdim/// matches 'try {}' 2143327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt; 2144243830Sdim 2145341825Sdim/// Matches throw expressions. 2146243830Sdim/// 2147243830Sdim/// \code 2148243830Sdim/// try { throw 5; } catch(int i) {} 2149243830Sdim/// \endcode 2150296417Sdim/// cxxThrowExpr() 2151243830Sdim/// matches 'throw 5' 2152327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> 2153327952Sdim cxxThrowExpr; 2154243830Sdim 2155341825Sdim/// Matches null statements. 2156243830Sdim/// 2157243830Sdim/// \code 2158243830Sdim/// foo();; 2159243830Sdim/// \endcode 2160243830Sdim/// nullStmt() 2161243830Sdim/// matches the second ';' 2162327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt; 2163243830Sdim 2164341825Sdim/// Matches asm statements. 2165243830Sdim/// 2166243830Sdim/// \code 2167243830Sdim/// int i = 100; 2168243830Sdim/// __asm("mov al, 2"); 2169243830Sdim/// \endcode 2170243830Sdim/// asmStmt() 2171243830Sdim/// matches '__asm("mov al, 2")' 2172327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt; 2173243830Sdim 2174341825Sdim/// Matches bool literals. 2175239313Sdim/// 2176239313Sdim/// Example matches true 2177243830Sdim/// \code 2178239313Sdim/// true 2179243830Sdim/// \endcode 2180327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr> 2181327952Sdim cxxBoolLiteral; 2182239313Sdim 2183341825Sdim/// Matches string literals (also matches wide string literals). 2184239313Sdim/// 2185239313Sdim/// Example matches "abcd", L"abcd" 2186243830Sdim/// \code 2187309124Sdim/// char *s = "abcd"; 2188309124Sdim/// wchar_t *ws = L"abcd"; 2189243830Sdim/// \endcode 2190327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral> 2191327952Sdim stringLiteral; 2192239313Sdim 2193341825Sdim/// Matches character literals (also matches wchar_t). 2194239313Sdim/// 2195239313Sdim/// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), 2196239313Sdim/// though. 2197239313Sdim/// 2198239313Sdim/// Example matches 'a', L'a' 2199243830Sdim/// \code 2200309124Sdim/// char ch = 'a'; 2201309124Sdim/// wchar_t chw = L'a'; 2202243830Sdim/// \endcode 2203327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CharacterLiteral> 2204327952Sdim characterLiteral; 2205239313Sdim 2206341825Sdim/// Matches integer literals of all sizes / encodings, e.g. 2207261991Sdim/// 1, 1L, 0x1 and 1U. 2208239313Sdim/// 2209261991Sdim/// Does not match character-encoded integers such as L'a'. 2210327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, IntegerLiteral> 2211327952Sdim integerLiteral; 2212239313Sdim 2213341825Sdim/// Matches float literals of all sizes / encodings, e.g. 2214261991Sdim/// 1.0, 1.0f, 1.0L and 1e10. 2215261991Sdim/// 2216261991Sdim/// Does not match implicit conversions such as 2217261991Sdim/// \code 2218261991Sdim/// float a = 10; 2219261991Sdim/// \endcode 2220327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, FloatingLiteral> 2221327952Sdim floatLiteral; 2222261991Sdim 2223344779Sdim/// Matches imaginary literals, which are based on integer and floating 2224344779Sdim/// point literals e.g.: 1i, 1.0i 2225344779Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ImaginaryLiteral> 2226344779Sdim imaginaryLiteral; 2227344779Sdim 2228341825Sdim/// Matches user defined literal operator call. 2229243830Sdim/// 2230243830Sdim/// Example match: "foo"_suffix 2231327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, UserDefinedLiteral> 2232327952Sdim userDefinedLiteral; 2233243830Sdim 2234341825Sdim/// Matches compound (i.e. non-scalar) literals 2235249423Sdim/// 2236249423Sdim/// Example match: {1}, (1, 2) 2237249423Sdim/// \code 2238309124Sdim/// int array[4] = {1}; 2239309124Sdim/// vector int myvec = (vector int)(1, 2); 2240249423Sdim/// \endcode 2241327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundLiteralExpr> 2242327952Sdim compoundLiteralExpr; 2243249423Sdim 2244341825Sdim/// Matches nullptr literal. 2245327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNullPtrLiteralExpr> 2246327952Sdim cxxNullPtrLiteralExpr; 2247243830Sdim 2248353358Sdim/// Matches GNU __builtin_choose_expr. 2249353358Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ChooseExpr> 2250353358Sdim chooseExpr; 2251353358Sdim 2252341825Sdim/// Matches GNU __null expression. 2253327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr> 2254327952Sdim gnuNullExpr; 2255288943Sdim 2256341825Sdim/// Matches atomic builtins. 2257309124Sdim/// Example matches __atomic_load_n(ptr, 1) 2258309124Sdim/// \code 2259309124Sdim/// void foo() { int *ptr; __atomic_load_n(ptr, 1); } 2260309124Sdim/// \endcode 2261327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, AtomicExpr> atomicExpr; 2262309124Sdim 2263341825Sdim/// Matches statement expression (GNU extension). 2264309124Sdim/// 2265309124Sdim/// Example match: ({ int X = 4; X; }) 2266309124Sdim/// \code 2267309124Sdim/// int C = ({ int X = 4; X; }); 2268309124Sdim/// \endcode 2269327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, StmtExpr> stmtExpr; 2270309124Sdim 2271341825Sdim/// Matches binary operator expressions. 2272239313Sdim/// 2273239313Sdim/// Example matches a || b 2274243830Sdim/// \code 2275239313Sdim/// !(a || b) 2276243830Sdim/// \endcode 2277327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, BinaryOperator> 2278327952Sdim binaryOperator; 2279239313Sdim 2280341825Sdim/// Matches unary operator expressions. 2281239313Sdim/// 2282239313Sdim/// Example matches !a 2283243830Sdim/// \code 2284239313Sdim/// !a || b 2285243830Sdim/// \endcode 2286327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, UnaryOperator> 2287327952Sdim unaryOperator; 2288239313Sdim 2289341825Sdim/// Matches conditional operator expressions. 2290239313Sdim/// 2291239313Sdim/// Example matches a ? b : c 2292243830Sdim/// \code 2293239313Sdim/// (a ? b : c) + 42 2294243830Sdim/// \endcode 2295327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ConditionalOperator> 2296327952Sdim conditionalOperator; 2297239313Sdim 2298341825Sdim/// Matches binary conditional operator expressions (GNU extension). 2299309124Sdim/// 2300309124Sdim/// Example matches a ?: b 2301309124Sdim/// \code 2302309124Sdim/// (a ?: b) + 42; 2303309124Sdim/// \endcode 2304327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 2305327952Sdim BinaryConditionalOperator> 2306327952Sdim binaryConditionalOperator; 2307309124Sdim 2308341825Sdim/// Matches opaque value expressions. They are used as helpers 2309309124Sdim/// to reference another expressions and can be met 2310309124Sdim/// in BinaryConditionalOperators, for example. 2311309124Sdim/// 2312309124Sdim/// Example matches 'a' 2313309124Sdim/// \code 2314309124Sdim/// (a ?: c) + 42; 2315309124Sdim/// \endcode 2316327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, OpaqueValueExpr> 2317327952Sdim opaqueValueExpr; 2318309124Sdim 2319341825Sdim/// Matches a C++ static_assert declaration. 2320288943Sdim/// 2321288943Sdim/// Example: 2322288943Sdim/// staticAssertExpr() 2323288943Sdim/// matches 2324288943Sdim/// static_assert(sizeof(S) == sizeof(int)) 2325288943Sdim/// in 2326288943Sdim/// \code 2327288943Sdim/// struct S { 2328288943Sdim/// int x; 2329288943Sdim/// }; 2330288943Sdim/// static_assert(sizeof(S) == sizeof(int)); 2331288943Sdim/// \endcode 2332327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Decl, StaticAssertDecl> 2333327952Sdim staticAssertDecl; 2334288943Sdim 2335341825Sdim/// Matches a reinterpret_cast expression. 2336239313Sdim/// 2337239313Sdim/// Either the source expression or the destination type can be matched 2338239313Sdim/// using has(), but hasDestinationType() is more specific and can be 2339239313Sdim/// more readable. 2340239313Sdim/// 2341239313Sdim/// Example matches reinterpret_cast<char*>(&p) in 2342243830Sdim/// \code 2343239313Sdim/// void* p = reinterpret_cast<char*>(&p); 2344243830Sdim/// \endcode 2345327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXReinterpretCastExpr> 2346327952Sdim cxxReinterpretCastExpr; 2347239313Sdim 2348341825Sdim/// Matches a C++ static_cast expression. 2349239313Sdim/// 2350239313Sdim/// \see hasDestinationType 2351239313Sdim/// \see reinterpretCast 2352239313Sdim/// 2353239313Sdim/// Example: 2354296417Sdim/// cxxStaticCastExpr() 2355239313Sdim/// matches 2356239313Sdim/// static_cast<long>(8) 2357239313Sdim/// in 2358243830Sdim/// \code 2359239313Sdim/// long eight(static_cast<long>(8)); 2360243830Sdim/// \endcode 2361327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXStaticCastExpr> 2362327952Sdim cxxStaticCastExpr; 2363239313Sdim 2364341825Sdim/// Matches a dynamic_cast expression. 2365239313Sdim/// 2366239313Sdim/// Example: 2367296417Sdim/// cxxDynamicCastExpr() 2368239313Sdim/// matches 2369239313Sdim/// dynamic_cast<D*>(&b); 2370239313Sdim/// in 2371243830Sdim/// \code 2372239313Sdim/// struct B { virtual ~B() {} }; struct D : B {}; 2373239313Sdim/// B b; 2374239313Sdim/// D* p = dynamic_cast<D*>(&b); 2375243830Sdim/// \endcode 2376327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDynamicCastExpr> 2377327952Sdim cxxDynamicCastExpr; 2378239313Sdim 2379341825Sdim/// Matches a const_cast expression. 2380239313Sdim/// 2381239313Sdim/// Example: Matches const_cast<int*>(&r) in 2382243830Sdim/// \code 2383239313Sdim/// int n = 42; 2384243830Sdim/// const int &r(n); 2385239313Sdim/// int* p = const_cast<int*>(&r); 2386243830Sdim/// \endcode 2387327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstCastExpr> 2388327952Sdim cxxConstCastExpr; 2389239313Sdim 2390341825Sdim/// Matches a C-style cast expression. 2391243830Sdim/// 2392314564Sdim/// Example: Matches (int) 2.2f in 2393243830Sdim/// \code 2394243830Sdim/// int i = (int) 2.2f; 2395243830Sdim/// \endcode 2396327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CStyleCastExpr> 2397327952Sdim cStyleCastExpr; 2398243830Sdim 2399341825Sdim/// Matches explicit cast expressions. 2400239313Sdim/// 2401239313Sdim/// Matches any cast expression written in user code, whether it be a 2402239313Sdim/// C-style cast, a functional-style cast, or a keyword cast. 2403239313Sdim/// 2404239313Sdim/// Does not match implicit conversions. 2405239313Sdim/// 2406239313Sdim/// Note: the name "explicitCast" is chosen to match Clang's terminology, as 2407239313Sdim/// Clang uses the term "cast" to apply to implicit conversions as well as to 2408239313Sdim/// actual cast expressions. 2409239313Sdim/// 2410239313Sdim/// \see hasDestinationType. 2411239313Sdim/// 2412239313Sdim/// Example: matches all five of the casts in 2413243830Sdim/// \code 2414239313Sdim/// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42))))) 2415243830Sdim/// \endcode 2416239313Sdim/// but does not match the implicit conversion in 2417243830Sdim/// \code 2418239313Sdim/// long ell = 42; 2419243830Sdim/// \endcode 2420327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ExplicitCastExpr> 2421327952Sdim explicitCastExpr; 2422239313Sdim 2423341825Sdim/// Matches the implicit cast nodes of Clang's AST. 2424239313Sdim/// 2425239313Sdim/// This matches many different places, including function call return value 2426239313Sdim/// eliding, as well as any type conversions. 2427327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitCastExpr> 2428327952Sdim implicitCastExpr; 2429239313Sdim 2430341825Sdim/// Matches any cast nodes of Clang's AST. 2431239462Sdim/// 2432239462Sdim/// Example: castExpr() matches each of the following: 2433243830Sdim/// \code 2434239462Sdim/// (int) 3; 2435239462Sdim/// const_cast<Expr *>(SubExpr); 2436239462Sdim/// char c = 0; 2437243830Sdim/// \endcode 2438239462Sdim/// but does not match 2439243830Sdim/// \code 2440239462Sdim/// int i = (0); 2441239462Sdim/// int k = 0; 2442243830Sdim/// \endcode 2443327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr; 2444239462Sdim 2445341825Sdim/// Matches functional cast expressions 2446239313Sdim/// 2447239313Sdim/// Example: Matches Foo(bar); 2448243830Sdim/// \code 2449239313Sdim/// Foo f = bar; 2450239313Sdim/// Foo g = (Foo) bar; 2451239313Sdim/// Foo h = Foo(bar); 2452243830Sdim/// \endcode 2453327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFunctionalCastExpr> 2454327952Sdim cxxFunctionalCastExpr; 2455239313Sdim 2456341825Sdim/// Matches functional cast expressions having N != 1 arguments 2457261991Sdim/// 2458261991Sdim/// Example: Matches Foo(bar, bar) 2459261991Sdim/// \code 2460261991Sdim/// Foo h = Foo(bar, bar); 2461261991Sdim/// \endcode 2462327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTemporaryObjectExpr> 2463327952Sdim cxxTemporaryObjectExpr; 2464261991Sdim 2465341825Sdim/// Matches predefined identifier expressions [C99 6.4.2.2]. 2466309124Sdim/// 2467309124Sdim/// Example: Matches __func__ 2468309124Sdim/// \code 2469309124Sdim/// printf("%s", __func__); 2470309124Sdim/// \endcode 2471327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, PredefinedExpr> 2472327952Sdim predefinedExpr; 2473309124Sdim 2474341825Sdim/// Matches C99 designated initializer expressions [C99 6.7.8]. 2475309124Sdim/// 2476309124Sdim/// Example: Matches { [2].y = 1.0, [0].x = 1.0 } 2477309124Sdim/// \code 2478309124Sdim/// point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 }; 2479309124Sdim/// \endcode 2480327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, DesignatedInitExpr> 2481327952Sdim designatedInitExpr; 2482309124Sdim 2483341825Sdim/// Matches designated initializer expressions that contain 2484309124Sdim/// a specific number of designators. 2485309124Sdim/// 2486309124Sdim/// Example: Given 2487309124Sdim/// \code 2488309124Sdim/// point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 }; 2489309124Sdim/// point ptarray2[10] = { [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 }; 2490309124Sdim/// \endcode 2491309124Sdim/// designatorCountIs(2) 2492309124Sdim/// matches '{ [2].y = 1.0, [0].x = 1.0 }', 2493309124Sdim/// but not '{ [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 }'. 2494309124SdimAST_MATCHER_P(DesignatedInitExpr, designatorCountIs, unsigned, N) { 2495309124Sdim return Node.size() == N; 2496309124Sdim} 2497309124Sdim 2498341825Sdim/// Matches \c QualTypes in the clang AST. 2499327952Sdimextern const internal::VariadicAllOfMatcher<QualType> qualType; 2500243830Sdim 2501341825Sdim/// Matches \c Types in the clang AST. 2502327952Sdimextern const internal::VariadicAllOfMatcher<Type> type; 2503243830Sdim 2504341825Sdim/// Matches \c TypeLocs in the clang AST. 2505327952Sdimextern const internal::VariadicAllOfMatcher<TypeLoc> typeLoc; 2506243830Sdim 2507341825Sdim/// Matches if any of the given matchers matches. 2508249423Sdim/// 2509249423Sdim/// Unlike \c anyOf, \c eachOf will generate a match result for each 2510249423Sdim/// matching submatcher. 2511249423Sdim/// 2512249423Sdim/// For example, in: 2513249423Sdim/// \code 2514249423Sdim/// class A { int a; int b; }; 2515249423Sdim/// \endcode 2516249423Sdim/// The matcher: 2517249423Sdim/// \code 2518296417Sdim/// cxxRecordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), 2519296417Sdim/// has(fieldDecl(hasName("b")).bind("v")))) 2520249423Sdim/// \endcode 2521249423Sdim/// will generate two results binding "v", the first of which binds 2522249423Sdim/// the field declaration of \c a, the second the field declaration of 2523249423Sdim/// \c b. 2524249423Sdim/// 2525249423Sdim/// Usable as: Any Matcher 2526327952Sdimextern const internal::VariadicOperatorMatcherFunc< 2527327952Sdim 2, std::numeric_limits<unsigned>::max()> 2528327952Sdim eachOf; 2529249423Sdim 2530341825Sdim/// Matches if any of the given matchers matches. 2531243830Sdim/// 2532243830Sdim/// Usable as: Any Matcher 2533327952Sdimextern const internal::VariadicOperatorMatcherFunc< 2534327952Sdim 2, std::numeric_limits<unsigned>::max()> 2535327952Sdim anyOf; 2536243830Sdim 2537341825Sdim/// Matches if all given matchers match. 2538243830Sdim/// 2539243830Sdim/// Usable as: Any Matcher 2540327952Sdimextern const internal::VariadicOperatorMatcherFunc< 2541327952Sdim 2, std::numeric_limits<unsigned>::max()> 2542327952Sdim allOf; 2543243830Sdim 2544360784Sdim/// Matches any node regardless of the submatchers. 2545360784Sdim/// 2546360784Sdim/// However, \c optionally will generate a result binding for each matching 2547360784Sdim/// submatcher. 2548360784Sdim/// 2549360784Sdim/// Useful when additional information which may or may not present about a 2550360784Sdim/// main matching node is desired. 2551360784Sdim/// 2552360784Sdim/// For example, in: 2553360784Sdim/// \code 2554360784Sdim/// class Foo { 2555360784Sdim/// int bar; 2556360784Sdim/// } 2557360784Sdim/// \endcode 2558360784Sdim/// The matcher: 2559360784Sdim/// \code 2560360784Sdim/// cxxRecordDecl( 2561360784Sdim/// optionally(has( 2562360784Sdim/// fieldDecl(hasName("bar")).bind("var") 2563360784Sdim/// ))).bind("record") 2564360784Sdim/// \endcode 2565360784Sdim/// will produce a result binding for both "record" and "var". 2566360784Sdim/// The matcher will produce a "record" binding for even if there is no data 2567360784Sdim/// member named "bar" in that class. 2568360784Sdim/// 2569360784Sdim/// Usable as: Any Matcher 2570360784Sdimextern const internal::VariadicOperatorMatcherFunc< 2571360784Sdim 1, std::numeric_limits<unsigned>::max()> 2572360784Sdim optionally; 2573360784Sdim 2574341825Sdim/// Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) 2575239313Sdim/// 2576239313Sdim/// Given 2577243830Sdim/// \code 2578239313Sdim/// Foo x = bar; 2579239313Sdim/// int y = sizeof(x) + alignof(x); 2580243830Sdim/// \endcode 2581239313Sdim/// unaryExprOrTypeTraitExpr() 2582239313Sdim/// matches \c sizeof(x) and \c alignof(x) 2583327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, 2584327952Sdim UnaryExprOrTypeTraitExpr> 2585327952Sdim unaryExprOrTypeTraitExpr; 2586239313Sdim 2587341825Sdim/// Matches unary expressions that have a specific type of argument. 2588239313Sdim/// 2589239313Sdim/// Given 2590243830Sdim/// \code 2591239313Sdim/// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c); 2592243830Sdim/// \endcode 2593239313Sdim/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int")) 2594239313Sdim/// matches \c sizeof(a) and \c alignof(c) 2595239313SdimAST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType, 2596243830Sdim internal::Matcher<QualType>, InnerMatcher) { 2597239313Sdim const QualType ArgumentType = Node.getTypeOfArgument(); 2598243830Sdim return InnerMatcher.matches(ArgumentType, Finder, Builder); 2599239313Sdim} 2600239313Sdim 2601341825Sdim/// Matches unary expressions of a certain kind. 2602239313Sdim/// 2603239313Sdim/// Given 2604243830Sdim/// \code 2605239313Sdim/// int x; 2606239313Sdim/// int s = sizeof(x) + alignof(x) 2607243830Sdim/// \endcode 2608239313Sdim/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)) 2609239313Sdim/// matches \c sizeof(x) 2610353358Sdim/// 2611353358Sdim/// If the matcher is use from clang-query, UnaryExprOrTypeTrait parameter 2612353358Sdim/// should be passed as a quoted string. e.g., ofKind("UETT_SizeOf"). 2613239313SdimAST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) { 2614239313Sdim return Node.getKind() == Kind; 2615239313Sdim} 2616239313Sdim 2617341825Sdim/// Same as unaryExprOrTypeTraitExpr, but only matching 2618239313Sdim/// alignof. 2619239313Sdiminline internal::Matcher<Stmt> alignOfExpr( 2620243830Sdim const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 2621344779Sdim return stmt(unaryExprOrTypeTraitExpr( 2622344779Sdim allOf(anyOf(ofKind(UETT_AlignOf), ofKind(UETT_PreferredAlignOf)), 2623344779Sdim InnerMatcher))); 2624239313Sdim} 2625239313Sdim 2626341825Sdim/// Same as unaryExprOrTypeTraitExpr, but only matching 2627239313Sdim/// sizeof. 2628239313Sdiminline internal::Matcher<Stmt> sizeOfExpr( 2629243830Sdim const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) { 2630249423Sdim return stmt(unaryExprOrTypeTraitExpr( 2631249423Sdim allOf(ofKind(UETT_SizeOf), InnerMatcher))); 2632239313Sdim} 2633239313Sdim 2634341825Sdim/// Matches NamedDecl nodes that have the specified name. 2635239313Sdim/// 2636239313Sdim/// Supports specifying enclosing namespaces or classes by prefixing the name 2637239313Sdim/// with '<enclosing>::'. 2638239313Sdim/// Does not match typedefs of an underlying type with the given name. 2639239313Sdim/// 2640239313Sdim/// Example matches X (Name == "X") 2641243830Sdim/// \code 2642239313Sdim/// class X; 2643243830Sdim/// \endcode 2644239313Sdim/// 2645239313Sdim/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X") 2646243830Sdim/// \code 2647243830Sdim/// namespace a { namespace b { class X; } } 2648243830Sdim/// \endcode 2649280031Sdiminline internal::Matcher<NamedDecl> hasName(const std::string &Name) { 2650341825Sdim return internal::Matcher<NamedDecl>(new internal::HasNameMatcher({Name})); 2651239313Sdim} 2652239313Sdim 2653341825Sdim/// Matches NamedDecl nodes that have any of the specified names. 2654309124Sdim/// 2655309124Sdim/// This matcher is only provided as a performance optimization of hasName. 2656309124Sdim/// \code 2657309124Sdim/// hasAnyName(a, b, c) 2658309124Sdim/// \endcode 2659309124Sdim/// is equivalent to, but faster than 2660309124Sdim/// \code 2661309124Sdim/// anyOf(hasName(a), hasName(b), hasName(c)) 2662309124Sdim/// \endcode 2663327952Sdimextern const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef, 2664327952Sdim internal::hasAnyNameFunc> 2665327952Sdim hasAnyName; 2666309124Sdim 2667341825Sdim/// Matches NamedDecl nodes whose fully qualified names contain 2668249423Sdim/// a substring matched by the given RegExp. 2669239313Sdim/// 2670239313Sdim/// Supports specifying enclosing namespaces or classes by 2671239313Sdim/// prefixing the name with '<enclosing>::'. Does not match typedefs 2672239313Sdim/// of an underlying type with the given name. 2673239313Sdim/// 2674239313Sdim/// Example matches X (regexp == "::X") 2675243830Sdim/// \code 2676239313Sdim/// class X; 2677243830Sdim/// \endcode 2678239313Sdim/// 2679239313Sdim/// Example matches X (regexp is one of "::X", "^foo::.*X", among others) 2680243830Sdim/// \code 2681243830Sdim/// namespace foo { namespace bar { class X; } } 2682243830Sdim/// \endcode 2683239313SdimAST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { 2684239313Sdim assert(!RegExp.empty()); 2685239313Sdim std::string FullNameString = "::" + Node.getQualifiedNameAsString(); 2686239313Sdim llvm::Regex RE(RegExp); 2687239313Sdim return RE.match(FullNameString); 2688239313Sdim} 2689239313Sdim 2690341825Sdim/// Matches overloaded operator names. 2691239313Sdim/// 2692239313Sdim/// Matches overloaded operator names specified in strings without the 2693249423Sdim/// "operator" prefix: e.g. "<<". 2694239313Sdim/// 2695249423Sdim/// Given: 2696243830Sdim/// \code 2697249423Sdim/// class A { int operator*(); }; 2698249423Sdim/// const A &operator<<(const A &a, const A &b); 2699249423Sdim/// A a; 2700249423Sdim/// a << a; // <-- This matches 2701243830Sdim/// \endcode 2702249423Sdim/// 2703296417Sdim/// \c cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))) matches the 2704296417Sdim/// specified line and 2705296417Sdim/// \c cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*"))) 2706296417Sdim/// matches the declaration of \c A. 2707249423Sdim/// 2708276479Sdim/// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl> 2709249423Sdiminline internal::PolymorphicMatcherWithParam1< 2710261991Sdim internal::HasOverloadedOperatorNameMatcher, StringRef, 2711288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl)> 2712280031SdimhasOverloadedOperatorName(StringRef Name) { 2713249423Sdim return internal::PolymorphicMatcherWithParam1< 2714261991Sdim internal::HasOverloadedOperatorNameMatcher, StringRef, 2715288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl)>(Name); 2716239313Sdim} 2717239313Sdim 2718360784Sdim/// Matches C++ classes that are directly or indirectly derived from a class 2719360784Sdim/// matching \c Base, or Objective-C classes that directly or indirectly 2720360784Sdim/// subclass a class matching \c Base. 2721239313Sdim/// 2722243830Sdim/// Note that a class is not considered to be derived from itself. 2723239313Sdim/// 2724243830Sdim/// Example matches Y, Z, C (Base == hasName("X")) 2725243830Sdim/// \code 2726243830Sdim/// class X; 2727239313Sdim/// class Y : public X {}; // directly derived 2728239313Sdim/// class Z : public Y {}; // indirectly derived 2729239313Sdim/// typedef X A; 2730239313Sdim/// typedef A B; 2731239313Sdim/// class C : public B {}; // derived from a typedef of X 2732243830Sdim/// \endcode 2733239313Sdim/// 2734239313Sdim/// In the following example, Bar matches isDerivedFrom(hasName("X")): 2735243830Sdim/// \code 2736239313Sdim/// class Foo; 2737239313Sdim/// typedef Foo X; 2738239313Sdim/// class Bar : public Foo {}; // derived from a type that X is a typedef of 2739243830Sdim/// \endcode 2740360784Sdim/// 2741360784Sdim/// In the following example, Bar matches isDerivedFrom(hasName("NSObject")) 2742360784Sdim/// \code 2743360784Sdim/// @interface NSObject @end 2744360784Sdim/// @interface Bar : NSObject @end 2745360784Sdim/// \endcode 2746360784Sdim/// 2747360784Sdim/// Usable as: Matcher<CXXRecordDecl>, Matcher<ObjCInterfaceDecl> 2748360784SdimAST_POLYMORPHIC_MATCHER_P( 2749360784Sdim isDerivedFrom, 2750360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl), 2751360784Sdim internal::Matcher<NamedDecl>, Base) { 2752360784Sdim // Check if the node is a C++ struct/union/class. 2753360784Sdim if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node)) 2754360784Sdim return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/false); 2755360784Sdim 2756360784Sdim // The node must be an Objective-C class. 2757360784Sdim const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node); 2758360784Sdim return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder, 2759360784Sdim /*Directly=*/false); 2760239313Sdim} 2761239313Sdim 2762341825Sdim/// Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). 2763360784SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 2764360784Sdim isDerivedFrom, 2765360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl), 2766360784Sdim std::string, BaseName, 1) { 2767360784Sdim if (BaseName.empty()) 2768360784Sdim return false; 2769360784Sdim 2770360784Sdim const auto M = isDerivedFrom(hasName(BaseName)); 2771360784Sdim 2772360784Sdim if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node)) 2773360784Sdim return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder); 2774360784Sdim 2775360784Sdim const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node); 2776360784Sdim return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder); 2777239313Sdim} 2778239313Sdim 2779341825Sdim/// Similar to \c isDerivedFrom(), but also matches classes that directly 2780243830Sdim/// match \c Base. 2781360784SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 2782360784Sdim isSameOrDerivedFrom, 2783360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl), 2784360784Sdim internal::Matcher<NamedDecl>, Base, 0) { 2785360784Sdim const auto M = anyOf(Base, isDerivedFrom(Base)); 2786360784Sdim 2787360784Sdim if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node)) 2788360784Sdim return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder); 2789360784Sdim 2790360784Sdim const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node); 2791360784Sdim return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder); 2792243830Sdim} 2793243830Sdim 2794341825Sdim/// Overloaded method as shortcut for 2795243830Sdim/// \c isSameOrDerivedFrom(hasName(...)). 2796360784SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 2797360784Sdim isSameOrDerivedFrom, 2798360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl), 2799360784Sdim std::string, BaseName, 1) { 2800360784Sdim if (BaseName.empty()) 2801360784Sdim return false; 2802360784Sdim 2803360784Sdim const auto M = isSameOrDerivedFrom(hasName(BaseName)); 2804360784Sdim 2805360784Sdim if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node)) 2806360784Sdim return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder); 2807360784Sdim 2808360784Sdim const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node); 2809360784Sdim return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder); 2810243830Sdim} 2811243830Sdim 2812360784Sdim/// Matches C++ or Objective-C classes that are directly derived from a class 2813360784Sdim/// matching \c Base. 2814360784Sdim/// 2815360784Sdim/// Note that a class is not considered to be derived from itself. 2816360784Sdim/// 2817360784Sdim/// Example matches Y, C (Base == hasName("X")) 2818360784Sdim/// \code 2819360784Sdim/// class X; 2820360784Sdim/// class Y : public X {}; // directly derived 2821360784Sdim/// class Z : public Y {}; // indirectly derived 2822360784Sdim/// typedef X A; 2823360784Sdim/// typedef A B; 2824360784Sdim/// class C : public B {}; // derived from a typedef of X 2825360784Sdim/// \endcode 2826360784Sdim/// 2827360784Sdim/// In the following example, Bar matches isDerivedFrom(hasName("X")): 2828360784Sdim/// \code 2829360784Sdim/// class Foo; 2830360784Sdim/// typedef Foo X; 2831360784Sdim/// class Bar : public Foo {}; // derived from a type that X is a typedef of 2832360784Sdim/// \endcode 2833360784SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 2834360784Sdim isDirectlyDerivedFrom, 2835360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl), 2836360784Sdim internal::Matcher<NamedDecl>, Base, 0) { 2837360784Sdim // Check if the node is a C++ struct/union/class. 2838360784Sdim if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node)) 2839360784Sdim return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/true); 2840360784Sdim 2841360784Sdim // The node must be an Objective-C class. 2842360784Sdim const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node); 2843360784Sdim return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder, 2844360784Sdim /*Directly=*/true); 2845360784Sdim} 2846360784Sdim 2847360784Sdim/// Overloaded method as shortcut for \c isDirectlyDerivedFrom(hasName(...)). 2848360784SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 2849360784Sdim isDirectlyDerivedFrom, 2850360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl), 2851360784Sdim std::string, BaseName, 1) { 2852360784Sdim if (BaseName.empty()) 2853360784Sdim return false; 2854360784Sdim const auto M = isDirectlyDerivedFrom(hasName(BaseName)); 2855360784Sdim 2856360784Sdim if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node)) 2857360784Sdim return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder); 2858360784Sdim 2859360784Sdim const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node); 2860360784Sdim return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder); 2861360784Sdim} 2862341825Sdim/// Matches the first method of a class or struct that satisfies \c 2863249423Sdim/// InnerMatcher. 2864249423Sdim/// 2865249423Sdim/// Given: 2866249423Sdim/// \code 2867249423Sdim/// class A { void func(); }; 2868249423Sdim/// class B { void member(); }; 2869296417Sdim/// \endcode 2870249423Sdim/// 2871296417Sdim/// \c cxxRecordDecl(hasMethod(hasName("func"))) matches the declaration of 2872296417Sdim/// \c A but not \c B. 2873249423SdimAST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>, 2874249423Sdim InnerMatcher) { 2875261991Sdim return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(), 2876261991Sdim Node.method_end(), Finder, Builder); 2877249423Sdim} 2878249423Sdim 2879341825Sdim/// Matches the generated class of lambda expressions. 2880309124Sdim/// 2881309124Sdim/// Given: 2882309124Sdim/// \code 2883309124Sdim/// auto x = []{}; 2884309124Sdim/// \endcode 2885309124Sdim/// 2886309124Sdim/// \c cxxRecordDecl(isLambda()) matches the implicit class declaration of 2887309124Sdim/// \c decltype(x) 2888309124SdimAST_MATCHER(CXXRecordDecl, isLambda) { 2889309124Sdim return Node.isLambda(); 2890309124Sdim} 2891309124Sdim 2892341825Sdim/// Matches AST nodes that have child AST nodes that match the 2893239313Sdim/// provided matcher. 2894239313Sdim/// 2895296417Sdim/// Example matches X, Y 2896296417Sdim/// (matcher = cxxRecordDecl(has(cxxRecordDecl(hasName("X"))) 2897243830Sdim/// \code 2898239313Sdim/// class X {}; // Matches X, because X::X is a class of name X inside X. 2899239313Sdim/// class Y { class X {}; }; 2900239313Sdim/// class Z { class Y { class X {}; }; }; // Does not match Z. 2901243830Sdim/// \endcode 2902239313Sdim/// 2903239313Sdim/// ChildT must be an AST base type. 2904243830Sdim/// 2905243830Sdim/// Usable as: Any Matcher 2906309124Sdim/// Note that has is direct matcher, so it also matches things like implicit 2907309124Sdim/// casts and paren casts. If you are matching with expr then you should 2908309124Sdim/// probably consider using ignoringParenImpCasts like: 2909309124Sdim/// has(ignoringParenImpCasts(expr())). 2910327952Sdimextern const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> has; 2911239313Sdim 2912341825Sdim/// Matches AST nodes that have descendant AST nodes that match the 2913239313Sdim/// provided matcher. 2914239313Sdim/// 2915239313Sdim/// Example matches X, Y, Z 2916296417Sdim/// (matcher = cxxRecordDecl(hasDescendant(cxxRecordDecl(hasName("X"))))) 2917243830Sdim/// \code 2918239313Sdim/// class X {}; // Matches X, because X::X is a class of name X inside X. 2919239313Sdim/// class Y { class X {}; }; 2920239313Sdim/// class Z { class Y { class X {}; }; }; 2921243830Sdim/// \endcode 2922239313Sdim/// 2923239313Sdim/// DescendantT must be an AST base type. 2924243830Sdim/// 2925243830Sdim/// Usable as: Any Matcher 2926327952Sdimextern const internal::ArgumentAdaptingMatcherFunc< 2927327952Sdim internal::HasDescendantMatcher> 2928327952Sdim hasDescendant; 2929239313Sdim 2930341825Sdim/// Matches AST nodes that have child AST nodes that match the 2931239313Sdim/// provided matcher. 2932239313Sdim/// 2933341825Sdim/// Example matches X, Y, Y::X, Z::Y, Z::Y::X 2934296417Sdim/// (matcher = cxxRecordDecl(forEach(cxxRecordDecl(hasName("X"))) 2935243830Sdim/// \code 2936341825Sdim/// class X {}; 2937341825Sdim/// class Y { class X {}; }; // Matches Y, because Y::X is a class of name X 2938341825Sdim/// // inside Y. 2939239313Sdim/// class Z { class Y { class X {}; }; }; // Does not match Z. 2940243830Sdim/// \endcode 2941239313Sdim/// 2942239313Sdim/// ChildT must be an AST base type. 2943239313Sdim/// 2944239313Sdim/// As opposed to 'has', 'forEach' will cause a match for each result that 2945239313Sdim/// matches instead of only on the first one. 2946243830Sdim/// 2947243830Sdim/// Usable as: Any Matcher 2948327952Sdimextern const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher> 2949327952Sdim forEach; 2950239313Sdim 2951341825Sdim/// Matches AST nodes that have descendant AST nodes that match the 2952239313Sdim/// provided matcher. 2953239313Sdim/// 2954341825Sdim/// Example matches X, A, A::X, B, B::C, B::C::X 2955296417Sdim/// (matcher = cxxRecordDecl(forEachDescendant(cxxRecordDecl(hasName("X"))))) 2956243830Sdim/// \code 2957341825Sdim/// class X {}; 2958341825Sdim/// class A { class X {}; }; // Matches A, because A::X is a class of name 2959341825Sdim/// // X inside A. 2960239313Sdim/// class B { class C { class X {}; }; }; 2961243830Sdim/// \endcode 2962239313Sdim/// 2963239313Sdim/// DescendantT must be an AST base type. 2964239313Sdim/// 2965239313Sdim/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for 2966239313Sdim/// each result that matches instead of only on the first one. 2967239313Sdim/// 2968239313Sdim/// Note: Recursively combined ForEachDescendant can cause many matches: 2969296417Sdim/// cxxRecordDecl(forEachDescendant(cxxRecordDecl( 2970296417Sdim/// forEachDescendant(cxxRecordDecl()) 2971296417Sdim/// ))) 2972239313Sdim/// will match 10 times (plus injected class name matches) on: 2973243830Sdim/// \code 2974239313Sdim/// class A { class B { class C { class D { class E {}; }; }; }; }; 2975243830Sdim/// \endcode 2976243830Sdim/// 2977243830Sdim/// Usable as: Any Matcher 2978327952Sdimextern const internal::ArgumentAdaptingMatcherFunc< 2979327952Sdim internal::ForEachDescendantMatcher> 2980327952Sdim forEachDescendant; 2981239313Sdim 2982341825Sdim/// Matches if the node or any descendant matches. 2983249423Sdim/// 2984249423Sdim/// Generates results for each match. 2985249423Sdim/// 2986249423Sdim/// For example, in: 2987249423Sdim/// \code 2988249423Sdim/// class A { class B {}; class C {}; }; 2989249423Sdim/// \endcode 2990249423Sdim/// The matcher: 2991249423Sdim/// \code 2992296417Sdim/// cxxRecordDecl(hasName("::A"), 2993296417Sdim/// findAll(cxxRecordDecl(isDefinition()).bind("m"))) 2994249423Sdim/// \endcode 2995249423Sdim/// will generate results for \c A, \c B and \c C. 2996249423Sdim/// 2997249423Sdim/// Usable as: Any Matcher 2998249423Sdimtemplate <typename T> 2999261991Sdiminternal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) { 3000249423Sdim return eachOf(Matcher, forEachDescendant(Matcher)); 3001249423Sdim} 3002249423Sdim 3003341825Sdim/// Matches AST nodes that have a parent that matches the provided 3004243830Sdim/// matcher. 3005243830Sdim/// 3006243830Sdim/// Given 3007243830Sdim/// \code 3008243830Sdim/// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } } 3009243830Sdim/// \endcode 3010243830Sdim/// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }". 3011243830Sdim/// 3012243830Sdim/// Usable as: Any Matcher 3013327952Sdimextern const internal::ArgumentAdaptingMatcherFunc< 3014296417Sdim internal::HasParentMatcher, 3015296417Sdim internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>, 3016296417Sdim internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>> 3017327952Sdim hasParent; 3018243830Sdim 3019341825Sdim/// Matches AST nodes that have an ancestor that matches the provided 3020243830Sdim/// matcher. 3021243830Sdim/// 3022243830Sdim/// Given 3023243830Sdim/// \code 3024243830Sdim/// void f() { if (true) { int x = 42; } } 3025243830Sdim/// void g() { for (;;) { int x = 43; } } 3026243830Sdim/// \endcode 3027243830Sdim/// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43. 3028243830Sdim/// 3029243830Sdim/// Usable as: Any Matcher 3030327952Sdimextern const internal::ArgumentAdaptingMatcherFunc< 3031296417Sdim internal::HasAncestorMatcher, 3032296417Sdim internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>, 3033296417Sdim internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>> 3034327952Sdim hasAncestor; 3035243830Sdim 3036341825Sdim/// Matches if the provided matcher does not match. 3037239313Sdim/// 3038296417Sdim/// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X")))) 3039243830Sdim/// \code 3040239313Sdim/// class X {}; 3041239313Sdim/// class Y {}; 3042243830Sdim/// \endcode 3043243830Sdim/// 3044243830Sdim/// Usable as: Any Matcher 3045327952Sdimextern const internal::VariadicOperatorMatcherFunc<1, 1> unless; 3046239313Sdim 3047341825Sdim/// Matches a node if the declaration associated with that node 3048261991Sdim/// matches the given matcher. 3049239313Sdim/// 3050261991Sdim/// The associated declaration is: 3051261991Sdim/// - for type nodes, the declaration of the underlying type 3052261991Sdim/// - for CallExpr, the declaration of the callee 3053261991Sdim/// - for MemberExpr, the declaration of the referenced member 3054261991Sdim/// - for CXXConstructExpr, the declaration of the constructor 3055314564Sdim/// - for CXXNewExpr, the declaration of the operator new 3056341825Sdim/// - for ObjCIvarExpr, the declaration of the ivar 3057249423Sdim/// 3058327952Sdim/// For type nodes, hasDeclaration will generally match the declaration of the 3059327952Sdim/// sugared type. Given 3060327952Sdim/// \code 3061327952Sdim/// class X {}; 3062327952Sdim/// typedef X Y; 3063327952Sdim/// Y y; 3064327952Sdim/// \endcode 3065327952Sdim/// in varDecl(hasType(hasDeclaration(decl()))) the decl will match the 3066327952Sdim/// typedefDecl. A common use case is to match the underlying, desugared type. 3067327952Sdim/// This can be achieved by using the hasUnqualifiedDesugaredType matcher: 3068327952Sdim/// \code 3069327952Sdim/// varDecl(hasType(hasUnqualifiedDesugaredType( 3070327952Sdim/// recordType(hasDeclaration(decl()))))) 3071327952Sdim/// \endcode 3072327952Sdim/// In this matcher, the decl will match the CXXRecordDecl of class X. 3073261991Sdim/// 3074314564Sdim/// Usable as: Matcher<AddrLabelExpr>, Matcher<CallExpr>, 3075314564Sdim/// Matcher<CXXConstructExpr>, Matcher<CXXNewExpr>, Matcher<DeclRefExpr>, 3076314564Sdim/// Matcher<EnumType>, Matcher<InjectedClassNameType>, Matcher<LabelStmt>, 3077314564Sdim/// Matcher<MemberExpr>, Matcher<QualType>, Matcher<RecordType>, 3078314564Sdim/// Matcher<TagType>, Matcher<TemplateSpecializationType>, 3079314564Sdim/// Matcher<TemplateTypeParmType>, Matcher<TypedefType>, 3080314564Sdim/// Matcher<UnresolvedUsingType> 3081261991Sdiminline internal::PolymorphicMatcherWithParam1< 3082261991Sdim internal::HasDeclarationMatcher, internal::Matcher<Decl>, 3083261991Sdim void(internal::HasDeclarationSupportedTypes)> 3084261991SdimhasDeclaration(const internal::Matcher<Decl> &InnerMatcher) { 3085239313Sdim return internal::PolymorphicMatcherWithParam1< 3086261991Sdim internal::HasDeclarationMatcher, internal::Matcher<Decl>, 3087261991Sdim void(internal::HasDeclarationSupportedTypes)>(InnerMatcher); 3088239313Sdim} 3089239313Sdim 3090341825Sdim/// Matches a \c NamedDecl whose underlying declaration matches the given 3091314564Sdim/// matcher. 3092314564Sdim/// 3093314564Sdim/// Given 3094314564Sdim/// \code 3095314564Sdim/// namespace N { template<class T> void f(T t); } 3096314564Sdim/// template <class T> void g() { using N::f; f(T()); } 3097314564Sdim/// \endcode 3098314564Sdim/// \c unresolvedLookupExpr(hasAnyDeclaration( 3099314564Sdim/// namedDecl(hasUnderlyingDecl(hasName("::N::f"))))) 3100314564Sdim/// matches the use of \c f in \c g() . 3101314564SdimAST_MATCHER_P(NamedDecl, hasUnderlyingDecl, internal::Matcher<NamedDecl>, 3102314564Sdim InnerMatcher) { 3103314564Sdim const NamedDecl *UnderlyingDecl = Node.getUnderlyingDecl(); 3104314564Sdim 3105314564Sdim return UnderlyingDecl != nullptr && 3106314564Sdim InnerMatcher.matches(*UnderlyingDecl, Finder, Builder); 3107314564Sdim} 3108314564Sdim 3109353358Sdim/// Matches on the implicit object argument of a member call expression, after 3110353358Sdim/// stripping off any parentheses or implicit casts. 3111239313Sdim/// 3112353358Sdim/// Given 3113243830Sdim/// \code 3114353358Sdim/// class Y { public: void m(); }; 3115353358Sdim/// Y g(); 3116353358Sdim/// class X : public Y {}; 3117353358Sdim/// void z(Y y, X x) { y.m(); (g()).m(); x.m(); } 3118243830Sdim/// \endcode 3119353358Sdim/// cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y"))))) 3120353358Sdim/// matches `y.m()` and `(g()).m()`. 3121353358Sdim/// cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("X"))))) 3122353358Sdim/// matches `x.m()`. 3123353358Sdim/// cxxMemberCallExpr(on(callExpr())) 3124353358Sdim/// matches `(g()).m()`. 3125239313Sdim/// 3126239313Sdim/// FIXME: Overload to allow directly matching types? 3127239313SdimAST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>, 3128239313Sdim InnerMatcher) { 3129249423Sdim const Expr *ExprNode = Node.getImplicitObjectArgument() 3130249423Sdim ->IgnoreParenImpCasts(); 3131276479Sdim return (ExprNode != nullptr && 3132239313Sdim InnerMatcher.matches(*ExprNode, Finder, Builder)); 3133239313Sdim} 3134239313Sdim 3135288943Sdim 3136341825Sdim/// Matches on the receiver of an ObjectiveC Message expression. 3137288943Sdim/// 3138288943Sdim/// Example 3139321369Sdim/// matcher = objCMessageExpr(hasReceiverType(asString("UIWebView *"))); 3140288943Sdim/// matches the [webView ...] message invocation. 3141288943Sdim/// \code 3142288943Sdim/// NSString *webViewJavaScript = ... 3143288943Sdim/// UIWebView *webView = ... 3144288943Sdim/// [webView stringByEvaluatingJavaScriptFromString:webViewJavascript]; 3145288943Sdim/// \endcode 3146288943SdimAST_MATCHER_P(ObjCMessageExpr, hasReceiverType, internal::Matcher<QualType>, 3147288943Sdim InnerMatcher) { 3148288943Sdim const QualType TypeDecl = Node.getReceiverType(); 3149288943Sdim return InnerMatcher.matches(TypeDecl, Finder, Builder); 3150288943Sdim} 3151341825Sdim 3152353358Sdim/// Returns true when the Objective-C method declaration is a class method. 3153353358Sdim/// 3154353358Sdim/// Example 3155353358Sdim/// matcher = objcMethodDecl(isClassMethod()) 3156353358Sdim/// matches 3157353358Sdim/// \code 3158353358Sdim/// @interface I + (void)foo; @end 3159353358Sdim/// \endcode 3160353358Sdim/// but not 3161353358Sdim/// \code 3162353358Sdim/// @interface I - (void)bar; @end 3163353358Sdim/// \endcode 3164353358SdimAST_MATCHER(ObjCMethodDecl, isClassMethod) { 3165353358Sdim return Node.isClassMethod(); 3166353358Sdim} 3167353358Sdim 3168353358Sdim/// Returns true when the Objective-C method declaration is an instance method. 3169353358Sdim/// 3170353358Sdim/// Example 3171353358Sdim/// matcher = objcMethodDecl(isInstanceMethod()) 3172353358Sdim/// matches 3173353358Sdim/// \code 3174353358Sdim/// @interface I - (void)bar; @end 3175353358Sdim/// \endcode 3176353358Sdim/// but not 3177353358Sdim/// \code 3178353358Sdim/// @interface I + (void)foo; @end 3179353358Sdim/// \endcode 3180353358SdimAST_MATCHER(ObjCMethodDecl, isInstanceMethod) { 3181353358Sdim return Node.isInstanceMethod(); 3182353358Sdim} 3183353358Sdim 3184353358Sdim/// Returns true when the Objective-C message is sent to a class. 3185353358Sdim/// 3186353358Sdim/// Example 3187353358Sdim/// matcher = objcMessageExpr(isClassMessage()) 3188353358Sdim/// matches 3189353358Sdim/// \code 3190353358Sdim/// [NSString stringWithFormat:@"format"]; 3191353358Sdim/// \endcode 3192353358Sdim/// but not 3193353358Sdim/// \code 3194353358Sdim/// NSString *x = @"hello"; 3195353358Sdim/// [x containsString:@"h"]; 3196353358Sdim/// \endcode 3197353358SdimAST_MATCHER(ObjCMessageExpr, isClassMessage) { 3198353358Sdim return Node.isClassMessage(); 3199353358Sdim} 3200353358Sdim 3201341825Sdim/// Returns true when the Objective-C message is sent to an instance. 3202288943Sdim/// 3203341825Sdim/// Example 3204353358Sdim/// matcher = objcMessageExpr(isInstanceMessage()) 3205341825Sdim/// matches 3206341825Sdim/// \code 3207341825Sdim/// NSString *x = @"hello"; 3208341825Sdim/// [x containsString:@"h"]; 3209341825Sdim/// \endcode 3210341825Sdim/// but not 3211341825Sdim/// \code 3212341825Sdim/// [NSString stringWithFormat:@"format"]; 3213341825Sdim/// \endcode 3214341825SdimAST_MATCHER(ObjCMessageExpr, isInstanceMessage) { 3215341825Sdim return Node.isInstanceMessage(); 3216341825Sdim} 3217341825Sdim 3218341825Sdim/// Matches if the Objective-C message is sent to an instance, 3219341825Sdim/// and the inner matcher matches on that instance. 3220341825Sdim/// 3221341825Sdim/// For example the method call in 3222341825Sdim/// \code 3223341825Sdim/// NSString *x = @"hello"; 3224341825Sdim/// [x containsString:@"h"]; 3225341825Sdim/// \endcode 3226341825Sdim/// is matched by 3227341825Sdim/// objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))) 3228341825SdimAST_MATCHER_P(ObjCMessageExpr, hasReceiver, internal::Matcher<Expr>, 3229341825Sdim InnerMatcher) { 3230341825Sdim const Expr *ReceiverNode = Node.getInstanceReceiver(); 3231341825Sdim return (ReceiverNode != nullptr && 3232341825Sdim InnerMatcher.matches(*ReceiverNode->IgnoreParenImpCasts(), Finder, 3233341825Sdim Builder)); 3234341825Sdim} 3235341825Sdim 3236341825Sdim/// Matches when BaseName == Selector.getAsString() 3237341825Sdim/// 3238288943Sdim/// matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:")); 3239288943Sdim/// matches the outer message expr in the code below, but NOT the message 3240288943Sdim/// invocation for self.bodyView. 3241288943Sdim/// \code 3242288943Sdim/// [self.bodyView loadHTMLString:html baseURL:NULL]; 3243288943Sdim/// \endcode 3244296417SdimAST_MATCHER_P(ObjCMessageExpr, hasSelector, std::string, BaseName) { 3245288943Sdim Selector Sel = Node.getSelector(); 3246288943Sdim return BaseName.compare(Sel.getAsString()) == 0; 3247288943Sdim} 3248288943Sdim 3249341825Sdim 3250341825Sdim/// Matches when at least one of the supplied string equals to the 3251341825Sdim/// Selector.getAsString() 3252341825Sdim/// 3253341825Sdim/// matcher = objCMessageExpr(hasSelector("methodA:", "methodB:")); 3254341825Sdim/// matches both of the expressions below: 3255341825Sdim/// \code 3256341825Sdim/// [myObj methodA:argA]; 3257341825Sdim/// [myObj methodB:argB]; 3258341825Sdim/// \endcode 3259341825Sdimextern const internal::VariadicFunction<internal::Matcher<ObjCMessageExpr>, 3260341825Sdim StringRef, 3261341825Sdim internal::hasAnySelectorFunc> 3262341825Sdim hasAnySelector; 3263341825Sdim 3264341825Sdim/// Matches ObjC selectors whose name contains 3265288943Sdim/// a substring matched by the given RegExp. 3266288943Sdim/// matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?")); 3267288943Sdim/// matches the outer message expr in the code below, but NOT the message 3268288943Sdim/// invocation for self.bodyView. 3269288943Sdim/// \code 3270288943Sdim/// [self.bodyView loadHTMLString:html baseURL:NULL]; 3271288943Sdim/// \endcode 3272288943SdimAST_MATCHER_P(ObjCMessageExpr, matchesSelector, std::string, RegExp) { 3273288943Sdim assert(!RegExp.empty()); 3274288943Sdim std::string SelectorString = Node.getSelector().getAsString(); 3275288943Sdim llvm::Regex RE(RegExp); 3276288943Sdim return RE.match(SelectorString); 3277288943Sdim} 3278288943Sdim 3279341825Sdim/// Matches when the selector is the empty selector 3280288943Sdim/// 3281288943Sdim/// Matches only when the selector of the objCMessageExpr is NULL. This may 3282288943Sdim/// represent an error condition in the tree! 3283288943SdimAST_MATCHER(ObjCMessageExpr, hasNullSelector) { 3284288943Sdim return Node.getSelector().isNull(); 3285288943Sdim} 3286288943Sdim 3287341825Sdim/// Matches when the selector is a Unary Selector 3288288943Sdim/// 3289288943Sdim/// matcher = objCMessageExpr(matchesSelector(hasUnarySelector()); 3290288943Sdim/// matches self.bodyView in the code below, but NOT the outer message 3291288943Sdim/// invocation of "loadHTMLString:baseURL:". 3292288943Sdim/// \code 3293288943Sdim/// [self.bodyView loadHTMLString:html baseURL:NULL]; 3294288943Sdim/// \endcode 3295288943SdimAST_MATCHER(ObjCMessageExpr, hasUnarySelector) { 3296288943Sdim return Node.getSelector().isUnarySelector(); 3297288943Sdim} 3298288943Sdim 3299341825Sdim/// Matches when the selector is a keyword selector 3300288943Sdim/// 3301288943Sdim/// objCMessageExpr(hasKeywordSelector()) matches the generated setFrame 3302288943Sdim/// message expression in 3303288943Sdim/// 3304288943Sdim/// \code 3305288943Sdim/// UIWebView *webView = ...; 3306288943Sdim/// CGRect bodyFrame = webView.frame; 3307288943Sdim/// bodyFrame.size.height = self.bodyContentHeight; 3308288943Sdim/// webView.frame = bodyFrame; 3309288943Sdim/// // ^---- matches here 3310288943Sdim/// \endcode 3311288943SdimAST_MATCHER(ObjCMessageExpr, hasKeywordSelector) { 3312288943Sdim return Node.getSelector().isKeywordSelector(); 3313288943Sdim} 3314288943Sdim 3315341825Sdim/// Matches when the selector has the specified number of arguments 3316288943Sdim/// 3317296417Sdim/// matcher = objCMessageExpr(numSelectorArgs(0)); 3318288943Sdim/// matches self.bodyView in the code below 3319288943Sdim/// 3320288943Sdim/// matcher = objCMessageExpr(numSelectorArgs(2)); 3321288943Sdim/// matches the invocation of "loadHTMLString:baseURL:" but not that 3322288943Sdim/// of self.bodyView 3323288943Sdim/// \code 3324288943Sdim/// [self.bodyView loadHTMLString:html baseURL:NULL]; 3325288943Sdim/// \endcode 3326288943SdimAST_MATCHER_P(ObjCMessageExpr, numSelectorArgs, unsigned, N) { 3327288943Sdim return Node.getSelector().getNumArgs() == N; 3328288943Sdim} 3329341825Sdim 3330341825Sdim/// Matches if the call expression's callee expression matches. 3331239313Sdim/// 3332239313Sdim/// Given 3333243830Sdim/// \code 3334239313Sdim/// class Y { void x() { this->x(); x(); Y y; y.x(); } }; 3335239313Sdim/// void f() { f(); } 3336243830Sdim/// \endcode 3337243830Sdim/// callExpr(callee(expr())) 3338239313Sdim/// matches this->x(), x(), y.x(), f() 3339239313Sdim/// with callee(...) 3340239313Sdim/// matching this->x, x, y.x, f respectively 3341239313Sdim/// 3342239313Sdim/// Note: Callee cannot take the more general internal::Matcher<Expr> 3343239313Sdim/// because this introduces ambiguous overloads with calls to Callee taking a 3344239313Sdim/// internal::Matcher<Decl>, as the matcher hierarchy is purely 3345239313Sdim/// implemented in terms of implicit casts. 3346239313SdimAST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>, 3347239313Sdim InnerMatcher) { 3348239313Sdim const Expr *ExprNode = Node.getCallee(); 3349276479Sdim return (ExprNode != nullptr && 3350239313Sdim InnerMatcher.matches(*ExprNode, Finder, Builder)); 3351239313Sdim} 3352239313Sdim 3353341825Sdim/// Matches if the call expression's callee's declaration matches the 3354239313Sdim/// given matcher. 3355239313Sdim/// 3356296417Sdim/// Example matches y.x() (matcher = callExpr(callee( 3357296417Sdim/// cxxMethodDecl(hasName("x"))))) 3358243830Sdim/// \code 3359239313Sdim/// class Y { public: void x(); }; 3360280031Sdim/// void z() { Y y; y.x(); } 3361243830Sdim/// \endcode 3362261991SdimAST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher, 3363261991Sdim 1) { 3364261991Sdim return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder); 3365239313Sdim} 3366239313Sdim 3367341825Sdim/// Matches if the expression's or declaration's type matches a type 3368239313Sdim/// matcher. 3369239313Sdim/// 3370296417Sdim/// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X"))))) 3371296417Sdim/// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X"))))) 3372309124Sdim/// and U (matcher = typedefDecl(hasType(asString("int"))) 3373341825Sdim/// and friend class X (matcher = friendDecl(hasType("X")) 3374243830Sdim/// \code 3375239313Sdim/// class X {}; 3376239313Sdim/// void y(X &x) { x; X z; } 3377309124Sdim/// typedef int U; 3378341825Sdim/// class Y { friend class X; }; 3379243830Sdim/// \endcode 3380261991SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 3381341825Sdim hasType, 3382341825Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, TypedefNameDecl, 3383341825Sdim ValueDecl), 3384261991Sdim internal::Matcher<QualType>, InnerMatcher, 0) { 3385341825Sdim QualType QT = internal::getUnderlyingType(Node); 3386341825Sdim if (!QT.isNull()) 3387341825Sdim return InnerMatcher.matches(QT, Finder, Builder); 3388341825Sdim return false; 3389239313Sdim} 3390239313Sdim 3391341825Sdim/// Overloaded to match the declaration of the expression's or value 3392239313Sdim/// declaration's type. 3393239313Sdim/// 3394239313Sdim/// In case of a value declaration (for example a variable declaration), 3395239313Sdim/// this resolves one layer of indirection. For example, in the value 3396296417Sdim/// declaration "X x;", cxxRecordDecl(hasName("X")) matches the declaration of 3397296417Sdim/// X, while varDecl(hasType(cxxRecordDecl(hasName("X")))) matches the 3398296417Sdim/// declaration of x. 3399239313Sdim/// 3400296417Sdim/// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X"))))) 3401296417Sdim/// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X"))))) 3402341825Sdim/// and friend class X (matcher = friendDecl(hasType("X")) 3403243830Sdim/// \code 3404239313Sdim/// class X {}; 3405239313Sdim/// void y(X &x) { x; X z; } 3406341825Sdim/// class Y { friend class X; }; 3407243830Sdim/// \endcode 3408239313Sdim/// 3409239313Sdim/// Usable as: Matcher<Expr>, Matcher<ValueDecl> 3410341825SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD( 3411341825Sdim hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl), 3412341825Sdim internal::Matcher<Decl>, InnerMatcher, 1) { 3413341825Sdim QualType QT = internal::getUnderlyingType(Node); 3414341825Sdim if (!QT.isNull()) 3415341825Sdim return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder); 3416341825Sdim return false; 3417239313Sdim} 3418239313Sdim 3419341825Sdim/// Matches if the type location of the declarator decl's type matches 3420261991Sdim/// the inner matcher. 3421261991Sdim/// 3422261991Sdim/// Given 3423261991Sdim/// \code 3424261991Sdim/// int x; 3425261991Sdim/// \endcode 3426261991Sdim/// declaratorDecl(hasTypeLoc(loc(asString("int")))) 3427261991Sdim/// matches int x 3428261991SdimAST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) { 3429261991Sdim if (!Node.getTypeSourceInfo()) 3430261991Sdim // This happens for example for implicit destructors. 3431261991Sdim return false; 3432261991Sdim return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder); 3433261991Sdim} 3434261991Sdim 3435341825Sdim/// Matches if the matched type is represented by the given string. 3436239313Sdim/// 3437239313Sdim/// Given 3438243830Sdim/// \code 3439239313Sdim/// class Y { public: void x(); }; 3440239313Sdim/// void z() { Y* y; y->x(); } 3441243830Sdim/// \endcode 3442296417Sdim/// cxxMemberCallExpr(on(hasType(asString("class Y *")))) 3443239313Sdim/// matches y->x() 3444239313SdimAST_MATCHER_P(QualType, asString, std::string, Name) { 3445239313Sdim return Name == Node.getAsString(); 3446239313Sdim} 3447239313Sdim 3448341825Sdim/// Matches if the matched type is a pointer type and the pointee type 3449239313Sdim/// matches the specified matcher. 3450239313Sdim/// 3451239313Sdim/// Example matches y->x() 3452296417Sdim/// (matcher = cxxMemberCallExpr(on(hasType(pointsTo 3453296417Sdim/// cxxRecordDecl(hasName("Y"))))))) 3454243830Sdim/// \code 3455239313Sdim/// class Y { public: void x(); }; 3456239313Sdim/// void z() { Y *y; y->x(); } 3457243830Sdim/// \endcode 3458239313SdimAST_MATCHER_P( 3459239313Sdim QualType, pointsTo, internal::Matcher<QualType>, 3460239313Sdim InnerMatcher) { 3461296417Sdim return (!Node.isNull() && Node->isAnyPointerType() && 3462239313Sdim InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 3463239313Sdim} 3464239313Sdim 3465341825Sdim/// Overloaded to match the pointee type's declaration. 3466261991SdimAST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>, 3467261991Sdim InnerMatcher, 1) { 3468261991Sdim return pointsTo(qualType(hasDeclaration(InnerMatcher))) 3469261991Sdim .matches(Node, Finder, Builder); 3470239313Sdim} 3471239313Sdim 3472341825Sdim/// Matches if the matched type matches the unqualified desugared 3473314564Sdim/// type of the matched node. 3474314564Sdim/// 3475314564Sdim/// For example, in: 3476314564Sdim/// \code 3477314564Sdim/// class A {}; 3478314564Sdim/// using B = A; 3479314564Sdim/// \endcode 3480341825Sdim/// The matcher type(hasUnqualifiedDesugaredType(recordType())) matches 3481314564Sdim/// both B and A. 3482314564SdimAST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<Type>, 3483314564Sdim InnerMatcher) { 3484314564Sdim return InnerMatcher.matches(*Node.getUnqualifiedDesugaredType(), Finder, 3485314564Sdim Builder); 3486314564Sdim} 3487314564Sdim 3488341825Sdim/// Matches if the matched type is a reference type and the referenced 3489239313Sdim/// type matches the specified matcher. 3490239313Sdim/// 3491239313Sdim/// Example matches X &x and const X &y 3492296417Sdim/// (matcher = varDecl(hasType(references(cxxRecordDecl(hasName("X")))))) 3493243830Sdim/// \code 3494239313Sdim/// class X { 3495239313Sdim/// void a(X b) { 3496239313Sdim/// X &x = b; 3497239313Sdim/// const X &y = b; 3498280031Sdim/// } 3499239313Sdim/// }; 3500243830Sdim/// \endcode 3501239313SdimAST_MATCHER_P(QualType, references, internal::Matcher<QualType>, 3502239313Sdim InnerMatcher) { 3503239313Sdim return (!Node.isNull() && Node->isReferenceType() && 3504239313Sdim InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)); 3505239313Sdim} 3506239313Sdim 3507341825Sdim/// Matches QualTypes whose canonical type matches InnerMatcher. 3508249423Sdim/// 3509249423Sdim/// Given: 3510249423Sdim/// \code 3511249423Sdim/// typedef int &int_ref; 3512249423Sdim/// int a; 3513249423Sdim/// int_ref b = a; 3514296417Sdim/// \endcode 3515249423Sdim/// 3516249423Sdim/// \c varDecl(hasType(qualType(referenceType()))))) will not match the 3517249423Sdim/// declaration of b but \c 3518249423Sdim/// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does. 3519249423SdimAST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>, 3520249423Sdim InnerMatcher) { 3521261991Sdim if (Node.isNull()) 3522261991Sdim return false; 3523249423Sdim return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder); 3524249423Sdim} 3525249423Sdim 3526341825Sdim/// Overloaded to match the referenced type's declaration. 3527261991SdimAST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>, 3528261991Sdim InnerMatcher, 1) { 3529261991Sdim return references(qualType(hasDeclaration(InnerMatcher))) 3530261991Sdim .matches(Node, Finder, Builder); 3531239313Sdim} 3532239313Sdim 3533353358Sdim/// Matches on the implicit object argument of a member call expression. Unlike 3534353358Sdim/// `on`, matches the argument directly without stripping away anything. 3535353358Sdim/// 3536353358Sdim/// Given 3537353358Sdim/// \code 3538353358Sdim/// class Y { public: void m(); }; 3539353358Sdim/// Y g(); 3540353358Sdim/// class X : public Y { void g(); }; 3541353358Sdim/// void z(Y y, X x) { y.m(); x.m(); x.g(); (g()).m(); } 3542353358Sdim/// \endcode 3543353358Sdim/// cxxMemberCallExpr(onImplicitObjectArgument(hasType( 3544353358Sdim/// cxxRecordDecl(hasName("Y"))))) 3545353358Sdim/// matches `y.m()`, `x.m()` and (g()).m(), but not `x.g()`. 3546353358Sdim/// cxxMemberCallExpr(on(callExpr())) 3547353358Sdim/// does not match `(g()).m()`, because the parens are not ignored. 3548353358Sdim/// 3549353358Sdim/// FIXME: Overload to allow directly matching types? 3550239313SdimAST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument, 3551239313Sdim internal::Matcher<Expr>, InnerMatcher) { 3552249423Sdim const Expr *ExprNode = Node.getImplicitObjectArgument(); 3553276479Sdim return (ExprNode != nullptr && 3554239313Sdim InnerMatcher.matches(*ExprNode, Finder, Builder)); 3555239313Sdim} 3556239313Sdim 3557353358Sdim/// Matches if the type of the expression's implicit object argument either 3558353358Sdim/// matches the InnerMatcher, or is a pointer to a type that matches the 3559353358Sdim/// InnerMatcher. 3560353358Sdim/// 3561353358Sdim/// Given 3562353358Sdim/// \code 3563353358Sdim/// class Y { public: void m(); }; 3564353358Sdim/// class X : public Y { void g(); }; 3565353358Sdim/// void z() { Y y; y.m(); Y *p; p->m(); X x; x.m(); x.g(); } 3566353358Sdim/// \endcode 3567353358Sdim/// cxxMemberCallExpr(thisPointerType(hasDeclaration( 3568353358Sdim/// cxxRecordDecl(hasName("Y"))))) 3569353358Sdim/// matches `y.m()`, `p->m()` and `x.m()`. 3570353358Sdim/// cxxMemberCallExpr(thisPointerType(hasDeclaration( 3571353358Sdim/// cxxRecordDecl(hasName("X"))))) 3572353358Sdim/// matches `x.g()`. 3573261991SdimAST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 3574261991Sdim internal::Matcher<QualType>, InnerMatcher, 0) { 3575239313Sdim return onImplicitObjectArgument( 3576261991Sdim anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 3577261991Sdim .matches(Node, Finder, Builder); 3578239313Sdim} 3579239313Sdim 3580341825Sdim/// Overloaded to match the type's declaration. 3581261991SdimAST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType, 3582261991Sdim internal::Matcher<Decl>, InnerMatcher, 1) { 3583239313Sdim return onImplicitObjectArgument( 3584261991Sdim anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher)))) 3585261991Sdim .matches(Node, Finder, Builder); 3586239313Sdim} 3587239313Sdim 3588341825Sdim/// Matches a DeclRefExpr that refers to a declaration that matches the 3589239313Sdim/// specified matcher. 3590239313Sdim/// 3591239313Sdim/// Example matches x in if(x) 3592243830Sdim/// (matcher = declRefExpr(to(varDecl(hasName("x"))))) 3593243830Sdim/// \code 3594239313Sdim/// bool x; 3595239313Sdim/// if (x) {} 3596243830Sdim/// \endcode 3597239313SdimAST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>, 3598239313Sdim InnerMatcher) { 3599239313Sdim const Decl *DeclNode = Node.getDecl(); 3600276479Sdim return (DeclNode != nullptr && 3601239313Sdim InnerMatcher.matches(*DeclNode, Finder, Builder)); 3602239313Sdim} 3603239313Sdim 3604341825Sdim/// Matches a \c DeclRefExpr that refers to a declaration through a 3605239313Sdim/// specific using shadow declaration. 3606239313Sdim/// 3607239313Sdim/// Given 3608243830Sdim/// \code 3609239313Sdim/// namespace a { void f() {} } 3610239313Sdim/// using a::f; 3611239313Sdim/// void g() { 3612239313Sdim/// f(); // Matches this .. 3613239313Sdim/// a::f(); // .. but not this. 3614239313Sdim/// } 3615243830Sdim/// \endcode 3616296417Sdim/// declRefExpr(throughUsingDecl(anything())) 3617239313Sdim/// matches \c f() 3618239313SdimAST_MATCHER_P(DeclRefExpr, throughUsingDecl, 3619243830Sdim internal::Matcher<UsingShadowDecl>, InnerMatcher) { 3620239313Sdim const NamedDecl *FoundDecl = Node.getFoundDecl(); 3621249423Sdim if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl)) 3622243830Sdim return InnerMatcher.matches(*UsingDecl, Finder, Builder); 3623239313Sdim return false; 3624239313Sdim} 3625239313Sdim 3626341825Sdim/// Matches an \c OverloadExpr if any of the declarations in the set of 3627314564Sdim/// overloads matches the given matcher. 3628314564Sdim/// 3629314564Sdim/// Given 3630314564Sdim/// \code 3631314564Sdim/// template <typename T> void foo(T); 3632314564Sdim/// template <typename T> void bar(T); 3633314564Sdim/// template <typename T> void baz(T t) { 3634314564Sdim/// foo(t); 3635314564Sdim/// bar(t); 3636314564Sdim/// } 3637314564Sdim/// \endcode 3638314564Sdim/// unresolvedLookupExpr(hasAnyDeclaration( 3639314564Sdim/// functionTemplateDecl(hasName("foo")))) 3640314564Sdim/// matches \c foo in \c foo(t); but not \c bar in \c bar(t); 3641314564SdimAST_MATCHER_P(OverloadExpr, hasAnyDeclaration, internal::Matcher<Decl>, 3642314564Sdim InnerMatcher) { 3643314564Sdim return matchesFirstInPointerRange(InnerMatcher, Node.decls_begin(), 3644314564Sdim Node.decls_end(), Finder, Builder); 3645314564Sdim} 3646314564Sdim 3647341825Sdim/// Matches the Decl of a DeclStmt which has a single declaration. 3648239462Sdim/// 3649239462Sdim/// Given 3650243830Sdim/// \code 3651239462Sdim/// int a, b; 3652239462Sdim/// int c; 3653243830Sdim/// \endcode 3654243830Sdim/// declStmt(hasSingleDecl(anything())) 3655239462Sdim/// matches 'int c;' but not 'int a, b;'. 3656239462SdimAST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) { 3657239462Sdim if (Node.isSingleDecl()) { 3658239462Sdim const Decl *FoundDecl = Node.getSingleDecl(); 3659239462Sdim return InnerMatcher.matches(*FoundDecl, Finder, Builder); 3660239462Sdim } 3661239462Sdim return false; 3662239462Sdim} 3663239462Sdim 3664341825Sdim/// Matches a variable declaration that has an initializer expression 3665239313Sdim/// that matches the given matcher. 3666239313Sdim/// 3667243830Sdim/// Example matches x (matcher = varDecl(hasInitializer(callExpr()))) 3668243830Sdim/// \code 3669239313Sdim/// bool y() { return true; } 3670239313Sdim/// bool x = y(); 3671243830Sdim/// \endcode 3672239313SdimAST_MATCHER_P( 3673239313Sdim VarDecl, hasInitializer, internal::Matcher<Expr>, 3674239313Sdim InnerMatcher) { 3675239313Sdim const Expr *Initializer = Node.getAnyInitializer(); 3676276479Sdim return (Initializer != nullptr && 3677239313Sdim InnerMatcher.matches(*Initializer, Finder, Builder)); 3678239313Sdim} 3679239313Sdim 3680344779Sdim/// \brief Matches a static variable with local scope. 3681344779Sdim/// 3682344779Sdim/// Example matches y (matcher = varDecl(isStaticLocal())) 3683344779Sdim/// \code 3684344779Sdim/// void f() { 3685344779Sdim/// int x; 3686344779Sdim/// static int y; 3687344779Sdim/// } 3688344779Sdim/// static int z; 3689344779Sdim/// \endcode 3690344779SdimAST_MATCHER(VarDecl, isStaticLocal) { 3691344779Sdim return Node.isStaticLocal(); 3692344779Sdim} 3693344779Sdim 3694341825Sdim/// Matches a variable declaration that has function scope and is a 3695276479Sdim/// non-static local variable. 3696276479Sdim/// 3697276479Sdim/// Example matches x (matcher = varDecl(hasLocalStorage()) 3698276479Sdim/// \code 3699276479Sdim/// void f() { 3700276479Sdim/// int x; 3701276479Sdim/// static int y; 3702276479Sdim/// } 3703276479Sdim/// int z; 3704276479Sdim/// \endcode 3705276479SdimAST_MATCHER(VarDecl, hasLocalStorage) { 3706276479Sdim return Node.hasLocalStorage(); 3707276479Sdim} 3708276479Sdim 3709341825Sdim/// Matches a variable declaration that does not have local storage. 3710276479Sdim/// 3711276479Sdim/// Example matches y and z (matcher = varDecl(hasGlobalStorage()) 3712276479Sdim/// \code 3713276479Sdim/// void f() { 3714276479Sdim/// int x; 3715276479Sdim/// static int y; 3716276479Sdim/// } 3717276479Sdim/// int z; 3718276479Sdim/// \endcode 3719276479SdimAST_MATCHER(VarDecl, hasGlobalStorage) { 3720276479Sdim return Node.hasGlobalStorage(); 3721276479Sdim} 3722276479Sdim 3723341825Sdim/// Matches a variable declaration that has automatic storage duration. 3724296417Sdim/// 3725296417Sdim/// Example matches x, but not y, z, or a. 3726296417Sdim/// (matcher = varDecl(hasAutomaticStorageDuration()) 3727296417Sdim/// \code 3728296417Sdim/// void f() { 3729296417Sdim/// int x; 3730296417Sdim/// static int y; 3731296417Sdim/// thread_local int z; 3732296417Sdim/// } 3733296417Sdim/// int a; 3734296417Sdim/// \endcode 3735296417SdimAST_MATCHER(VarDecl, hasAutomaticStorageDuration) { 3736296417Sdim return Node.getStorageDuration() == SD_Automatic; 3737296417Sdim} 3738296417Sdim 3739341825Sdim/// Matches a variable declaration that has static storage duration. 3740314564Sdim/// It includes the variable declared at namespace scope and those declared 3741314564Sdim/// with "static" and "extern" storage class specifiers. 3742296417Sdim/// 3743296417Sdim/// \code 3744296417Sdim/// void f() { 3745296417Sdim/// int x; 3746296417Sdim/// static int y; 3747296417Sdim/// thread_local int z; 3748296417Sdim/// } 3749296417Sdim/// int a; 3750314564Sdim/// static int b; 3751314564Sdim/// extern int c; 3752314564Sdim/// varDecl(hasStaticStorageDuration()) 3753314564Sdim/// matches the function declaration y, a, b and c. 3754296417Sdim/// \endcode 3755296417SdimAST_MATCHER(VarDecl, hasStaticStorageDuration) { 3756296417Sdim return Node.getStorageDuration() == SD_Static; 3757296417Sdim} 3758296417Sdim 3759341825Sdim/// Matches a variable declaration that has thread storage duration. 3760296417Sdim/// 3761296417Sdim/// Example matches z, but not x, z, or a. 3762296417Sdim/// (matcher = varDecl(hasThreadStorageDuration()) 3763296417Sdim/// \code 3764296417Sdim/// void f() { 3765296417Sdim/// int x; 3766296417Sdim/// static int y; 3767296417Sdim/// thread_local int z; 3768296417Sdim/// } 3769296417Sdim/// int a; 3770296417Sdim/// \endcode 3771296417SdimAST_MATCHER(VarDecl, hasThreadStorageDuration) { 3772296417Sdim return Node.getStorageDuration() == SD_Thread; 3773296417Sdim} 3774296417Sdim 3775341825Sdim/// Matches a variable declaration that is an exception variable from 3776296417Sdim/// a C++ catch block, or an Objective-C \@catch statement. 3777296417Sdim/// 3778296417Sdim/// Example matches x (matcher = varDecl(isExceptionVariable()) 3779296417Sdim/// \code 3780296417Sdim/// void f(int y) { 3781296417Sdim/// try { 3782296417Sdim/// } catch (int x) { 3783296417Sdim/// } 3784296417Sdim/// } 3785296417Sdim/// \endcode 3786296417SdimAST_MATCHER(VarDecl, isExceptionVariable) { 3787296417Sdim return Node.isExceptionVariable(); 3788296417Sdim} 3789296417Sdim 3790341825Sdim/// Checks that a call expression or a constructor call expression has 3791239313Sdim/// a specific number of arguments (including absent default arguments). 3792239313Sdim/// 3793243830Sdim/// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2))) 3794243830Sdim/// \code 3795239313Sdim/// void f(int x, int y); 3796239313Sdim/// f(0, 0); 3797243830Sdim/// \endcode 3798288943SdimAST_POLYMORPHIC_MATCHER_P(argumentCountIs, 3799288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr, 3800288943Sdim CXXConstructExpr, 3801288943Sdim ObjCMessageExpr), 3802261991Sdim unsigned, N) { 3803239313Sdim return Node.getNumArgs() == N; 3804239313Sdim} 3805239313Sdim 3806341825Sdim/// Matches the n'th argument of a call expression or a constructor 3807239313Sdim/// call expression. 3808239313Sdim/// 3809239313Sdim/// Example matches y in x(y) 3810243830Sdim/// (matcher = callExpr(hasArgument(0, declRefExpr()))) 3811243830Sdim/// \code 3812239313Sdim/// void x(int) { int y; x(y); } 3813243830Sdim/// \endcode 3814288943SdimAST_POLYMORPHIC_MATCHER_P2(hasArgument, 3815288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr, 3816288943Sdim CXXConstructExpr, 3817288943Sdim ObjCMessageExpr), 3818288943Sdim unsigned, N, internal::Matcher<Expr>, InnerMatcher) { 3819239313Sdim return (N < Node.getNumArgs() && 3820239313Sdim InnerMatcher.matches( 3821239313Sdim *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); 3822239313Sdim} 3823239313Sdim 3824344779Sdim/// Matches the n'th item of an initializer list expression. 3825344779Sdim/// 3826344779Sdim/// Example matches y. 3827344779Sdim/// (matcher = initListExpr(hasInit(0, expr()))) 3828344779Sdim/// \code 3829344779Sdim/// int x{y}. 3830344779Sdim/// \endcode 3831344779SdimAST_MATCHER_P2(InitListExpr, hasInit, unsigned, N, 3832344779Sdim ast_matchers::internal::Matcher<Expr>, InnerMatcher) { 3833344779Sdim return N < Node.getNumInits() && 3834344779Sdim InnerMatcher.matches(*Node.getInit(N), Finder, Builder); 3835344779Sdim} 3836344779Sdim 3837341825Sdim/// Matches declaration statements that contain a specific number of 3838239462Sdim/// declarations. 3839239462Sdim/// 3840239462Sdim/// Example: Given 3841243830Sdim/// \code 3842239462Sdim/// int a, b; 3843239462Sdim/// int c; 3844239462Sdim/// int d = 2, e; 3845243830Sdim/// \endcode 3846239462Sdim/// declCountIs(2) 3847239462Sdim/// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'. 3848239462SdimAST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) { 3849243830Sdim return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N; 3850239462Sdim} 3851239462Sdim 3852341825Sdim/// Matches the n'th declaration of a declaration statement. 3853239462Sdim/// 3854239462Sdim/// Note that this does not work for global declarations because the AST 3855239462Sdim/// breaks up multiple-declaration DeclStmt's into multiple single-declaration 3856239462Sdim/// DeclStmt's. 3857239462Sdim/// Example: Given non-global declarations 3858243830Sdim/// \code 3859239462Sdim/// int a, b = 0; 3860239462Sdim/// int c; 3861239462Sdim/// int d = 2, e; 3862243830Sdim/// \endcode 3863243830Sdim/// declStmt(containsDeclaration( 3864243830Sdim/// 0, varDecl(hasInitializer(anything())))) 3865239462Sdim/// matches only 'int d = 2, e;', and 3866243830Sdim/// declStmt(containsDeclaration(1, varDecl())) 3867243830Sdim/// \code 3868239462Sdim/// matches 'int a, b = 0' as well as 'int d = 2, e;' 3869239462Sdim/// but 'int c;' is not matched. 3870243830Sdim/// \endcode 3871239462SdimAST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, 3872239462Sdim internal::Matcher<Decl>, InnerMatcher) { 3873239462Sdim const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end()); 3874239462Sdim if (N >= NumDecls) 3875239462Sdim return false; 3876239462Sdim DeclStmt::const_decl_iterator Iterator = Node.decl_begin(); 3877239462Sdim std::advance(Iterator, N); 3878239462Sdim return InnerMatcher.matches(**Iterator, Finder, Builder); 3879239462Sdim} 3880239462Sdim 3881341825Sdim/// Matches a C++ catch statement that has a catch-all handler. 3882288943Sdim/// 3883288943Sdim/// Given 3884288943Sdim/// \code 3885288943Sdim/// try { 3886288943Sdim/// // ... 3887288943Sdim/// } catch (int) { 3888288943Sdim/// // ... 3889288943Sdim/// } catch (...) { 3890288943Sdim/// // ... 3891288943Sdim/// } 3892344779Sdim/// \endcode 3893296417Sdim/// cxxCatchStmt(isCatchAll()) matches catch(...) but not catch(int). 3894288943SdimAST_MATCHER(CXXCatchStmt, isCatchAll) { 3895288943Sdim return Node.getExceptionDecl() == nullptr; 3896288943Sdim} 3897288943Sdim 3898341825Sdim/// Matches a constructor initializer. 3899239313Sdim/// 3900239313Sdim/// Given 3901243830Sdim/// \code 3902239313Sdim/// struct Foo { 3903239313Sdim/// Foo() : foo_(1) { } 3904239313Sdim/// int foo_; 3905239313Sdim/// }; 3906243830Sdim/// \endcode 3907296417Sdim/// cxxRecordDecl(has(cxxConstructorDecl( 3908296417Sdim/// hasAnyConstructorInitializer(anything()) 3909296417Sdim/// ))) 3910239313Sdim/// record matches Foo, hasAnyConstructorInitializer matches foo_(1) 3911239313SdimAST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer, 3912239313Sdim internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 3913261991Sdim return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(), 3914261991Sdim Node.init_end(), Finder, Builder); 3915239313Sdim} 3916239313Sdim 3917341825Sdim/// Matches the field declaration of a constructor initializer. 3918239313Sdim/// 3919239313Sdim/// Given 3920243830Sdim/// \code 3921239313Sdim/// struct Foo { 3922239313Sdim/// Foo() : foo_(1) { } 3923239313Sdim/// int foo_; 3924239313Sdim/// }; 3925243830Sdim/// \endcode 3926296417Sdim/// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer( 3927239313Sdim/// forField(hasName("foo_")))))) 3928239313Sdim/// matches Foo 3929239313Sdim/// with forField matching foo_ 3930239313SdimAST_MATCHER_P(CXXCtorInitializer, forField, 3931239313Sdim internal::Matcher<FieldDecl>, InnerMatcher) { 3932327952Sdim const FieldDecl *NodeAsDecl = Node.getAnyMember(); 3933276479Sdim return (NodeAsDecl != nullptr && 3934239313Sdim InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); 3935239313Sdim} 3936239313Sdim 3937341825Sdim/// Matches the initializer expression of a constructor initializer. 3938239313Sdim/// 3939239313Sdim/// Given 3940243830Sdim/// \code 3941239313Sdim/// struct Foo { 3942239313Sdim/// Foo() : foo_(1) { } 3943239313Sdim/// int foo_; 3944239313Sdim/// }; 3945243830Sdim/// \endcode 3946296417Sdim/// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer( 3947239313Sdim/// withInitializer(integerLiteral(equals(1))))))) 3948239313Sdim/// matches Foo 3949239313Sdim/// with withInitializer matching (1) 3950239313SdimAST_MATCHER_P(CXXCtorInitializer, withInitializer, 3951239313Sdim internal::Matcher<Expr>, InnerMatcher) { 3952239313Sdim const Expr* NodeAsExpr = Node.getInit(); 3953276479Sdim return (NodeAsExpr != nullptr && 3954239313Sdim InnerMatcher.matches(*NodeAsExpr, Finder, Builder)); 3955239313Sdim} 3956239313Sdim 3957341825Sdim/// Matches a constructor initializer if it is explicitly written in 3958239313Sdim/// code (as opposed to implicitly added by the compiler). 3959239313Sdim/// 3960239313Sdim/// Given 3961243830Sdim/// \code 3962239313Sdim/// struct Foo { 3963239313Sdim/// Foo() { } 3964239313Sdim/// Foo(int) : foo_("A") { } 3965239313Sdim/// string foo_; 3966239313Sdim/// }; 3967243830Sdim/// \endcode 3968296417Sdim/// cxxConstructorDecl(hasAnyConstructorInitializer(isWritten())) 3969239313Sdim/// will match Foo(int), but not Foo() 3970239313SdimAST_MATCHER(CXXCtorInitializer, isWritten) { 3971239313Sdim return Node.isWritten(); 3972239313Sdim} 3973239313Sdim 3974341825Sdim/// Matches a constructor initializer if it is initializing a base, as 3975296417Sdim/// opposed to a member. 3976296417Sdim/// 3977296417Sdim/// Given 3978296417Sdim/// \code 3979296417Sdim/// struct B {}; 3980296417Sdim/// struct D : B { 3981296417Sdim/// int I; 3982296417Sdim/// D(int i) : I(i) {} 3983296417Sdim/// }; 3984296417Sdim/// struct E : B { 3985296417Sdim/// E() : B() {} 3986296417Sdim/// }; 3987296417Sdim/// \endcode 3988296417Sdim/// cxxConstructorDecl(hasAnyConstructorInitializer(isBaseInitializer())) 3989296417Sdim/// will match E(), but not match D(int). 3990296417SdimAST_MATCHER(CXXCtorInitializer, isBaseInitializer) { 3991296417Sdim return Node.isBaseInitializer(); 3992296417Sdim} 3993296417Sdim 3994341825Sdim/// Matches a constructor initializer if it is initializing a member, as 3995296417Sdim/// opposed to a base. 3996296417Sdim/// 3997296417Sdim/// Given 3998296417Sdim/// \code 3999296417Sdim/// struct B {}; 4000296417Sdim/// struct D : B { 4001296417Sdim/// int I; 4002296417Sdim/// D(int i) : I(i) {} 4003296417Sdim/// }; 4004296417Sdim/// struct E : B { 4005296417Sdim/// E() : B() {} 4006296417Sdim/// }; 4007296417Sdim/// \endcode 4008296417Sdim/// cxxConstructorDecl(hasAnyConstructorInitializer(isMemberInitializer())) 4009296417Sdim/// will match D(int), but not match E(). 4010296417SdimAST_MATCHER(CXXCtorInitializer, isMemberInitializer) { 4011296417Sdim return Node.isMemberInitializer(); 4012296417Sdim} 4013296417Sdim 4014341825Sdim/// Matches any argument of a call expression or a constructor call 4015341825Sdim/// expression, or an ObjC-message-send expression. 4016239313Sdim/// 4017239313Sdim/// Given 4018243830Sdim/// \code 4019239313Sdim/// void x(int, int, int) { int y; x(1, y, 42); } 4020243830Sdim/// \endcode 4021243830Sdim/// callExpr(hasAnyArgument(declRefExpr())) 4022239313Sdim/// matches x(1, y, 42) 4023239313Sdim/// with hasAnyArgument(...) 4024239313Sdim/// matching y 4025341825Sdim/// 4026341825Sdim/// For ObjectiveC, given 4027341825Sdim/// \code 4028341825Sdim/// @interface I - (void) f:(int) y; @end 4029341825Sdim/// void foo(I *i) { [i f:12]; } 4030341825Sdim/// \endcode 4031341825Sdim/// objcMessageExpr(hasAnyArgument(integerLiteral(equals(12)))) 4032341825Sdim/// matches [i f:12] 4033288943SdimAST_POLYMORPHIC_MATCHER_P(hasAnyArgument, 4034344779Sdim AST_POLYMORPHIC_SUPPORTED_TYPES( 4035344779Sdim CallExpr, CXXConstructExpr, 4036344779Sdim CXXUnresolvedConstructExpr, ObjCMessageExpr), 4037261991Sdim internal::Matcher<Expr>, InnerMatcher) { 4038280031Sdim for (const Expr *Arg : Node.arguments()) { 4039261991Sdim BoundNodesTreeBuilder Result(*Builder); 4040309124Sdim if (InnerMatcher.matches(*Arg, Finder, &Result)) { 4041280031Sdim *Builder = std::move(Result); 4042239313Sdim return true; 4043239313Sdim } 4044239313Sdim } 4045239313Sdim return false; 4046239313Sdim} 4047239313Sdim 4048360784Sdim/// Matches any capture of a lambda expression. 4049360784Sdim/// 4050360784Sdim/// Given 4051360784Sdim/// \code 4052360784Sdim/// void foo() { 4053360784Sdim/// int x; 4054360784Sdim/// auto f = [x](){}; 4055360784Sdim/// } 4056360784Sdim/// \endcode 4057360784Sdim/// lambdaExpr(hasAnyCapture(anything())) 4058360784Sdim/// matches [x](){}; 4059360784SdimAST_MATCHER_P_OVERLOAD(LambdaExpr, hasAnyCapture, internal::Matcher<VarDecl>, 4060360784Sdim InnerMatcher, 0) { 4061360784Sdim for (const LambdaCapture &Capture : Node.captures()) { 4062360784Sdim if (Capture.capturesVariable()) { 4063360784Sdim BoundNodesTreeBuilder Result(*Builder); 4064360784Sdim if (InnerMatcher.matches(*Capture.getCapturedVar(), Finder, &Result)) { 4065360784Sdim *Builder = std::move(Result); 4066360784Sdim return true; 4067360784Sdim } 4068360784Sdim } 4069360784Sdim } 4070360784Sdim return false; 4071360784Sdim} 4072360784Sdim 4073360784Sdim/// Matches any capture of 'this' in a lambda expression. 4074360784Sdim/// 4075360784Sdim/// Given 4076360784Sdim/// \code 4077360784Sdim/// struct foo { 4078360784Sdim/// void bar() { 4079360784Sdim/// auto f = [this](){}; 4080360784Sdim/// } 4081360784Sdim/// } 4082360784Sdim/// \endcode 4083360784Sdim/// lambdaExpr(hasAnyCapture(cxxThisExpr())) 4084360784Sdim/// matches [this](){}; 4085360784SdimAST_MATCHER_P_OVERLOAD(LambdaExpr, hasAnyCapture, 4086360784Sdim internal::Matcher<CXXThisExpr>, InnerMatcher, 1) { 4087360784Sdim return llvm::any_of(Node.captures(), [](const LambdaCapture &LC) { 4088360784Sdim return LC.capturesThis(); 4089360784Sdim }); 4090360784Sdim} 4091360784Sdim 4092341825Sdim/// Matches a constructor call expression which uses list initialization. 4093276479SdimAST_MATCHER(CXXConstructExpr, isListInitialization) { 4094276479Sdim return Node.isListInitialization(); 4095276479Sdim} 4096276479Sdim 4097341825Sdim/// Matches a constructor call expression which requires 4098309124Sdim/// zero initialization. 4099309124Sdim/// 4100309124Sdim/// Given 4101309124Sdim/// \code 4102309124Sdim/// void foo() { 4103309124Sdim/// struct point { double x; double y; }; 4104309124Sdim/// point pt[2] = { { 1.0, 2.0 } }; 4105309124Sdim/// } 4106309124Sdim/// \endcode 4107309124Sdim/// initListExpr(has(cxxConstructExpr(requiresZeroInitialization())) 4108309124Sdim/// will match the implicit array filler for pt[1]. 4109309124SdimAST_MATCHER(CXXConstructExpr, requiresZeroInitialization) { 4110309124Sdim return Node.requiresZeroInitialization(); 4111309124Sdim} 4112309124Sdim 4113341825Sdim/// Matches the n'th parameter of a function or an ObjC method 4114341825Sdim/// declaration or a block. 4115239313Sdim/// 4116239313Sdim/// Given 4117243830Sdim/// \code 4118239313Sdim/// class X { void f(int x) {} }; 4119243830Sdim/// \endcode 4120296417Sdim/// cxxMethodDecl(hasParameter(0, hasType(varDecl()))) 4121239313Sdim/// matches f(int x) {} 4122239313Sdim/// with hasParameter(...) 4123239313Sdim/// matching int x 4124341825Sdim/// 4125341825Sdim/// For ObjectiveC, given 4126341825Sdim/// \code 4127341825Sdim/// @interface I - (void) f:(int) y; @end 4128341825Sdim/// \endcode 4129341825Sdim// 4130341825Sdim/// the matcher objcMethodDecl(hasParameter(0, hasName("y"))) 4131341825Sdim/// matches the declaration of method f with hasParameter 4132341825Sdim/// matching y. 4133341825SdimAST_POLYMORPHIC_MATCHER_P2(hasParameter, 4134341825Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4135341825Sdim ObjCMethodDecl, 4136341825Sdim BlockDecl), 4137341825Sdim unsigned, N, internal::Matcher<ParmVarDecl>, 4138341825Sdim InnerMatcher) { 4139341825Sdim return (N < Node.parameters().size() 4140341825Sdim && InnerMatcher.matches(*Node.parameters()[N], Finder, Builder)); 4141239313Sdim} 4142239313Sdim 4143341825Sdim/// Matches all arguments and their respective ParmVarDecl. 4144309124Sdim/// 4145309124Sdim/// Given 4146309124Sdim/// \code 4147309124Sdim/// void f(int i); 4148309124Sdim/// int y; 4149309124Sdim/// f(y); 4150309124Sdim/// \endcode 4151309124Sdim/// callExpr( 4152309124Sdim/// forEachArgumentWithParam( 4153309124Sdim/// declRefExpr(to(varDecl(hasName("y")))), 4154309124Sdim/// parmVarDecl(hasType(isInteger())) 4155309124Sdim/// )) 4156309124Sdim/// matches f(y); 4157309124Sdim/// with declRefExpr(...) 4158309124Sdim/// matching int y 4159309124Sdim/// and parmVarDecl(...) 4160309124Sdim/// matching int i 4161309124SdimAST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam, 4162309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr, 4163309124Sdim CXXConstructExpr), 4164309124Sdim internal::Matcher<Expr>, ArgMatcher, 4165309124Sdim internal::Matcher<ParmVarDecl>, ParamMatcher) { 4166309124Sdim BoundNodesTreeBuilder Result; 4167309124Sdim // The first argument of an overloaded member operator is the implicit object 4168309124Sdim // argument of the method which should not be matched against a parameter, so 4169309124Sdim // we skip over it here. 4170309124Sdim BoundNodesTreeBuilder Matches; 4171309124Sdim unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl())) 4172309124Sdim .matches(Node, Finder, &Matches) 4173309124Sdim ? 1 4174309124Sdim : 0; 4175309124Sdim int ParamIndex = 0; 4176309124Sdim bool Matched = false; 4177309124Sdim for (; ArgIndex < Node.getNumArgs(); ++ArgIndex) { 4178309124Sdim BoundNodesTreeBuilder ArgMatches(*Builder); 4179309124Sdim if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()), 4180309124Sdim Finder, &ArgMatches)) { 4181309124Sdim BoundNodesTreeBuilder ParamMatches(ArgMatches); 4182309124Sdim if (expr(anyOf(cxxConstructExpr(hasDeclaration(cxxConstructorDecl( 4183309124Sdim hasParameter(ParamIndex, ParamMatcher)))), 4184309124Sdim callExpr(callee(functionDecl( 4185309124Sdim hasParameter(ParamIndex, ParamMatcher)))))) 4186309124Sdim .matches(Node, Finder, &ParamMatches)) { 4187309124Sdim Result.addMatch(ParamMatches); 4188309124Sdim Matched = true; 4189309124Sdim } 4190309124Sdim } 4191309124Sdim ++ParamIndex; 4192309124Sdim } 4193309124Sdim *Builder = std::move(Result); 4194309124Sdim return Matched; 4195309124Sdim} 4196309124Sdim 4197341825Sdim/// Matches any parameter of a function or an ObjC method declaration or a 4198341825Sdim/// block. 4199239313Sdim/// 4200239313Sdim/// Does not match the 'this' parameter of a method. 4201239313Sdim/// 4202239313Sdim/// Given 4203243830Sdim/// \code 4204239313Sdim/// class X { void f(int x, int y, int z) {} }; 4205243830Sdim/// \endcode 4206296417Sdim/// cxxMethodDecl(hasAnyParameter(hasName("y"))) 4207239313Sdim/// matches f(int x, int y, int z) {} 4208239313Sdim/// with hasAnyParameter(...) 4209239313Sdim/// matching int y 4210341825Sdim/// 4211341825Sdim/// For ObjectiveC, given 4212341825Sdim/// \code 4213341825Sdim/// @interface I - (void) f:(int) y; @end 4214341825Sdim/// \endcode 4215341825Sdim// 4216341825Sdim/// the matcher objcMethodDecl(hasAnyParameter(hasName("y"))) 4217341825Sdim/// matches the declaration of method f with hasParameter 4218341825Sdim/// matching y. 4219341825Sdim/// 4220341825Sdim/// For blocks, given 4221341825Sdim/// \code 4222341825Sdim/// b = ^(int y) { printf("%d", y) }; 4223341825Sdim/// \endcode 4224341825Sdim/// 4225341825Sdim/// the matcher blockDecl(hasAnyParameter(hasName("y"))) 4226341825Sdim/// matches the declaration of the block b with hasParameter 4227341825Sdim/// matching y. 4228341825SdimAST_POLYMORPHIC_MATCHER_P(hasAnyParameter, 4229341825Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4230341825Sdim ObjCMethodDecl, 4231341825Sdim BlockDecl), 4232341825Sdim internal::Matcher<ParmVarDecl>, 4233341825Sdim InnerMatcher) { 4234261991Sdim return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(), 4235261991Sdim Node.param_end(), Finder, Builder); 4236239313Sdim} 4237239313Sdim 4238341825Sdim/// Matches \c FunctionDecls and \c FunctionProtoTypes that have a 4239309124Sdim/// specific parameter count. 4240249423Sdim/// 4241249423Sdim/// Given 4242249423Sdim/// \code 4243249423Sdim/// void f(int i) {} 4244249423Sdim/// void g(int i, int j) {} 4245309124Sdim/// void h(int i, int j); 4246309124Sdim/// void j(int i); 4247309124Sdim/// void k(int x, int y, int z, ...); 4248249423Sdim/// \endcode 4249249423Sdim/// functionDecl(parameterCountIs(2)) 4250341825Sdim/// matches \c g and \c h 4251309124Sdim/// functionProtoType(parameterCountIs(2)) 4252341825Sdim/// matches \c g and \c h 4253309124Sdim/// functionProtoType(parameterCountIs(3)) 4254341825Sdim/// matches \c k 4255309124SdimAST_POLYMORPHIC_MATCHER_P(parameterCountIs, 4256309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4257309124Sdim FunctionProtoType), 4258309124Sdim unsigned, N) { 4259249423Sdim return Node.getNumParams() == N; 4260249423Sdim} 4261249423Sdim 4262341825Sdim/// Matches \c FunctionDecls that have a noreturn attribute. 4263239313Sdim/// 4264341825Sdim/// Given 4265341825Sdim/// \code 4266341825Sdim/// void nope(); 4267341825Sdim/// [[noreturn]] void a(); 4268341825Sdim/// __attribute__((noreturn)) void b(); 4269341825Sdim/// struct c { [[noreturn]] c(); }; 4270341825Sdim/// \endcode 4271341825Sdim/// functionDecl(isNoReturn()) 4272341825Sdim/// matches all of those except 4273341825Sdim/// \code 4274341825Sdim/// void nope(); 4275341825Sdim/// \endcode 4276341825SdimAST_MATCHER(FunctionDecl, isNoReturn) { return Node.isNoReturn(); } 4277341825Sdim 4278341825Sdim/// Matches the return type of a function declaration. 4279341825Sdim/// 4280239313Sdim/// Given: 4281243830Sdim/// \code 4282239313Sdim/// class X { int f() { return 1; } }; 4283243830Sdim/// \endcode 4284296417Sdim/// cxxMethodDecl(returns(asString("int"))) 4285239313Sdim/// matches int f() { return 1; } 4286243830SdimAST_MATCHER_P(FunctionDecl, returns, 4287243830Sdim internal::Matcher<QualType>, InnerMatcher) { 4288276479Sdim return InnerMatcher.matches(Node.getReturnType(), Finder, Builder); 4289239313Sdim} 4290239313Sdim 4291341825Sdim/// Matches extern "C" function or variable declarations. 4292239462Sdim/// 4293239462Sdim/// Given: 4294243830Sdim/// \code 4295239462Sdim/// extern "C" void f() {} 4296239462Sdim/// extern "C" { void g() {} } 4297239462Sdim/// void h() {} 4298327952Sdim/// extern "C" int x = 1; 4299327952Sdim/// extern "C" int y = 2; 4300327952Sdim/// int z = 3; 4301243830Sdim/// \endcode 4302243830Sdim/// functionDecl(isExternC()) 4303327952Sdim/// matches the declaration of f and g, but not the declaration of h. 4304327952Sdim/// varDecl(isExternC()) 4305327952Sdim/// matches the declaration of x and y, but not the declaration of z. 4306314564SdimAST_POLYMORPHIC_MATCHER(isExternC, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4307314564Sdim VarDecl)) { 4308239462Sdim return Node.isExternC(); 4309239462Sdim} 4310239462Sdim 4311341825Sdim/// Matches variable/function declarations that have "static" storage 4312314564Sdim/// class specifier ("static" keyword) written in the source. 4313314564Sdim/// 4314314564Sdim/// Given: 4315314564Sdim/// \code 4316314564Sdim/// static void f() {} 4317314564Sdim/// static int i = 0; 4318314564Sdim/// extern int j; 4319314564Sdim/// int k; 4320314564Sdim/// \endcode 4321314564Sdim/// functionDecl(isStaticStorageClass()) 4322314564Sdim/// matches the function declaration f. 4323314564Sdim/// varDecl(isStaticStorageClass()) 4324314564Sdim/// matches the variable declaration i. 4325314564SdimAST_POLYMORPHIC_MATCHER(isStaticStorageClass, 4326314564Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4327314564Sdim VarDecl)) { 4328314564Sdim return Node.getStorageClass() == SC_Static; 4329314564Sdim} 4330314564Sdim 4331341825Sdim/// Matches deleted function declarations. 4332280031Sdim/// 4333280031Sdim/// Given: 4334280031Sdim/// \code 4335280031Sdim/// void Func(); 4336280031Sdim/// void DeletedFunc() = delete; 4337280031Sdim/// \endcode 4338280031Sdim/// functionDecl(isDeleted()) 4339280031Sdim/// matches the declaration of DeletedFunc, but not Func. 4340280031SdimAST_MATCHER(FunctionDecl, isDeleted) { 4341280031Sdim return Node.isDeleted(); 4342280031Sdim} 4343280031Sdim 4344341825Sdim/// Matches defaulted function declarations. 4345309124Sdim/// 4346309124Sdim/// Given: 4347309124Sdim/// \code 4348309124Sdim/// class A { ~A(); }; 4349309124Sdim/// class B { ~B() = default; }; 4350309124Sdim/// \endcode 4351309124Sdim/// functionDecl(isDefaulted()) 4352309124Sdim/// matches the declaration of ~B, but not ~A. 4353309124SdimAST_MATCHER(FunctionDecl, isDefaulted) { 4354309124Sdim return Node.isDefaulted(); 4355309124Sdim} 4356309124Sdim 4357341825Sdim/// Matches functions that have a dynamic exception specification. 4358309124Sdim/// 4359309124Sdim/// Given: 4360309124Sdim/// \code 4361309124Sdim/// void f(); 4362309124Sdim/// void g() noexcept; 4363309124Sdim/// void h() noexcept(true); 4364309124Sdim/// void i() noexcept(false); 4365309124Sdim/// void j() throw(); 4366309124Sdim/// void k() throw(int); 4367309124Sdim/// void l() throw(...); 4368309124Sdim/// \endcode 4369309124Sdim/// functionDecl(hasDynamicExceptionSpec()) and 4370309124Sdim/// functionProtoType(hasDynamicExceptionSpec()) 4371309124Sdim/// match the declarations of j, k, and l, but not f, g, h, or i. 4372309124SdimAST_POLYMORPHIC_MATCHER(hasDynamicExceptionSpec, 4373309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4374309124Sdim FunctionProtoType)) { 4375309124Sdim if (const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node)) 4376309124Sdim return FnTy->hasDynamicExceptionSpec(); 4377309124Sdim return false; 4378309124Sdim} 4379309124Sdim 4380341825Sdim/// Matches functions that have a non-throwing exception specification. 4381296417Sdim/// 4382296417Sdim/// Given: 4383296417Sdim/// \code 4384296417Sdim/// void f(); 4385296417Sdim/// void g() noexcept; 4386296417Sdim/// void h() throw(); 4387296417Sdim/// void i() throw(int); 4388296417Sdim/// void j() noexcept(false); 4389296417Sdim/// \endcode 4390309124Sdim/// functionDecl(isNoThrow()) and functionProtoType(isNoThrow()) 4391309124Sdim/// match the declarations of g, and h, but not f, i or j. 4392309124SdimAST_POLYMORPHIC_MATCHER(isNoThrow, 4393309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, 4394309124Sdim FunctionProtoType)) { 4395309124Sdim const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node); 4396296417Sdim 4397296417Sdim // If the function does not have a prototype, then it is assumed to be a 4398296417Sdim // throwing function (as it would if the function did not have any exception 4399296417Sdim // specification). 4400296417Sdim if (!FnTy) 4401296417Sdim return false; 4402296417Sdim 4403296417Sdim // Assume the best for any unresolved exception specification. 4404296417Sdim if (isUnresolvedExceptionSpec(FnTy->getExceptionSpecType())) 4405296417Sdim return true; 4406296417Sdim 4407341825Sdim return FnTy->isNothrow(); 4408296417Sdim} 4409296417Sdim 4410341825Sdim/// Matches constexpr variable and function declarations, 4411341825Sdim/// and if constexpr. 4412288943Sdim/// 4413288943Sdim/// Given: 4414288943Sdim/// \code 4415288943Sdim/// constexpr int foo = 42; 4416288943Sdim/// constexpr int bar(); 4417341825Sdim/// void baz() { if constexpr(1 > 0) {} } 4418288943Sdim/// \endcode 4419288943Sdim/// varDecl(isConstexpr()) 4420288943Sdim/// matches the declaration of foo. 4421288943Sdim/// functionDecl(isConstexpr()) 4422288943Sdim/// matches the declaration of bar. 4423341825Sdim/// ifStmt(isConstexpr()) 4424341825Sdim/// matches the if statement in baz. 4425288943SdimAST_POLYMORPHIC_MATCHER(isConstexpr, 4426288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(VarDecl, 4427341825Sdim FunctionDecl, 4428341825Sdim IfStmt)) { 4429288943Sdim return Node.isConstexpr(); 4430288943Sdim} 4431288943Sdim 4432360784Sdim/// Matches selection statements with initializer. 4433360784Sdim/// 4434360784Sdim/// Given: 4435360784Sdim/// \code 4436360784Sdim/// void foo() { 4437360784Sdim/// if (int i = foobar(); i > 0) {} 4438360784Sdim/// switch (int i = foobar(); i) {} 4439360784Sdim/// for (auto& a = get_range(); auto& x : a) {} 4440360784Sdim/// } 4441360784Sdim/// void bar() { 4442360784Sdim/// if (foobar() > 0) {} 4443360784Sdim/// switch (foobar()) {} 4444360784Sdim/// for (auto& x : get_range()) {} 4445360784Sdim/// } 4446360784Sdim/// \endcode 4447360784Sdim/// ifStmt(hasInitStatement(anything())) 4448360784Sdim/// matches the if statement in foo but not in bar. 4449360784Sdim/// switchStmt(hasInitStatement(anything())) 4450360784Sdim/// matches the switch statement in foo but not in bar. 4451360784Sdim/// cxxForRangeStmt(hasInitStatement(anything())) 4452360784Sdim/// matches the range for statement in foo but not in bar. 4453360784SdimAST_POLYMORPHIC_MATCHER_P(hasInitStatement, 4454360784Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, SwitchStmt, 4455360784Sdim CXXForRangeStmt), 4456360784Sdim internal::Matcher<Stmt>, InnerMatcher) { 4457360784Sdim const Stmt *Init = Node.getInit(); 4458360784Sdim return Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder); 4459360784Sdim} 4460360784Sdim 4461341825Sdim/// Matches the condition expression of an if statement, for loop, 4462309124Sdim/// switch statement or conditional operator. 4463239313Sdim/// 4464296417Sdim/// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true)))) 4465243830Sdim/// \code 4466239313Sdim/// if (true) {} 4467243830Sdim/// \endcode 4468309124SdimAST_POLYMORPHIC_MATCHER_P( 4469309124Sdim hasCondition, 4470309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, ForStmt, WhileStmt, DoStmt, 4471309124Sdim SwitchStmt, AbstractConditionalOperator), 4472309124Sdim internal::Matcher<Expr>, InnerMatcher) { 4473239313Sdim const Expr *const Condition = Node.getCond(); 4474276479Sdim return (Condition != nullptr && 4475239313Sdim InnerMatcher.matches(*Condition, Finder, Builder)); 4476239313Sdim} 4477239313Sdim 4478341825Sdim/// Matches the then-statement of an if statement. 4479276479Sdim/// 4480276479Sdim/// Examples matches the if statement 4481296417Sdim/// (matcher = ifStmt(hasThen(cxxBoolLiteral(equals(true))))) 4482276479Sdim/// \code 4483276479Sdim/// if (false) true; else false; 4484276479Sdim/// \endcode 4485276479SdimAST_MATCHER_P(IfStmt, hasThen, internal::Matcher<Stmt>, InnerMatcher) { 4486276479Sdim const Stmt *const Then = Node.getThen(); 4487276479Sdim return (Then != nullptr && InnerMatcher.matches(*Then, Finder, Builder)); 4488276479Sdim} 4489261991Sdim 4490341825Sdim/// Matches the else-statement of an if statement. 4491276479Sdim/// 4492276479Sdim/// Examples matches the if statement 4493296417Sdim/// (matcher = ifStmt(hasElse(cxxBoolLiteral(equals(true))))) 4494276479Sdim/// \code 4495276479Sdim/// if (false) false; else true; 4496276479Sdim/// \endcode 4497276479SdimAST_MATCHER_P(IfStmt, hasElse, internal::Matcher<Stmt>, InnerMatcher) { 4498276479Sdim const Stmt *const Else = Node.getElse(); 4499276479Sdim return (Else != nullptr && InnerMatcher.matches(*Else, Finder, Builder)); 4500276479Sdim} 4501276479Sdim 4502341825Sdim/// Matches if a node equals a previously bound node. 4503261991Sdim/// 4504261991Sdim/// Matches a node if it equals the node previously bound to \p ID. 4505261991Sdim/// 4506261991Sdim/// Given 4507261991Sdim/// \code 4508261991Sdim/// class X { int a; int b; }; 4509261991Sdim/// \endcode 4510296417Sdim/// cxxRecordDecl( 4511261991Sdim/// has(fieldDecl(hasName("a"), hasType(type().bind("t")))), 4512261991Sdim/// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))) 4513261991Sdim/// matches the class \c X, as \c a and \c b have the same type. 4514261991Sdim/// 4515261991Sdim/// Note that when multiple matches are involved via \c forEach* matchers, 4516261991Sdim/// \c equalsBoundNodes acts as a filter. 4517261991Sdim/// For example: 4518261991Sdim/// compoundStmt( 4519261991Sdim/// forEachDescendant(varDecl().bind("d")), 4520261991Sdim/// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))) 4521261991Sdim/// will trigger a match for each combination of variable declaration 4522261991Sdim/// and reference to that variable declaration within a compound statement. 4523288943SdimAST_POLYMORPHIC_MATCHER_P(equalsBoundNode, 4524288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl, Type, 4525288943Sdim QualType), 4526261991Sdim std::string, ID) { 4527261991Sdim // FIXME: Figure out whether it makes sense to allow this 4528261991Sdim // on any other node types. 4529261991Sdim // For *Loc it probably does not make sense, as those seem 4530261991Sdim // unique. For NestedNameSepcifier it might make sense, as 4531261991Sdim // those also have pointer identity, but I'm not sure whether 4532261991Sdim // they're ever reused. 4533261991Sdim internal::NotEqualsBoundNodePredicate Predicate; 4534261991Sdim Predicate.ID = ID; 4535261991Sdim Predicate.Node = ast_type_traits::DynTypedNode::create(Node); 4536261991Sdim return Builder->removeBindings(Predicate); 4537261991Sdim} 4538261991Sdim 4539341825Sdim/// Matches the condition variable statement in an if statement. 4540239313Sdim/// 4541239313Sdim/// Given 4542243830Sdim/// \code 4543239313Sdim/// if (A* a = GetAPointer()) {} 4544243830Sdim/// \endcode 4545261991Sdim/// hasConditionVariableStatement(...) 4546239313Sdim/// matches 'A* a = GetAPointer()'. 4547239313SdimAST_MATCHER_P(IfStmt, hasConditionVariableStatement, 4548239313Sdim internal::Matcher<DeclStmt>, InnerMatcher) { 4549239313Sdim const DeclStmt* const DeclarationStatement = 4550239313Sdim Node.getConditionVariableDeclStmt(); 4551276479Sdim return DeclarationStatement != nullptr && 4552239313Sdim InnerMatcher.matches(*DeclarationStatement, Finder, Builder); 4553239313Sdim} 4554239313Sdim 4555341825Sdim/// Matches the index expression of an array subscript expression. 4556239313Sdim/// 4557239313Sdim/// Given 4558243830Sdim/// \code 4559239313Sdim/// int i[5]; 4560239313Sdim/// void f() { i[1] = 42; } 4561243830Sdim/// \endcode 4562239313Sdim/// arraySubscriptExpression(hasIndex(integerLiteral())) 4563239313Sdim/// matches \c i[1] with the \c integerLiteral() matching \c 1 4564239313SdimAST_MATCHER_P(ArraySubscriptExpr, hasIndex, 4565243830Sdim internal::Matcher<Expr>, InnerMatcher) { 4566239313Sdim if (const Expr* Expression = Node.getIdx()) 4567243830Sdim return InnerMatcher.matches(*Expression, Finder, Builder); 4568239313Sdim return false; 4569239313Sdim} 4570239313Sdim 4571341825Sdim/// Matches the base expression of an array subscript expression. 4572239313Sdim/// 4573239313Sdim/// Given 4574243830Sdim/// \code 4575239313Sdim/// int i[5]; 4576239313Sdim/// void f() { i[1] = 42; } 4577243830Sdim/// \endcode 4578243830Sdim/// arraySubscriptExpression(hasBase(implicitCastExpr( 4579243830Sdim/// hasSourceExpression(declRefExpr())))) 4580243830Sdim/// matches \c i[1] with the \c declRefExpr() matching \c i 4581239313SdimAST_MATCHER_P(ArraySubscriptExpr, hasBase, 4582243830Sdim internal::Matcher<Expr>, InnerMatcher) { 4583239313Sdim if (const Expr* Expression = Node.getBase()) 4584243830Sdim return InnerMatcher.matches(*Expression, Finder, Builder); 4585239313Sdim return false; 4586239313Sdim} 4587239313Sdim 4588341825Sdim/// Matches a 'for', 'while', 'do while' statement or a function 4589309124Sdim/// definition that has a given body. 4590239313Sdim/// 4591239313Sdim/// Given 4592243830Sdim/// \code 4593239313Sdim/// for (;;) {} 4594243830Sdim/// \endcode 4595243830Sdim/// hasBody(compoundStmt()) 4596239313Sdim/// matches 'for (;;) {}' 4597243830Sdim/// with compoundStmt() 4598239313Sdim/// matching '{}' 4599276479SdimAST_POLYMORPHIC_MATCHER_P(hasBody, 4600288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(DoStmt, ForStmt, 4601288943Sdim WhileStmt, 4602309124Sdim CXXForRangeStmt, 4603309124Sdim FunctionDecl), 4604276479Sdim internal::Matcher<Stmt>, InnerMatcher) { 4605309124Sdim const Stmt *const Statement = internal::GetBodyMatcher<NodeType>::get(Node); 4606276479Sdim return (Statement != nullptr && 4607239313Sdim InnerMatcher.matches(*Statement, Finder, Builder)); 4608239313Sdim} 4609239313Sdim 4610341825Sdim/// Matches compound statements where at least one substatement matches 4611309124Sdim/// a given matcher. Also matches StmtExprs that have CompoundStmt as children. 4612239313Sdim/// 4613239313Sdim/// Given 4614243830Sdim/// \code 4615239313Sdim/// { {}; 1+2; } 4616243830Sdim/// \endcode 4617243830Sdim/// hasAnySubstatement(compoundStmt()) 4618239313Sdim/// matches '{ {}; 1+2; }' 4619243830Sdim/// with compoundStmt() 4620239313Sdim/// matching '{}' 4621309124SdimAST_POLYMORPHIC_MATCHER_P(hasAnySubstatement, 4622309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CompoundStmt, 4623309124Sdim StmtExpr), 4624309124Sdim internal::Matcher<Stmt>, InnerMatcher) { 4625309124Sdim const CompoundStmt *CS = CompoundStmtMatcher<NodeType>::get(Node); 4626309124Sdim return CS && matchesFirstInPointerRange(InnerMatcher, CS->body_begin(), 4627309124Sdim CS->body_end(), Finder, Builder); 4628239313Sdim} 4629239313Sdim 4630341825Sdim/// Checks that a compound statement contains a specific number of 4631239313Sdim/// child statements. 4632239313Sdim/// 4633239313Sdim/// Example: Given 4634243830Sdim/// \code 4635239313Sdim/// { for (;;) {} } 4636243830Sdim/// \endcode 4637243830Sdim/// compoundStmt(statementCountIs(0))) 4638239313Sdim/// matches '{}' 4639239313Sdim/// but does not match the outer compound statement. 4640239313SdimAST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { 4641239313Sdim return Node.size() == N; 4642239313Sdim} 4643239313Sdim 4644341825Sdim/// Matches literals that are equal to the given value of type ValueT. 4645239313Sdim/// 4646321369Sdim/// Given 4647243830Sdim/// \code 4648321369Sdim/// f('\0', false, 3.14, 42); 4649243830Sdim/// \endcode 4650321369Sdim/// characterLiteral(equals(0)) 4651321369Sdim/// matches '\0' 4652321369Sdim/// cxxBoolLiteral(equals(false)) and cxxBoolLiteral(equals(0)) 4653321369Sdim/// match false 4654321369Sdim/// floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2)) 4655321369Sdim/// match 3.14 4656321369Sdim/// integerLiteral(equals(42)) 4657321369Sdim/// matches 42 4658239313Sdim/// 4659321369Sdim/// Note that you cannot directly match a negative numeric literal because the 4660321369Sdim/// minus sign is not part of the literal: It is a unary operator whose operand 4661321369Sdim/// is the positive numeric literal. Instead, you must use a unaryOperator() 4662321369Sdim/// matcher to match the minus sign: 4663321369Sdim/// 4664321369Sdim/// unaryOperator(hasOperatorName("-"), 4665321369Sdim/// hasUnaryOperand(integerLiteral(equals(13)))) 4666321369Sdim/// 4667321369Sdim/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteralExpr>, 4668239313Sdim/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral> 4669239313Sdimtemplate <typename ValueT> 4670239313Sdiminternal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT> 4671239313Sdimequals(const ValueT &Value) { 4672239313Sdim return internal::PolymorphicMatcherWithParam1< 4673239313Sdim internal::ValueEqualsMatcher, 4674239313Sdim ValueT>(Value); 4675239313Sdim} 4676239313Sdim 4677321369SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals, 4678321369Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral, 4679321369Sdim CXXBoolLiteralExpr, 4680321369Sdim IntegerLiteral), 4681321369Sdim bool, Value, 0) { 4682321369Sdim return internal::ValueEqualsMatcher<NodeType, ParamT>(Value) 4683321369Sdim .matchesNode(Node); 4684321369Sdim} 4685321369Sdim 4686321369SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals, 4687321369Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral, 4688321369Sdim CXXBoolLiteralExpr, 4689321369Sdim IntegerLiteral), 4690321369Sdim unsigned, Value, 1) { 4691321369Sdim return internal::ValueEqualsMatcher<NodeType, ParamT>(Value) 4692321369Sdim .matchesNode(Node); 4693321369Sdim} 4694321369Sdim 4695321369SdimAST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals, 4696321369Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral, 4697321369Sdim CXXBoolLiteralExpr, 4698321369Sdim FloatingLiteral, 4699321369Sdim IntegerLiteral), 4700321369Sdim double, Value, 2) { 4701321369Sdim return internal::ValueEqualsMatcher<NodeType, ParamT>(Value) 4702321369Sdim .matchesNode(Node); 4703321369Sdim} 4704321369Sdim 4705341825Sdim/// Matches the operator Name of operator expressions (binary or 4706239313Sdim/// unary). 4707239313Sdim/// 4708239313Sdim/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) 4709243830Sdim/// \code 4710239313Sdim/// !(a || b) 4711243830Sdim/// \endcode 4712288943SdimAST_POLYMORPHIC_MATCHER_P(hasOperatorName, 4713288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, 4714288943Sdim UnaryOperator), 4715261991Sdim std::string, Name) { 4716239313Sdim return Name == Node.getOpcodeStr(Node.getOpcode()); 4717239313Sdim} 4718239313Sdim 4719341825Sdim/// Matches all kinds of assignment operators. 4720239313Sdim/// 4721341825Sdim/// Example 1: matches a += b (matcher = binaryOperator(isAssignmentOperator())) 4722341825Sdim/// \code 4723341825Sdim/// if (a == b) 4724341825Sdim/// a += b; 4725341825Sdim/// \endcode 4726341825Sdim/// 4727341825Sdim/// Example 2: matches s1 = s2 4728341825Sdim/// (matcher = cxxOperatorCallExpr(isAssignmentOperator())) 4729341825Sdim/// \code 4730341825Sdim/// struct S { S& operator=(const S&); }; 4731341825Sdim/// void x() { S s1, s2; s1 = s2; }) 4732341825Sdim/// \endcode 4733341825SdimAST_POLYMORPHIC_MATCHER(isAssignmentOperator, 4734341825Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, 4735341825Sdim CXXOperatorCallExpr)) { 4736341825Sdim return Node.isAssignmentOp(); 4737341825Sdim} 4738341825Sdim 4739341825Sdim/// Matches the left hand side of binary operator expressions. 4740341825Sdim/// 4741239313Sdim/// Example matches a (matcher = binaryOperator(hasLHS())) 4742243830Sdim/// \code 4743239313Sdim/// a || b 4744243830Sdim/// \endcode 4745296417SdimAST_POLYMORPHIC_MATCHER_P(hasLHS, 4746296417Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, 4747296417Sdim ArraySubscriptExpr), 4748296417Sdim internal::Matcher<Expr>, InnerMatcher) { 4749296417Sdim const Expr *LeftHandSide = Node.getLHS(); 4750276479Sdim return (LeftHandSide != nullptr && 4751239313Sdim InnerMatcher.matches(*LeftHandSide, Finder, Builder)); 4752239313Sdim} 4753239313Sdim 4754341825Sdim/// Matches the right hand side of binary operator expressions. 4755239313Sdim/// 4756239313Sdim/// Example matches b (matcher = binaryOperator(hasRHS())) 4757243830Sdim/// \code 4758239313Sdim/// a || b 4759243830Sdim/// \endcode 4760296417SdimAST_POLYMORPHIC_MATCHER_P(hasRHS, 4761296417Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, 4762296417Sdim ArraySubscriptExpr), 4763296417Sdim internal::Matcher<Expr>, InnerMatcher) { 4764296417Sdim const Expr *RightHandSide = Node.getRHS(); 4765276479Sdim return (RightHandSide != nullptr && 4766239313Sdim InnerMatcher.matches(*RightHandSide, Finder, Builder)); 4767239313Sdim} 4768239313Sdim 4769341825Sdim/// Matches if either the left hand side or the right hand side of a 4770239313Sdim/// binary operator matches. 4771239313Sdiminline internal::Matcher<BinaryOperator> hasEitherOperand( 4772239313Sdim const internal::Matcher<Expr> &InnerMatcher) { 4773239313Sdim return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)); 4774239313Sdim} 4775239313Sdim 4776341825Sdim/// Matches if the operand of a unary operator matches. 4777239313Sdim/// 4778296417Sdim/// Example matches true (matcher = hasUnaryOperand( 4779296417Sdim/// cxxBoolLiteral(equals(true)))) 4780243830Sdim/// \code 4781239313Sdim/// !true 4782243830Sdim/// \endcode 4783239313SdimAST_MATCHER_P(UnaryOperator, hasUnaryOperand, 4784239313Sdim internal::Matcher<Expr>, InnerMatcher) { 4785239313Sdim const Expr * const Operand = Node.getSubExpr(); 4786276479Sdim return (Operand != nullptr && 4787239313Sdim InnerMatcher.matches(*Operand, Finder, Builder)); 4788239313Sdim} 4789239313Sdim 4790341825Sdim/// Matches if the cast's source expression 4791309124Sdim/// or opaque value's source expression matches the given matcher. 4792239313Sdim/// 4793309124Sdim/// Example 1: matches "a string" 4794309124Sdim/// (matcher = castExpr(hasSourceExpression(cxxConstructExpr()))) 4795243830Sdim/// \code 4796239313Sdim/// class URL { URL(string); }; 4797239313Sdim/// URL url = "a string"; 4798296417Sdim/// \endcode 4799309124Sdim/// 4800309124Sdim/// Example 2: matches 'b' (matcher = 4801309124Sdim/// opaqueValueExpr(hasSourceExpression(implicitCastExpr(declRefExpr()))) 4802309124Sdim/// \code 4803309124Sdim/// int a = b ?: 1; 4804309124Sdim/// \endcode 4805309124SdimAST_POLYMORPHIC_MATCHER_P(hasSourceExpression, 4806309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CastExpr, 4807309124Sdim OpaqueValueExpr), 4808309124Sdim internal::Matcher<Expr>, InnerMatcher) { 4809309124Sdim const Expr *const SubExpression = 4810309124Sdim internal::GetSourceExpressionMatcher<NodeType>::get(Node); 4811276479Sdim return (SubExpression != nullptr && 4812239313Sdim InnerMatcher.matches(*SubExpression, Finder, Builder)); 4813239313Sdim} 4814239313Sdim 4815341825Sdim/// Matches casts that has a given cast kind. 4816309124Sdim/// 4817309124Sdim/// Example: matches the implicit cast around \c 0 4818309124Sdim/// (matcher = castExpr(hasCastKind(CK_NullToPointer))) 4819309124Sdim/// \code 4820309124Sdim/// int *p = 0; 4821309124Sdim/// \endcode 4822353358Sdim/// 4823353358Sdim/// If the matcher is use from clang-query, CastKind parameter 4824353358Sdim/// should be passed as a quoted string. e.g., ofKind("CK_NullToPointer"). 4825309124SdimAST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) { 4826309124Sdim return Node.getCastKind() == Kind; 4827309124Sdim} 4828309124Sdim 4829341825Sdim/// Matches casts whose destination type matches a given matcher. 4830239313Sdim/// 4831239313Sdim/// (Note: Clang's AST refers to other conversions as "casts" too, and calls 4832239313Sdim/// actual casts "explicit" casts.) 4833239313SdimAST_MATCHER_P(ExplicitCastExpr, hasDestinationType, 4834239313Sdim internal::Matcher<QualType>, InnerMatcher) { 4835239313Sdim const QualType NodeType = Node.getTypeAsWritten(); 4836239313Sdim return InnerMatcher.matches(NodeType, Finder, Builder); 4837239313Sdim} 4838239313Sdim 4839341825Sdim/// Matches implicit casts whose destination type matches a given 4840239313Sdim/// matcher. 4841239313Sdim/// 4842239313Sdim/// FIXME: Unit test this matcher 4843239313SdimAST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType, 4844239313Sdim internal::Matcher<QualType>, InnerMatcher) { 4845239313Sdim return InnerMatcher.matches(Node.getType(), Finder, Builder); 4846239313Sdim} 4847239313Sdim 4848341825Sdim/// Matches RecordDecl object that are spelled with "struct." 4849296417Sdim/// 4850296417Sdim/// Example matches S, but not C or U. 4851296417Sdim/// \code 4852296417Sdim/// struct S {}; 4853296417Sdim/// class C {}; 4854296417Sdim/// union U {}; 4855296417Sdim/// \endcode 4856296417SdimAST_MATCHER(RecordDecl, isStruct) { 4857296417Sdim return Node.isStruct(); 4858296417Sdim} 4859296417Sdim 4860341825Sdim/// Matches RecordDecl object that are spelled with "union." 4861296417Sdim/// 4862296417Sdim/// Example matches U, but not C or S. 4863296417Sdim/// \code 4864296417Sdim/// struct S {}; 4865296417Sdim/// class C {}; 4866296417Sdim/// union U {}; 4867296417Sdim/// \endcode 4868296417SdimAST_MATCHER(RecordDecl, isUnion) { 4869296417Sdim return Node.isUnion(); 4870296417Sdim} 4871296417Sdim 4872341825Sdim/// Matches RecordDecl object that are spelled with "class." 4873296417Sdim/// 4874296417Sdim/// Example matches C, but not S or U. 4875296417Sdim/// \code 4876296417Sdim/// struct S {}; 4877296417Sdim/// class C {}; 4878296417Sdim/// union U {}; 4879296417Sdim/// \endcode 4880296417SdimAST_MATCHER(RecordDecl, isClass) { 4881296417Sdim return Node.isClass(); 4882296417Sdim} 4883296417Sdim 4884341825Sdim/// Matches the true branch expression of a conditional operator. 4885239313Sdim/// 4886309124Sdim/// Example 1 (conditional ternary operator): matches a 4887243830Sdim/// \code 4888239313Sdim/// condition ? a : b 4889243830Sdim/// \endcode 4890309124Sdim/// 4891309124Sdim/// Example 2 (conditional binary operator): matches opaqueValueExpr(condition) 4892309124Sdim/// \code 4893309124Sdim/// condition ?: b 4894309124Sdim/// \endcode 4895309124SdimAST_MATCHER_P(AbstractConditionalOperator, hasTrueExpression, 4896239313Sdim internal::Matcher<Expr>, InnerMatcher) { 4897296417Sdim const Expr *Expression = Node.getTrueExpr(); 4898276479Sdim return (Expression != nullptr && 4899239313Sdim InnerMatcher.matches(*Expression, Finder, Builder)); 4900239313Sdim} 4901239313Sdim 4902341825Sdim/// Matches the false branch expression of a conditional operator 4903309124Sdim/// (binary or ternary). 4904239313Sdim/// 4905239313Sdim/// Example matches b 4906243830Sdim/// \code 4907239313Sdim/// condition ? a : b 4908309124Sdim/// condition ?: b 4909243830Sdim/// \endcode 4910309124SdimAST_MATCHER_P(AbstractConditionalOperator, hasFalseExpression, 4911239313Sdim internal::Matcher<Expr>, InnerMatcher) { 4912296417Sdim const Expr *Expression = Node.getFalseExpr(); 4913276479Sdim return (Expression != nullptr && 4914239313Sdim InnerMatcher.matches(*Expression, Finder, Builder)); 4915239313Sdim} 4916239313Sdim 4917341825Sdim/// Matches if a declaration has a body attached. 4918239313Sdim/// 4919239313Sdim/// Example matches A, va, fa 4920243830Sdim/// \code 4921239313Sdim/// class A {}; 4922239313Sdim/// class B; // Doesn't match, as it has no body. 4923239313Sdim/// int va; 4924239313Sdim/// extern int vb; // Doesn't match, as it doesn't define the variable. 4925239313Sdim/// void fa() {} 4926239313Sdim/// void fb(); // Doesn't match, as it has no body. 4927327952Sdim/// @interface X 4928327952Sdim/// - (void)ma; // Doesn't match, interface is declaration. 4929327952Sdim/// @end 4930327952Sdim/// @implementation X 4931327952Sdim/// - (void)ma {} 4932327952Sdim/// @end 4933243830Sdim/// \endcode 4934239313Sdim/// 4935327952Sdim/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl>, 4936327952Sdim/// Matcher<ObjCMethodDecl> 4937288943SdimAST_POLYMORPHIC_MATCHER(isDefinition, 4938288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(TagDecl, VarDecl, 4939327952Sdim ObjCMethodDecl, 4940288943Sdim FunctionDecl)) { 4941249423Sdim return Node.isThisDeclarationADefinition(); 4942239313Sdim} 4943239313Sdim 4944341825Sdim/// Matches if a function declaration is variadic. 4945296417Sdim/// 4946296417Sdim/// Example matches f, but not g or h. The function i will not match, even when 4947296417Sdim/// compiled in C mode. 4948296417Sdim/// \code 4949296417Sdim/// void f(...); 4950296417Sdim/// void g(int); 4951296417Sdim/// template <typename... Ts> void h(Ts...); 4952296417Sdim/// void i(); 4953296417Sdim/// \endcode 4954296417SdimAST_MATCHER(FunctionDecl, isVariadic) { 4955296417Sdim return Node.isVariadic(); 4956296417Sdim} 4957296417Sdim 4958341825Sdim/// Matches the class declaration that the given method declaration 4959239313Sdim/// belongs to. 4960239313Sdim/// 4961239313Sdim/// FIXME: Generalize this for other kinds of declarations. 4962239313Sdim/// FIXME: What other kind of declarations would we need to generalize 4963239313Sdim/// this to? 4964239313Sdim/// 4965239313Sdim/// Example matches A() in the last line 4966296417Sdim/// (matcher = cxxConstructExpr(hasDeclaration(cxxMethodDecl( 4967239313Sdim/// ofClass(hasName("A")))))) 4968243830Sdim/// \code 4969239313Sdim/// class A { 4970239313Sdim/// public: 4971239313Sdim/// A(); 4972239313Sdim/// }; 4973239313Sdim/// A a = A(); 4974243830Sdim/// \endcode 4975239313SdimAST_MATCHER_P(CXXMethodDecl, ofClass, 4976239313Sdim internal::Matcher<CXXRecordDecl>, InnerMatcher) { 4977239313Sdim const CXXRecordDecl *Parent = Node.getParent(); 4978276479Sdim return (Parent != nullptr && 4979239313Sdim InnerMatcher.matches(*Parent, Finder, Builder)); 4980239313Sdim} 4981239313Sdim 4982341825Sdim/// Matches each method overridden by the given method. This matcher may 4983309124Sdim/// produce multiple matches. 4984309124Sdim/// 4985309124Sdim/// Given 4986309124Sdim/// \code 4987309124Sdim/// class A { virtual void f(); }; 4988309124Sdim/// class B : public A { void f(); }; 4989309124Sdim/// class C : public B { void f(); }; 4990309124Sdim/// \endcode 4991309124Sdim/// cxxMethodDecl(ofClass(hasName("C")), 4992309124Sdim/// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d") 4993309124Sdim/// matches once, with "b" binding "A::f" and "d" binding "C::f" (Note 4994309124Sdim/// that B::f is not overridden by C::f). 4995309124Sdim/// 4996309124Sdim/// The check can produce multiple matches in case of multiple inheritance, e.g. 4997309124Sdim/// \code 4998309124Sdim/// class A1 { virtual void f(); }; 4999309124Sdim/// class A2 { virtual void f(); }; 5000309124Sdim/// class C : public A1, public A2 { void f(); }; 5001309124Sdim/// \endcode 5002309124Sdim/// cxxMethodDecl(ofClass(hasName("C")), 5003309124Sdim/// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d") 5004309124Sdim/// matches twice, once with "b" binding "A1::f" and "d" binding "C::f", and 5005309124Sdim/// once with "b" binding "A2::f" and "d" binding "C::f". 5006309124SdimAST_MATCHER_P(CXXMethodDecl, forEachOverridden, 5007309124Sdim internal::Matcher<CXXMethodDecl>, InnerMatcher) { 5008309124Sdim BoundNodesTreeBuilder Result; 5009309124Sdim bool Matched = false; 5010309124Sdim for (const auto *Overridden : Node.overridden_methods()) { 5011309124Sdim BoundNodesTreeBuilder OverriddenBuilder(*Builder); 5012309124Sdim const bool OverriddenMatched = 5013309124Sdim InnerMatcher.matches(*Overridden, Finder, &OverriddenBuilder); 5014309124Sdim if (OverriddenMatched) { 5015309124Sdim Matched = true; 5016309124Sdim Result.addMatch(OverriddenBuilder); 5017309124Sdim } 5018309124Sdim } 5019309124Sdim *Builder = std::move(Result); 5020309124Sdim return Matched; 5021309124Sdim} 5022309124Sdim 5023341825Sdim/// Matches if the given method declaration is virtual. 5024251662Sdim/// 5025251662Sdim/// Given 5026251662Sdim/// \code 5027251662Sdim/// class A { 5028251662Sdim/// public: 5029251662Sdim/// virtual void x(); 5030251662Sdim/// }; 5031251662Sdim/// \endcode 5032251662Sdim/// matches A::x 5033251662SdimAST_MATCHER(CXXMethodDecl, isVirtual) { 5034251662Sdim return Node.isVirtual(); 5035251662Sdim} 5036251662Sdim 5037341825Sdim/// Matches if the given method declaration has an explicit "virtual". 5038309124Sdim/// 5039309124Sdim/// Given 5040309124Sdim/// \code 5041309124Sdim/// class A { 5042309124Sdim/// public: 5043309124Sdim/// virtual void x(); 5044309124Sdim/// }; 5045309124Sdim/// class B : public A { 5046309124Sdim/// public: 5047309124Sdim/// void x(); 5048309124Sdim/// }; 5049309124Sdim/// \endcode 5050309124Sdim/// matches A::x but not B::x 5051309124SdimAST_MATCHER(CXXMethodDecl, isVirtualAsWritten) { 5052309124Sdim return Node.isVirtualAsWritten(); 5053309124Sdim} 5054309124Sdim 5055341825Sdim/// Matches if the given method or class declaration is final. 5056296417Sdim/// 5057296417Sdim/// Given: 5058296417Sdim/// \code 5059296417Sdim/// class A final {}; 5060296417Sdim/// 5061296417Sdim/// struct B { 5062296417Sdim/// virtual void f(); 5063296417Sdim/// }; 5064296417Sdim/// 5065296417Sdim/// struct C : B { 5066296417Sdim/// void f() final; 5067296417Sdim/// }; 5068296417Sdim/// \endcode 5069296417Sdim/// matches A and C::f, but not B, C, or B::f 5070296417SdimAST_POLYMORPHIC_MATCHER(isFinal, 5071296417Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, 5072296417Sdim CXXMethodDecl)) { 5073296417Sdim return Node.template hasAttr<FinalAttr>(); 5074296417Sdim} 5075296417Sdim 5076341825Sdim/// Matches if the given method declaration is pure. 5077276479Sdim/// 5078276479Sdim/// Given 5079276479Sdim/// \code 5080276479Sdim/// class A { 5081276479Sdim/// public: 5082276479Sdim/// virtual void x() = 0; 5083276479Sdim/// }; 5084276479Sdim/// \endcode 5085276479Sdim/// matches A::x 5086276479SdimAST_MATCHER(CXXMethodDecl, isPure) { 5087276479Sdim return Node.isPure(); 5088276479Sdim} 5089276479Sdim 5090341825Sdim/// Matches if the given method declaration is const. 5091261991Sdim/// 5092261991Sdim/// Given 5093261991Sdim/// \code 5094261991Sdim/// struct A { 5095261991Sdim/// void foo() const; 5096261991Sdim/// void bar(); 5097261991Sdim/// }; 5098261991Sdim/// \endcode 5099261991Sdim/// 5100296417Sdim/// cxxMethodDecl(isConst()) matches A::foo() but not A::bar() 5101261991SdimAST_MATCHER(CXXMethodDecl, isConst) { 5102261991Sdim return Node.isConst(); 5103261991Sdim} 5104261991Sdim 5105341825Sdim/// Matches if the given method declaration declares a copy assignment 5106296417Sdim/// operator. 5107296417Sdim/// 5108296417Sdim/// Given 5109296417Sdim/// \code 5110296417Sdim/// struct A { 5111296417Sdim/// A &operator=(const A &); 5112296417Sdim/// A &operator=(A &&); 5113296417Sdim/// }; 5114296417Sdim/// \endcode 5115296417Sdim/// 5116296417Sdim/// cxxMethodDecl(isCopyAssignmentOperator()) matches the first method but not 5117296417Sdim/// the second one. 5118296417SdimAST_MATCHER(CXXMethodDecl, isCopyAssignmentOperator) { 5119296417Sdim return Node.isCopyAssignmentOperator(); 5120296417Sdim} 5121296417Sdim 5122341825Sdim/// Matches if the given method declaration declares a move assignment 5123309124Sdim/// operator. 5124309124Sdim/// 5125309124Sdim/// Given 5126309124Sdim/// \code 5127309124Sdim/// struct A { 5128309124Sdim/// A &operator=(const A &); 5129309124Sdim/// A &operator=(A &&); 5130309124Sdim/// }; 5131309124Sdim/// \endcode 5132309124Sdim/// 5133309124Sdim/// cxxMethodDecl(isMoveAssignmentOperator()) matches the second method but not 5134309124Sdim/// the first one. 5135309124SdimAST_MATCHER(CXXMethodDecl, isMoveAssignmentOperator) { 5136309124Sdim return Node.isMoveAssignmentOperator(); 5137309124Sdim} 5138309124Sdim 5139341825Sdim/// Matches if the given method declaration overrides another method. 5140251662Sdim/// 5141251662Sdim/// Given 5142251662Sdim/// \code 5143251662Sdim/// class A { 5144251662Sdim/// public: 5145251662Sdim/// virtual void x(); 5146251662Sdim/// }; 5147251662Sdim/// class B : public A { 5148251662Sdim/// public: 5149251662Sdim/// virtual void x(); 5150251662Sdim/// }; 5151251662Sdim/// \endcode 5152251662Sdim/// matches B::x 5153251662SdimAST_MATCHER(CXXMethodDecl, isOverride) { 5154288943Sdim return Node.size_overridden_methods() > 0 || Node.hasAttr<OverrideAttr>(); 5155251662Sdim} 5156251662Sdim 5157341825Sdim/// Matches method declarations that are user-provided. 5158309124Sdim/// 5159309124Sdim/// Given 5160309124Sdim/// \code 5161309124Sdim/// struct S { 5162309124Sdim/// S(); // #1 5163309124Sdim/// S(const S &) = default; // #2 5164309124Sdim/// S(S &&) = delete; // #3 5165309124Sdim/// }; 5166309124Sdim/// \endcode 5167309124Sdim/// cxxConstructorDecl(isUserProvided()) will match #1, but not #2 or #3. 5168309124SdimAST_MATCHER(CXXMethodDecl, isUserProvided) { 5169309124Sdim return Node.isUserProvided(); 5170309124Sdim} 5171309124Sdim 5172341825Sdim/// Matches member expressions that are called with '->' as opposed 5173239313Sdim/// to '.'. 5174239313Sdim/// 5175239313Sdim/// Member calls on the implicit this pointer match as called with '->'. 5176239313Sdim/// 5177239313Sdim/// Given 5178243830Sdim/// \code 5179239313Sdim/// class Y { 5180239313Sdim/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; } 5181344779Sdim/// template <class T> void f() { this->f<T>(); f<T>(); } 5182239313Sdim/// int a; 5183239313Sdim/// static int b; 5184239313Sdim/// }; 5185344779Sdim/// template <class T> 5186344779Sdim/// class Z { 5187344779Sdim/// void x() { this->m; } 5188344779Sdim/// }; 5189243830Sdim/// \endcode 5190243830Sdim/// memberExpr(isArrow()) 5191239313Sdim/// matches this->x, x, y.x, a, this->b 5192344779Sdim/// cxxDependentScopeMemberExpr(isArrow()) 5193344779Sdim/// matches this->m 5194344779Sdim/// unresolvedMemberExpr(isArrow()) 5195344779Sdim/// matches this->f<T>, f<T> 5196344779SdimAST_POLYMORPHIC_MATCHER( 5197344779Sdim isArrow, AST_POLYMORPHIC_SUPPORTED_TYPES(MemberExpr, UnresolvedMemberExpr, 5198344779Sdim CXXDependentScopeMemberExpr)) { 5199249423Sdim return Node.isArrow(); 5200239313Sdim} 5201239313Sdim 5202341825Sdim/// Matches QualType nodes that are of integer type. 5203239313Sdim/// 5204239313Sdim/// Given 5205243830Sdim/// \code 5206239313Sdim/// void a(int); 5207239313Sdim/// void b(long); 5208239313Sdim/// void c(double); 5209243830Sdim/// \endcode 5210243830Sdim/// functionDecl(hasAnyParameter(hasType(isInteger()))) 5211239313Sdim/// matches "a(int)", "b(long)", but not "c(double)". 5212239313SdimAST_MATCHER(QualType, isInteger) { 5213239313Sdim return Node->isIntegerType(); 5214239313Sdim} 5215239313Sdim 5216341825Sdim/// Matches QualType nodes that are of unsigned integer type. 5217309124Sdim/// 5218309124Sdim/// Given 5219309124Sdim/// \code 5220309124Sdim/// void a(int); 5221309124Sdim/// void b(unsigned long); 5222309124Sdim/// void c(double); 5223309124Sdim/// \endcode 5224314564Sdim/// functionDecl(hasAnyParameter(hasType(isUnsignedInteger()))) 5225309124Sdim/// matches "b(unsigned long)", but not "a(int)" and "c(double)". 5226309124SdimAST_MATCHER(QualType, isUnsignedInteger) { 5227309124Sdim return Node->isUnsignedIntegerType(); 5228309124Sdim} 5229309124Sdim 5230341825Sdim/// Matches QualType nodes that are of signed integer type. 5231309124Sdim/// 5232309124Sdim/// Given 5233309124Sdim/// \code 5234309124Sdim/// void a(int); 5235309124Sdim/// void b(unsigned long); 5236309124Sdim/// void c(double); 5237309124Sdim/// \endcode 5238314564Sdim/// functionDecl(hasAnyParameter(hasType(isSignedInteger()))) 5239309124Sdim/// matches "a(int)", but not "b(unsigned long)" and "c(double)". 5240309124SdimAST_MATCHER(QualType, isSignedInteger) { 5241309124Sdim return Node->isSignedIntegerType(); 5242309124Sdim} 5243309124Sdim 5244341825Sdim/// Matches QualType nodes that are of character type. 5245296417Sdim/// 5246296417Sdim/// Given 5247296417Sdim/// \code 5248296417Sdim/// void a(char); 5249296417Sdim/// void b(wchar_t); 5250296417Sdim/// void c(double); 5251296417Sdim/// \endcode 5252296417Sdim/// functionDecl(hasAnyParameter(hasType(isAnyCharacter()))) 5253296417Sdim/// matches "a(char)", "b(wchar_t)", but not "c(double)". 5254296417SdimAST_MATCHER(QualType, isAnyCharacter) { 5255296417Sdim return Node->isAnyCharacterType(); 5256296417Sdim} 5257296417Sdim 5258341825Sdim/// Matches QualType nodes that are of any pointer type; this includes 5259309124Sdim/// the Objective-C object pointer type, which is different despite being 5260309124Sdim/// syntactically similar. 5261309124Sdim/// 5262309124Sdim/// Given 5263309124Sdim/// \code 5264309124Sdim/// int *i = nullptr; 5265309124Sdim/// 5266309124Sdim/// @interface Foo 5267309124Sdim/// @end 5268309124Sdim/// Foo *f; 5269309124Sdim/// 5270309124Sdim/// int j; 5271309124Sdim/// \endcode 5272309124Sdim/// varDecl(hasType(isAnyPointer())) 5273309124Sdim/// matches "int *i" and "Foo *f", but not "int j". 5274309124SdimAST_MATCHER(QualType, isAnyPointer) { 5275309124Sdim return Node->isAnyPointerType(); 5276309124Sdim} 5277309124Sdim 5278341825Sdim/// Matches QualType nodes that are const-qualified, i.e., that 5279239313Sdim/// include "top-level" const. 5280239313Sdim/// 5281239313Sdim/// Given 5282243830Sdim/// \code 5283239313Sdim/// void a(int); 5284239313Sdim/// void b(int const); 5285239313Sdim/// void c(const int); 5286239313Sdim/// void d(const int*); 5287239313Sdim/// void e(int const) {}; 5288243830Sdim/// \endcode 5289243830Sdim/// functionDecl(hasAnyParameter(hasType(isConstQualified()))) 5290239313Sdim/// matches "void b(int const)", "void c(const int)" and 5291239313Sdim/// "void e(int const) {}". It does not match d as there 5292239313Sdim/// is no top-level const on the parameter type "const int *". 5293249423SdimAST_MATCHER(QualType, isConstQualified) { 5294249423Sdim return Node.isConstQualified(); 5295239313Sdim} 5296239313Sdim 5297341825Sdim/// Matches QualType nodes that are volatile-qualified, i.e., that 5298296417Sdim/// include "top-level" volatile. 5299296417Sdim/// 5300296417Sdim/// Given 5301296417Sdim/// \code 5302296417Sdim/// void a(int); 5303296417Sdim/// void b(int volatile); 5304296417Sdim/// void c(volatile int); 5305296417Sdim/// void d(volatile int*); 5306296417Sdim/// void e(int volatile) {}; 5307296417Sdim/// \endcode 5308296417Sdim/// functionDecl(hasAnyParameter(hasType(isVolatileQualified()))) 5309296417Sdim/// matches "void b(int volatile)", "void c(volatile int)" and 5310296417Sdim/// "void e(int volatile) {}". It does not match d as there 5311296417Sdim/// is no top-level volatile on the parameter type "volatile int *". 5312296417SdimAST_MATCHER(QualType, isVolatileQualified) { 5313296417Sdim return Node.isVolatileQualified(); 5314296417Sdim} 5315296417Sdim 5316341825Sdim/// Matches QualType nodes that have local CV-qualifiers attached to 5317249423Sdim/// the node, not hidden within a typedef. 5318249423Sdim/// 5319249423Sdim/// Given 5320249423Sdim/// \code 5321249423Sdim/// typedef const int const_int; 5322249423Sdim/// const_int i; 5323249423Sdim/// int *const j; 5324249423Sdim/// int *volatile k; 5325249423Sdim/// int m; 5326249423Sdim/// \endcode 5327249423Sdim/// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k. 5328249423Sdim/// \c i is const-qualified but the qualifier is not local. 5329249423SdimAST_MATCHER(QualType, hasLocalQualifiers) { 5330249423Sdim return Node.hasLocalQualifiers(); 5331249423Sdim} 5332249423Sdim 5333341825Sdim/// Matches a member expression where the member is matched by a 5334239313Sdim/// given matcher. 5335239313Sdim/// 5336239313Sdim/// Given 5337243830Sdim/// \code 5338239313Sdim/// struct { int first, second; } first, second; 5339239313Sdim/// int i(second.first); 5340239313Sdim/// int j(first.second); 5341243830Sdim/// \endcode 5342243830Sdim/// memberExpr(member(hasName("first"))) 5343239313Sdim/// matches second.first 5344239313Sdim/// but not first.second (because the member name there is "second"). 5345239313SdimAST_MATCHER_P(MemberExpr, member, 5346239313Sdim internal::Matcher<ValueDecl>, InnerMatcher) { 5347239313Sdim return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); 5348239313Sdim} 5349239313Sdim 5350353358Sdim/// Matches a member expression where the object expression is matched by a 5351353358Sdim/// given matcher. Implicit object expressions are included; that is, it matches 5352353358Sdim/// use of implicit `this`. 5353239313Sdim/// 5354239313Sdim/// Given 5355243830Sdim/// \code 5356353358Sdim/// struct X { 5357353358Sdim/// int m; 5358353358Sdim/// int f(X x) { x.m; return m; } 5359353358Sdim/// }; 5360243830Sdim/// \endcode 5361353358Sdim/// memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X"))))) 5362353358Sdim/// matches `x.m`, but not `m`; however, 5363353358Sdim/// memberExpr(hasObjectExpression(hasType(pointsTo( 5364353358Sdim// cxxRecordDecl(hasName("X")))))) 5365353358Sdim/// matches `m` (aka. `this->m`), but not `x.m`. 5366344779SdimAST_POLYMORPHIC_MATCHER_P( 5367344779Sdim hasObjectExpression, 5368344779Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(MemberExpr, UnresolvedMemberExpr, 5369344779Sdim CXXDependentScopeMemberExpr), 5370344779Sdim internal::Matcher<Expr>, InnerMatcher) { 5371344779Sdim if (const auto *E = dyn_cast<UnresolvedMemberExpr>(&Node)) 5372344779Sdim if (E->isImplicitAccess()) 5373344779Sdim return false; 5374344779Sdim if (const auto *E = dyn_cast<CXXDependentScopeMemberExpr>(&Node)) 5375344779Sdim if (E->isImplicitAccess()) 5376344779Sdim return false; 5377239313Sdim return InnerMatcher.matches(*Node.getBase(), Finder, Builder); 5378239313Sdim} 5379239313Sdim 5380341825Sdim/// Matches any using shadow declaration. 5381239313Sdim/// 5382239313Sdim/// Given 5383243830Sdim/// \code 5384239313Sdim/// namespace X { void b(); } 5385239313Sdim/// using X::b; 5386243830Sdim/// \endcode 5387239313Sdim/// usingDecl(hasAnyUsingShadowDecl(hasName("b")))) 5388239313Sdim/// matches \code using X::b \endcode 5389239313SdimAST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl, 5390243830Sdim internal::Matcher<UsingShadowDecl>, InnerMatcher) { 5391261991Sdim return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(), 5392261991Sdim Node.shadow_end(), Finder, Builder); 5393239313Sdim} 5394239313Sdim 5395341825Sdim/// Matches a using shadow declaration where the target declaration is 5396239313Sdim/// matched by the given matcher. 5397239313Sdim/// 5398239313Sdim/// Given 5399243830Sdim/// \code 5400239313Sdim/// namespace X { int a; void b(); } 5401239313Sdim/// using X::a; 5402239313Sdim/// using X::b; 5403243830Sdim/// \endcode 5404243830Sdim/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl()))) 5405239313Sdim/// matches \code using X::b \endcode 5406239313Sdim/// but not \code using X::a \endcode 5407239313SdimAST_MATCHER_P(UsingShadowDecl, hasTargetDecl, 5408243830Sdim internal::Matcher<NamedDecl>, InnerMatcher) { 5409243830Sdim return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder); 5410239313Sdim} 5411239313Sdim 5412341825Sdim/// Matches template instantiations of function, class, or static 5413239313Sdim/// member variable template instantiations. 5414239313Sdim/// 5415239313Sdim/// Given 5416243830Sdim/// \code 5417239313Sdim/// template <typename T> class X {}; class A {}; X<A> x; 5418243830Sdim/// \endcode 5419239313Sdim/// or 5420243830Sdim/// \code 5421239313Sdim/// template <typename T> class X {}; class A {}; template class X<A>; 5422243830Sdim/// \endcode 5423341825Sdim/// or 5424341825Sdim/// \code 5425341825Sdim/// template <typename T> class X {}; class A {}; extern template class X<A>; 5426341825Sdim/// \endcode 5427296417Sdim/// cxxRecordDecl(hasName("::X"), isTemplateInstantiation()) 5428239313Sdim/// matches the template instantiation of X<A>. 5429239313Sdim/// 5430239313Sdim/// But given 5431243830Sdim/// \code 5432243830Sdim/// template <typename T> class X {}; class A {}; 5433239313Sdim/// template <> class X<A> {}; X<A> x; 5434243830Sdim/// \endcode 5435296417Sdim/// cxxRecordDecl(hasName("::X"), isTemplateInstantiation()) 5436239313Sdim/// does not match, as X<A> is an explicit template specialization. 5437239313Sdim/// 5438239313Sdim/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 5439288943SdimAST_POLYMORPHIC_MATCHER(isTemplateInstantiation, 5440288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, VarDecl, 5441288943Sdim CXXRecordDecl)) { 5442249423Sdim return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation || 5443249423Sdim Node.getTemplateSpecializationKind() == 5444341825Sdim TSK_ExplicitInstantiationDefinition || 5445341825Sdim Node.getTemplateSpecializationKind() == 5446341825Sdim TSK_ExplicitInstantiationDeclaration); 5447239313Sdim} 5448239313Sdim 5449341825Sdim/// Matches declarations that are template instantiations or are inside 5450280031Sdim/// template instantiations. 5451280031Sdim/// 5452280031Sdim/// Given 5453280031Sdim/// \code 5454280031Sdim/// template<typename T> void A(T t) { T i; } 5455280031Sdim/// A(0); 5456280031Sdim/// A(0U); 5457280031Sdim/// \endcode 5458280031Sdim/// functionDecl(isInstantiated()) 5459280031Sdim/// matches 'A(int) {...};' and 'A(unsigned) {...}'. 5460280031SdimAST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) { 5461296417Sdim auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()), 5462280031Sdim functionDecl(isTemplateInstantiation()))); 5463280031Sdim return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation))); 5464280031Sdim} 5465280031Sdim 5466341825Sdim/// Matches statements inside of a template instantiation. 5467280031Sdim/// 5468280031Sdim/// Given 5469280031Sdim/// \code 5470280031Sdim/// int j; 5471280031Sdim/// template<typename T> void A(T t) { T i; j += 42;} 5472280031Sdim/// A(0); 5473280031Sdim/// A(0U); 5474280031Sdim/// \endcode 5475280031Sdim/// declStmt(isInTemplateInstantiation()) 5476280031Sdim/// matches 'int i;' and 'unsigned i'. 5477280031Sdim/// unless(stmt(isInTemplateInstantiation())) 5478280031Sdim/// will NOT match j += 42; as it's shared between the template definition and 5479280031Sdim/// instantiation. 5480280031SdimAST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) { 5481280031Sdim return stmt( 5482296417Sdim hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()), 5483280031Sdim functionDecl(isTemplateInstantiation()))))); 5484280031Sdim} 5485280031Sdim 5486341825Sdim/// Matches explicit template specializations of function, class, or 5487243830Sdim/// static member variable template instantiations. 5488243830Sdim/// 5489243830Sdim/// Given 5490243830Sdim/// \code 5491243830Sdim/// template<typename T> void A(T t) { } 5492243830Sdim/// template<> void A(int N) { } 5493243830Sdim/// \endcode 5494243830Sdim/// functionDecl(isExplicitTemplateSpecialization()) 5495243830Sdim/// matches the specialization A<int>(). 5496243830Sdim/// 5497243830Sdim/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl> 5498288943SdimAST_POLYMORPHIC_MATCHER(isExplicitTemplateSpecialization, 5499288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, VarDecl, 5500288943Sdim CXXRecordDecl)) { 5501249423Sdim return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization); 5502243830Sdim} 5503243830Sdim 5504341825Sdim/// Matches \c TypeLocs for which the given inner 5505243830Sdim/// QualType-matcher matches. 5506276479SdimAST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher<TypeLoc>, loc, 5507276479Sdim internal::Matcher<QualType>, InnerMatcher, 0) { 5508243830Sdim return internal::BindableMatcher<TypeLoc>( 5509243830Sdim new internal::TypeLocTypeMatcher(InnerMatcher)); 5510243830Sdim} 5511243830Sdim 5512341825Sdim/// Matches type \c bool. 5513296417Sdim/// 5514296417Sdim/// Given 5515296417Sdim/// \code 5516296417Sdim/// struct S { bool func(); }; 5517296417Sdim/// \endcode 5518296417Sdim/// functionDecl(returns(booleanType())) 5519296417Sdim/// matches "bool func();" 5520296417SdimAST_MATCHER(Type, booleanType) { 5521296417Sdim return Node.isBooleanType(); 5522296417Sdim} 5523296417Sdim 5524341825Sdim/// Matches type \c void. 5525280031Sdim/// 5526280031Sdim/// Given 5527280031Sdim/// \code 5528280031Sdim/// struct S { void func(); }; 5529280031Sdim/// \endcode 5530280031Sdim/// functionDecl(returns(voidType())) 5531280031Sdim/// matches "void func();" 5532280031SdimAST_MATCHER(Type, voidType) { 5533280031Sdim return Node.isVoidType(); 5534280031Sdim} 5535280031Sdim 5536327952Sdimtemplate <typename NodeType> 5537327952Sdimusing AstTypeMatcher = internal::VariadicDynCastAllOfMatcher<Type, NodeType>; 5538327952Sdim 5539341825Sdim/// Matches builtin Types. 5540243830Sdim/// 5541243830Sdim/// Given 5542243830Sdim/// \code 5543243830Sdim/// struct A {}; 5544243830Sdim/// A a; 5545243830Sdim/// int b; 5546243830Sdim/// float c; 5547243830Sdim/// bool d; 5548243830Sdim/// \endcode 5549243830Sdim/// builtinType() 5550243830Sdim/// matches "int b", "float c" and "bool d" 5551327952Sdimextern const AstTypeMatcher<BuiltinType> builtinType; 5552243830Sdim 5553341825Sdim/// Matches all kinds of arrays. 5554243830Sdim/// 5555243830Sdim/// Given 5556243830Sdim/// \code 5557243830Sdim/// int a[] = { 2, 3 }; 5558243830Sdim/// int b[4]; 5559243830Sdim/// void f() { int c[a[0]]; } 5560243830Sdim/// \endcode 5561243830Sdim/// arrayType() 5562243830Sdim/// matches "int a[]", "int b[4]" and "int c[a[0]]"; 5563327952Sdimextern const AstTypeMatcher<ArrayType> arrayType; 5564243830Sdim 5565341825Sdim/// Matches C99 complex types. 5566243830Sdim/// 5567243830Sdim/// Given 5568243830Sdim/// \code 5569243830Sdim/// _Complex float f; 5570243830Sdim/// \endcode 5571243830Sdim/// complexType() 5572243830Sdim/// matches "_Complex float f" 5573327952Sdimextern const AstTypeMatcher<ComplexType> complexType; 5574243830Sdim 5575341825Sdim/// Matches any real floating-point type (float, double, long double). 5576309124Sdim/// 5577309124Sdim/// Given 5578309124Sdim/// \code 5579309124Sdim/// int i; 5580309124Sdim/// float f; 5581309124Sdim/// \endcode 5582309124Sdim/// realFloatingPointType() 5583309124Sdim/// matches "float f" but not "int i" 5584309124SdimAST_MATCHER(Type, realFloatingPointType) { 5585309124Sdim return Node.isRealFloatingType(); 5586309124Sdim} 5587309124Sdim 5588341825Sdim/// Matches arrays and C99 complex types that have a specific element 5589243830Sdim/// type. 5590243830Sdim/// 5591243830Sdim/// Given 5592243830Sdim/// \code 5593243830Sdim/// struct A {}; 5594243830Sdim/// A a[7]; 5595243830Sdim/// int b[7]; 5596243830Sdim/// \endcode 5597243830Sdim/// arrayType(hasElementType(builtinType())) 5598243830Sdim/// matches "int b[7]" 5599243830Sdim/// 5600243830Sdim/// Usable as: Matcher<ArrayType>, Matcher<ComplexType> 5601327952SdimAST_TYPELOC_TRAVERSE_MATCHER_DECL(hasElementType, getElement, 5602327952Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType, 5603327952Sdim ComplexType)); 5604243830Sdim 5605341825Sdim/// Matches C arrays with a specified constant size. 5606243830Sdim/// 5607243830Sdim/// Given 5608243830Sdim/// \code 5609243830Sdim/// void() { 5610243830Sdim/// int a[2]; 5611243830Sdim/// int b[] = { 2, 3 }; 5612243830Sdim/// int c[b[0]]; 5613243830Sdim/// } 5614243830Sdim/// \endcode 5615243830Sdim/// constantArrayType() 5616243830Sdim/// matches "int a[2]" 5617327952Sdimextern const AstTypeMatcher<ConstantArrayType> constantArrayType; 5618243830Sdim 5619341825Sdim/// Matches nodes that have the specified size. 5620243830Sdim/// 5621243830Sdim/// Given 5622243830Sdim/// \code 5623243830Sdim/// int a[42]; 5624243830Sdim/// int b[2 * 21]; 5625243830Sdim/// int c[41], d[43]; 5626309124Sdim/// char *s = "abcd"; 5627309124Sdim/// wchar_t *ws = L"abcd"; 5628309124Sdim/// char *w = "a"; 5629243830Sdim/// \endcode 5630243830Sdim/// constantArrayType(hasSize(42)) 5631243830Sdim/// matches "int a[42]" and "int b[2 * 21]" 5632309124Sdim/// stringLiteral(hasSize(4)) 5633309124Sdim/// matches "abcd", L"abcd" 5634309124SdimAST_POLYMORPHIC_MATCHER_P(hasSize, 5635309124Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(ConstantArrayType, 5636309124Sdim StringLiteral), 5637309124Sdim unsigned, N) { 5638309124Sdim return internal::HasSizeMatcher<NodeType>::hasSize(Node, N); 5639243830Sdim} 5640243830Sdim 5641341825Sdim/// Matches C++ arrays whose size is a value-dependent expression. 5642243830Sdim/// 5643243830Sdim/// Given 5644243830Sdim/// \code 5645243830Sdim/// template<typename T, int Size> 5646243830Sdim/// class array { 5647243830Sdim/// T data[Size]; 5648243830Sdim/// }; 5649243830Sdim/// \endcode 5650243830Sdim/// dependentSizedArrayType 5651243830Sdim/// matches "T data[Size]" 5652327952Sdimextern const AstTypeMatcher<DependentSizedArrayType> dependentSizedArrayType; 5653243830Sdim 5654341825Sdim/// Matches C arrays with unspecified size. 5655243830Sdim/// 5656243830Sdim/// Given 5657243830Sdim/// \code 5658243830Sdim/// int a[] = { 2, 3 }; 5659243830Sdim/// int b[42]; 5660243830Sdim/// void f(int c[]) { int d[a[0]]; }; 5661243830Sdim/// \endcode 5662243830Sdim/// incompleteArrayType() 5663243830Sdim/// matches "int a[]" and "int c[]" 5664327952Sdimextern const AstTypeMatcher<IncompleteArrayType> incompleteArrayType; 5665243830Sdim 5666341825Sdim/// Matches C arrays with a specified size that is not an 5667243830Sdim/// integer-constant-expression. 5668243830Sdim/// 5669243830Sdim/// Given 5670243830Sdim/// \code 5671243830Sdim/// void f() { 5672243830Sdim/// int a[] = { 2, 3 } 5673243830Sdim/// int b[42]; 5674243830Sdim/// int c[a[0]]; 5675280031Sdim/// } 5676243830Sdim/// \endcode 5677243830Sdim/// variableArrayType() 5678243830Sdim/// matches "int c[a[0]]" 5679327952Sdimextern const AstTypeMatcher<VariableArrayType> variableArrayType; 5680243830Sdim 5681341825Sdim/// Matches \c VariableArrayType nodes that have a specific size 5682243830Sdim/// expression. 5683243830Sdim/// 5684243830Sdim/// Given 5685243830Sdim/// \code 5686243830Sdim/// void f(int b) { 5687243830Sdim/// int a[b]; 5688243830Sdim/// } 5689243830Sdim/// \endcode 5690243830Sdim/// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( 5691243830Sdim/// varDecl(hasName("b"))))))) 5692243830Sdim/// matches "int a[b]" 5693243830SdimAST_MATCHER_P(VariableArrayType, hasSizeExpr, 5694243830Sdim internal::Matcher<Expr>, InnerMatcher) { 5695243830Sdim return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder); 5696243830Sdim} 5697243830Sdim 5698341825Sdim/// Matches atomic types. 5699243830Sdim/// 5700243830Sdim/// Given 5701243830Sdim/// \code 5702243830Sdim/// _Atomic(int) i; 5703243830Sdim/// \endcode 5704243830Sdim/// atomicType() 5705243830Sdim/// matches "_Atomic(int) i" 5706327952Sdimextern const AstTypeMatcher<AtomicType> atomicType; 5707243830Sdim 5708341825Sdim/// Matches atomic types with a specific value type. 5709243830Sdim/// 5710243830Sdim/// Given 5711243830Sdim/// \code 5712243830Sdim/// _Atomic(int) i; 5713243830Sdim/// _Atomic(float) f; 5714243830Sdim/// \endcode 5715243830Sdim/// atomicType(hasValueType(isInteger())) 5716243830Sdim/// matches "_Atomic(int) i" 5717243830Sdim/// 5718243830Sdim/// Usable as: Matcher<AtomicType> 5719327952SdimAST_TYPELOC_TRAVERSE_MATCHER_DECL(hasValueType, getValue, 5720327952Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(AtomicType)); 5721243830Sdim 5722341825Sdim/// Matches types nodes representing C++11 auto types. 5723243830Sdim/// 5724243830Sdim/// Given: 5725243830Sdim/// \code 5726243830Sdim/// auto n = 4; 5727243830Sdim/// int v[] = { 2, 3 } 5728243830Sdim/// for (auto i : v) { } 5729243830Sdim/// \endcode 5730243830Sdim/// autoType() 5731243830Sdim/// matches "auto n" and "auto i" 5732327952Sdimextern const AstTypeMatcher<AutoType> autoType; 5733243830Sdim 5734341825Sdim/// Matches types nodes representing C++11 decltype(<expr>) types. 5735243830Sdim/// 5736341825Sdim/// Given: 5737341825Sdim/// \code 5738341825Sdim/// short i = 1; 5739341825Sdim/// int j = 42; 5740341825Sdim/// decltype(i + j) result = i + j; 5741341825Sdim/// \endcode 5742341825Sdim/// decltypeType() 5743341825Sdim/// matches "decltype(i + j)" 5744341825Sdimextern const AstTypeMatcher<DecltypeType> decltypeType; 5745341825Sdim 5746341825Sdim/// Matches \c AutoType nodes where the deduced type is a specific type. 5747341825Sdim/// 5748243830Sdim/// Note: There is no \c TypeLoc for the deduced type and thus no 5749243830Sdim/// \c getDeducedLoc() matcher. 5750243830Sdim/// 5751243830Sdim/// Given 5752243830Sdim/// \code 5753243830Sdim/// auto a = 1; 5754243830Sdim/// auto b = 2.0; 5755243830Sdim/// \endcode 5756243830Sdim/// autoType(hasDeducedType(isInteger())) 5757243830Sdim/// matches "auto a" 5758243830Sdim/// 5759243830Sdim/// Usable as: Matcher<AutoType> 5760261991SdimAST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType, 5761288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType)); 5762243830Sdim 5763341825Sdim/// Matches \c DecltypeType nodes to find out the underlying type. 5764243830Sdim/// 5765243830Sdim/// Given 5766243830Sdim/// \code 5767341825Sdim/// decltype(1) a = 1; 5768341825Sdim/// decltype(2.0) b = 2.0; 5769341825Sdim/// \endcode 5770341825Sdim/// decltypeType(hasUnderlyingType(isInteger())) 5771344779Sdim/// matches the type of "a" 5772341825Sdim/// 5773341825Sdim/// Usable as: Matcher<DecltypeType> 5774341825SdimAST_TYPE_TRAVERSE_MATCHER(hasUnderlyingType, getUnderlyingType, 5775341825Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(DecltypeType)); 5776341825Sdim 5777341825Sdim/// Matches \c FunctionType nodes. 5778341825Sdim/// 5779341825Sdim/// Given 5780341825Sdim/// \code 5781243830Sdim/// int (*f)(int); 5782243830Sdim/// void g(); 5783243830Sdim/// \endcode 5784243830Sdim/// functionType() 5785243830Sdim/// matches "int (*f)(int)" and the type of "g". 5786327952Sdimextern const AstTypeMatcher<FunctionType> functionType; 5787243830Sdim 5788341825Sdim/// Matches \c FunctionProtoType nodes. 5789309124Sdim/// 5790309124Sdim/// Given 5791309124Sdim/// \code 5792309124Sdim/// int (*f)(int); 5793309124Sdim/// void g(); 5794309124Sdim/// \endcode 5795309124Sdim/// functionProtoType() 5796309124Sdim/// matches "int (*f)(int)" and the type of "g" in C++ mode. 5797309124Sdim/// In C mode, "g" is not matched because it does not contain a prototype. 5798327952Sdimextern const AstTypeMatcher<FunctionProtoType> functionProtoType; 5799309124Sdim 5800341825Sdim/// Matches \c ParenType nodes. 5801249423Sdim/// 5802249423Sdim/// Given 5803249423Sdim/// \code 5804249423Sdim/// int (*ptr_to_array)[4]; 5805249423Sdim/// int *array_of_ptrs[4]; 5806249423Sdim/// \endcode 5807249423Sdim/// 5808249423Sdim/// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not 5809249423Sdim/// \c array_of_ptrs. 5810327952Sdimextern const AstTypeMatcher<ParenType> parenType; 5811249423Sdim 5812341825Sdim/// Matches \c ParenType nodes where the inner type is a specific type. 5813249423Sdim/// 5814249423Sdim/// Given 5815249423Sdim/// \code 5816249423Sdim/// int (*ptr_to_array)[4]; 5817249423Sdim/// int (*ptr_to_func)(int); 5818249423Sdim/// \endcode 5819249423Sdim/// 5820249423Sdim/// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches 5821249423Sdim/// \c ptr_to_func but not \c ptr_to_array. 5822249423Sdim/// 5823249423Sdim/// Usable as: Matcher<ParenType> 5824261991SdimAST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType, 5825288943Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(ParenType)); 5826249423Sdim 5827341825Sdim/// Matches block pointer types, i.e. types syntactically represented as 5828243830Sdim/// "void (^)(int)". 5829243830Sdim/// 5830243830Sdim/// The \c pointee is always required to be a \c FunctionType. 5831327952Sdimextern const AstTypeMatcher<BlockPointerType> blockPointerType; 5832243830Sdim 5833341825Sdim/// Matches member pointer types. 5834243830Sdim/// Given 5835243830Sdim/// \code 5836243830Sdim/// struct A { int i; } 5837243830Sdim/// A::* ptr = A::i; 5838243830Sdim/// \endcode 5839243830Sdim/// memberPointerType() 5840243830Sdim/// matches "A::* ptr" 5841327952Sdimextern const AstTypeMatcher<MemberPointerType> memberPointerType; 5842243830Sdim 5843341825Sdim/// Matches pointer types, but does not match Objective-C object pointer 5844296417Sdim/// types. 5845243830Sdim/// 5846243830Sdim/// Given 5847243830Sdim/// \code 5848243830Sdim/// int *a; 5849243830Sdim/// int &b = *a; 5850243830Sdim/// int c = 5; 5851296417Sdim/// 5852296417Sdim/// @interface Foo 5853296417Sdim/// @end 5854296417Sdim/// Foo *f; 5855243830Sdim/// \endcode 5856243830Sdim/// pointerType() 5857296417Sdim/// matches "int *a", but does not match "Foo *f". 5858327952Sdimextern const AstTypeMatcher<PointerType> pointerType; 5859243830Sdim 5860341825Sdim/// Matches an Objective-C object pointer type, which is different from 5861296417Sdim/// a pointer type, despite being syntactically similar. 5862296417Sdim/// 5863296417Sdim/// Given 5864296417Sdim/// \code 5865296417Sdim/// int *a; 5866296417Sdim/// 5867296417Sdim/// @interface Foo 5868296417Sdim/// @end 5869296417Sdim/// Foo *f; 5870296417Sdim/// \endcode 5871296417Sdim/// pointerType() 5872296417Sdim/// matches "Foo *f", but does not match "int *a". 5873327952Sdimextern const AstTypeMatcher<ObjCObjectPointerType> objcObjectPointerType; 5874296417Sdim 5875341825Sdim/// Matches both lvalue and rvalue reference types. 5876243830Sdim/// 5877243830Sdim/// Given 5878243830Sdim/// \code 5879243830Sdim/// int *a; 5880243830Sdim/// int &b = *a; 5881249423Sdim/// int &&c = 1; 5882249423Sdim/// auto &d = b; 5883249423Sdim/// auto &&e = c; 5884249423Sdim/// auto &&f = 2; 5885249423Sdim/// int g = 5; 5886243830Sdim/// \endcode 5887249423Sdim/// 5888249423Sdim/// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f. 5889327952Sdimextern const AstTypeMatcher<ReferenceType> referenceType; 5890243830Sdim 5891341825Sdim/// Matches lvalue reference types. 5892249423Sdim/// 5893249423Sdim/// Given: 5894249423Sdim/// \code 5895249423Sdim/// int *a; 5896249423Sdim/// int &b = *a; 5897249423Sdim/// int &&c = 1; 5898249423Sdim/// auto &d = b; 5899249423Sdim/// auto &&e = c; 5900249423Sdim/// auto &&f = 2; 5901249423Sdim/// int g = 5; 5902249423Sdim/// \endcode 5903249423Sdim/// 5904249423Sdim/// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is 5905249423Sdim/// matched since the type is deduced as int& by reference collapsing rules. 5906327952Sdimextern const AstTypeMatcher<LValueReferenceType> lValueReferenceType; 5907249423Sdim 5908341825Sdim/// Matches rvalue reference types. 5909249423Sdim/// 5910249423Sdim/// Given: 5911249423Sdim/// \code 5912249423Sdim/// int *a; 5913249423Sdim/// int &b = *a; 5914249423Sdim/// int &&c = 1; 5915249423Sdim/// auto &d = b; 5916249423Sdim/// auto &&e = c; 5917249423Sdim/// auto &&f = 2; 5918249423Sdim/// int g = 5; 5919249423Sdim/// \endcode 5920249423Sdim/// 5921249423Sdim/// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not 5922249423Sdim/// matched as it is deduced to int& by reference collapsing rules. 5923327952Sdimextern const AstTypeMatcher<RValueReferenceType> rValueReferenceType; 5924249423Sdim 5925341825Sdim/// Narrows PointerType (and similar) matchers to those where the 5926243830Sdim/// \c pointee matches a given matcher. 5927243830Sdim/// 5928243830Sdim/// Given 5929243830Sdim/// \code 5930243830Sdim/// int *a; 5931243830Sdim/// int const *b; 5932243830Sdim/// float const *f; 5933243830Sdim/// \endcode 5934243830Sdim/// pointerType(pointee(isConstQualified(), isInteger())) 5935243830Sdim/// matches "int const *b" 5936243830Sdim/// 5937243830Sdim/// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>, 5938243830Sdim/// Matcher<PointerType>, Matcher<ReferenceType> 5939327952SdimAST_TYPELOC_TRAVERSE_MATCHER_DECL( 5940327952Sdim pointee, getPointee, 5941327952Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType, 5942327952Sdim PointerType, ReferenceType)); 5943243830Sdim 5944341825Sdim/// Matches typedef types. 5945243830Sdim/// 5946243830Sdim/// Given 5947243830Sdim/// \code 5948243830Sdim/// typedef int X; 5949243830Sdim/// \endcode 5950243830Sdim/// typedefType() 5951243830Sdim/// matches "typedef int X" 5952327952Sdimextern const AstTypeMatcher<TypedefType> typedefType; 5953243830Sdim 5954341825Sdim/// Matches enum types. 5955309124Sdim/// 5956309124Sdim/// Given 5957309124Sdim/// \code 5958309124Sdim/// enum C { Green }; 5959309124Sdim/// enum class S { Red }; 5960309124Sdim/// 5961309124Sdim/// C c; 5962309124Sdim/// S s; 5963309124Sdim/// \endcode 5964309124Sdim// 5965309124Sdim/// \c enumType() matches the type of the variable declarations of both \c c and 5966309124Sdim/// \c s. 5967327952Sdimextern const AstTypeMatcher<EnumType> enumType; 5968309124Sdim 5969341825Sdim/// Matches template specialization types. 5970249423Sdim/// 5971249423Sdim/// Given 5972249423Sdim/// \code 5973249423Sdim/// template <typename T> 5974249423Sdim/// class C { }; 5975249423Sdim/// 5976249423Sdim/// template class C<int>; // A 5977249423Sdim/// C<char> var; // B 5978296417Sdim/// \endcode 5979249423Sdim/// 5980249423Sdim/// \c templateSpecializationType() matches the type of the explicit 5981249423Sdim/// instantiation in \c A and the type of the variable declaration in \c B. 5982327952Sdimextern const AstTypeMatcher<TemplateSpecializationType> 5983327952Sdim templateSpecializationType; 5984249423Sdim 5985341825Sdim/// Matches types nodes representing unary type transformations. 5986261991Sdim/// 5987261991Sdim/// Given: 5988261991Sdim/// \code 5989261991Sdim/// typedef __underlying_type(T) type; 5990261991Sdim/// \endcode 5991261991Sdim/// unaryTransformType() 5992261991Sdim/// matches "__underlying_type(T)" 5993327952Sdimextern const AstTypeMatcher<UnaryTransformType> unaryTransformType; 5994261991Sdim 5995341825Sdim/// Matches record types (e.g. structs, classes). 5996249423Sdim/// 5997249423Sdim/// Given 5998249423Sdim/// \code 5999249423Sdim/// class C {}; 6000249423Sdim/// struct S {}; 6001249423Sdim/// 6002249423Sdim/// C c; 6003249423Sdim/// S s; 6004296417Sdim/// \endcode 6005249423Sdim/// 6006249423Sdim/// \c recordType() matches the type of the variable declarations of both \c c 6007249423Sdim/// and \c s. 6008327952Sdimextern const AstTypeMatcher<RecordType> recordType; 6009249423Sdim 6010341825Sdim/// Matches tag types (record and enum types). 6011327952Sdim/// 6012327952Sdim/// Given 6013327952Sdim/// \code 6014327952Sdim/// enum E {}; 6015327952Sdim/// class C {}; 6016327952Sdim/// 6017327952Sdim/// E e; 6018327952Sdim/// C c; 6019327952Sdim/// \endcode 6020327952Sdim/// 6021327952Sdim/// \c tagType() matches the type of the variable declarations of both \c e 6022327952Sdim/// and \c c. 6023327952Sdimextern const AstTypeMatcher<TagType> tagType; 6024327952Sdim 6025341825Sdim/// Matches types specified with an elaborated type keyword or with a 6026249423Sdim/// qualified name. 6027249423Sdim/// 6028249423Sdim/// Given 6029249423Sdim/// \code 6030249423Sdim/// namespace N { 6031249423Sdim/// namespace M { 6032249423Sdim/// class D {}; 6033249423Sdim/// } 6034249423Sdim/// } 6035249423Sdim/// class C {}; 6036249423Sdim/// 6037249423Sdim/// class C c; 6038249423Sdim/// N::M::D d; 6039296417Sdim/// \endcode 6040249423Sdim/// 6041249423Sdim/// \c elaboratedType() matches the type of the variable declarations of both 6042249423Sdim/// \c c and \c d. 6043327952Sdimextern const AstTypeMatcher<ElaboratedType> elaboratedType; 6044249423Sdim 6045341825Sdim/// Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier, 6046249423Sdim/// matches \c InnerMatcher if the qualifier exists. 6047249423Sdim/// 6048249423Sdim/// Given 6049249423Sdim/// \code 6050249423Sdim/// namespace N { 6051249423Sdim/// namespace M { 6052249423Sdim/// class D {}; 6053249423Sdim/// } 6054249423Sdim/// } 6055249423Sdim/// N::M::D d; 6056296417Sdim/// \endcode 6057249423Sdim/// 6058249423Sdim/// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))) 6059249423Sdim/// matches the type of the variable declaration of \c d. 6060249423SdimAST_MATCHER_P(ElaboratedType, hasQualifier, 6061249423Sdim internal::Matcher<NestedNameSpecifier>, InnerMatcher) { 6062249423Sdim if (const NestedNameSpecifier *Qualifier = Node.getQualifier()) 6063249423Sdim return InnerMatcher.matches(*Qualifier, Finder, Builder); 6064249423Sdim 6065249423Sdim return false; 6066243830Sdim} 6067243830Sdim 6068341825Sdim/// Matches ElaboratedTypes whose named type matches \c InnerMatcher. 6069249423Sdim/// 6070249423Sdim/// Given 6071249423Sdim/// \code 6072249423Sdim/// namespace N { 6073249423Sdim/// namespace M { 6074249423Sdim/// class D {}; 6075249423Sdim/// } 6076249423Sdim/// } 6077249423Sdim/// N::M::D d; 6078296417Sdim/// \endcode 6079249423Sdim/// 6080249423Sdim/// \c elaboratedType(namesType(recordType( 6081249423Sdim/// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable 6082249423Sdim/// declaration of \c d. 6083249423SdimAST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>, 6084249423Sdim InnerMatcher) { 6085249423Sdim return InnerMatcher.matches(Node.getNamedType(), Finder, Builder); 6086249423Sdim} 6087249423Sdim 6088341825Sdim/// Matches types that represent the result of substituting a type for a 6089296417Sdim/// template type parameter. 6090296417Sdim/// 6091296417Sdim/// Given 6092296417Sdim/// \code 6093296417Sdim/// template <typename T> 6094296417Sdim/// void F(T t) { 6095296417Sdim/// int i = 1 + t; 6096296417Sdim/// } 6097296417Sdim/// \endcode 6098296417Sdim/// 6099296417Sdim/// \c substTemplateTypeParmType() matches the type of 't' but not '1' 6100327952Sdimextern const AstTypeMatcher<SubstTemplateTypeParmType> 6101327952Sdim substTemplateTypeParmType; 6102296417Sdim 6103341825Sdim/// Matches template type parameter substitutions that have a replacement 6104314564Sdim/// type that matches the provided matcher. 6105314564Sdim/// 6106314564Sdim/// Given 6107314564Sdim/// \code 6108314564Sdim/// template <typename T> 6109314564Sdim/// double F(T t); 6110314564Sdim/// int i; 6111314564Sdim/// double j = F(i); 6112314564Sdim/// \endcode 6113314564Sdim/// 6114314564Sdim/// \c substTemplateTypeParmType(hasReplacementType(type())) matches int 6115314564SdimAST_TYPE_TRAVERSE_MATCHER( 6116314564Sdim hasReplacementType, getReplacementType, 6117314564Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(SubstTemplateTypeParmType)); 6118314564Sdim 6119341825Sdim/// Matches template type parameter types. 6120296417Sdim/// 6121296417Sdim/// Example matches T, but not int. 6122296417Sdim/// (matcher = templateTypeParmType()) 6123296417Sdim/// \code 6124296417Sdim/// template <typename T> void f(int i); 6125296417Sdim/// \endcode 6126327952Sdimextern const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType; 6127296417Sdim 6128341825Sdim/// Matches injected class name types. 6129296417Sdim/// 6130296417Sdim/// Example matches S s, but not S<T> s. 6131296417Sdim/// (matcher = parmVarDecl(hasType(injectedClassNameType()))) 6132296417Sdim/// \code 6133296417Sdim/// template <typename T> struct S { 6134296417Sdim/// void f(S s); 6135296417Sdim/// void g(S<T> s); 6136296417Sdim/// }; 6137296417Sdim/// \endcode 6138327952Sdimextern const AstTypeMatcher<InjectedClassNameType> injectedClassNameType; 6139296417Sdim 6140341825Sdim/// Matches decayed type 6141296417Sdim/// Example matches i[] in declaration of f. 6142296417Sdim/// (matcher = valueDecl(hasType(decayedType(hasDecayedType(pointerType()))))) 6143296417Sdim/// Example matches i[1]. 6144296417Sdim/// (matcher = expr(hasType(decayedType(hasDecayedType(pointerType()))))) 6145296417Sdim/// \code 6146296417Sdim/// void f(int i[]) { 6147296417Sdim/// i[1] = 0; 6148296417Sdim/// } 6149296417Sdim/// \endcode 6150327952Sdimextern const AstTypeMatcher<DecayedType> decayedType; 6151296417Sdim 6152341825Sdim/// Matches the decayed type, whos decayed type matches \c InnerMatcher 6153296417SdimAST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>, 6154296417Sdim InnerType) { 6155296417Sdim return InnerType.matches(Node.getDecayedType(), Finder, Builder); 6156296417Sdim} 6157296417Sdim 6158341825Sdim/// Matches declarations whose declaration context, interpreted as a 6159249423Sdim/// Decl, matches \c InnerMatcher. 6160249423Sdim/// 6161249423Sdim/// Given 6162249423Sdim/// \code 6163249423Sdim/// namespace N { 6164249423Sdim/// namespace M { 6165249423Sdim/// class D {}; 6166249423Sdim/// } 6167249423Sdim/// } 6168296417Sdim/// \endcode 6169249423Sdim/// 6170296417Sdim/// \c cxxRcordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the 6171249423Sdim/// declaration of \c class \c D. 6172249423SdimAST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) { 6173280031Sdim const DeclContext *DC = Node.getDeclContext(); 6174280031Sdim if (!DC) return false; 6175280031Sdim return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder); 6176249423Sdim} 6177249423Sdim 6178341825Sdim/// Matches nested name specifiers. 6179243830Sdim/// 6180243830Sdim/// Given 6181243830Sdim/// \code 6182243830Sdim/// namespace ns { 6183243830Sdim/// struct A { static void f(); }; 6184243830Sdim/// void A::f() {} 6185243830Sdim/// void g() { A::f(); } 6186243830Sdim/// } 6187243830Sdim/// ns::A a; 6188243830Sdim/// \endcode 6189243830Sdim/// nestedNameSpecifier() 6190243830Sdim/// matches "ns::" and both "A::" 6191327952Sdimextern const internal::VariadicAllOfMatcher<NestedNameSpecifier> 6192327952Sdim nestedNameSpecifier; 6193243830Sdim 6194341825Sdim/// Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc. 6195327952Sdimextern const internal::VariadicAllOfMatcher<NestedNameSpecifierLoc> 6196327952Sdim nestedNameSpecifierLoc; 6197243830Sdim 6198341825Sdim/// Matches \c NestedNameSpecifierLocs for which the given inner 6199243830Sdim/// NestedNameSpecifier-matcher matches. 6200276479SdimAST_MATCHER_FUNCTION_P_OVERLOAD( 6201276479Sdim internal::BindableMatcher<NestedNameSpecifierLoc>, loc, 6202276479Sdim internal::Matcher<NestedNameSpecifier>, InnerMatcher, 1) { 6203243830Sdim return internal::BindableMatcher<NestedNameSpecifierLoc>( 6204243830Sdim new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>( 6205243830Sdim InnerMatcher)); 6206243830Sdim} 6207243830Sdim 6208341825Sdim/// Matches nested name specifiers that specify a type matching the 6209243830Sdim/// given \c QualType matcher without qualifiers. 6210243830Sdim/// 6211243830Sdim/// Given 6212243830Sdim/// \code 6213243830Sdim/// struct A { struct B { struct C {}; }; }; 6214243830Sdim/// A::B::C c; 6215243830Sdim/// \endcode 6216296417Sdim/// nestedNameSpecifier(specifiesType( 6217296417Sdim/// hasDeclaration(cxxRecordDecl(hasName("A"))) 6218296417Sdim/// )) 6219243830Sdim/// matches "A::" 6220243830SdimAST_MATCHER_P(NestedNameSpecifier, specifiesType, 6221243830Sdim internal::Matcher<QualType>, InnerMatcher) { 6222276479Sdim if (!Node.getAsType()) 6223243830Sdim return false; 6224243830Sdim return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder); 6225243830Sdim} 6226243830Sdim 6227341825Sdim/// Matches nested name specifier locs that specify a type matching the 6228243830Sdim/// given \c TypeLoc. 6229243830Sdim/// 6230243830Sdim/// Given 6231243830Sdim/// \code 6232243830Sdim/// struct A { struct B { struct C {}; }; }; 6233243830Sdim/// A::B::C c; 6234243830Sdim/// \endcode 6235243830Sdim/// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type( 6236296417Sdim/// hasDeclaration(cxxRecordDecl(hasName("A"))))))) 6237243830Sdim/// matches "A::" 6238243830SdimAST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, 6239243830Sdim internal::Matcher<TypeLoc>, InnerMatcher) { 6240341825Sdim return Node && Node.getNestedNameSpecifier()->getAsType() && 6241341825Sdim InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder); 6242243830Sdim} 6243243830Sdim 6244341825Sdim/// Matches on the prefix of a \c NestedNameSpecifier. 6245243830Sdim/// 6246243830Sdim/// Given 6247243830Sdim/// \code 6248243830Sdim/// struct A { struct B { struct C {}; }; }; 6249243830Sdim/// A::B::C c; 6250243830Sdim/// \endcode 6251243830Sdim/// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and 6252243830Sdim/// matches "A::" 6253249423SdimAST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix, 6254249423Sdim internal::Matcher<NestedNameSpecifier>, InnerMatcher, 6255249423Sdim 0) { 6256296417Sdim const NestedNameSpecifier *NextNode = Node.getPrefix(); 6257276479Sdim if (!NextNode) 6258249423Sdim return false; 6259249423Sdim return InnerMatcher.matches(*NextNode, Finder, Builder); 6260243830Sdim} 6261243830Sdim 6262341825Sdim/// Matches on the prefix of a \c NestedNameSpecifierLoc. 6263243830Sdim/// 6264243830Sdim/// Given 6265243830Sdim/// \code 6266243830Sdim/// struct A { struct B { struct C {}; }; }; 6267243830Sdim/// A::B::C c; 6268243830Sdim/// \endcode 6269243830Sdim/// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A"))))) 6270243830Sdim/// matches "A::" 6271249423SdimAST_MATCHER_P_OVERLOAD(NestedNameSpecifierLoc, hasPrefix, 6272249423Sdim internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher, 6273249423Sdim 1) { 6274249423Sdim NestedNameSpecifierLoc NextNode = Node.getPrefix(); 6275249423Sdim if (!NextNode) 6276249423Sdim return false; 6277249423Sdim return InnerMatcher.matches(NextNode, Finder, Builder); 6278243830Sdim} 6279243830Sdim 6280341825Sdim/// Matches nested name specifiers that specify a namespace matching the 6281243830Sdim/// given namespace matcher. 6282243830Sdim/// 6283243830Sdim/// Given 6284243830Sdim/// \code 6285243830Sdim/// namespace ns { struct A {}; } 6286243830Sdim/// ns::A a; 6287243830Sdim/// \endcode 6288243830Sdim/// nestedNameSpecifier(specifiesNamespace(hasName("ns"))) 6289243830Sdim/// matches "ns::" 6290243830SdimAST_MATCHER_P(NestedNameSpecifier, specifiesNamespace, 6291243830Sdim internal::Matcher<NamespaceDecl>, InnerMatcher) { 6292276479Sdim if (!Node.getAsNamespace()) 6293243830Sdim return false; 6294243830Sdim return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder); 6295243830Sdim} 6296243830Sdim 6297341825Sdim/// Overloads for the \c equalsNode matcher. 6298249423Sdim/// FIXME: Implement for other node types. 6299249423Sdim/// @{ 6300249423Sdim 6301341825Sdim/// Matches if a node equals another node. 6302249423Sdim/// 6303249423Sdim/// \c Decl has pointer identity in the AST. 6304276479SdimAST_MATCHER_P_OVERLOAD(Decl, equalsNode, const Decl*, Other, 0) { 6305249423Sdim return &Node == Other; 6306249423Sdim} 6307341825Sdim/// Matches if a node equals another node. 6308249423Sdim/// 6309249423Sdim/// \c Stmt has pointer identity in the AST. 6310276479SdimAST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) { 6311249423Sdim return &Node == Other; 6312249423Sdim} 6313341825Sdim/// Matches if a node equals another node. 6314296417Sdim/// 6315296417Sdim/// \c Type has pointer identity in the AST. 6316296417SdimAST_MATCHER_P_OVERLOAD(Type, equalsNode, const Type*, Other, 2) { 6317296417Sdim return &Node == Other; 6318296417Sdim} 6319249423Sdim 6320249423Sdim/// @} 6321249423Sdim 6322341825Sdim/// Matches each case or default statement belonging to the given switch 6323261991Sdim/// statement. This matcher may produce multiple matches. 6324261991Sdim/// 6325261991Sdim/// Given 6326261991Sdim/// \code 6327261991Sdim/// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } } 6328261991Sdim/// \endcode 6329261991Sdim/// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s") 6330261991Sdim/// matches four times, with "c" binding each of "case 1:", "case 2:", 6331261991Sdim/// "case 3:" and "case 4:", and "s" respectively binding "switch (1)", 6332261991Sdim/// "switch (1)", "switch (2)" and "switch (2)". 6333261991SdimAST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>, 6334261991Sdim InnerMatcher) { 6335261991Sdim BoundNodesTreeBuilder Result; 6336261991Sdim // FIXME: getSwitchCaseList() does not necessarily guarantee a stable 6337261991Sdim // iteration order. We should use the more general iterating matchers once 6338261991Sdim // they are capable of expressing this matcher (for example, it should ignore 6339261991Sdim // case statements belonging to nested switch statements). 6340261991Sdim bool Matched = false; 6341261991Sdim for (const SwitchCase *SC = Node.getSwitchCaseList(); SC; 6342261991Sdim SC = SC->getNextSwitchCase()) { 6343261991Sdim BoundNodesTreeBuilder CaseBuilder(*Builder); 6344261991Sdim bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder); 6345261991Sdim if (CaseMatched) { 6346261991Sdim Matched = true; 6347261991Sdim Result.addMatch(CaseBuilder); 6348261991Sdim } 6349261991Sdim } 6350280031Sdim *Builder = std::move(Result); 6351261991Sdim return Matched; 6352261991Sdim} 6353261991Sdim 6354341825Sdim/// Matches each constructor initializer in a constructor definition. 6355261991Sdim/// 6356261991Sdim/// Given 6357261991Sdim/// \code 6358261991Sdim/// class A { A() : i(42), j(42) {} int i; int j; }; 6359261991Sdim/// \endcode 6360296417Sdim/// cxxConstructorDecl(forEachConstructorInitializer( 6361296417Sdim/// forField(decl().bind("x")) 6362296417Sdim/// )) 6363261991Sdim/// will trigger two matches, binding for 'i' and 'j' respectively. 6364261991SdimAST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer, 6365261991Sdim internal::Matcher<CXXCtorInitializer>, InnerMatcher) { 6366261991Sdim BoundNodesTreeBuilder Result; 6367261991Sdim bool Matched = false; 6368276479Sdim for (const auto *I : Node.inits()) { 6369261991Sdim BoundNodesTreeBuilder InitBuilder(*Builder); 6370276479Sdim if (InnerMatcher.matches(*I, Finder, &InitBuilder)) { 6371261991Sdim Matched = true; 6372261991Sdim Result.addMatch(InitBuilder); 6373261991Sdim } 6374261991Sdim } 6375280031Sdim *Builder = std::move(Result); 6376261991Sdim return Matched; 6377261991Sdim} 6378261991Sdim 6379341825Sdim/// Matches constructor declarations that are copy constructors. 6380296417Sdim/// 6381296417Sdim/// Given 6382296417Sdim/// \code 6383296417Sdim/// struct S { 6384296417Sdim/// S(); // #1 6385296417Sdim/// S(const S &); // #2 6386296417Sdim/// S(S &&); // #3 6387296417Sdim/// }; 6388296417Sdim/// \endcode 6389296417Sdim/// cxxConstructorDecl(isCopyConstructor()) will match #2, but not #1 or #3. 6390296417SdimAST_MATCHER(CXXConstructorDecl, isCopyConstructor) { 6391296417Sdim return Node.isCopyConstructor(); 6392296417Sdim} 6393296417Sdim 6394341825Sdim/// Matches constructor declarations that are move constructors. 6395296417Sdim/// 6396296417Sdim/// Given 6397296417Sdim/// \code 6398296417Sdim/// struct S { 6399296417Sdim/// S(); // #1 6400296417Sdim/// S(const S &); // #2 6401296417Sdim/// S(S &&); // #3 6402296417Sdim/// }; 6403296417Sdim/// \endcode 6404296417Sdim/// cxxConstructorDecl(isMoveConstructor()) will match #3, but not #1 or #2. 6405296417SdimAST_MATCHER(CXXConstructorDecl, isMoveConstructor) { 6406296417Sdim return Node.isMoveConstructor(); 6407296417Sdim} 6408296417Sdim 6409341825Sdim/// Matches constructor declarations that are default constructors. 6410296417Sdim/// 6411296417Sdim/// Given 6412296417Sdim/// \code 6413296417Sdim/// struct S { 6414296417Sdim/// S(); // #1 6415296417Sdim/// S(const S &); // #2 6416296417Sdim/// S(S &&); // #3 6417296417Sdim/// }; 6418296417Sdim/// \endcode 6419296417Sdim/// cxxConstructorDecl(isDefaultConstructor()) will match #1, but not #2 or #3. 6420296417SdimAST_MATCHER(CXXConstructorDecl, isDefaultConstructor) { 6421296417Sdim return Node.isDefaultConstructor(); 6422296417Sdim} 6423296417Sdim 6424341825Sdim/// Matches constructors that delegate to another constructor. 6425309124Sdim/// 6426309124Sdim/// Given 6427309124Sdim/// \code 6428309124Sdim/// struct S { 6429309124Sdim/// S(); // #1 6430309124Sdim/// S(int) {} // #2 6431309124Sdim/// S(S &&) : S() {} // #3 6432309124Sdim/// }; 6433309124Sdim/// S::S() : S(0) {} // #4 6434309124Sdim/// \endcode 6435309124Sdim/// cxxConstructorDecl(isDelegatingConstructor()) will match #3 and #4, but not 6436309124Sdim/// #1 or #2. 6437309124SdimAST_MATCHER(CXXConstructorDecl, isDelegatingConstructor) { 6438309124Sdim return Node.isDelegatingConstructor(); 6439309124Sdim} 6440309124Sdim 6441353358Sdim/// Matches constructor, conversion function, and deduction guide declarations 6442353358Sdim/// that have an explicit specifier if this explicit specifier is resolved to 6443353358Sdim/// true. 6444296417Sdim/// 6445296417Sdim/// Given 6446296417Sdim/// \code 6447353358Sdim/// template<bool b> 6448296417Sdim/// struct S { 6449296417Sdim/// S(int); // #1 6450296417Sdim/// explicit S(double); // #2 6451296417Sdim/// operator int(); // #3 6452296417Sdim/// explicit operator bool(); // #4 6453353358Sdim/// explicit(false) S(bool) // # 7 6454353358Sdim/// explicit(true) S(char) // # 8 6455353358Sdim/// explicit(b) S(S) // # 9 6456296417Sdim/// }; 6457353358Sdim/// S(int) -> S<true> // #5 6458353358Sdim/// explicit S(double) -> S<false> // #6 6459296417Sdim/// \endcode 6460353358Sdim/// cxxConstructorDecl(isExplicit()) will match #2 and #8, but not #1, #7 or #9. 6461296417Sdim/// cxxConversionDecl(isExplicit()) will match #4, but not #3. 6462353358Sdim/// cxxDeductionGuideDecl(isExplicit()) will match #6, but not #5. 6463353358SdimAST_POLYMORPHIC_MATCHER(isExplicit, AST_POLYMORPHIC_SUPPORTED_TYPES( 6464353358Sdim CXXConstructorDecl, CXXConversionDecl, 6465353358Sdim CXXDeductionGuideDecl)) { 6466296417Sdim return Node.isExplicit(); 6467296417Sdim} 6468296417Sdim 6469353358Sdim/// Matches the expression in an explicit specifier if present in the given 6470353358Sdim/// declaration. 6471353358Sdim/// 6472353358Sdim/// Given 6473353358Sdim/// \code 6474353358Sdim/// template<bool b> 6475353358Sdim/// struct S { 6476353358Sdim/// S(int); // #1 6477353358Sdim/// explicit S(double); // #2 6478353358Sdim/// operator int(); // #3 6479353358Sdim/// explicit operator bool(); // #4 6480353358Sdim/// explicit(false) S(bool) // # 7 6481353358Sdim/// explicit(true) S(char) // # 8 6482353358Sdim/// explicit(b) S(S) // # 9 6483353358Sdim/// }; 6484353358Sdim/// S(int) -> S<true> // #5 6485353358Sdim/// explicit S(double) -> S<false> // #6 6486353358Sdim/// \endcode 6487353358Sdim/// cxxConstructorDecl(hasExplicitSpecifier(constantExpr())) will match #7, #8 and #9, but not #1 or #2. 6488353358Sdim/// cxxConversionDecl(hasExplicitSpecifier(constantExpr())) will not match #3 or #4. 6489353358Sdim/// cxxDeductionGuideDecl(hasExplicitSpecifier(constantExpr())) will not match #5 or #6. 6490353358SdimAST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, internal::Matcher<Expr>, 6491353358Sdim InnerMatcher) { 6492353358Sdim ExplicitSpecifier ES = ExplicitSpecifier::getFromDecl(&Node); 6493353358Sdim if (!ES.getExpr()) 6494353358Sdim return false; 6495353358Sdim return InnerMatcher.matches(*ES.getExpr(), Finder, Builder); 6496353358Sdim} 6497353358Sdim 6498341825Sdim/// Matches function and namespace declarations that are marked with 6499296417Sdim/// the inline keyword. 6500296417Sdim/// 6501296417Sdim/// Given 6502296417Sdim/// \code 6503296417Sdim/// inline void f(); 6504296417Sdim/// void g(); 6505296417Sdim/// namespace n { 6506296417Sdim/// inline namespace m {} 6507296417Sdim/// } 6508296417Sdim/// \endcode 6509296417Sdim/// functionDecl(isInline()) will match ::f(). 6510296417Sdim/// namespaceDecl(isInline()) will match n::m. 6511296417SdimAST_POLYMORPHIC_MATCHER(isInline, 6512296417Sdim AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl, 6513296417Sdim FunctionDecl)) { 6514296417Sdim // This is required because the spelling of the function used to determine 6515296417Sdim // whether inline is specified or not differs between the polymorphic types. 6516296417Sdim if (const auto *FD = dyn_cast<FunctionDecl>(&Node)) 6517296417Sdim return FD->isInlineSpecified(); 6518296417Sdim else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node)) 6519296417Sdim return NSD->isInline(); 6520296417Sdim llvm_unreachable("Not a valid polymorphic type"); 6521296417Sdim} 6522296417Sdim 6523341825Sdim/// Matches anonymous namespace declarations. 6524296417Sdim/// 6525296417Sdim/// Given 6526296417Sdim/// \code 6527296417Sdim/// namespace n { 6528296417Sdim/// namespace {} // #1 6529296417Sdim/// } 6530296417Sdim/// \endcode 6531296417Sdim/// namespaceDecl(isAnonymous()) will match #1 but not ::n. 6532296417SdimAST_MATCHER(NamespaceDecl, isAnonymous) { 6533296417Sdim return Node.isAnonymousNamespace(); 6534296417Sdim} 6535296417Sdim 6536353358Sdim/// Matches declarations in the namespace `std`, but not in nested namespaces. 6537353358Sdim/// 6538353358Sdim/// Given 6539353358Sdim/// \code 6540353358Sdim/// class vector {}; 6541353358Sdim/// namespace foo { 6542353358Sdim/// class vector {}; 6543353358Sdim/// namespace std { 6544353358Sdim/// class vector {}; 6545353358Sdim/// } 6546353358Sdim/// } 6547353358Sdim/// namespace std { 6548353358Sdim/// inline namespace __1 { 6549353358Sdim/// class vector {}; // #1 6550353358Sdim/// namespace experimental { 6551353358Sdim/// class vector {}; 6552353358Sdim/// } 6553353358Sdim/// } 6554353358Sdim/// } 6555353358Sdim/// \endcode 6556353358Sdim/// cxxRecordDecl(hasName("vector"), isInStdNamespace()) will match only #1. 6557353358SdimAST_MATCHER(Decl, isInStdNamespace) { return Node.isInStdNamespace(); } 6558353358Sdim 6559341825Sdim/// If the given case statement does not use the GNU case range 6560261991Sdim/// extension, matches the constant given in the statement. 6561261991Sdim/// 6562261991Sdim/// Given 6563261991Sdim/// \code 6564261991Sdim/// switch (1) { case 1: case 1+1: case 3 ... 4: ; } 6565261991Sdim/// \endcode 6566261991Sdim/// caseStmt(hasCaseConstant(integerLiteral())) 6567261991Sdim/// matches "case 1:" 6568261991SdimAST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>, 6569261991Sdim InnerMatcher) { 6570261991Sdim if (Node.getRHS()) 6571261991Sdim return false; 6572261991Sdim 6573261991Sdim return InnerMatcher.matches(*Node.getLHS(), Finder, Builder); 6574261991Sdim} 6575261991Sdim 6576341825Sdim/// Matches declaration that has a given attribute. 6577280031Sdim/// 6578280031Sdim/// Given 6579280031Sdim/// \code 6580280031Sdim/// __attribute__((device)) void f() { ... } 6581280031Sdim/// \endcode 6582280031Sdim/// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of 6583353358Sdim/// f. If the matcher is used from clang-query, attr::Kind parameter should be 6584296417Sdim/// passed as a quoted string. e.g., hasAttr("attr::CUDADevice"). 6585280031SdimAST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) { 6586280031Sdim for (const auto *Attr : Node.attrs()) { 6587280031Sdim if (Attr->getKind() == AttrKind) 6588280031Sdim return true; 6589280031Sdim } 6590280031Sdim return false; 6591280031Sdim} 6592280031Sdim 6593341825Sdim/// Matches the return value expression of a return statement 6594309124Sdim/// 6595309124Sdim/// Given 6596309124Sdim/// \code 6597309124Sdim/// return a + b; 6598309124Sdim/// \endcode 6599309124Sdim/// hasReturnValue(binaryOperator()) 6600309124Sdim/// matches 'return a + b' 6601309124Sdim/// with binaryOperator() 6602309124Sdim/// matching 'a + b' 6603309124SdimAST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher<Expr>, 6604309124Sdim InnerMatcher) { 6605309124Sdim if (const auto *RetValue = Node.getRetValue()) 6606309124Sdim return InnerMatcher.matches(*RetValue, Finder, Builder); 6607309124Sdim return false; 6608309124Sdim} 6609309124Sdim 6610341825Sdim/// Matches CUDA kernel call expression. 6611280031Sdim/// 6612280031Sdim/// Example matches, 6613280031Sdim/// \code 6614280031Sdim/// kernel<<<i,j>>>(); 6615280031Sdim/// \endcode 6616327952Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr> 6617327952Sdim cudaKernelCallExpr; 6618280031Sdim 6619341825Sdim/// Matches expressions that resolve to a null pointer constant, such as 6620309124Sdim/// GNU's __null, C++11's nullptr, or C's NULL macro. 6621309124Sdim/// 6622309124Sdim/// Given: 6623309124Sdim/// \code 6624309124Sdim/// void *v1 = NULL; 6625309124Sdim/// void *v2 = nullptr; 6626309124Sdim/// void *v3 = __null; // GNU extension 6627309124Sdim/// char *cp = (char *)0; 6628309124Sdim/// int *ip = 0; 6629309124Sdim/// int i = 0; 6630309124Sdim/// \endcode 6631309124Sdim/// expr(nullPointerConstant()) 6632309124Sdim/// matches the initializer for v1, v2, v3, cp, and ip. Does not match the 6633309124Sdim/// initializer for i. 6634360784SdimAST_MATCHER(Expr, nullPointerConstant) { 6635360784Sdim return Node.isNullPointerConstant(Finder->getASTContext(), 6636360784Sdim Expr::NPC_ValueDependentIsNull); 6637309124Sdim} 6638309124Sdim 6639341825Sdim/// Matches declaration of the function the statement belongs to 6640309124Sdim/// 6641309124Sdim/// Given: 6642309124Sdim/// \code 6643309124Sdim/// F& operator=(const F& o) { 6644309124Sdim/// std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; }); 6645309124Sdim/// return *this; 6646309124Sdim/// } 6647309124Sdim/// \endcode 6648309124Sdim/// returnStmt(forFunction(hasName("operator="))) 6649309124Sdim/// matches 'return *this' 6650360784Sdim/// but does not match 'return v > 0' 6651309124SdimAST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>, 6652309124Sdim InnerMatcher) { 6653309124Sdim const auto &Parents = Finder->getASTContext().getParents(Node); 6654309124Sdim 6655309124Sdim llvm::SmallVector<ast_type_traits::DynTypedNode, 8> Stack(Parents.begin(), 6656309124Sdim Parents.end()); 6657309124Sdim while(!Stack.empty()) { 6658309124Sdim const auto &CurNode = Stack.back(); 6659309124Sdim Stack.pop_back(); 6660309124Sdim if(const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) { 6661309124Sdim if(InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) { 6662309124Sdim return true; 6663309124Sdim } 6664309124Sdim } else if(const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) { 6665309124Sdim if(InnerMatcher.matches(*LambdaExprNode->getCallOperator(), 6666309124Sdim Finder, Builder)) { 6667309124Sdim return true; 6668309124Sdim } 6669309124Sdim } else { 6670309124Sdim for(const auto &Parent: Finder->getASTContext().getParents(CurNode)) 6671309124Sdim Stack.push_back(Parent); 6672309124Sdim } 6673309124Sdim } 6674309124Sdim return false; 6675309124Sdim} 6676309124Sdim 6677341825Sdim/// Matches a declaration that has external formal linkage. 6678314564Sdim/// 6679314564Sdim/// Example matches only z (matcher = varDecl(hasExternalFormalLinkage())) 6680314564Sdim/// \code 6681314564Sdim/// void f() { 6682314564Sdim/// int x; 6683314564Sdim/// static int y; 6684314564Sdim/// } 6685314564Sdim/// int z; 6686314564Sdim/// \endcode 6687314564Sdim/// 6688314564Sdim/// Example matches f() because it has external formal linkage despite being 6689314564Sdim/// unique to the translation unit as though it has internal likage 6690314564Sdim/// (matcher = functionDecl(hasExternalFormalLinkage())) 6691314564Sdim/// 6692314564Sdim/// \code 6693314564Sdim/// namespace { 6694314564Sdim/// void f() {} 6695314564Sdim/// } 6696314564Sdim/// \endcode 6697314564SdimAST_MATCHER(NamedDecl, hasExternalFormalLinkage) { 6698314564Sdim return Node.hasExternalFormalLinkage(); 6699314564Sdim} 6700314564Sdim 6701341825Sdim/// Matches a declaration that has default arguments. 6702327952Sdim/// 6703327952Sdim/// Example matches y (matcher = parmVarDecl(hasDefaultArgument())) 6704327952Sdim/// \code 6705327952Sdim/// void x(int val) {} 6706327952Sdim/// void y(int val = 0) {} 6707327952Sdim/// \endcode 6708360784Sdim/// 6709360784Sdim/// Deprecated. Use hasInitializer() instead to be able to 6710360784Sdim/// match on the contents of the default argument. For example: 6711360784Sdim/// 6712360784Sdim/// \code 6713360784Sdim/// void x(int val = 7) {} 6714360784Sdim/// void y(int val = 42) {} 6715360784Sdim/// \endcode 6716360784Sdim/// parmVarDecl(hasInitializer(integerLiteral(equals(42)))) 6717360784Sdim/// matches the parameter of y 6718360784Sdim/// 6719360784Sdim/// A matcher such as 6720360784Sdim/// parmVarDecl(hasInitializer(anything())) 6721360784Sdim/// is equivalent to parmVarDecl(hasDefaultArgument()). 6722341825SdimAST_MATCHER(ParmVarDecl, hasDefaultArgument) { 6723341825Sdim return Node.hasDefaultArg(); 6724327952Sdim} 6725239313Sdim 6726341825Sdim/// Matches array new expressions. 6727327952Sdim/// 6728327952Sdim/// Given: 6729327952Sdim/// \code 6730327952Sdim/// MyClass *p1 = new MyClass[10]; 6731327952Sdim/// \endcode 6732327952Sdim/// cxxNewExpr(isArray()) 6733327952Sdim/// matches the expression 'new MyClass[10]'. 6734327952SdimAST_MATCHER(CXXNewExpr, isArray) { 6735327952Sdim return Node.isArray(); 6736327952Sdim} 6737327952Sdim 6738341825Sdim/// Matches array new expressions with a given array size. 6739327952Sdim/// 6740327952Sdim/// Given: 6741327952Sdim/// \code 6742327952Sdim/// MyClass *p1 = new MyClass[10]; 6743327952Sdim/// \endcode 6744360784Sdim/// cxxNewExpr(hasArraySize(integerLiteral(equals(10)))) 6745327952Sdim/// matches the expression 'new MyClass[10]'. 6746327952SdimAST_MATCHER_P(CXXNewExpr, hasArraySize, internal::Matcher<Expr>, InnerMatcher) { 6747353358Sdim return Node.isArray() && *Node.getArraySize() && 6748353358Sdim InnerMatcher.matches(**Node.getArraySize(), Finder, Builder); 6749327952Sdim} 6750327952Sdim 6751341825Sdim/// Matches a class declaration that is defined. 6752327952Sdim/// 6753327952Sdim/// Example matches x (matcher = cxxRecordDecl(hasDefinition())) 6754327952Sdim/// \code 6755327952Sdim/// class x {}; 6756327952Sdim/// class y; 6757327952Sdim/// \endcode 6758327952SdimAST_MATCHER(CXXRecordDecl, hasDefinition) { 6759327952Sdim return Node.hasDefinition(); 6760327952Sdim} 6761327952Sdim 6762341825Sdim/// Matches C++11 scoped enum declaration. 6763341825Sdim/// 6764341825Sdim/// Example matches Y (matcher = enumDecl(isScoped())) 6765341825Sdim/// \code 6766341825Sdim/// enum X {}; 6767341825Sdim/// enum class Y {}; 6768341825Sdim/// \endcode 6769341825SdimAST_MATCHER(EnumDecl, isScoped) { 6770341825Sdim return Node.isScoped(); 6771341825Sdim} 6772341825Sdim 6773341825Sdim/// Matches a function declared with a trailing return type. 6774341825Sdim/// 6775341825Sdim/// Example matches Y (matcher = functionDecl(hasTrailingReturn())) 6776341825Sdim/// \code 6777341825Sdim/// int X() {} 6778341825Sdim/// auto Y() -> int {} 6779341825Sdim/// \endcode 6780341825SdimAST_MATCHER(FunctionDecl, hasTrailingReturn) { 6781341825Sdim if (const auto *F = Node.getType()->getAs<FunctionProtoType>()) 6782341825Sdim return F->hasTrailingReturn(); 6783341825Sdim return false; 6784341825Sdim} 6785341825Sdim 6786353358Sdim/// Matches expressions that match InnerMatcher that are possibly wrapped in an 6787360784Sdim/// elidable constructor and other corresponding bookkeeping nodes. 6788353358Sdim/// 6789360784Sdim/// In C++17, elidable copy constructors are no longer being generated in the 6790360784Sdim/// AST as it is not permitted by the standard. They are, however, part of the 6791360784Sdim/// AST in C++14 and earlier. So, a matcher must abstract over these differences 6792360784Sdim/// to work in all language modes. This matcher skips elidable constructor-call 6793360784Sdim/// AST nodes, `ExprWithCleanups` nodes wrapping elidable constructor-calls and 6794360784Sdim/// various implicit nodes inside the constructor calls, all of which will not 6795360784Sdim/// appear in the C++17 AST. 6796353358Sdim/// 6797353358Sdim/// Given 6798353358Sdim/// 6799353358Sdim/// \code 6800353358Sdim/// struct H {}; 6801353358Sdim/// H G(); 6802353358Sdim/// void f() { 6803353358Sdim/// H D = G(); 6804353358Sdim/// } 6805353358Sdim/// \endcode 6806353358Sdim/// 6807360784Sdim/// ``varDecl(hasInitializer(ignoringElidableConstructorCall(callExpr())))`` 6808360784Sdim/// matches ``H D = G()`` in C++11 through C++17 (and beyond). 6809353358SdimAST_MATCHER_P(Expr, ignoringElidableConstructorCall, 6810353358Sdim ast_matchers::internal::Matcher<Expr>, InnerMatcher) { 6811360784Sdim // E tracks the node that we are examining. 6812360784Sdim const Expr *E = &Node; 6813360784Sdim // If present, remove an outer `ExprWithCleanups` corresponding to the 6814360784Sdim // underlying `CXXConstructExpr`. This check won't cover all cases of added 6815360784Sdim // `ExprWithCleanups` corresponding to `CXXConstructExpr` nodes (because the 6816360784Sdim // EWC is placed on the outermost node of the expression, which this may not 6817360784Sdim // be), but, it still improves the coverage of this matcher. 6818360784Sdim if (const auto *CleanupsExpr = dyn_cast<ExprWithCleanups>(&Node)) 6819360784Sdim E = CleanupsExpr->getSubExpr(); 6820360784Sdim if (const auto *CtorExpr = dyn_cast<CXXConstructExpr>(E)) { 6821353358Sdim if (CtorExpr->isElidable()) { 6822353358Sdim if (const auto *MaterializeTemp = 6823353358Sdim dyn_cast<MaterializeTemporaryExpr>(CtorExpr->getArg(0))) { 6824360784Sdim return InnerMatcher.matches(*MaterializeTemp->getSubExpr(), Finder, 6825360784Sdim Builder); 6826353358Sdim } 6827353358Sdim } 6828353358Sdim } 6829353358Sdim return InnerMatcher.matches(Node, Finder, Builder); 6830353358Sdim} 6831353358Sdim 6832353358Sdim//----------------------------------------------------------------------------// 6833353358Sdim// OpenMP handling. 6834353358Sdim//----------------------------------------------------------------------------// 6835353358Sdim 6836353358Sdim/// Matches any ``#pragma omp`` executable directive. 6837353358Sdim/// 6838353358Sdim/// Given 6839353358Sdim/// 6840353358Sdim/// \code 6841353358Sdim/// #pragma omp parallel 6842353358Sdim/// #pragma omp parallel default(none) 6843353358Sdim/// #pragma omp taskyield 6844353358Sdim/// \endcode 6845353358Sdim/// 6846353358Sdim/// ``ompExecutableDirective()`` matches ``omp parallel``, 6847353358Sdim/// ``omp parallel default(none)`` and ``omp taskyield``. 6848353358Sdimextern const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective> 6849353358Sdim ompExecutableDirective; 6850353358Sdim 6851353358Sdim/// Matches standalone OpenMP directives, 6852353358Sdim/// i.e., directives that can't have a structured block. 6853353358Sdim/// 6854353358Sdim/// Given 6855353358Sdim/// 6856353358Sdim/// \code 6857353358Sdim/// #pragma omp parallel 6858353358Sdim/// {} 6859353358Sdim/// #pragma omp taskyield 6860353358Sdim/// \endcode 6861353358Sdim/// 6862353358Sdim/// ``ompExecutableDirective(isStandaloneDirective()))`` matches 6863353358Sdim/// ``omp taskyield``. 6864353358SdimAST_MATCHER(OMPExecutableDirective, isStandaloneDirective) { 6865353358Sdim return Node.isStandaloneDirective(); 6866353358Sdim} 6867353358Sdim 6868353358Sdim/// Matches the Stmt AST node that is marked as being the structured-block 6869353358Sdim/// of an OpenMP executable directive. 6870353358Sdim/// 6871353358Sdim/// Given 6872353358Sdim/// 6873353358Sdim/// \code 6874353358Sdim/// #pragma omp parallel 6875353358Sdim/// {} 6876353358Sdim/// \endcode 6877353358Sdim/// 6878353358Sdim/// ``stmt(isOMPStructuredBlock()))`` matches ``{}``. 6879353358SdimAST_MATCHER(Stmt, isOMPStructuredBlock) { return Node.isOMPStructuredBlock(); } 6880353358Sdim 6881353358Sdim/// Matches the structured-block of the OpenMP executable directive 6882353358Sdim/// 6883353358Sdim/// Prerequisite: the executable directive must not be standalone directive. 6884353358Sdim/// If it is, it will never match. 6885353358Sdim/// 6886353358Sdim/// Given 6887353358Sdim/// 6888353358Sdim/// \code 6889353358Sdim/// #pragma omp parallel 6890353358Sdim/// ; 6891353358Sdim/// #pragma omp parallel 6892353358Sdim/// {} 6893353358Sdim/// \endcode 6894353358Sdim/// 6895353358Sdim/// ``ompExecutableDirective(hasStructuredBlock(nullStmt()))`` will match ``;`` 6896353358SdimAST_MATCHER_P(OMPExecutableDirective, hasStructuredBlock, 6897353358Sdim internal::Matcher<Stmt>, InnerMatcher) { 6898353358Sdim if (Node.isStandaloneDirective()) 6899353358Sdim return false; // Standalone directives have no structured blocks. 6900353358Sdim return InnerMatcher.matches(*Node.getStructuredBlock(), Finder, Builder); 6901353358Sdim} 6902353358Sdim 6903353358Sdim/// Matches any clause in an OpenMP directive. 6904353358Sdim/// 6905353358Sdim/// Given 6906353358Sdim/// 6907353358Sdim/// \code 6908353358Sdim/// #pragma omp parallel 6909353358Sdim/// #pragma omp parallel default(none) 6910353358Sdim/// \endcode 6911353358Sdim/// 6912353358Sdim/// ``ompExecutableDirective(hasAnyClause(anything()))`` matches 6913353358Sdim/// ``omp parallel default(none)``. 6914353358SdimAST_MATCHER_P(OMPExecutableDirective, hasAnyClause, 6915353358Sdim internal::Matcher<OMPClause>, InnerMatcher) { 6916353358Sdim ArrayRef<OMPClause *> Clauses = Node.clauses(); 6917353358Sdim return matchesFirstInPointerRange(InnerMatcher, Clauses.begin(), 6918353358Sdim Clauses.end(), Finder, Builder); 6919353358Sdim} 6920353358Sdim 6921353358Sdim/// Matches OpenMP ``default`` clause. 6922353358Sdim/// 6923353358Sdim/// Given 6924353358Sdim/// 6925353358Sdim/// \code 6926353358Sdim/// #pragma omp parallel default(none) 6927353358Sdim/// #pragma omp parallel default(shared) 6928353358Sdim/// #pragma omp parallel 6929353358Sdim/// \endcode 6930353358Sdim/// 6931353358Sdim/// ``ompDefaultClause()`` matches ``default(none)`` and ``default(shared)``. 6932353358Sdimextern const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPDefaultClause> 6933353358Sdim ompDefaultClause; 6934353358Sdim 6935353358Sdim/// Matches if the OpenMP ``default`` clause has ``none`` kind specified. 6936353358Sdim/// 6937353358Sdim/// Given 6938353358Sdim/// 6939353358Sdim/// \code 6940353358Sdim/// #pragma omp parallel 6941353358Sdim/// #pragma omp parallel default(none) 6942353358Sdim/// #pragma omp parallel default(shared) 6943353358Sdim/// \endcode 6944353358Sdim/// 6945353358Sdim/// ``ompDefaultClause(isNoneKind())`` matches only ``default(none)``. 6946353358SdimAST_MATCHER(OMPDefaultClause, isNoneKind) { 6947353358Sdim return Node.getDefaultKind() == OMPC_DEFAULT_none; 6948353358Sdim} 6949353358Sdim 6950353358Sdim/// Matches if the OpenMP ``default`` clause has ``shared`` kind specified. 6951353358Sdim/// 6952353358Sdim/// Given 6953353358Sdim/// 6954353358Sdim/// \code 6955353358Sdim/// #pragma omp parallel 6956353358Sdim/// #pragma omp parallel default(none) 6957353358Sdim/// #pragma omp parallel default(shared) 6958353358Sdim/// \endcode 6959353358Sdim/// 6960353358Sdim/// ``ompDefaultClause(isSharedKind())`` matches only ``default(shared)``. 6961353358SdimAST_MATCHER(OMPDefaultClause, isSharedKind) { 6962353358Sdim return Node.getDefaultKind() == OMPC_DEFAULT_shared; 6963353358Sdim} 6964353358Sdim 6965353358Sdim/// Matches if the OpenMP directive is allowed to contain the specified OpenMP 6966353358Sdim/// clause kind. 6967353358Sdim/// 6968353358Sdim/// Given 6969353358Sdim/// 6970353358Sdim/// \code 6971353358Sdim/// #pragma omp parallel 6972353358Sdim/// #pragma omp parallel for 6973353358Sdim/// #pragma omp for 6974353358Sdim/// \endcode 6975353358Sdim/// 6976353358Sdim/// `ompExecutableDirective(isAllowedToContainClause(OMPC_default))`` matches 6977353358Sdim/// ``omp parallel`` and ``omp parallel for``. 6978353358Sdim/// 6979353358Sdim/// If the matcher is use from clang-query, ``OpenMPClauseKind`` parameter 6980353358Sdim/// should be passed as a quoted string. e.g., 6981353358Sdim/// ``isAllowedToContainClauseKind("OMPC_default").`` 6982353358SdimAST_MATCHER_P(OMPExecutableDirective, isAllowedToContainClauseKind, 6983353358Sdim OpenMPClauseKind, CKind) { 6984360784Sdim return isAllowedClauseForDirective( 6985360784Sdim Node.getDirectiveKind(), CKind, 6986360784Sdim Finder->getASTContext().getLangOpts().OpenMP); 6987353358Sdim} 6988353358Sdim 6989353358Sdim//----------------------------------------------------------------------------// 6990353358Sdim// End OpenMP handling. 6991353358Sdim//----------------------------------------------------------------------------// 6992353358Sdim 6993327952Sdim} // namespace ast_matchers 6994327952Sdim} // namespace clang 6995327952Sdim 6996327952Sdim#endif // LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H 6997