Skip to content

Commit 004acbb

Browse files
committed
[clangd] Suppress warning about system_header pragma when editing headers
Not sure it's OK to suppress this in clang itself - if we're building a PCH or module, maybe it matters? Differential Revision: https://reviews.llvm.org/D116925
1 parent c14cf92 commit 004acbb

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,14 @@ void StoreDiags::flushLastDiag() {
879879
}
880880

881881
bool isBuiltinDiagnosticSuppressed(unsigned ID,
882-
const llvm::StringSet<> &Suppress) {
882+
const llvm::StringSet<> &Suppress,
883+
const LangOptions &LangOpts) {
884+
// Don't complain about header-only stuff in mainfiles if it's a header.
885+
// FIXME: would be cleaner to suppress in clang, once we decide whether the
886+
// behavior should be to silently-ignore or respect the pragma.
887+
if (ID == diag::pp_pragma_sysheader_in_main_file && LangOpts.IsHeaderFile)
888+
return true;
889+
883890
if (const char *CodePtr = getDiagnosticCode(ID)) {
884891
if (Suppress.contains(normalizeSuppressedCode(CodePtr)))
885892
return true;

clang-tools-extra/clangd/Diagnostics.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ class StoreDiags : public DiagnosticConsumer {
180180

181181
/// Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
182182
bool isBuiltinDiagnosticSuppressed(unsigned ID,
183-
const llvm::StringSet<> &Suppressed);
183+
const llvm::StringSet<> &Suppressed,
184+
const LangOptions &);
184185
/// Take a user-specified diagnostic code, and convert it to a normalized form
185186
/// stored in the config and consumed by isBuiltinDiagnosticsSuppressed.
186187
///

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
445445
ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
446446
const clang::Diagnostic &Info) {
447447
if (Cfg.Diagnostics.SuppressAll ||
448-
isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress))
448+
isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
449+
Clang->getLangOpts()))
449450
return DiagnosticsEngine::Ignored;
450451

451452
auto It = OverriddenSeverity.find(Info.getID());

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
350350
PreambleDiagnostics.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
351351
const clang::Diagnostic &Info) {
352352
if (Cfg.Diagnostics.SuppressAll ||
353-
isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress))
353+
isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
354+
*CI.getLangOpts()))
354355
return DiagnosticsEngine::Ignored;
355356
switch (Info.getID()) {
356357
case diag::warn_no_newline_eof:

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,20 @@ TEST_F(ConfigCompileTests, DiagnosticSuppression) {
277277
"unreachable-code", "unused-variable",
278278
"typecheck_bool_condition",
279279
"unexpected_friend", "warn_alloca"));
280-
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(diag::warn_unreachable,
281-
Conf.Diagnostics.Suppress));
280+
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(
281+
diag::warn_unreachable, Conf.Diagnostics.Suppress, LangOptions()));
282282
// Subcategory not respected/suppressed.
283-
EXPECT_FALSE(isBuiltinDiagnosticSuppressed(diag::warn_unreachable_break,
284-
Conf.Diagnostics.Suppress));
285-
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(diag::warn_unused_variable,
286-
Conf.Diagnostics.Suppress));
283+
EXPECT_FALSE(isBuiltinDiagnosticSuppressed(
284+
diag::warn_unreachable_break, Conf.Diagnostics.Suppress, LangOptions()));
285+
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(
286+
diag::warn_unused_variable, Conf.Diagnostics.Suppress, LangOptions()));
287287
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(diag::err_typecheck_bool_condition,
288-
Conf.Diagnostics.Suppress));
289-
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(diag::err_unexpected_friend,
290-
Conf.Diagnostics.Suppress));
291-
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(diag::warn_alloca,
292-
Conf.Diagnostics.Suppress));
288+
Conf.Diagnostics.Suppress,
289+
LangOptions()));
290+
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(
291+
diag::err_unexpected_friend, Conf.Diagnostics.Suppress, LangOptions()));
292+
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(
293+
diag::warn_alloca, Conf.Diagnostics.Suppress, LangOptions()));
293294

294295
Frag.Diagnostics.Suppress.emplace_back("*");
295296
EXPECT_TRUE(compileAndApply());

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,17 @@ TEST(DiagnosticsTest, NoFixItInMacro) {
771771
Not(WithFix(_)))));
772772
}
773773

774+
TEST(DiagnosticsTest, PragmaSystemHeader) {
775+
Annotations Test("#pragma clang [[system_header]]\n");
776+
auto TU = TestTU::withCode(Test.code());
777+
EXPECT_THAT(
778+
*TU.build().getDiagnostics(),
779+
ElementsAre(AllOf(
780+
Diag(Test.range(), "#pragma system_header ignored in main file"))));
781+
TU.Filename = "TestTU.h";
782+
EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty());
783+
}
784+
774785
TEST(ClangdTest, MSAsm) {
775786
// Parsing MS assembly tries to use the target MCAsmInfo, which we don't link.
776787
// We used to crash here. Now clang emits a diagnostic, which we filter out.

0 commit comments

Comments
 (0)