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