[PATCH] D96324: [clangd] Rename references to function arguments within the same file

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 9 01:53:22 PST 2021


kbobyrev created this revision.
kbobyrev added a reviewer: hokein.
Herald added subscribers: usaxena95, arphaman.
kbobyrev requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

This is a fix of the problem (rather than a patch - D96247 <https://reviews.llvm.org/D96247>) discussed in
https://github.com/clangd/clangd/issues/685.

Fixes: https://github.com/clangd/clangd/issues/685


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96324

Files:
  clang-tools-extra/clangd/refactor/Rename.cpp
  clang-tools-extra/clangd/unittests/RenameTests.cpp


Index: clang-tools-extra/clangd/unittests/RenameTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/RenameTests.cpp
+++ clang-tools-extra/clangd/unittests/RenameTests.cpp
@@ -678,6 +678,34 @@
         }
       )cpp",
 
+      // Function argument.
+      R"cpp(
+        void foo(int [[Bar^]]);
+        void foo(int [[Bar^]]);
+
+        void foo(int [[Bar^]]) {
+        }
+      )cpp",
+
+      // Template function argument.
+      R"cpp(
+        template <typename T>
+        void foo(T [[Bar^]]);
+
+        template <typename T>
+        void foo(T [[Bar^]]) {}
+      )cpp",
+      R"cpp(
+        template <typename T>
+        void foo(T [[Bar^]]);
+
+        template <typename T>
+        void foo(T [[Bar^]]) {}
+
+        template <>
+        void foo(int [[Bar^]]) {}
+      )cpp",
+
       // Namespace alias.
       R"cpp(
         namespace a { namespace b { void foo(); } }
@@ -1089,6 +1117,15 @@
       )cpp",
        "conflict", !HeaderFile, nullptr, "Conflict"},
 
+      {R"cpp(
+        void func(int V^ar);
+
+        void func(int Var) {
+          bool Conflict;
+        }
+      )cpp",
+       "conflict", !HeaderFile, nullptr, "Conflict"},
+
       {R"cpp(
         void func(int V^ar, int Conflict) {
         }
Index: clang-tools-extra/clangd/refactor/Rename.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Rename.cpp
+++ clang-tools-extra/clangd/refactor/Rename.cpp
@@ -151,6 +151,20 @@
   if (const auto *VD = dyn_cast<VarDecl>(D)) {
     if (const VarDecl *OriginalVD = VD->getInstantiatedFromStaticDataMember())
       VD = OriginalVD;
+    // Arguments are canonicalized to their function's canonical function
+    // arguments.
+    if (const auto *Argument = dyn_cast<ParmVarDecl>(VD)) {
+      if (const auto *Function =
+              dyn_cast<FunctionDecl>(Argument->getDeclContext())) {
+        // FIXME(kirillbobyrev): This excludes constructors: their canonical
+        // decls are canonicalized RecordDecls.
+        if (const auto *Canonical =
+                dyn_cast<FunctionDecl>(canonicalRenameDecl(Function))) {
+          return Canonical->getParamDecl(Argument->getFunctionScopeIndex())
+              ->getCanonicalDecl();
+        }
+      }
+    }
     return VD->getCanonicalDecl();
   }
   return dyn_cast<NamedDecl>(D->getCanonicalDecl());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96324.322314.patch
Type: text/x-patch
Size: 2433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210209/91f89da7/attachment.bin>


More information about the cfe-commits mailing list