[PATCH] D96009: [clangd] Improve name conflict detection
Kirill Bobyrev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 4 05:38:56 PST 2021
kbobyrev updated this revision to Diff 321398.
kbobyrev marked an inline comment as done.
kbobyrev added a comment.
Use FunctionProtoTypeLoc
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,11 @@
};
// 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<FunctionProtoTypeLoc>()))
return nullptr;
Parent = GetSingleParent(*Parent);
@@ -407,8 +409,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.321398.patch
Type: text/x-patch
Size: 2862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210204/912e22d7/attachment.bin>
More information about the cfe-commits
mailing list