Clang Project

clang_source_code/include/clang/AST/RecursiveASTVisitor.h
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/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"
46#include <algorithm>
47#include <cstddef>
48#include <type_traits>
49
50// The following three macros are used for meta programming.  The code
51// using them is responsible for defining macro OPERATOR().
52
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)
59
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)
67
68// All compound assign operators.
69#define CAO_LIST()                                                             \
70  OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub)        \
71      OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
72
73namespace clang {
74
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
78// in CALL_EXPR).
79#define TRY_TO(CALL_EXPR)                                                      \
80  do {                                                                         \
81    if (!getDerived().CALL_EXPR)                                               \
82      return false;                                                            \
83  } while (false)
84
85/// A class that does preorder or postorder
86/// depth-first traversal on the entire Clang AST and visits each node.
87///
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.
96///
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
104///      similarly.
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.
110///
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.
115///
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()).
121///
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.
125///
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.
135///
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.
146///
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.
150template <typename Derived> class RecursiveASTVisitor {
151public:
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 *, 1bool>>
158    DataRecursionQueue;
159
160  /// Return a reference to the derived class.
161  Derived &getDerived() { return *static_cast<Derived *>(this); }
162
163  /// Return whether this visitor should recurse into
164  /// template instantiations.
165  bool shouldVisitTemplateInstantiations() const { return false; }
166
167  /// Return whether this visitor should recurse into the types of
168  /// TypeLocs.
169  bool shouldWalkTypesOfTypeLocs() const { return true; }
170
171  /// Return whether this visitor should recurse into implicit
172  /// code, e.g., implicit constructors and destructors.
173  bool shouldVisitImplicitCode() const { return false; }
174
175  /// Return whether this visitor should traverse post-order.
176  bool shouldTraversePostOrder() const { return false; }
177
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))
184        return false;
185    return true;
186  }
187
188  /// Recursively visit a statement or expression, by
189  /// dispatching to Traverse*() based on the argument's dynamic type.
190  ///
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);
194
195  /// Invoked before visiting a statement or expression via data recursion.
196  ///
197  /// \returns false to skip visiting the node, true otherwise.
198  bool dataTraverseStmtPre(Stmt *S) { return true; }
199
200  /// Invoked after visiting a statement or expression via data recursion.
201  /// This is not invoked if the previously invoked \c dataTraverseStmtPre
202  /// returned false.
203  ///
204  /// \returns false if the visitation was terminated early, true otherwise.
205  bool dataTraverseStmtPost(Stmt *S) { return true; }
206
207  /// Recursively visit a type, by dispatching to
208  /// Traverse*Type() based on the argument's getTypeClass() property.
209  ///
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);
213
214  /// Recursively visit a type with location, by dispatching to
215  /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
216  ///
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);
220
221  /// Recursively visit an attribute, by dispatching to
222  /// Traverse*Attr() based on the argument's dynamic type.
223  ///
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);
227
228  /// Recursively visit a declaration, by dispatching to
229  /// Traverse*Decl() based on the argument's dynamic type.
230  ///
231  /// \returns false if the visitation was terminated early, true
232  /// otherwise (including when the argument is NULL).
233  bool TraverseDecl(Decl *D);
234
235  /// Recursively visit a C++ nested-name-specifier.
236  ///
237  /// \returns false if the visitation was terminated early, true otherwise.
238  bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
239
240  /// Recursively visit a C++ nested-name-specifier with location
241  /// information.
242  ///
243  /// \returns false if the visitation was terminated early, true otherwise.
244  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
245
246  /// Recursively visit a name with its location information.
247  ///
248  /// \returns false if the visitation was terminated early, true otherwise.
249  bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
250
251  /// Recursively visit a template name and dispatch to the
252  /// appropriate method.
253  ///
254  /// \returns false if the visitation was terminated early, true otherwise.
255  bool TraverseTemplateName(TemplateName Template);
256
257  /// Recursively visit a template argument and dispatch to the
258  /// appropriate method for the argument type.
259  ///
260  /// \returns false if the visitation was terminated early, true otherwise.
261  // FIXME: migrate callers to TemplateArgumentLoc instead.
262  bool TraverseTemplateArgument(const TemplateArgument &Arg);
263
264  /// Recursively visit a template argument location and dispatch to the
265  /// appropriate method for the argument type.
266  ///
267  /// \returns false if the visitation was terminated early, true otherwise.
268  bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
269
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.
273  ///
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,
277                                 unsigned NumArgs);
278
279  /// Recursively visit a base specifier. This can be overridden by a
280  /// subclass.
281  ///
282  /// \returns false if the visitation was terminated early, true otherwise.
283  bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base);
284
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.
289  ///
290  /// \returns false if the visitation was terminated early, true otherwise.
291  bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
292
293  /// Recursively visit a lambda capture. \c Init is the expression that
294  /// will be used to initialize the capture.
295  ///
296  /// \returns false if the visitation was terminated early, true otherwise.
297  bool TraverseLambdaCapture(LambdaExpr *LEconst LambdaCapture *C,
298                             Expr *Init);
299
300  /// Recursively visit the syntactic or semantic form of an
301  /// initialization list.
302  ///
303  /// \returns false if the visitation was terminated early, true otherwise.
304  bool TraverseSynOrSemInitListExpr(InitListExpr *S,
305                                    DataRecursionQueue *Queue = nullptr);
306
307  // ---- Methods on Attrs ----
308
309  // Visit an attribute.
310  bool VisitAttr(Attr *A) { return true; }
311
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
316
317// ---- Methods on Stmts ----
318
319  Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); }
320
321private:
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...)>
326      : std::true_type {};
327
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)))
344
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)                                     \
349  do {                                                                         \
350    if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue))                             \
351      return false;                                                            \
352  } while (false)
353
354public:
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.
361
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));                                                   \
369    return true;                                                               \
370  }                                                                            \
371  bool Visit##CLASS(CLASS *S) { return true; }
372#include "clang/AST/StmtNodes.inc"
373
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());                          \
383    return true;                                                               \
384  }                                                                            \
385  bool WalkUpFromUnary##NAME(UnaryOperator *S) {                               \
386    TRY_TO(WalkUpFromUnaryOperator(S));                                        \
387    TRY_TO(VisitUnary##NAME(S));                                               \
388    return true;                                                               \
389  }                                                                            \
390  bool VisitUnary##NAME(UnaryOperator *S) { return true; }
391
392  UNARYOP_LIST()
393#undef OPERATOR
394
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());                              \
404    return true;                                                               \
405  }                                                                            \
406  bool WalkUpFromBin##NAME(BINOP_TYPE *S) {                                    \
407    TRY_TO(WalkUpFrom##BINOP_TYPE(S));                                         \
408    TRY_TO(VisitBin##NAME(S));                                                 \
409    return true;                                                               \
410  }                                                                            \
411  bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
412
413#define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
414  BINOP_LIST()
415#undef OPERATOR
416
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)
423
424  CAO_LIST()
425#undef OPERATOR
426#undef GENERAL_BINOP_FALLBACK
427
428// ---- Methods on Types ----
429// FIXME: revamp to take TypeLoc's rather than Types.
430
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.def"
435  // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
436
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));                                             \
444    return true;                                                               \
445  }                                                                            \
446  bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
447#include "clang/AST/TypeNodes.def"
448
449// ---- Methods on TypeLocs ----
450// FIXME: this currently just calls the matching Type methods
451
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.
457
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; }
461
462  // QualifiedTypeLoc and UnqualTypeLoc are not declared in
463  // TypeNodes.def and thus need to be handled specially.
464  bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
465    return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
466  }
467  bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
468  bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
469    return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
470  }
471  bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
472
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));                                         \
478    return true;                                                               \
479  }                                                                            \
480  bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
481#include "clang/AST/TypeNodes.def"
482
483// ---- Methods on Decls ----
484
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.
490
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));                                             \
498    return true;                                                               \
499  }                                                                            \
500  bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
501#include "clang/AST/DeclNodes.inc"
502
503  bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
504
505private:
506  // These are helper methods used by more than one Traverse* method.
507  bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
508
509  // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
510  template <typename T>
511  bool TraverseDeclTemplateParameterLists(T *D);
512
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,
520                                          unsigned Count);
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);
538
539  bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
540  bool PostVisitStmt(Stmt *S);
541};
542
543template <typename Derived>
544bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
545                                                    DataRecursionQueue *Queue) {
546#define DISPATCH_STMT(NAME, CLASS, VAR)                                        \
547  return TRAVERSE_STMT_BASE(NAME, CLASS, VAR, Queue);
548
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
551  // below.
552  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
553    switch (BinOp->getOpcode()) {
554#define OPERATOR(NAME)                                                         \
555  case BO_##NAME:                                                              \
556    DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
557
558      BINOP_LIST()
559#undef OPERATOR
560#undef BINOP_LIST
561
562#define OPERATOR(NAME)                                                         \
563  case BO_##NAME##Assign:                                                      \
564    DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
565
566      CAO_LIST()
567#undef OPERATOR
568#undef CAO_LIST
569    }
570  } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
571    switch (UnOp->getOpcode()) {
572#define OPERATOR(NAME)                                                         \
573  case UO_##NAME:                                                              \
574    DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
575
576      UNARYOP_LIST()
577#undef OPERATOR
578#undef UNARYOP_LIST
579    }
580  }
581
582  // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
583  switch (S->getStmtClass()) {
584  case Stmt::NoStmtClass:
585    break;
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"
591  }
592
593  return true;
594}
595
596#undef DISPATCH_STMT
597
598template <typename Derived>
599bool RecursiveASTVisitor<Derived>::PostVisitStmt(Stmt *S) {
600  switch (S->getStmtClass()) {
601  case Stmt::NoStmtClass:
602    break;
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:                                                     \
609    {                                                                          \
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));                                        \
615      break;                                                                   \
616    }
617#include "clang/AST/StmtNodes.inc"
618  }
619
620  return true;
621}
622
623#undef DISPATCH_STMT
624
625template <typename Derived>
626bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S,
627                                                DataRecursionQueue *Queue) {
628  if (!S)
629    return true;
630
631  if (Queue) {
632    Queue->push_back({S, false});
633    return true;
634  }
635
636  SmallVector<llvm::PointerIntPair<Stmt *, 1bool>, 8> LocalQueue;
637  LocalQueue.push_back({S, false});
638
639  while (!LocalQueue.empty()) {
640    auto &CurrSAndVisited = LocalQueue.back();
641    Stmt *CurrS = CurrSAndVisited.getPointer();
642    bool Visited = CurrSAndVisited.getInt();
643    if (Visited) {
644      LocalQueue.pop_back();
645      TRY_TO(dataTraverseStmtPost(CurrS));
646      if (getDerived().shouldTraversePostOrder()) {
647        TRY_TO(PostVisitStmt(CurrS));
648      }
649      continue;
650    }
651
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());
658    } else {
659      LocalQueue.pop_back();
660    }
661  }
662
663  return true;
664}
665
666#define DISPATCH(NAME, CLASS, VAR)                                             \
667  return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))
668
669template <typename Derived>
670bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
671  if (T.isNull())
672    return true;
673
674  switch (T->getTypeClass()) {
675#define ABSTRACT_TYPE(CLASS, BASE)
676#define TYPE(CLASS, BASE)                                                      \
677  case Type::CLASS:                                                            \
678    DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr()));
679#include "clang/AST/TypeNodes.def"
680  }
681
682  return true;
683}
684
685template <typename Derived>
686bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
687  if (TL.isNull())
688    return true;
689
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"
696  }
697
698  return true;
699}
700
701// Define the Traverse*Attr(Attr* A) methods
702#define VISITORCLASS RecursiveASTVisitor
703#include "clang/AST/AttrVisitor.inc"
704#undef VISITORCLASS
705
706template <typename Derived>
707bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
708  if (!D)
709    return true;
710
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())
714    return true;
715
716  switch (D->getKind()) {
717#define ABSTRACT_DECL(DECL)
718#define DECL(CLASS, BASE)                                                      \
719  case Decl::CLASS:                                                            \
720    if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D)))    \
721      return false;                                                            \
722    break;
723#include "clang/AST/DeclNodes.inc"
724  }
725
726  // Visit any attributes attached to this declaration.
727  for (auto *I : D->attrs()) {
728    if (!getDerived().TraverseAttr(I))
729      return false;
730  }
731  return true;
732}
733
734#undef DISPATCH
735
736template <typename Derived>
737bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
738    NestedNameSpecifier *NNS) {
739  if (!NNS)
740    return true;
741
742  if (NNS->getPrefix())
743    TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
744
745  switch (NNS->getKind()) {
746  case NestedNameSpecifier::Identifier:
747  case NestedNameSpecifier::Namespace:
748  case NestedNameSpecifier::NamespaceAlias:
749  case NestedNameSpecifier::Global:
750  case NestedNameSpecifier::Super:
751    return true;
752
753  case NestedNameSpecifier::TypeSpec:
754  case NestedNameSpecifier::TypeSpecWithTemplate:
755    TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
756  }
757
758  return true;
759}
760
761template <typename Derived>
762bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
763    NestedNameSpecifierLoc NNS) {
764  if (!NNS)
765    return true;
766
767  if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
768    TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
769
770  switch (NNS.getNestedNameSpecifier()->getKind()) {
771  case NestedNameSpecifier::Identifier:
772  case NestedNameSpecifier::Namespace:
773  case NestedNameSpecifier::NamespaceAlias:
774  case NestedNameSpecifier::Global:
775  case NestedNameSpecifier::Super:
776    return true;
777
778  case NestedNameSpecifier::TypeSpec:
779  case NestedNameSpecifier::TypeSpecWithTemplate:
780    TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
781    break;
782  }
783
784  return true;
785}
786
787template <typename Derived>
788bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
789    DeclarationNameInfo NameInfo) {
790  switch (NameInfo.getName().getNameKind()) {
791  case DeclarationName::CXXConstructorName:
792  case DeclarationName::CXXDestructorName:
793  case DeclarationName::CXXConversionFunctionName:
794    if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
795      TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
796    break;
797
798  case DeclarationName::CXXDeductionGuideName:
799    TRY_TO(TraverseTemplateName(
800        TemplateName(NameInfo.getName().getCXXDeductionGuideTemplate())));
801    break;
802
803  case DeclarationName::Identifier:
804  case DeclarationName::ObjCZeroArgSelector:
805  case DeclarationName::ObjCOneArgSelector:
806  case DeclarationName::ObjCMultiArgSelector:
807  case DeclarationName::CXXOperatorName:
808  case DeclarationName::CXXLiteralOperatorName:
809  case DeclarationName::CXXUsingDirective:
810    break;
811  }
812
813  return true;
814}
815
816template <typename Derived>
817bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
818  if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
819    TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
820  else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
821    TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
822
823  return true;
824}
825
826template <typename Derived>
827bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
828    const TemplateArgument &Arg) {
829  switch (Arg.getKind()) {
830  case TemplateArgument::Null:
831  case TemplateArgument::Declaration:
832  case TemplateArgument::Integral:
833  case TemplateArgument::NullPtr:
834    return true;
835
836  case TemplateArgument::Type:
837    return getDerived().TraverseType(Arg.getAsType());
838
839  case TemplateArgument::Template:
840  case TemplateArgument::TemplateExpansion:
841    return getDerived().TraverseTemplateName(
842        Arg.getAsTemplateOrTemplatePattern());
843
844  case TemplateArgument::Expression:
845    return getDerived().TraverseStmt(Arg.getAsExpr());
846
847  case TemplateArgument::Pack:
848    return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
849                                                  Arg.pack_size());
850  }
851
852  return true;
853}
854
855// FIXME: no template name location?
856// FIXME: no source locations for a template argument pack?
857template <typename Derived>
858bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
859    const TemplateArgumentLoc &ArgLoc) {
860  const TemplateArgument &Arg = ArgLoc.getArgument();
861
862  switch (Arg.getKind()) {
863  case TemplateArgument::Null:
864  case TemplateArgument::Declaration:
865  case TemplateArgument::Integral:
866  case TemplateArgument::NullPtr:
867    return true;
868
869  case TemplateArgument::Type: {
870    // FIXME: how can TSI ever be NULL?
871    if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
872      return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
873    else
874      return getDerived().TraverseType(Arg.getAsType());
875  }
876
877  case TemplateArgument::Template:
878  case TemplateArgument::TemplateExpansion:
879    if (ArgLoc.getTemplateQualifierLoc())
880      TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
881          ArgLoc.getTemplateQualifierLoc()));
882    return getDerived().TraverseTemplateName(
883        Arg.getAsTemplateOrTemplatePattern());
884
885  case TemplateArgument::Expression:
886    return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
887
888  case TemplateArgument::Pack:
889    return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
890                                                  Arg.pack_size());
891  }
892
893  return true;
894}
895
896template <typename Derived>
897bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
898    const TemplateArgument *Argsunsigned NumArgs) {
899  for (unsigned I = 0I != NumArgs; ++I) {
900    TRY_TO(TraverseTemplateArgument(Args[I]));
901  }
902
903  return true;
904}
905
906template <typename Derived>
907bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
908    CXXCtorInitializer *Init) {
909  if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
910    TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
911
912  if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
913    TRY_TO(TraverseStmt(Init->getInit()));
914
915  return true;
916}
917
918template <typename Derived>
919bool
920RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
921                                                    const LambdaCapture *C,
922                                                    Expr *Init) {
923  if (LE->isInitCapture(C))
924    TRY_TO(TraverseDecl(C->getCapturedVar()));
925  else
926    TRY_TO(TraverseStmt(Init));
927  return true;
928}
929
930// ----------------- Type traversal -----------------
931
932// This macro makes available a variable T, the passed-in type.
933#define DEF_TRAVERSE_TYPE(TYPE, CODE)                                          \
934  template <typename Derived>                                                  \
935  bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) {                 \
936    if (!getDerived().shouldTraversePostOrder())                               \
937      TRY_TO(WalkUpFrom##TYPE(T));                                             \
938    { CODE; }                                                                  \
939    if (getDerived().shouldTraversePostOrder())                                \
940      TRY_TO(WalkUpFrom##TYPE(T));                                             \
941    return true;                                                               \
942  }
943
944DEF_TRAVERSE_TYPE(BuiltinType, {})
945
946DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })
947
948DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
949
950DEF_TRAVERSE_TYPE(BlockPointerType,
951                  { TRY_TO(TraverseType(T->getPointeeType())); })
952
953DEF_TRAVERSE_TYPE(LValueReferenceType,
954                  { TRY_TO(TraverseType(T->getPointeeType())); })
955
956DEF_TRAVERSE_TYPE(RValueReferenceType,
957                  { TRY_TO(TraverseType(T->getPointeeType())); })
958
959DEF_TRAVERSE_TYPE(MemberPointerType, {
960  TRY_TO(TraverseType(QualType(T->getClass(), 0)));
961  TRY_TO(TraverseType(T->getPointeeType()));
962})
963
964DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); })
965
966DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); })
967
968DEF_TRAVERSE_TYPE(ConstantArrayType,
969                  { TRY_TO(TraverseType(T->getElementType())); })
970
971DEF_TRAVERSE_TYPE(IncompleteArrayType,
972                  { TRY_TO(TraverseType(T->getElementType())); })
973
974DEF_TRAVERSE_TYPE(VariableArrayType, {
975  TRY_TO(TraverseType(T->getElementType()));
976  TRY_TO(TraverseStmt(T->getSizeExpr()));
977})
978
979DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
980  TRY_TO(TraverseType(T->getElementType()));
981  if (T->getSizeExpr())
982    TRY_TO(TraverseStmt(T->getSizeExpr()));
983})
984
985DEF_TRAVERSE_TYPE(DependentAddressSpaceType, {
986  TRY_TO(TraverseStmt(T->getAddrSpaceExpr()));
987  TRY_TO(TraverseType(T->getPointeeType()));
988})
989
990DEF_TRAVERSE_TYPE(DependentVectorType, {
991  if (T->getSizeExpr())
992    TRY_TO(TraverseStmt(T->getSizeExpr()));
993  TRY_TO(TraverseType(T->getElementType()));
994})
995
996DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
997  if (T->getSizeExpr())
998    TRY_TO(TraverseStmt(T->getSizeExpr()));
999  TRY_TO(TraverseType(T->getElementType()));
1000})
1001
1002DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); })
1003
1004DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); })
1005
1006DEF_TRAVERSE_TYPE(FunctionNoProtoType,
1007                  { TRY_TO(TraverseType(T->getReturnType())); })
1008
1009DEF_TRAVERSE_TYPE(FunctionProtoType, {
1010  TRY_TO(TraverseType(T->getReturnType()));
1011
1012  for (const auto &A : T->param_types()) {
1013    TRY_TO(TraverseType(A));
1014  }
1015
1016  for (const auto &E : T->exceptions()) {
1017    TRY_TO(TraverseType(E));
1018  }
1019
1020  if (Expr *NE = T->getNoexceptExpr())
1021    TRY_TO(TraverseStmt(NE));
1022})
1023
1024DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
1025DEF_TRAVERSE_TYPE(TypedefType, {})
1026
1027DEF_TRAVERSE_TYPE(TypeOfExprType,
1028                  { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1029
1030DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); })
1031
1032DEF_TRAVERSE_TYPE(DecltypeType,
1033                  { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); })
1034
1035DEF_TRAVERSE_TYPE(UnaryTransformType, {
1036  TRY_TO(TraverseType(T->getBaseType()));
1037  TRY_TO(TraverseType(T->getUnderlyingType()));
1038})
1039
1040DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); })
1041DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, {
1042  TRY_TO(TraverseTemplateName(T->getTemplateName()));
1043  TRY_TO(TraverseType(T->getDeducedType()));
1044})
1045
1046DEF_TRAVERSE_TYPE(RecordType, {})
1047DEF_TRAVERSE_TYPE(EnumType, {})
1048DEF_TRAVERSE_TYPE(TemplateTypeParmType, {})
1049DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {
1050  TRY_TO(TraverseType(T->getReplacementType()));
1051})
1052DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {
1053  TRY_TO(TraverseTemplateArgument(T->getArgumentPack()));
1054})
1055
1056DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
1057  TRY_TO(TraverseTemplateName(T->getTemplateName()));
1058  TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1059})
1060
1061DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
1062
1063DEF_TRAVERSE_TYPE(AttributedType,
1064                  { TRY_TO(TraverseType(T->getModifiedType())); })
1065
1066DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
1067
1068DEF_TRAVERSE_TYPE(ElaboratedType, {
1069  if (T->getQualifier()) {
1070    TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1071  }
1072  TRY_TO(TraverseType(T->getNamedType()));
1073})
1074
1075DEF_TRAVERSE_TYPE(DependentNameType,
1076                  { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
1077
1078DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1079  TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1080  TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
1081})
1082
1083DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); })
1084
1085DEF_TRAVERSE_TYPE(ObjCTypeParamType, {})
1086
1087DEF_TRAVERSE_TYPE(ObjCInterfaceType, {})
1088
1089DEF_TRAVERSE_TYPE(ObjCObjectType, {
1090  // We have to watch out here because an ObjCInterfaceType's base
1091  // type is itself.
1092  if (T->getBaseType().getTypePtr() != T)
1093    TRY_TO(TraverseType(T->getBaseType()));
1094  for (auto typeArg : T->getTypeArgsAsWritten()) {
1095    TRY_TO(TraverseType(typeArg));
1096  }
1097})
1098
1099DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
1100                  { TRY_TO(TraverseType(T->getPointeeType())); })
1101
1102DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); })
1103
1104DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
1105
1106#undef DEF_TRAVERSE_TYPE
1107
1108// ----------------- TypeLoc traversal -----------------
1109
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));                                         \
1121    { CODE; }                                                                  \
1122    return true;                                                               \
1123  }
1124
1125template <typename Derived>
1126bool
1127RecursiveASTVisitor<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
1142  // type hierarchy.
1143  return TraverseTypeLoc(TL.getUnqualifiedLoc());
1144}
1145
1146DEF_TRAVERSE_TYPELOC(BuiltinType, {})
1147
1148// FIXME: ComplexTypeLoc is unfinished
1149DEF_TRAVERSE_TYPELOC(ComplexType, {
1150  TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1151})
1152
1153DEF_TRAVERSE_TYPELOC(PointerType,
1154                     { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1155
1156DEF_TRAVERSE_TYPELOC(BlockPointerType,
1157                     { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1158
1159DEF_TRAVERSE_TYPELOC(LValueReferenceType,
1160                     { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1161
1162DEF_TRAVERSE_TYPELOC(RValueReferenceType,
1163                     { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1164
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?
1168DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1169  TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1170  TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1171})
1172
1173DEF_TRAVERSE_TYPELOC(AdjustedType,
1174                     { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1175
1176DEF_TRAVERSE_TYPELOC(DecayedType,
1177                     { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); })
1178
1179template <typename Derived>
1180bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1181  // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1182  TRY_TO(TraverseStmt(TL.getSizeExpr()));
1183  return true;
1184}
1185
1186DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1187  TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1188  return TraverseArrayTypeLocHelper(TL);
1189})
1190
1191DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1192  TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1193  return TraverseArrayTypeLocHelper(TL);
1194})
1195
1196DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1197  TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1198  return TraverseArrayTypeLocHelper(TL);
1199})
1200
1201DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1202  TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1203  return TraverseArrayTypeLocHelper(TL);
1204})
1205
1206DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
1207  TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr()));
1208  TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType()));
1209})
1210
1211// FIXME: order? why not size expr first?
1212// FIXME: base VectorTypeLoc is unfinished
1213DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1214  if (TL.getTypePtr()->getSizeExpr())
1215    TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1216  TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1217})
1218
1219// FIXME: VectorTypeLoc is unfinished
1220DEF_TRAVERSE_TYPELOC(VectorType, {
1221  TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1222})
1223
1224DEF_TRAVERSE_TYPELOC(DependentVectorType, {
1225  if (TL.getTypePtr()->getSizeExpr())
1226    TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1227  TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1228})
1229
1230// FIXME: size and attributes
1231// FIXME: base VectorTypeLoc is unfinished
1232DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1233  TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1234})
1235
1236DEF_TRAVERSE_TYPELOC(FunctionNoProtoType,
1237                     { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); })
1238
1239// FIXME: location of exception specifications (attributes?)
1240DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1241  TRY_TO(TraverseTypeLoc(TL.getReturnLoc()));
1242
1243  const FunctionProtoType *T = TL.getTypePtr();
1244
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)));
1250    }
1251  }
1252
1253  for (const auto &E : T->exceptions()) {
1254    TRY_TO(TraverseType(E));
1255  }
1256
1257  if (Expr *NE = T->getNoexceptExpr())
1258    TRY_TO(TraverseStmt(NE));
1259})
1260
1261DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
1262DEF_TRAVERSE_TYPELOC(TypedefType, {})
1263
1264DEF_TRAVERSE_TYPELOC(TypeOfExprType,
1265                     { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); })
1266
1267DEF_TRAVERSE_TYPELOC(TypeOfType, {
1268  TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1269})
1270
1271// FIXME: location of underlying expr
1272DEF_TRAVERSE_TYPELOC(DecltypeType, {
1273  TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1274})
1275
1276DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1277  TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1278})
1279
1280DEF_TRAVERSE_TYPELOC(AutoType, {
1281  TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1282})
1283
1284DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, {
1285  TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1286  TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1287})
1288
1289DEF_TRAVERSE_TYPELOC(RecordType, {})
1290DEF_TRAVERSE_TYPELOC(EnumType, {})
1291DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {})
1292DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {
1293  TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType()));
1294})
1295DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {
1296  TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack()));
1297})
1298
1299// FIXME: use the loc for the template name?
1300DEF_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)));
1304  }
1305})
1306
1307DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {})
1308
1309DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); })
1310
1311DEF_TRAVERSE_TYPELOC(AttributedType,
1312                     { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
1313
1314DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1315  if (TL.getQualifierLoc()) {
1316    TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1317  }
1318  TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1319})
1320
1321DEF_TRAVERSE_TYPELOC(DependentNameType, {
1322  TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1323})
1324
1325DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1326  if (TL.getQualifierLoc()) {
1327    TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1328  }
1329
1330  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1331    TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1332  }
1333})
1334
1335DEF_TRAVERSE_TYPELOC(PackExpansionType,
1336                     { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); })
1337
1338DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {})
1339
1340DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {})
1341
1342DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1343  // We have to watch out here because an ObjCInterfaceType's base
1344  // type is itself.
1345  if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1346    TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1347  for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
1348    TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
1349})
1350
1351DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
1352                     { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); })
1353
1354DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1355
1356DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); })
1357
1358#undef DEF_TRAVERSE_TYPELOC
1359
1360// ----------------- Decl traversal -----------------
1361//
1362// For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1363// the children that come from the DeclContext associated with it.
1364// Therefore each Traverse* only needs to worry about children other
1365// than those.
1366
1367template <typename Derived>
1368bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext(
1369    const Decl *Child) {
1370  // BlockDecls are traversed through BlockExprs,
1371  // CapturedDecls are traversed through CapturedStmts.
1372  if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child))
1373    return true;
1374  // Lambda classes are traversed through LambdaExprs.
1375  if (const CXXRecordDeclCls = dyn_cast<CXXRecordDecl>(Child))
1376    return Cls->isLambda();
1377  return false;
1378}
1379
1380template <typename Derived>
1381bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1382  if (!DC)
1383    return true;
1384
1385  for (auto *Child : DC->decls()) {
1386    if (!canIgnoreChildDeclWhileTraversingDeclContext(Child))
1387      TRY_TO(TraverseDecl(Child));
1388  }
1389
1390  return true;
1391}
1392
1393// This macro makes available a variable D, the passed-in decl.
1394#define DEF_TRAVERSE_DECL(DECL, CODE)                                          \
1395  template <typename Derived>                                                  \
1396  bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) {                 \
1397    bool ShouldVisitChildren = true;                                           \
1398    bool ReturnValue = true;                                                   \
1399    if (!getDerived().shouldTraversePostOrder())                               \
1400      TRY_TO(WalkUpFrom##DECL(D));                                             \
1401    { CODE; }                                                                  \
1402    if (ReturnValue && ShouldVisitChildren)                                    \
1403      TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));             \
1404    if (ReturnValue && getDerived().shouldTraversePostOrder())                 \
1405      TRY_TO(WalkUpFrom##DECL(D));                                             \
1406    return ReturnValue;                                                        \
1407  }
1408
1409DEF_TRAVERSE_DECL(AccessSpecDecl, {})
1410
1411DEF_TRAVERSE_DECL(BlockDecl, {
1412  if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1413    TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1414  TRY_TO(TraverseStmt(D->getBody()));
1415  for (const auto &I : D->captures()) {
1416    if (I.hasCopyExpr()) {
1417      TRY_TO(TraverseStmt(I.getCopyExpr()));
1418    }
1419  }
1420  ShouldVisitChildren = false;
1421})
1422
1423DEF_TRAVERSE_DECL(CapturedDecl, {
1424  TRY_TO(TraverseStmt(D->getBody()));
1425  ShouldVisitChildren = false;
1426})
1427
1428DEF_TRAVERSE_DECL(EmptyDecl, {})
1429
1430DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1431                  { TRY_TO(TraverseStmt(D->getAsmString())); })
1432
1433DEF_TRAVERSE_DECL(ImportDecl, {})
1434
1435DEF_TRAVERSE_DECL(FriendDecl, {
1436  // Friend is either decl or a type.
1437  if (D->getFriendType())
1438    TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1439  else
1440    TRY_TO(TraverseDecl(D->getFriendDecl()));
1441})
1442
1443DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1444  if (D->getFriendType())
1445    TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1446  else
1447    TRY_TO(TraverseDecl(D->getFriendDecl()));
1448  for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1449    TemplateParameterList *TPL = D->getTemplateParameterList(I);
1450    for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end();
1451         ITPL != ETPL; ++ITPL) {
1452      TRY_TO(TraverseDecl(*ITPL));
1453    }
1454  }
1455})
1456
1457DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1458  TRY_TO(TraverseDecl(D->getSpecialization()));
1459
1460  if (D->hasExplicitTemplateArgs()) {
1461    const TemplateArgumentListInfo &args = D->templateArgs();
1462    TRY_TO(TraverseTemplateArgumentLocsHelper(args.getArgumentArray(),
1463                                              args.size()));
1464  }
1465})
1466
1467DEF_TRAVERSE_DECL(LinkageSpecDecl, {})
1468
1469DEF_TRAVERSE_DECL(ExportDecl, {})
1470
1471DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this
1472                                        })
1473
1474DEF_TRAVERSE_DECL(StaticAssertDecl, {
1475  TRY_TO(TraverseStmt(D->getAssertExpr()));
1476  TRY_TO(TraverseStmt(D->getMessage()));
1477})
1478
1479DEF_TRAVERSE_DECL(
1480    TranslationUnitDecl,
1481    {// Code in an unnamed namespace shows up automatically in
1482     // decls_begin()/decls_end().  Thus we don't need to recurse on
1483     // D->getAnonymousNamespace().
1484    })
1485
1486DEF_TRAVERSE_DECL(PragmaCommentDecl, {})
1487
1488DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {})
1489
1490DEF_TRAVERSE_DECL(ExternCContextDecl, {})
1491
1492DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1493  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1494
1495  // We shouldn't traverse an aliased namespace, since it will be
1496  // defined (and, therefore, traversed) somewhere else.
1497  ShouldVisitChildren = false;
1498})
1499
1500DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl.
1501                             })
1502
1503DEF_TRAVERSE_DECL(
1504    NamespaceDecl,
1505    {// Code in an unnamed namespace shows up automatically in
1506     // decls_begin()/decls_end().  Thus we don't need to recurse on
1507     // D->getAnonymousNamespace().
1508    })
1509
1510DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
1511                                           })
1512
1513DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
1514  if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
1515    for (auto typeParam : *typeParamList) {
1516      TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1517    }
1518  }
1519})
1520
1521DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
1522                                        })
1523
1524DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
1525                                          })
1526
1527DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
1528  if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
1529    for (auto typeParam : *typeParamList) {
1530      TRY_TO(TraverseObjCTypeParamDecl(typeParam));
1531    }
1532  }
1533
1534  if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
1535    TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
1536  }
1537})
1538
1539DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
1540                                    })
1541
1542DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1543  if (D->getReturnTypeSourceInfo()) {
1544    TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc()));
1545  }
1546  for (ParmVarDecl *Parameter : D->parameters()) {
1547    TRY_TO(TraverseDecl(Parameter));
1548  }
1549  if (D->isThisDeclarationADefinition()) {
1550    TRY_TO(TraverseStmt(D->getBody()));
1551  }
1552  ShouldVisitChildren = false;
1553})
1554
1555DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
1556  if (D->hasExplicitBound()) {
1557    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1558    // We shouldn't traverse D->getTypeForDecl(); it's a result of
1559    // declaring the type alias, not something that was written in the
1560    // source.
1561  }
1562})
1563
1564DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1565  if (D->getTypeSourceInfo())
1566    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1567  else
1568    TRY_TO(TraverseType(D->getType()));
1569  ShouldVisitChildren = false;
1570})
1571
1572DEF_TRAVERSE_DECL(UsingDecl, {
1573  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1574  TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1575})
1576
1577DEF_TRAVERSE_DECL(UsingPackDecl, {})
1578
1579DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1580  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1581})
1582
1583DEF_TRAVERSE_DECL(UsingShadowDecl, {})
1584
1585DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {})
1586
1587DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1588  for (auto *I : D->varlists()) {
1589    TRY_TO(TraverseStmt(I));
1590  }
1591 })
1592
1593DEF_TRAVERSE_DECL(OMPRequiresDecl, {
1594  for (auto *C : D->clauselists()) {
1595    TRY_TO(TraverseOMPClause(C));
1596  }
1597})
1598
1599DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, {
1600  TRY_TO(TraverseStmt(D->getCombiner()));
1601  if (auto *Initializer = D->getInitializer())
1602    TRY_TO(TraverseStmt(Initializer));
1603  TRY_TO(TraverseType(D->getType()));
1604  return true;
1605})
1606
1607DEF_TRAVERSE_DECL(OMPDeclareMapperDecl, {
1608  for (auto *C : D->clauselists())
1609    TRY_TO(TraverseOMPClause(C));
1610  TRY_TO(TraverseType(D->getType()));
1611  return true;
1612})
1613
1614DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
1615
1616DEF_TRAVERSE_DECL(OMPAllocateDecl, {
1617  for (auto *I : D->varlists())
1618    TRY_TO(TraverseStmt(I));
1619  for (auto *C : D->clauselists())
1620    TRY_TO(TraverseOMPClause(C));
1621})
1622
1623// A helper method for TemplateDecl's children.
1624template <typename Derived>
1625bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1626    TemplateParameterList *TPL) {
1627  if (TPL) {
1628    for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1629         I != E; ++I) {
1630      TRY_TO(TraverseDecl(*I));
1631    }
1632  }
1633  return true;
1634}
1635
1636template <typename Derived>
1637template <typename T>
1638bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) {
1639  for (unsigned i = 0i < D->getNumTemplateParameterLists(); i++) {
1640    TemplateParameterList *TPL = D->getTemplateParameterList(i);
1641    TraverseTemplateParameterListHelper(TPL);
1642  }
1643  return true;
1644}
1645
1646template <typename Derived>
1647bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1648    ClassTemplateDecl *D) {
1649  for (auto *SD : D->specializations()) {
1650    for (auto *RD : SD->redecls()) {
1651      // We don't want to visit injected-class-names in this traversal.
1652      if (cast<CXXRecordDecl>(RD)->isInjectedClassName())
1653        continue;
1654
1655      switch (
1656          cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1657      // Visit the implicit instantiations with the requested pattern.
1658      case TSK_Undeclared:
1659      case TSK_ImplicitInstantiation:
1660        TRY_TO(TraverseDecl(RD));
1661        break;
1662
1663      // We don't need to do anything on an explicit instantiation
1664      // or explicit specialization because there will be an explicit
1665      // node for it elsewhere.
1666      case TSK_ExplicitInstantiationDeclaration:
1667      case TSK_ExplicitInstantiationDefinition:
1668      case TSK_ExplicitSpecialization:
1669        break;
1670      }
1671    }
1672  }
1673
1674  return true;
1675}
1676
1677template <typename Derived>
1678bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1679    VarTemplateDecl *D) {
1680  for (auto *SD : D->specializations()) {
1681    for (auto *RD : SD->redecls()) {
1682      switch (
1683          cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) {
1684      case TSK_Undeclared:
1685      case TSK_ImplicitInstantiation:
1686        TRY_TO(TraverseDecl(RD));
1687        break;
1688
1689      case TSK_ExplicitInstantiationDeclaration:
1690      case TSK_ExplicitInstantiationDefinition:
1691      case TSK_ExplicitSpecialization:
1692        break;
1693      }
1694    }
1695  }
1696
1697  return true;
1698}
1699
1700// A helper method for traversing the instantiations of a
1701// function while skipping its specializations.
1702template <typename Derived>
1703bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations(
1704    FunctionTemplateDecl *D) {
1705  for (auto *FD : D->specializations()) {
1706    for (auto *RD : FD->redecls()) {
1707      switch (RD->getTemplateSpecializationKind()) {
1708      case TSK_Undeclared:
1709      case TSK_ImplicitInstantiation:
1710        // We don't know what kind of FunctionDecl this is.
1711        TRY_TO(TraverseDecl(RD));
1712        break;
1713
1714      // FIXME: For now traverse explicit instantiations here. Change that
1715      // once they are represented as dedicated nodes in the AST.
1716      case TSK_ExplicitInstantiationDeclaration:
1717      case TSK_ExplicitInstantiationDefinition:
1718        TRY_TO(TraverseDecl(RD));
1719        break;
1720
1721      case TSK_ExplicitSpecialization:
1722        break;
1723      }
1724    }
1725  }
1726
1727  return true;
1728}
1729
1730// This macro unifies the traversal of class, variable and function
1731// template declarations.
1732#define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND)                                   \
1733  DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, {                              \
1734    TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
1735    TRY_TO(TraverseDecl(D->getTemplatedDecl()));                               \
1736                                                                               \
1737    /* By default, we do not traverse the instantiations of                    \
1738       class templates since they do not appear in the user code. The          \
1739       following code optionally traverses them.                               \
1740                                                                               \
1741       We only traverse the class instantiations when we see the canonical     \
1742       declaration of the template, to ensure we only visit them once. */      \
1743    if (getDerived().shouldVisitTemplateInstantiations() &&                    \
1744        D == D->getCanonicalDecl())                                            \
1745      TRY_TO(TraverseTemplateInstantiations(D));                               \
1746                                                                               \
1747    /* Note that getInstantiatedFromMemberTemplate() is just a link            \
1748       from a template instantiation back to the template from which           \
1749       it was instantiated, and thus should not be traversed. */               \
1750  })
1751
1752DEF_TRAVERSE_TMPL_DECL(Class)
1753DEF_TRAVERSE_TMPL_DECL(Var)
1754DEF_TRAVERSE_TMPL_DECL(Function)
1755
1756DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1757  // D is the "T" in something like
1758  //   template <template <typename> class T> class container { };
1759  TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1760  if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1761    TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1762  }
1763  TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1764})
1765
1766DEF_TRAVERSE_DECL(BuiltinTemplateDecl, {
1767  TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1768})
1769
1770DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1771  // D is the "T" in something like "template<typename T> class vector;"
1772  if (D->getTypeForDecl())
1773    TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1774  if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1775    TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1776})
1777
1778DEF_TRAVERSE_DECL(TypedefDecl, {
1779  TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1780  // We shouldn't traverse D->getTypeForDecl(); it's a result of
1781  // declaring the typedef, not something that was written in the
1782  // source.
1783})
1784
1785DEF_TRAVERSE_DECL(TypeAliasDecl, {
1786  TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1787  // We shouldn't traverse D->getTypeForDecl(); it's a result of
1788  // declaring the type alias, not something that was written in the
1789  // source.
1790})
1791
1792DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1793  TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1794  TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1795})
1796
1797DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1798  // A dependent using declaration which was marked with 'typename'.
1799  //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1800  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1801  // We shouldn't traverse D->getTypeForDecl(); it's a result of
1802  // declaring the type, not something that was written in the
1803  // source.
1804})
1805
1806DEF_TRAVERSE_DECL(EnumDecl, {
1807  TRY_TO(TraverseDeclTemplateParameterLists(D));
1808
1809  if (D->getTypeForDecl())
1810    TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1811
1812  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1813  // The enumerators are already traversed by
1814  // decls_begin()/decls_end().
1815})
1816
1817// Helper methods for RecordDecl and its children.
1818template <typename Derived>
1819bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) {
1820  // We shouldn't traverse D->getTypeForDecl(); it's a result of
1821  // declaring the type, not something that was written in the source.
1822
1823  TRY_TO(TraverseDeclTemplateParameterLists(D));
1824  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1825  return true;
1826}
1827
1828template <typename Derived>
1829bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier(
1830    const CXXBaseSpecifier &Base) {
1831  TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc()));
1832  return true;
1833}
1834
1835template <typename Derived>
1836bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) {
1837  if (!TraverseRecordHelper(D))
1838    return false;
1839  if (D->isCompleteDefinition()) {
1840    for (const auto &I : D->bases()) {
1841      TRY_TO(TraverseCXXBaseSpecifier(I));
1842    }
1843    // We don't traverse the friends or the conversions, as they are
1844    // already in decls_begin()/decls_end().
1845  }
1846  return true;
1847}
1848
1849DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); })
1850
1851DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
1852
1853#define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND)                              \
1854  DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
1855    /* For implicit instantiations ("set<int> x;"), we don't want to           \
1856       recurse at all, since the instatiated template isn't written in         \
1857       the source code anywhere.  (Note the instatiated *type* --              \
1858       set<int> -- is written, and will still get a callback of                \
1859       TemplateSpecializationType).  For explicit instantiations               \
1860       ("template set<int>;"), we do need a callback, since this               \
1861       is the only callback that's made for this instantiation.                \
1862       We use getTypeAsWritten() to distinguish. */                            \
1863    if (TypeSourceInfo *TSI = D->getTypeAsWritten())                           \
1864      TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
1865                                                                               \
1866    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));              \
1867    if (!getDerived().shouldVisitTemplateInstantiations() &&                   \
1868        D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)      \
1869      /* Returning from here skips traversing the                              \
1870         declaration context of the *TemplateSpecializationDecl                \
1871         (embedded in the DEF_TRAVERSE_DECL() macro)                           \
1872         which contains the instantiated members of the template. */           \
1873      return true;                                                             \
1874  })
1875
1876DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
1877DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
1878
1879template <typename Derived>
1880bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1881    const TemplateArgumentLoc *TALunsigned Count) {
1882  for (unsigned I = 0I < Count; ++I) {
1883    TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1884  }
1885  return true;
1886}
1887
1888#define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
1889  DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
1890    /* The partial specialization. */                                          \
1891    if (TemplateParameterList *TPL = D->getTemplateParameters()) {             \
1892      for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();   \
1893           I != E; ++I) {                                                      \
1894        TRY_TO(TraverseDecl(*I));                                              \
1895      }                                                                        \
1896    }                                                                          \
1897    /* The args that remains unspecialized. */                                 \
1898    TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
1899        D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
1900        D->getTemplateArgsAsWritten()->NumTemplateArgs));                      \
1901                                                                               \
1902    /* Don't need the *TemplatePartialSpecializationHelper, even               \
1903       though that's our parent class -- we already visit all the              \
1904       template args here. */                                                  \
1905    TRY_TO(Traverse##DECLKIND##Helper(D));                                     \
1906                                                                               \
1907    /* Instantiations will have been visited with the primary template. */     \
1908  })
1909
1910DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord)
1911DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var)
1912
1913DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); })
1914
1915DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1916  // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1917  //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
1918  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1919  TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1920})
1921
1922DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1923
1924template <typename Derived>
1925bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1926  TRY_TO(TraverseDeclTemplateParameterLists(D));
1927  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1928  if (D->getTypeSourceInfo())
1929    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1930  else
1931    TRY_TO(TraverseType(D->getType()));
1932  return true;
1933}
1934
1935DEF_TRAVERSE_DECL(DecompositionDecl, {
1936  TRY_TO(TraverseVarHelper(D));
1937  for (auto *Binding : D->bindings()) {
1938    TRY_TO(TraverseDecl(Binding));
1939  }
1940})
1941
1942DEF_TRAVERSE_DECL(BindingDecl, {
1943  if (getDerived().shouldVisitImplicitCode())
1944    TRY_TO(TraverseStmt(D->getBinding()));
1945})
1946
1947DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); })
1948
1949DEF_TRAVERSE_DECL(FieldDecl, {
1950  TRY_TO(TraverseDeclaratorHelper(D));
1951  if (D->isBitField())
1952    TRY_TO(TraverseStmt(D->getBitWidth()));
1953  else if (D->hasInClassInitializer())
1954    TRY_TO(TraverseStmt(D->getInClassInitializer()));
1955})
1956
1957DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1958  TRY_TO(TraverseDeclaratorHelper(D));
1959  if (D->isBitField())
1960    TRY_TO(TraverseStmt(D->getBitWidth()));
1961  // FIXME: implement the rest.
1962})
1963
1964DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1965  TRY_TO(TraverseDeclaratorHelper(D));
1966  if (D->isBitField())
1967    TRY_TO(TraverseStmt(D->getBitWidth()));
1968  // FIXME: implement the rest.
1969})
1970
1971template <typename Derived>
1972bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1973  TRY_TO(TraverseDeclTemplateParameterLists(D));
1974  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1975  TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1976
1977  // If we're an explicit template specialization, iterate over the
1978  // template args that were explicitly specified.  If we were doing
1979  // this in typing order, we'd do it between the return type and
1980  // the function args, but both are handled by the FunctionTypeLoc
1981  // above, so we have to choose one side.  I've decided to do before.
1982  if (const FunctionTemplateSpecializationInfo *FTSI =
1983          D->getTemplateSpecializationInfo()) {
1984    if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1985        FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1986      // A specialization might not have explicit template arguments if it has
1987      // a templated return type and concrete arguments.
1988      if (const ASTTemplateArgumentListInfo *TALI =
1989              FTSI->TemplateArgumentsAsWritten) {
1990        TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1991                                                  TALI->NumTemplateArgs));
1992      }
1993    }
1994  }
1995
1996  // Visit the function type itself, which can be either
1997  // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
1998  // also covers the return type and the function parameters,
1999  // including exception specifications.
2000  if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
2001    TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2002  } else if (getDerived().shouldVisitImplicitCode()) {
2003    // Visit parameter variable declarations of the implicit function
2004    // if the traverser is visiting implicit code. Parameter variable
2005    // declarations do not have valid TypeSourceInfo, so to visit them
2006    // we need to traverse the declarations explicitly.
2007    for (ParmVarDecl *Parameter : D->parameters()) {
2008      TRY_TO(TraverseDecl(Parameter));
2009    }
2010  }
2011
2012  if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2013    // Constructor initializers.
2014    for (auto *I : Ctor->inits()) {
2015      TRY_TO(TraverseConstructorInitializer(I));
2016    }
2017  }
2018
2019  if (D->isThisDeclarationADefinition()) {
2020    TRY_TO(TraverseStmt(D->getBody()))// Function body.
2021  }
2022  return true;
2023}
2024
2025DEF_TRAVERSE_DECL(FunctionDecl, {
2026  // We skip decls_begin/decls_end, which are already covered by
2027  // TraverseFunctionHelper().
2028  ShouldVisitChildren = false;
2029  ReturnValue = TraverseFunctionHelper(D);
2030})
2031
2032DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
2033  // We skip decls_begin/decls_end, which are already covered by
2034  // TraverseFunctionHelper().
2035  ShouldVisitChildren = false;
2036  ReturnValue = TraverseFunctionHelper(D);
2037})
2038
2039DEF_TRAVERSE_DECL(CXXMethodDecl, {
2040  // We skip decls_begin/decls_end, which are already covered by
2041  // TraverseFunctionHelper().
2042  ShouldVisitChildren = false;
2043  ReturnValue = TraverseFunctionHelper(D);
2044})
2045
2046DEF_TRAVERSE_DECL(CXXConstructorDecl, {
2047  // We skip decls_begin/decls_end, which are already covered by
2048  // TraverseFunctionHelper().
2049  ShouldVisitChildren = false;
2050  ReturnValue = TraverseFunctionHelper(D);
2051})
2052
2053// CXXConversionDecl is the declaration of a type conversion operator.
2054// It's not a cast expression.
2055DEF_TRAVERSE_DECL(CXXConversionDecl, {
2056  // We skip decls_begin/decls_end, which are already covered by
2057  // TraverseFunctionHelper().
2058  ShouldVisitChildren = false;
2059  ReturnValue = TraverseFunctionHelper(D);
2060})
2061
2062DEF_TRAVERSE_DECL(CXXDestructorDecl, {
2063  // We skip decls_begin/decls_end, which are already covered by
2064  // TraverseFunctionHelper().
2065  ShouldVisitChildren = false;
2066  ReturnValue = TraverseFunctionHelper(D);
2067})
2068
2069template <typename Derived>
2070bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
2071  TRY_TO(TraverseDeclaratorHelper(D));
2072  // Default params are taken care of when we traverse the ParmVarDecl.
2073  if (!isa<ParmVarDecl>(D) &&
2074      (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
2075    TRY_TO(TraverseStmt(D->getInit()));
2076  return true;
2077}
2078
2079DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); })
2080
2081DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); })
2082
2083DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
2084  // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
2085  TRY_TO(TraverseDeclaratorHelper(D));
2086  if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
2087    TRY_TO(TraverseStmt(D->getDefaultArgument()));
2088})
2089
2090DEF_TRAVERSE_DECL(ParmVarDecl, {
2091  TRY_TO(TraverseVarHelper(D));
2092
2093  if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() &&
2094      !D->hasUnparsedDefaultArg())
2095    TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
2096
2097  if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() &&
2098      !D->hasUnparsedDefaultArg())
2099    TRY_TO(TraverseStmt(D->getDefaultArg()));
2100})
2101
2102#undef DEF_TRAVERSE_DECL
2103
2104// ----------------- Stmt traversal -----------------
2105//
2106// For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
2107// over the children defined in children() (every stmt defines these,
2108// though sometimes the range is empty).  Each individual Traverse*
2109// method only needs to worry about children other than those.  To see
2110// what children() does for a given class, see, e.g.,
2111//   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
2112
2113// This macro makes available a variable S, the passed-in stmt.
2114#define DEF_TRAVERSE_STMT(STMT, CODE)                                          \
2115  template <typename Derived>                                                  \
2116  bool RecursiveASTVisitor<Derived>::Traverse##STMT(                           \
2117      STMT *S, DataRecursionQueue *Queue) {                                    \
2118    bool ShouldVisitChildren = true;                                           \
2119    bool ReturnValue = true;                                                   \
2120    if (!getDerived().shouldTraversePostOrder())                               \
2121      TRY_TO(WalkUpFrom##STMT(S));                                             \
2122    { CODE; }                                                                  \
2123    if (ShouldVisitChildren) {                                                 \
2124      for (Stmt * SubStmt : getDerived().getStmtChildren(S)) {                 \
2125        TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);                              \
2126      }                                                                        \
2127    }                                                                          \
2128    if (!Queue && ReturnValue && getDerived().shouldTraversePostOrder())       \
2129      TRY_TO(WalkUpFrom##STMT(S));                                             \
2130    return ReturnValue;                                                        \
2131  }
2132
2133DEF_TRAVERSE_STMT(GCCAsmStmt, {
2134  TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString());
2135  for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
2136    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I));
2137  }
2138  for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
2139    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I));
2140  }
2141  for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
2142    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I));
2143  }
2144  // children() iterates over inputExpr and outputExpr.
2145})
2146
2147DEF_TRAVERSE_STMT(
2148    MSAsmStmt,
2149    {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
2150     // added this needs to be implemented.
2151    })
2152
2153DEF_TRAVERSE_STMT(CXXCatchStmt, {
2154  TRY_TO(TraverseDecl(S->getExceptionDecl()));
2155  // children() iterates over the handler block.
2156})
2157
2158DEF_TRAVERSE_STMT(DeclStmt, {
2159  for (auto *I : S->decls()) {
2160    TRY_TO(TraverseDecl(I));
2161  }
2162  // Suppress the default iteration over children() by
2163  // returning.  Here's why: A DeclStmt looks like 'type var [=
2164  // initializer]'.  The decls above already traverse over the
2165  // initializers, so we don't have to do it again (which
2166  // children() would do).
2167  ShouldVisitChildren = false;
2168})
2169
2170// These non-expr stmts (most of them), do not need any action except
2171// iterating over the children.
2172DEF_TRAVERSE_STMT(BreakStmt, {})
2173DEF_TRAVERSE_STMT(CXXTryStmt, {})
2174DEF_TRAVERSE_STMT(CaseStmt, {})
2175DEF_TRAVERSE_STMT(CompoundStmt, {})
2176DEF_TRAVERSE_STMT(ContinueStmt, {})
2177DEF_TRAVERSE_STMT(DefaultStmt, {})
2178DEF_TRAVERSE_STMT(DoStmt, {})
2179DEF_TRAVERSE_STMT(ForStmt, {})
2180DEF_TRAVERSE_STMT(GotoStmt, {})
2181DEF_TRAVERSE_STMT(IfStmt, {})
2182DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
2183DEF_TRAVERSE_STMT(LabelStmt, {})
2184DEF_TRAVERSE_STMT(AttributedStmt, {})
2185DEF_TRAVERSE_STMT(NullStmt, {})
2186DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
2187DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
2188DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
2189DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
2190DEF_TRAVERSE_STMT(ObjCAtTryStmt, {})
2191DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {})
2192DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {})
2193
2194DEF_TRAVERSE_STMT(CXXForRangeStmt, {
2195  if (!getDerived().shouldVisitImplicitCode()) {
2196    if (S->getInit())
2197      TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInit());
2198    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt());
2199    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit());
2200    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2201    // Visit everything else only if shouldVisitImplicitCode().
2202    ShouldVisitChildren = false;
2203  }
2204})
2205
2206DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
2207  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2208  TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2209})
2210
2211DEF_TRAVERSE_STMT(ReturnStmt, {})
2212DEF_TRAVERSE_STMT(SwitchStmt, {})
2213DEF_TRAVERSE_STMT(WhileStmt, {})
2214
2215DEF_TRAVERSE_STMT(ConstantExpr, {})
2216
2217DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
2218  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2219  TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2220  if (S->hasExplicitTemplateArgs()) {
2221    TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2222                                              S->getNumTemplateArgs()));
2223  }
2224})
2225
2226DEF_TRAVERSE_STMT(DeclRefExpr, {
2227  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2228  TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2229  TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2230                                            S->getNumTemplateArgs()));
2231})
2232
2233DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
2234  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2235  TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
2236  if (S->hasExplicitTemplateArgs()) {
2237    TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2238                                              S->getNumTemplateArgs()));
2239  }
2240})
2241
2242DEF_TRAVERSE_STMT(MemberExpr, {
2243  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2244  TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
2245  TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2246                                            S->getNumTemplateArgs()));
2247})
2248
2249DEF_TRAVERSE_STMT(
2250    ImplicitCastExpr,
2251    {// We don't traverse the cast type, as it's not written in the
2252     // source code.
2253    })
2254
2255DEF_TRAVERSE_STMT(CStyleCastExpr, {
2256  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2257})
2258
2259DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
2260  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2261})
2262
2263DEF_TRAVERSE_STMT(CXXConstCastExpr, {
2264  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2265})
2266
2267DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
2268  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2269})
2270
2271DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
2272  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2273})
2274
2275DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
2276  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2277})
2278
2279template <typename Derived>
2280bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr(
2281    InitListExpr *S, DataRecursionQueue *Queue) {
2282  if (S) {
2283    // Skip this if we traverse postorder. We will visit it later
2284    // in PostVisitStmt.
2285    if (!getDerived().shouldTraversePostOrder())
2286      TRY_TO(WalkUpFromInitListExpr(S));
2287
2288    // All we need are the default actions.  FIXME: use a helper function.
2289    for (Stmt *SubStmt : S->children()) {
2290      TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt);
2291    }
2292  }
2293  return true;
2294}
2295
2296// This method is called once for each pair of syntactic and semantic
2297// InitListExpr, and it traverses the subtrees defined by the two forms. This
2298// may cause some of the children to be visited twice, if they appear both in
2299// the syntactic and the semantic form.
2300//
2301// There is no guarantee about which form \p S takes when this method is called.
2302template <typename Derived>
2303bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(
2304    InitListExpr *S, DataRecursionQueue *Queue) {
2305  TRY_TO(TraverseSynOrSemInitListExpr(
2306      S->isSemanticForm() ? S->getSyntacticForm() : S, Queue));
2307  TRY_TO(TraverseSynOrSemInitListExpr(
2308      S->isSemanticForm() ? S : S->getSemanticForm(), Queue));
2309  return true;
2310}
2311
2312// GenericSelectionExpr is a special case because the types and expressions
2313// are interleaved.  We also need to watch out for null types (default
2314// generic associations).
2315DEF_TRAVERSE_STMT(GenericSelectionExpr, {
2316  TRY_TO(TraverseStmt(S->getControllingExpr()));
2317  for (const GenericSelectionExpr::Association &Assoc : S->associations()) {
2318    if (TypeSourceInfo *TSI = Assoc.getTypeSourceInfo())
2319      TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
2320    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(Assoc.getAssociationExpr());
2321  }
2322  ShouldVisitChildren = false;
2323})
2324
2325// PseudoObjectExpr is a special case because of the weirdness with
2326// syntactic expressions and opaque values.
2327DEF_TRAVERSE_STMT(PseudoObjectExpr, {
2328  TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm());
2329  for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(),
2330                                            e = S->semantics_end();
2331       i != e; ++i) {
2332    Expr *sub = *i;
2333    if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2334      sub = OVE->getSourceExpr();
2335    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub);
2336  }
2337  ShouldVisitChildren = false;
2338})
2339
2340DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2341  // This is called for code like 'return T()' where T is a built-in
2342  // (i.e. non-class) type.
2343  TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2344})
2345
2346DEF_TRAVERSE_STMT(CXXNewExpr, {
2347  // The child-iterator will pick up the other arguments.
2348  TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2349})
2350
2351DEF_TRAVERSE_STMT(OffsetOfExpr, {
2352  // The child-iterator will pick up the expression representing
2353  // the field.
2354  // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2355  // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2356  TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2357})
2358
2359DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2360  // The child-iterator will pick up the arg if it's an expression,
2361  // but not if it's a type.
2362  if (S->isArgumentType())
2363    TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2364})
2365
2366DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2367  // The child-iterator will pick up the arg if it's an expression,
2368  // but not if it's a type.
2369  if (S->isTypeOperand())
2370    TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2371})
2372
2373DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2374  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2375})
2376
2377DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {})
2378
2379DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2380  // The child-iterator will pick up the arg if it's an expression,
2381  // but not if it's a type.
2382  if (S->isTypeOperand())
2383    TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2384})
2385
2386DEF_TRAVERSE_STMT(TypeTraitExpr, {
2387  for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2388    TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2389})
2390
2391DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2392  TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2393})
2394
2395DEF_TRAVERSE_STMT(ExpressionTraitExpr,
2396                  { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); })
2397
2398DEF_TRAVERSE_STMT(VAArgExpr, {
2399  // The child-iterator will pick up the expression argument.
2400  TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2401})
2402
2403DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2404  // This is called for code like 'return T()' where T is a class type.
2405  TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2406})
2407
2408// Walk only the visible parts of lambda expressions.
2409DEF_TRAVERSE_STMT(LambdaExpr, {
2410  // Visit the capture list.
2411  for (unsigned I = 0, N = S->capture_size(); I != N; ++I) {
2412    const LambdaCapture *C = S->capture_begin() + I;
2413    if (C->isExplicit() || getDerived().shouldVisitImplicitCode()) {
2414      TRY_TO(TraverseLambdaCapture(S, C, S->capture_init_begin()[I]));
2415    }
2416  }
2417
2418  if (getDerived().shouldVisitImplicitCode()) {
2419    // The implicit model is simple: everything else is in the lambda class.
2420    TRY_TO(TraverseDecl(S->getLambdaClass()));
2421  } else {
2422    // We need to poke around to find the bits that might be explicitly written.
2423    TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2424    FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>();
2425
2426    if (S->hasExplicitParameters()) {
2427      // Visit parameters.
2428      for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
2429        TRY_TO(TraverseDecl(Proto.getParam(I)));
2430    }
2431    if (S->hasExplicitResultType())
2432      TRY_TO(TraverseTypeLoc(Proto.getReturnLoc()));
2433
2434    auto *T = Proto.getTypePtr();
2435    for (const auto &E : T->exceptions())
2436      TRY_TO(TraverseType(E));
2437
2438    if (Expr *NE = T->getNoexceptExpr())
2439      TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE);
2440
2441    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2442  }
2443  ShouldVisitChildren = false;
2444})
2445
2446DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2447  // This is called for code like 'T()', where T is a template argument.
2448  TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2449})
2450
2451// These expressions all might take explicit template arguments.
2452// We traverse those if so.  FIXME: implement these.
2453DEF_TRAVERSE_STMT(CXXConstructExpr, {})
2454DEF_TRAVERSE_STMT(CallExpr, {})
2455DEF_TRAVERSE_STMT(CXXMemberCallExpr, {})
2456
2457// These exprs (most of them), do not need any action except iterating
2458// over the children.
2459DEF_TRAVERSE_STMT(AddrLabelExpr, {})
2460DEF_TRAVERSE_STMT(ArraySubscriptExpr, {})
2461DEF_TRAVERSE_STMT(OMPArraySectionExpr, {})
2462
2463DEF_TRAVERSE_STMT(BlockExpr, {
2464  TRY_TO(TraverseDecl(S->getBlockDecl()));
2465  return true// no child statements to loop through.
2466})
2467
2468DEF_TRAVERSE_STMT(ChooseExpr, {})
2469DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2470  TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2471})
2472DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {})
2473DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {})
2474
2475DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {
2476  if (getDerived().shouldVisitImplicitCode())
2477    TRY_TO(TraverseStmt(S->getExpr()));
2478})
2479
2480DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {})
2481DEF_TRAVERSE_STMT(CXXDeleteExpr, {})
2482DEF_TRAVERSE_STMT(ExprWithCleanups, {})
2483DEF_TRAVERSE_STMT(CXXInheritedCtorInitExpr, {})
2484DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {})
2485DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {})
2486
2487DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2488  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2489  if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2490    TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2491  if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2492    TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2493})
2494
2495DEF_TRAVERSE_STMT(CXXThisExpr, {})
2496DEF_TRAVERSE_STMT(CXXThrowExpr, {})
2497DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
2498DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
2499DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
2500DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2501DEF_TRAVERSE_STMT(GNUNullExpr, {})
2502DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
2503DEF_TRAVERSE_STMT(NoInitExpr, {})
2504DEF_TRAVERSE_STMT(ArrayInitLoopExpr, {
2505  // FIXME: The source expression of the OVE should be listed as
2506  // a child of the ArrayInitLoopExpr.
2507  if (OpaqueValueExpr *OVE = S->getCommonExpr())
2508    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr());
2509})
2510DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {})
2511DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
2512
2513DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2514  if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2515    TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2516})
2517
2518DEF_TRAVERSE_STMT(ObjCIsaExpr, {})
2519DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {})
2520
2521DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2522  if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2523    TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2524})
2525
2526DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {})
2527DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {})
2528DEF_TRAVERSE_STMT(ObjCProtocolExpr, {})
2529DEF_TRAVERSE_STMT(ObjCSelectorExpr, {})
2530DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {})
2531
2532DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2533  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2534})
2535
2536DEF_TRAVERSE_STMT(ObjCAvailabilityCheckExpr, {})
2537DEF_TRAVERSE_STMT(ParenExpr, {})
2538DEF_TRAVERSE_STMT(ParenListExpr, {})
2539DEF_TRAVERSE_STMT(PredefinedExpr, {})
2540DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
2541DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
2542DEF_TRAVERSE_STMT(StmtExpr, {})
2543DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2544  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2545  if (S->hasExplicitTemplateArgs()) {
2546    TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2547                                              S->getNumTemplateArgs()));
2548  }
2549})
2550
2551DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2552  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2553  if (S->hasExplicitTemplateArgs()) {
2554    TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2555                                              S->getNumTemplateArgs()));
2556  }
2557})
2558
2559DEF_TRAVERSE_STMT(SEHTryStmt, {})
2560DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2561DEF_TRAVERSE_STMT(SEHFinallyStmt, {})
2562DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
2563DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); })
2564
2565DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {})
2566DEF_TRAVERSE_STMT(OpaqueValueExpr, {})
2567DEF_TRAVERSE_STMT(TypoExpr, {})
2568DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {})
2569
2570// These operators (all of them) do not need any action except
2571// iterating over the children.
2572DEF_TRAVERSE_STMT(BinaryConditionalOperator, {})
2573DEF_TRAVERSE_STMT(ConditionalOperator, {})
2574DEF_TRAVERSE_STMT(UnaryOperator, {})
2575DEF_TRAVERSE_STMT(BinaryOperator, {})
2576DEF_TRAVERSE_STMT(CompoundAssignOperator, {})
2577DEF_TRAVERSE_STMT(CXXNoexceptExpr, {})
2578DEF_TRAVERSE_STMT(PackExpansionExpr, {})
2579DEF_TRAVERSE_STMT(SizeOfPackExpr, {})
2580DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {})
2581DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {})
2582DEF_TRAVERSE_STMT(FunctionParmPackExpr, {})
2583DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {})
2584DEF_TRAVERSE_STMT(CXXFoldExpr, {})
2585DEF_TRAVERSE_STMT(AtomicExpr, {})
2586
2587// For coroutines expressions, traverse either the operand
2588// as written or the implied calls, depending on what the
2589// derived class requests.
2590DEF_TRAVERSE_STMT(CoroutineBodyStmt, {
2591  if (!getDerived().shouldVisitImplicitCode()) {
2592    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody());
2593    ShouldVisitChildren = false;
2594  }
2595})
2596DEF_TRAVERSE_STMT(CoreturnStmt, {
2597  if (!getDerived().shouldVisitImplicitCode()) {
2598    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2599    ShouldVisitChildren = false;
2600  }
2601})
2602DEF_TRAVERSE_STMT(CoawaitExpr, {
2603  if (!getDerived().shouldVisitImplicitCode()) {
2604    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2605    ShouldVisitChildren = false;
2606  }
2607})
2608DEF_TRAVERSE_STMT(DependentCoawaitExpr, {
2609  if (!getDerived().shouldVisitImplicitCode()) {
2610    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2611    ShouldVisitChildren = false;
2612  }
2613})
2614DEF_TRAVERSE_STMT(CoyieldExpr, {
2615  if (!getDerived().shouldVisitImplicitCode()) {
2616    TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand());
2617    ShouldVisitChildren = false;
2618  }
2619})
2620
2621// These literals (all of them) do not need any action.
2622DEF_TRAVERSE_STMT(IntegerLiteral, {})
2623DEF_TRAVERSE_STMT(FixedPointLiteral, {})
2624DEF_TRAVERSE_STMT(CharacterLiteral, {})
2625DEF_TRAVERSE_STMT(FloatingLiteral, {})
2626DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
2627DEF_TRAVERSE_STMT(StringLiteral, {})
2628DEF_TRAVERSE_STMT(ObjCStringLiteral, {})
2629DEF_TRAVERSE_STMT(ObjCBoxedExpr, {})
2630DEF_TRAVERSE_STMT(ObjCArrayLiteral, {})
2631DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {})
2632
2633// Traverse OpenCL: AsType, Convert.
2634DEF_TRAVERSE_STMT(AsTypeExpr, {})
2635
2636// OpenMP directives.
2637template <typename Derived>
2638bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective(
2639    OMPExecutableDirective *S) {
2640  for (auto *C : S->clauses()) {
2641    TRY_TO(TraverseOMPClause(C));
2642  }
2643  return true;
2644}
2645
2646template <typename Derived>
2647bool
2648RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
2649  return TraverseOMPExecutableDirective(S);
2650}
2651
2652DEF_TRAVERSE_STMT(OMPParallelDirective,
2653                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2654
2655DEF_TRAVERSE_STMT(OMPSimdDirective,
2656                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2657
2658DEF_TRAVERSE_STMT(OMPForDirective,
2659                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2660
2661DEF_TRAVERSE_STMT(OMPForSimdDirective,
2662                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2663
2664DEF_TRAVERSE_STMT(OMPSectionsDirective,
2665                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2666
2667DEF_TRAVERSE_STMT(OMPSectionDirective,
2668                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2669
2670DEF_TRAVERSE_STMT(OMPSingleDirective,
2671                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2672
2673DEF_TRAVERSE_STMT(OMPMasterDirective,
2674                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2675
2676DEF_TRAVERSE_STMT(OMPCriticalDirective, {
2677  TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName()));
2678  TRY_TO(TraverseOMPExecutableDirective(S));
2679})
2680
2681DEF_TRAVERSE_STMT(OMPParallelForDirective,
2682                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2683
2684DEF_TRAVERSE_STMT(OMPParallelForSimdDirective,
2685                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2686
2687DEF_TRAVERSE_STMT(OMPParallelSectionsDirective,
2688                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2689
2690DEF_TRAVERSE_STMT(OMPTaskDirective,
2691                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2692
2693DEF_TRAVERSE_STMT(OMPTaskyieldDirective,
2694                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2695
2696DEF_TRAVERSE_STMT(OMPBarrierDirective,
2697                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2698
2699DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
2700                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2701
2702DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
2703                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2704
2705DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
2706                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2707
2708DEF_TRAVERSE_STMT(OMPCancelDirective,
2709                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2710
2711DEF_TRAVERSE_STMT(OMPFlushDirective,
2712                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2713
2714DEF_TRAVERSE_STMT(OMPOrderedDirective,
2715                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2716
2717DEF_TRAVERSE_STMT(OMPAtomicDirective,
2718                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2719
2720DEF_TRAVERSE_STMT(OMPTargetDirective,
2721                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2722
2723DEF_TRAVERSE_STMT(OMPTargetDataDirective,
2724                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2725
2726DEF_TRAVERSE_STMT(OMPTargetEnterDataDirective,
2727                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2728
2729DEF_TRAVERSE_STMT(OMPTargetExitDataDirective,
2730                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2731
2732DEF_TRAVERSE_STMT(OMPTargetParallelDirective,
2733                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2734
2735DEF_TRAVERSE_STMT(OMPTargetParallelForDirective,
2736                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2737
2738DEF_TRAVERSE_STMT(OMPTeamsDirective,
2739                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2740
2741DEF_TRAVERSE_STMT(OMPTargetUpdateDirective,
2742                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2743
2744DEF_TRAVERSE_STMT(OMPTaskLoopDirective,
2745                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2746
2747DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective,
2748                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2749
2750DEF_TRAVERSE_STMT(OMPDistributeDirective,
2751                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2752
2753DEF_TRAVERSE_STMT(OMPDistributeParallelForDirective,
2754                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2755
2756DEF_TRAVERSE_STMT(OMPDistributeParallelForSimdDirective,
2757                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2758
2759DEF_TRAVERSE_STMT(OMPDistributeSimdDirective,
2760                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2761
2762DEF_TRAVERSE_STMT(OMPTargetParallelForSimdDirective,
2763                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2764
2765DEF_TRAVERSE_STMT(OMPTargetSimdDirective,
2766                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2767
2768DEF_TRAVERSE_STMT(OMPTeamsDistributeDirective,
2769                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2770
2771DEF_TRAVERSE_STMT(OMPTeamsDistributeSimdDirective,
2772                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2773
2774DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForSimdDirective,
2775                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2776
2777DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForDirective,
2778                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2779
2780DEF_TRAVERSE_STMT(OMPTargetTeamsDirective,
2781                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2782
2783DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeDirective,
2784                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2785
2786DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForDirective,
2787                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2788
2789DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForSimdDirective,
2790                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2791
2792DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeSimdDirective,
2793                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
2794
2795// OpenMP clauses.
2796template <typename Derived>
2797bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
2798  if (!C)
2799    return true;
2800  switch (C->getClauseKind()) {
2801#define OPENMP_CLAUSE(Name, Class)                                             \
2802  case OMPC_##Name:                                                            \
2803    TRY_TO(Visit##Class(static_cast<Class *>(C)));                             \
2804    break;
2805#include "clang/Basic/OpenMPKinds.def"
2806  case OMPC_threadprivate:
2807  case OMPC_uniform:
2808  case OMPC_unknown:
2809    break;
2810  }
2811  return true;
2812}
2813
2814template <typename Derived>
2815bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPreInit(
2816    OMPClauseWithPreInit *Node) {
2817  TRY_TO(TraverseStmt(Node->getPreInitStmt()));
2818  return true;
2819}
2820
2821template <typename Derived>
2822bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPostUpdate(
2823    OMPClauseWithPostUpdate *Node) {
2824  TRY_TO(VisitOMPClauseWithPreInit(Node));
2825  TRY_TO(TraverseStmt(Node->getPostUpdateExpr()));
2826  return true;
2827}
2828
2829template <typename Derived>
2830bool RecursiveASTVisitor<Derived>::VisitOMPAllocatorClause(
2831    OMPAllocatorClause *C) {
2832  TRY_TO(TraverseStmt(C->getAllocator()));
2833  return true;
2834}
2835
2836template <typename Derived>
2837bool RecursiveASTVisitor<Derived>::VisitOMPAllocateClause(OMPAllocateClause *C) {
2838  TRY_TO(TraverseStmt(C->getAllocator()));
2839  TRY_TO(VisitOMPClauseList(C));
2840  return true;
2841}
2842
2843template <typename Derived>
2844bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
2845  TRY_TO(VisitOMPClauseWithPreInit(C));
2846  TRY_TO(TraverseStmt(C->getCondition()));
2847  return true;
2848}
2849
2850template <typename Derived>
2851bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
2852  TRY_TO(TraverseStmt(C->getCondition()));
2853  return true;
2854}
2855
2856template <typename Derived>
2857bool
2858RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
2859  TRY_TO(VisitOMPClauseWithPreInit(C));
2860  TRY_TO(TraverseStmt(C->getNumThreads()));
2861  return true;
2862}
2863
2864template <typename Derived>
2865bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2866  TRY_TO(TraverseStmt(C->getSafelen()));
2867  return true;
2868}
2869
2870template <typename Derived>
2871bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
2872  TRY_TO(TraverseStmt(C->getSimdlen()));
2873  return true;
2874}
2875
2876template <typename Derived>
2877bool
2878RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) {
2879  TRY_TO(TraverseStmt(C->getNumForLoops()));
2880  return true;
2881}
2882
2883template <typename Derived>
2884bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) {
2885  return true;
2886}
2887
2888template <typename Derived>
2889bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) {
2890  return true;
2891}
2892
2893template <typename Derived>
2894bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedAddressClause(
2895    OMPUnifiedAddressClause *) {
2896  return true;
2897}
2898
2899template <typename Derived>
2900bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedSharedMemoryClause(
2901    OMPUnifiedSharedMemoryClause *) {
2902  return true;
2903}
2904
2905template <typename Derived>
2906bool RecursiveASTVisitor<Derived>::VisitOMPReverseOffloadClause(
2907    OMPReverseOffloadClause *) {
2908  return true;
2909}
2910
2911template <typename Derived>
2912bool RecursiveASTVisitor<Derived>::VisitOMPDynamicAllocatorsClause(
2913    OMPDynamicAllocatorsClause *) {
2914  return true;
2915}
2916
2917template <typename Derived>
2918bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause(
2919    OMPAtomicDefaultMemOrderClause *) {
2920  return true;
2921}
2922
2923template <typename Derived>
2924bool
2925RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) {
2926  TRY_TO(VisitOMPClauseWithPreInit(C));
2927  TRY_TO(TraverseStmt(C->getChunkSize()));
2928  return true;
2929}
2930
2931template <typename Derived>
2932bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) {
2933  TRY_TO(TraverseStmt(C->getNumForLoops()));
2934  return true;
2935}
2936
2937template <typename Derived>
2938bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) {
2939  return true;
2940}
2941
2942template <typename Derived>
2943bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) {
2944  return true;
2945}
2946
2947template <typename Derived>
2948bool
2949RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
2950  return true;
2951}
2952
2953template <typename Derived>
2954bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
2955  return true;
2956}
2957
2958template <typename Derived>
2959bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) {
2960  return true;
2961}
2962
2963template <typename Derived>
2964bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) {
2965  return true;
2966}
2967
2968template <typename Derived>
2969bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) {
2970  return true;
2971}
2972
2973template <typename Derived>
2974bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) {
2975  return true;
2976}
2977
2978template <typename Derived>
2979bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
2980  return true;
2981}
2982
2983template <typename Derived>
2984bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) {
2985  return true;
2986}
2987
2988template <typename Derived>
2989bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) {
2990  return true;
2991}
2992
2993template <typename Derived>
2994template <typename T>
2995bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
2996  for (auto *E : Node->varlists()) {
2997    TRY_TO(TraverseStmt(E));
2998  }
2999  return true;
3000}
3001
3002template <typename Derived>
3003bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) {
3004  TRY_TO(VisitOMPClauseList(C));
3005  for (auto *E : C->private_copies()) {
3006    TRY_TO(TraverseStmt(E));
3007  }
3008  return true;
3009}
3010
3011template <typename Derived>
3012bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause(
3013    OMPFirstprivateClause *C) {
3014  TRY_TO(VisitOMPClauseList(C));
3015  TRY_TO(VisitOMPClauseWithPreInit(C));
3016  for (auto *E : C->private_copies()) {
3017    TRY_TO(TraverseStmt(E));
3018  }
3019  for (auto *E : C->inits()) {
3020    TRY_TO(TraverseStmt(E));
3021  }
3022  return true;
3023}
3024
3025template <typename Derived>
3026bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause(
3027    OMPLastprivateClause *C) {
3028  TRY_TO(VisitOMPClauseList(C));
3029  TRY_TO(VisitOMPClauseWithPostUpdate(C));
3030  for (auto *E : C->private_copies()) {
3031    TRY_TO(TraverseStmt(E));
3032  }
3033  for (auto *E : C->source_exprs()) {
3034    TRY_TO(TraverseStmt(E));
3035  }
3036  for (auto *E : C->destination_exprs()) {
3037    TRY_TO(TraverseStmt(E));
3038  }
3039  for (auto *E : C->assignment_ops()) {
3040    TRY_TO(TraverseStmt(E));
3041  }
3042  return true;
3043}
3044
3045template <typename Derived>
3046bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) {
3047  TRY_TO(VisitOMPClauseList(C));
3048  return true;
3049}
3050
3051template <typename Derived>
3052bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) {
3053  TRY_TO(TraverseStmt(C->getStep()));
3054  TRY_TO(TraverseStmt(C->getCalcStep()));
3055  TRY_TO(VisitOMPClauseList(C));
3056  TRY_TO(VisitOMPClauseWithPostUpdate(C));
3057  for (auto *E : C->privates()) {
3058    TRY_TO(TraverseStmt(E));
3059  }
3060  for (auto *E : C->inits()) {
3061    TRY_TO(TraverseStmt(E));
3062  }
3063  for (auto *E : C->updates()) {
3064    TRY_TO(TraverseStmt(E));
3065  }
3066  for (auto *E : C->finals()) {
3067    TRY_TO(TraverseStmt(E));
3068  }
3069  return true;
3070}
3071
3072template <typename Derived>
3073bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) {
3074  TRY_TO(TraverseStmt(C->getAlignment()));
3075  TRY_TO(VisitOMPClauseList(C));
3076  return true;
3077}
3078
3079template <typename Derived>
3080bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) {
3081  TRY_TO(VisitOMPClauseList(C));
3082  for (auto *E : C->source_exprs()) {
3083    TRY_TO(TraverseStmt(E));
3084  }
3085  for (auto *E : C->destination_exprs()) {
3086    TRY_TO(TraverseStmt(E));
3087  }
3088  for (auto *E : C->assignment_ops()) {
3089    TRY_TO(TraverseStmt(E));
3090  }
3091  return true;
3092}
3093
3094template <typename Derived>
3095bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause(
3096    OMPCopyprivateClause *C) {
3097  TRY_TO(VisitOMPClauseList(C));
3098  for (auto *E : C->source_exprs()) {
3099    TRY_TO(TraverseStmt(E));
3100  }
3101  for (auto *E : C->destination_exprs()) {
3102    TRY_TO(TraverseStmt(E));
3103  }
3104  for (auto *E : C->assignment_ops()) {
3105    TRY_TO(TraverseStmt(E));
3106  }
3107  return true;
3108}
3109
3110template <typename Derived>
3111bool
3112RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) {
3113  TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3114  TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3115  TRY_TO(VisitOMPClauseList(C));
3116  TRY_TO(VisitOMPClauseWithPostUpdate(C));
3117  for (auto *E : C->privates()) {
3118    TRY_TO(TraverseStmt(E));
3119  }
3120  for (auto *E : C->lhs_exprs()) {
3121    TRY_TO(TraverseStmt(E));
3122  }
3123  for (auto *E : C->rhs_exprs()) {
3124    TRY_TO(TraverseStmt(E));
3125  }
3126  for (auto *E : C->reduction_ops()) {
3127    TRY_TO(TraverseStmt(E));
3128  }
3129  return true;
3130}
3131
3132template <typename Derived>
3133bool RecursiveASTVisitor<Derived>::VisitOMPTaskReductionClause(
3134    OMPTaskReductionClause *C) {
3135  TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3136  TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3137  TRY_TO(VisitOMPClauseList(C));
3138  TRY_TO(VisitOMPClauseWithPostUpdate(C));
3139  for (auto *E : C->privates()) {
3140    TRY_TO(TraverseStmt(E));
3141  }
3142  for (auto *E : C->lhs_exprs()) {
3143    TRY_TO(TraverseStmt(E));
3144  }
3145  for (auto *E : C->rhs_exprs()) {
3146    TRY_TO(TraverseStmt(E));
3147  }
3148  for (auto *E : C->reduction_ops()) {
3149    TRY_TO(TraverseStmt(E));
3150  }
3151  return true;
3152}
3153
3154template <typename Derived>
3155bool RecursiveASTVisitor<Derived>::VisitOMPInReductionClause(
3156    OMPInReductionClause *C) {
3157  TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc()));
3158  TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo()));
3159  TRY_TO(VisitOMPClauseList(C));
3160  TRY_TO(VisitOMPClauseWithPostUpdate(C));
3161  for (auto *E : C->privates()) {
3162    TRY_TO(TraverseStmt(E));
3163  }
3164  for (auto *E : C->lhs_exprs()) {
3165    TRY_TO(TraverseStmt(E));
3166  }
3167  for (auto *E : C->rhs_exprs()) {
3168    TRY_TO(TraverseStmt(E));
3169  }
3170  for (auto *E : C->reduction_ops()) {
3171    TRY_TO(TraverseStmt(E));
3172  }
3173  for (auto *E : C->taskgroup_descriptors())
3174    TRY_TO(TraverseStmt(E));
3175  return true;
3176}
3177
3178template <typename Derived>
3179bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) {
3180  TRY_TO(VisitOMPClauseList(C));
3181  return true;
3182}
3183
3184template <typename Derived>
3185bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
3186  TRY_TO(VisitOMPClauseList(C));
3187  return true;
3188}
3189
3190template <typename Derived>
3191bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
3192  TRY_TO(VisitOMPClauseWithPreInit(C));
3193  TRY_TO(TraverseStmt(C->getDevice()));
3194  return true;
3195}
3196
3197template <typename Derived>
3198bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) {
3199  TRY_TO(VisitOMPClauseList(C));
3200  return true;
3201}
3202
3203template <typename Derived>
3204bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause(
3205    OMPNumTeamsClause *C) {
3206  TRY_TO(VisitOMPClauseWithPreInit(C));
3207  TRY_TO(TraverseStmt(C->getNumTeams()));
3208  return true;
3209}
3210
3211template <typename Derived>
3212bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause(
3213    OMPThreadLimitClause *C) {
3214  TRY_TO(VisitOMPClauseWithPreInit(C));
3215  TRY_TO(TraverseStmt(C->getThreadLimit()));
3216  return true;
3217}
3218
3219template <typename Derived>
3220bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
3221    OMPPriorityClause *C) {
3222  TRY_TO(TraverseStmt(C->getPriority()));
3223  return true;
3224}
3225
3226template <typename Derived>
3227bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
3228    OMPGrainsizeClause *C) {
3229  TRY_TO(TraverseStmt(C->getGrainsize()));
3230  return true;
3231}
3232
3233template <typename Derived>
3234bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause(
3235    OMPNumTasksClause *C) {
3236  TRY_TO(TraverseStmt(C->getNumTasks()));
3237  return true;
3238}
3239
3240template <typename Derived>
3241bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) {
3242  TRY_TO(TraverseStmt(C->getHint()));
3243  return true;
3244}
3245
3246template <typename Derived>
3247bool RecursiveASTVisitor<Derived>::VisitOMPDistScheduleClause(
3248    OMPDistScheduleClause *C) {
3249  TRY_TO(VisitOMPClauseWithPreInit(C));
3250  TRY_TO(TraverseStmt(C->getChunkSize()));
3251  return true;
3252}
3253
3254template <typename Derived>
3255bool
3256RecursiveASTVisitor<Derived>::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
3257  return true;
3258}
3259
3260template <typename Derived>
3261bool RecursiveASTVisitor<Derived>::VisitOMPToClause(OMPToClause *C) {
3262  TRY_TO(VisitOMPClauseList(C));
3263  return true;
3264}
3265
3266template <typename Derived>
3267bool RecursiveASTVisitor<Derived>::VisitOMPFromClause(OMPFromClause *C) {
3268  TRY_TO(VisitOMPClauseList(C));
3269  return true;
3270}
3271
3272template <typename Derived>
3273bool RecursiveASTVisitor<Derived>::VisitOMPUseDevicePtrClause(
3274    OMPUseDevicePtrClause *C) {
3275  TRY_TO(VisitOMPClauseList(C));
3276  return true;
3277}
3278
3279template <typename Derived>
3280bool RecursiveASTVisitor<Derived>::VisitOMPIsDevicePtrClause(
3281    OMPIsDevicePtrClause *C) {
3282  TRY_TO(VisitOMPClauseList(C));
3283  return true;
3284}
3285
3286// FIXME: look at the following tricky-seeming exprs to see if we
3287// need to recurse on anything.  These are ones that have methods
3288// returning decls or qualtypes or nestednamespecifier -- though I'm
3289// not sure if they own them -- or just seemed very complicated, or
3290// had lots of sub-types to explore.
3291//
3292// VisitOverloadExpr and its children: recurse on template args? etc?
3293
3294// FIXME: go through all the stmts and exprs again, and see which of them
3295// create new types, and recurse on the types (TypeLocs?) of those.
3296// Candidates:
3297//
3298//    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
3299//    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
3300//    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
3301//    Every class that has getQualifier.
3302
3303#undef DEF_TRAVERSE_STMT
3304#undef TRAVERSE_STMT
3305#undef TRAVERSE_STMT_BASE
3306
3307#undef TRY_TO
3308
3309// end namespace clang
3310
3311#endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
3312
clang::RecursiveASTVisitor::getDerived
clang::RecursiveASTVisitor::shouldVisitTemplateInstantiations
clang::RecursiveASTVisitor::shouldWalkTypesOfTypeLocs
clang::RecursiveASTVisitor::shouldVisitImplicitCode
clang::RecursiveASTVisitor::shouldTraversePostOrder
clang::RecursiveASTVisitor::TraverseAST
clang::RecursiveASTVisitor::TraverseStmt
clang::RecursiveASTVisitor::dataTraverseStmtPre
clang::RecursiveASTVisitor::dataTraverseStmtPost
clang::RecursiveASTVisitor::TraverseType
clang::RecursiveASTVisitor::TraverseTypeLoc
clang::RecursiveASTVisitor::TraverseAttr
clang::RecursiveASTVisitor::TraverseDecl
clang::RecursiveASTVisitor::TraverseNestedNameSpecifier
clang::RecursiveASTVisitor::TraverseNestedNameSpecifierLoc
clang::RecursiveASTVisitor::TraverseDeclarationNameInfo
clang::RecursiveASTVisitor::TraverseTemplateName
clang::RecursiveASTVisitor::TraverseTemplateArgument
clang::RecursiveASTVisitor::TraverseTemplateArgumentLoc
clang::RecursiveASTVisitor::TraverseTemplateArguments
clang::RecursiveASTVisitor::TraverseCXXBaseSpecifier
clang::RecursiveASTVisitor::TraverseConstructorInitializer
clang::RecursiveASTVisitor::TraverseLambdaCapture
clang::RecursiveASTVisitor::TraverseSynOrSemInitListExpr
clang::RecursiveASTVisitor::VisitAttr
clang::RecursiveASTVisitor::getStmtChildren
clang::RecursiveASTVisitor::has_same_member_pointer_type
clang::RecursiveASTVisitor::WalkUpFromStmt
clang::RecursiveASTVisitor::VisitStmt
clang::RecursiveASTVisitor::WalkUpFromType
clang::RecursiveASTVisitor::VisitType
clang::RecursiveASTVisitor::WalkUpFromTypeLoc
clang::RecursiveASTVisitor::VisitTypeLoc
clang::RecursiveASTVisitor::WalkUpFromQualifiedTypeLoc
clang::RecursiveASTVisitor::VisitQualifiedTypeLoc
clang::RecursiveASTVisitor::WalkUpFromUnqualTypeLoc
clang::RecursiveASTVisitor::VisitUnqualTypeLoc
clang::RecursiveASTVisitor::WalkUpFromDecl
clang::RecursiveASTVisitor::VisitDecl
clang::RecursiveASTVisitor::canIgnoreChildDeclWhileTraversingDeclContext
clang::RecursiveASTVisitor::TraverseTemplateParameterListHelper
clang::RecursiveASTVisitor::TraverseDeclTemplateParameterLists
clang::RecursiveASTVisitor::TraverseTemplateArgumentLocsHelper
clang::RecursiveASTVisitor::TraverseArrayTypeLocHelper
clang::RecursiveASTVisitor::TraverseRecordHelper
clang::RecursiveASTVisitor::TraverseCXXRecordHelper
clang::RecursiveASTVisitor::TraverseDeclaratorHelper
clang::RecursiveASTVisitor::TraverseDeclContextHelper
clang::RecursiveASTVisitor::TraverseFunctionHelper
clang::RecursiveASTVisitor::TraverseVarHelper
clang::RecursiveASTVisitor::TraverseOMPExecutableDirective
clang::RecursiveASTVisitor::TraverseOMPLoopDirective
clang::RecursiveASTVisitor::TraverseOMPClause
clang::RecursiveASTVisitor::VisitOMPClauseList
clang::RecursiveASTVisitor::VisitOMPClauseWithPreInit
clang::RecursiveASTVisitor::VisitOMPClauseWithPostUpdate
clang::RecursiveASTVisitor::dataTraverseNode
clang::RecursiveASTVisitor::PostVisitStmt
clang::RecursiveASTVisitor::dataTraverseNode
clang::RecursiveASTVisitor::PostVisitStmt
clang::RecursiveASTVisitor::TraverseStmt
clang::RecursiveASTVisitor::TraverseType
clang::RecursiveASTVisitor::TraverseTypeLoc
clang::RecursiveASTVisitor::TraverseDecl
clang::RecursiveASTVisitor::TraverseNestedNameSpecifier
clang::RecursiveASTVisitor::TraverseNestedNameSpecifierLoc
clang::RecursiveASTVisitor::TraverseDeclarationNameInfo
clang::RecursiveASTVisitor::TraverseTemplateName
clang::RecursiveASTVisitor::TraverseTemplateArgument
clang::RecursiveASTVisitor::TraverseTemplateArgumentLoc
clang::RecursiveASTVisitor::TraverseTemplateArguments
clang::RecursiveASTVisitor::TraverseConstructorInitializer
clang::RecursiveASTVisitor::TraverseLambdaCapture
clang::RecursiveASTVisitor::TraverseQualifiedTypeLoc
clang::RecursiveASTVisitor::TraverseArrayTypeLocHelper
clang::RecursiveASTVisitor::canIgnoreChildDeclWhileTraversingDeclContext
clang::RecursiveASTVisitor::TraverseDeclContextHelper
clang::RecursiveASTVisitor::TraverseTemplateParameterListHelper
clang::RecursiveASTVisitor::TraverseDeclTemplateParameterLists
clang::RecursiveASTVisitor::TraverseTemplateInstantiations
clang::RecursiveASTVisitor::TraverseTemplateInstantiations
clang::RecursiveASTVisitor::TraverseTemplateInstantiations
clang::RecursiveASTVisitor::TraverseRecordHelper
clang::RecursiveASTVisitor::TraverseCXXBaseSpecifier
clang::RecursiveASTVisitor::TraverseCXXRecordHelper
clang::RecursiveASTVisitor::TraverseTemplateArgumentLocsHelper
clang::RecursiveASTVisitor::TraverseDeclaratorHelper
clang::RecursiveASTVisitor::TraverseFunctionHelper
clang::RecursiveASTVisitor::TraverseVarHelper
clang::RecursiveASTVisitor::TraverseSynOrSemInitListExpr
clang::RecursiveASTVisitor::TraverseInitListExpr
clang::RecursiveASTVisitor::TraverseOMPExecutableDirective
clang::RecursiveASTVisitor::TraverseOMPLoopDirective
clang::RecursiveASTVisitor::TraverseOMPClause
clang::RecursiveASTVisitor::VisitOMPClauseWithPreInit
clang::RecursiveASTVisitor::VisitOMPClauseWithPostUpdate
clang::RecursiveASTVisitor::VisitOMPAllocatorClause
clang::RecursiveASTVisitor::VisitOMPAllocateClause
clang::RecursiveASTVisitor::VisitOMPIfClause
clang::RecursiveASTVisitor::VisitOMPFinalClause
clang::RecursiveASTVisitor::VisitOMPNumThreadsClause
clang::RecursiveASTVisitor::VisitOMPSafelenClause
clang::RecursiveASTVisitor::VisitOMPSimdlenClause
clang::RecursiveASTVisitor::VisitOMPCollapseClause
clang::RecursiveASTVisitor::VisitOMPDefaultClause
clang::RecursiveASTVisitor::VisitOMPProcBindClause
clang::RecursiveASTVisitor::VisitOMPUnifiedAddressClause
clang::RecursiveASTVisitor::VisitOMPUnifiedSharedMemoryClause
clang::RecursiveASTVisitor::VisitOMPReverseOffloadClause
clang::RecursiveASTVisitor::VisitOMPDynamicAllocatorsClause
clang::RecursiveASTVisitor::VisitOMPAtomicDefaultMemOrderClause
clang::RecursiveASTVisitor::VisitOMPScheduleClause
clang::RecursiveASTVisitor::VisitOMPOrderedClause
clang::RecursiveASTVisitor::VisitOMPNowaitClause
clang::RecursiveASTVisitor::VisitOMPUntiedClause
clang::RecursiveASTVisitor::VisitOMPMergeableClause
clang::RecursiveASTVisitor::VisitOMPReadClause
clang::RecursiveASTVisitor::VisitOMPWriteClause
clang::RecursiveASTVisitor::VisitOMPUpdateClause
clang::RecursiveASTVisitor::VisitOMPCaptureClause
clang::RecursiveASTVisitor::VisitOMPSeqCstClause
clang::RecursiveASTVisitor::VisitOMPThreadsClause
clang::RecursiveASTVisitor::VisitOMPSIMDClause
clang::RecursiveASTVisitor::VisitOMPNogroupClause
clang::RecursiveASTVisitor::VisitOMPClauseList
clang::RecursiveASTVisitor::VisitOMPPrivateClause
clang::RecursiveASTVisitor::VisitOMPFirstprivateClause
clang::RecursiveASTVisitor::VisitOMPLastprivateClause
clang::RecursiveASTVisitor::VisitOMPSharedClause
clang::RecursiveASTVisitor::VisitOMPLinearClause
clang::RecursiveASTVisitor::VisitOMPAlignedClause
clang::RecursiveASTVisitor::VisitOMPCopyinClause
clang::RecursiveASTVisitor::VisitOMPCopyprivateClause
clang::RecursiveASTVisitor::VisitOMPReductionClause
clang::RecursiveASTVisitor::VisitOMPTaskReductionClause
clang::RecursiveASTVisitor::VisitOMPInReductionClause
clang::RecursiveASTVisitor::VisitOMPFlushClause
clang::RecursiveASTVisitor::VisitOMPDependClause
clang::RecursiveASTVisitor::VisitOMPDeviceClause
clang::RecursiveASTVisitor::VisitOMPMapClause
clang::RecursiveASTVisitor::VisitOMPNumTeamsClause
clang::RecursiveASTVisitor::VisitOMPThreadLimitClause
clang::RecursiveASTVisitor::VisitOMPPriorityClause
clang::RecursiveASTVisitor::VisitOMPGrainsizeClause
clang::RecursiveASTVisitor::VisitOMPNumTasksClause
clang::RecursiveASTVisitor::VisitOMPHintClause
clang::RecursiveASTVisitor::VisitOMPDistScheduleClause
clang::RecursiveASTVisitor::VisitOMPDefaultmapClause
clang::RecursiveASTVisitor::VisitOMPToClause
clang::RecursiveASTVisitor::VisitOMPFromClause
clang::RecursiveASTVisitor::VisitOMPUseDevicePtrClause
clang::RecursiveASTVisitor::VisitOMPIsDevicePtrClause