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