From c8f7496257f6c385e3eff33cd14cdb060c49063f Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Fri, 22 Feb 2019 09:43:56 +0000 Subject: [PATCH] [clangd] Don't attach FixIt to the source code in macro. Summary: We are less certain it is the correct fix. Also, clang doesn't apply FixIt to the source code in macro. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D58525 llvm-svn: 354664 --- clang-tools-extra/clangd/Diagnostics.cpp | 5 +++++ clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp index 186ab06..2105395 100644 --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -335,6 +335,11 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, llvm::SmallVector Edits; for (auto &FixIt : Info.getFixItHints()) { + // Follow clang's behavior, don't apply FixIt to the code in macros, + // we are less certain it is the right fix. + if (FixIt.RemoveRange.getBegin().isMacroID() || + FixIt.RemoveRange.getEnd().isMacroID()) + return false; if (!isInsideMainFile(FixIt.RemoveRange.getBegin(), Info.getSourceManager())) return false; diff --git a/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp b/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp index bbd94b8..4bbc7c7 100644 --- a/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp +++ b/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp @@ -215,6 +215,18 @@ TEST(DiagnosticsTest, InsideMacros) { "'int *' with an rvalue of type 'int'"))); } +TEST(DiagnosticsTest, NoFixItInMacro) { + Annotations Test(R"cpp( + #define Define(name) void name() {} + + [[Define]](main) + )cpp"); + auto TU = TestTU::withCode(Test.code()); + EXPECT_THAT(TU.build().getDiagnostics(), + ElementsAre(AllOf(Diag(Test.range(), "'main' must return 'int'"), + Not(WithFix(_))))); +} + TEST(DiagnosticsTest, ToLSP) { clangd::Diag D; D.Message = "something terrible happened"; -- 2.7.4