[clang-rename] Fix rename on variable templates.

This patch adds support for renaming variable templates.

Differential Revision: https://reviews.llvm.org/D89300
This commit is contained in:
Haojian Wu 2020-10-19 09:44:29 +02:00
parent 3945b69e81
commit 1e32df2f91
4 changed files with 59 additions and 4 deletions

View File

@ -3095,7 +3095,7 @@ protected:
/// Retrieve the set of partial specializations of this class /// Retrieve the set of partial specializations of this class
/// template. /// template.
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
getPartialSpecializations(); getPartialSpecializations() const;
VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
DeclarationName Name, TemplateParameterList *Params, DeclarationName Name, TemplateParameterList *Params,
@ -3191,7 +3191,7 @@ public:
/// Retrieve the partial specializations as an ordered list. /// Retrieve the partial specializations as an ordered list.
void getPartialSpecializations( void getPartialSpecializations(
SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS); SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const;
/// Find a variable template partial specialization which was /// Find a variable template partial specialization which was
/// instantiated /// instantiated

View File

@ -1142,7 +1142,7 @@ VarTemplateDecl::getSpecializations() const {
} }
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
VarTemplateDecl::getPartialSpecializations() { VarTemplateDecl::getPartialSpecializations() const {
LoadLazySpecializations(); LoadLazySpecializations();
return getCommonPtr()->PartialSpecializations; return getCommonPtr()->PartialSpecializations;
} }
@ -1198,7 +1198,7 @@ void VarTemplateDecl::AddPartialSpecialization(
} }
void VarTemplateDecl::getPartialSpecializations( void VarTemplateDecl::getPartialSpecializations(
SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) { SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const {
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs = llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
getPartialSpecializations(); getPartialSpecializations();
PS.clear(); PS.clear();

View File

@ -86,6 +86,16 @@ public:
handleFunctionTemplateDecl(FTD); handleFunctionTemplateDecl(FTD);
} else if (const auto *FD = dyn_cast<FunctionTemplateDecl>(FoundDecl)) { } else if (const auto *FD = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
handleFunctionTemplateDecl(FD); 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 { } else {
USRSet.insert(getUSRForDecl(FoundDecl)); USRSet.insert(getUSRForDecl(FoundDecl));
} }
@ -132,6 +142,19 @@ private:
USRSet.insert(getUSRForDecl(S)); 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) { void addUSRsOfCtorDtors(const CXXRecordDecl *RD) {
const auto* RecordDecl = RD->getDefinition(); const auto* RecordDecl = RD->getDefinition();

View File

@ -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>