diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -122,8 +122,12 @@ return SM.getIncludeLoc(SM.getFileID(SLoc)); }; for (auto IncludeLocation = GetIncludeLoc(DiagLoc); IncludeLocation.isValid(); - IncludeLocation = GetIncludeLoc(IncludeLocation)) - IncludeInMainFile = IncludeLocation; + IncludeLocation = GetIncludeLoc(IncludeLocation)) { + if (clangd::isInsideMainFile(IncludeLocation, SM)) { + IncludeInMainFile = IncludeLocation; + break; + } + } if (IncludeInMainFile.isInvalid()) return false; diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -948,6 +948,13 @@ EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); } +TEST(IgnoreDiags, FromNonWrittenInclude) { + TestTU TU = TestTU::withCode(""); + TU.ExtraArgs.push_back("--include=a.h"); + TU.AdditionalFiles = {{"a.h", "void main();"}}; + EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); +} + } // namespace } // namespace clangd