1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecursiveASTVisitor interface, which recursively
11 // traverses the entire AST.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclarationName.h"
20 #include "clang/AST/DeclBase.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/LambdaCapture.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/OpenMPClause.h"
33 #include "clang/AST/Stmt.h"
34 #include "clang/AST/StmtCXX.h"
35 #include "clang/AST/StmtObjC.h"
36 #include "clang/AST/StmtOpenMP.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/Basic/LLVM.h"
42 #include "clang/Basic/OpenMPKinds.h"
43 #include "clang/Basic/Specifiers.h"
44 #include "llvm/ADT/PointerIntPair.h"
45 #include "llvm/ADT/SmallVector.h"
46 #include "llvm/Support/Casting.h"
49 #include <type_traits>
51 // The following three macros are used for meta programming. The code
52 // using them is responsible for defining macro OPERATOR().
54 // All unary operators.
55 #define UNARYOP_LIST() \
56 OPERATOR(PostInc) OPERATOR(PostDec) OPERATOR(PreInc) OPERATOR(PreDec) \
57 OPERATOR(AddrOf) OPERATOR(Deref) OPERATOR(Plus) OPERATOR(Minus) \
58 OPERATOR(Not) OPERATOR(LNot) OPERATOR(Real) OPERATOR(Imag) \
59 OPERATOR(Extension) OPERATOR(Coawait)
61 // All binary operators (excluding compound assign operators).
62 #define BINOP_LIST() \
63 OPERATOR(PtrMemD) OPERATOR(PtrMemI) OPERATOR(Mul) OPERATOR(Div) \
64 OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) OPERATOR(Shr) \
65 OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) OPERATOR(GE) OPERATOR(EQ) \
66 OPERATOR(NE) OPERATOR(Cmp) OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) \
67 OPERATOR(LAnd) OPERATOR(LOr) OPERATOR(Assign) OPERATOR(Comma)
69 // All compound assign operators.
71 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
72 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
76 // A helper macro to implement short-circuiting when recursing. It
77 // invokes CALL_EXPR, which must be a method call, on the derived
78 // object (s.t. a user of RecursiveASTVisitor can override the method
80 #define TRY_TO(CALL_EXPR) \
82 if (!getDerived().CALL_EXPR) \
86 /// A class that does preorder or postorder
87 /// depth-first traversal on the entire Clang AST and visits each node.
89 /// This class performs three distinct tasks:
90 /// 1. traverse the AST (i.e. go to each node);
91 /// 2. at a given node, walk up the class hierarchy, starting from
92 /// the node's dynamic type, until the top-most class (e.g. Stmt,
93 /// Decl, or Type) is reached.
94 /// 3. given a (node, class) combination, where 'class' is some base
95 /// class of the dynamic type of 'node', call a user-overridable
96 /// function to actually visit the node.
98 /// These tasks are done by three groups of methods, respectively:
99 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
100 /// for traversing an AST rooted at x. This method simply
101 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
102 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
103 /// then recursively visits the child nodes of x.
104 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
106 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
107 /// any child node of x. Instead, it first calls WalkUpFromBar(x)
108 /// where Bar is the direct parent class of Foo (unless Foo has
109 /// no parent), and then calls VisitFoo(x) (see the next list item).
110 /// 3. VisitFoo(Foo *x) does task #3.
112 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
113 /// Visit*). A method (e.g. Traverse*) may call methods from the same
114 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
115 /// It may not call methods from a higher tier.
117 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
118 /// is Foo's super class) before calling VisitFoo(), the result is
119 /// that the Visit*() methods for a given node are called in the
120 /// top-down order (e.g. for a node of type NamespaceDecl, the order will
121 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
123 /// This scheme guarantees that all Visit*() calls for the same AST
124 /// node are grouped together. In other words, Visit*() methods for
125 /// different nodes are never interleaved.
127 /// Clients of this visitor should subclass the visitor (providing
128 /// themselves as the template argument, using the curiously recurring
129 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
130 /// and Visit* methods for declarations, types, statements,
131 /// expressions, or other AST nodes where the visitor should customize
132 /// behavior. Most users only need to override Visit*. Advanced
133 /// users may override Traverse* and WalkUpFrom* to implement custom
134 /// traversal strategies. Returning false from one of these overridden
135 /// functions will abort the entire traversal.
137 /// By default, this visitor tries to visit every part of the explicit
138 /// source code exactly once. The default policy towards templates
139 /// is to descend into the 'pattern' class or function body, not any
140 /// explicit or implicit instantiations. Explicit specializations
141 /// are still visited, and the patterns of partial specializations
142 /// are visited separately. This behavior can be changed by
143 /// overriding shouldVisitTemplateInstantiations() in the derived class
144 /// to return true, in which case all known implicit and explicit
145 /// instantiations will be visited at the same time as the pattern
146 /// from which they were produced.
148 /// By default, this visitor preorder traverses the AST. If postorder traversal
149 /// is needed, the \c shouldTraversePostOrder method needs to be overridden
150 /// to return \c true.
151 template <typename Derived> class RecursiveASTVisitor {
153 /// A queue used for performing data recursion over statements.
154 /// Parameters involving this type are used to implement data
155 /// recursion over Stmts and Exprs within this class, and should
156 /// typically not be explicitly specified by derived classes.
157 /// The bool bit indicates whether the statement has been traversed or not.
158 typedef SmallVectorImpl<llvm::PointerIntPair<Stmt *, 1, bool>>
161 /// Return a reference to the derived class.
162 Derived &getDerived() { return *static_cast<Derived *>(this); }
164 /// Return whether this visitor should recurse into
165 /// template instantiations.
166 bool shouldVisitTemplateInstantiations() const { return false; }
168 /// Return whether this visitor should recurse into the types of
170 bool shouldWalkTypesOfTypeLocs() const { return true; }
172 /// Return whether this visitor should recurse into implicit
173 /// code, e.g., implicit constructors and destructors.
174 bool shouldVisitImplicitCode() const { return false; }
176 /// Return whether this visitor should traverse post-order.
177 bool shouldTraversePostOrder() const { return false; }
179 /// Recursively visits an entire AST, starting from the top-level Decls
180 /// in the AST traversal scope (by default, the TranslationUnitDecl).
181 /// \returns false if visitation was terminated early.
182 bool TraverseAST(ASTContext &AST) {
183 for (Decl *D : AST.getTraversalScope())
184 if (!getDerived().TraverseDecl(D))
189 /// Recursively visit a statement or expression, by
190 /// dispatching to Traverse*() based on the argument's dynamic type.
192 /// \returns false if the visitation was terminated early, true
193 /// otherwise (including when the argument is nullptr).
194 bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr);
196 /// Invoked before visiting a statement or expression via data recursion.
198 /// \returns false to skip visiting the node, true otherwise.
199 bool dataTraverseStmtPre(Stmt *S) { return true; }
201 /// Invoked after visiting a statement or expression via data recursion.
202 /// This is not invoked if the previously invoked \c dataTraverseStmtPre
205 /// \returns false if the visitation was terminated early, true otherwise.
206 bool dataTraverseStmtPost(Stmt *S) { return true; }
208 /// Recursively visit a type, by dispatching to
209 /// Traverse*Type() based on the argument's getTypeClass() property.
211 /// \returns false if the visitation was terminated early, true
212 /// otherwise (including when the argument is a Null type).
213 bool TraverseType(QualType T);
215 /// Recursively visit a type with location, by dispatching to
216 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
218 /// \returns false if the visitation was terminated early, true
219 /// otherwise (including when the argument is a Null type location).
220 bool TraverseTypeLoc(TypeLoc TL);
222 /// Recursively visit an attribute, by dispatching to
223 /// Traverse*Attr() based on the argument's dynamic type.
225 /// \returns false if the visitation was terminated early, true
226 /// otherwise (including when the argument is a Null type location).
227 bool TraverseAttr(Attr *At);
229 /// Recursively visit a declaration, by dispatching to
230 /// Traverse*Decl() based on the argument's dynamic type.
232 /// \returns false if the visitation was terminated early, true
233 /// otherwise (including when the argument is NULL).
234 bool TraverseDecl(Decl *D);
236 /// Recursively visit a C++ nested-name-specifier.
238 /// \returns false if the visitation was terminated early, true otherwise.
239 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
241 /// Recursively visit a C++ nested-name-specifier with location
244 /// \returns false if the visitation was terminated early, true otherwise.
245 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
247 /// Recursively visit a name with its location information.
249 /// \returns false if the visitation was terminated early, true otherwise.
250 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
252 /// Recursively visit a template name and dispatch to the
253 /// appropriate method.
255 /// \returns false if the visitation was terminated early, true otherwise.
256 bool TraverseTemplateName(TemplateName Template);
258 /// Recursively visit a template argument and dispatch to the
259 /// appropriate method for the argument type.
261 /// \returns false if the visitation was terminated early, true otherwise.
262 // FIXME: migrate callers to TemplateArgumentLoc instead.
263 bool TraverseTemplateArgument(const TemplateArgument &Arg);
265 /// Recursively visit a template argument location and dispatch to the
266 /// appropriate method for the argument type.
268 /// \returns false if the visitation was terminated early, true otherwise.
269 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
271 /// Recursively visit a set of template arguments.
272 /// This can be overridden by a subclass, but it's not expected that
273 /// will be needed -- this visitor always dispatches to another.
275 /// \returns false if the visitation was terminated early, true otherwise.
276 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
277 bool TraverseTemplateArguments(const TemplateArgument *Args,
280 /// Recursively visit a base specifier. This can be overridden by a
283 /// \returns false if the visitation was terminated early, true otherwise.
284 bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base);
286 /// Recursively visit a constructor initializer. This
287 /// automatically dispatches to another visitor for the initializer
288 /// expression, but not for the name of the initializer, so may
289 /// be overridden for clients that need access to the name.
291 /// \returns false if the visitation was terminated early, true otherwise.
292 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
294 /// Recursively visit a lambda capture. \c Init is the expression that
295 /// will be used to initialize the capture.
297 /// \returns false if the visitation was terminated early, true otherwise.
298 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
301 /// Recursively visit the syntactic or semantic form of an
302 /// initialization list.
304 /// \returns false if the visitation was terminated early, true otherwise.
305 bool TraverseSynOrSemInitListExpr(InitListExpr *S,
306 DataRecursionQueue *Queue = nullptr);
308 // ---- Methods on Attrs ----
310 // Visit an attribute.
311 bool VisitAttr(Attr *A) { return true; }
313 // Declare Traverse* and empty Visit* for all Attr classes.
314 #define ATTR_VISITOR_DECLS_ONLY
315 #include "clang/AST/AttrVisitor.inc"
316 #undef ATTR_VISITOR_DECLS_ONLY
318 // ---- Methods on Stmts ----
320 Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); }
323 template<typename T, typename U>
324 struct has_same_member_pointer_type : std::false_type {};
325 template<typename T, typename U, typename R, typename... P>
326 struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)>
329 // Traverse the given statement. If the most-derived traverse function takes a
330 // data recursion queue, pass it on; otherwise, discard it. Note that the
331 // first branch of this conditional must compile whether or not the derived
332 // class can take a queue, so if we're taking the second arm, make the first
333 // arm call our function rather than the derived class version.
334 #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE) \
335 (has_same_member_pointer_type<decltype( \
336 &RecursiveASTVisitor::Traverse##NAME), \
337 decltype(&Derived::Traverse##NAME)>::value \
338 ? static_cast<typename std::conditional< \
339 has_same_member_pointer_type< \
340 decltype(&RecursiveASTVisitor::Traverse##NAME), \
341 decltype(&Derived::Traverse##NAME)>::value, \
342 Derived &, RecursiveASTVisitor &>::type>(*this) \
343 .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE) \
344 : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)))
346 // Try to traverse the given statement, or enqueue it if we're performing data
347 // recursion in the middle of traversing another statement. Can only be called
348 // from within a DEF_TRAVERSE_STMT body or similar context.
349 #define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S) \
351 if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue)) \
356 // Declare Traverse*() for all concrete Stmt classes.
357 #define ABSTRACT_STMT(STMT)
358 #define STMT(CLASS, PARENT) \
359 bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
360 #include "clang/AST/StmtNodes.inc"
361 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
363 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
364 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
365 bool VisitStmt(Stmt *S) { return true; }
366 #define STMT(CLASS, PARENT) \
367 bool WalkUpFrom##CLASS(CLASS *S) { \
368 TRY_TO(WalkUpFrom##PARENT(S)); \
369 TRY_TO(Visit##CLASS(S)); \
372 bool Visit##CLASS(CLASS *S) { return true; }
373 #include "clang/AST/StmtNodes.inc"
375 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
376 // operator methods. Unary operators are not classes in themselves
377 // (they're all opcodes in UnaryOperator) but do have visitors.
378 #define OPERATOR(NAME) \
379 bool TraverseUnary##NAME(UnaryOperator *S, \
380 DataRecursionQueue *Queue = nullptr) { \
381 if (!getDerived().shouldTraversePostOrder()) \
382 TRY_TO(WalkUpFromUnary##NAME(S)); \
383 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr()); \
386 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
387 TRY_TO(WalkUpFromUnaryOperator(S)); \
388 TRY_TO(VisitUnary##NAME(S)); \
391 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
396 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
397 // operator methods. Binary operators are not classes in themselves
398 // (they're all opcodes in BinaryOperator) but do have visitors.
399 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
400 bool TraverseBin##NAME(BINOP_TYPE *S, DataRecursionQueue *Queue = nullptr) { \
401 if (!getDerived().shouldTraversePostOrder()) \
402 TRY_TO(WalkUpFromBin##NAME(S)); \
403 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLHS()); \
404 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRHS()); \
407 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
408 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
409 TRY_TO(VisitBin##NAME(S)); \
412 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
414 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
418 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
419 // assignment methods. Compound assignment operators are not
420 // classes in themselves (they're all opcodes in
421 // CompoundAssignOperator) but do have visitors.
422 #define OPERATOR(NAME) \
423 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
427 #undef GENERAL_BINOP_FALLBACK
429 // ---- Methods on Types ----
430 // FIXME: revamp to take TypeLoc's rather than Types.
432 // Declare Traverse*() for all concrete Type classes.
433 #define ABSTRACT_TYPE(CLASS, BASE)
434 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T);
435 #include "clang/AST/TypeNodes.def"
436 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
438 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
439 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
440 bool VisitType(Type *T) { return true; }
441 #define TYPE(CLASS, BASE) \
442 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
443 TRY_TO(WalkUpFrom##BASE(T)); \
444 TRY_TO(Visit##CLASS##Type(T)); \
447 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
448 #include "clang/AST/TypeNodes.def"
450 // ---- Methods on TypeLocs ----
451 // FIXME: this currently just calls the matching Type methods
453 // Declare Traverse*() for all concrete TypeLoc classes.
454 #define ABSTRACT_TYPELOC(CLASS, BASE)
455 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
456 #include "clang/AST/TypeLocNodes.def"
457 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
459 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
460 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
461 bool VisitTypeLoc(TypeLoc TL) { return true; }
463 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
464 // TypeNodes.def and thus need to be handled specially.
465 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
466 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
468 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
469 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
470 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
472 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
474 // Note that BASE includes trailing 'Type' which CLASS doesn't.
475 #define TYPE(CLASS, BASE) \
476 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
477 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
478 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
481 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
482 #include "clang/AST/TypeNodes.def"
484 // ---- Methods on Decls ----
486 // Declare Traverse*() for all concrete Decl classes.
487 #define ABSTRACT_DECL(DECL)
488 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D);
489 #include "clang/AST/DeclNodes.inc"
490 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
492 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
493 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
494 bool VisitDecl(Decl *D) { return true; }
495 #define DECL(CLASS, BASE) \
496 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
497 TRY_TO(WalkUpFrom##BASE(D)); \
498 TRY_TO(Visit##CLASS##Decl(D)); \
501 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
502 #include "clang/AST/DeclNodes.inc"
504 bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
507 // These are helper methods used by more than one Traverse* method.
508 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
510 // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
511 template <typename T>
512 bool TraverseDeclTemplateParameterLists(T *D);
514 #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \
515 bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D);
516 DEF_TRAVERSE_TMPL_INST(Class)
517 DEF_TRAVERSE_TMPL_INST(Var)
518 DEF_TRAVERSE_TMPL_INST(Function)
519 #undef DEF_TRAVERSE_TMPL_INST
520 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
522 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
523 bool TraverseRecordHelper(RecordDecl *D);
524 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
525 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
526 bool TraverseDeclContextHelper(DeclContext *DC);
527 bool TraverseFunctionHelper(FunctionDecl *D);
528 bool TraverseVarHelper(VarDecl *D);
529 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
530 bool TraverseOMPLoopDirective(OMPLoopDirective *S);
531 bool TraverseOMPClause(OMPClause *C);
532 #define OPENMP_CLAUSE(Name, Class) bool Visit##Class(Class *C);
533 #include "clang/Basic/OpenMPKinds.def"
534 /// Process clauses with list of variables.
535 template <typename T> bool VisitOMPClauseList(T *Node);
536 /// Process clauses with pre-initis.
537 bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node);
538 bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node);
540 bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
541 bool PostVisitStmt(Stmt *S);
544 template <typename Derived>
545 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
546 DataRecursionQueue *Queue) {
547 #define DISPATCH_STMT(NAME, CLASS, VAR) \
548 return TRAVERSE_STMT_BASE(NAME, CLASS, VAR, Queue);
550 // If we have a binary expr, dispatch to the subcode of the binop. A smart
551 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
553 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
554 switch (BinOp->getOpcode()) {
555 #define OPERATOR(NAME) \
557 DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
563 #define OPERATOR(NAME) \
564 case BO_##NAME##Assign: \
565 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
571 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
572 switch (UnOp->getOpcode()) {
573 #define OPERATOR(NAME) \
575 DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
583 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
584 switch (S->getStmtClass()) {
585 case Stmt::NoStmtClass:
587 #define ABSTRACT_STMT(STMT)
588 #define STMT(CLASS, PARENT) \
589 case Stmt::CLASS##Class: \
590 DISPATCH_STMT(CLASS, CLASS, S);
591 #include "clang/AST/StmtNodes.inc"
599 template <typename Derived>
600 bool RecursiveASTVisitor<Derived>::PostVisitStmt(Stmt *S) {
601 switch (S->getStmtClass()) {
602 case Stmt::NoStmtClass:
604 #define ABSTRACT_STMT(STMT)
605 #define STMT(CLASS, PARENT) \
606 case Stmt::CLASS##Class: \
607 TRY_TO(WalkUpFrom##CLASS(static_cast<CLASS *>(S))); break;
608 #define INITLISTEXPR(CLASS, PARENT) \
609 case Stmt::CLASS##Class: \
611 auto ILE = static_cast<CLASS *>(S); \
612 if (auto Syn = ILE->isSemanticForm() ? ILE->getSyntacticForm() : ILE) \
613 TRY_TO(WalkUpFrom##CLASS(Syn)); \
614 if (auto Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm()) \
615 TRY_TO(WalkUpFrom##CLASS(Sem)); \
618 #include "clang/AST/StmtNodes.inc"
626 template <typename Derived>
627 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
628 DataRecursionQueue *Queue) {
633 Queue->push_back({S, false});
637 SmallVector<llvm::PointerIntPair<Stmt *, 1, bool>, 8> LocalQueue;
638 LocalQueue.push_back({S, false});
640 while (!LocalQueue.empty()) {
641 auto &CurrSAndVisited = LocalQueue.back();
642 Stmt *CurrS = CurrSAndVisited.getPointer();
643 bool Visited = CurrSAndVisited.getInt();
645 LocalQueue.pop_back();
646 TRY_TO(dataTraverseStmtPost(CurrS));
647 if (getDerived().shouldTraversePostOrder()) {
648 TRY_TO(PostVisitStmt(CurrS));
653 if (getDerived().dataTraverseStmtPre(CurrS)) {
654 CurrSAndVisited.setInt(true);
655 size_t N = LocalQueue.size();
656 TRY_TO(dataTraverseNode(CurrS, &LocalQueue));
657 // Process new children in the order they were added.
658 std::reverse(LocalQueue.begin() + N, LocalQueue.end());
660 LocalQueue.pop_back();
667 #define DISPATCH(NAME, CLASS, VAR) \
668 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))
670 template <typename Derived>
671 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
675 switch (T->getTypeClass()) {
676 #define ABSTRACT_TYPE(CLASS, BASE)
677 #define TYPE(CLASS, BASE) \
679 DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr()));
680 #include "clang/AST/TypeNodes.def"
686 template <typename Derived>
687 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
691 switch (TL.getTypeLocClass()) {
692 #define ABSTRACT_TYPELOC(CLASS, BASE)
693 #define TYPELOC(CLASS, BASE) \
694 case TypeLoc::CLASS: \
695 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
696 #include "clang/AST/TypeLocNodes.def"
702 // Define the Traverse*Attr(Attr* A) methods
703 #define VISITORCLASS RecursiveASTVisitor
704 #include "clang/AST/AttrVisitor.inc"
707 template <typename Derived>
708 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
712 // As a syntax visitor, by default we want to ignore declarations for
713 // implicit declarations (ones not typed explicitly by the user).
714 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
717 switch (D->getKind()) {
718 #define ABSTRACT_DECL(DECL)
719 #define DECL(CLASS, BASE) \
721 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \
724 #include "clang/AST/DeclNodes.inc"
727 // Visit any attributes attached to this declaration.
728 for (auto *I : D->attrs()) {
729 if (!getDerived().TraverseAttr(I))
737 template <typename Derived>
738 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
739 NestedNameSpecifier *NNS) {
743 if (NNS->getPrefix())
744 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
746 switch (NNS->getKind()) {
747 case NestedNameSpecifier::Identifier:
748 case NestedNameSpecifier::Namespace:
749 case NestedNameSpecifier::NamespaceAlias:
750 case NestedNameSpecifier::Global:
751 case NestedNameSpecifier::Super:
754 case NestedNameSpecifier::TypeSpec:
755 case NestedNameSpecifier::TypeSpecWithTemplate:
756 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
762 template <typename Derived>
763 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
764 NestedNameSpecifierLoc NNS) {
768 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
769 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
771 switch (NNS.getNestedNameSpecifier()->getKind()) {
772 case NestedNameSpecifier::Identifier:
773 case NestedNameSpecifier::Namespace:
774 case NestedNameSpecifier::NamespaceAlias:
775 case NestedNameSpecifier::Global:
776 case NestedNameSpecifier::Super:
779 case NestedNameSpecifier::TypeSpec:
780 case NestedNameSpecifier::TypeSpecWithTemplate:
781 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
788 template <typename Derived>
789 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
790 DeclarationNameInfo NameInfo) {
791 switch (NameInfo.getName().getNameKind()) {
792 case DeclarationName::CXXConstructorName:
793 case DeclarationName::CXXDestructorName:
794 case DeclarationName::CXXConversionFunctionName:
795 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
796 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
799 case DeclarationName::CXXDeductionGuideName:
800 TRY_TO(TraverseTemplateName(
801 TemplateName(NameInfo.getName().getCXXDeductionGuideTemplate())));
804 case DeclarationName::Identifier:
805 case DeclarationName::ObjCZeroArgSelector:
806 case DeclarationName::ObjCOneArgSelector:
807 case DeclarationName::ObjCMultiArgSelector:
808 case DeclarationName::CXXOperatorName:
809 case DeclarationName::CXXLiteralOperatorName:
810 case DeclarationName::CXXUsingDirective:
817 template <typename Derived>
818 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
819 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
820 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
821 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
822 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
827 template <typename Derived>
828 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
829 const TemplateArgument &Arg) {
830 switch (Arg.getKind()) {
831 case TemplateArgument::Null:
832 case TemplateArgument::Declaration:
833 case TemplateArgument::Integral:
834 case TemplateArgument::NullPtr:
837 case TemplateArgument::Type:
838 return getDerived().TraverseType(Arg.getAsType());
840 case TemplateArgument::Template:
841 case TemplateArgument::TemplateExpansion:
842 return getDerived().TraverseTemplateName(
843 Arg.getAsTemplateOrTemplatePattern());
845 case TemplateArgument::Expression:
846 return getDerived().TraverseStmt(Arg.getAsExpr());
848 case TemplateArgument::Pack:
849 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
856 // FIXME: no template name location?
857 // FIXME: no source locations for a template argument pack?
858 template <typename Derived>
859 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
860 const TemplateArgumentLoc &ArgLoc) {
861 const TemplateArgument &Arg = ArgLoc.getArgument();
863 switch (Arg.getKind()) {
864 case TemplateArgument::Null:
865 case TemplateArgument::Declaration:
866 case TemplateArgument::Integral:
867 case TemplateArgument::NullPtr:
870 case TemplateArgument::Type: {
871 // FIXME: how can TSI ever be NULL?
872 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
873 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
875 return getDerived().TraverseType(Arg.getAsType());
878 case TemplateArgument::Template:
879 case TemplateArgument::TemplateExpansion:
880 if (ArgLoc.getTemplateQualifierLoc())
881 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
882 ArgLoc.getTemplateQualifierLoc()));
883 return getDerived().TraverseTemplateName(
884 Arg.getAsTemplateOrTemplatePattern());
886 case TemplateArgument::Expression:
887 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
889 case TemplateArgument::Pack:
890 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
897 template <typename Derived>
898 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
899 const TemplateArgument *Args, unsigned NumArgs) {
900 for (unsigned I = 0; I != NumArgs; ++I) {
901 TRY_TO(TraverseTemplateArgument(Args[I]));
907 template <typename Derived>
908 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
909 CXXCtorInitializer *Init) {
910 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
911 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
913 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
914 TRY_TO(TraverseStmt(Init->getInit()));
919 template <typename Derived>
921 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
922 const LambdaCapture *C,
924 if (LE->isInitCapture(C))
925 TRY_TO(TraverseDecl(C->getCapturedVar()));
927 TRY_TO(TraverseStmt(Init));
931 // ----------------- Type traversal -----------------
933 // This macro makes available a variable T, the passed-in type.
934 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
935 template <typename Derived> \
936 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \
937 if (!getDerived().shouldTraversePostOrder()) \
938 TRY_TO(WalkUpFrom##TYPE(T)); \
940 if (getDerived().shouldTraversePostOrder()) \
941 TRY_TO(WalkUpFrom##TYPE(T)); \
945 DEF_TRAVERSE_TYPE(BuiltinType, {})
947 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
949 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
951 DEF_TRAVERSE_TYPE(BlockPointerType,
952 { TRY_TO(TraverseType(T->getPointeeType())); })
954 DEF_TRAVERSE_TYPE(LValueReferenceType,
955 { TRY_TO(TraverseType(T->getPointeeType())); })
957 DEF_TRAVERSE_TYPE(RValueReferenceType,
958 { TRY_TO(TraverseType(T->getPointeeType())); })
960 DEF_TRAVERSE_TYPE(MemberPointerType, {
961 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
962 TRY_TO(TraverseType(T->getPointeeType()));
965 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
967 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
969 DEF_TRAVERSE_TYPE(ConstantArrayType,
970 { TRY_TO(TraverseType(T->getElementType())); })
972 DEF_TRAVERSE_TYPE(IncompleteArrayType,
973 { TRY_TO(TraverseType(T->getElementType())); })
975 DEF_TRAVERSE_TYPE(VariableArrayType, {
976 TRY_TO(TraverseType(T->getElementType()));
977 TRY_TO(TraverseStmt(T->getSizeExpr()));
980 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
981 TRY_TO(TraverseType(T->getElementType()));
982 if (T->getSizeExpr())
983 TRY_TO(TraverseStmt(T->getSizeExpr()));
986 DEF_TRAVERSE_TYPE(DependentAddressSpaceType, {
987 TRY_TO(TraverseStmt(T->getAddrSpaceExpr()));
988 TRY_TO(TraverseType(T->getPointeeType()));
991 DEF_TRAVERSE_TYPE(DependentVectorType, {
992 if (T->getSizeExpr())
993 TRY_TO(TraverseStmt(T->getSizeExpr()));
994 TRY_TO(TraverseType(T->getElementType()));
997 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
998 if (T->getSizeExpr())
999 TRY_TO(TraverseStmt(T->getSizeExpr()));
1000 TRY_TO(TraverseType(T->getElementType()));
1003 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
1005 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
1007 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
1008 { TRY_TO(TraverseType(T->getReturnType())); })
1010 DEF_TRAVERSE_TYPE(FunctionProtoType, {
1011 TRY_TO(TraverseType(T->getReturnType()));
1013 for (const auto &A : T->param_types()) {
1014 TRY_TO(TraverseType(A));
1017 for (const auto &E : T->exceptions()) {
1018 TRY_TO(TraverseType(E));
1021 if (Expr *NE = T->getNoexceptExpr())
1022 TRY_TO(TraverseStmt(NE));
1025 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
1026 DEF_TRAVERSE_TYPE(TypedefType, {})
1028 DEF_TRAVERSE_TYPE(TypeOfExprType,
1029 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1031 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
1033 DEF_TRAVERSE_TYPE(DecltypeType,
1034 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1036 DEF_TRAVERSE_TYPE(UnaryTransformType, {
1037 TRY_TO(TraverseType(T->getBaseType()));
1038 TRY_TO(TraverseType(T->getUnderlyingType()));
1041 DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); })
1042 DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, {
1043 TRY_TO(TraverseTemplateName(T->getTemplateName()));
1044 TRY_TO(TraverseType(T->getDeducedType()));
1047 DEF_TRAVERSE_TYPE(RecordType, {})
1048 DEF_TRAVERSE_TYPE(EnumType, {})
1049 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
1050 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {
1051 TRY_TO(TraverseType(T->getReplacementType()));
1053 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {
1054 TRY_TO(TraverseTemplateArgument(T->getArgumentPack()));
1057 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
1058 TRY_TO(TraverseTemplateName(T->getTemplateName()));
1059 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1062 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
1064 DEF_TRAVERSE_TYPE(AttributedType,
1065 { TRY_TO(TraverseType(T->getModifiedType())); })
1067 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
1069 DEF_TRAVERSE_TYPE(ElaboratedType, {
1070 if (T->getQualifier()) {
1071 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1073 TRY_TO(TraverseType(T->getNamedType()));
1076 DEF_TRAVERSE_TYPE(DependentNameType,
1077 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
1079 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1080 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1081 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1084 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1086 DEF_TRAVERSE_TYPE(ObjCTypeParamType, {})
1088 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1090 DEF_TRAVERSE_TYPE(ObjCObjectType, {
1091 // We have to watch out here because an ObjCInterfaceType's base
1093 if (T->getBaseType().getTypePtr() != T)
1094 TRY_TO(TraverseType(T->getBaseType()));
1095 for (auto typeArg : T->getTypeArgsAsWritten()) {
1096 TRY_TO(TraverseType(typeArg));
1100 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1101 { TRY_TO(TraverseType(T->getPointeeType())); })
1103 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1105 DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
1107 #undef DEF_TRAVERSE_TYPE
1109 // ----------------- TypeLoc traversal -----------------
1111 // This macro makes available a variable TL, the passed-in TypeLoc.
1112 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1113 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1114 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1115 // continue to work.
1116 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
1117 template <typename Derived> \
1118 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
1119 if (getDerived().shouldWalkTypesOfTypeLocs()) \
1120 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
1121 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1126 template <typename Derived>
1128 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1129 // Move this over to the 'main' typeloc tree. Note that this is a
1130 // move -- we pretend that we were really looking at the unqualified
1131 // typeloc all along -- rather than a recursion, so we don't follow
1132 // the normal CRTP plan of going through
1133 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1134 // twice for the same type (once as a QualifiedTypeLoc version of
1135 // the type, once as an UnqualifiedTypeLoc version of the type),
1136 // which in effect means we'd call VisitTypeLoc twice with the
1137 // 'same' type. This solves that problem, at the cost of never
1138 // seeing the qualified version of the type (unless the client
1139 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1140 // perfect solution. A perfect solution probably requires making
1141 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1142 // wrapper around Type* -- rather than being its own class in the
1144 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1147 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1149 // FIXME: ComplexTypeLoc is unfinished
1150 DEF_TRAVERSE_TYPELOC(ComplexType, {
1151 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1154 DEF_TRAVERSE_TYPELOC(PointerType,
1155 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1157 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1158 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1160 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1161 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1163 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1164 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1166 // FIXME: location of base class?
1167 // We traverse this in the type case as well, but how is it not reached through
1168 // the pointee type?
1169 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1170 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1171 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1174 DEF_TRAVERSE_TYPELOC(AdjustedType,
1175 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1177 DEF_TRAVERSE_TYPELOC(DecayedType,
1178 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1180 template <typename Derived>
1181 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1182 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1183 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1187 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1188 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1189 return TraverseArrayTypeLocHelper(TL);
1192 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1193 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1194 return TraverseArrayTypeLocHelper(TL);
1197 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1198 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1199 return TraverseArrayTypeLocHelper(TL);
1202 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1203 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1204 return TraverseArrayTypeLocHelper(TL);
1207 DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
1208 TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr()));
1209 TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType()));
1212 // FIXME: order? why not size expr first?
1213 // FIXME: base VectorTypeLoc is unfinished
1214 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1215 if (TL.getTypePtr()->getSizeExpr())
1216 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1217 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1220 // FIXME: VectorTypeLoc is unfinished
1221 DEF_TRAVERSE_TYPELOC(VectorType, {
1222 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1225 DEF_TRAVERSE_TYPELOC(DependentVectorType, {
1226 if (TL.getTypePtr()->getSizeExpr())
1227 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1228 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1231 // FIXME: size and attributes
1232 // FIXME: base VectorTypeLoc is unfinished
1233 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1234 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1237 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1238 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1240 // FIXME: location of exception specifications (attributes?)
1241 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1242 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1244 const FunctionProtoType *T = TL.getTypePtr();
1246 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1247 if (TL.getParam(I)) {
1248 TRY_TO(TraverseDecl(TL.getParam(I)));
1249 } else if (I < T->getNumParams()) {
1250 TRY_TO(TraverseType(T->getParamType(I)));
1254 for (const auto &E : T->exceptions()) {
1255 TRY_TO(TraverseType(E));
1258 if (Expr *NE = T->getNoexceptExpr())
1259 TRY_TO(TraverseStmt(NE));
1262 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1263 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1265 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1266 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1268 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1269 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1272 // FIXME: location of underlying expr
1273 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1274 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1277 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1278 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1281 DEF_TRAVERSE_TYPELOC(AutoType, {
1282 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1285 DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, {
1286 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1287 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1290 DEF_TRAVERSE_TYPELOC(RecordType, {})
1291 DEF_TRAVERSE_TYPELOC(EnumType, {})
1292 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1293 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {
1294 TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType()));
1296 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {
1297 TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack()));
1300 // FIXME: use the loc for the template name?
1301 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1302 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1303 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1304 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1308 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1310 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1312 DEF_TRAVERSE_TYPELOC(AttributedType,
1313 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1315 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1316 if (TL.getQualifierLoc()) {
1317 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1319 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1322 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1323 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1326 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1327 if (TL.getQualifierLoc()) {
1328 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1331 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1332 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1336 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1337 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1339 DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {})
1341 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1343 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1344 // We have to watch out here because an ObjCInterfaceType's base
1346 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1347 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1348 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1349 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1352 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1353 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1355 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1357 DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1359 #undef DEF_TRAVERSE_TYPELOC
1361 // ----------------- Decl traversal -----------------
1363 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1364 // the children that come from the DeclContext associated with it.
1365 // Therefore each Traverse* only needs to worry about children other
1368 template <typename Derived>
1369 bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext(
1370 const Decl *Child) {
1371 // BlockDecls are traversed through BlockExprs,
1372 // CapturedDecls are traversed through CapturedStmts.
1373 if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child))
1375 // Lambda classes are traversed through LambdaExprs.
1376 if (const CXXRecordDecl* Cls = dyn_cast<CXXRecordDecl>(Child))
1377 return Cls->isLambda();
1381 template <typename Derived>
1382 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1386 for (auto *Child : DC->decls()) {
1387 if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1388 TRY_TO(TraverseDecl(Child));
1394 // This macro makes available a variable D, the passed-in decl.
1395 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1396 template <typename Derived> \
1397 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \
1398 bool ShouldVisitChildren = true; \
1399 bool ReturnValue = true; \
1400 if (!getDerived().shouldTraversePostOrder()) \
1401 TRY_TO(WalkUpFrom##DECL(D)); \
1403 if (ReturnValue && ShouldVisitChildren) \
1404 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1405 if (ReturnValue && getDerived().shouldTraversePostOrder()) \
1406 TRY_TO(WalkUpFrom##DECL(D)); \
1407 return ReturnValue; \
1410 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1412 DEF_TRAVERSE_DECL(BlockDecl, {
1413 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1414 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1415 TRY_TO(TraverseStmt(D->getBody()));
1416 for (const auto &I : D->captures()) {
1417 if (I.hasCopyExpr()) {
1418 TRY_TO(TraverseStmt(I.getCopyExpr()));
1421 ShouldVisitChildren = false;
1424 DEF_TRAVERSE_DECL(CapturedDecl, {
1425 TRY_TO(TraverseStmt(D->getBody()));
1426 ShouldVisitChildren = false;
1429 DEF_TRAVERSE_DECL(EmptyDecl, {})
1431 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1432 { TRY_TO(TraverseStmt(D->getAsmString())); })
1434 DEF_TRAVERSE_DECL(ImportDecl, {})
1436 DEF_TRAVERSE_DECL(FriendDecl, {
1437 // Friend is either decl or a type.
1438 if (D->getFriendType())
1439 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1441 TRY_TO(TraverseDecl(D->getFriendDecl()));
1444 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1445 if (D->getFriendType())
1446 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1448 TRY_TO(TraverseDecl(D->getFriendDecl()));
1449 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1450 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1451 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1452 ITPL != ETPL; ++ITPL) {
1453 TRY_TO(TraverseDecl(*ITPL));
1458 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1459 TRY_TO(TraverseDecl(D->getSpecialization()));
1461 if (D->hasExplicitTemplateArgs()) {
1462 const TemplateArgumentListInfo &args = D->templateArgs();
1463 TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1468 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1470 DEF_TRAVERSE_DECL(ExportDecl, {})
1472 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1475 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1476 TRY_TO(TraverseStmt(D->getAssertExpr()));
1477 TRY_TO(TraverseStmt(D->getMessage()));
1481 TranslationUnitDecl,
1482 {// Code in an unnamed namespace shows up automatically in
1483 // decls_begin()/decls_end(). Thus we don't need to recurse on
1484 // D->getAnonymousNamespace().
1487 DEF_TRAVERSE_DECL(PragmaCommentDecl, {})
1489 DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {})
1491 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1493 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1494 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1496 // We shouldn't traverse an aliased namespace, since it will be
1497 // defined (and, therefore, traversed) somewhere else.
1498 ShouldVisitChildren = false;
1501 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1506 {// Code in an unnamed namespace shows up automatically in
1507 // decls_begin()/decls_end(). Thus we don't need to recurse on
1508 // D->getAnonymousNamespace().
1511 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1514 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1515 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1516 for (auto typeParam : *typeParamList) {
1517 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1522 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1525 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1528 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1529 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1530 for (auto typeParam : *typeParamList) {
1531 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1535 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1536 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1540 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1543 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1544 if (D->getReturnTypeSourceInfo()) {
1545 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1547 for (ParmVarDecl *Parameter : D->parameters()) {
1548 TRY_TO(TraverseDecl(Parameter));
1550 if (D->isThisDeclarationADefinition()) {
1551 TRY_TO(TraverseStmt(D->getBody()));
1553 ShouldVisitChildren = false;
1556 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1557 if (D->hasExplicitBound()) {
1558 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1559 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1560 // declaring the type alias, not something that was written in the
1565 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1566 if (D->getTypeSourceInfo())
1567 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1569 TRY_TO(TraverseType(D->getType()));
1570 ShouldVisitChildren = false;
1573 DEF_TRAVERSE_DECL(UsingDecl, {
1574 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1575 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1578 DEF_TRAVERSE_DECL(UsingPackDecl, {})
1580 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1581 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1584 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1586 DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {})
1588 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1589 for (auto *I : D->varlists()) {
1590 TRY_TO(TraverseStmt(I));
1594 DEF_TRAVERSE_DECL(OMPRequiresDecl, {
1595 for (auto *C : D->clauselists()) {
1596 TRY_TO(TraverseOMPClause(C));
1600 DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, {
1601 TRY_TO(TraverseStmt(D->getCombiner()));
1602 if (auto *Initializer = D->getInitializer())
1603 TRY_TO(TraverseStmt(Initializer));
1604 TRY_TO(TraverseType(D->getType()));
1608 DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
1610 // A helper method for TemplateDecl's children.
1611 template <typename Derived>
1612 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1613 TemplateParameterList *TPL) {
1615 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1617 TRY_TO(TraverseDecl(*I));
1623 template <typename Derived>
1624 template <typename T>
1625 bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) {
1626 for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) {
1627 TemplateParameterList *TPL = D->getTemplateParameterList(i);
1628 TraverseTemplateParameterListHelper(TPL);
1633 template <typename Derived>
1634 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1635 ClassTemplateDecl *D) {
1636 for (auto *SD : D->specializations()) {
1637 for (auto *RD : SD->redecls()) {
1638 // We don't want to visit injected-class-names in this traversal.
1639 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1643 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1644 // Visit the implicit instantiations with the requested pattern.
1645 case TSK_Undeclared:
1646 case TSK_ImplicitInstantiation:
1647 TRY_TO(TraverseDecl(RD));
1650 // We don't need to do anything on an explicit instantiation
1651 // or explicit specialization because there will be an explicit
1652 // node for it elsewhere.
1653 case TSK_ExplicitInstantiationDeclaration:
1654 case TSK_ExplicitInstantiationDefinition:
1655 case TSK_ExplicitSpecialization:
1664 template <typename Derived>
1665 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1666 VarTemplateDecl *D) {
1667 for (auto *SD : D->specializations()) {
1668 for (auto *RD : SD->redecls()) {
1670 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1671 case TSK_Undeclared:
1672 case TSK_ImplicitInstantiation:
1673 TRY_TO(TraverseDecl(RD));
1676 case TSK_ExplicitInstantiationDeclaration:
1677 case TSK_ExplicitInstantiationDefinition:
1678 case TSK_ExplicitSpecialization:
1687 // A helper method for traversing the instantiations of a
1688 // function while skipping its specializations.
1689 template <typename Derived>
1690 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1691 FunctionTemplateDecl *D) {
1692 for (auto *FD : D->specializations()) {
1693 for (auto *RD : FD->redecls()) {
1694 switch (RD->getTemplateSpecializationKind()) {
1695 case TSK_Undeclared:
1696 case TSK_ImplicitInstantiation:
1697 // We don't know what kind of FunctionDecl this is.
1698 TRY_TO(TraverseDecl(RD));
1701 // FIXME: For now traverse explicit instantiations here. Change that
1702 // once they are represented as dedicated nodes in the AST.
1703 case TSK_ExplicitInstantiationDeclaration:
1704 case TSK_ExplicitInstantiationDefinition:
1705 TRY_TO(TraverseDecl(RD));
1708 case TSK_ExplicitSpecialization:
1717 // This macro unifies the traversal of class, variable and function
1718 // template declarations.
1719 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1720 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1721 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1722 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1724 /* By default, we do not traverse the instantiations of \
1725 class templates since they do not appear in the user code. The \
1726 following code optionally traverses them. \
1728 We only traverse the class instantiations when we see the canonical \
1729 declaration of the template, to ensure we only visit them once. */ \
1730 if (getDerived().shouldVisitTemplateInstantiations() && \
1731 D == D->getCanonicalDecl()) \
1732 TRY_TO(TraverseTemplateInstantiations(D)); \
1734 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1735 from a template instantiation back to the template from which \
1736 it was instantiated, and thus should not be traversed. */ \
1739 DEF_TRAVERSE_TMPL_DECL(Class)
1740 DEF_TRAVERSE_TMPL_DECL(Var)
1741 DEF_TRAVERSE_TMPL_DECL(Function)
1743 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1744 // D is the "T" in something like
1745 // template <template <typename> class T> class container { };
1746 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1747 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1748 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1750 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1753 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1754 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1757 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1758 // D is the "T" in something like "template<typename T> class vector;"
1759 if (D->getTypeForDecl())
1760 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1761 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1762 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1765 DEF_TRAVERSE_DECL(TypedefDecl, {
1766 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1767 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1768 // declaring the typedef, not something that was written in the
1772 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1773 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1774 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1775 // declaring the type alias, not something that was written in the
1779 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1780 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1781 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1784 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1785 // A dependent using declaration which was marked with 'typename'.
1786 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1787 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1788 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1789 // declaring the type, not something that was written in the
1793 DEF_TRAVERSE_DECL(EnumDecl, {
1794 TRY_TO(TraverseDeclTemplateParameterLists(D));
1796 if (D->getTypeForDecl())
1797 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1799 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1800 // The enumerators are already traversed by
1801 // decls_begin()/decls_end().
1804 // Helper methods for RecordDecl and its children.
1805 template <typename Derived>
1806 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1807 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1808 // declaring the type, not something that was written in the source.
1810 TRY_TO(TraverseDeclTemplateParameterLists(D));
1811 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1815 template <typename Derived>
1816 bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier(
1817 const CXXBaseSpecifier &Base) {
1818 TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc()));
1822 template <typename Derived>
1823 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1824 if (!TraverseRecordHelper(D))
1826 if (D->isCompleteDefinition()) {
1827 for (const auto &I : D->bases()) {
1828 TRY_TO(TraverseCXXBaseSpecifier(I));
1830 // We don't traverse the friends or the conversions, as they are
1831 // already in decls_begin()/decls_end().
1836 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1838 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1840 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1841 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1842 /* For implicit instantiations ("set<int> x;"), we don't want to \
1843 recurse at all, since the instatiated template isn't written in \
1844 the source code anywhere. (Note the instatiated *type* -- \
1845 set<int> -- is written, and will still get a callback of \
1846 TemplateSpecializationType). For explicit instantiations \
1847 ("template set<int>;"), we do need a callback, since this \
1848 is the only callback that's made for this instantiation. \
1849 We use getTypeAsWritten() to distinguish. */ \
1850 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1851 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1853 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); \
1854 if (!getDerived().shouldVisitTemplateInstantiations() && \
1855 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1856 /* Returning from here skips traversing the \
1857 declaration context of the *TemplateSpecializationDecl \
1858 (embedded in the DEF_TRAVERSE_DECL() macro) \
1859 which contains the instantiated members of the template. */ \
1863 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1864 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1866 template <typename Derived>
1867 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1868 const TemplateArgumentLoc *TAL, unsigned Count) {
1869 for (unsigned I = 0; I < Count; ++I) {
1870 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1875 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1876 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1877 /* The partial specialization. */ \
1878 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1879 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1881 TRY_TO(TraverseDecl(*I)); \
1884 /* The args that remains unspecialized. */ \
1885 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1886 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1887 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1889 /* Don't need the *TemplatePartialSpecializationHelper, even \
1890 though that's our parent class -- we already visit all the \
1891 template args here. */ \
1892 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1894 /* Instantiations will have been visited with the primary template. */ \
1897 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1898 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1900 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1902 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1903 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1904 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1905 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1906 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1909 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1911 template <typename Derived>
1912 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1913 TRY_TO(TraverseDeclTemplateParameterLists(D));
1914 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1915 if (D->getTypeSourceInfo())
1916 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1918 TRY_TO(TraverseType(D->getType()));
1922 DEF_TRAVERSE_DECL(DecompositionDecl, {
1923 TRY_TO(TraverseVarHelper(D));
1924 for (auto *Binding : D->bindings()) {
1925 TRY_TO(TraverseDecl(Binding));
1929 DEF_TRAVERSE_DECL(BindingDecl, {
1930 if (getDerived().shouldVisitImplicitCode())
1931 TRY_TO(TraverseStmt(D->getBinding()));
1934 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1936 DEF_TRAVERSE_DECL(FieldDecl, {
1937 TRY_TO(TraverseDeclaratorHelper(D));
1938 if (D->isBitField())
1939 TRY_TO(TraverseStmt(D->getBitWidth()));
1940 else if (D->hasInClassInitializer())
1941 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1944 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1945 TRY_TO(TraverseDeclaratorHelper(D));
1946 if (D->isBitField())
1947 TRY_TO(TraverseStmt(D->getBitWidth()));
1948 // FIXME: implement the rest.
1951 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1952 TRY_TO(TraverseDeclaratorHelper(D));
1953 if (D->isBitField())
1954 TRY_TO(TraverseStmt(D->getBitWidth()));
1955 // FIXME: implement the rest.
1958 template <typename Derived>
1959 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1960 TRY_TO(TraverseDeclTemplateParameterLists(D));
1961 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1962 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1964 // If we're an explicit template specialization, iterate over the
1965 // template args that were explicitly specified. If we were doing
1966 // this in typing order, we'd do it between the return type and
1967 // the function args, but both are handled by the FunctionTypeLoc
1968 // above, so we have to choose one side. I've decided to do before.
1969 if (const FunctionTemplateSpecializationInfo *FTSI =
1970 D->getTemplateSpecializationInfo()) {
1971 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1972 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1973 // A specialization might not have explicit template arguments if it has
1974 // a templated return type and concrete arguments.
1975 if (const ASTTemplateArgumentListInfo *TALI =
1976 FTSI->TemplateArgumentsAsWritten) {
1977 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1978 TALI->NumTemplateArgs));
1983 // Visit the function type itself, which can be either
1984 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1985 // also covers the return type and the function parameters,
1986 // including exception specifications.
1987 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1988 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1989 } else if (getDerived().shouldVisitImplicitCode()) {
1990 // Visit parameter variable declarations of the implicit function
1991 // if the traverser is visiting implicit code. Parameter variable
1992 // declarations do not have valid TypeSourceInfo, so to visit them
1993 // we need to traverse the declarations explicitly.
1994 for (ParmVarDecl *Parameter : D->parameters()) {
1995 TRY_TO(TraverseDecl(Parameter));
1999 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2000 // Constructor initializers.
2001 for (auto *I : Ctor->inits()) {
2002 TRY_TO(TraverseConstructorInitializer(I));
2006 if (D->isThisDeclarationADefinition()) {
2007 TRY_TO(TraverseStmt(D->getBody())); // Function body.
2012 DEF_TRAVERSE_DECL(FunctionDecl, {
2013 // We skip decls_begin/decls_end, which are already covered by
2014 // TraverseFunctionHelper().
2015 ShouldVisitChildren = false;
2016 ReturnValue = TraverseFunctionHelper(D);
2019 DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
2020 // We skip decls_begin/decls_end, which are already covered by
2021 // TraverseFunctionHelper().
2022 ShouldVisitChildren = false;
2023 ReturnValue = TraverseFunctionHelper(D);
2026 DEF_TRAVERSE_DECL(CXXMethodDecl, {
2027 // We skip decls_begin/decls_end, which are already covered by
2028 // TraverseFunctionHelper().
2029 ShouldVisitChildren = false;
2030 ReturnValue = TraverseFunctionHelper(D);
2033 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
2034 // We skip decls_begin/decls_end, which are already covered by
2035 // TraverseFunctionHelper().
2036 ShouldVisitChildren = false;
2037 ReturnValue = TraverseFunctionHelper(D);
2040 // CXXConversionDecl is the declaration of a type conversion operator.
2041 // It's not a cast expression.
2042 DEF_TRAVERSE_DECL(CXXConversionDecl, {
2043 // We skip decls_begin/decls_end, which are already covered by
2044 // TraverseFunctionHelper().
2045 ShouldVisitChildren = false;
2046 ReturnValue = TraverseFunctionHelper(D);
2049 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
2050 // We skip decls_begin/decls_end, which are already covered by
2051 // TraverseFunctionHelper().
2052 ShouldVisitChildren = false;
2053 ReturnValue = TraverseFunctionHelper(D);
2056 template <typename Derived>
2057 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
2058 TRY_TO(TraverseDeclaratorHelper(D));
2059 // Default params are taken care of when we traverse the ParmVarDecl.
2060 if (!isa<ParmVarDecl>(D) &&
2061 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
2062 TRY_TO(TraverseStmt(D->getInit()));
2066 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
2068 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
2070 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
2071 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
2072 TRY_TO(TraverseDeclaratorHelper(D));
2073 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
2074 TRY_TO(TraverseStmt(D->getDefaultArgument()));
2077 DEF_TRAVERSE_DECL(ParmVarDecl, {
2078 TRY_TO(TraverseVarHelper(D));
2080 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
2081 !D->hasUnparsedDefaultArg())
2082 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
2084 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
2085 !D->hasUnparsedDefaultArg())
2086 TRY_TO(TraverseStmt(D->getDefaultArg()));
2089 #undef DEF_TRAVERSE_DECL
2091 // ----------------- Stmt traversal -----------------
2093 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
2094 // over the children defined in children() (every stmt defines these,
2095 // though sometimes the range is empty). Each individual Traverse*
2096 // method only needs to worry about children other than those. To see
2097 // what children() does for a given class, see, e.g.,
2098 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
2100 // This macro makes available a variable S, the passed-in stmt.
2101 #define DEF_TRAVERSE_STMT(STMT, CODE) \
2102 template <typename Derived> \
2103 bool RecursiveASTVisitor<Derived>::Traverse##STMT( \
2104 STMT *S, DataRecursionQueue *Queue) { \
2105 bool ShouldVisitChildren = true; \
2106 bool ReturnValue = true; \
2107 if (!getDerived().shouldTraversePostOrder()) \
2108 TRY_TO(WalkUpFrom##STMT(S)); \
2110 if (ShouldVisitChildren) { \
2111 for (Stmt * SubStmt : getDerived().getStmtChildren(S)) { \
2112 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \
2115 if (!Queue && ReturnValue && getDerived().shouldTraversePostOrder()) \
2116 TRY_TO(WalkUpFrom##STMT(S)); \
2117 return ReturnValue; \
2120 DEF_TRAVERSE_STMT(GCCAsmStmt, {
2121 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
2122 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
2123 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
2125 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
2126 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
2128 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
2129 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
2131 // children() iterates over inputExpr and outputExpr.
2136 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
2137 // added this needs to be implemented.
2140 DEF_TRAVERSE_STMT(CXXCatchStmt, {
2141 TRY_TO(TraverseDecl(S->getExceptionDecl()));
2142 // children() iterates over the handler block.
2145 DEF_TRAVERSE_STMT(DeclStmt, {
2146 for (auto *I : S->decls()) {
2147 TRY_TO(TraverseDecl(I));
2149 // Suppress the default iteration over children() by
2150 // returning. Here's why: A DeclStmt looks like 'type var [=
2151 // initializer]'. The decls above already traverse over the
2152 // initializers, so we don't have to do it again (which
2153 // children() would do).
2154 ShouldVisitChildren = false;
2157 // These non-expr stmts (most of them), do not need any action except
2158 // iterating over the children.
2159 DEF_TRAVERSE_STMT(BreakStmt, {})
2160 DEF_TRAVERSE_STMT(CXXTryStmt, {})
2161 DEF_TRAVERSE_STMT(CaseStmt, {})
2162 DEF_TRAVERSE_STMT(CompoundStmt, {})
2163 DEF_TRAVERSE_STMT(ContinueStmt, {})
2164 DEF_TRAVERSE_STMT(DefaultStmt, {})
2165 DEF_TRAVERSE_STMT(DoStmt, {})
2166 DEF_TRAVERSE_STMT(ForStmt, {})
2167 DEF_TRAVERSE_STMT(GotoStmt, {})
2168 DEF_TRAVERSE_STMT(IfStmt, {})
2169 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
2170 DEF_TRAVERSE_STMT(LabelStmt, {})
2171 DEF_TRAVERSE_STMT(AttributedStmt, {})
2172 DEF_TRAVERSE_STMT(NullStmt, {})
2173 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
2174 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
2175 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
2176 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
2177 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
2178 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
2179 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
2181 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
2182 if (!getDerived().shouldVisitImplicitCode()) {
2184 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInit());
2185 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
2186 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
2187 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2188 // Visit everything else only if shouldVisitImplicitCode().
2189 ShouldVisitChildren = false;
2193 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
2194 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2195 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2198 DEF_TRAVERSE_STMT(ReturnStmt, {})
2199 DEF_TRAVERSE_STMT(SwitchStmt, {})
2200 DEF_TRAVERSE_STMT(WhileStmt, {})
2202 DEF_TRAVERSE_STMT(ConstantExpr, {})
2204 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
2205 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2206 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2207 if (S->hasExplicitTemplateArgs()) {
2208 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2209 S->getNumTemplateArgs()));
2213 DEF_TRAVERSE_STMT(DeclRefExpr, {
2214 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2215 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2216 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2217 S->getNumTemplateArgs()));
2220 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
2221 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2222 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2223 if (S->hasExplicitTemplateArgs()) {
2224 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2225 S->getNumTemplateArgs()));
2229 DEF_TRAVERSE_STMT(MemberExpr, {
2230 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2231 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2232 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2233 S->getNumTemplateArgs()));
2238 {// We don't traverse the cast type, as it's not written in the
2242 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2243 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2246 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2247 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2250 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2251 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2254 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2255 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2258 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2259 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2262 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2263 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2266 template <typename Derived>
2267 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2268 InitListExpr *S, DataRecursionQueue *Queue) {
2270 // Skip this if we traverse postorder. We will visit it later
2271 // in PostVisitStmt.
2272 if (!getDerived().shouldTraversePostOrder())
2273 TRY_TO(WalkUpFromInitListExpr(S));
2275 // All we need are the default actions. FIXME: use a helper function.
2276 for (Stmt *SubStmt : S->children()) {
2277 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2283 // This method is called once for each pair of syntactic and semantic
2284 // InitListExpr, and it traverses the subtrees defined by the two forms. This
2285 // may cause some of the children to be visited twice, if they appear both in
2286 // the syntactic and the semantic form.
2288 // There is no guarantee about which form \p S takes when this method is called.
2289 template <typename Derived>
2290 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(
2291 InitListExpr *S, DataRecursionQueue *Queue) {
2292 TRY_TO(TraverseSynOrSemInitListExpr(
2293 S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2294 TRY_TO(TraverseSynOrSemInitListExpr(
2295 S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2299 // GenericSelectionExpr is a special case because the types and expressions
2300 // are interleaved. We also need to watch out for null types (default
2301 // generic associations).
2302 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2303 TRY_TO(TraverseStmt(S->getControllingExpr()));
2304 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2305 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2306 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2307 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAssocExpr(i));
2309 ShouldVisitChildren = false;
2312 // PseudoObjectExpr is a special case because of the weirdness with
2313 // syntactic expressions and opaque values.
2314 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2315 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
2316 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2317 e = S->semantics_end();
2320 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2321 sub = OVE->getSourceExpr();
2322 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
2324 ShouldVisitChildren = false;
2327 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2328 // This is called for code like 'return T()' where T is a built-in
2329 // (i.e. non-class) type.
2330 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2333 DEF_TRAVERSE_STMT(CXXNewExpr, {
2334 // The child-iterator will pick up the other arguments.
2335 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2338 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2339 // The child-iterator will pick up the expression representing
2341 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2342 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2343 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2346 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2347 // The child-iterator will pick up the arg if it's an expression,
2348 // but not if it's a type.
2349 if (S->isArgumentType())
2350 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2353 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2354 // The child-iterator will pick up the arg if it's an expression,
2355 // but not if it's a type.
2356 if (S->isTypeOperand())
2357 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2360 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2361 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2364 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
2366 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2367 // The child-iterator will pick up the arg if it's an expression,
2368 // but not if it's a type.
2369 if (S->isTypeOperand())
2370 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2373 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2374 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2375 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2378 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2379 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2382 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2383 { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2385 DEF_TRAVERSE_STMT(VAArgExpr, {
2386 // The child-iterator will pick up the expression argument.
2387 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2390 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2391 // This is called for code like 'return T()' where T is a class type.
2392 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2395 // Walk only the visible parts of lambda expressions.
2396 DEF_TRAVERSE_STMT(LambdaExpr, {
2397 // Visit the capture list.
2398 for (unsigned I = 0, N = S->capture_size(); I != N; ++I) {
2399 const LambdaCapture *C = S->capture_begin() + I;
2400 if (C->isExplicit() || getDerived().shouldVisitImplicitCode()) {
2401 TRY_TO(TraverseLambdaCapture(S, C, S->capture_init_begin()[I]));
2405 if (getDerived().shouldVisitImplicitCode()) {
2406 // The implicit model is simple: everything else is in the lambda class.
2407 TRY_TO(TraverseDecl(S->getLambdaClass()));
2409 // We need to poke around to find the bits that might be explicitly written.
2410 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2411 FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>();
2413 if (S->hasExplicitParameters()) {
2414 // Visit parameters.
2415 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
2416 TRY_TO(TraverseDecl(Proto.getParam(I)));
2418 if (S->hasExplicitResultType())
2419 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2421 auto *T = Proto.getTypePtr();
2422 for (const auto &E : T->exceptions())
2423 TRY_TO(TraverseType(E));
2425 if (Expr *NE = T->getNoexceptExpr())
2426 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2428 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2430 ShouldVisitChildren = false;
2433 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2434 // This is called for code like 'T()', where T is a template argument.
2435 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2438 // These expressions all might take explicit template arguments.
2439 // We traverse those if so. FIXME: implement these.
2440 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2441 DEF_TRAVERSE_STMT(CallExpr, {})
2442 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2444 // These exprs (most of them), do not need any action except iterating
2445 // over the children.
2446 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2447 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2448 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2450 DEF_TRAVERSE_STMT(BlockExpr, {
2451 TRY_TO(TraverseDecl(S->getBlockDecl()));
2452 return true; // no child statements to loop through.
2455 DEF_TRAVERSE_STMT(ChooseExpr, {})
2456 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2457 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2459 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2460 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2462 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {
2463 if (getDerived().shouldVisitImplicitCode())
2464 TRY_TO(TraverseStmt(S->getExpr()));
2467 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2468 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2469 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2470 DEF_TRAVERSE_STMT(CXXInheritedCtorInitExpr, {})
2471 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2472 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2474 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2475 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2476 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2477 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2478 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2479 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2482 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2483 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2484 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2485 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2486 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2487 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2488 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2489 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2490 DEF_TRAVERSE_STMT(NoInitExpr, {})
2491 DEF_TRAVERSE_STMT(ArrayInitLoopExpr, {
2492 // FIXME: The source expression of the OVE should be listed as
2493 // a child of the ArrayInitLoopExpr.
2494 if (OpaqueValueExpr *OVE = S->getCommonExpr())
2495 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr());
2497 DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {})
2498 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2500 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2501 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2502 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2505 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2506 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2508 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2509 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2510 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2513 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2514 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2515 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2516 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2517 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2519 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2520 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2523 DEF_TRAVERSE_STMT(ObjCAvailabilityCheckExpr, {})
2524 DEF_TRAVERSE_STMT(ParenExpr, {})
2525 DEF_TRAVERSE_STMT(ParenListExpr, {})
2526 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2527 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2528 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2529 DEF_TRAVERSE_STMT(StmtExpr, {})
2530 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2531 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2532 if (S->hasExplicitTemplateArgs()) {
2533 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2534 S->getNumTemplateArgs()));
2538 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2539 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2540 if (S->hasExplicitTemplateArgs()) {
2541 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2542 S->getNumTemplateArgs()));
2546 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2547 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2548 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2549 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2550 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2552 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2553 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2554 DEF_TRAVERSE_STMT(TypoExpr, {})
2555 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2557 // These operators (all of them) do not need any action except
2558 // iterating over the children.
2559 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2560 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2561 DEF_TRAVERSE_STMT(UnaryOperator, {})
2562 DEF_TRAVERSE_STMT(BinaryOperator, {})
2563 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2564 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2565 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2566 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2567 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2568 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2569 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2570 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2571 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2572 DEF_TRAVERSE_STMT(AtomicExpr, {})
2574 // For coroutines expressions, traverse either the operand
2575 // as written or the implied calls, depending on what the
2576 // derived class requests.
2577 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2578 if (!getDerived().shouldVisitImplicitCode()) {
2579 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2580 ShouldVisitChildren = false;
2583 DEF_TRAVERSE_STMT(CoreturnStmt, {
2584 if (!getDerived().shouldVisitImplicitCode()) {
2585 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2586 ShouldVisitChildren = false;
2589 DEF_TRAVERSE_STMT(CoawaitExpr, {
2590 if (!getDerived().shouldVisitImplicitCode()) {
2591 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2592 ShouldVisitChildren = false;
2595 DEF_TRAVERSE_STMT(DependentCoawaitExpr, {
2596 if (!getDerived().shouldVisitImplicitCode()) {
2597 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2598 ShouldVisitChildren = false;
2601 DEF_TRAVERSE_STMT(CoyieldExpr, {
2602 if (!getDerived().shouldVisitImplicitCode()) {
2603 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2604 ShouldVisitChildren = false;
2608 // These literals (all of them) do not need any action.
2609 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2610 DEF_TRAVERSE_STMT(FixedPointLiteral, {})
2611 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2612 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2613 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2614 DEF_TRAVERSE_STMT(StringLiteral, {})
2615 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2616 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2617 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2618 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2620 // Traverse OpenCL: AsType, Convert.
2621 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2623 // OpenMP directives.
2624 template <typename Derived>
2625 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2626 OMPExecutableDirective *S) {
2627 for (auto *C : S->clauses()) {
2628 TRY_TO(TraverseOMPClause(C));
2633 template <typename Derived>
2635 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2636 return TraverseOMPExecutableDirective(S);
2639 DEF_TRAVERSE_STMT(OMPParallelDirective,
2640 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2642 DEF_TRAVERSE_STMT(OMPSimdDirective,
2643 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2645 DEF_TRAVERSE_STMT(OMPForDirective,
2646 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2648 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2649 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2651 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2652 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2654 DEF_TRAVERSE_STMT(OMPSectionDirective,
2655 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2657 DEF_TRAVERSE_STMT(OMPSingleDirective,
2658 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2660 DEF_TRAVERSE_STMT(OMPMasterDirective,
2661 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2663 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2664 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2665 TRY_TO(TraverseOMPExecutableDirective(S));
2668 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2669 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2671 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2672 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2674 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2675 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2677 DEF_TRAVERSE_STMT(OMPTaskDirective,
2678 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2680 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2681 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2683 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2684 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2686 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2687 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2689 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2690 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2692 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2693 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2695 DEF_TRAVERSE_STMT(OMPCancelDirective,
2696 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2698 DEF_TRAVERSE_STMT(OMPFlushDirective,
2699 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2701 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2702 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2704 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2705 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2707 DEF_TRAVERSE_STMT(OMPTargetDirective,
2708 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2710 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
2711 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2713 DEF_TRAVERSE_STMT(OMPTargetEnterDataDirective,
2714 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2716 DEF_TRAVERSE_STMT(OMPTargetExitDataDirective,
2717 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2719 DEF_TRAVERSE_STMT(OMPTargetParallelDirective,
2720 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2722 DEF_TRAVERSE_STMT(OMPTargetParallelForDirective,
2723 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2725 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2726 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2728 DEF_TRAVERSE_STMT(OMPTargetUpdateDirective,
2729 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2731 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
2732 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2734 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
2735 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2737 DEF_TRAVERSE_STMT(OMPDistributeDirective,
2738 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2740 DEF_TRAVERSE_STMT(OMPDistributeParallelForDirective,
2741 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2743 DEF_TRAVERSE_STMT(OMPDistributeParallelForSimdDirective,
2744 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2746 DEF_TRAVERSE_STMT(OMPDistributeSimdDirective,
2747 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2749 DEF_TRAVERSE_STMT(OMPTargetParallelForSimdDirective,
2750 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2752 DEF_TRAVERSE_STMT(OMPTargetSimdDirective,
2753 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2755 DEF_TRAVERSE_STMT(OMPTeamsDistributeDirective,
2756 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2758 DEF_TRAVERSE_STMT(OMPTeamsDistributeSimdDirective,
2759 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2761 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForSimdDirective,
2762 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2764 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForDirective,
2765 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2767 DEF_TRAVERSE_STMT(OMPTargetTeamsDirective,
2768 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2770 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeDirective,
2771 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2773 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForDirective,
2774 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2776 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForSimdDirective,
2777 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2779 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeSimdDirective,
2780 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2783 template <typename Derived>
2784 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2787 switch (C->getClauseKind()) {
2788 #define OPENMP_CLAUSE(Name, Class) \
2790 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2792 #include "clang/Basic/OpenMPKinds.def"
2793 case OMPC_threadprivate:
2801 template <typename Derived>
2802 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPreInit(
2803 OMPClauseWithPreInit *Node) {
2804 TRY_TO(TraverseStmt(Node->getPreInitStmt()));
2808 template <typename Derived>
2809 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPostUpdate(
2810 OMPClauseWithPostUpdate *Node) {
2811 TRY_TO(VisitOMPClauseWithPreInit(Node));
2812 TRY_TO(TraverseStmt(Node->getPostUpdateExpr()));
2816 template <typename Derived>
2817 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2818 TRY_TO(VisitOMPClauseWithPreInit(C));
2819 TRY_TO(TraverseStmt(C->getCondition()));
2823 template <typename Derived>
2824 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2825 TRY_TO(TraverseStmt(C->getCondition()));
2829 template <typename Derived>
2831 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2832 TRY_TO(VisitOMPClauseWithPreInit(C));
2833 TRY_TO(TraverseStmt(C->getNumThreads()));
2837 template <typename Derived>
2838 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2839 TRY_TO(TraverseStmt(C->getSafelen()));
2843 template <typename Derived>
2844 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
2845 TRY_TO(TraverseStmt(C->getSimdlen()));
2849 template <typename Derived>
2851 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2852 TRY_TO(TraverseStmt(C->getNumForLoops()));
2856 template <typename Derived>
2857 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2861 template <typename Derived>
2862 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2866 template <typename Derived>
2867 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedAddressClause(
2868 OMPUnifiedAddressClause *) {
2872 template <typename Derived>
2873 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedSharedMemoryClause(
2874 OMPUnifiedSharedMemoryClause *) {
2878 template <typename Derived>
2879 bool RecursiveASTVisitor<Derived>::VisitOMPReverseOffloadClause(
2880 OMPReverseOffloadClause *) {
2884 template <typename Derived>
2885 bool RecursiveASTVisitor<Derived>::VisitOMPDynamicAllocatorsClause(
2886 OMPDynamicAllocatorsClause *) {
2890 template <typename Derived>
2891 bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause(
2892 OMPAtomicDefaultMemOrderClause *) {
2896 template <typename Derived>
2898 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2899 TRY_TO(VisitOMPClauseWithPreInit(C));
2900 TRY_TO(TraverseStmt(C->getChunkSize()));
2904 template <typename Derived>
2905 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
2906 TRY_TO(TraverseStmt(C->getNumForLoops()));
2910 template <typename Derived>
2911 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2915 template <typename Derived>
2916 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2920 template <typename Derived>
2922 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2926 template <typename Derived>
2927 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2931 template <typename Derived>
2932 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2936 template <typename Derived>
2937 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2941 template <typename Derived>
2942 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2946 template <typename Derived>
2947 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2951 template <typename Derived>
2952 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
2956 template <typename Derived>
2957 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
2961 template <typename Derived>
2962 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
2966 template <typename Derived>
2967 template <typename T>
2968 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2969 for (auto *E : Node->varlists()) {
2970 TRY_TO(TraverseStmt(E));
2975 template <typename Derived>
2976 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
2977 TRY_TO(VisitOMPClauseList(C));
2978 for (auto *E : C->private_copies()) {
2979 TRY_TO(TraverseStmt(E));
2984 template <typename Derived>
2985 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
2986 OMPFirstprivateClause *C) {
2987 TRY_TO(VisitOMPClauseList(C));
2988 TRY_TO(VisitOMPClauseWithPreInit(C));
2989 for (auto *E : C->private_copies()) {
2990 TRY_TO(TraverseStmt(E));
2992 for (auto *E : C->inits()) {
2993 TRY_TO(TraverseStmt(E));
2998 template <typename Derived>
2999 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
3000 OMPLastprivateClause *C) {
3001 TRY_TO(VisitOMPClauseList(C));
3002 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3003 for (auto *E : C->private_copies()) {
3004 TRY_TO(TraverseStmt(E));
3006 for (auto *E : C->source_exprs()) {
3007 TRY_TO(TraverseStmt(E));
3009 for (auto *E : C->destination_exprs()) {
3010 TRY_TO(TraverseStmt(E));
3012 for (auto *E : C->assignment_ops()) {
3013 TRY_TO(TraverseStmt(E));
3018 template <typename Derived>
3019 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
3020 TRY_TO(VisitOMPClauseList(C));
3024 template <typename Derived>
3025 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
3026 TRY_TO(TraverseStmt(C->getStep()));
3027 TRY_TO(TraverseStmt(C->getCalcStep()));
3028 TRY_TO(VisitOMPClauseList(C));
3029 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3030 for (auto *E : C->privates()) {
3031 TRY_TO(TraverseStmt(E));
3033 for (auto *E : C->inits()) {
3034 TRY_TO(TraverseStmt(E));
3036 for (auto *E : C->updates()) {
3037 TRY_TO(TraverseStmt(E));
3039 for (auto *E : C->finals()) {
3040 TRY_TO(TraverseStmt(E));
3045 template <typename Derived>
3046 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
3047 TRY_TO(TraverseStmt(C->getAlignment()));
3048 TRY_TO(VisitOMPClauseList(C));
3052 template <typename Derived>
3053 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
3054 TRY_TO(VisitOMPClauseList(C));
3055 for (auto *E : C->source_exprs()) {
3056 TRY_TO(TraverseStmt(E));
3058 for (auto *E : C->destination_exprs()) {
3059 TRY_TO(TraverseStmt(E));
3061 for (auto *E : C->assignment_ops()) {
3062 TRY_TO(TraverseStmt(E));
3067 template <typename Derived>
3068 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
3069 OMPCopyprivateClause *C) {
3070 TRY_TO(VisitOMPClauseList(C));
3071 for (auto *E : C->source_exprs()) {
3072 TRY_TO(TraverseStmt(E));
3074 for (auto *E : C->destination_exprs()) {
3075 TRY_TO(TraverseStmt(E));
3077 for (auto *E : C->assignment_ops()) {
3078 TRY_TO(TraverseStmt(E));
3083 template <typename Derived>
3085 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
3086 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3087 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3088 TRY_TO(VisitOMPClauseList(C));
3089 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3090 for (auto *E : C->privates()) {
3091 TRY_TO(TraverseStmt(E));
3093 for (auto *E : C->lhs_exprs()) {
3094 TRY_TO(TraverseStmt(E));
3096 for (auto *E : C->rhs_exprs()) {
3097 TRY_TO(TraverseStmt(E));
3099 for (auto *E : C->reduction_ops()) {
3100 TRY_TO(TraverseStmt(E));
3105 template <typename Derived>
3106 bool RecursiveASTVisitor<Derived>::VisitOMPTaskReductionClause(
3107 OMPTaskReductionClause *C) {
3108 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3109 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3110 TRY_TO(VisitOMPClauseList(C));
3111 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3112 for (auto *E : C->privates()) {
3113 TRY_TO(TraverseStmt(E));
3115 for (auto *E : C->lhs_exprs()) {
3116 TRY_TO(TraverseStmt(E));
3118 for (auto *E : C->rhs_exprs()) {
3119 TRY_TO(TraverseStmt(E));
3121 for (auto *E : C->reduction_ops()) {
3122 TRY_TO(TraverseStmt(E));
3127 template <typename Derived>
3128 bool RecursiveASTVisitor<Derived>::VisitOMPInReductionClause(
3129 OMPInReductionClause *C) {
3130 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3131 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3132 TRY_TO(VisitOMPClauseList(C));
3133 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3134 for (auto *E : C->privates()) {
3135 TRY_TO(TraverseStmt(E));
3137 for (auto *E : C->lhs_exprs()) {
3138 TRY_TO(TraverseStmt(E));
3140 for (auto *E : C->rhs_exprs()) {
3141 TRY_TO(TraverseStmt(E));
3143 for (auto *E : C->reduction_ops()) {
3144 TRY_TO(TraverseStmt(E));
3146 for (auto *E : C->taskgroup_descriptors())
3147 TRY_TO(TraverseStmt(E));
3151 template <typename Derived>
3152 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
3153 TRY_TO(VisitOMPClauseList(C));
3157 template <typename Derived>
3158 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
3159 TRY_TO(VisitOMPClauseList(C));
3163 template <typename Derived>
3164 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
3165 TRY_TO(VisitOMPClauseWithPreInit(C));
3166 TRY_TO(TraverseStmt(C->getDevice()));
3170 template <typename Derived>
3171 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
3172 TRY_TO(VisitOMPClauseList(C));
3176 template <typename Derived>
3177 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
3178 OMPNumTeamsClause *C) {
3179 TRY_TO(VisitOMPClauseWithPreInit(C));
3180 TRY_TO(TraverseStmt(C->getNumTeams()));
3184 template <typename Derived>
3185 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
3186 OMPThreadLimitClause *C) {
3187 TRY_TO(VisitOMPClauseWithPreInit(C));
3188 TRY_TO(TraverseStmt(C->getThreadLimit()));
3192 template <typename Derived>
3193 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
3194 OMPPriorityClause *C) {
3195 TRY_TO(TraverseStmt(C->getPriority()));
3199 template <typename Derived>
3200 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
3201 OMPGrainsizeClause *C) {
3202 TRY_TO(TraverseStmt(C->getGrainsize()));
3206 template <typename Derived>
3207 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
3208 OMPNumTasksClause *C) {
3209 TRY_TO(TraverseStmt(C->getNumTasks()));
3213 template <typename Derived>
3214 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
3215 TRY_TO(TraverseStmt(C->getHint()));
3219 template <typename Derived>
3220 bool RecursiveASTVisitor<Derived>::VisitOMPDistScheduleClause(
3221 OMPDistScheduleClause *C) {
3222 TRY_TO(VisitOMPClauseWithPreInit(C));
3223 TRY_TO(TraverseStmt(C->getChunkSize()));
3227 template <typename Derived>
3229 RecursiveASTVisitor<Derived>::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
3233 template <typename Derived>
3234 bool RecursiveASTVisitor<Derived>::VisitOMPToClause(OMPToClause *C) {
3235 TRY_TO(VisitOMPClauseList(C));
3239 template <typename Derived>
3240 bool RecursiveASTVisitor<Derived>::VisitOMPFromClause(OMPFromClause *C) {
3241 TRY_TO(VisitOMPClauseList(C));
3245 template <typename Derived>
3246 bool RecursiveASTVisitor<Derived>::VisitOMPUseDevicePtrClause(
3247 OMPUseDevicePtrClause *C) {
3248 TRY_TO(VisitOMPClauseList(C));
3252 template <typename Derived>
3253 bool RecursiveASTVisitor<Derived>::VisitOMPIsDevicePtrClause(
3254 OMPIsDevicePtrClause *C) {
3255 TRY_TO(VisitOMPClauseList(C));
3259 // FIXME: look at the following tricky-seeming exprs to see if we
3260 // need to recurse on anything. These are ones that have methods
3261 // returning decls or qualtypes or nestednamespecifier -- though I'm
3262 // not sure if they own them -- or just seemed very complicated, or
3263 // had lots of sub-types to explore.
3265 // VisitOverloadExpr and its children: recurse on template args? etc?
3267 // FIXME: go through all the stmts and exprs again, and see which of them
3268 // create new types, and recurse on the types (TypeLocs?) of those.
3271 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
3272 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
3273 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
3274 // Every class that has getQualifier.
3276 #undef DEF_TRAVERSE_STMT
3277 #undef TRAVERSE_STMT
3278 #undef TRAVERSE_STMT_BASE
3282 } // end namespace clang
3284 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H