Skip to content

Commit 8a3cbb1

Browse files
committed
[clangd] Add basic keyword-name-validation in rename.
Differential Revision: https://reviews.llvm.org/D88875
1 parent 68e002e commit 8a3cbb1

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,9 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
412412
// - for cross-file rename, we deliberately pass a nullptr index to save
413413
// the cost, thus the result may be incomplete as it only contains
414414
// main-file occurrences;
415-
auto Results = clangd::rename({Pos, /*NewName*/ "", InpAST->AST, File,
416-
RenameOpts.AllowCrossFile ? nullptr : Index,
417-
RenameOpts});
415+
auto Results = clangd::rename(
416+
{Pos, /*NewName=*/"__clangd_rename_dummy", InpAST->AST, File,
417+
RenameOpts.AllowCrossFile ? nullptr : Index, RenameOpts});
418418
if (!Results) {
419419
// LSP says to return null on failure, but that will result in a generic
420420
// failure message. If we send an LSP error response, clients can surface

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,6 @@ class ClangdServer {
273273
StringRef TriggerText, Callback<std::vector<TextEdit>> CB);
274274

275275
/// Test the validity of a rename operation.
276-
///
277-
/// The returned result describes edits in the main-file only (all
278-
/// occurrences of the renamed symbol are simply deleted.
279276
void prepareRename(PathRef File, Position Pos,
280277
const RenameOptions &RenameOpts,
281278
Callback<RenameResult> CB);

clang-tools-extra/clangd/refactor/Rename.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ enum class ReasonToReject {
120120
UsedOutsideFile, // for within-file rename only.
121121
UnsupportedSymbol,
122122
AmbiguousSymbol,
123+
124+
// name validation.
125+
RenameToKeywords,
123126
};
124127

125128
llvm::Optional<ReasonToReject> renameable(const NamedDecl &RenameDecl,
@@ -208,6 +211,8 @@ llvm::Error makeError(ReasonToReject Reason) {
208211
return "symbol is not a supported kind (e.g. namespace, macro)";
209212
case ReasonToReject::AmbiguousSymbol:
210213
return "there are multiple symbols at the given location";
214+
case ReasonToReject::RenameToKeywords:
215+
return "the chosen name is a keyword";
211216
}
212217
llvm_unreachable("unhandled reason kind");
213218
};
@@ -471,6 +476,8 @@ llvm::Expected<RenameResult> rename(const RenameInputs &RInputs) {
471476
return makeError(ReasonToReject::NoSymbolFound);
472477
if (DeclsUnderCursor.size() > 1)
473478
return makeError(ReasonToReject::AmbiguousSymbol);
479+
if (isKeyword(RInputs.NewName, AST.getLangOpts()))
480+
return makeError(ReasonToReject::RenameToKeywords);
474481

475482
const auto &RenameDecl =
476483
llvm::cast<NamedDecl>(*(*DeclsUnderCursor.begin())->getCanonicalDecl());

clang-tools-extra/clangd/unittests/RenameTests.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ TEST(RenameTest, Renameable) {
516516
const char* ErrorMessage; // null if no error
517517
bool IsHeaderFile;
518518
const SymbolIndex *Index;
519+
llvm::StringRef NewName = "DummyName";
519520
};
520521
TestTU OtherFile = TestTU::withCode("Outside s; auto ss = &foo;");
521522
const char *CommonHeader = R"cpp(
@@ -542,6 +543,11 @@ TEST(RenameTest, Renameable) {
542543
)cpp",
543544
nullptr, HeaderFile, Index},
544545

546+
{R"cpp(
547+
void ^f();
548+
)cpp",
549+
"keyword", HeaderFile, Index, "return"},
550+
545551
{R"cpp(// disallow -- symbol is indexable and has other refs in index.
546552
void f() {
547553
Out^side s;
@@ -639,7 +645,7 @@ TEST(RenameTest, Renameable) {
639645
TU.ExtraArgs.push_back("-xobjective-c++-header");
640646
}
641647
auto AST = TU.build();
642-
llvm::StringRef NewName = "dummyNewName";
648+
llvm::StringRef NewName = Case.NewName;
643649
auto Results =
644650
rename({T.point(), NewName, AST, testPath(TU.Filename), Case.Index});
645651
bool WantRename = true;

0 commit comments

Comments
 (0)