Author: Fangrui Song Date: 2026-03-09T19:35:17-07:00 New Revision: 88bd3f475bd8083bfc8ddc5eaa15ef7ba1d999a4
URL: https://github.com/llvm/llvm-project/commit/88bd3f475bd8083bfc8ddc5eaa15ef7ba1d999a4 DIFF: https://github.com/llvm/llvm-project/commit/88bd3f475bd8083bfc8ddc5eaa15ef7ba1d999a4.diff LOG: [clang] Adjust -pedantic-errors -WX/-Wno-error=X interaction (#184756) While -Wno-long-long suppresses -pedantic-errors diagnostics in both GCC and Clang, GCC -Wno-error=long-long emits warnings while Clang still emits errors. ``` % echo 'long long x = 0;' | gcc -std=c89 -pedantic-errors -Wno-error=long-long -x c -fsyntax-only - <stdin>:1:6: warning: ISO C90 does not support 'long long' [-Wlong-long] % echo 'long long x = 0;' | clang -std=c89 -pedantic-errors -Wno-error=long-long -x c -fsyntax-only - <stdin>:1:1: error: 'long long' is an extension when C99 mode is not enabled [-Werror,-Wlong-long] 1 | long long x = 0; | ^ 1 error generated. ``` The order of -pedantic-errors and -Wno-error=long-long does not matter. Two fixes to how extension diagnostics interact with -pedantic-errors in getDiagnosticSeverity(): 1. -Wno-error=X now downgrades -pedantic-errors diagnostics to warnings. setDiagnosticGroupWarningAsError() sets HasNoWarningAsError but not IsUser, so the ExtBehavior upgrade would re-promote to error. When HasNoWarningAsError is set, cap the ExtBehavior upgrade at Warning. 2. -Wfoo no longer prevents -pedantic-errors from upgrading to error. Previously, -Wfoo set IsUser which blocked the ExtBehavior upgrade entirely. Now only -Wno-foo (IsUser + Ignored) blocks the upgrade, matching GCC behavior. ``` % echo 'long long x = 0;' | myclang -std=c89 -pedantic-errors -Wno-error=long-long -x c -fsyntax-only - <stdin>:1:1: warning: 'long long' is an extension when C99 mode is not enabled [-Wlong-long] 1 | long long x = 0; | ^ 1 warning generated. ``` Fixes https://github.com/llvm/llvm-project/issues/184250 Added: Modified: clang/lib/Basic/DiagnosticIDs.cpp clang/test/Misc/diag-mapping.c Removed: ################################################################################ diff --git a/clang/lib/Basic/DiagnosticIDs.cpp b/clang/lib/Basic/DiagnosticIDs.cpp index dd0ba2a8b68cb..0aa543c8bcb1d 100644 --- a/clang/lib/Basic/DiagnosticIDs.cpp +++ b/clang/lib/Basic/DiagnosticIDs.cpp @@ -568,9 +568,16 @@ DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc, return diag::Severity::Ignored; // For extension diagnostics that haven't been explicitly mapped, check if we - // should upgrade the diagnostic. - if (IsExtensionDiag && !Mapping.isUser()) - Result = std::max(Result, State->ExtBehavior); + // should upgrade the diagnostic. Skip if the user explicitly suppressed it + // (e.g. -Wno-foo). + if (IsExtensionDiag && + !(Mapping.isUser() && Result == diag::Severity::Ignored)) { + if (Mapping.hasNoWarningAsError()) + Result = std::max(Result, + std::min(State->ExtBehavior, diag::Severity::Warning)); + else + Result = std::max(Result, State->ExtBehavior); + } // At this point, ignored errors can no longer be upgraded. if (Result == diag::Severity::Ignored) diff --git a/clang/test/Misc/diag-mapping.c b/clang/test/Misc/diag-mapping.c index edc137ec68fef..20b6cccb77884 100644 --- a/clang/test/Misc/diag-mapping.c +++ b/clang/test/Misc/diag-mapping.c @@ -18,12 +18,17 @@ // This should emit an error with -pedantic-errors. // RUN: not %clang_cc1 %s -pedantic-errors 2>&1 | grep "error:" -// This should emit a warning, because -Wfoo overrides -pedantic*. -// RUN: %clang_cc1 %s -pedantic-errors -Wextra-tokens 2>&1 | grep "warning:" +// RUN: %clang_cc1 %s -pedantic -Wno-error=extra-tokens 2>&1 | grep "warning:" + +// -Wfoo does not override -pedantic-errors; the diagnostic stays an error. +// RUN: not %clang_cc1 %s -pedantic-errors -Wextra-tokens 2>&1 | grep "error:" // This should emit nothing, because -Wno-extra-tokens overrides -pedantic* // RUN: %clang_cc1 %s -pedantic-errors -Wno-extra-tokens 2>&1 | not grep diagnostic +// -Wno-error=extra-tokens should downgrade -pedantic-errors to warning. +// RUN: %clang_cc1 %s -pedantic-errors -Wno-error=extra-tokens 2>&1 | grep "warning:" + #ifdef foo #endif bad // extension! _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
