[clang] 45a15dc - [clang-rename] Fix rename on function template specializations.

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


Author: Haojian Wu
Date: 2020-10-19T09:32:17+02:00
New Revision: 45a15dc682c06b95cd9182a889e972e93b58aa8e

URL: https://github.com/llvm/llvm-project/commit/45a15dc682c06b95cd9182a889e972e93b58aa8e
DIFF: https://github.com/llvm/llvm-project/commit/45a15dc682c06b95cd9182a889e972e93b58aa8e.diff

LOG: [clang-rename] Fix rename on function template specializations.

previously, we missed to rename occurrences to explicit function template
specilizations.

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

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

Modified: 
    clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp b/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
index 886c8ee551a0..e4056f701683 100644
--- a/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
+++ b/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
@@ -80,6 +80,12 @@ class AdditionalUSRFinder : public RecursiveASTVisitor<AdditionalUSRFinder> {
     } else if (const auto *TemplateDecl =
                    dyn_cast<ClassTemplateDecl>(FoundDecl)) {
       handleClassTemplateDecl(TemplateDecl);
+    } else if (const auto *FD = dyn_cast<FunctionDecl>(FoundDecl)) {
+      USRSet.insert(getUSRForDecl(FD));
+      if (const auto *FTD = FD->getPrimaryTemplate())
+        handleFunctionTemplateDecl(FTD);
+    } else if (const auto *FD = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
+      handleFunctionTemplateDecl(FD);
     } else {
       USRSet.insert(getUSRForDecl(FoundDecl));
     }
@@ -119,6 +125,13 @@ class AdditionalUSRFinder : public RecursiveASTVisitor<AdditionalUSRFinder> {
     addUSRsOfCtorDtors(TemplateDecl->getTemplatedDecl());
   }
 
+  void handleFunctionTemplateDecl(const FunctionTemplateDecl *FTD) {
+    USRSet.insert(getUSRForDecl(FTD));
+    USRSet.insert(getUSRForDecl(FTD->getTemplatedDecl()));
+    for (const auto *S : FTD->specializations())
+      USRSet.insert(getUSRForDecl(S));
+  }
+
   void addUSRsOfCtorDtors(const CXXRecordDecl *RD) {
     const auto* RecordDecl = RD->getDefinition();
 

diff  --git a/clang/test/clang-rename/FunctionTemplate.cpp b/clang/test/clang-rename/FunctionTemplate.cpp
new file mode 100644
index 000000000000..51b2515b8894
--- /dev/null
+++ b/clang/test/clang-rename/FunctionTemplate.cpp
@@ -0,0 +1,19 @@
+template <typename T>
+void Foo(T t); // CHECK: void Bar(T t);
+
+template <>
+void Foo(int a); // CHECK: void Bar(int a);
+
+void test() {
+  Foo<double>(1); // CHECK: Bar<double>(1);
+}
+
+// Test 1.
+// RUN: clang-rename -offset=28 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
+// Test 2.
+// RUN: clang-rename -offset=81 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
+// Test 3.
+// RUN: clang-rename -offset=137 -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