clang 21.0.0git
DynamicRecursiveASTVisitor.cpp
Go to the documentation of this file.
1//=== DynamicRecursiveASTVisitor.cpp - Dynamic AST Visitor Implementation -===//
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 implements DynamicRecursiveASTVisitor in terms of the CRTP-based
10// RecursiveASTVisitor.
11//
12//===----------------------------------------------------------------------===//
15
16using namespace clang;
17
18// The implementation of DRAV deserves some explanation:
19//
20// We want to implement DynamicRecursiveASTVisitor without having to inherit or
21// reference RecursiveASTVisitor in any way in the header: if we instantiate
22// RAV in the header, then every user of (or rather every file that uses) DRAV
23// still has to instantiate a RAV, which gets us nowhere. Moreover, even just
24// including RecursiveASTVisitor.h would probably cause some amount of slowdown
25// because we'd have to parse a huge template. For these reasons, the fact that
26// DRAV is implemented using a RAV is solely an implementation detail.
27//
28// As for the implementation itself, DRAV by default acts exactly like a RAV
29// that overrides none of RAV's functions. There are two parts to this:
30//
31// 1. Any function in DRAV has to act like the corresponding function in RAV,
32// unless overridden by a derived class, of course.
33//
34// 2. Any call to a function by the RAV implementation that DRAV allows to be
35// overridden must be transformed to a virtual call on the user-provided
36// DRAV object: if some function in RAV calls e.g. TraverseCallExpr()
37// during traversal, then the derived class's TraverseCallExpr() must be
38// called (provided it overrides TraverseCallExpr()).
39//
40// The 'Impl' class is a helper that connects the two implementations; it is
41// a wrapper around a reference to a DRAV that is itself a RecursiveASTVisitor.
42// It overrides every function in RAV *that is virtual in DRAV* to perform a
43// virtual call on its DRAV reference. This accomplishes point 2 above.
44//
45// Point 1 is accomplished by, first, having the base class implementation of
46// each of the virtual functions construct an Impl object (which is actually
47// just a no-op), passing in itself so that any virtual calls use the right
48// vtable. Secondly, it then calls RAV's implementation of that same function
49// *on Impl* (using a qualified call so that we actually call into the RAV
50// implementation instead of Impl's version of that same function); this way,
51// we both execute RAV's implementation for this function only and ensure that
52// calls to subsequent functions call into Impl via CRTP (and Impl then calls
53// back into DRAV and so on).
54//
55// While this ends up constructing a lot of Impl instances (almost one per
56// function call), this doesn't really matter since Impl just holds a single
57// pointer, and everything in this file should get inlined into all the DRAV
58// functions here anyway.
59//
60//===----------------------------------------------------------------------===//
61//
62// The following illustrates how a call to an (overridden) function is actually
63// resolved: given some class 'Derived' that derives from DRAV and overrides
64// TraverseStmt(), if we are traversing some AST, and TraverseStmt() is called
65// by the RAV implementation, the following happens:
66//
67// 1. Impl::TraverseStmt() overrides RAV::TraverseStmt() via CRTP, so the
68// former is called.
69//
70// 2. Impl::TraverseStmt() performs a virtual call to the visitor (which is
71// an instance to Derived), so Derived::TraverseStmt() is called.
72//
73// End result: Derived::TraverseStmt() is executed.
74//
75// Suppose some other function, e.g. TraverseCallExpr(), which is NOT overridden
76// by Derived is called, we get:
77//
78// 1. Impl::TraverseCallExpr() overrides RAV::TraverseCallExpr() via CRTP,
79// so the former is called.
80//
81// 2. Impl::TraverseCallExpr() performs a virtual call, but since Derived
82// does not override that function, DRAV::TraverseCallExpr() is called.
83//
84// 3. DRAV::TraverseCallExpr() creates a new instance of Impl, passing in
85// itself (this doesn't change that the pointer is an instance of Derived);
86// it then calls RAV::TraverseCallExpr() on the Impl object, which actually
87// ends up executing RAV's implementation because we used a qualified
88// function call.
89//
90// End result: RAV::TraverseCallExpr() is executed,
91namespace {
92template <bool Const> struct Impl : RecursiveASTVisitor<Impl<Const>> {
94 Impl(DynamicRecursiveASTVisitorBase<Const> &Visitor) : Visitor(Visitor) {}
95
97 return Visitor.ShouldVisitTemplateInstantiations;
98 }
99
100 bool shouldWalkTypesOfTypeLocs() const {
101 return Visitor.ShouldWalkTypesOfTypeLocs;
102 }
103
104 bool shouldVisitImplicitCode() const {
105 return Visitor.ShouldVisitImplicitCode;
106 }
107
108 bool shouldVisitLambdaBody() const { return Visitor.ShouldVisitLambdaBody; }
109
110 // Supporting post-order would be very hard because of quirks of the
111 // RAV implementation that only work with CRTP. It also is only used
112 // by less than 5 visitors in the entire code base.
113 bool shouldTraversePostOrder() const { return false; }
114
115 bool TraverseAST(ASTContext &AST) { return Visitor.TraverseAST(AST); }
116 bool TraverseAttr(Attr *At) { return Visitor.TraverseAttr(At); }
117 bool TraverseDecl(Decl *D) { return Visitor.TraverseDecl(D); }
118 bool TraverseType(QualType T) { return Visitor.TraverseType(T); }
119 bool TraverseTypeLoc(TypeLoc TL) { return Visitor.TraverseTypeLoc(TL); }
120 bool TraverseStmt(Stmt *S) { return Visitor.TraverseStmt(S); }
121
123 return Visitor.TraverseConstructorInitializer(Init);
124 }
125
127 return Visitor.TraverseTemplateArgument(Arg);
128 }
129
131 return Visitor.TraverseTemplateArgumentLoc(ArgLoc);
132 }
133
134 bool TraverseTemplateName(TemplateName Template) {
135 return Visitor.TraverseTemplateName(Template);
136 }
137
138 bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) {
139 return Visitor.TraverseObjCProtocolLoc(ProtocolLoc);
140 }
141
143 return Visitor.TraverseTypeConstraint(C);
144 }
146 return Visitor.TraverseConceptRequirement(R);
147 }
149 return Visitor.TraverseConceptTypeRequirement(R);
150 }
152 return Visitor.TraverseConceptExprRequirement(R);
153 }
155 return Visitor.TraverseConceptNestedRequirement(R);
156 }
157
159 return Visitor.TraverseConceptReference(CR);
160 }
161
163 return Visitor.TraverseCXXBaseSpecifier(Base);
164 }
165
167 return Visitor.TraverseDeclarationNameInfo(NameInfo);
168 }
169
171 Expr *Init) {
172 return Visitor.TraverseLambdaCapture(LE, C, Init);
173 }
174
176 return Visitor.TraverseNestedNameSpecifier(NNS);
177 }
178
180 return Visitor.TraverseNestedNameSpecifierLoc(NNS);
181 }
182
184 return Visitor.VisitConceptReference(CR);
185 }
186
187 bool dataTraverseStmtPre(Stmt *S) { return Visitor.dataTraverseStmtPre(S); }
188 bool dataTraverseStmtPost(Stmt *S) { return Visitor.dataTraverseStmtPost(S); }
189
190 // TraverseStmt() always passes in a queue, so we have no choice but to
191 // accept it as a parameter here.
192 bool dataTraverseNode(
193 Stmt *S,
195 // But since we don't support postorder traversal, we don't need it, so
196 // simply discard it here. This way, derived classes don't need to worry
197 // about including it as a parameter that they never use.
198 return Visitor.dataTraverseNode(S);
199 }
200
201 /// Visit a node.
202 bool VisitAttr(Attr *A) { return Visitor.VisitAttr(A); }
203 bool VisitDecl(Decl *D) { return Visitor.VisitDecl(D); }
204 bool VisitStmt(Stmt *S) { return Visitor.VisitStmt(S); }
205 bool VisitType(Type *T) { return Visitor.VisitType(T); }
206 bool VisitTypeLoc(TypeLoc TL) { return Visitor.VisitTypeLoc(TL); }
207
208#define DEF_TRAVERSE_TMPL_INST(kind) \
209 bool TraverseTemplateInstantiations(kind##TemplateDecl *D) { \
210 return Visitor.TraverseTemplateInstantiations(D); \
211 }
214 DEF_TRAVERSE_TMPL_INST(Function)
215#undef DEF_TRAVERSE_TMPL_INST
216
217 // Decls.
218#define ABSTRACT_DECL(DECL)
219#define DECL(CLASS, BASE) \
220 bool Traverse##CLASS##Decl(CLASS##Decl *D) { \
221 return Visitor.Traverse##CLASS##Decl(D); \
222 }
223#include "clang/AST/DeclNodes.inc"
224
225#define DECL(CLASS, BASE) \
226 bool Visit##CLASS##Decl(CLASS##Decl *D) { \
227 return Visitor.Visit##CLASS##Decl(D); \
228 }
229#include "clang/AST/DeclNodes.inc"
230
231 // Stmts.
232#define ABSTRACT_STMT(STMT)
233#define STMT(CLASS, PARENT) \
234 bool Traverse##CLASS(CLASS *S) { return Visitor.Traverse##CLASS(S); }
235#include "clang/AST/StmtNodes.inc"
236
237#define STMT(CLASS, PARENT) \
238 bool Visit##CLASS(CLASS *S) { return Visitor.Visit##CLASS(S); }
239#include "clang/AST/StmtNodes.inc"
240
241 // Types.
242#define ABSTRACT_TYPE(CLASS, BASE)
243#define TYPE(CLASS, BASE) \
244 bool Traverse##CLASS##Type(CLASS##Type *T) { \
245 return Visitor.Traverse##CLASS##Type(T); \
246 }
247#include "clang/AST/TypeNodes.inc"
248
249#define TYPE(CLASS, BASE) \
250 bool Visit##CLASS##Type(CLASS##Type *T) { \
251 return Visitor.Visit##CLASS##Type(T); \
252 }
253#include "clang/AST/TypeNodes.inc"
254
255 // TypeLocs.
256#define ABSTRACT_TYPELOC(CLASS, BASE)
257#define TYPELOC(CLASS, BASE) \
258 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
259 return Visitor.Traverse##CLASS##TypeLoc(TL); \
260 }
261#include "clang/AST/TypeLocNodes.def"
262
263#define TYPELOC(CLASS, BASE) \
264 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
265 return Visitor.Visit##CLASS##TypeLoc(TL); \
266 }
267#include "clang/AST/TypeLocNodes.def"
268};
269} // namespace
270
272
273// Helper macros to forward a call to the base implementation since that
274// ends up getting very verbose otherwise.
275
276// This calls the RecursiveASTVisitor implementation of the same function,
277// stripping any 'const' that the DRAV implementation may have added since
278// the RAV implementation largely doesn't use 'const'.
279#define FORWARD_TO_BASE(Function, Type, RefOrPointer) \
280 template <bool Const> \
281 bool DynamicRecursiveASTVisitorBase<Const>::Function( \
282 MaybeConst<Type> RefOrPointer Param) { \
283 return Impl<Const>(*this).RecursiveASTVisitor<Impl<Const>>::Function( \
284 const_cast<Type RefOrPointer>(Param)); \
285 }
286
287// Same as 'FORWARD_TO_BASE', but doesn't change the parameter type in any way.
288#define FORWARD_TO_BASE_EXACT(Function, Type) \
289 template <bool Const> \
290 bool DynamicRecursiveASTVisitorBase<Const>::Function(Type Param) { \
291 return Impl<Const>(*this).RecursiveASTVisitor<Impl<Const>>::Function( \
292 Param); \
293 }
294
295FORWARD_TO_BASE(TraverseAST, ASTContext, &)
296FORWARD_TO_BASE(TraverseAttr, Attr, *)
297FORWARD_TO_BASE(TraverseConstructorInitializer, CXXCtorInitializer, *)
298FORWARD_TO_BASE(TraverseDecl, Decl, *)
299FORWARD_TO_BASE(TraverseStmt, Stmt, *)
300FORWARD_TO_BASE(TraverseNestedNameSpecifier, NestedNameSpecifier, *)
301FORWARD_TO_BASE(TraverseTemplateInstantiations, ClassTemplateDecl, *)
302FORWARD_TO_BASE(TraverseTemplateInstantiations, VarTemplateDecl, *)
303FORWARD_TO_BASE(TraverseTemplateInstantiations, FunctionTemplateDecl, *)
304FORWARD_TO_BASE(TraverseConceptRequirement, concepts::Requirement, *)
305FORWARD_TO_BASE(TraverseConceptTypeRequirement, concepts::TypeRequirement, *)
306FORWARD_TO_BASE(TraverseConceptExprRequirement, concepts::ExprRequirement, *)
307FORWARD_TO_BASE(TraverseConceptReference, ConceptReference, *)
308FORWARD_TO_BASE(TraverseConceptNestedRequirement,
309 concepts::NestedRequirement, *)
310
311FORWARD_TO_BASE_EXACT(TraverseCXXBaseSpecifier, const CXXBaseSpecifier &)
312FORWARD_TO_BASE_EXACT(TraverseDeclarationNameInfo, DeclarationNameInfo)
313FORWARD_TO_BASE_EXACT(TraverseTemplateArgument, const TemplateArgument &)
314FORWARD_TO_BASE_EXACT(TraverseTemplateArguments, ArrayRef<TemplateArgument>)
315FORWARD_TO_BASE_EXACT(TraverseTemplateArgumentLoc, const TemplateArgumentLoc &)
316FORWARD_TO_BASE_EXACT(TraverseTemplateName, TemplateName)
317FORWARD_TO_BASE_EXACT(TraverseType, QualType)
318FORWARD_TO_BASE_EXACT(TraverseTypeLoc, TypeLoc)
319FORWARD_TO_BASE_EXACT(TraverseTypeConstraint, const TypeConstraint *)
320FORWARD_TO_BASE_EXACT(TraverseObjCProtocolLoc, ObjCProtocolLoc)
321FORWARD_TO_BASE_EXACT(TraverseNestedNameSpecifierLoc, NestedNameSpecifierLoc)
322
323template <bool Const>
324bool DynamicRecursiveASTVisitorBase<Const>::TraverseLambdaCapture(
325 MaybeConst<LambdaExpr> *LE, const LambdaCapture *C,
326 MaybeConst<Expr> *Init) {
327 return Impl<Const>(*this)
328 .RecursiveASTVisitor<Impl<Const>>::TraverseLambdaCapture(
329 const_cast<LambdaExpr *>(LE), C, const_cast<Expr *>(Init));
330}
331
332template <bool Const>
334 MaybeConst<Stmt> *S) {
335 return Impl<Const>(*this).RecursiveASTVisitor<Impl<Const>>::dataTraverseNode(
336 const_cast<Stmt *>(S), nullptr);
337}
338
339// Declare Traverse*() for and friends all concrete Decl classes.
340#define ABSTRACT_DECL(DECL)
341#define DECL(CLASS, BASE) \
342 FORWARD_TO_BASE(Traverse##CLASS##Decl, CLASS##Decl, *) \
343 FORWARD_TO_BASE(WalkUpFrom##CLASS##Decl, CLASS##Decl, *)
344#include "clang/AST/DeclNodes.inc"
345
346// Declare Traverse*() and friends for all concrete Stmt classes.
347#define ABSTRACT_STMT(STMT)
348#define STMT(CLASS, PARENT) FORWARD_TO_BASE(Traverse##CLASS, CLASS, *)
349#include "clang/AST/StmtNodes.inc"
350
351#define STMT(CLASS, PARENT) FORWARD_TO_BASE(WalkUpFrom##CLASS, CLASS, *)
352#include "clang/AST/StmtNodes.inc"
353
354// Declare Traverse*() and friends for all concrete Type classes.
355#define ABSTRACT_TYPE(CLASS, BASE)
356#define TYPE(CLASS, BASE) \
357 FORWARD_TO_BASE(Traverse##CLASS##Type, CLASS##Type, *) \
358 FORWARD_TO_BASE(WalkUpFrom##CLASS##Type, CLASS##Type, *)
359#include "clang/AST/TypeNodes.inc"
360
361#define ABSTRACT_TYPELOC(CLASS, BASE)
362#define TYPELOC(CLASS, BASE) \
363 FORWARD_TO_BASE_EXACT(Traverse##CLASS##TypeLoc, CLASS##TypeLoc)
364#include "clang/AST/TypeLocNodes.def"
365
366#define TYPELOC(CLASS, BASE) \
367 FORWARD_TO_BASE_EXACT(WalkUpFrom##CLASS##TypeLoc, CLASS##TypeLoc)
368#include "clang/AST/TypeLocNodes.def"
369
370namespace clang {
373} // namespace clang
const Decl * D
#define FORWARD_TO_BASE_EXACT(Function, Type)
#define FORWARD_TO_BASE(Function, Type, RefOrPointer)
#define DEF_TRAVERSE_TMPL_INST(kind)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Attr - This represents one attribute.
Definition: Attr.h:43
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2357
Declaration of a class template.
A reference to a concept and its template args, as it appears in the code.
Definition: ASTConcept.h:124
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Recursive AST visitor that supports extension via dynamic dispatch.
std::conditional_t< IsConst, const ASTNode, ASTNode > MaybeConst
virtual bool dataTraverseNode(MaybeConst< Stmt > *S)
This represents one expression.
Definition: Expr.h:110
Declaration of a template function.
Definition: DeclTemplate.h:958
Describes the capture of a variable or of this, or of a C++1y init-capture.
Definition: LambdaCapture.h:25
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1954
A C++ nested-name-specifier augmented with source location information.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
A (possibly-)qualified type.
Definition: Type.h:929
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
bool TraverseTemplateArgument(const TemplateArgument &Arg)
Recursively visit a template argument and dispatch to the appropriate method for the argument type.
bool TraverseConceptRequirement(concepts::Requirement *R)
bool TraverseType(QualType T)
Recursively visit a type, by dispatching to Traverse*Type() based on the argument's getTypeClass() pr...
bool dataTraverseStmtPre(Stmt *S)
Invoked before visiting a statement or expression via data recursion.
bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc)
Recursively visit an Objective-C protocol reference with location information.
bool TraverseConceptExprRequirement(concepts::ExprRequirement *R)
bool TraverseAST(ASTContext &AST)
Recursively visits an entire AST, starting from the TranslationUnitDecl.
bool shouldVisitTemplateInstantiations() const
Return whether this visitor should recurse into template instantiations.
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
Recursively visit a template argument location and dispatch to the appropriate method for the argumen...
bool dataTraverseStmtPost(Stmt *S)
Invoked after visiting a statement or expression via data recursion.
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Recursively visit a C++ nested-name-specifier with location information.
bool TraverseTemplateName(TemplateName Template)
Recursively visit a template name and dispatch to the appropriate method.
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
Recursively visit a C++ nested-name-specifier.
bool shouldVisitImplicitCode() const
Return whether this visitor should recurse into implicit code, e.g., implicit constructors and destru...
bool TraverseConceptReference(ConceptReference *CR)
Recursively visit concept reference with location information.
bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue)
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
bool TraverseTypeLoc(TypeLoc TL)
Recursively visit a type with location, by dispatching to Traverse*TypeLoc() based on the argument ty...
bool TraverseTypeConstraint(const TypeConstraint *C)
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
Recursively visit a lambda capture.
bool VisitConceptReference(ConceptReference *CR)
bool shouldTraversePostOrder() const
Return whether this visitor should traverse post-order.
bool shouldVisitLambdaBody() const
Return whether this visitor should recurse into lambda body.
bool TraverseAttr(Attr *At)
Recursively visit an attribute, by dispatching to Traverse*Attr() based on the argument's dynamic typ...
bool TraverseConceptNestedRequirement(concepts::NestedRequirement *R)
bool shouldWalkTypesOfTypeLocs() const
Return whether this visitor should recurse into the types of TypeLocs.
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
Recursively visit a name with its location information.
bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base)
Recursively visit a base specifier.
bool TraverseConceptTypeRequirement(concepts::TypeRequirement *R)
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
Recursively visit a constructor initializer.
Stmt - This represents one statement.
Definition: Stmt.h:84
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:524
Represents a template argument.
Definition: TemplateBase.h:61
Represents a C++ template name within the type system.
Definition: TemplateName.h:220
Models the abbreviated syntax to constrain a template type parameter: template <convertible_to<string...
Definition: ASTConcept.h:227
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
The base class of the type hierarchy.
Definition: Type.h:1828
Declaration of a variable template.
A requires-expression requirement which queries the validity and properties of an expression ('simple...
Definition: ExprConcepts.h:280
A requires-expression requirement which is satisfied when a general constraint expression is satisfie...
Definition: ExprConcepts.h:429
A static requirement that can be used in a requires-expression to check properties of types and expre...
Definition: ExprConcepts.h:168
A requires-expression requirement which queries the existence of a type name or type template special...
Definition: ExprConcepts.h:225
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...