[clang-tools-extra] Add support for renaming objc methods, even those with multiple selector pieces (PR #76466)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 27 11:37:39 PST 2023
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 8b485070844d03cda467e75aa8c924184ba671cf 76385b51db0e33a25df9646eb6d59f04a4f65af7 -- clang-tools-extra/clangd/ClangdLSPServer.cpp clang-tools-extra/clangd/ClangdLSPServer.h clang-tools-extra/clangd/Protocol.cpp clang-tools-extra/clangd/Protocol.h clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/clangd/SourceCode.h clang-tools-extra/clangd/index/SymbolCollector.cpp clang-tools-extra/clangd/refactor/Rename.cpp clang-tools-extra/clangd/refactor/Rename.h clang-tools-extra/clangd/unittests/RenameTests.cpp clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index 0e628cfc71..641266b76c 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -843,8 +843,9 @@ void ClangdLSPServer::onWorkspaceSymbol(
});
}
-void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
- Callback<std::optional<PrepareRenameResult>> Reply) {
+void ClangdLSPServer::onPrepareRename(
+ const TextDocumentPositionParams &Params,
+ Callback<std::optional<PrepareRenameResult>> Reply) {
Server->prepareRename(
Params.textDocument.uri.file(), Params.position, /*NewName*/ std::nullopt,
Opts.Rename,
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index 73e12fc7eb..ac20ed095b 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1243,7 +1243,8 @@ SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
return Loc;
}
-clangd::Range tokenRangeForLoc(SourceLocation TokLoc, const SourceManager &SM, const LangOptions &LangOpts) {
+clangd::Range tokenRangeForLoc(SourceLocation TokLoc, const SourceManager &SM,
+ const LangOptions &LangOpts) {
auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
clangd::Range Result;
Result.start = sourceLocToPosition(SM, TokLoc);
diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h
index d671e05aed..efe6d66c16 100644
--- a/clang-tools-extra/clangd/SourceCode.h
+++ b/clang-tools-extra/clangd/SourceCode.h
@@ -338,8 +338,7 @@ inline bool isReservedName(llvm::StringRef Name) {
SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
const SourceManager &SM);
-clangd::Range tokenRangeForLoc(SourceLocation TokLoc,
- const SourceManager &SM,
+clangd::Range tokenRangeForLoc(SourceLocation TokLoc, const SourceManager &SM,
const LangOptions &LangOpts);
/// Returns the range starting at offset and spanning the whole line. Escaped
diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp
index 4f945f74e8..e5686c963a 100644
--- a/clang-tools-extra/clangd/refactor/Rename.cpp
+++ b/clang-tools-extra/clangd/refactor/Rename.cpp
@@ -585,11 +585,9 @@ renameWithinFile(ParsedAST &AST, const NamedDecl &RenameDecl,
for (const auto &Loc : Locs)
Ranges.push_back(tokenRangeForLoc(Loc, SM, LangOpts));
auto FilePath = AST.tuPath();
- auto RenameRanges =
- adjustRenameRanges(Code, RenameIdentifier,
- std::move(Ranges),
- RenameDecl.getASTContext().getLangOpts(),
- MD->getSelector());
+ auto RenameRanges = adjustRenameRanges(
+ Code, RenameIdentifier, std::move(Ranges),
+ RenameDecl.getASTContext().getLangOpts(), MD->getSelector());
if (!RenameRanges) {
// Our heuristics fails to adjust rename ranges to the current state of
// the file, it is most likely the index is stale, so we give up the
@@ -598,8 +596,7 @@ renameWithinFile(ParsedAST &AST, const NamedDecl &RenameDecl,
"(selector handling may be incorrect)",
FilePath);
}
- auto RenameEdit =
- buildRenameEdit(FilePath, Code, *RenameRanges, NewNames);
+ auto RenameEdit = buildRenameEdit(FilePath, Code, *RenameRanges, NewNames);
if (!RenameEdit)
return error("failed to rename in file {0}: {1}", FilePath,
RenameEdit.takeError());
@@ -736,8 +733,7 @@ renameOutsideFile(const NamedDecl &RenameDecl, llvm::StringRef MainFilePath,
auto RenameRanges =
adjustRenameRanges(AffectedFileCode, RenameIdentifier,
std::move(FileAndOccurrences.second),
- RenameDecl.getASTContext().getLangOpts(),
- Selector);
+ RenameDecl.getASTContext().getLangOpts(), Selector);
if (!RenameRanges) {
// Our heuristics fails to adjust rename ranges to the current state of
// the file, it is most likely the index is stale, so we give up the
@@ -785,7 +781,8 @@ void findNearMiss(
MatchedCB(PartialMatch);
return;
}
- if (impliesSimpleEdit(IndexedRest.front().start, LexedRest.front().range().start)) {
+ if (impliesSimpleEdit(IndexedRest.front().start,
+ LexedRest.front().range().start)) {
PartialMatch.push_back(LexedIndex);
findNearMiss(PartialMatch, IndexedRest.drop_front(), LexedRest.drop_front(),
LexedIndex + 1, Fuel, MatchedCB);
@@ -804,11 +801,9 @@ std::vector<SymbolRange> symbolRanges(const std::vector<Range> Ranges) {
return Result;
}
-std::vector<SymbolRange>
-collectRenameIdentifierRanges(
- llvm::StringRef Identifier, llvm::StringRef Content,
- const LangOptions &LangOpts,
- std::optional<Selector> Selector);
+std::vector<SymbolRange> collectRenameIdentifierRanges(
+ llvm::StringRef Identifier, llvm::StringRef Content,
+ const LangOptions &LangOpts, std::optional<Selector> Selector);
llvm::Expected<RenameResult> rename(const RenameInputs &RInputs) {
assert(!RInputs.Index == !RInputs.FS &&
@@ -938,10 +933,10 @@ llvm::Expected<RenameResult> rename(const RenameInputs &RInputs) {
return Result;
}
-llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
- llvm::StringRef InitialCode,
- std::vector<SymbolRange> Occurrences,
- llvm::SmallVectorImpl<llvm::StringRef> &NewNames) {
+llvm::Expected<Edit>
+buildRenameEdit(llvm::StringRef AbsFilePath, llvm::StringRef InitialCode,
+ std::vector<SymbolRange> Occurrences,
+ llvm::SmallVectorImpl<llvm::StringRef> &NewNames) {
trace::Span Tracer("BuildRenameEdit");
SPAN_ATTACH(Tracer, "file_path", AbsFilePath);
SPAN_ATTACH(Tracer, "rename_occurrences",
@@ -976,8 +971,8 @@ llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
size_t End;
llvm::StringRef NewName;
- OccurrenceOffset(size_t Start, size_t End, llvm::StringRef NewName) :
- Start(Start), End(End), NewName(NewName) {}
+ OccurrenceOffset(size_t Start, size_t End, llvm::StringRef NewName)
+ : Start(Start), End(End), NewName(NewName) {}
};
std::vector<OccurrenceOffset> OccurrencesOffsets;
@@ -992,7 +987,8 @@ llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
return EndOffset.takeError();
auto Index = It - SR.Ranges.begin();
// Nothing to do if the token/name hasn't changed.
- auto CurName = InitialCode.substr(*StartOffset, *EndOffset - *StartOffset);
+ auto CurName =
+ InitialCode.substr(*StartOffset, *EndOffset - *StartOffset);
if (CurName == NewNames[Index])
continue;
OccurrencesOffsets.emplace_back(*StartOffset, *EndOffset,
@@ -1036,8 +1032,8 @@ adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier,
return getMappedRanges(Indexed, Lexed);
}
-std::optional<std::vector<SymbolRange>> getMappedRanges(ArrayRef<Range> Indexed,
- ArrayRef<SymbolRange> Lexed) {
+std::optional<std::vector<SymbolRange>>
+getMappedRanges(ArrayRef<Range> Indexed, ArrayRef<SymbolRange> Lexed) {
trace::Span Tracer("GetMappedRanges");
assert(!Indexed.empty());
assert(llvm::is_sorted(Indexed));
@@ -1115,9 +1111,10 @@ size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed,
int LastDLine = 0, LastDColumn = 0;
int Cost = 0;
for (size_t I = 0; I < Indexed.size(); ++I) {
- int DLine = Indexed[I].start.line - Lexed[MappedIndex[I]].range().start.line;
- int DColumn =
- Indexed[I].start.character - Lexed[MappedIndex[I]].range().start.character;
+ int DLine =
+ Indexed[I].start.line - Lexed[MappedIndex[I]].range().start.line;
+ int DColumn = Indexed[I].start.character -
+ Lexed[MappedIndex[I]].range().start.character;
int Line = Indexed[I].start.line;
if (Line != LastLine)
LastDColumn = 0; // column offsets don't carry cross lines.
@@ -1160,10 +1157,9 @@ lex(llvm::StringRef Code, const LangOptions &LangOpts,
Action(Tok, SM);
}
-std::vector<SymbolRange>
-collectRenameIdentifierRanges(llvm::StringRef Identifier, llvm::StringRef Content,
- const LangOptions &LangOpts,
- std::optional<Selector> Selector) {
+std::vector<SymbolRange> collectRenameIdentifierRanges(
+ llvm::StringRef Identifier, llvm::StringRef Content,
+ const LangOptions &LangOpts, std::optional<Selector> Selector) {
std::vector<SymbolRange> Ranges;
if (!Selector) {
lex(Content, LangOpts,
@@ -1255,7 +1251,7 @@ collectRenameIdentifierRanges(llvm::StringRef Identifier, llvm::StringRef Conten
break;
case tok::l_brace:
++State.BraceCount;
- LLVM_FALLTHROUGH;
+ LLVM_FALLTHROUGH;
case tok::semi:
// At the top level scope we should only have method decls/defs.
if (States.size() == 1) {
diff --git a/clang-tools-extra/clangd/refactor/Rename.h b/clang-tools-extra/clangd/refactor/Rename.h
index c96363324e..3951d7d559 100644
--- a/clang-tools-extra/clangd/refactor/Rename.h
+++ b/clang-tools-extra/clangd/refactor/Rename.h
@@ -98,10 +98,10 @@ llvm::Expected<RenameResult> rename(const RenameInputs &RInputs);
/// NewName.
/// Exposed for testing only.
/// REQUIRED: Occurrences is sorted and doesn't have duplicated ranges.
-llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
- llvm::StringRef InitialCode,
- std::vector<SymbolRange> Occurrences,
- llvm::SmallVectorImpl<llvm::StringRef> &NewNames);
+llvm::Expected<Edit>
+buildRenameEdit(llvm::StringRef AbsFilePath, llvm::StringRef InitialCode,
+ std::vector<SymbolRange> Occurrences,
+ llvm::SmallVectorImpl<llvm::StringRef> &NewNames);
/// Adjusts indexed occurrences to match the current state of the file.
///
@@ -124,15 +124,16 @@ adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier,
/// Exposed for testing only.
///
/// REQUIRED: Indexed and Lexed are sorted.
-std::optional<std::vector<SymbolRange>> getMappedRanges(ArrayRef<Range> Indexed,
- ArrayRef<SymbolRange> Lexed);
+std::optional<std::vector<SymbolRange>>
+getMappedRanges(ArrayRef<Range> Indexed, ArrayRef<SymbolRange> Lexed);
/// Evaluates how good the mapped result is. 0 indicates a perfect match.
///
/// Exposed for testing only.
///
/// REQUIRED: Indexed and Lexed are sorted, Indexed and MappedIndex have the
/// same size.
-size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed, ArrayRef<SymbolRange> Lexed,
+size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed,
+ ArrayRef<SymbolRange> Lexed,
ArrayRef<size_t> MappedIndex);
} // namespace clangd
diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp
index a459314256..54118adaa4 100644
--- a/clang-tools-extra/clangd/unittests/RenameTests.cpp
+++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp
@@ -937,7 +937,8 @@ TEST(RenameTest, Renameable) {
- (int)fo^o:(int)x;
@end
)cpp",
- "invalid name: the chosen name \"MockName\" is not a valid identifier", HeaderFile},
+ "invalid name: the chosen name \"MockName\" is not a valid identifier",
+ HeaderFile},
{R"cpp(
@interface Foo {}
- (int)[[o^ne]]:(int)one two:(int)two;
@@ -1156,7 +1157,7 @@ TEST(RenameTest, Renameable) {
int [[V^ar]];
}
)cpp",
- nullptr, !HeaderFile},
+ nullptr, !HeaderFile},
};
for (const auto& Case : Cases) {
@@ -1817,7 +1818,8 @@ TEST(CrossFileRenameTests, BuildRenameEdits) {
[[range]]
[[range]]
)cpp");
- Edit = buildRenameEdit(FilePath, T.code(), symbolRanges(T.ranges()), NewNames);
+ Edit =
+ buildRenameEdit(FilePath, T.code(), symbolRanges(T.ranges()), NewNames);
ASSERT_TRUE(bool(Edit)) << Edit.takeError();
EXPECT_EQ(applyEdits(FileEdits{{T.code(), std::move(*Edit)}}).front().second,
expectedResult(T, NewNames[0]));
@@ -1874,8 +1876,9 @@ TEST(CrossFileRenameTests, adjustRenameRanges) {
for (const auto &T : Tests) {
SCOPED_TRACE(T.DraftCode);
Annotations Draft(T.DraftCode);
- auto ActualRanges = adjustRenameRanges(
- Draft.code(), "x", Annotations(T.IndexedCode).ranges(), LangOpts, std::nullopt);
+ auto ActualRanges = adjustRenameRanges(Draft.code(), "x",
+ Annotations(T.IndexedCode).ranges(),
+ LangOpts, std::nullopt);
if (!ActualRanges)
EXPECT_THAT(Draft.ranges(), testing::IsEmpty());
else
@@ -1992,7 +1995,7 @@ TEST(RangePatchingHeuristic, GetMappedRanges) {
auto LexedRanges = symbolRanges(Lexed.ranges());
std::vector<SymbolRange> ExpectedMatches;
for (auto P : Lexed.points()) {
- auto Match = llvm::find_if(LexedRanges, [&P](const SymbolRange& R) {
+ auto Match = llvm::find_if(LexedRanges, [&P](const SymbolRange &R) {
return R.range().start == P;
});
ASSERT_NE(Match, LexedRanges.end());
@@ -2112,9 +2115,8 @@ TEST(CrossFileRenameTests, adjustmentCost) {
std::vector<size_t> MappedIndex;
for (size_t I = 0; I < C.ranges("lex").size(); ++I)
MappedIndex.push_back(I);
- EXPECT_EQ(renameRangeAdjustmentCost(C.ranges("idx"),
- symbolRanges(C.ranges("lex")),
- MappedIndex),
+ EXPECT_EQ(renameRangeAdjustmentCost(
+ C.ranges("idx"), symbolRanges(C.ranges("lex")), MappedIndex),
T.ExpectedCost);
}
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/76466
More information about the cfe-commits
mailing list