Skip to content

Commit dbc9e1c

Browse files
committed
[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
1 parent 9ffaba8 commit dbc9e1c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) {
124124
bool adjustDiagFromHeader(Diag &D, const clang::Diagnostic &Info,
125125
const LangOptions &LangOpts) {
126126
// We only report diagnostics with at least error severity from headers.
127-
if (D.Severity < DiagnosticsEngine::Level::Error)
127+
// Use default severity to avoid noise with -Werror.
128+
if (!Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
129+
Info.getID()))
128130
return false;
129131

130132
const SourceManager &SM = Info.getSourceManager();
@@ -514,7 +516,8 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
514516
if (Info.getLocation().isInvalid()) {
515517
// Handle diagnostics coming from command-line arguments. The source manager
516518
// is *not* available at this point, so we cannot use it.
517-
if (DiagLevel < DiagnosticsEngine::Level::Error) {
519+
if (!Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
520+
Info.getID())) {
518521
IgnoreDiagnostics::log(DiagLevel, Info);
519522
return; // non-errors add too much noise, do not show them.
520523
}

clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,20 @@ TEST(DiagsInHeaders, OnlyErrorOrFatal) {
10331033
WithNote(Diag(Header.range(), "error occurred here")))));
10341034
}
10351035

1036+
TEST(DiagsInHeaders, OnlyDefaultErrorOrFatal) {
1037+
Annotations Main(R"cpp(
1038+
#include [["a.h"]] // get unused "foo" warning when building preamble.
1039+
)cpp");
1040+
Annotations Header(R"cpp(
1041+
namespace { void foo() {} }
1042+
void func() {foo();} ;)cpp");
1043+
TestTU TU = TestTU::withCode(Main.code());
1044+
TU.AdditionalFiles = {{"a.h", std::string(Header.code())}};
1045+
// promote warnings to errors.
1046+
TU.ExtraArgs = {"-Werror", "-Wunused"};
1047+
EXPECT_THAT(TU.build().getDiagnostics(), IsEmpty());
1048+
}
1049+
10361050
TEST(DiagsInHeaders, FromNonWrittenSources) {
10371051
Annotations Main(R"cpp(
10381052
#include [["a.h"]]

0 commit comments

Comments
 (0)