[PATCH] D74216: [clang-rename] Fix the missing template constructors.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 7 05:36:07 PST 2020


hokein created this revision.
hokein added a reviewer: kbobyrev.
Herald added subscribers: usaxena95, kadircet, arphaman, jkorous.
Herald added a project: clang.

When renaming a class with template constructors, we are missing the
occurrences of the template constructors, because getUSRsForDeclaration doesn't
give USRs of the templated constructors (they are not in the normal `ctors()`
method).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74216

Files:
  clang-tools-extra/clangd/unittests/RenameTests.cpp
  clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
  clang/test/clang-rename/TemplateCtor.cpp


Index: clang/test/clang-rename/TemplateCtor.cpp
===================================================================
--- /dev/null
+++ clang/test/clang-rename/TemplateCtor.cpp
@@ -0,0 +1,10 @@
+class Foo { // CHECK: class Bar {
+public:
+  template <typename T>
+  Foo(); // CHECK: Bar();
+
+  template <typename T>
+  Foo(Foo &); // CHECK: Bar(Bar &);
+};
+
+// RUN: clang-rename -offset=6 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
Index: clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
===================================================================
--- clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
+++ clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
@@ -135,6 +135,13 @@
 
     for (const auto *CtorDecl : RecordDecl->ctors())
       USRSet.insert(getUSRForDecl(CtorDecl));
+    // Add template constructor decls, they are not in ctors() unfortunately.
+    if (RecordDecl->hasUserDeclaredConstructor())
+      for (const auto *MD : RecordDecl->decls())
+        if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(MD))
+          if (const auto *Ctor =
+                  dyn_cast<CXXConstructorDecl>(FTD->getTemplatedDecl()))
+            USRSet.insert(getUSRForDecl(Ctor));
 
     USRSet.insert(getUSRForDecl(RecordDecl->getDestructor()));
     USRSet.insert(getUSRForDecl(RecordDecl));
Index: clang-tools-extra/clangd/unittests/RenameTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/RenameTests.cpp
+++ clang-tools-extra/clangd/unittests/RenameTests.cpp
@@ -137,6 +137,17 @@
         };
       )cpp",
 
+      // Rename template class constructor.
+       R"cpp(
+        class [[F^oo]] {
+          template<typename T>
+          [[Foo]]();
+
+          template<typename T>
+          [[Foo]](T t);
+        };
+      )cpp",
+
       // Class in template argument.
       R"cpp(
         class [[F^oo]] {};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74216.243147.patch
Type: text/x-patch
Size: 1932 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200207/2826e0cc/attachment.bin>


More information about the cfe-commits mailing list