From 192f8d97002f13ab5a74ee11c4d5559b5ca693a8 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 5 Jan 2022 10:42:45 +0100 Subject: [PATCH] [clangd] Don't rename on symbols from system headers. Fixes https://github.com/clangd/clangd/issues/963. Differential Revision: https://reviews.llvm.org/D116643 --- clang-tools-extra/clangd/refactor/Rename.cpp | 14 +++++++---- .../clangd/unittests/RenameTests.cpp | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp index e092c677c57c..a00d2f51b56c 100644 --- a/clang-tools-extra/clangd/refactor/Rename.cpp +++ b/clang-tools-extra/clangd/refactor/Rename.cpp @@ -159,13 +159,17 @@ llvm::DenseSet locateDeclAt(ParsedAST &AST, return Result; } -// By default, we exclude C++ standard symbols and protobuf symbols as rename -// these symbols would change system/generated files which are unlikely to be -// modified. +// By default, we exclude symbols from system headers and protobuf symbols as +// renaming these symbols would change system/generated files which are unlikely +// to be good candidates for modification. bool isExcluded(const NamedDecl &RenameDecl) { - if (isProtoFile(RenameDecl.getLocation(), - RenameDecl.getASTContext().getSourceManager())) + const auto &SM = RenameDecl.getASTContext().getSourceManager(); + if (SM.isInSystemHeader(RenameDecl.getLocation())) return true; + if (isProtoFile(RenameDecl.getLocation(), SM)) + return true; + // FIXME: Remove this std symbol list, as they should be covered by the + // above isInSystemHeader check. static const auto *StdSymbols = new llvm::DenseSet({ #define SYMBOL(Name, NameSpace, Header) {#NameSpace #Name}, #include "StdSymbolMap.inc" diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp index 26b3d7ad1c6c..e3de3e7411c7 100644 --- a/clang-tools-extra/clangd/unittests/RenameTests.cpp +++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp @@ -1198,6 +1198,29 @@ TEST(RenameTest, MainFileReferencesOnly) { expectedResult(Code, NewName)); } +TEST(RenameTest, NoRenameOnSymbolsFromSystemHeaders) { + // filter out references not from main file. + llvm::StringRef Test = + R"cpp( + #include + SystemSym^bol abc; + )cpp"; + + Annotations Code(Test); + auto TU = TestTU::withCode(Code.code()); + TU.AdditionalFiles["system"] = R"cpp( + class SystemSymbol {}; + )cpp"; + TU.ExtraArgs = {"-isystem", testRoot()}; + auto AST = TU.build(); + llvm::StringRef NewName = "abcde"; + + auto Results = rename({Code.point(), NewName, AST, testPath(TU.Filename)}); + EXPECT_FALSE(Results) << "expected rename returned an error: " << Code.code(); + auto ActualMessage = llvm::toString(Results.takeError()); + EXPECT_THAT(ActualMessage, testing::HasSubstr("not a supported kind")); +} + TEST(RenameTest, ProtobufSymbolIsExcluded) { Annotations Code("Prot^obuf buf;"); auto TU = TestTU::withCode(Code.code());