Home | History | Annotate | Download | only in ASTMatchers
      1 //===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file implements matchers to be used together with the MatchFinder to
     11 //  match AST nodes.
     12 //
     13 //  Matchers are created by generator functions, which can be combined in
     14 //  a functional in-language DSL to express queries over the C++ AST.
     15 //
     16 //  For example, to match a class with a certain name, one would call:
     17 //    cxxRecordDecl(hasName("MyClass"))
     18 //  which returns a matcher that can be used to find all AST nodes that declare
     19 //  a class named 'MyClass'.
     20 //
     21 //  For more complicated match expressions we're often interested in accessing
     22 //  multiple parts of the matched AST nodes once a match is found. In that case,
     23 //  use the id(...) matcher around the match expressions that match the nodes
     24 //  you want to access.
     25 //
     26 //  For example, when we're interested in child classes of a certain class, we
     27 //  would write:
     28 //    cxxRecordDecl(hasName("MyClass"), has(id("child", recordDecl())))
     29 //  When the match is found via the MatchFinder, a user provided callback will
     30 //  be called with a BoundNodes instance that contains a mapping from the
     31 //  strings that we provided for the id(...) calls to the nodes that were
     32 //  matched.
     33 //  In the given example, each time our matcher finds a match we get a callback
     34 //  where "child" is bound to the RecordDecl node of the matching child
     35 //  class declaration.
     36 //
     37 //  See ASTMatchersInternal.h for a more in-depth explanation of the
     38 //  implementation details of the matcher framework.
     39 //
     40 //  See ASTMatchFinder.h for how to use the generated matchers to run over
     41 //  an AST.
     42 //
     43 //===----------------------------------------------------------------------===//
     44 
     45 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
     46 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
     47 
     48 #include "clang/AST/ASTContext.h"
     49 #include "clang/AST/DeclFriend.h"
     50 #include "clang/AST/DeclObjC.h"
     51 #include "clang/AST/DeclTemplate.h"
     52 #include "clang/ASTMatchers/ASTMatchersInternal.h"
     53 #include "clang/ASTMatchers/ASTMatchersMacros.h"
     54 #include "llvm/Support/Regex.h"
     55 #include <iterator>
     56 
     57 namespace clang {
     58 namespace ast_matchers {
     59 
     60 /// \brief Maps string IDs to AST nodes matched by parts of a matcher.
     61 ///
     62 /// The bound nodes are generated by calling \c bind("id") on the node matchers
     63 /// of the nodes we want to access later.
     64 ///
     65 /// The instances of BoundNodes are created by \c MatchFinder when the user's
     66 /// callbacks are executed every time a match is found.
     67 class BoundNodes {
     68 public:
     69   /// \brief Returns the AST node bound to \c ID.
     70   ///
     71   /// Returns NULL if there was no node bound to \c ID or if there is a node but
     72   /// it cannot be converted to the specified type.
     73   template <typename T>
     74   const T *getNodeAs(StringRef ID) const {
     75     return MyBoundNodes.getNodeAs<T>(ID);
     76   }
     77 
     78   /// \brief Type of mapping from binding identifiers to bound nodes. This type
     79   /// is an associative container with a key type of \c std::string and a value
     80   /// type of \c clang::ast_type_traits::DynTypedNode
     81   typedef internal::BoundNodesMap::IDToNodeMap IDToNodeMap;
     82 
     83   /// \brief Retrieve mapping from binding identifiers to bound nodes.
     84   const IDToNodeMap &getMap() const {
     85     return MyBoundNodes.getMap();
     86   }
     87 
     88 private:
     89   /// \brief Create BoundNodes from a pre-filled map of bindings.
     90   BoundNodes(internal::BoundNodesMap &MyBoundNodes)
     91       : MyBoundNodes(MyBoundNodes) {}
     92 
     93   internal::BoundNodesMap MyBoundNodes;
     94 
     95   friend class internal::BoundNodesTreeBuilder;
     96 };
     97 
     98 /// \brief If the provided matcher matches a node, binds the node to \c ID.
     99 ///
    100 /// FIXME: Do we want to support this now that we have bind()?
    101 template <typename T>
    102 internal::Matcher<T> id(StringRef ID,
    103                         const internal::BindableMatcher<T> &InnerMatcher) {
    104   return InnerMatcher.bind(ID);
    105 }
    106 
    107 /// \brief Types of matchers for the top-level classes in the AST class
    108 /// hierarchy.
    109 /// @{
    110 typedef internal::Matcher<Decl> DeclarationMatcher;
    111 typedef internal::Matcher<Stmt> StatementMatcher;
    112 typedef internal::Matcher<QualType> TypeMatcher;
    113 typedef internal::Matcher<TypeLoc> TypeLocMatcher;
    114 typedef internal::Matcher<NestedNameSpecifier> NestedNameSpecifierMatcher;
    115 typedef internal::Matcher<NestedNameSpecifierLoc> NestedNameSpecifierLocMatcher;
    116 typedef internal::Matcher<CXXCtorInitializer> CXXCtorInitializerMatcher;
    117 /// @}
    118 
    119 /// \brief Matches any node.
    120 ///
    121 /// Useful when another matcher requires a child matcher, but there's no
    122 /// additional constraint. This will often be used with an explicit conversion
    123 /// to an \c internal::Matcher<> type such as \c TypeMatcher.
    124 ///
    125 /// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g.,
    126 /// \code
    127 /// "int* p" and "void f()" in
    128 ///   int* p;
    129 ///   void f();
    130 /// \endcode
    131 ///
    132 /// Usable as: Any Matcher
    133 inline internal::TrueMatcher anything() { return internal::TrueMatcher(); }
    134 
    135 /// \brief Matches the top declaration context.
    136 ///
    137 /// Given
    138 /// \code
    139 ///   int X;
    140 ///   namespace NS {
    141 ///   int Y;
    142 ///   }  // namespace NS
    143 /// \endcode
    144 /// decl(hasDeclContext(translationUnitDecl()))
    145 ///   matches "int X", but not "int Y".
    146 const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl>
    147     translationUnitDecl;
    148 
    149 /// \brief Matches typedef declarations.
    150 ///
    151 /// Given
    152 /// \code
    153 ///   typedef int X;
    154 ///   using Y = int;
    155 /// \endcode
    156 /// typedefDecl()
    157 ///   matches "typedef int X", but not "using Y = int"
    158 const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl> typedefDecl;
    159 
    160 /// \brief Matches typedef name declarations.
    161 ///
    162 /// Given
    163 /// \code
    164 ///   typedef int X;
    165 ///   using Y = int;
    166 /// \endcode
    167 /// typedefNameDecl()
    168 ///   matches "typedef int X" and "using Y = int"
    169 const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl>
    170     typedefNameDecl;
    171 
    172 /// \brief Matches type alias declarations.
    173 ///
    174 /// Given
    175 /// \code
    176 ///   typedef int X;
    177 ///   using Y = int;
    178 /// \endcode
    179 /// typeAliasDecl()
    180 ///   matches "using Y = int", but not "typedef int X"
    181 const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> typeAliasDecl;
    182 
    183 /// \brief Matches type alias template declarations.
    184 ///
    185 /// typeAliasTemplateDecl() matches
    186 /// \code
    187 ///   template <typename T>
    188 ///   using Y = X<T>;
    189 /// \endcode
    190 const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasTemplateDecl>
    191     typeAliasTemplateDecl;
    192 
    193 /// \brief Matches AST nodes that were expanded within the main-file.
    194 ///
    195 /// Example matches X but not Y
    196 ///   (matcher = cxxRecordDecl(isExpansionInMainFile())
    197 /// \code
    198 ///   #include <Y.h>
    199 ///   class X {};
    200 /// \endcode
    201 /// Y.h:
    202 /// \code
    203 ///   class Y {};
    204 /// \endcode
    205 ///
    206 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
    207 AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
    208                         AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
    209   auto &SourceManager = Finder->getASTContext().getSourceManager();
    210   return SourceManager.isInMainFile(
    211       SourceManager.getExpansionLoc(Node.getLocStart()));
    212 }
    213 
    214 /// \brief Matches AST nodes that were expanded within system-header-files.
    215 ///
    216 /// Example matches Y but not X
    217 ///     (matcher = cxxRecordDecl(isExpansionInSystemHeader())
    218 /// \code
    219 ///   #include <SystemHeader.h>
    220 ///   class X {};
    221 /// \endcode
    222 /// SystemHeader.h:
    223 /// \code
    224 ///   class Y {};
    225 /// \endcode
    226 ///
    227 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
    228 AST_POLYMORPHIC_MATCHER(isExpansionInSystemHeader,
    229                         AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
    230   auto &SourceManager = Finder->getASTContext().getSourceManager();
    231   auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
    232   if (ExpansionLoc.isInvalid()) {
    233     return false;
    234   }
    235   return SourceManager.isInSystemHeader(ExpansionLoc);
    236 }
    237 
    238 /// \brief Matches AST nodes that were expanded within files whose name is
    239 /// partially matching a given regex.
    240 ///
    241 /// Example matches Y but not X
    242 ///     (matcher = cxxRecordDecl(isExpansionInFileMatching("AST.*"))
    243 /// \code
    244 ///   #include "ASTMatcher.h"
    245 ///   class X {};
    246 /// \endcode
    247 /// ASTMatcher.h:
    248 /// \code
    249 ///   class Y {};
    250 /// \endcode
    251 ///
    252 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
    253 AST_POLYMORPHIC_MATCHER_P(isExpansionInFileMatching,
    254                           AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
    255                           std::string, RegExp) {
    256   auto &SourceManager = Finder->getASTContext().getSourceManager();
    257   auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
    258   if (ExpansionLoc.isInvalid()) {
    259     return false;
    260   }
    261   auto FileEntry =
    262       SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
    263   if (!FileEntry) {
    264     return false;
    265   }
    266 
    267   auto Filename = FileEntry->getName();
    268   llvm::Regex RE(RegExp);
    269   return RE.match(Filename);
    270 }
    271 
    272 /// \brief Matches declarations.
    273 ///
    274 /// Examples matches \c X, \c C, and the friend declaration inside \c C;
    275 /// \code
    276 ///   void X();
    277 ///   class C {
    278 ///     friend X;
    279 ///   };
    280 /// \endcode
    281 const internal::VariadicAllOfMatcher<Decl> decl;
    282 
    283 /// \brief Matches a declaration of a linkage specification.
    284 ///
    285 /// Given
    286 /// \code
    287 ///   extern "C" {}
    288 /// \endcode
    289 /// linkageSpecDecl()
    290 ///   matches "extern "C" {}"
    291 const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl>
    292     linkageSpecDecl;
    293 
    294 /// \brief Matches a declaration of anything that could have a name.
    295 ///
    296 /// Example matches \c X, \c S, the anonymous union type, \c i, and \c U;
    297 /// \code
    298 ///   typedef int X;
    299 ///   struct S {
    300 ///     union {
    301 ///       int i;
    302 ///     } U;
    303 ///   };
    304 /// \endcode
    305 const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl;
    306 
    307 /// \brief Matches a declaration of label.
    308 ///
    309 /// Given
    310 /// \code
    311 ///   goto FOO;
    312 ///   FOO: bar();
    313 /// \endcode
    314 /// labelDecl()
    315 ///   matches 'FOO:'
    316 const internal::VariadicDynCastAllOfMatcher<Decl, LabelDecl> labelDecl;
    317 
    318 /// \brief Matches a declaration of a namespace.
    319 ///
    320 /// Given
    321 /// \code
    322 ///   namespace {}
    323 ///   namespace test {}
    324 /// \endcode
    325 /// namespaceDecl()
    326 ///   matches "namespace {}" and "namespace test {}"
    327 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> namespaceDecl;
    328 
    329 /// \brief Matches a declaration of a namespace alias.
    330 ///
    331 /// Given
    332 /// \code
    333 ///   namespace test {}
    334 ///   namespace alias = ::test;
    335 /// \endcode
    336 /// namespaceAliasDecl()
    337 ///   matches "namespace alias" but not "namespace test"
    338 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceAliasDecl>
    339     namespaceAliasDecl;
    340 
    341 /// \brief Matches class, struct, and union declarations.
    342 ///
    343 /// Example matches \c X, \c Z, \c U, and \c S
    344 /// \code
    345 ///   class X;
    346 ///   template<class T> class Z {};
    347 ///   struct S {};
    348 ///   union U {};
    349 /// \endcode
    350 const internal::VariadicDynCastAllOfMatcher<
    351   Decl,
    352   RecordDecl> recordDecl;
    353 
    354 /// \brief Matches C++ class declarations.
    355 ///
    356 /// Example matches \c X, \c Z
    357 /// \code
    358 ///   class X;
    359 ///   template<class T> class Z {};
    360 /// \endcode
    361 const internal::VariadicDynCastAllOfMatcher<
    362   Decl,
    363   CXXRecordDecl> cxxRecordDecl;
    364 
    365 /// \brief Matches C++ class template declarations.
    366 ///
    367 /// Example matches \c Z
    368 /// \code
    369 ///   template<class T> class Z {};
    370 /// \endcode
    371 const internal::VariadicDynCastAllOfMatcher<
    372   Decl,
    373   ClassTemplateDecl> classTemplateDecl;
    374 
    375 /// \brief Matches C++ class template specializations.
    376 ///
    377 /// Given
    378 /// \code
    379 ///   template<typename T> class A {};
    380 ///   template<> class A<double> {};
    381 ///   A<int> a;
    382 /// \endcode
    383 /// classTemplateSpecializationDecl()
    384 ///   matches the specializations \c A<int> and \c A<double>
    385 const internal::VariadicDynCastAllOfMatcher<
    386   Decl,
    387   ClassTemplateSpecializationDecl> classTemplateSpecializationDecl;
    388 
    389 /// \brief Matches declarator declarations (field, variable, function
    390 /// and non-type template parameter declarations).
    391 ///
    392 /// Given
    393 /// \code
    394 ///   class X { int y; };
    395 /// \endcode
    396 /// declaratorDecl()
    397 ///   matches \c int y.
    398 const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl>
    399     declaratorDecl;
    400 
    401 /// \brief Matches parameter variable declarations.
    402 ///
    403 /// Given
    404 /// \code
    405 ///   void f(int x);
    406 /// \endcode
    407 /// parmVarDecl()
    408 ///   matches \c int x.
    409 const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> parmVarDecl;
    410 
    411 /// \brief Matches C++ access specifier declarations.
    412 ///
    413 /// Given
    414 /// \code
    415 ///   class C {
    416 ///   public:
    417 ///     int a;
    418 ///   };
    419 /// \endcode
    420 /// accessSpecDecl()
    421 ///   matches 'public:'
    422 const internal::VariadicDynCastAllOfMatcher<
    423   Decl,
    424   AccessSpecDecl> accessSpecDecl;
    425 
    426 /// \brief Matches constructor initializers.
    427 ///
    428 /// Examples matches \c i(42).
    429 /// \code
    430 ///   class C {
    431 ///     C() : i(42) {}
    432 ///     int i;
    433 ///   };
    434 /// \endcode
    435 const internal::VariadicAllOfMatcher<CXXCtorInitializer> cxxCtorInitializer;
    436 
    437 /// \brief Matches template arguments.
    438 ///
    439 /// Given
    440 /// \code
    441 ///   template <typename T> struct C {};
    442 ///   C<int> c;
    443 /// \endcode
    444 /// templateArgument()
    445 ///   matches 'int' in C<int>.
    446 const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument;
    447 
    448 /// \brief Matches template name.
    449 ///
    450 /// Given
    451 /// \code
    452 ///   template <typename T> class X { };
    453 ///   X<int> xi;
    454 /// \endcode
    455 /// templateName()
    456 ///   matches 'X' in X<int>.
    457 const internal::VariadicAllOfMatcher<TemplateName> templateName;
    458 
    459 /// \brief Matches non-type template parameter declarations.
    460 ///
    461 /// Given
    462 /// \code
    463 ///   template <typename T, int N> struct C {};
    464 /// \endcode
    465 /// nonTypeTemplateParmDecl()
    466 ///   matches 'N', but not 'T'.
    467 const internal::VariadicDynCastAllOfMatcher<
    468   Decl,
    469   NonTypeTemplateParmDecl> nonTypeTemplateParmDecl;
    470 
    471 /// \brief Matches template type parameter declarations.
    472 ///
    473 /// Given
    474 /// \code
    475 ///   template <typename T, int N> struct C {};
    476 /// \endcode
    477 /// templateTypeParmDecl()
    478 ///   matches 'T', but not 'N'.
    479 const internal::VariadicDynCastAllOfMatcher<
    480   Decl,
    481   TemplateTypeParmDecl> templateTypeParmDecl;
    482 
    483 /// \brief Matches public C++ declarations.
    484 ///
    485 /// Given
    486 /// \code
    487 ///   class C {
    488 ///   public:    int a;
    489 ///   protected: int b;
    490 ///   private:   int c;
    491 ///   };
    492 /// \endcode
    493 /// fieldDecl(isPublic())
    494 ///   matches 'int a;'
    495 AST_MATCHER(Decl, isPublic) {
    496   return Node.getAccess() == AS_public;
    497 }
    498 
    499 /// \brief Matches protected C++ declarations.
    500 ///
    501 /// Given
    502 /// \code
    503 ///   class C {
    504 ///   public:    int a;
    505 ///   protected: int b;
    506 ///   private:   int c;
    507 ///   };
    508 /// \endcode
    509 /// fieldDecl(isProtected())
    510 ///   matches 'int b;'
    511 AST_MATCHER(Decl, isProtected) {
    512   return Node.getAccess() == AS_protected;
    513 }
    514 
    515 /// \brief Matches private C++ declarations.
    516 ///
    517 /// Given
    518 /// \code
    519 ///   class C {
    520 ///   public:    int a;
    521 ///   protected: int b;
    522 ///   private:   int c;
    523 ///   };
    524 /// \endcode
    525 /// fieldDecl(isPrivate())
    526 ///   matches 'int c;'
    527 AST_MATCHER(Decl, isPrivate) {
    528   return Node.getAccess() == AS_private;
    529 }
    530 
    531 /// \brief Matches non-static data members that are bit-fields.
    532 ///
    533 /// Given
    534 /// \code
    535 ///   class C {
    536 ///     int a : 2;
    537 ///     int b;
    538 ///   };
    539 /// \endcode
    540 /// fieldDecl(isBitField())
    541 ///   matches 'int a;' but not 'int b;'.
    542 AST_MATCHER(FieldDecl, isBitField) {
    543   return Node.isBitField();
    544 }
    545 
    546 /// \brief Matches non-static data members that are bit-fields of the specified
    547 /// bit width.
    548 ///
    549 /// Given
    550 /// \code
    551 ///   class C {
    552 ///     int a : 2;
    553 ///     int b : 4;
    554 ///     int c : 2;
    555 ///   };
    556 /// \endcode
    557 /// fieldDecl(hasBitWidth(2))
    558 ///   matches 'int a;' and 'int c;' but not 'int b;'.
    559 AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
    560   return Node.isBitField() &&
    561          Node.getBitWidthValue(Finder->getASTContext()) == Width;
    562 }
    563 
    564 /// \brief Matches non-static data members that have an in-class initializer.
    565 ///
    566 /// Given
    567 /// \code
    568 ///   class C {
    569 ///     int a = 2;
    570 ///     int b = 3;
    571 ///     int c;
    572 ///   };
    573 /// \endcode
    574 /// fieldDecl(hasInClassInitializer(integerLiteral(equals(2))))
    575 ///   matches 'int a;' but not 'int b;'.
    576 /// fieldDecl(hasInClassInitializer(anything()))
    577 ///   matches 'int a;' and 'int b;' but not 'int c;'.
    578 AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>,
    579               InnerMatcher) {
    580   const Expr *Initializer = Node.getInClassInitializer();
    581   return (Initializer != nullptr &&
    582           InnerMatcher.matches(*Initializer, Finder, Builder));
    583 }
    584 
    585 /// \brief Matches the specialized template of a specialization declaration.
    586 ///
    587 /// Given
    588 /// \code
    589 ///   tempalate<typename T> class A {};
    590 ///   typedef A<int> B;
    591 /// \endcode
    592 /// classTemplateSpecializationDecl(hasSpecializedTemplate(classTemplateDecl()))
    593 ///   matches 'B' with classTemplateDecl() matching the class template
    594 ///   declaration of 'A'.
    595 AST_MATCHER_P(ClassTemplateSpecializationDecl, hasSpecializedTemplate,
    596               internal::Matcher<ClassTemplateDecl>, InnerMatcher) {
    597   const ClassTemplateDecl* Decl = Node.getSpecializedTemplate();
    598   return (Decl != nullptr &&
    599           InnerMatcher.matches(*Decl, Finder, Builder));
    600 }
    601 
    602 /// \brief Matches a declaration that has been implicitly added
    603 /// by the compiler (eg. implicit default/copy constructors).
    604 AST_MATCHER(Decl, isImplicit) {
    605   return Node.isImplicit();
    606 }
    607 
    608 /// \brief Matches classTemplateSpecializations, templateSpecializationType and
    609 /// functionDecl that have at least one TemplateArgument matching the given
    610 /// InnerMatcher.
    611 ///
    612 /// Given
    613 /// \code
    614 ///   template<typename T> class A {};
    615 ///   template<> class A<double> {};
    616 ///   A<int> a;
    617 ///
    618 ///   template<typename T> f() {};
    619 ///   void func() { f<int>(); };
    620 /// \endcode
    621 ///
    622 /// \endcode
    623 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
    624 ///     refersToType(asString("int"))))
    625 ///   matches the specialization \c A<int>
    626 ///
    627 /// functionDecl(hasAnyTemplateArgument(refersToType(asString("int"))))
    628 ///   matches the specialization \c f<int>
    629 AST_POLYMORPHIC_MATCHER_P(
    630     hasAnyTemplateArgument,
    631     AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
    632                                     TemplateSpecializationType,
    633                                     FunctionDecl),
    634     internal::Matcher<TemplateArgument>, InnerMatcher) {
    635   ArrayRef<TemplateArgument> List =
    636       internal::getTemplateSpecializationArgs(Node);
    637   return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
    638                              Builder);
    639 }
    640 
    641 /// \brief Matches expressions that match InnerMatcher after any implicit AST
    642 /// nodes are stripped off.
    643 ///
    644 /// Parentheses and explicit casts are not discarded.
    645 /// Given
    646 /// \code
    647 ///   class C {};
    648 ///   C a = C();
    649 ///   C b;
    650 ///   C c = b;
    651 /// \endcode
    652 /// The matchers
    653 /// \code
    654 ///    varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr())))
    655 /// \endcode
    656 /// would match the declarations for a, b, and c.
    657 /// While
    658 /// \code
    659 ///    varDecl(hasInitializer(cxxConstructExpr()))
    660 /// \endcode
    661 /// only match the declarations for b and c.
    662 AST_MATCHER_P(Expr, ignoringImplicit, ast_matchers::internal::Matcher<Expr>,
    663               InnerMatcher) {
    664   return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
    665 }
    666 
    667 /// \brief Matches expressions that match InnerMatcher after any implicit casts
    668 /// are stripped off.
    669 ///
    670 /// Parentheses and explicit casts are not discarded.
    671 /// Given
    672 /// \code
    673 ///   int arr[5];
    674 ///   int a = 0;
    675 ///   char b = 0;
    676 ///   const int c = a;
    677 ///   int *d = arr;
    678 ///   long e = (long) 0l;
    679 /// \endcode
    680 /// The matchers
    681 /// \code
    682 ///    varDecl(hasInitializer(ignoringImpCasts(integerLiteral())))
    683 ///    varDecl(hasInitializer(ignoringImpCasts(declRefExpr())))
    684 /// \endcode
    685 /// would match the declarations for a, b, c, and d, but not e.
    686 /// While
    687 /// \code
    688 ///    varDecl(hasInitializer(integerLiteral()))
    689 ///    varDecl(hasInitializer(declRefExpr()))
    690 /// \endcode
    691 /// only match the declarations for b, c, and d.
    692 AST_MATCHER_P(Expr, ignoringImpCasts,
    693               internal::Matcher<Expr>, InnerMatcher) {
    694   return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder);
    695 }
    696 
    697 /// \brief Matches expressions that match InnerMatcher after parentheses and
    698 /// casts are stripped off.
    699 ///
    700 /// Implicit and non-C Style casts are also discarded.
    701 /// Given
    702 /// \code
    703 ///   int a = 0;
    704 ///   char b = (0);
    705 ///   void* c = reinterpret_cast<char*>(0);
    706 ///   char d = char(0);
    707 /// \endcode
    708 /// The matcher
    709 ///    varDecl(hasInitializer(ignoringParenCasts(integerLiteral())))
    710 /// would match the declarations for a, b, c, and d.
    711 /// while
    712 ///    varDecl(hasInitializer(integerLiteral()))
    713 /// only match the declaration for a.
    714 AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) {
    715   return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder);
    716 }
    717 
    718 /// \brief Matches expressions that match InnerMatcher after implicit casts and
    719 /// parentheses are stripped off.
    720 ///
    721 /// Explicit casts are not discarded.
    722 /// Given
    723 /// \code
    724 ///   int arr[5];
    725 ///   int a = 0;
    726 ///   char b = (0);
    727 ///   const int c = a;
    728 ///   int *d = (arr);
    729 ///   long e = ((long) 0l);
    730 /// \endcode
    731 /// The matchers
    732 ///    varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral())))
    733 ///    varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr())))
    734 /// would match the declarations for a, b, c, and d, but not e.
    735 /// while
    736 ///    varDecl(hasInitializer(integerLiteral()))
    737 ///    varDecl(hasInitializer(declRefExpr()))
    738 /// would only match the declaration for a.
    739 AST_MATCHER_P(Expr, ignoringParenImpCasts,
    740               internal::Matcher<Expr>, InnerMatcher) {
    741   return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
    742 }
    743 
    744 /// \brief Matches types that match InnerMatcher after any parens are stripped.
    745 ///
    746 /// Given
    747 /// \code
    748 ///   void (*fp)(void);
    749 /// \endcode
    750 /// The matcher
    751 /// \code
    752 ///   varDecl(hasType(pointerType(pointee(ignoringParens(functionType())))))
    753 /// \endcode
    754 /// would match the declaration for fp.
    755 AST_MATCHER_P(QualType, ignoringParens,
    756               internal::Matcher<QualType>, InnerMatcher) {
    757   return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder);
    758 }
    759 
    760 /// \brief Matches classTemplateSpecializations, templateSpecializationType and
    761 /// functionDecl where the n'th TemplateArgument matches the given InnerMatcher.
    762 ///
    763 /// Given
    764 /// \code
    765 ///   template<typename T, typename U> class A {};
    766 ///   A<bool, int> b;
    767 ///   A<int, bool> c;
    768 ///
    769 ///   template<typename T> f() {};
    770 ///   void func() { f<int>(); };
    771 /// \endcode
    772 /// classTemplateSpecializationDecl(hasTemplateArgument(
    773 ///     1, refersToType(asString("int"))))
    774 ///   matches the specialization \c A<bool, int>
    775 ///
    776 /// functionDecl(hasTemplateArgument(0, refersToType(asString("int"))))
    777 ///   matches the specialization \c f<int>
    778 AST_POLYMORPHIC_MATCHER_P2(
    779     hasTemplateArgument,
    780     AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
    781                                     TemplateSpecializationType,
    782                                     FunctionDecl),
    783     unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
    784   ArrayRef<TemplateArgument> List =
    785       internal::getTemplateSpecializationArgs(Node);
    786   if (List.size() <= N)
    787     return false;
    788   return InnerMatcher.matches(List[N], Finder, Builder);
    789 }
    790 
    791 /// \brief Matches if the number of template arguments equals \p N.
    792 ///
    793 /// Given
    794 /// \code
    795 ///   template<typename T> struct C {};
    796 ///   C<int> c;
    797 /// \endcode
    798 /// classTemplateSpecializationDecl(templateArgumentCountIs(1))
    799 ///   matches C<int>.
    800 AST_POLYMORPHIC_MATCHER_P(
    801     templateArgumentCountIs,
    802     AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
    803                                     TemplateSpecializationType),
    804     unsigned, N) {
    805   return internal::getTemplateSpecializationArgs(Node).size() == N;
    806 }
    807 
    808 /// \brief Matches a TemplateArgument that refers to a certain type.
    809 ///
    810 /// Given
    811 /// \code
    812 ///   struct X {};
    813 ///   template<typename T> struct A {};
    814 ///   A<X> a;
    815 /// \endcode
    816 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
    817 ///     refersToType(class(hasName("X")))))
    818 ///   matches the specialization \c A<X>
    819 AST_MATCHER_P(TemplateArgument, refersToType,
    820               internal::Matcher<QualType>, InnerMatcher) {
    821   if (Node.getKind() != TemplateArgument::Type)
    822     return false;
    823   return InnerMatcher.matches(Node.getAsType(), Finder, Builder);
    824 }
    825 
    826 /// \brief Matches a TemplateArgument that refers to a certain template.
    827 ///
    828 /// Given
    829 /// \code
    830 ///   template<template <typename> class S> class X {};
    831 ///   template<typename T> class Y {};"
    832 ///   X<Y> xi;
    833 /// \endcode
    834 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
    835 ///     refersToTemplate(templateName())))
    836 ///   matches the specialization \c X<Y>
    837 AST_MATCHER_P(TemplateArgument, refersToTemplate,
    838               internal::Matcher<TemplateName>, InnerMatcher) {
    839   if (Node.getKind() != TemplateArgument::Template)
    840     return false;
    841   return InnerMatcher.matches(Node.getAsTemplate(), Finder, Builder);
    842 }
    843 
    844 /// \brief Matches a canonical TemplateArgument that refers to a certain
    845 /// declaration.
    846 ///
    847 /// Given
    848 /// \code
    849 ///   template<typename T> struct A {};
    850 ///   struct B { B* next; };
    851 ///   A<&B::next> a;
    852 /// \endcode
    853 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
    854 ///     refersToDeclaration(fieldDecl(hasName("next"))))
    855 ///   matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
    856 ///     \c B::next
    857 AST_MATCHER_P(TemplateArgument, refersToDeclaration,
    858               internal::Matcher<Decl>, InnerMatcher) {
    859   if (Node.getKind() == TemplateArgument::Declaration)
    860     return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder);
    861   return false;
    862 }
    863 
    864 /// \brief Matches a sugar TemplateArgument that refers to a certain expression.
    865 ///
    866 /// Given
    867 /// \code
    868 ///   template<typename T> struct A {};
    869 ///   struct B { B* next; };
    870 ///   A<&B::next> a;
    871 /// \endcode
    872 /// templateSpecializationType(hasAnyTemplateArgument(
    873 ///   isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))
    874 ///   matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
    875 ///     \c B::next
    876 AST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher<Expr>, InnerMatcher) {
    877   if (Node.getKind() == TemplateArgument::Expression)
    878     return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder);
    879   return false;
    880 }
    881 
    882 /// \brief Matches a TemplateArgument that is an integral value.
    883 ///
    884 /// Given
    885 /// \code
    886 ///   template<int T> struct A {};
    887 ///   C<42> c;
    888 /// \endcode
    889 /// classTemplateSpecializationDecl(
    890 ///   hasAnyTemplateArgument(isIntegral()))
    891 ///   matches the implicit instantiation of C in C<42>
    892 ///   with isIntegral() matching 42.
    893 AST_MATCHER(TemplateArgument, isIntegral) {
    894   return Node.getKind() == TemplateArgument::Integral;
    895 }
    896 
    897 /// \brief Matches a TemplateArgument that referes to an integral type.
    898 ///
    899 /// Given
    900 /// \code
    901 ///   template<int T> struct A {};
    902 ///   C<42> c;
    903 /// \endcode
    904 /// classTemplateSpecializationDecl(
    905 ///   hasAnyTemplateArgument(refersToIntegralType(asString("int"))))
    906 ///   matches the implicit instantiation of C in C<42>.
    907 AST_MATCHER_P(TemplateArgument, refersToIntegralType,
    908               internal::Matcher<QualType>, InnerMatcher) {
    909   if (Node.getKind() != TemplateArgument::Integral)
    910     return false;
    911   return InnerMatcher.matches(Node.getIntegralType(), Finder, Builder);
    912 }
    913 
    914 /// \brief Matches a TemplateArgument of integral type with a given value.
    915 ///
    916 /// Note that 'Value' is a string as the template argument's value is
    917 /// an arbitrary precision integer. 'Value' must be euqal to the canonical
    918 /// representation of that integral value in base 10.
    919 ///
    920 /// Given
    921 /// \code
    922 ///   template<int T> struct A {};
    923 ///   C<42> c;
    924 /// \endcode
    925 /// classTemplateSpecializationDecl(
    926 ///   hasAnyTemplateArgument(equalsIntegralValue("42")))
    927 ///   matches the implicit instantiation of C in C<42>.
    928 AST_MATCHER_P(TemplateArgument, equalsIntegralValue,
    929               std::string, Value) {
    930   if (Node.getKind() != TemplateArgument::Integral)
    931     return false;
    932   return Node.getAsIntegral().toString(10) == Value;
    933 }
    934 
    935 /// \brief Matches any value declaration.
    936 ///
    937 /// Example matches A, B, C and F
    938 /// \code
    939 ///   enum X { A, B, C };
    940 ///   void F();
    941 /// \endcode
    942 const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl;
    943 
    944 /// \brief Matches C++ constructor declarations.
    945 ///
    946 /// Example matches Foo::Foo() and Foo::Foo(int)
    947 /// \code
    948 ///   class Foo {
    949 ///    public:
    950 ///     Foo();
    951 ///     Foo(int);
    952 ///     int DoSomething();
    953 ///   };
    954 /// \endcode
    955 const internal::VariadicDynCastAllOfMatcher<
    956   Decl,
    957   CXXConstructorDecl> cxxConstructorDecl;
    958 
    959 /// \brief Matches explicit C++ destructor declarations.
    960 ///
    961 /// Example matches Foo::~Foo()
    962 /// \code
    963 ///   class Foo {
    964 ///    public:
    965 ///     virtual ~Foo();
    966 ///   };
    967 /// \endcode
    968 const internal::VariadicDynCastAllOfMatcher<
    969   Decl,
    970   CXXDestructorDecl> cxxDestructorDecl;
    971 
    972 /// \brief Matches enum declarations.
    973 ///
    974 /// Example matches X
    975 /// \code
    976 ///   enum X {
    977 ///     A, B, C
    978 ///   };
    979 /// \endcode
    980 const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl;
    981 
    982 /// \brief Matches enum constants.
    983 ///
    984 /// Example matches A, B, C
    985 /// \code
    986 ///   enum X {
    987 ///     A, B, C
    988 ///   };
    989 /// \endcode
    990 const internal::VariadicDynCastAllOfMatcher<
    991   Decl,
    992   EnumConstantDecl> enumConstantDecl;
    993 
    994 /// \brief Matches method declarations.
    995 ///
    996 /// Example matches y
    997 /// \code
    998 ///   class X { void y(); };
    999 /// \endcode
   1000 const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> cxxMethodDecl;
   1001 
   1002 /// \brief Matches conversion operator declarations.
   1003 ///
   1004 /// Example matches the operator.
   1005 /// \code
   1006 ///   class X { operator int() const; };
   1007 /// \endcode
   1008 const internal::VariadicDynCastAllOfMatcher<Decl, CXXConversionDecl>
   1009     cxxConversionDecl;
   1010 
   1011 /// \brief Matches variable declarations.
   1012 ///
   1013 /// Note: this does not match declarations of member variables, which are
   1014 /// "field" declarations in Clang parlance.
   1015 ///
   1016 /// Example matches a
   1017 /// \code
   1018 ///   int a;
   1019 /// \endcode
   1020 const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl;
   1021 
   1022 /// \brief Matches field declarations.
   1023 ///
   1024 /// Given
   1025 /// \code
   1026 ///   class X { int m; };
   1027 /// \endcode
   1028 /// fieldDecl()
   1029 ///   matches 'm'.
   1030 const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl;
   1031 
   1032 /// \brief Matches function declarations.
   1033 ///
   1034 /// Example matches f
   1035 /// \code
   1036 ///   void f();
   1037 /// \endcode
   1038 const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl;
   1039 
   1040 /// \brief Matches C++ function template declarations.
   1041 ///
   1042 /// Example matches f
   1043 /// \code
   1044 ///   template<class T> void f(T t) {}
   1045 /// \endcode
   1046 const internal::VariadicDynCastAllOfMatcher<
   1047   Decl,
   1048   FunctionTemplateDecl> functionTemplateDecl;
   1049 
   1050 /// \brief Matches friend declarations.
   1051 ///
   1052 /// Given
   1053 /// \code
   1054 ///   class X { friend void foo(); };
   1055 /// \endcode
   1056 /// friendDecl()
   1057 ///   matches 'friend void foo()'.
   1058 const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl;
   1059 
   1060 /// \brief Matches statements.
   1061 ///
   1062 /// Given
   1063 /// \code
   1064 ///   { ++a; }
   1065 /// \endcode
   1066 /// stmt()
   1067 ///   matches both the compound statement '{ ++a; }' and '++a'.
   1068 const internal::VariadicAllOfMatcher<Stmt> stmt;
   1069 
   1070 /// \brief Matches declaration statements.
   1071 ///
   1072 /// Given
   1073 /// \code
   1074 ///   int a;
   1075 /// \endcode
   1076 /// declStmt()
   1077 ///   matches 'int a'.
   1078 const internal::VariadicDynCastAllOfMatcher<
   1079   Stmt,
   1080   DeclStmt> declStmt;
   1081 
   1082 /// \brief Matches member expressions.
   1083 ///
   1084 /// Given
   1085 /// \code
   1086 ///   class Y {
   1087 ///     void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
   1088 ///     int a; static int b;
   1089 ///   };
   1090 /// \endcode
   1091 /// memberExpr()
   1092 ///   matches this->x, x, y.x, a, this->b
   1093 const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr;
   1094 
   1095 /// \brief Matches call expressions.
   1096 ///
   1097 /// Example matches x.y() and y()
   1098 /// \code
   1099 ///   X x;
   1100 ///   x.y();
   1101 ///   y();
   1102 /// \endcode
   1103 const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr;
   1104 
   1105 /// \brief Matches lambda expressions.
   1106 ///
   1107 /// Example matches [&](){return 5;}
   1108 /// \code
   1109 ///   [&](){return 5;}
   1110 /// \endcode
   1111 const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr;
   1112 
   1113 /// \brief Matches member call expressions.
   1114 ///
   1115 /// Example matches x.y()
   1116 /// \code
   1117 ///   X x;
   1118 ///   x.y();
   1119 /// \endcode
   1120 const internal::VariadicDynCastAllOfMatcher<
   1121   Stmt,
   1122   CXXMemberCallExpr> cxxMemberCallExpr;
   1123 
   1124 /// \brief Matches ObjectiveC Message invocation expressions.
   1125 ///
   1126 /// The innermost message send invokes the "alloc" class method on the
   1127 /// NSString class, while the outermost message send invokes the
   1128 /// "initWithString" instance method on the object returned from
   1129 /// NSString's "alloc". This matcher should match both message sends.
   1130 /// \code
   1131 ///   [[NSString alloc] initWithString:@"Hello"]
   1132 /// \endcode
   1133 const internal::VariadicDynCastAllOfMatcher<
   1134   Stmt,
   1135   ObjCMessageExpr> objcMessageExpr;
   1136 
   1137 /// \brief Matches Objective-C interface declarations.
   1138 ///
   1139 /// Example matches Foo
   1140 /// \code
   1141 ///   @interface Foo
   1142 ///   @end
   1143 /// \endcode
   1144 const internal::VariadicDynCastAllOfMatcher<
   1145   Decl,
   1146   ObjCInterfaceDecl> objcInterfaceDecl;
   1147 
   1148 /// \brief Matches Objective-C implementation declarations.
   1149 ///
   1150 /// Example matches Foo
   1151 /// \code
   1152 ///   @implementation Foo
   1153 ///   @end
   1154 /// \endcode
   1155 const internal::VariadicDynCastAllOfMatcher<
   1156   Decl,
   1157   ObjCImplementationDecl> objcImplementationDecl;
   1158 
   1159 /// \brief Matches Objective-C protocol declarations.
   1160 ///
   1161 /// Example matches FooDelegate
   1162 /// \code
   1163 ///   @protocol FooDelegate
   1164 ///   @end
   1165 /// \endcode
   1166 const internal::VariadicDynCastAllOfMatcher<
   1167   Decl,
   1168   ObjCProtocolDecl> objcProtocolDecl;
   1169 
   1170 /// \brief Matches Objective-C category declarations.
   1171 ///
   1172 /// Example matches Foo (Additions)
   1173 /// \code
   1174 ///   @interface Foo (Additions)
   1175 ///   @end
   1176 /// \endcode
   1177 const internal::VariadicDynCastAllOfMatcher<
   1178   Decl,
   1179   ObjCCategoryDecl> objcCategoryDecl;
   1180 
   1181 /// \brief Matches Objective-C method declarations.
   1182 ///
   1183 /// Example matches both declaration and definition of -[Foo method]
   1184 /// \code
   1185 ///   @interface Foo
   1186 ///   - (void)method;
   1187 ///   @end
   1188 ///
   1189 ///   @implementation Foo
   1190 ///   - (void)method {}
   1191 ///   @end
   1192 /// \endcode
   1193 const internal::VariadicDynCastAllOfMatcher<
   1194   Decl,
   1195   ObjCMethodDecl> objcMethodDecl;
   1196 
   1197 /// \brief Matches Objective-C instance variable declarations.
   1198 ///
   1199 /// Example matches _enabled
   1200 /// \code
   1201 ///   @implementation Foo {
   1202 ///     BOOL _enabled;
   1203 ///   }
   1204 ///   @end
   1205 /// \endcode
   1206 const internal::VariadicDynCastAllOfMatcher<
   1207   Decl,
   1208   ObjCIvarDecl> objcIvarDecl;
   1209 
   1210 /// \brief Matches Objective-C property declarations.
   1211 ///
   1212 /// Example matches enabled
   1213 /// \code
   1214 ///   @interface Foo
   1215 ///   @property BOOL enabled;
   1216 ///   @end
   1217 /// \endcode
   1218 const internal::VariadicDynCastAllOfMatcher<
   1219   Decl,
   1220   ObjCPropertyDecl> objcPropertyDecl;
   1221 
   1222 /// \brief Matches expressions that introduce cleanups to be run at the end
   1223 /// of the sub-expression's evaluation.
   1224 ///
   1225 /// Example matches std::string()
   1226 /// \code
   1227 ///   const std::string str = std::string();
   1228 /// \endcode
   1229 const internal::VariadicDynCastAllOfMatcher<
   1230   Stmt,
   1231   ExprWithCleanups> exprWithCleanups;
   1232 
   1233 /// \brief Matches init list expressions.
   1234 ///
   1235 /// Given
   1236 /// \code
   1237 ///   int a[] = { 1, 2 };
   1238 ///   struct B { int x, y; };
   1239 ///   B b = { 5, 6 };
   1240 /// \endcode
   1241 /// initListExpr()
   1242 ///   matches "{ 1, 2 }" and "{ 5, 6 }"
   1243 const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr;
   1244 
   1245 /// \brief Matches the syntactic form of init list expressions
   1246 /// (if expression have it).
   1247 AST_MATCHER_P(InitListExpr, hasSyntacticForm,
   1248               internal::Matcher<Expr>, InnerMatcher) {
   1249   const Expr *SyntForm = Node.getSyntacticForm();
   1250   return (SyntForm != nullptr &&
   1251           InnerMatcher.matches(*SyntForm, Finder, Builder));
   1252 }
   1253 
   1254 /// \brief Matches C++ initializer list expressions.
   1255 ///
   1256 /// Given
   1257 /// \code
   1258 ///   std::vector<int> a({ 1, 2, 3 });
   1259 ///   std::vector<int> b = { 4, 5 };
   1260 ///   int c[] = { 6, 7 };
   1261 ///   std::pair<int, int> d = { 8, 9 };
   1262 /// \endcode
   1263 /// cxxStdInitializerListExpr()
   1264 ///   matches "{ 1, 2, 3 }" and "{ 4, 5 }"
   1265 const internal::VariadicDynCastAllOfMatcher<Stmt,
   1266   CXXStdInitializerListExpr> cxxStdInitializerListExpr;
   1267 
   1268 /// \brief Matches implicit initializers of init list expressions.
   1269 ///
   1270 /// Given
   1271 /// \code
   1272 ///   point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
   1273 /// \endcode
   1274 /// implicitValueInitExpr()
   1275 ///   matches "[0].y" (implicitly)
   1276 const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitValueInitExpr>
   1277 implicitValueInitExpr;
   1278 
   1279 /// \brief Matches paren list expressions.
   1280 /// ParenListExprs don't have a predefined type and are used for late parsing.
   1281 /// In the final AST, they can be met in template declarations.
   1282 ///
   1283 /// Given
   1284 /// \code
   1285 ///   template<typename T> class X {
   1286 ///     void f() {
   1287 ///       X x(*this);
   1288 ///       int a = 0, b = 1; int i = (a, b);
   1289 ///     }
   1290 ///   };
   1291 /// \endcode
   1292 /// parenListExpr() matches "*this" but NOT matches (a, b) because (a, b)
   1293 /// has a predefined type and is a ParenExpr, not a ParenListExpr.
   1294 const internal::VariadicDynCastAllOfMatcher<Stmt, ParenListExpr> parenListExpr;
   1295 
   1296 /// \brief Matches substitutions of non-type template parameters.
   1297 ///
   1298 /// Given
   1299 /// \code
   1300 ///   template <int N>
   1301 ///   struct A { static const int n = N; };
   1302 ///   struct B : public A<42> {};
   1303 /// \endcode
   1304 /// substNonTypeTemplateParmExpr()
   1305 ///   matches "N" in the right-hand side of "static const int n = N;"
   1306 const internal::VariadicDynCastAllOfMatcher<
   1307   Stmt,
   1308   SubstNonTypeTemplateParmExpr> substNonTypeTemplateParmExpr;
   1309 
   1310 /// \brief Matches using declarations.
   1311 ///
   1312 /// Given
   1313 /// \code
   1314 ///   namespace X { int x; }
   1315 ///   using X::x;
   1316 /// \endcode
   1317 /// usingDecl()
   1318 ///   matches \code using X::x \endcode
   1319 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl;
   1320 
   1321 /// \brief Matches using namespace declarations.
   1322 ///
   1323 /// Given
   1324 /// \code
   1325 ///   namespace X { int x; }
   1326 ///   using namespace X;
   1327 /// \endcode
   1328 /// usingDirectiveDecl()
   1329 ///   matches \code using namespace X \endcode
   1330 const internal::VariadicDynCastAllOfMatcher<
   1331   Decl,
   1332   UsingDirectiveDecl> usingDirectiveDecl;
   1333 
   1334 /// \brief Matches reference to a name that can be looked up during parsing
   1335 /// but could not be resolved to a specific declaration.
   1336 ///
   1337 /// Given
   1338 /// \code
   1339 ///   template<typename T>
   1340 ///   T foo() { T a; return a; }
   1341 ///   template<typename T>
   1342 ///   void bar() {
   1343 ///     foo<T>();
   1344 ///   }
   1345 /// \endcode
   1346 /// unresolvedLookupExpr()
   1347 ///   matches \code foo<T>() \endcode
   1348 const internal::VariadicDynCastAllOfMatcher<
   1349    Stmt,
   1350    UnresolvedLookupExpr> unresolvedLookupExpr;
   1351 
   1352 /// \brief Matches unresolved using value declarations.
   1353 ///
   1354 /// Given
   1355 /// \code
   1356 ///   template<typename X>
   1357 ///   class C : private X {
   1358 ///     using X::x;
   1359 ///   };
   1360 /// \endcode
   1361 /// unresolvedUsingValueDecl()
   1362 ///   matches \code using X::x \endcode
   1363 const internal::VariadicDynCastAllOfMatcher<
   1364   Decl,
   1365   UnresolvedUsingValueDecl> unresolvedUsingValueDecl;
   1366 
   1367 /// \brief Matches unresolved using value declarations that involve the
   1368 /// typename.
   1369 ///
   1370 /// Given
   1371 /// \code
   1372 ///   template <typename T>
   1373 ///   struct Base { typedef T Foo; };
   1374 ///
   1375 ///   template<typename T>
   1376 ///   struct S : private Base<T> {
   1377 ///     using typename Base<T>::Foo;
   1378 ///   };
   1379 /// \endcode
   1380 /// unresolvedUsingTypenameDecl()
   1381 ///   matches \code using Base<T>::Foo \endcode
   1382 const internal::VariadicDynCastAllOfMatcher<
   1383   Decl,
   1384   UnresolvedUsingTypenameDecl> unresolvedUsingTypenameDecl;
   1385 
   1386 /// \brief Matches parentheses used in expressions.
   1387 ///
   1388 /// Example matches (foo() + 1)
   1389 /// \code
   1390 ///   int foo() { return 1; }
   1391 ///   int a = (foo() + 1);
   1392 /// \endcode
   1393 const internal::VariadicDynCastAllOfMatcher<
   1394   Stmt,
   1395   ParenExpr> parenExpr;
   1396 
   1397 /// \brief Matches constructor call expressions (including implicit ones).
   1398 ///
   1399 /// Example matches string(ptr, n) and ptr within arguments of f
   1400 ///     (matcher = cxxConstructExpr())
   1401 /// \code
   1402 ///   void f(const string &a, const string &b);
   1403 ///   char *ptr;
   1404 ///   int n;
   1405 ///   f(string(ptr, n), ptr);
   1406 /// \endcode
   1407 const internal::VariadicDynCastAllOfMatcher<
   1408   Stmt,
   1409   CXXConstructExpr> cxxConstructExpr;
   1410 
   1411 /// \brief Matches unresolved constructor call expressions.
   1412 ///
   1413 /// Example matches T(t) in return statement of f
   1414 ///     (matcher = cxxUnresolvedConstructExpr())
   1415 /// \code
   1416 ///   template <typename T>
   1417 ///   void f(const T& t) { return T(t); }
   1418 /// \endcode
   1419 const internal::VariadicDynCastAllOfMatcher<
   1420   Stmt,
   1421   CXXUnresolvedConstructExpr> cxxUnresolvedConstructExpr;
   1422 
   1423 /// \brief Matches implicit and explicit this expressions.
   1424 ///
   1425 /// Example matches the implicit this expression in "return i".
   1426 ///     (matcher = cxxThisExpr())
   1427 /// \code
   1428 /// struct foo {
   1429 ///   int i;
   1430 ///   int f() { return i; }
   1431 /// };
   1432 /// \endcode
   1433 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> cxxThisExpr;
   1434 
   1435 /// \brief Matches nodes where temporaries are created.
   1436 ///
   1437 /// Example matches FunctionTakesString(GetStringByValue())
   1438 ///     (matcher = cxxBindTemporaryExpr())
   1439 /// \code
   1440 ///   FunctionTakesString(GetStringByValue());
   1441 ///   FunctionTakesStringByPointer(GetStringPointer());
   1442 /// \endcode
   1443 const internal::VariadicDynCastAllOfMatcher<
   1444   Stmt,
   1445   CXXBindTemporaryExpr> cxxBindTemporaryExpr;
   1446 
   1447 /// \brief Matches nodes where temporaries are materialized.
   1448 ///
   1449 /// Example: Given
   1450 /// \code
   1451 ///   struct T {void func();};
   1452 ///   T f();
   1453 ///   void g(T);
   1454 /// \endcode
   1455 /// materializeTemporaryExpr() matches 'f()' in these statements
   1456 /// \code
   1457 ///   T u(f());
   1458 ///   g(f());
   1459 /// \endcode
   1460 /// but does not match
   1461 /// \code
   1462 ///   f();
   1463 ///   f().func();
   1464 /// \endcode
   1465 const internal::VariadicDynCastAllOfMatcher<
   1466   Stmt,
   1467   MaterializeTemporaryExpr> materializeTemporaryExpr;
   1468 
   1469 /// \brief Matches new expressions.
   1470 ///
   1471 /// Given
   1472 /// \code
   1473 ///   new X;
   1474 /// \endcode
   1475 /// cxxNewExpr()
   1476 ///   matches 'new X'.
   1477 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> cxxNewExpr;
   1478 
   1479 /// \brief Matches delete expressions.
   1480 ///
   1481 /// Given
   1482 /// \code
   1483 ///   delete X;
   1484 /// \endcode
   1485 /// cxxDeleteExpr()
   1486 ///   matches 'delete X'.
   1487 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> cxxDeleteExpr;
   1488 
   1489 /// \brief Matches array subscript expressions.
   1490 ///
   1491 /// Given
   1492 /// \code
   1493 ///   int i = a[1];
   1494 /// \endcode
   1495 /// arraySubscriptExpr()
   1496 ///   matches "a[1]"
   1497 const internal::VariadicDynCastAllOfMatcher<
   1498   Stmt,
   1499   ArraySubscriptExpr> arraySubscriptExpr;
   1500 
   1501 /// \brief Matches the value of a default argument at the call site.
   1502 ///
   1503 /// Example matches the CXXDefaultArgExpr placeholder inserted for the
   1504 ///     default value of the second parameter in the call expression f(42)
   1505 ///     (matcher = cxxDefaultArgExpr())
   1506 /// \code
   1507 ///   void f(int x, int y = 0);
   1508 ///   f(42);
   1509 /// \endcode
   1510 const internal::VariadicDynCastAllOfMatcher<
   1511   Stmt,
   1512   CXXDefaultArgExpr> cxxDefaultArgExpr;
   1513 
   1514 /// \brief Matches overloaded operator calls.
   1515 ///
   1516 /// Note that if an operator isn't overloaded, it won't match. Instead, use
   1517 /// binaryOperator matcher.
   1518 /// Currently it does not match operators such as new delete.
   1519 /// FIXME: figure out why these do not match?
   1520 ///
   1521 /// Example matches both operator<<((o << b), c) and operator<<(o, b)
   1522 ///     (matcher = cxxOperatorCallExpr())
   1523 /// \code
   1524 ///   ostream &operator<< (ostream &out, int i) { };
   1525 ///   ostream &o; int b = 1, c = 1;
   1526 ///   o << b << c;
   1527 /// \endcode
   1528 const internal::VariadicDynCastAllOfMatcher<
   1529   Stmt,
   1530   CXXOperatorCallExpr> cxxOperatorCallExpr;
   1531 
   1532 /// \brief Matches expressions.
   1533 ///
   1534 /// Example matches x()
   1535 /// \code
   1536 ///   void f() { x(); }
   1537 /// \endcode
   1538 const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
   1539 
   1540 /// \brief Matches expressions that refer to declarations.
   1541 ///
   1542 /// Example matches x in if (x)
   1543 /// \code
   1544 ///   bool x;
   1545 ///   if (x) {}
   1546 /// \endcode
   1547 const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr;
   1548 
   1549 /// \brief Matches if statements.
   1550 ///
   1551 /// Example matches 'if (x) {}'
   1552 /// \code
   1553 ///   if (x) {}
   1554 /// \endcode
   1555 const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
   1556 
   1557 /// \brief Matches for statements.
   1558 ///
   1559 /// Example matches 'for (;;) {}'
   1560 /// \code
   1561 ///   for (;;) {}
   1562 ///   int i[] =  {1, 2, 3}; for (auto a : i);
   1563 /// \endcode
   1564 const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
   1565 
   1566 /// \brief Matches the increment statement of a for loop.
   1567 ///
   1568 /// Example:
   1569 ///     forStmt(hasIncrement(unaryOperator(hasOperatorName("++"))))
   1570 /// matches '++x' in
   1571 /// \code
   1572 ///     for (x; x < N; ++x) { }
   1573 /// \endcode
   1574 AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>,
   1575               InnerMatcher) {
   1576   const Stmt *const Increment = Node.getInc();
   1577   return (Increment != nullptr &&
   1578           InnerMatcher.matches(*Increment, Finder, Builder));
   1579 }
   1580 
   1581 /// \brief Matches the initialization statement of a for loop.
   1582 ///
   1583 /// Example:
   1584 ///     forStmt(hasLoopInit(declStmt()))
   1585 /// matches 'int x = 0' in
   1586 /// \code
   1587 ///     for (int x = 0; x < N; ++x) { }
   1588 /// \endcode
   1589 AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
   1590               InnerMatcher) {
   1591   const Stmt *const Init = Node.getInit();
   1592   return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
   1593 }
   1594 
   1595 /// \brief Matches range-based for statements.
   1596 ///
   1597 /// cxxForRangeStmt() matches 'for (auto a : i)'
   1598 /// \code
   1599 ///   int i[] =  {1, 2, 3}; for (auto a : i);
   1600 ///   for(int j = 0; j < 5; ++j);
   1601 /// \endcode
   1602 const internal::VariadicDynCastAllOfMatcher<
   1603   Stmt,
   1604   CXXForRangeStmt> cxxForRangeStmt;
   1605 
   1606 /// \brief Matches the initialization statement of a for loop.
   1607 ///
   1608 /// Example:
   1609 ///     forStmt(hasLoopVariable(anything()))
   1610 /// matches 'int x' in
   1611 /// \code
   1612 ///     for (int x : a) { }
   1613 /// \endcode
   1614 AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
   1615               InnerMatcher) {
   1616   const VarDecl *const Var = Node.getLoopVariable();
   1617   return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder));
   1618 }
   1619 
   1620 /// \brief Matches the range initialization statement of a for loop.
   1621 ///
   1622 /// Example:
   1623 ///     forStmt(hasRangeInit(anything()))
   1624 /// matches 'a' in
   1625 /// \code
   1626 ///     for (int x : a) { }
   1627 /// \endcode
   1628 AST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>,
   1629               InnerMatcher) {
   1630   const Expr *const Init = Node.getRangeInit();
   1631   return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
   1632 }
   1633 
   1634 /// \brief Matches while statements.
   1635 ///
   1636 /// Given
   1637 /// \code
   1638 ///   while (true) {}
   1639 /// \endcode
   1640 /// whileStmt()
   1641 ///   matches 'while (true) {}'.
   1642 const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt;
   1643 
   1644 /// \brief Matches do statements.
   1645 ///
   1646 /// Given
   1647 /// \code
   1648 ///   do {} while (true);
   1649 /// \endcode
   1650 /// doStmt()
   1651 ///   matches 'do {} while(true)'
   1652 const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt;
   1653 
   1654 /// \brief Matches break statements.
   1655 ///
   1656 /// Given
   1657 /// \code
   1658 ///   while (true) { break; }
   1659 /// \endcode
   1660 /// breakStmt()
   1661 ///   matches 'break'
   1662 const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt;
   1663 
   1664 /// \brief Matches continue statements.
   1665 ///
   1666 /// Given
   1667 /// \code
   1668 ///   while (true) { continue; }
   1669 /// \endcode
   1670 /// continueStmt()
   1671 ///   matches 'continue'
   1672 const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> continueStmt;
   1673 
   1674 /// \brief Matches return statements.
   1675 ///
   1676 /// Given
   1677 /// \code
   1678 ///   return 1;
   1679 /// \endcode
   1680 /// returnStmt()
   1681 ///   matches 'return 1'
   1682 const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt;
   1683 
   1684 /// \brief Matches goto statements.
   1685 ///
   1686 /// Given
   1687 /// \code
   1688 ///   goto FOO;
   1689 ///   FOO: bar();
   1690 /// \endcode
   1691 /// gotoStmt()
   1692 ///   matches 'goto FOO'
   1693 const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt;
   1694 
   1695 /// \brief Matches label statements.
   1696 ///
   1697 /// Given
   1698 /// \code
   1699 ///   goto FOO;
   1700 ///   FOO: bar();
   1701 /// \endcode
   1702 /// labelStmt()
   1703 ///   matches 'FOO:'
   1704 const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt;
   1705 
   1706 /// \brief Matches address of label statements (GNU extension).
   1707 ///
   1708 /// Given
   1709 /// \code
   1710 ///   FOO: bar();
   1711 ///   void *ptr = &&FOO;
   1712 ///   goto *bar;
   1713 /// \endcode
   1714 /// addrLabelExpr()
   1715 ///   matches '&&FOO'
   1716 const internal::VariadicDynCastAllOfMatcher<Stmt, AddrLabelExpr> addrLabelExpr;
   1717 
   1718 /// \brief Matches switch statements.
   1719 ///
   1720 /// Given
   1721 /// \code
   1722 ///   switch(a) { case 42: break; default: break; }
   1723 /// \endcode
   1724 /// switchStmt()
   1725 ///   matches 'switch(a)'.
   1726 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt;
   1727 
   1728 /// \brief Matches case and default statements inside switch statements.
   1729 ///
   1730 /// Given
   1731 /// \code
   1732 ///   switch(a) { case 42: break; default: break; }
   1733 /// \endcode
   1734 /// switchCase()
   1735 ///   matches 'case 42: break;' and 'default: break;'.
   1736 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase;
   1737 
   1738 /// \brief Matches case statements inside switch statements.
   1739 ///
   1740 /// Given
   1741 /// \code
   1742 ///   switch(a) { case 42: break; default: break; }
   1743 /// \endcode
   1744 /// caseStmt()
   1745 ///   matches 'case 42: break;'.
   1746 const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt;
   1747 
   1748 /// \brief Matches default statements inside switch statements.
   1749 ///
   1750 /// Given
   1751 /// \code
   1752 ///   switch(a) { case 42: break; default: break; }
   1753 /// \endcode
   1754 /// defaultStmt()
   1755 ///   matches 'default: break;'.
   1756 const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt;
   1757 
   1758 /// \brief Matches compound statements.
   1759 ///
   1760 /// Example matches '{}' and '{{}}'in 'for (;;) {{}}'
   1761 /// \code
   1762 ///   for (;;) {{}}
   1763 /// \endcode
   1764 const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt;
   1765 
   1766 /// \brief Matches catch statements.
   1767 ///
   1768 /// \code
   1769 ///   try {} catch(int i) {}
   1770 /// \endcode
   1771 /// cxxCatchStmt()
   1772 ///   matches 'catch(int i)'
   1773 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> cxxCatchStmt;
   1774 
   1775 /// \brief Matches try statements.
   1776 ///
   1777 /// \code
   1778 ///   try {} catch(int i) {}
   1779 /// \endcode
   1780 /// cxxTryStmt()
   1781 ///   matches 'try {}'
   1782 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
   1783 
   1784 /// \brief Matches throw expressions.
   1785 ///
   1786 /// \code
   1787 ///   try { throw 5; } catch(int i) {}
   1788 /// \endcode
   1789 /// cxxThrowExpr()
   1790 ///   matches 'throw 5'
   1791 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> cxxThrowExpr;
   1792 
   1793 /// \brief Matches null statements.
   1794 ///
   1795 /// \code
   1796 ///   foo();;
   1797 /// \endcode
   1798 /// nullStmt()
   1799 ///   matches the second ';'
   1800 const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
   1801 
   1802 /// \brief Matches asm statements.
   1803 ///
   1804 /// \code
   1805 ///  int i = 100;
   1806 ///   __asm("mov al, 2");
   1807 /// \endcode
   1808 /// asmStmt()
   1809 ///   matches '__asm("mov al, 2")'
   1810 const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
   1811 
   1812 /// \brief Matches bool literals.
   1813 ///
   1814 /// Example matches true
   1815 /// \code
   1816 ///   true
   1817 /// \endcode
   1818 const internal::VariadicDynCastAllOfMatcher<
   1819   Stmt,
   1820   CXXBoolLiteralExpr> cxxBoolLiteral;
   1821 
   1822 /// \brief Matches string literals (also matches wide string literals).
   1823 ///
   1824 /// Example matches "abcd", L"abcd"
   1825 /// \code
   1826 ///   char *s = "abcd";
   1827 ///   wchar_t *ws = L"abcd";
   1828 /// \endcode
   1829 const internal::VariadicDynCastAllOfMatcher<
   1830   Stmt,
   1831   StringLiteral> stringLiteral;
   1832 
   1833 /// \brief Matches character literals (also matches wchar_t).
   1834 ///
   1835 /// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral),
   1836 /// though.
   1837 ///
   1838 /// Example matches 'a', L'a'
   1839 /// \code
   1840 ///   char ch = 'a';
   1841 ///   wchar_t chw = L'a';
   1842 /// \endcode
   1843 const internal::VariadicDynCastAllOfMatcher<
   1844   Stmt,
   1845   CharacterLiteral> characterLiteral;
   1846 
   1847 /// \brief Matches integer literals of all sizes / encodings, e.g.
   1848 /// 1, 1L, 0x1 and 1U.
   1849 ///
   1850 /// Does not match character-encoded integers such as L'a'.
   1851 const internal::VariadicDynCastAllOfMatcher<
   1852   Stmt,
   1853   IntegerLiteral> integerLiteral;
   1854 
   1855 /// \brief Matches float literals of all sizes / encodings, e.g.
   1856 /// 1.0, 1.0f, 1.0L and 1e10.
   1857 ///
   1858 /// Does not match implicit conversions such as
   1859 /// \code
   1860 ///   float a = 10;
   1861 /// \endcode
   1862 const internal::VariadicDynCastAllOfMatcher<
   1863   Stmt,
   1864   FloatingLiteral> floatLiteral;
   1865 
   1866 /// \brief Matches user defined literal operator call.
   1867 ///
   1868 /// Example match: "foo"_suffix
   1869 const internal::VariadicDynCastAllOfMatcher<
   1870   Stmt,
   1871   UserDefinedLiteral> userDefinedLiteral;
   1872 
   1873 /// \brief Matches compound (i.e. non-scalar) literals
   1874 ///
   1875 /// Example match: {1}, (1, 2)
   1876 /// \code
   1877 ///   int array[4] = {1};
   1878 ///   vector int myvec = (vector int)(1, 2);
   1879 /// \endcode
   1880 const internal::VariadicDynCastAllOfMatcher<
   1881   Stmt,
   1882   CompoundLiteralExpr> compoundLiteralExpr;
   1883 
   1884 /// \brief Matches nullptr literal.
   1885 const internal::VariadicDynCastAllOfMatcher<
   1886   Stmt,
   1887   CXXNullPtrLiteralExpr> cxxNullPtrLiteralExpr;
   1888 
   1889 /// \brief Matches GNU __null expression.
   1890 const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr> gnuNullExpr;
   1891 
   1892 /// \brief Matches atomic builtins.
   1893 /// Example matches __atomic_load_n(ptr, 1)
   1894 /// \code
   1895 ///   void foo() { int *ptr; __atomic_load_n(ptr, 1); }
   1896 /// \endcode
   1897 const internal::VariadicDynCastAllOfMatcher<Stmt, AtomicExpr> atomicExpr;
   1898 
   1899 /// \brief Matches statement expression (GNU extension).
   1900 ///
   1901 /// Example match: ({ int X = 4; X; })
   1902 /// \code
   1903 ///   int C = ({ int X = 4; X; });
   1904 /// \endcode
   1905 const internal::VariadicDynCastAllOfMatcher<Stmt, StmtExpr> stmtExpr;
   1906 
   1907 /// \brief Matches binary operator expressions.
   1908 ///
   1909 /// Example matches a || b
   1910 /// \code
   1911 ///   !(a || b)
   1912 /// \endcode
   1913 const internal::VariadicDynCastAllOfMatcher<
   1914   Stmt,
   1915   BinaryOperator> binaryOperator;
   1916 
   1917 /// \brief Matches unary operator expressions.
   1918 ///
   1919 /// Example matches !a
   1920 /// \code
   1921 ///   !a || b
   1922 /// \endcode
   1923 const internal::VariadicDynCastAllOfMatcher<
   1924   Stmt,
   1925   UnaryOperator> unaryOperator;
   1926 
   1927 /// \brief Matches conditional operator expressions.
   1928 ///
   1929 /// Example matches a ? b : c
   1930 /// \code
   1931 ///   (a ? b : c) + 42
   1932 /// \endcode
   1933 const internal::VariadicDynCastAllOfMatcher<
   1934   Stmt,
   1935   ConditionalOperator> conditionalOperator;
   1936 
   1937 /// \brief Matches binary conditional operator expressions (GNU extension).
   1938 ///
   1939 /// Example matches a ?: b
   1940 /// \code
   1941 ///   (a ?: b) + 42;
   1942 /// \endcode
   1943 const internal::VariadicDynCastAllOfMatcher<
   1944   Stmt,
   1945   BinaryConditionalOperator> binaryConditionalOperator;
   1946 
   1947 /// \brief Matches opaque value expressions. They are used as helpers
   1948 /// to reference another expressions and can be met
   1949 /// in BinaryConditionalOperators, for example.
   1950 ///
   1951 /// Example matches 'a'
   1952 /// \code
   1953 ///   (a ?: c) + 42;
   1954 /// \endcode
   1955 const internal::VariadicDynCastAllOfMatcher<
   1956   Stmt,
   1957   OpaqueValueExpr> opaqueValueExpr;
   1958 
   1959 /// \brief Matches a C++ static_assert declaration.
   1960 ///
   1961 /// Example:
   1962 ///   staticAssertExpr()
   1963 /// matches
   1964 ///   static_assert(sizeof(S) == sizeof(int))
   1965 /// in
   1966 /// \code
   1967 ///   struct S {
   1968 ///     int x;
   1969 ///   };
   1970 ///   static_assert(sizeof(S) == sizeof(int));
   1971 /// \endcode
   1972 const internal::VariadicDynCastAllOfMatcher<
   1973   Decl,
   1974   StaticAssertDecl> staticAssertDecl;
   1975 
   1976 /// \brief Matches a reinterpret_cast expression.
   1977 ///
   1978 /// Either the source expression or the destination type can be matched
   1979 /// using has(), but hasDestinationType() is more specific and can be
   1980 /// more readable.
   1981 ///
   1982 /// Example matches reinterpret_cast<char*>(&p) in
   1983 /// \code
   1984 ///   void* p = reinterpret_cast<char*>(&p);
   1985 /// \endcode
   1986 const internal::VariadicDynCastAllOfMatcher<
   1987   Stmt,
   1988   CXXReinterpretCastExpr> cxxReinterpretCastExpr;
   1989 
   1990 /// \brief Matches a C++ static_cast expression.
   1991 ///
   1992 /// \see hasDestinationType
   1993 /// \see reinterpretCast
   1994 ///
   1995 /// Example:
   1996 ///   cxxStaticCastExpr()
   1997 /// matches
   1998 ///   static_cast<long>(8)
   1999 /// in
   2000 /// \code
   2001 ///   long eight(static_cast<long>(8));
   2002 /// \endcode
   2003 const internal::VariadicDynCastAllOfMatcher<
   2004   Stmt,
   2005   CXXStaticCastExpr> cxxStaticCastExpr;
   2006 
   2007 /// \brief Matches a dynamic_cast expression.
   2008 ///
   2009 /// Example:
   2010 ///   cxxDynamicCastExpr()
   2011 /// matches
   2012 ///   dynamic_cast<D*>(&b);
   2013 /// in
   2014 /// \code
   2015 ///   struct B { virtual ~B() {} }; struct D : B {};
   2016 ///   B b;
   2017 ///   D* p = dynamic_cast<D*>(&b);
   2018 /// \endcode
   2019 const internal::VariadicDynCastAllOfMatcher<
   2020   Stmt,
   2021   CXXDynamicCastExpr> cxxDynamicCastExpr;
   2022 
   2023 /// \brief Matches a const_cast expression.
   2024 ///
   2025 /// Example: Matches const_cast<int*>(&r) in
   2026 /// \code
   2027 ///   int n = 42;
   2028 ///   const int &r(n);
   2029 ///   int* p = const_cast<int*>(&r);
   2030 /// \endcode
   2031 const internal::VariadicDynCastAllOfMatcher<
   2032   Stmt,
   2033   CXXConstCastExpr> cxxConstCastExpr;
   2034 
   2035 /// \brief Matches a C-style cast expression.
   2036 ///
   2037 /// Example: Matches (int) 2.2f in
   2038 /// \code
   2039 ///   int i = (int) 2.2f;
   2040 /// \endcode
   2041 const internal::VariadicDynCastAllOfMatcher<
   2042   Stmt,
   2043   CStyleCastExpr> cStyleCastExpr;
   2044 
   2045 /// \brief Matches explicit cast expressions.
   2046 ///
   2047 /// Matches any cast expression written in user code, whether it be a
   2048 /// C-style cast, a functional-style cast, or a keyword cast.
   2049 ///
   2050 /// Does not match implicit conversions.
   2051 ///
   2052 /// Note: the name "explicitCast" is chosen to match Clang's terminology, as
   2053 /// Clang uses the term "cast" to apply to implicit conversions as well as to
   2054 /// actual cast expressions.
   2055 ///
   2056 /// \see hasDestinationType.
   2057 ///
   2058 /// Example: matches all five of the casts in
   2059 /// \code
   2060 ///   int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42)))))
   2061 /// \endcode
   2062 /// but does not match the implicit conversion in
   2063 /// \code
   2064 ///   long ell = 42;
   2065 /// \endcode
   2066 const internal::VariadicDynCastAllOfMatcher<
   2067   Stmt,
   2068   ExplicitCastExpr> explicitCastExpr;
   2069 
   2070 /// \brief Matches the implicit cast nodes of Clang's AST.
   2071 ///
   2072 /// This matches many different places, including function call return value
   2073 /// eliding, as well as any type conversions.
   2074 const internal::VariadicDynCastAllOfMatcher<
   2075   Stmt,
   2076   ImplicitCastExpr> implicitCastExpr;
   2077 
   2078 /// \brief Matches any cast nodes of Clang's AST.
   2079 ///
   2080 /// Example: castExpr() matches each of the following:
   2081 /// \code
   2082 ///   (int) 3;
   2083 ///   const_cast<Expr *>(SubExpr);
   2084 ///   char c = 0;
   2085 /// \endcode
   2086 /// but does not match
   2087 /// \code
   2088 ///   int i = (0);
   2089 ///   int k = 0;
   2090 /// \endcode
   2091 const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr;
   2092 
   2093 /// \brief Matches functional cast expressions
   2094 ///
   2095 /// Example: Matches Foo(bar);
   2096 /// \code
   2097 ///   Foo f = bar;
   2098 ///   Foo g = (Foo) bar;
   2099 ///   Foo h = Foo(bar);
   2100 /// \endcode
   2101 const internal::VariadicDynCastAllOfMatcher<
   2102   Stmt,
   2103   CXXFunctionalCastExpr> cxxFunctionalCastExpr;
   2104 
   2105 /// \brief Matches functional cast expressions having N != 1 arguments
   2106 ///
   2107 /// Example: Matches Foo(bar, bar)
   2108 /// \code
   2109 ///   Foo h = Foo(bar, bar);
   2110 /// \endcode
   2111 const internal::VariadicDynCastAllOfMatcher<
   2112   Stmt,
   2113   CXXTemporaryObjectExpr> cxxTemporaryObjectExpr;
   2114 
   2115 /// \brief Matches predefined identifier expressions [C99 6.4.2.2].
   2116 ///
   2117 /// Example: Matches __func__
   2118 /// \code
   2119 ///   printf("%s", __func__);
   2120 /// \endcode
   2121 const internal::VariadicDynCastAllOfMatcher<
   2122   Stmt,
   2123   PredefinedExpr> predefinedExpr;
   2124 
   2125 /// \brief Matches C99 designated initializer expressions [C99 6.7.8].
   2126 ///
   2127 /// Example: Matches { [2].y = 1.0, [0].x = 1.0 }
   2128 /// \code
   2129 ///   point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
   2130 /// \endcode
   2131 const internal::VariadicDynCastAllOfMatcher<
   2132   Stmt,
   2133   DesignatedInitExpr> designatedInitExpr;
   2134 
   2135 /// \brief Matches designated initializer expressions that contain
   2136 /// a specific number of designators.
   2137 ///
   2138 /// Example: Given
   2139 /// \code
   2140 ///   point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
   2141 ///   point ptarray2[10] = { [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 };
   2142 /// \endcode
   2143 /// designatorCountIs(2)
   2144 ///   matches '{ [2].y = 1.0, [0].x = 1.0 }',
   2145 ///   but not '{ [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 }'.
   2146 AST_MATCHER_P(DesignatedInitExpr, designatorCountIs, unsigned, N) {
   2147   return Node.size() == N;
   2148 }
   2149 
   2150 /// \brief Matches \c QualTypes in the clang AST.
   2151 const internal::VariadicAllOfMatcher<QualType> qualType;
   2152 
   2153 /// \brief Matches \c Types in the clang AST.
   2154 const internal::VariadicAllOfMatcher<Type> type;
   2155 
   2156 /// \brief Matches \c TypeLocs in the clang AST.
   2157 const internal::VariadicAllOfMatcher<TypeLoc> typeLoc;
   2158 
   2159 /// \brief Matches if any of the given matchers matches.
   2160 ///
   2161 /// Unlike \c anyOf, \c eachOf will generate a match result for each
   2162 /// matching submatcher.
   2163 ///
   2164 /// For example, in:
   2165 /// \code
   2166 ///   class A { int a; int b; };
   2167 /// \endcode
   2168 /// The matcher:
   2169 /// \code
   2170 ///   cxxRecordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
   2171 ///                        has(fieldDecl(hasName("b")).bind("v"))))
   2172 /// \endcode
   2173 /// will generate two results binding "v", the first of which binds
   2174 /// the field declaration of \c a, the second the field declaration of
   2175 /// \c b.
   2176 ///
   2177 /// Usable as: Any Matcher
   2178 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> eachOf = {
   2179   internal::DynTypedMatcher::VO_EachOf
   2180 };
   2181 
   2182 /// \brief Matches if any of the given matchers matches.
   2183 ///
   2184 /// Usable as: Any Matcher
   2185 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> anyOf = {
   2186   internal::DynTypedMatcher::VO_AnyOf
   2187 };
   2188 
   2189 /// \brief Matches if all given matchers match.
   2190 ///
   2191 /// Usable as: Any Matcher
   2192 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> allOf = {
   2193   internal::DynTypedMatcher::VO_AllOf
   2194 };
   2195 
   2196 /// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
   2197 ///
   2198 /// Given
   2199 /// \code
   2200 ///   Foo x = bar;
   2201 ///   int y = sizeof(x) + alignof(x);
   2202 /// \endcode
   2203 /// unaryExprOrTypeTraitExpr()
   2204 ///   matches \c sizeof(x) and \c alignof(x)
   2205 const internal::VariadicDynCastAllOfMatcher<
   2206   Stmt,
   2207   UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr;
   2208 
   2209 /// \brief Matches unary expressions that have a specific type of argument.
   2210 ///
   2211 /// Given
   2212 /// \code
   2213 ///   int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c);
   2214 /// \endcode
   2215 /// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int"))
   2216 ///   matches \c sizeof(a) and \c alignof(c)
   2217 AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType,
   2218               internal::Matcher<QualType>, InnerMatcher) {
   2219   const QualType ArgumentType = Node.getTypeOfArgument();
   2220   return InnerMatcher.matches(ArgumentType, Finder, Builder);
   2221 }
   2222 
   2223 /// \brief Matches unary expressions of a certain kind.
   2224 ///
   2225 /// Given
   2226 /// \code
   2227 ///   int x;
   2228 ///   int s = sizeof(x) + alignof(x)
   2229 /// \endcode
   2230 /// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf))
   2231 ///   matches \c sizeof(x)
   2232 AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) {
   2233   return Node.getKind() == Kind;
   2234 }
   2235 
   2236 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching
   2237 /// alignof.
   2238 inline internal::Matcher<Stmt> alignOfExpr(
   2239     const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
   2240   return stmt(unaryExprOrTypeTraitExpr(allOf(
   2241       ofKind(UETT_AlignOf), InnerMatcher)));
   2242 }
   2243 
   2244 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching
   2245 /// sizeof.
   2246 inline internal::Matcher<Stmt> sizeOfExpr(
   2247     const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
   2248   return stmt(unaryExprOrTypeTraitExpr(
   2249       allOf(ofKind(UETT_SizeOf), InnerMatcher)));
   2250 }
   2251 
   2252 /// \brief Matches NamedDecl nodes that have the specified name.
   2253 ///
   2254 /// Supports specifying enclosing namespaces or classes by prefixing the name
   2255 /// with '<enclosing>::'.
   2256 /// Does not match typedefs of an underlying type with the given name.
   2257 ///
   2258 /// Example matches X (Name == "X")
   2259 /// \code
   2260 ///   class X;
   2261 /// \endcode
   2262 ///
   2263 /// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X")
   2264 /// \code
   2265 ///   namespace a { namespace b { class X; } }
   2266 /// \endcode
   2267 inline internal::Matcher<NamedDecl> hasName(const std::string &Name) {
   2268   std::vector<std::string> Names;
   2269   Names.push_back(Name);
   2270   return internal::Matcher<NamedDecl>(new internal::HasNameMatcher(Names));
   2271 }
   2272 
   2273 /// \brief Matches NamedDecl nodes that have any of the specified names.
   2274 ///
   2275 /// This matcher is only provided as a performance optimization of hasName.
   2276 /// \code
   2277 ///     hasAnyName(a, b, c)
   2278 /// \endcode
   2279 ///  is equivalent to, but faster than
   2280 /// \code
   2281 ///     anyOf(hasName(a), hasName(b), hasName(c))
   2282 /// \endcode
   2283 const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
   2284                                  internal::hasAnyNameFunc>
   2285     hasAnyName = {};
   2286 
   2287 /// \brief Matches NamedDecl nodes whose fully qualified names contain
   2288 /// a substring matched by the given RegExp.
   2289 ///
   2290 /// Supports specifying enclosing namespaces or classes by
   2291 /// prefixing the name with '<enclosing>::'.  Does not match typedefs
   2292 /// of an underlying type with the given name.
   2293 ///
   2294 /// Example matches X (regexp == "::X")
   2295 /// \code
   2296 ///   class X;
   2297 /// \endcode
   2298 ///
   2299 /// Example matches X (regexp is one of "::X", "^foo::.*X", among others)
   2300 /// \code
   2301 ///   namespace foo { namespace bar { class X; } }
   2302 /// \endcode
   2303 AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) {
   2304   assert(!RegExp.empty());
   2305   std::string FullNameString = "::" + Node.getQualifiedNameAsString();
   2306   llvm::Regex RE(RegExp);
   2307   return RE.match(FullNameString);
   2308 }
   2309 
   2310 /// \brief Matches overloaded operator names.
   2311 ///
   2312 /// Matches overloaded operator names specified in strings without the
   2313 /// "operator" prefix: e.g. "<<".
   2314 ///
   2315 /// Given:
   2316 /// \code
   2317 ///   class A { int operator*(); };
   2318 ///   const A &operator<<(const A &a, const A &b);
   2319 ///   A a;
   2320 ///   a << a;   // <-- This matches
   2321 /// \endcode
   2322 ///
   2323 /// \c cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))) matches the
   2324 /// specified line and
   2325 /// \c cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*")))
   2326 /// matches the declaration of \c A.
   2327 ///
   2328 /// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl>
   2329 inline internal::PolymorphicMatcherWithParam1<
   2330     internal::HasOverloadedOperatorNameMatcher, StringRef,
   2331     AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl)>
   2332 hasOverloadedOperatorName(StringRef Name) {
   2333   return internal::PolymorphicMatcherWithParam1<
   2334       internal::HasOverloadedOperatorNameMatcher, StringRef,
   2335       AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl)>(Name);
   2336 }
   2337 
   2338 /// \brief Matches C++ classes that are directly or indirectly derived from
   2339 /// a class matching \c Base.
   2340 ///
   2341 /// Note that a class is not considered to be derived from itself.
   2342 ///
   2343 /// Example matches Y, Z, C (Base == hasName("X"))
   2344 /// \code
   2345 ///   class X;
   2346 ///   class Y : public X {};  // directly derived
   2347 ///   class Z : public Y {};  // indirectly derived
   2348 ///   typedef X A;
   2349 ///   typedef A B;
   2350 ///   class C : public B {};  // derived from a typedef of X
   2351 /// \endcode
   2352 ///
   2353 /// In the following example, Bar matches isDerivedFrom(hasName("X")):
   2354 /// \code
   2355 ///   class Foo;
   2356 ///   typedef Foo X;
   2357 ///   class Bar : public Foo {};  // derived from a type that X is a typedef of
   2358 /// \endcode
   2359 AST_MATCHER_P(CXXRecordDecl, isDerivedFrom,
   2360               internal::Matcher<NamedDecl>, Base) {
   2361   return Finder->classIsDerivedFrom(&Node, Base, Builder);
   2362 }
   2363 
   2364 /// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
   2365 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, std::string, BaseName, 1) {
   2366   assert(!BaseName.empty());
   2367   return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
   2368 }
   2369 
   2370 /// \brief Similar to \c isDerivedFrom(), but also matches classes that directly
   2371 /// match \c Base.
   2372 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom,
   2373                        internal::Matcher<NamedDecl>, Base, 0) {
   2374   return Matcher<CXXRecordDecl>(anyOf(Base, isDerivedFrom(Base)))
   2375       .matches(Node, Finder, Builder);
   2376 }
   2377 
   2378 /// \brief Overloaded method as shortcut for
   2379 /// \c isSameOrDerivedFrom(hasName(...)).
   2380 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, std::string,
   2381                        BaseName, 1) {
   2382   assert(!BaseName.empty());
   2383   return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
   2384 }
   2385 
   2386 /// \brief Matches the first method of a class or struct that satisfies \c
   2387 /// InnerMatcher.
   2388 ///
   2389 /// Given:
   2390 /// \code
   2391 ///   class A { void func(); };
   2392 ///   class B { void member(); };
   2393 /// \endcode
   2394 ///
   2395 /// \c cxxRecordDecl(hasMethod(hasName("func"))) matches the declaration of
   2396 /// \c A but not \c B.
   2397 AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>,
   2398               InnerMatcher) {
   2399   return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(),
   2400                                     Node.method_end(), Finder, Builder);
   2401 }
   2402 
   2403 /// \brief Matches the generated class of lambda expressions.
   2404 ///
   2405 /// Given:
   2406 /// \code
   2407 ///   auto x = []{};
   2408 /// \endcode
   2409 ///
   2410 /// \c cxxRecordDecl(isLambda()) matches the implicit class declaration of
   2411 /// \c decltype(x)
   2412 AST_MATCHER(CXXRecordDecl, isLambda) {
   2413   return Node.isLambda();
   2414 }
   2415 
   2416 /// \brief Matches AST nodes that have child AST nodes that match the
   2417 /// provided matcher.
   2418 ///
   2419 /// Example matches X, Y
   2420 ///   (matcher = cxxRecordDecl(has(cxxRecordDecl(hasName("X")))
   2421 /// \code
   2422 ///   class X {};  // Matches X, because X::X is a class of name X inside X.
   2423 ///   class Y { class X {}; };
   2424 ///   class Z { class Y { class X {}; }; };  // Does not match Z.
   2425 /// \endcode
   2426 ///
   2427 /// ChildT must be an AST base type.
   2428 ///
   2429 /// Usable as: Any Matcher
   2430 /// Note that has is direct matcher, so it also matches things like implicit
   2431 /// casts and paren casts. If you are matching with expr then you should
   2432 /// probably consider using ignoringParenImpCasts like:
   2433 /// has(ignoringParenImpCasts(expr())).
   2434 const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher>
   2435 LLVM_ATTRIBUTE_UNUSED has = {};
   2436 
   2437 /// \brief Matches AST nodes that have descendant AST nodes that match the
   2438 /// provided matcher.
   2439 ///
   2440 /// Example matches X, Y, Z
   2441 ///     (matcher = cxxRecordDecl(hasDescendant(cxxRecordDecl(hasName("X")))))
   2442 /// \code
   2443 ///   class X {};  // Matches X, because X::X is a class of name X inside X.
   2444 ///   class Y { class X {}; };
   2445 ///   class Z { class Y { class X {}; }; };
   2446 /// \endcode
   2447 ///
   2448 /// DescendantT must be an AST base type.
   2449 ///
   2450 /// Usable as: Any Matcher
   2451 const internal::ArgumentAdaptingMatcherFunc<internal::HasDescendantMatcher>
   2452 LLVM_ATTRIBUTE_UNUSED hasDescendant = {};
   2453 
   2454 /// \brief Matches AST nodes that have child AST nodes that match the
   2455 /// provided matcher.
   2456 ///
   2457 /// Example matches X, Y
   2458 ///   (matcher = cxxRecordDecl(forEach(cxxRecordDecl(hasName("X")))
   2459 /// \code
   2460 ///   class X {};  // Matches X, because X::X is a class of name X inside X.
   2461 ///   class Y { class X {}; };
   2462 ///   class Z { class Y { class X {}; }; };  // Does not match Z.
   2463 /// \endcode
   2464 ///
   2465 /// ChildT must be an AST base type.
   2466 ///
   2467 /// As opposed to 'has', 'forEach' will cause a match for each result that
   2468 /// matches instead of only on the first one.
   2469 ///
   2470 /// Usable as: Any Matcher
   2471 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher>
   2472 LLVM_ATTRIBUTE_UNUSED forEach = {};
   2473 
   2474 /// \brief Matches AST nodes that have descendant AST nodes that match the
   2475 /// provided matcher.
   2476 ///
   2477 /// Example matches X, A, B, C
   2478 ///   (matcher = cxxRecordDecl(forEachDescendant(cxxRecordDecl(hasName("X")))))
   2479 /// \code
   2480 ///   class X {};  // Matches X, because X::X is a class of name X inside X.
   2481 ///   class A { class X {}; };
   2482 ///   class B { class C { class X {}; }; };
   2483 /// \endcode
   2484 ///
   2485 /// DescendantT must be an AST base type.
   2486 ///
   2487 /// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for
   2488 /// each result that matches instead of only on the first one.
   2489 ///
   2490 /// Note: Recursively combined ForEachDescendant can cause many matches:
   2491 ///   cxxRecordDecl(forEachDescendant(cxxRecordDecl(
   2492 ///     forEachDescendant(cxxRecordDecl())
   2493 ///   )))
   2494 /// will match 10 times (plus injected class name matches) on:
   2495 /// \code
   2496 ///   class A { class B { class C { class D { class E {}; }; }; }; };
   2497 /// \endcode
   2498 ///
   2499 /// Usable as: Any Matcher
   2500 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachDescendantMatcher>
   2501 LLVM_ATTRIBUTE_UNUSED forEachDescendant = {};
   2502 
   2503 /// \brief Matches if the node or any descendant matches.
   2504 ///
   2505 /// Generates results for each match.
   2506 ///
   2507 /// For example, in:
   2508 /// \code
   2509 ///   class A { class B {}; class C {}; };
   2510 /// \endcode
   2511 /// The matcher:
   2512 /// \code
   2513 ///   cxxRecordDecl(hasName("::A"),
   2514 ///                 findAll(cxxRecordDecl(isDefinition()).bind("m")))
   2515 /// \endcode
   2516 /// will generate results for \c A, \c B and \c C.
   2517 ///
   2518 /// Usable as: Any Matcher
   2519 template <typename T>
   2520 internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
   2521   return eachOf(Matcher, forEachDescendant(Matcher));
   2522 }
   2523 
   2524 /// \brief Matches AST nodes that have a parent that matches the provided
   2525 /// matcher.
   2526 ///
   2527 /// Given
   2528 /// \code
   2529 /// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } }
   2530 /// \endcode
   2531 /// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }".
   2532 ///
   2533 /// Usable as: Any Matcher
   2534 const internal::ArgumentAdaptingMatcherFunc<
   2535     internal::HasParentMatcher,
   2536     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
   2537     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
   2538     LLVM_ATTRIBUTE_UNUSED hasParent = {};
   2539 
   2540 /// \brief Matches AST nodes that have an ancestor that matches the provided
   2541 /// matcher.
   2542 ///
   2543 /// Given
   2544 /// \code
   2545 /// void f() { if (true) { int x = 42; } }
   2546 /// void g() { for (;;) { int x = 43; } }
   2547 /// \endcode
   2548 /// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43.
   2549 ///
   2550 /// Usable as: Any Matcher
   2551 const internal::ArgumentAdaptingMatcherFunc<
   2552     internal::HasAncestorMatcher,
   2553     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
   2554     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
   2555     LLVM_ATTRIBUTE_UNUSED hasAncestor = {};
   2556 
   2557 /// \brief Matches if the provided matcher does not match.
   2558 ///
   2559 /// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X"))))
   2560 /// \code
   2561 ///   class X {};
   2562 ///   class Y {};
   2563 /// \endcode
   2564 ///
   2565 /// Usable as: Any Matcher
   2566 const internal::VariadicOperatorMatcherFunc<1, 1> unless = {
   2567   internal::DynTypedMatcher::VO_UnaryNot
   2568 };
   2569 
   2570 /// \brief Matches a node if the declaration associated with that node
   2571 /// matches the given matcher.
   2572 ///
   2573 /// The associated declaration is:
   2574 /// - for type nodes, the declaration of the underlying type
   2575 /// - for CallExpr, the declaration of the callee
   2576 /// - for MemberExpr, the declaration of the referenced member
   2577 /// - for CXXConstructExpr, the declaration of the constructor
   2578 /// - for CXXNewExpr, the declaration of the operator new
   2579 ///
   2580 /// For type nodes, hasDeclaration will generally match the declaration of the
   2581 /// sugared type. Given
   2582 /// \code
   2583 ///   class X {};
   2584 ///   typedef X Y;
   2585 ///   Y y;
   2586 /// \endcode
   2587 /// in varDecl(hasType(hasDeclaration(decl()))) the decl will match the
   2588 /// typedefDecl. A common use case is to match the underlying, desugared type.
   2589 /// This can be achieved by using the hasUnqualifiedDesugaredType matcher:
   2590 /// \code
   2591 ///   varDecl(hasType(hasUnqualifiedDesugaredType(
   2592 ///       recordType(hasDeclaration(decl())))))
   2593 /// \endcode
   2594 /// In this matcher, the decl will match the CXXRecordDecl of class X.
   2595 ///
   2596 /// Usable as: Matcher<AddrLabelExpr>, Matcher<CallExpr>,
   2597 ///   Matcher<CXXConstructExpr>, Matcher<CXXNewExpr>, Matcher<DeclRefExpr>,
   2598 ///   Matcher<EnumType>, Matcher<InjectedClassNameType>, Matcher<LabelStmt>,
   2599 ///   Matcher<MemberExpr>, Matcher<QualType>, Matcher<RecordType>,
   2600 ///   Matcher<TagType>, Matcher<TemplateSpecializationType>,
   2601 ///   Matcher<TemplateTypeParmType>, Matcher<TypedefType>,
   2602 ///   Matcher<UnresolvedUsingType>
   2603 inline internal::PolymorphicMatcherWithParam1<
   2604     internal::HasDeclarationMatcher, internal::Matcher<Decl>,
   2605     void(internal::HasDeclarationSupportedTypes)>
   2606 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) {
   2607   return internal::PolymorphicMatcherWithParam1<
   2608       internal::HasDeclarationMatcher, internal::Matcher<Decl>,
   2609       void(internal::HasDeclarationSupportedTypes)>(InnerMatcher);
   2610 }
   2611 
   2612 /// \brief Matches a \c NamedDecl whose underlying declaration matches the given
   2613 /// matcher.
   2614 ///
   2615 /// Given
   2616 /// \code
   2617 ///   namespace N { template<class T> void f(T t); }
   2618 ///   template <class T> void g() { using N::f; f(T()); }
   2619 /// \endcode
   2620 /// \c unresolvedLookupExpr(hasAnyDeclaration(
   2621 ///     namedDecl(hasUnderlyingDecl(hasName("::N::f")))))
   2622 ///   matches the use of \c f in \c g() .
   2623 AST_MATCHER_P(NamedDecl, hasUnderlyingDecl, internal::Matcher<NamedDecl>,
   2624               InnerMatcher) {
   2625   const NamedDecl *UnderlyingDecl = Node.getUnderlyingDecl();
   2626 
   2627   return UnderlyingDecl != nullptr &&
   2628          InnerMatcher.matches(*UnderlyingDecl, Finder, Builder);
   2629 }
   2630 
   2631 /// \brief Matches on the implicit object argument of a member call expression.
   2632 ///
   2633 /// Example matches y.x()
   2634 ///   (matcher = cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y"))))))
   2635 /// \code
   2636 ///   class Y { public: void x(); };
   2637 ///   void z() { Y y; y.x(); }",
   2638 /// \endcode
   2639 ///
   2640 /// FIXME: Overload to allow directly matching types?
   2641 AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
   2642               InnerMatcher) {
   2643   const Expr *ExprNode = Node.getImplicitObjectArgument()
   2644                             ->IgnoreParenImpCasts();
   2645   return (ExprNode != nullptr &&
   2646           InnerMatcher.matches(*ExprNode, Finder, Builder));
   2647 }
   2648 
   2649 
   2650 /// \brief Matches on the receiver of an ObjectiveC Message expression.
   2651 ///
   2652 /// Example
   2653 /// matcher = objCMessageExpr(hasReceiverType(asString("UIWebView *")));
   2654 /// matches the [webView ...] message invocation.
   2655 /// \code
   2656 ///   NSString *webViewJavaScript = ...
   2657 ///   UIWebView *webView = ...
   2658 ///   [webView stringByEvaluatingJavaScriptFromString:webViewJavascript];
   2659 /// \endcode
   2660 AST_MATCHER_P(ObjCMessageExpr, hasReceiverType, internal::Matcher<QualType>,
   2661               InnerMatcher) {
   2662   const QualType TypeDecl = Node.getReceiverType();
   2663   return InnerMatcher.matches(TypeDecl, Finder, Builder);
   2664 }
   2665 
   2666 /// \brief Matches when BaseName == Selector.getAsString()
   2667 ///
   2668 ///  matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:"));
   2669 ///  matches the outer message expr in the code below, but NOT the message
   2670 ///  invocation for self.bodyView.
   2671 /// \code
   2672 ///     [self.bodyView loadHTMLString:html baseURL:NULL];
   2673 /// \endcode
   2674 AST_MATCHER_P(ObjCMessageExpr, hasSelector, std::string, BaseName) {
   2675   Selector Sel = Node.getSelector();
   2676   return BaseName.compare(Sel.getAsString()) == 0;
   2677 }
   2678 
   2679 
   2680 /// \brief Matches ObjC selectors whose name contains
   2681 /// a substring matched by the given RegExp.
   2682 ///  matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?"));
   2683 ///  matches the outer message expr in the code below, but NOT the message
   2684 ///  invocation for self.bodyView.
   2685 /// \code
   2686 ///     [self.bodyView loadHTMLString:html baseURL:NULL];
   2687 /// \endcode
   2688 AST_MATCHER_P(ObjCMessageExpr, matchesSelector, std::string, RegExp) {
   2689   assert(!RegExp.empty());
   2690   std::string SelectorString = Node.getSelector().getAsString();
   2691   llvm::Regex RE(RegExp);
   2692   return RE.match(SelectorString);
   2693 }
   2694 
   2695 /// \brief Matches when the selector is the empty selector
   2696 ///
   2697 /// Matches only when the selector of the objCMessageExpr is NULL. This may
   2698 /// represent an error condition in the tree!
   2699 AST_MATCHER(ObjCMessageExpr, hasNullSelector) {
   2700   return Node.getSelector().isNull();
   2701 }
   2702 
   2703 /// \brief Matches when the selector is a Unary Selector
   2704 ///
   2705 ///  matcher = objCMessageExpr(matchesSelector(hasUnarySelector());
   2706 ///  matches self.bodyView in the code below, but NOT the outer message
   2707 ///  invocation of "loadHTMLString:baseURL:".
   2708 /// \code
   2709 ///     [self.bodyView loadHTMLString:html baseURL:NULL];
   2710 /// \endcode
   2711 AST_MATCHER(ObjCMessageExpr, hasUnarySelector) {
   2712   return Node.getSelector().isUnarySelector();
   2713 }
   2714 
   2715 /// \brief Matches when the selector is a keyword selector
   2716 ///
   2717 /// objCMessageExpr(hasKeywordSelector()) matches the generated setFrame
   2718 /// message expression in
   2719 ///
   2720 /// \code
   2721 ///   UIWebView *webView = ...;
   2722 ///   CGRect bodyFrame = webView.frame;
   2723 ///   bodyFrame.size.height = self.bodyContentHeight;
   2724 ///   webView.frame = bodyFrame;
   2725 ///   //     ^---- matches here
   2726 /// \endcode
   2727 AST_MATCHER(ObjCMessageExpr, hasKeywordSelector) {
   2728   return Node.getSelector().isKeywordSelector();
   2729 }
   2730 
   2731 /// \brief Matches when the selector has the specified number of arguments
   2732 ///
   2733 ///  matcher = objCMessageExpr(numSelectorArgs(0));
   2734 ///  matches self.bodyView in the code below
   2735 ///
   2736 ///  matcher = objCMessageExpr(numSelectorArgs(2));
   2737 ///  matches the invocation of "loadHTMLString:baseURL:" but not that
   2738 ///  of self.bodyView
   2739 /// \code
   2740 ///     [self.bodyView loadHTMLString:html baseURL:NULL];
   2741 /// \endcode
   2742 AST_MATCHER_P(ObjCMessageExpr, numSelectorArgs, unsigned, N) {
   2743   return Node.getSelector().getNumArgs() == N;
   2744 }
   2745 
   2746 /// \brief Matches if the call expression's callee expression matches.
   2747 ///
   2748 /// Given
   2749 /// \code
   2750 ///   class Y { void x() { this->x(); x(); Y y; y.x(); } };
   2751 ///   void f() { f(); }
   2752 /// \endcode
   2753 /// callExpr(callee(expr()))
   2754 ///   matches this->x(), x(), y.x(), f()
   2755 /// with callee(...)
   2756 ///   matching this->x, x, y.x, f respectively
   2757 ///
   2758 /// Note: Callee cannot take the more general internal::Matcher<Expr>
   2759 /// because this introduces ambiguous overloads with calls to Callee taking a
   2760 /// internal::Matcher<Decl>, as the matcher hierarchy is purely
   2761 /// implemented in terms of implicit casts.
   2762 AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
   2763               InnerMatcher) {
   2764   const Expr *ExprNode = Node.getCallee();
   2765   return (ExprNode != nullptr &&
   2766           InnerMatcher.matches(*ExprNode, Finder, Builder));
   2767 }
   2768 
   2769 /// \brief Matches if the call expression's callee's declaration matches the
   2770 /// given matcher.
   2771 ///
   2772 /// Example matches y.x() (matcher = callExpr(callee(
   2773 ///                                    cxxMethodDecl(hasName("x")))))
   2774 /// \code
   2775 ///   class Y { public: void x(); };
   2776 ///   void z() { Y y; y.x(); }
   2777 /// \endcode
   2778 AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
   2779                        1) {
   2780   return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder);
   2781 }
   2782 
   2783 /// \brief Matches if the expression's or declaration's type matches a type
   2784 /// matcher.
   2785 ///
   2786 /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
   2787 ///             and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
   2788 ///             and U (matcher = typedefDecl(hasType(asString("int")))
   2789 /// \code
   2790 ///  class X {};
   2791 ///  void y(X &x) { x; X z; }
   2792 ///  typedef int U;
   2793 /// \endcode
   2794 AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
   2795     hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, TypedefNameDecl, ValueDecl),
   2796     internal::Matcher<QualType>, InnerMatcher, 0) {
   2797   return InnerMatcher.matches(internal::getUnderlyingType(Node),
   2798                               Finder, Builder);
   2799 }
   2800 
   2801 /// \brief Overloaded to match the declaration of the expression's or value
   2802 /// declaration's type.
   2803 ///
   2804 /// In case of a value declaration (for example a variable declaration),
   2805 /// this resolves one layer of indirection. For example, in the value
   2806 /// declaration "X x;", cxxRecordDecl(hasName("X")) matches the declaration of
   2807 /// X, while varDecl(hasType(cxxRecordDecl(hasName("X")))) matches the
   2808 /// declaration of x.
   2809 ///
   2810 /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
   2811 ///             and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
   2812 /// \code
   2813 ///  class X {};
   2814 ///  void y(X &x) { x; X z; }
   2815 /// \endcode
   2816 ///
   2817 /// Usable as: Matcher<Expr>, Matcher<ValueDecl>
   2818 AST_POLYMORPHIC_MATCHER_P_OVERLOAD(hasType,
   2819                                    AST_POLYMORPHIC_SUPPORTED_TYPES(Expr,
   2820                                                                    ValueDecl),
   2821                                    internal::Matcher<Decl>, InnerMatcher, 1) {
   2822   return qualType(hasDeclaration(InnerMatcher))
   2823       .matches(Node.getType(), Finder, Builder);
   2824 }
   2825 
   2826 /// \brief Matches if the type location of the declarator decl's type matches
   2827 /// the inner matcher.
   2828 ///
   2829 /// Given
   2830 /// \code
   2831 ///   int x;
   2832 /// \endcode
   2833 /// declaratorDecl(hasTypeLoc(loc(asString("int"))))
   2834 ///   matches int x
   2835 AST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) {
   2836   if (!Node.getTypeSourceInfo())
   2837     // This happens for example for implicit destructors.
   2838     return false;
   2839   return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder);
   2840 }
   2841 
   2842 /// \brief Matches if the matched type is represented by the given string.
   2843 ///
   2844 /// Given
   2845 /// \code
   2846 ///   class Y { public: void x(); };
   2847 ///   void z() { Y* y; y->x(); }
   2848 /// \endcode
   2849 /// cxxMemberCallExpr(on(hasType(asString("class Y *"))))
   2850 ///   matches y->x()
   2851 AST_MATCHER_P(QualType, asString, std::string, Name) {
   2852   return Name == Node.getAsString();
   2853 }
   2854 
   2855 /// \brief Matches if the matched type is a pointer type and the pointee type
   2856 /// matches the specified matcher.
   2857 ///
   2858 /// Example matches y->x()
   2859 ///   (matcher = cxxMemberCallExpr(on(hasType(pointsTo
   2860 ///      cxxRecordDecl(hasName("Y")))))))
   2861 /// \code
   2862 ///   class Y { public: void x(); };
   2863 ///   void z() { Y *y; y->x(); }
   2864 /// \endcode
   2865 AST_MATCHER_P(
   2866     QualType, pointsTo, internal::Matcher<QualType>,
   2867     InnerMatcher) {
   2868   return (!Node.isNull() && Node->isAnyPointerType() &&
   2869           InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
   2870 }
   2871 
   2872 /// \brief Overloaded to match the pointee type's declaration.
   2873 AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>,
   2874                        InnerMatcher, 1) {
   2875   return pointsTo(qualType(hasDeclaration(InnerMatcher)))
   2876       .matches(Node, Finder, Builder);
   2877 }
   2878 
   2879 /// \brief Matches if the matched type matches the unqualified desugared
   2880 /// type of the matched node.
   2881 ///
   2882 /// For example, in:
   2883 /// \code
   2884 ///   class A {};
   2885 ///   using B = A;
   2886 /// \endcode
   2887 /// The matcher type(hasUnqualifeidDesugaredType(recordType())) matches
   2888 /// both B and A.
   2889 AST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<Type>,
   2890               InnerMatcher) {
   2891   return InnerMatcher.matches(*Node.getUnqualifiedDesugaredType(), Finder,
   2892                               Builder);
   2893 }
   2894 
   2895 /// \brief Matches if the matched type is a reference type and the referenced
   2896 /// type matches the specified matcher.
   2897 ///
   2898 /// Example matches X &x and const X &y
   2899 ///     (matcher = varDecl(hasType(references(cxxRecordDecl(hasName("X"))))))
   2900 /// \code
   2901 ///   class X {
   2902 ///     void a(X b) {
   2903 ///       X &x = b;
   2904 ///       const X &y = b;
   2905 ///     }
   2906 ///   };
   2907 /// \endcode
   2908 AST_MATCHER_P(QualType, references, internal::Matcher<QualType>,
   2909               InnerMatcher) {
   2910   return (!Node.isNull() && Node->isReferenceType() &&
   2911           InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
   2912 }
   2913 
   2914 /// \brief Matches QualTypes whose canonical type matches InnerMatcher.
   2915 ///
   2916 /// Given:
   2917 /// \code
   2918 ///   typedef int &int_ref;
   2919 ///   int a;
   2920 ///   int_ref b = a;
   2921 /// \endcode
   2922 ///
   2923 /// \c varDecl(hasType(qualType(referenceType()))))) will not match the
   2924 /// declaration of b but \c
   2925 /// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does.
   2926 AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>,
   2927               InnerMatcher) {
   2928   if (Node.isNull())
   2929     return false;
   2930   return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder);
   2931 }
   2932 
   2933 /// \brief Overloaded to match the referenced type's declaration.
   2934 AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>,
   2935                        InnerMatcher, 1) {
   2936   return references(qualType(hasDeclaration(InnerMatcher)))
   2937       .matches(Node, Finder, Builder);
   2938 }
   2939 
   2940 AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument,
   2941               internal::Matcher<Expr>, InnerMatcher) {
   2942   const Expr *ExprNode = Node.getImplicitObjectArgument();
   2943   return (ExprNode != nullptr &&
   2944           InnerMatcher.matches(*ExprNode, Finder, Builder));
   2945 }
   2946 
   2947 /// \brief Matches if the expression's type either matches the specified
   2948 /// matcher, or is a pointer to a type that matches the InnerMatcher.
   2949 AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType,
   2950                        internal::Matcher<QualType>, InnerMatcher, 0) {
   2951   return onImplicitObjectArgument(
   2952       anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
   2953       .matches(Node, Finder, Builder);
   2954 }
   2955 
   2956 /// \brief Overloaded to match the type's declaration.
   2957 AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType,
   2958                        internal::Matcher<Decl>, InnerMatcher, 1) {
   2959   return onImplicitObjectArgument(
   2960       anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
   2961       .matches(Node, Finder, Builder);
   2962 }
   2963 
   2964 /// \brief Matches a DeclRefExpr that refers to a declaration that matches the
   2965 /// specified matcher.
   2966 ///
   2967 /// Example matches x in if(x)
   2968 ///     (matcher = declRefExpr(to(varDecl(hasName("x")))))
   2969 /// \code
   2970 ///   bool x;
   2971 ///   if (x) {}
   2972 /// \endcode
   2973 AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
   2974               InnerMatcher) {
   2975   const Decl *DeclNode = Node.getDecl();
   2976   return (DeclNode != nullptr &&
   2977           InnerMatcher.matches(*DeclNode, Finder, Builder));
   2978 }
   2979 
   2980 /// \brief Matches a \c DeclRefExpr that refers to a declaration through a
   2981 /// specific using shadow declaration.
   2982 ///
   2983 /// Given
   2984 /// \code
   2985 ///   namespace a { void f() {} }
   2986 ///   using a::f;
   2987 ///   void g() {
   2988 ///     f();     // Matches this ..
   2989 ///     a::f();  // .. but not this.
   2990 ///   }
   2991 /// \endcode
   2992 /// declRefExpr(throughUsingDecl(anything()))
   2993 ///   matches \c f()
   2994 AST_MATCHER_P(DeclRefExpr, throughUsingDecl,
   2995               internal::Matcher<UsingShadowDecl>, InnerMatcher) {
   2996   const NamedDecl *FoundDecl = Node.getFoundDecl();
   2997   if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl))
   2998     return InnerMatcher.matches(*UsingDecl, Finder, Builder);
   2999   return false;
   3000 }
   3001 
   3002 /// \brief Matches an \c OverloadExpr if any of the declarations in the set of
   3003 /// overloads matches the given matcher.
   3004 ///
   3005 /// Given
   3006 /// \code
   3007 ///   template <typename T> void foo(T);
   3008 ///   template <typename T> void bar(T);
   3009 ///   template <typename T> void baz(T t) {
   3010 ///     foo(t);
   3011 ///     bar(t);
   3012 ///   }
   3013 /// \endcode
   3014 /// unresolvedLookupExpr(hasAnyDeclaration(
   3015 ///     functionTemplateDecl(hasName("foo"))))
   3016 ///   matches \c foo in \c foo(t); but not \c bar in \c bar(t);
   3017 AST_MATCHER_P(OverloadExpr, hasAnyDeclaration, internal::Matcher<Decl>,
   3018               InnerMatcher) {
   3019   return matchesFirstInPointerRange(InnerMatcher, Node.decls_begin(),
   3020                                     Node.decls_end(), Finder, Builder);
   3021 }
   3022 
   3023 /// \brief Matches the Decl of a DeclStmt which has a single declaration.
   3024 ///
   3025 /// Given
   3026 /// \code
   3027 ///   int a, b;
   3028 ///   int c;
   3029 /// \endcode
   3030 /// declStmt(hasSingleDecl(anything()))
   3031 ///   matches 'int c;' but not 'int a, b;'.
   3032 AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) {
   3033   if (Node.isSingleDecl()) {
   3034     const Decl *FoundDecl = Node.getSingleDecl();
   3035     return InnerMatcher.matches(*FoundDecl, Finder, Builder);
   3036   }
   3037   return false;
   3038 }
   3039 
   3040 /// \brief Matches a variable declaration that has an initializer expression
   3041 /// that matches the given matcher.
   3042 ///
   3043 /// Example matches x (matcher = varDecl(hasInitializer(callExpr())))
   3044 /// \code
   3045 ///   bool y() { return true; }
   3046 ///   bool x = y();
   3047 /// \endcode
   3048 AST_MATCHER_P(
   3049     VarDecl, hasInitializer, internal::Matcher<Expr>,
   3050     InnerMatcher) {
   3051   const Expr *Initializer = Node.getAnyInitializer();
   3052   return (Initializer != nullptr &&
   3053           InnerMatcher.matches(*Initializer, Finder, Builder));
   3054 }
   3055 
   3056 /// \brief Matches a variable declaration that has function scope and is a
   3057 /// non-static local variable.
   3058 ///
   3059 /// Example matches x (matcher = varDecl(hasLocalStorage())
   3060 /// \code
   3061 /// void f() {
   3062 ///   int x;
   3063 ///   static int y;
   3064 /// }
   3065 /// int z;
   3066 /// \endcode
   3067 AST_MATCHER(VarDecl, hasLocalStorage) {
   3068   return Node.hasLocalStorage();
   3069 }
   3070 
   3071 /// \brief Matches a variable declaration that does not have local storage.
   3072 ///
   3073 /// Example matches y and z (matcher = varDecl(hasGlobalStorage())
   3074 /// \code
   3075 /// void f() {
   3076 ///   int x;
   3077 ///   static int y;
   3078 /// }
   3079 /// int z;
   3080 /// \endcode
   3081 AST_MATCHER(VarDecl, hasGlobalStorage) {
   3082   return Node.hasGlobalStorage();
   3083 }
   3084 
   3085 /// \brief Matches a variable declaration that has automatic storage duration.
   3086 ///
   3087 /// Example matches x, but not y, z, or a.
   3088 /// (matcher = varDecl(hasAutomaticStorageDuration())
   3089 /// \code
   3090 /// void f() {
   3091 ///   int x;
   3092 ///   static int y;
   3093 ///   thread_local int z;
   3094 /// }
   3095 /// int a;
   3096 /// \endcode
   3097 AST_MATCHER(VarDecl, hasAutomaticStorageDuration) {
   3098   return Node.getStorageDuration() == SD_Automatic;
   3099 }
   3100 
   3101 /// \brief Matches a variable declaration that has static storage duration.
   3102 /// It includes the variable declared at namespace scope and those declared
   3103 /// with "static" and "extern" storage class specifiers.
   3104 ///
   3105 /// \code
   3106 /// void f() {
   3107 ///   int x;
   3108 ///   static int y;
   3109 ///   thread_local int z;
   3110 /// }
   3111 /// int a;
   3112 /// static int b;
   3113 /// extern int c;
   3114 /// varDecl(hasStaticStorageDuration())
   3115 ///   matches the function declaration y, a, b and c.
   3116 /// \endcode
   3117 AST_MATCHER(VarDecl, hasStaticStorageDuration) {
   3118   return Node.getStorageDuration() == SD_Static;
   3119 }
   3120 
   3121 /// \brief Matches a variable declaration that has thread storage duration.
   3122 ///
   3123 /// Example matches z, but not x, z, or a.
   3124 /// (matcher = varDecl(hasThreadStorageDuration())
   3125 /// \code
   3126 /// void f() {
   3127 ///   int x;
   3128 ///   static int y;
   3129 ///   thread_local int z;
   3130 /// }
   3131 /// int a;
   3132 /// \endcode
   3133 AST_MATCHER(VarDecl, hasThreadStorageDuration) {
   3134   return Node.getStorageDuration() == SD_Thread;
   3135 }
   3136 
   3137 /// \brief Matches a variable declaration that is an exception variable from
   3138 /// a C++ catch block, or an Objective-C \@catch statement.
   3139 ///
   3140 /// Example matches x (matcher = varDecl(isExceptionVariable())
   3141 /// \code
   3142 /// void f(int y) {
   3143 ///   try {
   3144 ///   } catch (int x) {
   3145 ///   }
   3146 /// }
   3147 /// \endcode
   3148 AST_MATCHER(VarDecl, isExceptionVariable) {
   3149   return Node.isExceptionVariable();
   3150 }
   3151 
   3152 /// \brief Checks that a call expression or a constructor call expression has
   3153 /// a specific number of arguments (including absent default arguments).
   3154 ///
   3155 /// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2)))
   3156 /// \code
   3157 ///   void f(int x, int y);
   3158 ///   f(0, 0);
   3159 /// \endcode
   3160 AST_POLYMORPHIC_MATCHER_P(argumentCountIs,
   3161                           AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
   3162                                                           CXXConstructExpr,
   3163                                                           ObjCMessageExpr),
   3164                           unsigned, N) {
   3165   return Node.getNumArgs() == N;
   3166 }
   3167 
   3168 /// \brief Matches the n'th argument of a call expression or a constructor
   3169 /// call expression.
   3170 ///
   3171 /// Example matches y in x(y)
   3172 ///     (matcher = callExpr(hasArgument(0, declRefExpr())))
   3173 /// \code
   3174 ///   void x(int) { int y; x(y); }
   3175 /// \endcode
   3176 AST_POLYMORPHIC_MATCHER_P2(hasArgument,
   3177                            AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
   3178                                                            CXXConstructExpr,
   3179                                                            ObjCMessageExpr),
   3180                            unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
   3181   return (N < Node.getNumArgs() &&
   3182           InnerMatcher.matches(
   3183               *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder));
   3184 }
   3185 
   3186 /// \brief Matches declaration statements that contain a specific number of
   3187 /// declarations.
   3188 ///
   3189 /// Example: Given
   3190 /// \code
   3191 ///   int a, b;
   3192 ///   int c;
   3193 ///   int d = 2, e;
   3194 /// \endcode
   3195 /// declCountIs(2)
   3196 ///   matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'.
   3197 AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) {
   3198   return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N;
   3199 }
   3200 
   3201 /// \brief Matches the n'th declaration of a declaration statement.
   3202 ///
   3203 /// Note that this does not work for global declarations because the AST
   3204 /// breaks up multiple-declaration DeclStmt's into multiple single-declaration
   3205 /// DeclStmt's.
   3206 /// Example: Given non-global declarations
   3207 /// \code
   3208 ///   int a, b = 0;
   3209 ///   int c;
   3210 ///   int d = 2, e;
   3211 /// \endcode
   3212 /// declStmt(containsDeclaration(
   3213 ///       0, varDecl(hasInitializer(anything()))))
   3214 ///   matches only 'int d = 2, e;', and
   3215 /// declStmt(containsDeclaration(1, varDecl()))
   3216 /// \code
   3217 ///   matches 'int a, b = 0' as well as 'int d = 2, e;'
   3218 ///   but 'int c;' is not matched.
   3219 /// \endcode
   3220 AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N,
   3221                internal::Matcher<Decl>, InnerMatcher) {
   3222   const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end());
   3223   if (N >= NumDecls)
   3224     return false;
   3225   DeclStmt::const_decl_iterator Iterator = Node.decl_begin();
   3226   std::advance(Iterator, N);
   3227   return InnerMatcher.matches(**Iterator, Finder, Builder);
   3228 }
   3229 
   3230 /// \brief Matches a C++ catch statement that has a catch-all handler.
   3231 ///
   3232 /// Given
   3233 /// \code
   3234 ///   try {
   3235 ///     // ...
   3236 ///   } catch (int) {
   3237 ///     // ...
   3238 ///   } catch (...) {
   3239 ///     // ...
   3240 ///   }
   3241 /// /endcode
   3242 /// cxxCatchStmt(isCatchAll()) matches catch(...) but not catch(int).
   3243 AST_MATCHER(CXXCatchStmt, isCatchAll) {
   3244   return Node.getExceptionDecl() == nullptr;
   3245 }
   3246 
   3247 /// \brief Matches a constructor initializer.
   3248 ///
   3249 /// Given
   3250 /// \code
   3251 ///   struct Foo {
   3252 ///     Foo() : foo_(1) { }
   3253 ///     int foo_;
   3254 ///   };
   3255 /// \endcode
   3256 /// cxxRecordDecl(has(cxxConstructorDecl(
   3257 ///   hasAnyConstructorInitializer(anything())
   3258 /// )))
   3259 ///   record matches Foo, hasAnyConstructorInitializer matches foo_(1)
   3260 AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer,
   3261               internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
   3262   return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(),
   3263                                     Node.init_end(), Finder, Builder);
   3264 }
   3265 
   3266 /// \brief Matches the field declaration of a constructor initializer.
   3267 ///
   3268 /// Given
   3269 /// \code
   3270 ///   struct Foo {
   3271 ///     Foo() : foo_(1) { }
   3272 ///     int foo_;
   3273 ///   };
   3274 /// \endcode
   3275 /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
   3276 ///     forField(hasName("foo_"))))))
   3277 ///   matches Foo
   3278 /// with forField matching foo_
   3279 AST_MATCHER_P(CXXCtorInitializer, forField,
   3280               internal::Matcher<FieldDecl>, InnerMatcher) {
   3281   const FieldDecl *NodeAsDecl = Node.getAnyMember();
   3282   return (NodeAsDecl != nullptr &&
   3283       InnerMatcher.matches(*NodeAsDecl, Finder, Builder));
   3284 }
   3285 
   3286 /// \brief Matches the initializer expression of a constructor initializer.
   3287 ///
   3288 /// Given
   3289 /// \code
   3290 ///   struct Foo {
   3291 ///     Foo() : foo_(1) { }
   3292 ///     int foo_;
   3293 ///   };
   3294 /// \endcode
   3295 /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
   3296 ///     withInitializer(integerLiteral(equals(1)))))))
   3297 ///   matches Foo
   3298 /// with withInitializer matching (1)
   3299 AST_MATCHER_P(CXXCtorInitializer, withInitializer,
   3300               internal::Matcher<Expr>, InnerMatcher) {
   3301   const Expr* NodeAsExpr = Node.getInit();
   3302   return (NodeAsExpr != nullptr &&
   3303       InnerMatcher.matches(*NodeAsExpr, Finder, Builder));
   3304 }
   3305 
   3306 /// \brief Matches a constructor initializer if it is explicitly written in
   3307 /// code (as opposed to implicitly added by the compiler).
   3308 ///
   3309 /// Given
   3310 /// \code
   3311 ///   struct Foo {
   3312 ///     Foo() { }
   3313 ///     Foo(int) : foo_("A") { }
   3314 ///     string foo_;
   3315 ///   };
   3316 /// \endcode
   3317 /// cxxConstructorDecl(hasAnyConstructorInitializer(isWritten()))
   3318 ///   will match Foo(int), but not Foo()
   3319 AST_MATCHER(CXXCtorInitializer, isWritten) {
   3320   return Node.isWritten();
   3321 }
   3322 
   3323 /// \brief Matches a constructor initializer if it is initializing a base, as
   3324 /// opposed to a member.
   3325 ///
   3326 /// Given
   3327 /// \code
   3328 ///   struct B {};
   3329 ///   struct D : B {
   3330 ///     int I;
   3331 ///     D(int i) : I(i) {}
   3332 ///   };
   3333 ///   struct E : B {
   3334 ///     E() : B() {}
   3335 ///   };
   3336 /// \endcode
   3337 /// cxxConstructorDecl(hasAnyConstructorInitializer(isBaseInitializer()))
   3338 ///   will match E(), but not match D(int).
   3339 AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
   3340   return Node.isBaseInitializer();
   3341 }
   3342 
   3343 /// \brief Matches a constructor initializer if it is initializing a member, as
   3344 /// opposed to a base.
   3345 ///
   3346 /// Given
   3347 /// \code
   3348 ///   struct B {};
   3349 ///   struct D : B {
   3350 ///     int I;
   3351 ///     D(int i) : I(i) {}
   3352 ///   };
   3353 ///   struct E : B {
   3354 ///     E() : B() {}
   3355 ///   };
   3356 /// \endcode
   3357 /// cxxConstructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
   3358 ///   will match D(int), but not match E().
   3359 AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
   3360   return Node.isMemberInitializer();
   3361 }
   3362 
   3363 /// \brief Matches any argument of a call expression or a constructor call
   3364 /// expression.
   3365 ///
   3366 /// Given
   3367 /// \code
   3368 ///   void x(int, int, int) { int y; x(1, y, 42); }
   3369 /// \endcode
   3370 /// callExpr(hasAnyArgument(declRefExpr()))
   3371 ///   matches x(1, y, 42)
   3372 /// with hasAnyArgument(...)
   3373 ///   matching y
   3374 AST_POLYMORPHIC_MATCHER_P(hasAnyArgument,
   3375                           AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
   3376                                                           CXXConstructExpr),
   3377                           internal::Matcher<Expr>, InnerMatcher) {
   3378   for (const Expr *Arg : Node.arguments()) {
   3379     BoundNodesTreeBuilder Result(*Builder);
   3380     if (InnerMatcher.matches(*Arg, Finder, &Result)) {
   3381       *Builder = std::move(Result);
   3382       return true;
   3383     }
   3384   }
   3385   return false;
   3386 }
   3387 
   3388 /// \brief Matches a constructor call expression which uses list initialization.
   3389 AST_MATCHER(CXXConstructExpr, isListInitialization) {
   3390   return Node.isListInitialization();
   3391 }
   3392 
   3393 /// \brief Matches a constructor call expression which requires
   3394 /// zero initialization.
   3395 ///
   3396 /// Given
   3397 /// \code
   3398 /// void foo() {
   3399 ///   struct point { double x; double y; };
   3400 ///   point pt[2] = { { 1.0, 2.0 } };
   3401 /// }
   3402 /// \endcode
   3403 /// initListExpr(has(cxxConstructExpr(requiresZeroInitialization()))
   3404 /// will match the implicit array filler for pt[1].
   3405 AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) {
   3406   return Node.requiresZeroInitialization();
   3407 }
   3408 
   3409 /// \brief Matches the n'th parameter of a function declaration.
   3410 ///
   3411 /// Given
   3412 /// \code
   3413 ///   class X { void f(int x) {} };
   3414 /// \endcode
   3415 /// cxxMethodDecl(hasParameter(0, hasType(varDecl())))
   3416 ///   matches f(int x) {}
   3417 /// with hasParameter(...)
   3418 ///   matching int x
   3419 AST_MATCHER_P2(FunctionDecl, hasParameter,
   3420                unsigned, N, internal::Matcher<ParmVarDecl>,
   3421                InnerMatcher) {
   3422   return (N < Node.getNumParams() &&
   3423           InnerMatcher.matches(
   3424               *Node.getParamDecl(N), Finder, Builder));
   3425 }
   3426 
   3427 /// \brief Matches all arguments and their respective ParmVarDecl.
   3428 ///
   3429 /// Given
   3430 /// \code
   3431 ///   void f(int i);
   3432 ///   int y;
   3433 ///   f(y);
   3434 /// \endcode
   3435 /// callExpr(
   3436 ///   forEachArgumentWithParam(
   3437 ///     declRefExpr(to(varDecl(hasName("y")))),
   3438 ///     parmVarDecl(hasType(isInteger()))
   3439 /// ))
   3440 ///   matches f(y);
   3441 /// with declRefExpr(...)
   3442 ///   matching int y
   3443 /// and parmVarDecl(...)
   3444 ///   matching int i
   3445 AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam,
   3446                            AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
   3447                                                            CXXConstructExpr),
   3448                            internal::Matcher<Expr>, ArgMatcher,
   3449                            internal::Matcher<ParmVarDecl>, ParamMatcher) {
   3450   BoundNodesTreeBuilder Result;
   3451   // The first argument of an overloaded member operator is the implicit object
   3452   // argument of the method which should not be matched against a parameter, so
   3453   // we skip over it here.
   3454   BoundNodesTreeBuilder Matches;
   3455   unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl()))
   3456                               .matches(Node, Finder, &Matches)
   3457                           ? 1
   3458                           : 0;
   3459   int ParamIndex = 0;
   3460   bool Matched = false;
   3461   for (; ArgIndex < Node.getNumArgs(); ++ArgIndex) {
   3462     BoundNodesTreeBuilder ArgMatches(*Builder);
   3463     if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()),
   3464                            Finder, &ArgMatches)) {
   3465       BoundNodesTreeBuilder ParamMatches(ArgMatches);
   3466       if (expr(anyOf(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
   3467                          hasParameter(ParamIndex, ParamMatcher)))),
   3468                      callExpr(callee(functionDecl(
   3469                          hasParameter(ParamIndex, ParamMatcher))))))
   3470               .matches(Node, Finder, &ParamMatches)) {
   3471         Result.addMatch(ParamMatches);
   3472         Matched = true;
   3473       }
   3474     }
   3475     ++ParamIndex;
   3476   }
   3477   *Builder = std::move(Result);
   3478   return Matched;
   3479 }
   3480 
   3481 /// \brief Matches any parameter of a function declaration.
   3482 ///
   3483 /// Does not match the 'this' parameter of a method.
   3484 ///
   3485 /// Given
   3486 /// \code
   3487 ///   class X { void f(int x, int y, int z) {} };
   3488 /// \endcode
   3489 /// cxxMethodDecl(hasAnyParameter(hasName("y")))
   3490 ///   matches f(int x, int y, int z) {}
   3491 /// with hasAnyParameter(...)
   3492 ///   matching int y
   3493 AST_MATCHER_P(FunctionDecl, hasAnyParameter,
   3494               internal::Matcher<ParmVarDecl>, InnerMatcher) {
   3495   return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(),
   3496                                     Node.param_end(), Finder, Builder);
   3497 }
   3498 
   3499 /// \brief Matches \c FunctionDecls and \c FunctionProtoTypes that have a
   3500 /// specific parameter count.
   3501 ///
   3502 /// Given
   3503 /// \code
   3504 ///   void f(int i) {}
   3505 ///   void g(int i, int j) {}
   3506 ///   void h(int i, int j);
   3507 ///   void j(int i);
   3508 ///   void k(int x, int y, int z, ...);
   3509 /// \endcode
   3510 /// functionDecl(parameterCountIs(2))
   3511 ///   matches void g(int i, int j) {}
   3512 /// functionProtoType(parameterCountIs(2))
   3513 ///   matches void h(int i, int j)
   3514 /// functionProtoType(parameterCountIs(3))
   3515 ///   matches void k(int x, int y, int z, ...);
   3516 AST_POLYMORPHIC_MATCHER_P(parameterCountIs,
   3517                           AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
   3518                                                           FunctionProtoType),
   3519                           unsigned, N) {
   3520   return Node.getNumParams() == N;
   3521 }
   3522 
   3523 /// \brief Matches the return type of a function declaration.
   3524 ///
   3525 /// Given:
   3526 /// \code
   3527 ///   class X { int f() { return 1; } };
   3528 /// \endcode
   3529 /// cxxMethodDecl(returns(asString("int")))
   3530 ///   matches int f() { return 1; }
   3531 AST_MATCHER_P(FunctionDecl, returns,
   3532               internal::Matcher<QualType>, InnerMatcher) {
   3533   return InnerMatcher.matches(Node.getReturnType(), Finder, Builder);
   3534 }
   3535 
   3536 /// \brief Matches extern "C" function or variable declarations.
   3537 ///
   3538 /// Given:
   3539 /// \code
   3540 ///   extern "C" void f() {}
   3541 ///   extern "C" { void g() {} }
   3542 ///   void h() {}
   3543 ///   extern "C" int x = 1;
   3544 ///   extern "C" int y = 2;
   3545 ///   int z = 3;
   3546 /// \endcode
   3547 /// functionDecl(isExternC())
   3548 ///   matches the declaration of f and g, but not the declaration of h.
   3549 /// varDecl(isExternC())
   3550 ///   matches the declaration of x and y, but not the declaration of z.
   3551 AST_POLYMORPHIC_MATCHER(isExternC, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
   3552                                                                    VarDecl)) {
   3553   return Node.isExternC();
   3554 }
   3555 
   3556 /// \brief Matches variable/function declarations that have "static" storage
   3557 /// class specifier ("static" keyword) written in the source.
   3558 ///
   3559 /// Given:
   3560 /// \code
   3561 ///   static void f() {}
   3562 ///   static int i = 0;
   3563 ///   extern int j;
   3564 ///   int k;
   3565 /// \endcode
   3566 /// functionDecl(isStaticStorageClass())
   3567 ///   matches the function declaration f.
   3568 /// varDecl(isStaticStorageClass())
   3569 ///   matches the variable declaration i.
   3570 AST_POLYMORPHIC_MATCHER(isStaticStorageClass,
   3571                         AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
   3572                                                         VarDecl)) {
   3573   return Node.getStorageClass() == SC_Static;
   3574 }
   3575 
   3576 /// \brief Matches deleted function declarations.
   3577 ///
   3578 /// Given:
   3579 /// \code
   3580 ///   void Func();
   3581 ///   void DeletedFunc() = delete;
   3582 /// \endcode
   3583 /// functionDecl(isDeleted())
   3584 ///   matches the declaration of DeletedFunc, but not Func.
   3585 AST_MATCHER(FunctionDecl, isDeleted) {
   3586   return Node.isDeleted();
   3587 }
   3588 
   3589 /// \brief Matches defaulted function declarations.
   3590 ///
   3591 /// Given:
   3592 /// \code
   3593 ///   class A { ~A(); };
   3594 ///   class B { ~B() = default; };
   3595 /// \endcode
   3596 /// functionDecl(isDefaulted())
   3597 ///   matches the declaration of ~B, but not ~A.
   3598 AST_MATCHER(FunctionDecl, isDefaulted) {
   3599   return Node.isDefaulted();
   3600 }
   3601 
   3602 /// \brief Matches functions that have a dynamic exception specification.
   3603 ///
   3604 /// Given:
   3605 /// \code
   3606 ///   void f();
   3607 ///   void g() noexcept;
   3608 ///   void h() noexcept(true);
   3609 ///   void i() noexcept(false);
   3610 ///   void j() throw();
   3611 ///   void k() throw(int);
   3612 ///   void l() throw(...);
   3613 /// \endcode
   3614 /// functionDecl(hasDynamicExceptionSpec()) and
   3615 ///   functionProtoType(hasDynamicExceptionSpec())
   3616 ///   match the declarations of j, k, and l, but not f, g, h, or i.
   3617 AST_POLYMORPHIC_MATCHER(hasDynamicExceptionSpec,
   3618                         AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
   3619                                                         FunctionProtoType)) {
   3620   if (const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node))
   3621     return FnTy->hasDynamicExceptionSpec();
   3622   return false;
   3623 }
   3624 
   3625 /// \brief Matches functions that have a non-throwing exception specification.
   3626 ///
   3627 /// Given:
   3628 /// \code
   3629 ///   void f();
   3630 ///   void g() noexcept;
   3631 ///   void h() throw();
   3632 ///   void i() throw(int);
   3633 ///   void j() noexcept(false);
   3634 /// \endcode
   3635 /// functionDecl(isNoThrow()) and functionProtoType(isNoThrow())
   3636 ///   match the declarations of g, and h, but not f, i or j.
   3637 AST_POLYMORPHIC_MATCHER(isNoThrow,
   3638                         AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
   3639                                                         FunctionProtoType)) {
   3640   const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node);
   3641 
   3642   // If the function does not have a prototype, then it is assumed to be a
   3643   // throwing function (as it would if the function did not have any exception
   3644   // specification).
   3645   if (!FnTy)
   3646     return false;
   3647 
   3648   // Assume the best for any unresolved exception specification.
   3649   if (isUnresolvedExceptionSpec(FnTy->getExceptionSpecType()))
   3650     return true;
   3651 
   3652   return FnTy->isNothrow(Finder->getASTContext());
   3653 }
   3654 
   3655 /// \brief Matches constexpr variable and function declarations.
   3656 ///
   3657 /// Given:
   3658 /// \code
   3659 ///   constexpr int foo = 42;
   3660 ///   constexpr int bar();
   3661 /// \endcode
   3662 /// varDecl(isConstexpr())
   3663 ///   matches the declaration of foo.
   3664 /// functionDecl(isConstexpr())
   3665 ///   matches the declaration of bar.
   3666 AST_POLYMORPHIC_MATCHER(isConstexpr,
   3667                         AST_POLYMORPHIC_SUPPORTED_TYPES(VarDecl,
   3668                                                         FunctionDecl)) {
   3669   return Node.isConstexpr();
   3670 }
   3671 
   3672 /// \brief Matches the condition expression of an if statement, for loop,
   3673 /// switch statement or conditional operator.
   3674 ///
   3675 /// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
   3676 /// \code
   3677 ///   if (true) {}
   3678 /// \endcode
   3679 AST_POLYMORPHIC_MATCHER_P(
   3680     hasCondition,
   3681     AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, ForStmt, WhileStmt, DoStmt,
   3682                                     SwitchStmt, AbstractConditionalOperator),
   3683     internal::Matcher<Expr>, InnerMatcher) {
   3684   const Expr *const Condition = Node.getCond();
   3685   return (Condition != nullptr &&
   3686           InnerMatcher.matches(*Condition, Finder, Builder));
   3687 }
   3688 
   3689 /// \brief Matches the then-statement of an if statement.
   3690 ///
   3691 /// Examples matches the if statement
   3692 ///   (matcher = ifStmt(hasThen(cxxBoolLiteral(equals(true)))))
   3693 /// \code
   3694 ///   if (false) true; else false;
   3695 /// \endcode
   3696 AST_MATCHER_P(IfStmt, hasThen, internal::Matcher<Stmt>, InnerMatcher) {
   3697   const Stmt *const Then = Node.getThen();
   3698   return (Then != nullptr && InnerMatcher.matches(*Then, Finder, Builder));
   3699 }
   3700 
   3701 /// \brief Matches the else-statement of an if statement.
   3702 ///
   3703 /// Examples matches the if statement
   3704 ///   (matcher = ifStmt(hasElse(cxxBoolLiteral(equals(true)))))
   3705 /// \code
   3706 ///   if (false) false; else true;
   3707 /// \endcode
   3708 AST_MATCHER_P(IfStmt, hasElse, internal::Matcher<Stmt>, InnerMatcher) {
   3709   const Stmt *const Else = Node.getElse();
   3710   return (Else != nullptr && InnerMatcher.matches(*Else, Finder, Builder));
   3711 }
   3712 
   3713 /// \brief Matches if a node equals a previously bound node.
   3714 ///
   3715 /// Matches a node if it equals the node previously bound to \p ID.
   3716 ///
   3717 /// Given
   3718 /// \code
   3719 ///   class X { int a; int b; };
   3720 /// \endcode
   3721 /// cxxRecordDecl(
   3722 ///     has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
   3723 ///     has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))
   3724 ///   matches the class \c X, as \c a and \c b have the same type.
   3725 ///
   3726 /// Note that when multiple matches are involved via \c forEach* matchers,
   3727 /// \c equalsBoundNodes acts as a filter.
   3728 /// For example:
   3729 /// compoundStmt(
   3730 ///     forEachDescendant(varDecl().bind("d")),
   3731 ///     forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d"))))))
   3732 /// will trigger a match for each combination of variable declaration
   3733 /// and reference to that variable declaration within a compound statement.
   3734 AST_POLYMORPHIC_MATCHER_P(equalsBoundNode,
   3735                           AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl, Type,
   3736                                                           QualType),
   3737                           std::string, ID) {
   3738   // FIXME: Figure out whether it makes sense to allow this
   3739   // on any other node types.
   3740   // For *Loc it probably does not make sense, as those seem
   3741   // unique. For NestedNameSepcifier it might make sense, as
   3742   // those also have pointer identity, but I'm not sure whether
   3743   // they're ever reused.
   3744   internal::NotEqualsBoundNodePredicate Predicate;
   3745   Predicate.ID = ID;
   3746   Predicate.Node = ast_type_traits::DynTypedNode::create(Node);
   3747   return Builder->removeBindings(Predicate);
   3748 }
   3749 
   3750 /// \brief Matches the condition variable statement in an if statement.
   3751 ///
   3752 /// Given
   3753 /// \code
   3754 ///   if (A* a = GetAPointer()) {}
   3755 /// \endcode
   3756 /// hasConditionVariableStatement(...)
   3757 ///   matches 'A* a = GetAPointer()'.
   3758 AST_MATCHER_P(IfStmt, hasConditionVariableStatement,
   3759               internal::Matcher<DeclStmt>, InnerMatcher) {
   3760   const DeclStmt* const DeclarationStatement =
   3761     Node.getConditionVariableDeclStmt();
   3762   return DeclarationStatement != nullptr &&
   3763          InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
   3764 }
   3765 
   3766 /// \brief Matches the index expression of an array subscript expression.
   3767 ///
   3768 /// Given
   3769 /// \code
   3770 ///   int i[5];
   3771 ///   void f() { i[1] = 42; }
   3772 /// \endcode
   3773 /// arraySubscriptExpression(hasIndex(integerLiteral()))
   3774 ///   matches \c i[1] with the \c integerLiteral() matching \c 1
   3775 AST_MATCHER_P(ArraySubscriptExpr, hasIndex,
   3776               internal::Matcher<Expr>, InnerMatcher) {
   3777   if (const Expr* Expression = Node.getIdx())
   3778     return InnerMatcher.matches(*Expression, Finder, Builder);
   3779   return false;
   3780 }
   3781 
   3782 /// \brief Matches the base expression of an array subscript expression.
   3783 ///
   3784 /// Given
   3785 /// \code
   3786 ///   int i[5];
   3787 ///   void f() { i[1] = 42; }
   3788 /// \endcode
   3789 /// arraySubscriptExpression(hasBase(implicitCastExpr(
   3790 ///     hasSourceExpression(declRefExpr()))))
   3791 ///   matches \c i[1] with the \c declRefExpr() matching \c i
   3792 AST_MATCHER_P(ArraySubscriptExpr, hasBase,
   3793               internal::Matcher<Expr>, InnerMatcher) {
   3794   if (const Expr* Expression = Node.getBase())
   3795     return InnerMatcher.matches(*Expression, Finder, Builder);
   3796   return false;
   3797 }
   3798 
   3799 /// \brief Matches a 'for', 'while', 'do while' statement or a function
   3800 /// definition that has a given body.
   3801 ///
   3802 /// Given
   3803 /// \code
   3804 ///   for (;;) {}
   3805 /// \endcode
   3806 /// hasBody(compoundStmt())
   3807 ///   matches 'for (;;) {}'
   3808 /// with compoundStmt()
   3809 ///   matching '{}'
   3810 AST_POLYMORPHIC_MATCHER_P(hasBody,
   3811                           AST_POLYMORPHIC_SUPPORTED_TYPES(DoStmt, ForStmt,
   3812                                                           WhileStmt,
   3813                                                           CXXForRangeStmt,
   3814                                                           FunctionDecl),
   3815                           internal::Matcher<Stmt>, InnerMatcher) {
   3816   const Stmt *const Statement = internal::GetBodyMatcher<NodeType>::get(Node);
   3817   return (Statement != nullptr &&
   3818           InnerMatcher.matches(*Statement, Finder, Builder));
   3819 }
   3820 
   3821 /// \brief Matches compound statements where at least one substatement matches
   3822 /// a given matcher. Also matches StmtExprs that have CompoundStmt as children.
   3823 ///
   3824 /// Given
   3825 /// \code
   3826 ///   { {}; 1+2; }
   3827 /// \endcode
   3828 /// hasAnySubstatement(compoundStmt())
   3829 ///   matches '{ {}; 1+2; }'
   3830 /// with compoundStmt()
   3831 ///   matching '{}'
   3832 AST_POLYMORPHIC_MATCHER_P(hasAnySubstatement,
   3833                           AST_POLYMORPHIC_SUPPORTED_TYPES(CompoundStmt,
   3834                                                           StmtExpr),
   3835                           internal::Matcher<Stmt>, InnerMatcher) {
   3836   const CompoundStmt *CS = CompoundStmtMatcher<NodeType>::get(Node);
   3837   return CS && matchesFirstInPointerRange(InnerMatcher, CS->body_begin(),
   3838                                           CS->body_end(), Finder, Builder);
   3839 }
   3840 
   3841 /// \brief Checks that a compound statement contains a specific number of
   3842 /// child statements.
   3843 ///
   3844 /// Example: Given
   3845 /// \code
   3846 ///   { for (;;) {} }
   3847 /// \endcode
   3848 /// compoundStmt(statementCountIs(0)))
   3849 ///   matches '{}'
   3850 ///   but does not match the outer compound statement.
   3851 AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) {
   3852   return Node.size() == N;
   3853 }
   3854 
   3855 /// \brief Matches literals that are equal to the given value of type ValueT.
   3856 ///
   3857 /// Given
   3858 /// \code
   3859 ///   f('\0', false, 3.14, 42);
   3860 /// \endcode
   3861 /// characterLiteral(equals(0))
   3862 ///   matches '\0'
   3863 /// cxxBoolLiteral(equals(false)) and cxxBoolLiteral(equals(0))
   3864 ///   match false
   3865 /// floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2))
   3866 ///   match 3.14
   3867 /// integerLiteral(equals(42))
   3868 ///   matches 42
   3869 ///
   3870 /// Note that you cannot directly match a negative numeric literal because the
   3871 /// minus sign is not part of the literal: It is a unary operator whose operand
   3872 /// is the positive numeric literal. Instead, you must use a unaryOperator()
   3873 /// matcher to match the minus sign:
   3874 ///
   3875 /// unaryOperator(hasOperatorName("-"),
   3876 ///               hasUnaryOperand(integerLiteral(equals(13))))
   3877 ///
   3878 /// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteralExpr>,
   3879 ///            Matcher<FloatingLiteral>, Matcher<IntegerLiteral>
   3880 template <typename ValueT>
   3881 internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT>
   3882 equals(const ValueT &Value) {
   3883   return internal::PolymorphicMatcherWithParam1<
   3884     internal::ValueEqualsMatcher,
   3885     ValueT>(Value);
   3886 }
   3887 
   3888 AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
   3889                           AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral,
   3890                                                           CXXBoolLiteralExpr,
   3891                                                           IntegerLiteral),
   3892                           bool, Value, 0) {
   3893   return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
   3894     .matchesNode(Node);
   3895 }
   3896 
   3897 AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
   3898                           AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral,
   3899                                                           CXXBoolLiteralExpr,
   3900                                                           IntegerLiteral),
   3901                           unsigned, Value, 1) {
   3902   return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
   3903     .matchesNode(Node);
   3904 }
   3905 
   3906 AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
   3907                           AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral,
   3908                                                           CXXBoolLiteralExpr,
   3909                                                           FloatingLiteral,
   3910                                                           IntegerLiteral),
   3911                           double, Value, 2) {
   3912   return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
   3913     .matchesNode(Node);
   3914 }
   3915 
   3916 /// \brief Matches the operator Name of operator expressions (binary or
   3917 /// unary).
   3918 ///
   3919 /// Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
   3920 /// \code
   3921 ///   !(a || b)
   3922 /// \endcode
   3923 AST_POLYMORPHIC_MATCHER_P(hasOperatorName,
   3924                           AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator,
   3925                                                           UnaryOperator),
   3926                           std::string, Name) {
   3927   return Name == Node.getOpcodeStr(Node.getOpcode());
   3928 }
   3929 
   3930 /// \brief Matches the left hand side of binary operator expressions.
   3931 ///
   3932 /// Example matches a (matcher = binaryOperator(hasLHS()))
   3933 /// \code
   3934 ///   a || b
   3935 /// \endcode
   3936 AST_POLYMORPHIC_MATCHER_P(hasLHS,
   3937                           AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator,
   3938                                                           ArraySubscriptExpr),
   3939                           internal::Matcher<Expr>, InnerMatcher) {
   3940   const Expr *LeftHandSide = Node.getLHS();
   3941   return (LeftHandSide != nullptr &&
   3942           InnerMatcher.matches(*LeftHandSide, Finder, Builder));
   3943 }
   3944 
   3945 /// \brief Matches the right hand side of binary operator expressions.
   3946 ///
   3947 /// Example matches b (matcher = binaryOperator(hasRHS()))
   3948 /// \code
   3949 ///   a || b
   3950 /// \endcode
   3951 AST_POLYMORPHIC_MATCHER_P(hasRHS,
   3952                           AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator,
   3953                                                           ArraySubscriptExpr),
   3954                           internal::Matcher<Expr>, InnerMatcher) {
   3955   const Expr *RightHandSide = Node.getRHS();
   3956   return (RightHandSide != nullptr &&
   3957           InnerMatcher.matches(*RightHandSide, Finder, Builder));
   3958 }
   3959 
   3960 /// \brief Matches if either the left hand side or the right hand side of a
   3961 /// binary operator matches.
   3962 inline internal::Matcher<BinaryOperator> hasEitherOperand(
   3963     const internal::Matcher<Expr> &InnerMatcher) {
   3964   return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher));
   3965 }
   3966 
   3967 /// \brief Matches if the operand of a unary operator matches.
   3968 ///
   3969 /// Example matches true (matcher = hasUnaryOperand(
   3970 ///                                   cxxBoolLiteral(equals(true))))
   3971 /// \code
   3972 ///   !true
   3973 /// \endcode
   3974 AST_MATCHER_P(UnaryOperator, hasUnaryOperand,
   3975               internal::Matcher<Expr>, InnerMatcher) {
   3976   const Expr * const Operand = Node.getSubExpr();
   3977   return (Operand != nullptr &&
   3978           InnerMatcher.matches(*Operand, Finder, Builder));
   3979 }
   3980 
   3981 /// \brief Matches if the cast's source expression
   3982 /// or opaque value's source expression matches the given matcher.
   3983 ///
   3984 /// Example 1: matches "a string"
   3985 /// (matcher = castExpr(hasSourceExpression(cxxConstructExpr())))
   3986 /// \code
   3987 /// class URL { URL(string); };
   3988 /// URL url = "a string";
   3989 /// \endcode
   3990 ///
   3991 /// Example 2: matches 'b' (matcher =
   3992 /// opaqueValueExpr(hasSourceExpression(implicitCastExpr(declRefExpr())))
   3993 /// \code
   3994 /// int a = b ?: 1;
   3995 /// \endcode
   3996 
   3997 AST_POLYMORPHIC_MATCHER_P(hasSourceExpression,
   3998                           AST_POLYMORPHIC_SUPPORTED_TYPES(CastExpr,
   3999                                                           OpaqueValueExpr),
   4000                           internal::Matcher<Expr>, InnerMatcher) {
   4001   const Expr *const SubExpression =
   4002       internal::GetSourceExpressionMatcher<NodeType>::get(Node);
   4003   return (SubExpression != nullptr &&
   4004           InnerMatcher.matches(*SubExpression, Finder, Builder));
   4005 }
   4006 
   4007 /// \brief Matches casts that has a given cast kind.
   4008 ///
   4009 /// Example: matches the implicit cast around \c 0
   4010 /// (matcher = castExpr(hasCastKind(CK_NullToPointer)))
   4011 /// \code
   4012 ///   int *p = 0;
   4013 /// \endcode
   4014 AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) {
   4015   return Node.getCastKind() == Kind;
   4016 }
   4017 
   4018 /// \brief Matches casts whose destination type matches a given matcher.
   4019 ///
   4020 /// (Note: Clang's AST refers to other conversions as "casts" too, and calls
   4021 /// actual casts "explicit" casts.)
   4022 AST_MATCHER_P(ExplicitCastExpr, hasDestinationType,
   4023               internal::Matcher<QualType>, InnerMatcher) {
   4024   const QualType NodeType = Node.getTypeAsWritten();
   4025   return InnerMatcher.matches(NodeType, Finder, Builder);
   4026 }
   4027 
   4028 /// \brief Matches implicit casts whose destination type matches a given
   4029 /// matcher.
   4030 ///
   4031 /// FIXME: Unit test this matcher
   4032 AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType,
   4033               internal::Matcher<QualType>, InnerMatcher) {
   4034   return InnerMatcher.matches(Node.getType(), Finder, Builder);
   4035 }
   4036 
   4037 /// \brief Matches RecordDecl object that are spelled with "struct."
   4038 ///
   4039 /// Example matches S, but not C or U.
   4040 /// \code
   4041 ///   struct S {};
   4042 ///   class C {};
   4043 ///   union U {};
   4044 /// \endcode
   4045 AST_MATCHER(RecordDecl, isStruct) {
   4046   return Node.isStruct();
   4047 }
   4048 
   4049 /// \brief Matches RecordDecl object that are spelled with "union."
   4050 ///
   4051 /// Example matches U, but not C or S.
   4052 /// \code
   4053 ///   struct S {};
   4054 ///   class C {};
   4055 ///   union U {};
   4056 /// \endcode
   4057 AST_MATCHER(RecordDecl, isUnion) {
   4058   return Node.isUnion();
   4059 }
   4060 
   4061 /// \brief Matches RecordDecl object that are spelled with "class."
   4062 ///
   4063 /// Example matches C, but not S or U.
   4064 /// \code
   4065 ///   struct S {};
   4066 ///   class C {};
   4067 ///   union U {};
   4068 /// \endcode
   4069 AST_MATCHER(RecordDecl, isClass) {
   4070   return Node.isClass();
   4071 }
   4072 
   4073 /// \brief Matches the true branch expression of a conditional operator.
   4074 ///
   4075 /// Example 1 (conditional ternary operator): matches a
   4076 /// \code
   4077 ///   condition ? a : b
   4078 /// \endcode
   4079 ///
   4080 /// Example 2 (conditional binary operator): matches opaqueValueExpr(condition)
   4081 /// \code
   4082 ///   condition ?: b
   4083 /// \endcode
   4084 AST_MATCHER_P(AbstractConditionalOperator, hasTrueExpression,
   4085               internal::Matcher<Expr>, InnerMatcher) {
   4086   const Expr *Expression = Node.getTrueExpr();
   4087   return (Expression != nullptr &&
   4088           InnerMatcher.matches(*Expression, Finder, Builder));
   4089 }
   4090 
   4091 /// \brief Matches the false branch expression of a conditional operator
   4092 /// (binary or ternary).
   4093 ///
   4094 /// Example matches b
   4095 /// \code
   4096 ///   condition ? a : b
   4097 ///   condition ?: b
   4098 /// \endcode
   4099 AST_MATCHER_P(AbstractConditionalOperator, hasFalseExpression,
   4100               internal::Matcher<Expr>, InnerMatcher) {
   4101   const Expr *Expression = Node.getFalseExpr();
   4102   return (Expression != nullptr &&
   4103           InnerMatcher.matches(*Expression, Finder, Builder));
   4104 }
   4105 
   4106 /// \brief Matches if a declaration has a body attached.
   4107 ///
   4108 /// Example matches A, va, fa
   4109 /// \code
   4110 ///   class A {};
   4111 ///   class B;  // Doesn't match, as it has no body.
   4112 ///   int va;
   4113 ///   extern int vb;  // Doesn't match, as it doesn't define the variable.
   4114 ///   void fa() {}
   4115 ///   void fb();  // Doesn't match, as it has no body.
   4116 /// \endcode
   4117 ///
   4118 /// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl>
   4119 AST_POLYMORPHIC_MATCHER(isDefinition,
   4120                         AST_POLYMORPHIC_SUPPORTED_TYPES(TagDecl, VarDecl,
   4121                                                         FunctionDecl)) {
   4122   return Node.isThisDeclarationADefinition();
   4123 }
   4124 
   4125 /// \brief Matches if a function declaration is variadic.
   4126 ///
   4127 /// Example matches f, but not g or h. The function i will not match, even when
   4128 /// compiled in C mode.
   4129 /// \code
   4130 ///   void f(...);
   4131 ///   void g(int);
   4132 ///   template <typename... Ts> void h(Ts...);
   4133 ///   void i();
   4134 /// \endcode
   4135 AST_MATCHER(FunctionDecl, isVariadic) {
   4136   return Node.isVariadic();
   4137 }
   4138 
   4139 /// \brief Matches the class declaration that the given method declaration
   4140 /// belongs to.
   4141 ///
   4142 /// FIXME: Generalize this for other kinds of declarations.
   4143 /// FIXME: What other kind of declarations would we need to generalize
   4144 /// this to?
   4145 ///
   4146 /// Example matches A() in the last line
   4147 ///     (matcher = cxxConstructExpr(hasDeclaration(cxxMethodDecl(
   4148 ///         ofClass(hasName("A"))))))
   4149 /// \code
   4150 ///   class A {
   4151 ///    public:
   4152 ///     A();
   4153 ///   };
   4154 ///   A a = A();
   4155 /// \endcode
   4156 AST_MATCHER_P(CXXMethodDecl, ofClass,
   4157               internal::Matcher<CXXRecordDecl>, InnerMatcher) {
   4158   const CXXRecordDecl *Parent = Node.getParent();
   4159   return (Parent != nullptr &&
   4160           InnerMatcher.matches(*Parent, Finder, Builder));
   4161 }
   4162 
   4163 /// \brief Matches each method overriden by the given method. This matcher may
   4164 /// produce multiple matches.
   4165 ///
   4166 /// Given
   4167 /// \code
   4168 ///   class A { virtual void f(); };
   4169 ///   class B : public A { void f(); };
   4170 ///   class C : public B { void f(); };
   4171 /// \endcode
   4172 /// cxxMethodDecl(ofClass(hasName("C")),
   4173 ///               forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
   4174 ///   matches once, with "b" binding "A::f" and "d" binding "C::f" (Note
   4175 ///   that B::f is not overridden by C::f).
   4176 ///
   4177 /// The check can produce multiple matches in case of multiple inheritance, e.g.
   4178 /// \code
   4179 ///   class A1 { virtual void f(); };
   4180 ///   class A2 { virtual void f(); };
   4181 ///   class C : public A1, public A2 { void f(); };
   4182 /// \endcode
   4183 /// cxxMethodDecl(ofClass(hasName("C")),
   4184 ///               forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
   4185 ///   matches twice, once with "b" binding "A1::f" and "d" binding "C::f", and
   4186 ///   once with "b" binding "A2::f" and "d" binding "C::f".
   4187 AST_MATCHER_P(CXXMethodDecl, forEachOverridden,
   4188               internal::Matcher<CXXMethodDecl>, InnerMatcher) {
   4189   BoundNodesTreeBuilder Result;
   4190   bool Matched = false;
   4191   for (const auto *Overridden : Node.overridden_methods()) {
   4192     BoundNodesTreeBuilder OverriddenBuilder(*Builder);
   4193     const bool OverriddenMatched =
   4194         InnerMatcher.matches(*Overridden, Finder, &OverriddenBuilder);
   4195     if (OverriddenMatched) {
   4196       Matched = true;
   4197       Result.addMatch(OverriddenBuilder);
   4198     }
   4199   }
   4200   *Builder = std::move(Result);
   4201   return Matched;
   4202 }
   4203 
   4204 /// \brief Matches if the given method declaration is virtual.
   4205 ///
   4206 /// Given
   4207 /// \code
   4208 ///   class A {
   4209 ///    public:
   4210 ///     virtual void x();
   4211 ///   };
   4212 /// \endcode
   4213 ///   matches A::x
   4214 AST_MATCHER(CXXMethodDecl, isVirtual) {
   4215   return Node.isVirtual();
   4216 }
   4217 
   4218 /// \brief Matches if the given method declaration has an explicit "virtual".
   4219 ///
   4220 /// Given
   4221 /// \code
   4222 ///   class A {
   4223 ///    public:
   4224 ///     virtual void x();
   4225 ///   };
   4226 ///   class B : public A {
   4227 ///    public:
   4228 ///     void x();
   4229 ///   };
   4230 /// \endcode
   4231 ///   matches A::x but not B::x
   4232 AST_MATCHER(CXXMethodDecl, isVirtualAsWritten) {
   4233   return Node.isVirtualAsWritten();
   4234 }
   4235 
   4236 /// \brief Matches if the given method or class declaration is final.
   4237 ///
   4238 /// Given:
   4239 /// \code
   4240 ///   class A final {};
   4241 ///
   4242 ///   struct B {
   4243 ///     virtual void f();
   4244 ///   };
   4245 ///
   4246 ///   struct C : B {
   4247 ///     void f() final;
   4248 ///   };
   4249 /// \endcode
   4250 /// matches A and C::f, but not B, C, or B::f
   4251 AST_POLYMORPHIC_MATCHER(isFinal,
   4252                         AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl,
   4253                                                         CXXMethodDecl)) {
   4254   return Node.template hasAttr<FinalAttr>();
   4255 }
   4256 
   4257 /// \brief Matches if the given method declaration is pure.
   4258 ///
   4259 /// Given
   4260 /// \code
   4261 ///   class A {
   4262 ///    public:
   4263 ///     virtual void x() = 0;
   4264 ///   };
   4265 /// \endcode
   4266 ///   matches A::x
   4267 AST_MATCHER(CXXMethodDecl, isPure) {
   4268   return Node.isPure();
   4269 }
   4270 
   4271 /// \brief Matches if the given method declaration is const.
   4272 ///
   4273 /// Given
   4274 /// \code
   4275 /// struct A {
   4276 ///   void foo() const;
   4277 ///   void bar();
   4278 /// };
   4279 /// \endcode
   4280 ///
   4281 /// cxxMethodDecl(isConst()) matches A::foo() but not A::bar()
   4282 AST_MATCHER(CXXMethodDecl, isConst) {
   4283   return Node.isConst();
   4284 }
   4285 
   4286 /// \brief Matches if the given method declaration declares a copy assignment
   4287 /// operator.
   4288 ///
   4289 /// Given
   4290 /// \code
   4291 /// struct A {
   4292 ///   A &operator=(const A &);
   4293 ///   A &operator=(A &&);
   4294 /// };
   4295 /// \endcode
   4296 ///
   4297 /// cxxMethodDecl(isCopyAssignmentOperator()) matches the first method but not
   4298 /// the second one.
   4299 AST_MATCHER(CXXMethodDecl, isCopyAssignmentOperator) {
   4300   return Node.isCopyAssignmentOperator();
   4301 }
   4302 
   4303 /// \brief Matches if the given method declaration declares a move assignment
   4304 /// operator.
   4305 ///
   4306 /// Given
   4307 /// \code
   4308 /// struct A {
   4309 ///   A &operator=(const A &);
   4310 ///   A &operator=(A &&);
   4311 /// };
   4312 /// \endcode
   4313 ///
   4314 /// cxxMethodDecl(isMoveAssignmentOperator()) matches the second method but not
   4315 /// the first one.
   4316 AST_MATCHER(CXXMethodDecl, isMoveAssignmentOperator) {
   4317   return Node.isMoveAssignmentOperator();
   4318 }
   4319 
   4320 /// \brief Matches if the given method declaration overrides another method.
   4321 ///
   4322 /// Given
   4323 /// \code
   4324 ///   class A {
   4325 ///    public:
   4326 ///     virtual void x();
   4327 ///   };
   4328 ///   class B : public A {
   4329 ///    public:
   4330 ///     virtual void x();
   4331 ///   };
   4332 /// \endcode
   4333 ///   matches B::x
   4334 AST_MATCHER(CXXMethodDecl, isOverride) {
   4335   return Node.size_overridden_methods() > 0 || Node.hasAttr<OverrideAttr>();
   4336 }
   4337 
   4338 /// \brief Matches method declarations that are user-provided.
   4339 ///
   4340 /// Given
   4341 /// \code
   4342 ///   struct S {
   4343 ///     S(); // #1
   4344 ///     S(const S &) = default; // #2
   4345 ///     S(S &&) = delete; // #3
   4346 ///   };
   4347 /// \endcode
   4348 /// cxxConstructorDecl(isUserProvided()) will match #1, but not #2 or #3.
   4349 AST_MATCHER(CXXMethodDecl, isUserProvided) {
   4350   return Node.isUserProvided();
   4351 }
   4352 
   4353 /// \brief Matches member expressions that are called with '->' as opposed
   4354 /// to '.'.
   4355 ///
   4356 /// Member calls on the implicit this pointer match as called with '->'.
   4357 ///
   4358 /// Given
   4359 /// \code
   4360 ///   class Y {
   4361 ///     void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
   4362 ///     int a;
   4363 ///     static int b;
   4364 ///   };
   4365 /// \endcode
   4366 /// memberExpr(isArrow())
   4367 ///   matches this->x, x, y.x, a, this->b
   4368 AST_MATCHER(MemberExpr, isArrow) {
   4369   return Node.isArrow();
   4370 }
   4371 
   4372 /// \brief Matches QualType nodes that are of integer type.
   4373 ///
   4374 /// Given
   4375 /// \code
   4376 ///   void a(int);
   4377 ///   void b(long);
   4378 ///   void c(double);
   4379 /// \endcode
   4380 /// functionDecl(hasAnyParameter(hasType(isInteger())))
   4381 /// matches "a(int)", "b(long)", but not "c(double)".
   4382 AST_MATCHER(QualType, isInteger) {
   4383     return Node->isIntegerType();
   4384 }
   4385 
   4386 /// \brief Matches QualType nodes that are of unsigned integer type.
   4387 ///
   4388 /// Given
   4389 /// \code
   4390 ///   void a(int);
   4391 ///   void b(unsigned long);
   4392 ///   void c(double);
   4393 /// \endcode
   4394 /// functionDecl(hasAnyParameter(hasType(isUnsignedInteger())))
   4395 /// matches "b(unsigned long)", but not "a(int)" and "c(double)".
   4396 AST_MATCHER(QualType, isUnsignedInteger) {
   4397     return Node->isUnsignedIntegerType();
   4398 }
   4399 
   4400 /// \brief Matches QualType nodes that are of signed integer type.
   4401 ///
   4402 /// Given
   4403 /// \code
   4404 ///   void a(int);
   4405 ///   void b(unsigned long);
   4406 ///   void c(double);
   4407 /// \endcode
   4408 /// functionDecl(hasAnyParameter(hasType(isSignedInteger())))
   4409 /// matches "a(int)", but not "b(unsigned long)" and "c(double)".
   4410 AST_MATCHER(QualType, isSignedInteger) {
   4411     return Node->isSignedIntegerType();
   4412 }
   4413 
   4414 /// \brief Matches QualType nodes that are of character type.
   4415 ///
   4416 /// Given
   4417 /// \code
   4418 ///   void a(char);
   4419 ///   void b(wchar_t);
   4420 ///   void c(double);
   4421 /// \endcode
   4422 /// functionDecl(hasAnyParameter(hasType(isAnyCharacter())))
   4423 /// matches "a(char)", "b(wchar_t)", but not "c(double)".
   4424 AST_MATCHER(QualType, isAnyCharacter) {
   4425     return Node->isAnyCharacterType();
   4426 }
   4427 
   4428 /// \brief Matches QualType nodes that are of any pointer type; this includes
   4429 /// the Objective-C object pointer type, which is different despite being
   4430 /// syntactically similar.
   4431 ///
   4432 /// Given
   4433 /// \code
   4434 ///   int *i = nullptr;
   4435 ///
   4436 ///   @interface Foo
   4437 ///   @end
   4438 ///   Foo *f;
   4439 ///
   4440 ///   int j;
   4441 /// \endcode
   4442 /// varDecl(hasType(isAnyPointer()))
   4443 ///   matches "int *i" and "Foo *f", but not "int j".
   4444 AST_MATCHER(QualType, isAnyPointer) {
   4445   return Node->isAnyPointerType();
   4446 }
   4447 
   4448 /// \brief Matches QualType nodes that are const-qualified, i.e., that
   4449 /// include "top-level" const.
   4450 ///
   4451 /// Given
   4452 /// \code
   4453 ///   void a(int);
   4454 ///   void b(int const);
   4455 ///   void c(const int);
   4456 ///   void d(const int*);
   4457 ///   void e(int const) {};
   4458 /// \endcode
   4459 /// functionDecl(hasAnyParameter(hasType(isConstQualified())))
   4460 ///   matches "void b(int const)", "void c(const int)" and
   4461 ///   "void e(int const) {}". It does not match d as there
   4462 ///   is no top-level const on the parameter type "const int *".
   4463 AST_MATCHER(QualType, isConstQualified) {
   4464   return Node.isConstQualified();
   4465 }
   4466 
   4467 /// \brief Matches QualType nodes that are volatile-qualified, i.e., that
   4468 /// include "top-level" volatile.
   4469 ///
   4470 /// Given
   4471 /// \code
   4472 ///   void a(int);
   4473 ///   void b(int volatile);
   4474 ///   void c(volatile int);
   4475 ///   void d(volatile int*);
   4476 ///   void e(int volatile) {};
   4477 /// \endcode
   4478 /// functionDecl(hasAnyParameter(hasType(isVolatileQualified())))
   4479 ///   matches "void b(int volatile)", "void c(volatile int)" and
   4480 ///   "void e(int volatile) {}". It does not match d as there
   4481 ///   is no top-level volatile on the parameter type "volatile int *".
   4482 AST_MATCHER(QualType, isVolatileQualified) {
   4483   return Node.isVolatileQualified();
   4484 }
   4485 
   4486 /// \brief Matches QualType nodes that have local CV-qualifiers attached to
   4487 /// the node, not hidden within a typedef.
   4488 ///
   4489 /// Given
   4490 /// \code
   4491 ///   typedef const int const_int;
   4492 ///   const_int i;
   4493 ///   int *const j;
   4494 ///   int *volatile k;
   4495 ///   int m;
   4496 /// \endcode
   4497 /// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k.
   4498 /// \c i is const-qualified but the qualifier is not local.
   4499 AST_MATCHER(QualType, hasLocalQualifiers) {
   4500   return Node.hasLocalQualifiers();
   4501 }
   4502 
   4503 /// \brief Matches a member expression where the member is matched by a
   4504 /// given matcher.
   4505 ///
   4506 /// Given
   4507 /// \code
   4508 ///   struct { int first, second; } first, second;
   4509 ///   int i(second.first);
   4510 ///   int j(first.second);
   4511 /// \endcode
   4512 /// memberExpr(member(hasName("first")))
   4513 ///   matches second.first
   4514 ///   but not first.second (because the member name there is "second").
   4515 AST_MATCHER_P(MemberExpr, member,
   4516               internal::Matcher<ValueDecl>, InnerMatcher) {
   4517   return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
   4518 }
   4519 
   4520 /// \brief Matches a member expression where the object expression is
   4521 /// matched by a given matcher.
   4522 ///
   4523 /// Given
   4524 /// \code
   4525 ///   struct X { int m; };
   4526 ///   void f(X x) { x.m; m; }
   4527 /// \endcode
   4528 /// memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X")))))))
   4529 ///   matches "x.m" and "m"
   4530 /// with hasObjectExpression(...)
   4531 ///   matching "x" and the implicit object expression of "m" which has type X*.
   4532 AST_MATCHER_P(MemberExpr, hasObjectExpression,
   4533               internal::Matcher<Expr>, InnerMatcher) {
   4534   return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
   4535 }
   4536 
   4537 /// \brief Matches any using shadow declaration.
   4538 ///
   4539 /// Given
   4540 /// \code
   4541 ///   namespace X { void b(); }
   4542 ///   using X::b;
   4543 /// \endcode
   4544 /// usingDecl(hasAnyUsingShadowDecl(hasName("b"))))
   4545 ///   matches \code using X::b \endcode
   4546 AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl,
   4547               internal::Matcher<UsingShadowDecl>, InnerMatcher) {
   4548   return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(),
   4549                                     Node.shadow_end(), Finder, Builder);
   4550 }
   4551 
   4552 /// \brief Matches a using shadow declaration where the target declaration is
   4553 /// matched by the given matcher.
   4554 ///
   4555 /// Given
   4556 /// \code
   4557 ///   namespace X { int a; void b(); }
   4558 ///   using X::a;
   4559 ///   using X::b;
   4560 /// \endcode
   4561 /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl())))
   4562 ///   matches \code using X::b \endcode
   4563 ///   but not \code using X::a \endcode
   4564 AST_MATCHER_P(UsingShadowDecl, hasTargetDecl,
   4565               internal::Matcher<NamedDecl>, InnerMatcher) {
   4566   return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder);
   4567 }
   4568 
   4569 /// \brief Matches template instantiations of function, class, or static
   4570 /// member variable template instantiations.
   4571 ///
   4572 /// Given
   4573 /// \code
   4574 ///   template <typename T> class X {}; class A {}; X<A> x;
   4575 /// \endcode
   4576 /// or
   4577 /// \code
   4578 ///   template <typename T> class X {}; class A {}; template class X<A>;
   4579 /// \endcode
   4580 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
   4581 ///   matches the template instantiation of X<A>.
   4582 ///
   4583 /// But given
   4584 /// \code
   4585 ///   template <typename T>  class X {}; class A {};
   4586 ///   template <> class X<A> {}; X<A> x;
   4587 /// \endcode
   4588 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
   4589 ///   does not match, as X<A> is an explicit template specialization.
   4590 ///
   4591 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
   4592 AST_POLYMORPHIC_MATCHER(isTemplateInstantiation,
   4593                         AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, VarDecl,
   4594                                                         CXXRecordDecl)) {
   4595   return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation ||
   4596           Node.getTemplateSpecializationKind() ==
   4597           TSK_ExplicitInstantiationDefinition);
   4598 }
   4599 
   4600 /// \brief Matches declarations that are template instantiations or are inside
   4601 /// template instantiations.
   4602 ///
   4603 /// Given
   4604 /// \code
   4605 ///   template<typename T> void A(T t) { T i; }
   4606 ///   A(0);
   4607 ///   A(0U);
   4608 /// \endcode
   4609 /// functionDecl(isInstantiated())
   4610 ///   matches 'A(int) {...};' and 'A(unsigned) {...}'.
   4611 AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
   4612   auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
   4613                                     functionDecl(isTemplateInstantiation())));
   4614   return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation)));
   4615 }
   4616 
   4617 /// \brief Matches statements inside of a template instantiation.
   4618 ///
   4619 /// Given
   4620 /// \code
   4621 ///   int j;
   4622 ///   template<typename T> void A(T t) { T i; j += 42;}
   4623 ///   A(0);
   4624 ///   A(0U);
   4625 /// \endcode
   4626 /// declStmt(isInTemplateInstantiation())
   4627 ///   matches 'int i;' and 'unsigned i'.
   4628 /// unless(stmt(isInTemplateInstantiation()))
   4629 ///   will NOT match j += 42; as it's shared between the template definition and
   4630 ///   instantiation.
   4631 AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
   4632   return stmt(
   4633       hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
   4634                              functionDecl(isTemplateInstantiation())))));
   4635 }
   4636 
   4637 /// \brief Matches explicit template specializations of function, class, or
   4638 /// static member variable template instantiations.
   4639 ///
   4640 /// Given
   4641 /// \code
   4642 ///   template<typename T> void A(T t) { }
   4643 ///   template<> void A(int N) { }
   4644 /// \endcode
   4645 /// functionDecl(isExplicitTemplateSpecialization())
   4646 ///   matches the specialization A<int>().
   4647 ///
   4648 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
   4649 AST_POLYMORPHIC_MATCHER(isExplicitTemplateSpecialization,
   4650                         AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, VarDecl,
   4651                                                         CXXRecordDecl)) {
   4652   return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization);
   4653 }
   4654 
   4655 /// \brief Matches \c TypeLocs for which the given inner
   4656 /// QualType-matcher matches.
   4657 AST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher<TypeLoc>, loc,
   4658                                 internal::Matcher<QualType>, InnerMatcher, 0) {
   4659   return internal::BindableMatcher<TypeLoc>(
   4660       new internal::TypeLocTypeMatcher(InnerMatcher));
   4661 }
   4662 
   4663 /// \brief Matches type \c bool.
   4664 ///
   4665 /// Given
   4666 /// \code
   4667 ///  struct S { bool func(); };
   4668 /// \endcode
   4669 /// functionDecl(returns(booleanType()))
   4670 ///   matches "bool func();"
   4671 AST_MATCHER(Type, booleanType) {
   4672   return Node.isBooleanType();
   4673 }
   4674 
   4675 /// \brief Matches type \c void.
   4676 ///
   4677 /// Given
   4678 /// \code
   4679 ///  struct S { void func(); };
   4680 /// \endcode
   4681 /// functionDecl(returns(voidType()))
   4682 ///   matches "void func();"
   4683 AST_MATCHER(Type, voidType) {
   4684   return Node.isVoidType();
   4685 }
   4686 
   4687 /// \brief Matches builtin Types.
   4688 ///
   4689 /// Given
   4690 /// \code
   4691 ///   struct A {};
   4692 ///   A a;
   4693 ///   int b;
   4694 ///   float c;
   4695 ///   bool d;
   4696 /// \endcode
   4697 /// builtinType()
   4698 ///   matches "int b", "float c" and "bool d"
   4699 AST_TYPE_MATCHER(BuiltinType, builtinType);
   4700 
   4701 /// \brief Matches all kinds of arrays.
   4702 ///
   4703 /// Given
   4704 /// \code
   4705 ///   int a[] = { 2, 3 };
   4706 ///   int b[4];
   4707 ///   void f() { int c[a[0]]; }
   4708 /// \endcode
   4709 /// arrayType()
   4710 ///   matches "int a[]", "int b[4]" and "int c[a[0]]";
   4711 AST_TYPE_MATCHER(ArrayType, arrayType);
   4712 
   4713 /// \brief Matches C99 complex types.
   4714 ///
   4715 /// Given
   4716 /// \code
   4717 ///   _Complex float f;
   4718 /// \endcode
   4719 /// complexType()
   4720 ///   matches "_Complex float f"
   4721 AST_TYPE_MATCHER(ComplexType, complexType);
   4722 
   4723 /// \brief Matches any real floating-point type (float, double, long double).
   4724 ///
   4725 /// Given
   4726 /// \code
   4727 ///   int i;
   4728 ///   float f;
   4729 /// \endcode
   4730 /// realFloatingPointType()
   4731 ///   matches "float f" but not "int i"
   4732 AST_MATCHER(Type, realFloatingPointType) {
   4733   return Node.isRealFloatingType();
   4734 }
   4735 
   4736 /// \brief Matches arrays and C99 complex types that have a specific element
   4737 /// type.
   4738 ///
   4739 /// Given
   4740 /// \code
   4741 ///   struct A {};
   4742 ///   A a[7];
   4743 ///   int b[7];
   4744 /// \endcode
   4745 /// arrayType(hasElementType(builtinType()))
   4746 ///   matches "int b[7]"
   4747 ///
   4748 /// Usable as: Matcher<ArrayType>, Matcher<ComplexType>
   4749 AST_TYPELOC_TRAVERSE_MATCHER(hasElementType, getElement,
   4750                              AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
   4751                                                              ComplexType));
   4752 
   4753 /// \brief Matches C arrays with a specified constant size.
   4754 ///
   4755 /// Given
   4756 /// \code
   4757 ///   void() {
   4758 ///     int a[2];
   4759 ///     int b[] = { 2, 3 };
   4760 ///     int c[b[0]];
   4761 ///   }
   4762 /// \endcode
   4763 /// constantArrayType()
   4764 ///   matches "int a[2]"
   4765 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType);
   4766 
   4767 /// \brief Matches nodes that have the specified size.
   4768 ///
   4769 /// Given
   4770 /// \code
   4771 ///   int a[42];
   4772 ///   int b[2 * 21];
   4773 ///   int c[41], d[43];
   4774 ///   char *s = "abcd";
   4775 ///   wchar_t *ws = L"abcd";
   4776 ///   char *w = "a";
   4777 /// \endcode
   4778 /// constantArrayType(hasSize(42))
   4779 ///   matches "int a[42]" and "int b[2 * 21]"
   4780 /// stringLiteral(hasSize(4))
   4781 ///   matches "abcd", L"abcd"
   4782 AST_POLYMORPHIC_MATCHER_P(hasSize,
   4783                           AST_POLYMORPHIC_SUPPORTED_TYPES(ConstantArrayType,
   4784                                                           StringLiteral),
   4785                           unsigned, N) {
   4786   return internal::HasSizeMatcher<NodeType>::hasSize(Node, N);
   4787 }
   4788 
   4789 /// \brief Matches C++ arrays whose size is a value-dependent expression.
   4790 ///
   4791 /// Given
   4792 /// \code
   4793 ///   template<typename T, int Size>
   4794 ///   class array {
   4795 ///     T data[Size];
   4796 ///   };
   4797 /// \endcode
   4798 /// dependentSizedArrayType
   4799 ///   matches "T data[Size]"
   4800 AST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType);
   4801 
   4802 /// \brief Matches C arrays with unspecified size.
   4803 ///
   4804 /// Given
   4805 /// \code
   4806 ///   int a[] = { 2, 3 };
   4807 ///   int b[42];
   4808 ///   void f(int c[]) { int d[a[0]]; };
   4809 /// \endcode
   4810 /// incompleteArrayType()
   4811 ///   matches "int a[]" and "int c[]"
   4812 AST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType);
   4813 
   4814 /// \brief Matches C arrays with a specified size that is not an
   4815 /// integer-constant-expression.
   4816 ///
   4817 /// Given
   4818 /// \code
   4819 ///   void f() {
   4820 ///     int a[] = { 2, 3 }
   4821 ///     int b[42];
   4822 ///     int c[a[0]];
   4823 ///   }
   4824 /// \endcode
   4825 /// variableArrayType()
   4826 ///   matches "int c[a[0]]"
   4827 AST_TYPE_MATCHER(VariableArrayType, variableArrayType);
   4828 
   4829 /// \brief Matches \c VariableArrayType nodes that have a specific size
   4830 /// expression.
   4831 ///
   4832 /// Given
   4833 /// \code
   4834 ///   void f(int b) {
   4835 ///     int a[b];
   4836 ///   }
   4837 /// \endcode
   4838 /// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
   4839 ///   varDecl(hasName("b")))))))
   4840 ///   matches "int a[b]"
   4841 AST_MATCHER_P(VariableArrayType, hasSizeExpr,
   4842               internal::Matcher<Expr>, InnerMatcher) {
   4843   return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder);
   4844 }
   4845 
   4846 /// \brief Matches atomic types.
   4847 ///
   4848 /// Given
   4849 /// \code
   4850 ///   _Atomic(int) i;
   4851 /// \endcode
   4852 /// atomicType()
   4853 ///   matches "_Atomic(int) i"
   4854 AST_TYPE_MATCHER(AtomicType, atomicType);
   4855 
   4856 /// \brief Matches atomic types with a specific value type.
   4857 ///
   4858 /// Given
   4859 /// \code
   4860 ///   _Atomic(int) i;
   4861 ///   _Atomic(float) f;
   4862 /// \endcode
   4863 /// atomicType(hasValueType(isInteger()))
   4864 ///  matches "_Atomic(int) i"
   4865 ///
   4866 /// Usable as: Matcher<AtomicType>
   4867 AST_TYPELOC_TRAVERSE_MATCHER(hasValueType, getValue,
   4868                              AST_POLYMORPHIC_SUPPORTED_TYPES(AtomicType));
   4869 
   4870 /// \brief Matches types nodes representing C++11 auto types.
   4871 ///
   4872 /// Given:
   4873 /// \code
   4874 ///   auto n = 4;
   4875 ///   int v[] = { 2, 3 }
   4876 ///   for (auto i : v) { }
   4877 /// \endcode
   4878 /// autoType()
   4879 ///   matches "auto n" and "auto i"
   4880 AST_TYPE_MATCHER(AutoType, autoType);
   4881 
   4882 /// \brief Matches \c AutoType nodes where the deduced type is a specific type.
   4883 ///
   4884 /// Note: There is no \c TypeLoc for the deduced type and thus no
   4885 /// \c getDeducedLoc() matcher.
   4886 ///
   4887 /// Given
   4888 /// \code
   4889 ///   auto a = 1;
   4890 ///   auto b = 2.0;
   4891 /// \endcode
   4892 /// autoType(hasDeducedType(isInteger()))
   4893 ///   matches "auto a"
   4894 ///
   4895 /// Usable as: Matcher<AutoType>
   4896 AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
   4897                           AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType));
   4898 
   4899 /// \brief Matches \c FunctionType nodes.
   4900 ///
   4901 /// Given
   4902 /// \code
   4903 ///   int (*f)(int);
   4904 ///   void g();
   4905 /// \endcode
   4906 /// functionType()
   4907 ///   matches "int (*f)(int)" and the type of "g".
   4908 AST_TYPE_MATCHER(FunctionType, functionType);
   4909 
   4910 /// \brief Matches \c FunctionProtoType nodes.
   4911 ///
   4912 /// Given
   4913 /// \code
   4914 ///   int (*f)(int);
   4915 ///   void g();
   4916 /// \endcode
   4917 /// functionProtoType()
   4918 ///   matches "int (*f)(int)" and the type of "g" in C++ mode.
   4919 ///   In C mode, "g" is not matched because it does not contain a prototype.
   4920 AST_TYPE_MATCHER(FunctionProtoType, functionProtoType);
   4921 
   4922 /// \brief Matches \c ParenType nodes.
   4923 ///
   4924 /// Given
   4925 /// \code
   4926 ///   int (*ptr_to_array)[4];
   4927 ///   int *array_of_ptrs[4];
   4928 /// \endcode
   4929 ///
   4930 /// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not
   4931 /// \c array_of_ptrs.
   4932 AST_TYPE_MATCHER(ParenType, parenType);
   4933 
   4934 /// \brief Matches \c ParenType nodes where the inner type is a specific type.
   4935 ///
   4936 /// Given
   4937 /// \code
   4938 ///   int (*ptr_to_array)[4];
   4939 ///   int (*ptr_to_func)(int);
   4940 /// \endcode
   4941 ///
   4942 /// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches
   4943 /// \c ptr_to_func but not \c ptr_to_array.
   4944 ///
   4945 /// Usable as: Matcher<ParenType>
   4946 AST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType,
   4947                           AST_POLYMORPHIC_SUPPORTED_TYPES(ParenType));
   4948 
   4949 /// \brief Matches block pointer types, i.e. types syntactically represented as
   4950 /// "void (^)(int)".
   4951 ///
   4952 /// The \c pointee is always required to be a \c FunctionType.
   4953 AST_TYPE_MATCHER(BlockPointerType, blockPointerType);
   4954 
   4955 /// \brief Matches member pointer types.
   4956 /// Given
   4957 /// \code
   4958 ///   struct A { int i; }
   4959 ///   A::* ptr = A::i;
   4960 /// \endcode
   4961 /// memberPointerType()
   4962 ///   matches "A::* ptr"
   4963 AST_TYPE_MATCHER(MemberPointerType, memberPointerType);
   4964 
   4965 /// \brief Matches pointer types, but does not match Objective-C object pointer
   4966 /// types.
   4967 ///
   4968 /// Given
   4969 /// \code
   4970 ///   int *a;
   4971 ///   int &b = *a;
   4972 ///   int c = 5;
   4973 ///
   4974 ///   @interface Foo
   4975 ///   @end
   4976 ///   Foo *f;
   4977 /// \endcode
   4978 /// pointerType()
   4979 ///   matches "int *a", but does not match "Foo *f".
   4980 AST_TYPE_MATCHER(PointerType, pointerType);
   4981 
   4982 /// \brief Matches an Objective-C object pointer type, which is different from
   4983 /// a pointer type, despite being syntactically similar.
   4984 ///
   4985 /// Given
   4986 /// \code
   4987 ///   int *a;
   4988 ///
   4989 ///   @interface Foo
   4990 ///   @end
   4991 ///   Foo *f;
   4992 /// \endcode
   4993 /// pointerType()
   4994 ///   matches "Foo *f", but does not match "int *a".
   4995 AST_TYPE_MATCHER(ObjCObjectPointerType, objcObjectPointerType);
   4996 
   4997 /// \brief Matches both lvalue and rvalue reference types.
   4998 ///
   4999 /// Given
   5000 /// \code
   5001 ///   int *a;
   5002 ///   int &b = *a;
   5003 ///   int &&c = 1;
   5004 ///   auto &d = b;
   5005 ///   auto &&e = c;
   5006 ///   auto &&f = 2;
   5007 ///   int g = 5;
   5008 /// \endcode
   5009 ///
   5010 /// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f.
   5011 AST_TYPE_MATCHER(ReferenceType, referenceType);
   5012 
   5013 /// \brief Matches lvalue reference types.
   5014 ///
   5015 /// Given:
   5016 /// \code
   5017 ///   int *a;
   5018 ///   int &b = *a;
   5019 ///   int &&c = 1;
   5020 ///   auto &d = b;
   5021 ///   auto &&e = c;
   5022 ///   auto &&f = 2;
   5023 ///   int g = 5;
   5024 /// \endcode
   5025 ///
   5026 /// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is
   5027 /// matched since the type is deduced as int& by reference collapsing rules.
   5028 AST_TYPE_MATCHER(LValueReferenceType, lValueReferenceType);
   5029 
   5030 /// \brief Matches rvalue reference types.
   5031 ///
   5032 /// Given:
   5033 /// \code
   5034 ///   int *a;
   5035 ///   int &b = *a;
   5036 ///   int &&c = 1;
   5037 ///   auto &d = b;
   5038 ///   auto &&e = c;
   5039 ///   auto &&f = 2;
   5040 ///   int g = 5;
   5041 /// \endcode
   5042 ///
   5043 /// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not
   5044 /// matched as it is deduced to int& by reference collapsing rules.
   5045 AST_TYPE_MATCHER(RValueReferenceType, rValueReferenceType);
   5046 
   5047 /// \brief Narrows PointerType (and similar) matchers to those where the
   5048 /// \c pointee matches a given matcher.
   5049 ///
   5050 /// Given
   5051 /// \code
   5052 ///   int *a;
   5053 ///   int const *b;
   5054 ///   float const *f;
   5055 /// \endcode
   5056 /// pointerType(pointee(isConstQualified(), isInteger()))
   5057 ///   matches "int const *b"
   5058 ///
   5059 /// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>,
   5060 ///   Matcher<PointerType>, Matcher<ReferenceType>
   5061 AST_TYPELOC_TRAVERSE_MATCHER(pointee, getPointee,
   5062                              AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType,
   5063                                                              MemberPointerType,
   5064                                                              PointerType,
   5065                                                              ReferenceType));
   5066 
   5067 /// \brief Matches typedef types.
   5068 ///
   5069 /// Given
   5070 /// \code
   5071 ///   typedef int X;
   5072 /// \endcode
   5073 /// typedefType()
   5074 ///   matches "typedef int X"
   5075 AST_TYPE_MATCHER(TypedefType, typedefType);
   5076 
   5077 /// \brief Matches enum types.
   5078 ///
   5079 /// Given
   5080 /// \code
   5081 ///   enum C { Green };
   5082 ///   enum class S { Red };
   5083 ///
   5084 ///   C c;
   5085 ///   S s;
   5086 /// \endcode
   5087 //
   5088 /// \c enumType() matches the type of the variable declarations of both \c c and
   5089 /// \c s.
   5090 AST_TYPE_MATCHER(EnumType, enumType);
   5091 
   5092 /// \brief Matches template specialization types.
   5093 ///
   5094 /// Given
   5095 /// \code
   5096 ///   template <typename T>
   5097 ///   class C { };
   5098 ///
   5099 ///   template class C<int>;  // A
   5100 ///   C<char> var;            // B
   5101 /// \endcode
   5102 ///
   5103 /// \c templateSpecializationType() matches the type of the explicit
   5104 /// instantiation in \c A and the type of the variable declaration in \c B.
   5105 AST_TYPE_MATCHER(TemplateSpecializationType, templateSpecializationType);
   5106 
   5107 /// \brief Matches types nodes representing unary type transformations.
   5108 ///
   5109 /// Given:
   5110 /// \code
   5111 ///   typedef __underlying_type(T) type;
   5112 /// \endcode
   5113 /// unaryTransformType()
   5114 ///   matches "__underlying_type(T)"
   5115 AST_TYPE_MATCHER(UnaryTransformType, unaryTransformType);
   5116 
   5117 /// \brief Matches record types (e.g. structs, classes).
   5118 ///
   5119 /// Given
   5120 /// \code
   5121 ///   class C {};
   5122 ///   struct S {};
   5123 ///
   5124 ///   C c;
   5125 ///   S s;
   5126 /// \endcode
   5127 ///
   5128 /// \c recordType() matches the type of the variable declarations of both \c c
   5129 /// and \c s.
   5130 AST_TYPE_MATCHER(RecordType, recordType);
   5131 
   5132 /// \brief Matches tag types (record and enum types).
   5133 ///
   5134 /// Given
   5135 /// \code
   5136 ///   enum E {};
   5137 ///   class C {};
   5138 ///
   5139 ///   E e;
   5140 ///   C c;
   5141 /// \endcode
   5142 ///
   5143 /// \c tagType() matches the type of the variable declarations of both \c e
   5144 /// and \c c.
   5145 AST_TYPE_MATCHER(TagType, tagType);
   5146 
   5147 /// \brief Matches types specified with an elaborated type keyword or with a
   5148 /// qualified name.
   5149 ///
   5150 /// Given
   5151 /// \code
   5152 ///   namespace N {
   5153 ///     namespace M {
   5154 ///       class D {};
   5155 ///     }
   5156 ///   }
   5157 ///   class C {};
   5158 ///
   5159 ///   class C c;
   5160 ///   N::M::D d;
   5161 /// \endcode
   5162 ///
   5163 /// \c elaboratedType() matches the type of the variable declarations of both
   5164 /// \c c and \c d.
   5165 AST_TYPE_MATCHER(ElaboratedType, elaboratedType);
   5166 
   5167 /// \brief Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier,
   5168 /// matches \c InnerMatcher if the qualifier exists.
   5169 ///
   5170 /// Given
   5171 /// \code
   5172 ///   namespace N {
   5173 ///     namespace M {
   5174 ///       class D {};
   5175 ///     }
   5176 ///   }
   5177 ///   N::M::D d;
   5178 /// \endcode
   5179 ///
   5180 /// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))
   5181 /// matches the type of the variable declaration of \c d.
   5182 AST_MATCHER_P(ElaboratedType, hasQualifier,
   5183               internal::Matcher<NestedNameSpecifier>, InnerMatcher) {
   5184   if (const NestedNameSpecifier *Qualifier = Node.getQualifier())
   5185     return InnerMatcher.matches(*Qualifier, Finder, Builder);
   5186 
   5187   return false;
   5188 }
   5189 
   5190 /// \brief Matches ElaboratedTypes whose named type matches \c InnerMatcher.
   5191 ///
   5192 /// Given
   5193 /// \code
   5194 ///   namespace N {
   5195 ///     namespace M {
   5196 ///       class D {};
   5197 ///     }
   5198 ///   }
   5199 ///   N::M::D d;
   5200 /// \endcode
   5201 ///
   5202 /// \c elaboratedType(namesType(recordType(
   5203 /// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable
   5204 /// declaration of \c d.
   5205 AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>,
   5206               InnerMatcher) {
   5207   return InnerMatcher.matches(Node.getNamedType(), Finder, Builder);
   5208 }
   5209 
   5210 /// \brief Matches types that represent the result of substituting a type for a
   5211 /// template type parameter.
   5212 ///
   5213 /// Given
   5214 /// \code
   5215 ///   template <typename T>
   5216 ///   void F(T t) {
   5217 ///     int i = 1 + t;
   5218 ///   }
   5219 /// \endcode
   5220 ///
   5221 /// \c substTemplateTypeParmType() matches the type of 't' but not '1'
   5222 AST_TYPE_MATCHER(SubstTemplateTypeParmType, substTemplateTypeParmType);
   5223 
   5224 /// \brief Matches template type parameter substitutions that have a replacement
   5225 /// type that matches the provided matcher.
   5226 ///
   5227 /// Given
   5228 /// \code
   5229 ///   template <typename T>
   5230 ///   double F(T t);
   5231 ///   int i;
   5232 ///   double j = F(i);
   5233 /// \endcode
   5234 ///
   5235 /// \c substTemplateTypeParmType(hasReplacementType(type())) matches int
   5236 AST_TYPE_TRAVERSE_MATCHER(
   5237     hasReplacementType, getReplacementType,
   5238     AST_POLYMORPHIC_SUPPORTED_TYPES(SubstTemplateTypeParmType));
   5239 
   5240 /// \brief Matches template type parameter types.
   5241 ///
   5242 /// Example matches T, but not int.
   5243 ///     (matcher = templateTypeParmType())
   5244 /// \code
   5245 ///   template <typename T> void f(int i);
   5246 /// \endcode
   5247 AST_TYPE_MATCHER(TemplateTypeParmType, templateTypeParmType);
   5248 
   5249 /// \brief Matches injected class name types.
   5250 ///
   5251 /// Example matches S s, but not S<T> s.
   5252 ///     (matcher = parmVarDecl(hasType(injectedClassNameType())))
   5253 /// \code
   5254 ///   template <typename T> struct S {
   5255 ///     void f(S s);
   5256 ///     void g(S<T> s);
   5257 ///   };
   5258 /// \endcode
   5259 AST_TYPE_MATCHER(InjectedClassNameType, injectedClassNameType);
   5260 
   5261 /// \brief Matches decayed type
   5262 /// Example matches i[] in declaration of f.
   5263 ///     (matcher = valueDecl(hasType(decayedType(hasDecayedType(pointerType())))))
   5264 /// Example matches i[1].
   5265 ///     (matcher = expr(hasType(decayedType(hasDecayedType(pointerType())))))
   5266 /// \code
   5267 ///   void f(int i[]) {
   5268 ///     i[1] = 0;
   5269 ///   }
   5270 /// \endcode
   5271 AST_TYPE_MATCHER(DecayedType, decayedType);
   5272 
   5273 /// \brief Matches the decayed type, whos decayed type matches \c InnerMatcher
   5274 AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
   5275               InnerType) {
   5276   return InnerType.matches(Node.getDecayedType(), Finder, Builder);
   5277 }
   5278 
   5279 /// \brief Matches declarations whose declaration context, interpreted as a
   5280 /// Decl, matches \c InnerMatcher.
   5281 ///
   5282 /// Given
   5283 /// \code
   5284 ///   namespace N {
   5285 ///     namespace M {
   5286 ///       class D {};
   5287 ///     }
   5288 ///   }
   5289 /// \endcode
   5290 ///
   5291 /// \c cxxRcordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the
   5292 /// declaration of \c class \c D.
   5293 AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) {
   5294   const DeclContext *DC = Node.getDeclContext();
   5295   if (!DC) return false;
   5296   return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder);
   5297 }
   5298 
   5299 /// \brief Matches nested name specifiers.
   5300 ///
   5301 /// Given
   5302 /// \code
   5303 ///   namespace ns {
   5304 ///     struct A { static void f(); };
   5305 ///     void A::f() {}
   5306 ///     void g() { A::f(); }
   5307 ///   }
   5308 ///   ns::A a;
   5309 /// \endcode
   5310 /// nestedNameSpecifier()
   5311 ///   matches "ns::" and both "A::"
   5312 const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier;
   5313 
   5314 /// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc.
   5315 const internal::VariadicAllOfMatcher<
   5316   NestedNameSpecifierLoc> nestedNameSpecifierLoc;
   5317 
   5318 /// \brief Matches \c NestedNameSpecifierLocs for which the given inner
   5319 /// NestedNameSpecifier-matcher matches.
   5320 AST_MATCHER_FUNCTION_P_OVERLOAD(
   5321     internal::BindableMatcher<NestedNameSpecifierLoc>, loc,
   5322     internal::Matcher<NestedNameSpecifier>, InnerMatcher, 1) {
   5323   return internal::BindableMatcher<NestedNameSpecifierLoc>(
   5324       new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>(
   5325           InnerMatcher));
   5326 }
   5327 
   5328 /// \brief Matches nested name specifiers that specify a type matching the
   5329 /// given \c QualType matcher without qualifiers.
   5330 ///
   5331 /// Given
   5332 /// \code
   5333 ///   struct A { struct B { struct C {}; }; };
   5334 ///   A::B::C c;
   5335 /// \endcode
   5336 /// nestedNameSpecifier(specifiesType(
   5337 ///   hasDeclaration(cxxRecordDecl(hasName("A")))
   5338 /// ))
   5339 ///   matches "A::"
   5340 AST_MATCHER_P(NestedNameSpecifier, specifiesType,
   5341               internal::Matcher<QualType>, InnerMatcher) {
   5342   if (!Node.getAsType())
   5343     return false;
   5344   return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder);
   5345 }
   5346 
   5347 /// \brief Matches nested name specifier locs that specify a type matching the
   5348 /// given \c TypeLoc.
   5349 ///
   5350 /// Given
   5351 /// \code
   5352 ///   struct A { struct B { struct C {}; }; };
   5353 ///   A::B::C c;
   5354 /// \endcode
   5355 /// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type(
   5356 ///   hasDeclaration(cxxRecordDecl(hasName("A")))))))
   5357 ///   matches "A::"
   5358 AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc,
   5359               internal::Matcher<TypeLoc>, InnerMatcher) {
   5360   return Node && InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder);
   5361 }
   5362 
   5363 /// \brief Matches on the prefix of a \c NestedNameSpecifier.
   5364 ///
   5365 /// Given
   5366 /// \code
   5367 ///   struct A { struct B { struct C {}; }; };
   5368 ///   A::B::C c;
   5369 /// \endcode
   5370 /// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and
   5371 ///   matches "A::"
   5372 AST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix,
   5373                        internal::Matcher<NestedNameSpecifier>, InnerMatcher,
   5374                        0) {
   5375   const NestedNameSpecifier *NextNode = Node.getPrefix();
   5376   if (!NextNode)
   5377     return false;
   5378   return InnerMatcher.matches(*NextNode, Finder, Builder);
   5379 }
   5380 
   5381 /// \brief Matches on the prefix of a \c NestedNameSpecifierLoc.
   5382 ///
   5383 /// Given
   5384 /// \code
   5385 ///   struct A { struct B { struct C {}; }; };
   5386 ///   A::B::C c;
   5387 /// \endcode
   5388 /// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A")))))
   5389 ///   matches "A::"
   5390 AST_MATCHER_P_OVERLOAD(NestedNameSpecifierLoc, hasPrefix,
   5391                        internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher,
   5392                        1) {
   5393   NestedNameSpecifierLoc NextNode = Node.getPrefix();
   5394   if (!NextNode)
   5395     return false;
   5396   return InnerMatcher.matches(NextNode, Finder, Builder);
   5397 }
   5398 
   5399 /// \brief Matches nested name specifiers that specify a namespace matching the
   5400 /// given namespace matcher.
   5401 ///
   5402 /// Given
   5403 /// \code
   5404 ///   namespace ns { struct A {}; }
   5405 ///   ns::A a;
   5406 /// \endcode
   5407 /// nestedNameSpecifier(specifiesNamespace(hasName("ns")))
   5408 ///   matches "ns::"
   5409 AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace,
   5410               internal::Matcher<NamespaceDecl>, InnerMatcher) {
   5411   if (!Node.getAsNamespace())
   5412     return false;
   5413   return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder);
   5414 }
   5415 
   5416 /// \brief Overloads for the \c equalsNode matcher.
   5417 /// FIXME: Implement for other node types.
   5418 /// @{
   5419 
   5420 /// \brief Matches if a node equals another node.
   5421 ///
   5422 /// \c Decl has pointer identity in the AST.
   5423 AST_MATCHER_P_OVERLOAD(Decl, equalsNode, const Decl*, Other, 0) {
   5424   return &Node == Other;
   5425 }
   5426 /// \brief Matches if a node equals another node.
   5427 ///
   5428 /// \c Stmt has pointer identity in the AST.
   5429 AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) {
   5430   return &Node == Other;
   5431 }
   5432 /// \brief Matches if a node equals another node.
   5433 ///
   5434 /// \c Type has pointer identity in the AST.
   5435 AST_MATCHER_P_OVERLOAD(Type, equalsNode, const Type*, Other, 2) {
   5436     return &Node == Other;
   5437 }
   5438 
   5439 /// @}
   5440 
   5441 /// \brief Matches each case or default statement belonging to the given switch
   5442 /// statement. This matcher may produce multiple matches.
   5443 ///
   5444 /// Given
   5445 /// \code
   5446 ///   switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } }
   5447 /// \endcode
   5448 /// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s")
   5449 ///   matches four times, with "c" binding each of "case 1:", "case 2:",
   5450 /// "case 3:" and "case 4:", and "s" respectively binding "switch (1)",
   5451 /// "switch (1)", "switch (2)" and "switch (2)".
   5452 AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>,
   5453               InnerMatcher) {
   5454   BoundNodesTreeBuilder Result;
   5455   // FIXME: getSwitchCaseList() does not necessarily guarantee a stable
   5456   // iteration order. We should use the more general iterating matchers once
   5457   // they are capable of expressing this matcher (for example, it should ignore
   5458   // case statements belonging to nested switch statements).
   5459   bool Matched = false;
   5460   for (const SwitchCase *SC = Node.getSwitchCaseList(); SC;
   5461        SC = SC->getNextSwitchCase()) {
   5462     BoundNodesTreeBuilder CaseBuilder(*Builder);
   5463     bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder);
   5464     if (CaseMatched) {
   5465       Matched = true;
   5466       Result.addMatch(CaseBuilder);
   5467     }
   5468   }
   5469   *Builder = std::move(Result);
   5470   return Matched;
   5471 }
   5472 
   5473 /// \brief Matches each constructor initializer in a constructor definition.
   5474 ///
   5475 /// Given
   5476 /// \code
   5477 ///   class A { A() : i(42), j(42) {} int i; int j; };
   5478 /// \endcode
   5479 /// cxxConstructorDecl(forEachConstructorInitializer(
   5480 ///   forField(decl().bind("x"))
   5481 /// ))
   5482 ///   will trigger two matches, binding for 'i' and 'j' respectively.
   5483 AST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer,
   5484               internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
   5485   BoundNodesTreeBuilder Result;
   5486   bool Matched = false;
   5487   for (const auto *I : Node.inits()) {
   5488     BoundNodesTreeBuilder InitBuilder(*Builder);
   5489     if (InnerMatcher.matches(*I, Finder, &InitBuilder)) {
   5490       Matched = true;
   5491       Result.addMatch(InitBuilder);
   5492     }
   5493   }
   5494   *Builder = std::move(Result);
   5495   return Matched;
   5496 }
   5497 
   5498 /// \brief Matches constructor declarations that are copy constructors.
   5499 ///
   5500 /// Given
   5501 /// \code
   5502 ///   struct S {
   5503 ///     S(); // #1
   5504 ///     S(const S &); // #2
   5505 ///     S(S &&); // #3
   5506 ///   };
   5507 /// \endcode
   5508 /// cxxConstructorDecl(isCopyConstructor()) will match #2, but not #1 or #3.
   5509 AST_MATCHER(CXXConstructorDecl, isCopyConstructor) {
   5510   return Node.isCopyConstructor();
   5511 }
   5512 
   5513 /// \brief Matches constructor declarations that are move constructors.
   5514 ///
   5515 /// Given
   5516 /// \code
   5517 ///   struct S {
   5518 ///     S(); // #1
   5519 ///     S(const S &); // #2
   5520 ///     S(S &&); // #3
   5521 ///   };
   5522 /// \endcode
   5523 /// cxxConstructorDecl(isMoveConstructor()) will match #3, but not #1 or #2.
   5524 AST_MATCHER(CXXConstructorDecl, isMoveConstructor) {
   5525   return Node.isMoveConstructor();
   5526 }
   5527 
   5528 /// \brief Matches constructor declarations that are default constructors.
   5529 ///
   5530 /// Given
   5531 /// \code
   5532 ///   struct S {
   5533 ///     S(); // #1
   5534 ///     S(const S &); // #2
   5535 ///     S(S &&); // #3
   5536 ///   };
   5537 /// \endcode
   5538 /// cxxConstructorDecl(isDefaultConstructor()) will match #1, but not #2 or #3.
   5539 AST_MATCHER(CXXConstructorDecl, isDefaultConstructor) {
   5540   return Node.isDefaultConstructor();
   5541 }
   5542 
   5543 /// \brief Matches constructors that delegate to another constructor.
   5544 ///
   5545 /// Given
   5546 /// \code
   5547 ///   struct S {
   5548 ///     S(); // #1
   5549 ///     S(int) {} // #2
   5550 ///     S(S &&) : S() {} // #3
   5551 ///   };
   5552 ///   S::S() : S(0) {} // #4
   5553 /// \endcode
   5554 /// cxxConstructorDecl(isDelegatingConstructor()) will match #3 and #4, but not
   5555 /// #1 or #2.
   5556 AST_MATCHER(CXXConstructorDecl, isDelegatingConstructor) {
   5557   return Node.isDelegatingConstructor();
   5558 }
   5559 
   5560 /// \brief Matches constructor and conversion declarations that are marked with
   5561 /// the explicit keyword.
   5562 ///
   5563 /// Given
   5564 /// \code
   5565 ///   struct S {
   5566 ///     S(int); // #1
   5567 ///     explicit S(double); // #2
   5568 ///     operator int(); // #3
   5569 ///     explicit operator bool(); // #4
   5570 ///   };
   5571 /// \endcode
   5572 /// cxxConstructorDecl(isExplicit()) will match #2, but not #1.
   5573 /// cxxConversionDecl(isExplicit()) will match #4, but not #3.
   5574 AST_POLYMORPHIC_MATCHER(isExplicit,
   5575                         AST_POLYMORPHIC_SUPPORTED_TYPES(CXXConstructorDecl,
   5576                                                         CXXConversionDecl)) {
   5577   return Node.isExplicit();
   5578 }
   5579 
   5580 /// \brief Matches function and namespace declarations that are marked with
   5581 /// the inline keyword.
   5582 ///
   5583 /// Given
   5584 /// \code
   5585 ///   inline void f();
   5586 ///   void g();
   5587 ///   namespace n {
   5588 ///   inline namespace m {}
   5589 ///   }
   5590 /// \endcode
   5591 /// functionDecl(isInline()) will match ::f().
   5592 /// namespaceDecl(isInline()) will match n::m.
   5593 AST_POLYMORPHIC_MATCHER(isInline,
   5594                         AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl,
   5595                                                         FunctionDecl)) {
   5596   // This is required because the spelling of the function used to determine
   5597   // whether inline is specified or not differs between the polymorphic types.
   5598   if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
   5599     return FD->isInlineSpecified();
   5600   else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
   5601     return NSD->isInline();
   5602   llvm_unreachable("Not a valid polymorphic type");
   5603 }
   5604 
   5605 /// \brief Matches anonymous namespace declarations.
   5606 ///
   5607 /// Given
   5608 /// \code
   5609 ///   namespace n {
   5610 ///   namespace {} // #1
   5611 ///   }
   5612 /// \endcode
   5613 /// namespaceDecl(isAnonymous()) will match #1 but not ::n.
   5614 AST_MATCHER(NamespaceDecl, isAnonymous) {
   5615   return Node.isAnonymousNamespace();
   5616 }
   5617 
   5618 /// \brief If the given case statement does not use the GNU case range
   5619 /// extension, matches the constant given in the statement.
   5620 ///
   5621 /// Given
   5622 /// \code
   5623 ///   switch (1) { case 1: case 1+1: case 3 ... 4: ; }
   5624 /// \endcode
   5625 /// caseStmt(hasCaseConstant(integerLiteral()))
   5626 ///   matches "case 1:"
   5627 AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>,
   5628               InnerMatcher) {
   5629   if (Node.getRHS())
   5630     return false;
   5631 
   5632   return InnerMatcher.matches(*Node.getLHS(), Finder, Builder);
   5633 }
   5634 
   5635 /// \brief Matches declaration that has a given attribute.
   5636 ///
   5637 /// Given
   5638 /// \code
   5639 ///   __attribute__((device)) void f() { ... }
   5640 /// \endcode
   5641 /// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of
   5642 /// f. If the matcher is use from clang-query, attr::Kind parameter should be
   5643 /// passed as a quoted string. e.g., hasAttr("attr::CUDADevice").
   5644 AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) {
   5645   for (const auto *Attr : Node.attrs()) {
   5646     if (Attr->getKind() == AttrKind)
   5647       return true;
   5648   }
   5649   return false;
   5650 }
   5651 
   5652 /// \brief Matches the return value expression of a return statement
   5653 ///
   5654 /// Given
   5655 /// \code
   5656 ///   return a + b;
   5657 /// \endcode
   5658 /// hasReturnValue(binaryOperator())
   5659 ///   matches 'return a + b'
   5660 /// with binaryOperator()
   5661 ///   matching 'a + b'
   5662 AST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher<Expr>,
   5663               InnerMatcher) {
   5664   if (const auto *RetValue = Node.getRetValue())
   5665     return InnerMatcher.matches(*RetValue, Finder, Builder);
   5666   return false;
   5667 }
   5668 
   5669 
   5670 /// \brief Matches CUDA kernel call expression.
   5671 ///
   5672 /// Example matches,
   5673 /// \code
   5674 ///   kernel<<<i,j>>>();
   5675 /// \endcode
   5676 const internal::VariadicDynCastAllOfMatcher<
   5677   Stmt,
   5678   CUDAKernelCallExpr> cudaKernelCallExpr;
   5679 
   5680 
   5681 /// \brief Matches expressions that resolve to a null pointer constant, such as
   5682 /// GNU's __null, C++11's nullptr, or C's NULL macro.
   5683 ///
   5684 /// Given:
   5685 /// \code
   5686 ///   void *v1 = NULL;
   5687 ///   void *v2 = nullptr;
   5688 ///   void *v3 = __null; // GNU extension
   5689 ///   char *cp = (char *)0;
   5690 ///   int *ip = 0;
   5691 ///   int i = 0;
   5692 /// \endcode
   5693 /// expr(nullPointerConstant())
   5694 ///   matches the initializer for v1, v2, v3, cp, and ip. Does not match the
   5695 ///   initializer for i.
   5696 AST_MATCHER_FUNCTION(internal::Matcher<Expr>, nullPointerConstant) {
   5697   return anyOf(
   5698       gnuNullExpr(), cxxNullPtrLiteralExpr(),
   5699       integerLiteral(equals(0), hasParent(expr(hasType(pointerType())))));
   5700 }
   5701 
   5702 /// \brief Matches declaration of the function the statement belongs to
   5703 ///
   5704 /// Given:
   5705 /// \code
   5706 /// F& operator=(const F& o) {
   5707 ///   std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
   5708 ///   return *this;
   5709 /// }
   5710 /// \endcode
   5711 /// returnStmt(forFunction(hasName("operator=")))
   5712 ///   matches 'return *this'
   5713 ///   but does match 'return > 0'
   5714 AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
   5715               InnerMatcher) {
   5716   const auto &Parents = Finder->getASTContext().getParents(Node);
   5717 
   5718   llvm::SmallVector<ast_type_traits::DynTypedNode, 8> Stack(Parents.begin(),
   5719                                                             Parents.end());
   5720   while(!Stack.empty()) {
   5721     const auto &CurNode = Stack.back();
   5722     Stack.pop_back();
   5723     if(const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
   5724       if(InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
   5725         return true;
   5726       }
   5727     } else if(const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
   5728       if(InnerMatcher.matches(*LambdaExprNode->getCallOperator(),
   5729                               Finder, Builder)) {
   5730         return true;
   5731       }
   5732     } else {
   5733       for(const auto &Parent: Finder->getASTContext().getParents(CurNode))
   5734         Stack.push_back(Parent);
   5735     }
   5736   }
   5737   return false;
   5738 }
   5739 
   5740 /// \brief Matches a declaration that has external formal linkage.
   5741 ///
   5742 /// Example matches only z (matcher = varDecl(hasExternalFormalLinkage()))
   5743 /// \code
   5744 /// void f() {
   5745 ///   int x;
   5746 ///   static int y;
   5747 /// }
   5748 /// int z;
   5749 /// \endcode
   5750 ///
   5751 /// Example matches f() because it has external formal linkage despite being
   5752 /// unique to the translation unit as though it has internal likage
   5753 /// (matcher = functionDecl(hasExternalFormalLinkage()))
   5754 ///
   5755 /// \code
   5756 /// namespace {
   5757 /// void f() {}
   5758 /// }
   5759 /// \endcode
   5760 AST_MATCHER(NamedDecl, hasExternalFormalLinkage) {
   5761   return Node.hasExternalFormalLinkage();
   5762 }
   5763 
   5764 } // end namespace ast_matchers
   5765 } // end namespace clang
   5766 
   5767 #endif
   5768