Author: Kadir Cetinkaya Date: 2019-11-25T10:45:14+01:00 New Revision: e841029aef74d99d1cb9443edd4a7b761d84ff45
URL: https://github.com/llvm/llvm-project/commit/e841029aef74d99d1cb9443edd4a7b761d84ff45 DIFF: https://github.com/llvm/llvm-project/commit/e841029aef74d99d1cb9443edd4a7b761d84ff45.diff LOG: [clangd] Fix diagnostic location for macro expansions Summary: Diagnostic locations were broken when it was result of a macro expansion. This patch fixes it by using expansion location instead of location inside macro body. Fixes https://github.com/clangd/clangd/issues/201. Reviewers: hokein Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70494 Added: Modified: clang-tools-extra/clangd/Diagnostics.cpp clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp index c9e1ed6bc687..cd95807162bc 100644 --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -117,8 +117,8 @@ bool adjustDiagFromHeader(Diag &D, const clang::Diagnostic &Info, if (D.Severity < DiagnosticsEngine::Level::Error) return false; - const SourceLocation &DiagLoc = Info.getLocation(); const SourceManager &SM = Info.getSourceManager(); + const SourceLocation &DiagLoc = SM.getExpansionLoc(Info.getLocation()); SourceLocation IncludeInMainFile; auto GetIncludeLoc = [&SM](SourceLocation SLoc) { return SM.getIncludeLoc(SM.getFileID(SLoc)); diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index 374dae073e0b..fe7a8898c5de 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -941,7 +941,7 @@ TEST(DiagsInHeaders, OnlyErrorOrFatal) { WithNote(Diag(Header.range(), "error occurred here"))))); } -TEST(IgnoreDiags, FromNonWrittenSources) { +TEST(DiagsInHeaders, FromNonWrittenSources) { Annotations Main(R"cpp( #include [["a.h"]] void foo() {})cpp"); @@ -951,11 +951,49 @@ TEST(IgnoreDiags, FromNonWrittenSources) { TestTU TU = TestTU::withCode(Main.code()); TU.AdditionalFiles = {{"a.h", Header.code()}}; TU.ExtraArgs = {"-DFOO=NOOO"}; - EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); + EXPECT_THAT(TU.build().getDiagnostics(), + UnorderedElementsAre(AllOf( + Diag(Main.range(), + "in included file: use of undeclared identifier 'NOOO'"), + WithNote(Diag(Header.range(), "error occurred here"))))); +} + +TEST(DiagsInHeaders, ErrorFromMacroExpansion) { + Annotations Main(R"cpp( + void bar() { + int fo; + #include [["a.h"]] + })cpp"); + Annotations Header(R"cpp( + #define X foo + X;)cpp"); + TestTU TU = TestTU::withCode(Main.code()); + TU.AdditionalFiles = {{"a.h", Header.code()}}; + EXPECT_THAT(TU.build().getDiagnostics(), + UnorderedElementsAre( + Diag(Main.range(), "in included file: use of undeclared " + "identifier 'foo'; did you mean 'fo'?"))); +} + +TEST(DiagsInHeaders, ErrorFromMacroArgument) { + Annotations Main(R"cpp( + void bar() { + int fo; + #include [["a.h"]] + })cpp"); + Annotations Header(R"cpp( + #define X(arg) arg + X(foo);)cpp"); + TestTU TU = TestTU::withCode(Main.code()); + TU.AdditionalFiles = {{"a.h", Header.code()}}; + EXPECT_THAT(TU.build().getDiagnostics(), + UnorderedElementsAre( + Diag(Main.range(), "in included file: use of undeclared " + "identifier 'foo'; did you mean 'fo'?"))); } TEST(IgnoreDiags, FromNonWrittenInclude) { - TestTU TU = TestTU::withCode(""); + TestTU TU; TU.ExtraArgs.push_back("--include=a.h"); TU.AdditionalFiles = {{"a.h", "void main();"}}; // The diagnostic "main must return int" is from the header, we don't attempt @@ -964,6 +1002,5 @@ TEST(IgnoreDiags, FromNonWrittenInclude) { } } // namespace - } // namespace clangd } // namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits