[PATCH] D101816: [clangd] don't rename if the triggering loc is not actually being renamed.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 11 04:49:55 PDT 2021
hokein updated this revision to Diff 351408.
hokein added a comment.
address comments
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D101816/new/
https://reviews.llvm.org/D101816
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
@@ -1071,6 +1071,13 @@
struct B : priv^ate A {};
)cpp",
"Cannot rename symbol: there is no symbol at the given location", false},
+ {R"cpp(// Ensure it doesn't associate base specifier with base name.
+ /*error-ok*/
+ struct A {
+ A() : inva^lid(0) {}
+ };
+ )cpp",
+ "no symbol", false},
};
for (const auto& Case : Cases) {
Index: clang-tools-extra/clangd/refactor/Rename.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Rename.cpp
+++ clang-tools-extra/clangd/refactor/Rename.cpp
@@ -751,6 +751,26 @@
auto MainFileRenameEdit = renameWithinFile(AST, RenameDecl, RInputs.NewName);
if (!MainFileRenameEdit)
return MainFileRenameEdit.takeError();
+
+ // Check the rename-triggering location is actually being renamed.
+ // This is a robustness check to avoid surprising rename results -- if the
+ // rename is not actually the name of the node we identified (e.g. for broken
+ // code), then rename is likely not what users expect, so we reject this kind
+ // of rename.
+ auto StartOffset = positionToOffset(MainFileCode, CurrentIdentifier.start);
+ auto EndOffset = positionToOffset(MainFileCode, CurrentIdentifier.end);
+ if (!StartOffset)
+ return StartOffset.takeError();
+ if (!EndOffset)
+ return EndOffset.takeError();
+ if (llvm::find_if(
+ *MainFileRenameEdit,
+ [&StartOffset, &EndOffset](const clang::tooling::Replacement &R) {
+ return R.getOffset() == *StartOffset &&
+ R.getLength() == *EndOffset - *StartOffset;
+ }) == MainFileRenameEdit->end()) {
+ return makeError(ReasonToReject::NoSymbolFound);
+ }
RenameResult Result;
Result.Target = CurrentIdentifier;
Edit MainFileEdits = Edit(MainFileCode, std::move(*MainFileRenameEdit));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101816.351408.patch
Type: text/x-patch
Size: 2118 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210611/fc7cbf24/attachment.bin>
More information about the cfe-commits
mailing list