1//===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the RecursiveASTVisitor interface, which recursively
10// traverses the entire AST.
11//
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
14#define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15
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/ExprConcepts.h"
27#include "clang/AST/ExprCXX.h"
28#include "clang/AST/ExprObjC.h"
29#include "clang/AST/ExprOpenMP.h"
30#include "clang/AST/LambdaCapture.h"
31#include "clang/AST/NestedNameSpecifier.h"
32#include "clang/AST/OpenMPClause.h"
33#include "clang/AST/Stmt.h"
34#include "clang/AST/StmtCXX.h"
35#include "clang/AST/StmtObjC.h"
36#include "clang/AST/StmtOpenMP.h"
37#include "clang/AST/TemplateBase.h"
38#include "clang/AST/TemplateName.h"
39#include "clang/AST/Type.h"
40#include "clang/AST/TypeLoc.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/OpenMPKinds.h"
43#include "clang/Basic/Specifiers.h"
44#include "llvm/ADT/PointerIntPair.h"
45#include "llvm/ADT/SmallVector.h"
46#include "llvm/Support/Casting.h"
47#include <algorithm>
48#include <cstddef>
49#include <type_traits>
50
51namespace clang {
52
53// A helper macro to implement short-circuiting when recursing. It
54// invokes CALL_EXPR, which must be a method call, on the derived
55// object (s.t. a user of RecursiveASTVisitor can override the method
56// in CALL_EXPR).
57#define TRY_TO(CALL_EXPR) \
58 do { \
59 if (!getDerived().CALL_EXPR) \
60 return false; \
61 } while (false)
62
63namespace detail {
64
65template <typename T, typename U>
66struct has_same_member_pointer_type : std::false_type {};
67template <typename T, typename U, typename R, typename... P>
68struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)>
69 : std::true_type {};
70
71template <bool has_same_type> struct is_same_method_impl {
72 template <typename FirstMethodPtrTy, typename SecondMethodPtrTy>
73 static bool isSameMethod(FirstMethodPtrTy FirstMethodPtr,
74 SecondMethodPtrTy SecondMethodPtr) {
75 return false;
76 }
77};
78
79template <> struct is_same_method_impl<true> {
80 template <typename FirstMethodPtrTy, typename SecondMethodPtrTy>
81 static bool isSameMethod(FirstMethodPtrTy FirstMethodPtr,
82 SecondMethodPtrTy SecondMethodPtr) {
83 return FirstMethodPtr == SecondMethodPtr;
84 }
85};
86
87/// Returns true if and only if \p FirstMethodPtr and \p SecondMethodPtr
88/// are pointers to the same non-static member function.
89template <typename FirstMethodPtrTy, typename SecondMethodPtrTy>
90bool isSameMethod(FirstMethodPtrTy FirstMethodPtr,
91 SecondMethodPtrTy SecondMethodPtr) {
92 return is_same_method_impl<has_same_member_pointer_type<
93 FirstMethodPtrTy,
94 SecondMethodPtrTy>::value>::isSameMethod(FirstMethodPtr, SecondMethodPtr);
95}
96
97} // end namespace detail
98
99/// A class that does preorder or postorder
100/// depth-first traversal on the entire Clang AST and visits each node.
101///
102/// This class performs three distinct tasks:
103/// 1. traverse the AST (i.e. go to each node);
104/// 2. at a given node, walk up the class hierarchy, starting from
105/// the node's dynamic type, until the top-most class (e.g. Stmt,
106/// Decl, or Type) is reached.
107/// 3. given a (node, class) combination, where 'class' is some base
108/// class of the dynamic type of 'node', call a user-overridable
109/// function to actually visit the node.
110///
111/// These tasks are done by three groups of methods, respectively:
112/// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
113/// for traversing an AST rooted at x. This method simply
114/// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
115/// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
116/// then recursively visits the child nodes of x.
117/// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
118/// similarly.
119/// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
120/// any child node of x. Instead, it first calls WalkUpFromBar(x)
121/// where Bar is the direct parent class of Foo (unless Foo has
122/// no parent), and then calls VisitFoo(x) (see the next list item).
123/// 3. VisitFoo(Foo *x) does task #3.
124///
125/// These three method groups are tiered (Traverse* > WalkUpFrom* >
126/// Visit*). A method (e.g. Traverse*) may call methods from the same
127/// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
128/// It may not call methods from a higher tier.
129///
130/// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
131/// is Foo's super class) before calling VisitFoo(), the result is
132/// that the Visit*() methods for a given node are called in the
133/// top-down order (e.g. for a node of type NamespaceDecl, the order will
134/// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
135///
136/// This scheme guarantees that all Visit*() calls for the same AST
137/// node are grouped together. In other words, Visit*() methods for
138/// different nodes are never interleaved.
139///
140/// Clients of this visitor should subclass the visitor (providing
141/// themselves as the template argument, using the curiously recurring
142/// template pattern) and override any of the Traverse*, WalkUpFrom*,
143/// and Visit* methods for declarations, types, statements,
144/// expressions, or other AST nodes where the visitor should customize
145/// behavior. Most users only need to override Visit*. Advanced
146/// users may override Traverse* and WalkUpFrom* to implement custom
147/// traversal strategies. Returning false from one of these overridden
148/// functions will abort the entire traversal.
149///
150/// By default, this visitor tries to visit every part of the explicit
151/// source code exactly once. The default policy towards templates
152/// is to descend into the 'pattern' class or function body, not any
153/// explicit or implicit instantiations. Explicit specializations
154/// are still visited, and the patterns of partial specializations
155/// are visited separately. This behavior can be changed by
156/// overriding shouldVisitTemplateInstantiations() in the derived class
157/// to return true, in which case all known implicit and explicit
158/// instantiations will be visited at the same time as the pattern
159/// from which they were produced.
160///
161/// By default, this visitor preorder traverses the AST. If postorder traversal
162/// is needed, the \c shouldTraversePostOrder method needs to be overridden
163/// to return \c true.
164template <typename Derived> class RecursiveASTVisitor {
165public:
166 /// A queue used for performing data recursion over statements.
167 /// Parameters involving this type are used to implement data
168 /// recursion over Stmts and Exprs within this class, and should
169 /// typically not be explicitly specified by derived classes.
170 /// The bool bit indicates whether the statement has been traversed or not.
171 typedef SmallVectorImpl<llvm::PointerIntPair<Stmt *, 1, bool>>
172 DataRecursionQueue;
173
174 /// Return a reference to the derived class.
175 Derived &getDerived() { return *static_cast<Derived *>(this); }
176
177 /// Return whether this visitor should recurse into
178 /// template instantiations.
179 bool shouldVisitTemplateInstantiations() const { return false; }
180
181 /// Return whether this visitor should recurse into the types of
182 /// TypeLocs.
183 bool shouldWalkTypesOfTypeLocs() const { return true; }
184
185 /// Return whether this visitor should recurse into implicit
186 /// code, e.g., implicit constructors and destructors.
187 bool shouldVisitImplicitCode() const { return false; }
188
189 /// Return whether this visitor should recurse into lambda body
190 bool shouldVisitLambdaBody() const { return true; }
191
192 /// Return whether this visitor should traverse post-order.
193 bool shouldTraversePostOrder() const { return false; }
194
195 /// Recursively visits an entire AST, starting from the top-level Decls
196 /// in the AST traversal scope (by default, the TranslationUnitDecl).
197 /// \returns false if visitation was terminated early.
198 bool TraverseAST(ASTContext &AST) {
199 for (Decl *D : AST.getTraversalScope())
200 if (!getDerived().TraverseDecl(D))
201 return false;
202 return true;
203 }
204
205 /// Recursively visit a statement or expression, by
206 /// dispatching to Traverse*() based on the argument's dynamic type.
207 ///
208 /// \returns false if the visitation was terminated early, true
209 /// otherwise (including when the argument is nullptr).
210 bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr);
211
212 /// Invoked before visiting a statement or expression via data recursion.
213 ///
214 /// \returns false to skip visiting the node, true otherwise.
215 bool dataTraverseStmtPre(Stmt *S) { return true; }
216
217 /// Invoked after visiting a statement or expression via data recursion.
218 /// This is not invoked if the previously invoked \c dataTraverseStmtPre
219 /// returned false.
220 ///
221 /// \returns false if the visitation was terminated early, true otherwise.
222 bool dataTraverseStmtPost(Stmt *S) { return true; }
223
224 /// Recursively visit a type, by dispatching to
225 /// Traverse*Type() based on the argument's getTypeClass() property.
226 ///
227 /// \returns false if the visitation was terminated early, true
228 /// otherwise (including when the argument is a Null type).
229 bool TraverseType(QualType T);
230
231 /// Recursively visit a type with location, by dispatching to
232 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
233 ///
234 /// \returns false if the visitation was terminated early, true
235 /// otherwise (including when the argument is a Null type location).
236 bool TraverseTypeLoc(TypeLoc TL);
237
238 /// Recursively visit an attribute, by dispatching to
239 /// Traverse*Attr() based on the argument's dynamic type.
240 ///
241 /// \returns false if the visitation was terminated early, true
242 /// otherwise (including when the argument is a Null type location).
243 bool TraverseAttr(Attr *At);
244
245 /// Recursively visit a declaration, by dispatching to
246 /// Traverse*Decl() based on the argument's dynamic type.
247 ///
248 /// \returns false if the visitation was terminated early, true
249 /// otherwise (including when the argument is NULL).
250 bool TraverseDecl(Decl *D);
251
252 /// Recursively visit a C++ nested-name-specifier.
253 ///
254 /// \returns false if the visitation was terminated early, true otherwise.
255 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
256
257 /// Recursively visit a C++ nested-name-specifier with location
258 /// information.
259 ///
260 /// \returns false if the visitation was terminated early, true otherwise.
261 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
262
263 /// Recursively visit a name with its location information.
264 ///
265 /// \returns false if the visitation was terminated early, true otherwise.
266 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
267
268 /// Recursively visit a template name and dispatch to the
269 /// appropriate method.
270 ///
271 /// \returns false if the visitation was terminated early, true otherwise.
272 bool TraverseTemplateName(TemplateName Template);
273
274 /// Recursively visit a template argument and dispatch to the
275 /// appropriate method for the argument type.
276 ///
277 /// \returns false if the visitation was terminated early, true otherwise.
278 // FIXME: migrate callers to TemplateArgumentLoc instead.
279 bool TraverseTemplateArgument(const TemplateArgument &Arg);
280
281 /// Recursively visit a template argument location and dispatch to the
282 /// appropriate method for the argument type.
283 ///
284 /// \returns false if the visitation was terminated early, true otherwise.
285 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
286
287 /// Recursively visit a set of template arguments.
288 /// This can be overridden by a subclass, but it's not expected that
289 /// will be needed -- this visitor always dispatches to another.
290 ///
291 /// \returns false if the visitation was terminated early, true otherwise.
292 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
293 bool TraverseTemplateArguments(const TemplateArgument *Args,
294 unsigned NumArgs);
295
296 /// Recursively visit a base specifier. This can be overridden by a
297 /// subclass.
298 ///
299 /// \returns false if the visitation was terminated early, true otherwise.
300 bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base);
301
302 /// Recursively visit a constructor initializer. This
303 /// automatically dispatches to another visitor for the initializer
304 /// expression, but not for the name of the initializer, so may
305 /// be overridden for clients that need access to the name.
306 ///
307 /// \returns false if the visitation was terminated early, true otherwise.
308 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
309
310 /// Recursively visit a lambda capture. \c Init is the expression that
311 /// will be used to initialize the capture.
312 ///
313 /// \returns false if the visitation was terminated early, true otherwise.
314 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
315 Expr *Init);
316
317 /// Recursively visit the syntactic or semantic form of an
318 /// initialization list.
319 ///
320 /// \returns false if the visitation was terminated early, true otherwise.
321 bool TraverseSynOrSemInitListExpr(InitListExpr *S,
322 DataRecursionQueue *Queue = nullptr);
323
324 /// Recursively visit a reference to a concept with potential arguments.
325 ///
326 /// \returns false if the visitation was terminated early, true otherwise.
327 bool TraverseConceptReference(const ConceptReference &C);
328
329 // ---- Methods on Attrs ----
330
331 // Visit an attribute.
332 bool VisitAttr(Attr *A) { return true; }
333
334// Declare Traverse* and empty Visit* for all Attr classes.
335#define ATTR_VISITOR_DECLS_ONLY
336#include "clang/AST/AttrVisitor.inc"
337#undef ATTR_VISITOR_DECLS_ONLY
338
339// ---- Methods on Stmts ----
340
341 Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); }
342
343private:
344 // Traverse the given statement. If the most-derived traverse function takes a
345 // data recursion queue, pass it on; otherwise, discard it. Note that the
346 // first branch of this conditional must compile whether or not the derived
347 // class can take a queue, so if we're taking the second arm, make the first
348 // arm call our function rather than the derived class version.
349#define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE) \
350 (::clang::detail::has_same_member_pointer_type< \
351 decltype(&RecursiveASTVisitor::Traverse##NAME), \
352 decltype(&Derived::Traverse##NAME)>::value \
353 ? static_cast<std::conditional_t< \
354 ::clang::detail::has_same_member_pointer_type< \
355 decltype(&RecursiveASTVisitor::Traverse##NAME), \
356 decltype(&Derived::Traverse##NAME)>::value, \
357 Derived &, RecursiveASTVisitor &>>(*this) \
358 .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE) \
359 : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)))
360
361// Try to traverse the given statement, or enqueue it if we're performing data
362// recursion in the middle of traversing another statement. Can only be called
363// from within a DEF_TRAVERSE_STMT body or similar context.
364#define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S) \
365 do { \
366 if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue)) \
367 return false; \
368 } while (false)
369
370public:
371// Declare Traverse*() for all concrete Stmt classes.
372#define ABSTRACT_STMT(STMT)
373#define STMT(CLASS, PARENT) \
374 bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
375#include "clang/AST/StmtNodes.inc"
376 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
377
378 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
379 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
380 bool VisitStmt(Stmt *S) { return true; }
381#define STMT(CLASS, PARENT) \
382 bool WalkUpFrom##CLASS(CLASS *S) { \
383 TRY_TO(WalkUpFrom##PARENT(S)); \
384 TRY_TO(Visit##CLASS(S)); \
385 return true; \
386 } \
387 bool Visit##CLASS(CLASS *S) { return true; }
388#include "clang/AST/StmtNodes.inc"
389
390// ---- Methods on Types ----
391// FIXME: revamp to take TypeLoc's rather than Types.
392
393// Declare Traverse*() for all concrete Type classes.
394#define ABSTRACT_TYPE(CLASS, BASE)
395#define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T);
396#include "clang/AST/TypeNodes.inc"
397 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
398
399 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
400 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
401 bool VisitType(Type *T) { return true; }
402#define TYPE(CLASS, BASE) \
403 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
404 TRY_TO(WalkUpFrom##BASE(T)); \
405 TRY_TO(Visit##CLASS##Type(T)); \
406 return true; \
407 } \
408 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
409#include "clang/AST/TypeNodes.inc"
410
411// ---- Methods on TypeLocs ----
412// FIXME: this currently just calls the matching Type methods
413
414// Declare Traverse*() for all concrete TypeLoc classes.
415#define ABSTRACT_TYPELOC(CLASS, BASE)
416#define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
417#include "clang/AST/TypeLocNodes.def"
418 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
419
420 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
421 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
422 bool VisitTypeLoc(TypeLoc TL) { return true; }
423
424 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
425 // TypeNodes.inc and thus need to be handled specially.
426 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
427 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
428 }
429 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
430 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
431 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
432 }
433 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
434
435// Note that BASE includes trailing 'Type' which CLASS doesn't.
436#define TYPE(CLASS, BASE) \
437 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
438 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
439 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
440 return true; \
441 } \
442 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
443#include "clang/AST/TypeNodes.inc"
444
445// ---- Methods on Decls ----
446
447// Declare Traverse*() for all concrete Decl classes.
448#define ABSTRACT_DECL(DECL)
449#define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D);
450#include "clang/AST/DeclNodes.inc"
451 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
452
453 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
454 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
455 bool VisitDecl(Decl *D) { return true; }
456#define DECL(CLASS, BASE) \
457 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
458 TRY_TO(WalkUpFrom##BASE(D)); \
459 TRY_TO(Visit##CLASS##Decl(D)); \
460 return true; \
461 } \
462 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
463#include "clang/AST/DeclNodes.inc"
464
465 bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
466
467#define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \
468 bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D);
469 DEF_TRAVERSE_TMPL_INST(Class)
470 DEF_TRAVERSE_TMPL_INST(Var)
471 DEF_TRAVERSE_TMPL_INST(Function)
472#undef DEF_TRAVERSE_TMPL_INST
473
474 bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
475
476private:
477 // These are helper methods used by more than one Traverse* method.
478 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
479
480 // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
481 template <typename T>
482 bool TraverseDeclTemplateParameterLists(T *D);
483
484 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
485 unsigned Count);
486 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
487 bool TraverseRecordHelper(RecordDecl *D);
488 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
489 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
490 bool TraverseDeclContextHelper(DeclContext *DC);
491 bool TraverseFunctionHelper(FunctionDecl *D);
492 bool TraverseVarHelper(VarDecl *D);
493 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
494 bool TraverseOMPLoopDirective(OMPLoopDirective *S);
495 bool TraverseOMPClause(OMPClause *C);
496#define GEN_CLANG_CLAUSE_CLASS
497#define CLAUSE_CLASS(Enum, Str, Class) bool Visit##Class(Class *C);
498#include "llvm/Frontend/OpenMP/OMP.inc"
499 /// Process clauses with list of variables.
500 template <typename T> bool VisitOMPClauseList(T *Node);
501 /// Process clauses with pre-initis.
502 bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node);
503 bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node);
504
505 bool PostVisitStmt(Stmt *S);
506};
507
508template <typename Derived>
509bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
510 DataRecursionQueue *Queue) {
511 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
512 switch (S->getStmtClass()) {
513 case Stmt::NoStmtClass:
514 break;
515#define ABSTRACT_STMT(STMT)
516#define STMT(CLASS, PARENT) \
517 case Stmt::CLASS##Class: \
518 return TRAVERSE_STMT_BASE(CLASS, CLASS, S, Queue);
519#include "clang/AST/StmtNodes.inc"
520 }
521
522 return true;
523}
524
525#undef DISPATCH_STMT
526
527template <typename Derived>
528bool RecursiveASTVisitor<Derived>::PostVisitStmt(Stmt *S) {
529 // In pre-order traversal mode, each Traverse##STMT method is responsible for
530 // calling WalkUpFrom. Therefore, if the user overrides Traverse##STMT and
531 // does not call the default implementation, the WalkUpFrom callback is not
532 // called. Post-order traversal mode should provide the same behavior
533 // regarding method overrides.
534 //
535 // In post-order traversal mode the Traverse##STMT method, when it receives a
536 // DataRecursionQueue, can't call WalkUpFrom after traversing children because
537 // it only enqueues the children and does not traverse them. TraverseStmt
538 // traverses the enqueued children, and we call WalkUpFrom here.
539 //
540 // However, to make pre-order and post-order modes identical with regards to
541 // whether they call WalkUpFrom at all, we call WalkUpFrom if and only if the
542 // user did not override the Traverse##STMT method. We implement the override
543 // check with isSameMethod calls below.
544
545 switch (S->getStmtClass()) {
546 case Stmt::NoStmtClass:
547 break;
548#define ABSTRACT_STMT(STMT)
549#define STMT(CLASS, PARENT) \
550 case Stmt::CLASS##Class: \
551 if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS, \
552 &Derived::Traverse##CLASS)) { \
553 TRY_TO(WalkUpFrom##CLASS(static_cast<CLASS *>(S))); \
554 } \
555 break;
556#define INITLISTEXPR(CLASS, PARENT) \
557 case Stmt::CLASS##Class: \
558 if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS, \
559 &Derived::Traverse##CLASS)) { \
560 auto ILE = static_cast<CLASS *>(S); \
561 if (auto Syn = ILE->isSemanticForm() ? ILE->getSyntacticForm() : ILE) \
562 TRY_TO(WalkUpFrom##CLASS(Syn)); \
563 if (auto Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm()) \
564 TRY_TO(WalkUpFrom##CLASS(Sem)); \
565 } \
566 break;
567#include "clang/AST/StmtNodes.inc"
568 }
569
570 return true;
571}
572
573#undef DISPATCH_STMT
574
575template <typename Derived>
576bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
577 DataRecursionQueue *Queue) {
578 if (!S)
579 return true;
580
581 if (Queue) {
582 Queue->push_back({S, false});
583 return true;
584 }
585
586 SmallVector<llvm::PointerIntPair<Stmt *, 1, bool>, 8> LocalQueue;
587 LocalQueue.push_back({S, false});
588
589 while (!LocalQueue.empty()) {
590 auto &CurrSAndVisited = LocalQueue.back();
591 Stmt *CurrS = CurrSAndVisited.getPointer();
592 bool Visited = CurrSAndVisited.getInt();
593 if (Visited) {
594 LocalQueue.pop_back();
595 TRY_TO(dataTraverseStmtPost(CurrS));
596 if (getDerived().shouldTraversePostOrder()) {
597 TRY_TO(PostVisitStmt(CurrS));
598 }
599 continue;
600 }
601
602 if (getDerived().dataTraverseStmtPre(CurrS)) {
603 CurrSAndVisited.setInt(true);
604 size_t N = LocalQueue.size();
605 TRY_TO(dataTraverseNode(CurrS, &LocalQueue));
606 // Process new children in the order they were added.
607 std::reverse(LocalQueue.begin() + N, LocalQueue.end());
608 } else {
609 LocalQueue.pop_back();
610 }
611 }
612
613 return true;
614}
615
616template <typename Derived>
617bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
618 if (T.isNull())
619 return true;
620
621 switch (T->getTypeClass()) {
622#define ABSTRACT_TYPE(CLASS, BASE)
623#define TYPE(CLASS, BASE) \
624 case Type::CLASS: \
625 return getDerived().Traverse##CLASS##Type( \
626 static_cast<CLASS##Type *>(const_cast<Type *>(T.getTypePtr())));
627#include "clang/AST/TypeNodes.inc"
628 }
629
630 return true;
631}
632
633template <typename Derived>
634bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
635 if (TL.isNull())
636 return true;
637
638 switch (TL.getTypeLocClass()) {
639#define ABSTRACT_TYPELOC(CLASS, BASE)
640#define TYPELOC(CLASS, BASE) \
641 case TypeLoc::CLASS: \
642 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
643#include "clang/AST/TypeLocNodes.def"
644 }
645
646 return true;
647}
648
649// Define the Traverse*Attr(Attr* A) methods
650#define VISITORCLASS RecursiveASTVisitor
651#include "clang/AST/AttrVisitor.inc"
652#undef VISITORCLASS
653
654template <typename Derived>
655bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
656 if (!D)
657 return true;
658
659 // As a syntax visitor, by default we want to ignore declarations for
660 // implicit declarations (ones not typed explicitly by the user).
661 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
662 return true;
663
664 switch (D->getKind()) {
665#define ABSTRACT_DECL(DECL)
666#define DECL(CLASS, BASE) \
667 case Decl::CLASS: \
668 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \
669 return false; \
670 break;
671#include "clang/AST/DeclNodes.inc"
672 }
673 return true;
674}
675
676template <typename Derived>
677bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
678 NestedNameSpecifier *NNS) {
679 if (!NNS)
680 return true;
681
682 if (NNS->getPrefix())
683 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
684
685 switch (NNS->getKind()) {
686 case NestedNameSpecifier::Identifier:
687 case NestedNameSpecifier::Namespace:
688 case NestedNameSpecifier::NamespaceAlias:
689 case NestedNameSpecifier::Global:
690 case NestedNameSpecifier::Super:
691 return true;
692
693 case NestedNameSpecifier::TypeSpec:
694 case NestedNameSpecifier::TypeSpecWithTemplate:
695 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
696 }
697
698 return true;
699}
700
701template <typename Derived>
702bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
703 NestedNameSpecifierLoc NNS) {
704 if (!NNS)
705 return true;
706
707 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
708 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
709
710 switch (NNS.getNestedNameSpecifier()->getKind()) {
711 case NestedNameSpecifier::Identifier:
712 case NestedNameSpecifier::Namespace:
713 case NestedNameSpecifier::NamespaceAlias:
714 case NestedNameSpecifier::Global:
715 case NestedNameSpecifier::Super:
716 return true;
717
718 case NestedNameSpecifier::TypeSpec:
719 case NestedNameSpecifier::TypeSpecWithTemplate:
720 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
721 break;
722 }
723
724 return true;
725}
726
727template <typename Derived>
728bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
729 DeclarationNameInfo NameInfo) {
730 switch (NameInfo.getName().getNameKind()) {
731 case DeclarationName::CXXConstructorName:
732 case DeclarationName::CXXDestructorName:
733 case DeclarationName::CXXConversionFunctionName:
734 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
735 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
736 break;
737
738 case DeclarationName::CXXDeductionGuideName:
739 TRY_TO(TraverseTemplateName(
740 TemplateName(NameInfo.getName().getCXXDeductionGuideTemplate())));
741 break;
742
743 case DeclarationName::Identifier:
744 case DeclarationName::ObjCZeroArgSelector:
745 case DeclarationName::ObjCOneArgSelector:
746 case DeclarationName::ObjCMultiArgSelector:
747 case DeclarationName::CXXOperatorName:
748 case DeclarationName::CXXLiteralOperatorName:
749 case DeclarationName::CXXUsingDirective:
750 break;
751 }
752
753 return true;
754}
755
756template <typename Derived>
757bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
758 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
759 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
760 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
761 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
762
763 return true;
764}
765
766template <typename Derived>
767bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
768 const TemplateArgument &Arg) {
769 switch (Arg.getKind()) {
770 case TemplateArgument::Null:
771 case TemplateArgument::Declaration:
772 case TemplateArgument::Integral:
773 case TemplateArgument::NullPtr:
774 return true;
775
776 case TemplateArgument::Type:
777 return getDerived().TraverseType(Arg.getAsType());
778
779 case TemplateArgument::Template:
780 case TemplateArgument::TemplateExpansion:
781 return getDerived().TraverseTemplateName(
782 Arg.getAsTemplateOrTemplatePattern());
783
784 case TemplateArgument::Expression:
785 return getDerived().TraverseStmt(Arg.getAsExpr());
786
787 case TemplateArgument::Pack:
788 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
789 Arg.pack_size());
790 }
791
792 return true;
793}
794
795// FIXME: no template name location?
796// FIXME: no source locations for a template argument pack?
797template <typename Derived>
798bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
799 const TemplateArgumentLoc &ArgLoc) {
800 const TemplateArgument &Arg = ArgLoc.getArgument();
801
802 switch (Arg.getKind()) {
803 case TemplateArgument::Null:
804 case TemplateArgument::Declaration:
805 case TemplateArgument::Integral:
806 case TemplateArgument::NullPtr:
807 return true;
808
809 case TemplateArgument::Type: {
810 // FIXME: how can TSI ever be NULL?
811 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
812 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
813 else
814 return getDerived().TraverseType(Arg.getAsType());
815 }
816
817 case TemplateArgument::Template:
818 case TemplateArgument::TemplateExpansion:
819 if (ArgLoc.getTemplateQualifierLoc())
820 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
821 ArgLoc.getTemplateQualifierLoc()));
822 return getDerived().TraverseTemplateName(
823 Arg.getAsTemplateOrTemplatePattern());
824
825 case TemplateArgument::Expression:
826 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
827
828 case TemplateArgument::Pack:
829 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
830 Arg.pack_size());
831 }
832
833 return true;
834}
835
836template <typename Derived>
837bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
838 const TemplateArgument *Args, unsigned NumArgs) {
839 for (unsigned I = 0; I != NumArgs; ++I) {
840 TRY_TO(TraverseTemplateArgument(Args[I]));
841 }
842
843 return true;
844}
845
846template <typename Derived>
847bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
848 CXXCtorInitializer *Init) {
849 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
850 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
851
852 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
853 TRY_TO(TraverseStmt(Init->getInit()));
854
855 return true;
856}
857
858template <typename Derived>
859bool
860RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
861 const LambdaCapture *C,
862 Expr *Init) {
863 if (LE->isInitCapture(C))
864 TRY_TO(TraverseDecl(C->getCapturedVar()));
865 else
866 TRY_TO(TraverseStmt(Init));
867 return true;
868}
869
870// ----------------- Type traversal -----------------
871
872// This macro makes available a variable T, the passed-in type.
873#define DEF_TRAVERSE_TYPE(TYPE, CODE) \
874 template <typename Derived> \
875 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \
876 if (!getDerived().shouldTraversePostOrder()) \
877 TRY_TO(WalkUpFrom##TYPE(T)); \
878 { CODE; } \
879 if (getDerived().shouldTraversePostOrder()) \
880 TRY_TO(WalkUpFrom##TYPE(T)); \
881 return true; \
882 }
883
884DEF_TRAVERSE_TYPE(BuiltinType, {})
885
886DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
887
888DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
889
890DEF_TRAVERSE_TYPE(BlockPointerType,
891 { TRY_TO(TraverseType(T->getPointeeType())); })
892
893DEF_TRAVERSE_TYPE(LValueReferenceType,
894 { TRY_TO(TraverseType(T->getPointeeType())); })
895
896DEF_TRAVERSE_TYPE(RValueReferenceType,
897 { TRY_TO(TraverseType(T->getPointeeType())); })
898
899DEF_TRAVERSE_TYPE(MemberPointerType, {
900 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
901 TRY_TO(TraverseType(T->getPointeeType()));
902})
903
904DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
905
906DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
907
908DEF_TRAVERSE_TYPE(ConstantArrayType, {
909 TRY_TO(TraverseType(T->getElementType()));
910 if (T->getSizeExpr())
911 TRY_TO(TraverseStmt(const_cast<Expr*>(T->getSizeExpr())));
912})
913
914DEF_TRAVERSE_TYPE(IncompleteArrayType,
915 { TRY_TO(TraverseType(T->getElementType())); })
916
917DEF_TRAVERSE_TYPE(VariableArrayType, {
918 TRY_TO(TraverseType(T->getElementType()));
919 TRY_TO(TraverseStmt(T->getSizeExpr()));
920})
921
922DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
923 TRY_TO(TraverseType(T->getElementType()));
924 if (T->getSizeExpr())
925 TRY_TO(TraverseStmt(T->getSizeExpr()));
926})
927
928DEF_TRAVERSE_TYPE(DependentAddressSpaceType, {
929 TRY_TO(TraverseStmt(T->getAddrSpaceExpr()));
930 TRY_TO(TraverseType(T->getPointeeType()));
931})
932
933DEF_TRAVERSE_TYPE(DependentVectorType, {
934 if (T->getSizeExpr())
935 TRY_TO(TraverseStmt(T->getSizeExpr()));
936 TRY_TO(TraverseType(T->getElementType()));
937})
938
939DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
940 if (T->getSizeExpr())
941 TRY_TO(TraverseStmt(T->getSizeExpr()));
942 TRY_TO(TraverseType(T->getElementType()));
943})
944
945DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
946
947DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
948
949DEF_TRAVERSE_TYPE(ConstantMatrixType,
950 { TRY_TO(TraverseType(T->getElementType())); })
951
952DEF_TRAVERSE_TYPE(DependentSizedMatrixType, {
953 if (T->getRowExpr())
954 TRY_TO(TraverseStmt(T->getRowExpr()));
955 if (T->getColumnExpr())
956 TRY_TO(TraverseStmt(T->getColumnExpr()));
957 TRY_TO(TraverseType(T->getElementType()));
958})
959
960DEF_TRAVERSE_TYPE(FunctionNoProtoType,
961 { TRY_TO(TraverseType(T->getReturnType())); })
962
963DEF_TRAVERSE_TYPE(FunctionProtoType, {
964 TRY_TO(TraverseType(T->getReturnType()));
965
966 for (const auto &A : T->param_types()) {
967 TRY_TO(TraverseType(A));
968 }
969
970 for (const auto &E : T->exceptions()) {
971 TRY_TO(TraverseType(E));
972 }
973
974 if (Expr *NE = T->getNoexceptExpr())
975 TRY_TO(TraverseStmt(NE));
976})
977
978DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
979DEF_TRAVERSE_TYPE(TypedefType, {})
980
981DEF_TRAVERSE_TYPE(TypeOfExprType,
982 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
983
984DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
985
986DEF_TRAVERSE_TYPE(DecltypeType,
987 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
988
989DEF_TRAVERSE_TYPE(UnaryTransformType, {
990 TRY_TO(TraverseType(T->getBaseType()));
991 TRY_TO(TraverseType(T->getUnderlyingType()));
992})
993
994DEF_TRAVERSE_TYPE(AutoType, {
995 TRY_TO(TraverseType(T->getDeducedType()));
996 if (T->isConstrained()) {
997 TRY_TO(TraverseDecl(T->getTypeConstraintConcept()));
998 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
999 }
1000})
1001DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, {
1002 TRY_TO(TraverseTemplateName(T->getTemplateName()));
1003 TRY_TO(TraverseType(T->getDeducedType()));
1004})
1005
1006DEF_TRAVERSE_TYPE(RecordType, {})
1007DEF_TRAVERSE_TYPE(EnumType, {})
1008DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
1009DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {
1010 TRY_TO(TraverseType(T->getReplacementType()));
1011})
1012DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {
1013 TRY_TO(TraverseTemplateArgument(T->getArgumentPack()));
1014})
1015
1016DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
1017 TRY_TO(TraverseTemplateName(T->getTemplateName()));
1018 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1019})
1020
1021DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
1022
1023DEF_TRAVERSE_TYPE(AttributedType,
1024 { TRY_TO(TraverseType(T->getModifiedType())); })
1025
1026DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
1027
1028DEF_TRAVERSE_TYPE(MacroQualifiedType,
1029 { TRY_TO(TraverseType(T->getUnderlyingType())); })
1030
1031DEF_TRAVERSE_TYPE(ElaboratedType, {
1032 if (T->getQualifier()) {
1033 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1034 }
1035 TRY_TO(TraverseType(T->getNamedType()));
1036})
1037
1038DEF_TRAVERSE_TYPE(DependentNameType,
1039 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
1040
1041DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1042 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1043 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1044})
1045
1046DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1047
1048DEF_TRAVERSE_TYPE(ObjCTypeParamType, {})
1049
1050DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1051
1052DEF_TRAVERSE_TYPE(ObjCObjectType, {
1053 // We have to watch out here because an ObjCInterfaceType's base
1054 // type is itself.
1055 if (T->getBaseType().getTypePtr() != T)
1056 TRY_TO(TraverseType(T->getBaseType()));
1057 for (auto typeArg : T->getTypeArgsAsWritten()) {
1058 TRY_TO(TraverseType(typeArg));
1059 }
1060})
1061
1062DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1063 { TRY_TO(TraverseType(T->getPointeeType())); })
1064
1065DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1066
1067DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
1068
1069DEF_TRAVERSE_TYPE(ExtIntType, {})
1070DEF_TRAVERSE_TYPE(DependentExtIntType,
1071 { TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
1072
1073#undef DEF_TRAVERSE_TYPE
1074
1075// ----------------- TypeLoc traversal -----------------
1076
1077// This macro makes available a variable TL, the passed-in TypeLoc.
1078// If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
1079// in addition to WalkUpFrom* for the TypeLoc itself, such that existing
1080// clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
1081// continue to work.
1082#define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
1083 template <typename Derived> \
1084 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
1085 if (!getDerived().shouldTraversePostOrder()) { \
1086 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1087 if (getDerived().shouldWalkTypesOfTypeLocs()) \
1088 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
1089 } \
1090 { CODE; } \
1091 if (getDerived().shouldTraversePostOrder()) { \
1092 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1093 if (getDerived().shouldWalkTypesOfTypeLocs()) \
1094 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \
1095 } \
1096 return true; \
1097 }
1098
1099template <typename Derived>
1100bool
1101RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) {
1102 // Move this over to the 'main' typeloc tree. Note that this is a
1103 // move -- we pretend that we were really looking at the unqualified
1104 // typeloc all along -- rather than a recursion, so we don't follow
1105 // the normal CRTP plan of going through
1106 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1107 // twice for the same type (once as a QualifiedTypeLoc version of
1108 // the type, once as an UnqualifiedTypeLoc version of the type),
1109 // which in effect means we'd call VisitTypeLoc twice with the
1110 // 'same' type. This solves that problem, at the cost of never
1111 // seeing the qualified version of the type (unless the client
1112 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1113 // perfect solution. A perfect solution probably requires making
1114 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1115 // wrapper around Type* -- rather than being its own class in the
1116 // type hierarchy.
1117 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1118}
1119
1120DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1121
1122// FIXME: ComplexTypeLoc is unfinished
1123DEF_TRAVERSE_TYPELOC(ComplexType, {
1124 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1125})
1126
1127DEF_TRAVERSE_TYPELOC(PointerType,
1128 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1129
1130DEF_TRAVERSE_TYPELOC(BlockPointerType,
1131 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1132
1133DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1134 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1135
1136DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1137 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1138
1139// We traverse this in the type case as well, but how is it not reached through
1140// the pointee type?
1141DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1142 if (auto *TSI = TL.getClassTInfo())
1143 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1144 else
1145 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1146 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1147})
1148
1149DEF_TRAVERSE_TYPELOC(AdjustedType,
1150 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1151
1152DEF_TRAVERSE_TYPELOC(DecayedType,
1153 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1154
1155template <typename Derived>
1156bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1157 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1158 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1159 return true;
1160}
1161
1162DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1163 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1164 TRY_TO(TraverseArrayTypeLocHelper(TL));
1165})
1166
1167DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1168 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1169 TRY_TO(TraverseArrayTypeLocHelper(TL));
1170})
1171
1172DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1173 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1174 TRY_TO(TraverseArrayTypeLocHelper(TL));
1175})
1176
1177DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1178 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1179 TRY_TO(TraverseArrayTypeLocHelper(TL));
1180})
1181
1182DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
1183 TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr()));
1184 TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType()));
1185})
1186
1187// FIXME: order? why not size expr first?
1188// FIXME: base VectorTypeLoc is unfinished
1189DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1190 if (TL.getTypePtr()->getSizeExpr())
1191 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1192 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1193})
1194
1195// FIXME: VectorTypeLoc is unfinished
1196DEF_TRAVERSE_TYPELOC(VectorType, {
1197 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1198})
1199
1200DEF_TRAVERSE_TYPELOC(DependentVectorType, {
1201 if (TL.getTypePtr()->getSizeExpr())
1202 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1203 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1204})
1205
1206// FIXME: size and attributes
1207// FIXME: base VectorTypeLoc is unfinished
1208DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1209 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1210})
1211
1212DEF_TRAVERSE_TYPELOC(ConstantMatrixType, {
1213 TRY_TO(TraverseStmt(TL.getAttrRowOperand()));
1214 TRY_TO(TraverseStmt(TL.getAttrColumnOperand()));
1215 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1216})
1217
1218DEF_TRAVERSE_TYPELOC(DependentSizedMatrixType, {
1219 TRY_TO(TraverseStmt(TL.getAttrRowOperand()));
1220 TRY_TO(TraverseStmt(TL.getAttrColumnOperand()));
1221 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1222})
1223
1224DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1225 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1226
1227// FIXME: location of exception specifications (attributes?)
1228DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1229 TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1230
1231 const FunctionProtoType *T = TL.getTypePtr();
1232
1233 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) {
1234 if (TL.getParam(I)) {
1235 TRY_TO(TraverseDecl(TL.getParam(I)));
1236 } else if (I < T->getNumParams()) {
1237 TRY_TO(TraverseType(T->getParamType(I)));
1238 }
1239 }
1240
1241 for (const auto &E : T->exceptions()) {
1242 TRY_TO(TraverseType(E));
1243 }
1244
1245 if (Expr *NE = T->getNoexceptExpr())
1246 TRY_TO(TraverseStmt(NE));
1247})
1248
1249DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1250DEF_TRAVERSE_TYPELOC(TypedefType, {})
1251
1252DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1253 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1254
1255DEF_TRAVERSE_TYPELOC(TypeOfType, {
1256 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1257})
1258
1259// FIXME: location of underlying expr
1260DEF_TRAVERSE_TYPELOC(DecltypeType, {
1261 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1262})
1263
1264DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1265 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1266})
1267
1268DEF_TRAVERSE_TYPELOC(AutoType, {
1269 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1270 if (TL.isConstrained()) {
1271 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc()));
1272 TRY_TO(TraverseDeclarationNameInfo(TL.getConceptNameInfo()));
1273 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
1274 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1275 }
1276})
1277
1278DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, {
1279 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1280 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1281})
1282
1283DEF_TRAVERSE_TYPELOC(RecordType, {})
1284DEF_TRAVERSE_TYPELOC(EnumType, {})
1285DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1286DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {
1287 TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType()));
1288})
1289DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {
1290 TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack()));
1291})
1292
1293// FIXME: use the loc for the template name?
1294DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1295 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1296 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1297 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1298 }
1299})
1300
1301DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1302
1303DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1304
1305DEF_TRAVERSE_TYPELOC(MacroQualifiedType,
1306 { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1307
1308DEF_TRAVERSE_TYPELOC(AttributedType,
1309 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1310
1311DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1312 if (TL.getQualifierLoc()) {
1313 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1314 }
1315 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1316})
1317
1318DEF_TRAVERSE_TYPELOC(DependentNameType, {
1319 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1320})
1321
1322DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1323 if (TL.getQualifierLoc()) {
1324 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1325 }
1326
1327 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1328 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1329 }
1330})
1331
1332DEF_TRAVERSE_TYPELOC(PackExpansionType,
1333 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1334
1335DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {})
1336
1337DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1338
1339DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1340 // We have to watch out here because an ObjCInterfaceType's base
1341 // type is itself.
1342 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1343 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1344 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1345 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1346})
1347
1348DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1349 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1350
1351DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1352
1353DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1354
1355DEF_TRAVERSE_TYPELOC(ExtIntType, {})
1356DEF_TRAVERSE_TYPELOC(DependentExtIntType, {
1357 TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
1358})
1359
1360#undef DEF_TRAVERSE_TYPELOC
1361
1362// ----------------- Decl traversal -----------------
1363//
1364// For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1365// the children that come from the DeclContext associated with it.
1366// Therefore each Traverse* only needs to worry about children other
1367// than those.
1368
1369template <typename Derived>
1370bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext(
1371 const Decl *Child) {
1372 // BlockDecls are traversed through BlockExprs,
1373 // CapturedDecls are traversed through CapturedStmts.
1374 if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child))
1375 return true;
1376 // Lambda classes are traversed through LambdaExprs.
1377 if (const CXXRecordDecl* Cls = dyn_cast<CXXRecordDecl>(Child))
1378 return Cls->isLambda();
1379 return false;
1380}
1381
1382template <typename Derived>
1383bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1384 if (!DC)
1385 return true;
1386
1387 for (auto *Child : DC->decls()) {
1388 if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1389 TRY_TO(TraverseDecl(Child));
1390 }
1391
1392 return true;
1393}
1394
1395// This macro makes available a variable D, the passed-in decl.
1396#define DEF_TRAVERSE_DECL(DECL, CODE) \
1397 template <typename Derived> \
1398 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \
1399 bool ShouldVisitChildren = true; \
1400 bool ReturnValue = true; \
1401 if (!getDerived().shouldTraversePostOrder()) \
1402 TRY_TO(WalkUpFrom##DECL(D)); \
1403 { CODE; } \
1404 if (ReturnValue && ShouldVisitChildren) \
1405 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1406 if (ReturnValue) { \
1407 /* Visit any attributes attached to this declaration. */ \
1408 for (auto *I : D->attrs()) \
1409 TRY_TO(getDerived().TraverseAttr(I)); \
1410 } \
1411 if (ReturnValue && getDerived().shouldTraversePostOrder()) \
1412 TRY_TO(WalkUpFrom##DECL(D)); \
1413 return ReturnValue; \
1414 }
1415
1416DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1417
1418DEF_TRAVERSE_DECL(BlockDecl, {
1419 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1420 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1421 TRY_TO(TraverseStmt(D->getBody()));
1422 for (const auto &I : D->captures()) {
1423 if (I.hasCopyExpr()) {
1424 TRY_TO(TraverseStmt(I.getCopyExpr()));
1425 }
1426 }
1427 ShouldVisitChildren = false;
1428})
1429
1430DEF_TRAVERSE_DECL(CapturedDecl, {
1431 TRY_TO(TraverseStmt(D->getBody()));
1432 ShouldVisitChildren = false;
1433})
1434
1435DEF_TRAVERSE_DECL(EmptyDecl, {})
1436
1437DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
1438 TRY_TO(TraverseStmt(D->getTemporaryExpr()));
1439})
1440
1441DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1442 { TRY_TO(TraverseStmt(D->getAsmString())); })
1443
1444DEF_TRAVERSE_DECL(ImportDecl, {})
1445
1446DEF_TRAVERSE_DECL(FriendDecl, {
1447 // Friend is either decl or a type.
1448 if (D->getFriendType())
1449 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1450 else
1451 TRY_TO(TraverseDecl(D->getFriendDecl()));
1452})
1453
1454DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1455 if (D->getFriendType())
1456 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1457 else
1458 TRY_TO(TraverseDecl(D->getFriendDecl()));
1459 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1460 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1461 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1462 ITPL != ETPL; ++ITPL) {
1463 TRY_TO(TraverseDecl(*ITPL));
1464 }
1465 }
1466})
1467
1468DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1469 TRY_TO(TraverseDecl(D->getSpecialization()));
1470
1471 if (D->hasExplicitTemplateArgs()) {
1472 TRY_TO(TraverseTemplateArgumentLocsHelper(
1473 D->getTemplateArgsAsWritten()->getTemplateArgs(),
1474 D->getTemplateArgsAsWritten()->NumTemplateArgs));
1475 }
1476})
1477
1478DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1479
1480DEF_TRAVERSE_DECL(ExportDecl, {})
1481
1482DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1483 })
1484
1485DEF_TRAVERSE_DECL(StaticAssertDecl, {
1486 TRY_TO(TraverseStmt(D->getAssertExpr()));
1487 TRY_TO(TraverseStmt(D->getMessage()));
1488})
1489
1490DEF_TRAVERSE_DECL(
1491 TranslationUnitDecl,
1492 {// Code in an unnamed namespace shows up automatically in
1493 // decls_begin()/decls_end(). Thus we don't need to recurse on
1494 // D->getAnonymousNamespace().
1495 })
1496
1497DEF_TRAVERSE_DECL(PragmaCommentDecl, {})
1498
1499DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {})
1500
1501DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1502
1503DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1504 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1505
1506 // We shouldn't traverse an aliased namespace, since it will be
1507 // defined (and, therefore, traversed) somewhere else.
1508 ShouldVisitChildren = false;
1509})
1510
1511DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1512 })
1513
1514DEF_TRAVERSE_DECL(
1515 NamespaceDecl,
1516 {// Code in an unnamed namespace shows up automatically in
1517 // decls_begin()/decls_end(). Thus we don't need to recurse on
1518 // D->getAnonymousNamespace().
1519 })
1520
1521DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1522 })
1523
1524DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1525 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1526 for (auto typeParam : *typeParamList) {
1527 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1528 }
1529 }
1530})
1531
1532DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1533 })
1534
1535DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1536 })
1537
1538DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1539 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1540 for (auto typeParam : *typeParamList) {
1541 TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1542 }
1543 }
1544
1545 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1546 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1547 }
1548})
1549
1550DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1551 })
1552
1553DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1554 if (D->getReturnTypeSourceInfo()) {
1555 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1556 }
1557 for (ParmVarDecl *Parameter : D->parameters()) {
1558 TRY_TO(TraverseDecl(Parameter));
1559 }
1560 if (D->isThisDeclarationADefinition()) {
1561 TRY_TO(TraverseStmt(D->getBody()));
1562 }
1563 ShouldVisitChildren = false;
1564})
1565
1566DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1567 if (D->hasExplicitBound()) {
1568 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1569 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1570 // declaring the type alias, not something that was written in the
1571 // source.
1572 }
1573})
1574
1575DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1576 if (D->getTypeSourceInfo())
1577 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1578 else
1579 TRY_TO(TraverseType(D->getType()));
1580 ShouldVisitChildren = false;
1581})
1582
1583DEF_TRAVERSE_DECL(UsingDecl, {
1584 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1585 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1586})
1587
1588DEF_TRAVERSE_DECL(UsingPackDecl, {})
1589
1590DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1591 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1592})
1593
1594DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1595
1596DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {})
1597
1598DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1599 for (auto *I : D->varlists()) {
1600 TRY_TO(TraverseStmt(I));
1601 }
1602 })
1603
1604DEF_TRAVERSE_DECL(OMPRequiresDecl, {
1605 for (auto *C : D->clauselists()) {
1606 TRY_TO(TraverseOMPClause(C));
1607 }
1608})
1609
1610DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, {
1611 TRY_TO(TraverseStmt(D->getCombiner()));
1612 if (auto *Initializer = D->getInitializer())
1613 TRY_TO(TraverseStmt(Initializer));
1614 TRY_TO(TraverseType(D->getType()));
1615 return true;
1616})
1617
1618DEF_TRAVERSE_DECL(OMPDeclareMapperDecl, {
1619 for (auto *C : D->clauselists())
1620 TRY_TO(TraverseOMPClause(C));
1621 TRY_TO(TraverseType(D->getType()));
1622 return true;
1623})
1624
1625DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
1626
1627DEF_TRAVERSE_DECL(OMPAllocateDecl, {
1628 for (auto *I : D->varlists())
1629 TRY_TO(TraverseStmt(I));
1630 for (auto *C : D->clauselists())
1631 TRY_TO(TraverseOMPClause(C));
1632})
1633
1634// A helper method for TemplateDecl's children.
1635template <typename Derived>
1636bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1637 TemplateParameterList *TPL) {
1638 if (TPL) {
1639 for (NamedDecl *D : *TPL) {
1640 TRY_TO(TraverseDecl(D));
1641 }
1642 if (Expr *RequiresClause = TPL->getRequiresClause()) {
1643 TRY_TO(TraverseStmt(RequiresClause));
1644 }
1645 }
1646 return true;
1647}
1648
1649template <typename Derived>
1650template <typename T>
1651bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) {
1652 for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) {
1653 TemplateParameterList *TPL = D->getTemplateParameterList(i);
1654 TraverseTemplateParameterListHelper(TPL);
1655 }
1656 return true;
1657}
1658
1659template <typename Derived>
1660bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1661 ClassTemplateDecl *D) {
1662 for (auto *SD : D->specializations()) {
1663 for (auto *RD : SD->redecls()) {
1664 // We don't want to visit injected-class-names in this traversal.
1665 if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1666 continue;
1667
1668 switch (
1669 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1670 // Visit the implicit instantiations with the requested pattern.
1671 case TSK_Undeclared:
1672 case TSK_ImplicitInstantiation:
1673 TRY_TO(TraverseDecl(RD));
1674 break;
1675
1676 // We don't need to do anything on an explicit instantiation
1677 // or explicit specialization because there will be an explicit
1678 // node for it elsewhere.
1679 case TSK_ExplicitInstantiationDeclaration:
1680 case TSK_ExplicitInstantiationDefinition:
1681 case TSK_ExplicitSpecialization:
1682 break;
1683 }
1684 }
1685 }
1686
1687 return true;
1688}
1689
1690template <typename Derived>
1691bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1692 VarTemplateDecl *D) {
1693 for (auto *SD : D->specializations()) {
1694 for (auto *RD : SD->redecls()) {
1695 switch (
1696 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1697 case TSK_Undeclared:
1698 case TSK_ImplicitInstantiation:
1699 TRY_TO(TraverseDecl(RD));
1700 break;
1701
1702 case TSK_ExplicitInstantiationDeclaration:
1703 case TSK_ExplicitInstantiationDefinition:
1704 case TSK_ExplicitSpecialization:
1705 break;
1706 }
1707 }
1708 }
1709
1710 return true;
1711}
1712
1713// A helper method for traversing the instantiations of a
1714// function while skipping its specializations.
1715template <typename Derived>
1716bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1717 FunctionTemplateDecl *D) {
1718 for (auto *FD : D->specializations()) {
1719 for (auto *RD : FD->redecls()) {
1720 switch (RD->getTemplateSpecializationKind()) {
1721 case TSK_Undeclared:
1722 case TSK_ImplicitInstantiation:
1723 // We don't know what kind of FunctionDecl this is.
1724 TRY_TO(TraverseDecl(RD));
1725 break;
1726
1727 // FIXME: For now traverse explicit instantiations here. Change that
1728 // once they are represented as dedicated nodes in the AST.
1729 case TSK_ExplicitInstantiationDeclaration:
1730 case TSK_ExplicitInstantiationDefinition:
1731 TRY_TO(TraverseDecl(RD));
1732 break;
1733
1734 case TSK_ExplicitSpecialization:
1735 break;
1736 }
1737 }
1738 }
1739
1740 return true;
1741}
1742
1743// This macro unifies the traversal of class, variable and function
1744// template declarations.
1745#define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \
1746 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \
1747 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \
1748 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \
1749 \
1750 /* By default, we do not traverse the instantiations of \
1751 class templates since they do not appear in the user code. The \
1752 following code optionally traverses them. \
1753 \
1754 We only traverse the class instantiations when we see the canonical \
1755 declaration of the template, to ensure we only visit them once. */ \
1756 if (getDerived().shouldVisitTemplateInstantiations() && \
1757 D == D->getCanonicalDecl()) \
1758 TRY_TO(TraverseTemplateInstantiations(D)); \
1759 \
1760 /* Note that getInstantiatedFromMemberTemplate() is just a link \
1761 from a template instantiation back to the template from which \
1762 it was instantiated, and thus should not be traversed. */ \
1763 })
1764
1765DEF_TRAVERSE_TMPL_DECL(Class)
1766DEF_TRAVERSE_TMPL_DECL(Var)
1767DEF_TRAVERSE_TMPL_DECL(Function)
1768
1769DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1770 // D is the "T" in something like
1771 // template <template <typename> class T> class container { };
1772 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1773 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1774 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1775 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1776})
1777
1778DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1779 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1780})
1781
1782DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1783 // D is the "T" in something like "template<typename T> class vector;"
1784 if (D->getTypeForDecl())
1785 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1786 if (const auto *TC = D->getTypeConstraint()) {
1787 if (Expr *IDC = TC->getImmediatelyDeclaredConstraint()) {
1788 TRY_TO(TraverseStmt(IDC));
1789 } else {
1790 // Avoid traversing the ConceptReference in the TypeCosntraint
1791 // if we have an immediately-declared-constraint, otherwise
1792 // we'll end up visiting the concept and the arguments in
1793 // the TC twice.
1794 TRY_TO(TraverseConceptReference(*TC));
1795 }
1796 }
1797 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1798 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1799})
1800
1801DEF_TRAVERSE_DECL(TypedefDecl, {
1802 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1803 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1804 // declaring the typedef, not something that was written in the
1805 // source.
1806})
1807
1808DEF_TRAVERSE_DECL(TypeAliasDecl, {
1809 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1810 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1811 // declaring the type alias, not something that was written in the
1812 // source.
1813})
1814
1815DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1816 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1817 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1818})
1819
1820DEF_TRAVERSE_DECL(ConceptDecl, {
1821 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1822 TRY_TO(TraverseStmt(D->getConstraintExpr()));
1823})
1824
1825DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1826 // A dependent using declaration which was marked with 'typename'.
1827 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1828 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1829 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1830 // declaring the type, not something that was written in the
1831 // source.
1832})
1833
1834DEF_TRAVERSE_DECL(EnumDecl, {
1835 TRY_TO(TraverseDeclTemplateParameterLists(D));
1836
1837 if (D->getTypeForDecl())
1838 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1839
1840 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1841 // The enumerators are already traversed by
1842 // decls_begin()/decls_end().
1843})
1844
1845// Helper methods for RecordDecl and its children.
1846template <typename Derived>
1847bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1848 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1849 // declaring the type, not something that was written in the source.
1850
1851 TRY_TO(TraverseDeclTemplateParameterLists(D));
1852 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1853 return true;
1854}
1855
1856template <typename Derived>
1857bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier(
1858 const CXXBaseSpecifier &Base) {
1859 TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc()));
1860 return true;
1861}
1862
1863template <typename Derived>
1864bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1865 if (!TraverseRecordHelper(D))
1866 return false;
1867 if (D->isCompleteDefinition()) {
1868 for (const auto &I : D->bases()) {
1869 TRY_TO(TraverseCXXBaseSpecifier(I));
1870 }
1871 // We don't traverse the friends or the conversions, as they are
1872 // already in decls_begin()/decls_end().
1873 }
1874 return true;
1875}
1876
1877DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1878
1879DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1880
1881#define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \
1882 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \
1883 /* For implicit instantiations ("set<int> x;"), we don't want to \
1884 recurse at all, since the instatiated template isn't written in \
1885 the source code anywhere. (Note the instatiated *type* -- \
1886 set<int> -- is written, and will still get a callback of \
1887 TemplateSpecializationType). For explicit instantiations \
1888 ("template set<int>;"), we do need a callback, since this \
1889 is the only callback that's made for this instantiation. \
1890 We use getTypeAsWritten() to distinguish. */ \
1891 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \
1892 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \
1893 \
1894 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); \
1895 if (!getDerived().shouldVisitTemplateInstantiations() && \
1896 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \
1897 /* Returning from here skips traversing the \
1898 declaration context of the *TemplateSpecializationDecl \
1899 (embedded in the DEF_TRAVERSE_DECL() macro) \
1900 which contains the instantiated members of the template. */ \
1901 return true; \
1902 })
1903
1904DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1905DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1906
1907template <typename Derived>
1908bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1909 const TemplateArgumentLoc *TAL, unsigned Count) {
1910 for (unsigned I = 0; I < Count; ++I) {
1911 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1912 }
1913 return true;
1914}
1915
1916#define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \
1917 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \
1918 /* The partial specialization. */ \
1919 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \
1920 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \
1921 I != E; ++I) { \
1922 TRY_TO(TraverseDecl(*I)); \
1923 } \
1924 } \
1925 /* The args that remains unspecialized. */ \
1926 TRY_TO(TraverseTemplateArgumentLocsHelper( \
1927 D->getTemplateArgsAsWritten()->getTemplateArgs(), \
1928 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \
1929 \
1930 /* Don't need the *TemplatePartialSpecializationHelper, even \
1931 though that's our parent class -- we already visit all the \
1932 template args here. */ \
1933 TRY_TO(Traverse##DECLKIND##Helper(D)); \
1934 \
1935 /* Instantiations will have been visited with the primary template. */ \
1936 })
1937
1938DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1939DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1940
1941DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1942
1943DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1944 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1945 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1946 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1947 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1948})
1949
1950DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1951
1952template <typename Derived>
1953bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1954 TRY_TO(TraverseDeclTemplateParameterLists(D));
1955 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1956 if (D->getTypeSourceInfo())
1957 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1958 else
1959 TRY_TO(TraverseType(D->getType()));
1960 return true;
1961}
1962
1963DEF_TRAVERSE_DECL(DecompositionDecl, {
1964 TRY_TO(TraverseVarHelper(D));
1965 for (auto *Binding : D->bindings()) {
1966 TRY_TO(TraverseDecl(Binding));
1967 }
1968})
1969
1970DEF_TRAVERSE_DECL(BindingDecl, {
1971 if (getDerived().shouldVisitImplicitCode())
1972 TRY_TO(TraverseStmt(D->getBinding()));
1973})
1974
1975DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1976
1977DEF_TRAVERSE_DECL(MSGuidDecl, {})
1978
1979DEF_TRAVERSE_DECL(TemplateParamObjectDecl, {})
1980
1981DEF_TRAVERSE_DECL(FieldDecl, {
1982 TRY_TO(TraverseDeclaratorHelper(D));
1983 if (D->isBitField())
1984 TRY_TO(TraverseStmt(D->getBitWidth()));
1985 else if (D->hasInClassInitializer())
1986 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1987})
1988
1989DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1990 TRY_TO(TraverseDeclaratorHelper(D));
1991 if (D->isBitField())
1992 TRY_TO(TraverseStmt(D->getBitWidth()));
1993 // FIXME: implement the rest.
1994})
1995
1996DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1997 TRY_TO(TraverseDeclaratorHelper(D));
1998 if (D->isBitField())
1999 TRY_TO(TraverseStmt(D->getBitWidth()));
2000 // FIXME: implement the rest.
2001})
2002
2003template <typename Derived>
2004bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
2005 TRY_TO(TraverseDeclTemplateParameterLists(D));
2006 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
2007 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
2008
2009 // If we're an explicit template specialization, iterate over the
2010 // template args that were explicitly specified. If we were doing
2011 // this in typing order, we'd do it between the return type and
2012 // the function args, but both are handled by the FunctionTypeLoc
2013 // above, so we have to choose one side. I've decided to do before.
2014 if (const FunctionTemplateSpecializationInfo *FTSI =
2015 D->getTemplateSpecializationInfo()) {
2016 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
2017 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
2018 // A specialization might not have explicit template arguments if it has
2019 // a templated return type and concrete arguments.
2020 if (const ASTTemplateArgumentListInfo *TALI =
2021 FTSI->TemplateArgumentsAsWritten) {
2022 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
2023 TALI->NumTemplateArgs));
2024 }
2025 }
2026 }
2027
2028 // Visit the function type itself, which can be either
2029 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
2030 // also covers the return type and the function parameters,
2031 // including exception specifications.
2032 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
2033 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2034 } else if (getDerived().shouldVisitImplicitCode()) {
2035 // Visit parameter variable declarations of the implicit function
2036 // if the traverser is visiting implicit code. Parameter variable
2037 // declarations do not have valid TypeSourceInfo, so to visit them
2038 // we need to traverse the declarations explicitly.
2039 for (ParmVarDecl *Parameter : D->parameters()) {
2040 TRY_TO(TraverseDecl(Parameter));
2041 }
2042 }
2043
2044 // Visit the trailing requires clause, if any.
2045 if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
2046 TRY_TO(TraverseStmt(TrailingRequiresClause));
2047 }
2048
2049 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2050 // Constructor initializers.
2051 for (auto *I : Ctor->inits()) {
2052 if (I->isWritten() || getDerived().shouldVisitImplicitCode())
2053 TRY_TO(TraverseConstructorInitializer(I));
2054 }
2055 }
2056
2057 bool VisitBody =
2058 D->isThisDeclarationADefinition() &&
2059 // Don't visit the function body if the function definition is generated
2060 // by clang.
2061 (!D->isDefaulted() || getDerived().shouldVisitImplicitCode());
2062
2063 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2064 if (const CXXRecordDecl *RD = MD->getParent()) {
2065 if (RD->isLambda() &&
2066 declaresSameEntity(RD->getLambdaCallOperator(), MD)) {
2067 VisitBody = VisitBody && getDerived().shouldVisitLambdaBody();
2068 }
2069 }
2070 }
2071
2072 if (VisitBody) {
2073 TRY_TO(TraverseStmt(D->getBody())); // Function body.
2074 }
2075 return true;
2076}
2077
2078DEF_TRAVERSE_DECL(FunctionDecl, {
2079 // We skip decls_begin/decls_end, which are already covered by
2080 // TraverseFunctionHelper().
2081 ShouldVisitChildren = false;
2082 ReturnValue = TraverseFunctionHelper(D);
2083})
2084
2085DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
2086 // We skip decls_begin/decls_end, which are already covered by
2087 // TraverseFunctionHelper().
2088 ShouldVisitChildren = false;
2089 ReturnValue = TraverseFunctionHelper(D);
2090})
2091
2092DEF_TRAVERSE_DECL(CXXMethodDecl, {
2093 // We skip decls_begin/decls_end, which are already covered by
2094 // TraverseFunctionHelper().
2095 ShouldVisitChildren = false;
2096 ReturnValue = TraverseFunctionHelper(D);
2097})
2098
2099DEF_TRAVERSE_DECL(CXXConstructorDecl, {
2100 // We skip decls_begin/decls_end, which are already covered by
2101 // TraverseFunctionHelper().
2102 ShouldVisitChildren = false;
2103 ReturnValue = TraverseFunctionHelper(D);
2104})
2105
2106// CXXConversionDecl is the declaration of a type conversion operator.
2107// It's not a cast expression.
2108DEF_TRAVERSE_DECL(CXXConversionDecl, {
2109 // We skip decls_begin/decls_end, which are already covered by
2110 // TraverseFunctionHelper().
2111 ShouldVisitChildren = false;
2112 ReturnValue = TraverseFunctionHelper(D);
2113})
2114
2115DEF_TRAVERSE_DECL(CXXDestructorDecl, {
2116 // We skip decls_begin/decls_end, which are already covered by
2117 // TraverseFunctionHelper().
2118 ShouldVisitChildren = false;
2119 ReturnValue = TraverseFunctionHelper(D);
2120})
2121
2122template <typename Derived>
2123bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
2124 TRY_TO(TraverseDeclaratorHelper(D));
2125 // Default params are taken care of when we traverse the ParmVarDecl.
2126 if (!isa<ParmVarDecl>(D) &&
2127 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
2128 TRY_TO(TraverseStmt(D->getInit()));
2129 return true;
2130}
2131
2132DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
2133
2134DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
2135
2136DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
2137 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
2138 TRY_TO(TraverseDeclaratorHelper(D));
2139 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
2140 TRY_TO(TraverseStmt(D->getDefaultArgument()));
2141})
2142
2143DEF_TRAVERSE_DECL(ParmVarDecl, {
2144 TRY_TO(TraverseVarHelper(D));
2145
2146 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
2147 !D->hasUnparsedDefaultArg())
2148 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
2149
2150 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
2151 !D->hasUnparsedDefaultArg())
2152 TRY_TO(TraverseStmt(D->getDefaultArg()));
2153})
2154
2155DEF_TRAVERSE_DECL(RequiresExprBodyDecl, {})
2156
2157#undef DEF_TRAVERSE_DECL
2158
2159// ----------------- Stmt traversal -----------------
2160//
2161// For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
2162// over the children defined in children() (every stmt defines these,
2163// though sometimes the range is empty). Each individual Traverse*
2164// method only needs to worry about children other than those. To see
2165// what children() does for a given class, see, e.g.,
2166// http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
2167
2168// This macro makes available a variable S, the passed-in stmt.
2169#define DEF_TRAVERSE_STMT(STMT, CODE) \
2170 template <typename Derived> \
2171 bool RecursiveASTVisitor<Derived>::Traverse##STMT( \
2172 STMT *S, DataRecursionQueue *Queue) { \
2173 bool