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.
getDerived()136   Derived &getDerived() { return *static_cast<Derived *>(this); }
137 
138   /// \brief Return whether this visitor should recurse into
139   /// template instantiations.
shouldVisitTemplateInstantiations()140   bool shouldVisitTemplateInstantiations() const { return false; }
141 
142   /// \brief Return whether this visitor should recurse into the types of
143   /// TypeLocs.
shouldWalkTypesOfTypeLocs()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.
shouldVisitImplicitCode()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.
shouldUseDataRecursionFor(Stmt * S)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.
VisitAttr(Attr * A)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.
WalkUpFromStmt(Stmt * S)276   bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
VisitStmt(Stmt * 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)
BINOP_LIST()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); }
VisitType(Type * 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.
WalkUpFromTypeLoc(TypeLoc TL)369   bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
VisitTypeLoc(TypeLoc 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.
WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL)374   bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
375     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
376   }
VisitQualifiedTypeLoc(QualifiedTypeLoc TL)377   bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL)378   bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
379     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
380   }
VisitUnqualTypeLoc(UnqualTypeLoc TL)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.
WalkUpFromDecl(Decl * D)402   bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
VisitDecl(Decl * 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 
EnqueueJobEnqueueJob443     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>
dataTraverse(Stmt * S)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>
dataTraverseNode(Stmt * S,bool & EnqueueChildren)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>
TraverseStmt(Stmt * S)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>
TraverseType(QualType T)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>
TraverseTypeLoc(TypeLoc TL)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>
TraverseDecl(Decl * D)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>
TraverseNestedNameSpecifier(NestedNameSpecifier * NNS)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>
TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)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>
TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)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>
TraverseTemplateName(TemplateName Template)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>
TraverseTemplateArgument(const TemplateArgument & Arg)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>
TraverseTemplateArgumentLoc(const TemplateArgumentLoc & ArgLoc)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>
TraverseTemplateArguments(const TemplateArgument * Args,unsigned NumArgs)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>
TraverseConstructorInitializer(CXXCtorInitializer * Init)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
TraverseLambdaCapture(LambdaExpr * LE,const LambdaCapture * C)858 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
859                                                     const LambdaCapture *C) {
860   if (C->isInitCapture())
861     TRY_TO(TraverseDecl(C->getCapturedVar()));
862   return true;
863 }
864 
865 template <typename Derived>
TraverseLambdaBody(LambdaExpr * LE)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
TraverseQualifiedTypeLoc(QualifiedTypeLoc TL)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>
TraverseArrayTypeLocHelper(ArrayTypeLoc TL)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>
TraverseDeclContextHelper(DeclContext * DC)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(NamespaceAliasDecl, {
1360   // We shouldn't traverse an aliased namespace, since it will be
1361   // defined (and, therefore, traversed) somewhere else.
1362   //
1363   // This return statement makes sure the traversal of nodes in
1364   // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1365   // is skipped - don't remove it.
1366   return true;
1367 })
1368 
1369 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1370                              })
1371 
1372 DEF_TRAVERSE_DECL(
1373     NamespaceDecl,
1374     {// Code in an unnamed namespace shows up automatically in
1375      // decls_begin()/decls_end().  Thus we don't need to recurse on
1376      // D->getAnonymousNamespace().
1377     })
1378 
1379 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1380                                            })
1381 
1382 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1383                                     })
1384 
1385 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1386                                         })
1387 
1388 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1389                                           })
1390 
1391 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1392                                      })
1393 
1394 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1395                                     })
1396 
1397 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1398   if (D->getReturnTypeSourceInfo()) {
1399     TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1400   }
1401   for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1402        I != E; ++I) {
1403     TRY_TO(TraverseDecl(*I));
1404   }
1405   if (D->isThisDeclarationADefinition()) {
1406     TRY_TO(TraverseStmt(D->getBody()));
1407   }
1408   return true;
1409 })
1410 
1411 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1412   if (D->getTypeSourceInfo())
1413     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1414   else
1415     TRY_TO(TraverseType(D->getType()));
1416   return true;
1417 })
1418 
1419 DEF_TRAVERSE_DECL(UsingDecl, {
1420   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1421   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1422 })
1423 
1424 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1425   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1426 })
1427 
1428 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1429 
1430 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1431   for (auto *I : D->varlists()) {
1432     TRY_TO(TraverseStmt(I));
1433   }
1434 })
1435 
1436 // A helper method for TemplateDecl's children.
1437 template <typename Derived>
TraverseTemplateParameterListHelper(TemplateParameterList * TPL)1438 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1439     TemplateParameterList *TPL) {
1440   if (TPL) {
1441     for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1442          I != E; ++I) {
1443       TRY_TO(TraverseDecl(*I));
1444     }
1445   }
1446   return true;
1447 }
1448 
1449 template <typename Derived>
TraverseTemplateInstantiations(ClassTemplateDecl * D)1450 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1451     ClassTemplateDecl *D) {
1452   for (auto *SD : D->specializations()) {
1453     for (auto *RD : SD->redecls()) {
1454       // We don't want to visit injected-class-names in this traversal.
1455       if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1456         continue;
1457 
1458       switch (
1459           cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1460       // Visit the implicit instantiations with the requested pattern.
1461       case TSK_Undeclared:
1462       case TSK_ImplicitInstantiation:
1463         TRY_TO(TraverseDecl(RD));
1464         break;
1465 
1466       // We don't need to do anything on an explicit instantiation
1467       // or explicit specialization because there will be an explicit
1468       // node for it elsewhere.
1469       case TSK_ExplicitInstantiationDeclaration:
1470       case TSK_ExplicitInstantiationDefinition:
1471       case TSK_ExplicitSpecialization:
1472         break;
1473       }
1474     }
1475   }
1476 
1477   return true;
1478 }
1479 
1480 template <typename Derived>
TraverseTemplateInstantiations(VarTemplateDecl * D)1481 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1482     VarTemplateDecl *D) {
1483   for (auto *SD : D->specializations()) {
1484     for (auto *RD : SD->redecls()) {
1485       switch (
1486           cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1487       case TSK_Undeclared:
1488       case TSK_ImplicitInstantiation:
1489         TRY_TO(TraverseDecl(RD));
1490         break;
1491 
1492       case TSK_ExplicitInstantiationDeclaration:
1493       case TSK_ExplicitInstantiationDefinition:
1494       case TSK_ExplicitSpecialization:
1495         break;
1496       }
1497     }
1498   }
1499 
1500   return true;
1501 }
1502 
1503 // A helper method for traversing the instantiations of a
1504 // function while skipping its specializations.
1505 template <typename Derived>
TraverseTemplateInstantiations(FunctionTemplateDecl * D)1506 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1507     FunctionTemplateDecl *D) {
1508   for (auto *FD : D->specializations()) {
1509     for (auto *RD : FD->redecls()) {
1510       switch (RD->getTemplateSpecializationKind()) {
1511       case TSK_Undeclared:
1512       case TSK_ImplicitInstantiation:
1513         // We don't know what kind of FunctionDecl this is.
1514         TRY_TO(TraverseDecl(RD));
1515         break;
1516 
1517       // FIXME: For now traverse explicit instantiations here. Change that
1518       // once they are represented as dedicated nodes in the AST.
1519       case TSK_ExplicitInstantiationDeclaration:
1520       case TSK_ExplicitInstantiationDefinition:
1521         TRY_TO(TraverseDecl(RD));
1522         break;
1523 
1524       case TSK_ExplicitSpecialization:
1525         break;
1526       }
1527     }
1528   }
1529 
1530   return true;
1531 }
1532 
1533 // This macro unifies the traversal of class, variable and function
1534 // template declarations.
1535 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND)                                   \
1536   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, {                              \
1537     TRY_TO(TraverseDecl(D->getTemplatedDecl()));                               \
1538     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
1539                                                                                \
1540     /* By default, we do not traverse the instantiations of                    \
1541        class templates since they do not appear in the user code. The          \
1542        following code optionally traverses them.                               \
1543                                                                                \
1544        We only traverse the class instantiations when we see the canonical     \
1545        declaration of the template, to ensure we only visit them once. */      \
1546     if (getDerived().shouldVisitTemplateInstantiations() &&                    \
1547         D == D->getCanonicalDecl())                                            \
1548       TRY_TO(TraverseTemplateInstantiations(D));                               \
1549                                                                                \
1550     /* Note that getInstantiatedFromMemberTemplate() is just a link            \
1551        from a template instantiation back to the template from which           \
1552        it was instantiated, and thus should not be traversed. */               \
1553   })
1554 
1555 DEF_TRAVERSE_TMPL_DECL(Class)
DEF_TRAVERSE_TMPL_DECL(Var)1556 DEF_TRAVERSE_TMPL_DECL(Var)
1557 DEF_TRAVERSE_TMPL_DECL(Function)
1558 
1559 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1560   // D is the "T" in something like
1561   //   template <template <typename> class T> class container { };
1562   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1563   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1564     TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1565   }
1566   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1567 })
1568 
1569 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1570   // D is the "T" in something like "template<typename T> class vector;"
1571   if (D->getTypeForDecl())
1572     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1573   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1574     TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1575 })
1576 
1577 DEF_TRAVERSE_DECL(TypedefDecl, {
1578   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1579   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1580   // declaring the typedef, not something that was written in the
1581   // source.
1582 })
1583 
1584 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1585   TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1586   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1587   // declaring the type alias, not something that was written in the
1588   // source.
1589 })
1590 
1591 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1592   TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1593   TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1594 })
1595 
1596 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1597   // A dependent using declaration which was marked with 'typename'.
1598   //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1599   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1600   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1601   // declaring the type, not something that was written in the
1602   // source.
1603 })
1604 
1605 DEF_TRAVERSE_DECL(EnumDecl, {
1606   if (D->getTypeForDecl())
1607     TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1608 
1609   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1610   // The enumerators are already traversed by
1611   // decls_begin()/decls_end().
1612 })
1613 
1614 // Helper methods for RecordDecl and its children.
1615 template <typename Derived>
1616 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1617   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1618   // declaring the type, not something that was written in the source.
1619 
1620   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1621   return true;
1622 }
1623 
1624 template <typename Derived>
TraverseCXXRecordHelper(CXXRecordDecl * D)1625 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1626   if (!TraverseRecordHelper(D))
1627     return false;
1628   if (D->isCompleteDefinition()) {
1629     for (const auto &I : D->bases()) {
1630       TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1631     }
1632     // We don't traverse the friends or the conversions, as they are
1633     // already in decls_begin()/decls_end().
1634   }
1635   return true;
1636 }
1637 
1638 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1639 
1640 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1641 
1642 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND)                              \
1643   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
1644     /* For implicit instantiations ("set<int> x;"), we don't want to           \
1645        recurse at all, since the instatiated template isn't written in         \
1646        the source code anywhere.  (Note the instatiated *type* --              \
1647        set<int> -- is written, and will still get a callback of                \
1648        TemplateSpecializationType).  For explicit instantiations               \
1649        ("template set<int>;"), we do need a callback, since this               \
1650        is the only callback that's made for this instantiation.                \
1651        We use getTypeAsWritten() to distinguish. */                            \
1652     if (TypeSourceInfo *TSI = D->getTypeAsWritten())                           \
1653       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
1654                                                                                \
1655     if (!getDerived().shouldVisitTemplateInstantiations() &&                   \
1656         D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)      \
1657       /* Returning from here skips traversing the                              \
1658          declaration context of the *TemplateSpecializationDecl                \
1659          (embedded in the DEF_TRAVERSE_DECL() macro)                           \
1660          which contains the instantiated members of the template. */           \
1661       return true;                                                             \
1662   })
1663 
DEF_TRAVERSE_TMPL_SPEC_DECL(Class)1664 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1665 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1666 
1667 template <typename Derived>
1668 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1669     const TemplateArgumentLoc *TAL, unsigned Count) {
1670   for (unsigned I = 0; I < Count; ++I) {
1671     TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1672   }
1673   return true;
1674 }
1675 
1676 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
1677   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
1678     /* The partial specialization. */                                          \
1679     if (TemplateParameterList *TPL = D->getTemplateParameters()) {             \
1680       for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();   \
1681            I != E; ++I) {                                                      \
1682         TRY_TO(TraverseDecl(*I));                                              \
1683       }                                                                        \
1684     }                                                                          \
1685     /* The args that remains unspecialized. */                                 \
1686     TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
1687         D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
1688         D->getTemplateArgsAsWritten()->NumTemplateArgs));                      \
1689                                                                                \
1690     /* Don't need the *TemplatePartialSpecializationHelper, even               \
1691        though that's our parent class -- we already visit all the              \
1692        template args here. */                                                  \
1693     TRY_TO(Traverse##DECLKIND##Helper(D));                                     \
1694                                                                                \
1695     /* Instantiations will have been visited with the primary template. */     \
1696   })
1697 
DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class,CXXRecord)1698 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1699 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1700 
1701 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1702 
1703 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1704   // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1705   //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
1706   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1707   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1708 })
1709 
1710 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1711 
1712 template <typename Derived>
1713 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1714   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1715   if (D->getTypeSourceInfo())
1716     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1717   else
1718     TRY_TO(TraverseType(D->getType()));
1719   return true;
1720 }
1721 
1722 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1723 
1724 DEF_TRAVERSE_DECL(FieldDecl, {
1725   TRY_TO(TraverseDeclaratorHelper(D));
1726   if (D->isBitField())
1727     TRY_TO(TraverseStmt(D->getBitWidth()));
1728   else if (D->hasInClassInitializer())
1729     TRY_TO(TraverseStmt(D->getInClassInitializer()));
1730 })
1731 
1732 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1733   TRY_TO(TraverseDeclaratorHelper(D));
1734   if (D->isBitField())
1735     TRY_TO(TraverseStmt(D->getBitWidth()));
1736   // FIXME: implement the rest.
1737 })
1738 
1739 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1740   TRY_TO(TraverseDeclaratorHelper(D));
1741   if (D->isBitField())
1742     TRY_TO(TraverseStmt(D->getBitWidth()));
1743   // FIXME: implement the rest.
1744 })
1745 
1746 template <typename Derived>
TraverseFunctionHelper(FunctionDecl * D)1747 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1748   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1749   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1750 
1751   // If we're an explicit template specialization, iterate over the
1752   // template args that were explicitly specified.  If we were doing
1753   // this in typing order, we'd do it between the return type and
1754   // the function args, but both are handled by the FunctionTypeLoc
1755   // above, so we have to choose one side.  I've decided to do before.
1756   if (const FunctionTemplateSpecializationInfo *FTSI =
1757           D->getTemplateSpecializationInfo()) {
1758     if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1759         FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1760       // A specialization might not have explicit template arguments if it has
1761       // a templated return type and concrete arguments.
1762       if (const ASTTemplateArgumentListInfo *TALI =
1763               FTSI->TemplateArgumentsAsWritten) {
1764         TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1765                                                   TALI->NumTemplateArgs));
1766       }
1767     }
1768   }
1769 
1770   // Visit the function type itself, which can be either
1771   // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
1772   // also covers the return type and the function parameters,
1773   // including exception specifications.
1774   if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1775     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1776   } else if (getDerived().shouldVisitImplicitCode()) {
1777     // Visit parameter variable declarations of the implicit function
1778     // if the traverser is visiting implicit code. Parameter variable
1779     // declarations do not have valid TypeSourceInfo, so to visit them
1780     // we need to traverse the declarations explicitly.
1781     for (FunctionDecl::param_const_iterator I = D->param_begin(),
1782                                             E = D->param_end();
1783          I != E; ++I)
1784       TRY_TO(TraverseDecl(*I));
1785   }
1786 
1787   if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1788     // Constructor initializers.
1789     for (auto *I : Ctor->inits()) {
1790       TRY_TO(TraverseConstructorInitializer(I));
1791     }
1792   }
1793 
1794   if (D->isThisDeclarationADefinition()) {
1795     TRY_TO(TraverseStmt(D->getBody())); // Function body.
1796   }
1797   return true;
1798 }
1799 
1800 DEF_TRAVERSE_DECL(FunctionDecl, {
1801   // We skip decls_begin/decls_end, which are already covered by
1802   // TraverseFunctionHelper().
1803   return TraverseFunctionHelper(D);
1804 })
1805 
1806 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1807   // We skip decls_begin/decls_end, which are already covered by
1808   // TraverseFunctionHelper().
1809   return TraverseFunctionHelper(D);
1810 })
1811 
1812 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1813   // We skip decls_begin/decls_end, which are already covered by
1814   // TraverseFunctionHelper().
1815   return TraverseFunctionHelper(D);
1816 })
1817 
1818 // CXXConversionDecl is the declaration of a type conversion operator.
1819 // It's not a cast expression.
1820 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1821   // We skip decls_begin/decls_end, which are already covered by
1822   // TraverseFunctionHelper().
1823   return TraverseFunctionHelper(D);
1824 })
1825 
1826 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1827   // We skip decls_begin/decls_end, which are already covered by
1828   // TraverseFunctionHelper().
1829   return TraverseFunctionHelper(D);
1830 })
1831 
1832 template <typename Derived>
TraverseVarHelper(VarDecl * D)1833 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1834   TRY_TO(TraverseDeclaratorHelper(D));
1835   // Default params are taken care of when we traverse the ParmVarDecl.
1836   if (!isa<ParmVarDecl>(D) &&
1837       (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1838     TRY_TO(TraverseStmt(D->getInit()));
1839   return true;
1840 }
1841 
1842 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1843 
1844 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1845 
1846 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1847   // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1848   TRY_TO(TraverseDeclaratorHelper(D));
1849   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1850     TRY_TO(TraverseStmt(D->getDefaultArgument()));
1851 })
1852 
1853 DEF_TRAVERSE_DECL(ParmVarDecl, {
1854   TRY_TO(TraverseVarHelper(D));
1855 
1856   if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1857       !D->hasUnparsedDefaultArg())
1858     TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1859 
1860   if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1861       !D->hasUnparsedDefaultArg())
1862     TRY_TO(TraverseStmt(D->getDefaultArg()));
1863 })
1864 
1865 #undef DEF_TRAVERSE_DECL
1866 
1867 // ----------------- Stmt traversal -----------------
1868 //
1869 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1870 // over the children defined in children() (every stmt defines these,
1871 // though sometimes the range is empty).  Each individual Traverse*
1872 // method only needs to worry about children other than those.  To see
1873 // what children() does for a given class, see, e.g.,
1874 //   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1875 
1876 // This macro makes available a variable S, the passed-in stmt.
1877 #define DEF_TRAVERSE_STMT(STMT, CODE)                                          \
1878   template <typename Derived>                                                  \
1879   bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) {                 \
1880     TRY_TO(WalkUpFrom##STMT(S));                                               \
1881     { CODE; }                                                                  \
1882     for (Stmt::child_range range = S->children(); range; ++range) {            \
1883       TRY_TO(TraverseStmt(*range));                                            \
1884     }                                                                          \
1885     return true;                                                               \
1886   }
1887 
1888 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1889   TRY_TO(TraverseStmt(S->getAsmString()));
1890   for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1891     TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1892   }
1893   for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1894     TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1895   }
1896   for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1897     TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1898   }
1899   // children() iterates over inputExpr and outputExpr.
1900 })
1901 
1902 DEF_TRAVERSE_STMT(
1903     MSAsmStmt,
1904     {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
1905      // added this needs to be implemented.
1906     })
1907 
1908 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1909   TRY_TO(TraverseDecl(S->getExceptionDecl()));
1910   // children() iterates over the handler block.
1911 })
1912 
1913 DEF_TRAVERSE_STMT(DeclStmt, {
1914   for (auto *I : S->decls()) {
1915     TRY_TO(TraverseDecl(I));
1916   }
1917   // Suppress the default iteration over children() by
1918   // returning.  Here's why: A DeclStmt looks like 'type var [=
1919   // initializer]'.  The decls above already traverse over the
1920   // initializers, so we don't have to do it again (which
1921   // children() would do).
1922   return true;
1923 })
1924 
1925 // These non-expr stmts (most of them), do not need any action except
1926 // iterating over the children.
1927 DEF_TRAVERSE_STMT(BreakStmt, {})
1928 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1929 DEF_TRAVERSE_STMT(CaseStmt, {})
1930 DEF_TRAVERSE_STMT(CompoundStmt, {})
1931 DEF_TRAVERSE_STMT(ContinueStmt, {})
1932 DEF_TRAVERSE_STMT(DefaultStmt, {})
1933 DEF_TRAVERSE_STMT(DoStmt, {})
1934 DEF_TRAVERSE_STMT(ForStmt, {})
1935 DEF_TRAVERSE_STMT(GotoStmt, {})
1936 DEF_TRAVERSE_STMT(IfStmt, {})
1937 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1938 DEF_TRAVERSE_STMT(LabelStmt, {})
1939 DEF_TRAVERSE_STMT(AttributedStmt, {})
1940 DEF_TRAVERSE_STMT(NullStmt, {})
1941 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1942 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1943 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1944 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1945 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1946 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1947 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1948 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1949   if (!getDerived().shouldVisitImplicitCode()) {
1950     TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1951     TRY_TO(TraverseStmt(S->getRangeInit()));
1952     TRY_TO(TraverseStmt(S->getBody()));
1953     // Visit everything else only if shouldVisitImplicitCode().
1954     return true;
1955   }
1956 })
1957 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1958   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1959   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1960 })
1961 DEF_TRAVERSE_STMT(ReturnStmt, {})
1962 DEF_TRAVERSE_STMT(SwitchStmt, {})
1963 DEF_TRAVERSE_STMT(WhileStmt, {})
1964 
1965 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1966   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1967   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1968   if (S->hasExplicitTemplateArgs()) {
1969     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1970                                               S->getNumTemplateArgs()));
1971   }
1972 })
1973 
1974 DEF_TRAVERSE_STMT(DeclRefExpr, {
1975   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1976   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1977   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1978                                             S->getNumTemplateArgs()));
1979 })
1980 
1981 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1982   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1983   TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1984   if (S->hasExplicitTemplateArgs()) {
1985     TRY_TO(TraverseTemplateArgumentLocsHelper(
1986         S->getExplicitTemplateArgs().getTemplateArgs(),
1987         S->getNumTemplateArgs()));
1988   }
1989 })
1990 
1991 DEF_TRAVERSE_STMT(MemberExpr, {
1992   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1993   TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1994   TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1995                                             S->getNumTemplateArgs()));
1996 })
1997 
1998 DEF_TRAVERSE_STMT(
1999     ImplicitCastExpr,
2000     {// We don't traverse the cast type, as it's not written in the
2001      // source code.
2002     })
2003 
2004 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2005   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2006 })
2007 
2008 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2009   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2010 })
2011 
2012 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2013   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2014 })
2015 
2016 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2017   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2018 })
2019 
2020 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2021   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2022 })
2023 
2024 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2025   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2026 })
2027 
2028 // InitListExpr is a tricky one, because we want to do all our work on
2029 // the syntactic form of the listexpr, but this method takes the
2030 // semantic form by default.  We can't use the macro helper because it
2031 // calls WalkUp*() on the semantic form, before our code can convert
2032 // to the syntactic form.
2033 template <typename Derived>
TraverseInitListExpr(InitListExpr * S)2034 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
2035   if (InitListExpr *Syn = S->getSyntacticForm())
2036     S = Syn;
2037   TRY_TO(WalkUpFromInitListExpr(S));
2038   // All we need are the default actions.  FIXME: use a helper function.
2039   for (Stmt::child_range range = S->children(); range; ++range) {
2040     TRY_TO(TraverseStmt(*range));
2041   }
2042   return true;
2043 }
2044 
2045 // GenericSelectionExpr is a special case because the types and expressions
2046 // are interleaved.  We also need to watch out for null types (default
2047 // generic associations).
2048 template <typename Derived>
TraverseGenericSelectionExpr(GenericSelectionExpr * S)2049 bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr(
2050     GenericSelectionExpr *S) {
2051   TRY_TO(WalkUpFromGenericSelectionExpr(S));
2052   TRY_TO(TraverseStmt(S->getControllingExpr()));
2053   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2054     if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2055       TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2056     TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2057   }
2058   return true;
2059 }
2060 
2061 // PseudoObjectExpr is a special case because of the wierdness with
2062 // syntactic expressions and opaque values.
2063 template <typename Derived>
2064 bool
TraversePseudoObjectExpr(PseudoObjectExpr * S)2065 RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2066   TRY_TO(WalkUpFromPseudoObjectExpr(S));
2067   TRY_TO(TraverseStmt(S->getSyntacticForm()));
2068   for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2069                                             e = S->semantics_end();
2070        i != e; ++i) {
2071     Expr *sub = *i;
2072     if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2073       sub = OVE->getSourceExpr();
2074     TRY_TO(TraverseStmt(sub));
2075   }
2076   return true;
2077 }
2078 
2079 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2080   // This is called for code like 'return T()' where T is a built-in
2081   // (i.e. non-class) type.
2082   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2083 })
2084 
2085 DEF_TRAVERSE_STMT(CXXNewExpr, {
2086   // The child-iterator will pick up the other arguments.
2087   TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2088 })
2089 
2090 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2091   // The child-iterator will pick up the expression representing
2092   // the field.
2093   // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2094   // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2095   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2096 })
2097 
2098 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2099   // The child-iterator will pick up the arg if it's an expression,
2100   // but not if it's a type.
2101   if (S->isArgumentType())
2102     TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2103 })
2104 
2105 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2106   // The child-iterator will pick up the arg if it's an expression,
2107   // but not if it's a type.
2108   if (S->isTypeOperand())
2109     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2110 })
2111 
2112 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2113   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2114 })
2115 
2116 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2117   // The child-iterator will pick up the arg if it's an expression,
2118   // but not if it's a type.
2119   if (S->isTypeOperand())
2120     TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2121 })
2122 
2123 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2124   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2125     TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2126 })
2127 
2128 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2129   TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2130 })
2131 
2132 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2133                   { TRY_TO(TraverseStmt(S->getQueriedExpression())); })
2134 
2135 DEF_TRAVERSE_STMT(VAArgExpr, {
2136   // The child-iterator will pick up the expression argument.
2137   TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2138 })
2139 
2140 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2141   // This is called for code like 'return T()' where T is a class type.
2142   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2143 })
2144 
2145 // Walk only the visible parts of lambda expressions.
2146 template <typename Derived>
TraverseLambdaExpr(LambdaExpr * S)2147 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2148   TRY_TO(WalkUpFromLambdaExpr(S));
2149 
2150   for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2151                                     CEnd = S->explicit_capture_end();
2152        C != CEnd; ++C) {
2153     TRY_TO(TraverseLambdaCapture(S, C));
2154   }
2155 
2156   TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2157   FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2158 
2159   if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2160     // Visit the whole type.
2161     TRY_TO(TraverseTypeLoc(TL));
2162   } else {
2163     if (S->hasExplicitParameters()) {
2164       // Visit parameters.
2165       for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2166         TRY_TO(TraverseDecl(Proto.getParam(I)));
2167       }
2168     } else if (S->hasExplicitResultType()) {
2169       TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2170     }
2171 
2172     auto *T = Proto.getTypePtr();
2173     for (const auto &E : T->exceptions()) {
2174       TRY_TO(TraverseType(E));
2175     }
2176 
2177     if (Expr *NE = T->getNoexceptExpr())
2178       TRY_TO(TraverseStmt(NE));
2179   }
2180 
2181   TRY_TO(TraverseLambdaBody(S));
2182   return true;
2183 }
2184 
2185 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2186   // This is called for code like 'T()', where T is a template argument.
2187   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2188 })
2189 
2190 // These expressions all might take explicit template arguments.
2191 // We traverse those if so.  FIXME: implement these.
2192 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2193 DEF_TRAVERSE_STMT(CallExpr, {})
2194 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2195 
2196 // These exprs (most of them), do not need any action except iterating
2197 // over the children.
2198 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2199 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2200 DEF_TRAVERSE_STMT(BlockExpr, {
2201   TRY_TO(TraverseDecl(S->getBlockDecl()));
2202   return true; // no child statements to loop through.
2203 })
2204 DEF_TRAVERSE_STMT(ChooseExpr, {})
2205 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2206   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2207 })
2208 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2209 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2210 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2211 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2212 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2213 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2214 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2215 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2216 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2217   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2218   if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2219     TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2220   if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2221     TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2222 })
2223 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2224 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2225 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2226 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2227 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2228 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2229 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2230 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2231 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2232   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2233     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2234 })
2235 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2236 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2237 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2238   if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2239     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2240 })
2241 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2242 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2243 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2244 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2245 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2246 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2247   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2248 })
2249 DEF_TRAVERSE_STMT(ParenExpr, {})
2250 DEF_TRAVERSE_STMT(ParenListExpr, {})
2251 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2252 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2253 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2254 DEF_TRAVERSE_STMT(StmtExpr, {})
2255 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2256   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2257   if (S->hasExplicitTemplateArgs()) {
2258     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2259                                               S->getNumTemplateArgs()));
2260   }
2261 })
2262 
2263 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2264   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2265   if (S->hasExplicitTemplateArgs()) {
2266     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2267                                               S->getNumTemplateArgs()));
2268   }
2269 })
2270 
2271 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2272 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2273 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2274 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2275 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2276 
2277 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2278 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2279 DEF_TRAVERSE_STMT(TypoExpr, {})
2280 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2281 
2282 // These operators (all of them) do not need any action except
2283 // iterating over the children.
2284 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2285 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2286 DEF_TRAVERSE_STMT(UnaryOperator, {})
2287 DEF_TRAVERSE_STMT(BinaryOperator, {})
2288 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2289 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2290 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2291 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2292 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2293 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2294 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2295 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2296 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2297 DEF_TRAVERSE_STMT(AtomicExpr, {})
2298 
2299 // These literals (all of them) do not need any action.
2300 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2301 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2302 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2303 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2304 DEF_TRAVERSE_STMT(StringLiteral, {})
2305 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2306 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2307 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2308 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2309 
2310 // Traverse OpenCL: AsType, Convert.
2311 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2312 
2313 // OpenMP directives.
2314 template <typename Derived>
TraverseOMPExecutableDirective(OMPExecutableDirective * S)2315 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2316     OMPExecutableDirective *S) {
2317   for (auto *C : S->clauses()) {
2318     TRY_TO(TraverseOMPClause(C));
2319   }
2320   return true;
2321 }
2322 
2323 template <typename Derived>
2324 bool
TraverseOMPLoopDirective(OMPLoopDirective * S)2325 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2326   return TraverseOMPExecutableDirective(S);
2327 }
2328 
2329 DEF_TRAVERSE_STMT(OMPParallelDirective,
2330                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2331 
2332 DEF_TRAVERSE_STMT(OMPSimdDirective,
2333                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2334 
2335 DEF_TRAVERSE_STMT(OMPForDirective,
2336                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2337 
2338 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2339                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2340 
2341 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2342                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2343 
2344 DEF_TRAVERSE_STMT(OMPSectionDirective,
2345                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2346 
2347 DEF_TRAVERSE_STMT(OMPSingleDirective,
2348                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2349 
2350 DEF_TRAVERSE_STMT(OMPMasterDirective,
2351                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2352 
2353 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2354   TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2355   TRY_TO(TraverseOMPExecutableDirective(S));
2356 })
2357 
2358 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2359                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2360 
2361 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2362                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2363 
2364 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2365                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2366 
2367 DEF_TRAVERSE_STMT(OMPTaskDirective,
2368                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2369 
2370 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2371                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2372 
2373 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2374                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2375 
2376 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2377                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2378 
2379 DEF_TRAVERSE_STMT(OMPFlushDirective,
2380                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2381 
2382 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2383                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2384 
2385 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2386                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2387 
2388 DEF_TRAVERSE_STMT(OMPTargetDirective,
2389                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2390 
2391 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2392                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
2393 
2394 // OpenMP clauses.
2395 template <typename Derived>
TraverseOMPClause(OMPClause * C)2396 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2397   if (!C)
2398     return true;
2399   switch (C->getClauseKind()) {
2400 #define OPENMP_CLAUSE(Name, Class)                                             \
2401   case OMPC_##Name:                                                            \
2402     TRY_TO(Visit##Class(static_cast<Class *>(C)));                             \
2403     break;
2404 #include "clang/Basic/OpenMPKinds.def"
2405   case OMPC_threadprivate:
2406   case OMPC_unknown:
2407     break;
2408   }
2409   return true;
2410 }
2411 
2412 template <typename Derived>
VisitOMPIfClause(OMPIfClause * C)2413 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2414   TRY_TO(TraverseStmt(C->getCondition()));
2415   return true;
2416 }
2417 
2418 template <typename Derived>
VisitOMPFinalClause(OMPFinalClause * C)2419 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2420   TRY_TO(TraverseStmt(C->getCondition()));
2421   return true;
2422 }
2423 
2424 template <typename Derived>
2425 bool
VisitOMPNumThreadsClause(OMPNumThreadsClause * C)2426 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2427   TRY_TO(TraverseStmt(C->getNumThreads()));
2428   return true;
2429 }
2430 
2431 template <typename Derived>
VisitOMPSafelenClause(OMPSafelenClause * C)2432 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2433   TRY_TO(TraverseStmt(C->getSafelen()));
2434   return true;
2435 }
2436 
2437 template <typename Derived>
2438 bool
VisitOMPCollapseClause(OMPCollapseClause * C)2439 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2440   TRY_TO(TraverseStmt(C->getNumForLoops()));
2441   return true;
2442 }
2443 
2444 template <typename Derived>
VisitOMPDefaultClause(OMPDefaultClause *)2445 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2446   return true;
2447 }
2448 
2449 template <typename Derived>
VisitOMPProcBindClause(OMPProcBindClause *)2450 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2451   return true;
2452 }
2453 
2454 template <typename Derived>
2455 bool
VisitOMPScheduleClause(OMPScheduleClause * C)2456 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2457   TRY_TO(TraverseStmt(C->getChunkSize()));
2458   return true;
2459 }
2460 
2461 template <typename Derived>
VisitOMPOrderedClause(OMPOrderedClause *)2462 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) {
2463   return true;
2464 }
2465 
2466 template <typename Derived>
VisitOMPNowaitClause(OMPNowaitClause *)2467 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2468   return true;
2469 }
2470 
2471 template <typename Derived>
VisitOMPUntiedClause(OMPUntiedClause *)2472 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2473   return true;
2474 }
2475 
2476 template <typename Derived>
2477 bool
VisitOMPMergeableClause(OMPMergeableClause *)2478 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2479   return true;
2480 }
2481 
2482 template <typename Derived>
VisitOMPReadClause(OMPReadClause *)2483 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2484   return true;
2485 }
2486 
2487 template <typename Derived>
VisitOMPWriteClause(OMPWriteClause *)2488 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2489   return true;
2490 }
2491 
2492 template <typename Derived>
VisitOMPUpdateClause(OMPUpdateClause *)2493 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2494   return true;
2495 }
2496 
2497 template <typename Derived>
VisitOMPCaptureClause(OMPCaptureClause *)2498 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2499   return true;
2500 }
2501 
2502 template <typename Derived>
VisitOMPSeqCstClause(OMPSeqCstClause *)2503 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2504   return true;
2505 }
2506 
2507 template <typename Derived>
2508 template <typename T>
VisitOMPClauseList(T * Node)2509 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2510   for (auto *E : Node->varlists()) {
2511     TRY_TO(TraverseStmt(E));
2512   }
2513   return true;
2514 }
2515 
2516 template <typename Derived>
VisitOMPPrivateClause(OMPPrivateClause * C)2517 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2518   TRY_TO(VisitOMPClauseList(C));
2519   for (auto *E : C->private_copies()) {
2520     TRY_TO(TraverseStmt(E));
2521   }
2522   return true;
2523 }
2524 
2525 template <typename Derived>
VisitOMPFirstprivateClause(OMPFirstprivateClause * C)2526 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2527     OMPFirstprivateClause *C) {
2528   TRY_TO(VisitOMPClauseList(C));
2529   for (auto *E : C->private_copies()) {
2530     TRY_TO(TraverseStmt(E));
2531   }
2532   for (auto *E : C->inits()) {
2533     TRY_TO(TraverseStmt(E));
2534   }
2535   return true;
2536 }
2537 
2538 template <typename Derived>
VisitOMPLastprivateClause(OMPLastprivateClause * C)2539 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2540     OMPLastprivateClause *C) {
2541   TRY_TO(VisitOMPClauseList(C));
2542   return true;
2543 }
2544 
2545 template <typename Derived>
VisitOMPSharedClause(OMPSharedClause * C)2546 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2547   TRY_TO(VisitOMPClauseList(C));
2548   return true;
2549 }
2550 
2551 template <typename Derived>
VisitOMPLinearClause(OMPLinearClause * C)2552 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2553   TRY_TO(TraverseStmt(C->getStep()));
2554   TRY_TO(VisitOMPClauseList(C));
2555   return true;
2556 }
2557 
2558 template <typename Derived>
VisitOMPAlignedClause(OMPAlignedClause * C)2559 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2560   TRY_TO(TraverseStmt(C->getAlignment()));
2561   TRY_TO(VisitOMPClauseList(C));
2562   return true;
2563 }
2564 
2565 template <typename Derived>
VisitOMPCopyinClause(OMPCopyinClause * C)2566 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2567   TRY_TO(VisitOMPClauseList(C));
2568   return true;
2569 }
2570 
2571 template <typename Derived>
VisitOMPCopyprivateClause(OMPCopyprivateClause * C)2572 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2573     OMPCopyprivateClause *C) {
2574   TRY_TO(VisitOMPClauseList(C));
2575   return true;
2576 }
2577 
2578 template <typename Derived>
2579 bool
VisitOMPReductionClause(OMPReductionClause * C)2580 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2581   TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2582   TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2583   TRY_TO(VisitOMPClauseList(C));
2584   return true;
2585 }
2586 
2587 template <typename Derived>
VisitOMPFlushClause(OMPFlushClause * C)2588 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2589   TRY_TO(VisitOMPClauseList(C));
2590   return true;
2591 }
2592 
2593 // FIXME: look at the following tricky-seeming exprs to see if we
2594 // need to recurse on anything.  These are ones that have methods
2595 // returning decls or qualtypes or nestednamespecifier -- though I'm
2596 // not sure if they own them -- or just seemed very complicated, or
2597 // had lots of sub-types to explore.
2598 //
2599 // VisitOverloadExpr and its children: recurse on template args? etc?
2600 
2601 // FIXME: go through all the stmts and exprs again, and see which of them
2602 // create new types, and recurse on the types (TypeLocs?) of those.
2603 // Candidates:
2604 //
2605 //    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2606 //    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2607 //    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2608 //    Every class that has getQualifier.
2609 
2610 #undef DEF_TRAVERSE_STMT
2611 
2612 #undef TRY_TO
2613 
2614 } // end namespace clang
2615 
2616 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
2617