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 (C->isInitCapture())
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(NamespaceAliasDecl, {
1360 // We shouldn't traverse an aliased namespace, since it will be
1361 // defined (and, therefore, traversed) somewhere else.
1363 // This return statement makes sure the traversal of nodes in
1364 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1365 // is skipped - don't remove it.
1369 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1374 {// Code in an unnamed namespace shows up automatically in
1375 // decls_begin()/decls_end(). Thus we don't need to recurse on
1376 // D->getAnonymousNamespace().
1379 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1382 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1385 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1388 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1391 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1394 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1397 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1398 if (D->getReturnTypeSourceInfo()) {
1399 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1401 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1403 TRY_TO(TraverseDecl(*I));
1405 if (D->isThisDeclarationADefinition()) {
1406 TRY_TO(TraverseStmt(D->getBody()));
1411 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1412 if (D->getTypeSourceInfo())
1413 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1415 TRY_TO(TraverseType(D->getType()));
1419 DEF_TRAVERSE_DECL(UsingDecl, {
1420 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1421 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1424 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1425 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1428 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1430 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1431 for (auto *I : D->varlists()) {
1432 TRY_TO(TraverseStmt(I));
1436 // A helper method for TemplateDecl's children.
1437 template <typename Derived>
1438 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1439 TemplateParameterList *TPL) {
1441 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1443 TRY_TO(TraverseDecl(*I));
1449 template <typename Derived>
1450 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1451 ClassTemplateDecl *D) {
1452 for (auto *SD : D->specializations()) {
1453 for (auto *RD : SD->redecls()) {
1454 // We don't want to visit injected-class-names in this traversal.
1455 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1459 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1460 // Visit the implicit instantiations with the requested pattern.
1461 case TSK_Undeclared:
1462 case TSK_ImplicitInstantiation:
1463 TRY_TO(TraverseDecl(RD));
1466 // We don't need to do anything on an explicit instantiation
1467 // or explicit specialization because there will be an explicit
1468 // node for it elsewhere.
1469 case TSK_ExplicitInstantiationDeclaration:
1470 case TSK_ExplicitInstantiationDefinition:
1471 case TSK_ExplicitSpecialization:
1480 template <typename Derived>
1481 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1482 VarTemplateDecl *D) {
1483 for (auto *SD : D->specializations()) {
1484 for (auto *RD : SD->redecls()) {
1486 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1487 case TSK_Undeclared:
1488 case TSK_ImplicitInstantiation:
1489 TRY_TO(TraverseDecl(RD));
1492 case TSK_ExplicitInstantiationDeclaration:
1493 case TSK_ExplicitInstantiationDefinition:
1494 case TSK_ExplicitSpecialization:
1503 // A helper method for traversing the instantiations of a
1504 // function while skipping its specializations.
1505 template <typename Derived>
1506 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1507 FunctionTemplateDecl *D) {
1508 for (auto *FD : D->specializations()) {
1509 for (auto *RD : FD->redecls()) {
1510 switch (RD->getTemplateSpecializationKind()) {
1511 case TSK_Undeclared:
1512 case TSK_ImplicitInstantiation:
1513 // We don't know what kind of FunctionDecl this is.
1514 TRY_TO(TraverseDecl(RD));
1517 // FIXME: For now traverse explicit instantiations here. Change that
1518 // once they are represented as dedicated nodes in the AST.
1519 case TSK_ExplicitInstantiationDeclaration:
1520 case TSK_ExplicitInstantiationDefinition:
1521 TRY_TO(TraverseDecl(RD));
1524 case TSK_ExplicitSpecialization:
1533 // This macro unifies the traversal of class, variable and function
1534 // template declarations.
1535 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1536 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1537 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1538 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1540 /* By default, we do not traverse the instantiations of \
1541 class templates since they do not appear in the user code. The \
1542 following code optionally traverses them. \
1544 We only traverse the class instantiations when we see the canonical \
1545 declaration of the template, to ensure we only visit them once. */ \
1546 if (getDerived().shouldVisitTemplateInstantiations() && \
1547 D == D->getCanonicalDecl()) \
1548 TRY_TO(TraverseTemplateInstantiations(D)); \
1550 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1551 from a template instantiation back to the template from which \
1552 it was instantiated, and thus should not be traversed. */ \
1555 DEF_TRAVERSE_TMPL_DECL(Class)
1556 DEF_TRAVERSE_TMPL_DECL(Var)
1557 DEF_TRAVERSE_TMPL_DECL(Function)
1559 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1560 // D is the "T" in something like
1561 // template <template <typename> class T> class container { };
1562 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1563 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1564 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1566 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1569 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1570 // D is the "T" in something like "template<typename T> class vector;"
1571 if (D->getTypeForDecl())
1572 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1573 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1574 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1577 DEF_TRAVERSE_DECL(TypedefDecl, {
1578 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1579 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1580 // declaring the typedef, not something that was written in the
1584 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1585 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1586 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1587 // declaring the type alias, not something that was written in the
1591 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1592 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1593 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1596 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1597 // A dependent using declaration which was marked with 'typename'.
1598 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1599 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1600 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1601 // declaring the type, not something that was written in the
1605 DEF_TRAVERSE_DECL(EnumDecl, {
1606 if (D->getTypeForDecl())
1607 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1609 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1610 // The enumerators are already traversed by
1611 // decls_begin()/decls_end().
1614 // Helper methods for RecordDecl and its children.
1615 template <typename Derived>
1616 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1617 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1618 // declaring the type, not something that was written in the source.
1620 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1624 template <typename Derived>
1625 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1626 if (!TraverseRecordHelper(D))
1628 if (D->isCompleteDefinition()) {
1629 for (const auto &I : D->bases()) {
1630 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1632 // We don't traverse the friends or the conversions, as they are
1633 // already in decls_begin()/decls_end().
1638 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1640 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1642 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1643 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1644 /* For implicit instantiations ("set<int> x;"), we don't want to \
1645 recurse at all, since the instatiated template isn't written in \
1646 the source code anywhere. (Note the instatiated *type* -- \
1647 set<int> -- is written, and will still get a callback of \
1648 TemplateSpecializationType). For explicit instantiations \
1649 ("template set<int>;"), we do need a callback, since this \
1650 is the only callback that's made for this instantiation. \
1651 We use getTypeAsWritten() to distinguish. */ \
1652 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1653 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1655 if (!getDerived().shouldVisitTemplateInstantiations() && \
1656 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1657 /* Returning from here skips traversing the \
1658 declaration context of the *TemplateSpecializationDecl \
1659 (embedded in the DEF_TRAVERSE_DECL() macro) \
1660 which contains the instantiated members of the template. */ \
1664 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1665 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1667 template <typename Derived>
1668 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1669 const TemplateArgumentLoc *TAL, unsigned Count) {
1670 for (unsigned I = 0; I < Count; ++I) {
1671 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1676 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1677 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1678 /* The partial specialization. */ \
1679 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1680 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1682 TRY_TO(TraverseDecl(*I)); \
1685 /* The args that remains unspecialized. */ \
1686 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1687 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1688 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1690 /* Don't need the *TemplatePartialSpecializationHelper, even \
1691 though that's our parent class -- we already visit all the \
1692 template args here. */ \
1693 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1695 /* Instantiations will have been visited with the primary template. */ \
1698 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1699 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1701 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1703 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1704 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1705 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1706 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1707 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1710 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1712 template <typename Derived>
1713 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1714 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1715 if (D->getTypeSourceInfo())
1716 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1718 TRY_TO(TraverseType(D->getType()));
1722 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1724 DEF_TRAVERSE_DECL(FieldDecl, {
1725 TRY_TO(TraverseDeclaratorHelper(D));
1726 if (D->isBitField())
1727 TRY_TO(TraverseStmt(D->getBitWidth()));
1728 else if (D->hasInClassInitializer())
1729 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1732 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1733 TRY_TO(TraverseDeclaratorHelper(D));
1734 if (D->isBitField())
1735 TRY_TO(TraverseStmt(D->getBitWidth()));
1736 // FIXME: implement the rest.
1739 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1740 TRY_TO(TraverseDeclaratorHelper(D));
1741 if (D->isBitField())
1742 TRY_TO(TraverseStmt(D->getBitWidth()));
1743 // FIXME: implement the rest.
1746 template <typename Derived>
1747 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1748 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1749 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1751 // If we're an explicit template specialization, iterate over the
1752 // template args that were explicitly specified. If we were doing
1753 // this in typing order, we'd do it between the return type and
1754 // the function args, but both are handled by the FunctionTypeLoc
1755 // above, so we have to choose one side. I've decided to do before.
1756 if (const FunctionTemplateSpecializationInfo *FTSI =
1757 D->getTemplateSpecializationInfo()) {
1758 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1759 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1760 // A specialization might not have explicit template arguments if it has
1761 // a templated return type and concrete arguments.
1762 if (const ASTTemplateArgumentListInfo *TALI =
1763 FTSI->TemplateArgumentsAsWritten) {
1764 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1765 TALI->NumTemplateArgs));
1770 // Visit the function type itself, which can be either
1771 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1772 // also covers the return type and the function parameters,
1773 // including exception specifications.
1774 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1775 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1776 } else if (getDerived().shouldVisitImplicitCode()) {
1777 // Visit parameter variable declarations of the implicit function
1778 // if the traverser is visiting implicit code. Parameter variable
1779 // declarations do not have valid TypeSourceInfo, so to visit them
1780 // we need to traverse the declarations explicitly.
1781 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1784 TRY_TO(TraverseDecl(*I));
1787 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1788 // Constructor initializers.
1789 for (auto *I : Ctor->inits()) {
1790 TRY_TO(TraverseConstructorInitializer(I));
1794 if (D->isThisDeclarationADefinition()) {
1795 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1800 DEF_TRAVERSE_DECL(FunctionDecl, {
1801 // We skip decls_begin/decls_end, which are already covered by
1802 // TraverseFunctionHelper().
1803 return TraverseFunctionHelper(D);
1806 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1807 // We skip decls_begin/decls_end, which are already covered by
1808 // TraverseFunctionHelper().
1809 return TraverseFunctionHelper(D);
1812 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1813 // We skip decls_begin/decls_end, which are already covered by
1814 // TraverseFunctionHelper().
1815 return TraverseFunctionHelper(D);
1818 // CXXConversionDecl is the declaration of a type conversion operator.
1819 // It's not a cast expression.
1820 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1821 // We skip decls_begin/decls_end, which are already covered by
1822 // TraverseFunctionHelper().
1823 return TraverseFunctionHelper(D);
1826 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1827 // We skip decls_begin/decls_end, which are already covered by
1828 // TraverseFunctionHelper().
1829 return TraverseFunctionHelper(D);
1832 template <typename Derived>
1833 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1834 TRY_TO(TraverseDeclaratorHelper(D));
1835 // Default params are taken care of when we traverse the ParmVarDecl.
1836 if (!isa<ParmVarDecl>(D) &&
1837 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1838 TRY_TO(TraverseStmt(D->getInit()));
1842 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1844 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1846 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1847 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1848 TRY_TO(TraverseDeclaratorHelper(D));
1849 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1850 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1853 DEF_TRAVERSE_DECL(ParmVarDecl, {
1854 TRY_TO(TraverseVarHelper(D));
1856 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1857 !D->hasUnparsedDefaultArg())
1858 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1860 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1861 !D->hasUnparsedDefaultArg())
1862 TRY_TO(TraverseStmt(D->getDefaultArg()));
1865 #undef DEF_TRAVERSE_DECL
1867 // ----------------- Stmt traversal -----------------
1869 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1870 // over the children defined in children() (every stmt defines these,
1871 // though sometimes the range is empty). Each individual Traverse*
1872 // method only needs to worry about children other than those. To see
1873 // what children() does for a given class, see, e.g.,
1874 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1876 // This macro makes available a variable S, the passed-in stmt.
1877 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1878 template <typename Derived> \
1879 bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) { \
1880 TRY_TO(WalkUpFrom##STMT(S)); \
1882 for (Stmt::child_range range = S->children(); range; ++range) { \
1883 TRY_TO(TraverseStmt(*range)); \
1888 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1889 TRY_TO(TraverseStmt(S->getAsmString()));
1890 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1891 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1893 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1894 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1896 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1897 TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1899 // children() iterates over inputExpr and outputExpr.
1904 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1905 // added this needs to be implemented.
1908 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1909 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1910 // children() iterates over the handler block.
1913 DEF_TRAVERSE_STMT(DeclStmt, {
1914 for (auto *I : S->decls()) {
1915 TRY_TO(TraverseDecl(I));
1917 // Suppress the default iteration over children() by
1918 // returning. Here's why: A DeclStmt looks like 'type var [=
1919 // initializer]'. The decls above already traverse over the
1920 // initializers, so we don't have to do it again (which
1921 // children() would do).
1925 // These non-expr stmts (most of them), do not need any action except
1926 // iterating over the children.
1927 DEF_TRAVERSE_STMT(BreakStmt, {})
1928 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1929 DEF_TRAVERSE_STMT(CaseStmt, {})
1930 DEF_TRAVERSE_STMT(CompoundStmt, {})
1931 DEF_TRAVERSE_STMT(ContinueStmt, {})
1932 DEF_TRAVERSE_STMT(DefaultStmt, {})
1933 DEF_TRAVERSE_STMT(DoStmt, {})
1934 DEF_TRAVERSE_STMT(ForStmt, {})
1935 DEF_TRAVERSE_STMT(GotoStmt, {})
1936 DEF_TRAVERSE_STMT(IfStmt, {})
1937 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1938 DEF_TRAVERSE_STMT(LabelStmt, {})
1939 DEF_TRAVERSE_STMT(AttributedStmt, {})
1940 DEF_TRAVERSE_STMT(NullStmt, {})
1941 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1942 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1943 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1944 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1945 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1946 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1947 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1948 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1949 if (!getDerived().shouldVisitImplicitCode()) {
1950 TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1951 TRY_TO(TraverseStmt(S->getRangeInit()));
1952 TRY_TO(TraverseStmt(S->getBody()));
1953 // Visit everything else only if shouldVisitImplicitCode().
1957 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1958 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1959 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1961 DEF_TRAVERSE_STMT(ReturnStmt, {})
1962 DEF_TRAVERSE_STMT(SwitchStmt, {})
1963 DEF_TRAVERSE_STMT(WhileStmt, {})
1965 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1966 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1967 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1968 if (S->hasExplicitTemplateArgs()) {
1969 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1970 S->getNumTemplateArgs()));
1974 DEF_TRAVERSE_STMT(DeclRefExpr, {
1975 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1976 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1977 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1978 S->getNumTemplateArgs()));
1981 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1982 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1983 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1984 if (S->hasExplicitTemplateArgs()) {
1985 TRY_TO(TraverseTemplateArgumentLocsHelper(
1986 S->getExplicitTemplateArgs().getTemplateArgs(),
1987 S->getNumTemplateArgs()));
1991 DEF_TRAVERSE_STMT(MemberExpr, {
1992 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1993 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1994 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1995 S->getNumTemplateArgs()));
2000 {// We don't traverse the cast type, as it's not written in the
2004 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2005 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2008 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2009 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2012 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2013 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2016 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2017 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2020 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2021 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2024 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2025 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2028 // InitListExpr is a tricky one, because we want to do all our work on
2029 // the syntactic form of the listexpr, but this method takes the
2030 // semantic form by default. We can't use the macro helper because it
2031 // calls WalkUp*() on the semantic form, before our code can convert
2032 // to the syntactic form.
2033 template <typename Derived>
2034 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
2035 if (InitListExpr *Syn = S->getSyntacticForm())
2037 TRY_TO(WalkUpFromInitListExpr(S));
2038 // All we need are the default actions. FIXME: use a helper function.
2039 for (Stmt::child_range range = S->children(); range; ++range) {
2040 TRY_TO(TraverseStmt(*range));
2045 // GenericSelectionExpr is a special case because the types and expressions
2046 // are interleaved. We also need to watch out for null types (default
2047 // generic associations).
2048 template <typename Derived>
2049 bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr(
2050 GenericSelectionExpr *S) {
2051 TRY_TO(WalkUpFromGenericSelectionExpr(S));
2052 TRY_TO(TraverseStmt(S->getControllingExpr()));
2053 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2054 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2055 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2056 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2061 // PseudoObjectExpr is a special case because of the wierdness with
2062 // syntactic expressions and opaque values.
2063 template <typename Derived>
2065 RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2066 TRY_TO(WalkUpFromPseudoObjectExpr(S));
2067 TRY_TO(TraverseStmt(S->getSyntacticForm()));
2068 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2069 e = S->semantics_end();
2072 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2073 sub = OVE->getSourceExpr();
2074 TRY_TO(TraverseStmt(sub));
2079 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2080 // This is called for code like 'return T()' where T is a built-in
2081 // (i.e. non-class) type.
2082 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2085 DEF_TRAVERSE_STMT(CXXNewExpr, {
2086 // The child-iterator will pick up the other arguments.
2087 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2090 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2091 // The child-iterator will pick up the expression representing
2093 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2094 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2095 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2098 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2099 // The child-iterator will pick up the arg if it's an expression,
2100 // but not if it's a type.
2101 if (S->isArgumentType())
2102 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2105 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2106 // The child-iterator will pick up the arg if it's an expression,
2107 // but not if it's a type.
2108 if (S->isTypeOperand())
2109 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2112 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2113 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2116 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2117 // The child-iterator will pick up the arg if it's an expression,
2118 // but not if it's a type.
2119 if (S->isTypeOperand())
2120 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2123 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2124 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2125 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2128 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2129 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2132 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2133 { TRY_TO(TraverseStmt(S->getQueriedExpression())); })
2135 DEF_TRAVERSE_STMT(VAArgExpr, {
2136 // The child-iterator will pick up the expression argument.
2137 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2140 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2141 // This is called for code like 'return T()' where T is a class type.
2142 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2145 // Walk only the visible parts of lambda expressions.
2146 template <typename Derived>
2147 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2148 TRY_TO(WalkUpFromLambdaExpr(S));
2150 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2151 CEnd = S->explicit_capture_end();
2153 TRY_TO(TraverseLambdaCapture(S, C));
2156 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2157 FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2159 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2160 // Visit the whole type.
2161 TRY_TO(TraverseTypeLoc(TL));
2163 if (S->hasExplicitParameters()) {
2164 // Visit parameters.
2165 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2166 TRY_TO(TraverseDecl(Proto.getParam(I)));
2168 } else if (S->hasExplicitResultType()) {
2169 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2172 auto *T = Proto.getTypePtr();
2173 for (const auto &E : T->exceptions()) {
2174 TRY_TO(TraverseType(E));
2177 if (Expr *NE = T->getNoexceptExpr())
2178 TRY_TO(TraverseStmt(NE));
2181 TRY_TO(TraverseLambdaBody(S));
2185 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2186 // This is called for code like 'T()', where T is a template argument.
2187 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2190 // These expressions all might take explicit template arguments.
2191 // We traverse those if so. FIXME: implement these.
2192 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2193 DEF_TRAVERSE_STMT(CallExpr, {})
2194 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2196 // These exprs (most of them), do not need any action except iterating
2197 // over the children.
2198 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2199 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2200 DEF_TRAVERSE_STMT(BlockExpr, {
2201 TRY_TO(TraverseDecl(S->getBlockDecl()));
2202 return true; // no child statements to loop through.
2204 DEF_TRAVERSE_STMT(ChooseExpr, {})
2205 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2206 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2208 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2209 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2210 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2211 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2212 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2213 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2214 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2215 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2216 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2217 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2218 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2219 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2220 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2221 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2223 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2224 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2225 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2226 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2227 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2228 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2229 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2230 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2231 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2232 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2233 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2235 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2236 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2237 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2238 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2239 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2241 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2242 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2243 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2244 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2245 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2246 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2247 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2249 DEF_TRAVERSE_STMT(ParenExpr, {})
2250 DEF_TRAVERSE_STMT(ParenListExpr, {})
2251 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2252 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2253 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2254 DEF_TRAVERSE_STMT(StmtExpr, {})
2255 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2256 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2257 if (S->hasExplicitTemplateArgs()) {
2258 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2259 S->getNumTemplateArgs()));
2263 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2264 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2265 if (S->hasExplicitTemplateArgs()) {
2266 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2267 S->getNumTemplateArgs()));
2271 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2272 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2273 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2274 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2275 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2277 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2278 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2279 DEF_TRAVERSE_STMT(TypoExpr, {})
2280 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2282 // These operators (all of them) do not need any action except
2283 // iterating over the children.
2284 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2285 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2286 DEF_TRAVERSE_STMT(UnaryOperator, {})
2287 DEF_TRAVERSE_STMT(BinaryOperator, {})
2288 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2289 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2290 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2291 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2292 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2293 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2294 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2295 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2296 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2297 DEF_TRAVERSE_STMT(AtomicExpr, {})
2299 // These literals (all of them) do not need any action.
2300 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2301 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2302 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2303 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2304 DEF_TRAVERSE_STMT(StringLiteral, {})
2305 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2306 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2307 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2308 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2310 // Traverse OpenCL: AsType, Convert.
2311 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2313 // OpenMP directives.
2314 template <typename Derived>
2315 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2316 OMPExecutableDirective *S) {
2317 for (auto *C : S->clauses()) {
2318 TRY_TO(TraverseOMPClause(C));
2323 template <typename Derived>
2325 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2326 return TraverseOMPExecutableDirective(S);
2329 DEF_TRAVERSE_STMT(OMPParallelDirective,
2330 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2332 DEF_TRAVERSE_STMT(OMPSimdDirective,
2333 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2335 DEF_TRAVERSE_STMT(OMPForDirective,
2336 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2338 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2339 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2341 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2342 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2344 DEF_TRAVERSE_STMT(OMPSectionDirective,
2345 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2347 DEF_TRAVERSE_STMT(OMPSingleDirective,
2348 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2350 DEF_TRAVERSE_STMT(OMPMasterDirective,
2351 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2353 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2354 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2355 TRY_TO(TraverseOMPExecutableDirective(S));
2358 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2359 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2361 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2362 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2364 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2365 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2367 DEF_TRAVERSE_STMT(OMPTaskDirective,
2368 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2370 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2371 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2373 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2374 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2376 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2377 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2379 DEF_TRAVERSE_STMT(OMPFlushDirective,
2380 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2382 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2383 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2385 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2386 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2388 DEF_TRAVERSE_STMT(OMPTargetDirective,
2389 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2391 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2392 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2395 template <typename Derived>
2396 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2399 switch (C->getClauseKind()) {
2400 #define OPENMP_CLAUSE(Name, Class) \
2402 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2404 #include "clang/Basic/OpenMPKinds.def"
2405 case OMPC_threadprivate:
2412 template <typename Derived>
2413 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2414 TRY_TO(TraverseStmt(C->getCondition()));
2418 template <typename Derived>
2419 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2420 TRY_TO(TraverseStmt(C->getCondition()));
2424 template <typename Derived>
2426 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2427 TRY_TO(TraverseStmt(C->getNumThreads()));
2431 template <typename Derived>
2432 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2433 TRY_TO(TraverseStmt(C->getSafelen()));
2437 template <typename Derived>
2439 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2440 TRY_TO(TraverseStmt(C->getNumForLoops()));
2444 template <typename Derived>
2445 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2449 template <typename Derived>
2450 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2454 template <typename Derived>
2456 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2457 TRY_TO(TraverseStmt(C->getChunkSize()));
2461 template <typename Derived>
2462 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) {
2466 template <typename Derived>
2467 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2471 template <typename Derived>
2472 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2476 template <typename Derived>
2478 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2482 template <typename Derived>
2483 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2487 template <typename Derived>
2488 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2492 template <typename Derived>
2493 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2497 template <typename Derived>
2498 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2502 template <typename Derived>
2503 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2507 template <typename Derived>
2508 template <typename T>
2509 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2510 for (auto *E : Node->varlists()) {
2511 TRY_TO(TraverseStmt(E));
2516 template <typename Derived>
2517 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2518 TRY_TO(VisitOMPClauseList(C));
2519 for (auto *E : C->private_copies()) {
2520 TRY_TO(TraverseStmt(E));
2525 template <typename Derived>
2526 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2527 OMPFirstprivateClause *C) {
2528 TRY_TO(VisitOMPClauseList(C));
2529 for (auto *E : C->private_copies()) {
2530 TRY_TO(TraverseStmt(E));
2532 for (auto *E : C->inits()) {
2533 TRY_TO(TraverseStmt(E));
2538 template <typename Derived>
2539 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2540 OMPLastprivateClause *C) {
2541 TRY_TO(VisitOMPClauseList(C));
2545 template <typename Derived>
2546 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2547 TRY_TO(VisitOMPClauseList(C));
2551 template <typename Derived>
2552 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2553 TRY_TO(TraverseStmt(C->getStep()));
2554 TRY_TO(VisitOMPClauseList(C));
2558 template <typename Derived>
2559 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2560 TRY_TO(TraverseStmt(C->getAlignment()));
2561 TRY_TO(VisitOMPClauseList(C));
2565 template <typename Derived>
2566 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2567 TRY_TO(VisitOMPClauseList(C));
2571 template <typename Derived>
2572 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2573 OMPCopyprivateClause *C) {
2574 TRY_TO(VisitOMPClauseList(C));
2578 template <typename Derived>
2580 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2581 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2582 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2583 TRY_TO(VisitOMPClauseList(C));
2587 template <typename Derived>
2588 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2589 TRY_TO(VisitOMPClauseList(C));
2593 // FIXME: look at the following tricky-seeming exprs to see if we
2594 // need to recurse on anything. These are ones that have methods
2595 // returning decls or qualtypes or nestednamespecifier -- though I'm
2596 // not sure if they own them -- or just seemed very complicated, or
2597 // had lots of sub-types to explore.
2599 // VisitOverloadExpr and its children: recurse on template args? etc?
2601 // FIXME: go through all the stmts and exprs again, and see which of them
2602 // create new types, and recurse on the types (TypeLocs?) of those.
2605 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2606 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2607 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2608 // Every class that has getQualifier.
2610 #undef DEF_TRAVERSE_STMT
2614 } // end namespace clang
2616 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H