https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/132369
>From ab0cca1c85dcf08ba78e4f916325c86f5a425b8f Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Fri, 21 Mar 2025 18:18:39 +0800 Subject: [PATCH 1/2] [Clang][NFC] Use MarkUsedTemplateParameters It turns out that TemplateParamsReferencedInTemplateArgumentList() and MarkUsedTemplateParameters() have the similar goal, so let's drop the hand-written ASTVisitor. --- clang/include/clang/Sema/Sema.h | 4 ++ clang/lib/Sema/SemaTemplateDeduction.cpp | 8 +++ clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 50 ++----------------- 3 files changed, 17 insertions(+), 45 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index fc3936d649320..193601cd21abd 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -12590,6 +12590,10 @@ class Sema final : public SemaBase { void MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, bool OnlyDeduced, unsigned Depth, llvm::SmallBitVector &Used); + + void MarkUsedTemplateParameters(ArrayRef<TemplateArgument> TemplateArgs, + unsigned Depth, llvm::SmallBitVector &Used); + void MarkDeducedTemplateParameters(const FunctionTemplateDecl *FunctionTemplate, llvm::SmallBitVector &Deduced) { diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp index e6ec4a7178e81..2adf325d5d588 100644 --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -7167,6 +7167,14 @@ Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, Depth, Used); } +void Sema::MarkUsedTemplateParameters(ArrayRef<TemplateArgument> TemplateArgs, + unsigned Depth, + llvm::SmallBitVector &Used) { + for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) + ::MarkUsedTemplateParameters(Context, TemplateArgs[I], + /*OnlyDeduced=*/false, Depth, Used); +} + void Sema::MarkDeducedTemplateParameters( ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, llvm::SmallBitVector &Deduced) { diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp index ee89ee8594bc4..aebdce6d686a4 100644 --- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp +++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp @@ -21,7 +21,6 @@ #include "clang/AST/DeclFriend.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclarationName.h" -#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/OperationKinds.h" @@ -681,54 +680,15 @@ struct ConvertConstructorToDeductionGuideTransform { // Find all template parameters that appear in the given DeducedArgs. // Return the indices of the template parameters in the TemplateParams. SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList( - const TemplateParameterList *TemplateParamsList, + Sema &SemaRef, const TemplateParameterList *TemplateParamsList, ArrayRef<TemplateArgument> DeducedArgs) { - struct TemplateParamsReferencedFinder : DynamicRecursiveASTVisitor { - const TemplateParameterList *TemplateParamList; - llvm::BitVector ReferencedTemplateParams; - - TemplateParamsReferencedFinder( - const TemplateParameterList *TemplateParamList) - : TemplateParamList(TemplateParamList), - ReferencedTemplateParams(TemplateParamList->size()) {} - - bool VisitTemplateTypeParmType(TemplateTypeParmType *TTP) override { - // We use the index and depth to retrieve the corresponding template - // parameter from the parameter list, which is more robost. - Mark(TTP->getDepth(), TTP->getIndex()); - return true; - } - - bool VisitDeclRefExpr(DeclRefExpr *DRE) override { - MarkAppeared(DRE->getFoundDecl()); - return true; - } - bool TraverseTemplateName(TemplateName Template) override { - if (auto *TD = Template.getAsTemplateDecl()) - MarkAppeared(TD); - return DynamicRecursiveASTVisitor::TraverseTemplateName(Template); - } - - void MarkAppeared(NamedDecl *ND) { - if (llvm::isa<NonTypeTemplateParmDecl, TemplateTypeParmDecl, - TemplateTemplateParmDecl>(ND)) { - auto [Depth, Index] = getDepthAndIndex(ND); - Mark(Depth, Index); - } - } - void Mark(unsigned Depth, unsigned Index) { - if (Index < TemplateParamList->size() && - TemplateParamList->getParam(Index)->getTemplateDepth() == Depth) - ReferencedTemplateParams.set(Index); - } - }; - TemplateParamsReferencedFinder Finder(TemplateParamsList); - Finder.TraverseTemplateArguments(DeducedArgs); + llvm::SmallBitVector ReferencedTemplateParams(TemplateParamsList->size()); + SemaRef.MarkUsedTemplateParameters(DeducedArgs, TemplateParamsList->getDepth(), ReferencedTemplateParams); SmallVector<unsigned> Results; for (unsigned Index = 0; Index < TemplateParamsList->size(); ++Index) { - if (Finder.ReferencedTemplateParams[Index]) + if (ReferencedTemplateParams[Index]) Results.push_back(Index); } return Results; @@ -1084,7 +1044,7 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef, } auto DeducedAliasTemplateParams = TemplateParamsReferencedInTemplateArgumentList( - AliasTemplate->getTemplateParameters(), DeducedArgs); + SemaRef, AliasTemplate->getTemplateParameters(), DeducedArgs); // All template arguments null by default. SmallVector<TemplateArgument> TemplateArgsForBuildingFPrime( F->getTemplateParameters()->size()); >From 0a022eac938c79af9acfb5f459446b98bd3f7b93 Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Fri, 21 Mar 2025 18:25:51 +0800 Subject: [PATCH 2/2] clang-format --- clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp index aebdce6d686a4..91a528ec02007 100644 --- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp +++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp @@ -684,7 +684,8 @@ SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList( ArrayRef<TemplateArgument> DeducedArgs) { llvm::SmallBitVector ReferencedTemplateParams(TemplateParamsList->size()); - SemaRef.MarkUsedTemplateParameters(DeducedArgs, TemplateParamsList->getDepth(), ReferencedTemplateParams); + SemaRef.MarkUsedTemplateParameters( + DeducedArgs, TemplateParamsList->getDepth(), ReferencedTemplateParams); SmallVector<unsigned> Results; for (unsigned Index = 0; Index < TemplateParamsList->size(); ++Index) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits