Home | History | Annotate | Download | only in AST
      1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- 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 defines the RecursiveASTVisitor interface, which recursively
     11 //  traverses the entire AST.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
     15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
     16 
     17 #include <type_traits>
     18 
     19 #include "clang/AST/Attr.h"
     20 #include "clang/AST/Decl.h"
     21 #include "clang/AST/DeclCXX.h"
     22 #include "clang/AST/DeclFriend.h"
     23 #include "clang/AST/DeclObjC.h"
     24 #include "clang/AST/DeclOpenMP.h"
     25 #include "clang/AST/DeclTemplate.h"
     26 #include "clang/AST/Expr.h"
     27 #include "clang/AST/ExprCXX.h"
     28 #include "clang/AST/ExprObjC.h"
     29 #include "clang/AST/ExprOpenMP.h"
     30 #include "clang/AST/NestedNameSpecifier.h"
     31 #include "clang/AST/Stmt.h"
     32 #include "clang/AST/StmtCXX.h"
     33 #include "clang/AST/StmtObjC.h"
     34 #include "clang/AST/StmtOpenMP.h"
     35 #include "clang/AST/TemplateBase.h"
     36 #include "clang/AST/TemplateName.h"
     37 #include "clang/AST/Type.h"
     38 #include "clang/AST/TypeLoc.h"
     39 
     40 // The following three macros are used for meta programming.  The code
     41 // using them is responsible for defining macro OPERATOR().
     42 
     43 // All unary operators.
     44 #define UNARYOP_LIST()                                                         \
     45   OPERATOR(PostInc) OPERATOR(PostDec) OPERATOR(PreInc) OPERATOR(PreDec)        \
     46       OPERATOR(AddrOf) OPERATOR(Deref) OPERATOR(Plus) OPERATOR(Minus)          \
     47       OPERATOR(Not) OPERATOR(LNot) OPERATOR(Real) OPERATOR(Imag)               \
     48       OPERATOR(Extension) OPERATOR(Coawait)
     49 
     50 // All binary operators (excluding compound assign operators).
     51 #define BINOP_LIST()                                                           \
     52   OPERATOR(PtrMemD) OPERATOR(PtrMemI) OPERATOR(Mul) OPERATOR(Div)              \
     53       OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) OPERATOR(Shr)    \
     54       OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) OPERATOR(GE) OPERATOR(EQ)         \
     55       OPERATOR(NE) OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) OPERATOR(LAnd)     \
     56       OPERATOR(LOr) OPERATOR(Assign) OPERATOR(Comma)
     57 
     58 // All compound assign operators.
     59 #define CAO_LIST()                                                             \
     60   OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub)        \
     61       OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
     62 
     63 namespace clang {
     64 
     65 // A helper macro to implement short-circuiting when recursing.  It
     66 // invokes CALL_EXPR, which must be a method call, on the derived
     67 // object (s.t. a user of RecursiveASTVisitor can override the method
     68 // in CALL_EXPR).
     69 #define TRY_TO(CALL_EXPR)                                                      \
     70   do {                                                                         \
     71     if (!getDerived().CALL_EXPR)                                               \
     72       return false;                                                            \
     73   } while (0)
     74 
     75 /// \brief A class that does preorder depth-first traversal on the
     76 /// entire Clang AST and visits each node.
     77 ///
     78 /// This class performs three distinct tasks:
     79 ///   1. traverse the AST (i.e. go to each node);
     80 ///   2. at a given node, walk up the class hierarchy, starting from
     81 ///      the node's dynamic type, until the top-most class (e.g. Stmt,
     82 ///      Decl, or Type) is reached.
     83 ///   3. given a (node, class) combination, where 'class' is some base
     84 ///      class of the dynamic type of 'node', call a user-overridable
     85 ///      function to actually visit the node.
     86 ///
     87 /// These tasks are done by three groups of methods, respectively:
     88 ///   1. TraverseDecl(Decl *x) does task #1.  It is the entry point
     89 ///      for traversing an AST rooted at x.  This method simply
     90 ///      dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
     91 ///      is the dynamic type of *x, which calls WalkUpFromFoo(x) and
     92 ///      then recursively visits the child nodes of x.
     93 ///      TraverseStmt(Stmt *x) and TraverseType(QualType x) work
     94 ///      similarly.
     95 ///   2. WalkUpFromFoo(Foo *x) does task #2.  It does not try to visit
     96 ///      any child node of x.  Instead, it first calls WalkUpFromBar(x)
     97 ///      where Bar is the direct parent class of Foo (unless Foo has
     98 ///      no parent), and then calls VisitFoo(x) (see the next list item).
     99 ///   3. VisitFoo(Foo *x) does task #3.
    100 ///
    101 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
    102 /// Visit*).  A method (e.g. Traverse*) may call methods from the same
    103 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
    104 /// It may not call methods from a higher tier.
    105 ///
    106 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
    107 /// is Foo's super class) before calling VisitFoo(), the result is
    108 /// that the Visit*() methods for a given node are called in the
    109 /// top-down order (e.g. for a node of type NamespaceDecl, the order will
    110 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
    111 ///
    112 /// This scheme guarantees that all Visit*() calls for the same AST
    113 /// node are grouped together.  In other words, Visit*() methods for
    114 /// different nodes are never interleaved.
    115 ///
    116 /// Clients of this visitor should subclass the visitor (providing
    117 /// themselves as the template argument, using the curiously recurring
    118 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
    119 /// and Visit* methods for declarations, types, statements,
    120 /// expressions, or other AST nodes where the visitor should customize
    121 /// behavior.  Most users only need to override Visit*.  Advanced
    122 /// users may override Traverse* and WalkUpFrom* to implement custom
    123 /// traversal strategies.  Returning false from one of these overridden
    124 /// functions will abort the entire traversal.
    125 ///
    126 /// By default, this visitor tries to visit every part of the explicit
    127 /// source code exactly once.  The default policy towards templates
    128 /// is to descend into the 'pattern' class or function body, not any
    129 /// explicit or implicit instantiations.  Explicit specializations
    130 /// are still visited, and the patterns of partial specializations
    131 /// are visited separately.  This behavior can be changed by
    132 /// overriding shouldVisitTemplateInstantiations() in the derived class
    133 /// to return true, in which case all known implicit and explicit
    134 /// instantiations will be visited at the same time as the pattern
    135 /// from which they were produced.
    136 template <typename Derived> class RecursiveASTVisitor {
    137 public:
    138   /// A queue used for performing data recursion over statements.
    139   /// Parameters involving this type are used to implement data
    140   /// recursion over Stmts and Exprs within this class, and should
    141   /// typically not be explicitly specified by derived classes.
    142   typedef SmallVectorImpl<Stmt *> DataRecursionQueue;
    143 
    144   /// \brief Return a reference to the derived class.
    145   Derived &getDerived() { return *static_cast<Derived *>(this); }
    146 
    147   /// \brief Return whether this visitor should recurse into
    148   /// template instantiations.
    149   bool shouldVisitTemplateInstantiations() const { return false; }
    150 
    151   /// \brief Return whether this visitor should recurse into the types of
    152   /// TypeLocs.
    153   bool shouldWalkTypesOfTypeLocs() const { return true; }
    154 
    155   /// \brief Return whether this visitor should recurse into implicit
    156   /// code, e.g., implicit constructors and destructors.
    157   bool shouldVisitImplicitCode() const { return false; }
    158 
    159   /// \brief Recursively visit a statement or expression, by
    160   /// dispatching to Traverse*() based on the argument's dynamic type.
    161   ///
    162   /// \returns false if the visitation was terminated early, true
    163   /// otherwise (including when the argument is nullptr).
    164   bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr);
    165 
    166   /// \brief Recursively visit a type, by dispatching to
    167   /// Traverse*Type() based on the argument's getTypeClass() property.
    168   ///
    169   /// \returns false if the visitation was terminated early, true
    170   /// otherwise (including when the argument is a Null type).
    171   bool TraverseType(QualType T);
    172 
    173   /// \brief Recursively visit a type with location, by dispatching to
    174   /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
    175   ///
    176   /// \returns false if the visitation was terminated early, true
    177   /// otherwise (including when the argument is a Null type location).
    178   bool TraverseTypeLoc(TypeLoc TL);
    179 
    180   /// \brief Recursively visit an attribute, by dispatching to
    181   /// Traverse*Attr() based on the argument's dynamic type.
    182   ///
    183   /// \returns false if the visitation was terminated early, true
    184   /// otherwise (including when the argument is a Null type location).
    185   bool TraverseAttr(Attr *At);
    186 
    187   /// \brief Recursively visit a declaration, by dispatching to
    188   /// Traverse*Decl() based on the argument's dynamic type.
    189   ///
    190   /// \returns false if the visitation was terminated early, true
    191   /// otherwise (including when the argument is NULL).
    192   bool TraverseDecl(Decl *D);
    193 
    194   /// \brief Recursively visit a C++ nested-name-specifier.
    195   ///
    196   /// \returns false if the visitation was terminated early, true otherwise.
    197   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
    198 
    199   /// \brief Recursively visit a C++ nested-name-specifier with location
    200   /// information.
    201   ///
    202   /// \returns false if the visitation was terminated early, true otherwise.
    203   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
    204 
    205   /// \brief Recursively visit a name with its location information.
    206   ///
    207   /// \returns false if the visitation was terminated early, true otherwise.
    208   bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
    209 
    210   /// \brief Recursively visit a template name and dispatch to the
    211   /// appropriate method.
    212   ///
    213   /// \returns false if the visitation was terminated early, true otherwise.
    214   bool TraverseTemplateName(TemplateName Template);
    215 
    216   /// \brief Recursively visit a template argument and dispatch to the
    217   /// appropriate method for the argument type.
    218   ///
    219   /// \returns false if the visitation was terminated early, true otherwise.
    220   // FIXME: migrate callers to TemplateArgumentLoc instead.
    221   bool TraverseTemplateArgument(const TemplateArgument &Arg);
    222 
    223   /// \brief Recursively visit a template argument location and dispatch to the
    224   /// appropriate method for the argument type.
    225   ///
    226   /// \returns false if the visitation was terminated early, true otherwise.
    227   bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
    228 
    229   /// \brief Recursively visit a set of template arguments.
    230   /// This can be overridden by a subclass, but it's not expected that
    231   /// will be needed -- this visitor always dispatches to another.
    232   ///
    233   /// \returns false if the visitation was terminated early, true otherwise.
    234   // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
    235   bool TraverseTemplateArguments(const TemplateArgument *Args,
    236                                  unsigned NumArgs);
    237 
    238   /// \brief Recursively visit a constructor initializer.  This
    239   /// automatically dispatches to another visitor for the initializer
    240   /// expression, but not for the name of the initializer, so may
    241   /// be overridden for clients that need access to the name.
    242   ///
    243   /// \returns false if the visitation was terminated early, true otherwise.
    244   bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
    245 
    246   /// \brief Recursively visit a lambda capture.
    247   ///
    248   /// \returns false if the visitation was terminated early, true otherwise.
    249   bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C);
    250 
    251   /// \brief Recursively visit the body of a lambda expression.
    252   ///
    253   /// This provides a hook for visitors that need more context when visiting
    254   /// \c LE->getBody().
    255   ///
    256   /// \returns false if the visitation was terminated early, true otherwise.
    257   bool TraverseLambdaBody(LambdaExpr *LE, DataRecursionQueue *Queue = nullptr);
    258 
    259   /// \brief Recursively visit the syntactic or semantic form of an
    260   /// initialization list.
    261   ///
    262   /// \returns false if the visitation was terminated early, true otherwise.
    263   bool TraverseSynOrSemInitListExpr(InitListExpr *S,
    264                                     DataRecursionQueue *Queue = nullptr);
    265 
    266   // ---- Methods on Attrs ----
    267 
    268   // \brief Visit an attribute.
    269   bool VisitAttr(Attr *A) { return true; }
    270 
    271 // Declare Traverse* and empty Visit* for all Attr classes.
    272 #define ATTR_VISITOR_DECLS_ONLY
    273 #include "clang/AST/AttrVisitor.inc"
    274 #undef ATTR_VISITOR_DECLS_ONLY
    275 
    276 // ---- Methods on Stmts ----
    277 
    278 private:
    279   template<typename T, typename U>
    280   struct has_same_member_pointer_type : std::false_type {};
    281   template<typename T, typename U, typename R, typename... P>
    282   struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)>
    283       : std::true_type {};
    284 
    285   // Traverse the given statement. If the most-derived traverse function takes a
    286   // data recursion queue, pass it on; otherwise, discard it. Note that the
    287   // first branch of this conditional must compile whether or not the derived
    288   // class can take a queue, so if we're taking the second arm, make the first
    289   // arm call our function rather than the derived class version.
    290 #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE)                            \
    291   (has_same_member_pointer_type<decltype(                                      \
    292                                     &RecursiveASTVisitor::Traverse##NAME),     \
    293                                 decltype(&Derived::Traverse##NAME)>::value     \
    294        ? static_cast<typename std::conditional<                                \
    295              has_same_member_pointer_type<                                     \
    296                  decltype(&RecursiveASTVisitor::Traverse##NAME),               \
    297                  decltype(&Derived::Traverse##NAME)>::value,                   \
    298              Derived &, RecursiveASTVisitor &>::type>(*this)                   \
    299              .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE)                 \
    300        : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)))
    301 
    302 // Try to traverse the given statement, or enqueue it if we're performing data
    303 // recursion in the middle of traversing another statement. Can only be called
    304 // from within a DEF_TRAVERSE_STMT body or similar context.
    305 #define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S)                                     \
    306   do {                                                                         \
    307     if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue))                             \
    308       return false;                                                            \
    309   } while (0)
    310 
    311 public:
    312 // Declare Traverse*() for all concrete Stmt classes.
    313 #define ABSTRACT_STMT(STMT)
    314 #define STMT(CLASS, PARENT) \
    315   bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
    316 #include "clang/AST/StmtNodes.inc"
    317   // The above header #undefs ABSTRACT_STMT and STMT upon exit.
    318 
    319   // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
    320   bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
    321   bool VisitStmt(Stmt *S) { return true; }
    322 #define STMT(CLASS, PARENT)                                                    \
    323   bool WalkUpFrom##CLASS(CLASS *S) {                                           \
    324     TRY_TO(WalkUpFrom##PARENT(S));                                             \
    325     TRY_TO(Visit##CLASS(S));                                                   \
    326     return true;                                                               \
    327   }                                                                            \
    328   bool Visit##CLASS(CLASS *S) { return true; }
    329 #include "clang/AST/StmtNodes.inc"
    330 
    331 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
    332 // operator methods.  Unary operators are not classes in themselves
    333 // (they're all opcodes in UnaryOperator) but do have visitors.
    334 #define OPERATOR(NAME)                                                         \
    335   bool TraverseUnary##NAME(UnaryOperator *S,                                   \
    336                            DataRecursionQueue *Queue = nullptr) {              \
    337     TRY_TO(WalkUpFromUnary##NAME(S));                                          \
    338     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr());                                    \
    339     return true;                                                               \
    340   }                                                                            \
    341   bool WalkUpFromUnary##NAME(UnaryOperator *S) {                               \
    342     TRY_TO(WalkUpFromUnaryOperator(S));                                        \
    343     TRY_TO(VisitUnary##NAME(S));                                               \
    344     return true;                                                               \
    345   }                                                                            \
    346   bool VisitUnary##NAME(UnaryOperator *S) { return true; }
    347 
    348   UNARYOP_LIST()
    349 #undef OPERATOR
    350 
    351 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
    352 // operator methods.  Binary operators are not classes in themselves
    353 // (they're all opcodes in BinaryOperator) but do have visitors.
    354 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE)                               \
    355   bool TraverseBin##NAME(BINOP_TYPE *S, DataRecursionQueue *Queue = nullptr) { \
    356     TRY_TO(WalkUpFromBin##NAME(S));                                            \
    357     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLHS());                                        \
    358     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRHS());                                        \
    359     return true;                                                               \
    360   }                                                                            \
    361   bool WalkUpFromBin##NAME(BINOP_TYPE *S) {                                    \
    362     TRY_TO(WalkUpFrom##BINOP_TYPE(S));                                         \
    363     TRY_TO(VisitBin##NAME(S));                                                 \
    364     return true;                                                               \
    365   }                                                                            \
    366   bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
    367 
    368 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
    369   BINOP_LIST()
    370 #undef OPERATOR
    371 
    372 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
    373 // assignment methods.  Compound assignment operators are not
    374 // classes in themselves (they're all opcodes in
    375 // CompoundAssignOperator) but do have visitors.
    376 #define OPERATOR(NAME)                                                         \
    377   GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
    378 
    379   CAO_LIST()
    380 #undef OPERATOR
    381 #undef GENERAL_BINOP_FALLBACK
    382 
    383 // ---- Methods on Types ----
    384 // FIXME: revamp to take TypeLoc's rather than Types.
    385 
    386 // Declare Traverse*() for all concrete Type classes.
    387 #define ABSTRACT_TYPE(CLASS, BASE)
    388 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T);
    389 #include "clang/AST/TypeNodes.def"
    390   // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
    391 
    392   // Define WalkUpFrom*() and empty Visit*() for all Type classes.
    393   bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
    394   bool VisitType(Type *T) { return true; }
    395 #define TYPE(CLASS, BASE)                                                      \
    396   bool WalkUpFrom##CLASS##Type(CLASS##Type *T) {                               \
    397     TRY_TO(WalkUpFrom##BASE(T));                                               \
    398     TRY_TO(Visit##CLASS##Type(T));                                             \
    399     return true;                                                               \
    400   }                                                                            \
    401   bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
    402 #include "clang/AST/TypeNodes.def"
    403 
    404 // ---- Methods on TypeLocs ----
    405 // FIXME: this currently just calls the matching Type methods
    406 
    407 // Declare Traverse*() for all concrete TypeLoc classes.
    408 #define ABSTRACT_TYPELOC(CLASS, BASE)
    409 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
    410 #include "clang/AST/TypeLocNodes.def"
    411   // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
    412 
    413   // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
    414   bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
    415   bool VisitTypeLoc(TypeLoc TL) { return true; }
    416 
    417   // QualifiedTypeLoc and UnqualTypeLoc are not declared in
    418   // TypeNodes.def and thus need to be handled specially.
    419   bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
    420     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
    421   }
    422   bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
    423   bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
    424     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
    425   }
    426   bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
    427 
    428 // Note that BASE includes trailing 'Type' which CLASS doesn't.
    429 #define TYPE(CLASS, BASE)                                                      \
    430   bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) {                         \
    431     TRY_TO(WalkUpFrom##BASE##Loc(TL));                                         \
    432     TRY_TO(Visit##CLASS##TypeLoc(TL));                                         \
    433     return true;                                                               \
    434   }                                                                            \
    435   bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
    436 #include "clang/AST/TypeNodes.def"
    437 
    438 // ---- Methods on Decls ----
    439 
    440 // Declare Traverse*() for all concrete Decl classes.
    441 #define ABSTRACT_DECL(DECL)
    442 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D);
    443 #include "clang/AST/DeclNodes.inc"
    444   // The above header #undefs ABSTRACT_DECL and DECL upon exit.
    445 
    446   // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
    447   bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
    448   bool VisitDecl(Decl *D) { return true; }
    449 #define DECL(CLASS, BASE)                                                      \
    450   bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) {                               \
    451     TRY_TO(WalkUpFrom##BASE(D));                                               \
    452     TRY_TO(Visit##CLASS##Decl(D));                                             \
    453     return true;                                                               \
    454   }                                                                            \
    455   bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
    456 #include "clang/AST/DeclNodes.inc"
    457 
    458 private:
    459   // These are helper methods used by more than one Traverse* method.
    460   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
    461 #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND)                                   \
    462   bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D);
    463   DEF_TRAVERSE_TMPL_INST(Class)
    464   DEF_TRAVERSE_TMPL_INST(Var)
    465   DEF_TRAVERSE_TMPL_INST(Function)
    466 #undef DEF_TRAVERSE_TMPL_INST
    467   bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
    468                                           unsigned Count);
    469   bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
    470   bool TraverseRecordHelper(RecordDecl *D);
    471   bool TraverseCXXRecordHelper(CXXRecordDecl *D);
    472   bool TraverseDeclaratorHelper(DeclaratorDecl *D);
    473   bool TraverseDeclContextHelper(DeclContext *DC);
    474   bool TraverseFunctionHelper(FunctionDecl *D);
    475   bool TraverseVarHelper(VarDecl *D);
    476   bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
    477   bool TraverseOMPLoopDirective(OMPLoopDirective *S);
    478   bool TraverseOMPClause(OMPClause *C);
    479 #define OPENMP_CLAUSE(Name, Class) bool Visit##Class(Class *C);
    480 #include "clang/Basic/OpenMPKinds.def"
    481   /// \brief Process clauses with list of variables.
    482   template <typename T> bool VisitOMPClauseList(T *Node);
    483 
    484   bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
    485 };
    486 
    487 template <typename Derived>
    488 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
    489                                                     DataRecursionQueue *Queue) {
    490 #define DISPATCH_STMT(NAME, CLASS, VAR)                                        \
    491   return TRAVERSE_STMT_BASE(NAME, CLASS, VAR, Queue);
    492 
    493   // If we have a binary expr, dispatch to the subcode of the binop.  A smart
    494   // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
    495   // below.
    496   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
    497     switch (BinOp->getOpcode()) {
    498 #define OPERATOR(NAME)                                                         \
    499   case BO_##NAME:                                                              \
    500     DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
    501 
    502       BINOP_LIST()
    503 #undef OPERATOR
    504 #undef BINOP_LIST
    505 
    506 #define OPERATOR(NAME)                                                         \
    507   case BO_##NAME##Assign:                                                      \
    508     DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
    509 
    510       CAO_LIST()
    511 #undef OPERATOR
    512 #undef CAO_LIST
    513     }
    514   } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
    515     switch (UnOp->getOpcode()) {
    516 #define OPERATOR(NAME)                                                         \
    517   case UO_##NAME:                                                              \
    518     DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
    519 
    520       UNARYOP_LIST()
    521 #undef OPERATOR
    522 #undef UNARYOP_LIST
    523     }
    524   }
    525 
    526   // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
    527   switch (S->getStmtClass()) {
    528   case Stmt::NoStmtClass:
    529     break;
    530 #define ABSTRACT_STMT(STMT)
    531 #define STMT(CLASS, PARENT)                                                    \
    532   case Stmt::CLASS##Class:                                                     \
    533     DISPATCH_STMT(CLASS, CLASS, S);
    534 #include "clang/AST/StmtNodes.inc"
    535   }
    536 
    537   return true;
    538 }
    539 
    540 #undef DISPATCH_STMT
    541 
    542 template <typename Derived>
    543 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
    544                                                 DataRecursionQueue *Queue) {
    545   if (!S)
    546     return true;
    547 
    548   if (Queue) {
    549     Queue->push_back(S);
    550     return true;
    551   }
    552 
    553   SmallVector<Stmt *, 8> LocalQueue;
    554   LocalQueue.push_back(S);
    555 
    556   while (!LocalQueue.empty()) {
    557     Stmt *CurrS = LocalQueue.pop_back_val();
    558 
    559     size_t N = LocalQueue.size();
    560     TRY_TO(dataTraverseNode(CurrS, &LocalQueue));
    561     // Process new children in the order they were added.
    562     std::reverse(LocalQueue.begin() + N, LocalQueue.end());
    563   }
    564 
    565   return true;
    566 }
    567 
    568 #define DISPATCH(NAME, CLASS, VAR)                                             \
    569   return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))
    570 
    571 template <typename Derived>
    572 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
    573   if (T.isNull())
    574     return true;
    575 
    576   switch (T->getTypeClass()) {
    577 #define ABSTRACT_TYPE(CLASS, BASE)
    578 #define TYPE(CLASS, BASE)                                                      \
    579   case Type::CLASS:                                                            \
    580     DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr()));
    581 #include "clang/AST/TypeNodes.def"
    582   }
    583 
    584   return true;
    585 }
    586 
    587 template <typename Derived>
    588 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
    589   if (TL.isNull())
    590     return true;
    591 
    592   switch (TL.getTypeLocClass()) {
    593 #define ABSTRACT_TYPELOC(CLASS, BASE)
    594 #define TYPELOC(CLASS, BASE)                                                   \
    595   case TypeLoc::CLASS:                                                         \
    596     return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
    597 #include "clang/AST/TypeLocNodes.def"
    598   }
    599 
    600   return true;
    601 }
    602 
    603 // Define the Traverse*Attr(Attr* A) methods
    604 #define VISITORCLASS RecursiveASTVisitor
    605 #include "clang/AST/AttrVisitor.inc"
    606 #undef VISITORCLASS
    607 
    608 template <typename Derived>
    609 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
    610   if (!D)
    611     return true;
    612 
    613   // As a syntax visitor, by default we want to ignore declarations for
    614   // implicit declarations (ones not typed explicitly by the user).
    615   if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
    616     return true;
    617 
    618   switch (D->getKind()) {
    619 #define ABSTRACT_DECL(DECL)
    620 #define DECL(CLASS, BASE)                                                      \
    621   case Decl::CLASS:                                                            \
    622     if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D)))    \
    623       return false;                                                            \
    624     break;
    625 #include "clang/AST/DeclNodes.inc"
    626   }
    627 
    628   // Visit any attributes attached to this declaration.
    629   for (auto *I : D->attrs()) {
    630     if (!getDerived().TraverseAttr(I))
    631       return false;
    632   }
    633   return true;
    634 }
    635 
    636 #undef DISPATCH
    637 
    638 template <typename Derived>
    639 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
    640     NestedNameSpecifier *NNS) {
    641   if (!NNS)
    642     return true;
    643 
    644   if (NNS->getPrefix())
    645     TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
    646 
    647   switch (NNS->getKind()) {
    648   case NestedNameSpecifier::Identifier:
    649   case NestedNameSpecifier::Namespace:
    650   case NestedNameSpecifier::NamespaceAlias:
    651   case NestedNameSpecifier::Global:
    652   case NestedNameSpecifier::Super:
    653     return true;
    654 
    655   case NestedNameSpecifier::TypeSpec:
    656   case NestedNameSpecifier::TypeSpecWithTemplate:
    657     TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
    658   }
    659 
    660   return true;
    661 }
    662 
    663 template <typename Derived>
    664 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
    665     NestedNameSpecifierLoc NNS) {
    666   if (!NNS)
    667     return true;
    668 
    669   if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
    670     TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
    671 
    672   switch (NNS.getNestedNameSpecifier()->getKind()) {
    673   case NestedNameSpecifier::Identifier:
    674   case NestedNameSpecifier::Namespace:
    675   case NestedNameSpecifier::NamespaceAlias:
    676   case NestedNameSpecifier::Global:
    677   case NestedNameSpecifier::Super:
    678     return true;
    679 
    680   case NestedNameSpecifier::TypeSpec:
    681   case NestedNameSpecifier::TypeSpecWithTemplate:
    682     TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
    683     break;
    684   }
    685 
    686   return true;
    687 }
    688 
    689 template <typename Derived>
    690 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
    691     DeclarationNameInfo NameInfo) {
    692   switch (NameInfo.getName().getNameKind()) {
    693   case DeclarationName::CXXConstructorName:
    694   case DeclarationName::CXXDestructorName:
    695   case DeclarationName::CXXConversionFunctionName:
    696     if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
    697       TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
    698 
    699     break;
    700 
    701   case DeclarationName::Identifier:
    702   case DeclarationName::ObjCZeroArgSelector:
    703   case DeclarationName::ObjCOneArgSelector:
    704   case DeclarationName::ObjCMultiArgSelector:
    705   case DeclarationName::CXXOperatorName:
    706   case DeclarationName::CXXLiteralOperatorName:
    707   case DeclarationName::CXXUsingDirective:
    708     break;
    709   }
    710 
    711   return true;
    712 }
    713 
    714 template <typename Derived>
    715 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
    716   if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
    717     TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
    718   else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
    719     TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
    720 
    721   return true;
    722 }
    723 
    724 template <typename Derived>
    725 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
    726     const TemplateArgument &Arg) {
    727   switch (Arg.getKind()) {
    728   case TemplateArgument::Null:
    729   case TemplateArgument::Declaration:
    730   case TemplateArgument::Integral:
    731   case TemplateArgument::NullPtr:
    732     return true;
    733 
    734   case TemplateArgument::Type:
    735     return getDerived().TraverseType(Arg.getAsType());
    736 
    737   case TemplateArgument::Template:
    738   case TemplateArgument::TemplateExpansion:
    739     return getDerived().TraverseTemplateName(
    740         Arg.getAsTemplateOrTemplatePattern());
    741 
    742   case TemplateArgument::Expression:
    743     return getDerived().TraverseStmt(Arg.getAsExpr());
    744 
    745   case TemplateArgument::Pack:
    746     return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
    747                                                   Arg.pack_size());
    748   }
    749 
    750   return true;
    751 }
    752 
    753 // FIXME: no template name location?
    754 // FIXME: no source locations for a template argument pack?
    755 template <typename Derived>
    756 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
    757     const TemplateArgumentLoc &ArgLoc) {
    758   const TemplateArgument &Arg = ArgLoc.getArgument();
    759 
    760   switch (Arg.getKind()) {
    761   case TemplateArgument::Null:
    762   case TemplateArgument::Declaration:
    763   case TemplateArgument::Integral:
    764   case TemplateArgument::NullPtr:
    765     return true;
    766 
    767   case TemplateArgument::Type: {
    768     // FIXME: how can TSI ever be NULL?
    769     if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
    770       return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
    771     else
    772       return getDerived().TraverseType(Arg.getAsType());
    773   }
    774 
    775   case TemplateArgument::Template:
    776   case TemplateArgument::TemplateExpansion:
    777     if (ArgLoc.getTemplateQualifierLoc())
    778       TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
    779           ArgLoc.getTemplateQualifierLoc()));
    780     return getDerived().TraverseTemplateName(
    781         Arg.getAsTemplateOrTemplatePattern());
    782 
    783   case TemplateArgument::Expression:
    784     return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
    785 
    786   case TemplateArgument::Pack:
    787     return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
    788                                                   Arg.pack_size());
    789   }
    790 
    791   return true;
    792 }
    793 
    794 template <typename Derived>
    795 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
    796     const TemplateArgument *Args, unsigned NumArgs) {
    797   for (unsigned I = 0; I != NumArgs; ++I) {
    798     TRY_TO(TraverseTemplateArgument(Args[I]));
    799   }
    800 
    801   return true;
    802 }
    803 
    804 template <typename Derived>
    805 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
    806     CXXCtorInitializer *Init) {
    807   if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
    808     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
    809 
    810   if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
    811     TRY_TO(TraverseStmt(Init->getInit()));
    812   return true;
    813 }
    814 
    815 template <typename Derived>
    816 bool
    817 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
    818                                                     const LambdaCapture *C) {
    819   if (LE->isInitCapture(C))
    820     TRY_TO(TraverseDecl(C->getCapturedVar()));
    821   return true;
    822 }
    823 
    824 template <typename Derived>
    825 bool RecursiveASTVisitor<Derived>::TraverseLambdaBody(
    826     LambdaExpr *LE, DataRecursionQueue *Queue) {
    827   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(LE->getBody());
    828   return true;
    829 }
    830 
    831 // ----------------- Type traversal -----------------
    832 
    833 // This macro makes available a variable T, the passed-in type.
    834 #define DEF_TRAVERSE_TYPE(TYPE, CODE)                                          \
    835   template <typename Derived>                                                  \
    836   bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) {                 \
    837     TRY_TO(WalkUpFrom##TYPE(T));                                               \
    838     { CODE; }                                                                  \
    839     return true;                                                               \
    840   }
    841 
    842 DEF_TRAVERSE_TYPE(BuiltinType, {})
    843 
    844 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
    845 
    846 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
    847 
    848 DEF_TRAVERSE_TYPE(BlockPointerType,
    849                   { TRY_TO(TraverseType(T->getPointeeType())); })
    850 
    851 DEF_TRAVERSE_TYPE(LValueReferenceType,
    852                   { TRY_TO(TraverseType(T->getPointeeType())); })
    853 
    854 DEF_TRAVERSE_TYPE(RValueReferenceType,
    855                   { TRY_TO(TraverseType(T->getPointeeType())); })
    856 
    857 DEF_TRAVERSE_TYPE(MemberPointerType, {
    858   TRY_TO(TraverseType(QualType(T->getClass(), 0)));
    859   TRY_TO(TraverseType(T->getPointeeType()));
    860 })
    861 
    862 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
    863 
    864 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
    865 
    866 DEF_TRAVERSE_TYPE(ConstantArrayType,
    867                   { TRY_TO(TraverseType(T->getElementType())); })
    868 
    869 DEF_TRAVERSE_TYPE(IncompleteArrayType,
    870                   { TRY_TO(TraverseType(T->getElementType())); })
    871 
    872 DEF_TRAVERSE_TYPE(VariableArrayType, {
    873   TRY_TO(TraverseType(T->getElementType()));
    874   TRY_TO(TraverseStmt(T->getSizeExpr()));
    875 })
    876 
    877 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
    878   TRY_TO(TraverseType(T->getElementType()));
    879   if (T->getSizeExpr())
    880     TRY_TO(TraverseStmt(T->getSizeExpr()));
    881 })
    882 
    883 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
    884   if (T->getSizeExpr())
    885     TRY_TO(TraverseStmt(T->getSizeExpr()));
    886   TRY_TO(TraverseType(T->getElementType()));
    887 })
    888 
    889 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
    890 
    891 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
    892 
    893 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
    894                   { TRY_TO(TraverseType(T->getReturnType())); })
    895 
    896 DEF_TRAVERSE_TYPE(FunctionProtoType, {
    897   TRY_TO(TraverseType(T->getReturnType()));
    898 
    899   for (const auto &A : T->param_types()) {
    900     TRY_TO(TraverseType(A));
    901   }
    902 
    903   for (const auto &E : T->exceptions()) {
    904     TRY_TO(TraverseType(E));
    905   }
    906 
    907   if (Expr *NE = T->getNoexceptExpr())
    908     TRY_TO(TraverseStmt(NE));
    909 })
    910 
    911 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
    912 DEF_TRAVERSE_TYPE(TypedefType, {})
    913 
    914 DEF_TRAVERSE_TYPE(TypeOfExprType,
    915                   { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
    916 
    917 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
    918 
    919 DEF_TRAVERSE_TYPE(DecltypeType,
    920                   { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
    921 
    922 DEF_TRAVERSE_TYPE(UnaryTransformType, {
    923   TRY_TO(TraverseType(T->getBaseType()));
    924   TRY_TO(TraverseType(T->getUnderlyingType()));
    925 })
    926 
    927 DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); })
    928 
    929 DEF_TRAVERSE_TYPE(RecordType, {})
    930 DEF_TRAVERSE_TYPE(EnumType, {})
    931 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
    932 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {})
    933 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {})
    934 
    935 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
    936   TRY_TO(TraverseTemplateName(T->getTemplateName()));
    937   TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
    938 })
    939 
    940 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
    941 
    942 DEF_TRAVERSE_TYPE(AttributedType,
    943                   { TRY_TO(TraverseType(T->getModifiedType())); })
    944 
    945 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
    946 
    947 DEF_TRAVERSE_TYPE(ElaboratedType, {
    948   if (T->getQualifier()) {
    949     TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
    950   }
    951   TRY_TO(TraverseType(T->getNamedType()));
    952 })
    953 
    954 DEF_TRAVERSE_TYPE(DependentNameType,
    955                   { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
    956 
    957 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
    958   TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
    959   TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
    960 })
    961 
    962 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
    963 
    964 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
    965 
    966 DEF_TRAVERSE_TYPE(ObjCObjectType, {
    967   // We have to watch out here because an ObjCInterfaceType's base
    968   // type is itself.
    969   if (T->getBaseType().getTypePtr() != T)
    970     TRY_TO(TraverseType(T->getBaseType()));
    971   for (auto typeArg : T->getTypeArgsAsWritten()) {
    972     TRY_TO(TraverseType(typeArg));
    973   }
    974 })
    975 
    976 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
    977                   { TRY_TO(TraverseType(T->getPointeeType())); })
    978 
    979 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
    980 
    981 #undef DEF_TRAVERSE_TYPE
    982 
    983 // ----------------- TypeLoc traversal -----------------
    984 
    985 // This macro makes available a variable TL, the passed-in TypeLoc.
    986 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
    987 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
    988 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
    989 // continue to work.
    990 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE)                                       \
    991   template <typename Derived>                                                  \
    992   bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) {       \
    993     if (getDerived().shouldWalkTypesOfTypeLocs())                              \
    994       TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr())));           \
    995     TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                         \
    996     { CODE; }                                                                  \
    997     return true;                                                               \
    998   }
    999 
   1000 template <typename Derived>
   1001 bool
   1002 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
   1003   // Move this over to the 'main' typeloc tree.  Note that this is a
   1004   // move -- we pretend that we were really looking at the unqualified
   1005   // typeloc all along -- rather than a recursion, so we don't follow
   1006   // the normal CRTP plan of going through
   1007   // getDerived().TraverseTypeLoc.  If we did, we'd be traversing
   1008   // twice for the same type (once as a QualifiedTypeLoc version of
   1009   // the type, once as an UnqualifiedTypeLoc version of the type),
   1010   // which in effect means we'd call VisitTypeLoc twice with the
   1011   // 'same' type.  This solves that problem, at the cost of never
   1012   // seeing the qualified version of the type (unless the client
   1013   // subclasses TraverseQualifiedTypeLoc themselves).  It's not a
   1014   // perfect solution.  A perfect solution probably requires making
   1015   // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
   1016   // wrapper around Type* -- rather than being its own class in the
   1017   // type hierarchy.
   1018   return TraverseTypeLoc(TL.getUnqualifiedLoc());
   1019 }
   1020 
   1021 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
   1022 
   1023 // FIXME: ComplexTypeLoc is unfinished
   1024 DEF_TRAVERSE_TYPELOC(ComplexType, {
   1025   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
   1026 })
   1027 
   1028 DEF_TRAVERSE_TYPELOC(PointerType,
   1029                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
   1030 
   1031 DEF_TRAVERSE_TYPELOC(BlockPointerType,
   1032                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
   1033 
   1034 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
   1035                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
   1036 
   1037 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
   1038                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
   1039 
   1040 // FIXME: location of base class?
   1041 // We traverse this in the type case as well, but how is it not reached through
   1042 // the pointee type?
   1043 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
   1044   TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
   1045   TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
   1046 })
   1047 
   1048 DEF_TRAVERSE_TYPELOC(AdjustedType,
   1049                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
   1050 
   1051 DEF_TRAVERSE_TYPELOC(DecayedType,
   1052                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
   1053 
   1054 template <typename Derived>
   1055 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
   1056   // This isn't available for ArrayType, but is for the ArrayTypeLoc.
   1057   TRY_TO(TraverseStmt(TL.getSizeExpr()));
   1058   return true;
   1059 }
   1060 
   1061 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
   1062   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
   1063   return TraverseArrayTypeLocHelper(TL);
   1064 })
   1065 
   1066 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
   1067   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
   1068   return TraverseArrayTypeLocHelper(TL);
   1069 })
   1070 
   1071 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
   1072   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
   1073   return TraverseArrayTypeLocHelper(TL);
   1074 })
   1075 
   1076 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
   1077   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
   1078   return TraverseArrayTypeLocHelper(TL);
   1079 })
   1080 
   1081 // FIXME: order? why not size expr first?
   1082 // FIXME: base VectorTypeLoc is unfinished
   1083 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
   1084   if (TL.getTypePtr()->getSizeExpr())
   1085     TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
   1086   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
   1087 })
   1088 
   1089 // FIXME: VectorTypeLoc is unfinished
   1090 DEF_TRAVERSE_TYPELOC(VectorType, {
   1091   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
   1092 })
   1093 
   1094 // FIXME: size and attributes
   1095 // FIXME: base VectorTypeLoc is unfinished
   1096 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
   1097   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
   1098 })
   1099 
   1100 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
   1101                      { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
   1102 
   1103 // FIXME: location of exception specifications (attributes?)
   1104 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
   1105   TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
   1106 
   1107   const FunctionProtoType *T = TL.getTypePtr();
   1108 
   1109   for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
   1110     if (TL.getParam(I)) {
   1111       TRY_TO(TraverseDecl(TL.getParam(I)));
   1112     } else if (I < T->getNumParams()) {
   1113       TRY_TO(TraverseType(T->getParamType(I)));
   1114     }
   1115   }
   1116 
   1117   for (const auto &E : T->exceptions()) {
   1118     TRY_TO(TraverseType(E));
   1119   }
   1120 
   1121   if (Expr *NE = T->getNoexceptExpr())
   1122     TRY_TO(TraverseStmt(NE));
   1123 })
   1124 
   1125 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
   1126 DEF_TRAVERSE_TYPELOC(TypedefType, {})
   1127 
   1128 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
   1129                      { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
   1130 
   1131 DEF_TRAVERSE_TYPELOC(TypeOfType, {
   1132   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
   1133 })
   1134 
   1135 // FIXME: location of underlying expr
   1136 DEF_TRAVERSE_TYPELOC(DecltypeType, {
   1137   TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
   1138 })
   1139 
   1140 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
   1141   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
   1142 })
   1143 
   1144 DEF_TRAVERSE_TYPELOC(AutoType, {
   1145   TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
   1146 })
   1147 
   1148 DEF_TRAVERSE_TYPELOC(RecordType, {})
   1149 DEF_TRAVERSE_TYPELOC(EnumType, {})
   1150 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
   1151 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {})
   1152 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {})
   1153 
   1154 // FIXME: use the loc for the template name?
   1155 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
   1156   TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
   1157   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
   1158     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
   1159   }
   1160 })
   1161 
   1162 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
   1163 
   1164 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
   1165 
   1166 DEF_TRAVERSE_TYPELOC(AttributedType,
   1167                      { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
   1168 
   1169 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
   1170   if (TL.getQualifierLoc()) {
   1171     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
   1172   }
   1173   TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
   1174 })
   1175 
   1176 DEF_TRAVERSE_TYPELOC(DependentNameType, {
   1177   TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
   1178 })
   1179 
   1180 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
   1181   if (TL.getQualifierLoc()) {
   1182     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
   1183   }
   1184 
   1185   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
   1186     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
   1187   }
   1188 })
   1189 
   1190 DEF_TRAVERSE_TYPELOC(PackExpansionType,
   1191                      { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
   1192 
   1193 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
   1194 
   1195 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
   1196   // We have to watch out here because an ObjCInterfaceType's base
   1197   // type is itself.
   1198   if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
   1199     TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
   1200   for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
   1201     TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
   1202 })
   1203 
   1204 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
   1205                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
   1206 
   1207 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
   1208 
   1209 #undef DEF_TRAVERSE_TYPELOC
   1210 
   1211 // ----------------- Decl traversal -----------------
   1212 //
   1213 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
   1214 // the children that come from the DeclContext associated with it.
   1215 // Therefore each Traverse* only needs to worry about children other
   1216 // than those.
   1217 
   1218 template <typename Derived>
   1219 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
   1220   if (!DC)
   1221     return true;
   1222 
   1223   for (auto *Child : DC->decls()) {
   1224     // BlockDecls and CapturedDecls are traversed through BlockExprs and
   1225     // CapturedStmts respectively.
   1226     if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child))
   1227       TRY_TO(TraverseDecl(Child));
   1228   }
   1229 
   1230   return true;
   1231 }
   1232 
   1233 // This macro makes available a variable D, the passed-in decl.
   1234 #define DEF_TRAVERSE_DECL(DECL, CODE)                                          \
   1235   template <typename Derived>                                                  \
   1236   bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) {                 \
   1237     TRY_TO(WalkUpFrom##DECL(D));                                               \
   1238     { CODE; }                                                                  \
   1239     TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));               \
   1240     return true;                                                               \
   1241   }
   1242 
   1243 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
   1244 
   1245 DEF_TRAVERSE_DECL(BlockDecl, {
   1246   if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
   1247     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
   1248   TRY_TO(TraverseStmt(D->getBody()));
   1249   for (const auto &I : D->captures()) {
   1250     if (I.hasCopyExpr()) {
   1251       TRY_TO(TraverseStmt(I.getCopyExpr()));
   1252     }
   1253   }
   1254   // This return statement makes sure the traversal of nodes in
   1255   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
   1256   // is skipped - don't remove it.
   1257   return true;
   1258 })
   1259 
   1260 DEF_TRAVERSE_DECL(CapturedDecl, {
   1261   TRY_TO(TraverseStmt(D->getBody()));
   1262   // This return statement makes sure the traversal of nodes in
   1263   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
   1264   // is skipped - don't remove it.
   1265   return true;
   1266 })
   1267 
   1268 DEF_TRAVERSE_DECL(EmptyDecl, {})
   1269 
   1270 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
   1271                   { TRY_TO(TraverseStmt(D->getAsmString())); })
   1272 
   1273 DEF_TRAVERSE_DECL(ImportDecl, {})
   1274 
   1275 DEF_TRAVERSE_DECL(FriendDecl, {
   1276   // Friend is either decl or a type.
   1277   if (D->getFriendType())
   1278     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
   1279   else
   1280     TRY_TO(TraverseDecl(D->getFriendDecl()));
   1281 })
   1282 
   1283 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
   1284   if (D->getFriendType())
   1285     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
   1286   else
   1287     TRY_TO(TraverseDecl(D->getFriendDecl()));
   1288   for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
   1289     TemplateParameterList *TPL = D->getTemplateParameterList(I);
   1290     for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
   1291          ITPL != ETPL; ++ITPL) {
   1292       TRY_TO(TraverseDecl(*ITPL));
   1293     }
   1294   }
   1295 })
   1296 
   1297 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
   1298   TRY_TO(TraverseDecl(D->getSpecialization()));
   1299 
   1300   if (D->hasExplicitTemplateArgs()) {
   1301     const TemplateArgumentListInfo &args = D->templateArgs();
   1302     TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
   1303                                               args.size()));
   1304   }
   1305 })
   1306 
   1307 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
   1308 
   1309 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
   1310                                         })
   1311 
   1312 DEF_TRAVERSE_DECL(StaticAssertDecl, {
   1313   TRY_TO(TraverseStmt(D->getAssertExpr()));
   1314   TRY_TO(TraverseStmt(D->getMessage()));
   1315 })
   1316 
   1317 DEF_TRAVERSE_DECL(
   1318     TranslationUnitDecl,
   1319     {// Code in an unnamed namespace shows up automatically in
   1320      // decls_begin()/decls_end().  Thus we don't need to recurse on
   1321      // D->getAnonymousNamespace().
   1322     })
   1323 
   1324 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
   1325 
   1326 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
   1327   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1328 
   1329   // We shouldn't traverse an aliased namespace, since it will be
   1330   // defined (and, therefore, traversed) somewhere else.
   1331   //
   1332   // This return statement makes sure the traversal of nodes in
   1333   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
   1334   // is skipped - don't remove it.
   1335   return true;
   1336 })
   1337 
   1338 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
   1339                              })
   1340 
   1341 DEF_TRAVERSE_DECL(
   1342     NamespaceDecl,
   1343     {// Code in an unnamed namespace shows up automatically in
   1344      // decls_begin()/decls_end().  Thus we don't need to recurse on
   1345      // D->getAnonymousNamespace().
   1346     })
   1347 
   1348 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
   1349                                            })
   1350 
   1351 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
   1352   if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
   1353     for (auto typeParam : *typeParamList) {
   1354       TRY_TO(TraverseObjCTypeParamDecl(typeParam));
   1355     }
   1356   }
   1357 })
   1358 
   1359 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
   1360                                         })
   1361 
   1362 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
   1363                                           })
   1364 
   1365 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
   1366   if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
   1367     for (auto typeParam : *typeParamList) {
   1368       TRY_TO(TraverseObjCTypeParamDecl(typeParam));
   1369     }
   1370   }
   1371 
   1372   if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
   1373     TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
   1374   }
   1375 })
   1376 
   1377 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
   1378                                     })
   1379 
   1380 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
   1381   if (D->getReturnTypeSourceInfo()) {
   1382     TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
   1383   }
   1384   for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
   1385        I != E; ++I) {
   1386     TRY_TO(TraverseDecl(*I));
   1387   }
   1388   if (D->isThisDeclarationADefinition()) {
   1389     TRY_TO(TraverseStmt(D->getBody()));
   1390   }
   1391   return true;
   1392 })
   1393 
   1394 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
   1395   if (D->hasExplicitBound()) {
   1396     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
   1397     // We shouldn't traverse D->getTypeForDecl(); it's a result of
   1398     // declaring the type alias, not something that was written in the
   1399     // source.
   1400   }
   1401 })
   1402 
   1403 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
   1404   if (D->getTypeSourceInfo())
   1405     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
   1406   else
   1407     TRY_TO(TraverseType(D->getType()));
   1408   return true;
   1409 })
   1410 
   1411 DEF_TRAVERSE_DECL(UsingDecl, {
   1412   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1413   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
   1414 })
   1415 
   1416 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
   1417   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1418 })
   1419 
   1420 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
   1421 
   1422 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
   1423   for (auto *I : D->varlists()) {
   1424     TRY_TO(TraverseStmt(I));
   1425   }
   1426 })
   1427 
   1428 // A helper method for TemplateDecl's children.
   1429 template <typename Derived>
   1430 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
   1431     TemplateParameterList *TPL) {
   1432   if (TPL) {
   1433     for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
   1434          I != E; ++I) {
   1435       TRY_TO(TraverseDecl(*I));
   1436     }
   1437   }
   1438   return true;
   1439 }
   1440 
   1441 template <typename Derived>
   1442 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
   1443     ClassTemplateDecl *D) {
   1444   for (auto *SD : D->specializations()) {
   1445     for (auto *RD : SD->redecls()) {
   1446       // We don't want to visit injected-class-names in this traversal.
   1447       if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
   1448         continue;
   1449 
   1450       switch (
   1451           cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
   1452       // Visit the implicit instantiations with the requested pattern.
   1453       case TSK_Undeclared:
   1454       case TSK_ImplicitInstantiation:
   1455         TRY_TO(TraverseDecl(RD));
   1456         break;
   1457 
   1458       // We don't need to do anything on an explicit instantiation
   1459       // or explicit specialization because there will be an explicit
   1460       // node for it elsewhere.
   1461       case TSK_ExplicitInstantiationDeclaration:
   1462       case TSK_ExplicitInstantiationDefinition:
   1463       case TSK_ExplicitSpecialization:
   1464         break;
   1465       }
   1466     }
   1467   }
   1468 
   1469   return true;
   1470 }
   1471 
   1472 template <typename Derived>
   1473 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
   1474     VarTemplateDecl *D) {
   1475   for (auto *SD : D->specializations()) {
   1476     for (auto *RD : SD->redecls()) {
   1477       switch (
   1478           cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
   1479       case TSK_Undeclared:
   1480       case TSK_ImplicitInstantiation:
   1481         TRY_TO(TraverseDecl(RD));
   1482         break;
   1483 
   1484       case TSK_ExplicitInstantiationDeclaration:
   1485       case TSK_ExplicitInstantiationDefinition:
   1486       case TSK_ExplicitSpecialization:
   1487         break;
   1488       }
   1489     }
   1490   }
   1491 
   1492   return true;
   1493 }
   1494 
   1495 // A helper method for traversing the instantiations of a
   1496 // function while skipping its specializations.
   1497 template <typename Derived>
   1498 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
   1499     FunctionTemplateDecl *D) {
   1500   for (auto *FD : D->specializations()) {
   1501     for (auto *RD : FD->redecls()) {
   1502       switch (RD->getTemplateSpecializationKind()) {
   1503       case TSK_Undeclared:
   1504       case TSK_ImplicitInstantiation:
   1505         // We don't know what kind of FunctionDecl this is.
   1506         TRY_TO(TraverseDecl(RD));
   1507         break;
   1508 
   1509       // FIXME: For now traverse explicit instantiations here. Change that
   1510       // once they are represented as dedicated nodes in the AST.
   1511       case TSK_ExplicitInstantiationDeclaration:
   1512       case TSK_ExplicitInstantiationDefinition:
   1513         TRY_TO(TraverseDecl(RD));
   1514         break;
   1515 
   1516       case TSK_ExplicitSpecialization:
   1517         break;
   1518       }
   1519     }
   1520   }
   1521 
   1522   return true;
   1523 }
   1524 
   1525 // This macro unifies the traversal of class, variable and function
   1526 // template declarations.
   1527 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND)                                   \
   1528   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, {                              \
   1529     TRY_TO(TraverseDecl(D->getTemplatedDecl()));                               \
   1530     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
   1531                                                                                \
   1532     /* By default, we do not traverse the instantiations of                    \
   1533        class templates since they do not appear in the user code. The          \
   1534        following code optionally traverses them.                               \
   1535                                                                                \
   1536        We only traverse the class instantiations when we see the canonical     \
   1537        declaration of the template, to ensure we only visit them once. */      \
   1538     if (getDerived().shouldVisitTemplateInstantiations() &&                    \
   1539         D == D->getCanonicalDecl())                                            \
   1540       TRY_TO(TraverseTemplateInstantiations(D));                               \
   1541                                                                                \
   1542     /* Note that getInstantiatedFromMemberTemplate() is just a link            \
   1543        from a template instantiation back to the template from which           \
   1544        it was instantiated, and thus should not be traversed. */               \
   1545   })
   1546 
   1547 DEF_TRAVERSE_TMPL_DECL(Class)
   1548 DEF_TRAVERSE_TMPL_DECL(Var)
   1549 DEF_TRAVERSE_TMPL_DECL(Function)
   1550 
   1551 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
   1552   // D is the "T" in something like
   1553   //   template <template <typename> class T> class container { };
   1554   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
   1555   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
   1556     TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
   1557   }
   1558   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
   1559 })
   1560 
   1561 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
   1562   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
   1563 })
   1564 
   1565 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
   1566   // D is the "T" in something like "template<typename T> class vector;"
   1567   if (D->getTypeForDecl())
   1568     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
   1569   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
   1570     TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
   1571 })
   1572 
   1573 DEF_TRAVERSE_DECL(TypedefDecl, {
   1574   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
   1575   // We shouldn't traverse D->getTypeForDecl(); it's a result of
   1576   // declaring the typedef, not something that was written in the
   1577   // source.
   1578 })
   1579 
   1580 DEF_TRAVERSE_DECL(TypeAliasDecl, {
   1581   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
   1582   // We shouldn't traverse D->getTypeForDecl(); it's a result of
   1583   // declaring the type alias, not something that was written in the
   1584   // source.
   1585 })
   1586 
   1587 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
   1588   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
   1589   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
   1590 })
   1591 
   1592 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
   1593   // A dependent using declaration which was marked with 'typename'.
   1594   //   template<class T> class A : public B<T> { using typename B<T>::foo; };
   1595   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1596   // We shouldn't traverse D->getTypeForDecl(); it's a result of
   1597   // declaring the type, not something that was written in the
   1598   // source.
   1599 })
   1600 
   1601 DEF_TRAVERSE_DECL(EnumDecl, {
   1602   if (D->getTypeForDecl())
   1603     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
   1604 
   1605   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1606   // The enumerators are already traversed by
   1607   // decls_begin()/decls_end().
   1608 })
   1609 
   1610 // Helper methods for RecordDecl and its children.
   1611 template <typename Derived>
   1612 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
   1613   // We shouldn't traverse D->getTypeForDecl(); it's a result of
   1614   // declaring the type, not something that was written in the source.
   1615 
   1616   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1617   return true;
   1618 }
   1619 
   1620 template <typename Derived>
   1621 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
   1622   if (!TraverseRecordHelper(D))
   1623     return false;
   1624   if (D->isCompleteDefinition()) {
   1625     for (const auto &I : D->bases()) {
   1626       TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
   1627     }
   1628     // We don't traverse the friends or the conversions, as they are
   1629     // already in decls_begin()/decls_end().
   1630   }
   1631   return true;
   1632 }
   1633 
   1634 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
   1635 
   1636 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
   1637 
   1638 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND)                              \
   1639   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
   1640     /* For implicit instantiations ("set<int> x;"), we don't want to           \
   1641        recurse at all, since the instatiated template isn't written in         \
   1642        the source code anywhere.  (Note the instatiated *type* --              \
   1643        set<int> -- is written, and will still get a callback of                \
   1644        TemplateSpecializationType).  For explicit instantiations               \
   1645        ("template set<int>;"), we do need a callback, since this               \
   1646        is the only callback that's made for this instantiation.                \
   1647        We use getTypeAsWritten() to distinguish. */                            \
   1648     if (TypeSourceInfo *TSI = D->getTypeAsWritten())                           \
   1649       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
   1650                                                                                \
   1651     if (!getDerived().shouldVisitTemplateInstantiations() &&                   \
   1652         D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)      \
   1653       /* Returning from here skips traversing the                              \
   1654          declaration context of the *TemplateSpecializationDecl                \
   1655          (embedded in the DEF_TRAVERSE_DECL() macro)                           \
   1656          which contains the instantiated members of the template. */           \
   1657       return true;                                                             \
   1658   })
   1659 
   1660 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
   1661 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
   1662 
   1663 template <typename Derived>
   1664 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
   1665     const TemplateArgumentLoc *TAL, unsigned Count) {
   1666   for (unsigned I = 0; I < Count; ++I) {
   1667     TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
   1668   }
   1669   return true;
   1670 }
   1671 
   1672 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
   1673   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
   1674     /* The partial specialization. */                                          \
   1675     if (TemplateParameterList *TPL = D->getTemplateParameters()) {             \
   1676       for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();   \
   1677            I != E; ++I) {                                                      \
   1678         TRY_TO(TraverseDecl(*I));                                              \
   1679       }                                                                        \
   1680     }                                                                          \
   1681     /* The args that remains unspecialized. */                                 \
   1682     TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
   1683         D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
   1684         D->getTemplateArgsAsWritten()->NumTemplateArgs));                      \
   1685                                                                                \
   1686     /* Don't need the *TemplatePartialSpecializationHelper, even               \
   1687        though that's our parent class -- we already visit all the              \
   1688        template args here. */                                                  \
   1689     TRY_TO(Traverse##DECLKIND##Helper(D));                                     \
   1690                                                                                \
   1691     /* Instantiations will have been visited with the primary template. */     \
   1692   })
   1693 
   1694 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
   1695 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
   1696 
   1697 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
   1698 
   1699 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
   1700   // Like UnresolvedUsingTypenameDecl, but without the 'typename':
   1701   //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
   1702   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1703   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
   1704 })
   1705 
   1706 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
   1707 
   1708 template <typename Derived>
   1709 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
   1710   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1711   if (D->getTypeSourceInfo())
   1712     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
   1713   else
   1714     TRY_TO(TraverseType(D->getType()));
   1715   return true;
   1716 }
   1717 
   1718 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
   1719 
   1720 DEF_TRAVERSE_DECL(FieldDecl, {
   1721   TRY_TO(TraverseDeclaratorHelper(D));
   1722   if (D->isBitField())
   1723     TRY_TO(TraverseStmt(D->getBitWidth()));
   1724   else if (D->hasInClassInitializer())
   1725     TRY_TO(TraverseStmt(D->getInClassInitializer()));
   1726 })
   1727 
   1728 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
   1729   TRY_TO(TraverseDeclaratorHelper(D));
   1730   if (D->isBitField())
   1731     TRY_TO(TraverseStmt(D->getBitWidth()));
   1732   // FIXME: implement the rest.
   1733 })
   1734 
   1735 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
   1736   TRY_TO(TraverseDeclaratorHelper(D));
   1737   if (D->isBitField())
   1738     TRY_TO(TraverseStmt(D->getBitWidth()));
   1739   // FIXME: implement the rest.
   1740 })
   1741 
   1742 template <typename Derived>
   1743 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
   1744   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   1745   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
   1746 
   1747   // If we're an explicit template specialization, iterate over the
   1748   // template args that were explicitly specified.  If we were doing
   1749   // this in typing order, we'd do it between the return type and
   1750   // the function args, but both are handled by the FunctionTypeLoc
   1751   // above, so we have to choose one side.  I've decided to do before.
   1752   if (const FunctionTemplateSpecializationInfo *FTSI =
   1753           D->getTemplateSpecializationInfo()) {
   1754     if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
   1755         FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
   1756       // A specialization might not have explicit template arguments if it has
   1757       // a templated return type and concrete arguments.
   1758       if (const ASTTemplateArgumentListInfo *TALI =
   1759               FTSI->TemplateArgumentsAsWritten) {
   1760         TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
   1761                                                   TALI->NumTemplateArgs));
   1762       }
   1763     }
   1764   }
   1765 
   1766   // Visit the function type itself, which can be either
   1767   // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
   1768   // also covers the return type and the function parameters,
   1769   // including exception specifications.
   1770   if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
   1771     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
   1772   } else if (getDerived().shouldVisitImplicitCode()) {
   1773     // Visit parameter variable declarations of the implicit function
   1774     // if the traverser is visiting implicit code. Parameter variable
   1775     // declarations do not have valid TypeSourceInfo, so to visit them
   1776     // we need to traverse the declarations explicitly.
   1777     for (FunctionDecl::param_const_iterator I = D->param_begin(),
   1778                                             E = D->param_end();
   1779          I != E; ++I)
   1780       TRY_TO(TraverseDecl(*I));
   1781   }
   1782 
   1783   if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
   1784     // Constructor initializers.
   1785     for (auto *I : Ctor->inits()) {
   1786       TRY_TO(TraverseConstructorInitializer(I));
   1787     }
   1788   }
   1789 
   1790   if (D->isThisDeclarationADefinition()) {
   1791     TRY_TO(TraverseStmt(D->getBody())); // Function body.
   1792   }
   1793   return true;
   1794 }
   1795 
   1796 DEF_TRAVERSE_DECL(FunctionDecl, {
   1797   // We skip decls_begin/decls_end, which are already covered by
   1798   // TraverseFunctionHelper().
   1799   return TraverseFunctionHelper(D);
   1800 })
   1801 
   1802 DEF_TRAVERSE_DECL(CXXMethodDecl, {
   1803   // We skip decls_begin/decls_end, which are already covered by
   1804   // TraverseFunctionHelper().
   1805   return TraverseFunctionHelper(D);
   1806 })
   1807 
   1808 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
   1809   // We skip decls_begin/decls_end, which are already covered by
   1810   // TraverseFunctionHelper().
   1811   return TraverseFunctionHelper(D);
   1812 })
   1813 
   1814 // CXXConversionDecl is the declaration of a type conversion operator.
   1815 // It's not a cast expression.
   1816 DEF_TRAVERSE_DECL(CXXConversionDecl, {
   1817   // We skip decls_begin/decls_end, which are already covered by
   1818   // TraverseFunctionHelper().
   1819   return TraverseFunctionHelper(D);
   1820 })
   1821 
   1822 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
   1823   // We skip decls_begin/decls_end, which are already covered by
   1824   // TraverseFunctionHelper().
   1825   return TraverseFunctionHelper(D);
   1826 })
   1827 
   1828 template <typename Derived>
   1829 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
   1830   TRY_TO(TraverseDeclaratorHelper(D));
   1831   // Default params are taken care of when we traverse the ParmVarDecl.
   1832   if (!isa<ParmVarDecl>(D) &&
   1833       (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
   1834     TRY_TO(TraverseStmt(D->getInit()));
   1835   return true;
   1836 }
   1837 
   1838 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
   1839 
   1840 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
   1841 
   1842 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
   1843   // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
   1844   TRY_TO(TraverseDeclaratorHelper(D));
   1845   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
   1846     TRY_TO(TraverseStmt(D->getDefaultArgument()));
   1847 })
   1848 
   1849 DEF_TRAVERSE_DECL(ParmVarDecl, {
   1850   TRY_TO(TraverseVarHelper(D));
   1851 
   1852   if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
   1853       !D->hasUnparsedDefaultArg())
   1854     TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
   1855 
   1856   if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
   1857       !D->hasUnparsedDefaultArg())
   1858     TRY_TO(TraverseStmt(D->getDefaultArg()));
   1859 })
   1860 
   1861 #undef DEF_TRAVERSE_DECL
   1862 
   1863 // ----------------- Stmt traversal -----------------
   1864 //
   1865 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
   1866 // over the children defined in children() (every stmt defines these,
   1867 // though sometimes the range is empty).  Each individual Traverse*
   1868 // method only needs to worry about children other than those.  To see
   1869 // what children() does for a given class, see, e.g.,
   1870 //   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
   1871 
   1872 // This macro makes available a variable S, the passed-in stmt.
   1873 #define DEF_TRAVERSE_STMT(STMT, CODE)                                          \
   1874   template <typename Derived>                                                  \
   1875   bool RecursiveASTVisitor<Derived>::Traverse##STMT(                           \
   1876       STMT *S, DataRecursionQueue *Queue) {                                    \
   1877     TRY_TO(WalkUpFrom##STMT(S));                                               \
   1878     { CODE; }                                                                  \
   1879     for (Stmt *SubStmt : S->children()) {                                      \
   1880       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);                                          \
   1881     }                                                                          \
   1882     return true;                                                               \
   1883   }
   1884 
   1885 DEF_TRAVERSE_STMT(GCCAsmStmt, {
   1886   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
   1887   for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
   1888     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
   1889   }
   1890   for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
   1891     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
   1892   }
   1893   for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
   1894     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
   1895   }
   1896   // children() iterates over inputExpr and outputExpr.
   1897 })
   1898 
   1899 DEF_TRAVERSE_STMT(
   1900     MSAsmStmt,
   1901     {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
   1902      // added this needs to be implemented.
   1903     })
   1904 
   1905 DEF_TRAVERSE_STMT(CXXCatchStmt, {
   1906   TRY_TO(TraverseDecl(S->getExceptionDecl()));
   1907   // children() iterates over the handler block.
   1908 })
   1909 
   1910 DEF_TRAVERSE_STMT(DeclStmt, {
   1911   for (auto *I : S->decls()) {
   1912     TRY_TO(TraverseDecl(I));
   1913   }
   1914   // Suppress the default iteration over children() by
   1915   // returning.  Here's why: A DeclStmt looks like 'type var [=
   1916   // initializer]'.  The decls above already traverse over the
   1917   // initializers, so we don't have to do it again (which
   1918   // children() would do).
   1919   return true;
   1920 })
   1921 
   1922 // These non-expr stmts (most of them), do not need any action except
   1923 // iterating over the children.
   1924 DEF_TRAVERSE_STMT(BreakStmt, {})
   1925 DEF_TRAVERSE_STMT(CXXTryStmt, {})
   1926 DEF_TRAVERSE_STMT(CaseStmt, {})
   1927 DEF_TRAVERSE_STMT(CompoundStmt, {})
   1928 DEF_TRAVERSE_STMT(ContinueStmt, {})
   1929 DEF_TRAVERSE_STMT(DefaultStmt, {})
   1930 DEF_TRAVERSE_STMT(DoStmt, {})
   1931 DEF_TRAVERSE_STMT(ForStmt, {})
   1932 DEF_TRAVERSE_STMT(GotoStmt, {})
   1933 DEF_TRAVERSE_STMT(IfStmt, {})
   1934 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
   1935 DEF_TRAVERSE_STMT(LabelStmt, {})
   1936 DEF_TRAVERSE_STMT(AttributedStmt, {})
   1937 DEF_TRAVERSE_STMT(NullStmt, {})
   1938 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
   1939 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
   1940 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
   1941 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
   1942 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
   1943 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
   1944 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
   1945 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
   1946   if (!getDerived().shouldVisitImplicitCode()) {
   1947     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
   1948     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
   1949     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
   1950     // Visit everything else only if shouldVisitImplicitCode().
   1951     return true;
   1952   }
   1953 })
   1954 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
   1955   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   1956   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
   1957 })
   1958 DEF_TRAVERSE_STMT(ReturnStmt, {})
   1959 DEF_TRAVERSE_STMT(SwitchStmt, {})
   1960 DEF_TRAVERSE_STMT(WhileStmt, {})
   1961 
   1962 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
   1963   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   1964   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
   1965   if (S->hasExplicitTemplateArgs()) {
   1966     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
   1967                                               S->getNumTemplateArgs()));
   1968   }
   1969 })
   1970 
   1971 DEF_TRAVERSE_STMT(DeclRefExpr, {
   1972   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   1973   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
   1974   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
   1975                                             S->getNumTemplateArgs()));
   1976 })
   1977 
   1978 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
   1979   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   1980   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
   1981   if (S->hasExplicitTemplateArgs()) {
   1982     TRY_TO(TraverseTemplateArgumentLocsHelper(
   1983         S->getExplicitTemplateArgs().getTemplateArgs(),
   1984         S->getNumTemplateArgs()));
   1985   }
   1986 })
   1987 
   1988 DEF_TRAVERSE_STMT(MemberExpr, {
   1989   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   1990   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
   1991   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
   1992                                             S->getNumTemplateArgs()));
   1993 })
   1994 
   1995 DEF_TRAVERSE_STMT(
   1996     ImplicitCastExpr,
   1997     {// We don't traverse the cast type, as it's not written in the
   1998      // source code.
   1999     })
   2000 
   2001 DEF_TRAVERSE_STMT(CStyleCastExpr, {
   2002   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2003 })
   2004 
   2005 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
   2006   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2007 })
   2008 
   2009 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
   2010   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2011 })
   2012 
   2013 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
   2014   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2015 })
   2016 
   2017 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
   2018   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2019 })
   2020 
   2021 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
   2022   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2023 })
   2024 
   2025 template <typename Derived>
   2026 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
   2027     InitListExpr *S, DataRecursionQueue *Queue) {
   2028   if (S) {
   2029     TRY_TO(WalkUpFromInitListExpr(S));
   2030     // All we need are the default actions.  FIXME: use a helper function.
   2031     for (Stmt *SubStmt : S->children()) {
   2032       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
   2033     }
   2034   }
   2035   return true;
   2036 }
   2037 
   2038 // This method is called once for each pair of syntactic and semantic
   2039 // InitListExpr, and it traverses the subtrees defined by the two forms. This
   2040 // may cause some of the children to be visited twice, if they appear both in
   2041 // the syntactic and the semantic form.
   2042 //
   2043 // There is no guarantee about which form \p S takes when this method is called.
   2044 DEF_TRAVERSE_STMT(InitListExpr, {
   2045   TRY_TO(TraverseSynOrSemInitListExpr(
   2046       S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
   2047   TRY_TO(TraverseSynOrSemInitListExpr(
   2048       S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
   2049   return true;
   2050 })
   2051 
   2052 // GenericSelectionExpr is a special case because the types and expressions
   2053 // are interleaved.  We also need to watch out for null types (default
   2054 // generic associations).
   2055 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
   2056   TRY_TO(TraverseStmt(S->getControllingExpr()));
   2057   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
   2058     if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
   2059       TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
   2060     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAssocExpr(i));
   2061   }
   2062   return true;
   2063 })
   2064 
   2065 // PseudoObjectExpr is a special case because of the weirdness with
   2066 // syntactic expressions and opaque values.
   2067 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
   2068   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
   2069   for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
   2070                                             e = S->semantics_end();
   2071        i != e; ++i) {
   2072     Expr *sub = *i;
   2073     if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
   2074       sub = OVE->getSourceExpr();
   2075     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
   2076   }
   2077   return true;
   2078 })
   2079 
   2080 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
   2081   // This is called for code like 'return T()' where T is a built-in
   2082   // (i.e. non-class) type.
   2083   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
   2084 })
   2085 
   2086 DEF_TRAVERSE_STMT(CXXNewExpr, {
   2087   // The child-iterator will pick up the other arguments.
   2088   TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
   2089 })
   2090 
   2091 DEF_TRAVERSE_STMT(OffsetOfExpr, {
   2092   // The child-iterator will pick up the expression representing
   2093   // the field.
   2094   // FIMXE: for code like offsetof(Foo, a.b.c), should we get
   2095   // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
   2096   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
   2097 })
   2098 
   2099 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
   2100   // The child-iterator will pick up the arg if it's an expression,
   2101   // but not if it's a type.
   2102   if (S->isArgumentType())
   2103     TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
   2104 })
   2105 
   2106 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
   2107   // The child-iterator will pick up the arg if it's an expression,
   2108   // but not if it's a type.
   2109   if (S->isTypeOperand())
   2110     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
   2111 })
   2112 
   2113 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
   2114   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   2115 })
   2116 
   2117 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
   2118 
   2119 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
   2120   // The child-iterator will pick up the arg if it's an expression,
   2121   // but not if it's a type.
   2122   if (S->isTypeOperand())
   2123     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
   2124 })
   2125 
   2126 DEF_TRAVERSE_STMT(TypeTraitExpr, {
   2127   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
   2128     TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
   2129 })
   2130 
   2131 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
   2132   TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
   2133 })
   2134 
   2135 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
   2136                   { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
   2137 
   2138 DEF_TRAVERSE_STMT(VAArgExpr, {
   2139   // The child-iterator will pick up the expression argument.
   2140   TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
   2141 })
   2142 
   2143 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
   2144   // This is called for code like 'return T()' where T is a class type.
   2145   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
   2146 })
   2147 
   2148 // Walk only the visible parts of lambda expressions.
   2149 DEF_TRAVERSE_STMT(LambdaExpr, {
   2150   for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
   2151                                     CEnd = S->explicit_capture_end();
   2152        C != CEnd; ++C) {
   2153     TRY_TO(TraverseLambdaCapture(S, C));
   2154   }
   2155 
   2156   TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
   2157   FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
   2158 
   2159   if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
   2160     // Visit the whole type.
   2161     TRY_TO(TraverseTypeLoc(TL));
   2162   } else {
   2163     if (S->hasExplicitParameters()) {
   2164       // Visit parameters.
   2165       for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
   2166         TRY_TO(TraverseDecl(Proto.getParam(I)));
   2167       }
   2168     } else if (S->hasExplicitResultType()) {
   2169       TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
   2170     }
   2171 
   2172     auto *T = Proto.getTypePtr();
   2173     for (const auto &E : T->exceptions()) {
   2174       TRY_TO(TraverseType(E));
   2175     }
   2176 
   2177     if (Expr *NE = T->getNoexceptExpr())
   2178       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
   2179   }
   2180 
   2181   return TRAVERSE_STMT_BASE(LambdaBody, LambdaExpr, S, Queue);
   2182 })
   2183 
   2184 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
   2185   // This is called for code like 'T()', where T is a template argument.
   2186   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
   2187 })
   2188 
   2189 // These expressions all might take explicit template arguments.
   2190 // We traverse those if so.  FIXME: implement these.
   2191 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
   2192 DEF_TRAVERSE_STMT(CallExpr, {})
   2193 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
   2194 
   2195 // These exprs (most of them), do not need any action except iterating
   2196 // over the children.
   2197 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
   2198 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
   2199 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
   2200 DEF_TRAVERSE_STMT(BlockExpr, {
   2201   TRY_TO(TraverseDecl(S->getBlockDecl()));
   2202   return true; // no child statements to loop through.
   2203 })
   2204 DEF_TRAVERSE_STMT(ChooseExpr, {})
   2205 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
   2206   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
   2207 })
   2208 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
   2209 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
   2210 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
   2211 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
   2212 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
   2213 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
   2214 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
   2215 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
   2216 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
   2217   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   2218   if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
   2219     TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
   2220   if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
   2221     TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
   2222 })
   2223 DEF_TRAVERSE_STMT(CXXThisExpr, {})
   2224 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
   2225 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
   2226 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
   2227 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
   2228 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
   2229 DEF_TRAVERSE_STMT(GNUNullExpr, {})
   2230 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
   2231 DEF_TRAVERSE_STMT(NoInitExpr, {})
   2232 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
   2233 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
   2234   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
   2235     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
   2236 })
   2237 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
   2238 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
   2239 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
   2240   if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
   2241     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
   2242 })
   2243 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
   2244 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
   2245 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
   2246 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
   2247 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
   2248 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
   2249   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
   2250 })
   2251 DEF_TRAVERSE_STMT(ParenExpr, {})
   2252 DEF_TRAVERSE_STMT(ParenListExpr, {})
   2253 DEF_TRAVERSE_STMT(PredefinedExpr, {})
   2254 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
   2255 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
   2256 DEF_TRAVERSE_STMT(StmtExpr, {})
   2257 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
   2258   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   2259   if (S->hasExplicitTemplateArgs()) {
   2260     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
   2261                                               S->getNumTemplateArgs()));
   2262   }
   2263 })
   2264 
   2265 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
   2266   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
   2267   if (S->hasExplicitTemplateArgs()) {
   2268     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
   2269                                               S->getNumTemplateArgs()));
   2270   }
   2271 })
   2272 
   2273 DEF_TRAVERSE_STMT(SEHTryStmt, {})
   2274 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
   2275 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
   2276 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
   2277 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
   2278 
   2279 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
   2280 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
   2281 DEF_TRAVERSE_STMT(TypoExpr, {})
   2282 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
   2283 
   2284 // These operators (all of them) do not need any action except
   2285 // iterating over the children.
   2286 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
   2287 DEF_TRAVERSE_STMT(ConditionalOperator, {})
   2288 DEF_TRAVERSE_STMT(UnaryOperator, {})
   2289 DEF_TRAVERSE_STMT(BinaryOperator, {})
   2290 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
   2291 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
   2292 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
   2293 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
   2294 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
   2295 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
   2296 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
   2297 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
   2298 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
   2299 DEF_TRAVERSE_STMT(AtomicExpr, {})
   2300 
   2301 // For coroutines expressions, traverse either the operand
   2302 // as written or the implied calls, depending on what the
   2303 // derived class requests.
   2304 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
   2305   if (!getDerived().shouldVisitImplicitCode()) {
   2306     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
   2307     return true;
   2308   }
   2309 })
   2310 DEF_TRAVERSE_STMT(CoreturnStmt, {
   2311   if (!getDerived().shouldVisitImplicitCode()) {
   2312     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
   2313     return true;
   2314   }
   2315 })
   2316 DEF_TRAVERSE_STMT(CoawaitExpr, {
   2317   if (!getDerived().shouldVisitImplicitCode()) {
   2318     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
   2319     return true;
   2320   }
   2321 })
   2322 DEF_TRAVERSE_STMT(CoyieldExpr, {
   2323   if (!getDerived().shouldVisitImplicitCode()) {
   2324     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
   2325     return true;
   2326   }
   2327 })
   2328 
   2329 // These literals (all of them) do not need any action.
   2330 DEF_TRAVERSE_STMT(IntegerLiteral, {})
   2331 DEF_TRAVERSE_STMT(CharacterLiteral, {})
   2332 DEF_TRAVERSE_STMT(FloatingLiteral, {})
   2333 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
   2334 DEF_TRAVERSE_STMT(StringLiteral, {})
   2335 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
   2336 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
   2337 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
   2338 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
   2339 
   2340 // Traverse OpenCL: AsType, Convert.
   2341 DEF_TRAVERSE_STMT(AsTypeExpr, {})
   2342 
   2343 // OpenMP directives.
   2344 template <typename Derived>
   2345 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
   2346     OMPExecutableDirective *S) {
   2347   for (auto *C : S->clauses()) {
   2348     TRY_TO(TraverseOMPClause(C));
   2349   }
   2350   return true;
   2351 }
   2352 
   2353 template <typename Derived>
   2354 bool
   2355 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
   2356   return TraverseOMPExecutableDirective(S);
   2357 }
   2358 
   2359 DEF_TRAVERSE_STMT(OMPParallelDirective,
   2360                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2361 
   2362 DEF_TRAVERSE_STMT(OMPSimdDirective,
   2363                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2364 
   2365 DEF_TRAVERSE_STMT(OMPForDirective,
   2366                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2367 
   2368 DEF_TRAVERSE_STMT(OMPForSimdDirective,
   2369                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2370 
   2371 DEF_TRAVERSE_STMT(OMPSectionsDirective,
   2372                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2373 
   2374 DEF_TRAVERSE_STMT(OMPSectionDirective,
   2375                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2376 
   2377 DEF_TRAVERSE_STMT(OMPSingleDirective,
   2378                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2379 
   2380 DEF_TRAVERSE_STMT(OMPMasterDirective,
   2381                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2382 
   2383 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
   2384   TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
   2385   TRY_TO(TraverseOMPExecutableDirective(S));
   2386 })
   2387 
   2388 DEF_TRAVERSE_STMT(OMPParallelForDirective,
   2389                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2390 
   2391 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
   2392                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2393 
   2394 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
   2395                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2396 
   2397 DEF_TRAVERSE_STMT(OMPTaskDirective,
   2398                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2399 
   2400 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
   2401                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2402 
   2403 DEF_TRAVERSE_STMT(OMPBarrierDirective,
   2404                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2405 
   2406 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
   2407                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2408 
   2409 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
   2410                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2411 
   2412 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
   2413                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2414 
   2415 DEF_TRAVERSE_STMT(OMPCancelDirective,
   2416                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2417 
   2418 DEF_TRAVERSE_STMT(OMPFlushDirective,
   2419                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2420 
   2421 DEF_TRAVERSE_STMT(OMPOrderedDirective,
   2422                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2423 
   2424 DEF_TRAVERSE_STMT(OMPAtomicDirective,
   2425                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2426 
   2427 DEF_TRAVERSE_STMT(OMPTargetDirective,
   2428                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2429 
   2430 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
   2431                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2432 
   2433 DEF_TRAVERSE_STMT(OMPTeamsDirective,
   2434                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2435 
   2436 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
   2437                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2438 
   2439 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
   2440                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2441 
   2442 DEF_TRAVERSE_STMT(OMPDistributeDirective,
   2443                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
   2444 
   2445 // OpenMP clauses.
   2446 template <typename Derived>
   2447 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
   2448   if (!C)
   2449     return true;
   2450   switch (C->getClauseKind()) {
   2451 #define OPENMP_CLAUSE(Name, Class)                                             \
   2452   case OMPC_##Name:                                                            \
   2453     TRY_TO(Visit##Class(static_cast<Class *>(C)));                             \
   2454     break;
   2455 #include "clang/Basic/OpenMPKinds.def"
   2456   case OMPC_threadprivate:
   2457   case OMPC_unknown:
   2458     break;
   2459   }
   2460   return true;
   2461 }
   2462 
   2463 template <typename Derived>
   2464 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
   2465   TRY_TO(TraverseStmt(C->getCondition()));
   2466   return true;
   2467 }
   2468 
   2469 template <typename Derived>
   2470 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
   2471   TRY_TO(TraverseStmt(C->getCondition()));
   2472   return true;
   2473 }
   2474 
   2475 template <typename Derived>
   2476 bool
   2477 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
   2478   TRY_TO(TraverseStmt(C->getNumThreads()));
   2479   return true;
   2480 }
   2481 
   2482 template <typename Derived>
   2483 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
   2484   TRY_TO(TraverseStmt(C->getSafelen()));
   2485   return true;
   2486 }
   2487 
   2488 template <typename Derived>
   2489 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
   2490   TRY_TO(TraverseStmt(C->getSimdlen()));
   2491   return true;
   2492 }
   2493 
   2494 template <typename Derived>
   2495 bool
   2496 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
   2497   TRY_TO(TraverseStmt(C->getNumForLoops()));
   2498   return true;
   2499 }
   2500 
   2501 template <typename Derived>
   2502 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
   2503   return true;
   2504 }
   2505 
   2506 template <typename Derived>
   2507 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
   2508   return true;
   2509 }
   2510 
   2511 template <typename Derived>
   2512 bool
   2513 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
   2514   TRY_TO(TraverseStmt(C->getChunkSize()));
   2515   TRY_TO(TraverseStmt(C->getHelperChunkSize()));
   2516   return true;
   2517 }
   2518 
   2519 template <typename Derived>
   2520 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
   2521   TRY_TO(TraverseStmt(C->getNumForLoops()));
   2522   return true;
   2523 }
   2524 
   2525 template <typename Derived>
   2526 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
   2527   return true;
   2528 }
   2529 
   2530 template <typename Derived>
   2531 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
   2532   return true;
   2533 }
   2534 
   2535 template <typename Derived>
   2536 bool
   2537 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
   2538   return true;
   2539 }
   2540 
   2541 template <typename Derived>
   2542 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
   2543   return true;
   2544 }
   2545 
   2546 template <typename Derived>
   2547 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
   2548   return true;
   2549 }
   2550 
   2551 template <typename Derived>
   2552 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
   2553   return true;
   2554 }
   2555 
   2556 template <typename Derived>
   2557 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
   2558   return true;
   2559 }
   2560 
   2561 template <typename Derived>
   2562 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
   2563   return true;
   2564 }
   2565 
   2566 template <typename Derived>
   2567 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
   2568   return true;
   2569 }
   2570 
   2571 template <typename Derived>
   2572 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
   2573   return true;
   2574 }
   2575 
   2576 template <typename Derived>
   2577 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
   2578   return true;
   2579 }
   2580 
   2581 template <typename Derived>
   2582 template <typename T>
   2583 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
   2584   for (auto *E : Node->varlists()) {
   2585     TRY_TO(TraverseStmt(E));
   2586   }
   2587   return true;
   2588 }
   2589 
   2590 template <typename Derived>
   2591 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
   2592   TRY_TO(VisitOMPClauseList(C));
   2593   for (auto *E : C->private_copies()) {
   2594     TRY_TO(TraverseStmt(E));
   2595   }
   2596   return true;
   2597 }
   2598 
   2599 template <typename Derived>
   2600 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
   2601     OMPFirstprivateClause *C) {
   2602   TRY_TO(VisitOMPClauseList(C));
   2603   for (auto *E : C->private_copies()) {
   2604     TRY_TO(TraverseStmt(E));
   2605   }
   2606   for (auto *E : C->inits()) {
   2607     TRY_TO(TraverseStmt(E));
   2608   }
   2609   return true;
   2610 }
   2611 
   2612 template <typename Derived>
   2613 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
   2614     OMPLastprivateClause *C) {
   2615   TRY_TO(VisitOMPClauseList(C));
   2616   for (auto *E : C->private_copies()) {
   2617     TRY_TO(TraverseStmt(E));
   2618   }
   2619   for (auto *E : C->source_exprs()) {
   2620     TRY_TO(TraverseStmt(E));
   2621   }
   2622   for (auto *E : C->destination_exprs()) {
   2623     TRY_TO(TraverseStmt(E));
   2624   }
   2625   for (auto *E : C->assignment_ops()) {
   2626     TRY_TO(TraverseStmt(E));
   2627   }
   2628   return true;
   2629 }
   2630 
   2631 template <typename Derived>
   2632 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
   2633   TRY_TO(VisitOMPClauseList(C));
   2634   return true;
   2635 }
   2636 
   2637 template <typename Derived>
   2638 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
   2639   TRY_TO(TraverseStmt(C->getStep()));
   2640   TRY_TO(TraverseStmt(C->getCalcStep()));
   2641   TRY_TO(VisitOMPClauseList(C));
   2642   for (auto *E : C->privates()) {
   2643     TRY_TO(TraverseStmt(E));
   2644   }
   2645   for (auto *E : C->inits()) {
   2646     TRY_TO(TraverseStmt(E));
   2647   }
   2648   for (auto *E : C->updates()) {
   2649     TRY_TO(TraverseStmt(E));
   2650   }
   2651   for (auto *E : C->finals()) {
   2652     TRY_TO(TraverseStmt(E));
   2653   }
   2654   return true;
   2655 }
   2656 
   2657 template <typename Derived>
   2658 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
   2659   TRY_TO(TraverseStmt(C->getAlignment()));
   2660   TRY_TO(VisitOMPClauseList(C));
   2661   return true;
   2662 }
   2663 
   2664 template <typename Derived>
   2665 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
   2666   TRY_TO(VisitOMPClauseList(C));
   2667   for (auto *E : C->source_exprs()) {
   2668     TRY_TO(TraverseStmt(E));
   2669   }
   2670   for (auto *E : C->destination_exprs()) {
   2671     TRY_TO(TraverseStmt(E));
   2672   }
   2673   for (auto *E : C->assignment_ops()) {
   2674     TRY_TO(TraverseStmt(E));
   2675   }
   2676   return true;
   2677 }
   2678 
   2679 template <typename Derived>
   2680 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
   2681     OMPCopyprivateClause *C) {
   2682   TRY_TO(VisitOMPClauseList(C));
   2683   for (auto *E : C->source_exprs()) {
   2684     TRY_TO(TraverseStmt(E));
   2685   }
   2686   for (auto *E : C->destination_exprs()) {
   2687     TRY_TO(TraverseStmt(E));
   2688   }
   2689   for (auto *E : C->assignment_ops()) {
   2690     TRY_TO(TraverseStmt(E));
   2691   }
   2692   return true;
   2693 }
   2694 
   2695 template <typename Derived>
   2696 bool
   2697 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
   2698   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
   2699   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
   2700   TRY_TO(VisitOMPClauseList(C));
   2701   for (auto *E : C->privates()) {
   2702     TRY_TO(TraverseStmt(E));
   2703   }
   2704   for (auto *E : C->lhs_exprs()) {
   2705     TRY_TO(TraverseStmt(E));
   2706   }
   2707   for (auto *E : C->rhs_exprs()) {
   2708     TRY_TO(TraverseStmt(E));
   2709   }
   2710   for (auto *E : C->reduction_ops()) {
   2711     TRY_TO(TraverseStmt(E));
   2712   }
   2713   return true;
   2714 }
   2715 
   2716 template <typename Derived>
   2717 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
   2718   TRY_TO(VisitOMPClauseList(C));
   2719   return true;
   2720 }
   2721 
   2722 template <typename Derived>
   2723 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
   2724   TRY_TO(VisitOMPClauseList(C));
   2725   return true;
   2726 }
   2727 
   2728 template <typename Derived>
   2729 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
   2730   TRY_TO(TraverseStmt(C->getDevice()));
   2731   return true;
   2732 }
   2733 
   2734 template <typename Derived>
   2735 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
   2736   TRY_TO(VisitOMPClauseList(C));
   2737   return true;
   2738 }
   2739 
   2740 template <typename Derived>
   2741 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
   2742     OMPNumTeamsClause *C) {
   2743   TRY_TO(TraverseStmt(C->getNumTeams()));
   2744   return true;
   2745 }
   2746 
   2747 template <typename Derived>
   2748 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
   2749     OMPThreadLimitClause *C) {
   2750   TRY_TO(TraverseStmt(C->getThreadLimit()));
   2751   return true;
   2752 }
   2753 
   2754 template <typename Derived>
   2755 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
   2756     OMPPriorityClause *C) {
   2757   TRY_TO(TraverseStmt(C->getPriority()));
   2758   return true;
   2759 }
   2760 
   2761 template <typename Derived>
   2762 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
   2763     OMPGrainsizeClause *C) {
   2764   TRY_TO(TraverseStmt(C->getGrainsize()));
   2765   return true;
   2766 }
   2767 
   2768 template <typename Derived>
   2769 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
   2770     OMPNumTasksClause *C) {
   2771   TRY_TO(TraverseStmt(C->getNumTasks()));
   2772   return true;
   2773 }
   2774 
   2775 template <typename Derived>
   2776 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
   2777   TRY_TO(TraverseStmt(C->getHint()));
   2778   return true;
   2779 }
   2780 
   2781 // FIXME: look at the following tricky-seeming exprs to see if we
   2782 // need to recurse on anything.  These are ones that have methods
   2783 // returning decls or qualtypes or nestednamespecifier -- though I'm
   2784 // not sure if they own them -- or just seemed very complicated, or
   2785 // had lots of sub-types to explore.
   2786 //
   2787 // VisitOverloadExpr and its children: recurse on template args? etc?
   2788 
   2789 // FIXME: go through all the stmts and exprs again, and see which of them
   2790 // create new types, and recurse on the types (TypeLocs?) of those.
   2791 // Candidates:
   2792 //
   2793 //    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
   2794 //    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
   2795 //    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
   2796 //    Every class that has getQualifier.
   2797 
   2798 #undef DEF_TRAVERSE_STMT
   2799 #undef TRAVERSE_STMT
   2800 #undef TRAVERSE_STMT_BASE
   2801 
   2802 #undef TRY_TO
   2803 
   2804 } // end namespace clang
   2805 
   2806 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
   2807