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 <type_traits>
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclFriend.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/DeclOpenMP.h"
25 #include "clang/AST/DeclTemplate.h"
26 #include "clang/AST/Expr.h"
27 #include "clang/AST/ExprCXX.h"
28 #include "clang/AST/ExprObjC.h"
29 #include "clang/AST/ExprOpenMP.h"
30 #include "clang/AST/NestedNameSpecifier.h"
31 #include "clang/AST/Stmt.h"
32 #include "clang/AST/StmtCXX.h"
33 #include "clang/AST/StmtObjC.h"
34 #include "clang/AST/StmtOpenMP.h"
35 #include "clang/AST/TemplateBase.h"
36 #include "clang/AST/TemplateName.h"
37 #include "clang/AST/Type.h"
38 #include "clang/AST/TypeLoc.h"
40 // The following three macros are used for meta programming. The code
41 // using them is responsible for defining macro OPERATOR().
43 // All unary operators.
44 #define UNARYOP_LIST() \
45 OPERATOR(PostInc) OPERATOR(PostDec) OPERATOR(PreInc) OPERATOR(PreDec) \
46 OPERATOR(AddrOf) OPERATOR(Deref) OPERATOR(Plus) OPERATOR(Minus) \
47 OPERATOR(Not) OPERATOR(LNot) OPERATOR(Real) OPERATOR(Imag) \
48 OPERATOR(Extension) OPERATOR(Coawait)
50 // All binary operators (excluding compound assign operators).
51 #define BINOP_LIST() \
52 OPERATOR(PtrMemD) OPERATOR(PtrMemI) OPERATOR(Mul) OPERATOR(Div) \
53 OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) OPERATOR(Shr) \
54 OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) OPERATOR(GE) OPERATOR(EQ) \
55 OPERATOR(NE) OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) OPERATOR(LAnd) \
56 OPERATOR(LOr) OPERATOR(Assign) OPERATOR(Comma)
58 // All compound assign operators.
60 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
61 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
65 // A helper macro to implement short-circuiting when recursing. It
66 // invokes CALL_EXPR, which must be a method call, on the derived
67 // object (s.t. a user of RecursiveASTVisitor can override the method
69 #define TRY_TO(CALL_EXPR) \
71 if (!getDerived().CALL_EXPR) \
75 /// \brief A class that does preorder depth-first traversal on the
76 /// entire Clang AST and visits each node.
78 /// This class performs three distinct tasks:
79 /// 1. traverse the AST (i.e. go to each node);
80 /// 2. at a given node, walk up the class hierarchy, starting from
81 /// the node's dynamic type, until the top-most class (e.g. Stmt,
82 /// Decl, or Type) is reached.
83 /// 3. given a (node, class) combination, where 'class' is some base
84 /// class of the dynamic type of 'node', call a user-overridable
85 /// function to actually visit the node.
87 /// These tasks are done by three groups of methods, respectively:
88 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
89 /// for traversing an AST rooted at x. This method simply
90 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
91 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
92 /// then recursively visits the child nodes of x.
93 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
95 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
96 /// any child node of x. Instead, it first calls WalkUpFromBar(x)
97 /// where Bar is the direct parent class of Foo (unless Foo has
98 /// no parent), and then calls VisitFoo(x) (see the next list item).
99 /// 3. VisitFoo(Foo *x) does task #3.
101 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
102 /// Visit*). A method (e.g. Traverse*) may call methods from the same
103 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
104 /// It may not call methods from a higher tier.
106 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
107 /// is Foo's super class) before calling VisitFoo(), the result is
108 /// that the Visit*() methods for a given node are called in the
109 /// top-down order (e.g. for a node of type NamespaceDecl, the order will
110 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
112 /// This scheme guarantees that all Visit*() calls for the same AST
113 /// node are grouped together. In other words, Visit*() methods for
114 /// different nodes are never interleaved.
116 /// Clients of this visitor should subclass the visitor (providing
117 /// themselves as the template argument, using the curiously recurring
118 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
119 /// and Visit* methods for declarations, types, statements,
120 /// expressions, or other AST nodes where the visitor should customize
121 /// behavior. Most users only need to override Visit*. Advanced
122 /// users may override Traverse* and WalkUpFrom* to implement custom
123 /// traversal strategies. Returning false from one of these overridden
124 /// functions will abort the entire traversal.
126 /// By default, this visitor tries to visit every part of the explicit
127 /// source code exactly once. The default policy towards templates
128 /// is to descend into the 'pattern' class or function body, not any
129 /// explicit or implicit instantiations. Explicit specializations
130 /// are still visited, and the patterns of partial specializations
131 /// are visited separately. This behavior can be changed by
132 /// overriding shouldVisitTemplateInstantiations() in the derived class
133 /// to return true, in which case all known implicit and explicit
134 /// instantiations will be visited at the same time as the pattern
135 /// from which they were produced.
136 template <typename Derived> class RecursiveASTVisitor {
138 /// A queue used for performing data recursion over statements.
139 /// Parameters involving this type are used to implement data
140 /// recursion over Stmts and Exprs within this class, and should
141 /// typically not be explicitly specified by derived classes.
142 typedef SmallVectorImpl<Stmt *> DataRecursionQueue;
144 /// \brief Return a reference to the derived class.
145 Derived &getDerived() { return *static_cast<Derived *>(this); }
147 /// \brief Return whether this visitor should recurse into
148 /// template instantiations.
149 bool shouldVisitTemplateInstantiations() const { return false; }
151 /// \brief Return whether this visitor should recurse into the types of
153 bool shouldWalkTypesOfTypeLocs() const { return true; }
155 /// \brief Return whether this visitor should recurse into implicit
156 /// code, e.g., implicit constructors and destructors.
157 bool shouldVisitImplicitCode() const { return false; }
159 /// \brief Recursively visit a statement or expression, by
160 /// dispatching to Traverse*() based on the argument's dynamic type.
162 /// \returns false if the visitation was terminated early, true
163 /// otherwise (including when the argument is nullptr).
164 bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr);
166 /// \brief Recursively visit a type, by dispatching to
167 /// Traverse*Type() based on the argument's getTypeClass() property.
169 /// \returns false if the visitation was terminated early, true
170 /// otherwise (including when the argument is a Null type).
171 bool TraverseType(QualType T);
173 /// \brief Recursively visit a type with location, by dispatching to
174 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
176 /// \returns false if the visitation was terminated early, true
177 /// otherwise (including when the argument is a Null type location).
178 bool TraverseTypeLoc(TypeLoc TL);
180 /// \brief Recursively visit an attribute, by dispatching to
181 /// Traverse*Attr() based on the argument's dynamic type.
183 /// \returns false if the visitation was terminated early, true
184 /// otherwise (including when the argument is a Null type location).
185 bool TraverseAttr(Attr *At);
187 /// \brief Recursively visit a declaration, by dispatching to
188 /// Traverse*Decl() based on the argument's dynamic type.
190 /// \returns false if the visitation was terminated early, true
191 /// otherwise (including when the argument is NULL).
192 bool TraverseDecl(Decl *D);
194 /// \brief Recursively visit a C++ nested-name-specifier.
196 /// \returns false if the visitation was terminated early, true otherwise.
197 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
199 /// \brief Recursively visit a C++ nested-name-specifier with location
202 /// \returns false if the visitation was terminated early, true otherwise.
203 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
205 /// \brief Recursively visit a name with its location information.
207 /// \returns false if the visitation was terminated early, true otherwise.
208 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
210 /// \brief Recursively visit a template name and dispatch to the
211 /// appropriate method.
213 /// \returns false if the visitation was terminated early, true otherwise.
214 bool TraverseTemplateName(TemplateName Template);
216 /// \brief Recursively visit a template argument and dispatch to the
217 /// appropriate method for the argument type.
219 /// \returns false if the visitation was terminated early, true otherwise.
220 // FIXME: migrate callers to TemplateArgumentLoc instead.
221 bool TraverseTemplateArgument(const TemplateArgument &Arg);
223 /// \brief Recursively visit a template argument location and dispatch to the
224 /// appropriate method for the argument type.
226 /// \returns false if the visitation was terminated early, true otherwise.
227 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
229 /// \brief Recursively visit a set of template arguments.
230 /// This can be overridden by a subclass, but it's not expected that
231 /// will be needed -- this visitor always dispatches to another.
233 /// \returns false if the visitation was terminated early, true otherwise.
234 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
235 bool TraverseTemplateArguments(const TemplateArgument *Args,
238 /// \brief Recursively visit a constructor initializer. This
239 /// automatically dispatches to another visitor for the initializer
240 /// expression, but not for the name of the initializer, so may
241 /// be overridden for clients that need access to the name.
243 /// \returns false if the visitation was terminated early, true otherwise.
244 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
246 /// \brief Recursively visit a lambda capture.
248 /// \returns false if the visitation was terminated early, true otherwise.
249 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C);
251 /// \brief Recursively visit the body of a lambda expression.
253 /// This provides a hook for visitors that need more context when visiting
254 /// \c LE->getBody().
256 /// \returns false if the visitation was terminated early, true otherwise.
257 bool TraverseLambdaBody(LambdaExpr *LE, DataRecursionQueue *Queue = nullptr);
259 /// \brief Recursively visit the syntactic or semantic form of an
260 /// initialization list.
262 /// \returns false if the visitation was terminated early, true otherwise.
263 bool TraverseSynOrSemInitListExpr(InitListExpr *S,
264 DataRecursionQueue *Queue = nullptr);
266 // ---- Methods on Attrs ----
268 // \brief Visit an attribute.
269 bool VisitAttr(Attr *A) { return true; }
271 // Declare Traverse* and empty Visit* for all Attr classes.
272 #define ATTR_VISITOR_DECLS_ONLY
273 #include "clang/AST/AttrVisitor.inc"
274 #undef ATTR_VISITOR_DECLS_ONLY
276 // ---- Methods on Stmts ----
279 template<typename T, typename U>
280 struct has_same_member_pointer_type : std::false_type {};
281 template<typename T, typename U, typename R, typename... P>
282 struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)>
285 // Traverse the given statement. If the most-derived traverse function takes a
286 // data recursion queue, pass it on; otherwise, discard it. Note that the
287 // first branch of this conditional must compile whether or not the derived
288 // class can take a queue, so if we're taking the second arm, make the first
289 // arm call our function rather than the derived class version.
290 #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE) \
291 (has_same_member_pointer_type<decltype( \
292 &RecursiveASTVisitor::Traverse##NAME), \
293 decltype(&Derived::Traverse##NAME)>::value \
294 ? static_cast<typename std::conditional< \
295 has_same_member_pointer_type< \
296 decltype(&RecursiveASTVisitor::Traverse##NAME), \
297 decltype(&Derived::Traverse##NAME)>::value, \
298 Derived &, RecursiveASTVisitor &>::type>(*this) \
299 .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE) \
300 : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)))
302 // Try to traverse the given statement, or enqueue it if we're performing data
303 // recursion in the middle of traversing another statement. Can only be called
304 // from within a DEF_TRAVERSE_STMT body or similar context.
305 #define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S) \
307 if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue)) \
312 // Declare Traverse*() for all concrete Stmt classes.
313 #define ABSTRACT_STMT(STMT)
314 #define STMT(CLASS, PARENT) \
315 bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
316 #include "clang/AST/StmtNodes.inc"
317 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
319 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
320 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
321 bool VisitStmt(Stmt *S) { return true; }
322 #define STMT(CLASS, PARENT) \
323 bool WalkUpFrom##CLASS(CLASS *S) { \
324 TRY_TO(WalkUpFrom##PARENT(S)); \
325 TRY_TO(Visit##CLASS(S)); \
328 bool Visit##CLASS(CLASS *S) { return true; }
329 #include "clang/AST/StmtNodes.inc"
331 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
332 // operator methods. Unary operators are not classes in themselves
333 // (they're all opcodes in UnaryOperator) but do have visitors.
334 #define OPERATOR(NAME) \
335 bool TraverseUnary##NAME(UnaryOperator *S, \
336 DataRecursionQueue *Queue = nullptr) { \
337 TRY_TO(WalkUpFromUnary##NAME(S)); \
338 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr()); \
341 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
342 TRY_TO(WalkUpFromUnaryOperator(S)); \
343 TRY_TO(VisitUnary##NAME(S)); \
346 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
351 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
352 // operator methods. Binary operators are not classes in themselves
353 // (they're all opcodes in BinaryOperator) but do have visitors.
354 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
355 bool TraverseBin##NAME(BINOP_TYPE *S, DataRecursionQueue *Queue = nullptr) { \
356 TRY_TO(WalkUpFromBin##NAME(S)); \
357 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLHS()); \
358 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRHS()); \
361 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
362 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
363 TRY_TO(VisitBin##NAME(S)); \
366 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
368 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
372 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
373 // assignment methods. Compound assignment operators are not
374 // classes in themselves (they're all opcodes in
375 // CompoundAssignOperator) but do have visitors.
376 #define OPERATOR(NAME) \
377 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
381 #undef GENERAL_BINOP_FALLBACK
383 // ---- Methods on Types ----
384 // FIXME: revamp to take TypeLoc's rather than Types.
386 // Declare Traverse*() for all concrete Type classes.
387 #define ABSTRACT_TYPE(CLASS, BASE)
388 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T);
389 #include "clang/AST/TypeNodes.def"
390 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
392 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
393 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
394 bool VisitType(Type *T) { return true; }
395 #define TYPE(CLASS, BASE) \
396 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
397 TRY_TO(WalkUpFrom##BASE(T)); \
398 TRY_TO(Visit##CLASS##Type(T)); \
401 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
402 #include "clang/AST/TypeNodes.def"
404 // ---- Methods on TypeLocs ----
405 // FIXME: this currently just calls the matching Type methods
407 // Declare Traverse*() for all concrete TypeLoc classes.
408 #define ABSTRACT_TYPELOC(CLASS, BASE)
409 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
410 #include "clang/AST/TypeLocNodes.def"
411 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
413 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
414 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
415 bool VisitTypeLoc(TypeLoc TL) { return true; }
417 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
418 // TypeNodes.def and thus need to be handled specially.
419 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
420 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
422 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
423 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
424 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
426 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
428 // Note that BASE includes trailing 'Type' which CLASS doesn't.
429 #define TYPE(CLASS, BASE) \
430 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
431 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
432 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
435 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
436 #include "clang/AST/TypeNodes.def"
438 // ---- Methods on Decls ----
440 // Declare Traverse*() for all concrete Decl classes.
441 #define ABSTRACT_DECL(DECL)
442 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D);
443 #include "clang/AST/DeclNodes.inc"
444 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
446 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
447 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
448 bool VisitDecl(Decl *D) { return true; }
449 #define DECL(CLASS, BASE) \
450 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
451 TRY_TO(WalkUpFrom##BASE(D)); \
452 TRY_TO(Visit##CLASS##Decl(D)); \
455 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
456 #include "clang/AST/DeclNodes.inc"
459 // These are helper methods used by more than one Traverse* method.
460 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
461 #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \
462 bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D);
463 DEF_TRAVERSE_TMPL_INST(Class)
464 DEF_TRAVERSE_TMPL_INST(Var)
465 DEF_TRAVERSE_TMPL_INST(Function)
466 #undef DEF_TRAVERSE_TMPL_INST
467 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
469 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
470 bool TraverseRecordHelper(RecordDecl *D);
471 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
472 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
473 bool TraverseDeclContextHelper(DeclContext *DC);
474 bool TraverseFunctionHelper(FunctionDecl *D);
475 bool TraverseVarHelper(VarDecl *D);
476 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
477 bool TraverseOMPLoopDirective(OMPLoopDirective *S);
478 bool TraverseOMPClause(OMPClause *C);
479 #define OPENMP_CLAUSE(Name, Class) bool Visit##Class(Class *C);
480 #include "clang/Basic/OpenMPKinds.def"
481 /// \brief Process clauses with list of variables.
482 template <typename T> bool VisitOMPClauseList(T *Node);
484 bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
487 template <typename Derived>
488 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
489 DataRecursionQueue *Queue) {
490 #define DISPATCH_STMT(NAME, CLASS, VAR) \
491 return TRAVERSE_STMT_BASE(NAME, CLASS, VAR, Queue);
493 // If we have a binary expr, dispatch to the subcode of the binop. A smart
494 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
496 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
497 switch (BinOp->getOpcode()) {
498 #define OPERATOR(NAME) \
500 DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
506 #define OPERATOR(NAME) \
507 case BO_##NAME##Assign: \
508 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
514 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
515 switch (UnOp->getOpcode()) {
516 #define OPERATOR(NAME) \
518 DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
526 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
527 switch (S->getStmtClass()) {
528 case Stmt::NoStmtClass:
530 #define ABSTRACT_STMT(STMT)
531 #define STMT(CLASS, PARENT) \
532 case Stmt::CLASS##Class: \
533 DISPATCH_STMT(CLASS, CLASS, S);
534 #include "clang/AST/StmtNodes.inc"
542 template <typename Derived>
543 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
544 DataRecursionQueue *Queue) {
553 SmallVector<Stmt *, 8> LocalQueue;
554 LocalQueue.push_back(S);
556 while (!LocalQueue.empty()) {
557 Stmt *CurrS = LocalQueue.pop_back_val();
559 size_t N = LocalQueue.size();
560 TRY_TO(dataTraverseNode(CurrS, &LocalQueue));
561 // Process new children in the order they were added.
562 std::reverse(LocalQueue.begin() + N, LocalQueue.end());
568 #define DISPATCH(NAME, CLASS, VAR) \
569 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))
571 template <typename Derived>
572 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
576 switch (T->getTypeClass()) {
577 #define ABSTRACT_TYPE(CLASS, BASE)
578 #define TYPE(CLASS, BASE) \
580 DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr()));
581 #include "clang/AST/TypeNodes.def"
587 template <typename Derived>
588 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
592 switch (TL.getTypeLocClass()) {
593 #define ABSTRACT_TYPELOC(CLASS, BASE)
594 #define TYPELOC(CLASS, BASE) \
595 case TypeLoc::CLASS: \
596 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
597 #include "clang/AST/TypeLocNodes.def"
603 // Define the Traverse*Attr(Attr* A) methods
604 #define VISITORCLASS RecursiveASTVisitor
605 #include "clang/AST/AttrVisitor.inc"
608 template <typename Derived>
609 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
613 // As a syntax visitor, by default we want to ignore declarations for
614 // implicit declarations (ones not typed explicitly by the user).
615 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
618 switch (D->getKind()) {
619 #define ABSTRACT_DECL(DECL)
620 #define DECL(CLASS, BASE) \
622 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \
625 #include "clang/AST/DeclNodes.inc"
628 // Visit any attributes attached to this declaration.
629 for (auto *I : D->attrs()) {
630 if (!getDerived().TraverseAttr(I))
638 template <typename Derived>
639 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
640 NestedNameSpecifier *NNS) {
644 if (NNS->getPrefix())
645 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
647 switch (NNS->getKind()) {
648 case NestedNameSpecifier::Identifier:
649 case NestedNameSpecifier::Namespace:
650 case NestedNameSpecifier::NamespaceAlias:
651 case NestedNameSpecifier::Global:
652 case NestedNameSpecifier::Super:
655 case NestedNameSpecifier::TypeSpec:
656 case NestedNameSpecifier::TypeSpecWithTemplate:
657 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
663 template <typename Derived>
664 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
665 NestedNameSpecifierLoc NNS) {
669 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
670 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
672 switch (NNS.getNestedNameSpecifier()->getKind()) {
673 case NestedNameSpecifier::Identifier:
674 case NestedNameSpecifier::Namespace:
675 case NestedNameSpecifier::NamespaceAlias:
676 case NestedNameSpecifier::Global:
677 case NestedNameSpecifier::Super:
680 case NestedNameSpecifier::TypeSpec:
681 case NestedNameSpecifier::TypeSpecWithTemplate:
682 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
689 template <typename Derived>
690 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
691 DeclarationNameInfo NameInfo) {
692 switch (NameInfo.getName().getNameKind()) {
693 case DeclarationName::CXXConstructorName:
694 case DeclarationName::CXXDestructorName:
695 case DeclarationName::CXXConversionFunctionName:
696 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
697 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
701 case DeclarationName::Identifier:
702 case DeclarationName::ObjCZeroArgSelector:
703 case DeclarationName::ObjCOneArgSelector:
704 case DeclarationName::ObjCMultiArgSelector:
705 case DeclarationName::CXXOperatorName:
706 case DeclarationName::CXXLiteralOperatorName:
707 case DeclarationName::CXXUsingDirective:
714 template <typename Derived>
715 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
716 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
717 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
718 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
719 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
724 template <typename Derived>
725 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
726 const TemplateArgument &Arg) {
727 switch (Arg.getKind()) {
728 case TemplateArgument::Null:
729 case TemplateArgument::Declaration:
730 case TemplateArgument::Integral:
731 case TemplateArgument::NullPtr:
734 case TemplateArgument::Type:
735 return getDerived().TraverseType(Arg.getAsType());
737 case TemplateArgument::Template:
738 case TemplateArgument::TemplateExpansion:
739 return getDerived().TraverseTemplateName(
740 Arg.getAsTemplateOrTemplatePattern());
742 case TemplateArgument::Expression:
743 return getDerived().TraverseStmt(Arg.getAsExpr());
745 case TemplateArgument::Pack:
746 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
753 // FIXME: no template name location?
754 // FIXME: no source locations for a template argument pack?
755 template <typename Derived>
756 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
757 const TemplateArgumentLoc &ArgLoc) {
758 const TemplateArgument &Arg = ArgLoc.getArgument();
760 switch (Arg.getKind()) {
761 case TemplateArgument::Null:
762 case TemplateArgument::Declaration:
763 case TemplateArgument::Integral:
764 case TemplateArgument::NullPtr:
767 case TemplateArgument::Type: {
768 // FIXME: how can TSI ever be NULL?
769 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
770 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
772 return getDerived().TraverseType(Arg.getAsType());
775 case TemplateArgument::Template:
776 case TemplateArgument::TemplateExpansion:
777 if (ArgLoc.getTemplateQualifierLoc())
778 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
779 ArgLoc.getTemplateQualifierLoc()));
780 return getDerived().TraverseTemplateName(
781 Arg.getAsTemplateOrTemplatePattern());
783 case TemplateArgument::Expression:
784 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
786 case TemplateArgument::Pack:
787 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
794 template <typename Derived>
795 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
796 const TemplateArgument *Args, unsigned NumArgs) {
797 for (unsigned I = 0; I != NumArgs; ++I) {
798 TRY_TO(TraverseTemplateArgument(Args[I]));
804 template <typename Derived>
805 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
806 CXXCtorInitializer *Init) {
807 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
808 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
810 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
811 TRY_TO(TraverseStmt(Init->getInit()));
815 template <typename Derived>
817 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
818 const LambdaCapture *C) {
819 if (LE->isInitCapture(C))
820 TRY_TO(TraverseDecl(C->getCapturedVar()));
824 template <typename Derived>
825 bool RecursiveASTVisitor<Derived>::TraverseLambdaBody(
826 LambdaExpr *LE, DataRecursionQueue *Queue) {
827 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(LE->getBody());
831 // ----------------- Type traversal -----------------
833 // This macro makes available a variable T, the passed-in type.
834 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
835 template <typename Derived> \
836 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \
837 TRY_TO(WalkUpFrom##TYPE(T)); \
842 DEF_TRAVERSE_TYPE(BuiltinType, {})
844 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
846 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
848 DEF_TRAVERSE_TYPE(BlockPointerType,
849 { TRY_TO(TraverseType(T->getPointeeType())); })
851 DEF_TRAVERSE_TYPE(LValueReferenceType,
852 { TRY_TO(TraverseType(T->getPointeeType())); })
854 DEF_TRAVERSE_TYPE(RValueReferenceType,
855 { TRY_TO(TraverseType(T->getPointeeType())); })
857 DEF_TRAVERSE_TYPE(MemberPointerType, {
858 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
859 TRY_TO(TraverseType(T->getPointeeType()));
862 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
864 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
866 DEF_TRAVERSE_TYPE(ConstantArrayType,
867 { TRY_TO(TraverseType(T->getElementType())); })
869 DEF_TRAVERSE_TYPE(IncompleteArrayType,
870 { TRY_TO(TraverseType(T->getElementType())); })
872 DEF_TRAVERSE_TYPE(VariableArrayType, {
873 TRY_TO(TraverseType(T->getElementType()));
874 TRY_TO(TraverseStmt(T->getSizeExpr()));
877 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
878 TRY_TO(TraverseType(T->getElementType()));
879 if (T->getSizeExpr())
880 TRY_TO(TraverseStmt(T->getSizeExpr()));
883 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
884 if (T->getSizeExpr())
885 TRY_TO(TraverseStmt(T->getSizeExpr()));
886 TRY_TO(TraverseType(T->getElementType()));
889 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
891 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
893 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
894 { TRY_TO(TraverseType(T->getReturnType())); })
896 DEF_TRAVERSE_TYPE(FunctionProtoType, {
897 TRY_TO(TraverseType(T->getReturnType()));
899 for (const auto &A : T->param_types()) {
900 TRY_TO(TraverseType(A));
903 for (const auto &E : T->exceptions()) {
904 TRY_TO(TraverseType(E));
907 if (Expr *NE = T->getNoexceptExpr())
908 TRY_TO(TraverseStmt(NE));
911 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
912 DEF_TRAVERSE_TYPE(TypedefType, {})
914 DEF_TRAVERSE_TYPE(TypeOfExprType,
915 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
917 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
919 DEF_TRAVERSE_TYPE(DecltypeType,
920 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
922 DEF_TRAVERSE_TYPE(UnaryTransformType, {
923 TRY_TO(TraverseType(T->getBaseType()));
924 TRY_TO(TraverseType(T->getUnderlyingType()));
927 DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); })
929 DEF_TRAVERSE_TYPE(RecordType, {})
930 DEF_TRAVERSE_TYPE(EnumType, {})
931 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
932 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {})
933 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {})
935 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
936 TRY_TO(TraverseTemplateName(T->getTemplateName()));
937 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
940 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
942 DEF_TRAVERSE_TYPE(AttributedType,
943 { TRY_TO(TraverseType(T->getModifiedType())); })
945 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
947 DEF_TRAVERSE_TYPE(ElaboratedType, {
948 if (T->getQualifier()) {
949 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
951 TRY_TO(TraverseType(T->getNamedType()));
954 DEF_TRAVERSE_TYPE(DependentNameType,
955 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
957 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
958 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
959 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
962 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
964 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
966 DEF_TRAVERSE_TYPE(ObjCObjectType, {
967 // We have to watch out here because an ObjCInterfaceType's base
969 if (T->getBaseType().getTypePtr() != T)
970 TRY_TO(TraverseType(T->getBaseType()));
971 for (auto typeArg : T->getTypeArgsAsWritten()) {
972 TRY_TO(TraverseType(typeArg));
976 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
977 { TRY_TO(TraverseType(T->getPointeeType())); })
979 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
981 #undef DEF_TRAVERSE_TYPE
983 // ----------------- TypeLoc traversal -----------------
985 // This macro makes available a variable TL, the passed-in TypeLoc.
986 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
987 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
988 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
990 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
991 template <typename Derived> \
992 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
993 if (getDerived().shouldWalkTypesOfTypeLocs()) \
994 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
995 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1000 template <typename Derived>
1002 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1003 // Move this over to the 'main' typeloc tree. Note that this is a
1004 // move -- we pretend that we were really looking at the unqualified
1005 // typeloc all along -- rather than a recursion, so we don't follow
1006 // the normal CRTP plan of going through
1007 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1008 // twice for the same type (once as a QualifiedTypeLoc version of
1009 // the type, once as an UnqualifiedTypeLoc version of the type),
1010 // which in effect means we'd call VisitTypeLoc twice with the
1011 // 'same' type. This solves that problem, at the cost of never
1012 // seeing the qualified version of the type (unless the client
1013 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1014 // perfect solution. A perfect solution probably requires making
1015 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1016 // wrapper around Type* -- rather than being its own class in the
1018 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1021 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1023 // FIXME: ComplexTypeLoc is unfinished
1024 DEF_TRAVERSE_TYPELOC(ComplexType, {
1025 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1028 DEF_TRAVERSE_TYPELOC(PointerType,
1029 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1031 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1032 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1034 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1035 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1037 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1038 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1040 // FIXME: location of base class?
1041 // We traverse this in the type case as well, but how is it not reached through
1042 // the pointee type?
1043 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1044 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1045 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1048 DEF_TRAVERSE_TYPELOC(AdjustedType,
1049 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1051 DEF_TRAVERSE_TYPELOC(DecayedType,
1052 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1054 template <typename Derived>
1055 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1056 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1057 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1061 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1062 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1063 return TraverseArrayTypeLocHelper(TL);
1066 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1067 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1068 return TraverseArrayTypeLocHelper(TL);
1071 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1072 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1073 return TraverseArrayTypeLocHelper(TL);
1076 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1077 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1078 return TraverseArrayTypeLocHelper(TL);
1081 // FIXME: order? why not size expr first?
1082 // FIXME: base VectorTypeLoc is unfinished
1083 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1084 if (TL.getTypePtr()->getSizeExpr())
1085 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1086 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1089 // FIXME: VectorTypeLoc is unfinished
1090 DEF_TRAVERSE_TYPELOC(VectorType, {
1091 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1094 // FIXME: size and attributes
1095 // FIXME: base VectorTypeLoc is unfinished
1096 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1097 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1100 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1101 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1103 // FIXME: location of exception specifications (attributes?)
1104 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1105 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1107 const FunctionProtoType *T = TL.getTypePtr();
1109 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1110 if (TL.getParam(I)) {
1111 TRY_TO(TraverseDecl(TL.getParam(I)));
1112 } else if (I < T->getNumParams()) {
1113 TRY_TO(TraverseType(T->getParamType(I)));
1117 for (const auto &E : T->exceptions()) {
1118 TRY_TO(TraverseType(E));
1121 if (Expr *NE = T->getNoexceptExpr())
1122 TRY_TO(TraverseStmt(NE));
1125 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1126 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1128 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1129 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1131 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1132 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1135 // FIXME: location of underlying expr
1136 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1137 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1140 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1141 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1144 DEF_TRAVERSE_TYPELOC(AutoType, {
1145 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1148 DEF_TRAVERSE_TYPELOC(RecordType, {})
1149 DEF_TRAVERSE_TYPELOC(EnumType, {})
1150 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1151 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {})
1152 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {})
1154 // FIXME: use the loc for the template name?
1155 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1156 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1157 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1158 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1162 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1164 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1166 DEF_TRAVERSE_TYPELOC(AttributedType,
1167 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1169 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1170 if (TL.getQualifierLoc()) {
1171 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1173 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1176 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1177 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1180 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1181 if (TL.getQualifierLoc()) {
1182 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1185 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1186 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1190 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1191 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1193 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1195 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1196 // We have to watch out here because an ObjCInterfaceType's base
1198 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1199 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1200 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1201 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1204 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1205 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1207 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1209 #undef DEF_TRAVERSE_TYPELOC
1211 // ----------------- Decl traversal -----------------
1213 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1214 // the children that come from the DeclContext associated with it.
1215 // Therefore each Traverse* only needs to worry about children other
1218 template <typename Derived>
1219 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1223 for (auto *Child : DC->decls()) {
1224 // BlockDecls and CapturedDecls are traversed through BlockExprs and
1225 // CapturedStmts respectively.
1226 if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child))
1227 TRY_TO(TraverseDecl(Child));
1233 // This macro makes available a variable D, the passed-in decl.
1234 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1235 template <typename Derived> \
1236 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \
1237 TRY_TO(WalkUpFrom##DECL(D)); \
1239 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1243 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1245 DEF_TRAVERSE_DECL(BlockDecl, {
1246 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1247 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1248 TRY_TO(TraverseStmt(D->getBody()));
1249 for (const auto &I : D->captures()) {
1250 if (I.hasCopyExpr()) {
1251 TRY_TO(TraverseStmt(I.getCopyExpr()));
1254 // This return statement makes sure the traversal of nodes in
1255 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1256 // is skipped - don't remove it.
1260 DEF_TRAVERSE_DECL(CapturedDecl, {
1261 TRY_TO(TraverseStmt(D->getBody()));
1262 // This return statement makes sure the traversal of nodes in
1263 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1264 // is skipped - don't remove it.
1268 DEF_TRAVERSE_DECL(EmptyDecl, {})
1270 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1271 { TRY_TO(TraverseStmt(D->getAsmString())); })
1273 DEF_TRAVERSE_DECL(ImportDecl, {})
1275 DEF_TRAVERSE_DECL(FriendDecl, {
1276 // Friend is either decl or a type.
1277 if (D->getFriendType())
1278 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1280 TRY_TO(TraverseDecl(D->getFriendDecl()));
1283 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1284 if (D->getFriendType())
1285 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1287 TRY_TO(TraverseDecl(D->getFriendDecl()));
1288 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1289 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1290 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1291 ITPL != ETPL; ++ITPL) {
1292 TRY_TO(TraverseDecl(*ITPL));
1297 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1298 TRY_TO(TraverseDecl(D->getSpecialization()));
1300 if (D->hasExplicitTemplateArgs()) {
1301 const TemplateArgumentListInfo &args = D->templateArgs();
1302 TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1307 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1309 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1312 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1313 TRY_TO(TraverseStmt(D->getAssertExpr()));
1314 TRY_TO(TraverseStmt(D->getMessage()));
1318 TranslationUnitDecl,
1319 {// Code in an unnamed namespace shows up automatically in
1320 // decls_begin()/decls_end(). Thus we don't need to recurse on
1321 // D->getAnonymousNamespace().
1324 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1326 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1327 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1329 // We shouldn't traverse an aliased namespace, since it will be
1330 // defined (and, therefore, traversed) somewhere else.
1332 // This return statement makes sure the traversal of nodes in
1333 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1334 // is skipped - don't remove it.
1338 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1343 {// Code in an unnamed namespace shows up automatically in
1344 // decls_begin()/decls_end(). Thus we don't need to recurse on
1345 // D->getAnonymousNamespace().
1348 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1351 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1352 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1353 for (auto typeParam : *typeParamList) {
1354 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1359 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1362 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1365 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1366 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1367 for (auto typeParam : *typeParamList) {
1368 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1372 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1373 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1377 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1380 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1381 if (D->getReturnTypeSourceInfo()) {
1382 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1384 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1386 TRY_TO(TraverseDecl(*I));
1388 if (D->isThisDeclarationADefinition()) {
1389 TRY_TO(TraverseStmt(D->getBody()));
1394 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1395 if (D->hasExplicitBound()) {
1396 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1397 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1398 // declaring the type alias, not something that was written in the
1403 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1404 if (D->getTypeSourceInfo())
1405 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1407 TRY_TO(TraverseType(D->getType()));
1411 DEF_TRAVERSE_DECL(UsingDecl, {
1412 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1413 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1416 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1417 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1420 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1422 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1423 for (auto *I : D->varlists()) {
1424 TRY_TO(TraverseStmt(I));
1428 // A helper method for TemplateDecl's children.
1429 template <typename Derived>
1430 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1431 TemplateParameterList *TPL) {
1433 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1435 TRY_TO(TraverseDecl(*I));
1441 template <typename Derived>
1442 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1443 ClassTemplateDecl *D) {
1444 for (auto *SD : D->specializations()) {
1445 for (auto *RD : SD->redecls()) {
1446 // We don't want to visit injected-class-names in this traversal.
1447 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1451 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1452 // Visit the implicit instantiations with the requested pattern.
1453 case TSK_Undeclared:
1454 case TSK_ImplicitInstantiation:
1455 TRY_TO(TraverseDecl(RD));
1458 // We don't need to do anything on an explicit instantiation
1459 // or explicit specialization because there will be an explicit
1460 // node for it elsewhere.
1461 case TSK_ExplicitInstantiationDeclaration:
1462 case TSK_ExplicitInstantiationDefinition:
1463 case TSK_ExplicitSpecialization:
1472 template <typename Derived>
1473 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1474 VarTemplateDecl *D) {
1475 for (auto *SD : D->specializations()) {
1476 for (auto *RD : SD->redecls()) {
1478 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1479 case TSK_Undeclared:
1480 case TSK_ImplicitInstantiation:
1481 TRY_TO(TraverseDecl(RD));
1484 case TSK_ExplicitInstantiationDeclaration:
1485 case TSK_ExplicitInstantiationDefinition:
1486 case TSK_ExplicitSpecialization:
1495 // A helper method for traversing the instantiations of a
1496 // function while skipping its specializations.
1497 template <typename Derived>
1498 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1499 FunctionTemplateDecl *D) {
1500 for (auto *FD : D->specializations()) {
1501 for (auto *RD : FD->redecls()) {
1502 switch (RD->getTemplateSpecializationKind()) {
1503 case TSK_Undeclared:
1504 case TSK_ImplicitInstantiation:
1505 // We don't know what kind of FunctionDecl this is.
1506 TRY_TO(TraverseDecl(RD));
1509 // FIXME: For now traverse explicit instantiations here. Change that
1510 // once they are represented as dedicated nodes in the AST.
1511 case TSK_ExplicitInstantiationDeclaration:
1512 case TSK_ExplicitInstantiationDefinition:
1513 TRY_TO(TraverseDecl(RD));
1516 case TSK_ExplicitSpecialization:
1525 // This macro unifies the traversal of class, variable and function
1526 // template declarations.
1527 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1528 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1529 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1530 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1532 /* By default, we do not traverse the instantiations of \
1533 class templates since they do not appear in the user code. The \
1534 following code optionally traverses them. \
1536 We only traverse the class instantiations when we see the canonical \
1537 declaration of the template, to ensure we only visit them once. */ \
1538 if (getDerived().shouldVisitTemplateInstantiations() && \
1539 D == D->getCanonicalDecl()) \
1540 TRY_TO(TraverseTemplateInstantiations(D)); \
1542 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1543 from a template instantiation back to the template from which \
1544 it was instantiated, and thus should not be traversed. */ \
1547 DEF_TRAVERSE_TMPL_DECL(Class)
1548 DEF_TRAVERSE_TMPL_DECL(Var)
1549 DEF_TRAVERSE_TMPL_DECL(Function)
1551 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1552 // D is the "T" in something like
1553 // template <template <typename> class T> class container { };
1554 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1555 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1556 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1558 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1561 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1562 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1565 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1566 // D is the "T" in something like "template<typename T> class vector;"
1567 if (D->getTypeForDecl())
1568 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1569 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1570 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1573 DEF_TRAVERSE_DECL(TypedefDecl, {
1574 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1575 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1576 // declaring the typedef, not something that was written in the
1580 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1581 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1582 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1583 // declaring the type alias, not something that was written in the
1587 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1588 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1589 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1592 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1593 // A dependent using declaration which was marked with 'typename'.
1594 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1595 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1596 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1597 // declaring the type, not something that was written in the
1601 DEF_TRAVERSE_DECL(EnumDecl, {
1602 if (D->getTypeForDecl())
1603 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1605 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1606 // The enumerators are already traversed by
1607 // decls_begin()/decls_end().
1610 // Helper methods for RecordDecl and its children.
1611 template <typename Derived>
1612 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1613 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1614 // declaring the type, not something that was written in the source.
1616 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1620 template <typename Derived>
1621 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1622 if (!TraverseRecordHelper(D))
1624 if (D->isCompleteDefinition()) {
1625 for (const auto &I : D->bases()) {
1626 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1628 // We don't traverse the friends or the conversions, as they are
1629 // already in decls_begin()/decls_end().
1634 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1636 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1638 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1639 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1640 /* For implicit instantiations ("set<int> x;"), we don't want to \
1641 recurse at all, since the instatiated template isn't written in \
1642 the source code anywhere. (Note the instatiated *type* -- \
1643 set<int> -- is written, and will still get a callback of \
1644 TemplateSpecializationType). For explicit instantiations \
1645 ("template set<int>;"), we do need a callback, since this \
1646 is the only callback that's made for this instantiation. \
1647 We use getTypeAsWritten() to distinguish. */ \
1648 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1649 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1651 if (!getDerived().shouldVisitTemplateInstantiations() && \
1652 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1653 /* Returning from here skips traversing the \
1654 declaration context of the *TemplateSpecializationDecl \
1655 (embedded in the DEF_TRAVERSE_DECL() macro) \
1656 which contains the instantiated members of the template. */ \
1660 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1661 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1663 template <typename Derived>
1664 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1665 const TemplateArgumentLoc *TAL, unsigned Count) {
1666 for (unsigned I = 0; I < Count; ++I) {
1667 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1672 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1673 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1674 /* The partial specialization. */ \
1675 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1676 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1678 TRY_TO(TraverseDecl(*I)); \
1681 /* The args that remains unspecialized. */ \
1682 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1683 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1684 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1686 /* Don't need the *TemplatePartialSpecializationHelper, even \
1687 though that's our parent class -- we already visit all the \
1688 template args here. */ \
1689 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1691 /* Instantiations will have been visited with the primary template. */ \
1694 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1695 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1697 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1699 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1700 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1701 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1702 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1703 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1706 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1708 template <typename Derived>
1709 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1710 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1711 if (D->getTypeSourceInfo())
1712 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1714 TRY_TO(TraverseType(D->getType()));
1718 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1720 DEF_TRAVERSE_DECL(FieldDecl, {
1721 TRY_TO(TraverseDeclaratorHelper(D));
1722 if (D->isBitField())
1723 TRY_TO(TraverseStmt(D->getBitWidth()));
1724 else if (D->hasInClassInitializer())
1725 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1728 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1729 TRY_TO(TraverseDeclaratorHelper(D));
1730 if (D->isBitField())
1731 TRY_TO(TraverseStmt(D->getBitWidth()));
1732 // FIXME: implement the rest.
1735 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1736 TRY_TO(TraverseDeclaratorHelper(D));
1737 if (D->isBitField())
1738 TRY_TO(TraverseStmt(D->getBitWidth()));
1739 // FIXME: implement the rest.
1742 template <typename Derived>
1743 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1744 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1745 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1747 // If we're an explicit template specialization, iterate over the
1748 // template args that were explicitly specified. If we were doing
1749 // this in typing order, we'd do it between the return type and
1750 // the function args, but both are handled by the FunctionTypeLoc
1751 // above, so we have to choose one side. I've decided to do before.
1752 if (const FunctionTemplateSpecializationInfo *FTSI =
1753 D->getTemplateSpecializationInfo()) {
1754 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1755 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1756 // A specialization might not have explicit template arguments if it has
1757 // a templated return type and concrete arguments.
1758 if (const ASTTemplateArgumentListInfo *TALI =
1759 FTSI->TemplateArgumentsAsWritten) {
1760 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1761 TALI->NumTemplateArgs));
1766 // Visit the function type itself, which can be either
1767 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1768 // also covers the return type and the function parameters,
1769 // including exception specifications.
1770 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1771 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1772 } else if (getDerived().shouldVisitImplicitCode()) {
1773 // Visit parameter variable declarations of the implicit function
1774 // if the traverser is visiting implicit code. Parameter variable
1775 // declarations do not have valid TypeSourceInfo, so to visit them
1776 // we need to traverse the declarations explicitly.
1777 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1780 TRY_TO(TraverseDecl(*I));
1783 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1784 // Constructor initializers.
1785 for (auto *I : Ctor->inits()) {
1786 TRY_TO(TraverseConstructorInitializer(I));
1790 if (D->isThisDeclarationADefinition()) {
1791 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1796 DEF_TRAVERSE_DECL(FunctionDecl, {
1797 // We skip decls_begin/decls_end, which are already covered by
1798 // TraverseFunctionHelper().
1799 return TraverseFunctionHelper(D);
1802 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1803 // We skip decls_begin/decls_end, which are already covered by
1804 // TraverseFunctionHelper().
1805 return TraverseFunctionHelper(D);
1808 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1809 // We skip decls_begin/decls_end, which are already covered by
1810 // TraverseFunctionHelper().
1811 return TraverseFunctionHelper(D);
1814 // CXXConversionDecl is the declaration of a type conversion operator.
1815 // It's not a cast expression.
1816 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1817 // We skip decls_begin/decls_end, which are already covered by
1818 // TraverseFunctionHelper().
1819 return TraverseFunctionHelper(D);
1822 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1823 // We skip decls_begin/decls_end, which are already covered by
1824 // TraverseFunctionHelper().
1825 return TraverseFunctionHelper(D);
1828 template <typename Derived>
1829 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1830 TRY_TO(TraverseDeclaratorHelper(D));
1831 // Default params are taken care of when we traverse the ParmVarDecl.
1832 if (!isa<ParmVarDecl>(D) &&
1833 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1834 TRY_TO(TraverseStmt(D->getInit()));
1838 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1840 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1842 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1843 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1844 TRY_TO(TraverseDeclaratorHelper(D));
1845 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1846 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1849 DEF_TRAVERSE_DECL(ParmVarDecl, {
1850 TRY_TO(TraverseVarHelper(D));
1852 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1853 !D->hasUnparsedDefaultArg())
1854 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1856 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1857 !D->hasUnparsedDefaultArg())
1858 TRY_TO(TraverseStmt(D->getDefaultArg()));
1861 #undef DEF_TRAVERSE_DECL
1863 // ----------------- Stmt traversal -----------------
1865 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1866 // over the children defined in children() (every stmt defines these,
1867 // though sometimes the range is empty). Each individual Traverse*
1868 // method only needs to worry about children other than those. To see
1869 // what children() does for a given class, see, e.g.,
1870 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1872 // This macro makes available a variable S, the passed-in stmt.
1873 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1874 template <typename Derived> \
1875 bool RecursiveASTVisitor<Derived>::Traverse##STMT( \
1876 STMT *S, DataRecursionQueue *Queue) { \
1877 TRY_TO(WalkUpFrom##STMT(S)); \
1879 for (Stmt *SubStmt : S->children()) { \
1880 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \
1885 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1886 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
1887 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1888 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
1890 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1891 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
1893 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1894 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
1896 // children() iterates over inputExpr and outputExpr.
1901 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1902 // added this needs to be implemented.
1905 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1906 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1907 // children() iterates over the handler block.
1910 DEF_TRAVERSE_STMT(DeclStmt, {
1911 for (auto *I : S->decls()) {
1912 TRY_TO(TraverseDecl(I));
1914 // Suppress the default iteration over children() by
1915 // returning. Here's why: A DeclStmt looks like 'type var [=
1916 // initializer]'. The decls above already traverse over the
1917 // initializers, so we don't have to do it again (which
1918 // children() would do).
1922 // These non-expr stmts (most of them), do not need any action except
1923 // iterating over the children.
1924 DEF_TRAVERSE_STMT(BreakStmt, {})
1925 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1926 DEF_TRAVERSE_STMT(CaseStmt, {})
1927 DEF_TRAVERSE_STMT(CompoundStmt, {})
1928 DEF_TRAVERSE_STMT(ContinueStmt, {})
1929 DEF_TRAVERSE_STMT(DefaultStmt, {})
1930 DEF_TRAVERSE_STMT(DoStmt, {})
1931 DEF_TRAVERSE_STMT(ForStmt, {})
1932 DEF_TRAVERSE_STMT(GotoStmt, {})
1933 DEF_TRAVERSE_STMT(IfStmt, {})
1934 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1935 DEF_TRAVERSE_STMT(LabelStmt, {})
1936 DEF_TRAVERSE_STMT(AttributedStmt, {})
1937 DEF_TRAVERSE_STMT(NullStmt, {})
1938 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1939 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1940 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1941 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1942 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1943 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1944 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1945 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1946 if (!getDerived().shouldVisitImplicitCode()) {
1947 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
1948 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
1949 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
1950 // Visit everything else only if shouldVisitImplicitCode().
1954 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1955 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1956 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1958 DEF_TRAVERSE_STMT(ReturnStmt, {})
1959 DEF_TRAVERSE_STMT(SwitchStmt, {})
1960 DEF_TRAVERSE_STMT(WhileStmt, {})
1962 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1963 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1964 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1965 if (S->hasExplicitTemplateArgs()) {
1966 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1967 S->getNumTemplateArgs()));
1971 DEF_TRAVERSE_STMT(DeclRefExpr, {
1972 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1973 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1974 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1975 S->getNumTemplateArgs()));
1978 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1979 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1980 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1981 if (S->hasExplicitTemplateArgs()) {
1982 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1983 S->getNumTemplateArgs()));
1987 DEF_TRAVERSE_STMT(MemberExpr, {
1988 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1989 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1990 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1991 S->getNumTemplateArgs()));
1996 {// We don't traverse the cast type, as it's not written in the
2000 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2001 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2004 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2005 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2008 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2009 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2012 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2013 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2016 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2017 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2020 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2021 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2024 template <typename Derived>
2025 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2026 InitListExpr *S, DataRecursionQueue *Queue) {
2028 TRY_TO(WalkUpFromInitListExpr(S));
2029 // All we need are the default actions. FIXME: use a helper function.
2030 for (Stmt *SubStmt : S->children()) {
2031 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2037 // This method is called once for each pair of syntactic and semantic
2038 // InitListExpr, and it traverses the subtrees defined by the two forms. This
2039 // may cause some of the children to be visited twice, if they appear both in
2040 // the syntactic and the semantic form.
2042 // There is no guarantee about which form \p S takes when this method is called.
2043 DEF_TRAVERSE_STMT(InitListExpr, {
2044 TRY_TO(TraverseSynOrSemInitListExpr(
2045 S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2046 TRY_TO(TraverseSynOrSemInitListExpr(
2047 S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2051 // GenericSelectionExpr is a special case because the types and expressions
2052 // are interleaved. We also need to watch out for null types (default
2053 // generic associations).
2054 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2055 TRY_TO(TraverseStmt(S->getControllingExpr()));
2056 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2057 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2058 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2059 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAssocExpr(i));
2064 // PseudoObjectExpr is a special case because of the weirdness with
2065 // syntactic expressions and opaque values.
2066 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2067 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(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_TRAVERSE_OR_ENQUEUE_STMT(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(MSPropertySubscriptExpr, {})
2118 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2119 // The child-iterator will pick up the arg if it's an expression,
2120 // but not if it's a type.
2121 if (S->isTypeOperand())
2122 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2125 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2126 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2127 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2130 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2131 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2134 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2135 { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2137 DEF_TRAVERSE_STMT(VAArgExpr, {
2138 // The child-iterator will pick up the expression argument.
2139 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2142 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2143 // This is called for code like 'return T()' where T is a class type.
2144 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2147 // Walk only the visible parts of lambda expressions.
2148 DEF_TRAVERSE_STMT(LambdaExpr, {
2149 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2150 CEnd = S->explicit_capture_end();
2152 TRY_TO(TraverseLambdaCapture(S, C));
2155 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2156 FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2158 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2159 // Visit the whole type.
2160 TRY_TO(TraverseTypeLoc(TL));
2162 if (S->hasExplicitParameters()) {
2163 // Visit parameters.
2164 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2165 TRY_TO(TraverseDecl(Proto.getParam(I)));
2167 } else if (S->hasExplicitResultType()) {
2168 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2171 auto *T = Proto.getTypePtr();
2172 for (const auto &E : T->exceptions()) {
2173 TRY_TO(TraverseType(E));
2176 if (Expr *NE = T->getNoexceptExpr())
2177 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2180 return TRAVERSE_STMT_BASE(LambdaBody, LambdaExpr, S, Queue);
2183 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2184 // This is called for code like 'T()', where T is a template argument.
2185 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2188 // These expressions all might take explicit template arguments.
2189 // We traverse those if so. FIXME: implement these.
2190 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2191 DEF_TRAVERSE_STMT(CallExpr, {})
2192 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2194 // These exprs (most of them), do not need any action except iterating
2195 // over the children.
2196 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2197 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2198 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2199 DEF_TRAVERSE_STMT(BlockExpr, {
2200 TRY_TO(TraverseDecl(S->getBlockDecl()));
2201 return true; // no child statements to loop through.
2203 DEF_TRAVERSE_STMT(ChooseExpr, {})
2204 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2205 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2207 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2208 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2209 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2210 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2211 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2212 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2213 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2214 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2215 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2216 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2217 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2218 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2219 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2220 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2222 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2223 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2224 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2225 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2226 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2227 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2228 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2229 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2230 DEF_TRAVERSE_STMT(NoInitExpr, {})
2231 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2232 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2233 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2234 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2236 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2237 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2238 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2239 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2240 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2242 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2243 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2244 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2245 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2246 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2247 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2248 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2250 DEF_TRAVERSE_STMT(ParenExpr, {})
2251 DEF_TRAVERSE_STMT(ParenListExpr, {})
2252 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2253 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2254 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2255 DEF_TRAVERSE_STMT(StmtExpr, {})
2256 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2257 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2258 if (S->hasExplicitTemplateArgs()) {
2259 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2260 S->getNumTemplateArgs()));
2264 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2265 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2266 if (S->hasExplicitTemplateArgs()) {
2267 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2268 S->getNumTemplateArgs()));
2272 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2273 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2274 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2275 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2276 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2278 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2279 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2280 DEF_TRAVERSE_STMT(TypoExpr, {})
2281 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2283 // These operators (all of them) do not need any action except
2284 // iterating over the children.
2285 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2286 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2287 DEF_TRAVERSE_STMT(UnaryOperator, {})
2288 DEF_TRAVERSE_STMT(BinaryOperator, {})
2289 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2290 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2291 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2292 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2293 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2294 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2295 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2296 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2297 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2298 DEF_TRAVERSE_STMT(AtomicExpr, {})
2300 // For coroutines expressions, traverse either the operand
2301 // as written or the implied calls, depending on what the
2302 // derived class requests.
2303 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2304 if (!getDerived().shouldVisitImplicitCode()) {
2305 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2309 DEF_TRAVERSE_STMT(CoreturnStmt, {
2310 if (!getDerived().shouldVisitImplicitCode()) {
2311 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2315 DEF_TRAVERSE_STMT(CoawaitExpr, {
2316 if (!getDerived().shouldVisitImplicitCode()) {
2317 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2321 DEF_TRAVERSE_STMT(CoyieldExpr, {
2322 if (!getDerived().shouldVisitImplicitCode()) {
2323 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2328 // These literals (all of them) do not need any action.
2329 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2330 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2331 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2332 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2333 DEF_TRAVERSE_STMT(StringLiteral, {})
2334 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2335 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2336 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2337 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2339 // Traverse OpenCL: AsType, Convert.
2340 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2342 // OpenMP directives.
2343 template <typename Derived>
2344 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2345 OMPExecutableDirective *S) {
2346 for (auto *C : S->clauses()) {
2347 TRY_TO(TraverseOMPClause(C));
2352 template <typename Derived>
2354 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2355 return TraverseOMPExecutableDirective(S);
2358 DEF_TRAVERSE_STMT(OMPParallelDirective,
2359 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2361 DEF_TRAVERSE_STMT(OMPSimdDirective,
2362 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2364 DEF_TRAVERSE_STMT(OMPForDirective,
2365 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2367 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2368 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2370 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2371 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2373 DEF_TRAVERSE_STMT(OMPSectionDirective,
2374 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2376 DEF_TRAVERSE_STMT(OMPSingleDirective,
2377 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2379 DEF_TRAVERSE_STMT(OMPMasterDirective,
2380 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2382 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2383 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2384 TRY_TO(TraverseOMPExecutableDirective(S));
2387 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2388 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2390 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2391 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2393 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2394 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2396 DEF_TRAVERSE_STMT(OMPTaskDirective,
2397 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2399 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2400 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2402 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2403 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2405 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2406 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2408 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2409 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2411 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2412 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2414 DEF_TRAVERSE_STMT(OMPCancelDirective,
2415 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2417 DEF_TRAVERSE_STMT(OMPFlushDirective,
2418 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2420 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2421 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2423 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2424 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2426 DEF_TRAVERSE_STMT(OMPTargetDirective,
2427 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2429 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
2430 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2432 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2433 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2435 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
2436 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2438 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
2439 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2441 DEF_TRAVERSE_STMT(OMPDistributeDirective,
2442 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2445 template <typename Derived>
2446 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2449 switch (C->getClauseKind()) {
2450 #define OPENMP_CLAUSE(Name, Class) \
2452 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2454 #include "clang/Basic/OpenMPKinds.def"
2455 case OMPC_threadprivate:
2462 template <typename Derived>
2463 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2464 TRY_TO(TraverseStmt(C->getCondition()));
2468 template <typename Derived>
2469 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2470 TRY_TO(TraverseStmt(C->getCondition()));
2474 template <typename Derived>
2476 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2477 TRY_TO(TraverseStmt(C->getNumThreads()));
2481 template <typename Derived>
2482 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2483 TRY_TO(TraverseStmt(C->getSafelen()));
2487 template <typename Derived>
2488 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
2489 TRY_TO(TraverseStmt(C->getSimdlen()));
2493 template <typename Derived>
2495 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2496 TRY_TO(TraverseStmt(C->getNumForLoops()));
2500 template <typename Derived>
2501 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2505 template <typename Derived>
2506 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2510 template <typename Derived>
2512 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2513 TRY_TO(TraverseStmt(C->getChunkSize()));
2514 TRY_TO(TraverseStmt(C->getHelperChunkSize()));
2518 template <typename Derived>
2519 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
2520 TRY_TO(TraverseStmt(C->getNumForLoops()));
2524 template <typename Derived>
2525 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2529 template <typename Derived>
2530 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2534 template <typename Derived>
2536 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2540 template <typename Derived>
2541 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2545 template <typename Derived>
2546 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2550 template <typename Derived>
2551 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2555 template <typename Derived>
2556 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2560 template <typename Derived>
2561 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2565 template <typename Derived>
2566 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
2570 template <typename Derived>
2571 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
2575 template <typename Derived>
2576 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
2580 template <typename Derived>
2581 template <typename T>
2582 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2583 for (auto *E : Node->varlists()) {
2584 TRY_TO(TraverseStmt(E));
2589 template <typename Derived>
2590 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2591 TRY_TO(VisitOMPClauseList(C));
2592 for (auto *E : C->private_copies()) {
2593 TRY_TO(TraverseStmt(E));
2598 template <typename Derived>
2599 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2600 OMPFirstprivateClause *C) {
2601 TRY_TO(VisitOMPClauseList(C));
2602 for (auto *E : C->private_copies()) {
2603 TRY_TO(TraverseStmt(E));
2605 for (auto *E : C->inits()) {
2606 TRY_TO(TraverseStmt(E));
2611 template <typename Derived>
2612 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2613 OMPLastprivateClause *C) {
2614 TRY_TO(VisitOMPClauseList(C));
2615 for (auto *E : C->private_copies()) {
2616 TRY_TO(TraverseStmt(E));
2618 for (auto *E : C->source_exprs()) {
2619 TRY_TO(TraverseStmt(E));
2621 for (auto *E : C->destination_exprs()) {
2622 TRY_TO(TraverseStmt(E));
2624 for (auto *E : C->assignment_ops()) {
2625 TRY_TO(TraverseStmt(E));
2630 template <typename Derived>
2631 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2632 TRY_TO(VisitOMPClauseList(C));
2636 template <typename Derived>
2637 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2638 TRY_TO(TraverseStmt(C->getStep()));
2639 TRY_TO(TraverseStmt(C->getCalcStep()));
2640 TRY_TO(VisitOMPClauseList(C));
2641 for (auto *E : C->privates()) {
2642 TRY_TO(TraverseStmt(E));
2644 for (auto *E : C->inits()) {
2645 TRY_TO(TraverseStmt(E));
2647 for (auto *E : C->updates()) {
2648 TRY_TO(TraverseStmt(E));
2650 for (auto *E : C->finals()) {
2651 TRY_TO(TraverseStmt(E));
2656 template <typename Derived>
2657 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2658 TRY_TO(TraverseStmt(C->getAlignment()));
2659 TRY_TO(VisitOMPClauseList(C));
2663 template <typename Derived>
2664 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2665 TRY_TO(VisitOMPClauseList(C));
2666 for (auto *E : C->source_exprs()) {
2667 TRY_TO(TraverseStmt(E));
2669 for (auto *E : C->destination_exprs()) {
2670 TRY_TO(TraverseStmt(E));
2672 for (auto *E : C->assignment_ops()) {
2673 TRY_TO(TraverseStmt(E));
2678 template <typename Derived>
2679 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2680 OMPCopyprivateClause *C) {
2681 TRY_TO(VisitOMPClauseList(C));
2682 for (auto *E : C->source_exprs()) {
2683 TRY_TO(TraverseStmt(E));
2685 for (auto *E : C->destination_exprs()) {
2686 TRY_TO(TraverseStmt(E));
2688 for (auto *E : C->assignment_ops()) {
2689 TRY_TO(TraverseStmt(E));
2694 template <typename Derived>
2696 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2697 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2698 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2699 TRY_TO(VisitOMPClauseList(C));
2700 for (auto *E : C->privates()) {
2701 TRY_TO(TraverseStmt(E));
2703 for (auto *E : C->lhs_exprs()) {
2704 TRY_TO(TraverseStmt(E));
2706 for (auto *E : C->rhs_exprs()) {
2707 TRY_TO(TraverseStmt(E));
2709 for (auto *E : C->reduction_ops()) {
2710 TRY_TO(TraverseStmt(E));
2715 template <typename Derived>
2716 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2717 TRY_TO(VisitOMPClauseList(C));
2721 template <typename Derived>
2722 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
2723 TRY_TO(VisitOMPClauseList(C));
2727 template <typename Derived>
2728 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
2729 TRY_TO(TraverseStmt(C->getDevice()));
2733 template <typename Derived>
2734 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
2735 TRY_TO(VisitOMPClauseList(C));
2739 template <typename Derived>
2740 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
2741 OMPNumTeamsClause *C) {
2742 TRY_TO(TraverseStmt(C->getNumTeams()));
2746 template <typename Derived>
2747 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
2748 OMPThreadLimitClause *C) {
2749 TRY_TO(TraverseStmt(C->getThreadLimit()));
2753 template <typename Derived>
2754 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
2755 OMPPriorityClause *C) {
2756 TRY_TO(TraverseStmt(C->getPriority()));
2760 template <typename Derived>
2761 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
2762 OMPGrainsizeClause *C) {
2763 TRY_TO(TraverseStmt(C->getGrainsize()));
2767 template <typename Derived>
2768 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
2769 OMPNumTasksClause *C) {
2770 TRY_TO(TraverseStmt(C->getNumTasks()));
2774 template <typename Derived>
2775 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
2776 TRY_TO(TraverseStmt(C->getHint()));
2780 // FIXME: look at the following tricky-seeming exprs to see if we
2781 // need to recurse on anything. These are ones that have methods
2782 // returning decls or qualtypes or nestednamespecifier -- though I'm
2783 // not sure if they own them -- or just seemed very complicated, or
2784 // had lots of sub-types to explore.
2786 // VisitOverloadExpr and its children: recurse on template args? etc?
2788 // FIXME: go through all the stmts and exprs again, and see which of them
2789 // create new types, and recurse on the types (TypeLocs?) of those.
2792 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2793 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2794 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2795 // Every class that has getQualifier.
2797 #undef DEF_TRAVERSE_STMT
2798 #undef TRAVERSE_STMT
2799 #undef TRAVERSE_STMT_BASE
2803 } // end namespace clang
2805 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H