[clangd] Add basic keyword-name-validation in rename.

Differential Revision: https://reviews.llvm.org/D88875
This commit is contained in:
Haojian Wu 2020-10-06 15:46:40 +02:00
parent 68e002e181
commit 8a3cbb1535
4 changed files with 17 additions and 7 deletions

View File

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

View File

@ -273,9 +273,6 @@ public:
StringRef TriggerText, Callback<std::vector<TextEdit>> CB);
/// Test the validity of a rename operation.
///
/// The returned result describes edits in the main-file only (all
/// occurrences of the renamed symbol are simply deleted.
void prepareRename(PathRef File, Position Pos,
const RenameOptions &RenameOpts,
Callback<RenameResult> CB);

View File

@ -120,6 +120,9 @@ enum class ReasonToReject {
UsedOutsideFile, // for within-file rename only.
UnsupportedSymbol,
AmbiguousSymbol,
// name validation.
RenameToKeywords,
};
llvm::Optional<ReasonToReject> renameable(const NamedDecl &RenameDecl,
@ -208,6 +211,8 @@ llvm::Error makeError(ReasonToReject Reason) {
return "symbol is not a supported kind (e.g. namespace, macro)";
case ReasonToReject::AmbiguousSymbol:
return "there are multiple symbols at the given location";
case ReasonToReject::RenameToKeywords:
return "the chosen name is a keyword";
}
llvm_unreachable("unhandled reason kind");
};
@ -471,6 +476,8 @@ llvm::Expected<RenameResult> rename(const RenameInputs &RInputs) {
return makeError(ReasonToReject::NoSymbolFound);
if (DeclsUnderCursor.size() > 1)
return makeError(ReasonToReject::AmbiguousSymbol);
if (isKeyword(RInputs.NewName, AST.getLangOpts()))
return makeError(ReasonToReject::RenameToKeywords);
const auto &RenameDecl =
llvm::cast<NamedDecl>(*(*DeclsUnderCursor.begin())->getCanonicalDecl());

View File

@ -516,6 +516,7 @@ TEST(RenameTest, Renameable) {
const char* ErrorMessage; // null if no error
bool IsHeaderFile;
const SymbolIndex *Index;
llvm::StringRef NewName = "DummyName";
};
TestTU OtherFile = TestTU::withCode("Outside s; auto ss = &foo;");
const char *CommonHeader = R"cpp(
@ -542,6 +543,11 @@ TEST(RenameTest, Renameable) {
)cpp",
nullptr, HeaderFile, Index},
{R"cpp(
void ^f();
)cpp",
"keyword", HeaderFile, Index, "return"},
{R"cpp(// disallow -- symbol is indexable and has other refs in index.
void f() {
Out^side s;
@ -639,7 +645,7 @@ TEST(RenameTest, Renameable) {
TU.ExtraArgs.push_back("-xobjective-c++-header");
}
auto AST = TU.build();
llvm::StringRef NewName = "dummyNewName";
llvm::StringRef NewName = Case.NewName;
auto Results =
rename({T.point(), NewName, AST, testPath(TU.Filename), Case.Index});
bool WantRename = true;