1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecursiveASTVisitor interface, which recursively
11 // traverses the entire AST.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
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"
37 // The following three macros are used for meta programming. The code
38 // using them is responsible for defining macro OPERATOR().
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) \
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)
55 // All compound assign operators.
57 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
58 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
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
66 #define TRY_TO(CALL_EXPR) \
68 if (!getDerived().CALL_EXPR) \
72 /// \brief A class that does preorder depth-first traversal on the
73 /// entire Clang AST and visits each node.
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.
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
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.
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.
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()).
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.
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.
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 {
135 /// \brief Return a reference to the derived class.
136 Derived &getDerived() { return *static_cast<Derived *>(this); }
138 /// \brief Return whether this visitor should recurse into
139 /// template instantiations.
140 bool shouldVisitTemplateInstantiations() const { return false; }
142 /// \brief Return whether this visitor should recurse into the types of
144 bool shouldWalkTypesOfTypeLocs() const { return true; }
146 /// \brief Return whether this visitor should recurse into implicit
147 /// code, e.g., implicit constructors and destructors.
148 bool shouldVisitImplicitCode() const { return false; }
150 /// \brief Return whether \param S should be traversed using data recursion
151 /// to avoid a stack overflow with extreme cases.
152 bool shouldUseDataRecursionFor(Stmt *S) const {
153 return isa<BinaryOperator>(S) || isa<UnaryOperator>(S) ||
154 isa<CaseStmt>(S) || isa<CXXOperatorCallExpr>(S);
157 /// \brief Recursively visit a statement or expression, by
158 /// dispatching to Traverse*() based on the argument's dynamic type.
160 /// \returns false if the visitation was terminated early, true
161 /// otherwise (including when the argument is NULL).
162 bool TraverseStmt(Stmt *S);
164 /// \brief Recursively visit a type, by dispatching to
165 /// Traverse*Type() based on the argument's getTypeClass() property.
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);
171 /// \brief Recursively visit a type with location, by dispatching to
172 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
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);
178 /// \brief Recursively visit an attribute, by dispatching to
179 /// Traverse*Attr() based on the argument's dynamic type.
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);
185 /// \brief Recursively visit a declaration, by dispatching to
186 /// Traverse*Decl() based on the argument's dynamic type.
188 /// \returns false if the visitation was terminated early, true
189 /// otherwise (including when the argument is NULL).
190 bool TraverseDecl(Decl *D);
192 /// \brief Recursively visit a C++ nested-name-specifier.
194 /// \returns false if the visitation was terminated early, true otherwise.
195 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
197 /// \brief Recursively visit a C++ nested-name-specifier with location
200 /// \returns false if the visitation was terminated early, true otherwise.
201 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
203 /// \brief Recursively visit a name with its location information.
205 /// \returns false if the visitation was terminated early, true otherwise.
206 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
208 /// \brief Recursively visit a template name and dispatch to the
209 /// appropriate method.
211 /// \returns false if the visitation was terminated early, true otherwise.
212 bool TraverseTemplateName(TemplateName Template);
214 /// \brief Recursively visit a template argument and dispatch to the
215 /// appropriate method for the argument type.
217 /// \returns false if the visitation was terminated early, true otherwise.
218 // FIXME: migrate callers to TemplateArgumentLoc instead.
219 bool TraverseTemplateArgument(const TemplateArgument &Arg);
221 /// \brief Recursively visit a template argument location and dispatch to the
222 /// appropriate method for the argument type.
224 /// \returns false if the visitation was terminated early, true otherwise.
225 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
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.
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,
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.
241 /// \returns false if the visitation was terminated early, true otherwise.
242 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
244 /// \brief Recursively visit a lambda capture.
246 /// \returns false if the visitation was terminated early, true otherwise.
247 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C);
249 /// \brief Recursively visit the body of a lambda expression.
251 /// This provides a hook for visitors that need more context when visiting
252 /// \c LE->getBody().
254 /// \returns false if the visitation was terminated early, true otherwise.
255 bool TraverseLambdaBody(LambdaExpr *LE);
257 // ---- Methods on Attrs ----
259 // \brief Visit an attribute.
260 bool VisitAttr(Attr *A) { return true; }
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
267 // ---- Methods on Stmts ----
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.
275 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
276 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
277 bool VisitStmt(Stmt *S) { return true; }
278 #define STMT(CLASS, PARENT) \
279 bool WalkUpFrom##CLASS(CLASS *S) { \
280 TRY_TO(WalkUpFrom##PARENT(S)); \
281 TRY_TO(Visit##CLASS(S)); \
284 bool Visit##CLASS(CLASS *S) { return true; }
285 #include "clang/AST/StmtNodes.inc"
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())); \
296 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
297 TRY_TO(WalkUpFromUnaryOperator(S)); \
298 TRY_TO(VisitUnary##NAME(S)); \
301 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
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())); \
316 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
317 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
318 TRY_TO(VisitBin##NAME(S)); \
321 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
323 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
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)
336 #undef GENERAL_BINOP_FALLBACK
338 // ---- Methods on Types ----
339 // FIXME: revamp to take TypeLoc's rather than Types.
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.
347 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
348 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
349 bool VisitType(Type *T) { return true; }
350 #define TYPE(CLASS, BASE) \
351 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
352 TRY_TO(WalkUpFrom##BASE(T)); \
353 TRY_TO(Visit##CLASS##Type(T)); \
356 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
357 #include "clang/AST/TypeNodes.def"
359 // ---- Methods on TypeLocs ----
360 // FIXME: this currently just calls the matching Type methods
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.
368 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
369 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
370 bool VisitTypeLoc(TypeLoc TL) { return true; }
372 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
373 // TypeNodes.def and thus need to be handled specially.
374 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
375 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
377 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
378 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
379 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
381 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
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)); \
390 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
391 #include "clang/AST/TypeNodes.def"
393 // ---- Methods on Decls ----
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.
401 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
402 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
403 bool VisitDecl(Decl *D) { return true; }
404 #define DECL(CLASS, BASE) \
405 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
406 TRY_TO(WalkUpFrom##BASE(D)); \
407 TRY_TO(Visit##CLASS##Decl(D)); \
410 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
411 #include "clang/AST/DeclNodes.inc"
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,
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);
441 Stmt::child_iterator StmtIt;
443 EnqueueJob(Stmt *S) : S(S), StmtIt() {}
445 bool dataTraverse(Stmt *S);
446 bool dataTraverseNode(Stmt *S, bool &EnqueueChildren);
449 template <typename Derived>
450 bool RecursiveASTVisitor<Derived>::dataTraverse(Stmt *S) {
452 SmallVector<EnqueueJob, 16> Queue;
455 while (!Queue.empty()) {
456 EnqueueJob &job = Queue.back();
463 if (getDerived().shouldUseDataRecursionFor(CurrS)) {
464 if (job.StmtIt == Stmt::child_iterator()) {
465 bool EnqueueChildren = true;
466 if (!dataTraverseNode(CurrS, EnqueueChildren))
468 if (!EnqueueChildren) {
472 job.StmtIt = CurrS->child_begin();
477 if (job.StmtIt != CurrS->child_end())
478 Queue.push_back(*job.StmtIt);
485 TRY_TO(TraverseStmt(CurrS));
491 template <typename Derived>
492 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
493 bool &EnqueueChildren) {
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) \
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)); \
504 EnqueueChildren = false; \
505 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR));
507 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
508 switch (BinOp->getOpcode()) {
509 #define OPERATOR(NAME) \
511 DISPATCH_WALK(Bin##NAME, BinaryOperator, S);
516 #define OPERATOR(NAME) \
517 case BO_##NAME##Assign: \
518 DISPATCH_WALK(Bin##NAME##Assign, CompoundAssignOperator, S);
523 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
524 switch (UnOp->getOpcode()) {
525 #define OPERATOR(NAME) \
527 DISPATCH_WALK(Unary##NAME, UnaryOperator, S);
534 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
535 switch (S->getStmtClass()) {
536 case Stmt::NoStmtClass:
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"
550 #define DISPATCH(NAME, CLASS, VAR) \
551 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))
553 template <typename Derived>
554 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
558 #define DISPATCH_STMT(NAME, CLASS, VAR) DISPATCH(NAME, CLASS, VAR)
560 if (getDerived().shouldUseDataRecursionFor(S))
561 return dataTraverse(S);
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
566 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
567 switch (BinOp->getOpcode()) {
568 #define OPERATOR(NAME) \
570 DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
576 #define OPERATOR(NAME) \
577 case BO_##NAME##Assign: \
578 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
584 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
585 switch (UnOp->getOpcode()) {
586 #define OPERATOR(NAME) \
588 DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
596 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
597 switch (S->getStmtClass()) {
598 case Stmt::NoStmtClass:
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"
612 template <typename Derived>
613 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
617 switch (T->getTypeClass()) {
618 #define ABSTRACT_TYPE(CLASS, BASE)
619 #define TYPE(CLASS, BASE) \
621 DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr()));
622 #include "clang/AST/TypeNodes.def"
628 template <typename Derived>
629 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
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"
644 // Define the Traverse*Attr(Attr* A) methods
645 #define VISITORCLASS RecursiveASTVisitor
646 #include "clang/AST/AttrVisitor.inc"
649 template <typename Derived>
650 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
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())
659 switch (D->getKind()) {
660 #define ABSTRACT_DECL(DECL)
661 #define DECL(CLASS, BASE) \
663 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \
666 #include "clang/AST/DeclNodes.inc"
669 // Visit any attributes attached to this declaration.
670 for (auto *I : D->attrs()) {
671 if (!getDerived().TraverseAttr(I))
679 template <typename Derived>
680 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
681 NestedNameSpecifier *NNS) {
685 if (NNS->getPrefix())
686 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
688 switch (NNS->getKind()) {
689 case NestedNameSpecifier::Identifier:
690 case NestedNameSpecifier::Namespace:
691 case NestedNameSpecifier::NamespaceAlias:
692 case NestedNameSpecifier::Global:
693 case NestedNameSpecifier::Super:
696 case NestedNameSpecifier::TypeSpec:
697 case NestedNameSpecifier::TypeSpecWithTemplate:
698 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
704 template <typename Derived>
705 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
706 NestedNameSpecifierLoc NNS) {
710 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
711 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
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:
721 case NestedNameSpecifier::TypeSpec:
722 case NestedNameSpecifier::TypeSpecWithTemplate:
723 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
730 template <typename Derived>
731 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
732 DeclarationNameInfo NameInfo) {
733 switch (NameInfo.getName().getNameKind()) {
734 case DeclarationName::CXXConstructorName:
735 case DeclarationName::CXXDestructorName:
736 case DeclarationName::CXXConversionFunctionName:
737 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
738 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
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:
755 template <typename Derived>
756 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
757 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
758 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
759 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
760 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
765 template <typename Derived>
766 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
767 const TemplateArgument &Arg) {
768 switch (Arg.getKind()) {
769 case TemplateArgument::Null:
770 case TemplateArgument::Declaration:
771 case TemplateArgument::Integral:
772 case TemplateArgument::NullPtr:
775 case TemplateArgument::Type:
776 return getDerived().TraverseType(Arg.getAsType());
778 case TemplateArgument::Template:
779 case TemplateArgument::TemplateExpansion:
780 return getDerived().TraverseTemplateName(
781 Arg.getAsTemplateOrTemplatePattern());
783 case TemplateArgument::Expression:
784 return getDerived().TraverseStmt(Arg.getAsExpr());
786 case TemplateArgument::Pack:
787 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
794 // FIXME: no template name location?
795 // FIXME: no source locations for a template argument pack?
796 template <typename Derived>
797 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
798 const TemplateArgumentLoc &ArgLoc) {
799 const TemplateArgument &Arg = ArgLoc.getArgument();
801 switch (Arg.getKind()) {
802 case TemplateArgument::Null:
803 case TemplateArgument::Declaration:
804 case TemplateArgument::Integral:
805 case TemplateArgument::NullPtr:
808 case TemplateArgument::Type: {
809 // FIXME: how can TSI ever be NULL?
810 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
811 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
813 return getDerived().TraverseType(Arg.getAsType());
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());
824 case TemplateArgument::Expression:
825 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
827 case TemplateArgument::Pack:
828 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
835 template <typename Derived>
836 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
837 const TemplateArgument *Args, unsigned NumArgs) {
838 for (unsigned I = 0; I != NumArgs; ++I) {
839 TRY_TO(TraverseTemplateArgument(Args[I]));
845 template <typename Derived>
846 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
847 CXXCtorInitializer *Init) {
848 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
849 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
851 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
852 TRY_TO(TraverseStmt(Init->getInit()));
856 template <typename Derived>
858 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
859 const LambdaCapture *C) {
860 if (LE->isInitCapture(C))
861 TRY_TO(TraverseDecl(C->getCapturedVar()));
865 template <typename Derived>
866 bool RecursiveASTVisitor<Derived>::TraverseLambdaBody(LambdaExpr *LE) {
867 TRY_TO(TraverseStmt(LE->getBody()));
871 // ----------------- Type traversal -----------------
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)); \
882 DEF_TRAVERSE_TYPE(BuiltinType, {})
884 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
886 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
888 DEF_TRAVERSE_TYPE(BlockPointerType,
889 { TRY_TO(TraverseType(T->getPointeeType())); })
891 DEF_TRAVERSE_TYPE(LValueReferenceType,
892 { TRY_TO(TraverseType(T->getPointeeType())); })
894 DEF_TRAVERSE_TYPE(RValueReferenceType,
895 { TRY_TO(TraverseType(T->getPointeeType())); })
897 DEF_TRAVERSE_TYPE(MemberPointerType, {
898 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
899 TRY_TO(TraverseType(T->getPointeeType()));
902 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
904 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
906 DEF_TRAVERSE_TYPE(ConstantArrayType,
907 { TRY_TO(TraverseType(T->getElementType())); })
909 DEF_TRAVERSE_TYPE(IncompleteArrayType,
910 { TRY_TO(TraverseType(T->getElementType())); })
912 DEF_TRAVERSE_TYPE(VariableArrayType, {
913 TRY_TO(TraverseType(T->getElementType()));
914 TRY_TO(TraverseStmt(T->getSizeExpr()));
917 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
918 TRY_TO(TraverseType(T->getElementType()));
919 if (T->getSizeExpr())
920 TRY_TO(TraverseStmt(T->getSizeExpr()));
923 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
924 if (T->getSizeExpr())
925 TRY_TO(TraverseStmt(T->getSizeExpr()));
926 TRY_TO(TraverseType(T->getElementType()));
929 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
931 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
933 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
934 { TRY_TO(TraverseType(T->getReturnType())); })
936 DEF_TRAVERSE_TYPE(FunctionProtoType, {
937 TRY_TO(TraverseType(T->getReturnType()));
939 for (const auto &A : T->param_types()) {
940 TRY_TO(TraverseType(A));
943 for (const auto &E : T->exceptions()) {
944 TRY_TO(TraverseType(E));
947 if (Expr *NE = T->getNoexceptExpr())
948 TRY_TO(TraverseStmt(NE));
951 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
952 DEF_TRAVERSE_TYPE(TypedefType, {})
954 DEF_TRAVERSE_TYPE(TypeOfExprType,
955 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
957 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
959 DEF_TRAVERSE_TYPE(DecltypeType,
960 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
962 DEF_TRAVERSE_TYPE(UnaryTransformType, {
963 TRY_TO(TraverseType(T->getBaseType()));
964 TRY_TO(TraverseType(T->getUnderlyingType()));
967 DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); })
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, {})
975 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
976 TRY_TO(TraverseTemplateName(T->getTemplateName()));
977 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
980 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
982 DEF_TRAVERSE_TYPE(AttributedType,
983 { TRY_TO(TraverseType(T->getModifiedType())); })
985 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
987 DEF_TRAVERSE_TYPE(ElaboratedType, {
988 if (T->getQualifier()) {
989 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
991 TRY_TO(TraverseType(T->getNamedType()));
994 DEF_TRAVERSE_TYPE(DependentNameType,
995 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
997 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
998 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
999 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1002 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1004 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1006 DEF_TRAVERSE_TYPE(ObjCObjectType, {
1007 // We have to watch out here because an ObjCInterfaceType's base
1009 if (T->getBaseType().getTypePtr() != T)
1010 TRY_TO(TraverseType(T->getBaseType()));
1013 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1014 { TRY_TO(TraverseType(T->getPointeeType())); })
1016 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1018 #undef DEF_TRAVERSE_TYPE
1020 // ----------------- TypeLoc traversal -----------------
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)); \
1037 template <typename Derived>
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
1055 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1058 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1060 // FIXME: ComplexTypeLoc is unfinished
1061 DEF_TRAVERSE_TYPELOC(ComplexType, {
1062 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1065 DEF_TRAVERSE_TYPELOC(PointerType,
1066 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1068 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1069 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1071 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1072 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1074 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1075 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
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()));
1085 DEF_TRAVERSE_TYPELOC(AdjustedType,
1086 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1088 DEF_TRAVERSE_TYPELOC(DecayedType,
1089 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1091 template <typename Derived>
1092 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1093 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1094 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1098 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1099 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1100 return TraverseArrayTypeLocHelper(TL);
1103 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1104 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1105 return TraverseArrayTypeLocHelper(TL);
1108 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1109 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1110 return TraverseArrayTypeLocHelper(TL);
1113 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1114 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1115 return TraverseArrayTypeLocHelper(TL);
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()));
1126 // FIXME: VectorTypeLoc is unfinished
1127 DEF_TRAVERSE_TYPELOC(VectorType, {
1128 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1131 // FIXME: size and attributes
1132 // FIXME: base VectorTypeLoc is unfinished
1133 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1134 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1137 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1138 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1140 // FIXME: location of exception specifications (attributes?)
1141 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1142 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1144 const FunctionProtoType *T = TL.getTypePtr();
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)));
1154 for (const auto &E : T->exceptions()) {
1155 TRY_TO(TraverseType(E));
1158 if (Expr *NE = T->getNoexceptExpr())
1159 TRY_TO(TraverseStmt(NE));
1162 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1163 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1165 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1166 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1168 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1169 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1172 // FIXME: location of underlying expr
1173 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1174 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1177 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1178 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1181 DEF_TRAVERSE_TYPELOC(AutoType, {
1182 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
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, {})
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)));
1199 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1201 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1203 DEF_TRAVERSE_TYPELOC(AttributedType,
1204 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1206 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1207 if (TL.getQualifierLoc()) {
1208 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1210 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1213 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1214 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1217 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1218 if (TL.getQualifierLoc()) {
1219 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1222 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1223 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1227 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1228 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1230 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1232 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1233 // We have to watch out here because an ObjCInterfaceType's base
1235 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1236 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1239 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1240 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1242 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1244 #undef DEF_TRAVERSE_TYPELOC
1246 // ----------------- Decl traversal -----------------
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
1253 template <typename Derived>
1254 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
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));
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)); \
1274 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1278 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
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()));
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.
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.
1303 DEF_TRAVERSE_DECL(EmptyDecl, {})
1305 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1306 { TRY_TO(TraverseStmt(D->getAsmString())); })
1308 DEF_TRAVERSE_DECL(ImportDecl, {})
1310 DEF_TRAVERSE_DECL(FriendDecl, {
1311 // Friend is either decl or a type.
1312 if (D->getFriendType())
1313 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1315 TRY_TO(TraverseDecl(D->getFriendDecl()));
1318 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1319 if (D->getFriendType())
1320 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
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));
1332 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1333 TRY_TO(TraverseDecl(D->getSpecialization()));
1335 if (D->hasExplicitTemplateArgs()) {
1336 const TemplateArgumentListInfo &args = D->templateArgs();
1337 TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1342 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1344 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1347 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1348 TRY_TO(TraverseStmt(D->getAssertExpr()));
1349 TRY_TO(TraverseStmt(D->getMessage()));
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().
1359 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1361 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1362 // We shouldn't traverse an aliased namespace, since it will be
1363 // defined (and, therefore, traversed) somewhere else.
1365 // This return statement makes sure the traversal of nodes in
1366 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1367 // is skipped - don't remove it.
1371 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1376 {// Code in an unnamed namespace shows up automatically in
1377 // decls_begin()/decls_end(). Thus we don't need to recurse on
1378 // D->getAnonymousNamespace().
1381 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1384 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1387 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1390 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1393 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1396 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1399 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1400 if (D->getReturnTypeSourceInfo()) {
1401 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1403 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1405 TRY_TO(TraverseDecl(*I));
1407 if (D->isThisDeclarationADefinition()) {
1408 TRY_TO(TraverseStmt(D->getBody()));
1413 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1414 if (D->getTypeSourceInfo())
1415 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1417 TRY_TO(TraverseType(D->getType()));
1421 DEF_TRAVERSE_DECL(UsingDecl, {
1422 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1423 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1426 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1427 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1430 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1432 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1433 for (auto *I : D->varlists()) {
1434 TRY_TO(TraverseStmt(I));
1438 // A helper method for TemplateDecl's children.
1439 template <typename Derived>
1440 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1441 TemplateParameterList *TPL) {
1443 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1445 TRY_TO(TraverseDecl(*I));
1451 template <typename Derived>
1452 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1453 ClassTemplateDecl *D) {
1454 for (auto *SD : D->specializations()) {
1455 for (auto *RD : SD->redecls()) {
1456 // We don't want to visit injected-class-names in this traversal.
1457 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1461 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1462 // Visit the implicit instantiations with the requested pattern.
1463 case TSK_Undeclared:
1464 case TSK_ImplicitInstantiation:
1465 TRY_TO(TraverseDecl(RD));
1468 // We don't need to do anything on an explicit instantiation
1469 // or explicit specialization because there will be an explicit
1470 // node for it elsewhere.
1471 case TSK_ExplicitInstantiationDeclaration:
1472 case TSK_ExplicitInstantiationDefinition:
1473 case TSK_ExplicitSpecialization:
1482 template <typename Derived>
1483 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1484 VarTemplateDecl *D) {
1485 for (auto *SD : D->specializations()) {
1486 for (auto *RD : SD->redecls()) {
1488 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1489 case TSK_Undeclared:
1490 case TSK_ImplicitInstantiation:
1491 TRY_TO(TraverseDecl(RD));
1494 case TSK_ExplicitInstantiationDeclaration:
1495 case TSK_ExplicitInstantiationDefinition:
1496 case TSK_ExplicitSpecialization:
1505 // A helper method for traversing the instantiations of a
1506 // function while skipping its specializations.
1507 template <typename Derived>
1508 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1509 FunctionTemplateDecl *D) {
1510 for (auto *FD : D->specializations()) {
1511 for (auto *RD : FD->redecls()) {
1512 switch (RD->getTemplateSpecializationKind()) {
1513 case TSK_Undeclared:
1514 case TSK_ImplicitInstantiation:
1515 // We don't know what kind of FunctionDecl this is.
1516 TRY_TO(TraverseDecl(RD));
1519 // FIXME: For now traverse explicit instantiations here. Change that
1520 // once they are represented as dedicated nodes in the AST.
1521 case TSK_ExplicitInstantiationDeclaration:
1522 case TSK_ExplicitInstantiationDefinition:
1523 TRY_TO(TraverseDecl(RD));
1526 case TSK_ExplicitSpecialization:
1535 // This macro unifies the traversal of class, variable and function
1536 // template declarations.
1537 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1538 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1539 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1540 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1542 /* By default, we do not traverse the instantiations of \
1543 class templates since they do not appear in the user code. The \
1544 following code optionally traverses them. \
1546 We only traverse the class instantiations when we see the canonical \
1547 declaration of the template, to ensure we only visit them once. */ \
1548 if (getDerived().shouldVisitTemplateInstantiations() && \
1549 D == D->getCanonicalDecl()) \
1550 TRY_TO(TraverseTemplateInstantiations(D)); \
1552 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1553 from a template instantiation back to the template from which \
1554 it was instantiated, and thus should not be traversed. */ \
1557 DEF_TRAVERSE_TMPL_DECL(Class)
1558 DEF_TRAVERSE_TMPL_DECL(Var)
1559 DEF_TRAVERSE_TMPL_DECL(Function)
1561 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1562 // D is the "T" in something like
1563 // template <template <typename> class T> class container { };
1564 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1565 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1566 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1568 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1571 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1572 // D is the "T" in something like "template<typename T> class vector;"
1573 if (D->getTypeForDecl())
1574 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1575 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1576 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1579 DEF_TRAVERSE_DECL(TypedefDecl, {
1580 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1581 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1582 // declaring the typedef, not something that was written in the
1586 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1587 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1588 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1589 // declaring the type alias, not something that was written in the
1593 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1594 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1595 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1598 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1599 // A dependent using declaration which was marked with 'typename'.
1600 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1601 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1602 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1603 // declaring the type, not something that was written in the
1607 DEF_TRAVERSE_DECL(EnumDecl, {
1608 if (D->getTypeForDecl())
1609 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1611 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1612 // The enumerators are already traversed by
1613 // decls_begin()/decls_end().
1616 // Helper methods for RecordDecl and its children.
1617 template <typename Derived>
1618 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1619 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1620 // declaring the type, not something that was written in the source.
1622 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1626 template <typename Derived>
1627 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1628 if (!TraverseRecordHelper(D))
1630 if (D->isCompleteDefinition()) {
1631 for (const auto &I : D->bases()) {
1632 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1634 // We don't traverse the friends or the conversions, as they are
1635 // already in decls_begin()/decls_end().
1640 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1642 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1644 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1645 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1646 /* For implicit instantiations ("set<int> x;"), we don't want to \
1647 recurse at all, since the instatiated template isn't written in \
1648 the source code anywhere. (Note the instatiated *type* -- \
1649 set<int> -- is written, and will still get a callback of \
1650 TemplateSpecializationType). For explicit instantiations \
1651 ("template set<int>;"), we do need a callback, since this \
1652 is the only callback that's made for this instantiation. \
1653 We use getTypeAsWritten() to distinguish. */ \
1654 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1655 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1657 if (!getDerived().shouldVisitTemplateInstantiations() && \
1658 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1659 /* Returning from here skips traversing the \
1660 declaration context of the *TemplateSpecializationDecl \
1661 (embedded in the DEF_TRAVERSE_DECL() macro) \
1662 which contains the instantiated members of the template. */ \
1666 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1667 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1669 template <typename Derived>
1670 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1671 const TemplateArgumentLoc *TAL, unsigned Count) {
1672 for (unsigned I = 0; I < Count; ++I) {
1673 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1678 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1679 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1680 /* The partial specialization. */ \
1681 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1682 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1684 TRY_TO(TraverseDecl(*I)); \
1687 /* The args that remains unspecialized. */ \
1688 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1689 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1690 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1692 /* Don't need the *TemplatePartialSpecializationHelper, even \
1693 though that's our parent class -- we already visit all the \
1694 template args here. */ \
1695 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1697 /* Instantiations will have been visited with the primary template. */ \
1700 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1701 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1703 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1705 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1706 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1707 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1708 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1709 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1712 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1714 template <typename Derived>
1715 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1716 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1717 if (D->getTypeSourceInfo())
1718 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1720 TRY_TO(TraverseType(D->getType()));
1724 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1726 DEF_TRAVERSE_DECL(FieldDecl, {
1727 TRY_TO(TraverseDeclaratorHelper(D));
1728 if (D->isBitField())
1729 TRY_TO(TraverseStmt(D->getBitWidth()));
1730 else if (D->hasInClassInitializer())
1731 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1734 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1735 TRY_TO(TraverseDeclaratorHelper(D));
1736 if (D->isBitField())
1737 TRY_TO(TraverseStmt(D->getBitWidth()));
1738 // FIXME: implement the rest.
1741 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1742 TRY_TO(TraverseDeclaratorHelper(D));
1743 if (D->isBitField())
1744 TRY_TO(TraverseStmt(D->getBitWidth()));
1745 // FIXME: implement the rest.
1748 template <typename Derived>
1749 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1750 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1751 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1753 // If we're an explicit template specialization, iterate over the
1754 // template args that were explicitly specified. If we were doing
1755 // this in typing order, we'd do it between the return type and
1756 // the function args, but both are handled by the FunctionTypeLoc
1757 // above, so we have to choose one side. I've decided to do before.
1758 if (const FunctionTemplateSpecializationInfo *FTSI =
1759 D->getTemplateSpecializationInfo()) {
1760 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1761 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1762 // A specialization might not have explicit template arguments if it has
1763 // a templated return type and concrete arguments.
1764 if (const ASTTemplateArgumentListInfo *TALI =
1765 FTSI->TemplateArgumentsAsWritten) {
1766 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1767 TALI->NumTemplateArgs));
1772 // Visit the function type itself, which can be either
1773 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1774 // also covers the return type and the function parameters,
1775 // including exception specifications.
1776 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1777 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1778 } else if (getDerived().shouldVisitImplicitCode()) {
1779 // Visit parameter variable declarations of the implicit function
1780 // if the traverser is visiting implicit code. Parameter variable
1781 // declarations do not have valid TypeSourceInfo, so to visit them
1782 // we need to traverse the declarations explicitly.
1783 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1786 TRY_TO(TraverseDecl(*I));
1789 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1790 // Constructor initializers.
1791 for (auto *I : Ctor->inits()) {
1792 TRY_TO(TraverseConstructorInitializer(I));
1796 if (D->isThisDeclarationADefinition()) {
1797 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1802 DEF_TRAVERSE_DECL(FunctionDecl, {
1803 // We skip decls_begin/decls_end, which are already covered by
1804 // TraverseFunctionHelper().
1805 return TraverseFunctionHelper(D);
1808 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1809 // We skip decls_begin/decls_end, which are already covered by
1810 // TraverseFunctionHelper().
1811 return TraverseFunctionHelper(D);
1814 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1815 // We skip decls_begin/decls_end, which are already covered by
1816 // TraverseFunctionHelper().
1817 return TraverseFunctionHelper(D);
1820 // CXXConversionDecl is the declaration of a type conversion operator.
1821 // It's not a cast expression.
1822 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1823 // We skip decls_begin/decls_end, which are already covered by
1824 // TraverseFunctionHelper().
1825 return TraverseFunctionHelper(D);
1828 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1829 // We skip decls_begin/decls_end, which are already covered by
1830 // TraverseFunctionHelper().
1831 return TraverseFunctionHelper(D);
1834 template <typename Derived>
1835 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1836 TRY_TO(TraverseDeclaratorHelper(D));
1837 // Default params are taken care of when we traverse the ParmVarDecl.
1838 if (!isa<ParmVarDecl>(D) &&
1839 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1840 TRY_TO(TraverseStmt(D->getInit()));
1844 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1846 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1848 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1849 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1850 TRY_TO(TraverseDeclaratorHelper(D));
1851 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1852 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1855 DEF_TRAVERSE_DECL(ParmVarDecl, {
1856 TRY_TO(TraverseVarHelper(D));
1858 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1859 !D->hasUnparsedDefaultArg())
1860 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1862 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1863 !D->hasUnparsedDefaultArg())
1864 TRY_TO(TraverseStmt(D->getDefaultArg()));
1867 #undef DEF_TRAVERSE_DECL
1869 // ----------------- Stmt traversal -----------------
1871 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1872 // over the children defined in children() (every stmt defines these,
1873 // though sometimes the range is empty). Each individual Traverse*
1874 // method only needs to worry about children other than those. To see
1875 // what children() does for a given class, see, e.g.,
1876 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1878 // This macro makes available a variable S, the passed-in stmt.
1879 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1880 template <typename Derived> \
1881 bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) { \
1882 TRY_TO(WalkUpFrom##STMT(S)); \
1884 for (Stmt::child_range range = S->children(); range; ++range) { \
1885 TRY_TO(TraverseStmt(*range)); \
1890 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1891 TRY_TO(TraverseStmt(S->getAsmString()));
1892 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1893 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1895 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1896 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1898 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1899 TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1901 // children() iterates over inputExpr and outputExpr.
1906 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1907 // added this needs to be implemented.
1910 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1911 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1912 // children() iterates over the handler block.
1915 DEF_TRAVERSE_STMT(DeclStmt, {
1916 for (auto *I : S->decls()) {
1917 TRY_TO(TraverseDecl(I));
1919 // Suppress the default iteration over children() by
1920 // returning. Here's why: A DeclStmt looks like 'type var [=
1921 // initializer]'. The decls above already traverse over the
1922 // initializers, so we don't have to do it again (which
1923 // children() would do).
1927 // These non-expr stmts (most of them), do not need any action except
1928 // iterating over the children.
1929 DEF_TRAVERSE_STMT(BreakStmt, {})
1930 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1931 DEF_TRAVERSE_STMT(CaseStmt, {})
1932 DEF_TRAVERSE_STMT(CompoundStmt, {})
1933 DEF_TRAVERSE_STMT(ContinueStmt, {})
1934 DEF_TRAVERSE_STMT(DefaultStmt, {})
1935 DEF_TRAVERSE_STMT(DoStmt, {})
1936 DEF_TRAVERSE_STMT(ForStmt, {})
1937 DEF_TRAVERSE_STMT(GotoStmt, {})
1938 DEF_TRAVERSE_STMT(IfStmt, {})
1939 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1940 DEF_TRAVERSE_STMT(LabelStmt, {})
1941 DEF_TRAVERSE_STMT(AttributedStmt, {})
1942 DEF_TRAVERSE_STMT(NullStmt, {})
1943 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1944 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1945 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1946 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1947 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1948 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1949 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1950 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1951 if (!getDerived().shouldVisitImplicitCode()) {
1952 TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1953 TRY_TO(TraverseStmt(S->getRangeInit()));
1954 TRY_TO(TraverseStmt(S->getBody()));
1955 // Visit everything else only if shouldVisitImplicitCode().
1959 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1960 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1961 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1963 DEF_TRAVERSE_STMT(ReturnStmt, {})
1964 DEF_TRAVERSE_STMT(SwitchStmt, {})
1965 DEF_TRAVERSE_STMT(WhileStmt, {})
1967 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1968 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1969 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1970 if (S->hasExplicitTemplateArgs()) {
1971 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1972 S->getNumTemplateArgs()));
1976 DEF_TRAVERSE_STMT(DeclRefExpr, {
1977 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1978 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1979 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1980 S->getNumTemplateArgs()));
1983 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1984 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1985 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1986 if (S->hasExplicitTemplateArgs()) {
1987 TRY_TO(TraverseTemplateArgumentLocsHelper(
1988 S->getExplicitTemplateArgs().getTemplateArgs(),
1989 S->getNumTemplateArgs()));
1993 DEF_TRAVERSE_STMT(MemberExpr, {
1994 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1995 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1996 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1997 S->getNumTemplateArgs()));
2002 {// We don't traverse the cast type, as it's not written in the
2006 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2007 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2010 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2011 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2014 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2015 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2018 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2019 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2022 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2023 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2026 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2027 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2030 // InitListExpr is a tricky one, because we want to do all our work on
2031 // the syntactic form of the listexpr, but this method takes the
2032 // semantic form by default. We can't use the macro helper because it
2033 // calls WalkUp*() on the semantic form, before our code can convert
2034 // to the syntactic form.
2035 template <typename Derived>
2036 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
2037 InitListExpr *Syn = S->isSemanticForm() ? S->getSyntacticForm() : S;
2039 TRY_TO(WalkUpFromInitListExpr(Syn));
2040 // All we need are the default actions. FIXME: use a helper function.
2041 for (Stmt::child_range range = Syn->children(); range; ++range) {
2042 TRY_TO(TraverseStmt(*range));
2045 InitListExpr *Sem = S->isSemanticForm() ? S : S->getSemanticForm();
2047 TRY_TO(WalkUpFromInitListExpr(Sem));
2048 for (Stmt::child_range range = Sem->children(); range; ++range) {
2049 TRY_TO(TraverseStmt(*range));
2055 // GenericSelectionExpr is a special case because the types and expressions
2056 // are interleaved. We also need to watch out for null types (default
2057 // generic associations).
2058 template <typename Derived>
2059 bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr(
2060 GenericSelectionExpr *S) {
2061 TRY_TO(WalkUpFromGenericSelectionExpr(S));
2062 TRY_TO(TraverseStmt(S->getControllingExpr()));
2063 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2064 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2065 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2066 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2071 // PseudoObjectExpr is a special case because of the wierdness with
2072 // syntactic expressions and opaque values.
2073 template <typename Derived>
2075 RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2076 TRY_TO(WalkUpFromPseudoObjectExpr(S));
2077 TRY_TO(TraverseStmt(S->getSyntacticForm()));
2078 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2079 e = S->semantics_end();
2082 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2083 sub = OVE->getSourceExpr();
2084 TRY_TO(TraverseStmt(sub));
2089 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2090 // This is called for code like 'return T()' where T is a built-in
2091 // (i.e. non-class) type.
2092 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2095 DEF_TRAVERSE_STMT(CXXNewExpr, {
2096 // The child-iterator will pick up the other arguments.
2097 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2100 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2101 // The child-iterator will pick up the expression representing
2103 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2104 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2105 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2108 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2109 // The child-iterator will pick up the arg if it's an expression,
2110 // but not if it's a type.
2111 if (S->isArgumentType())
2112 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2115 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2116 // The child-iterator will pick up the arg if it's an expression,
2117 // but not if it's a type.
2118 if (S->isTypeOperand())
2119 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2122 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2123 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2126 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2127 // The child-iterator will pick up the arg if it's an expression,
2128 // but not if it's a type.
2129 if (S->isTypeOperand())
2130 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2133 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2134 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2135 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2138 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2139 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2142 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2143 { TRY_TO(TraverseStmt(S->getQueriedExpression())); })
2145 DEF_TRAVERSE_STMT(VAArgExpr, {
2146 // The child-iterator will pick up the expression argument.
2147 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2150 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2151 // This is called for code like 'return T()' where T is a class type.
2152 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2155 // Walk only the visible parts of lambda expressions.
2156 template <typename Derived>
2157 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2158 TRY_TO(WalkUpFromLambdaExpr(S));
2160 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2161 CEnd = S->explicit_capture_end();
2163 TRY_TO(TraverseLambdaCapture(S, C));
2166 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2167 FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2169 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2170 // Visit the whole type.
2171 TRY_TO(TraverseTypeLoc(TL));
2173 if (S->hasExplicitParameters()) {
2174 // Visit parameters.
2175 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2176 TRY_TO(TraverseDecl(Proto.getParam(I)));
2178 } else if (S->hasExplicitResultType()) {
2179 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2182 auto *T = Proto.getTypePtr();
2183 for (const auto &E : T->exceptions()) {
2184 TRY_TO(TraverseType(E));
2187 if (Expr *NE = T->getNoexceptExpr())
2188 TRY_TO(TraverseStmt(NE));
2191 TRY_TO(TraverseLambdaBody(S));
2195 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2196 // This is called for code like 'T()', where T is a template argument.
2197 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2200 // These expressions all might take explicit template arguments.
2201 // We traverse those if so. FIXME: implement these.
2202 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2203 DEF_TRAVERSE_STMT(CallExpr, {})
2204 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2206 // These exprs (most of them), do not need any action except iterating
2207 // over the children.
2208 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2209 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2210 DEF_TRAVERSE_STMT(BlockExpr, {
2211 TRY_TO(TraverseDecl(S->getBlockDecl()));
2212 return true; // no child statements to loop through.
2214 DEF_TRAVERSE_STMT(ChooseExpr, {})
2215 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2216 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2218 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2219 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2220 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2221 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2222 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2223 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2224 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2225 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2226 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2227 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2228 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2229 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2230 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2231 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2233 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2234 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2235 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2236 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2237 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2238 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2239 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2240 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2241 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2242 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2243 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2245 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2246 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2247 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2248 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2249 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2251 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2252 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2253 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2254 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2255 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2256 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2257 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2259 DEF_TRAVERSE_STMT(ParenExpr, {})
2260 DEF_TRAVERSE_STMT(ParenListExpr, {})
2261 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2262 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2263 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2264 DEF_TRAVERSE_STMT(StmtExpr, {})
2265 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2266 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2267 if (S->hasExplicitTemplateArgs()) {
2268 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2269 S->getNumTemplateArgs()));
2273 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2274 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2275 if (S->hasExplicitTemplateArgs()) {
2276 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2277 S->getNumTemplateArgs()));
2281 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2282 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2283 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2284 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2285 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2287 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2288 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2289 DEF_TRAVERSE_STMT(TypoExpr, {})
2290 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2292 // These operators (all of them) do not need any action except
2293 // iterating over the children.
2294 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2295 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2296 DEF_TRAVERSE_STMT(UnaryOperator, {})
2297 DEF_TRAVERSE_STMT(BinaryOperator, {})
2298 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2299 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2300 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2301 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2302 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2303 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2304 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2305 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2306 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2307 DEF_TRAVERSE_STMT(AtomicExpr, {})
2309 // These literals (all of them) do not need any action.
2310 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2311 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2312 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2313 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2314 DEF_TRAVERSE_STMT(StringLiteral, {})
2315 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2316 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2317 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2318 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2320 // Traverse OpenCL: AsType, Convert.
2321 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2323 // OpenMP directives.
2324 template <typename Derived>
2325 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2326 OMPExecutableDirective *S) {
2327 for (auto *C : S->clauses()) {
2328 TRY_TO(TraverseOMPClause(C));
2333 template <typename Derived>
2335 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2336 return TraverseOMPExecutableDirective(S);
2339 DEF_TRAVERSE_STMT(OMPParallelDirective,
2340 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2342 DEF_TRAVERSE_STMT(OMPSimdDirective,
2343 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2345 DEF_TRAVERSE_STMT(OMPForDirective,
2346 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2348 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2349 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2351 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2352 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2354 DEF_TRAVERSE_STMT(OMPSectionDirective,
2355 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2357 DEF_TRAVERSE_STMT(OMPSingleDirective,
2358 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2360 DEF_TRAVERSE_STMT(OMPMasterDirective,
2361 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2363 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2364 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2365 TRY_TO(TraverseOMPExecutableDirective(S));
2368 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2369 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2371 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2372 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2374 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2375 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2377 DEF_TRAVERSE_STMT(OMPTaskDirective,
2378 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2380 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2381 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2383 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2384 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2386 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2387 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2389 DEF_TRAVERSE_STMT(OMPFlushDirective,
2390 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2392 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2393 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2395 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2396 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2398 DEF_TRAVERSE_STMT(OMPTargetDirective,
2399 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2401 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2402 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2405 template <typename Derived>
2406 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2409 switch (C->getClauseKind()) {
2410 #define OPENMP_CLAUSE(Name, Class) \
2412 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2414 #include "clang/Basic/OpenMPKinds.def"
2415 case OMPC_threadprivate:
2422 template <typename Derived>
2423 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2424 TRY_TO(TraverseStmt(C->getCondition()));
2428 template <typename Derived>
2429 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2430 TRY_TO(TraverseStmt(C->getCondition()));
2434 template <typename Derived>
2436 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2437 TRY_TO(TraverseStmt(C->getNumThreads()));
2441 template <typename Derived>
2442 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2443 TRY_TO(TraverseStmt(C->getSafelen()));
2447 template <typename Derived>
2449 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2450 TRY_TO(TraverseStmt(C->getNumForLoops()));
2454 template <typename Derived>
2455 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2459 template <typename Derived>
2460 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2464 template <typename Derived>
2466 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2467 TRY_TO(TraverseStmt(C->getChunkSize()));
2468 TRY_TO(TraverseStmt(C->getHelperChunkSize()));
2472 template <typename Derived>
2473 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) {
2477 template <typename Derived>
2478 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2482 template <typename Derived>
2483 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2487 template <typename Derived>
2489 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2493 template <typename Derived>
2494 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2498 template <typename Derived>
2499 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2503 template <typename Derived>
2504 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2508 template <typename Derived>
2509 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2513 template <typename Derived>
2514 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2518 template <typename Derived>
2519 template <typename T>
2520 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2521 for (auto *E : Node->varlists()) {
2522 TRY_TO(TraverseStmt(E));
2527 template <typename Derived>
2528 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2529 TRY_TO(VisitOMPClauseList(C));
2530 for (auto *E : C->private_copies()) {
2531 TRY_TO(TraverseStmt(E));
2536 template <typename Derived>
2537 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2538 OMPFirstprivateClause *C) {
2539 TRY_TO(VisitOMPClauseList(C));
2540 for (auto *E : C->private_copies()) {
2541 TRY_TO(TraverseStmt(E));
2543 for (auto *E : C->inits()) {
2544 TRY_TO(TraverseStmt(E));
2549 template <typename Derived>
2550 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2551 OMPLastprivateClause *C) {
2552 TRY_TO(VisitOMPClauseList(C));
2553 for (auto *E : C->private_copies()) {
2554 TRY_TO(TraverseStmt(E));
2556 for (auto *E : C->source_exprs()) {
2557 TRY_TO(TraverseStmt(E));
2559 for (auto *E : C->destination_exprs()) {
2560 TRY_TO(TraverseStmt(E));
2562 for (auto *E : C->assignment_ops()) {
2563 TRY_TO(TraverseStmt(E));
2568 template <typename Derived>
2569 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2570 TRY_TO(VisitOMPClauseList(C));
2574 template <typename Derived>
2575 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2576 TRY_TO(TraverseStmt(C->getStep()));
2577 TRY_TO(TraverseStmt(C->getCalcStep()));
2578 TRY_TO(VisitOMPClauseList(C));
2579 for (auto *E : C->inits()) {
2580 TRY_TO(TraverseStmt(E));
2582 for (auto *E : C->updates()) {
2583 TRY_TO(TraverseStmt(E));
2585 for (auto *E : C->finals()) {
2586 TRY_TO(TraverseStmt(E));
2591 template <typename Derived>
2592 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2593 TRY_TO(TraverseStmt(C->getAlignment()));
2594 TRY_TO(VisitOMPClauseList(C));
2598 template <typename Derived>
2599 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2600 TRY_TO(VisitOMPClauseList(C));
2601 for (auto *E : C->source_exprs()) {
2602 TRY_TO(TraverseStmt(E));
2604 for (auto *E : C->destination_exprs()) {
2605 TRY_TO(TraverseStmt(E));
2607 for (auto *E : C->assignment_ops()) {
2608 TRY_TO(TraverseStmt(E));
2613 template <typename Derived>
2614 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2615 OMPCopyprivateClause *C) {
2616 TRY_TO(VisitOMPClauseList(C));
2617 for (auto *E : C->source_exprs()) {
2618 TRY_TO(TraverseStmt(E));
2620 for (auto *E : C->destination_exprs()) {
2621 TRY_TO(TraverseStmt(E));
2623 for (auto *E : C->assignment_ops()) {
2624 TRY_TO(TraverseStmt(E));
2629 template <typename Derived>
2631 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2632 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2633 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2634 TRY_TO(VisitOMPClauseList(C));
2635 for (auto *E : C->lhs_exprs()) {
2636 TRY_TO(TraverseStmt(E));
2638 for (auto *E : C->rhs_exprs()) {
2639 TRY_TO(TraverseStmt(E));
2641 for (auto *E : C->reduction_ops()) {
2642 TRY_TO(TraverseStmt(E));
2647 template <typename Derived>
2648 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2649 TRY_TO(VisitOMPClauseList(C));
2653 // FIXME: look at the following tricky-seeming exprs to see if we
2654 // need to recurse on anything. These are ones that have methods
2655 // returning decls or qualtypes or nestednamespecifier -- though I'm
2656 // not sure if they own them -- or just seemed very complicated, or
2657 // had lots of sub-types to explore.
2659 // VisitOverloadExpr and its children: recurse on template args? etc?
2661 // FIXME: go through all the stmts and exprs again, and see which of them
2662 // create new types, and recurse on the types (TypeLocs?) of those.
2665 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2666 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2667 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2668 // Every class that has getQualifier.
2670 #undef DEF_TRAVERSE_STMT
2674 } // end namespace clang
2676 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H