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()));
1011 for (auto typeArg : T->getTypeArgsAsWritten()) {
1012 TRY_TO(TraverseType(typeArg));
1016 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1017 { TRY_TO(TraverseType(T->getPointeeType())); })
1019 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1021 #undef DEF_TRAVERSE_TYPE
1023 // ----------------- TypeLoc traversal -----------------
1025 // This macro makes available a variable TL, the passed-in TypeLoc.
1026 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1027 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1028 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1029 // continue to work.
1030 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
1031 template <typename Derived> \
1032 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
1033 if (getDerived().shouldWalkTypesOfTypeLocs()) \
1034 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
1035 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1040 template <typename Derived>
1042 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1043 // Move this over to the 'main' typeloc tree. Note that this is a
1044 // move -- we pretend that we were really looking at the unqualified
1045 // typeloc all along -- rather than a recursion, so we don't follow
1046 // the normal CRTP plan of going through
1047 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1048 // twice for the same type (once as a QualifiedTypeLoc version of
1049 // the type, once as an UnqualifiedTypeLoc version of the type),
1050 // which in effect means we'd call VisitTypeLoc twice with the
1051 // 'same' type. This solves that problem, at the cost of never
1052 // seeing the qualified version of the type (unless the client
1053 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1054 // perfect solution. A perfect solution probably requires making
1055 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1056 // wrapper around Type* -- rather than being its own class in the
1058 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1061 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1063 // FIXME: ComplexTypeLoc is unfinished
1064 DEF_TRAVERSE_TYPELOC(ComplexType, {
1065 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1068 DEF_TRAVERSE_TYPELOC(PointerType,
1069 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1071 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1072 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1074 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1075 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1077 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1078 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1080 // FIXME: location of base class?
1081 // We traverse this in the type case as well, but how is it not reached through
1082 // the pointee type?
1083 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1084 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1085 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1088 DEF_TRAVERSE_TYPELOC(AdjustedType,
1089 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1091 DEF_TRAVERSE_TYPELOC(DecayedType,
1092 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1094 template <typename Derived>
1095 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1096 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1097 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1101 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1102 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1103 return TraverseArrayTypeLocHelper(TL);
1106 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1107 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1108 return TraverseArrayTypeLocHelper(TL);
1111 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1112 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1113 return TraverseArrayTypeLocHelper(TL);
1116 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1117 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1118 return TraverseArrayTypeLocHelper(TL);
1121 // FIXME: order? why not size expr first?
1122 // FIXME: base VectorTypeLoc is unfinished
1123 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1124 if (TL.getTypePtr()->getSizeExpr())
1125 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1126 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1129 // FIXME: VectorTypeLoc is unfinished
1130 DEF_TRAVERSE_TYPELOC(VectorType, {
1131 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1134 // FIXME: size and attributes
1135 // FIXME: base VectorTypeLoc is unfinished
1136 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1137 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1140 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1141 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1143 // FIXME: location of exception specifications (attributes?)
1144 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1145 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1147 const FunctionProtoType *T = TL.getTypePtr();
1149 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1150 if (TL.getParam(I)) {
1151 TRY_TO(TraverseDecl(TL.getParam(I)));
1152 } else if (I < T->getNumParams()) {
1153 TRY_TO(TraverseType(T->getParamType(I)));
1157 for (const auto &E : T->exceptions()) {
1158 TRY_TO(TraverseType(E));
1161 if (Expr *NE = T->getNoexceptExpr())
1162 TRY_TO(TraverseStmt(NE));
1165 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1166 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1168 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1169 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1171 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1172 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1175 // FIXME: location of underlying expr
1176 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1177 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1180 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1181 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1184 DEF_TRAVERSE_TYPELOC(AutoType, {
1185 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1188 DEF_TRAVERSE_TYPELOC(RecordType, {})
1189 DEF_TRAVERSE_TYPELOC(EnumType, {})
1190 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1191 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {})
1192 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {})
1194 // FIXME: use the loc for the template name?
1195 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1196 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1197 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1198 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1202 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1204 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1206 DEF_TRAVERSE_TYPELOC(AttributedType,
1207 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1209 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1210 if (TL.getQualifierLoc()) {
1211 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1213 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1216 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1217 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1220 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1221 if (TL.getQualifierLoc()) {
1222 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1225 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1226 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1230 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1231 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1233 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1235 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1236 // We have to watch out here because an ObjCInterfaceType's base
1238 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1239 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1240 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1241 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1244 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1245 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1247 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1249 #undef DEF_TRAVERSE_TYPELOC
1251 // ----------------- Decl traversal -----------------
1253 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1254 // the children that come from the DeclContext associated with it.
1255 // Therefore each Traverse* only needs to worry about children other
1258 template <typename Derived>
1259 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1263 for (auto *Child : DC->decls()) {
1264 // BlockDecls and CapturedDecls are traversed through BlockExprs and
1265 // CapturedStmts respectively.
1266 if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child))
1267 TRY_TO(TraverseDecl(Child));
1273 // This macro makes available a variable D, the passed-in decl.
1274 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1275 template <typename Derived> \
1276 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \
1277 TRY_TO(WalkUpFrom##DECL(D)); \
1279 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1283 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1285 DEF_TRAVERSE_DECL(BlockDecl, {
1286 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1287 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1288 TRY_TO(TraverseStmt(D->getBody()));
1289 for (const auto &I : D->captures()) {
1290 if (I.hasCopyExpr()) {
1291 TRY_TO(TraverseStmt(I.getCopyExpr()));
1294 // This return statement makes sure the traversal of nodes in
1295 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1296 // is skipped - don't remove it.
1300 DEF_TRAVERSE_DECL(CapturedDecl, {
1301 TRY_TO(TraverseStmt(D->getBody()));
1302 // This return statement makes sure the traversal of nodes in
1303 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1304 // is skipped - don't remove it.
1308 DEF_TRAVERSE_DECL(EmptyDecl, {})
1310 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1311 { TRY_TO(TraverseStmt(D->getAsmString())); })
1313 DEF_TRAVERSE_DECL(ImportDecl, {})
1315 DEF_TRAVERSE_DECL(FriendDecl, {
1316 // Friend is either decl or a type.
1317 if (D->getFriendType())
1318 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1320 TRY_TO(TraverseDecl(D->getFriendDecl()));
1323 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1324 if (D->getFriendType())
1325 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1327 TRY_TO(TraverseDecl(D->getFriendDecl()));
1328 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1329 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1330 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1331 ITPL != ETPL; ++ITPL) {
1332 TRY_TO(TraverseDecl(*ITPL));
1337 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1338 TRY_TO(TraverseDecl(D->getSpecialization()));
1340 if (D->hasExplicitTemplateArgs()) {
1341 const TemplateArgumentListInfo &args = D->templateArgs();
1342 TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1347 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1349 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1352 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1353 TRY_TO(TraverseStmt(D->getAssertExpr()));
1354 TRY_TO(TraverseStmt(D->getMessage()));
1358 TranslationUnitDecl,
1359 {// Code in an unnamed namespace shows up automatically in
1360 // decls_begin()/decls_end(). Thus we don't need to recurse on
1361 // D->getAnonymousNamespace().
1364 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1366 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1367 // We shouldn't traverse an aliased namespace, since it will be
1368 // defined (and, therefore, traversed) somewhere else.
1370 // This return statement makes sure the traversal of nodes in
1371 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1372 // is skipped - don't remove it.
1376 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1381 {// Code in an unnamed namespace shows up automatically in
1382 // decls_begin()/decls_end(). Thus we don't need to recurse on
1383 // D->getAnonymousNamespace().
1386 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1389 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1390 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1391 for (auto typeParam : *typeParamList) {
1392 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1397 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1400 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1403 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1404 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1405 for (auto typeParam : *typeParamList) {
1406 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1410 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1411 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1415 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1418 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1419 if (D->getReturnTypeSourceInfo()) {
1420 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1422 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1424 TRY_TO(TraverseDecl(*I));
1426 if (D->isThisDeclarationADefinition()) {
1427 TRY_TO(TraverseStmt(D->getBody()));
1432 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1433 if (D->hasExplicitBound()) {
1434 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1435 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1436 // declaring the type alias, not something that was written in the
1441 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1442 if (D->getTypeSourceInfo())
1443 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1445 TRY_TO(TraverseType(D->getType()));
1449 DEF_TRAVERSE_DECL(UsingDecl, {
1450 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1451 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1454 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1455 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1458 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1460 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1461 for (auto *I : D->varlists()) {
1462 TRY_TO(TraverseStmt(I));
1466 // A helper method for TemplateDecl's children.
1467 template <typename Derived>
1468 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1469 TemplateParameterList *TPL) {
1471 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1473 TRY_TO(TraverseDecl(*I));
1479 template <typename Derived>
1480 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1481 ClassTemplateDecl *D) {
1482 for (auto *SD : D->specializations()) {
1483 for (auto *RD : SD->redecls()) {
1484 // We don't want to visit injected-class-names in this traversal.
1485 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1489 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1490 // Visit the implicit instantiations with the requested pattern.
1491 case TSK_Undeclared:
1492 case TSK_ImplicitInstantiation:
1493 TRY_TO(TraverseDecl(RD));
1496 // We don't need to do anything on an explicit instantiation
1497 // or explicit specialization because there will be an explicit
1498 // node for it elsewhere.
1499 case TSK_ExplicitInstantiationDeclaration:
1500 case TSK_ExplicitInstantiationDefinition:
1501 case TSK_ExplicitSpecialization:
1510 template <typename Derived>
1511 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1512 VarTemplateDecl *D) {
1513 for (auto *SD : D->specializations()) {
1514 for (auto *RD : SD->redecls()) {
1516 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1517 case TSK_Undeclared:
1518 case TSK_ImplicitInstantiation:
1519 TRY_TO(TraverseDecl(RD));
1522 case TSK_ExplicitInstantiationDeclaration:
1523 case TSK_ExplicitInstantiationDefinition:
1524 case TSK_ExplicitSpecialization:
1533 // A helper method for traversing the instantiations of a
1534 // function while skipping its specializations.
1535 template <typename Derived>
1536 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1537 FunctionTemplateDecl *D) {
1538 for (auto *FD : D->specializations()) {
1539 for (auto *RD : FD->redecls()) {
1540 switch (RD->getTemplateSpecializationKind()) {
1541 case TSK_Undeclared:
1542 case TSK_ImplicitInstantiation:
1543 // We don't know what kind of FunctionDecl this is.
1544 TRY_TO(TraverseDecl(RD));
1547 // FIXME: For now traverse explicit instantiations here. Change that
1548 // once they are represented as dedicated nodes in the AST.
1549 case TSK_ExplicitInstantiationDeclaration:
1550 case TSK_ExplicitInstantiationDefinition:
1551 TRY_TO(TraverseDecl(RD));
1554 case TSK_ExplicitSpecialization:
1563 // This macro unifies the traversal of class, variable and function
1564 // template declarations.
1565 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1566 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1567 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1568 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1570 /* By default, we do not traverse the instantiations of \
1571 class templates since they do not appear in the user code. The \
1572 following code optionally traverses them. \
1574 We only traverse the class instantiations when we see the canonical \
1575 declaration of the template, to ensure we only visit them once. */ \
1576 if (getDerived().shouldVisitTemplateInstantiations() && \
1577 D == D->getCanonicalDecl()) \
1578 TRY_TO(TraverseTemplateInstantiations(D)); \
1580 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1581 from a template instantiation back to the template from which \
1582 it was instantiated, and thus should not be traversed. */ \
1585 DEF_TRAVERSE_TMPL_DECL(Class)
1586 DEF_TRAVERSE_TMPL_DECL(Var)
1587 DEF_TRAVERSE_TMPL_DECL(Function)
1589 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1590 // D is the "T" in something like
1591 // template <template <typename> class T> class container { };
1592 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1593 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1594 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1596 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1599 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1600 // D is the "T" in something like "template<typename T> class vector;"
1601 if (D->getTypeForDecl())
1602 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1603 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1604 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1607 DEF_TRAVERSE_DECL(TypedefDecl, {
1608 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1609 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1610 // declaring the typedef, not something that was written in the
1614 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1615 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1616 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1617 // declaring the type alias, not something that was written in the
1621 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1622 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1623 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1626 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1627 // A dependent using declaration which was marked with 'typename'.
1628 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1629 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1630 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1631 // declaring the type, not something that was written in the
1635 DEF_TRAVERSE_DECL(EnumDecl, {
1636 if (D->getTypeForDecl())
1637 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1639 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1640 // The enumerators are already traversed by
1641 // decls_begin()/decls_end().
1644 // Helper methods for RecordDecl and its children.
1645 template <typename Derived>
1646 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1647 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1648 // declaring the type, not something that was written in the source.
1650 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1654 template <typename Derived>
1655 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1656 if (!TraverseRecordHelper(D))
1658 if (D->isCompleteDefinition()) {
1659 for (const auto &I : D->bases()) {
1660 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1662 // We don't traverse the friends or the conversions, as they are
1663 // already in decls_begin()/decls_end().
1668 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1670 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1672 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1673 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1674 /* For implicit instantiations ("set<int> x;"), we don't want to \
1675 recurse at all, since the instatiated template isn't written in \
1676 the source code anywhere. (Note the instatiated *type* -- \
1677 set<int> -- is written, and will still get a callback of \
1678 TemplateSpecializationType). For explicit instantiations \
1679 ("template set<int>;"), we do need a callback, since this \
1680 is the only callback that's made for this instantiation. \
1681 We use getTypeAsWritten() to distinguish. */ \
1682 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1683 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1685 if (!getDerived().shouldVisitTemplateInstantiations() && \
1686 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1687 /* Returning from here skips traversing the \
1688 declaration context of the *TemplateSpecializationDecl \
1689 (embedded in the DEF_TRAVERSE_DECL() macro) \
1690 which contains the instantiated members of the template. */ \
1694 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1695 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1697 template <typename Derived>
1698 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1699 const TemplateArgumentLoc *TAL, unsigned Count) {
1700 for (unsigned I = 0; I < Count; ++I) {
1701 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1706 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1707 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1708 /* The partial specialization. */ \
1709 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1710 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1712 TRY_TO(TraverseDecl(*I)); \
1715 /* The args that remains unspecialized. */ \
1716 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1717 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1718 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1720 /* Don't need the *TemplatePartialSpecializationHelper, even \
1721 though that's our parent class -- we already visit all the \
1722 template args here. */ \
1723 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1725 /* Instantiations will have been visited with the primary template. */ \
1728 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1729 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1731 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1733 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1734 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1735 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1736 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1737 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1740 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1742 template <typename Derived>
1743 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1744 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1745 if (D->getTypeSourceInfo())
1746 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1748 TRY_TO(TraverseType(D->getType()));
1752 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1754 DEF_TRAVERSE_DECL(FieldDecl, {
1755 TRY_TO(TraverseDeclaratorHelper(D));
1756 if (D->isBitField())
1757 TRY_TO(TraverseStmt(D->getBitWidth()));
1758 else if (D->hasInClassInitializer())
1759 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1762 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1763 TRY_TO(TraverseDeclaratorHelper(D));
1764 if (D->isBitField())
1765 TRY_TO(TraverseStmt(D->getBitWidth()));
1766 // FIXME: implement the rest.
1769 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1770 TRY_TO(TraverseDeclaratorHelper(D));
1771 if (D->isBitField())
1772 TRY_TO(TraverseStmt(D->getBitWidth()));
1773 // FIXME: implement the rest.
1776 template <typename Derived>
1777 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1778 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1779 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1781 // If we're an explicit template specialization, iterate over the
1782 // template args that were explicitly specified. If we were doing
1783 // this in typing order, we'd do it between the return type and
1784 // the function args, but both are handled by the FunctionTypeLoc
1785 // above, so we have to choose one side. I've decided to do before.
1786 if (const FunctionTemplateSpecializationInfo *FTSI =
1787 D->getTemplateSpecializationInfo()) {
1788 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1789 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1790 // A specialization might not have explicit template arguments if it has
1791 // a templated return type and concrete arguments.
1792 if (const ASTTemplateArgumentListInfo *TALI =
1793 FTSI->TemplateArgumentsAsWritten) {
1794 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1795 TALI->NumTemplateArgs));
1800 // Visit the function type itself, which can be either
1801 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1802 // also covers the return type and the function parameters,
1803 // including exception specifications.
1804 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1805 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1806 } else if (getDerived().shouldVisitImplicitCode()) {
1807 // Visit parameter variable declarations of the implicit function
1808 // if the traverser is visiting implicit code. Parameter variable
1809 // declarations do not have valid TypeSourceInfo, so to visit them
1810 // we need to traverse the declarations explicitly.
1811 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1814 TRY_TO(TraverseDecl(*I));
1817 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1818 // Constructor initializers.
1819 for (auto *I : Ctor->inits()) {
1820 TRY_TO(TraverseConstructorInitializer(I));
1824 if (D->isThisDeclarationADefinition()) {
1825 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1830 DEF_TRAVERSE_DECL(FunctionDecl, {
1831 // We skip decls_begin/decls_end, which are already covered by
1832 // TraverseFunctionHelper().
1833 return TraverseFunctionHelper(D);
1836 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1837 // We skip decls_begin/decls_end, which are already covered by
1838 // TraverseFunctionHelper().
1839 return TraverseFunctionHelper(D);
1842 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1843 // We skip decls_begin/decls_end, which are already covered by
1844 // TraverseFunctionHelper().
1845 return TraverseFunctionHelper(D);
1848 // CXXConversionDecl is the declaration of a type conversion operator.
1849 // It's not a cast expression.
1850 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1851 // We skip decls_begin/decls_end, which are already covered by
1852 // TraverseFunctionHelper().
1853 return TraverseFunctionHelper(D);
1856 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1857 // We skip decls_begin/decls_end, which are already covered by
1858 // TraverseFunctionHelper().
1859 return TraverseFunctionHelper(D);
1862 template <typename Derived>
1863 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1864 TRY_TO(TraverseDeclaratorHelper(D));
1865 // Default params are taken care of when we traverse the ParmVarDecl.
1866 if (!isa<ParmVarDecl>(D) &&
1867 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1868 TRY_TO(TraverseStmt(D->getInit()));
1872 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1874 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1876 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1877 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1878 TRY_TO(TraverseDeclaratorHelper(D));
1879 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1880 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1883 DEF_TRAVERSE_DECL(ParmVarDecl, {
1884 TRY_TO(TraverseVarHelper(D));
1886 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1887 !D->hasUnparsedDefaultArg())
1888 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1890 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1891 !D->hasUnparsedDefaultArg())
1892 TRY_TO(TraverseStmt(D->getDefaultArg()));
1895 #undef DEF_TRAVERSE_DECL
1897 // ----------------- Stmt traversal -----------------
1899 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1900 // over the children defined in children() (every stmt defines these,
1901 // though sometimes the range is empty). Each individual Traverse*
1902 // method only needs to worry about children other than those. To see
1903 // what children() does for a given class, see, e.g.,
1904 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1906 // This macro makes available a variable S, the passed-in stmt.
1907 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1908 template <typename Derived> \
1909 bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) { \
1910 TRY_TO(WalkUpFrom##STMT(S)); \
1912 for (Stmt *SubStmt : S->children()) { \
1913 TRY_TO(TraverseStmt(SubStmt)); \
1918 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1919 TRY_TO(TraverseStmt(S->getAsmString()));
1920 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1921 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1923 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1924 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1926 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1927 TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1929 // children() iterates over inputExpr and outputExpr.
1934 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1935 // added this needs to be implemented.
1938 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1939 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1940 // children() iterates over the handler block.
1943 DEF_TRAVERSE_STMT(DeclStmt, {
1944 for (auto *I : S->decls()) {
1945 TRY_TO(TraverseDecl(I));
1947 // Suppress the default iteration over children() by
1948 // returning. Here's why: A DeclStmt looks like 'type var [=
1949 // initializer]'. The decls above already traverse over the
1950 // initializers, so we don't have to do it again (which
1951 // children() would do).
1955 // These non-expr stmts (most of them), do not need any action except
1956 // iterating over the children.
1957 DEF_TRAVERSE_STMT(BreakStmt, {})
1958 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1959 DEF_TRAVERSE_STMT(CaseStmt, {})
1960 DEF_TRAVERSE_STMT(CompoundStmt, {})
1961 DEF_TRAVERSE_STMT(ContinueStmt, {})
1962 DEF_TRAVERSE_STMT(DefaultStmt, {})
1963 DEF_TRAVERSE_STMT(DoStmt, {})
1964 DEF_TRAVERSE_STMT(ForStmt, {})
1965 DEF_TRAVERSE_STMT(GotoStmt, {})
1966 DEF_TRAVERSE_STMT(IfStmt, {})
1967 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1968 DEF_TRAVERSE_STMT(LabelStmt, {})
1969 DEF_TRAVERSE_STMT(AttributedStmt, {})
1970 DEF_TRAVERSE_STMT(NullStmt, {})
1971 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1972 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1973 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1974 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1975 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1976 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1977 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1978 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1979 if (!getDerived().shouldVisitImplicitCode()) {
1980 TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1981 TRY_TO(TraverseStmt(S->getRangeInit()));
1982 TRY_TO(TraverseStmt(S->getBody()));
1983 // Visit everything else only if shouldVisitImplicitCode().
1987 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1988 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1989 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1991 DEF_TRAVERSE_STMT(ReturnStmt, {})
1992 DEF_TRAVERSE_STMT(SwitchStmt, {})
1993 DEF_TRAVERSE_STMT(WhileStmt, {})
1995 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1996 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1997 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1998 if (S->hasExplicitTemplateArgs()) {
1999 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2000 S->getNumTemplateArgs()));
2004 DEF_TRAVERSE_STMT(DeclRefExpr, {
2005 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2006 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2007 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2008 S->getNumTemplateArgs()));
2011 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
2012 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2013 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2014 if (S->hasExplicitTemplateArgs()) {
2015 TRY_TO(TraverseTemplateArgumentLocsHelper(
2016 S->getExplicitTemplateArgs().getTemplateArgs(),
2017 S->getNumTemplateArgs()));
2021 DEF_TRAVERSE_STMT(MemberExpr, {
2022 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2023 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2024 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2025 S->getNumTemplateArgs()));
2030 {// We don't traverse the cast type, as it's not written in the
2034 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2035 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2038 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2039 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2042 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2043 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2046 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2047 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2050 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2051 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2054 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2055 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2058 // InitListExpr is a tricky one, because we want to do all our work on
2059 // the syntactic form of the listexpr, but this method takes the
2060 // semantic form by default. We can't use the macro helper because it
2061 // calls WalkUp*() on the semantic form, before our code can convert
2062 // to the syntactic form.
2063 template <typename Derived>
2064 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
2065 InitListExpr *Syn = S->isSemanticForm() ? S->getSyntacticForm() : S;
2067 TRY_TO(WalkUpFromInitListExpr(Syn));
2068 // All we need are the default actions. FIXME: use a helper function.
2069 for (Stmt *SubStmt : Syn->children()) {
2070 TRY_TO(TraverseStmt(SubStmt));
2073 InitListExpr *Sem = S->isSemanticForm() ? S : S->getSemanticForm();
2075 TRY_TO(WalkUpFromInitListExpr(Sem));
2076 for (Stmt *SubStmt : Sem->children()) {
2077 TRY_TO(TraverseStmt(SubStmt));
2083 // GenericSelectionExpr is a special case because the types and expressions
2084 // are interleaved. We also need to watch out for null types (default
2085 // generic associations).
2086 template <typename Derived>
2087 bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr(
2088 GenericSelectionExpr *S) {
2089 TRY_TO(WalkUpFromGenericSelectionExpr(S));
2090 TRY_TO(TraverseStmt(S->getControllingExpr()));
2091 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2092 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2093 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2094 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2099 // PseudoObjectExpr is a special case because of the wierdness with
2100 // syntactic expressions and opaque values.
2101 template <typename Derived>
2103 RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2104 TRY_TO(WalkUpFromPseudoObjectExpr(S));
2105 TRY_TO(TraverseStmt(S->getSyntacticForm()));
2106 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2107 e = S->semantics_end();
2110 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2111 sub = OVE->getSourceExpr();
2112 TRY_TO(TraverseStmt(sub));
2117 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2118 // This is called for code like 'return T()' where T is a built-in
2119 // (i.e. non-class) type.
2120 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2123 DEF_TRAVERSE_STMT(CXXNewExpr, {
2124 // The child-iterator will pick up the other arguments.
2125 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2128 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2129 // The child-iterator will pick up the expression representing
2131 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2132 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2133 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2136 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2137 // The child-iterator will pick up the arg if it's an expression,
2138 // but not if it's a type.
2139 if (S->isArgumentType())
2140 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2143 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2144 // The child-iterator will pick up the arg if it's an expression,
2145 // but not if it's a type.
2146 if (S->isTypeOperand())
2147 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2150 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2151 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2154 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2155 // The child-iterator will pick up the arg if it's an expression,
2156 // but not if it's a type.
2157 if (S->isTypeOperand())
2158 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2161 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2162 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2163 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2166 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2167 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2170 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2171 { TRY_TO(TraverseStmt(S->getQueriedExpression())); })
2173 DEF_TRAVERSE_STMT(VAArgExpr, {
2174 // The child-iterator will pick up the expression argument.
2175 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2178 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2179 // This is called for code like 'return T()' where T is a class type.
2180 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2183 // Walk only the visible parts of lambda expressions.
2184 template <typename Derived>
2185 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2186 TRY_TO(WalkUpFromLambdaExpr(S));
2188 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2189 CEnd = S->explicit_capture_end();
2191 TRY_TO(TraverseLambdaCapture(S, C));
2194 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2195 FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2197 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2198 // Visit the whole type.
2199 TRY_TO(TraverseTypeLoc(TL));
2201 if (S->hasExplicitParameters()) {
2202 // Visit parameters.
2203 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2204 TRY_TO(TraverseDecl(Proto.getParam(I)));
2206 } else if (S->hasExplicitResultType()) {
2207 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2210 auto *T = Proto.getTypePtr();
2211 for (const auto &E : T->exceptions()) {
2212 TRY_TO(TraverseType(E));
2215 if (Expr *NE = T->getNoexceptExpr())
2216 TRY_TO(TraverseStmt(NE));
2219 TRY_TO(TraverseLambdaBody(S));
2223 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2224 // This is called for code like 'T()', where T is a template argument.
2225 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2228 // These expressions all might take explicit template arguments.
2229 // We traverse those if so. FIXME: implement these.
2230 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2231 DEF_TRAVERSE_STMT(CallExpr, {})
2232 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2234 // These exprs (most of them), do not need any action except iterating
2235 // over the children.
2236 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2237 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2238 DEF_TRAVERSE_STMT(BlockExpr, {
2239 TRY_TO(TraverseDecl(S->getBlockDecl()));
2240 return true; // no child statements to loop through.
2242 DEF_TRAVERSE_STMT(ChooseExpr, {})
2243 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2244 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2246 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2247 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2248 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2249 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2250 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2251 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2252 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2253 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2254 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2255 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2256 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2257 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2258 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2259 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2261 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2262 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2263 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2264 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2265 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2266 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2267 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2268 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2269 DEF_TRAVERSE_STMT(NoInitExpr, {})
2270 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2271 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2272 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2273 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2275 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2276 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2277 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2278 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2279 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2281 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2282 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2283 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2284 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2285 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2286 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2287 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2289 DEF_TRAVERSE_STMT(ParenExpr, {})
2290 DEF_TRAVERSE_STMT(ParenListExpr, {})
2291 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2292 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2293 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2294 DEF_TRAVERSE_STMT(StmtExpr, {})
2295 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2296 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2297 if (S->hasExplicitTemplateArgs()) {
2298 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2299 S->getNumTemplateArgs()));
2303 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2304 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2305 if (S->hasExplicitTemplateArgs()) {
2306 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2307 S->getNumTemplateArgs()));
2311 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2312 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2313 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2314 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2315 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2317 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2318 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2319 DEF_TRAVERSE_STMT(TypoExpr, {})
2320 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2322 // These operators (all of them) do not need any action except
2323 // iterating over the children.
2324 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2325 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2326 DEF_TRAVERSE_STMT(UnaryOperator, {})
2327 DEF_TRAVERSE_STMT(BinaryOperator, {})
2328 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2329 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2330 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2331 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2332 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2333 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2334 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2335 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2336 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2337 DEF_TRAVERSE_STMT(AtomicExpr, {})
2339 // These literals (all of them) do not need any action.
2340 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2341 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2342 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2343 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2344 DEF_TRAVERSE_STMT(StringLiteral, {})
2345 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2346 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2347 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2348 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2350 // Traverse OpenCL: AsType, Convert.
2351 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2353 // OpenMP directives.
2354 template <typename Derived>
2355 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2356 OMPExecutableDirective *S) {
2357 for (auto *C : S->clauses()) {
2358 TRY_TO(TraverseOMPClause(C));
2363 template <typename Derived>
2365 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2366 return TraverseOMPExecutableDirective(S);
2369 DEF_TRAVERSE_STMT(OMPParallelDirective,
2370 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2372 DEF_TRAVERSE_STMT(OMPSimdDirective,
2373 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2375 DEF_TRAVERSE_STMT(OMPForDirective,
2376 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2378 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2379 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2381 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2382 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2384 DEF_TRAVERSE_STMT(OMPSectionDirective,
2385 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2387 DEF_TRAVERSE_STMT(OMPSingleDirective,
2388 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2390 DEF_TRAVERSE_STMT(OMPMasterDirective,
2391 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2393 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2394 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2395 TRY_TO(TraverseOMPExecutableDirective(S));
2398 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2399 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2401 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2402 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2404 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2405 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2407 DEF_TRAVERSE_STMT(OMPTaskDirective,
2408 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2410 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2411 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2413 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2414 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2416 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2417 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2419 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2420 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2422 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2423 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2425 DEF_TRAVERSE_STMT(OMPCancelDirective,
2426 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2428 DEF_TRAVERSE_STMT(OMPFlushDirective,
2429 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2431 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2432 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2434 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2435 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2437 DEF_TRAVERSE_STMT(OMPTargetDirective,
2438 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2440 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2441 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2444 template <typename Derived>
2445 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2448 switch (C->getClauseKind()) {
2449 #define OPENMP_CLAUSE(Name, Class) \
2451 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2453 #include "clang/Basic/OpenMPKinds.def"
2454 case OMPC_threadprivate:
2461 template <typename Derived>
2462 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2463 TRY_TO(TraverseStmt(C->getCondition()));
2467 template <typename Derived>
2468 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2469 TRY_TO(TraverseStmt(C->getCondition()));
2473 template <typename Derived>
2475 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2476 TRY_TO(TraverseStmt(C->getNumThreads()));
2480 template <typename Derived>
2481 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2482 TRY_TO(TraverseStmt(C->getSafelen()));
2486 template <typename Derived>
2488 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2489 TRY_TO(TraverseStmt(C->getNumForLoops()));
2493 template <typename Derived>
2494 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2498 template <typename Derived>
2499 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2503 template <typename Derived>
2505 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2506 TRY_TO(TraverseStmt(C->getChunkSize()));
2507 TRY_TO(TraverseStmt(C->getHelperChunkSize()));
2511 template <typename Derived>
2512 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) {
2516 template <typename Derived>
2517 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2521 template <typename Derived>
2522 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2526 template <typename Derived>
2528 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2532 template <typename Derived>
2533 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2537 template <typename Derived>
2538 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2542 template <typename Derived>
2543 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2547 template <typename Derived>
2548 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2552 template <typename Derived>
2553 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2557 template <typename Derived>
2558 template <typename T>
2559 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2560 for (auto *E : Node->varlists()) {
2561 TRY_TO(TraverseStmt(E));
2566 template <typename Derived>
2567 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2568 TRY_TO(VisitOMPClauseList(C));
2569 for (auto *E : C->private_copies()) {
2570 TRY_TO(TraverseStmt(E));
2575 template <typename Derived>
2576 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2577 OMPFirstprivateClause *C) {
2578 TRY_TO(VisitOMPClauseList(C));
2579 for (auto *E : C->private_copies()) {
2580 TRY_TO(TraverseStmt(E));
2582 for (auto *E : C->inits()) {
2583 TRY_TO(TraverseStmt(E));
2588 template <typename Derived>
2589 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2590 OMPLastprivateClause *C) {
2591 TRY_TO(VisitOMPClauseList(C));
2592 for (auto *E : C->private_copies()) {
2593 TRY_TO(TraverseStmt(E));
2595 for (auto *E : C->source_exprs()) {
2596 TRY_TO(TraverseStmt(E));
2598 for (auto *E : C->destination_exprs()) {
2599 TRY_TO(TraverseStmt(E));
2601 for (auto *E : C->assignment_ops()) {
2602 TRY_TO(TraverseStmt(E));
2607 template <typename Derived>
2608 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2609 TRY_TO(VisitOMPClauseList(C));
2613 template <typename Derived>
2614 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2615 TRY_TO(TraverseStmt(C->getStep()));
2616 TRY_TO(TraverseStmt(C->getCalcStep()));
2617 TRY_TO(VisitOMPClauseList(C));
2618 for (auto *E : C->inits()) {
2619 TRY_TO(TraverseStmt(E));
2621 for (auto *E : C->updates()) {
2622 TRY_TO(TraverseStmt(E));
2624 for (auto *E : C->finals()) {
2625 TRY_TO(TraverseStmt(E));
2630 template <typename Derived>
2631 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2632 TRY_TO(TraverseStmt(C->getAlignment()));
2633 TRY_TO(VisitOMPClauseList(C));
2637 template <typename Derived>
2638 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2639 TRY_TO(VisitOMPClauseList(C));
2640 for (auto *E : C->source_exprs()) {
2641 TRY_TO(TraverseStmt(E));
2643 for (auto *E : C->destination_exprs()) {
2644 TRY_TO(TraverseStmt(E));
2646 for (auto *E : C->assignment_ops()) {
2647 TRY_TO(TraverseStmt(E));
2652 template <typename Derived>
2653 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2654 OMPCopyprivateClause *C) {
2655 TRY_TO(VisitOMPClauseList(C));
2656 for (auto *E : C->source_exprs()) {
2657 TRY_TO(TraverseStmt(E));
2659 for (auto *E : C->destination_exprs()) {
2660 TRY_TO(TraverseStmt(E));
2662 for (auto *E : C->assignment_ops()) {
2663 TRY_TO(TraverseStmt(E));
2668 template <typename Derived>
2670 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2671 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2672 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2673 TRY_TO(VisitOMPClauseList(C));
2674 for (auto *E : C->lhs_exprs()) {
2675 TRY_TO(TraverseStmt(E));
2677 for (auto *E : C->rhs_exprs()) {
2678 TRY_TO(TraverseStmt(E));
2680 for (auto *E : C->reduction_ops()) {
2681 TRY_TO(TraverseStmt(E));
2686 template <typename Derived>
2687 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2688 TRY_TO(VisitOMPClauseList(C));
2692 template <typename Derived>
2693 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
2694 TRY_TO(VisitOMPClauseList(C));
2698 // FIXME: look at the following tricky-seeming exprs to see if we
2699 // need to recurse on anything. These are ones that have methods
2700 // returning decls or qualtypes or nestednamespecifier -- though I'm
2701 // not sure if they own them -- or just seemed very complicated, or
2702 // had lots of sub-types to explore.
2704 // VisitOverloadExpr and its children: recurse on template args? etc?
2706 // FIXME: go through all the stmts and exprs again, and see which of them
2707 // create new types, and recurse on the types (TypeLocs?) of those.
2710 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2711 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2712 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2713 // Every class that has getQualifier.
2715 #undef DEF_TRAVERSE_STMT
2719 } // end namespace clang
2721 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H