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 DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
983 #undef DEF_TRAVERSE_TYPE
985 // ----------------- TypeLoc traversal -----------------
987 // This macro makes available a variable TL, the passed-in TypeLoc.
988 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
989 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
990 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
992 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
993 template <typename Derived> \
994 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
995 if (getDerived().shouldWalkTypesOfTypeLocs()) \
996 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
997 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1002 template <typename Derived>
1004 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1005 // Move this over to the 'main' typeloc tree. Note that this is a
1006 // move -- we pretend that we were really looking at the unqualified
1007 // typeloc all along -- rather than a recursion, so we don't follow
1008 // the normal CRTP plan of going through
1009 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1010 // twice for the same type (once as a QualifiedTypeLoc version of
1011 // the type, once as an UnqualifiedTypeLoc version of the type),
1012 // which in effect means we'd call VisitTypeLoc twice with the
1013 // 'same' type. This solves that problem, at the cost of never
1014 // seeing the qualified version of the type (unless the client
1015 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1016 // perfect solution. A perfect solution probably requires making
1017 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1018 // wrapper around Type* -- rather than being its own class in the
1020 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1023 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1025 // FIXME: ComplexTypeLoc is unfinished
1026 DEF_TRAVERSE_TYPELOC(ComplexType, {
1027 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1030 DEF_TRAVERSE_TYPELOC(PointerType,
1031 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1033 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1034 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1036 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1037 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1039 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1040 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1042 // FIXME: location of base class?
1043 // We traverse this in the type case as well, but how is it not reached through
1044 // the pointee type?
1045 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1046 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1047 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1050 DEF_TRAVERSE_TYPELOC(AdjustedType,
1051 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1053 DEF_TRAVERSE_TYPELOC(DecayedType,
1054 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1056 template <typename Derived>
1057 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1058 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1059 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1063 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1064 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1065 return TraverseArrayTypeLocHelper(TL);
1068 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1069 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1070 return TraverseArrayTypeLocHelper(TL);
1073 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1074 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1075 return TraverseArrayTypeLocHelper(TL);
1078 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1079 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1080 return TraverseArrayTypeLocHelper(TL);
1083 // FIXME: order? why not size expr first?
1084 // FIXME: base VectorTypeLoc is unfinished
1085 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1086 if (TL.getTypePtr()->getSizeExpr())
1087 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1088 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1091 // FIXME: VectorTypeLoc is unfinished
1092 DEF_TRAVERSE_TYPELOC(VectorType, {
1093 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1096 // FIXME: size and attributes
1097 // FIXME: base VectorTypeLoc is unfinished
1098 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1099 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1102 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1103 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1105 // FIXME: location of exception specifications (attributes?)
1106 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1107 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1109 const FunctionProtoType *T = TL.getTypePtr();
1111 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1112 if (TL.getParam(I)) {
1113 TRY_TO(TraverseDecl(TL.getParam(I)));
1114 } else if (I < T->getNumParams()) {
1115 TRY_TO(TraverseType(T->getParamType(I)));
1119 for (const auto &E : T->exceptions()) {
1120 TRY_TO(TraverseType(E));
1123 if (Expr *NE = T->getNoexceptExpr())
1124 TRY_TO(TraverseStmt(NE));
1127 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1128 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1130 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1131 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1133 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1134 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1137 // FIXME: location of underlying expr
1138 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1139 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1142 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1143 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1146 DEF_TRAVERSE_TYPELOC(AutoType, {
1147 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1150 DEF_TRAVERSE_TYPELOC(RecordType, {})
1151 DEF_TRAVERSE_TYPELOC(EnumType, {})
1152 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1153 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {})
1154 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {})
1156 // FIXME: use the loc for the template name?
1157 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1158 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1159 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1160 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1164 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1166 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1168 DEF_TRAVERSE_TYPELOC(AttributedType,
1169 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1171 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1172 if (TL.getQualifierLoc()) {
1173 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1175 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1178 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1179 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1182 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1183 if (TL.getQualifierLoc()) {
1184 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1187 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1188 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1192 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1193 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1195 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1197 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1198 // We have to watch out here because an ObjCInterfaceType's base
1200 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1201 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1202 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1203 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1206 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1207 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1209 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1211 DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1213 #undef DEF_TRAVERSE_TYPELOC
1215 // ----------------- Decl traversal -----------------
1217 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1218 // the children that come from the DeclContext associated with it.
1219 // Therefore each Traverse* only needs to worry about children other
1222 template <typename Derived>
1223 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1227 for (auto *Child : DC->decls()) {
1228 // BlockDecls and CapturedDecls are traversed through BlockExprs and
1229 // CapturedStmts respectively.
1230 if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child))
1231 TRY_TO(TraverseDecl(Child));
1237 // This macro makes available a variable D, the passed-in decl.
1238 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1239 template <typename Derived> \
1240 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \
1241 TRY_TO(WalkUpFrom##DECL(D)); \
1243 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1247 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1249 DEF_TRAVERSE_DECL(BlockDecl, {
1250 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1251 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1252 TRY_TO(TraverseStmt(D->getBody()));
1253 for (const auto &I : D->captures()) {
1254 if (I.hasCopyExpr()) {
1255 TRY_TO(TraverseStmt(I.getCopyExpr()));
1258 // This return statement makes sure the traversal of nodes in
1259 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1260 // is skipped - don't remove it.
1264 DEF_TRAVERSE_DECL(CapturedDecl, {
1265 TRY_TO(TraverseStmt(D->getBody()));
1266 // This return statement makes sure the traversal of nodes in
1267 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1268 // is skipped - don't remove it.
1272 DEF_TRAVERSE_DECL(EmptyDecl, {})
1274 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1275 { TRY_TO(TraverseStmt(D->getAsmString())); })
1277 DEF_TRAVERSE_DECL(ImportDecl, {})
1279 DEF_TRAVERSE_DECL(FriendDecl, {
1280 // Friend is either decl or a type.
1281 if (D->getFriendType())
1282 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1284 TRY_TO(TraverseDecl(D->getFriendDecl()));
1287 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1288 if (D->getFriendType())
1289 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1291 TRY_TO(TraverseDecl(D->getFriendDecl()));
1292 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1293 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1294 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1295 ITPL != ETPL; ++ITPL) {
1296 TRY_TO(TraverseDecl(*ITPL));
1301 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1302 TRY_TO(TraverseDecl(D->getSpecialization()));
1304 if (D->hasExplicitTemplateArgs()) {
1305 const TemplateArgumentListInfo &args = D->templateArgs();
1306 TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1311 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1313 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1316 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1317 TRY_TO(TraverseStmt(D->getAssertExpr()));
1318 TRY_TO(TraverseStmt(D->getMessage()));
1322 TranslationUnitDecl,
1323 {// Code in an unnamed namespace shows up automatically in
1324 // decls_begin()/decls_end(). Thus we don't need to recurse on
1325 // D->getAnonymousNamespace().
1328 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1330 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1331 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1333 // We shouldn't traverse an aliased namespace, since it will be
1334 // defined (and, therefore, traversed) somewhere else.
1336 // This return statement makes sure the traversal of nodes in
1337 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1338 // is skipped - don't remove it.
1342 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1347 {// Code in an unnamed namespace shows up automatically in
1348 // decls_begin()/decls_end(). Thus we don't need to recurse on
1349 // D->getAnonymousNamespace().
1352 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1355 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1356 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1357 for (auto typeParam : *typeParamList) {
1358 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1363 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1366 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1369 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1370 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1371 for (auto typeParam : *typeParamList) {
1372 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1376 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1377 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1381 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1384 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1385 if (D->getReturnTypeSourceInfo()) {
1386 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1388 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end();
1390 TRY_TO(TraverseDecl(*I));
1392 if (D->isThisDeclarationADefinition()) {
1393 TRY_TO(TraverseStmt(D->getBody()));
1398 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1399 if (D->hasExplicitBound()) {
1400 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1401 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1402 // declaring the type alias, not something that was written in the
1407 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1408 if (D->getTypeSourceInfo())
1409 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1411 TRY_TO(TraverseType(D->getType()));
1415 DEF_TRAVERSE_DECL(UsingDecl, {
1416 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1417 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1420 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1421 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1424 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1426 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1427 for (auto *I : D->varlists()) {
1428 TRY_TO(TraverseStmt(I));
1432 // A helper method for TemplateDecl's children.
1433 template <typename Derived>
1434 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1435 TemplateParameterList *TPL) {
1437 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1439 TRY_TO(TraverseDecl(*I));
1445 template <typename Derived>
1446 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1447 ClassTemplateDecl *D) {
1448 for (auto *SD : D->specializations()) {
1449 for (auto *RD : SD->redecls()) {
1450 // We don't want to visit injected-class-names in this traversal.
1451 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1455 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1456 // Visit the implicit instantiations with the requested pattern.
1457 case TSK_Undeclared:
1458 case TSK_ImplicitInstantiation:
1459 TRY_TO(TraverseDecl(RD));
1462 // We don't need to do anything on an explicit instantiation
1463 // or explicit specialization because there will be an explicit
1464 // node for it elsewhere.
1465 case TSK_ExplicitInstantiationDeclaration:
1466 case TSK_ExplicitInstantiationDefinition:
1467 case TSK_ExplicitSpecialization:
1476 template <typename Derived>
1477 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1478 VarTemplateDecl *D) {
1479 for (auto *SD : D->specializations()) {
1480 for (auto *RD : SD->redecls()) {
1482 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1483 case TSK_Undeclared:
1484 case TSK_ImplicitInstantiation:
1485 TRY_TO(TraverseDecl(RD));
1488 case TSK_ExplicitInstantiationDeclaration:
1489 case TSK_ExplicitInstantiationDefinition:
1490 case TSK_ExplicitSpecialization:
1499 // A helper method for traversing the instantiations of a
1500 // function while skipping its specializations.
1501 template <typename Derived>
1502 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1503 FunctionTemplateDecl *D) {
1504 for (auto *FD : D->specializations()) {
1505 for (auto *RD : FD->redecls()) {
1506 switch (RD->getTemplateSpecializationKind()) {
1507 case TSK_Undeclared:
1508 case TSK_ImplicitInstantiation:
1509 // We don't know what kind of FunctionDecl this is.
1510 TRY_TO(TraverseDecl(RD));
1513 // FIXME: For now traverse explicit instantiations here. Change that
1514 // once they are represented as dedicated nodes in the AST.
1515 case TSK_ExplicitInstantiationDeclaration:
1516 case TSK_ExplicitInstantiationDefinition:
1517 TRY_TO(TraverseDecl(RD));
1520 case TSK_ExplicitSpecialization:
1529 // This macro unifies the traversal of class, variable and function
1530 // template declarations.
1531 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1532 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1533 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1534 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1536 /* By default, we do not traverse the instantiations of \
1537 class templates since they do not appear in the user code. The \
1538 following code optionally traverses them. \
1540 We only traverse the class instantiations when we see the canonical \
1541 declaration of the template, to ensure we only visit them once. */ \
1542 if (getDerived().shouldVisitTemplateInstantiations() && \
1543 D == D->getCanonicalDecl()) \
1544 TRY_TO(TraverseTemplateInstantiations(D)); \
1546 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1547 from a template instantiation back to the template from which \
1548 it was instantiated, and thus should not be traversed. */ \
1551 DEF_TRAVERSE_TMPL_DECL(Class)
1552 DEF_TRAVERSE_TMPL_DECL(Var)
1553 DEF_TRAVERSE_TMPL_DECL(Function)
1555 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1556 // D is the "T" in something like
1557 // template <template <typename> class T> class container { };
1558 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1559 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1560 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1562 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1565 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1566 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1569 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1570 // D is the "T" in something like "template<typename T> class vector;"
1571 if (D->getTypeForDecl())
1572 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1573 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1574 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1577 DEF_TRAVERSE_DECL(TypedefDecl, {
1578 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1579 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1580 // declaring the typedef, not something that was written in the
1584 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1585 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1586 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1587 // declaring the type alias, not something that was written in the
1591 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1592 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1593 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1596 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1597 // A dependent using declaration which was marked with 'typename'.
1598 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1599 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1600 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1601 // declaring the type, not something that was written in the
1605 DEF_TRAVERSE_DECL(EnumDecl, {
1606 if (D->getTypeForDecl())
1607 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1609 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1610 // The enumerators are already traversed by
1611 // decls_begin()/decls_end().
1614 // Helper methods for RecordDecl and its children.
1615 template <typename Derived>
1616 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1617 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1618 // declaring the type, not something that was written in the source.
1620 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1624 template <typename Derived>
1625 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1626 if (!TraverseRecordHelper(D))
1628 if (D->isCompleteDefinition()) {
1629 for (const auto &I : D->bases()) {
1630 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc()));
1632 // We don't traverse the friends or the conversions, as they are
1633 // already in decls_begin()/decls_end().
1638 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1640 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1642 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1643 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1644 /* For implicit instantiations ("set<int> x;"), we don't want to \
1645 recurse at all, since the instatiated template isn't written in \
1646 the source code anywhere. (Note the instatiated *type* -- \
1647 set<int> -- is written, and will still get a callback of \
1648 TemplateSpecializationType). For explicit instantiations \
1649 ("template set<int>;"), we do need a callback, since this \
1650 is the only callback that's made for this instantiation. \
1651 We use getTypeAsWritten() to distinguish. */ \
1652 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1653 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1655 if (!getDerived().shouldVisitTemplateInstantiations() && \
1656 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1657 /* Returning from here skips traversing the \
1658 declaration context of the *TemplateSpecializationDecl \
1659 (embedded in the DEF_TRAVERSE_DECL() macro) \
1660 which contains the instantiated members of the template. */ \
1664 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1665 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1667 template <typename Derived>
1668 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1669 const TemplateArgumentLoc *TAL, unsigned Count) {
1670 for (unsigned I = 0; I < Count; ++I) {
1671 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1676 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1677 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1678 /* The partial specialization. */ \
1679 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1680 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1682 TRY_TO(TraverseDecl(*I)); \
1685 /* The args that remains unspecialized. */ \
1686 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1687 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1688 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1690 /* Don't need the *TemplatePartialSpecializationHelper, even \
1691 though that's our parent class -- we already visit all the \
1692 template args here. */ \
1693 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1695 /* Instantiations will have been visited with the primary template. */ \
1698 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1699 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1701 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1703 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1704 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1705 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1706 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1707 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1710 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1712 template <typename Derived>
1713 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1714 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1715 if (D->getTypeSourceInfo())
1716 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1718 TRY_TO(TraverseType(D->getType()));
1722 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1724 DEF_TRAVERSE_DECL(FieldDecl, {
1725 TRY_TO(TraverseDeclaratorHelper(D));
1726 if (D->isBitField())
1727 TRY_TO(TraverseStmt(D->getBitWidth()));
1728 else if (D->hasInClassInitializer())
1729 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1732 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1733 TRY_TO(TraverseDeclaratorHelper(D));
1734 if (D->isBitField())
1735 TRY_TO(TraverseStmt(D->getBitWidth()));
1736 // FIXME: implement the rest.
1739 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1740 TRY_TO(TraverseDeclaratorHelper(D));
1741 if (D->isBitField())
1742 TRY_TO(TraverseStmt(D->getBitWidth()));
1743 // FIXME: implement the rest.
1746 template <typename Derived>
1747 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1748 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1749 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1751 // If we're an explicit template specialization, iterate over the
1752 // template args that were explicitly specified. If we were doing
1753 // this in typing order, we'd do it between the return type and
1754 // the function args, but both are handled by the FunctionTypeLoc
1755 // above, so we have to choose one side. I've decided to do before.
1756 if (const FunctionTemplateSpecializationInfo *FTSI =
1757 D->getTemplateSpecializationInfo()) {
1758 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1759 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1760 // A specialization might not have explicit template arguments if it has
1761 // a templated return type and concrete arguments.
1762 if (const ASTTemplateArgumentListInfo *TALI =
1763 FTSI->TemplateArgumentsAsWritten) {
1764 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1765 TALI->NumTemplateArgs));
1770 // Visit the function type itself, which can be either
1771 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1772 // also covers the return type and the function parameters,
1773 // including exception specifications.
1774 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1775 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1776 } else if (getDerived().shouldVisitImplicitCode()) {
1777 // Visit parameter variable declarations of the implicit function
1778 // if the traverser is visiting implicit code. Parameter variable
1779 // declarations do not have valid TypeSourceInfo, so to visit them
1780 // we need to traverse the declarations explicitly.
1781 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1784 TRY_TO(TraverseDecl(*I));
1787 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1788 // Constructor initializers.
1789 for (auto *I : Ctor->inits()) {
1790 TRY_TO(TraverseConstructorInitializer(I));
1794 if (D->isThisDeclarationADefinition()) {
1795 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1800 DEF_TRAVERSE_DECL(FunctionDecl, {
1801 // We skip decls_begin/decls_end, which are already covered by
1802 // TraverseFunctionHelper().
1803 return TraverseFunctionHelper(D);
1806 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1807 // We skip decls_begin/decls_end, which are already covered by
1808 // TraverseFunctionHelper().
1809 return TraverseFunctionHelper(D);
1812 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1813 // We skip decls_begin/decls_end, which are already covered by
1814 // TraverseFunctionHelper().
1815 return TraverseFunctionHelper(D);
1818 // CXXConversionDecl is the declaration of a type conversion operator.
1819 // It's not a cast expression.
1820 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1821 // We skip decls_begin/decls_end, which are already covered by
1822 // TraverseFunctionHelper().
1823 return TraverseFunctionHelper(D);
1826 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1827 // We skip decls_begin/decls_end, which are already covered by
1828 // TraverseFunctionHelper().
1829 return TraverseFunctionHelper(D);
1832 template <typename Derived>
1833 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1834 TRY_TO(TraverseDeclaratorHelper(D));
1835 // Default params are taken care of when we traverse the ParmVarDecl.
1836 if (!isa<ParmVarDecl>(D) &&
1837 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1838 TRY_TO(TraverseStmt(D->getInit()));
1842 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
1844 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
1846 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1847 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1848 TRY_TO(TraverseDeclaratorHelper(D));
1849 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1850 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1853 DEF_TRAVERSE_DECL(ParmVarDecl, {
1854 TRY_TO(TraverseVarHelper(D));
1856 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
1857 !D->hasUnparsedDefaultArg())
1858 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1860 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
1861 !D->hasUnparsedDefaultArg())
1862 TRY_TO(TraverseStmt(D->getDefaultArg()));
1865 #undef DEF_TRAVERSE_DECL
1867 // ----------------- Stmt traversal -----------------
1869 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1870 // over the children defined in children() (every stmt defines these,
1871 // though sometimes the range is empty). Each individual Traverse*
1872 // method only needs to worry about children other than those. To see
1873 // what children() does for a given class, see, e.g.,
1874 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1876 // This macro makes available a variable S, the passed-in stmt.
1877 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1878 template <typename Derived> \
1879 bool RecursiveASTVisitor<Derived>::Traverse##STMT( \
1880 STMT *S, DataRecursionQueue *Queue) { \
1881 TRY_TO(WalkUpFrom##STMT(S)); \
1883 for (Stmt *SubStmt : S->children()) { \
1884 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \
1889 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1890 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
1891 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1892 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
1894 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1895 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
1897 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1898 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
1900 // children() iterates over inputExpr and outputExpr.
1905 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1906 // added this needs to be implemented.
1909 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1910 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1911 // children() iterates over the handler block.
1914 DEF_TRAVERSE_STMT(DeclStmt, {
1915 for (auto *I : S->decls()) {
1916 TRY_TO(TraverseDecl(I));
1918 // Suppress the default iteration over children() by
1919 // returning. Here's why: A DeclStmt looks like 'type var [=
1920 // initializer]'. The decls above already traverse over the
1921 // initializers, so we don't have to do it again (which
1922 // children() would do).
1926 // These non-expr stmts (most of them), do not need any action except
1927 // iterating over the children.
1928 DEF_TRAVERSE_STMT(BreakStmt, {})
1929 DEF_TRAVERSE_STMT(CXXTryStmt, {})
1930 DEF_TRAVERSE_STMT(CaseStmt, {})
1931 DEF_TRAVERSE_STMT(CompoundStmt, {})
1932 DEF_TRAVERSE_STMT(ContinueStmt, {})
1933 DEF_TRAVERSE_STMT(DefaultStmt, {})
1934 DEF_TRAVERSE_STMT(DoStmt, {})
1935 DEF_TRAVERSE_STMT(ForStmt, {})
1936 DEF_TRAVERSE_STMT(GotoStmt, {})
1937 DEF_TRAVERSE_STMT(IfStmt, {})
1938 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
1939 DEF_TRAVERSE_STMT(LabelStmt, {})
1940 DEF_TRAVERSE_STMT(AttributedStmt, {})
1941 DEF_TRAVERSE_STMT(NullStmt, {})
1942 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
1943 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
1944 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
1945 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
1946 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
1947 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
1948 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
1949 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1950 if (!getDerived().shouldVisitImplicitCode()) {
1951 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
1952 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
1953 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
1954 // Visit everything else only if shouldVisitImplicitCode().
1958 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1959 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1960 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1962 DEF_TRAVERSE_STMT(ReturnStmt, {})
1963 DEF_TRAVERSE_STMT(SwitchStmt, {})
1964 DEF_TRAVERSE_STMT(WhileStmt, {})
1966 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1967 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1968 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1969 if (S->hasExplicitTemplateArgs()) {
1970 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1971 S->getNumTemplateArgs()));
1975 DEF_TRAVERSE_STMT(DeclRefExpr, {
1976 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1977 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1978 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1979 S->getNumTemplateArgs()));
1982 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1983 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1984 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1985 if (S->hasExplicitTemplateArgs()) {
1986 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1987 S->getNumTemplateArgs()));
1991 DEF_TRAVERSE_STMT(MemberExpr, {
1992 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1993 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1994 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1995 S->getNumTemplateArgs()));
2000 {// We don't traverse the cast type, as it's not written in the
2004 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2005 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2008 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2009 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2012 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2013 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2016 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2017 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2020 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2021 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2024 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2025 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2028 template <typename Derived>
2029 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2030 InitListExpr *S, DataRecursionQueue *Queue) {
2032 TRY_TO(WalkUpFromInitListExpr(S));
2033 // All we need are the default actions. FIXME: use a helper function.
2034 for (Stmt *SubStmt : S->children()) {
2035 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2041 // This method is called once for each pair of syntactic and semantic
2042 // InitListExpr, and it traverses the subtrees defined by the two forms. This
2043 // may cause some of the children to be visited twice, if they appear both in
2044 // the syntactic and the semantic form.
2046 // There is no guarantee about which form \p S takes when this method is called.
2047 DEF_TRAVERSE_STMT(InitListExpr, {
2048 TRY_TO(TraverseSynOrSemInitListExpr(
2049 S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2050 TRY_TO(TraverseSynOrSemInitListExpr(
2051 S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2055 // GenericSelectionExpr is a special case because the types and expressions
2056 // are interleaved. We also need to watch out for null types (default
2057 // generic associations).
2058 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2059 TRY_TO(TraverseStmt(S->getControllingExpr()));
2060 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2061 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2062 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2063 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAssocExpr(i));
2068 // PseudoObjectExpr is a special case because of the weirdness with
2069 // syntactic expressions and opaque values.
2070 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2071 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
2072 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2073 e = S->semantics_end();
2076 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2077 sub = OVE->getSourceExpr();
2078 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
2083 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2084 // This is called for code like 'return T()' where T is a built-in
2085 // (i.e. non-class) type.
2086 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2089 DEF_TRAVERSE_STMT(CXXNewExpr, {
2090 // The child-iterator will pick up the other arguments.
2091 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2094 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2095 // The child-iterator will pick up the expression representing
2097 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2098 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2099 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2102 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2103 // The child-iterator will pick up the arg if it's an expression,
2104 // but not if it's a type.
2105 if (S->isArgumentType())
2106 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2109 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2110 // The child-iterator will pick up the arg if it's an expression,
2111 // but not if it's a type.
2112 if (S->isTypeOperand())
2113 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2116 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2117 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2120 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
2122 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2123 // The child-iterator will pick up the arg if it's an expression,
2124 // but not if it's a type.
2125 if (S->isTypeOperand())
2126 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2129 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2130 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2131 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2134 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2135 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2138 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2139 { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2141 DEF_TRAVERSE_STMT(VAArgExpr, {
2142 // The child-iterator will pick up the expression argument.
2143 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2146 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2147 // This is called for code like 'return T()' where T is a class type.
2148 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2151 // Walk only the visible parts of lambda expressions.
2152 DEF_TRAVERSE_STMT(LambdaExpr, {
2153 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2154 CEnd = S->explicit_capture_end();
2156 TRY_TO(TraverseLambdaCapture(S, C));
2159 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2160 FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>();
2162 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2163 // Visit the whole type.
2164 TRY_TO(TraverseTypeLoc(TL));
2166 if (S->hasExplicitParameters()) {
2167 // Visit parameters.
2168 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) {
2169 TRY_TO(TraverseDecl(Proto.getParam(I)));
2171 } else if (S->hasExplicitResultType()) {
2172 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2175 auto *T = Proto.getTypePtr();
2176 for (const auto &E : T->exceptions()) {
2177 TRY_TO(TraverseType(E));
2180 if (Expr *NE = T->getNoexceptExpr())
2181 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2184 return TRAVERSE_STMT_BASE(LambdaBody, LambdaExpr, S, Queue);
2187 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2188 // This is called for code like 'T()', where T is a template argument.
2189 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2192 // These expressions all might take explicit template arguments.
2193 // We traverse those if so. FIXME: implement these.
2194 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2195 DEF_TRAVERSE_STMT(CallExpr, {})
2196 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2198 // These exprs (most of them), do not need any action except iterating
2199 // over the children.
2200 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2201 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2202 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2203 DEF_TRAVERSE_STMT(BlockExpr, {
2204 TRY_TO(TraverseDecl(S->getBlockDecl()));
2205 return true; // no child statements to loop through.
2207 DEF_TRAVERSE_STMT(ChooseExpr, {})
2208 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2209 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2211 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2212 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2213 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {})
2214 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2215 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2216 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2217 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2218 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2219 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2220 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2221 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2222 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2223 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2224 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2226 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2227 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2228 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2229 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2230 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2231 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2232 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2233 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2234 DEF_TRAVERSE_STMT(NoInitExpr, {})
2235 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2236 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2237 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2238 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2240 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2241 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2242 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2243 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2244 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2246 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2247 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2248 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2249 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2250 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2251 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2252 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2254 DEF_TRAVERSE_STMT(ParenExpr, {})
2255 DEF_TRAVERSE_STMT(ParenListExpr, {})
2256 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2257 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2258 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2259 DEF_TRAVERSE_STMT(StmtExpr, {})
2260 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2261 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2262 if (S->hasExplicitTemplateArgs()) {
2263 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2264 S->getNumTemplateArgs()));
2268 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2269 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2270 if (S->hasExplicitTemplateArgs()) {
2271 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2272 S->getNumTemplateArgs()));
2276 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2277 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2278 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2279 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2280 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2282 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2283 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2284 DEF_TRAVERSE_STMT(TypoExpr, {})
2285 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2287 // These operators (all of them) do not need any action except
2288 // iterating over the children.
2289 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2290 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2291 DEF_TRAVERSE_STMT(UnaryOperator, {})
2292 DEF_TRAVERSE_STMT(BinaryOperator, {})
2293 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2294 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2295 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2296 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2297 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2298 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2299 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2300 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2301 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2302 DEF_TRAVERSE_STMT(AtomicExpr, {})
2304 // For coroutines expressions, traverse either the operand
2305 // as written or the implied calls, depending on what the
2306 // derived class requests.
2307 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2308 if (!getDerived().shouldVisitImplicitCode()) {
2309 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2313 DEF_TRAVERSE_STMT(CoreturnStmt, {
2314 if (!getDerived().shouldVisitImplicitCode()) {
2315 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2319 DEF_TRAVERSE_STMT(CoawaitExpr, {
2320 if (!getDerived().shouldVisitImplicitCode()) {
2321 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2325 DEF_TRAVERSE_STMT(CoyieldExpr, {
2326 if (!getDerived().shouldVisitImplicitCode()) {
2327 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2332 // These literals (all of them) do not need any action.
2333 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2334 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2335 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2336 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2337 DEF_TRAVERSE_STMT(StringLiteral, {})
2338 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2339 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2340 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2341 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2343 // Traverse OpenCL: AsType, Convert.
2344 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2346 // OpenMP directives.
2347 template <typename Derived>
2348 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2349 OMPExecutableDirective *S) {
2350 for (auto *C : S->clauses()) {
2351 TRY_TO(TraverseOMPClause(C));
2356 template <typename Derived>
2358 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2359 return TraverseOMPExecutableDirective(S);
2362 DEF_TRAVERSE_STMT(OMPParallelDirective,
2363 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2365 DEF_TRAVERSE_STMT(OMPSimdDirective,
2366 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2368 DEF_TRAVERSE_STMT(OMPForDirective,
2369 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2371 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2372 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2374 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2375 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2377 DEF_TRAVERSE_STMT(OMPSectionDirective,
2378 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2380 DEF_TRAVERSE_STMT(OMPSingleDirective,
2381 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2383 DEF_TRAVERSE_STMT(OMPMasterDirective,
2384 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2386 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2387 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2388 TRY_TO(TraverseOMPExecutableDirective(S));
2391 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2392 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2394 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2395 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2397 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2398 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2400 DEF_TRAVERSE_STMT(OMPTaskDirective,
2401 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2403 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2404 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2406 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2407 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2409 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2410 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2412 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2413 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2415 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2416 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2418 DEF_TRAVERSE_STMT(OMPCancelDirective,
2419 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2421 DEF_TRAVERSE_STMT(OMPFlushDirective,
2422 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2424 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2425 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2427 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2428 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2430 DEF_TRAVERSE_STMT(OMPTargetDirective,
2431 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2433 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
2434 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2436 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2437 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2439 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
2440 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2442 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
2443 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2445 DEF_TRAVERSE_STMT(OMPDistributeDirective,
2446 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2449 template <typename Derived>
2450 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2453 switch (C->getClauseKind()) {
2454 #define OPENMP_CLAUSE(Name, Class) \
2456 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2458 #include "clang/Basic/OpenMPKinds.def"
2459 case OMPC_threadprivate:
2466 template <typename Derived>
2467 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2468 TRY_TO(TraverseStmt(C->getCondition()));
2472 template <typename Derived>
2473 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2474 TRY_TO(TraverseStmt(C->getCondition()));
2478 template <typename Derived>
2480 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2481 TRY_TO(TraverseStmt(C->getNumThreads()));
2485 template <typename Derived>
2486 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2487 TRY_TO(TraverseStmt(C->getSafelen()));
2491 template <typename Derived>
2492 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
2493 TRY_TO(TraverseStmt(C->getSimdlen()));
2497 template <typename Derived>
2499 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2500 TRY_TO(TraverseStmt(C->getNumForLoops()));
2504 template <typename Derived>
2505 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2509 template <typename Derived>
2510 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2514 template <typename Derived>
2516 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2517 TRY_TO(TraverseStmt(C->getChunkSize()));
2518 TRY_TO(TraverseStmt(C->getHelperChunkSize()));
2522 template <typename Derived>
2523 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
2524 TRY_TO(TraverseStmt(C->getNumForLoops()));
2528 template <typename Derived>
2529 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2533 template <typename Derived>
2534 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2538 template <typename Derived>
2540 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2544 template <typename Derived>
2545 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2549 template <typename Derived>
2550 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2554 template <typename Derived>
2555 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2559 template <typename Derived>
2560 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2564 template <typename Derived>
2565 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2569 template <typename Derived>
2570 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
2574 template <typename Derived>
2575 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
2579 template <typename Derived>
2580 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
2584 template <typename Derived>
2585 template <typename T>
2586 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2587 for (auto *E : Node->varlists()) {
2588 TRY_TO(TraverseStmt(E));
2593 template <typename Derived>
2594 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2595 TRY_TO(VisitOMPClauseList(C));
2596 for (auto *E : C->private_copies()) {
2597 TRY_TO(TraverseStmt(E));
2602 template <typename Derived>
2603 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2604 OMPFirstprivateClause *C) {
2605 TRY_TO(VisitOMPClauseList(C));
2606 for (auto *E : C->private_copies()) {
2607 TRY_TO(TraverseStmt(E));
2609 for (auto *E : C->inits()) {
2610 TRY_TO(TraverseStmt(E));
2615 template <typename Derived>
2616 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
2617 OMPLastprivateClause *C) {
2618 TRY_TO(VisitOMPClauseList(C));
2619 for (auto *E : C->private_copies()) {
2620 TRY_TO(TraverseStmt(E));
2622 for (auto *E : C->source_exprs()) {
2623 TRY_TO(TraverseStmt(E));
2625 for (auto *E : C->destination_exprs()) {
2626 TRY_TO(TraverseStmt(E));
2628 for (auto *E : C->assignment_ops()) {
2629 TRY_TO(TraverseStmt(E));
2634 template <typename Derived>
2635 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
2636 TRY_TO(VisitOMPClauseList(C));
2640 template <typename Derived>
2641 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
2642 TRY_TO(TraverseStmt(C->getStep()));
2643 TRY_TO(TraverseStmt(C->getCalcStep()));
2644 TRY_TO(VisitOMPClauseList(C));
2645 for (auto *E : C->privates()) {
2646 TRY_TO(TraverseStmt(E));
2648 for (auto *E : C->inits()) {
2649 TRY_TO(TraverseStmt(E));
2651 for (auto *E : C->updates()) {
2652 TRY_TO(TraverseStmt(E));
2654 for (auto *E : C->finals()) {
2655 TRY_TO(TraverseStmt(E));
2660 template <typename Derived>
2661 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
2662 TRY_TO(TraverseStmt(C->getAlignment()));
2663 TRY_TO(VisitOMPClauseList(C));
2667 template <typename Derived>
2668 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
2669 TRY_TO(VisitOMPClauseList(C));
2670 for (auto *E : C->source_exprs()) {
2671 TRY_TO(TraverseStmt(E));
2673 for (auto *E : C->destination_exprs()) {
2674 TRY_TO(TraverseStmt(E));
2676 for (auto *E : C->assignment_ops()) {
2677 TRY_TO(TraverseStmt(E));
2682 template <typename Derived>
2683 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
2684 OMPCopyprivateClause *C) {
2685 TRY_TO(VisitOMPClauseList(C));
2686 for (auto *E : C->source_exprs()) {
2687 TRY_TO(TraverseStmt(E));
2689 for (auto *E : C->destination_exprs()) {
2690 TRY_TO(TraverseStmt(E));
2692 for (auto *E : C->assignment_ops()) {
2693 TRY_TO(TraverseStmt(E));
2698 template <typename Derived>
2700 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
2701 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
2702 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
2703 TRY_TO(VisitOMPClauseList(C));
2704 for (auto *E : C->privates()) {
2705 TRY_TO(TraverseStmt(E));
2707 for (auto *E : C->lhs_exprs()) {
2708 TRY_TO(TraverseStmt(E));
2710 for (auto *E : C->rhs_exprs()) {
2711 TRY_TO(TraverseStmt(E));
2713 for (auto *E : C->reduction_ops()) {
2714 TRY_TO(TraverseStmt(E));
2719 template <typename Derived>
2720 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
2721 TRY_TO(VisitOMPClauseList(C));
2725 template <typename Derived>
2726 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
2727 TRY_TO(VisitOMPClauseList(C));
2731 template <typename Derived>
2732 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
2733 TRY_TO(TraverseStmt(C->getDevice()));
2737 template <typename Derived>
2738 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
2739 TRY_TO(VisitOMPClauseList(C));
2743 template <typename Derived>
2744 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
2745 OMPNumTeamsClause *C) {
2746 TRY_TO(TraverseStmt(C->getNumTeams()));
2750 template <typename Derived>
2751 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
2752 OMPThreadLimitClause *C) {
2753 TRY_TO(TraverseStmt(C->getThreadLimit()));
2757 template <typename Derived>
2758 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
2759 OMPPriorityClause *C) {
2760 TRY_TO(TraverseStmt(C->getPriority()));
2764 template <typename Derived>
2765 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
2766 OMPGrainsizeClause *C) {
2767 TRY_TO(TraverseStmt(C->getGrainsize()));
2771 template <typename Derived>
2772 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
2773 OMPNumTasksClause *C) {
2774 TRY_TO(TraverseStmt(C->getNumTasks()));
2778 template <typename Derived>
2779 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
2780 TRY_TO(TraverseStmt(C->getHint()));
2784 // FIXME: look at the following tricky-seeming exprs to see if we
2785 // need to recurse on anything. These are ones that have methods
2786 // returning decls or qualtypes or nestednamespecifier -- though I'm
2787 // not sure if they own them -- or just seemed very complicated, or
2788 // had lots of sub-types to explore.
2790 // VisitOverloadExpr and its children: recurse on template args? etc?
2792 // FIXME: go through all the stmts and exprs again, and see which of them
2793 // create new types, and recurse on the types (TypeLocs?) of those.
2796 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2797 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2798 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2799 // Every class that has getQualifier.
2801 #undef DEF_TRAVERSE_STMT
2802 #undef TRAVERSE_STMT
2803 #undef TRAVERSE_STMT_BASE
2807 } // end namespace clang
2809 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H