From: Haojian Wu Date: Thu, 14 May 2020 07:23:42 +0000 (+0200) Subject: [clangd] Only emit default error/fatal diagnostices from included files. X-Git-Tag: llvmorg-12-init~6107 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dbc9e1c39aed43e6faa6b29caeed7bc5b43569f1;p=platform%2Fupstream%2Fllvm.git [clangd] Only emit default error/fatal diagnostices from included files. Summary: This would avoid adding too much noise when there is a "-Wall" in the compile command. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79923 --- diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp index 3558ca9..f1fbaf4 100644 --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -124,7 +124,9 @@ Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) { bool adjustDiagFromHeader(Diag &D, const clang::Diagnostic &Info, const LangOptions &LangOpts) { // We only report diagnostics with at least error severity from headers. - if (D.Severity < DiagnosticsEngine::Level::Error) + // Use default severity to avoid noise with -Werror. + if (!Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError( + Info.getID())) return false; const SourceManager &SM = Info.getSourceManager(); @@ -514,7 +516,8 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, if (Info.getLocation().isInvalid()) { // Handle diagnostics coming from command-line arguments. The source manager // is *not* available at this point, so we cannot use it. - if (DiagLevel < DiagnosticsEngine::Level::Error) { + if (!Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError( + Info.getID())) { IgnoreDiagnostics::log(DiagLevel, Info); return; // non-errors add too much noise, do not show them. } diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index 3d838c7..e675d01 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -1033,6 +1033,20 @@ TEST(DiagsInHeaders, OnlyErrorOrFatal) { WithNote(Diag(Header.range(), "error occurred here"))))); } +TEST(DiagsInHeaders, OnlyDefaultErrorOrFatal) { + Annotations Main(R"cpp( + #include [["a.h"]] // get unused "foo" warning when building preamble. + )cpp"); + Annotations Header(R"cpp( + namespace { void foo() {} } + void func() {foo();} ;)cpp"); + TestTU TU = TestTU::withCode(Main.code()); + TU.AdditionalFiles = {{"a.h", std::string(Header.code())}}; + // promote warnings to errors. + TU.ExtraArgs = {"-Werror", "-Wunused"}; + EXPECT_THAT(TU.build().getDiagnostics(), IsEmpty()); +} + TEST(DiagsInHeaders, FromNonWrittenSources) { Annotations Main(R"cpp( #include [["a.h"]]