1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the RecursiveASTVisitor interface, which recursively
10 //  traverses the entire AST.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
14 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclFriend.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclOpenMP.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/ExprConcepts.h"
27 #include "clang/AST/ExprCXX.h"
28 #include "clang/AST/ExprObjC.h"
29 #include "clang/AST/ExprOpenMP.h"
30 #include "clang/AST/LambdaCapture.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/OpenMPClause.h"
33 #include "clang/AST/Stmt.h"
34 #include "clang/AST/StmtCXX.h"
35 #include "clang/AST/StmtObjC.h"
36 #include "clang/AST/StmtOpenMP.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/Basic/LLVM.h"
42 #include "clang/Basic/OpenMPKinds.h"
43 #include "clang/Basic/Specifiers.h"
44 #include "llvm/ADT/PointerIntPair.h"
45 #include "llvm/ADT/SmallVector.h"
46 #include "llvm/Support/Casting.h"
47 #include <algorithm>
48 #include <cstddef>
49 #include <type_traits>
50 
51 namespace clang {
52 
53 // A helper macro to implement short-circuiting when recursing.  It
54 // invokes CALL_EXPR, which must be a method call, on the derived
55 // object (s.t. a user of RecursiveASTVisitor can override the method
56 // in CALL_EXPR).
57 #define TRY_TO(CALL_EXPR)                                                      \
58   do {                                                                         \
59     if (!getDerived().CALL_EXPR)                                               \
60       return false;                                                            \
61   } while (false)
62 
63 namespace detail {
64 
65 template <typename T, typename U>
66 struct has_same_member_pointer_type : std::false_type {};
67 template <typename T, typename U, typename R, typename... P>
68 struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)>
69     : std::true_type {};
70 
71 template <bool has_same_type> struct is_same_method_impl {
72   template <typename FirstMethodPtrTy, typename SecondMethodPtrTy>
73   static bool isSameMethod(FirstMethodPtrTy FirstMethodPtr,
74                            SecondMethodPtrTy SecondMethodPtr) {
75     return false;
76   }
77 };
78 
79 template <> struct is_same_method_impl<true> {
80   template <typename FirstMethodPtrTy, typename SecondMethodPtrTy>
81   static bool isSameMethod(FirstMethodPtrTy FirstMethodPtr,
82                            SecondMethodPtrTy SecondMethodPtr) {
83     return FirstMethodPtr == SecondMethodPtr;
84   }
85 };
86 
87 /// Returns true if and only if \p FirstMethodPtr and \p SecondMethodPtr
88 /// are pointers to the same non-static member function.
89 template <typename FirstMethodPtrTy, typename SecondMethodPtrTy>
90 bool isSameMethod(FirstMethodPtrTy FirstMethodPtr,
91                   SecondMethodPtrTy SecondMethodPtr) {
92   return is_same_method_impl<has_same_member_pointer_type<
93       FirstMethodPtrTy,
94       SecondMethodPtrTy>::value>::isSameMethod(FirstMethodPtr, SecondMethodPtr);
95 }
96 
97 } // end namespace detail
98 
99 /// A class that does preorder or postorder
100 /// depth-first traversal on the entire Clang AST and visits each node.
101 ///
102 /// This class performs three distinct tasks:
103 ///   1. traverse the AST (i.e. go to each node);
104 ///   2. at a given node, walk up the class hierarchy, starting from
105 ///      the node's dynamic type, until the top-most class (e.g. Stmt,
106 ///      Decl, or Type) is reached.
107 ///   3. given a (node, class) combination, where 'class' is some base
108 ///      class of the dynamic type of 'node', call a user-overridable
109 ///      function to actually visit the node.
110 ///
111 /// These tasks are done by three groups of methods, respectively:
112 ///   1. TraverseDecl(Decl *x) does task #1.  It is the entry point
113 ///      for traversing an AST rooted at x.  This method simply
114 ///      dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
115 ///      is the dynamic type of *x, which calls WalkUpFromFoo(x) and
116 ///      then recursively visits the child nodes of x.
117 ///      TraverseStmt(Stmt *x) and TraverseType(QualType x) work
118 ///      similarly.
119 ///   2. WalkUpFromFoo(Foo *x) does task #2.  It does not try to visit
120 ///      any child node of x.  Instead, it first calls WalkUpFromBar(x)
121 ///      where Bar is the direct parent class of Foo (unless Foo has
122 ///      no parent), and then calls VisitFoo(x) (see the next list item).
123 ///   3. VisitFoo(Foo *x) does task #3.
124 ///
125 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
126 /// Visit*).  A method (e.g. Traverse*) may call methods from the same
127 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
128 /// It may not call methods from a higher tier.
129 ///
130 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
131 /// is Foo's super class) before calling VisitFoo(), the result is
132 /// that the Visit*() methods for a given node are called in the
133 /// top-down order (e.g. for a node of type NamespaceDecl, the order will
134 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
135 ///
136 /// This scheme guarantees that all Visit*() calls for the same AST
137 /// node are grouped together.  In other words, Visit*() methods for
138 /// different nodes are never interleaved.
139 ///
140 /// Clients of this visitor should subclass the visitor (providing
141 /// themselves as the template argument, using the curiously recurring
142 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
143 /// and Visit* methods for declarations, types, statements,
144 /// expressions, or other AST nodes where the visitor should customize
145 /// behavior.  Most users only need to override Visit*.  Advanced
146 /// users may override Traverse* and WalkUpFrom* to implement custom
147 /// traversal strategies.  Returning false from one of these overridden
148 /// functions will abort the entire traversal.
149 ///
150 /// By default, this visitor tries to visit every part of the explicit
151 /// source code exactly once.  The default policy towards templates
152 /// is to descend into the 'pattern' class or function body, not any
153 /// explicit or implicit instantiations.  Explicit specializations
154 /// are still visited, and the patterns of partial specializations
155 /// are visited separately.  This behavior can be changed by
156 /// overriding shouldVisitTemplateInstantiations() in the derived class
157 /// to return true, in which case all known implicit and explicit
158 /// instantiations will be visited at the same time as the pattern
159 /// from which they were produced.
160 ///
161 /// By default, this visitor preorder traverses the AST. If postorder traversal
162 /// is needed, the \c shouldTraversePostOrder method needs to be overridden
163 /// to return \c true.
164 template <typename Derived> class RecursiveASTVisitor {
165 public:
166   /// A queue used for performing data recursion over statements.
167   /// Parameters involving this type are used to implement data
168   /// recursion over Stmts and Exprs within this class, and should
169   /// typically not be explicitly specified by derived classes.
170   /// The bool bit indicates whether the statement has been traversed or not.
171   typedef SmallVectorImpl<llvm::PointerIntPair<Stmt *, 1, bool>>
172     DataRecursionQueue;
173 
174   /// Return a reference to the derived class.
175   Derived &getDerived() { return *static_cast<Derived *>(this); }
176 
177   /// Return whether this visitor should recurse into
178   /// template instantiations.
179   bool shouldVisitTemplateInstantiations() const { return false; }
180 
181   /// Return whether this visitor should recurse into the types of
182   /// TypeLocs.
183   bool shouldWalkTypesOfTypeLocs() const { return true; }
184 
185   /// Return whether this visitor should recurse into implicit
186   /// code, e.g., implicit constructors and destructors.
187   bool shouldVisitImplicitCode() const { return false; }
188 
189   /// Return whether this visitor should traverse post-order.
190   bool shouldTraversePostOrder() const { return false; }
191 
192   /// Recursively visits an entire AST, starting from the top-level Decls
193   /// in the AST traversal scope (by default, the TranslationUnitDecl).
194   /// \returns false if visitation was terminated early.
195   bool TraverseAST(ASTContext &AST) {
196     for (Decl *D : AST.getTraversalScope())
197       if (!getDerived().TraverseDecl(D))
198         return false;
199     return true;
200   }
201 
202   /// Recursively visit a statement or expression, by
203   /// dispatching to Traverse*() based on the argument's dynamic type.
204   ///
205   /// \returns false if the visitation was terminated early, true
206   /// otherwise (including when the argument is nullptr).
207   bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr);
208 
209   /// Invoked before visiting a statement or expression via data recursion.
210   ///
211   /// \returns false to skip visiting the node, true otherwise.
212   bool dataTraverseStmtPre(Stmt *S) { return true; }
213 
214   /// Invoked after visiting a statement or expression via data recursion.
215   /// This is not invoked if the previously invoked \c dataTraverseStmtPre
216   /// returned false.
217   ///
218   /// \returns false if the visitation was terminated early, true otherwise.
219   bool dataTraverseStmtPost(Stmt *S) { return true; }
220 
221   /// Recursively visit a type, by dispatching to
222   /// Traverse*Type() based on the argument's getTypeClass() property.
223   ///
224   /// \returns false if the visitation was terminated early, true
225   /// otherwise (including when the argument is a Null type).
226   bool TraverseType(QualType T);
227 
228   /// Recursively visit a type with location, by dispatching to
229   /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
230   ///
231   /// \returns false if the visitation was terminated early, true
232   /// otherwise (including when the argument is a Null type location).
233   bool TraverseTypeLoc(TypeLoc TL);
234 
235   /// Recursively visit an attribute, by dispatching to
236   /// Traverse*Attr() based on the argument's dynamic type.
237   ///
238   /// \returns false if the visitation was terminated early, true
239   /// otherwise (including when the argument is a Null type location).
240   bool TraverseAttr(Attr *At);
241 
242   /// Recursively visit a declaration, by dispatching to
243   /// Traverse*Decl() based on the argument's dynamic type.
244   ///
245   /// \returns false if the visitation was terminated early, true
246   /// otherwise (including when the argument is NULL).
247   bool TraverseDecl(Decl *D);
248 
249   /// Recursively visit a C++ nested-name-specifier.
250   ///
251   /// \returns false if the visitation was terminated early, true otherwise.
252   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
253 
254   /// Recursively visit a C++ nested-name-specifier with location
255   /// information.
256   ///
257   /// \returns false if the visitation was terminated early, true otherwise.
258   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
259 
260   /// Recursively visit a name with its location information.
261   ///
262   /// \returns false if the visitation was terminated early, true otherwise.
263   bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
264 
265   /// Recursively visit a template name and dispatch to the
266   /// appropriate method.
267   ///
268   /// \returns false if the visitation was terminated early, true otherwise.
269   bool TraverseTemplateName(TemplateName Template);
270 
271   /// Recursively visit a template argument and dispatch to the
272   /// appropriate method for the argument type.
273   ///
274   /// \returns false if the visitation was terminated early, true otherwise.
275   // FIXME: migrate callers to TemplateArgumentLoc instead.
276   bool TraverseTemplateArgument(const TemplateArgument &Arg);
277 
278   /// Recursively visit a template argument location and dispatch to the
279   /// appropriate method for the argument type.
280   ///
281   /// \returns false if the visitation was terminated early, true otherwise.
282   bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
283 
284   /// Recursively visit a set of template arguments.
285   /// This can be overridden by a subclass, but it's not expected that
286   /// will be needed -- this visitor always dispatches to another.
287   ///
288   /// \returns false if the visitation was terminated early, true otherwise.
289   // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
290   bool TraverseTemplateArguments(const TemplateArgument *Args,
291                                  unsigned NumArgs);
292 
293   /// Recursively visit a base specifier. This can be overridden by a
294   /// subclass.
295   ///
296   /// \returns false if the visitation was terminated early, true otherwise.
297   bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base);
298 
299   /// Recursively visit a constructor initializer.  This
300   /// automatically dispatches to another visitor for the initializer
301   /// expression, but not for the name of the initializer, so may
302   /// be overridden for clients that need access to the name.
303   ///
304   /// \returns false if the visitation was terminated early, true otherwise.
305   bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
306 
307   /// Recursively visit a lambda capture. \c Init is the expression that
308   /// will be used to initialize the capture.
309   ///
310   /// \returns false if the visitation was terminated early, true otherwise.
311   bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
312                              Expr *Init);
313 
314   /// Recursively visit the syntactic or semantic form of an
315   /// initialization list.
316   ///
317   /// \returns false if the visitation was terminated early, true otherwise.
318   bool TraverseSynOrSemInitListExpr(InitListExpr *S,
319                                     DataRecursionQueue *Queue = nullptr);
320 
321   /// Recursively visit a reference to a concept with potential arguments.
322   ///
323   /// \returns false if the visitation was terminated early, true otherwise.
324   bool TraverseConceptReference(const ConceptReference &C);
325 
326   // ---- Methods on Attrs ----
327 
328   // Visit an attribute.
329   bool VisitAttr(Attr *A) { return true; }
330 
331 // Declare Traverse* and empty Visit* for all Attr classes.
332 #define ATTR_VISITOR_DECLS_ONLY
333 #include "clang/AST/AttrVisitor.inc"
334 #undef ATTR_VISITOR_DECLS_ONLY
335 
336 // ---- Methods on Stmts ----
337 
338   Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); }
339 
340 private:
341   // Traverse the given statement. If the most-derived traverse function takes a
342   // data recursion queue, pass it on; otherwise, discard it. Note that the
343   // first branch of this conditional must compile whether or not the derived
344   // class can take a queue, so if we're taking the second arm, make the first
345   // arm call our function rather than the derived class version.
346 #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE)                            \
347   (::clang::detail::has_same_member_pointer_type<                              \
348        decltype(&RecursiveASTVisitor::Traverse##NAME),                         \
349        decltype(&Derived::Traverse##NAME)>::value                              \
350        ? static_cast<std::conditional_t<                                       \
351              ::clang::detail::has_same_member_pointer_type<                    \
352                  decltype(&RecursiveASTVisitor::Traverse##NAME),               \
353                  decltype(&Derived::Traverse##NAME)>::value,                   \
354              Derived &, RecursiveASTVisitor &>>(*this)                         \
355              .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE)                 \
356        : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)))
357 
358 // Try to traverse the given statement, or enqueue it if we're performing data
359 // recursion in the middle of traversing another statement. Can only be called
360 // from within a DEF_TRAVERSE_STMT body or similar context.
361 #define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S)                                     \
362   do {                                                                         \
363     if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue))                             \
364       return false;                                                            \
365   } while (false)
366 
367 public:
368 // Declare Traverse*() for all concrete Stmt classes.
369 #define ABSTRACT_STMT(STMT)
370 #define STMT(CLASS, PARENT) \
371   bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
372 #include "clang/AST/StmtNodes.inc"
373   // The above header #undefs ABSTRACT_STMT and STMT upon exit.
374 
375   // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
376   bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
377   bool VisitStmt(Stmt *S) { return true; }
378 #define STMT(CLASS, PARENT)                                                    \
379   bool WalkUpFrom##CLASS(CLASS *S) {                                           \
380     TRY_TO(WalkUpFrom##PARENT(S));                                             \
381     TRY_TO(Visit##CLASS(S));                                                   \
382     return true;                                                               \
383   }                                                                            \
384   bool Visit##CLASS(CLASS *S) { return true; }
385 #include "clang/AST/StmtNodes.inc"
386 
387 // ---- Methods on Types ----
388 // FIXME: revamp to take TypeLoc's rather than Types.
389 
390 // Declare Traverse*() for all concrete Type classes.
391 #define ABSTRACT_TYPE(CLASS, BASE)
392 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T);
393 #include "clang/AST/TypeNodes.inc"
394   // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
395 
396   // Define WalkUpFrom*() and empty Visit*() for all Type classes.
397   bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
398   bool VisitType(Type *T) { return true; }
399 #define TYPE(CLASS, BASE)                                                      \
400   bool WalkUpFrom##CLASS##Type(CLASS##Type *T) {                               \
401     TRY_TO(WalkUpFrom##BASE(T));                                               \
402     TRY_TO(Visit##CLASS##Type(T));                                             \
403     return true;                                                               \
404   }                                                                            \
405   bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
406 #include "clang/AST/TypeNodes.inc"
407 
408 // ---- Methods on TypeLocs ----
409 // FIXME: this currently just calls the matching Type methods
410 
411 // Declare Traverse*() for all concrete TypeLoc classes.
412 #define ABSTRACT_TYPELOC(CLASS, BASE)
413 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
414 #include "clang/AST/TypeLocNodes.def"
415   // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
416 
417   // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
418   bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
419   bool VisitTypeLoc(TypeLoc TL) { return true; }
420 
421   // QualifiedTypeLoc and UnqualTypeLoc are not declared in
422   // TypeNodes.inc and thus need to be handled specially.
423   bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
424     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
425   }
426   bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
427   bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
428     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
429   }
430   bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
431 
432 // Note that BASE includes trailing 'Type' which CLASS doesn't.
433 #define TYPE(CLASS, BASE)                                                      \
434   bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) {                         \
435     TRY_TO(WalkUpFrom##BASE##Loc(TL));                                         \
436     TRY_TO(Visit##CLASS##TypeLoc(TL));                                         \
437     return true;                                                               \
438   }                                                                            \
439   bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
440 #include "clang/AST/TypeNodes.inc"
441 
442 // ---- Methods on Decls ----
443 
444 // Declare Traverse*() for all concrete Decl classes.
445 #define ABSTRACT_DECL(DECL)
446 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D);
447 #include "clang/AST/DeclNodes.inc"
448   // The above header #undefs ABSTRACT_DECL and DECL upon exit.
449 
450   // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
451   bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
452   bool VisitDecl(Decl *D) { return true; }
453 #define DECL(CLASS, BASE)                                                      \
454   bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) {                               \
455     TRY_TO(WalkUpFrom##BASE(D));                                               \
456     TRY_TO(Visit##CLASS##Decl(D));                                             \
457     return true;                                                               \
458   }                                                                            \
459   bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
460 #include "clang/AST/DeclNodes.inc"
461 
462   bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
463 
464 private:
465   // These are helper methods used by more than one Traverse* method.
466   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
467 
468   // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
469   template <typename T>
470   bool TraverseDeclTemplateParameterLists(T *D);
471 
472 #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND)                                   \
473   bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D);
474   DEF_TRAVERSE_TMPL_INST(Class)
475   DEF_TRAVERSE_TMPL_INST(Var)
476   DEF_TRAVERSE_TMPL_INST(Function)
477 #undef DEF_TRAVERSE_TMPL_INST
478   bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
479                                           unsigned Count);
480   bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
481   bool TraverseRecordHelper(RecordDecl *D);
482   bool TraverseCXXRecordHelper(CXXRecordDecl *D);
483   bool TraverseDeclaratorHelper(DeclaratorDecl *D);
484   bool TraverseDeclContextHelper(DeclContext *DC);
485   bool TraverseFunctionHelper(FunctionDecl *D);
486   bool TraverseVarHelper(VarDecl *D);
487   bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
488   bool TraverseOMPLoopDirective(OMPLoopDirective *S);
489   bool TraverseOMPClause(OMPClause *C);
490 #define OMP_CLAUSE_CLASS(Enum, Str, Class) bool Visit##Class(Class *C);
491 #include "llvm/Frontend/OpenMP/OMPKinds.def"
492   /// Process clauses with list of variables.
493   template <typename T> bool VisitOMPClauseList(T *Node);
494   /// Process clauses with pre-initis.
495   bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node);
496   bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node);
497 
498   bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
499   bool PostVisitStmt(Stmt *S);
500 };
501 
502 template <typename Derived>
503 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
504                                                     DataRecursionQueue *Queue) {
505   // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
506   switch (S->getStmtClass()) {
507   case Stmt::NoStmtClass:
508     break;
509 #define ABSTRACT_STMT(STMT)
510 #define STMT(CLASS, PARENT)                                                    \
511   case Stmt::CLASS##Class:                                                     \
512     return TRAVERSE_STMT_BASE(CLASS, CLASS, S, Queue);
513 #include "clang/AST/StmtNodes.inc"
514   }
515 
516   return true;
517 }
518 
519 #undef DISPATCH_STMT
520 
521 template <typename Derived>
522 bool RecursiveASTVisitor<Derived>::PostVisitStmt(Stmt *S) {
523   // In pre-order traversal mode, each Traverse##STMT method is responsible for
524   // calling WalkUpFrom. Therefore, if the user overrides Traverse##STMT and
525   // does not call the default implementation, the WalkUpFrom callback is not
526   // called. Post-order traversal mode should provide the same behavior
527   // regarding method overrides.
528   //
529   // In post-order traversal mode the Traverse##STMT method, when it receives a
530   // DataRecursionQueue, can't call WalkUpFrom after traversing children because
531   // it only enqueues the children and does not traverse them. TraverseStmt
532   // traverses the enqueued children, and we call WalkUpFrom here.
533   //
534   // However, to make pre-order and post-order modes identical with regards to
535   // whether they call WalkUpFrom at all, we call WalkUpFrom if and only if the
536   // user did not override the Traverse##STMT method. We implement the override
537   // check with isSameMethod calls below.
538 
539   switch (S->getStmtClass()) {
540   case Stmt::NoStmtClass:
541     break;
542 #define ABSTRACT_STMT(STMT)
543 #define STMT(CLASS, PARENT)                                                    \
544   case Stmt::CLASS##Class:                                                     \
545     if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS,   \
546                                       &Derived::Traverse##CLASS)) {            \
547       TRY_TO(WalkUpFrom##CLASS(static_cast<CLASS *>(S)));                      \
548     }                                                                          \
549     break;
550 #define INITLISTEXPR(CLASS, PARENT)                                            \
551   case Stmt::CLASS##Class:                                                     \
552     if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS,   \
553                                       &Derived::Traverse##CLASS)) {            \
554       auto ILE = static_cast<CLASS *>(S);                                      \
555       if (auto Syn = ILE->isSemanticForm() ? ILE->getSyntacticForm() : ILE)    \
556         TRY_TO(WalkUpFrom##CLASS(Syn));                                        \
557       if (auto Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm())     \
558         TRY_TO(WalkUpFrom##CLASS(Sem));                                        \
559     }                                                                          \
560     break;
561 #include "clang/AST/StmtNodes.inc"
562   }
563 
564   return true;
565 }
566 
567 #undef DISPATCH_STMT
568 
569 template <typename Derived>
570 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
571                                                 DataRecursionQueue *Queue) {
572   if (!S)
573     return true;
574 
575   if (Queue) {
576     Queue->push_back({S, false});
577     return true;
578   }
579 
580   SmallVector<llvm::PointerIntPair<Stmt *, 1, bool>, 8> LocalQueue;
581   LocalQueue.push_back({S, false});
582 
583   while (!LocalQueue.empty()) {
584     auto &CurrSAndVisited = LocalQueue.back();
585     Stmt *CurrS = CurrSAndVisited.getPointer();
586     bool Visited = CurrSAndVisited.getInt();
587     if (Visited) {
588       LocalQueue.pop_back();
589       TRY_TO(dataTraverseStmtPost(CurrS));
590       if (getDerived().shouldTraversePostOrder()) {
591         TRY_TO(PostVisitStmt(CurrS));
592       }
593       continue;
594     }
595 
596     if (getDerived().dataTraverseStmtPre(CurrS)) {
597       CurrSAndVisited.setInt(true);
598       size_t N = LocalQueue.size();
599       TRY_TO(dataTraverseNode(CurrS, &LocalQueue));
600       // Process new children in the order they were added.
601       std::reverse(LocalQueue.begin() + N, LocalQueue.end());
602     } else {
603       LocalQueue.pop_back();
604     }
605   }
606 
607   return true;
608 }
609 
610 template <typename Derived>
611 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
612   if (T.isNull())
613     return true;
614 
615   switch (T->getTypeClass()) {
616 #define ABSTRACT_TYPE(CLASS, BASE)
617 #define TYPE(CLASS, BASE)                                                      \
618   case Type::CLASS:                                                            \
619     return getDerived().Traverse##CLASS##Type(                                 \
620         static_cast<CLASS##Type *>(const_cast<Type *>(T.getTypePtr())));
621 #include "clang/AST/TypeNodes.inc"
622   }
623 
624   return true;
625 }
626 
627 template <typename Derived>
628 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
629   if (TL.isNull())
630     return true;
631 
632   switch (TL.getTypeLocClass()) {
633 #define ABSTRACT_TYPELOC(CLASS, BASE)
634 #define TYPELOC(CLASS, BASE)                                                   \
635   case TypeLoc::CLASS:                                                         \
636     return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
637 #include "clang/AST/TypeLocNodes.def"
638   }
639 
640   return true;
641 }
642 
643 // Define the Traverse*Attr(Attr* A) methods
644 #define VISITORCLASS RecursiveASTVisitor
645 #include "clang/AST/AttrVisitor.inc"
646 #undef VISITORCLASS
647 
648 template <typename Derived>
649 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
650   if (!D)
651     return true;
652 
653   // As a syntax visitor, by default we want to ignore declarations for
654   // implicit declarations (ones not typed explicitly by the user).
655   if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
656     return true;
657 
658   switch (D->getKind()) {
659 #define ABSTRACT_DECL(DECL)
660 #define DECL(CLASS, BASE)                                                      \
661   case Decl::CLASS:                                                            \
662     if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D)))    \
663       return false;                                                            \
664     break;
665 #include "clang/AST/DeclNodes.inc"
666   }
667   return true;
668 }
669 
670 template <typename Derived>
671 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
672     NestedNameSpecifier *NNS) {
673   if (!NNS)
674     return true;
675 
676   if (NNS->getPrefix())
677     TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
678 
679   switch (NNS->getKind()) {
680   case NestedNameSpecifier::Identifier:
681   case NestedNameSpecifier::Namespace:
682   case NestedNameSpecifier::NamespaceAlias:
683   case NestedNameSpecifier::Global:
684   case NestedNameSpecifier::Super:
685     return true;
686 
687   case NestedNameSpecifier::TypeSpec:
688   case NestedNameSpecifier::TypeSpecWithTemplate:
689     TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
690   }
691 
692   return true;
693 }
694 
695 template <typename Derived>
696 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
697     NestedNameSpecifierLoc NNS) {
698   if (!NNS)
699     return true;
700 
701   if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
702     TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
703 
704   switch (NNS.getNestedNameSpecifier()->getKind()) {
705   case NestedNameSpecifier::Identifier:
706   case NestedNameSpecifier::Namespace:
707   case NestedNameSpecifier::NamespaceAlias:
708   case NestedNameSpecifier::Global:
709   case NestedNameSpecifier::Super:
710     return true;
711 
712   case NestedNameSpecifier::TypeSpec:
713   case NestedNameSpecifier::TypeSpecWithTemplate:
714     TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
715     break;
716   }
717 
718   return true;
719 }
720 
721 template <typename Derived>
722 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
723     DeclarationNameInfo NameInfo) {
724   switch (NameInfo.getName().getNameKind()) {
725   case DeclarationName::CXXConstructorName:
726   case DeclarationName::CXXDestructorName:
727   case DeclarationName::CXXConversionFunctionName:
728     if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
729       TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
730     break;
731 
732   case DeclarationName::CXXDeductionGuideName:
733     TRY_TO(TraverseTemplateName(
734         TemplateName(NameInfo.getName().getCXXDeductionGuideTemplate())));
735     break;
736 
737   case DeclarationName::Identifier:
738   case DeclarationName::ObjCZeroArgSelector:
739   case DeclarationName::ObjCOneArgSelector:
740   case DeclarationName::ObjCMultiArgSelector:
741   case DeclarationName::CXXOperatorName:
742   case DeclarationName::CXXLiteralOperatorName:
743   case DeclarationName::CXXUsingDirective:
744     break;
745   }
746 
747   return true;
748 }
749 
750 template <typename Derived>
751 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
752   if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
753     TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
754   else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
755     TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
756 
757   return true;
758 }
759 
760 template <typename Derived>
761 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
762     const TemplateArgument &Arg) {
763   switch (Arg.getKind()) {
764   case TemplateArgument::Null:
765   case TemplateArgument::Declaration:
766   case TemplateArgument::Integral:
767   case TemplateArgument::NullPtr:
768     return true;
769 
770   case TemplateArgument::Type:
771     return getDerived().TraverseType(Arg.getAsType());
772 
773   case TemplateArgument::Template:
774   case TemplateArgument::TemplateExpansion:
775     return getDerived().TraverseTemplateName(
776         Arg.getAsTemplateOrTemplatePattern());
777 
778   case TemplateArgument::Expression:
779     return getDerived().TraverseStmt(Arg.getAsExpr());
780 
781   case TemplateArgument::Pack:
782     return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
783                                                   Arg.pack_size());
784   }
785 
786   return true;
787 }
788 
789 // FIXME: no template name location?
790 // FIXME: no source locations for a template argument pack?
791 template <typename Derived>
792 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
793     const TemplateArgumentLoc &ArgLoc) {
794   const TemplateArgument &Arg = ArgLoc.getArgument();
795 
796   switch (Arg.getKind()) {
797   case TemplateArgument::Null:
798   case TemplateArgument::Declaration:
799   case TemplateArgument::Integral:
800   case TemplateArgument::NullPtr:
801     return true;
802 
803   case TemplateArgument::Type: {
804     // FIXME: how can TSI ever be NULL?
805     if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
806       return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
807     else
808       return getDerived().TraverseType(Arg.getAsType());
809   }
810 
811   case TemplateArgument::Template:
812   case TemplateArgument::TemplateExpansion:
813     if (ArgLoc.getTemplateQualifierLoc())
814       TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
815           ArgLoc.getTemplateQualifierLoc()));
816     return getDerived().TraverseTemplateName(
817         Arg.getAsTemplateOrTemplatePattern());
818 
819   case TemplateArgument::Expression:
820     return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
821 
822   case TemplateArgument::Pack:
823     return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
824                                                   Arg.pack_size());
825   }
826 
827   return true;
828 }
829 
830 template <typename Derived>
831 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
832     const TemplateArgument *Args, unsigned NumArgs) {
833   for (unsigned I = 0; I != NumArgs; ++I) {
834     TRY_TO(TraverseTemplateArgument(Args[I]));
835   }
836 
837   return true;
838 }
839 
840 template <typename Derived>
841 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
842     CXXCtorInitializer *Init) {
843   if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
844     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
845 
846   if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
847     TRY_TO(TraverseStmt(Init->getInit()));
848 
849   return true;
850 }
851 
852 template <typename Derived>
853 bool
854 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
855                                                     const LambdaCapture *C,
856                                                     Expr *Init) {
857   if (LE->isInitCapture(C))
858     TRY_TO(TraverseDecl(C->getCapturedVar()));
859   else
860     TRY_TO(TraverseStmt(Init));
861   return true;
862 }
863 
864 // ----------------- Type traversal -----------------
865 
866 // This macro makes available a variable T, the passed-in type.
867 #define DEF_TRAVERSE_TYPE(TYPE, CODE)                                          \
868   template <typename Derived>                                                  \
869   bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) {                 \
870     if (!getDerived().shouldTraversePostOrder())                               \
871       TRY_TO(WalkUpFrom##TYPE(T));                                             \
872     { CODE; }                                                                  \
873     if (getDerived().shouldTraversePostOrder())                                \
874       TRY_TO(WalkUpFrom##TYPE(T));                                             \
875     return true;                                                               \
876   }
877 
878 DEF_TRAVERSE_TYPE(BuiltinType, {})
879 
880 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
881 
882 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
883 
884 DEF_TRAVERSE_TYPE(BlockPointerType,
885                   { TRY_TO(TraverseType(T->getPointeeType())); })
886 
887 DEF_TRAVERSE_TYPE(LValueReferenceType,
888                   { TRY_TO(TraverseType(T->getPointeeType())); })
889 
890 DEF_TRAVERSE_TYPE(RValueReferenceType,
891                   { TRY_TO(TraverseType(T->getPointeeType())); })
892 
893 DEF_TRAVERSE_TYPE(MemberPointerType, {
894   TRY_TO(TraverseType(QualType(T->getClass(), 0)));
895   TRY_TO(TraverseType(T->getPointeeType()));
896 })
897 
898 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
899 
900 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
901 
902 DEF_TRAVERSE_TYPE(ConstantArrayType, {
903   TRY_TO(TraverseType(T->getElementType()));
904   if (T->getSizeExpr())
905     TRY_TO(TraverseStmt(const_cast<Expr*>(T->getSizeExpr())));
906 })
907 
908 DEF_TRAVERSE_TYPE(IncompleteArrayType,
909                   { TRY_TO(TraverseType(T->getElementType())); })
910 
911 DEF_TRAVERSE_TYPE(VariableArrayType, {
912   TRY_TO(TraverseType(T->getElementType()));
913   TRY_TO(TraverseStmt(T->getSizeExpr()));
914 })
915 
916 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
917   TRY_TO(TraverseType(T->getElementType()));
918   if (T->getSizeExpr())
919     TRY_TO(TraverseStmt(T->getSizeExpr()));
920 })
921 
922 DEF_TRAVERSE_TYPE(DependentAddressSpaceType, {
923   TRY_TO(TraverseStmt(T->getAddrSpaceExpr()));
924   TRY_TO(TraverseType(T->getPointeeType()));
925 })
926 
927 DEF_TRAVERSE_TYPE(DependentVectorType, {
928   if (T->getSizeExpr())
929     TRY_TO(TraverseStmt(T->getSizeExpr()));
930   TRY_TO(TraverseType(T->getElementType()));
931 })
932 
933 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
934   if (T->getSizeExpr())
935     TRY_TO(TraverseStmt(T->getSizeExpr()));
936   TRY_TO(TraverseType(T->getElementType()));
937 })
938 
939 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
940 
941 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
942 
943 DEF_TRAVERSE_TYPE(ConstantMatrixType,
944                   { TRY_TO(TraverseType(T->getElementType())); })
945 
946 DEF_TRAVERSE_TYPE(DependentSizedMatrixType, {
947   if (T->getRowExpr())
948     TRY_TO(TraverseStmt(T->getRowExpr()));
949   if (T->getColumnExpr())
950     TRY_TO(TraverseStmt(T->getColumnExpr()));
951   TRY_TO(TraverseType(T->getElementType()));
952 })
953 
954 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
955                   { TRY_TO(TraverseType(T->getReturnType())); })
956 
957 DEF_TRAVERSE_TYPE(FunctionProtoType, {
958   TRY_TO(TraverseType(T->getReturnType()));
959 
960   for (const auto &A : T->param_types()) {
961     TRY_TO(TraverseType(A));
962   }
963 
964   for (const auto &E : T->exceptions()) {
965     TRY_TO(TraverseType(E));
966   }
967 
968   if (Expr *NE = T->getNoexceptExpr())
969     TRY_TO(TraverseStmt(NE));
970 })
971 
972 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
973 DEF_TRAVERSE_TYPE(TypedefType, {})
974 
975 DEF_TRAVERSE_TYPE(TypeOfExprType,
976                   { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
977 
978 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
979 
980 DEF_TRAVERSE_TYPE(DecltypeType,
981                   { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
982 
983 DEF_TRAVERSE_TYPE(UnaryTransformType, {
984   TRY_TO(TraverseType(T->getBaseType()));
985   TRY_TO(TraverseType(T->getUnderlyingType()));
986 })
987 
988 DEF_TRAVERSE_TYPE(AutoType, {
989   TRY_TO(TraverseType(T->getDeducedType()));
990   if (T->isConstrained()) {
991     TRY_TO(TraverseDecl(T->getTypeConstraintConcept()));
992     TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
993   }
994 })
995 DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, {
996   TRY_TO(TraverseTemplateName(T->getTemplateName()));
997   TRY_TO(TraverseType(T->getDeducedType()));
998 })
999 
1000 DEF_TRAVERSE_TYPE(RecordType, {})
1001 DEF_TRAVERSE_TYPE(EnumType, {})
1002 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
1003 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {
1004   TRY_TO(TraverseType(T->getReplacementType()));
1005 })
1006 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {
1007   TRY_TO(TraverseTemplateArgument(T->getArgumentPack()));
1008 })
1009 
1010 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
1011   TRY_TO(TraverseTemplateName(T->getTemplateName()));
1012   TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1013 })
1014 
1015 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
1016 
1017 DEF_TRAVERSE_TYPE(AttributedType,
1018                   { TRY_TO(TraverseType(T->getModifiedType())); })
1019 
1020 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
1021 
1022 DEF_TRAVERSE_TYPE(MacroQualifiedType,
1023                   { TRY_TO(TraverseType(T->getUnderlyingType())); })
1024 
1025 DEF_TRAVERSE_TYPE(ElaboratedType, {
1026   if (T->getQualifier()) {
1027     TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1028   }
1029   TRY_TO(TraverseType(T->getNamedType()));
1030 })
1031 
1032 DEF_TRAVERSE_TYPE(DependentNameType,
1033                   { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
1034 
1035 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1036   TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1037   TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1038 })
1039 
1040 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1041 
1042 DEF_TRAVERSE_TYPE(ObjCTypeParamType, {})
1043 
1044 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1045 
1046 DEF_TRAVERSE_TYPE(ObjCObjectType, {
1047   // We have to watch out here because an ObjCInterfaceType's base
1048   // type is itself.
1049   if (T->getBaseType().getTypePtr() != T)
1050     TRY_TO(TraverseType(T->getBaseType()));
1051   for (auto typeArg : T->getTypeArgsAsWritten()) {
1052     TRY_TO(TraverseType(typeArg));
1053   }
1054 })
1055 
1056 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1057                   { TRY_TO(TraverseType(T->getPointeeType())); })
1058 
1059 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1060 
1061 DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
1062 
1063 DEF_TRAVERSE_TYPE(ExtIntType, {})
1064 DEF_TRAVERSE_TYPE(DependentExtIntType,
1065                   { TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
1066 
1067 #undef DEF_TRAVERSE_TYPE
1068 
1069 // ----------------- TypeLoc traversal -----------------
1070 
1071 // This macro makes available a variable TL, the passed-in TypeLoc.
1072 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1073 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1074 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1075 // continue to work.
1076 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE)                                       \
1077   template <typename Derived>                                                  \
1078   bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) {       \
1079     if (!getDerived().shouldTraversePostOrder()) {                             \
1080       TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                       \
1081       if (getDerived().shouldWalkTypesOfTypeLocs())                            \
1082         TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr())));         \
1083     }                                                                          \
1084     { CODE; }                                                                  \
1085     if (getDerived().shouldTraversePostOrder()) {                              \
1086       TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                       \
1087       if (getDerived().shouldWalkTypesOfTypeLocs())                            \
1088         TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr())));         \
1089     }                                                                          \
1090     return true;                                                               \
1091   }
1092 
1093 template <typename Derived>
1094 bool
1095 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1096   // Move this over to the 'main' typeloc tree.  Note that this is a
1097   // move -- we pretend that we were really looking at the unqualified
1098   // typeloc all along -- rather than a recursion, so we don't follow
1099   // the normal CRTP plan of going through
1100   // getDerived().TraverseTypeLoc.  If we did, we'd be traversing
1101   // twice for the same type (once as a QualifiedTypeLoc version of
1102   // the type, once as an UnqualifiedTypeLoc version of the type),
1103   // which in effect means we'd call VisitTypeLoc twice with the
1104   // 'same' type.  This solves that problem, at the cost of never
1105   // seeing the qualified version of the type (unless the client
1106   // subclasses TraverseQualifiedTypeLoc themselves).  It's not a
1107   // perfect solution.  A perfect solution probably requires making
1108   // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1109   // wrapper around Type* -- rather than being its own class in the
1110   // type hierarchy.
1111   return TraverseTypeLoc(TL.getUnqualifiedLoc());
1112 }
1113 
1114 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1115 
1116 // FIXME: ComplexTypeLoc is unfinished
1117 DEF_TRAVERSE_TYPELOC(ComplexType, {
1118   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1119 })
1120 
1121 DEF_TRAVERSE_TYPELOC(PointerType,
1122                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1123 
1124 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1125                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1126 
1127 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1128                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1129 
1130 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1131                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1132 
1133 // We traverse this in the type case as well, but how is it not reached through
1134 // the pointee type?
1135 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1136   if (auto *TSI = TL.getClassTInfo())
1137     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1138   else
1139     TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1140   TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1141 })
1142 
1143 DEF_TRAVERSE_TYPELOC(AdjustedType,
1144                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1145 
1146 DEF_TRAVERSE_TYPELOC(DecayedType,
1147                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1148 
1149 template <typename Derived>
1150 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1151   // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1152   TRY_TO(TraverseStmt(TL.getSizeExpr()));
1153   return true;
1154 }
1155 
1156 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1157   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1158   TRY_TO(TraverseArrayTypeLocHelper(TL));
1159 })
1160 
1161 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1162   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1163   TRY_TO(TraverseArrayTypeLocHelper(TL));
1164 })
1165 
1166 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1167   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1168   TRY_TO(TraverseArrayTypeLocHelper(TL));
1169 })
1170 
1171 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1172   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1173   TRY_TO(TraverseArrayTypeLocHelper(TL));
1174 })
1175 
1176 DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
1177   TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr()));
1178   TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType()));
1179 })
1180 
1181 // FIXME: order? why not size expr first?
1182 // FIXME: base VectorTypeLoc is unfinished
1183 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1184   if (TL.getTypePtr()->getSizeExpr())
1185     TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1186   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1187 })
1188 
1189 // FIXME: VectorTypeLoc is unfinished
1190 DEF_TRAVERSE_TYPELOC(VectorType, {
1191   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1192 })
1193 
1194 DEF_TRAVERSE_TYPELOC(DependentVectorType, {
1195   if (TL.getTypePtr()->getSizeExpr())
1196     TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1197   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1198 })
1199 
1200 // FIXME: size and attributes
1201 // FIXME: base VectorTypeLoc is unfinished
1202 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1203   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1204 })
1205 
1206 DEF_TRAVERSE_TYPELOC(ConstantMatrixType, {
1207   TRY_TO(TraverseStmt(TL.getAttrRowOperand()));
1208   TRY_TO(TraverseStmt(TL.getAttrColumnOperand()));
1209   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1210 })
1211 
1212 DEF_TRAVERSE_TYPELOC(DependentSizedMatrixType, {
1213   TRY_TO(TraverseStmt(TL.getAttrRowOperand()));
1214   TRY_TO(TraverseStmt(TL.getAttrColumnOperand()));
1215   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1216 })
1217 
1218 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1219                      { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1220 
1221 // FIXME: location of exception specifications (attributes?)
1222 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1223   TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1224 
1225   const FunctionProtoType *T = TL.getTypePtr();
1226 
1227   for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1228     if (TL.getParam(I)) {
1229       TRY_TO(TraverseDecl(TL.getParam(I)));
1230     } else if (I < T->getNumParams()) {
1231       TRY_TO(TraverseType(T->getParamType(I)));
1232     }
1233   }
1234 
1235   for (const auto &E : T->exceptions()) {
1236     TRY_TO(TraverseType(E));
1237   }
1238 
1239   if (Expr *NE = T->getNoexceptExpr())
1240     TRY_TO(TraverseStmt(NE));
1241 })
1242 
1243 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1244 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1245 
1246 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1247                      { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1248 
1249 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1250   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1251 })
1252 
1253 // FIXME: location of underlying expr
1254 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1255   TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1256 })
1257 
1258 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1259   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1260 })
1261 
1262 DEF_TRAVERSE_TYPELOC(AutoType, {
1263   TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1264   if (TL.isConstrained()) {
1265     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc()));
1266     TRY_TO(TraverseDeclarationNameInfo(TL.getConceptNameInfo()));
1267     for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
1268       TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1269   }
1270 })
1271 
1272 DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, {
1273   TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1274   TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1275 })
1276 
1277 DEF_TRAVERSE_TYPELOC(RecordType, {})
1278 DEF_TRAVERSE_TYPELOC(EnumType, {})
1279 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1280 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {
1281   TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType()));
1282 })
1283 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {
1284   TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack()));
1285 })
1286 
1287 // FIXME: use the loc for the template name?
1288 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1289   TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1290   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1291     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1292   }
1293 })
1294 
1295 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1296 
1297 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1298 
1299 DEF_TRAVERSE_TYPELOC(MacroQualifiedType,
1300                      { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1301 
1302 DEF_TRAVERSE_TYPELOC(AttributedType,
1303                      { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1304 
1305 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1306   if (TL.getQualifierLoc()) {
1307     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1308   }
1309   TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1310 })
1311 
1312 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1313   TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1314 })
1315 
1316 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1317   if (TL.getQualifierLoc()) {
1318     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1319   }
1320 
1321   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1322     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1323   }
1324 })
1325 
1326 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1327                      { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1328 
1329 DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {})
1330 
1331 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1332 
1333 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1334   // We have to watch out here because an ObjCInterfaceType's base
1335   // type is itself.
1336   if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1337     TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1338   for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1339     TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1340 })
1341 
1342 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1343                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1344 
1345 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1346 
1347 DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1348 
1349 DEF_TRAVERSE_TYPELOC(ExtIntType, {})
1350 DEF_TRAVERSE_TYPELOC(DependentExtIntType, {
1351   TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
1352 })
1353 
1354 #undef DEF_TRAVERSE_TYPELOC
1355 
1356 // ----------------- Decl traversal -----------------
1357 //
1358 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1359 // the children that come from the DeclContext associated with it.
1360 // Therefore each Traverse* only needs to worry about children other
1361 // than those.
1362 
1363 template <typename Derived>
1364 bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext(
1365     const Decl *Child) {
1366   // BlockDecls are traversed through BlockExprs,
1367   // CapturedDecls are traversed through CapturedStmts.
1368   if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child))
1369     return true;
1370   // Lambda classes are traversed through LambdaExprs.
1371   if (const CXXRecordDecl* Cls = dyn_cast<CXXRecordDecl>(Child))
1372     return Cls->isLambda();
1373   return false;
1374 }
1375 
1376 template <typename Derived>
1377 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1378   if (!DC)
1379     return true;
1380 
1381   for (auto *Child : DC->decls()) {
1382     if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1383       TRY_TO(TraverseDecl(Child));
1384   }
1385 
1386   return true;
1387 }
1388 
1389 // This macro makes available a variable D, the passed-in decl.
1390 #define DEF_TRAVERSE_DECL(DECL, CODE)                                          \
1391   template <typename Derived>                                                  \
1392   bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) {                 \
1393     bool ShouldVisitChildren = true;                                           \
1394     bool ReturnValue = true;                                                   \
1395     if (!getDerived().shouldTraversePostOrder())                               \
1396       TRY_TO(WalkUpFrom##DECL(D));                                             \
1397     { CODE; }                                                                  \
1398     if (ReturnValue && ShouldVisitChildren)                                    \
1399       TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));             \
1400     if (ReturnValue) {                                                         \
1401       /* Visit any attributes attached to this declaration. */                 \
1402       for (auto *I : D->attrs())                                               \
1403         TRY_TO(getDerived().TraverseAttr(I));                                  \
1404     }                                                                          \
1405     if (ReturnValue && getDerived().shouldTraversePostOrder())                 \
1406       TRY_TO(WalkUpFrom##DECL(D));                                             \
1407     return ReturnValue;                                                        \
1408   }
1409 
1410 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1411 
1412 DEF_TRAVERSE_DECL(BlockDecl, {
1413   if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1414     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1415   TRY_TO(TraverseStmt(D->getBody()));
1416   for (const auto &I : D->captures()) {
1417     if (I.hasCopyExpr()) {
1418       TRY_TO(TraverseStmt(I.getCopyExpr()));
1419     }
1420   }
1421   ShouldVisitChildren = false;
1422 })
1423 
1424 DEF_TRAVERSE_DECL(CapturedDecl, {
1425   TRY_TO(TraverseStmt(D->getBody()));
1426   ShouldVisitChildren = false;
1427 })
1428 
1429 DEF_TRAVERSE_DECL(EmptyDecl, {})
1430 
1431 DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
1432   TRY_TO(TraverseStmt(D->getTemporaryExpr()));
1433 })
1434 
1435 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1436                   { TRY_TO(TraverseStmt(D->getAsmString())); })
1437 
1438 DEF_TRAVERSE_DECL(ImportDecl, {})
1439 
1440 DEF_TRAVERSE_DECL(FriendDecl, {
1441   // Friend is either decl or a type.
1442   if (D->getFriendType())
1443     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1444   else
1445     TRY_TO(TraverseDecl(D->getFriendDecl()));
1446 })
1447 
1448 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1449   if (D->getFriendType())
1450     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1451   else
1452     TRY_TO(TraverseDecl(D->getFriendDecl()));
1453   for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1454     TemplateParameterList *TPL = D->getTemplateParameterList(I);
1455     for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1456          ITPL != ETPL; ++ITPL) {
1457       TRY_TO(TraverseDecl(*ITPL));
1458     }
1459   }
1460 })
1461 
1462 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1463   TRY_TO(TraverseDecl(D->getSpecialization()));
1464 
1465   if (D->hasExplicitTemplateArgs()) {
1466     TRY_TO(TraverseTemplateArgumentLocsHelper(
1467         D->getTemplateArgsAsWritten()->getTemplateArgs(),
1468         D->getTemplateArgsAsWritten()->NumTemplateArgs));
1469   }
1470 })
1471 
1472 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1473 
1474 DEF_TRAVERSE_DECL(ExportDecl, {})
1475 
1476 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1477                                         })
1478 
1479 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1480   TRY_TO(TraverseStmt(D->getAssertExpr()));
1481   TRY_TO(TraverseStmt(D->getMessage()));
1482 })
1483 
1484 DEF_TRAVERSE_DECL(
1485     TranslationUnitDecl,
1486     {// Code in an unnamed namespace shows up automatically in
1487      // decls_begin()/decls_end().  Thus we don't need to recurse on
1488      // D->getAnonymousNamespace().
1489     })
1490 
1491 DEF_TRAVERSE_DECL(PragmaCommentDecl, {})
1492 
1493 DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {})
1494 
1495 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1496 
1497 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1498   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1499 
1500   // We shouldn't traverse an aliased namespace, since it will be
1501   // defined (and, therefore, traversed) somewhere else.
1502   ShouldVisitChildren = false;
1503 })
1504 
1505 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1506                              })
1507 
1508 DEF_TRAVERSE_DECL(
1509     NamespaceDecl,
1510     {// Code in an unnamed namespace shows up automatically in
1511      // decls_begin()/decls_end().  Thus we don't need to recurse on
1512      // D->getAnonymousNamespace().
1513     })
1514 
1515 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1516                                            })
1517 
1518 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1519   if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1520     for (auto typeParam : *typeParamList) {
1521       TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1522     }
1523   }
1524 })
1525 
1526 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1527                                         })
1528 
1529 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1530                                           })
1531 
1532 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1533   if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1534     for (auto typeParam : *typeParamList) {
1535       TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1536     }
1537   }
1538 
1539   if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1540     TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1541   }
1542 })
1543 
1544 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1545                                     })
1546 
1547 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1548   if (D->getReturnTypeSourceInfo()) {
1549     TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1550   }
1551   for (ParmVarDecl *Parameter : D->parameters()) {
1552     TRY_TO(TraverseDecl(Parameter));
1553   }
1554   if (D->isThisDeclarationADefinition()) {
1555     TRY_TO(TraverseStmt(D->getBody()));
1556   }
1557   ShouldVisitChildren = false;
1558 })
1559 
1560 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1561   if (D->hasExplicitBound()) {
1562     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1563     // We shouldn't traverse D->getTypeForDecl(); it's a result of
1564     // declaring the type alias, not something that was written in the
1565     // source.
1566   }
1567 })
1568 
1569 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1570   if (D->getTypeSourceInfo())
1571     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1572   else
1573     TRY_TO(TraverseType(D->getType()));
1574   ShouldVisitChildren = false;
1575 })
1576 
1577 DEF_TRAVERSE_DECL(UsingDecl, {
1578   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1579   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1580 })
1581 
1582 DEF_TRAVERSE_DECL(UsingPackDecl, {})
1583 
1584 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1585   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1586 })
1587 
1588 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1589 
1590 DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {})
1591 
1592 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1593   for (auto *I : D->varlists()) {
1594     TRY_TO(TraverseStmt(I));
1595   }
1596  })
1597 
1598 DEF_TRAVERSE_DECL(OMPRequiresDecl, {
1599   for (auto *C : D->clauselists()) {
1600     TRY_TO(TraverseOMPClause(C));
1601   }
1602 })
1603 
1604 DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, {
1605   TRY_TO(TraverseStmt(D->getCombiner()));
1606   if (auto *Initializer = D->getInitializer())
1607     TRY_TO(TraverseStmt(Initializer));
1608   TRY_TO(TraverseType(D->getType()));
1609   return true;
1610 })
1611 
1612 DEF_TRAVERSE_DECL(OMPDeclareMapperDecl, {
1613   for (auto *C : D->clauselists())
1614     TRY_TO(TraverseOMPClause(C));
1615   TRY_TO(TraverseType(D->getType()));
1616   return true;
1617 })
1618 
1619 DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
1620 
1621 DEF_TRAVERSE_DECL(OMPAllocateDecl, {
1622   for (auto *I : D->varlists())
1623     TRY_TO(TraverseStmt(I));
1624   for (auto *C : D->clauselists())
1625     TRY_TO(TraverseOMPClause(C));
1626 })
1627 
1628 // A helper method for TemplateDecl's children.
1629 template <typename Derived>
1630 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1631     TemplateParameterList *TPL) {
1632   if (TPL) {
1633     for (NamedDecl *D : *TPL) {
1634       TRY_TO(TraverseDecl(D));
1635     }
1636     if (Expr *RequiresClause = TPL->getRequiresClause()) {
1637       TRY_TO(TraverseStmt(RequiresClause));
1638     }
1639   }
1640   return true;
1641 }
1642 
1643 template <typename Derived>
1644 template <typename T>
1645 bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) {
1646   for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) {
1647     TemplateParameterList *TPL = D->getTemplateParameterList(i);
1648     TraverseTemplateParameterListHelper(TPL);
1649   }
1650   return true;
1651 }
1652 
1653 template <typename Derived>
1654 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1655     ClassTemplateDecl *D) {
1656   for (auto *SD : D->specializations()) {
1657     for (auto *RD : SD->redecls()) {
1658       // We don't want to visit injected-class-names in this traversal.
1659       if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1660         continue;
1661 
1662       switch (
1663           cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1664       // Visit the implicit instantiations with the requested pattern.
1665       case TSK_Undeclared:
1666       case TSK_ImplicitInstantiation:
1667         TRY_TO(TraverseDecl(RD));
1668         break;
1669 
1670       // We don't need to do anything on an explicit instantiation
1671       // or explicit specialization because there will be an explicit
1672       // node for it elsewhere.
1673       case TSK_ExplicitInstantiationDeclaration:
1674       case TSK_ExplicitInstantiationDefinition:
1675       case TSK_ExplicitSpecialization:
1676         break;
1677       }
1678     }
1679   }
1680 
1681   return true;
1682 }
1683 
1684 template <typename Derived>
1685 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1686     VarTemplateDecl *D) {
1687   for (auto *SD : D->specializations()) {
1688     for (auto *RD : SD->redecls()) {
1689       switch (
1690           cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1691       case TSK_Undeclared:
1692       case TSK_ImplicitInstantiation:
1693         TRY_TO(TraverseDecl(RD));
1694         break;
1695 
1696       case TSK_ExplicitInstantiationDeclaration:
1697       case TSK_ExplicitInstantiationDefinition:
1698       case TSK_ExplicitSpecialization:
1699         break;
1700       }
1701     }
1702   }
1703 
1704   return true;
1705 }
1706 
1707 // A helper method for traversing the instantiations of a
1708 // function while skipping its specializations.
1709 template <typename Derived>
1710 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1711     FunctionTemplateDecl *D) {
1712   for (auto *FD : D->specializations()) {
1713     for (auto *RD : FD->redecls()) {
1714       switch (RD->getTemplateSpecializationKind()) {
1715       case TSK_Undeclared:
1716       case TSK_ImplicitInstantiation:
1717         // We don't know what kind of FunctionDecl this is.
1718         TRY_TO(TraverseDecl(RD));
1719         break;
1720 
1721       // FIXME: For now traverse explicit instantiations here. Change that
1722       // once they are represented as dedicated nodes in the AST.
1723       case TSK_ExplicitInstantiationDeclaration:
1724       case TSK_ExplicitInstantiationDefinition:
1725         TRY_TO(TraverseDecl(RD));
1726         break;
1727 
1728       case TSK_ExplicitSpecialization:
1729         break;
1730       }
1731     }
1732   }
1733 
1734   return true;
1735 }
1736 
1737 // This macro unifies the traversal of class, variable and function
1738 // template declarations.
1739 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND)                                   \
1740   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, {                              \
1741     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
1742     TRY_TO(TraverseDecl(D->getTemplatedDecl()));                               \
1743                                                                                \
1744     /* By default, we do not traverse the instantiations of                    \
1745        class templates since they do not appear in the user code. The          \
1746        following code optionally traverses them.                               \
1747                                                                                \
1748        We only traverse the class instantiations when we see the canonical     \
1749        declaration of the template, to ensure we only visit them once. */      \
1750     if (getDerived().shouldVisitTemplateInstantiations() &&                    \
1751         D == D->getCanonicalDecl())                                            \
1752       TRY_TO(TraverseTemplateInstantiations(D));                               \
1753                                                                                \
1754     /* Note that getInstantiatedFromMemberTemplate() is just a link            \
1755        from a template instantiation back to the template from which           \
1756        it was instantiated, and thus should not be traversed. */               \
1757   })
1758 
1759 DEF_TRAVERSE_TMPL_DECL(Class)
1760 DEF_TRAVERSE_TMPL_DECL(Var)
1761 DEF_TRAVERSE_TMPL_DECL(Function)
1762 
1763 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1764   // D is the "T" in something like
1765   //   template <template <typename> class T> class container { };
1766   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1767   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1768     TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1769   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1770 })
1771 
1772 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1773   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1774 })
1775 
1776 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1777   // D is the "T" in something like "template<typename T> class vector;"
1778   if (D->getTypeForDecl())
1779     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1780   if (const auto *TC = D->getTypeConstraint())
1781     TRY_TO(TraverseConceptReference(*TC));
1782   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1783     TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1784 })
1785 
1786 DEF_TRAVERSE_DECL(TypedefDecl, {
1787   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1788   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1789   // declaring the typedef, not something that was written in the
1790   // source.
1791 })
1792 
1793 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1794   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1795   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1796   // declaring the type alias, not something that was written in the
1797   // source.
1798 })
1799 
1800 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1801   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1802   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1803 })
1804 
1805 DEF_TRAVERSE_DECL(ConceptDecl, {
1806   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1807   TRY_TO(TraverseStmt(D->getConstraintExpr()));
1808 })
1809 
1810 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1811   // A dependent using declaration which was marked with 'typename'.
1812   //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1813   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1814   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1815   // declaring the type, not something that was written in the
1816   // source.
1817 })
1818 
1819 DEF_TRAVERSE_DECL(EnumDecl, {
1820   TRY_TO(TraverseDeclTemplateParameterLists(D));
1821 
1822   if (D->getTypeForDecl())
1823     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1824 
1825   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1826   // The enumerators are already traversed by
1827   // decls_begin()/decls_end().
1828 })
1829 
1830 // Helper methods for RecordDecl and its children.
1831 template <typename Derived>
1832 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1833   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1834   // declaring the type, not something that was written in the source.
1835 
1836   TRY_TO(TraverseDeclTemplateParameterLists(D));
1837   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1838   return true;
1839 }
1840 
1841 template <typename Derived>
1842 bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier(
1843     const CXXBaseSpecifier &Base) {
1844   TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc()));
1845   return true;
1846 }
1847 
1848 template <typename Derived>
1849 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1850   if (!TraverseRecordHelper(D))
1851     return false;
1852   if (D->isCompleteDefinition()) {
1853     for (const auto &I : D->bases()) {
1854       TRY_TO(TraverseCXXBaseSpecifier(I));
1855     }
1856     // We don't traverse the friends or the conversions, as they are
1857     // already in decls_begin()/decls_end().
1858   }
1859   return true;
1860 }
1861 
1862 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1863 
1864 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1865 
1866 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND)                              \
1867   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
1868     /* For implicit instantiations ("set<int> x;"), we don't want to           \
1869        recurse at all, since the instatiated template isn't written in         \
1870        the source code anywhere.  (Note the instatiated *type* --              \
1871        set<int> -- is written, and will still get a callback of                \
1872        TemplateSpecializationType).  For explicit instantiations               \
1873        ("template set<int>;"), we do need a callback, since this               \
1874        is the only callback that's made for this instantiation.                \
1875        We use getTypeAsWritten() to distinguish. */                            \
1876     if (TypeSourceInfo *TSI = D->getTypeAsWritten())                           \
1877       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
1878                                                                                \
1879     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));              \
1880     if (!getDerived().shouldVisitTemplateInstantiations() &&                   \
1881         D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)      \
1882       /* Returning from here skips traversing the                              \
1883          declaration context of the *TemplateSpecializationDecl                \
1884          (embedded in the DEF_TRAVERSE_DECL() macro)                           \
1885          which contains the instantiated members of the template. */           \
1886       return true;                                                             \
1887   })
1888 
1889 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1890 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1891 
1892 template <typename Derived>
1893 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1894     const TemplateArgumentLoc *TAL, unsigned Count) {
1895   for (unsigned I = 0; I < Count; ++I) {
1896     TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1897   }
1898   return true;
1899 }
1900 
1901 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
1902   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
1903     /* The partial specialization. */                                          \
1904     if (TemplateParameterList *TPL = D->getTemplateParameters()) {             \
1905       for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();   \
1906            I != E; ++I) {                                                      \
1907         TRY_TO(TraverseDecl(*I));                                              \
1908       }                                                                        \
1909     }                                                                          \
1910     /* The args that remains unspecialized. */                                 \
1911     TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
1912         D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
1913         D->getTemplateArgsAsWritten()->NumTemplateArgs));                      \
1914                                                                                \
1915     /* Don't need the *TemplatePartialSpecializationHelper, even               \
1916        though that's our parent class -- we already visit all the              \
1917        template args here. */                                                  \
1918     TRY_TO(Traverse##DECLKIND##Helper(D));                                     \
1919                                                                                \
1920     /* Instantiations will have been visited with the primary template. */     \
1921   })
1922 
1923 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1924 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1925 
1926 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1927 
1928 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1929   // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1930   //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
1931   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1932   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1933 })
1934 
1935 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1936 
1937 template <typename Derived>
1938 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1939   TRY_TO(TraverseDeclTemplateParameterLists(D));
1940   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1941   if (D->getTypeSourceInfo())
1942     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1943   else
1944     TRY_TO(TraverseType(D->getType()));
1945   return true;
1946 }
1947 
1948 DEF_TRAVERSE_DECL(DecompositionDecl, {
1949   TRY_TO(TraverseVarHelper(D));
1950   for (auto *Binding : D->bindings()) {
1951     TRY_TO(TraverseDecl(Binding));
1952   }
1953 })
1954 
1955 DEF_TRAVERSE_DECL(BindingDecl, {
1956   if (getDerived().shouldVisitImplicitCode())
1957     TRY_TO(TraverseStmt(D->getBinding()));
1958 })
1959 
1960 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1961 
1962 DEF_TRAVERSE_DECL(MSGuidDecl, {})
1963 
1964 DEF_TRAVERSE_DECL(FieldDecl, {
1965   TRY_TO(TraverseDeclaratorHelper(D));
1966   if (D->isBitField())
1967     TRY_TO(TraverseStmt(D->getBitWidth()));
1968   else if (D->hasInClassInitializer())
1969     TRY_TO(TraverseStmt(D->getInClassInitializer()));
1970 })
1971 
1972 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1973   TRY_TO(TraverseDeclaratorHelper(D));
1974   if (D->isBitField())
1975     TRY_TO(TraverseStmt(D->getBitWidth()));
1976   // FIXME: implement the rest.
1977 })
1978 
1979 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1980   TRY_TO(TraverseDeclaratorHelper(D));
1981   if (D->isBitField())
1982     TRY_TO(TraverseStmt(D->getBitWidth()));
1983   // FIXME: implement the rest.
1984 })
1985 
1986 template <typename Derived>
1987 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1988   TRY_TO(TraverseDeclTemplateParameterLists(D));
1989   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1990   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1991 
1992   // If we're an explicit template specialization, iterate over the
1993   // template args that were explicitly specified.  If we were doing
1994   // this in typing order, we'd do it between the return type and
1995   // the function args, but both are handled by the FunctionTypeLoc
1996   // above, so we have to choose one side.  I've decided to do before.
1997   if (const FunctionTemplateSpecializationInfo *FTSI =
1998           D->getTemplateSpecializationInfo()) {
1999     if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
2000         FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
2001       // A specialization might not have explicit template arguments if it has
2002       // a templated return type and concrete arguments.
2003       if (const ASTTemplateArgumentListInfo *TALI =
2004               FTSI->TemplateArgumentsAsWritten) {
2005         TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
2006                                                   TALI->NumTemplateArgs));
2007       }
2008     }
2009   }
2010 
2011   // Visit the function type itself, which can be either
2012   // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
2013   // also covers the return type and the function parameters,
2014   // including exception specifications.
2015   if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
2016     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2017   } else if (getDerived().shouldVisitImplicitCode()) {
2018     // Visit parameter variable declarations of the implicit function
2019     // if the traverser is visiting implicit code. Parameter variable
2020     // declarations do not have valid TypeSourceInfo, so to visit them
2021     // we need to traverse the declarations explicitly.
2022     for (ParmVarDecl *Parameter : D->parameters()) {
2023       TRY_TO(TraverseDecl(Parameter));
2024     }
2025   }
2026 
2027   // Visit the trailing requires clause, if any.
2028   if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
2029     TRY_TO(TraverseStmt(TrailingRequiresClause));
2030   }
2031 
2032   if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2033     // Constructor initializers.
2034     for (auto *I : Ctor->inits()) {
2035       if (I->isWritten() || getDerived().shouldVisitImplicitCode())
2036         TRY_TO(TraverseConstructorInitializer(I));
2037     }
2038   }
2039 
2040   bool VisitBody =
2041       D->isThisDeclarationADefinition() &&
2042       // Don't visit the function body if the function definition is generated
2043       // by clang.
2044       (!D->isDefaulted() || getDerived().shouldVisitImplicitCode());
2045 
2046   if (VisitBody) {
2047     TRY_TO(TraverseStmt(D->getBody())); // Function body.
2048   }
2049   return true;
2050 }
2051 
2052 DEF_TRAVERSE_DECL(FunctionDecl, {
2053   // We skip decls_begin/decls_end, which are already covered by
2054   // TraverseFunctionHelper().
2055   ShouldVisitChildren = false;
2056   ReturnValue = TraverseFunctionHelper(D);
2057 })
2058 
2059 DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
2060   // We skip decls_begin/decls_end, which are already covered by
2061   // TraverseFunctionHelper().
2062   ShouldVisitChildren = false;
2063   ReturnValue = TraverseFunctionHelper(D);
2064 })
2065 
2066 DEF_TRAVERSE_DECL(CXXMethodDecl, {
2067   // We skip decls_begin/decls_end, which are already covered by
2068   // TraverseFunctionHelper().
2069   ShouldVisitChildren = false;
2070   ReturnValue = TraverseFunctionHelper(D);
2071 })
2072 
2073 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
2074   // We skip decls_begin/decls_end, which are already covered by
2075   // TraverseFunctionHelper().
2076   ShouldVisitChildren = false;
2077   ReturnValue = TraverseFunctionHelper(D);
2078 })
2079 
2080 // CXXConversionDecl is the declaration of a type conversion operator.
2081 // It's not a cast expression.
2082 DEF_TRAVERSE_DECL(CXXConversionDecl, {
2083   // We skip decls_begin/decls_end, which are already covered by
2084   // TraverseFunctionHelper().
2085   ShouldVisitChildren = false;
2086   ReturnValue = TraverseFunctionHelper(D);
2087 })
2088 
2089 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
2090   // We skip decls_begin/decls_end, which are already covered by
2091   // TraverseFunctionHelper().
2092   ShouldVisitChildren = false;
2093   ReturnValue = TraverseFunctionHelper(D);
2094 })
2095 
2096 template <typename Derived>
2097 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
2098   TRY_TO(TraverseDeclaratorHelper(D));
2099   // Default params are taken care of when we traverse the ParmVarDecl.
2100   if (!isa<ParmVarDecl>(D) &&
2101       (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
2102     TRY_TO(TraverseStmt(D->getInit()));
2103   return true;
2104 }
2105 
2106 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
2107 
2108 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
2109 
2110 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
2111   // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
2112   TRY_TO(TraverseDeclaratorHelper(D));
2113   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
2114     TRY_TO(TraverseStmt(D->getDefaultArgument()));
2115 })
2116 
2117 DEF_TRAVERSE_DECL(ParmVarDecl, {
2118   TRY_TO(TraverseVarHelper(D));
2119 
2120   if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
2121       !D->hasUnparsedDefaultArg())
2122     TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
2123 
2124   if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
2125       !D->hasUnparsedDefaultArg())
2126     TRY_TO(TraverseStmt(D->getDefaultArg()));
2127 })
2128 
2129 DEF_TRAVERSE_DECL(RequiresExprBodyDecl, {})
2130 
2131 #undef DEF_TRAVERSE_DECL
2132 
2133 // ----------------- Stmt traversal -----------------
2134 //
2135 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
2136 // over the children defined in children() (every stmt defines these,
2137 // though sometimes the range is empty).  Each individual Traverse*
2138 // method only needs to worry about children other than those.  To see
2139 // what children() does for a given class, see, e.g.,
2140 //   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
2141 
2142 // This macro makes available a variable S, the passed-in stmt.
2143 #define DEF_TRAVERSE_STMT(STMT, CODE)                                          \
2144   template <typename Derived>                                                  \
2145   bool RecursiveASTVisitor<Derived>::Traverse##STMT(                           \
2146       STMT *S, DataRecursionQueue *Queue) {                                    \
2147     bool ShouldVisitChildren = true;                                           \
2148     bool ReturnValue = true;                                                   \
2149     if (!getDerived().shouldTraversePostOrder())                               \
2150       TRY_TO(WalkUpFrom##STMT(S));                                             \
2151     { CODE; }                                                                  \
2152     if (ShouldVisitChildren) {                                                 \
2153       for (Stmt * SubStmt : getDerived().getStmtChildren(S)) {                 \
2154         TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);                              \
2155       }                                                                        \
2156     }                                                                          \
2157     /* Call WalkUpFrom if TRY_TO_TRAVERSE_OR_ENQUEUE_STMT has traversed the    \
2158      * children already. If TRY_TO_TRAVERSE_OR_ENQUEUE_STMT only enqueued the  \
2159      * children, PostVisitStmt will call WalkUpFrom after we are done visiting \
2160      * children. */                                                            \
2161     if (!Queue && ReturnValue && getDerived().shouldTraversePostOrder()) {     \
2162       TRY_TO(WalkUpFrom##STMT(S));                                             \
2163     }                                                                          \
2164     return ReturnValue;                                                        \
2165   }
2166 
2167 DEF_TRAVERSE_STMT(GCCAsmStmt, {
2168   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
2169   for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
2170     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
2171   }
2172   for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
2173     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
2174   }
2175   for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
2176     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
2177   }
2178   // children() iterates over inputExpr and outputExpr.
2179 })
2180 
2181 DEF_TRAVERSE_STMT(
2182     MSAsmStmt,
2183     {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
2184      // added this needs to be implemented.
2185     })
2186 
2187 DEF_TRAVERSE_STMT(CXXCatchStmt, {
2188   TRY_TO(TraverseDecl(S->getExceptionDecl()));
2189   // children() iterates over the handler block.
2190 })
2191 
2192 DEF_TRAVERSE_STMT(DeclStmt, {
2193   for (auto *I : S->decls()) {
2194     TRY_TO(TraverseDecl(I));
2195   }
2196   // Suppress the default iteration over children() by
2197   // returning.  Here's why: A DeclStmt looks like 'type var [=
2198   // initializer]'.  The decls above already traverse over the
2199   // initializers, so we don't have to do it again (which
2200   // children() would do).
2201   ShouldVisitChildren = false;
2202 })
2203 
2204 // These non-expr stmts (most of them), do not need any action except
2205 // iterating over the children.
2206 DEF_TRAVERSE_STMT(BreakStmt, {})
2207 DEF_TRAVERSE_STMT(CXXTryStmt, {})
2208 DEF_TRAVERSE_STMT(CaseStmt, {})
2209 DEF_TRAVERSE_STMT(CompoundStmt, {})
2210 DEF_TRAVERSE_STMT(ContinueStmt, {})
2211 DEF_TRAVERSE_STMT(DefaultStmt, {})
2212 DEF_TRAVERSE_STMT(DoStmt, {})
2213 DEF_TRAVERSE_STMT(ForStmt, {})
2214 DEF_TRAVERSE_STMT(GotoStmt, {})
2215 DEF_TRAVERSE_STMT(IfStmt, {})
2216 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
2217 DEF_TRAVERSE_STMT(LabelStmt, {})
2218 DEF_TRAVERSE_STMT(AttributedStmt, {})
2219 DEF_TRAVERSE_STMT(NullStmt, {})
2220 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
2221 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
2222 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
2223 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
2224 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
2225 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
2226 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
2227 
2228 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
2229   if (!getDerived().shouldVisitImplicitCode()) {
2230     if (S->getInit())
2231       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInit());
2232     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
2233     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
2234     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2235     // Visit everything else only if shouldVisitImplicitCode().
2236     ShouldVisitChildren = false;
2237   }
2238 })
2239 
2240 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
2241   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2242   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2243 })
2244 
2245 DEF_TRAVERSE_STMT(ReturnStmt, {})
2246 DEF_TRAVERSE_STMT(SwitchStmt, {})
2247 DEF_TRAVERSE_STMT(WhileStmt, {})
2248 
2249 DEF_TRAVERSE_STMT(ConstantExpr, {})
2250 
2251 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
2252   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2253   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2254   if (S->hasExplicitTemplateArgs()) {
2255     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2256                                               S->getNumTemplateArgs()));
2257   }
2258 })
2259 
2260 DEF_TRAVERSE_STMT(DeclRefExpr, {
2261   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2262   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2263   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2264                                             S->getNumTemplateArgs()));
2265 })
2266 
2267 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
2268   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2269   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2270   if (S->hasExplicitTemplateArgs()) {
2271     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2272                                               S->getNumTemplateArgs()));
2273   }
2274 })
2275 
2276 DEF_TRAVERSE_STMT(MemberExpr, {
2277   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2278   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2279   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2280                                             S->getNumTemplateArgs()));
2281 })
2282 
2283 DEF_TRAVERSE_STMT(
2284     ImplicitCastExpr,
2285     {// We don't traverse the cast type, as it's not written in the
2286      // source code.
2287     })
2288 
2289 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2290   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2291 })
2292 
2293 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2294   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2295 })
2296 
2297 DEF_TRAVERSE_STMT(CXXAddrspaceCastExpr, {
2298   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2299 })
2300 
2301 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2302   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2303 })
2304 
2305 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2306   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2307 })
2308 
2309 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2310   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2311 })
2312 
2313 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2314   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2315 })
2316 
2317 DEF_TRAVERSE_STMT(BuiltinBitCastExpr, {
2318   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2319 })
2320 
2321 template <typename Derived>
2322 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2323     InitListExpr *S, DataRecursionQueue *Queue) {
2324   if (S) {
2325     // Skip this if we traverse postorder. We will visit it later
2326     // in PostVisitStmt.
2327     if (!getDerived().shouldTraversePostOrder())
2328       TRY_TO(WalkUpFromInitListExpr(S));
2329 
2330     // All we need are the default actions.  FIXME: use a helper function.
2331     for (Stmt *SubStmt : S->children()) {
2332       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2333     }
2334 
2335     if (!Queue && getDerived().shouldTraversePostOrder())
2336       TRY_TO(WalkUpFromInitListExpr(S));
2337   }
2338   return true;
2339 }
2340 
2341 template<typename Derived>
2342 bool RecursiveASTVisitor<Derived>::TraverseConceptReference(
2343     const ConceptReference &C) {
2344   TRY_TO(TraverseNestedNameSpecifierLoc(C.getNestedNameSpecifierLoc()));
2345   TRY_TO(TraverseDeclarationNameInfo(C.getConceptNameInfo()));
2346   if (C.hasExplicitTemplateArgs())
2347     TRY_TO(TraverseTemplateArgumentLocsHelper(
2348         C.getTemplateArgsAsWritten()->getTemplateArgs(),
2349         C.getTemplateArgsAsWritten()->NumTemplateArgs));
2350   return true;
2351 }
2352 
2353 // If shouldVisitImplicitCode() returns false, this method traverses only the
2354 // syntactic form of InitListExpr.
2355 // If shouldVisitImplicitCode() return true, this method is called once for
2356 // each pair of syntactic and semantic InitListExpr, and it traverses the
2357 // subtrees defined by the two forms. This may cause some of the children to be
2358 // visited twice, if they appear both in the syntactic and the semantic form.
2359 //
2360 // There is no guarantee about which form \p S takes when this method is called.
2361 template <typename Derived>
2362 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(
2363     InitListExpr *S, DataRecursionQueue *Queue) {
2364   if (S->isSemanticForm() && S->isSyntacticForm()) {
2365     // `S` does not have alternative forms, traverse only once.
2366     TRY_TO(TraverseSynOrSemInitListExpr(S, Queue));
2367     return true;
2368   }
2369   TRY_TO(TraverseSynOrSemInitListExpr(
2370       S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2371   if (getDerived().shouldVisitImplicitCode()) {
2372     // Only visit the semantic form if the clients are interested in implicit
2373     // compiler-generated.
2374     TRY_TO(TraverseSynOrSemInitListExpr(
2375         S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2376   }
2377   return true;
2378 }
2379 
2380 // GenericSelectionExpr is a special case because the types and expressions
2381 // are interleaved.  We also need to watch out for null types (default
2382 // generic associations).
2383 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2384   TRY_TO(TraverseStmt(S->getControllingExpr()));
2385   for (const GenericSelectionExpr::Association Assoc : S->associations()) {
2386     if (TypeSourceInfo *TSI = Assoc.getTypeSourceInfo())
2387       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2388     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(Assoc.getAssociationExpr());
2389   }
2390   ShouldVisitChildren = false;
2391 })
2392 
2393 // PseudoObjectExpr is a special case because of the weirdness with
2394 // syntactic expressions and opaque values.
2395 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2396   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
2397   for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2398                                             e = S->semantics_end();
2399        i != e; ++i) {
2400     Expr *sub = *i;
2401     if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2402       sub = OVE->getSourceExpr();
2403     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
2404   }
2405   ShouldVisitChildren = false;
2406 })
2407 
2408 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2409   // This is called for code like 'return T()' where T is a built-in
2410   // (i.e. non-class) type.
2411   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2412 })
2413 
2414 DEF_TRAVERSE_STMT(CXXNewExpr, {
2415   // The child-iterator will pick up the other arguments.
2416   TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2417 })
2418 
2419 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2420   // The child-iterator will pick up the expression representing
2421   // the field.
2422   // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2423   // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2424   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2425 })
2426 
2427 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2428   // The child-iterator will pick up the arg if it's an expression,
2429   // but not if it's a type.
2430   if (S->isArgumentType())
2431     TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2432 })
2433 
2434 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2435   // The child-iterator will pick up the arg if it's an expression,
2436   // but not if it's a type.
2437   if (S->isTypeOperand())
2438     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2439 })
2440 
2441 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2442   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2443 })
2444 
2445 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
2446 
2447 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2448   // The child-iterator will pick up the arg if it's an expression,
2449   // but not if it's a type.
2450   if (S->isTypeOperand())
2451     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2452 })
2453 
2454 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2455   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2456     TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2457 })
2458 
2459 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2460   TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2461 })
2462 
2463 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2464                   { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2465 
2466 DEF_TRAVERSE_STMT(VAArgExpr, {
2467   // The child-iterator will pick up the expression argument.
2468   TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2469 })
2470 
2471 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2472   // This is called for code like 'return T()' where T is a class type.
2473   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2474 })
2475 
2476 // Walk only the visible parts of lambda expressions.
2477 DEF_TRAVERSE_STMT(LambdaExpr, {
2478   // Visit the capture list.
2479   for (unsigned I = 0, N = S->capture_size(); I != N; ++I) {
2480     const LambdaCapture *C = S->capture_begin() + I;
2481     if (C->isExplicit() || getDerived().shouldVisitImplicitCode()) {
2482       TRY_TO(TraverseLambdaCapture(S, C, S->capture_init_begin()[I]));
2483     }
2484   }
2485 
2486   if (getDerived().shouldVisitImplicitCode()) {
2487     // The implicit model is simple: everything else is in the lambda class.
2488     TRY_TO(TraverseDecl(S->getLambdaClass()));
2489   } else {
2490     // We need to poke around to find the bits that might be explicitly written.
2491     TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2492     FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>();
2493 
2494     for (Decl *D : S->getExplicitTemplateParameters()) {
2495       // Visit explicit template parameters.
2496       TRY_TO(TraverseDecl(D));
2497     }
2498     if (S->hasExplicitParameters()) {
2499       // Visit parameters.
2500       for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
2501         TRY_TO(TraverseDecl(Proto.getParam(I)));
2502     }
2503     if (S->hasExplicitResultType())
2504       TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2505 
2506     auto *T = Proto.getTypePtr();
2507     for (const auto &E : T->exceptions())
2508       TRY_TO(TraverseType(E));
2509 
2510     if (Expr *NE = T->getNoexceptExpr())
2511       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2512 
2513     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2514   }
2515   ShouldVisitChildren = false;
2516 })
2517 
2518 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2519   // This is called for code like 'T()', where T is a template argument.
2520   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2521 })
2522 
2523 // These expressions all might take explicit template arguments.
2524 // We traverse those if so.  FIXME: implement these.
2525 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2526 DEF_TRAVERSE_STMT(CallExpr, {})
2527 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2528 
2529 // These exprs (most of them), do not need any action except iterating
2530 // over the children.
2531 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2532 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2533 DEF_TRAVERSE_STMT(MatrixSubscriptExpr, {})
2534 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2535 DEF_TRAVERSE_STMT(OMPArrayShapingExpr, {})
2536 DEF_TRAVERSE_STMT(OMPIteratorExpr, {})
2537 
2538 DEF_TRAVERSE_STMT(BlockExpr, {
2539   TRY_TO(TraverseDecl(S->getBlockDecl()));
2540   return true; // no child statements to loop through.
2541 })
2542 
2543 DEF_TRAVERSE_STMT(ChooseExpr, {})
2544 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2545   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2546 })
2547 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2548 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2549 
2550 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {
2551   if (getDerived().shouldVisitImplicitCode())
2552     TRY_TO(TraverseStmt(S->getExpr()));
2553 })
2554 
2555 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2556 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2557 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2558 DEF_TRAVERSE_STMT(CXXInheritedCtorInitExpr, {})
2559 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2560 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2561 
2562 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2563   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2564   if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2565     TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2566   if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2567     TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2568 })
2569 
2570 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2571 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2572 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2573 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2574 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2575 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2576 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2577 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2578 DEF_TRAVERSE_STMT(NoInitExpr, {})
2579 DEF_TRAVERSE_STMT(ArrayInitLoopExpr, {
2580   // FIXME: The source expression of the OVE should be listed as
2581   // a child of the ArrayInitLoopExpr.
2582   if (OpaqueValueExpr *OVE = S->getCommonExpr())
2583     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr());
2584 })
2585 DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {})
2586 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2587 
2588 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2589   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2590     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2591 })
2592 
2593 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2594 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2595 
2596 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2597   if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2598     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2599 })
2600 
2601 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2602 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2603 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2604 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2605 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2606 
2607 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2608   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2609 })
2610 
2611 DEF_TRAVERSE_STMT(ObjCAvailabilityCheckExpr, {})
2612 DEF_TRAVERSE_STMT(ParenExpr, {})
2613 DEF_TRAVERSE_STMT(ParenListExpr, {})
2614 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2615 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2616 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2617 DEF_TRAVERSE_STMT(StmtExpr, {})
2618 DEF_TRAVERSE_STMT(SourceLocExpr, {})
2619 
2620 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2621   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2622   if (S->hasExplicitTemplateArgs()) {
2623     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2624                                               S->getNumTemplateArgs()));
2625   }
2626 })
2627 
2628 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2629   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2630   if (S->hasExplicitTemplateArgs()) {
2631     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2632                                               S->getNumTemplateArgs()));
2633   }
2634 })
2635 
2636 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2637 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2638 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2639 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2640 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2641 
2642 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2643 DEF_TRAVERSE_STMT(CXXRewrittenBinaryOperator, {
2644   if (!getDerived().shouldVisitImplicitCode()) {
2645     CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
2646         S->getDecomposedForm();
2647     TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.LHS)));
2648     TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.RHS)));
2649     ShouldVisitChildren = false;
2650   }
2651 })
2652 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2653 DEF_TRAVERSE_STMT(TypoExpr, {})
2654 DEF_TRAVERSE_STMT(RecoveryExpr, {})
2655 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2656 
2657 // These operators (all of them) do not need any action except
2658 // iterating over the children.
2659 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2660 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2661 DEF_TRAVERSE_STMT(UnaryOperator, {})
2662 DEF_TRAVERSE_STMT(BinaryOperator, {})
2663 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2664 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2665 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2666 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2667 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2668 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2669 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2670 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2671 DEF_TRAVERSE_STMT(AtomicExpr, {})
2672 
2673 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {
2674   if (S->getLifetimeExtendedTemporaryDecl()) {
2675     TRY_TO(TraverseLifetimeExtendedTemporaryDecl(
2676         S->getLifetimeExtendedTemporaryDecl()));
2677     ShouldVisitChildren = false;
2678   }
2679 })
2680 // For coroutines expressions, traverse either the operand
2681 // as written or the implied calls, depending on what the
2682 // derived class requests.
2683 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2684   if (!getDerived().shouldVisitImplicitCode()) {
2685     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2686     ShouldVisitChildren = false;
2687   }
2688 })
2689 DEF_TRAVERSE_STMT(CoreturnStmt, {
2690   if (!getDerived().shouldVisitImplicitCode()) {
2691     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2692     ShouldVisitChildren = false;
2693   }
2694 })
2695 DEF_TRAVERSE_STMT(CoawaitExpr, {
2696   if (!getDerived().shouldVisitImplicitCode()) {
2697     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2698     ShouldVisitChildren = false;
2699   }
2700 })
2701 DEF_TRAVERSE_STMT(DependentCoawaitExpr, {
2702   if (!getDerived().shouldVisitImplicitCode()) {
2703     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2704     ShouldVisitChildren = false;
2705   }
2706 })
2707 DEF_TRAVERSE_STMT(CoyieldExpr, {
2708   if (!getDerived().shouldVisitImplicitCode()) {
2709     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2710     ShouldVisitChildren = false;
2711   }
2712 })
2713 
2714 DEF_TRAVERSE_STMT(ConceptSpecializationExpr, {
2715   TRY_TO(TraverseConceptReference(*S));
2716 })
2717 
2718 DEF_TRAVERSE_STMT(RequiresExpr, {
2719   TRY_TO(TraverseDecl(S->getBody()));
2720   for (ParmVarDecl *Parm : S->getLocalParameters())
2721     TRY_TO(TraverseDecl(Parm));
2722   for (concepts::Requirement *Req : S->getRequirements())
2723     if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2724       if (!TypeReq->isSubstitutionFailure())
2725         TRY_TO(TraverseTypeLoc(TypeReq->getType()->getTypeLoc()));
2726     } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2727       if (!ExprReq->isExprSubstitutionFailure())
2728         TRY_TO(TraverseStmt(ExprReq->getExpr()));
2729       auto &RetReq = ExprReq->getReturnTypeRequirement();
2730       if (RetReq.isTypeConstraint())
2731         TRY_TO(TraverseTemplateParameterListHelper(
2732                    RetReq.getTypeConstraintTemplateParameterList()));
2733     } else {
2734       auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2735       if (!NestedReq->isSubstitutionFailure())
2736         TRY_TO(TraverseStmt(NestedReq->getConstraintExpr()));
2737     }
2738 })
2739 
2740 // These literals (all of them) do not need any action.
2741 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2742 DEF_TRAVERSE_STMT(FixedPointLiteral, {})
2743 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2744 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2745 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2746 DEF_TRAVERSE_STMT(StringLiteral, {})
2747 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2748 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2749 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2750 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2751 
2752 // Traverse OpenCL: AsType, Convert.
2753 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2754 
2755 // OpenMP directives.
2756 template <typename Derived>
2757 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2758     OMPExecutableDirective *S) {
2759   for (auto *C : S->clauses()) {
2760     TRY_TO(TraverseOMPClause(C));
2761   }
2762   return true;
2763 }
2764 
2765 template <typename Derived>
2766 bool
2767 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2768   return TraverseOMPExecutableDirective(S);
2769 }
2770 
2771 DEF_TRAVERSE_STMT(OMPParallelDirective,
2772                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2773 
2774 DEF_TRAVERSE_STMT(OMPSimdDirective,
2775                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2776 
2777 DEF_TRAVERSE_STMT(OMPForDirective,
2778                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2779 
2780 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2781                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2782 
2783 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2784                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2785 
2786 DEF_TRAVERSE_STMT(OMPSectionDirective,
2787                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2788 
2789 DEF_TRAVERSE_STMT(OMPSingleDirective,
2790                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2791 
2792 DEF_TRAVERSE_STMT(OMPMasterDirective,
2793                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2794 
2795 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2796   TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2797   TRY_TO(TraverseOMPExecutableDirective(S));
2798 })
2799 
2800 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2801                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2802 
2803 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2804                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2805 
2806 DEF_TRAVERSE_STMT(OMPParallelMasterDirective,
2807                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2808 
2809 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2810                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2811 
2812 DEF_TRAVERSE_STMT(OMPTaskDirective,
2813                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2814 
2815 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2816                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2817 
2818 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2819                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2820 
2821 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2822                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2823 
2824 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2825                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2826 
2827 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2828                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2829 
2830 DEF_TRAVERSE_STMT(OMPCancelDirective,
2831                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2832 
2833 DEF_TRAVERSE_STMT(OMPFlushDirective,
2834                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2835 
2836 DEF_TRAVERSE_STMT(OMPDepobjDirective,
2837                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2838 
2839 DEF_TRAVERSE_STMT(OMPScanDirective,
2840                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2841 
2842 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2843                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2844 
2845 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2846                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2847 
2848 DEF_TRAVERSE_STMT(OMPTargetDirective,
2849                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2850 
2851 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
2852                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2853 
2854 DEF_TRAVERSE_STMT(OMPTargetEnterDataDirective,
2855                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2856 
2857 DEF_TRAVERSE_STMT(OMPTargetExitDataDirective,
2858                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2859 
2860 DEF_TRAVERSE_STMT(OMPTargetParallelDirective,
2861                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2862 
2863 DEF_TRAVERSE_STMT(OMPTargetParallelForDirective,
2864                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2865 
2866 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2867                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2868 
2869 DEF_TRAVERSE_STMT(OMPTargetUpdateDirective,
2870                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2871 
2872 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
2873                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2874 
2875 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
2876                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2877 
2878 DEF_TRAVERSE_STMT(OMPMasterTaskLoopDirective,
2879                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2880 
2881 DEF_TRAVERSE_STMT(OMPMasterTaskLoopSimdDirective,
2882                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2883 
2884 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopDirective,
2885                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2886 
2887 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopSimdDirective,
2888                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2889 
2890 DEF_TRAVERSE_STMT(OMPDistributeDirective,
2891                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2892 
2893 DEF_TRAVERSE_STMT(OMPDistributeParallelForDirective,
2894                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2895 
2896 DEF_TRAVERSE_STMT(OMPDistributeParallelForSimdDirective,
2897                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2898 
2899 DEF_TRAVERSE_STMT(OMPDistributeSimdDirective,
2900                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2901 
2902 DEF_TRAVERSE_STMT(OMPTargetParallelForSimdDirective,
2903                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2904 
2905 DEF_TRAVERSE_STMT(OMPTargetSimdDirective,
2906                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2907 
2908 DEF_TRAVERSE_STMT(OMPTeamsDistributeDirective,
2909                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2910 
2911 DEF_TRAVERSE_STMT(OMPTeamsDistributeSimdDirective,
2912                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2913 
2914 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForSimdDirective,
2915                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2916 
2917 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForDirective,
2918                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2919 
2920 DEF_TRAVERSE_STMT(OMPTargetTeamsDirective,
2921                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2922 
2923 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeDirective,
2924                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2925 
2926 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForDirective,
2927                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2928 
2929 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForSimdDirective,
2930                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2931 
2932 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeSimdDirective,
2933                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2934 
2935 // OpenMP clauses.
2936 template <typename Derived>
2937 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2938   if (!C)
2939     return true;
2940   switch (C->getClauseKind()) {
2941 #define OMP_CLAUSE_CLASS(Enum, Str, Class)                                     \
2942   case llvm::omp::Clause::Enum:                                                \
2943     TRY_TO(Visit##Class(static_cast<Class *>(C)));                             \
2944     break;
2945 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
2946   case llvm::omp::Clause::Enum:                                                \
2947     break;
2948 #include "llvm/Frontend/OpenMP/OMPKinds.def"
2949   default:
2950     break;
2951   }
2952   return true;
2953 }
2954 
2955 template <typename Derived>
2956 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPreInit(
2957     OMPClauseWithPreInit *Node) {
2958   TRY_TO(TraverseStmt(Node->getPreInitStmt()));
2959   return true;
2960 }
2961 
2962 template <typename Derived>
2963 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPostUpdate(
2964     OMPClauseWithPostUpdate *Node) {
2965   TRY_TO(VisitOMPClauseWithPreInit(Node));
2966   TRY_TO(TraverseStmt(Node->getPostUpdateExpr()));
2967   return true;
2968 }
2969 
2970 template <typename Derived>
2971 bool RecursiveASTVisitor<Derived>::VisitOMPAllocatorClause(
2972     OMPAllocatorClause *C) {
2973   TRY_TO(TraverseStmt(C->getAllocator()));
2974   return true;
2975 }
2976 
2977 template <typename Derived>
2978 bool RecursiveASTVisitor<Derived>::VisitOMPAllocateClause(OMPAllocateClause *C) {
2979   TRY_TO(TraverseStmt(C->getAllocator()));
2980   TRY_TO(VisitOMPClauseList(C));
2981   return true;
2982 }
2983 
2984 template <typename Derived>
2985 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2986   TRY_TO(VisitOMPClauseWithPreInit(C));
2987   TRY_TO(TraverseStmt(C->getCondition()));
2988   return true;
2989 }
2990 
2991 template <typename Derived>
2992 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2993   TRY_TO(VisitOMPClauseWithPreInit(C));
2994   TRY_TO(TraverseStmt(C->getCondition()));
2995   return true;
2996 }
2997 
2998 template <typename Derived>
2999 bool
3000 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
3001   TRY_TO(VisitOMPClauseWithPreInit(C));
3002   TRY_TO(TraverseStmt(C->getNumThreads()));
3003   return true;
3004 }
3005 
3006 template <typename Derived>
3007 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
3008   TRY_TO(TraverseStmt(C->getSafelen()));
3009   return true;
3010 }
3011 
3012 template <typename Derived>
3013 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
3014   TRY_TO(TraverseStmt(C->getSimdlen()));
3015   return true;
3016 }
3017 
3018 template <typename Derived>
3019 bool
3020 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
3021   TRY_TO(TraverseStmt(C->getNumForLoops()));
3022   return true;
3023 }
3024 
3025 template <typename Derived>
3026 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
3027   return true;
3028 }
3029 
3030 template <typename Derived>
3031 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
3032   return true;
3033 }
3034 
3035 template <typename Derived>
3036 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedAddressClause(
3037     OMPUnifiedAddressClause *) {
3038   return true;
3039 }
3040 
3041 template <typename Derived>
3042 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedSharedMemoryClause(
3043     OMPUnifiedSharedMemoryClause *) {
3044   return true;
3045 }
3046 
3047 template <typename Derived>
3048 bool RecursiveASTVisitor<Derived>::VisitOMPReverseOffloadClause(
3049     OMPReverseOffloadClause *) {
3050   return true;
3051 }
3052 
3053 template <typename Derived>
3054 bool RecursiveASTVisitor<Derived>::VisitOMPDynamicAllocatorsClause(
3055     OMPDynamicAllocatorsClause *) {
3056   return true;
3057 }
3058 
3059 template <typename Derived>
3060 bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause(
3061     OMPAtomicDefaultMemOrderClause *) {
3062   return true;
3063 }
3064 
3065 template <typename Derived>
3066 bool
3067 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
3068   TRY_TO(VisitOMPClauseWithPreInit(C));
3069   TRY_TO(TraverseStmt(C->getChunkSize()));
3070   return true;
3071 }
3072 
3073 template <typename Derived>
3074 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
3075   TRY_TO(TraverseStmt(C->getNumForLoops()));
3076   return true;
3077 }
3078 
3079 template <typename Derived>
3080 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
3081   return true;
3082 }
3083 
3084 template <typename Derived>
3085 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
3086   return true;
3087 }
3088 
3089 template <typename Derived>
3090 bool
3091 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
3092   return true;
3093 }
3094 
3095 template <typename Derived>
3096 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
3097   return true;
3098 }
3099 
3100 template <typename Derived>
3101 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
3102   return true;
3103 }
3104 
3105 template <typename Derived>
3106 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
3107   return true;
3108 }
3109 
3110 template <typename Derived>
3111 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
3112   return true;
3113 }
3114 
3115 template <typename Derived>
3116 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
3117   return true;
3118 }
3119 
3120 template <typename Derived>
3121 bool RecursiveASTVisitor<Derived>::VisitOMPAcqRelClause(OMPAcqRelClause *) {
3122   return true;
3123 }
3124 
3125 template <typename Derived>
3126 bool RecursiveASTVisitor<Derived>::VisitOMPAcquireClause(OMPAcquireClause *) {
3127   return true;
3128 }
3129 
3130 template <typename Derived>
3131 bool RecursiveASTVisitor<Derived>::VisitOMPReleaseClause(OMPReleaseClause *) {
3132   return true;
3133 }
3134 
3135 template <typename Derived>
3136 bool RecursiveASTVisitor<Derived>::VisitOMPRelaxedClause(OMPRelaxedClause *) {
3137   return true;
3138 }
3139 
3140 template <typename Derived>
3141 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
3142   return true;
3143 }
3144 
3145 template <typename Derived>
3146 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
3147   return true;
3148 }
3149 
3150 template <typename Derived>
3151 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
3152   return true;
3153 }
3154 
3155 template <typename Derived>
3156 bool RecursiveASTVisitor<Derived>::VisitOMPDestroyClause(OMPDestroyClause *) {
3157   return true;
3158 }
3159 
3160 template <typename Derived>
3161 template <typename T>
3162 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
3163   for (auto *E : Node->varlists()) {
3164     TRY_TO(TraverseStmt(E));
3165   }
3166   return true;
3167 }
3168 
3169 template <typename Derived>
3170 bool RecursiveASTVisitor<Derived>::VisitOMPInclusiveClause(
3171     OMPInclusiveClause *C) {
3172   TRY_TO(VisitOMPClauseList(C));
3173   return true;
3174 }
3175 
3176 template <typename Derived>
3177 bool RecursiveASTVisitor<Derived>::VisitOMPExclusiveClause(
3178     OMPExclusiveClause *C) {
3179   TRY_TO(VisitOMPClauseList(C));
3180   return true;
3181 }
3182 
3183 template <typename Derived>
3184 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
3185   TRY_TO(VisitOMPClauseList(C));
3186   for (auto *E : C->private_copies()) {
3187     TRY_TO(TraverseStmt(E));
3188   }
3189   return true;
3190 }
3191 
3192 template <typename Derived>
3193 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
3194     OMPFirstprivateClause *C) {
3195   TRY_TO(VisitOMPClauseList(C));
3196   TRY_TO(VisitOMPClauseWithPreInit(C));
3197   for (auto *E : C->private_copies()) {
3198     TRY_TO(TraverseStmt(E));
3199   }
3200   for (auto *E : C->inits()) {
3201     TRY_TO(TraverseStmt(E));
3202   }
3203   return true;
3204 }
3205 
3206 template <typename Derived>
3207 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
3208     OMPLastprivateClause *C) {
3209   TRY_TO(VisitOMPClauseList(C));
3210   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3211   for (auto *E : C->private_copies()) {
3212     TRY_TO(TraverseStmt(E));
3213   }
3214   for (auto *E : C->source_exprs()) {
3215     TRY_TO(TraverseStmt(E));
3216   }
3217   for (auto *E : C->destination_exprs()) {
3218     TRY_TO(TraverseStmt(E));
3219   }
3220   for (auto *E : C->assignment_ops()) {
3221     TRY_TO(TraverseStmt(E));
3222   }
3223   return true;
3224 }
3225 
3226 template <typename Derived>
3227 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
3228   TRY_TO(VisitOMPClauseList(C));
3229   return true;
3230 }
3231 
3232 template <typename Derived>
3233 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
3234   TRY_TO(TraverseStmt(C->getStep()));
3235   TRY_TO(TraverseStmt(C->getCalcStep()));
3236   TRY_TO(VisitOMPClauseList(C));
3237   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3238   for (auto *E : C->privates()) {
3239     TRY_TO(TraverseStmt(E));
3240   }
3241   for (auto *E : C->inits()) {
3242     TRY_TO(TraverseStmt(E));
3243   }
3244   for (auto *E : C->updates()) {
3245     TRY_TO(TraverseStmt(E));
3246   }
3247   for (auto *E : C->finals()) {
3248     TRY_TO(TraverseStmt(E));
3249   }
3250   return true;
3251 }
3252 
3253 template <typename Derived>
3254 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
3255   TRY_TO(TraverseStmt(C->getAlignment()));
3256   TRY_TO(VisitOMPClauseList(C));
3257   return true;
3258 }
3259 
3260 template <typename Derived>
3261 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
3262   TRY_TO(VisitOMPClauseList(C));
3263   for (auto *E : C->source_exprs()) {
3264     TRY_TO(TraverseStmt(E));
3265   }
3266   for (auto *E : C->destination_exprs()) {
3267     TRY_TO(TraverseStmt(E));
3268   }
3269   for (auto *E : C->assignment_ops()) {
3270     TRY_TO(TraverseStmt(E));
3271   }
3272   return true;
3273 }
3274 
3275 template <typename Derived>
3276 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
3277     OMPCopyprivateClause *C) {
3278   TRY_TO(VisitOMPClauseList(C));
3279   for (auto *E : C->source_exprs()) {
3280     TRY_TO(TraverseStmt(E));
3281   }
3282   for (auto *E : C->destination_exprs()) {
3283     TRY_TO(TraverseStmt(E));
3284   }
3285   for (auto *E : C->assignment_ops()) {
3286     TRY_TO(TraverseStmt(E));
3287   }
3288   return true;
3289 }
3290 
3291 template <typename Derived>
3292 bool
3293 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
3294   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3295   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3296   TRY_TO(VisitOMPClauseList(C));
3297   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3298   for (auto *E : C->privates()) {
3299     TRY_TO(TraverseStmt(E));
3300   }
3301   for (auto *E : C->lhs_exprs()) {
3302     TRY_TO(TraverseStmt(E));
3303   }
3304   for (auto *E : C->rhs_exprs()) {
3305     TRY_TO(TraverseStmt(E));
3306   }
3307   for (auto *E : C->reduction_ops()) {
3308     TRY_TO(TraverseStmt(E));
3309   }
3310   if (C->getModifier() == OMPC_REDUCTION_inscan) {
3311     for (auto *E : C->copy_ops()) {
3312       TRY_TO(TraverseStmt(E));
3313     }
3314     for (auto *E : C->copy_array_temps()) {
3315       TRY_TO(TraverseStmt(E));
3316     }
3317     for (auto *E : C->copy_array_elems()) {
3318       TRY_TO(TraverseStmt(E));
3319     }
3320   }
3321   return true;
3322 }
3323 
3324 template <typename Derived>
3325 bool RecursiveASTVisitor<Derived>::VisitOMPTaskReductionClause(
3326     OMPTaskReductionClause *C) {
3327   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3328   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3329   TRY_TO(VisitOMPClauseList(C));
3330   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3331   for (auto *E : C->privates()) {
3332     TRY_TO(TraverseStmt(E));
3333   }
3334   for (auto *E : C->lhs_exprs()) {
3335     TRY_TO(TraverseStmt(E));
3336   }
3337   for (auto *E : C->rhs_exprs()) {
3338     TRY_TO(TraverseStmt(E));
3339   }
3340   for (auto *E : C->reduction_ops()) {
3341     TRY_TO(TraverseStmt(E));
3342   }
3343   return true;
3344 }
3345 
3346 template <typename Derived>
3347 bool RecursiveASTVisitor<Derived>::VisitOMPInReductionClause(
3348     OMPInReductionClause *C) {
3349   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3350   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3351   TRY_TO(VisitOMPClauseList(C));
3352   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3353   for (auto *E : C->privates()) {
3354     TRY_TO(TraverseStmt(E));
3355   }
3356   for (auto *E : C->lhs_exprs()) {
3357     TRY_TO(TraverseStmt(E));
3358   }
3359   for (auto *E : C->rhs_exprs()) {
3360     TRY_TO(TraverseStmt(E));
3361   }
3362   for (auto *E : C->reduction_ops()) {
3363     TRY_TO(TraverseStmt(E));
3364   }
3365   for (auto *E : C->taskgroup_descriptors())
3366     TRY_TO(TraverseStmt(E));
3367   return true;
3368 }
3369 
3370 template <typename Derived>
3371 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
3372   TRY_TO(VisitOMPClauseList(C));
3373   return true;
3374 }
3375 
3376 template <typename Derived>
3377 bool RecursiveASTVisitor<Derived>::VisitOMPDepobjClause(OMPDepobjClause *C) {
3378   TRY_TO(TraverseStmt(C->getDepobj()));
3379   return true;
3380 }
3381 
3382 template <typename Derived>
3383 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
3384   TRY_TO(VisitOMPClauseList(C));
3385   return true;
3386 }
3387 
3388 template <typename Derived>
3389 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
3390   TRY_TO(VisitOMPClauseWithPreInit(C));
3391   TRY_TO(TraverseStmt(C->getDevice()));
3392   return true;
3393 }
3394 
3395 template <typename Derived>
3396 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
3397   TRY_TO(VisitOMPClauseList(C));
3398   return true;
3399 }
3400 
3401 template <typename Derived>
3402 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
3403     OMPNumTeamsClause *C) {
3404   TRY_TO(VisitOMPClauseWithPreInit(C));
3405   TRY_TO(TraverseStmt(C->getNumTeams()));
3406   return true;
3407 }
3408 
3409 template <typename Derived>
3410 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
3411     OMPThreadLimitClause *C) {
3412   TRY_TO(VisitOMPClauseWithPreInit(C));
3413   TRY_TO(TraverseStmt(C->getThreadLimit()));
3414   return true;
3415 }
3416 
3417 template <typename Derived>
3418 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
3419     OMPPriorityClause *C) {
3420   TRY_TO(VisitOMPClauseWithPreInit(C));
3421   TRY_TO(TraverseStmt(C->getPriority()));
3422   return true;
3423 }
3424 
3425 template <typename Derived>
3426 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
3427     OMPGrainsizeClause *C) {
3428   TRY_TO(VisitOMPClauseWithPreInit(C));
3429   TRY_TO(TraverseStmt(C->getGrainsize()));
3430   return true;
3431 }
3432 
3433 template <typename Derived>
3434 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
3435     OMPNumTasksClause *C) {
3436   TRY_TO(VisitOMPClauseWithPreInit(C));
3437   TRY_TO(TraverseStmt(C->getNumTasks()));
3438   return true;
3439 }
3440 
3441 template <typename Derived>
3442 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
3443   TRY_TO(TraverseStmt(C->getHint()));
3444   return true;
3445 }
3446 
3447 template <typename Derived>
3448 bool RecursiveASTVisitor<Derived>::VisitOMPDistScheduleClause(
3449     OMPDistScheduleClause *C) {
3450   TRY_TO(VisitOMPClauseWithPreInit(C));
3451   TRY_TO(TraverseStmt(C->getChunkSize()));
3452   return true;
3453 }
3454 
3455 template <typename Derived>
3456 bool
3457 RecursiveASTVisitor<Derived>::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
3458   return true;
3459 }
3460 
3461 template <typename Derived>
3462 bool RecursiveASTVisitor<Derived>::VisitOMPToClause(OMPToClause *C) {
3463   TRY_TO(VisitOMPClauseList(C));
3464   return true;
3465 }
3466 
3467 template <typename Derived>
3468 bool RecursiveASTVisitor<Derived>::VisitOMPFromClause(OMPFromClause *C) {
3469   TRY_TO(VisitOMPClauseList(C));
3470   return true;
3471 }
3472 
3473 template <typename Derived>
3474 bool RecursiveASTVisitor<Derived>::VisitOMPUseDevicePtrClause(
3475     OMPUseDevicePtrClause *C) {
3476   TRY_TO(VisitOMPClauseList(C));
3477   return true;
3478 }
3479 
3480 template <typename Derived>
3481 bool RecursiveASTVisitor<Derived>::VisitOMPUseDeviceAddrClause(
3482     OMPUseDeviceAddrClause *C) {
3483   TRY_TO(VisitOMPClauseList(C));
3484   return true;
3485 }
3486 
3487 template <typename Derived>
3488 bool RecursiveASTVisitor<Derived>::VisitOMPIsDevicePtrClause(
3489     OMPIsDevicePtrClause *C) {
3490   TRY_TO(VisitOMPClauseList(C));
3491   return true;
3492 }
3493 
3494 template <typename Derived>
3495 bool RecursiveASTVisitor<Derived>::VisitOMPNontemporalClause(
3496     OMPNontemporalClause *C) {
3497   TRY_TO(VisitOMPClauseList(C));
3498   for (auto *E : C->private_refs()) {
3499     TRY_TO(TraverseStmt(E));
3500   }
3501   return true;
3502 }
3503 
3504 template <typename Derived>
3505 bool RecursiveASTVisitor<Derived>::VisitOMPOrderClause(OMPOrderClause *) {
3506   return true;
3507 }
3508 
3509 template <typename Derived>
3510 bool RecursiveASTVisitor<Derived>::VisitOMPDetachClause(OMPDetachClause *C) {
3511   TRY_TO(TraverseStmt(C->getEventHandler()));
3512   return true;
3513 }
3514 
3515 template <typename Derived>
3516 bool RecursiveASTVisitor<Derived>::VisitOMPUsesAllocatorsClause(
3517     OMPUsesAllocatorsClause *C) {
3518   for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
3519     const OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
3520     TRY_TO(TraverseStmt(Data.Allocator));
3521     TRY_TO(TraverseStmt(Data.AllocatorTraits));
3522   }
3523   return true;
3524 }
3525 
3526 template <typename Derived>
3527 bool RecursiveASTVisitor<Derived>::VisitOMPAffinityClause(
3528     OMPAffinityClause *C) {
3529   TRY_TO(TraverseStmt(C->getModifier()));
3530   for (Expr *E : C->varlists())
3531     TRY_TO(TraverseStmt(E));
3532   return true;
3533 }
3534 
3535 // FIXME: look at the following tricky-seeming exprs to see if we
3536 // need to recurse on anything.  These are ones that have methods
3537 // returning decls or qualtypes or nestednamespecifier -- though I'm
3538 // not sure if they own them -- or just seemed very complicated, or
3539 // had lots of sub-types to explore.
3540 //
3541 // VisitOverloadExpr and its children: recurse on template args? etc?
3542 
3543 // FIXME: go through all the stmts and exprs again, and see which of them
3544 // create new types, and recurse on the types (TypeLocs?) of those.
3545 // Candidates:
3546 //
3547 //    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
3548 //    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
3549 //    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
3550 //    Every class that has getQualifier.
3551 
3552 #undef DEF_TRAVERSE_STMT
3553 #undef TRAVERSE_STMT
3554 #undef TRAVERSE_STMT_BASE
3555 
3556 #undef TRY_TO
3557 
3558 } // end namespace clang
3559 
3560 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
3561