[clang] 1e32df2 - [clang-rename] Fix rename on variable templates.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 19 00:45:17 PDT 2020


Author: Haojian Wu
Date: 2020-10-19T09:44:59+02:00
New Revision: 1e32df2f91f1aa1f8cd400ce50a621578fa0534e

URL: https://github.com/llvm/llvm-project/commit/1e32df2f91f1aa1f8cd400ce50a621578fa0534e
DIFF: https://github.com/llvm/llvm-project/commit/1e32df2f91f1aa1f8cd400ce50a621578fa0534e.diff

LOG: [clang-rename] Fix rename on variable templates.

This patch adds support for renaming variable templates.

Differential Revision: https://reviews.llvm.org/D89300

Added: 
    clang/test/clang-rename/VariableTemplate.cpp

Modified: 
    clang/include/clang/AST/DeclTemplate.h
    clang/lib/AST/DeclTemplate.cpp
    clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h
index d10103752d81..5f3525739091 100644
--- a/clang/include/clang/AST/DeclTemplate.h
+++ b/clang/include/clang/AST/DeclTemplate.h
@@ -3095,7 +3095,7 @@ class VarTemplateDecl : public RedeclarableTemplateDecl {
   /// 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 @@ class VarTemplateDecl : public RedeclarableTemplateDecl {
 
   /// 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

diff  --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index 5538651a453d..d99a9c19c506 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -1142,7 +1142,7 @@ VarTemplateDecl::getSpecializations() const {
 }
 
 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
-VarTemplateDecl::getPartialSpecializations() {
+VarTemplateDecl::getPartialSpecializations() const {
   LoadLazySpecializations();
   return getCommonPtr()->PartialSpecializations;
 }
@@ -1198,7 +1198,7 @@ void VarTemplateDecl::AddPartialSpecialization(
 }
 
 void VarTemplateDecl::getPartialSpecializations(
-    SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
+    SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const {
   llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
       getPartialSpecializations();
   PS.clear();

diff  --git a/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp b/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
index e4056f701683..a69b76a3c971 100644
--- a/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
+++ b/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
@@ -86,6 +86,16 @@ class AdditionalUSRFinder : public RecursiveASTVisitor<AdditionalUSRFinder> {
         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 @@ class AdditionalUSRFinder : public RecursiveASTVisitor<AdditionalUSRFinder> {
       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();
 

diff  --git a/clang/test/clang-rename/VariableTemplate.cpp b/clang/test/clang-rename/VariableTemplate.cpp
new file mode 100644
index 000000000000..a345ede5a7f6
--- /dev/null
+++ b/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>


        


More information about the cfe-commits mailing list