1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the RecursiveASTVisitor interface, which recursively
10 // traverses the entire AST.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
14 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclFriend.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclOpenMP.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/ExprCXX.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/AST/ExprOpenMP.h"
29 #include "clang/AST/LambdaCapture.h"
30 #include "clang/AST/NestedNameSpecifier.h"
31 #include "clang/AST/OpenMPClause.h"
32 #include "clang/AST/Stmt.h"
33 #include "clang/AST/StmtCXX.h"
34 #include "clang/AST/StmtObjC.h"
35 #include "clang/AST/StmtOpenMP.h"
36 #include "clang/AST/TemplateBase.h"
37 #include "clang/AST/TemplateName.h"
38 #include "clang/AST/Type.h"
39 #include "clang/AST/TypeLoc.h"
40 #include "clang/Basic/LLVM.h"
41 #include "clang/Basic/OpenMPKinds.h"
42 #include "clang/Basic/Specifiers.h"
43 #include "llvm/ADT/PointerIntPair.h"
44 #include "llvm/ADT/SmallVector.h"
45 #include "llvm/Support/Casting.h"
48 #include <type_traits>
50 // The following three macros are used for meta programming. The code
51 // using them is responsible for defining macro OPERATOR().
53 // All unary operators.
54 #define UNARYOP_LIST() \
55 OPERATOR(PostInc) OPERATOR(PostDec) OPERATOR(PreInc) OPERATOR(PreDec) \
56 OPERATOR(AddrOf) OPERATOR(Deref) OPERATOR(Plus) OPERATOR(Minus) \
57 OPERATOR(Not) OPERATOR(LNot) OPERATOR(Real) OPERATOR(Imag) \
58 OPERATOR(Extension) OPERATOR(Coawait)
60 // All binary operators (excluding compound assign operators).
61 #define BINOP_LIST() \
62 OPERATOR(PtrMemD) OPERATOR(PtrMemI) OPERATOR(Mul) OPERATOR(Div) \
63 OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) OPERATOR(Shr) \
64 OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) OPERATOR(GE) OPERATOR(EQ) \
65 OPERATOR(NE) OPERATOR(Cmp) OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) \
66 OPERATOR(LAnd) OPERATOR(LOr) OPERATOR(Assign) OPERATOR(Comma)
68 // All compound assign operators.
70 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
71 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
75 // A helper macro to implement short-circuiting when recursing. It
76 // invokes CALL_EXPR, which must be a method call, on the derived
77 // object (s.t. a user of RecursiveASTVisitor can override the method
79 #define TRY_TO(CALL_EXPR) \
81 if (!getDerived().CALL_EXPR) \
85 /// A class that does preorder or postorder
86 /// depth-first traversal on the entire Clang AST and visits each node.
88 /// This class performs three distinct tasks:
89 /// 1. traverse the AST (i.e. go to each node);
90 /// 2. at a given node, walk up the class hierarchy, starting from
91 /// the node's dynamic type, until the top-most class (e.g. Stmt,
92 /// Decl, or Type) is reached.
93 /// 3. given a (node, class) combination, where 'class' is some base
94 /// class of the dynamic type of 'node', call a user-overridable
95 /// function to actually visit the node.
97 /// These tasks are done by three groups of methods, respectively:
98 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
99 /// for traversing an AST rooted at x. This method simply
100 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
101 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
102 /// then recursively visits the child nodes of x.
103 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
105 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
106 /// any child node of x. Instead, it first calls WalkUpFromBar(x)
107 /// where Bar is the direct parent class of Foo (unless Foo has
108 /// no parent), and then calls VisitFoo(x) (see the next list item).
109 /// 3. VisitFoo(Foo *x) does task #3.
111 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
112 /// Visit*). A method (e.g. Traverse*) may call methods from the same
113 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
114 /// It may not call methods from a higher tier.
116 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
117 /// is Foo's super class) before calling VisitFoo(), the result is
118 /// that the Visit*() methods for a given node are called in the
119 /// top-down order (e.g. for a node of type NamespaceDecl, the order will
120 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
122 /// This scheme guarantees that all Visit*() calls for the same AST
123 /// node are grouped together. In other words, Visit*() methods for
124 /// different nodes are never interleaved.
126 /// Clients of this visitor should subclass the visitor (providing
127 /// themselves as the template argument, using the curiously recurring
128 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
129 /// and Visit* methods for declarations, types, statements,
130 /// expressions, or other AST nodes where the visitor should customize
131 /// behavior. Most users only need to override Visit*. Advanced
132 /// users may override Traverse* and WalkUpFrom* to implement custom
133 /// traversal strategies. Returning false from one of these overridden
134 /// functions will abort the entire traversal.
136 /// By default, this visitor tries to visit every part of the explicit
137 /// source code exactly once. The default policy towards templates
138 /// is to descend into the 'pattern' class or function body, not any
139 /// explicit or implicit instantiations. Explicit specializations
140 /// are still visited, and the patterns of partial specializations
141 /// are visited separately. This behavior can be changed by
142 /// overriding shouldVisitTemplateInstantiations() in the derived class
143 /// to return true, in which case all known implicit and explicit
144 /// instantiations will be visited at the same time as the pattern
145 /// from which they were produced.
147 /// By default, this visitor preorder traverses the AST. If postorder traversal
148 /// is needed, the \c shouldTraversePostOrder method needs to be overridden
149 /// to return \c true.
150 template <typename Derived> class RecursiveASTVisitor {
152 /// A queue used for performing data recursion over statements.
153 /// Parameters involving this type are used to implement data
154 /// recursion over Stmts and Exprs within this class, and should
155 /// typically not be explicitly specified by derived classes.
156 /// The bool bit indicates whether the statement has been traversed or not.
157 typedef SmallVectorImpl<llvm::PointerIntPair<Stmt *, 1, bool>>
160 /// Return a reference to the derived class.
161 Derived &getDerived() { return *static_cast<Derived *>(this); }
163 /// Return whether this visitor should recurse into
164 /// template instantiations.
165 bool shouldVisitTemplateInstantiations() const { return false; }
167 /// Return whether this visitor should recurse into the types of
169 bool shouldWalkTypesOfTypeLocs() const { return true; }
171 /// Return whether this visitor should recurse into implicit
172 /// code, e.g., implicit constructors and destructors.
173 bool shouldVisitImplicitCode() const { return false; }
175 /// Return whether this visitor should traverse post-order.
176 bool shouldTraversePostOrder() const { return false; }
178 /// Recursively visits an entire AST, starting from the top-level Decls
179 /// in the AST traversal scope (by default, the TranslationUnitDecl).
180 /// \returns false if visitation was terminated early.
181 bool TraverseAST(ASTContext &AST) {
182 for (Decl *D : AST.getTraversalScope())
183 if (!getDerived().TraverseDecl(D))
188 /// Recursively visit a statement or expression, by
189 /// dispatching to Traverse*() based on the argument's dynamic type.
191 /// \returns false if the visitation was terminated early, true
192 /// otherwise (including when the argument is nullptr).
193 bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr);
195 /// Invoked before visiting a statement or expression via data recursion.
197 /// \returns false to skip visiting the node, true otherwise.
198 bool dataTraverseStmtPre(Stmt *S) { return true; }
200 /// Invoked after visiting a statement or expression via data recursion.
201 /// This is not invoked if the previously invoked \c dataTraverseStmtPre
204 /// \returns false if the visitation was terminated early, true otherwise.
205 bool dataTraverseStmtPost(Stmt *S) { return true; }
207 /// Recursively visit a type, by dispatching to
208 /// Traverse*Type() based on the argument's getTypeClass() property.
210 /// \returns false if the visitation was terminated early, true
211 /// otherwise (including when the argument is a Null type).
212 bool TraverseType(QualType T);
214 /// Recursively visit a type with location, by dispatching to
215 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
217 /// \returns false if the visitation was terminated early, true
218 /// otherwise (including when the argument is a Null type location).
219 bool TraverseTypeLoc(TypeLoc TL);
221 /// Recursively visit an attribute, by dispatching to
222 /// Traverse*Attr() based on the argument's dynamic type.
224 /// \returns false if the visitation was terminated early, true
225 /// otherwise (including when the argument is a Null type location).
226 bool TraverseAttr(Attr *At);
228 /// Recursively visit a declaration, by dispatching to
229 /// Traverse*Decl() based on the argument's dynamic type.
231 /// \returns false if the visitation was terminated early, true
232 /// otherwise (including when the argument is NULL).
233 bool TraverseDecl(Decl *D);
235 /// Recursively visit a C++ nested-name-specifier.
237 /// \returns false if the visitation was terminated early, true otherwise.
238 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
240 /// Recursively visit a C++ nested-name-specifier with location
243 /// \returns false if the visitation was terminated early, true otherwise.
244 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
246 /// Recursively visit a name with its location information.
248 /// \returns false if the visitation was terminated early, true otherwise.
249 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
251 /// Recursively visit a template name and dispatch to the
252 /// appropriate method.
254 /// \returns false if the visitation was terminated early, true otherwise.
255 bool TraverseTemplateName(TemplateName Template);
257 /// Recursively visit a template argument and dispatch to the
258 /// appropriate method for the argument type.
260 /// \returns false if the visitation was terminated early, true otherwise.
261 // FIXME: migrate callers to TemplateArgumentLoc instead.
262 bool TraverseTemplateArgument(const TemplateArgument &Arg);
264 /// Recursively visit a template argument location and dispatch to the
265 /// appropriate method for the argument type.
267 /// \returns false if the visitation was terminated early, true otherwise.
268 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
270 /// Recursively visit a set of template arguments.
271 /// This can be overridden by a subclass, but it's not expected that
272 /// will be needed -- this visitor always dispatches to another.
274 /// \returns false if the visitation was terminated early, true otherwise.
275 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
276 bool TraverseTemplateArguments(const TemplateArgument *Args,
279 /// Recursively visit a base specifier. This can be overridden by a
282 /// \returns false if the visitation was terminated early, true otherwise.
283 bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base);
285 /// Recursively visit a constructor initializer. This
286 /// automatically dispatches to another visitor for the initializer
287 /// expression, but not for the name of the initializer, so may
288 /// be overridden for clients that need access to the name.
290 /// \returns false if the visitation was terminated early, true otherwise.
291 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
293 /// Recursively visit a lambda capture. \c Init is the expression that
294 /// will be used to initialize the capture.
296 /// \returns false if the visitation was terminated early, true otherwise.
297 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
300 /// Recursively visit the syntactic or semantic form of an
301 /// initialization list.
303 /// \returns false if the visitation was terminated early, true otherwise.
304 bool TraverseSynOrSemInitListExpr(InitListExpr *S,
305 DataRecursionQueue *Queue = nullptr);
307 // ---- Methods on Attrs ----
309 // Visit an attribute.
310 bool VisitAttr(Attr *A) { return true; }
312 // Declare Traverse* and empty Visit* for all Attr classes.
313 #define ATTR_VISITOR_DECLS_ONLY
314 #include "clang/AST/AttrVisitor.inc"
315 #undef ATTR_VISITOR_DECLS_ONLY
317 // ---- Methods on Stmts ----
319 Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); }
322 template<typename T, typename U>
323 struct has_same_member_pointer_type : std::false_type {};
324 template<typename T, typename U, typename R, typename... P>
325 struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)>
328 // Traverse the given statement. If the most-derived traverse function takes a
329 // data recursion queue, pass it on; otherwise, discard it. Note that the
330 // first branch of this conditional must compile whether or not the derived
331 // class can take a queue, so if we're taking the second arm, make the first
332 // arm call our function rather than the derived class version.
333 #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE) \
334 (has_same_member_pointer_type<decltype( \
335 &RecursiveASTVisitor::Traverse##NAME), \
336 decltype(&Derived::Traverse##NAME)>::value \
337 ? static_cast<typename std::conditional< \
338 has_same_member_pointer_type< \
339 decltype(&RecursiveASTVisitor::Traverse##NAME), \
340 decltype(&Derived::Traverse##NAME)>::value, \
341 Derived &, RecursiveASTVisitor &>::type>(*this) \
342 .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE) \
343 : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)))
345 // Try to traverse the given statement, or enqueue it if we're performing data
346 // recursion in the middle of traversing another statement. Can only be called
347 // from within a DEF_TRAVERSE_STMT body or similar context.
348 #define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S) \
350 if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue)) \
355 // Declare Traverse*() for all concrete Stmt classes.
356 #define ABSTRACT_STMT(STMT)
357 #define STMT(CLASS, PARENT) \
358 bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
359 #include "clang/AST/StmtNodes.inc"
360 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
362 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
363 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
364 bool VisitStmt(Stmt *S) { return true; }
365 #define STMT(CLASS, PARENT) \
366 bool WalkUpFrom##CLASS(CLASS *S) { \
367 TRY_TO(WalkUpFrom##PARENT(S)); \
368 TRY_TO(Visit##CLASS(S)); \
371 bool Visit##CLASS(CLASS *S) { return true; }
372 #include "clang/AST/StmtNodes.inc"
374 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
375 // operator methods. Unary operators are not classes in themselves
376 // (they're all opcodes in UnaryOperator) but do have visitors.
377 #define OPERATOR(NAME) \
378 bool TraverseUnary##NAME(UnaryOperator *S, \
379 DataRecursionQueue *Queue = nullptr) { \
380 if (!getDerived().shouldTraversePostOrder()) \
381 TRY_TO(WalkUpFromUnary##NAME(S)); \
382 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr()); \
385 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
386 TRY_TO(WalkUpFromUnaryOperator(S)); \
387 TRY_TO(VisitUnary##NAME(S)); \
390 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
395 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
396 // operator methods. Binary operators are not classes in themselves
397 // (they're all opcodes in BinaryOperator) but do have visitors.
398 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
399 bool TraverseBin##NAME(BINOP_TYPE *S, DataRecursionQueue *Queue = nullptr) { \
400 if (!getDerived().shouldTraversePostOrder()) \
401 TRY_TO(WalkUpFromBin##NAME(S)); \
402 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLHS()); \
403 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRHS()); \
406 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
407 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
408 TRY_TO(VisitBin##NAME(S)); \
411 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
413 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
417 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
418 // assignment methods. Compound assignment operators are not
419 // classes in themselves (they're all opcodes in
420 // CompoundAssignOperator) but do have visitors.
421 #define OPERATOR(NAME) \
422 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
426 #undef GENERAL_BINOP_FALLBACK
428 // ---- Methods on Types ----
429 // FIXME: revamp to take TypeLoc's rather than Types.
431 // Declare Traverse*() for all concrete Type classes.
432 #define ABSTRACT_TYPE(CLASS, BASE)
433 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T);
434 #include "clang/AST/TypeNodes.inc"
435 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
437 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
438 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
439 bool VisitType(Type *T) { return true; }
440 #define TYPE(CLASS, BASE) \
441 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
442 TRY_TO(WalkUpFrom##BASE(T)); \
443 TRY_TO(Visit##CLASS##Type(T)); \
446 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
447 #include "clang/AST/TypeNodes.inc"
449 // ---- Methods on TypeLocs ----
450 // FIXME: this currently just calls the matching Type methods
452 // Declare Traverse*() for all concrete TypeLoc classes.
453 #define ABSTRACT_TYPELOC(CLASS, BASE)
454 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
455 #include "clang/AST/TypeLocNodes.def"
456 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
458 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
459 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
460 bool VisitTypeLoc(TypeLoc TL) { return true; }
462 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
463 // TypeNodes.inc and thus need to be handled specially.
464 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
465 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
467 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
468 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
469 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
471 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
473 // Note that BASE includes trailing 'Type' which CLASS doesn't.
474 #define TYPE(CLASS, BASE) \
475 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
476 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
477 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
480 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
481 #include "clang/AST/TypeNodes.inc"
483 // ---- Methods on Decls ----
485 // Declare Traverse*() for all concrete Decl classes.
486 #define ABSTRACT_DECL(DECL)
487 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D);
488 #include "clang/AST/DeclNodes.inc"
489 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
491 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
492 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
493 bool VisitDecl(Decl *D) { return true; }
494 #define DECL(CLASS, BASE) \
495 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
496 TRY_TO(WalkUpFrom##BASE(D)); \
497 TRY_TO(Visit##CLASS##Decl(D)); \
500 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
501 #include "clang/AST/DeclNodes.inc"
503 bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
506 // These are helper methods used by more than one Traverse* method.
507 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
509 // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
510 template <typename T>
511 bool TraverseDeclTemplateParameterLists(T *D);
513 #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \
514 bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D);
515 DEF_TRAVERSE_TMPL_INST(Class)
516 DEF_TRAVERSE_TMPL_INST(Var)
517 DEF_TRAVERSE_TMPL_INST(Function)
518 #undef DEF_TRAVERSE_TMPL_INST
519 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
521 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
522 bool TraverseRecordHelper(RecordDecl *D);
523 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
524 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
525 bool TraverseDeclContextHelper(DeclContext *DC);
526 bool TraverseFunctionHelper(FunctionDecl *D);
527 bool TraverseVarHelper(VarDecl *D);
528 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
529 bool TraverseOMPLoopDirective(OMPLoopDirective *S);
530 bool TraverseOMPClause(OMPClause *C);
531 #define OPENMP_CLAUSE(Name, Class) bool Visit##Class(Class *C);
532 #include "clang/Basic/OpenMPKinds.def"
533 /// Process clauses with list of variables.
534 template <typename T> bool VisitOMPClauseList(T *Node);
535 /// Process clauses with pre-initis.
536 bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node);
537 bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node);
539 bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
540 bool PostVisitStmt(Stmt *S);
543 template <typename Derived>
544 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
545 DataRecursionQueue *Queue) {
546 #define DISPATCH_STMT(NAME, CLASS, VAR) \
547 return TRAVERSE_STMT_BASE(NAME, CLASS, VAR, Queue);
549 // If we have a binary expr, dispatch to the subcode of the binop. A smart
550 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
552 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
553 switch (BinOp->getOpcode()) {
554 #define OPERATOR(NAME) \
556 DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
562 #define OPERATOR(NAME) \
563 case BO_##NAME##Assign: \
564 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
570 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
571 switch (UnOp->getOpcode()) {
572 #define OPERATOR(NAME) \
574 DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
582 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
583 switch (S->getStmtClass()) {
584 case Stmt::NoStmtClass:
586 #define ABSTRACT_STMT(STMT)
587 #define STMT(CLASS, PARENT) \
588 case Stmt::CLASS##Class: \
589 DISPATCH_STMT(CLASS, CLASS, S);
590 #include "clang/AST/StmtNodes.inc"
598 template <typename Derived>
599 bool RecursiveASTVisitor<Derived>::PostVisitStmt(Stmt *S) {
600 switch (S->getStmtClass()) {
601 case Stmt::NoStmtClass:
603 #define ABSTRACT_STMT(STMT)
604 #define STMT(CLASS, PARENT) \
605 case Stmt::CLASS##Class: \
606 TRY_TO(WalkUpFrom##CLASS(static_cast<CLASS *>(S))); break;
607 #define INITLISTEXPR(CLASS, PARENT) \
608 case Stmt::CLASS##Class: \
610 auto ILE = static_cast<CLASS *>(S); \
611 if (auto Syn = ILE->isSemanticForm() ? ILE->getSyntacticForm() : ILE) \
612 TRY_TO(WalkUpFrom##CLASS(Syn)); \
613 if (auto Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm()) \
614 TRY_TO(WalkUpFrom##CLASS(Sem)); \
617 #include "clang/AST/StmtNodes.inc"
625 template <typename Derived>
626 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
627 DataRecursionQueue *Queue) {
632 Queue->push_back({S, false});
636 SmallVector<llvm::PointerIntPair<Stmt *, 1, bool>, 8> LocalQueue;
637 LocalQueue.push_back({S, false});
639 while (!LocalQueue.empty()) {
640 auto &CurrSAndVisited = LocalQueue.back();
641 Stmt *CurrS = CurrSAndVisited.getPointer();
642 bool Visited = CurrSAndVisited.getInt();
644 LocalQueue.pop_back();
645 TRY_TO(dataTraverseStmtPost(CurrS));
646 if (getDerived().shouldTraversePostOrder()) {
647 TRY_TO(PostVisitStmt(CurrS));
652 if (getDerived().dataTraverseStmtPre(CurrS)) {
653 CurrSAndVisited.setInt(true);
654 size_t N = LocalQueue.size();
655 TRY_TO(dataTraverseNode(CurrS, &LocalQueue));
656 // Process new children in the order they were added.
657 std::reverse(LocalQueue.begin() + N, LocalQueue.end());
659 LocalQueue.pop_back();
666 #define DISPATCH(NAME, CLASS, VAR) \
667 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))
669 template <typename Derived>
670 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
674 switch (T->getTypeClass()) {
675 #define ABSTRACT_TYPE(CLASS, BASE)
676 #define TYPE(CLASS, BASE) \
678 DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr()));
679 #include "clang/AST/TypeNodes.inc"
685 template <typename Derived>
686 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
690 switch (TL.getTypeLocClass()) {
691 #define ABSTRACT_TYPELOC(CLASS, BASE)
692 #define TYPELOC(CLASS, BASE) \
693 case TypeLoc::CLASS: \
694 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
695 #include "clang/AST/TypeLocNodes.def"
701 // Define the Traverse*Attr(Attr* A) methods
702 #define VISITORCLASS RecursiveASTVisitor
703 #include "clang/AST/AttrVisitor.inc"
706 template <typename Derived>
707 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
711 // As a syntax visitor, by default we want to ignore declarations for
712 // implicit declarations (ones not typed explicitly by the user).
713 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
716 switch (D->getKind()) {
717 #define ABSTRACT_DECL(DECL)
718 #define DECL(CLASS, BASE) \
720 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \
723 #include "clang/AST/DeclNodes.inc"
730 template <typename Derived>
731 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
732 NestedNameSpecifier *NNS) {
736 if (NNS->getPrefix())
737 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
739 switch (NNS->getKind()) {
740 case NestedNameSpecifier::Identifier:
741 case NestedNameSpecifier::Namespace:
742 case NestedNameSpecifier::NamespaceAlias:
743 case NestedNameSpecifier::Global:
744 case NestedNameSpecifier::Super:
747 case NestedNameSpecifier::TypeSpec:
748 case NestedNameSpecifier::TypeSpecWithTemplate:
749 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
755 template <typename Derived>
756 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
757 NestedNameSpecifierLoc NNS) {
761 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
762 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
764 switch (NNS.getNestedNameSpecifier()->getKind()) {
765 case NestedNameSpecifier::Identifier:
766 case NestedNameSpecifier::Namespace:
767 case NestedNameSpecifier::NamespaceAlias:
768 case NestedNameSpecifier::Global:
769 case NestedNameSpecifier::Super:
772 case NestedNameSpecifier::TypeSpec:
773 case NestedNameSpecifier::TypeSpecWithTemplate:
774 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
781 template <typename Derived>
782 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
783 DeclarationNameInfo NameInfo) {
784 switch (NameInfo.getName().getNameKind()) {
785 case DeclarationName::CXXConstructorName:
786 case DeclarationName::CXXDestructorName:
787 case DeclarationName::CXXConversionFunctionName:
788 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
789 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
792 case DeclarationName::CXXDeductionGuideName:
793 TRY_TO(TraverseTemplateName(
794 TemplateName(NameInfo.getName().getCXXDeductionGuideTemplate())));
797 case DeclarationName::Identifier:
798 case DeclarationName::ObjCZeroArgSelector:
799 case DeclarationName::ObjCOneArgSelector:
800 case DeclarationName::ObjCMultiArgSelector:
801 case DeclarationName::CXXOperatorName:
802 case DeclarationName::CXXLiteralOperatorName:
803 case DeclarationName::CXXUsingDirective:
810 template <typename Derived>
811 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
812 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
813 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
814 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
815 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
820 template <typename Derived>
821 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
822 const TemplateArgument &Arg) {
823 switch (Arg.getKind()) {
824 case TemplateArgument::Null:
825 case TemplateArgument::Declaration:
826 case TemplateArgument::Integral:
827 case TemplateArgument::NullPtr:
830 case TemplateArgument::Type:
831 return getDerived().TraverseType(Arg.getAsType());
833 case TemplateArgument::Template:
834 case TemplateArgument::TemplateExpansion:
835 return getDerived().TraverseTemplateName(
836 Arg.getAsTemplateOrTemplatePattern());
838 case TemplateArgument::Expression:
839 return getDerived().TraverseStmt(Arg.getAsExpr());
841 case TemplateArgument::Pack:
842 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
849 // FIXME: no template name location?
850 // FIXME: no source locations for a template argument pack?
851 template <typename Derived>
852 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
853 const TemplateArgumentLoc &ArgLoc) {
854 const TemplateArgument &Arg = ArgLoc.getArgument();
856 switch (Arg.getKind()) {
857 case TemplateArgument::Null:
858 case TemplateArgument::Declaration:
859 case TemplateArgument::Integral:
860 case TemplateArgument::NullPtr:
863 case TemplateArgument::Type: {
864 // FIXME: how can TSI ever be NULL?
865 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
866 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
868 return getDerived().TraverseType(Arg.getAsType());
871 case TemplateArgument::Template:
872 case TemplateArgument::TemplateExpansion:
873 if (ArgLoc.getTemplateQualifierLoc())
874 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
875 ArgLoc.getTemplateQualifierLoc()));
876 return getDerived().TraverseTemplateName(
877 Arg.getAsTemplateOrTemplatePattern());
879 case TemplateArgument::Expression:
880 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
882 case TemplateArgument::Pack:
883 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
890 template <typename Derived>
891 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
892 const TemplateArgument *Args, unsigned NumArgs) {
893 for (unsigned I = 0; I != NumArgs; ++I) {
894 TRY_TO(TraverseTemplateArgument(Args[I]));
900 template <typename Derived>
901 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
902 CXXCtorInitializer *Init) {
903 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
904 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
906 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
907 TRY_TO(TraverseStmt(Init->getInit()));
912 template <typename Derived>
914 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
915 const LambdaCapture *C,
917 if (LE->isInitCapture(C))
918 TRY_TO(TraverseDecl(C->getCapturedVar()));
920 TRY_TO(TraverseStmt(Init));
924 // ----------------- Type traversal -----------------
926 // This macro makes available a variable T, the passed-in type.
927 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
928 template <typename Derived> \
929 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \
930 if (!getDerived().shouldTraversePostOrder()) \
931 TRY_TO(WalkUpFrom##TYPE(T)); \
933 if (getDerived().shouldTraversePostOrder()) \
934 TRY_TO(WalkUpFrom##TYPE(T)); \
938 DEF_TRAVERSE_TYPE(BuiltinType, {})
940 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
942 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
944 DEF_TRAVERSE_TYPE(BlockPointerType,
945 { TRY_TO(TraverseType(T->getPointeeType())); })
947 DEF_TRAVERSE_TYPE(LValueReferenceType,
948 { TRY_TO(TraverseType(T->getPointeeType())); })
950 DEF_TRAVERSE_TYPE(RValueReferenceType,
951 { TRY_TO(TraverseType(T->getPointeeType())); })
953 DEF_TRAVERSE_TYPE(MemberPointerType, {
954 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
955 TRY_TO(TraverseType(T->getPointeeType()));
958 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
960 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
962 DEF_TRAVERSE_TYPE(ConstantArrayType, {
963 TRY_TO(TraverseType(T->getElementType()));
964 if (T->getSizeExpr())
965 TRY_TO(TraverseStmt(const_cast<Expr*>(T->getSizeExpr())));
968 DEF_TRAVERSE_TYPE(IncompleteArrayType,
969 { TRY_TO(TraverseType(T->getElementType())); })
971 DEF_TRAVERSE_TYPE(VariableArrayType, {
972 TRY_TO(TraverseType(T->getElementType()));
973 TRY_TO(TraverseStmt(T->getSizeExpr()));
976 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
977 TRY_TO(TraverseType(T->getElementType()));
978 if (T->getSizeExpr())
979 TRY_TO(TraverseStmt(T->getSizeExpr()));
982 DEF_TRAVERSE_TYPE(DependentAddressSpaceType, {
983 TRY_TO(TraverseStmt(T->getAddrSpaceExpr()));
984 TRY_TO(TraverseType(T->getPointeeType()));
987 DEF_TRAVERSE_TYPE(DependentVectorType, {
988 if (T->getSizeExpr())
989 TRY_TO(TraverseStmt(T->getSizeExpr()));
990 TRY_TO(TraverseType(T->getElementType()));
993 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
994 if (T->getSizeExpr())
995 TRY_TO(TraverseStmt(T->getSizeExpr()));
996 TRY_TO(TraverseType(T->getElementType()));
999 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
1001 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
1003 DEF_TRAVERSE_TYPE(FunctionNoProtoType,
1004 { TRY_TO(TraverseType(T->getReturnType())); })
1006 DEF_TRAVERSE_TYPE(FunctionProtoType, {
1007 TRY_TO(TraverseType(T->getReturnType()));
1009 for (const auto &A : T->param_types()) {
1010 TRY_TO(TraverseType(A));
1013 for (const auto &E : T->exceptions()) {
1014 TRY_TO(TraverseType(E));
1017 if (Expr *NE = T->getNoexceptExpr())
1018 TRY_TO(TraverseStmt(NE));
1021 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
1022 DEF_TRAVERSE_TYPE(TypedefType, {})
1024 DEF_TRAVERSE_TYPE(TypeOfExprType,
1025 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1027 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
1029 DEF_TRAVERSE_TYPE(DecltypeType,
1030 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1032 DEF_TRAVERSE_TYPE(UnaryTransformType, {
1033 TRY_TO(TraverseType(T->getBaseType()));
1034 TRY_TO(TraverseType(T->getUnderlyingType()));
1037 DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); })
1038 DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, {
1039 TRY_TO(TraverseTemplateName(T->getTemplateName()));
1040 TRY_TO(TraverseType(T->getDeducedType()));
1043 DEF_TRAVERSE_TYPE(RecordType, {})
1044 DEF_TRAVERSE_TYPE(EnumType, {})
1045 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
1046 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {
1047 TRY_TO(TraverseType(T->getReplacementType()));
1049 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {
1050 TRY_TO(TraverseTemplateArgument(T->getArgumentPack()));
1053 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
1054 TRY_TO(TraverseTemplateName(T->getTemplateName()));
1055 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1058 DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
1060 DEF_TRAVERSE_TYPE(AttributedType,
1061 { TRY_TO(TraverseType(T->getModifiedType())); })
1063 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
1065 DEF_TRAVERSE_TYPE(MacroQualifiedType,
1066 { TRY_TO(TraverseType(T->getUnderlyingType())); })
1068 DEF_TRAVERSE_TYPE(ElaboratedType, {
1069 if (T->getQualifier()) {
1070 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1072 TRY_TO(TraverseType(T->getNamedType()));
1075 DEF_TRAVERSE_TYPE(DependentNameType,
1076 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
1078 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1079 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1080 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1083 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1085 DEF_TRAVERSE_TYPE(ObjCTypeParamType, {})
1087 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1089 DEF_TRAVERSE_TYPE(ObjCObjectType, {
1090 // We have to watch out here because an ObjCInterfaceType's base
1092 if (T->getBaseType().getTypePtr() != T)
1093 TRY_TO(TraverseType(T->getBaseType()));
1094 for (auto typeArg : T->getTypeArgsAsWritten()) {
1095 TRY_TO(TraverseType(typeArg));
1099 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1100 { TRY_TO(TraverseType(T->getPointeeType())); })
1102 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1104 DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
1106 #undef DEF_TRAVERSE_TYPE
1108 // ----------------- TypeLoc traversal -----------------
1110 // This macro makes available a variable TL, the passed-in TypeLoc.
1111 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1112 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1113 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1114 // continue to work.
1115 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
1116 template <typename Derived> \
1117 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
1118 if (getDerived().shouldWalkTypesOfTypeLocs()) \
1119 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
1120 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1125 template <typename Derived>
1127 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1128 // Move this over to the 'main' typeloc tree. Note that this is a
1129 // move -- we pretend that we were really looking at the unqualified
1130 // typeloc all along -- rather than a recursion, so we don't follow
1131 // the normal CRTP plan of going through
1132 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1133 // twice for the same type (once as a QualifiedTypeLoc version of
1134 // the type, once as an UnqualifiedTypeLoc version of the type),
1135 // which in effect means we'd call VisitTypeLoc twice with the
1136 // 'same' type. This solves that problem, at the cost of never
1137 // seeing the qualified version of the type (unless the client
1138 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1139 // perfect solution. A perfect solution probably requires making
1140 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1141 // wrapper around Type* -- rather than being its own class in the
1143 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1146 DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1148 // FIXME: ComplexTypeLoc is unfinished
1149 DEF_TRAVERSE_TYPELOC(ComplexType, {
1150 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1153 DEF_TRAVERSE_TYPELOC(PointerType,
1154 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1156 DEF_TRAVERSE_TYPELOC(BlockPointerType,
1157 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1159 DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1160 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1162 DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1163 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1165 // FIXME: location of base class?
1166 // We traverse this in the type case as well, but how is it not reached through
1167 // the pointee type?
1168 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1169 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1170 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1173 DEF_TRAVERSE_TYPELOC(AdjustedType,
1174 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1176 DEF_TRAVERSE_TYPELOC(DecayedType,
1177 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1179 template <typename Derived>
1180 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1181 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1182 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1186 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1187 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1188 return TraverseArrayTypeLocHelper(TL);
1191 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1192 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1193 return TraverseArrayTypeLocHelper(TL);
1196 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1197 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1198 return TraverseArrayTypeLocHelper(TL);
1201 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1202 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1203 return TraverseArrayTypeLocHelper(TL);
1206 DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
1207 TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr()));
1208 TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType()));
1211 // FIXME: order? why not size expr first?
1212 // FIXME: base VectorTypeLoc is unfinished
1213 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1214 if (TL.getTypePtr()->getSizeExpr())
1215 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1216 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1219 // FIXME: VectorTypeLoc is unfinished
1220 DEF_TRAVERSE_TYPELOC(VectorType, {
1221 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1224 DEF_TRAVERSE_TYPELOC(DependentVectorType, {
1225 if (TL.getTypePtr()->getSizeExpr())
1226 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1227 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1230 // FIXME: size and attributes
1231 // FIXME: base VectorTypeLoc is unfinished
1232 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1233 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1236 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1237 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1239 // FIXME: location of exception specifications (attributes?)
1240 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1241 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1243 const FunctionProtoType *T = TL.getTypePtr();
1245 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1246 if (TL.getParam(I)) {
1247 TRY_TO(TraverseDecl(TL.getParam(I)));
1248 } else if (I < T->getNumParams()) {
1249 TRY_TO(TraverseType(T->getParamType(I)));
1253 for (const auto &E : T->exceptions()) {
1254 TRY_TO(TraverseType(E));
1257 if (Expr *NE = T->getNoexceptExpr())
1258 TRY_TO(TraverseStmt(NE));
1261 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1262 DEF_TRAVERSE_TYPELOC(TypedefType, {})
1264 DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1265 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1267 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1268 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1271 // FIXME: location of underlying expr
1272 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1273 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1276 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1277 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1280 DEF_TRAVERSE_TYPELOC(AutoType, {
1281 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1284 DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, {
1285 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1286 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1289 DEF_TRAVERSE_TYPELOC(RecordType, {})
1290 DEF_TRAVERSE_TYPELOC(EnumType, {})
1291 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1292 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {
1293 TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType()));
1295 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {
1296 TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack()));
1299 // FIXME: use the loc for the template name?
1300 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1301 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1302 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1303 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1307 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1309 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1311 DEF_TRAVERSE_TYPELOC(MacroQualifiedType,
1312 { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1314 DEF_TRAVERSE_TYPELOC(AttributedType,
1315 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1317 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1318 if (TL.getQualifierLoc()) {
1319 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1321 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1324 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1325 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1328 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1329 if (TL.getQualifierLoc()) {
1330 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1333 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1334 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1338 DEF_TRAVERSE_TYPELOC(PackExpansionType,
1339 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1341 DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {})
1343 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1345 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1346 // We have to watch out here because an ObjCInterfaceType's base
1348 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1349 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1350 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1351 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1354 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1355 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1357 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1359 DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1361 #undef DEF_TRAVERSE_TYPELOC
1363 // ----------------- Decl traversal -----------------
1365 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1366 // the children that come from the DeclContext associated with it.
1367 // Therefore each Traverse* only needs to worry about children other
1370 template <typename Derived>
1371 bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext(
1372 const Decl *Child) {
1373 // BlockDecls are traversed through BlockExprs,
1374 // CapturedDecls are traversed through CapturedStmts.
1375 if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child))
1377 // Lambda classes are traversed through LambdaExprs.
1378 if (const CXXRecordDecl* Cls = dyn_cast<CXXRecordDecl>(Child))
1379 return Cls->isLambda();
1383 template <typename Derived>
1384 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1388 for (auto *Child : DC->decls()) {
1389 if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1390 TRY_TO(TraverseDecl(Child));
1396 // This macro makes available a variable D, the passed-in decl.
1397 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1398 template <typename Derived> \
1399 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \
1400 bool ShouldVisitChildren = true; \
1401 bool ReturnValue = true; \
1402 if (!getDerived().shouldTraversePostOrder()) \
1403 TRY_TO(WalkUpFrom##DECL(D)); \
1405 if (ReturnValue && ShouldVisitChildren) \
1406 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1407 if (ReturnValue) { \
1408 /* Visit any attributes attached to this declaration. */ \
1409 for (auto *I : D->attrs()) \
1410 TRY_TO(getDerived().TraverseAttr(I)); \
1412 if (ReturnValue && getDerived().shouldTraversePostOrder()) \
1413 TRY_TO(WalkUpFrom##DECL(D)); \
1414 return ReturnValue; \
1417 DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1419 DEF_TRAVERSE_DECL(BlockDecl, {
1420 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1421 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1422 TRY_TO(TraverseStmt(D->getBody()));
1423 for (const auto &I : D->captures()) {
1424 if (I.hasCopyExpr()) {
1425 TRY_TO(TraverseStmt(I.getCopyExpr()));
1428 ShouldVisitChildren = false;
1431 DEF_TRAVERSE_DECL(CapturedDecl, {
1432 TRY_TO(TraverseStmt(D->getBody()));
1433 ShouldVisitChildren = false;
1436 DEF_TRAVERSE_DECL(EmptyDecl, {})
1438 DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1439 { TRY_TO(TraverseStmt(D->getAsmString())); })
1441 DEF_TRAVERSE_DECL(ImportDecl, {})
1443 DEF_TRAVERSE_DECL(FriendDecl, {
1444 // Friend is either decl or a type.
1445 if (D->getFriendType())
1446 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1448 TRY_TO(TraverseDecl(D->getFriendDecl()));
1451 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1452 if (D->getFriendType())
1453 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1455 TRY_TO(TraverseDecl(D->getFriendDecl()));
1456 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1457 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1458 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1459 ITPL != ETPL; ++ITPL) {
1460 TRY_TO(TraverseDecl(*ITPL));
1465 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1466 TRY_TO(TraverseDecl(D->getSpecialization()));
1468 if (D->hasExplicitTemplateArgs()) {
1469 TRY_TO(TraverseTemplateArgumentLocsHelper(
1470 D->getTemplateArgsAsWritten()->getTemplateArgs(),
1471 D->getTemplateArgsAsWritten()->NumTemplateArgs));
1475 DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1477 DEF_TRAVERSE_DECL(ExportDecl, {})
1479 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1482 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1483 TRY_TO(TraverseStmt(D->getAssertExpr()));
1484 TRY_TO(TraverseStmt(D->getMessage()));
1488 TranslationUnitDecl,
1489 {// Code in an unnamed namespace shows up automatically in
1490 // decls_begin()/decls_end(). Thus we don't need to recurse on
1491 // D->getAnonymousNamespace().
1494 DEF_TRAVERSE_DECL(PragmaCommentDecl, {})
1496 DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {})
1498 DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1500 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1501 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1503 // We shouldn't traverse an aliased namespace, since it will be
1504 // defined (and, therefore, traversed) somewhere else.
1505 ShouldVisitChildren = false;
1508 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1513 {// Code in an unnamed namespace shows up automatically in
1514 // decls_begin()/decls_end(). Thus we don't need to recurse on
1515 // D->getAnonymousNamespace().
1518 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1521 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1522 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1523 for (auto typeParam : *typeParamList) {
1524 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1529 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1532 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1535 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1536 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1537 for (auto typeParam : *typeParamList) {
1538 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1542 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1543 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1547 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1550 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1551 if (D->getReturnTypeSourceInfo()) {
1552 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1554 for (ParmVarDecl *Parameter : D->parameters()) {
1555 TRY_TO(TraverseDecl(Parameter));
1557 if (D->isThisDeclarationADefinition()) {
1558 TRY_TO(TraverseStmt(D->getBody()));
1560 ShouldVisitChildren = false;
1563 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1564 if (D->hasExplicitBound()) {
1565 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1566 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1567 // declaring the type alias, not something that was written in the
1572 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1573 if (D->getTypeSourceInfo())
1574 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1576 TRY_TO(TraverseType(D->getType()));
1577 ShouldVisitChildren = false;
1580 DEF_TRAVERSE_DECL(UsingDecl, {
1581 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1582 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1585 DEF_TRAVERSE_DECL(UsingPackDecl, {})
1587 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1588 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1591 DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1593 DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {})
1595 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1596 for (auto *I : D->varlists()) {
1597 TRY_TO(TraverseStmt(I));
1601 DEF_TRAVERSE_DECL(OMPRequiresDecl, {
1602 for (auto *C : D->clauselists()) {
1603 TRY_TO(TraverseOMPClause(C));
1607 DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, {
1608 TRY_TO(TraverseStmt(D->getCombiner()));
1609 if (auto *Initializer = D->getInitializer())
1610 TRY_TO(TraverseStmt(Initializer));
1611 TRY_TO(TraverseType(D->getType()));
1615 DEF_TRAVERSE_DECL(OMPDeclareMapperDecl, {
1616 for (auto *C : D->clauselists())
1617 TRY_TO(TraverseOMPClause(C));
1618 TRY_TO(TraverseType(D->getType()));
1622 DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
1624 DEF_TRAVERSE_DECL(OMPAllocateDecl, {
1625 for (auto *I : D->varlists())
1626 TRY_TO(TraverseStmt(I));
1627 for (auto *C : D->clauselists())
1628 TRY_TO(TraverseOMPClause(C));
1631 // A helper method for TemplateDecl's children.
1632 template <typename Derived>
1633 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1634 TemplateParameterList *TPL) {
1636 for (NamedDecl *D : *TPL) {
1637 TRY_TO(TraverseDecl(D));
1639 if (Expr *RequiresClause = TPL->getRequiresClause()) {
1640 TRY_TO(TraverseStmt(RequiresClause));
1646 template <typename Derived>
1647 template <typename T>
1648 bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) {
1649 for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) {
1650 TemplateParameterList *TPL = D->getTemplateParameterList(i);
1651 TraverseTemplateParameterListHelper(TPL);
1656 template <typename Derived>
1657 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1658 ClassTemplateDecl *D) {
1659 for (auto *SD : D->specializations()) {
1660 for (auto *RD : SD->redecls()) {
1661 // We don't want to visit injected-class-names in this traversal.
1662 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1666 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1667 // Visit the implicit instantiations with the requested pattern.
1668 case TSK_Undeclared:
1669 case TSK_ImplicitInstantiation:
1670 TRY_TO(TraverseDecl(RD));
1673 // We don't need to do anything on an explicit instantiation
1674 // or explicit specialization because there will be an explicit
1675 // node for it elsewhere.
1676 case TSK_ExplicitInstantiationDeclaration:
1677 case TSK_ExplicitInstantiationDefinition:
1678 case TSK_ExplicitSpecialization:
1687 template <typename Derived>
1688 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1689 VarTemplateDecl *D) {
1690 for (auto *SD : D->specializations()) {
1691 for (auto *RD : SD->redecls()) {
1693 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1694 case TSK_Undeclared:
1695 case TSK_ImplicitInstantiation:
1696 TRY_TO(TraverseDecl(RD));
1699 case TSK_ExplicitInstantiationDeclaration:
1700 case TSK_ExplicitInstantiationDefinition:
1701 case TSK_ExplicitSpecialization:
1710 // A helper method for traversing the instantiations of a
1711 // function while skipping its specializations.
1712 template <typename Derived>
1713 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1714 FunctionTemplateDecl *D) {
1715 for (auto *FD : D->specializations()) {
1716 for (auto *RD : FD->redecls()) {
1717 switch (RD->getTemplateSpecializationKind()) {
1718 case TSK_Undeclared:
1719 case TSK_ImplicitInstantiation:
1720 // We don't know what kind of FunctionDecl this is.
1721 TRY_TO(TraverseDecl(RD));
1724 // FIXME: For now traverse explicit instantiations here. Change that
1725 // once they are represented as dedicated nodes in the AST.
1726 case TSK_ExplicitInstantiationDeclaration:
1727 case TSK_ExplicitInstantiationDefinition:
1728 TRY_TO(TraverseDecl(RD));
1731 case TSK_ExplicitSpecialization:
1740 // This macro unifies the traversal of class, variable and function
1741 // template declarations.
1742 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1743 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1744 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1745 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1747 /* By default, we do not traverse the instantiations of \
1748 class templates since they do not appear in the user code. The \
1749 following code optionally traverses them. \
1751 We only traverse the class instantiations when we see the canonical \
1752 declaration of the template, to ensure we only visit them once. */ \
1753 if (getDerived().shouldVisitTemplateInstantiations() && \
1754 D == D->getCanonicalDecl()) \
1755 TRY_TO(TraverseTemplateInstantiations(D)); \
1757 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1758 from a template instantiation back to the template from which \
1759 it was instantiated, and thus should not be traversed. */ \
1762 DEF_TRAVERSE_TMPL_DECL(Class)
1763 DEF_TRAVERSE_TMPL_DECL(Var)
1764 DEF_TRAVERSE_TMPL_DECL(Function)
1766 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1767 // D is the "T" in something like
1768 // template <template <typename> class T> class container { };
1769 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1770 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1771 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1773 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1776 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1777 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1780 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1781 // D is the "T" in something like "template<typename T> class vector;"
1782 if (D->getTypeForDecl())
1783 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1784 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1785 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1788 DEF_TRAVERSE_DECL(TypedefDecl, {
1789 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1790 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1791 // declaring the typedef, not something that was written in the
1795 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1796 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1797 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1798 // declaring the type alias, not something that was written in the
1802 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1803 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1804 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1807 DEF_TRAVERSE_DECL(ConceptDecl, {
1808 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1809 TRY_TO(TraverseStmt(D->getConstraintExpr()));
1812 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1813 // A dependent using declaration which was marked with 'typename'.
1814 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1815 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1816 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1817 // declaring the type, not something that was written in the
1821 DEF_TRAVERSE_DECL(EnumDecl, {
1822 TRY_TO(TraverseDeclTemplateParameterLists(D));
1824 if (D->getTypeForDecl())
1825 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1827 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1828 // The enumerators are already traversed by
1829 // decls_begin()/decls_end().
1832 // Helper methods for RecordDecl and its children.
1833 template <typename Derived>
1834 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1835 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1836 // declaring the type, not something that was written in the source.
1838 TRY_TO(TraverseDeclTemplateParameterLists(D));
1839 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1843 template <typename Derived>
1844 bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier(
1845 const CXXBaseSpecifier &Base) {
1846 TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc()));
1850 template <typename Derived>
1851 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1852 if (!TraverseRecordHelper(D))
1854 if (D->isCompleteDefinition()) {
1855 for (const auto &I : D->bases()) {
1856 TRY_TO(TraverseCXXBaseSpecifier(I));
1858 // We don't traverse the friends or the conversions, as they are
1859 // already in decls_begin()/decls_end().
1864 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1866 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1868 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1869 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1870 /* For implicit instantiations ("set<int> x;"), we don't want to \
1871 recurse at all, since the instatiated template isn't written in \
1872 the source code anywhere. (Note the instatiated *type* -- \
1873 set<int> -- is written, and will still get a callback of \
1874 TemplateSpecializationType). For explicit instantiations \
1875 ("template set<int>;"), we do need a callback, since this \
1876 is the only callback that's made for this instantiation. \
1877 We use getTypeAsWritten() to distinguish. */ \
1878 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1879 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1881 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); \
1882 if (!getDerived().shouldVisitTemplateInstantiations() && \
1883 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1884 /* Returning from here skips traversing the \
1885 declaration context of the *TemplateSpecializationDecl \
1886 (embedded in the DEF_TRAVERSE_DECL() macro) \
1887 which contains the instantiated members of the template. */ \
1891 DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1892 DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1894 template <typename Derived>
1895 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1896 const TemplateArgumentLoc *TAL, unsigned Count) {
1897 for (unsigned I = 0; I < Count; ++I) {
1898 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1903 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1904 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1905 /* The partial specialization. */ \
1906 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1907 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1909 TRY_TO(TraverseDecl(*I)); \
1912 /* The args that remains unspecialized. */ \
1913 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1914 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1915 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1917 /* Don't need the *TemplatePartialSpecializationHelper, even \
1918 though that's our parent class -- we already visit all the \
1919 template args here. */ \
1920 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1922 /* Instantiations will have been visited with the primary template. */ \
1925 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1926 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1928 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1930 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1931 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1932 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1933 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1934 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1937 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1939 template <typename Derived>
1940 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1941 TRY_TO(TraverseDeclTemplateParameterLists(D));
1942 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1943 if (D->getTypeSourceInfo())
1944 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1946 TRY_TO(TraverseType(D->getType()));
1950 DEF_TRAVERSE_DECL(DecompositionDecl, {
1951 TRY_TO(TraverseVarHelper(D));
1952 for (auto *Binding : D->bindings()) {
1953 TRY_TO(TraverseDecl(Binding));
1957 DEF_TRAVERSE_DECL(BindingDecl, {
1958 if (getDerived().shouldVisitImplicitCode())
1959 TRY_TO(TraverseStmt(D->getBinding()));
1962 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1964 DEF_TRAVERSE_DECL(FieldDecl, {
1965 TRY_TO(TraverseDeclaratorHelper(D));
1966 if (D->isBitField())
1967 TRY_TO(TraverseStmt(D->getBitWidth()));
1968 else if (D->hasInClassInitializer())
1969 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1972 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1973 TRY_TO(TraverseDeclaratorHelper(D));
1974 if (D->isBitField())
1975 TRY_TO(TraverseStmt(D->getBitWidth()));
1976 // FIXME: implement the rest.
1979 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1980 TRY_TO(TraverseDeclaratorHelper(D));
1981 if (D->isBitField())
1982 TRY_TO(TraverseStmt(D->getBitWidth()));
1983 // FIXME: implement the rest.
1986 template <typename Derived>
1987 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1988 TRY_TO(TraverseDeclTemplateParameterLists(D));
1989 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1990 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1992 // If we're an explicit template specialization, iterate over the
1993 // template args that were explicitly specified. If we were doing
1994 // this in typing order, we'd do it between the return type and
1995 // the function args, but both are handled by the FunctionTypeLoc
1996 // above, so we have to choose one side. I've decided to do before.
1997 if (const FunctionTemplateSpecializationInfo *FTSI =
1998 D->getTemplateSpecializationInfo()) {
1999 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
2000 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
2001 // A specialization might not have explicit template arguments if it has
2002 // a templated return type and concrete arguments.
2003 if (const ASTTemplateArgumentListInfo *TALI =
2004 FTSI->TemplateArgumentsAsWritten) {
2005 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
2006 TALI->NumTemplateArgs));
2011 // Visit the function type itself, which can be either
2012 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
2013 // also covers the return type and the function parameters,
2014 // including exception specifications.
2015 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
2016 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2017 } else if (getDerived().shouldVisitImplicitCode()) {
2018 // Visit parameter variable declarations of the implicit function
2019 // if the traverser is visiting implicit code. Parameter variable
2020 // declarations do not have valid TypeSourceInfo, so to visit them
2021 // we need to traverse the declarations explicitly.
2022 for (ParmVarDecl *Parameter : D->parameters()) {
2023 TRY_TO(TraverseDecl(Parameter));
2027 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2028 // Constructor initializers.
2029 for (auto *I : Ctor->inits()) {
2030 if (I->isWritten() || getDerived().shouldVisitImplicitCode())
2031 TRY_TO(TraverseConstructorInitializer(I));
2035 bool VisitBody = D->isThisDeclarationADefinition();
2036 // If a method is set to default outside the class definition the compiler
2037 // generates the method body and adds it to the AST.
2038 if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
2039 VisitBody &= !MD->isDefaulted() || getDerived().shouldVisitImplicitCode();
2042 TRY_TO(TraverseStmt(D->getBody())); // Function body.
2047 DEF_TRAVERSE_DECL(FunctionDecl, {
2048 // We skip decls_begin/decls_end, which are already covered by
2049 // TraverseFunctionHelper().
2050 ShouldVisitChildren = false;
2051 ReturnValue = TraverseFunctionHelper(D);
2054 DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
2055 // We skip decls_begin/decls_end, which are already covered by
2056 // TraverseFunctionHelper().
2057 ShouldVisitChildren = false;
2058 ReturnValue = TraverseFunctionHelper(D);
2061 DEF_TRAVERSE_DECL(CXXMethodDecl, {
2062 // We skip decls_begin/decls_end, which are already covered by
2063 // TraverseFunctionHelper().
2064 ShouldVisitChildren = false;
2065 ReturnValue = TraverseFunctionHelper(D);
2068 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
2069 // We skip decls_begin/decls_end, which are already covered by
2070 // TraverseFunctionHelper().
2071 ShouldVisitChildren = false;
2072 ReturnValue = TraverseFunctionHelper(D);
2075 // CXXConversionDecl is the declaration of a type conversion operator.
2076 // It's not a cast expression.
2077 DEF_TRAVERSE_DECL(CXXConversionDecl, {
2078 // We skip decls_begin/decls_end, which are already covered by
2079 // TraverseFunctionHelper().
2080 ShouldVisitChildren = false;
2081 ReturnValue = TraverseFunctionHelper(D);
2084 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
2085 // We skip decls_begin/decls_end, which are already covered by
2086 // TraverseFunctionHelper().
2087 ShouldVisitChildren = false;
2088 ReturnValue = TraverseFunctionHelper(D);
2091 template <typename Derived>
2092 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
2093 TRY_TO(TraverseDeclaratorHelper(D));
2094 // Default params are taken care of when we traverse the ParmVarDecl.
2095 if (!isa<ParmVarDecl>(D) &&
2096 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
2097 TRY_TO(TraverseStmt(D->getInit()));
2101 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
2103 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
2105 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
2106 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
2107 TRY_TO(TraverseDeclaratorHelper(D));
2108 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
2109 TRY_TO(TraverseStmt(D->getDefaultArgument()));
2112 DEF_TRAVERSE_DECL(ParmVarDecl, {
2113 TRY_TO(TraverseVarHelper(D));
2115 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
2116 !D->hasUnparsedDefaultArg())
2117 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
2119 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
2120 !D->hasUnparsedDefaultArg())
2121 TRY_TO(TraverseStmt(D->getDefaultArg()));
2124 #undef DEF_TRAVERSE_DECL
2126 // ----------------- Stmt traversal -----------------
2128 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
2129 // over the children defined in children() (every stmt defines these,
2130 // though sometimes the range is empty). Each individual Traverse*
2131 // method only needs to worry about children other than those. To see
2132 // what children() does for a given class, see, e.g.,
2133 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
2135 // This macro makes available a variable S, the passed-in stmt.
2136 #define DEF_TRAVERSE_STMT(STMT, CODE) \
2137 template <typename Derived> \
2138 bool RecursiveASTVisitor<Derived>::Traverse##STMT( \
2139 STMT *S, DataRecursionQueue *Queue) { \
2140 bool ShouldVisitChildren = true; \
2141 bool ReturnValue = true; \
2142 if (!getDerived().shouldTraversePostOrder()) \
2143 TRY_TO(WalkUpFrom##STMT(S)); \
2145 if (ShouldVisitChildren) { \
2146 for (Stmt * SubStmt : getDerived().getStmtChildren(S)) { \
2147 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \
2150 if (!Queue && ReturnValue && getDerived().shouldTraversePostOrder()) \
2151 TRY_TO(WalkUpFrom##STMT(S)); \
2152 return ReturnValue; \
2155 DEF_TRAVERSE_STMT(GCCAsmStmt, {
2156 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
2157 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
2158 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
2160 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
2161 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
2163 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
2164 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
2166 // children() iterates over inputExpr and outputExpr.
2171 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
2172 // added this needs to be implemented.
2175 DEF_TRAVERSE_STMT(CXXCatchStmt, {
2176 TRY_TO(TraverseDecl(S->getExceptionDecl()));
2177 // children() iterates over the handler block.
2180 DEF_TRAVERSE_STMT(DeclStmt, {
2181 for (auto *I : S->decls()) {
2182 TRY_TO(TraverseDecl(I));
2184 // Suppress the default iteration over children() by
2185 // returning. Here's why: A DeclStmt looks like 'type var [=
2186 // initializer]'. The decls above already traverse over the
2187 // initializers, so we don't have to do it again (which
2188 // children() would do).
2189 ShouldVisitChildren = false;
2192 // These non-expr stmts (most of them), do not need any action except
2193 // iterating over the children.
2194 DEF_TRAVERSE_STMT(BreakStmt, {})
2195 DEF_TRAVERSE_STMT(CXXTryStmt, {})
2196 DEF_TRAVERSE_STMT(CaseStmt, {})
2197 DEF_TRAVERSE_STMT(CompoundStmt, {})
2198 DEF_TRAVERSE_STMT(ContinueStmt, {})
2199 DEF_TRAVERSE_STMT(DefaultStmt, {})
2200 DEF_TRAVERSE_STMT(DoStmt, {})
2201 DEF_TRAVERSE_STMT(ForStmt, {})
2202 DEF_TRAVERSE_STMT(GotoStmt, {})
2203 DEF_TRAVERSE_STMT(IfStmt, {})
2204 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
2205 DEF_TRAVERSE_STMT(LabelStmt, {})
2206 DEF_TRAVERSE_STMT(AttributedStmt, {})
2207 DEF_TRAVERSE_STMT(NullStmt, {})
2208 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
2209 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
2210 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
2211 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
2212 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
2213 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
2214 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
2216 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
2217 if (!getDerived().shouldVisitImplicitCode()) {
2219 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInit());
2220 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
2221 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
2222 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2223 // Visit everything else only if shouldVisitImplicitCode().
2224 ShouldVisitChildren = false;
2228 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
2229 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2230 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2233 DEF_TRAVERSE_STMT(ReturnStmt, {})
2234 DEF_TRAVERSE_STMT(SwitchStmt, {})
2235 DEF_TRAVERSE_STMT(WhileStmt, {})
2237 DEF_TRAVERSE_STMT(ConstantExpr, {})
2239 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
2240 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2241 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2242 if (S->hasExplicitTemplateArgs()) {
2243 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2244 S->getNumTemplateArgs()));
2248 DEF_TRAVERSE_STMT(DeclRefExpr, {
2249 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2250 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2251 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2252 S->getNumTemplateArgs()));
2255 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
2256 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2257 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2258 if (S->hasExplicitTemplateArgs()) {
2259 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2260 S->getNumTemplateArgs()));
2264 DEF_TRAVERSE_STMT(MemberExpr, {
2265 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2266 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2267 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2268 S->getNumTemplateArgs()));
2273 {// We don't traverse the cast type, as it's not written in the
2277 DEF_TRAVERSE_STMT(CStyleCastExpr, {
2278 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2281 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2282 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2285 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2286 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2289 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2290 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2293 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2294 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2297 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2298 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2301 DEF_TRAVERSE_STMT(BuiltinBitCastExpr, {
2302 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2305 template <typename Derived>
2306 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2307 InitListExpr *S, DataRecursionQueue *Queue) {
2309 // Skip this if we traverse postorder. We will visit it later
2310 // in PostVisitStmt.
2311 if (!getDerived().shouldTraversePostOrder())
2312 TRY_TO(WalkUpFromInitListExpr(S));
2314 // All we need are the default actions. FIXME: use a helper function.
2315 for (Stmt *SubStmt : S->children()) {
2316 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2322 // If shouldVisitImplicitCode() returns false, this method traverses only the
2323 // syntactic form of InitListExpr.
2324 // If shouldVisitImplicitCode() return true, this method is called once for
2325 // each pair of syntactic and semantic InitListExpr, and it traverses the
2326 // subtrees defined by the two forms. This may cause some of the children to be
2327 // visited twice, if they appear both in the syntactic and the semantic form.
2329 // There is no guarantee about which form \p S takes when this method is called.
2330 template <typename Derived>
2331 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(
2332 InitListExpr *S, DataRecursionQueue *Queue) {
2333 if (S->isSemanticForm() && S->isSyntacticForm()) {
2334 // `S` does not have alternative forms, traverse only once.
2335 TRY_TO(TraverseSynOrSemInitListExpr(S, Queue));
2338 TRY_TO(TraverseSynOrSemInitListExpr(
2339 S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2340 if (getDerived().shouldVisitImplicitCode()) {
2341 // Only visit the semantic form if the clients are interested in implicit
2342 // compiler-generated.
2343 TRY_TO(TraverseSynOrSemInitListExpr(
2344 S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2349 // GenericSelectionExpr is a special case because the types and expressions
2350 // are interleaved. We also need to watch out for null types (default
2351 // generic associations).
2352 DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2353 TRY_TO(TraverseStmt(S->getControllingExpr()));
2354 for (const GenericSelectionExpr::Association &Assoc : S->associations()) {
2355 if (TypeSourceInfo *TSI = Assoc.getTypeSourceInfo())
2356 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2357 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(Assoc.getAssociationExpr());
2359 ShouldVisitChildren = false;
2362 // PseudoObjectExpr is a special case because of the weirdness with
2363 // syntactic expressions and opaque values.
2364 DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2365 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
2366 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2367 e = S->semantics_end();
2370 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2371 sub = OVE->getSourceExpr();
2372 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
2374 ShouldVisitChildren = false;
2377 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2378 // This is called for code like 'return T()' where T is a built-in
2379 // (i.e. non-class) type.
2380 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2383 DEF_TRAVERSE_STMT(CXXNewExpr, {
2384 // The child-iterator will pick up the other arguments.
2385 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2388 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2389 // The child-iterator will pick up the expression representing
2391 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2392 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2393 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2396 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2397 // The child-iterator will pick up the arg if it's an expression,
2398 // but not if it's a type.
2399 if (S->isArgumentType())
2400 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2403 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2404 // The child-iterator will pick up the arg if it's an expression,
2405 // but not if it's a type.
2406 if (S->isTypeOperand())
2407 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2410 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2411 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2414 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
2416 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2417 // The child-iterator will pick up the arg if it's an expression,
2418 // but not if it's a type.
2419 if (S->isTypeOperand())
2420 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2423 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2424 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2425 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2428 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2429 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2432 DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2433 { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2435 DEF_TRAVERSE_STMT(VAArgExpr, {
2436 // The child-iterator will pick up the expression argument.
2437 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2440 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2441 // This is called for code like 'return T()' where T is a class type.
2442 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2445 // Walk only the visible parts of lambda expressions.
2446 DEF_TRAVERSE_STMT(LambdaExpr, {
2447 // Visit the capture list.
2448 for (unsigned I = 0, N = S->capture_size(); I != N; ++I) {
2449 const LambdaCapture *C = S->capture_begin() + I;
2450 if (C->isExplicit() || getDerived().shouldVisitImplicitCode()) {
2451 TRY_TO(TraverseLambdaCapture(S, C, S->capture_init_begin()[I]));
2455 if (getDerived().shouldVisitImplicitCode()) {
2456 // The implicit model is simple: everything else is in the lambda class.
2457 TRY_TO(TraverseDecl(S->getLambdaClass()));
2459 // We need to poke around to find the bits that might be explicitly written.
2460 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2461 FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>();
2463 for (Decl *D : S->getExplicitTemplateParameters()) {
2464 // Visit explicit template parameters.
2465 TRY_TO(TraverseDecl(D));
2467 if (S->hasExplicitParameters()) {
2468 // Visit parameters.
2469 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
2470 TRY_TO(TraverseDecl(Proto.getParam(I)));
2472 if (S->hasExplicitResultType())
2473 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2475 auto *T = Proto.getTypePtr();
2476 for (const auto &E : T->exceptions())
2477 TRY_TO(TraverseType(E));
2479 if (Expr *NE = T->getNoexceptExpr())
2480 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2482 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2484 ShouldVisitChildren = false;
2487 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2488 // This is called for code like 'T()', where T is a template argument.
2489 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2492 // These expressions all might take explicit template arguments.
2493 // We traverse those if so. FIXME: implement these.
2494 DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2495 DEF_TRAVERSE_STMT(CallExpr, {})
2496 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2498 // These exprs (most of them), do not need any action except iterating
2499 // over the children.
2500 DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2501 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2502 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2504 DEF_TRAVERSE_STMT(BlockExpr, {
2505 TRY_TO(TraverseDecl(S->getBlockDecl()));
2506 return true; // no child statements to loop through.
2509 DEF_TRAVERSE_STMT(ChooseExpr, {})
2510 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2511 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2513 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2514 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2516 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {
2517 if (getDerived().shouldVisitImplicitCode())
2518 TRY_TO(TraverseStmt(S->getExpr()));
2521 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2522 DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2523 DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2524 DEF_TRAVERSE_STMT(CXXInheritedCtorInitExpr, {})
2525 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2526 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2528 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2529 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2530 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2531 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2532 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2533 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2536 DEF_TRAVERSE_STMT(CXXThisExpr, {})
2537 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2538 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2539 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2540 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2541 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2542 DEF_TRAVERSE_STMT(GNUNullExpr, {})
2543 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2544 DEF_TRAVERSE_STMT(NoInitExpr, {})
2545 DEF_TRAVERSE_STMT(ArrayInitLoopExpr, {
2546 // FIXME: The source expression of the OVE should be listed as
2547 // a child of the ArrayInitLoopExpr.
2548 if (OpaqueValueExpr *OVE = S->getCommonExpr())
2549 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr());
2551 DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {})
2552 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2554 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2555 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2556 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2559 DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2560 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2562 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2563 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2564 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2567 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2568 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2569 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2570 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2571 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2573 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2574 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2577 DEF_TRAVERSE_STMT(ObjCAvailabilityCheckExpr, {})
2578 DEF_TRAVERSE_STMT(ParenExpr, {})
2579 DEF_TRAVERSE_STMT(ParenListExpr, {})
2580 DEF_TRAVERSE_STMT(PredefinedExpr, {})
2581 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2582 DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2583 DEF_TRAVERSE_STMT(StmtExpr, {})
2584 DEF_TRAVERSE_STMT(SourceLocExpr, {})
2586 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2587 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2588 if (S->hasExplicitTemplateArgs()) {
2589 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2590 S->getNumTemplateArgs()));
2594 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2595 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2596 if (S->hasExplicitTemplateArgs()) {
2597 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2598 S->getNumTemplateArgs()));
2602 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2603 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2604 DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2605 DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2606 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2608 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2609 DEF_TRAVERSE_STMT(CXXRewrittenBinaryOperator, {
2610 if (!getDerived().shouldVisitImplicitCode()) {
2611 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
2612 S->getDecomposedForm();
2613 TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.LHS)));
2614 TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.RHS)));
2615 ShouldVisitChildren = false;
2618 DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2619 DEF_TRAVERSE_STMT(TypoExpr, {})
2620 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2622 // These operators (all of them) do not need any action except
2623 // iterating over the children.
2624 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2625 DEF_TRAVERSE_STMT(ConditionalOperator, {})
2626 DEF_TRAVERSE_STMT(UnaryOperator, {})
2627 DEF_TRAVERSE_STMT(BinaryOperator, {})
2628 DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2629 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2630 DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2631 DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2632 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2633 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2634 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2635 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2636 DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2637 DEF_TRAVERSE_STMT(AtomicExpr, {})
2639 // For coroutines expressions, traverse either the operand
2640 // as written or the implied calls, depending on what the
2641 // derived class requests.
2642 DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2643 if (!getDerived().shouldVisitImplicitCode()) {
2644 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2645 ShouldVisitChildren = false;
2648 DEF_TRAVERSE_STMT(CoreturnStmt, {
2649 if (!getDerived().shouldVisitImplicitCode()) {
2650 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2651 ShouldVisitChildren = false;
2654 DEF_TRAVERSE_STMT(CoawaitExpr, {
2655 if (!getDerived().shouldVisitImplicitCode()) {
2656 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2657 ShouldVisitChildren = false;
2660 DEF_TRAVERSE_STMT(DependentCoawaitExpr, {
2661 if (!getDerived().shouldVisitImplicitCode()) {
2662 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2663 ShouldVisitChildren = false;
2666 DEF_TRAVERSE_STMT(CoyieldExpr, {
2667 if (!getDerived().shouldVisitImplicitCode()) {
2668 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2669 ShouldVisitChildren = false;
2673 DEF_TRAVERSE_STMT(ConceptSpecializationExpr, {
2674 TRY_TO(TraverseTemplateArgumentLocsHelper(
2675 S->getTemplateArgsAsWritten()->getTemplateArgs(),
2676 S->getTemplateArgsAsWritten()->NumTemplateArgs));
2679 // These literals (all of them) do not need any action.
2680 DEF_TRAVERSE_STMT(IntegerLiteral, {})
2681 DEF_TRAVERSE_STMT(FixedPointLiteral, {})
2682 DEF_TRAVERSE_STMT(CharacterLiteral, {})
2683 DEF_TRAVERSE_STMT(FloatingLiteral, {})
2684 DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2685 DEF_TRAVERSE_STMT(StringLiteral, {})
2686 DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2687 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2688 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2689 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2691 // Traverse OpenCL: AsType, Convert.
2692 DEF_TRAVERSE_STMT(AsTypeExpr, {})
2694 // OpenMP directives.
2695 template <typename Derived>
2696 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2697 OMPExecutableDirective *S) {
2698 for (auto *C : S->clauses()) {
2699 TRY_TO(TraverseOMPClause(C));
2704 template <typename Derived>
2706 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2707 return TraverseOMPExecutableDirective(S);
2710 DEF_TRAVERSE_STMT(OMPParallelDirective,
2711 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2713 DEF_TRAVERSE_STMT(OMPSimdDirective,
2714 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2716 DEF_TRAVERSE_STMT(OMPForDirective,
2717 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2719 DEF_TRAVERSE_STMT(OMPForSimdDirective,
2720 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2722 DEF_TRAVERSE_STMT(OMPSectionsDirective,
2723 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2725 DEF_TRAVERSE_STMT(OMPSectionDirective,
2726 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2728 DEF_TRAVERSE_STMT(OMPSingleDirective,
2729 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2731 DEF_TRAVERSE_STMT(OMPMasterDirective,
2732 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2734 DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2735 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2736 TRY_TO(TraverseOMPExecutableDirective(S));
2739 DEF_TRAVERSE_STMT(OMPParallelForDirective,
2740 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2742 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2743 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2745 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2746 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2748 DEF_TRAVERSE_STMT(OMPTaskDirective,
2749 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2751 DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2752 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2754 DEF_TRAVERSE_STMT(OMPBarrierDirective,
2755 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2757 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2758 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2760 DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2761 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2763 DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2764 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2766 DEF_TRAVERSE_STMT(OMPCancelDirective,
2767 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2769 DEF_TRAVERSE_STMT(OMPFlushDirective,
2770 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2772 DEF_TRAVERSE_STMT(OMPOrderedDirective,
2773 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2775 DEF_TRAVERSE_STMT(OMPAtomicDirective,
2776 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2778 DEF_TRAVERSE_STMT(OMPTargetDirective,
2779 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2781 DEF_TRAVERSE_STMT(OMPTargetDataDirective,
2782 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2784 DEF_TRAVERSE_STMT(OMPTargetEnterDataDirective,
2785 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2787 DEF_TRAVERSE_STMT(OMPTargetExitDataDirective,
2788 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2790 DEF_TRAVERSE_STMT(OMPTargetParallelDirective,
2791 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2793 DEF_TRAVERSE_STMT(OMPTargetParallelForDirective,
2794 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2796 DEF_TRAVERSE_STMT(OMPTeamsDirective,
2797 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2799 DEF_TRAVERSE_STMT(OMPTargetUpdateDirective,
2800 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2802 DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
2803 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2805 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
2806 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2808 DEF_TRAVERSE_STMT(OMPMasterTaskLoopDirective,
2809 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2811 DEF_TRAVERSE_STMT(OMPMasterTaskLoopSimdDirective,
2812 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2814 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopDirective,
2815 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2817 DEF_TRAVERSE_STMT(OMPDistributeDirective,
2818 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2820 DEF_TRAVERSE_STMT(OMPDistributeParallelForDirective,
2821 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2823 DEF_TRAVERSE_STMT(OMPDistributeParallelForSimdDirective,
2824 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2826 DEF_TRAVERSE_STMT(OMPDistributeSimdDirective,
2827 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2829 DEF_TRAVERSE_STMT(OMPTargetParallelForSimdDirective,
2830 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2832 DEF_TRAVERSE_STMT(OMPTargetSimdDirective,
2833 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2835 DEF_TRAVERSE_STMT(OMPTeamsDistributeDirective,
2836 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2838 DEF_TRAVERSE_STMT(OMPTeamsDistributeSimdDirective,
2839 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2841 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForSimdDirective,
2842 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2844 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForDirective,
2845 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2847 DEF_TRAVERSE_STMT(OMPTargetTeamsDirective,
2848 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2850 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeDirective,
2851 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2853 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForDirective,
2854 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2856 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForSimdDirective,
2857 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2859 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeSimdDirective,
2860 { TRY_TO(TraverseOMPExecutableDirective(S)); })
2863 template <typename Derived>
2864 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2867 switch (C->getClauseKind()) {
2868 #define OPENMP_CLAUSE(Name, Class) \
2870 TRY_TO(Visit##Class(static_cast<Class *>(C))); \
2872 #include "clang/Basic/OpenMPKinds.def"
2873 case OMPC_threadprivate:
2875 case OMPC_device_type:
2883 template <typename Derived>
2884 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPreInit(
2885 OMPClauseWithPreInit *Node) {
2886 TRY_TO(TraverseStmt(Node->getPreInitStmt()));
2890 template <typename Derived>
2891 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPostUpdate(
2892 OMPClauseWithPostUpdate *Node) {
2893 TRY_TO(VisitOMPClauseWithPreInit(Node));
2894 TRY_TO(TraverseStmt(Node->getPostUpdateExpr()));
2898 template <typename Derived>
2899 bool RecursiveASTVisitor<Derived>::VisitOMPAllocatorClause(
2900 OMPAllocatorClause *C) {
2901 TRY_TO(TraverseStmt(C->getAllocator()));
2905 template <typename Derived>
2906 bool RecursiveASTVisitor<Derived>::VisitOMPAllocateClause(OMPAllocateClause *C) {
2907 TRY_TO(TraverseStmt(C->getAllocator()));
2908 TRY_TO(VisitOMPClauseList(C));
2912 template <typename Derived>
2913 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2914 TRY_TO(VisitOMPClauseWithPreInit(C));
2915 TRY_TO(TraverseStmt(C->getCondition()));
2919 template <typename Derived>
2920 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2921 TRY_TO(VisitOMPClauseWithPreInit(C));
2922 TRY_TO(TraverseStmt(C->getCondition()));
2926 template <typename Derived>
2928 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2929 TRY_TO(VisitOMPClauseWithPreInit(C));
2930 TRY_TO(TraverseStmt(C->getNumThreads()));
2934 template <typename Derived>
2935 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2936 TRY_TO(TraverseStmt(C->getSafelen()));
2940 template <typename Derived>
2941 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
2942 TRY_TO(TraverseStmt(C->getSimdlen()));
2946 template <typename Derived>
2948 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2949 TRY_TO(TraverseStmt(C->getNumForLoops()));
2953 template <typename Derived>
2954 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2958 template <typename Derived>
2959 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2963 template <typename Derived>
2964 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedAddressClause(
2965 OMPUnifiedAddressClause *) {
2969 template <typename Derived>
2970 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedSharedMemoryClause(
2971 OMPUnifiedSharedMemoryClause *) {
2975 template <typename Derived>
2976 bool RecursiveASTVisitor<Derived>::VisitOMPReverseOffloadClause(
2977 OMPReverseOffloadClause *) {
2981 template <typename Derived>
2982 bool RecursiveASTVisitor<Derived>::VisitOMPDynamicAllocatorsClause(
2983 OMPDynamicAllocatorsClause *) {
2987 template <typename Derived>
2988 bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause(
2989 OMPAtomicDefaultMemOrderClause *) {
2993 template <typename Derived>
2995 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2996 TRY_TO(VisitOMPClauseWithPreInit(C));
2997 TRY_TO(TraverseStmt(C->getChunkSize()));
3001 template <typename Derived>
3002 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
3003 TRY_TO(TraverseStmt(C->getNumForLoops()));
3007 template <typename Derived>
3008 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
3012 template <typename Derived>
3013 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
3017 template <typename Derived>
3019 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
3023 template <typename Derived>
3024 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
3028 template <typename Derived>
3029 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
3033 template <typename Derived>
3034 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
3038 template <typename Derived>
3039 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
3043 template <typename Derived>
3044 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
3048 template <typename Derived>
3049 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
3053 template <typename Derived>
3054 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
3058 template <typename Derived>
3059 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
3063 template <typename Derived>
3064 template <typename T>
3065 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
3066 for (auto *E : Node->varlists()) {
3067 TRY_TO(TraverseStmt(E));
3072 template <typename Derived>
3073 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
3074 TRY_TO(VisitOMPClauseList(C));
3075 for (auto *E : C->private_copies()) {
3076 TRY_TO(TraverseStmt(E));
3081 template <typename Derived>
3082 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
3083 OMPFirstprivateClause *C) {
3084 TRY_TO(VisitOMPClauseList(C));
3085 TRY_TO(VisitOMPClauseWithPreInit(C));
3086 for (auto *E : C->private_copies()) {
3087 TRY_TO(TraverseStmt(E));
3089 for (auto *E : C->inits()) {
3090 TRY_TO(TraverseStmt(E));
3095 template <typename Derived>
3096 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
3097 OMPLastprivateClause *C) {
3098 TRY_TO(VisitOMPClauseList(C));
3099 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3100 for (auto *E : C->private_copies()) {
3101 TRY_TO(TraverseStmt(E));
3103 for (auto *E : C->source_exprs()) {
3104 TRY_TO(TraverseStmt(E));
3106 for (auto *E : C->destination_exprs()) {
3107 TRY_TO(TraverseStmt(E));
3109 for (auto *E : C->assignment_ops()) {
3110 TRY_TO(TraverseStmt(E));
3115 template <typename Derived>
3116 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
3117 TRY_TO(VisitOMPClauseList(C));
3121 template <typename Derived>
3122 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
3123 TRY_TO(TraverseStmt(C->getStep()));
3124 TRY_TO(TraverseStmt(C->getCalcStep()));
3125 TRY_TO(VisitOMPClauseList(C));
3126 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3127 for (auto *E : C->privates()) {
3128 TRY_TO(TraverseStmt(E));
3130 for (auto *E : C->inits()) {
3131 TRY_TO(TraverseStmt(E));
3133 for (auto *E : C->updates()) {
3134 TRY_TO(TraverseStmt(E));
3136 for (auto *E : C->finals()) {
3137 TRY_TO(TraverseStmt(E));
3142 template <typename Derived>
3143 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
3144 TRY_TO(TraverseStmt(C->getAlignment()));
3145 TRY_TO(VisitOMPClauseList(C));
3149 template <typename Derived>
3150 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
3151 TRY_TO(VisitOMPClauseList(C));
3152 for (auto *E : C->source_exprs()) {
3153 TRY_TO(TraverseStmt(E));
3155 for (auto *E : C->destination_exprs()) {
3156 TRY_TO(TraverseStmt(E));
3158 for (auto *E : C->assignment_ops()) {
3159 TRY_TO(TraverseStmt(E));
3164 template <typename Derived>
3165 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
3166 OMPCopyprivateClause *C) {
3167 TRY_TO(VisitOMPClauseList(C));
3168 for (auto *E : C->source_exprs()) {
3169 TRY_TO(TraverseStmt(E));
3171 for (auto *E : C->destination_exprs()) {
3172 TRY_TO(TraverseStmt(E));
3174 for (auto *E : C->assignment_ops()) {
3175 TRY_TO(TraverseStmt(E));
3180 template <typename Derived>
3182 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
3183 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3184 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3185 TRY_TO(VisitOMPClauseList(C));
3186 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3187 for (auto *E : C->privates()) {
3188 TRY_TO(TraverseStmt(E));
3190 for (auto *E : C->lhs_exprs()) {
3191 TRY_TO(TraverseStmt(E));
3193 for (auto *E : C->rhs_exprs()) {
3194 TRY_TO(TraverseStmt(E));
3196 for (auto *E : C->reduction_ops()) {
3197 TRY_TO(TraverseStmt(E));
3202 template <typename Derived>
3203 bool RecursiveASTVisitor<Derived>::VisitOMPTaskReductionClause(
3204 OMPTaskReductionClause *C) {
3205 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3206 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3207 TRY_TO(VisitOMPClauseList(C));
3208 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3209 for (auto *E : C->privates()) {
3210 TRY_TO(TraverseStmt(E));
3212 for (auto *E : C->lhs_exprs()) {
3213 TRY_TO(TraverseStmt(E));
3215 for (auto *E : C->rhs_exprs()) {
3216 TRY_TO(TraverseStmt(E));
3218 for (auto *E : C->reduction_ops()) {
3219 TRY_TO(TraverseStmt(E));
3224 template <typename Derived>
3225 bool RecursiveASTVisitor<Derived>::VisitOMPInReductionClause(
3226 OMPInReductionClause *C) {
3227 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3228 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3229 TRY_TO(VisitOMPClauseList(C));
3230 TRY_TO(VisitOMPClauseWithPostUpdate(C));
3231 for (auto *E : C->privates()) {
3232 TRY_TO(TraverseStmt(E));
3234 for (auto *E : C->lhs_exprs()) {
3235 TRY_TO(TraverseStmt(E));
3237 for (auto *E : C->rhs_exprs()) {
3238 TRY_TO(TraverseStmt(E));
3240 for (auto *E : C->reduction_ops()) {
3241 TRY_TO(TraverseStmt(E));
3243 for (auto *E : C->taskgroup_descriptors())
3244 TRY_TO(TraverseStmt(E));
3248 template <typename Derived>
3249 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
3250 TRY_TO(VisitOMPClauseList(C));
3254 template <typename Derived>
3255 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
3256 TRY_TO(VisitOMPClauseList(C));
3260 template <typename Derived>
3261 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
3262 TRY_TO(VisitOMPClauseWithPreInit(C));
3263 TRY_TO(TraverseStmt(C->getDevice()));
3267 template <typename Derived>
3268 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
3269 TRY_TO(VisitOMPClauseList(C));
3273 template <typename Derived>
3274 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
3275 OMPNumTeamsClause *C) {
3276 TRY_TO(VisitOMPClauseWithPreInit(C));
3277 TRY_TO(TraverseStmt(C->getNumTeams()));
3281 template <typename Derived>
3282 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
3283 OMPThreadLimitClause *C) {
3284 TRY_TO(VisitOMPClauseWithPreInit(C));
3285 TRY_TO(TraverseStmt(C->getThreadLimit()));
3289 template <typename Derived>
3290 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
3291 OMPPriorityClause *C) {
3292 TRY_TO(VisitOMPClauseWithPreInit(C));
3293 TRY_TO(TraverseStmt(C->getPriority()));
3297 template <typename Derived>
3298 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
3299 OMPGrainsizeClause *C) {
3300 TRY_TO(VisitOMPClauseWithPreInit(C));
3301 TRY_TO(TraverseStmt(C->getGrainsize()));
3305 template <typename Derived>
3306 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
3307 OMPNumTasksClause *C) {
3308 TRY_TO(VisitOMPClauseWithPreInit(C));
3309 TRY_TO(TraverseStmt(C->getNumTasks()));
3313 template <typename Derived>
3314 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
3315 TRY_TO(TraverseStmt(C->getHint()));
3319 template <typename Derived>
3320 bool RecursiveASTVisitor<Derived>::VisitOMPDistScheduleClause(
3321 OMPDistScheduleClause *C) {
3322 TRY_TO(VisitOMPClauseWithPreInit(C));
3323 TRY_TO(TraverseStmt(C->getChunkSize()));
3327 template <typename Derived>
3329 RecursiveASTVisitor<Derived>::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
3333 template <typename Derived>
3334 bool RecursiveASTVisitor<Derived>::VisitOMPToClause(OMPToClause *C) {
3335 TRY_TO(VisitOMPClauseList(C));
3339 template <typename Derived>
3340 bool RecursiveASTVisitor<Derived>::VisitOMPFromClause(OMPFromClause *C) {
3341 TRY_TO(VisitOMPClauseList(C));
3345 template <typename Derived>
3346 bool RecursiveASTVisitor<Derived>::VisitOMPUseDevicePtrClause(
3347 OMPUseDevicePtrClause *C) {
3348 TRY_TO(VisitOMPClauseList(C));
3352 template <typename Derived>
3353 bool RecursiveASTVisitor<Derived>::VisitOMPIsDevicePtrClause(
3354 OMPIsDevicePtrClause *C) {
3355 TRY_TO(VisitOMPClauseList(C));
3359 // FIXME: look at the following tricky-seeming exprs to see if we
3360 // need to recurse on anything. These are ones that have methods
3361 // returning decls or qualtypes or nestednamespecifier -- though I'm
3362 // not sure if they own them -- or just seemed very complicated, or
3363 // had lots of sub-types to explore.
3365 // VisitOverloadExpr and its children: recurse on template args? etc?
3367 // FIXME: go through all the stmts and exprs again, and see which of them
3368 // create new types, and recurse on the types (TypeLocs?) of those.
3371 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
3372 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
3373 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
3374 // Every class that has getQualifier.
3376 #undef DEF_TRAVERSE_STMT
3377 #undef TRAVERSE_STMT
3378 #undef TRAVERSE_STMT_BASE
3382 } // end namespace clang
3384 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H