clang  16.0.0git
Public Types | Public Member Functions | List of all members
clang::RecursiveASTVisitor< Derived > Class Template Reference

A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each node. More...

#include "clang/AST/RecursiveASTVisitor.h"

Inheritance diagram for clang::RecursiveASTVisitor< Derived >:
Inheritance graph
[legend]

Public Types

typedef SmallVectorImpl< llvm::PointerIntPair< Stmt *, 1, bool > > DataRecursionQueue
 A queue used for performing data recursion over statements. More...
 

Public Member Functions

Derived & getDerived ()
 Return a reference to the derived class. More...
 
bool shouldVisitTemplateInstantiations () const
 Return whether this visitor should recurse into template instantiations. More...
 
bool shouldWalkTypesOfTypeLocs () const
 Return whether this visitor should recurse into the types of TypeLocs. More...
 
bool shouldVisitImplicitCode () const
 Return whether this visitor should recurse into implicit code, e.g., implicit constructors and destructors. More...
 
bool shouldVisitLambdaBody () const
 Return whether this visitor should recurse into lambda body. More...
 
bool shouldTraversePostOrder () const
 Return whether this visitor should traverse post-order. More...
 
bool TraverseAST (ASTContext &AST)
 Recursively visits an entire AST, starting from the TranslationUnitDecl. More...
 
bool TraverseStmt (Stmt *S, DataRecursionQueue *Queue=nullptr)
 Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dynamic type. More...
 
bool dataTraverseStmtPre (Stmt *S)
 Invoked before visiting a statement or expression via data recursion. More...
 
bool dataTraverseStmtPost (Stmt *S)
 Invoked after visiting a statement or expression via data recursion. More...
 
bool TraverseType (QualType T)
 Recursively visit a type, by dispatching to Traverse*Type() based on the argument's getTypeClass() property. More...
 
bool TraverseTypeLoc (TypeLoc TL)
 Recursively visit a type with location, by dispatching to Traverse*TypeLoc() based on the argument type's getTypeClass() property. More...
 
bool TraverseAttr (Attr *At)
 Recursively visit an attribute, by dispatching to Traverse*Attr() based on the argument's dynamic type. More...
 
bool TraverseDecl (Decl *D)
 Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic type. More...
 
bool TraverseNestedNameSpecifier (NestedNameSpecifier *NNS)
 Recursively visit a C++ nested-name-specifier. More...
 
bool TraverseNestedNameSpecifierLoc (NestedNameSpecifierLoc NNS)
 Recursively visit a C++ nested-name-specifier with location information. More...
 
bool TraverseDeclarationNameInfo (DeclarationNameInfo NameInfo)
 Recursively visit a name with its location information. More...
 
bool TraverseTemplateName (TemplateName Template)
 Recursively visit a template name and dispatch to the appropriate method. More...
 
bool TraverseTemplateArgument (const TemplateArgument &Arg)
 Recursively visit a template argument and dispatch to the appropriate method for the argument type. More...
 
bool TraverseTemplateArgumentLoc (const TemplateArgumentLoc &ArgLoc)
 Recursively visit a template argument location and dispatch to the appropriate method for the argument type. More...
 
bool TraverseTemplateArguments (ArrayRef< TemplateArgument > Args)
 Recursively visit a set of template arguments. More...
 
bool TraverseCXXBaseSpecifier (const CXXBaseSpecifier &Base)
 Recursively visit a base specifier. More...
 
bool TraverseConstructorInitializer (CXXCtorInitializer *Init)
 Recursively visit a constructor initializer. More...
 
bool TraverseLambdaCapture (LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
 Recursively visit a lambda capture. More...
 
bool TraverseSynOrSemInitListExpr (InitListExpr *S, DataRecursionQueue *Queue=nullptr)
 Recursively visit the syntactic or semantic form of an initialization list. More...
 
bool TraverseObjCProtocolLoc (ObjCProtocolLoc ProtocolLoc)
 Recursively visit an Objective-C protocol reference with location information. More...
 
bool VisitAttr (Attr *A)
 
Stmt::child_range getStmtChildren (Stmt *S)
 
bool WalkUpFromStmt (Stmt *S)
 
bool VisitStmt (Stmt *S)
 
bool WalkUpFromType (Type *T)
 
bool VisitType (Type *T)
 
bool WalkUpFromTypeLoc (TypeLoc TL)
 
bool VisitTypeLoc (TypeLoc TL)
 
bool WalkUpFromQualifiedTypeLoc (QualifiedTypeLoc TL)
 
bool VisitQualifiedTypeLoc (QualifiedTypeLoc TL)
 
bool WalkUpFromUnqualTypeLoc (UnqualTypeLoc TL)
 
bool VisitUnqualTypeLoc (UnqualTypeLoc TL)
 
bool WalkUpFromDecl (Decl *D)
 
bool VisitDecl (Decl *D)
 
bool canIgnoreChildDeclWhileTraversingDeclContext (const Decl *Child)
 
bool TraverseTypeConstraint (const TypeConstraint *C)
 
bool TraverseConceptRequirement (concepts::Requirement *R)
 
bool TraverseConceptTypeRequirement (concepts::TypeRequirement *R)
 
bool TraverseConceptExprRequirement (concepts::ExprRequirement *R)
 
bool TraverseConceptNestedRequirement (concepts::NestedRequirement *R)
 
bool dataTraverseNode (Stmt *S, DataRecursionQueue *Queue)
 

Detailed Description

template<typename Derived>
class clang::RecursiveASTVisitor< Derived >

A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each node.

This class performs three distinct tasks:

  1. traverse the AST (i.e. go to each node);
  2. at a given node, walk up the class hierarchy, starting from the node's dynamic type, until the top-most class (e.g. Stmt, Decl, or Type) is reached.
  3. given a (node, class) combination, where 'class' is some base class of the dynamic type of 'node', call a user-overridable function to actually visit the node.

These tasks are done by three groups of methods, respectively:

  1. TraverseDecl(Decl *x) does task #1. It is the entry point for traversing an AST rooted at x. This method simply dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo is the dynamic type of *x, which calls WalkUpFromFoo(x) and then recursively visits the child nodes of x. TraverseStmt(Stmt *x) and TraverseType(QualType x) work similarly.
  2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit any child node of x. Instead, it first calls WalkUpFromBar(x) where Bar is the direct parent class of Foo (unless Foo has no parent), and then calls VisitFoo(x) (see the next list item).
  3. VisitFoo(Foo *x) does task #3.

These three method groups are tiered (Traverse* > WalkUpFrom* > Visit*). A method (e.g. Traverse*) may call methods from the same tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*). It may not call methods from a higher tier.

Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar is Foo's super class) before calling VisitFoo(), the result is that the Visit*() methods for a given node are called in the top-down order (e.g. for a node of type NamespaceDecl, the order will be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).

This scheme guarantees that all Visit*() calls for the same AST node are grouped together. In other words, Visit*() methods for different nodes are never interleaved.

Clients of this visitor should subclass the visitor (providing themselves as the template argument, using the curiously recurring template pattern) and override any of the Traverse*, WalkUpFrom*, and Visit* methods for declarations, types, statements, expressions, or other AST nodes where the visitor should customize behavior. Most users only need to override Visit*. Advanced users may override Traverse* and WalkUpFrom* to implement custom traversal strategies. Returning false from one of these overridden functions will abort the entire traversal.

By default, this visitor tries to visit every part of the explicit source code exactly once. The default policy towards templates is to descend into the 'pattern' class or function body, not any explicit or implicit instantiations. Explicit specializations are still visited, and the patterns of partial specializations are visited separately. This behavior can be changed by overriding shouldVisitTemplateInstantiations() in the derived class to return true, in which case all known implicit and explicit instantiations will be visited at the same time as the pattern from which they were produced.

By default, this visitor preorder traverses the AST. If postorder traversal is needed, the shouldTraversePostOrder method needs to be overridden to return true.

Definition at line 152 of file RecursiveASTVisitor.h.

Member Typedef Documentation

◆ DataRecursionQueue

template<typename Derived >
typedef SmallVectorImpl<llvm::PointerIntPair<Stmt *, 1, bool> > clang::RecursiveASTVisitor< Derived >::DataRecursionQueue

A queue used for performing data recursion over statements.

Parameters involving this type are used to implement data recursion over Stmts and Exprs within this class, and should typically not be explicitly specified by derived classes. The bool bit indicates whether the statement has been traversed or not.

Definition at line 160 of file RecursiveASTVisitor.h.

Member Function Documentation

◆ canIgnoreChildDeclWhileTraversingDeclContext()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::canIgnoreChildDeclWhileTraversingDeclContext ( const Decl Child)
Initial value:
{
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()))

Definition at line 1470 of file RecursiveASTVisitor.h.

Referenced by clang::LexicallyOrderedRecursiveASTVisitor< Derived >::TraverseDeclContextHelper().

◆ dataTraverseNode()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::dataTraverseNode ( Stmt S,
DataRecursionQueue Queue 
)

Definition at line 556 of file RecursiveASTVisitor.h.

◆ dataTraverseStmtPost()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::dataTraverseStmtPost ( Stmt S)
inline

Invoked after visiting a statement or expression via data recursion.

This is not invoked if the previously invoked dataTraverseStmtPre returned false.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 208 of file RecursiveASTVisitor.h.

◆ dataTraverseStmtPre()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::dataTraverseStmtPre ( Stmt S)
inline

Invoked before visiting a statement or expression via data recursion.

Returns
false to skip visiting the node, true otherwise.

Definition at line 201 of file RecursiveASTVisitor.h.

◆ getDerived()

template<typename Derived >
Derived& clang::RecursiveASTVisitor< Derived >::getDerived ( )
inline

◆ getStmtChildren()

template<typename Derived >
Stmt::child_range clang::RecursiveASTVisitor< Derived >::getStmtChildren ( Stmt S)
inline

Definition at line 327 of file RecursiveASTVisitor.h.

◆ shouldTraversePostOrder()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::shouldTraversePostOrder ( ) const
inline

Return whether this visitor should traverse post-order.

Definition at line 181 of file RecursiveASTVisitor.h.

◆ shouldVisitImplicitCode()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::shouldVisitImplicitCode ( ) const
inline

Return whether this visitor should recurse into implicit code, e.g., implicit constructors and destructors.

Definition at line 175 of file RecursiveASTVisitor.h.

◆ shouldVisitLambdaBody()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::shouldVisitLambdaBody ( ) const
inline

Return whether this visitor should recurse into lambda body.

Definition at line 178 of file RecursiveASTVisitor.h.

◆ shouldVisitTemplateInstantiations()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::shouldVisitTemplateInstantiations ( ) const
inline

Return whether this visitor should recurse into template instantiations.

Definition at line 167 of file RecursiveASTVisitor.h.

◆ shouldWalkTypesOfTypeLocs()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::shouldWalkTypesOfTypeLocs ( ) const
inline

Return whether this visitor should recurse into the types of TypeLocs.

Definition at line 171 of file RecursiveASTVisitor.h.

◆ TraverseAST()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseAST ( ASTContext AST)
inline

Recursively visits an entire AST, starting from the TranslationUnitDecl.

Returns
false if visitation was terminated early.

Definition at line 185 of file RecursiveASTVisitor.h.

Referenced by clang::ParentMapContext::ParentMap::ParentMap().

◆ TraverseAttr()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseAttr ( Attr At)

Recursively visit an attribute, by dispatching to Traverse*Attr() based on the argument's dynamic type.

Returns
false if the visitation was terminated early, true otherwise (including when the argument is a Null type location).

◆ TraverseConceptExprRequirement()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseConceptExprRequirement ( concepts::ExprRequirement R)

Definition at line 583 of file RecursiveASTVisitor.h.

◆ TraverseConceptNestedRequirement()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseConceptNestedRequirement ( concepts::NestedRequirement R)

Definition at line 601 of file RecursiveASTVisitor.h.

◆ TraverseConceptRequirement()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseConceptRequirement ( concepts::Requirement R)

Definition at line 526 of file RecursiveASTVisitor.h.

◆ TraverseConceptTypeRequirement()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseConceptTypeRequirement ( concepts::TypeRequirement R)

Definition at line 575 of file RecursiveASTVisitor.h.

◆ TraverseConstructorInitializer()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseConstructorInitializer ( CXXCtorInitializer Init)

Recursively visit a constructor initializer.

This automatically dispatches to another visitor for the initializer expression, but not for the name of the initializer, so may be overridden for clients that need access to the name.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 931 of file RecursiveASTVisitor.h.

◆ TraverseCXXBaseSpecifier()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseCXXBaseSpecifier ( const CXXBaseSpecifier Base)

Recursively visit a base specifier.

This can be overridden by a subclass.

Returns
false if the visitation was terminated early, true otherwise.

◆ TraverseDecl()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseDecl ( Decl D)

Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic type.

Returns
false if the visitation was terminated early, true otherwise (including when the argument is NULL).

Definition at line 736 of file RecursiveASTVisitor.h.

Referenced by clang::LexicallyOrderedRecursiveASTVisitor< Derived >::TraverseDeclContextHelper().

◆ TraverseDeclarationNameInfo()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseDeclarationNameInfo ( DeclarationNameInfo  NameInfo)

Recursively visit a name with its location information.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 815 of file RecursiveASTVisitor.h.

◆ TraverseLambdaCapture()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseLambdaCapture ( LambdaExpr LE,
const LambdaCapture C,
Expr Init 
)

Recursively visit a lambda capture.

Init is the expression that will be used to initialize the capture.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 944 of file RecursiveASTVisitor.h.

◆ TraverseNestedNameSpecifier()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseNestedNameSpecifier ( NestedNameSpecifier NNS)

Recursively visit a C++ nested-name-specifier.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 764 of file RecursiveASTVisitor.h.

◆ TraverseNestedNameSpecifierLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseNestedNameSpecifierLoc ( NestedNameSpecifierLoc  NNS)

Recursively visit a C++ nested-name-specifier with location information.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 789 of file RecursiveASTVisitor.h.

◆ TraverseObjCProtocolLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseObjCProtocolLoc ( ObjCProtocolLoc  ProtocolLoc)

Recursively visit an Objective-C protocol reference with location information.

Returns
false if the visitation was terminated early, true otherwise.

◆ TraverseStmt()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseStmt ( Stmt S,
DataRecursionQueue Queue = nullptr 
)

Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dynamic type.

Returns
false if the visitation was terminated early, true otherwise (including when the argument is nullptr).

Definition at line 657 of file RecursiveASTVisitor.h.

◆ TraverseSynOrSemInitListExpr()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseSynOrSemInitListExpr ( InitListExpr S,
DataRecursionQueue Queue = nullptr 
)

Recursively visit the syntactic or semantic form of an initialization list.

Returns
false if the visitation was terminated early, true otherwise.

◆ TraverseTemplateArgument()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseTemplateArgument ( const TemplateArgument Arg)

Recursively visit a template argument and dispatch to the appropriate method for the argument type.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 854 of file RecursiveASTVisitor.h.

◆ TraverseTemplateArgumentLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseTemplateArgumentLoc ( const TemplateArgumentLoc ArgLoc)

Recursively visit a template argument location and dispatch to the appropriate method for the argument type.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 884 of file RecursiveASTVisitor.h.

◆ TraverseTemplateArguments()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseTemplateArguments ( ArrayRef< TemplateArgument Args)

Recursively visit a set of template arguments.

This can be overridden by a subclass, but it's not expected that will be needed – this visitor always dispatches to another.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 922 of file RecursiveASTVisitor.h.

◆ TraverseTemplateName()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseTemplateName ( TemplateName  Template)

Recursively visit a template name and dispatch to the appropriate method.

Returns
false if the visitation was terminated early, true otherwise.

Definition at line 844 of file RecursiveASTVisitor.h.

◆ TraverseType()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseType ( QualType  T)

Recursively visit a type, by dispatching to Traverse*Type() based on the argument's getTypeClass() property.

Returns
false if the visitation was terminated early, true otherwise (including when the argument is a Null type).

Definition at line 698 of file RecursiveASTVisitor.h.

◆ TraverseTypeConstraint()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseTypeConstraint ( const TypeConstraint C)

Definition at line 507 of file RecursiveASTVisitor.h.

◆ TraverseTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::TraverseTypeLoc ( TypeLoc  TL)

Recursively visit a type with location, by dispatching to Traverse*TypeLoc() based on the argument type's getTypeClass() property.

Returns
false if the visitation was terminated early, true otherwise (including when the argument is a Null type location).

Definition at line 715 of file RecursiveASTVisitor.h.

◆ VisitAttr()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitAttr ( Attr A)
inline

Definition at line 318 of file RecursiveASTVisitor.h.

◆ VisitDecl()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitDecl ( Decl D)
inline

Definition at line 441 of file RecursiveASTVisitor.h.

◆ VisitQualifiedTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitQualifiedTypeLoc ( QualifiedTypeLoc  TL)
inline

Definition at line 415 of file RecursiveASTVisitor.h.

◆ VisitStmt()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitStmt ( Stmt S)
inline

Definition at line 366 of file RecursiveASTVisitor.h.

◆ VisitType()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitType ( Type T)
inline

Definition at line 387 of file RecursiveASTVisitor.h.

◆ VisitTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitTypeLoc ( TypeLoc  TL)
inline

Definition at line 408 of file RecursiveASTVisitor.h.

◆ VisitUnqualTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::VisitUnqualTypeLoc ( UnqualTypeLoc  TL)
inline

Definition at line 419 of file RecursiveASTVisitor.h.

◆ WalkUpFromDecl()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::WalkUpFromDecl ( Decl D)
inline

Definition at line 440 of file RecursiveASTVisitor.h.

◆ WalkUpFromQualifiedTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::WalkUpFromQualifiedTypeLoc ( QualifiedTypeLoc  TL)
inline

Definition at line 412 of file RecursiveASTVisitor.h.

◆ WalkUpFromStmt()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::WalkUpFromStmt ( Stmt S)
inline

Definition at line 365 of file RecursiveASTVisitor.h.

◆ WalkUpFromType()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::WalkUpFromType ( Type T)
inline

Definition at line 386 of file RecursiveASTVisitor.h.

◆ WalkUpFromTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::WalkUpFromTypeLoc ( TypeLoc  TL)
inline

Definition at line 407 of file RecursiveASTVisitor.h.

◆ WalkUpFromUnqualTypeLoc()

template<typename Derived >
bool clang::RecursiveASTVisitor< Derived >::WalkUpFromUnqualTypeLoc ( UnqualTypeLoc  TL)
inline

Definition at line 416 of file RecursiveASTVisitor.h.


The documentation for this class was generated from the following file:
clang::RecursiveASTVisitor::TraverseStmt
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
Definition: RecursiveASTVisitor.h:657
clang::TRY_TO
TRY_TO(TraverseType(T->getPointeeType()))