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