hokein created this revision. hokein added a reviewer: kbobyrev. Herald added a project: clang. hokein requested review of this revision.
This patch adds support for renaming variable templates. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D89300 Files: clang/include/clang/AST/DeclTemplate.h clang/lib/AST/DeclTemplate.cpp clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp clang/test/clang-rename/VariableTemplate.cpp
Index: clang/test/clang-rename/VariableTemplate.cpp =================================================================== --- /dev/null +++ clang/test/clang-rename/VariableTemplate.cpp @@ -0,0 +1,32 @@ +template <typename T, int U> +bool Foo = true; // CHECK: bool Bar = true; + +// explicit template specialization +template <> +bool Foo<int, 0> = false; // CHECK: bool Bar<int, 0> = false; + +// partial template specialization +template <typename T> +bool Foo<T, 1> = false; // bool Bar<x, 1> = false; + +void k() { + // ref to the explicit template specialization + Foo<int, 0>; // CHECK: Bar<int, 0>; + // ref to the primary template. + Foo<double, 2>; // CHECK: Bar<double, 2>; +} + + +// Test 1. +// RUN: clang-rename -offset=34 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s +// Test 2. +// RUN: clang-rename -offset=128 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s +// Test 3. +// RUN: clang-rename -offset=248 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s +// Test 4. +// RUN: clang-rename -offset=357 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s +// Test 5. +// RUN: clang-rename -offset=431 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s + +// To find offsets after modifying the file, use: +// grep -Ubo 'Foo.*' <file> Index: clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp =================================================================== --- clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp +++ clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp @@ -87,6 +87,16 @@ handleFunctionTemplateDecl(FTD); } else if (const auto *FD = dyn_cast<FunctionTemplateDecl>(FoundDecl)) { handleFunctionTemplateDecl(FD); + } else if (const auto *VTD = dyn_cast<VarTemplateDecl>(FoundDecl)) { + handleVarTemplateDecl(VTD); + } else if (const auto *VD = + dyn_cast<VarTemplateSpecializationDecl>(FoundDecl)) { + // FIXME: figure out why FoundDecl can be a VarTemplateSpecializationDecl. + handleVarTemplateDecl(VD->getSpecializedTemplate()); + } else if (const auto *VD = dyn_cast<VarDecl>(FoundDecl)) { + USRSet.insert(getUSRForDecl(VD)); + if (const auto *VTD = VD->getDescribedVarTemplate()) + handleVarTemplateDecl(VTD); } else { USRSet.insert(getUSRForDecl(FoundDecl)); } @@ -132,6 +142,19 @@ USRSet.insert(getUSRForDecl(S)); } + void handleVarTemplateDecl(const VarTemplateDecl *VTD) { + USRSet.insert(getUSRForDecl(VTD)); + USRSet.insert(getUSRForDecl(VTD->getTemplatedDecl())); + llvm::for_each(VTD->specializations(), [&](const auto *Spec) { + USRSet.insert(getUSRForDecl(Spec)); + }); + SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; + VTD->getPartialSpecializations(PartialSpecs); + llvm::for_each(PartialSpecs, [&](const auto *Spec) { + USRSet.insert(getUSRForDecl(Spec)); + }); + } + void addUSRsOfCtorDtors(const CXXRecordDecl *RD) { const auto* RecordDecl = RD->getDefinition(); Index: clang/lib/AST/DeclTemplate.cpp =================================================================== --- clang/lib/AST/DeclTemplate.cpp +++ clang/lib/AST/DeclTemplate.cpp @@ -1142,7 +1142,7 @@ } llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & -VarTemplateDecl::getPartialSpecializations() { +VarTemplateDecl::getPartialSpecializations() const { LoadLazySpecializations(); return getCommonPtr()->PartialSpecializations; } @@ -1198,7 +1198,7 @@ } void VarTemplateDecl::getPartialSpecializations( - SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) { + SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const { llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs = getPartialSpecializations(); PS.clear(); Index: clang/include/clang/AST/DeclTemplate.h =================================================================== --- clang/include/clang/AST/DeclTemplate.h +++ clang/include/clang/AST/DeclTemplate.h @@ -3095,7 +3095,7 @@ /// Retrieve the set of partial specializations of this class /// template. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & - getPartialSpecializations(); + getPartialSpecializations() const; VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, @@ -3191,7 +3191,7 @@ /// Retrieve the partial specializations as an ordered list. void getPartialSpecializations( - SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS); + SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const; /// Find a variable template partial specialization which was /// instantiated
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits