]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/RecursiveASTVisitor.h
Merged ^/head r283871 through r284187.
[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 })
1012
1013 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1014                   { TRY_TO(TraverseType(T->getPointeeType())); })
1015
1016 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1017
1018 #undef DEF_TRAVERSE_TYPE
1019
1020 // ----------------- TypeLoc traversal -----------------
1021
1022 // This macro makes available a variable TL, the passed-in TypeLoc.
1023 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1024 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1025 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1026 // continue to work.
1027 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE)                                       \
1028   template <typename Derived>                                                  \
1029   bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) {       \
1030     if (getDerived().shouldWalkTypesOfTypeLocs())                              \
1031       TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr())));           \
1032     TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                         \
1033     { CODE; }                                                                  \
1034     return true;                                                               \
1035   }
1036
1037 template <typename Derived>
1038 bool
1039 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1040   // Move this over to the 'main' typeloc tree.  Note that this is a
1041   // move -- we pretend that we were really looking at the unqualified
1042   // typeloc all along -- rather than a recursion, so we don't follow
1043   // the normal CRTP plan of going through
1044   // getDerived().TraverseTypeLoc.  If we did, we'd be traversing
1045   // twice for the same type (once as a QualifiedTypeLoc version of
1046   // the type, once as an UnqualifiedTypeLoc version of the type),
1047   // which in effect means we'd call VisitTypeLoc twice with the
1048   // 'same' type.  This solves that problem, at the cost of never
1049   // seeing the qualified version of the type (unless the client
1050   // subclasses TraverseQualifiedTypeLoc themselves).  It's not a
1051   // perfect solution.  A perfect solution probably requires making
1052   // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1053   // wrapper around Type* -- rather than being its own class in the
1054   // type hierarchy.
1055   return TraverseTypeLoc(TL.getUnqualifiedLoc());
1056 }
1057
1058 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1059
1060 // FIXME: ComplexTypeLoc is unfinished
1061 DEF_TRAVERSE_TYPELOC(ComplexType, {
1062   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1063 })
1064
1065 DEF_TRAVERSE_TYPELOC(PointerType,
1066                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1067
1068 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1069                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1070
1071 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1072                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1073
1074 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1075                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1076
1077 // FIXME: location of base class?
1078 // We traverse this in the type case as well, but how is it not reached through
1079 // the pointee type?
1080 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1081   TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1082   TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1083 })
1084
1085 DEF_TRAVERSE_TYPELOC(AdjustedType,
1086                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1087
1088 DEF_TRAVERSE_TYPELOC(DecayedType,
1089                      { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1090
1091 template <typename Derived>
1092 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1093   // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1094   TRY_TO(TraverseStmt(TL.getSizeExpr()));
1095   return true;
1096 }
1097
1098 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1099   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1100   return TraverseArrayTypeLocHelper(TL);
1101 })
1102
1103 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1104   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1105   return TraverseArrayTypeLocHelper(TL);
1106 })
1107
1108 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1109   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1110   return TraverseArrayTypeLocHelper(TL);
1111 })
1112
1113 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1114   TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1115   return TraverseArrayTypeLocHelper(TL);
1116 })
1117
1118 // FIXME: order? why not size expr first?
1119 // FIXME: base VectorTypeLoc is unfinished
1120 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1121   if (TL.getTypePtr()->getSizeExpr())
1122     TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1123   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1124 })
1125
1126 // FIXME: VectorTypeLoc is unfinished
1127 DEF_TRAVERSE_TYPELOC(VectorType, {
1128   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1129 })
1130
1131 // FIXME: size and attributes
1132 // FIXME: base VectorTypeLoc is unfinished
1133 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1134   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1135 })
1136
1137 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1138                      { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1139
1140 // FIXME: location of exception specifications (attributes?)
1141 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1142   TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1143
1144   const FunctionProtoType *T = TL.getTypePtr();
1145
1146   for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1147     if (TL.getParam(I)) {
1148       TRY_TO(TraverseDecl(TL.getParam(I)));
1149     } else if (I < T->getNumParams()) {
1150       TRY_TO(TraverseType(T->getParamType(I)));
1151     }
1152   }
1153
1154   for (const auto &E : T->exceptions()) {
1155     TRY_TO(TraverseType(E));
1156   }
1157
1158   if (Expr *NE = T->getNoexceptExpr())
1159     TRY_TO(TraverseStmt(NE));
1160 })
1161
1162 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1163 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1164
1165 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1166                      { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1167
1168 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1169   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1170 })
1171
1172 // FIXME: location of underlying expr
1173 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1174   TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1175 })
1176
1177 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1178   TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1179 })
1180
1181 DEF_TRAVERSE_TYPELOC(AutoType, {
1182   TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1183 })
1184
1185 DEF_TRAVERSE_TYPELOC(RecordType, {})
1186 DEF_TRAVERSE_TYPELOC(EnumType, {})
1187 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1188 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {})
1189 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {})
1190
1191 // FIXME: use the loc for the template name?
1192 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1193   TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1194   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1195     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1196   }
1197 })
1198
1199 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1200
1201 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1202
1203 DEF_TRAVERSE_TYPELOC(AttributedType,
1204                      { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1205
1206 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1207   if (TL.getQualifierLoc()) {
1208     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1209   }
1210   TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1211 })
1212
1213 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1214   TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1215 })
1216
1217 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1218   if (TL.getQualifierLoc()) {
1219     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1220   }
1221
1222   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1223     TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1224   }
1225 })
1226
1227 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1228                      { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1229
1230 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1231
1232 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1233   // We have to watch out here because an ObjCInterfaceType's base
1234   // type is itself.
1235   if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1236     TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1237 })
1238
1239 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1240                      { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1241
1242 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1243
1244 #undef DEF_TRAVERSE_TYPELOC
1245
1246 // ----------------- Decl traversal -----------------
1247 //
1248 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1249 // the children that come from the DeclContext associated with it.
1250 // Therefore each Traverse* only needs to worry about children other
1251 // than those.
1252
1253 template <typename Derived>
1254 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1255   if (!DC)
1256     return true;
1257
1258   for (auto *Child : DC->decls()) {
1259     // BlockDecls and CapturedDecls are traversed through BlockExprs and
1260     // CapturedStmts respectively.
1261     if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child))
1262       TRY_TO(TraverseDecl(Child));
1263   }
1264
1265   return true;
1266 }
1267
1268 // This macro makes available a variable D, the passed-in decl.
1269 #define DEF_TRAVERSE_DECL(DECL, CODE)                                          \
1270   template <typename Derived>                                                  \
1271   bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) {                 \
1272     TRY_TO(WalkUpFrom##DECL(D));                                               \
1273     { CODE; }                                                                  \
1274     TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));               \
1275     return true;                                                               \
1276   }
1277
1278 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1279
1280 DEF_TRAVERSE_DECL(BlockDecl, {
1281   if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1282     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1283   TRY_TO(TraverseStmt(D->getBody()));
1284   for (const auto &I : D->captures()) {
1285     if (I.hasCopyExpr()) {
1286       TRY_TO(TraverseStmt(I.getCopyExpr()));
1287     }
1288   }
1289   // This return statement makes sure the traversal of nodes in
1290   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1291   // is skipped - don't remove it.
1292   return true;
1293 })
1294
1295 DEF_TRAVERSE_DECL(CapturedDecl, {
1296   TRY_TO(TraverseStmt(D->getBody()));
1297   // This return statement makes sure the traversal of nodes in
1298   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1299   // is skipped - don't remove it.
1300   return true;
1301 })
1302
1303 DEF_TRAVERSE_DECL(EmptyDecl, {})
1304
1305 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1306                   { TRY_TO(TraverseStmt(D->getAsmString())); })
1307
1308 DEF_TRAVERSE_DECL(ImportDecl, {})
1309
1310 DEF_TRAVERSE_DECL(FriendDecl, {
1311   // Friend is either decl or a type.
1312   if (D->getFriendType())
1313     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1314   else
1315     TRY_TO(TraverseDecl(D->getFriendDecl()));
1316 })
1317
1318 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1319   if (D->getFriendType())
1320     TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1321   else
1322     TRY_TO(TraverseDecl(D->getFriendDecl()));
1323   for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1324     TemplateParameterList *TPL = D->getTemplateParameterList(I);
1325     for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1326          ITPL != ETPL; ++ITPL) {
1327       TRY_TO(TraverseDecl(*ITPL));
1328     }
1329   }
1330 })
1331
1332 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1333   TRY_TO(TraverseDecl(D->getSpecialization()));
1334
1335   if (D->hasExplicitTemplateArgs()) {
1336     const TemplateArgumentListInfo &args = D->templateArgs();
1337     TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1338                                               args.size()));
1339   }
1340 })
1341
1342 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1343
1344 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1345                                         })
1346
1347 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1348   TRY_TO(TraverseStmt(D->getAssertExpr()));
1349   TRY_TO(TraverseStmt(D->getMessage()));
1350 })
1351
1352 DEF_TRAVERSE_DECL(
1353     TranslationUnitDecl,
1354     {// Code in an unnamed namespace shows up automatically in
1355      // decls_begin()/decls_end().  Thus we don't need to recurse on
1356      // D->getAnonymousNamespace().
1357     })
1358
1359 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1360
1361 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1362   // We shouldn't traverse an aliased namespace, since it will be
1363   // defined (and, therefore, traversed) somewhere else.
1364   //
1365   // This return statement makes sure the traversal of nodes in
1366   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1367   // is skipped - don't remove it.
1368   return true;
1369 })
1370
1371 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1372                              })
1373
1374 DEF_TRAVERSE_DECL(
1375     NamespaceDecl,
1376     {// Code in an unnamed namespace shows up automatically in
1377      // decls_begin()/decls_end().  Thus we don't need to recurse on
1378      // D->getAnonymousNamespace().
1379     })
1380
1381 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1382                                            })
1383
1384 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1385                                     })
1386
1387 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1388                                         })
1389
1390 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1391                                           })
1392
1393 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1394                                      })
1395
1396 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1397                                     })
1398
1399 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1400   if (D->getReturnTypeSourceInfo()) {
1401     TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1402   }
1403   for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1404        I != E; ++I) {
1405     TRY_TO(TraverseDecl(*I));
1406   }
1407   if (D->isThisDeclarationADefinition()) {
1408     TRY_TO(TraverseStmt(D->getBody()));
1409   }
1410   return true;
1411 })
1412
1413 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1414   if (D->getTypeSourceInfo())
1415     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1416   else
1417     TRY_TO(TraverseType(D->getType()));
1418   return true;
1419 })
1420
1421 DEF_TRAVERSE_DECL(UsingDecl, {
1422   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1423   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1424 })
1425
1426 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1427   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1428 })
1429
1430 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1431
1432 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1433   for (auto *I : D->varlists()) {
1434     TRY_TO(TraverseStmt(I));
1435   }
1436 })
1437
1438 // A helper method for TemplateDecl's children.
1439 template <typename Derived>
1440 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1441     TemplateParameterList *TPL) {
1442   if (TPL) {
1443     for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1444          I != E; ++I) {
1445       TRY_TO(TraverseDecl(*I));
1446     }
1447   }
1448   return true;
1449 }
1450
1451 template <typename Derived>
1452 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1453     ClassTemplateDecl *D) {
1454   for (auto *SD : D->specializations()) {
1455     for (auto *RD : SD->redecls()) {
1456       // We don't want to visit injected-class-names in this traversal.
1457       if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1458         continue;
1459
1460       switch (
1461           cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1462       // Visit the implicit instantiations with the requested pattern.
1463       case TSK_Undeclared:
1464       case TSK_ImplicitInstantiation:
1465         TRY_TO(TraverseDecl(RD));
1466         break;
1467
1468       // We don't need to do anything on an explicit instantiation
1469       // or explicit specialization because there will be an explicit
1470       // node for it elsewhere.
1471       case TSK_ExplicitInstantiationDeclaration:
1472       case TSK_ExplicitInstantiationDefinition:
1473       case TSK_ExplicitSpecialization:
1474         break;
1475       }
1476     }
1477   }
1478
1479   return true;
1480 }
1481
1482 template <typename Derived>
1483 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1484     VarTemplateDecl *D) {
1485   for (auto *SD : D->specializations()) {
1486     for (auto *RD : SD->redecls()) {
1487       switch (
1488           cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1489       case TSK_Undeclared:
1490       case TSK_ImplicitInstantiation:
1491         TRY_TO(TraverseDecl(RD));
1492         break;
1493
1494       case TSK_ExplicitInstantiationDeclaration:
1495       case TSK_ExplicitInstantiationDefinition:
1496       case TSK_ExplicitSpecialization:
1497         break;
1498       }
1499     }
1500   }
1501
1502   return true;
1503 }
1504
1505 // A helper method for traversing the instantiations of a
1506 // function while skipping its specializations.
1507 template <typename Derived>
1508 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1509     FunctionTemplateDecl *D) {
1510   for (auto *FD : D->specializations()) {
1511     for (auto *RD : FD->redecls()) {
1512       switch (RD->getTemplateSpecializationKind()) {
1513       case TSK_Undeclared:
1514       case TSK_ImplicitInstantiation:
1515         // We don't know what kind of FunctionDecl this is.
1516         TRY_TO(TraverseDecl(RD));
1517         break;
1518
1519       // FIXME: For now traverse explicit instantiations here. Change that
1520       // once they are represented as dedicated nodes in the AST.
1521       case TSK_ExplicitInstantiationDeclaration:
1522       case TSK_ExplicitInstantiationDefinition:
1523         TRY_TO(TraverseDecl(RD));
1524         break;
1525
1526       case TSK_ExplicitSpecialization:
1527         break;
1528       }
1529     }
1530   }
1531
1532   return true;
1533 }
1534
1535 // This macro unifies the traversal of class, variable and function
1536 // template declarations.
1537 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND)                                   \
1538   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, {                              \
1539     TRY_TO(TraverseDecl(D->getTemplatedDecl()));                               \
1540     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
1541                                                                                \
1542     /* By default, we do not traverse the instantiations of                    \
1543        class templates since they do not appear in the user code. The          \
1544        following code optionally traverses them.                               \
1545                                                                                \
1546        We only traverse the class instantiations when we see the canonical     \
1547        declaration of the template, to ensure we only visit them once. */      \
1548     if (getDerived().shouldVisitTemplateInstantiations() &&                    \
1549         D == D->getCanonicalDecl())                                            \
1550       TRY_TO(TraverseTemplateInstantiations(D));                               \
1551                                                                                \
1552     /* Note that getInstantiatedFromMemberTemplate() is just a link            \
1553        from a template instantiation back to the template from which           \
1554        it was instantiated, and thus should not be traversed. */               \
1555   })
1556
1557 DEF_TRAVERSE_TMPL_DECL(Class)
1558 DEF_TRAVERSE_TMPL_DECL(Var)
1559 DEF_TRAVERSE_TMPL_DECL(Function)
1560
1561 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1562   // D is the "T" in something like
1563   //   template <template <typename> class T> class container { };
1564   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1565   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1566     TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1567   }
1568   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1569 })
1570
1571 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1572   // D is the "T" in something like "template<typename T> class vector;"
1573   if (D->getTypeForDecl())
1574     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1575   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1576     TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1577 })
1578
1579 DEF_TRAVERSE_DECL(TypedefDecl, {
1580   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1581   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1582   // declaring the typedef, not something that was written in the
1583   // source.
1584 })
1585
1586 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1587   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1588   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1589   // declaring the type alias, not something that was written in the
1590   // source.
1591 })
1592
1593 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1594   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1595   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1596 })
1597
1598 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1599   // A dependent using declaration which was marked with 'typename'.
1600   //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1601   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1602   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1603   // declaring the type, not something that was written in the
1604   // source.
1605 })
1606
1607 DEF_TRAVERSE_DECL(EnumDecl, {
1608   if (D->getTypeForDecl())
1609     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1610
1611   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1612   // The enumerators are already traversed by
1613   // decls_begin()/decls_end().
1614 })
1615
1616 // Helper methods for RecordDecl and its children.
1617 template <typename Derived>
1618 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1619   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1620   // declaring the type, not something that was written in the source.
1621
1622   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1623   return true;
1624 }
1625
1626 template <typename Derived>
1627 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1628   if (!TraverseRecordHelper(D))
1629     return false;
1630   if (D->isCompleteDefinition()) {
1631     for (const auto &I : D->bases()) {
1632       TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1633     }
1634     // We don't traverse the friends or the conversions, as they are
1635     // already in decls_begin()/decls_end().
1636   }
1637   return true;
1638 }
1639
1640 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1641
1642 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1643
1644 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND)                              \
1645   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
1646     /* For implicit instantiations ("set<int> x;"), we don't want to           \
1647        recurse at all, since the instatiated template isn't written in         \
1648        the source code anywhere.  (Note the instatiated *type* --              \
1649        set<int> -- is written, and will still get a callback of                \
1650        TemplateSpecializationType).  For explicit instantiations               \
1651        ("template set<int>;"), we do need a callback, since this               \
1652        is the only callback that's made for this instantiation.                \
1653        We use getTypeAsWritten() to distinguish. */                            \
1654     if (TypeSourceInfo *TSI = D->getTypeAsWritten())                           \
1655       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
1656                                                                                \
1657     if (!getDerived().shouldVisitTemplateInstantiations() &&                   \
1658         D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)      \
1659       /* Returning from here skips traversing the                              \
1660          declaration context of the *TemplateSpecializationDecl                \
1661          (embedded in the DEF_TRAVERSE_DECL() macro)                           \
1662          which contains the instantiated members of the template. */           \
1663       return true;                                                             \
1664   })
1665
1666 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1667 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1668
1669 template <typename Derived>
1670 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1671     const TemplateArgumentLoc *TAL, unsigned Count) {
1672   for (unsigned I = 0; I < Count; ++I) {
1673     TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1674   }
1675   return true;
1676 }
1677
1678 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
1679   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
1680     /* The partial specialization. */                                          \
1681     if (TemplateParameterList *TPL = D->getTemplateParameters()) {             \
1682       for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();   \
1683            I != E; ++I) {                                                      \
1684         TRY_TO(TraverseDecl(*I));                                              \
1685       }                                                                        \
1686     }                                                                          \
1687     /* The args that remains unspecialized. */                                 \
1688     TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
1689         D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
1690         D->getTemplateArgsAsWritten()->NumTemplateArgs));                      \
1691                                                                                \
1692     /* Don't need the *TemplatePartialSpecializationHelper, even               \
1693        though that's our parent class -- we already visit all the              \
1694        template args here. */                                                  \
1695     TRY_TO(Traverse##DECLKIND##Helper(D));                                     \
1696                                                                                \
1697     /* Instantiations will have been visited with the primary template. */     \
1698   })
1699
1700 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1701 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1702
1703 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1704
1705 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1706   // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1707   //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
1708   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1709   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1710 })
1711
1712 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1713
1714 template <typename Derived>
1715 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1716   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1717   if (D->getTypeSourceInfo())
1718     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1719   else
1720     TRY_TO(TraverseType(D->getType()));
1721   return true;
1722 }
1723
1724 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1725
1726 DEF_TRAVERSE_DECL(FieldDecl, {
1727   TRY_TO(TraverseDeclaratorHelper(D));
1728   if (D->isBitField())
1729     TRY_TO(TraverseStmt(D->getBitWidth()));
1730   else if (D->hasInClassInitializer())
1731     TRY_TO(TraverseStmt(D->getInClassInitializer()));
1732 })
1733
1734 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1735   TRY_TO(TraverseDeclaratorHelper(D));
1736   if (D->isBitField())
1737     TRY_TO(TraverseStmt(D->getBitWidth()));
1738   // FIXME: implement the rest.
1739 })
1740
1741 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1742   TRY_TO(TraverseDeclaratorHelper(D));
1743   if (D->isBitField())
1744     TRY_TO(TraverseStmt(D->getBitWidth()));
1745   // FIXME: implement the rest.
1746 })
1747
1748 template <typename Derived>
1749 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1750   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1751   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1752
1753   // If we're an explicit template specialization, iterate over the
1754   // template args that were explicitly specified.  If we were doing
1755   // this in typing order, we'd do it between the return type and
1756   // the function args, but both are handled by the FunctionTypeLoc
1757   // above, so we have to choose one side.  I've decided to do before.
1758   if (const FunctionTemplateSpecializationInfo *FTSI =
1759           D->getTemplateSpecializationInfo()) {
1760     if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1761         FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1762       // A specialization might not have explicit template arguments if it has
1763       // a templated return type and concrete arguments.
1764       if (const ASTTemplateArgumentListInfo *TALI =
1765               FTSI->TemplateArgumentsAsWritten) {
1766         TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1767                                                   TALI->NumTemplateArgs));
1768       }
1769     }
1770   }
1771
1772   // Visit the function type itself, which can be either
1773   // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
1774   // also covers the return type and the function parameters,
1775   // including exception specifications.
1776   if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1777     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1778   } else if (getDerived().shouldVisitImplicitCode()) {
1779     // Visit parameter variable declarations of the implicit function
1780     // if the traverser is visiting implicit code. Parameter variable
1781     // declarations do not have valid TypeSourceInfo, so to visit them
1782     // we need to traverse the declarations explicitly.
1783     for (FunctionDecl::param_const_iterator I = D->param_begin(),
1784                                             E = D->param_end();
1785          I != E; ++I)
1786       TRY_TO(TraverseDecl(*I));
1787   }
1788
1789   if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1790     // Constructor initializers.
1791     for (auto *I : Ctor->inits()) {
1792       TRY_TO(TraverseConstructorInitializer(I));
1793     }
1794   }
1795
1796   if (D->isThisDeclarationADefinition()) {
1797     TRY_TO(TraverseStmt(D->getBody())); // Function body.
1798   }
1799   return true;
1800 }
1801
1802 DEF_TRAVERSE_DECL(FunctionDecl, {
1803   // We skip decls_begin/decls_end, which are already covered by
1804   // TraverseFunctionHelper().
1805   return TraverseFunctionHelper(D);
1806 })
1807
1808 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1809   // We skip decls_begin/decls_end, which are already covered by
1810   // TraverseFunctionHelper().
1811   return TraverseFunctionHelper(D);
1812 })
1813
1814 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1815   // We skip decls_begin/decls_end, which are already covered by
1816   // TraverseFunctionHelper().
1817   return TraverseFunctionHelper(D);
1818 })
1819
1820 // CXXConversionDecl is the declaration of a type conversion operator.
1821 // It's not a cast expression.
1822 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1823   // We skip decls_begin/decls_end, which are already covered by
1824   // TraverseFunctionHelper().
1825   return TraverseFunctionHelper(D);
1826 })
1827
1828 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1829   // We skip decls_begin/decls_end, which are already covered by
1830   // TraverseFunctionHelper().
1831   return TraverseFunctionHelper(D);
1832 })
1833
1834 template <typename Derived>
1835 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1836   TRY_TO(TraverseDeclaratorHelper(D));
1837   // Default params are taken care of when we traverse the ParmVarDecl.
1838   if (!isa<ParmVarDecl>(D) &&
1839       (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1840     TRY_TO(TraverseStmt(D->getInit()));
1841   return true;
1842 }
1843
1844 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1845
1846 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1847
1848 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1849   // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1850   TRY_TO(TraverseDeclaratorHelper(D));
1851   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1852     TRY_TO(TraverseStmt(D->getDefaultArgument()));
1853 })
1854
1855 DEF_TRAVERSE_DECL(ParmVarDecl, {
1856   TRY_TO(TraverseVarHelper(D));
1857
1858   if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1859       !D->hasUnparsedDefaultArg())
1860     TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1861
1862   if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1863       !D->hasUnparsedDefaultArg())
1864     TRY_TO(TraverseStmt(D->getDefaultArg()));
1865 })
1866
1867 #undef DEF_TRAVERSE_DECL
1868
1869 // ----------------- Stmt traversal -----------------
1870 //
1871 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1872 // over the children defined in children() (every stmt defines these,
1873 // though sometimes the range is empty).  Each individual Traverse*
1874 // method only needs to worry about children other than those.  To see
1875 // what children() does for a given class, see, e.g.,
1876 //   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1877
1878 // This macro makes available a variable S, the passed-in stmt.
1879 #define DEF_TRAVERSE_STMT(STMT, CODE)                                          \
1880   template <typename Derived>                                                  \
1881   bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) {                 \
1882     TRY_TO(WalkUpFrom##STMT(S));                                               \
1883     { CODE; }                                                                  \
1884     for (Stmt::child_range range = S->children(); range; ++range) {            \
1885       TRY_TO(TraverseStmt(*range));                                            \
1886     }                                                                          \
1887     return true;                                                               \
1888   }
1889
1890 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1891   TRY_TO(TraverseStmt(S->getAsmString()));
1892   for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1893     TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1894   }
1895   for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1896     TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1897   }
1898   for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1899     TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1900   }
1901   // children() iterates over inputExpr and outputExpr.
1902 })
1903
1904 DEF_TRAVERSE_STMT(
1905     MSAsmStmt,
1906     {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
1907      // added this needs to be implemented.
1908     })
1909
1910 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1911   TRY_TO(TraverseDecl(S->getExceptionDecl()));
1912   // children() iterates over the handler block.
1913 })
1914
1915 DEF_TRAVERSE_STMT(DeclStmt, {
1916   for (auto *I : S->decls()) {
1917     TRY_TO(TraverseDecl(I));
1918   }
1919   // Suppress the default iteration over children() by
1920   // returning.  Here's why: A DeclStmt looks like 'type var [=
1921   // initializer]'.  The decls above already traverse over the
1922   // initializers, so we don't have to do it again (which
1923   // children() would do).
1924   return true;
1925 })
1926
1927 // These non-expr stmts (most of them), do not need any action except
1928 // iterating over the children.
1929 DEF_TRAVERSE_STMT(BreakStmt, {})
1930 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1931 DEF_TRAVERSE_STMT(CaseStmt, {})
1932 DEF_TRAVERSE_STMT(CompoundStmt, {})
1933 DEF_TRAVERSE_STMT(ContinueStmt, {})
1934 DEF_TRAVERSE_STMT(DefaultStmt, {})
1935 DEF_TRAVERSE_STMT(DoStmt, {})
1936 DEF_TRAVERSE_STMT(ForStmt, {})
1937 DEF_TRAVERSE_STMT(GotoStmt, {})
1938 DEF_TRAVERSE_STMT(IfStmt, {})
1939 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1940 DEF_TRAVERSE_STMT(LabelStmt, {})
1941 DEF_TRAVERSE_STMT(AttributedStmt, {})
1942 DEF_TRAVERSE_STMT(NullStmt, {})
1943 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1944 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1945 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1946 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1947 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1948 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1949 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1950 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1951   if (!getDerived().shouldVisitImplicitCode()) {
1952     TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1953     TRY_TO(TraverseStmt(S->getRangeInit()));
1954     TRY_TO(TraverseStmt(S->getBody()));
1955     // Visit everything else only if shouldVisitImplicitCode().
1956     return true;
1957   }
1958 })
1959 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1960   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1961   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1962 })
1963 DEF_TRAVERSE_STMT(ReturnStmt, {})
1964 DEF_TRAVERSE_STMT(SwitchStmt, {})
1965 DEF_TRAVERSE_STMT(WhileStmt, {})
1966
1967 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1968   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1969   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1970   if (S->hasExplicitTemplateArgs()) {
1971     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1972                                               S->getNumTemplateArgs()));
1973   }
1974 })
1975
1976 DEF_TRAVERSE_STMT(DeclRefExpr, {
1977   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1978   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1979   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1980                                             S->getNumTemplateArgs()));
1981 })
1982
1983 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1984   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1985   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1986   if (S->hasExplicitTemplateArgs()) {
1987     TRY_TO(TraverseTemplateArgumentLocsHelper(
1988         S->getExplicitTemplateArgs().getTemplateArgs(),
1989         S->getNumTemplateArgs()));
1990   }
1991 })
1992
1993 DEF_TRAVERSE_STMT(MemberExpr, {
1994   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1995   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1996   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1997                                             S->getNumTemplateArgs()));
1998 })
1999
2000 DEF_TRAVERSE_STMT(
2001     ImplicitCastExpr,
2002     {// We don't traverse the cast type, as it's not written in the
2003      // source code.
2004     })
2005
2006 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2007   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2008 })
2009
2010 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2011   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2012 })
2013
2014 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2015   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2016 })
2017
2018 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2019   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2020 })
2021
2022 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2023   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2024 })
2025
2026 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2027   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2028 })
2029
2030 // InitListExpr is a tricky one, because we want to do all our work on
2031 // the syntactic form of the listexpr, but this method takes the
2032 // semantic form by default.  We can't use the macro helper because it
2033 // calls WalkUp*() on the semantic form, before our code can convert
2034 // to the syntactic form.
2035 template <typename Derived>
2036 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
2037   InitListExpr *Syn = S->isSemanticForm() ? S->getSyntacticForm() : S;
2038   if (Syn) {
2039     TRY_TO(WalkUpFromInitListExpr(Syn));
2040     // All we need are the default actions.  FIXME: use a helper function.
2041     for (Stmt::child_range range = Syn->children(); range; ++range) {
2042       TRY_TO(TraverseStmt(*range));
2043     }
2044   }
2045   InitListExpr *Sem = S->isSemanticForm() ? S : S->getSemanticForm();
2046   if (Sem) {
2047     TRY_TO(WalkUpFromInitListExpr(Sem));
2048     for (Stmt::child_range range = Sem->children(); range; ++range) {
2049       TRY_TO(TraverseStmt(*range));
2050     }
2051   }
2052   return true;
2053 }
2054
2055 // GenericSelectionExpr is a special case because the types and expressions
2056 // are interleaved.  We also need to watch out for null types (default
2057 // generic associations).
2058 template <typename Derived>
2059 bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr(
2060     GenericSelectionExpr *S) {
2061   TRY_TO(WalkUpFromGenericSelectionExpr(S));
2062   TRY_TO(TraverseStmt(S->getControllingExpr()));
2063   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2064     if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2065       TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2066     TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2067   }
2068   return true;
2069 }
2070
2071 // PseudoObjectExpr is a special case because of the wierdness with
2072 // syntactic expressions and opaque values.
2073 template <typename Derived>
2074 bool
2075 RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2076   TRY_TO(WalkUpFromPseudoObjectExpr(S));
2077   TRY_TO(TraverseStmt(S->getSyntacticForm()));
2078   for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2079                                             e = S->semantics_end();
2080        i != e; ++i) {
2081     Expr *sub = *i;
2082     if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2083       sub = OVE->getSourceExpr();
2084     TRY_TO(TraverseStmt(sub));
2085   }
2086   return true;
2087 }
2088
2089 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2090   // This is called for code like 'return T()' where T is a built-in
2091   // (i.e. non-class) type.
2092   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2093 })
2094
2095 DEF_TRAVERSE_STMT(CXXNewExpr, {
2096   // The child-iterator will pick up the other arguments.
2097   TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2098 })
2099
2100 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2101   // The child-iterator will pick up the expression representing
2102   // the field.
2103   // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2104   // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2105   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2106 })
2107
2108 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2109   // The child-iterator will pick up the arg if it's an expression,
2110   // but not if it's a type.
2111   if (S->isArgumentType())
2112     TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2113 })
2114
2115 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2116   // The child-iterator will pick up the arg if it's an expression,
2117   // but not if it's a type.
2118   if (S->isTypeOperand())
2119     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2120 })
2121
2122 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2123   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2124 })
2125
2126 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2127   // The child-iterator will pick up the arg if it's an expression,
2128   // but not if it's a type.
2129   if (S->isTypeOperand())
2130     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2131 })
2132
2133 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2134   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2135     TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2136 })
2137
2138 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2139   TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2140 })
2141
2142 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2143                   { TRY_TO(TraverseStmt(S->getQueriedExpression())); })
2144
2145 DEF_TRAVERSE_STMT(VAArgExpr, {
2146   // The child-iterator will pick up the expression argument.
2147   TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2148 })
2149
2150 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2151   // This is called for code like 'return T()' where T is a class type.
2152   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2153 })
2154
2155 // Walk only the visible parts of lambda expressions.
2156 template <typename Derived>
2157 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2158   TRY_TO(WalkUpFromLambdaExpr(S));
2159
2160   for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2161                                     CEnd = S->explicit_capture_end();
2162        C != CEnd; ++C) {
2163     TRY_TO(TraverseLambdaCapture(S, C));
2164   }
2165
2166   TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2167   FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2168
2169   if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2170     // Visit the whole type.
2171     TRY_TO(TraverseTypeLoc(TL));
2172   } else {
2173     if (S->hasExplicitParameters()) {
2174       // Visit parameters.
2175       for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2176         TRY_TO(TraverseDecl(Proto.getParam(I)));
2177       }
2178     } else if (S->hasExplicitResultType()) {
2179       TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2180     }
2181
2182     auto *T = Proto.getTypePtr();
2183     for (const auto &E : T->exceptions()) {
2184       TRY_TO(TraverseType(E));
2185     }
2186
2187     if (Expr *NE = T->getNoexceptExpr())
2188       TRY_TO(TraverseStmt(NE));
2189   }
2190
2191   TRY_TO(TraverseLambdaBody(S));
2192   return true;
2193 }
2194
2195 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2196   // This is called for code like 'T()', where T is a template argument.
2197   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2198 })
2199
2200 // These expressions all might take explicit template arguments.
2201 // We traverse those if so.  FIXME: implement these.
2202 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2203 DEF_TRAVERSE_STMT(CallExpr, {})
2204 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2205
2206 // These exprs (most of them), do not need any action except iterating
2207 // over the children.
2208 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2209 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2210 DEF_TRAVERSE_STMT(BlockExpr, {
2211   TRY_TO(TraverseDecl(S->getBlockDecl()));
2212   return true; // no child statements to loop through.
2213 })
2214 DEF_TRAVERSE_STMT(ChooseExpr, {})
2215 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2216   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2217 })
2218 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2219 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2220 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2221 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2222 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2223 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2224 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2225 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2226 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2227   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2228   if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2229     TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2230   if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2231     TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2232 })
2233 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2234 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2235 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2236 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2237 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2238 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2239 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2240 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2241 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2242   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2243     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2244 })
2245 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2246 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2247 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2248   if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2249     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2250 })
2251 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2252 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2253 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2254 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2255 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2256 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2257   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2258 })
2259 DEF_TRAVERSE_STMT(ParenExpr, {})
2260 DEF_TRAVERSE_STMT(ParenListExpr, {})
2261 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2262 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2263 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2264 DEF_TRAVERSE_STMT(StmtExpr, {})
2265 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2266   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2267   if (S->hasExplicitTemplateArgs()) {
2268     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2269                                               S->getNumTemplateArgs()));
2270   }
2271 })
2272
2273 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2274   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2275   if (S->hasExplicitTemplateArgs()) {
2276     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2277                                               S->getNumTemplateArgs()));
2278   }
2279 })
2280
2281 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2282 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2283 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2284 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2285 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2286
2287 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2288 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2289 DEF_TRAVERSE_STMT(TypoExpr, {})
2290 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2291
2292 // These operators (all of them) do not need any action except
2293 // iterating over the children.
2294 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2295 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2296 DEF_TRAVERSE_STMT(UnaryOperator, {})
2297 DEF_TRAVERSE_STMT(BinaryOperator, {})
2298 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2299 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2300 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2301 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2302 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2303 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2304 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2305 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2306 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2307 DEF_TRAVERSE_STMT(AtomicExpr, {})
2308
2309 // These literals (all of them) do not need any action.
2310 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2311 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2312 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2313 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2314 DEF_TRAVERSE_STMT(StringLiteral, {})
2315 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2316 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2317 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2318 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2319
2320 // Traverse OpenCL: AsType, Convert.
2321 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2322
2323 // OpenMP directives.
2324 template <typename Derived>
2325 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2326     OMPExecutableDirective *S) {
2327   for (auto *C : S->clauses()) {
2328     TRY_TO(TraverseOMPClause(C));
2329   }
2330   return true;
2331 }
2332
2333 template <typename Derived>
2334 bool
2335 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2336   return TraverseOMPExecutableDirective(S);
2337 }
2338
2339 DEF_TRAVERSE_STMT(OMPParallelDirective,
2340                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2341
2342 DEF_TRAVERSE_STMT(OMPSimdDirective,
2343                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2344
2345 DEF_TRAVERSE_STMT(OMPForDirective,
2346                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2347
2348 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2349                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2350
2351 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2352                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2353
2354 DEF_TRAVERSE_STMT(OMPSectionDirective,
2355                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2356
2357 DEF_TRAVERSE_STMT(OMPSingleDirective,
2358                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2359
2360 DEF_TRAVERSE_STMT(OMPMasterDirective,
2361                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2362
2363 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2364   TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2365   TRY_TO(TraverseOMPExecutableDirective(S));
2366 })
2367
2368 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2369                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2370
2371 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2372                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2373
2374 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2375                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2376
2377 DEF_TRAVERSE_STMT(OMPTaskDirective,
2378                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2379
2380 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2381                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2382
2383 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2384                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2385
2386 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2387                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2388
2389 DEF_TRAVERSE_STMT(OMPFlushDirective,
2390                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2391
2392 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2393                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2394
2395 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2396                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2397
2398 DEF_TRAVERSE_STMT(OMPTargetDirective,
2399                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2400
2401 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2402                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2403
2404 // OpenMP clauses.
2405 template <typename Derived>
2406 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2407   if (!C)
2408     return true;
2409   switch (C->getClauseKind()) {
2410 #define OPENMP_CLAUSE(Name, Class)                                             \
2411   case OMPC_##Name:                                                            \
2412     TRY_TO(Visit##Class(static_cast<Class *>(C)));                             \
2413     break;
2414 #include "clang/Basic/OpenMPKinds.def"
2415   case OMPC_threadprivate:
2416   case OMPC_unknown:
2417     break;
2418   }
2419   return true;
2420 }
2421
2422 template <typename Derived>
2423 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2424   TRY_TO(TraverseStmt(C->getCondition()));
2425   return true;
2426 }
2427
2428 template <typename Derived>
2429 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2430   TRY_TO(TraverseStmt(C->getCondition()));
2431   return true;
2432 }
2433
2434 template <typename Derived>
2435 bool
2436 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2437   TRY_TO(TraverseStmt(C->getNumThreads()));
2438   return true;
2439 }
2440
2441 template <typename Derived>
2442 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2443   TRY_TO(TraverseStmt(C->getSafelen()));
2444   return true;
2445 }
2446
2447 template <typename Derived>
2448 bool
2449 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2450   TRY_TO(TraverseStmt(C->getNumForLoops()));
2451   return true;
2452 }
2453
2454 template <typename Derived>
2455 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2456   return true;
2457 }
2458
2459 template <typename Derived>
2460 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2461   return true;
2462 }
2463
2464 template <typename Derived>
2465 bool
2466 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2467   TRY_TO(TraverseStmt(C->getChunkSize()));
2468   TRY_TO(TraverseStmt(C->getHelperChunkSize()));
2469   return true;
2470 }
2471
2472 template <typename Derived>
2473 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) {
2474   return true;
2475 }
2476
2477 template <typename Derived>
2478 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2479   return true;
2480 }
2481
2482 template <typename Derived>
2483 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2484   return true;
2485 }
2486
2487 template <typename Derived>
2488 bool
2489 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2490   return true;
2491 }
2492
2493 template <typename Derived>
2494 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2495   return true;
2496 }
2497
2498 template <typename Derived>
2499 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2500   return true;
2501 }
2502
2503 template <typename Derived>
2504 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2505   return true;
2506 }
2507
2508 template <typename Derived>
2509 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2510   return true;
2511 }
2512
2513 template <typename Derived>
2514 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2515   return true;
2516 }
2517
2518 template <typename Derived>
2519 template <typename T>
2520 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2521   for (auto *E : Node->varlists()) {
2522     TRY_TO(TraverseStmt(E));
2523   }
2524   return true;
2525 }
2526
2527 template <typename Derived>
2528 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2529   TRY_TO(VisitOMPClauseList(C));
2530   for (auto *E : C->private_copies()) {
2531     TRY_TO(TraverseStmt(E));
2532   }
2533   return true;
2534 }
2535
2536 template <typename Derived>
2537 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2538     OMPFirstprivateClause *C) {
2539   TRY_TO(VisitOMPClauseList(C));
2540   for (auto *E : C->private_copies()) {
2541     TRY_TO(TraverseStmt(E));
2542   }
2543   for (auto *E : C->inits()) {
2544     TRY_TO(TraverseStmt(E));
2545   }
2546   return true;
2547 }
2548
2549 template <typename Derived>
2550 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2551     OMPLastprivateClause *C) {
2552   TRY_TO(VisitOMPClauseList(C));
2553   for (auto *E : C->private_copies()) {
2554     TRY_TO(TraverseStmt(E));
2555   }
2556   for (auto *E : C->source_exprs()) {
2557     TRY_TO(TraverseStmt(E));
2558   }
2559   for (auto *E : C->destination_exprs()) {
2560     TRY_TO(TraverseStmt(E));
2561   }
2562   for (auto *E : C->assignment_ops()) {
2563     TRY_TO(TraverseStmt(E));
2564   }
2565   return true;
2566 }
2567
2568 template <typename Derived>
2569 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2570   TRY_TO(VisitOMPClauseList(C));
2571   return true;
2572 }
2573
2574 template <typename Derived>
2575 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2576   TRY_TO(TraverseStmt(C->getStep()));
2577   TRY_TO(TraverseStmt(C->getCalcStep()));
2578   TRY_TO(VisitOMPClauseList(C));
2579   for (auto *E : C->inits()) {
2580     TRY_TO(TraverseStmt(E));
2581   }
2582   for (auto *E : C->updates()) {
2583     TRY_TO(TraverseStmt(E));
2584   }
2585   for (auto *E : C->finals()) {
2586     TRY_TO(TraverseStmt(E));
2587   }
2588   return true;
2589 }
2590
2591 template <typename Derived>
2592 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2593   TRY_TO(TraverseStmt(C->getAlignment()));
2594   TRY_TO(VisitOMPClauseList(C));
2595   return true;
2596 }
2597
2598 template <typename Derived>
2599 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2600   TRY_TO(VisitOMPClauseList(C));
2601   for (auto *E : C->source_exprs()) {
2602     TRY_TO(TraverseStmt(E));
2603   }
2604   for (auto *E : C->destination_exprs()) {
2605     TRY_TO(TraverseStmt(E));
2606   }
2607   for (auto *E : C->assignment_ops()) {
2608     TRY_TO(TraverseStmt(E));
2609   }
2610   return true;
2611 }
2612
2613 template <typename Derived>
2614 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2615     OMPCopyprivateClause *C) {
2616   TRY_TO(VisitOMPClauseList(C));
2617   for (auto *E : C->source_exprs()) {
2618     TRY_TO(TraverseStmt(E));
2619   }
2620   for (auto *E : C->destination_exprs()) {
2621     TRY_TO(TraverseStmt(E));
2622   }
2623   for (auto *E : C->assignment_ops()) {
2624     TRY_TO(TraverseStmt(E));
2625   }
2626   return true;
2627 }
2628
2629 template <typename Derived>
2630 bool
2631 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2632   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2633   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2634   TRY_TO(VisitOMPClauseList(C));
2635   for (auto *E : C->lhs_exprs()) {
2636     TRY_TO(TraverseStmt(E));
2637   }
2638   for (auto *E : C->rhs_exprs()) {
2639     TRY_TO(TraverseStmt(E));
2640   }
2641   for (auto *E : C->reduction_ops()) {
2642     TRY_TO(TraverseStmt(E));
2643   }
2644   return true;
2645 }
2646
2647 template <typename Derived>
2648 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2649   TRY_TO(VisitOMPClauseList(C));
2650   return true;
2651 }
2652
2653 // FIXME: look at the following tricky-seeming exprs to see if we
2654 // need to recurse on anything.  These are ones that have methods
2655 // returning decls or qualtypes or nestednamespecifier -- though I'm
2656 // not sure if they own them -- or just seemed very complicated, or
2657 // had lots of sub-types to explore.
2658 //
2659 // VisitOverloadExpr and its children: recurse on template args? etc?
2660
2661 // FIXME: go through all the stmts and exprs again, and see which of them
2662 // create new types, and recurse on the types (TypeLocs?) of those.
2663 // Candidates:
2664 //
2665 //    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2666 //    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2667 //    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2668 //    Every class that has getQualifier.
2669
2670 #undef DEF_TRAVERSE_STMT
2671
2672 #undef TRY_TO
2673
2674 } // end namespace clang
2675
2676 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H