Skip to content

Commit 70a4ab9

Browse files
committed
[clangd][nfc] Demonstrate use of ConstRecursiveASTVisitor
On the example of DeducedTypeVisitor. This is just a demonstration of what I would like to do downstream (but also upstream for the other unnecessary uses of RecursiveASTVisitor).
1 parent a22916b commit 70a4ab9

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

clang-tools-extra/clangd/AST.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ namespace {
479479
/// not have the deduced type set. Instead, we have to go to the appropriate
480480
/// DeclaratorDecl/FunctionDecl and work our back to the AutoType that does have
481481
/// a deduced type set. The AST should be improved to simplify this scenario.
482-
class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
482+
class DeducedTypeVisitor : public ConstRecursiveASTVisitor<DeducedTypeVisitor> {
483483
SourceLocation SearchedLocation;
484484
const HeuristicResolver *Resolver;
485485

@@ -493,7 +493,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
493493
//- decltype(auto) i = 1;
494494
//- auto& i = 1;
495495
//- auto* i = &a;
496-
bool VisitDeclaratorDecl(DeclaratorDecl *D) {
496+
bool VisitDeclaratorDecl(const DeclaratorDecl *D) {
497497
if (!D->getTypeSourceInfo() ||
498498
!D->getTypeSourceInfo()->getTypeLoc().getContainedAutoTypeLoc() ||
499499
D->getTypeSourceInfo()
@@ -522,7 +522,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
522522
//- auto foo() -> int {}
523523
//- auto foo() -> decltype(1+1) {}
524524
//- operator auto() const { return 10; }
525-
bool VisitFunctionDecl(FunctionDecl *D) {
525+
bool VisitFunctionDecl(const FunctionDecl *D) {
526526
if (!D->getTypeSourceInfo())
527527
return true;
528528
// Loc of auto in return type (c++14).
@@ -553,7 +553,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
553553
// Handle non-auto decltype, e.g.:
554554
// - auto foo() -> decltype(expr) {}
555555
// - decltype(expr);
556-
bool VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
556+
bool VisitDecltypeTypeLoc(const DecltypeTypeLoc TL) {
557557
if (TL.getBeginLoc() != SearchedLocation)
558558
return true;
559559

@@ -571,7 +571,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
571571

572572
// Handle functions/lambdas with `auto` typed parameters.
573573
// We deduce the type if there's exactly one instantiation visible.
574-
bool VisitParmVarDecl(ParmVarDecl *PVD) {
574+
bool VisitParmVarDecl(const ParmVarDecl *PVD) {
575575
if (!PVD->getType()->isDependentType())
576576
return true;
577577
// 'auto' here does not name an AutoType, but an implicit template param.
@@ -606,7 +606,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
606606
return true;
607607
}
608608

609-
static int paramIndex(const TemplateDecl &TD, NamedDecl &Param) {
609+
static int paramIndex(const TemplateDecl &TD, const NamedDecl &Param) {
610610
unsigned I = 0;
611611
for (auto *ND : *TD.getTemplateParameters()) {
612612
if (&Param == ND)
@@ -620,7 +620,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
620620
};
621621
} // namespace
622622

623-
std::optional<QualType> getDeducedType(ASTContext &ASTCtx,
623+
std::optional<QualType> getDeducedType(const ASTContext &ASTCtx,
624624
const HeuristicResolver *Resolver,
625625
SourceLocation Loc) {
626626
if (!Loc.isValid())
@@ -659,7 +659,7 @@ static NamedDecl *getOnlyInstantiationImpl(TemplateDeclTy *TD) {
659659
return Only;
660660
}
661661

662-
NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl) {
662+
NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl) {
663663
if (TemplateDecl *TD = TemplatedDecl->getDescribedTemplate()) {
664664
if (auto *CTD = llvm::dyn_cast<ClassTemplateDecl>(TD))
665665
return getOnlyInstantiationImpl(CTD);

clang-tools-extra/clangd/AST.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ QualType declaredType(const TypeDecl *D);
168168
/// Retrieves the deduced type at a given location (auto, decltype).
169169
/// It will return the underlying type.
170170
/// If the type is an undeduced auto, returns the type itself.
171-
std::optional<QualType> getDeducedType(ASTContext &, const HeuristicResolver *,
171+
std::optional<QualType> getDeducedType(const ASTContext &, const HeuristicResolver *,
172172
SourceLocation Loc);
173173

174174
// Find the abbreviated-function-template `auto` within a type, or returns null.
@@ -180,7 +180,7 @@ TemplateTypeParmTypeLoc getContainedAutoParamType(TypeLoc TL);
180180

181181
// If TemplatedDecl is the generic body of a template, and the template has
182182
// exactly one visible instantiation, return the instantiated body.
183-
NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl);
183+
NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl);
184184

185185
/// Return attributes attached directly to a node.
186186
std::vector<const Attr *> getAttributes(const DynTypedNode &);

0 commit comments

Comments
 (0)