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

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 4 12:37:57 PST 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe8a2b7c91563: [clangd] Improve name conflict detection (authored by kbobyrev).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96009/new/

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 FunctionProtoTypeLoc 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.321546.patch
Type: text/x-patch
Size: 2842 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210204/e2668ae6/attachment.bin>


More information about the cfe-commits mailing list