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