Skip to content

Commit ab0cca1

Browse files
committed
[Clang][NFC] Use MarkUsedTemplateParameters
It turns out that TemplateParamsReferencedInTemplateArgumentList() and MarkUsedTemplateParameters() have the similar goal, so let's drop the hand-written ASTVisitor.
1 parent 0813c5c commit ab0cca1

File tree

3 files changed

+17
-45
lines changed

3 files changed

+17
-45
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12590,6 +12590,10 @@ class Sema final : public SemaBase {
1259012590
void MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1259112591
bool OnlyDeduced, unsigned Depth,
1259212592
llvm::SmallBitVector &Used);
12593+
12594+
void MarkUsedTemplateParameters(ArrayRef<TemplateArgument> TemplateArgs,
12595+
unsigned Depth, llvm::SmallBitVector &Used);
12596+
1259312597
void
1259412598
MarkDeducedTemplateParameters(const FunctionTemplateDecl *FunctionTemplate,
1259512599
llvm::SmallBitVector &Deduced) {

clang/lib/Sema/SemaTemplateDeduction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7167,6 +7167,14 @@ Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
71677167
Depth, Used);
71687168
}
71697169

7170+
void Sema::MarkUsedTemplateParameters(ArrayRef<TemplateArgument> TemplateArgs,
7171+
unsigned Depth,
7172+
llvm::SmallBitVector &Used) {
7173+
for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
7174+
::MarkUsedTemplateParameters(Context, TemplateArgs[I],
7175+
/*OnlyDeduced=*/false, Depth, Used);
7176+
}
7177+
71707178
void Sema::MarkDeducedTemplateParameters(
71717179
ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate,
71727180
llvm::SmallBitVector &Deduced) {

clang/lib/Sema/SemaTemplateDeductionGuide.cpp

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "clang/AST/DeclFriend.h"
2222
#include "clang/AST/DeclTemplate.h"
2323
#include "clang/AST/DeclarationName.h"
24-
#include "clang/AST/DynamicRecursiveASTVisitor.h"
2524
#include "clang/AST/Expr.h"
2625
#include "clang/AST/ExprCXX.h"
2726
#include "clang/AST/OperationKinds.h"
@@ -681,54 +680,15 @@ struct ConvertConstructorToDeductionGuideTransform {
681680
// Find all template parameters that appear in the given DeducedArgs.
682681
// Return the indices of the template parameters in the TemplateParams.
683682
SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
684-
const TemplateParameterList *TemplateParamsList,
683+
Sema &SemaRef, const TemplateParameterList *TemplateParamsList,
685684
ArrayRef<TemplateArgument> DeducedArgs) {
686-
struct TemplateParamsReferencedFinder : DynamicRecursiveASTVisitor {
687-
const TemplateParameterList *TemplateParamList;
688-
llvm::BitVector ReferencedTemplateParams;
689-
690-
TemplateParamsReferencedFinder(
691-
const TemplateParameterList *TemplateParamList)
692-
: TemplateParamList(TemplateParamList),
693-
ReferencedTemplateParams(TemplateParamList->size()) {}
694-
695-
bool VisitTemplateTypeParmType(TemplateTypeParmType *TTP) override {
696-
// We use the index and depth to retrieve the corresponding template
697-
// parameter from the parameter list, which is more robost.
698-
Mark(TTP->getDepth(), TTP->getIndex());
699-
return true;
700-
}
701-
702-
bool VisitDeclRefExpr(DeclRefExpr *DRE) override {
703-
MarkAppeared(DRE->getFoundDecl());
704-
return true;
705-
}
706685

707-
bool TraverseTemplateName(TemplateName Template) override {
708-
if (auto *TD = Template.getAsTemplateDecl())
709-
MarkAppeared(TD);
710-
return DynamicRecursiveASTVisitor::TraverseTemplateName(Template);
711-
}
712-
713-
void MarkAppeared(NamedDecl *ND) {
714-
if (llvm::isa<NonTypeTemplateParmDecl, TemplateTypeParmDecl,
715-
TemplateTemplateParmDecl>(ND)) {
716-
auto [Depth, Index] = getDepthAndIndex(ND);
717-
Mark(Depth, Index);
718-
}
719-
}
720-
void Mark(unsigned Depth, unsigned Index) {
721-
if (Index < TemplateParamList->size() &&
722-
TemplateParamList->getParam(Index)->getTemplateDepth() == Depth)
723-
ReferencedTemplateParams.set(Index);
724-
}
725-
};
726-
TemplateParamsReferencedFinder Finder(TemplateParamsList);
727-
Finder.TraverseTemplateArguments(DeducedArgs);
686+
llvm::SmallBitVector ReferencedTemplateParams(TemplateParamsList->size());
687+
SemaRef.MarkUsedTemplateParameters(DeducedArgs, TemplateParamsList->getDepth(), ReferencedTemplateParams);
728688

729689
SmallVector<unsigned> Results;
730690
for (unsigned Index = 0; Index < TemplateParamsList->size(); ++Index) {
731-
if (Finder.ReferencedTemplateParams[Index])
691+
if (ReferencedTemplateParams[Index])
732692
Results.push_back(Index);
733693
}
734694
return Results;
@@ -1084,7 +1044,7 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
10841044
}
10851045
auto DeducedAliasTemplateParams =
10861046
TemplateParamsReferencedInTemplateArgumentList(
1087-
AliasTemplate->getTemplateParameters(), DeducedArgs);
1047+
SemaRef, AliasTemplate->getTemplateParameters(), DeducedArgs);
10881048
// All template arguments null by default.
10891049
SmallVector<TemplateArgument> TemplateArgsForBuildingFPrime(
10901050
F->getTemplateParameters()->size());

0 commit comments

Comments
 (0)