Skip to content

Commit 39a72be

Browse files
authored
[Clang] [NFC] Introduce ConstDynamicRecursiveASTVisitor (reland) (#124821)
This relands #122991 (eeefa72). The last attempt at landing this caused some problems; I’m not entirely sure what happened, but it might have been due to an unnecessary use of the `template` keyword in a few places. This removes that and attempts to land the change again.
1 parent e9c2e0a commit 39a72be

File tree

3 files changed

+159
-210
lines changed

3 files changed

+159
-210
lines changed

clang/include/clang/AST/DynamicRecursiveASTVisitor.h

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ class ASTContext;
5252
/// WalkUpFromX or post-order traversal).
5353
///
5454
/// \see RecursiveASTVisitor.
55-
class DynamicRecursiveASTVisitor {
55+
template <bool IsConst> class DynamicRecursiveASTVisitorBase {
56+
protected:
57+
template <typename ASTNode>
58+
using MaybeConst = std::conditional_t<IsConst, const ASTNode, ASTNode>;
59+
5660
public:
5761
/// Whether this visitor should recurse into template instantiations.
5862
bool ShouldVisitTemplateInstantiations = false;
@@ -68,36 +72,38 @@ class DynamicRecursiveASTVisitor {
6872
bool ShouldVisitLambdaBody = true;
6973

7074
protected:
71-
DynamicRecursiveASTVisitor() = default;
72-
DynamicRecursiveASTVisitor(DynamicRecursiveASTVisitor &&) = default;
73-
DynamicRecursiveASTVisitor(const DynamicRecursiveASTVisitor &) = default;
74-
DynamicRecursiveASTVisitor &
75-
operator=(DynamicRecursiveASTVisitor &&) = default;
76-
DynamicRecursiveASTVisitor &
77-
operator=(const DynamicRecursiveASTVisitor &) = default;
75+
DynamicRecursiveASTVisitorBase() = default;
76+
DynamicRecursiveASTVisitorBase(DynamicRecursiveASTVisitorBase &&) = default;
77+
DynamicRecursiveASTVisitorBase(const DynamicRecursiveASTVisitorBase &) =
78+
default;
79+
DynamicRecursiveASTVisitorBase &
80+
operator=(DynamicRecursiveASTVisitorBase &&) = default;
81+
DynamicRecursiveASTVisitorBase &
82+
operator=(const DynamicRecursiveASTVisitorBase &) = default;
7883

7984
public:
8085
virtual void anchor();
81-
virtual ~DynamicRecursiveASTVisitor() = default;
86+
virtual ~DynamicRecursiveASTVisitorBase() = default;
8287

8388
/// Recursively visits an entire AST, starting from the TranslationUnitDecl.
8489
/// \returns false if visitation was terminated early.
85-
virtual bool TraverseAST(ASTContext &AST);
90+
virtual bool TraverseAST(MaybeConst<ASTContext> &AST);
8691

8792
/// Recursively visit an attribute, by dispatching to
8893
/// Traverse*Attr() based on the argument's dynamic type.
8994
///
9095
/// \returns false if the visitation was terminated early, true
9196
/// otherwise (including when the argument is a Null type location).
92-
virtual bool TraverseAttr(Attr *At);
97+
virtual bool TraverseAttr(MaybeConst<Attr> *At);
9398

9499
/// Recursively visit a constructor initializer. This
95100
/// automatically dispatches to another visitor for the initializer
96101
/// expression, but not for the name of the initializer, so may
97102
/// be overridden for clients that need access to the name.
98103
///
99104
/// \returns false if the visitation was terminated early, true otherwise.
100-
virtual bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
105+
virtual bool
106+
TraverseConstructorInitializer(MaybeConst<CXXCtorInitializer> *Init);
101107

102108
/// Recursively visit a base specifier. This can be overridden by a
103109
/// subclass.
@@ -110,7 +116,7 @@ class DynamicRecursiveASTVisitor {
110116
///
111117
/// \returns false if the visitation was terminated early, true
112118
/// otherwise (including when the argument is NULL).
113-
virtual bool TraverseDecl(Decl *D);
119+
virtual bool TraverseDecl(MaybeConst<Decl> *D);
114120

115121
/// Recursively visit a name with its location information.
116122
///
@@ -121,13 +127,15 @@ class DynamicRecursiveASTVisitor {
121127
/// will be used to initialize the capture.
122128
///
123129
/// \returns false if the visitation was terminated early, true otherwise.
124-
virtual bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
125-
Expr *Init);
130+
virtual bool TraverseLambdaCapture(MaybeConst<LambdaExpr> *LE,
131+
const LambdaCapture *C,
132+
MaybeConst<Expr> *Init);
126133

127134
/// Recursively visit a C++ nested-name-specifier.
128135
///
129136
/// \returns false if the visitation was terminated early, true otherwise.
130-
virtual bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
137+
virtual bool
138+
TraverseNestedNameSpecifier(MaybeConst<NestedNameSpecifier> *NNS);
131139

132140
/// Recursively visit a C++ nested-name-specifier with location
133141
/// information.
@@ -140,7 +148,7 @@ class DynamicRecursiveASTVisitor {
140148
///
141149
/// \returns false if the visitation was terminated early, true
142150
/// otherwise (including when the argument is nullptr).
143-
virtual bool TraverseStmt(Stmt *S);
151+
virtual bool TraverseStmt(MaybeConst<Stmt> *S);
144152

145153
/// Recursively visit a template argument and dispatch to the
146154
/// appropriate method for the argument type.
@@ -190,74 +198,86 @@ class DynamicRecursiveASTVisitor {
190198

191199
/// Traverse a concept (requirement).
192200
virtual bool TraverseTypeConstraint(const TypeConstraint *C);
193-
virtual bool TraverseConceptRequirement(concepts::Requirement *R);
194-
virtual bool TraverseConceptTypeRequirement(concepts::TypeRequirement *R);
195-
virtual bool TraverseConceptExprRequirement(concepts::ExprRequirement *R);
196-
virtual bool TraverseConceptNestedRequirement(concepts::NestedRequirement *R);
197-
virtual bool TraverseConceptReference(ConceptReference *CR);
198-
virtual bool VisitConceptReference(ConceptReference *CR) { return true; }
201+
virtual bool TraverseConceptRequirement(MaybeConst<concepts::Requirement> *R);
202+
203+
virtual bool
204+
TraverseConceptTypeRequirement(MaybeConst<concepts::TypeRequirement> *R);
205+
206+
virtual bool
207+
TraverseConceptExprRequirement(MaybeConst<concepts::ExprRequirement> *R);
208+
209+
virtual bool
210+
TraverseConceptNestedRequirement(MaybeConst<concepts::NestedRequirement> *R);
211+
212+
virtual bool TraverseConceptReference(MaybeConst<ConceptReference> *CR);
213+
virtual bool VisitConceptReference(MaybeConst<ConceptReference> *CR) {
214+
return true;
215+
}
199216

200217
/// Visit a node.
201-
virtual bool VisitAttr(Attr *A) { return true; }
202-
virtual bool VisitDecl(Decl *D) { return true; }
203-
virtual bool VisitStmt(Stmt *S) { return true; }
204-
virtual bool VisitType(Type *T) { return true; }
218+
virtual bool VisitAttr(MaybeConst<Attr> *A) { return true; }
219+
virtual bool VisitDecl(MaybeConst<Decl> *D) { return true; }
220+
virtual bool VisitStmt(MaybeConst<Stmt> *S) { return true; }
221+
virtual bool VisitType(MaybeConst<Type> *T) { return true; }
205222
virtual bool VisitTypeLoc(TypeLoc TL) { return true; }
206223

207224
/// Walk up from a node.
208-
bool WalkUpFromDecl(Decl *D) { return VisitDecl(D); }
209-
bool WalkUpFromStmt(Stmt *S) { return VisitStmt(S); }
210-
bool WalkUpFromType(Type *T) { return VisitType(T); }
225+
bool WalkUpFromDecl(MaybeConst<Decl> *D) { return VisitDecl(D); }
226+
bool WalkUpFromStmt(MaybeConst<Stmt> *S) { return VisitStmt(S); }
227+
bool WalkUpFromType(MaybeConst<Type> *T) { return VisitType(T); }
211228
bool WalkUpFromTypeLoc(TypeLoc TL) { return VisitTypeLoc(TL); }
212229

213230
/// Invoked before visiting a statement or expression via data recursion.
214231
///
215232
/// \returns false to skip visiting the node, true otherwise.
216-
virtual bool dataTraverseStmtPre(Stmt *S) { return true; }
233+
virtual bool dataTraverseStmtPre(MaybeConst<Stmt> *S) { return true; }
217234

218235
/// Invoked after visiting a statement or expression via data recursion.
219236
/// This is not invoked if the previously invoked \c dataTraverseStmtPre
220237
/// returned false.
221238
///
222239
/// \returns false if the visitation was terminated early, true otherwise.
223-
virtual bool dataTraverseStmtPost(Stmt *S) { return true; }
224-
virtual bool dataTraverseNode(Stmt *S);
240+
virtual bool dataTraverseStmtPost(MaybeConst<Stmt> *S) { return true; }
241+
virtual bool dataTraverseNode(MaybeConst<Stmt> *S);
225242

226243
#define DEF_TRAVERSE_TMPL_INST(kind) \
227-
virtual bool TraverseTemplateInstantiations(kind##TemplateDecl *D);
244+
virtual bool TraverseTemplateInstantiations( \
245+
MaybeConst<kind##TemplateDecl> *D);
228246
DEF_TRAVERSE_TMPL_INST(Class)
229247
DEF_TRAVERSE_TMPL_INST(Var)
230248
DEF_TRAVERSE_TMPL_INST(Function)
231249
#undef DEF_TRAVERSE_TMPL_INST
232250

233251
// Decls.
234252
#define ABSTRACT_DECL(DECL)
235-
#define DECL(CLASS, BASE) virtual bool Traverse##CLASS##Decl(CLASS##Decl *D);
253+
#define DECL(CLASS, BASE) \
254+
virtual bool Traverse##CLASS##Decl(MaybeConst<CLASS##Decl> *D);
236255
#include "clang/AST/DeclNodes.inc"
237256

238257
#define DECL(CLASS, BASE) \
239-
bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D); \
240-
virtual bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
258+
bool WalkUpFrom##CLASS##Decl(MaybeConst<CLASS##Decl> *D); \
259+
virtual bool Visit##CLASS##Decl(MaybeConst<CLASS##Decl> *D) { return true; }
241260
#include "clang/AST/DeclNodes.inc"
242261

243262
// Stmts.
244263
#define ABSTRACT_STMT(STMT)
245-
#define STMT(CLASS, PARENT) virtual bool Traverse##CLASS(CLASS *S);
264+
#define STMT(CLASS, PARENT) virtual bool Traverse##CLASS(MaybeConst<CLASS> *S);
246265
#include "clang/AST/StmtNodes.inc"
247266

248267
#define STMT(CLASS, PARENT) \
249-
bool WalkUpFrom##CLASS(CLASS *S); \
250-
virtual bool Visit##CLASS(CLASS *S) { return true; }
268+
bool WalkUpFrom##CLASS(MaybeConst<CLASS> *S); \
269+
virtual bool Visit##CLASS(MaybeConst<CLASS> *S) { return true; }
251270
#include "clang/AST/StmtNodes.inc"
252271

253272
// Types.
254273
#define ABSTRACT_TYPE(CLASS, BASE)
255-
#define TYPE(CLASS, BASE) virtual bool Traverse##CLASS##Type(CLASS##Type *T);
274+
#define TYPE(CLASS, BASE) \
275+
virtual bool Traverse##CLASS##Type(MaybeConst<CLASS##Type> *T);
256276
#include "clang/AST/TypeNodes.inc"
257277

258278
#define TYPE(CLASS, BASE) \
259-
bool WalkUpFrom##CLASS##Type(CLASS##Type *T); \
260-
virtual bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
279+
bool WalkUpFrom##CLASS##Type(MaybeConst<CLASS##Type> *T); \
280+
virtual bool Visit##CLASS##Type(MaybeConst<CLASS##Type> *T) { return true; }
261281
#include "clang/AST/TypeNodes.inc"
262282

263283
// TypeLocs.
@@ -271,6 +291,14 @@ class DynamicRecursiveASTVisitor {
271291
virtual bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
272292
#include "clang/AST/TypeLocNodes.def"
273293
};
294+
295+
extern template class DynamicRecursiveASTVisitorBase<false>;
296+
extern template class DynamicRecursiveASTVisitorBase<true>;
297+
298+
using DynamicRecursiveASTVisitor =
299+
DynamicRecursiveASTVisitorBase</*Const=*/false>;
300+
using ConstDynamicRecursiveASTVisitor =
301+
DynamicRecursiveASTVisitorBase</*Const=*/true>;
274302
} // namespace clang
275303

276304
#endif // LLVM_CLANG_AST_DYNAMIC_RECURSIVE_AST_VISITOR_H

0 commit comments

Comments
 (0)