Skip to content

Commit 192f8d9

Browse files
committed
[clangd] Don't rename on symbols from system headers.
Fixes clangd/clangd#963. Differential Revision: https://reviews.llvm.org/D116643
1 parent aa7f0e6 commit 192f8d9

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,17 @@ llvm::DenseSet<const NamedDecl *> locateDeclAt(ParsedAST &AST,
159159
return Result;
160160
}
161161

162-
// By default, we exclude C++ standard symbols and protobuf symbols as rename
163-
// these symbols would change system/generated files which are unlikely to be
164-
// modified.
162+
// By default, we exclude symbols from system headers and protobuf symbols as
163+
// renaming these symbols would change system/generated files which are unlikely
164+
// to be good candidates for modification.
165165
bool isExcluded(const NamedDecl &RenameDecl) {
166-
if (isProtoFile(RenameDecl.getLocation(),
167-
RenameDecl.getASTContext().getSourceManager()))
166+
const auto &SM = RenameDecl.getASTContext().getSourceManager();
167+
if (SM.isInSystemHeader(RenameDecl.getLocation()))
168168
return true;
169+
if (isProtoFile(RenameDecl.getLocation(), SM))
170+
return true;
171+
// FIXME: Remove this std symbol list, as they should be covered by the
172+
// above isInSystemHeader check.
169173
static const auto *StdSymbols = new llvm::DenseSet<llvm::StringRef>({
170174
#define SYMBOL(Name, NameSpace, Header) {#NameSpace #Name},
171175
#include "StdSymbolMap.inc"

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,29 @@ TEST(RenameTest, MainFileReferencesOnly) {
11981198
expectedResult(Code, NewName));
11991199
}
12001200

1201+
TEST(RenameTest, NoRenameOnSymbolsFromSystemHeaders) {
1202+
// filter out references not from main file.
1203+
llvm::StringRef Test =
1204+
R"cpp(
1205+
#include <system>
1206+
SystemSym^bol abc;
1207+
)cpp";
1208+
1209+
Annotations Code(Test);
1210+
auto TU = TestTU::withCode(Code.code());
1211+
TU.AdditionalFiles["system"] = R"cpp(
1212+
class SystemSymbol {};
1213+
)cpp";
1214+
TU.ExtraArgs = {"-isystem", testRoot()};
1215+
auto AST = TU.build();
1216+
llvm::StringRef NewName = "abcde";
1217+
1218+
auto Results = rename({Code.point(), NewName, AST, testPath(TU.Filename)});
1219+
EXPECT_FALSE(Results) << "expected rename returned an error: " << Code.code();
1220+
auto ActualMessage = llvm::toString(Results.takeError());
1221+
EXPECT_THAT(ActualMessage, testing::HasSubstr("not a supported kind"));
1222+
}
1223+
12011224
TEST(RenameTest, ProtobufSymbolIsExcluded) {
12021225
Annotations Code("Prot^obuf buf;");
12031226
auto TU = TestTU::withCode(Code.code());

0 commit comments

Comments
 (0)