[PATCH] D89300: [clang-rename] Fix rename on variable templates.
This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG1e32df2f91f1: [clang-rename] Fix rename on variable templates. (authored by hokein). Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89300/new/ 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 +bool Foo = true; // CHECK: bool Bar = true; + +// explicit template specialization +template <> +bool Foo = false; // CHECK: bool Bar = false; + +// partial template specialization +template +bool Foo = false; // bool Bar = false; + +void k() { + // ref to the explicit template specialization + Foo; // CHECK: Bar; + // ref to the primary template. + Foo; // CHECK: Bar; +} + + +// 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.*' Index: clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp === --- clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp +++ clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp @@ -86,6 +86,16 @@ handleFunctionTemplateDecl(FTD); } else if (const auto *FD = dyn_cast(FoundDecl)) { handleFunctionTemplateDecl(FD); +} else if (const auto *VTD = dyn_cast(FoundDecl)) { + handleVarTemplateDecl(VTD); +} else if (const auto *VD = + dyn_cast(FoundDecl)) { + // FIXME: figure out why FoundDecl can be a VarTemplateSpecializationDecl. + handleVarTemplateDecl(VD->getSpecializedTemplate()); +} else if (const auto *VD = dyn_cast(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 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 & -VarTemplateDecl::getPartialSpecializations() { +VarTemplateDecl::getPartialSpecializations() const { LoadLazySpecializations(); return getCommonPtr()->PartialSpecializations; } @@ -1198,7 +1198,7 @@ } void VarTemplateDecl::getPartialSpecializations( -SmallVectorImpl &PS) { +SmallVectorImpl &PS) const { llvm::FoldingSetVector &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 & - 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 &PS); + SmallVectorImpl &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
[PATCH] D89300: [clang-rename] Fix rename on variable templates.
kbobyrev added inline comments. Comment at: clang/include/clang/AST/DeclTemplate.h:3098 llvm::FoldingSetVector & - getPartialSpecializations(); + getPartialSpecializations() const; hokein wrote: > kbobyrev wrote: > > I believe these are from https://reviews.llvm.org/D89221, so probably > > rebase on top of it? > I think you mean the change in D89220? they are different, this is for > `VarTemplateDecl`, while that one is for `ClassTemplateDecl`. Ah, you're right, sorry! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89300/new/ https://reviews.llvm.org/D89300 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D89300: [clang-rename] Fix rename on variable templates.
hokein added inline comments. Comment at: clang/include/clang/AST/DeclTemplate.h:3098 llvm::FoldingSetVector & - getPartialSpecializations(); + getPartialSpecializations() const; kbobyrev wrote: > I believe these are from https://reviews.llvm.org/D89221, so probably rebase > on top of it? I think you mean the change in D89220? they are different, this is for `VarTemplateDecl`, while that one is for `ClassTemplateDecl`. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89300/new/ https://reviews.llvm.org/D89300 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D89300: [clang-rename] Fix rename on variable templates.
kbobyrev accepted this revision. kbobyrev added a comment. This revision is now accepted and ready to land. Oh, that's neat, thanks! Comment at: clang/include/clang/AST/DeclTemplate.h:3098 llvm::FoldingSetVector & - getPartialSpecializations(); + getPartialSpecializations() const; I believe these are from https://reviews.llvm.org/D89221, so probably rebase on top of it? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89300/new/ https://reviews.llvm.org/D89300 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D89300: [clang-rename] Fix rename on variable templates.
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 +bool Foo = true; // CHECK: bool Bar = true; + +// explicit template specialization +template <> +bool Foo = false; // CHECK: bool Bar = false; + +// partial template specialization +template +bool Foo = false; // bool Bar = false; + +void k() { + // ref to the explicit template specialization + Foo; // CHECK: Bar; + // ref to the primary template. + Foo; // CHECK: Bar; +} + + +// 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.*' 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(FoundDecl)) { handleFunctionTemplateDecl(FD); +} else if (const auto *VTD = dyn_cast(FoundDecl)) { + handleVarTemplateDecl(VTD); +} else if (const auto *VD = + dyn_cast(FoundDecl)) { + // FIXME: figure out why FoundDecl can be a VarTemplateSpecializationDecl. + handleVarTemplateDecl(VD->getSpecializedTemplate()); +} else if (const auto *VD = dyn_cast(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 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 & -VarTemplateDecl::getPartialSpecializations() { +VarTemplateDecl::getPartialSpecializations() const { LoadLazySpecializations(); return getCommonPtr()->PartialSpecializations; } @@ -1198,7 +1198,7 @@ } void VarTemplateDecl::getPartialSpecializations( -SmallVectorImpl &PS) { +SmallVectorImpl &PS) const { llvm::FoldingSetVector &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 & - 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 &PS); + SmallVectorImpl &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