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     return true;
854 
855   case TemplateArgument::Type:
856     return getDerived().TraverseType(Arg.getAsType());
857 
858   case TemplateArgument::Template:
859   case TemplateArgument::TemplateExpansion:
860     return getDerived().TraverseTemplateName(
861         Arg.getAsTemplateOrTemplatePattern());
862 
863   case TemplateArgument::Expression:
864     return getDerived().TraverseStmt(Arg.getAsExpr());
865 
866   case TemplateArgument::Pack:
867     return getDerived().TraverseTemplateArguments(Arg.pack_elements());
868   }
869 
870   return true;
871 }
872 
873 // FIXME: no template name location?
874 // FIXME: no source locations for a template argument pack?
875 template <typename Derived>
876 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
877     const TemplateArgumentLoc &ArgLoc) {
878   const TemplateArgument &Arg = ArgLoc.getArgument();
879 
880   switch (Arg.getKind()) {
881   case TemplateArgument::Null:
882   case TemplateArgument::Declaration:
883   case TemplateArgument::Integral:
884   case TemplateArgument::NullPtr:
885     return true;
886 
887   case TemplateArgument::Type: {
888     // FIXME: how can TSI ever be NULL?
889     if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
890       return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
891     else
892       return getDerived().TraverseType(Arg.getAsType());
893   }
894 
895   case TemplateArgument::Template:
896   case TemplateArgument::TemplateExpansion:
897     if (ArgLoc.getTemplateQualifierLoc())
898       TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
899           ArgLoc.getTemplateQualifierLoc()));
900     return getDerived().TraverseTemplateName(
901         Arg.getAsTemplateOrTemplatePattern());
902 
903   case TemplateArgument::Expression:
904     return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
905 
906   case TemplateArgument::Pack:
907     return getDerived().TraverseTemplateArguments(Arg.pack_elements());
908   }
909 
910   return true;
911 }
912 
913 template <typename Derived>
914 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
915     ArrayRef<TemplateArgument> Args) {
916   for (const TemplateArgument &Arg : Args)
917     TRY_TO(TraverseTemplateArgument(Arg));
918 
919   return true;
920 }
921 
922 template <typename Derived>
923 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
924     CXXCtorInitializer *Init) {
925   if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
926     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
927 
928   if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
929     TRY_TO(TraverseStmt(Init->getInit()));
930 
931   return true;
932 }
933 
934 template <typename Derived>
935 bool
936 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
937                                                     const LambdaCapture *C,
938                                                     Expr *Init) {
939   if (LE->isInitCapture(C))
940     TRY_TO(TraverseDecl(C->getCapturedVar()));
941   else
942     TRY_TO(TraverseStmt(Init));
943   return true;
944 }
945 
946 // ----------------- Type traversal -----------------
947 
948 // This macro makes available a variable T, the passed-in type.
949 #define DEF_TRAVERSE_TYPE(TYPE, CODE)                                          \
950   template <typename Derived>                                                  \
951   bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) {                 \
952     if (!getDerived().shouldTraversePostOrder())                               \
953       TRY_TO(WalkUpFrom##TYPE(T));                                             \
954     { CODE; }                                                                  \
955     if (getDerived().shouldTraversePostOrder())                                \
956       TRY_TO(WalkUpFrom##TYPE(T));                                             \
957     return true;                                                               \
958   }
959 
960 DEF_TRAVERSE_TYPE(BuiltinType, {})
961 
962 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
963 
964 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
965 
966 DEF_TRAVERSE_TYPE(BlockPointerType,
967                   { TRY_TO(TraverseType(T->getPointeeType())); })
968 
969 DEF_TRAVERSE_TYPE(LValueReferenceType,
970                   { TRY_TO(TraverseType(T->getPointeeType())); })
971 
972 DEF_TRAVERSE_TYPE(RValueReferenceType,
973                   { TRY_TO(TraverseType(T->getPointeeType())); })
974 
975 DEF_TRAVERSE_TYPE(MemberPointerType, {
976   TRY_TO(TraverseType(QualType(T->getClass(), 0)));
977   TRY_TO(TraverseType(T->getPointeeType()));
978 })
979 
980 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
981 
982 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
983 
984 DEF_TRAVERSE_TYPE(ConstantArrayType, {
985   TRY_TO(TraverseType(T->getElementType()));
986   if (T->getSizeExpr())
987     TRY_TO(TraverseStmt(const_cast<Expr*>(T->getSizeExpr())));
988 })
989 
990 DEF_TRAVERSE_TYPE(IncompleteArrayType,
991                   { TRY_TO(TraverseType(T->getElementType())); })
992 
993 DEF_TRAVERSE_TYPE(VariableArrayType, {
994   TRY_TO(TraverseType(T->getElementType()));
995   TRY_TO(TraverseStmt(T->getSizeExpr()));
996 })
997 
998 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
999   TRY_TO(TraverseType(T->getElementType()));
1000   if (T->getSizeExpr())
1001     TRY_TO(TraverseStmt(T->getSizeExpr()));
1002 })
1003 
1004 DEF_TRAVERSE_TYPE(DependentAddressSpaceType, {
1005   TRY_TO(TraverseStmt(T->getAddrSpaceExpr()));
1006   TRY_TO(TraverseType(T->getPointeeType()));
1007 })
1008 
1009 DEF_TRAVERSE_TYPE(DependentVectorType, {
1010   if (T->getSizeExpr())
1011     TRY_TO(TraverseStmt(T->getSizeExpr()));
1012   TRY_TO(TraverseType(T->getElementType()));
1013 })
1014 
1015 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
1016   if (T->getSizeExpr())
1017     TRY_TO(TraverseStmt(T->getSizeExpr()));
1018   TRY_TO(TraverseType(T->getElementType()));
1019 })
1020 
1021 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
1022 
1023 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
1024 
1025 DEF_TRAVERSE_TYPE(ConstantMatrixType,
1026                   { TRY_TO(TraverseType(T->getElementType())); })
1027 
1028 DEF_TRAVERSE_TYPE(DependentSizedMatrixType, {
1029   if (T->getRowExpr())
1030     TRY_TO(TraverseStmt(T->getRowExpr()));
1031   if (T->getColumnExpr())
1032     TRY_TO(TraverseStmt(T->getColumnExpr()));
1033   TRY_TO(TraverseType(T->getElementType()));
1034 })
1035 
1036 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
1037                   { TRY_TO(TraverseType(T->getReturnType())); })
1038 
1039 DEF_TRAVERSE_TYPE(FunctionProtoType, {
1040   TRY_TO(TraverseType(T->getReturnType()));
1041 
1042   for (const auto &A : T->param_types()) {
1043     TRY_TO(TraverseType(A));
1044   }
1045 
1046   for (const auto &E : T->exceptions()) {
1047     TRY_TO(TraverseType(E));
1048   }
1049 
1050   if (Expr *NE = T->getNoexceptExpr())
1051     TRY_TO(TraverseStmt(NE));
1052 })
1053 
1054 DEF_TRAVERSE_TYPE(UsingType, {})
1055 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
1056 DEF_TRAVERSE_TYPE(TypedefType, {})
1057 
1058 DEF_TRAVERSE_TYPE(TypeOfExprType,
1059                   { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1060 
1061 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnmodifiedType())); })
1062 
1063 DEF_TRAVERSE_TYPE(DecltypeType,
1064                   { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1065 
1066 DEF_TRAVERSE_TYPE(UnaryTransformType, {
1067   TRY_TO(TraverseType(T->getBaseType()));
1068   TRY_TO(TraverseType(T->getUnderlyingType()));
1069 })
1070 
1071 DEF_TRAVERSE_TYPE(AutoType, {
1072   TRY_TO(TraverseType(T->getDeducedType()));
1073   if (T->isConstrained()) {
1074     TRY_TO(TraverseTemplateArguments(T->getTypeConstraintArguments()));
1075   }
1076 })
1077 DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, {
1078   TRY_TO(TraverseTemplateName(T->getTemplateName()));
1079   TRY_TO(TraverseType(T->getDeducedType()));
1080 })
1081 
1082 DEF_TRAVERSE_TYPE(RecordType, {})
1083 DEF_TRAVERSE_TYPE(EnumType, {})
1084 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
1085 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {
1086   TRY_TO(TraverseType(T->getReplacementType()));
1087 })
1088 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {
1089   TRY_TO(TraverseTemplateArgument(T->getArgumentPack()));
1090 })
1091 
1092 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
1093   TRY_TO(TraverseTemplateName(T->getTemplateName()));
1094   TRY_TO(TraverseTemplateArguments(T->template_arguments()));
1095 })
1096 
1097 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
1098 
1099 DEF_TRAVERSE_TYPE(AttributedType,
1100                   { TRY_TO(TraverseType(T->getModifiedType())); })
1101 
1102 DEF_TRAVERSE_TYPE(BTFTagAttributedType,
1103                   { TRY_TO(TraverseType(T->getWrappedType())); })
1104 
1105 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
1106 
1107 DEF_TRAVERSE_TYPE(MacroQualifiedType,
1108                   { TRY_TO(TraverseType(T->getUnderlyingType())); })
1109 
1110 DEF_TRAVERSE_TYPE(ElaboratedType, {
1111   if (T->getQualifier()) {
1112     TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1113   }
1114   TRY_TO(TraverseType(T->getNamedType()));
1115 })
1116 
1117 DEF_TRAVERSE_TYPE(DependentNameType,
1118                   { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
1119 
1120 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1121   TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1122   TRY_TO(TraverseTemplateArguments(T->template_arguments()));
1123 })
1124 
1125 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1126 
1127 DEF_TRAVERSE_TYPE(ObjCTypeParamType, {})
1128 
1129 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1130 
1131 DEF_TRAVERSE_TYPE(ObjCObjectType, {
1132   // We have to watch out here because an ObjCInterfaceType's base
1133   // type is itself.
1134   if (T->getBaseType().getTypePtr() != T)
1135     TRY_TO(TraverseType(T->getBaseType()));
1136   for (auto typeArg : T->getTypeArgsAsWritten()) {
1137     TRY_TO(TraverseType(typeArg));
1138   }
1139 })
1140 
1141 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1142                   { TRY_TO(TraverseType(T->getPointeeType())); })
1143 
1144 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1145 
1146 DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
1147 
1148 DEF_TRAVERSE_TYPE(BitIntType, {})
1149 DEF_TRAVERSE_TYPE(DependentBitIntType,
1150                   { TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
1151 
1152 #undef DEF_TRAVERSE_TYPE
1153 
1154 // ----------------- TypeLoc traversal -----------------
1155 
1156 // This macro makes available a variable TL, the passed-in TypeLoc.
1157 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1158 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1159 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1160 // continue to work.
1161 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE)                                       \
1162   template <typename Derived>                                                  \
1163   bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) {       \
1164     if (!getDerived().shouldTraversePostOrder()) {                             \
1165       TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                       \
1166       if (getDerived().shouldWalkTypesOfTypeLocs())                            \
1167         TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr())));         \
1168     }                                                                          \
1169     { CODE; }                                                                  \
1170     if (getDerived().shouldTraversePostOrder()) {                              \
1171       TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                       \
1172       if (getDerived().shouldWalkTypesOfTypeLocs())                            \
1173         TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr())));         \
1174     }                                                                          \
1175     return true;                                                               \
1176   }
1177 
1178 template <typename Derived>
1179 bool
1180 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1181   // Move this over to the 'main' typeloc tree.  Note that this is a
1182   // move -- we pretend that we were really looking at the unqualified
1183   // typeloc all along -- rather than a recursion, so we don't follow
1184   // the normal CRTP plan of going through
1185   // getDerived().TraverseTypeLoc.  If we did, we'd be traversing
1186   // twice for the same type (once as a QualifiedTypeLoc version of
1187   // the type, once as an UnqualifiedTypeLoc version of the type),
1188   // which in effect means we'd call VisitTypeLoc twice with the
1189   // 'same' type.  This solves that problem, at the cost of never
1190   // seeing the qualified version of the type (unless the client
1191   // subclasses TraverseQualifiedTypeLoc themselves).  It's not a
1192   // perfect solution.  A perfect solution probably requires making
1193   // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1194   // wrapper around Type* -- rather than being its own class in the
1195   // type hierarchy.
1196   return TraverseTypeLoc(TL.getUnqualifiedLoc());
1197 }
1198 
1199 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1200 
1201 // FIXME: ComplexTypeLoc is unfinished
1202 DEF_TRAVERSE_TYPELOC(ComplexType, {
1203   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1204 })
1205 
1206 DEF_TRAVERSE_TYPELOC(PointerType,
1207                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1208 
1209 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1210                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1211 
1212 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1213                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1214 
1215 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1216                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1217 
1218 // We traverse this in the type case as well, but how is it not reached through
1219 // the pointee type?
1220 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1221   if (auto *TSI = TL.getClassTInfo())
1222     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1223   else
1224     TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1225   TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1226 })
1227 
1228 DEF_TRAVERSE_TYPELOC(AdjustedType,
1229                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1230 
1231 DEF_TRAVERSE_TYPELOC(DecayedType,
1232                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1233 
1234 template <typename Derived>
1235 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1236   // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1237   TRY_TO(TraverseStmt(TL.getSizeExpr()));
1238   return true;
1239 }
1240 
1241 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1242   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1243   TRY_TO(TraverseArrayTypeLocHelper(TL));
1244 })
1245 
1246 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1247   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1248   TRY_TO(TraverseArrayTypeLocHelper(TL));
1249 })
1250 
1251 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1252   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1253   TRY_TO(TraverseArrayTypeLocHelper(TL));
1254 })
1255 
1256 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1257   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1258   TRY_TO(TraverseArrayTypeLocHelper(TL));
1259 })
1260 
1261 DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
1262   TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr()));
1263   TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType()));
1264 })
1265 
1266 // FIXME: order? why not size expr first?
1267 // FIXME: base VectorTypeLoc is unfinished
1268 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1269   if (TL.getTypePtr()->getSizeExpr())
1270     TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1271   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1272 })
1273 
1274 // FIXME: VectorTypeLoc is unfinished
1275 DEF_TRAVERSE_TYPELOC(VectorType, {
1276   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1277 })
1278 
1279 DEF_TRAVERSE_TYPELOC(DependentVectorType, {
1280   if (TL.getTypePtr()->getSizeExpr())
1281     TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1282   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1283 })
1284 
1285 // FIXME: size and attributes
1286 // FIXME: base VectorTypeLoc is unfinished
1287 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1288   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1289 })
1290 
1291 DEF_TRAVERSE_TYPELOC(ConstantMatrixType, {
1292   TRY_TO(TraverseStmt(TL.getAttrRowOperand()));
1293   TRY_TO(TraverseStmt(TL.getAttrColumnOperand()));
1294   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1295 })
1296 
1297 DEF_TRAVERSE_TYPELOC(DependentSizedMatrixType, {
1298   TRY_TO(TraverseStmt(TL.getAttrRowOperand()));
1299   TRY_TO(TraverseStmt(TL.getAttrColumnOperand()));
1300   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1301 })
1302 
1303 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1304                      { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1305 
1306 // FIXME: location of exception specifications (attributes?)
1307 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1308   TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1309 
1310   const FunctionProtoType *T = TL.getTypePtr();
1311 
1312   for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1313     if (TL.getParam(I)) {
1314       TRY_TO(TraverseDecl(TL.getParam(I)));
1315     } else if (I < T->getNumParams()) {
1316       TRY_TO(TraverseType(T->getParamType(I)));
1317     }
1318   }
1319 
1320   for (const auto &E : T->exceptions()) {
1321     TRY_TO(TraverseType(E));
1322   }
1323 
1324   if (Expr *NE = T->getNoexceptExpr())
1325     TRY_TO(TraverseStmt(NE));
1326 })
1327 
1328 DEF_TRAVERSE_TYPELOC(UsingType, {})
1329 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1330 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1331 
1332 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1333                      { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1334 
1335 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1336   TRY_TO(TraverseTypeLoc(TL.getUnmodifiedTInfo()->getTypeLoc()));
1337 })
1338 
1339 // FIXME: location of underlying expr
1340 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1341   TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1342 })
1343 
1344 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1345   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1346 })
1347 
1348 DEF_TRAVERSE_TYPELOC(AutoType, {
1349   TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1350   if (TL.isConstrained()) {
1351     TRY_TO(TraverseConceptReference(TL.getConceptReference()));
1352   }
1353 })
1354 
1355 DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, {
1356   TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1357   TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1358 })
1359 
1360 DEF_TRAVERSE_TYPELOC(RecordType, {})
1361 DEF_TRAVERSE_TYPELOC(EnumType, {})
1362 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1363 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {
1364   TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType()));
1365 })
1366 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {
1367   TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack()));
1368 })
1369 
1370 // FIXME: use the loc for the template name?
1371 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1372   TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1373   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1374     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1375   }
1376 })
1377 
1378 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1379 
1380 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1381 
1382 DEF_TRAVERSE_TYPELOC(MacroQualifiedType,
1383                      { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1384 
1385 DEF_TRAVERSE_TYPELOC(AttributedType,
1386                      { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1387 
1388 DEF_TRAVERSE_TYPELOC(BTFTagAttributedType,
1389                      { TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); })
1390 
1391 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1392   if (TL.getQualifierLoc()) {
1393     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1394   }
1395   TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1396 })
1397 
1398 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1399   TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1400 })
1401 
1402 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1403   if (TL.getQualifierLoc()) {
1404     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1405   }
1406 
1407   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1408     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1409   }
1410 })
1411 
1412 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1413                      { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1414 
1415 DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {
1416   for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1417     ObjCProtocolLoc ProtocolLoc(TL.getProtocol(I), TL.getProtocolLoc(I));
1418     TRY_TO(TraverseObjCProtocolLoc(ProtocolLoc));
1419   }
1420 })
1421 
1422 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1423 
1424 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1425   // We have to watch out here because an ObjCInterfaceType's base
1426   // type is itself.
1427   if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1428     TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1429   for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1430     TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1431   for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1432     ObjCProtocolLoc ProtocolLoc(TL.getProtocol(I), TL.getProtocolLoc(I));
1433     TRY_TO(TraverseObjCProtocolLoc(ProtocolLoc));
1434   }
1435 })
1436 
1437 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1438                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1439 
1440 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1441 
1442 DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1443 
1444 DEF_TRAVERSE_TYPELOC(BitIntType, {})
1445 DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
1446   TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
1447 })
1448 
1449 #undef DEF_TRAVERSE_TYPELOC
1450 
1451 // ----------------- Decl traversal -----------------
1452 //
1453 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1454 // the children that come from the DeclContext associated with it.
1455 // Therefore each Traverse* only needs to worry about children other
1456 // than those.
1457 
1458 template <typename Derived>
1459 bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext(
1460     const Decl *Child) {
1461   // BlockDecls are traversed through BlockExprs,
1462   // CapturedDecls are traversed through CapturedStmts.
1463   if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child))
1464     return true;
1465   // Lambda classes are traversed through LambdaExprs.
1466   if (const CXXRecordDecl* Cls = dyn_cast<CXXRecordDecl>(Child))
1467     return Cls->isLambda();
1468   return false;
1469 }
1470 
1471 template <typename Derived>
1472 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1473   if (!DC)
1474     return true;
1475 
1476   for (auto *Child : DC->decls()) {
1477     if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1478       TRY_TO(TraverseDecl(Child));
1479   }
1480 
1481   return true;
1482 }
1483 
1484 // This macro makes available a variable D, the passed-in decl.
1485 #define DEF_TRAVERSE_DECL(DECL, CODE)                                          \
1486   template <typename Derived>                                                  \
1487   bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) {                 \
1488     bool ShouldVisitChildren = true;                                           \
1489     bool ReturnValue = true;                                                   \
1490     if (!getDerived().shouldTraversePostOrder())                               \
1491       TRY_TO(WalkUpFrom##DECL(D));                                             \
1492     { CODE; }                                                                  \
1493     if (ReturnValue && ShouldVisitChildren)                                    \
1494       TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));             \
1495     if (ReturnValue) {                                                         \
1496       /* Visit any attributes attached to this declaration. */                 \
1497       for (auto *I : D->attrs())                                               \
1498         TRY_TO(getDerived().TraverseAttr(I));                                  \
1499     }                                                                          \
1500     if (ReturnValue && getDerived().shouldTraversePostOrder())                 \
1501       TRY_TO(WalkUpFrom##DECL(D));                                             \
1502     return ReturnValue;                                                        \
1503   }
1504 
1505 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1506 
1507 DEF_TRAVERSE_DECL(BlockDecl, {
1508   if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1509     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1510   TRY_TO(TraverseStmt(D->getBody()));
1511   for (const auto &I : D->captures()) {
1512     if (I.hasCopyExpr()) {
1513       TRY_TO(TraverseStmt(I.getCopyExpr()));
1514     }
1515   }
1516   ShouldVisitChildren = false;
1517 })
1518 
1519 DEF_TRAVERSE_DECL(CapturedDecl, {
1520   TRY_TO(TraverseStmt(D->getBody()));
1521   ShouldVisitChildren = false;
1522 })
1523 
1524 DEF_TRAVERSE_DECL(EmptyDecl, {})
1525 
1526 DEF_TRAVERSE_DECL(HLSLBufferDecl, {})
1527 
1528 DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
1529   TRY_TO(TraverseStmt(D->getTemporaryExpr()));
1530 })
1531 
1532 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1533                   { TRY_TO(TraverseStmt(D->getAsmString())); })
1534 
1535 DEF_TRAVERSE_DECL(TopLevelStmtDecl, { TRY_TO(TraverseStmt(D->getStmt())); })
1536 
1537 DEF_TRAVERSE_DECL(ImportDecl, {})
1538 
1539 DEF_TRAVERSE_DECL(FriendDecl, {
1540   // Friend is either decl or a type.
1541   if (D->getFriendType()) {
1542     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1543     // Traverse any CXXRecordDecl owned by this type, since
1544     // it will not be in the parent context:
1545     if (auto *ET = D->getFriendType()->getType()->getAs<ElaboratedType>())
1546       TRY_TO(TraverseDecl(ET->getOwnedTagDecl()));
1547   } else {
1548     TRY_TO(TraverseDecl(D->getFriendDecl()));
1549   }
1550 })
1551 
1552 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1553   if (D->getFriendType())
1554     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1555   else
1556     TRY_TO(TraverseDecl(D->getFriendDecl()));
1557   for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1558     TemplateParameterList *TPL = D->getTemplateParameterList(I);
1559     for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1560          ITPL != ETPL; ++ITPL) {
1561       TRY_TO(TraverseDecl(*ITPL));
1562     }
1563   }
1564 })
1565 
1566 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1567 
1568 DEF_TRAVERSE_DECL(ExportDecl, {})
1569 
1570 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1571                                         })
1572 
1573 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1574   TRY_TO(TraverseStmt(D->getAssertExpr()));
1575   TRY_TO(TraverseStmt(D->getMessage()));
1576 })
1577 
1578 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1579   // Code in an unnamed namespace shows up automatically in
1580   // decls_begin()/decls_end().  Thus we don't need to recurse on
1581   // D->getAnonymousNamespace().
1582 
1583   // If the traversal scope is set, then consider them to be the children of
1584   // the TUDecl, rather than traversing (and loading?) all top-level decls.
1585   auto Scope = D->getASTContext().getTraversalScope();
1586   bool HasLimitedScope =
1587       Scope.size() != 1 || !isa<TranslationUnitDecl>(Scope.front());
1588   if (HasLimitedScope) {
1589     ShouldVisitChildren = false; // we'll do that here instead
1590     for (auto *Child : Scope) {
1591       if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1592         TRY_TO(TraverseDecl(Child));
1593     }
1594   }
1595 })
1596 
1597 DEF_TRAVERSE_DECL(PragmaCommentDecl, {})
1598 
1599 DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {})
1600 
1601 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1602 
1603 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1604   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1605 
1606   // We shouldn't traverse an aliased namespace, since it will be
1607   // defined (and, therefore, traversed) somewhere else.
1608   ShouldVisitChildren = false;
1609 })
1610 
1611 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1612                              })
1613 
1614 DEF_TRAVERSE_DECL(
1615     NamespaceDecl,
1616     {// Code in an unnamed namespace shows up automatically in
1617      // decls_begin()/decls_end().  Thus we don't need to recurse on
1618      // D->getAnonymousNamespace().
1619     })
1620 
1621 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1622                                            })
1623 
1624 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1625   if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1626     for (auto typeParam : *typeParamList) {
1627       TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1628     }
1629   }
1630   for (auto It : llvm::zip(D->protocols(), D->protocol_locs())) {
1631     ObjCProtocolLoc ProtocolLoc(std::get<0>(It), std::get<1>(It));
1632     TRY_TO(TraverseObjCProtocolLoc(ProtocolLoc));
1633   }
1634 })
1635 
1636 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1637                                         })
1638 
1639 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1640                                           })
1641 
1642 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1643   if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1644     for (auto typeParam : *typeParamList) {
1645       TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1646     }
1647   }
1648 
1649   if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1650     TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1651   }
1652   if (D->isThisDeclarationADefinition()) {
1653     for (auto It : llvm::zip(D->protocols(), D->protocol_locs())) {
1654       ObjCProtocolLoc ProtocolLoc(std::get<0>(It), std::get<1>(It));
1655       TRY_TO(TraverseObjCProtocolLoc(ProtocolLoc));
1656     }
1657   }
1658 })
1659 
1660 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1661   if (D->isThisDeclarationADefinition()) {
1662     for (auto It : llvm::zip(D->protocols(), D->protocol_locs())) {
1663       ObjCProtocolLoc ProtocolLoc(std::get<0>(It), std::get<1>(It));
1664       TRY_TO(TraverseObjCProtocolLoc(ProtocolLoc));
1665     }
1666   }
1667 })
1668 
1669 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1670   if (D->getReturnTypeSourceInfo()) {
1671     TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1672   }
1673   for (ParmVarDecl *Parameter : D->parameters()) {
1674     TRY_TO(TraverseDecl(Parameter));
1675   }
1676   if (D->isThisDeclarationADefinition()) {
1677     TRY_TO(TraverseStmt(D->getBody()));
1678   }
1679   ShouldVisitChildren = false;
1680 })
1681 
1682 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1683   if (D->hasExplicitBound()) {
1684     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1685     // We shouldn't traverse D->getTypeForDecl(); it's a result of
1686     // declaring the type alias, not something that was written in the
1687     // source.
1688   }
1689 })
1690 
1691 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1692   if (D->getTypeSourceInfo())
1693     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1694   else
1695     TRY_TO(TraverseType(D->getType()));
1696   ShouldVisitChildren = false;
1697 })
1698 
1699 DEF_TRAVERSE_DECL(UsingDecl, {
1700   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1701   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1702 })
1703 
1704 DEF_TRAVERSE_DECL(UsingEnumDecl,
1705                   { TRY_TO(TraverseTypeLoc(D->getEnumTypeLoc())); })
1706 
1707 DEF_TRAVERSE_DECL(UsingPackDecl, {})
1708 
1709 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1710   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1711 })
1712 
1713 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1714 
1715 DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {})
1716 
1717 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1718   for (auto *I : D->varlists()) {
1719     TRY_TO(TraverseStmt(I));
1720   }
1721  })
1722 
1723 DEF_TRAVERSE_DECL(OMPRequiresDecl, {
1724   for (auto *C : D->clauselists()) {
1725     TRY_TO(TraverseOMPClause(C));
1726   }
1727 })
1728 
1729 DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, {
1730   TRY_TO(TraverseStmt(D->getCombiner()));
1731   if (auto *Initializer = D->getInitializer())
1732     TRY_TO(TraverseStmt(Initializer));
1733   TRY_TO(TraverseType(D->getType()));
1734   return true;
1735 })
1736 
1737 DEF_TRAVERSE_DECL(OMPDeclareMapperDecl, {
1738   for (auto *C : D->clauselists())
1739     TRY_TO(TraverseOMPClause(C));
1740   TRY_TO(TraverseType(D->getType()));
1741   return true;
1742 })
1743 
1744 DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
1745 
1746 DEF_TRAVERSE_DECL(OMPAllocateDecl, {
1747   for (auto *I : D->varlists())
1748     TRY_TO(TraverseStmt(I));
1749   for (auto *C : D->clauselists())
1750     TRY_TO(TraverseOMPClause(C));
1751 })
1752 
1753 // A helper method for TemplateDecl's children.
1754 template <typename Derived>
1755 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1756     TemplateParameterList *TPL) {
1757   if (TPL) {
1758     for (NamedDecl *D : *TPL) {
1759       TRY_TO(TraverseDecl(D));
1760     }
1761     if (Expr *RequiresClause = TPL->getRequiresClause()) {
1762       TRY_TO(TraverseStmt(RequiresClause));
1763     }
1764   }
1765   return true;
1766 }
1767 
1768 template <typename Derived>
1769 template <typename T>
1770 bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) {
1771   for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) {
1772     TemplateParameterList *TPL = D->getTemplateParameterList(i);
1773     TraverseTemplateParameterListHelper(TPL);
1774   }
1775   return true;
1776 }
1777 
1778 template <typename Derived>
1779 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1780     ClassTemplateDecl *D) {
1781   for (auto *SD : D->specializations()) {
1782     for (auto *RD : SD->redecls()) {
1783       assert(!cast<CXXRecordDecl>(RD)->isInjectedClassName());
1784       switch (
1785           cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1786       // Visit the implicit instantiations with the requested pattern.
1787       case TSK_Undeclared:
1788       case TSK_ImplicitInstantiation:
1789         TRY_TO(TraverseDecl(RD));
1790         break;
1791 
1792       // We don't need to do anything on an explicit instantiation
1793       // or explicit specialization because there will be an explicit
1794       // node for it elsewhere.
1795       case TSK_ExplicitInstantiationDeclaration:
1796       case TSK_ExplicitInstantiationDefinition:
1797       case TSK_ExplicitSpecialization:
1798         break;
1799       }
1800     }
1801   }
1802 
1803   return true;
1804 }
1805 
1806 template <typename Derived>
1807 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1808     VarTemplateDecl *D) {
1809   for (auto *SD : D->specializations()) {
1810     for (auto *RD : SD->redecls()) {
1811       switch (
1812           cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1813       case TSK_Undeclared:
1814       case TSK_ImplicitInstantiation:
1815         TRY_TO(TraverseDecl(RD));
1816         break;
1817 
1818       case TSK_ExplicitInstantiationDeclaration:
1819       case TSK_ExplicitInstantiationDefinition:
1820       case TSK_ExplicitSpecialization:
1821         break;
1822       }
1823     }
1824   }
1825 
1826   return true;
1827 }
1828 
1829 // A helper method for traversing the instantiations of a
1830 // function while skipping its specializations.
1831 template <typename Derived>
1832 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1833     FunctionTemplateDecl *D) {
1834   for (auto *FD : D->specializations()) {
1835     for (auto *RD : FD->redecls()) {
1836       switch (RD->getTemplateSpecializationKind()) {
1837       case TSK_Undeclared:
1838       case TSK_ImplicitInstantiation:
1839         // We don't know what kind of FunctionDecl this is.
1840         TRY_TO(TraverseDecl(RD));
1841         break;
1842 
1843       // FIXME: For now traverse explicit instantiations here. Change that
1844       // once they are represented as dedicated nodes in the AST.
1845       case TSK_ExplicitInstantiationDeclaration:
1846       case TSK_ExplicitInstantiationDefinition:
1847         TRY_TO(TraverseDecl(RD));
1848         break;
1849 
1850       case TSK_ExplicitSpecialization:
1851         break;
1852       }
1853     }
1854   }
1855 
1856   return true;
1857 }
1858 
1859 // This macro unifies the traversal of class, variable and function
1860 // template declarations.
1861 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND)                                   \
1862   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, {                              \
1863     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
1864     TRY_TO(TraverseDecl(D->getTemplatedDecl()));                               \
1865                                                                                \
1866     /* By default, we do not traverse the instantiations of                    \
1867        class templates since they do not appear in the user code. The          \
1868        following code optionally traverses them.                               \
1869                                                                                \
1870        We only traverse the class instantiations when we see the canonical     \
1871        declaration of the template, to ensure we only visit them once. */      \
1872     if (getDerived().shouldVisitTemplateInstantiations() &&                    \
1873         D == D->getCanonicalDecl())                                            \
1874       TRY_TO(TraverseTemplateInstantiations(D));                               \
1875                                                                                \
1876     /* Note that getInstantiatedFromMemberTemplate() is just a link            \
1877        from a template instantiation back to the template from which           \
1878        it was instantiated, and thus should not be traversed. */               \
1879   })
1880 
1881 DEF_TRAVERSE_TMPL_DECL(Class)
1882 DEF_TRAVERSE_TMPL_DECL(Var)
1883 DEF_TRAVERSE_TMPL_DECL(Function)
1884 
1885 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1886   // D is the "T" in something like
1887   //   template <template <typename> class T> class container { };
1888   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1889   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1890     TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1891   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1892 })
1893 
1894 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1895   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1896 })
1897 
1898 template <typename Derived>
1899 bool RecursiveASTVisitor<Derived>::TraverseTemplateTypeParamDeclConstraints(
1900     const TemplateTypeParmDecl *D) {
1901   if (const auto *TC = D->getTypeConstraint())
1902     TRY_TO(TraverseTypeConstraint(TC));
1903   return true;
1904 }
1905 
1906 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1907   // D is the "T" in something like "template<typename T> class vector;"
1908   if (D->getTypeForDecl())
1909     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1910   TRY_TO(TraverseTemplateTypeParamDeclConstraints(D));
1911   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1912     TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1913 })
1914 
1915 DEF_TRAVERSE_DECL(TypedefDecl, {
1916   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1917   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1918   // declaring the typedef, not something that was written in the
1919   // source.
1920 })
1921 
1922 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1923   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1924   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1925   // declaring the type alias, not something that was written in the
1926   // source.
1927 })
1928 
1929 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1930   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1931   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1932 })
1933 
1934 DEF_TRAVERSE_DECL(ConceptDecl, {
1935   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1936   TRY_TO(TraverseStmt(D->getConstraintExpr()));
1937 })
1938 
1939 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1940   // A dependent using declaration which was marked with 'typename'.
1941   //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1942   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1943   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1944   // declaring the type, not something that was written in the
1945   // source.
1946 })
1947 
1948 DEF_TRAVERSE_DECL(UnresolvedUsingIfExistsDecl, {})
1949 
1950 DEF_TRAVERSE_DECL(EnumDecl, {
1951   TRY_TO(TraverseDeclTemplateParameterLists(D));
1952 
1953   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1954   if (auto *TSI = D->getIntegerTypeSourceInfo())
1955     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1956   // The enumerators are already traversed by
1957   // decls_begin()/decls_end().
1958 })
1959 
1960 // Helper methods for RecordDecl and its children.
1961 template <typename Derived>
1962 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1963   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1964   // declaring the type, not something that was written in the source.
1965 
1966   TRY_TO(TraverseDeclTemplateParameterLists(D));
1967   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1968   return true;
1969 }
1970 
1971 template <typename Derived>
1972 bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier(
1973     const CXXBaseSpecifier &Base) {
1974   TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc()));
1975   return true;
1976 }
1977 
1978 template <typename Derived>
1979 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1980   if (!TraverseRecordHelper(D))
1981     return false;
1982   if (D->isCompleteDefinition()) {
1983     for (const auto &I : D->bases()) {
1984       TRY_TO(TraverseCXXBaseSpecifier(I));
1985     }
1986     // We don't traverse the friends or the conversions, as they are
1987     // already in decls_begin()/decls_end().
1988   }
1989   return true;
1990 }
1991 
1992 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1993 
1994 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1995 
1996 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND, DECLKIND)                    \
1997   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
1998     /* For implicit instantiations ("set<int> x;"), we don't want to           \
1999        recurse at all, since the instatiated template isn't written in         \
2000        the source code anywhere.  (Note the instatiated *type* --              \
2001        set<int> -- is written, and will still get a callback of                \
2002        TemplateSpecializationType).  For explicit instantiations               \
2003        ("template set<int>;"), we do need a callback, since this               \
2004        is the only callback that's made for this instantiation.                \
2005        We use getTypeAsWritten() to distinguish. */                            \
2006     if (TypeSourceInfo *TSI = D->getTypeAsWritten())                           \
2007       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
2008                                                                                \
2009     if (getDerived().shouldVisitTemplateInstantiations() ||                    \
2010         D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {    \
2011       /* Traverse base definition for explicit specializations */              \
2012       TRY_TO(Traverse##DECLKIND##Helper(D));                                   \
2013     } else {                                                                   \
2014       TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));            \
2015                                                                                \
2016       /* Returning from here skips traversing the                              \
2017          declaration context of the *TemplateSpecializationDecl                \
2018          (embedded in the DEF_TRAVERSE_DECL() macro)                           \
2019          which contains the instantiated members of the template. */           \
2020       return true;                                                             \
2021     }                                                                          \
2022   })
2023 
2024 DEF_TRAVERSE_TMPL_SPEC_DECL(Class, CXXRecord)
2025 DEF_TRAVERSE_TMPL_SPEC_DECL(Var, Var)
2026 
2027 template <typename Derived>
2028 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
2029     const TemplateArgumentLoc *TAL, unsigned Count) {
2030   for (unsigned I = 0; I < Count; ++I) {
2031     TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
2032   }
2033   return true;
2034 }
2035 
2036 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
2037   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
2038     /* The partial specialization. */                                          \
2039     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
2040     /* The args that remains unspecialized. */                                 \
2041     TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
2042         D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
2043         D->getTemplateArgsAsWritten()->NumTemplateArgs));                      \
2044                                                                                \
2045     /* Don't need the *TemplatePartialSpecializationHelper, even               \
2046        though that's our parent class -- we already visit all the              \
2047        template args here. */                                                  \
2048     TRY_TO(Traverse##DECLKIND##Helper(D));                                     \
2049                                                                                \
2050     /* Instantiations will have been visited with the primary template. */     \
2051   })
2052 
2053 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
2054 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
2055 
2056 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
2057 
2058 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
2059   // Like UnresolvedUsingTypenameDecl, but without the 'typename':
2060   //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
2061   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
2062   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
2063 })
2064 
2065 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
2066 
2067 template <typename Derived>
2068 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
2069   TRY_TO(TraverseDeclTemplateParameterLists(D));
2070   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
2071   if (D->getTypeSourceInfo())
2072     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
2073   else
2074     TRY_TO(TraverseType(D->getType()));
2075   return true;
2076 }
2077 
2078 DEF_TRAVERSE_DECL(DecompositionDecl, {
2079   TRY_TO(TraverseVarHelper(D));
2080   for (auto *Binding : D->bindings()) {
2081     TRY_TO(TraverseDecl(Binding));
2082   }
2083 })
2084 
2085 DEF_TRAVERSE_DECL(BindingDecl, {
2086   if (getDerived().shouldVisitImplicitCode())
2087     TRY_TO(TraverseStmt(D->getBinding()));
2088 })
2089 
2090 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
2091 
2092 DEF_TRAVERSE_DECL(MSGuidDecl, {})
2093 DEF_TRAVERSE_DECL(UnnamedGlobalConstantDecl, {})
2094 
2095 DEF_TRAVERSE_DECL(TemplateParamObjectDecl, {})
2096 
2097 DEF_TRAVERSE_DECL(FieldDecl, {
2098   TRY_TO(TraverseDeclaratorHelper(D));
2099   if (D->isBitField())
2100     TRY_TO(TraverseStmt(D->getBitWidth()));
2101   if (D->hasInClassInitializer())
2102     TRY_TO(TraverseStmt(D->getInClassInitializer()));
2103 })
2104 
2105 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
2106   TRY_TO(TraverseDeclaratorHelper(D));
2107   if (D->isBitField())
2108     TRY_TO(TraverseStmt(D->getBitWidth()));
2109   // FIXME: implement the rest.
2110 })
2111 
2112 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
2113   TRY_TO(TraverseDeclaratorHelper(D));
2114   if (D->isBitField())
2115     TRY_TO(TraverseStmt(D->getBitWidth()));
2116   // FIXME: implement the rest.
2117 })
2118 
2119 template <typename Derived>
2120 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
2121   TRY_TO(TraverseDeclTemplateParameterLists(D));
2122   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
2123   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
2124 
2125   // If we're an explicit template specialization, iterate over the
2126   // template args that were explicitly specified.  If we were doing
2127   // this in typing order, we'd do it between the return type and
2128   // the function args, but both are handled by the FunctionTypeLoc
2129   // above, so we have to choose one side.  I've decided to do before.
2130   if (const FunctionTemplateSpecializationInfo *FTSI =
2131           D->getTemplateSpecializationInfo()) {
2132     if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
2133         FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
2134       // A specialization might not have explicit template arguments if it has
2135       // a templated return type and concrete arguments.
2136       if (const ASTTemplateArgumentListInfo *TALI =
2137               FTSI->TemplateArgumentsAsWritten) {
2138         TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
2139                                                   TALI->NumTemplateArgs));
2140       }
2141     }
2142   } else if (const DependentFunctionTemplateSpecializationInfo *DFSI =
2143                  D->getDependentSpecializationInfo()) {
2144     if (const ASTTemplateArgumentListInfo *TALI =
2145             DFSI->TemplateArgumentsAsWritten) {
2146       TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
2147                                                 TALI->NumTemplateArgs));
2148     }
2149   }
2150 
2151   // Visit the function type itself, which can be either
2152   // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
2153   // also covers the return type and the function parameters,
2154   // including exception specifications.
2155   if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
2156     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2157   } else if (getDerived().shouldVisitImplicitCode()) {
2158     // Visit parameter variable declarations of the implicit function
2159     // if the traverser is visiting implicit code. Parameter variable
2160     // declarations do not have valid TypeSourceInfo, so to visit them
2161     // we need to traverse the declarations explicitly.
2162     for (ParmVarDecl *Parameter : D->parameters()) {
2163       TRY_TO(TraverseDecl(Parameter));
2164     }
2165   }
2166 
2167   // Visit the trailing requires clause, if any.
2168   if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
2169     TRY_TO(TraverseStmt(TrailingRequiresClause));
2170   }
2171 
2172   if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2173     // Constructor initializers.
2174     for (auto *I : Ctor->inits()) {
2175       if (I->isWritten() || getDerived().shouldVisitImplicitCode())
2176         TRY_TO(TraverseConstructorInitializer(I));
2177     }
2178   }
2179 
2180   bool VisitBody =
2181       D->isThisDeclarationADefinition() &&
2182       // Don't visit the function body if the function definition is generated
2183       // by clang.
2184       (!D->isDefaulted() || getDerived().shouldVisitImplicitCode());
2185 
2186   if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2187     if (const CXXRecordDecl *RD = MD->getParent()) {
2188       if (RD->isLambda() &&
2189           declaresSameEntity(RD->getLambdaCallOperator(), MD)) {
2190         VisitBody = VisitBody && getDerived().shouldVisitLambdaBody();
2191       }
2192     }
2193   }
2194 
2195   if (VisitBody) {
2196     TRY_TO(TraverseStmt(D->getBody()));
2197     // Body may contain using declarations whose shadows are parented to the
2198     // FunctionDecl itself.
2199     for (auto *Child : D->decls()) {
2200       if (isa<UsingShadowDecl>(Child))
2201         TRY_TO(TraverseDecl(Child));
2202     }
2203   }
2204   return true;
2205 }
2206 
2207 DEF_TRAVERSE_DECL(FunctionDecl, {
2208   // We skip decls_begin/decls_end, which are already covered by
2209   // TraverseFunctionHelper().
2210   ShouldVisitChildren = false;
2211   ReturnValue = TraverseFunctionHelper(D);
2212 })
2213 
2214 DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
2215   // We skip decls_begin/decls_end, which are already covered by
2216   // TraverseFunctionHelper().
2217   ShouldVisitChildren = false;
2218   ReturnValue = TraverseFunctionHelper(D);
2219 })
2220 
2221 DEF_TRAVERSE_DECL(CXXMethodDecl, {
2222   // We skip decls_begin/decls_end, which are already covered by
2223   // TraverseFunctionHelper().
2224   ShouldVisitChildren = false;
2225   ReturnValue = TraverseFunctionHelper(D);
2226 })
2227 
2228 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
2229   // We skip decls_begin/decls_end, which are already covered by
2230   // TraverseFunctionHelper().
2231   ShouldVisitChildren = false;
2232   ReturnValue = TraverseFunctionHelper(D);
2233 })
2234 
2235 // CXXConversionDecl is the declaration of a type conversion operator.
2236 // It's not a cast expression.
2237 DEF_TRAVERSE_DECL(CXXConversionDecl, {
2238   // We skip decls_begin/decls_end, which are already covered by
2239   // TraverseFunctionHelper().
2240   ShouldVisitChildren = false;
2241   ReturnValue = TraverseFunctionHelper(D);
2242 })
2243 
2244 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
2245   // We skip decls_begin/decls_end, which are already covered by
2246   // TraverseFunctionHelper().
2247   ShouldVisitChildren = false;
2248   ReturnValue = TraverseFunctionHelper(D);
2249 })
2250 
2251 template <typename Derived>
2252 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
2253   TRY_TO(TraverseDeclaratorHelper(D));
2254   // Default params are taken care of when we traverse the ParmVarDecl.
2255   if (!isa<ParmVarDecl>(D) &&
2256       (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
2257     TRY_TO(TraverseStmt(D->getInit()));
2258   return true;
2259 }
2260 
2261 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
2262 
2263 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
2264 
2265 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
2266   // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
2267   TRY_TO(TraverseDeclaratorHelper(D));
2268   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
2269     TRY_TO(TraverseStmt(D->getDefaultArgument()));
2270 })
2271 
2272 DEF_TRAVERSE_DECL(ParmVarDecl, {
2273   TRY_TO(TraverseVarHelper(D));
2274 
2275   if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
2276       !D->hasUnparsedDefaultArg())
2277     TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
2278 
2279   if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
2280       !D->hasUnparsedDefaultArg())
2281     TRY_TO(TraverseStmt(D->getDefaultArg()));
2282 })
2283 
2284 DEF_TRAVERSE_DECL(RequiresExprBodyDecl, {})
2285 
2286 DEF_TRAVERSE_DECL(ImplicitConceptSpecializationDecl, {
2287   TRY_TO(TraverseTemplateArguments(D->getTemplateArguments()));
2288 })
2289 
2290 #undef DEF_TRAVERSE_DECL
2291 
2292 // ----------------- Stmt traversal -----------------
2293 //
2294 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
2295 // over the children defined in children() (every stmt defines these,
2296 // though sometimes the range is empty).  Each individual Traverse*
2297 // method only needs to worry about children other than those.  To see
2298 // what children() does for a given class, see, e.g.,
2299 //   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
2300 
2301 // This macro makes available a variable S, the passed-in stmt.
2302 #define DEF_TRAVERSE_STMT(STMT, CODE)                                          \
2303   template <typename Derived>                                                  \
2304   bool RecursiveASTVisitor<Derived>::Traverse##STMT(                           \
2305       STMT *S, DataRecursionQueue *Queue) {                                    \
2306     bool ShouldVisitChildren = true;                                           \
2307     bool ReturnValue = true;                                                   \
2308     if (!getDerived().shouldTraversePostOrder())                               \
2309       TRY_TO(WalkUpFrom##STMT(S));                                             \
2310     { CODE; }                                                                  \
2311     if (ShouldVisitChildren) {                                                 \
2312       for (Stmt * SubStmt : getDerived().getStmtChildren(S)) {                 \
2313         TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);                              \
2314       }                                                                        \
2315     }                                                                          \
2316     /* Call WalkUpFrom if TRY_TO_TRAVERSE_OR_ENQUEUE_STMT has traversed the    \
2317      * children already. If TRY_TO_TRAVERSE_OR_ENQUEUE_STMT only enqueued the  \
2318      * children, PostVisitStmt will call WalkUpFrom after we are done visiting \
2319      * children. */                                                            \
2320     if (!Queue && ReturnValue && getDerived().shouldTraversePostOrder()) {     \
2321       TRY_TO(WalkUpFrom##STMT(S));                                             \
2322     }                                                                          \
2323     return ReturnValue;                                                        \
2324   }
2325 
2326 DEF_TRAVERSE_STMT(GCCAsmStmt, {
2327   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
2328   for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
2329     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
2330   }
2331   for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
2332     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
2333   }
2334   for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
2335     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
2336   }
2337   // children() iterates over inputExpr and outputExpr.
2338 })
2339 
2340 DEF_TRAVERSE_STMT(
2341     MSAsmStmt,
2342     {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
2343      // added this needs to be implemented.
2344     })
2345 
2346 DEF_TRAVERSE_STMT(CXXCatchStmt, {
2347   TRY_TO(TraverseDecl(S->getExceptionDecl()));
2348   // children() iterates over the handler block.
2349 })
2350 
2351 DEF_TRAVERSE_STMT(DeclStmt, {
2352   for (auto *I : S->decls()) {
2353     TRY_TO(TraverseDecl(I));
2354   }
2355   // Suppress the default iteration over children() by
2356   // returning.  Here's why: A DeclStmt looks like 'type var [=
2357   // initializer]'.  The decls above already traverse over the
2358   // initializers, so we don't have to do it again (which
2359   // children() would do).
2360   ShouldVisitChildren = false;
2361 })
2362 
2363 // These non-expr stmts (most of them), do not need any action except
2364 // iterating over the children.
2365 DEF_TRAVERSE_STMT(BreakStmt, {})
2366 DEF_TRAVERSE_STMT(CXXTryStmt, {})
2367 DEF_TRAVERSE_STMT(CaseStmt, {})
2368 DEF_TRAVERSE_STMT(CompoundStmt, {})
2369 DEF_TRAVERSE_STMT(ContinueStmt, {})
2370 DEF_TRAVERSE_STMT(DefaultStmt, {})
2371 DEF_TRAVERSE_STMT(DoStmt, {})
2372 DEF_TRAVERSE_STMT(ForStmt, {})
2373 DEF_TRAVERSE_STMT(GotoStmt, {})
2374 DEF_TRAVERSE_STMT(IfStmt, {})
2375 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
2376 DEF_TRAVERSE_STMT(LabelStmt, {})
2377 DEF_TRAVERSE_STMT(AttributedStmt, {})
2378 DEF_TRAVERSE_STMT(NullStmt, {})
2379 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
2380 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
2381 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
2382 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
2383 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
2384 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
2385 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
2386 
2387 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
2388   if (!getDerived().shouldVisitImplicitCode()) {
2389     if (S->getInit())
2390       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInit());
2391     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
2392     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
2393     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2394     // Visit everything else only if shouldVisitImplicitCode().
2395     ShouldVisitChildren = false;
2396   }
2397 })
2398 
2399 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
2400   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2401   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2402 })
2403 
2404 DEF_TRAVERSE_STMT(ReturnStmt, {})
2405 DEF_TRAVERSE_STMT(SwitchStmt, {})
2406 DEF_TRAVERSE_STMT(WhileStmt, {})
2407 
2408 DEF_TRAVERSE_STMT(ConstantExpr, {})
2409 
2410 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
2411   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2412   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2413   if (S->hasExplicitTemplateArgs()) {
2414     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2415                                               S->getNumTemplateArgs()));
2416   }
2417 })
2418 
2419 DEF_TRAVERSE_STMT(DeclRefExpr, {
2420   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2421   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2422   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2423                                             S->getNumTemplateArgs()));
2424 })
2425 
2426 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
2427   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2428   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2429   if (S->hasExplicitTemplateArgs()) {
2430     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2431                                               S->getNumTemplateArgs()));
2432   }
2433 })
2434 
2435 DEF_TRAVERSE_STMT(MemberExpr, {
2436   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2437   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2438   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2439                                             S->getNumTemplateArgs()));
2440 })
2441 
2442 DEF_TRAVERSE_STMT(
2443     ImplicitCastExpr,
2444     {// We don't traverse the cast type, as it's not written in the
2445      // source code.
2446     })
2447 
2448 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2449   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2450 })
2451 
2452 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2453   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2454 })
2455 
2456 DEF_TRAVERSE_STMT(CXXAddrspaceCastExpr, {
2457   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2458 })
2459 
2460 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2461   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2462 })
2463 
2464 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2465   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2466 })
2467 
2468 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2469   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2470 })
2471 
2472 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2473   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2474 })
2475 
2476 DEF_TRAVERSE_STMT(BuiltinBitCastExpr, {
2477   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2478 })
2479 
2480 template <typename Derived>
2481 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2482     InitListExpr *S, DataRecursionQueue *Queue) {
2483   if (S) {
2484     // Skip this if we traverse postorder. We will visit it later
2485     // in PostVisitStmt.
2486     if (!getDerived().shouldTraversePostOrder())
2487       TRY_TO(WalkUpFromInitListExpr(S));
2488 
2489     // All we need are the default actions.  FIXME: use a helper function.
2490     for (Stmt *SubStmt : S->children()) {
2491       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2492     }
2493 
2494     if (!Queue && getDerived().shouldTraversePostOrder())
2495       TRY_TO(WalkUpFromInitListExpr(S));
2496   }
2497   return true;
2498 }
2499 
2500 template <typename Derived>
2501 bool RecursiveASTVisitor<Derived>::TraverseObjCProtocolLoc(
2502     ObjCProtocolLoc ProtocolLoc) {
2503   return true;
2504 }
2505 
2506 template <typename Derived>
2507 bool RecursiveASTVisitor<Derived>::TraverseConceptReference(
2508     ConceptReference *CR) {
2509   if (!getDerived().shouldTraversePostOrder())
2510     TRY_TO(VisitConceptReference(CR));
2511   TRY_TO(TraverseNestedNameSpecifierLoc(CR->getNestedNameSpecifierLoc()));
2512   TRY_TO(TraverseDeclarationNameInfo(CR->getConceptNameInfo()));
2513   if (CR->hasExplicitTemplateArgs())
2514     TRY_TO(TraverseTemplateArgumentLocsHelper(
2515         CR->getTemplateArgsAsWritten()->getTemplateArgs(),
2516         CR->getTemplateArgsAsWritten()->NumTemplateArgs));
2517   if (getDerived().shouldTraversePostOrder())
2518     TRY_TO(VisitConceptReference(CR));
2519   return true;
2520 }
2521 
2522 // If shouldVisitImplicitCode() returns false, this method traverses only the
2523 // syntactic form of InitListExpr.
2524 // If shouldVisitImplicitCode() return true, this method is called once for
2525 // each pair of syntactic and semantic InitListExpr, and it traverses the
2526 // subtrees defined by the two forms. This may cause some of the children to be
2527 // visited twice, if they appear both in the syntactic and the semantic form.
2528 //
2529 // There is no guarantee about which form \p S takes when this method is called.
2530 template <typename Derived>
2531 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(
2532     InitListExpr *S, DataRecursionQueue *Queue) {
2533   if (S->isSemanticForm() && S->isSyntacticForm()) {
2534     // `S` does not have alternative forms, traverse only once.
2535     TRY_TO(TraverseSynOrSemInitListExpr(S, Queue));
2536     return true;
2537   }
2538   TRY_TO(TraverseSynOrSemInitListExpr(
2539       S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2540   if (getDerived().shouldVisitImplicitCode()) {
2541     // Only visit the semantic form if the clients are interested in implicit
2542     // compiler-generated.
2543     TRY_TO(TraverseSynOrSemInitListExpr(
2544         S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2545   }
2546   return true;
2547 }
2548 
2549 // GenericSelectionExpr is a special case because the types and expressions
2550 // are interleaved.  We also need to watch out for null types (default
2551 // generic associations).
2552 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2553   if (S->isExprPredicate())
2554     TRY_TO(TraverseStmt(S->getControllingExpr()));
2555   else
2556     TRY_TO(TraverseTypeLoc(S->getControllingType()->getTypeLoc()));
2557 
2558   for (const GenericSelectionExpr::Association Assoc : S->associations()) {
2559     if (TypeSourceInfo *TSI = Assoc.getTypeSourceInfo())
2560       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2561     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(Assoc.getAssociationExpr());
2562   }
2563   ShouldVisitChildren = false;
2564 })
2565 
2566 // PseudoObjectExpr is a special case because of the weirdness with
2567 // syntactic expressions and opaque values.
2568 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2569   TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
2570   for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2571                                             e = S->semantics_end();
2572        i != e; ++i) {
2573     Expr *sub = *i;
2574     if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2575       sub = OVE->getSourceExpr();
2576     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
2577   }
2578   ShouldVisitChildren = false;
2579 })
2580 
2581 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2582   // This is called for code like 'return T()' where T is a built-in
2583   // (i.e. non-class) type.
2584   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2585 })
2586 
2587 DEF_TRAVERSE_STMT(CXXNewExpr, {
2588   // The child-iterator will pick up the other arguments.
2589   TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2590 })
2591 
2592 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2593   // The child-iterator will pick up the expression representing
2594   // the field.
2595   // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2596   // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2597   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2598 })
2599 
2600 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2601   // The child-iterator will pick up the arg if it's an expression,
2602   // but not if it's a type.
2603   if (S->isArgumentType())
2604     TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2605 })
2606 
2607 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2608   // The child-iterator will pick up the arg if it's an expression,
2609   // but not if it's a type.
2610   if (S->isTypeOperand())
2611     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2612 })
2613 
2614 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2615   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2616 })
2617 
2618 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
2619 
2620 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2621   // The child-iterator will pick up the arg if it's an expression,
2622   // but not if it's a type.
2623   if (S->isTypeOperand())
2624     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2625 })
2626 
2627 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2628   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2629     TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2630 })
2631 
2632 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2633   TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2634 })
2635 
2636 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2637                   { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2638 
2639 DEF_TRAVERSE_STMT(VAArgExpr, {
2640   // The child-iterator will pick up the expression argument.
2641   TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2642 })
2643 
2644 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2645   // This is called for code like 'return T()' where T is a class type.
2646   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2647 })
2648 
2649 // Walk only the visible parts of lambda expressions.
2650 DEF_TRAVERSE_STMT(LambdaExpr, {
2651   // Visit the capture list.
2652   for (unsigned I = 0, N = S->capture_size(); I != N; ++I) {
2653     const LambdaCapture *C = S->capture_begin() + I;
2654     if (C->isExplicit() || getDerived().shouldVisitImplicitCode()) {
2655       TRY_TO(TraverseLambdaCapture(S, C, S->capture_init_begin()[I]));
2656     }
2657   }
2658 
2659   if (getDerived().shouldVisitImplicitCode()) {
2660     // The implicit model is simple: everything else is in the lambda class.
2661     TRY_TO(TraverseDecl(S->getLambdaClass()));
2662   } else {
2663     // We need to poke around to find the bits that might be explicitly written.
2664     TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2665     FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>();
2666 
2667     TRY_TO(TraverseTemplateParameterListHelper(S->getTemplateParameterList()));
2668     if (S->hasExplicitParameters()) {
2669       // Visit parameters.
2670       for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
2671         TRY_TO(TraverseDecl(Proto.getParam(I)));
2672     }
2673 
2674     auto *T = Proto.getTypePtr();
2675     for (const auto &E : T->exceptions())
2676       TRY_TO(TraverseType(E));
2677 
2678     if (Expr *NE = T->getNoexceptExpr())
2679       TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2680 
2681     if (S->hasExplicitResultType())
2682       TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2683     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getTrailingRequiresClause());
2684 
2685     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2686   }
2687   ShouldVisitChildren = false;
2688 })
2689 
2690 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2691   // This is called for code like 'T()', where T is a template argument.
2692   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2693 })
2694 
2695 // These expressions all might take explicit template arguments.
2696 // We traverse those if so.  FIXME: implement these.
2697 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2698 DEF_TRAVERSE_STMT(CallExpr, {})
2699 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2700 
2701 // These exprs (most of them), do not need any action except iterating
2702 // over the children.
2703 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2704 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2705 DEF_TRAVERSE_STMT(MatrixSubscriptExpr, {})
2706 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2707 DEF_TRAVERSE_STMT(OMPArrayShapingExpr, {})
2708 DEF_TRAVERSE_STMT(OMPIteratorExpr, {})
2709 
2710 DEF_TRAVERSE_STMT(BlockExpr, {
2711   TRY_TO(TraverseDecl(S->getBlockDecl()));
2712   return true; // no child statements to loop through.
2713 })
2714 
2715 DEF_TRAVERSE_STMT(ChooseExpr, {})
2716 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2717   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2718 })
2719 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2720 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2721 
2722 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {
2723   if (getDerived().shouldVisitImplicitCode())
2724     TRY_TO(TraverseStmt(S->getExpr()));
2725 })
2726 
2727 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {
2728   if (getDerived().shouldVisitImplicitCode())
2729     TRY_TO(TraverseStmt(S->getExpr()));
2730 })
2731 
2732 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2733 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2734 DEF_TRAVERSE_STMT(CXXInheritedCtorInitExpr, {})
2735 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2736 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2737 
2738 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2739   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2740   if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2741     TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2742   if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2743     TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2744 })
2745 
2746 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2747 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2748 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2749 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2750 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2751 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2752 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2753 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2754 DEF_TRAVERSE_STMT(NoInitExpr, {})
2755 DEF_TRAVERSE_STMT(ArrayInitLoopExpr, {
2756   // FIXME: The source expression of the OVE should be listed as
2757   // a child of the ArrayInitLoopExpr.
2758   if (OpaqueValueExpr *OVE = S->getCommonExpr())
2759     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr());
2760 })
2761 DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {})
2762 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2763 
2764 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2765   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2766     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2767 })
2768 
2769 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2770 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2771 
2772 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2773   if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2774     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2775 })
2776 
2777 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {
2778   if (S->isClassReceiver()) {
2779     ObjCInterfaceDecl *IDecl = S->getClassReceiver();
2780     QualType Type = IDecl->getASTContext().getObjCInterfaceType(IDecl);
2781     ObjCInterfaceLocInfo Data;
2782     Data.NameLoc = S->getReceiverLocation();
2783     Data.NameEndLoc = Data.NameLoc;
2784     TRY_TO(TraverseTypeLoc(TypeLoc(Type, &Data)));
2785   }
2786 })
2787 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2788 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2789 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2790 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2791 
2792 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2793   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2794 })
2795 
2796 DEF_TRAVERSE_STMT(ObjCAvailabilityCheckExpr, {})
2797 DEF_TRAVERSE_STMT(ParenExpr, {})
2798 DEF_TRAVERSE_STMT(ParenListExpr, {})
2799 DEF_TRAVERSE_STMT(SYCLUniqueStableNameExpr, {
2800   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2801 })
2802 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2803 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2804 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2805 DEF_TRAVERSE_STMT(StmtExpr, {})
2806 DEF_TRAVERSE_STMT(SourceLocExpr, {})
2807 
2808 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2809   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2810   if (S->hasExplicitTemplateArgs()) {
2811     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2812                                               S->getNumTemplateArgs()));
2813   }
2814 })
2815 
2816 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2817   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2818   if (S->hasExplicitTemplateArgs()) {
2819     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2820                                               S->getNumTemplateArgs()));
2821   }
2822 })
2823 
2824 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2825 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2826 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2827 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2828 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2829 
2830 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2831 DEF_TRAVERSE_STMT(CXXRewrittenBinaryOperator, {
2832   if (!getDerived().shouldVisitImplicitCode()) {
2833     CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
2834         S->getDecomposedForm();
2835     TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.LHS)));
2836     TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.RHS)));
2837     ShouldVisitChildren = false;
2838   }
2839 })
2840 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2841 DEF_TRAVERSE_STMT(TypoExpr, {})
2842 DEF_TRAVERSE_STMT(RecoveryExpr, {})
2843 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2844 
2845 // These operators (all of them) do not need any action except
2846 // iterating over the children.
2847 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2848 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2849 DEF_TRAVERSE_STMT(UnaryOperator, {})
2850 DEF_TRAVERSE_STMT(BinaryOperator, {})
2851 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2852 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2853 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2854 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2855 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2856 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2857 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2858 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2859 DEF_TRAVERSE_STMT(AtomicExpr, {})
2860 DEF_TRAVERSE_STMT(CXXParenListInitExpr, {})
2861 
2862 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {
2863   if (S->getLifetimeExtendedTemporaryDecl()) {
2864     TRY_TO(TraverseLifetimeExtendedTemporaryDecl(
2865         S->getLifetimeExtendedTemporaryDecl()));
2866     ShouldVisitChildren = false;
2867   }
2868 })
2869 // For coroutines expressions, traverse either the operand
2870 // as written or the implied calls, depending on what the
2871 // derived class requests.
2872 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2873   if (!getDerived().shouldVisitImplicitCode()) {
2874     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2875     ShouldVisitChildren = false;
2876   }
2877 })
2878 DEF_TRAVERSE_STMT(CoreturnStmt, {
2879   if (!getDerived().shouldVisitImplicitCode()) {
2880     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2881     ShouldVisitChildren = false;
2882   }
2883 })
2884 DEF_TRAVERSE_STMT(CoawaitExpr, {
2885   if (!getDerived().shouldVisitImplicitCode()) {
2886     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2887     ShouldVisitChildren = false;
2888   }
2889 })
2890 DEF_TRAVERSE_STMT(DependentCoawaitExpr, {
2891   if (!getDerived().shouldVisitImplicitCode()) {
2892     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2893     ShouldVisitChildren = false;
2894   }
2895 })
2896 DEF_TRAVERSE_STMT(CoyieldExpr, {
2897   if (!getDerived().shouldVisitImplicitCode()) {
2898     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2899     ShouldVisitChildren = false;
2900   }
2901 })
2902 
2903 DEF_TRAVERSE_STMT(ConceptSpecializationExpr, {
2904   TRY_TO(TraverseConceptReference(S->getConceptReference()));
2905 })
2906 
2907 DEF_TRAVERSE_STMT(RequiresExpr, {
2908   TRY_TO(TraverseDecl(S->getBody()));
2909   for (ParmVarDecl *Parm : S->getLocalParameters())
2910     TRY_TO(TraverseDecl(Parm));
2911   for (concepts::Requirement *Req : S->getRequirements())
2912     TRY_TO(TraverseConceptRequirement(Req));
2913 })
2914 
2915 // These literals (all of them) do not need any action.
2916 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2917 DEF_TRAVERSE_STMT(FixedPointLiteral, {})
2918 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2919 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2920 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2921 DEF_TRAVERSE_STMT(StringLiteral, {})
2922 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2923 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2924 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2925 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2926 
2927 // Traverse OpenCL: AsType, Convert.
2928 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2929 
2930 // OpenMP directives.
2931 template <typename Derived>
2932 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2933     OMPExecutableDirective *S) {
2934   for (auto *C : S->clauses()) {
2935     TRY_TO(TraverseOMPClause(C));
2936   }
2937   return true;
2938 }
2939 
2940 DEF_TRAVERSE_STMT(OMPCanonicalLoop, {
2941   if (!getDerived().shouldVisitImplicitCode()) {
2942     // Visit only the syntactical loop.
2943     TRY_TO(TraverseStmt(S->getLoopStmt()));
2944     ShouldVisitChildren = false;
2945   }
2946 })
2947 
2948 template <typename Derived>
2949 bool
2950 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2951   return TraverseOMPExecutableDirective(S);
2952 }
2953 
2954 DEF_TRAVERSE_STMT(OMPMetaDirective,
2955                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2956 
2957 DEF_TRAVERSE_STMT(OMPParallelDirective,
2958                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2959 
2960 DEF_TRAVERSE_STMT(OMPSimdDirective,
2961                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2962 
2963 DEF_TRAVERSE_STMT(OMPTileDirective,
2964                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2965 
2966 DEF_TRAVERSE_STMT(OMPUnrollDirective,
2967                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2968 
2969 DEF_TRAVERSE_STMT(OMPForDirective,
2970                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2971 
2972 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2973                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2974 
2975 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2976                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2977 
2978 DEF_TRAVERSE_STMT(OMPSectionDirective,
2979                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2980 
2981 DEF_TRAVERSE_STMT(OMPScopeDirective,
2982                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2983 
2984 DEF_TRAVERSE_STMT(OMPSingleDirective,
2985                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2986 
2987 DEF_TRAVERSE_STMT(OMPMasterDirective,
2988                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2989 
2990 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2991   TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2992   TRY_TO(TraverseOMPExecutableDirective(S));
2993 })
2994 
2995 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2996                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2997 
2998 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2999                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3000 
3001 DEF_TRAVERSE_STMT(OMPParallelMasterDirective,
3002                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3003 
3004 DEF_TRAVERSE_STMT(OMPParallelMaskedDirective,
3005                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3006 
3007 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
3008                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3009 
3010 DEF_TRAVERSE_STMT(OMPTaskDirective,
3011                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3012 
3013 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
3014                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3015 
3016 DEF_TRAVERSE_STMT(OMPBarrierDirective,
3017                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3018 
3019 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
3020                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3021 
3022 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
3023                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3024 
3025 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
3026                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3027 
3028 DEF_TRAVERSE_STMT(OMPCancelDirective,
3029                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3030 
3031 DEF_TRAVERSE_STMT(OMPFlushDirective,
3032                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3033 
3034 DEF_TRAVERSE_STMT(OMPDepobjDirective,
3035                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3036 
3037 DEF_TRAVERSE_STMT(OMPScanDirective,
3038                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3039 
3040 DEF_TRAVERSE_STMT(OMPOrderedDirective,
3041                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3042 
3043 DEF_TRAVERSE_STMT(OMPAtomicDirective,
3044                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3045 
3046 DEF_TRAVERSE_STMT(OMPTargetDirective,
3047                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3048 
3049 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
3050                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3051 
3052 DEF_TRAVERSE_STMT(OMPTargetEnterDataDirective,
3053                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3054 
3055 DEF_TRAVERSE_STMT(OMPTargetExitDataDirective,
3056                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3057 
3058 DEF_TRAVERSE_STMT(OMPTargetParallelDirective,
3059                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3060 
3061 DEF_TRAVERSE_STMT(OMPTargetParallelForDirective,
3062                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3063 
3064 DEF_TRAVERSE_STMT(OMPTeamsDirective,
3065                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3066 
3067 DEF_TRAVERSE_STMT(OMPTargetUpdateDirective,
3068                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3069 
3070 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
3071                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3072 
3073 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
3074                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3075 
3076 DEF_TRAVERSE_STMT(OMPMasterTaskLoopDirective,
3077                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3078 
3079 DEF_TRAVERSE_STMT(OMPMasterTaskLoopSimdDirective,
3080                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3081 
3082 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopDirective,
3083                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3084 
3085 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopSimdDirective,
3086                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3087 
3088 DEF_TRAVERSE_STMT(OMPMaskedTaskLoopDirective,
3089                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3090 
3091 DEF_TRAVERSE_STMT(OMPMaskedTaskLoopSimdDirective,
3092                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3093 
3094 DEF_TRAVERSE_STMT(OMPParallelMaskedTaskLoopDirective,
3095                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3096 
3097 DEF_TRAVERSE_STMT(OMPParallelMaskedTaskLoopSimdDirective,
3098                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3099 
3100 DEF_TRAVERSE_STMT(OMPDistributeDirective,
3101                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3102 
3103 DEF_TRAVERSE_STMT(OMPDistributeParallelForDirective,
3104                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3105 
3106 DEF_TRAVERSE_STMT(OMPDistributeParallelForSimdDirective,
3107                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3108 
3109 DEF_TRAVERSE_STMT(OMPDistributeSimdDirective,
3110                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3111 
3112 DEF_TRAVERSE_STMT(OMPTargetParallelForSimdDirective,
3113                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3114 
3115 DEF_TRAVERSE_STMT(OMPTargetSimdDirective,
3116                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3117 
3118 DEF_TRAVERSE_STMT(OMPTeamsDistributeDirective,
3119                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3120 
3121 DEF_TRAVERSE_STMT(OMPTeamsDistributeSimdDirective,
3122                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3123 
3124 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForSimdDirective,
3125                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3126 
3127 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForDirective,
3128                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3129 
3130 DEF_TRAVERSE_STMT(OMPTargetTeamsDirective,
3131                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3132 
3133 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeDirective,
3134                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3135 
3136 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForDirective,
3137                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3138 
3139 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForSimdDirective,
3140                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3141 
3142 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeSimdDirective,
3143                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3144 
3145 DEF_TRAVERSE_STMT(OMPInteropDirective,
3146                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3147 
3148 DEF_TRAVERSE_STMT(OMPDispatchDirective,
3149                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3150 
3151 DEF_TRAVERSE_STMT(OMPMaskedDirective,
3152                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3153 
3154 DEF_TRAVERSE_STMT(OMPGenericLoopDirective,
3155                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3156 
3157 DEF_TRAVERSE_STMT(OMPTeamsGenericLoopDirective,
3158                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3159 
3160 DEF_TRAVERSE_STMT(OMPTargetTeamsGenericLoopDirective,
3161                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3162 
3163 DEF_TRAVERSE_STMT(OMPParallelGenericLoopDirective,
3164                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3165 
3166 DEF_TRAVERSE_STMT(OMPTargetParallelGenericLoopDirective,
3167                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3168 
3169 DEF_TRAVERSE_STMT(OMPErrorDirective,
3170                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
3171 
3172 // OpenMP clauses.
3173 template <typename Derived>
3174 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
3175   if (!C)
3176     return true;
3177   switch (C->getClauseKind()) {
3178 #define GEN_CLANG_CLAUSE_CLASS
3179 #define CLAUSE_CLASS(Enum, Str, Class)                                         \
3180   case llvm::omp::Clause::Enum:                                                \
3181     TRY_TO(Visit##Class(static_cast<Class *>(C)));                             \
3182     break;
3183 #define CLAUSE_NO_CLASS(Enum, Str)                                             \
3184   case llvm::omp::Clause::Enum:                                                \
3185     break;
3186 #include "llvm/Frontend/OpenMP/OMP.inc"
3187   }
3188   return true;
3189 }
3190 
3191 template <typename Derived>
3192 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPreInit(
3193     OMPClauseWithPreInit *Node) {
3194   TRY_TO(TraverseStmt(Node->getPreInitStmt()));
3195   return true;
3196 }
3197 
3198 template <typename Derived>
3199 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPostUpdate(
3200     OMPClauseWithPostUpdate *Node) {
3201   TRY_TO(VisitOMPClauseWithPreInit(Node));
3202   TRY_TO(TraverseStmt(Node->getPostUpdateExpr()));
3203   return true;
3204 }
3205 
3206 template <typename Derived>
3207 bool RecursiveASTVisitor<Derived>::VisitOMPAllocatorClause(
3208     OMPAllocatorClause *C) {
3209   TRY_TO(TraverseStmt(C->getAllocator()));
3210   return true;
3211 }
3212 
3213 template <typename Derived>
3214 bool RecursiveASTVisitor<Derived>::VisitOMPAllocateClause(OMPAllocateClause *C) {
3215   TRY_TO(TraverseStmt(C->getAllocator()));
3216   TRY_TO(VisitOMPClauseList(C));
3217   return true;
3218 }
3219 
3220 template <typename Derived>
3221 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
3222   TRY_TO(VisitOMPClauseWithPreInit(C));
3223   TRY_TO(TraverseStmt(C->getCondition()));
3224   return true;
3225 }
3226 
3227 template <typename Derived>
3228 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
3229   TRY_TO(VisitOMPClauseWithPreInit(C));
3230   TRY_TO(TraverseStmt(C->getCondition()));
3231   return true;
3232 }
3233 
3234 template <typename Derived>
3235 bool
3236 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
3237   TRY_TO(VisitOMPClauseWithPreInit(C));
3238   TRY_TO(TraverseStmt(C->getNumThreads()));
3239   return true;
3240 }
3241 
3242 template <typename Derived>
3243 bool RecursiveASTVisitor<Derived>::VisitOMPAlignClause(OMPAlignClause *C) {
3244   TRY_TO(TraverseStmt(C->getAlignment()));
3245   return true;
3246 }
3247 
3248 template <typename Derived>
3249 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
3250   TRY_TO(TraverseStmt(C->getSafelen()));
3251   return true;
3252 }
3253 
3254 template <typename Derived>
3255 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
3256   TRY_TO(TraverseStmt(C->getSimdlen()));
3257   return true;
3258 }
3259 
3260 template <typename Derived>
3261 bool RecursiveASTVisitor<Derived>::VisitOMPSizesClause(OMPSizesClause *C) {
3262   for (Expr *E : C->getSizesRefs())
3263     TRY_TO(TraverseStmt(E));
3264   return true;
3265 }
3266 
3267 template <typename Derived>
3268 bool RecursiveASTVisitor<Derived>::VisitOMPFullClause(OMPFullClause *C) {
3269   return true;
3270 }
3271 
3272 template <typename Derived>
3273 bool RecursiveASTVisitor<Derived>::VisitOMPPartialClause(OMPPartialClause *C) {
3274   TRY_TO(TraverseStmt(C->getFactor()));
3275   return true;
3276 }
3277 
3278 template <typename Derived>
3279 bool
3280 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
3281   TRY_TO(TraverseStmt(C->getNumForLoops()));
3282   return true;
3283 }
3284 
3285 template <typename Derived>
3286 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
3287   return true;
3288 }
3289 
3290 template <typename Derived>
3291 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
3292   return true;
3293 }
3294 
3295 template <typename Derived>
3296 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedAddressClause(
3297     OMPUnifiedAddressClause *) {
3298   return true;
3299 }
3300 
3301 template <typename Derived>
3302 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedSharedMemoryClause(
3303     OMPUnifiedSharedMemoryClause *) {
3304   return true;
3305 }
3306 
3307 template <typename Derived>
3308 bool RecursiveASTVisitor<Derived>::VisitOMPReverseOffloadClause(
3309     OMPReverseOffloadClause *) {
3310   return true;
3311 }
3312 
3313 template <typename Derived>
3314 bool RecursiveASTVisitor<Derived>::VisitOMPDynamicAllocatorsClause(
3315     OMPDynamicAllocatorsClause *) {
3316   return true;
3317 }
3318 
3319 template <typename Derived>
3320 bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause(
3321     OMPAtomicDefaultMemOrderClause *) {
3322   return true;
3323 }
3324 
3325 template <typename Derived>
3326 bool RecursiveASTVisitor<Derived>::VisitOMPAtClause(OMPAtClause *) {
3327   return true;
3328 }
3329 
3330 template <typename Derived>
3331 bool RecursiveASTVisitor<Derived>::VisitOMPSeverityClause(OMPSeverityClause *) {
3332   return true;
3333 }
3334 
3335 template <typename Derived>
3336 bool RecursiveASTVisitor<Derived>::VisitOMPMessageClause(OMPMessageClause *C) {
3337   TRY_TO(TraverseStmt(C->getMessageString()));
3338   return true;
3339 }
3340 
3341 template <typename Derived>
3342 bool
3343 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
3344   TRY_TO(VisitOMPClauseWithPreInit(C));
3345   TRY_TO(TraverseStmt(C->getChunkSize()));
3346   return true;
3347 }
3348 
3349 template <typename Derived>
3350 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
3351   TRY_TO(TraverseStmt(C->getNumForLoops()));
3352   return true;
3353 }
3354 
3355 template <typename Derived>
3356 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
3357   return true;
3358 }
3359 
3360 template <typename Derived>
3361 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
3362   return true;
3363 }
3364 
3365 template <typename Derived>
3366 bool
3367 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
3368   return true;
3369 }
3370 
3371 template <typename Derived>
3372 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
3373   return true;
3374 }
3375 
3376 template <typename Derived>
3377 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
3378   return true;
3379 }
3380 
3381 template <typename Derived>
3382 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
3383   return true;
3384 }
3385 
3386 template <typename Derived>
3387 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
3388   return true;
3389 }
3390 
3391 template <typename Derived>
3392 bool RecursiveASTVisitor<Derived>::VisitOMPCompareClause(OMPCompareClause *) {
3393   return true;
3394 }
3395 
3396 template <typename Derived>
3397 bool RecursiveASTVisitor<Derived>::VisitOMPFailClause(OMPFailClause *) {
3398   return true;
3399 }
3400 
3401 template <typename Derived>
3402 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
3403   return true;
3404 }
3405 
3406 template <typename Derived>
3407 bool RecursiveASTVisitor<Derived>::VisitOMPAcqRelClause(OMPAcqRelClause *) {
3408   return true;
3409 }
3410 
3411 template <typename Derived>
3412 bool RecursiveASTVisitor<Derived>::VisitOMPAcquireClause(OMPAcquireClause *) {
3413   return true;
3414 }
3415 
3416 template <typename Derived>
3417 bool RecursiveASTVisitor<Derived>::VisitOMPReleaseClause(OMPReleaseClause *) {
3418   return true;
3419 }
3420 
3421 template <typename Derived>
3422 bool RecursiveASTVisitor<Derived>::VisitOMPRelaxedClause(OMPRelaxedClause *) {
3423   return true;
3424 }
3425 
3426 template <typename Derived>
3427 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
3428   return true;
3429 }
3430 
3431 template <typename Derived>
3432 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
3433   return true;
3434 }
3435 
3436 template <typename Derived>
3437 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
3438   return true;
3439 }
3440 
3441 template <typename Derived>
3442 bool RecursiveASTVisitor<Derived>::VisitOMPInitClause(OMPInitClause *C) {
3443   TRY_TO(VisitOMPClauseList(C));
3444   return true;
3445 }
3446 
3447 template <typename Derived>
3448 bool RecursiveASTVisitor<Derived>::VisitOMPUseClause(OMPUseClause *C) {
3449   TRY_TO(TraverseStmt(C->getInteropVar()));
3450   return true;
3451 }
3452 
3453 template <typename Derived>
3454 bool RecursiveASTVisitor<Derived>::VisitOMPDestroyClause(OMPDestroyClause *C) {
3455   TRY_TO(TraverseStmt(C->getInteropVar()));
3456   return true;
3457 }
3458 
3459 template <typename Derived>
3460 bool RecursiveASTVisitor<Derived>::VisitOMPNovariantsClause(
3461     OMPNovariantsClause *C) {
3462   TRY_TO(VisitOMPClauseWithPreInit(C));
3463   TRY_TO(TraverseStmt(C->getCondition()));
3464   return true;
3465 }
3466 
3467 template <typename Derived>
3468 bool RecursiveASTVisitor<Derived>::VisitOMPNocontextClause(
3469     OMPNocontextClause *C) {
3470   TRY_TO(VisitOMPClauseWithPreInit(C));
3471   TRY_TO(TraverseStmt(C->getCondition()));
3472   return true;
3473 }
3474 
3475 template <typename Derived>
3476 template <typename T>
3477 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
3478   for (auto *E : Node->varlists()) {
3479     TRY_TO(TraverseStmt(E));
3480   }
3481   return true;
3482 }
3483 
3484 template <typename Derived>
3485 bool RecursiveASTVisitor<Derived>::VisitOMPInclusiveClause(
3486     OMPInclusiveClause *C) {
3487   TRY_TO(VisitOMPClauseList(C));
3488   return true;
3489 }
3490 
3491 template <typename Derived>
3492 bool RecursiveASTVisitor<Derived>::VisitOMPExclusiveClause(
3493     OMPExclusiveClause *C) {
3494   TRY_TO(VisitOMPClauseList(C));
3495   return true;
3496 }
3497 
3498 template <typename Derived>
3499 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
3500   TRY_TO(VisitOMPClauseList(C));
3501   for (auto *E : C->private_copies()) {
3502     TRY_TO(TraverseStmt(E));
3503   }
3504   return true;
3505 }
3506 
3507 template <typename Derived>
3508 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
3509     OMPFirstprivateClause *C) {
3510   TRY_TO(VisitOMPClauseList(C));
3511   TRY_TO(VisitOMPClauseWithPreInit(C));
3512   for (auto *E : C->private_copies()) {
3513     TRY_TO(TraverseStmt(E));
3514   }
3515   for (auto *E : C->inits()) {
3516     TRY_TO(TraverseStmt(E));
3517   }
3518   return true;
3519 }
3520 
3521 template <typename Derived>
3522 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
3523     OMPLastprivateClause *C) {
3524   TRY_TO(VisitOMPClauseList(C));
3525   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3526   for (auto *E : C->private_copies()) {
3527     TRY_TO(TraverseStmt(E));
3528   }
3529   for (auto *E : C->source_exprs()) {
3530     TRY_TO(TraverseStmt(E));
3531   }
3532   for (auto *E : C->destination_exprs()) {
3533     TRY_TO(TraverseStmt(E));
3534   }
3535   for (auto *E : C->assignment_ops()) {
3536     TRY_TO(TraverseStmt(E));
3537   }
3538   return true;
3539 }
3540 
3541 template <typename Derived>
3542 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
3543   TRY_TO(VisitOMPClauseList(C));
3544   return true;
3545 }
3546 
3547 template <typename Derived>
3548 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
3549   TRY_TO(TraverseStmt(C->getStep()));
3550   TRY_TO(TraverseStmt(C->getCalcStep()));
3551   TRY_TO(VisitOMPClauseList(C));
3552   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3553   for (auto *E : C->privates()) {
3554     TRY_TO(TraverseStmt(E));
3555   }
3556   for (auto *E : C->inits()) {
3557     TRY_TO(TraverseStmt(E));
3558   }
3559   for (auto *E : C->updates()) {
3560     TRY_TO(TraverseStmt(E));
3561   }
3562   for (auto *E : C->finals()) {
3563     TRY_TO(TraverseStmt(E));
3564   }
3565   return true;
3566 }
3567 
3568 template <typename Derived>
3569 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
3570   TRY_TO(TraverseStmt(C->getAlignment()));
3571   TRY_TO(VisitOMPClauseList(C));
3572   return true;
3573 }
3574 
3575 template <typename Derived>
3576 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
3577   TRY_TO(VisitOMPClauseList(C));
3578   for (auto *E : C->source_exprs()) {
3579     TRY_TO(TraverseStmt(E));
3580   }
3581   for (auto *E : C->destination_exprs()) {
3582     TRY_TO(TraverseStmt(E));
3583   }
3584   for (auto *E : C->assignment_ops()) {
3585     TRY_TO(TraverseStmt(E));
3586   }
3587   return true;
3588 }
3589 
3590 template <typename Derived>
3591 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
3592     OMPCopyprivateClause *C) {
3593   TRY_TO(VisitOMPClauseList(C));
3594   for (auto *E : C->source_exprs()) {
3595     TRY_TO(TraverseStmt(E));
3596   }
3597   for (auto *E : C->destination_exprs()) {
3598     TRY_TO(TraverseStmt(E));
3599   }
3600   for (auto *E : C->assignment_ops()) {
3601     TRY_TO(TraverseStmt(E));
3602   }
3603   return true;
3604 }
3605 
3606 template <typename Derived>
3607 bool
3608 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
3609   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3610   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3611   TRY_TO(VisitOMPClauseList(C));
3612   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3613   for (auto *E : C->privates()) {
3614     TRY_TO(TraverseStmt(E));
3615   }
3616   for (auto *E : C->lhs_exprs()) {
3617     TRY_TO(TraverseStmt(E));
3618   }
3619   for (auto *E : C->rhs_exprs()) {
3620     TRY_TO(TraverseStmt(E));
3621   }
3622   for (auto *E : C->reduction_ops()) {
3623     TRY_TO(TraverseStmt(E));
3624   }
3625   if (C->getModifier() == OMPC_REDUCTION_inscan) {
3626     for (auto *E : C->copy_ops()) {
3627       TRY_TO(TraverseStmt(E));
3628     }
3629     for (auto *E : C->copy_array_temps()) {
3630       TRY_TO(TraverseStmt(E));
3631     }
3632     for (auto *E : C->copy_array_elems()) {
3633       TRY_TO(TraverseStmt(E));
3634     }
3635   }
3636   return true;
3637 }
3638 
3639 template <typename Derived>
3640 bool RecursiveASTVisitor<Derived>::VisitOMPTaskReductionClause(
3641     OMPTaskReductionClause *C) {
3642   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3643   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3644   TRY_TO(VisitOMPClauseList(C));
3645   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3646   for (auto *E : C->privates()) {
3647     TRY_TO(TraverseStmt(E));
3648   }
3649   for (auto *E : C->lhs_exprs()) {
3650     TRY_TO(TraverseStmt(E));
3651   }
3652   for (auto *E : C->rhs_exprs()) {
3653     TRY_TO(TraverseStmt(E));
3654   }
3655   for (auto *E : C->reduction_ops()) {
3656     TRY_TO(TraverseStmt(E));
3657   }
3658   return true;
3659 }
3660 
3661 template <typename Derived>
3662 bool RecursiveASTVisitor<Derived>::VisitOMPInReductionClause(
3663     OMPInReductionClause *C) {
3664   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3665   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3666   TRY_TO(VisitOMPClauseList(C));
3667   TRY_TO(VisitOMPClauseWithPostUpdate(C));
3668   for (auto *E : C->privates()) {
3669     TRY_TO(TraverseStmt(E));
3670   }
3671   for (auto *E : C->lhs_exprs()) {
3672     TRY_TO(TraverseStmt(E));
3673   }
3674   for (auto *E : C->rhs_exprs()) {
3675     TRY_TO(TraverseStmt(E));
3676   }
3677   for (auto *E : C->reduction_ops()) {
3678     TRY_TO(TraverseStmt(E));
3679   }
3680   for (auto *E : C->taskgroup_descriptors())
3681     TRY_TO(TraverseStmt(E));
3682   return true;
3683 }
3684 
3685 template <typename Derived>
3686 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
3687   TRY_TO(VisitOMPClauseList(C));
3688   return true;
3689 }
3690 
3691 template <typename Derived>
3692 bool RecursiveASTVisitor<Derived>::VisitOMPDepobjClause(OMPDepobjClause *C) {
3693   TRY_TO(TraverseStmt(C->getDepobj()));
3694   return true;
3695 }
3696 
3697 template <typename Derived>
3698 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
3699   TRY_TO(VisitOMPClauseList(C));
3700   return true;
3701 }
3702 
3703 template <typename Derived>
3704 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
3705   TRY_TO(VisitOMPClauseWithPreInit(C));
3706   TRY_TO(TraverseStmt(C->getDevice()));
3707   return true;
3708 }
3709 
3710 template <typename Derived>
3711 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
3712   TRY_TO(VisitOMPClauseList(C));
3713   return true;
3714 }
3715 
3716 template <typename Derived>
3717 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
3718     OMPNumTeamsClause *C) {
3719   TRY_TO(VisitOMPClauseWithPreInit(C));
3720   TRY_TO(TraverseStmt(C->getNumTeams()));
3721   return true;
3722 }
3723 
3724 template <typename Derived>
3725 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
3726     OMPThreadLimitClause *C) {
3727   TRY_TO(VisitOMPClauseWithPreInit(C));
3728   TRY_TO(TraverseStmt(C->getThreadLimit()));
3729   return true;
3730 }
3731 
3732 template <typename Derived>
3733 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
3734     OMPPriorityClause *C) {
3735   TRY_TO(VisitOMPClauseWithPreInit(C));
3736   TRY_TO(TraverseStmt(C->getPriority()));
3737   return true;
3738 }
3739 
3740 template <typename Derived>
3741 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
3742     OMPGrainsizeClause *C) {
3743   TRY_TO(VisitOMPClauseWithPreInit(C));
3744   TRY_TO(TraverseStmt(C->getGrainsize()));
3745   return true;
3746 }
3747 
3748 template <typename Derived>
3749 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
3750     OMPNumTasksClause *C) {
3751   TRY_TO(VisitOMPClauseWithPreInit(C));
3752   TRY_TO(TraverseStmt(C->getNumTasks()));
3753   return true;
3754 }
3755 
3756 template <typename Derived>
3757 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
3758   TRY_TO(TraverseStmt(C->getHint()));
3759   return true;
3760 }
3761 
3762 template <typename Derived>
3763 bool RecursiveASTVisitor<Derived>::VisitOMPDistScheduleClause(
3764     OMPDistScheduleClause *C) {
3765   TRY_TO(VisitOMPClauseWithPreInit(C));
3766   TRY_TO(TraverseStmt(C->getChunkSize()));
3767   return true;
3768 }
3769 
3770 template <typename Derived>
3771 bool
3772 RecursiveASTVisitor<Derived>::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
3773   return true;
3774 }
3775 
3776 template <typename Derived>
3777 bool RecursiveASTVisitor<Derived>::VisitOMPToClause(OMPToClause *C) {
3778   TRY_TO(VisitOMPClauseList(C));
3779   return true;
3780 }
3781 
3782 template <typename Derived>
3783 bool RecursiveASTVisitor<Derived>::VisitOMPFromClause(OMPFromClause *C) {
3784   TRY_TO(VisitOMPClauseList(C));
3785   return true;
3786 }
3787 
3788 template <typename Derived>
3789 bool RecursiveASTVisitor<Derived>::VisitOMPUseDevicePtrClause(
3790     OMPUseDevicePtrClause *C) {
3791   TRY_TO(VisitOMPClauseList(C));
3792   return true;
3793 }
3794 
3795 template <typename Derived>
3796 bool RecursiveASTVisitor<Derived>::VisitOMPUseDeviceAddrClause(
3797     OMPUseDeviceAddrClause *C) {
3798   TRY_TO(VisitOMPClauseList(C));
3799   return true;
3800 }
3801 
3802 template <typename Derived>
3803 bool RecursiveASTVisitor<Derived>::VisitOMPIsDevicePtrClause(
3804     OMPIsDevicePtrClause *C) {
3805   TRY_TO(VisitOMPClauseList(C));
3806   return true;
3807 }
3808 
3809 template <typename Derived>
3810 bool RecursiveASTVisitor<Derived>::VisitOMPHasDeviceAddrClause(
3811     OMPHasDeviceAddrClause *C) {
3812   TRY_TO(VisitOMPClauseList(C));
3813   return true;
3814 }
3815 
3816 template <typename Derived>
3817 bool RecursiveASTVisitor<Derived>::VisitOMPNontemporalClause(
3818     OMPNontemporalClause *C) {
3819   TRY_TO(VisitOMPClauseList(C));
3820   for (auto *E : C->private_refs()) {
3821     TRY_TO(TraverseStmt(E));
3822   }
3823   return true;
3824 }
3825 
3826 template <typename Derived>
3827 bool RecursiveASTVisitor<Derived>::VisitOMPOrderClause(OMPOrderClause *) {
3828   return true;
3829 }
3830 
3831 template <typename Derived>
3832 bool RecursiveASTVisitor<Derived>::VisitOMPDetachClause(OMPDetachClause *C) {
3833   TRY_TO(TraverseStmt(C->getEventHandler()));
3834   return true;
3835 }
3836 
3837 template <typename Derived>
3838 bool RecursiveASTVisitor<Derived>::VisitOMPUsesAllocatorsClause(
3839     OMPUsesAllocatorsClause *C) {
3840   for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
3841     const OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
3842     TRY_TO(TraverseStmt(Data.Allocator));
3843     TRY_TO(TraverseStmt(Data.AllocatorTraits));
3844   }
3845   return true;
3846 }
3847 
3848 template <typename Derived>
3849 bool RecursiveASTVisitor<Derived>::VisitOMPAffinityClause(
3850     OMPAffinityClause *C) {
3851   TRY_TO(TraverseStmt(C->getModifier()));
3852   for (Expr *E : C->varlists())
3853     TRY_TO(TraverseStmt(E));
3854   return true;
3855 }
3856 
3857 template <typename Derived>
3858 bool RecursiveASTVisitor<Derived>::VisitOMPFilterClause(OMPFilterClause *C) {
3859   TRY_TO(VisitOMPClauseWithPreInit(C));
3860   TRY_TO(TraverseStmt(C->getThreadID()));
3861   return true;
3862 }
3863 
3864 template <typename Derived>
3865 bool RecursiveASTVisitor<Derived>::VisitOMPBindClause(OMPBindClause *C) {
3866   return true;
3867 }
3868 
3869 template <typename Derived>
3870 bool RecursiveASTVisitor<Derived>::VisitOMPXDynCGroupMemClause(
3871     OMPXDynCGroupMemClause *C) {
3872   TRY_TO(VisitOMPClauseWithPreInit(C));
3873   TRY_TO(TraverseStmt(C->getSize()));
3874   return true;
3875 }
3876 
3877 template <typename Derived>
3878 bool RecursiveASTVisitor<Derived>::VisitOMPDoacrossClause(
3879     OMPDoacrossClause *C) {
3880   TRY_TO(VisitOMPClauseList(C));
3881   return true;
3882 }
3883 
3884 template <typename Derived>
3885 bool RecursiveASTVisitor<Derived>::VisitOMPXAttributeClause(
3886     OMPXAttributeClause *C) {
3887   return true;
3888 }
3889 
3890 template <typename Derived>
3891 bool RecursiveASTVisitor<Derived>::VisitOMPXBareClause(OMPXBareClause *C) {
3892   return true;
3893 }
3894 
3895 // FIXME: look at the following tricky-seeming exprs to see if we
3896 // need to recurse on anything.  These are ones that have methods
3897 // returning decls or qualtypes or nestednamespecifier -- though I'm
3898 // not sure if they own them -- or just seemed very complicated, or
3899 // had lots of sub-types to explore.
3900 //
3901 // VisitOverloadExpr and its children: recurse on template args? etc?
3902 
3903 // FIXME: go through all the stmts and exprs again, and see which of them
3904 // create new types, and recurse on the types (TypeLocs?) of those.
3905 // Candidates:
3906 //
3907 //    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
3908 //    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
3909 //    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
3910 //    Every class that has getQualifier.
3911 
3912 #undef DEF_TRAVERSE_STMT
3913 #undef TRAVERSE_STMT
3914 #undef TRAVERSE_STMT_BASE
3915 
3916 #undef TRY_TO
3917 
3918 } // end namespace clang
3919 
3920 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
3921