[PATCH] D92220: [clangd] Add support for static fields in rename

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 27 03:28:23 PST 2020


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

Follow-up on D91952 <https://reviews.llvm.org/D91952> and D71880 <https://reviews.llvm.org/D71880>.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92220

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
@@ -627,6 +627,30 @@
           bool LocalBool = Foo<bool>::[[Var^iable]];
         }
       )cpp",
+      R"cpp(
+        template <typename T, typename U> struct Foo { static T Variable; };
+
+        template <typename T> struct Foo<T, bool> {
+          static T [[Var^iable]];
+        };
+
+        void test() {
+          Foo<int, bool>::[[Var^iable]] = 5;
+        }
+      )cpp",
+      R"cpp(
+        template <typename T, typename U> struct Foo {
+          static T [[Var^iable]];
+        };
+
+        template <typename T> struct Foo<T, bool> {
+          static T Variable;
+        };
+
+        void test() {
+          Foo<int, int>::[[Var^iable]] = 5;
+        }
+      )cpp",
 
       // Template parameters.
       R"cpp(
Index: clang-tools-extra/clangd/refactor/Rename.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Rename.cpp
+++ clang-tools-extra/clangd/refactor/Rename.cpp
@@ -144,7 +144,16 @@
   if (const auto *VD = dyn_cast<VarDecl>(D)) {
     if (const VarDecl *OriginalVD = VD->getInstantiatedFromStaticDataMember())
       VD = OriginalVD;
-    return VD->getCanonicalDecl();
+    VD = VD->getCanonicalDecl();
+    const auto *Context = VD->getDeclContext()->getParent();
+    if (!Context || Context->getDeclKind() != Decl::Kind::CXXRecord)
+      return VD;
+    const auto LookupResult = Context->lookup(D->getDeclName());
+    if (LookupResult.size() != 1)
+      return VD;
+    if (const auto *Result = dyn_cast<VarDecl>(LookupResult.front()))
+      return Result->getCanonicalDecl();
+    return VD;
   }
   return dyn_cast<NamedDecl>(D->getCanonicalDecl());
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92220.308007.patch
Type: text/x-patch
Size: 1903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201127/0279354b/attachment.bin>


More information about the cfe-commits mailing list