[PATCH] D96009: [clangd] Improve name conflict detection

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 4 03:08:22 PST 2021


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

Follow-up on D95925 <https://reviews.llvm.org/D95925>: adds better detection for function arguments and also
checks for conflicts in muli-variable init statements in ForStmt.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96009

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
@@ -1067,6 +1067,14 @@
       )cpp",
        "conflict", !HeaderFile, nullptr, "Conflict"},
 
+      {R"cpp(
+        void func() {
+          for (int V^ar = 14, Conflict = 42;;) {
+          }
+        }
+      )cpp",
+       "conflict", !HeaderFile, nullptr, "Conflict"},
+
       {R"cpp(
         void func(int Conflict) {
           bool V^ar;
@@ -1074,6 +1082,19 @@
       )cpp",
        "conflict", !HeaderFile, nullptr, "Conflict"},
 
+      {R"cpp(
+        void func(int V^ar) {
+          bool Conflict;
+        }
+      )cpp",
+       "conflict", !HeaderFile, nullptr, "Conflict"},
+
+      {R"cpp(
+        void func(int V^ar, int Conflict) {
+        }
+      )cpp",
+       "conflict", !HeaderFile, nullptr, "Conflict"},
+
       {R"cpp(// Trying to rename into the same name, SameName == SameName.
         void func() {
           int S^ameName;
Index: clang-tools-extra/clangd/refactor/Rename.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Rename.cpp
+++ clang-tools-extra/clangd/refactor/Rename.cpp
@@ -337,9 +337,10 @@
   };
 
   // We need to get to the enclosing scope: NamedDecl's parent is typically
-  // DeclStmt, so enclosing scope would be the second order parent.
+  // DeclStmt (or TypeLoc in case of function arguments), so enclosing scope
+  // would be the second order parent.
   const auto *Parent = GetSingleParent(DynTypedNode::create(RenamedDecl));
-  if (!Parent || !Parent->get<DeclStmt>())
+  if (!Parent || !(Parent->get<DeclStmt>() || Parent->get<TypeLoc>()))
     return nullptr;
   Parent = GetSingleParent(*Parent);
 
@@ -407,8 +408,21 @@
   }
   if (const auto *EnclosingWhile = Parent->get<WhileStmt>())
     return CheckCompoundStmt(EnclosingWhile->getBody(), NewName);
-  if (const auto *EnclosingFor = Parent->get<ForStmt>())
+  if (const auto *EnclosingFor = Parent->get<ForStmt>()) {
+    // Check for conflicts with other declarations within initialization
+    // statement.
+    if (const auto *Result = CheckDeclStmt(
+            dyn_cast_or_null<DeclStmt>(EnclosingFor->getInit()), NewName))
+      return Result;
     return CheckCompoundStmt(EnclosingFor->getBody(), NewName);
+  }
+  if (const auto *EnclosingFunction = Parent->get<FunctionDecl>()) {
+    // Check for conflicts with other arguments.
+    for (const auto *Parameter : EnclosingFunction->parameters())
+      if (Parameter != &RenamedDecl && Parameter->getName() == NewName)
+        return Parameter;
+    return CheckCompoundStmt(EnclosingFunction->getBody(), NewName);
+  }
 
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96009.321363.patch
Type: text/x-patch
Size: 2829 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210204/03479089/attachment-0001.bin>


More information about the cfe-commits mailing list