[clang] [clang][diagnostics] add '-Wundef-true' warning option (PR #128265)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 1 11:35:26 PST 2025


================
@@ -260,7 +260,8 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
           PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
 
           const DiagnosticsEngine &DiagEngine = PP.getDiagnostics();
-          // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
+          // If 'Wundef' is enabled, do not emit 'undef-prefix' or 'undef-true'
----------------
zygoloid wrote:

You should pick the diagnostic ID (`warn_pp_undef_identifier` or `warn_pp_undef_true_identifier`) first, then change the code above to emit that diagnostic, and change this test to check that diagnostic, rather than conditionally emitting another diagnostic below.

Branching on whether a diagnostic is enabled is discouraged. We do it here because `-Wundef-prefix=` is a weird special case, but `-Wundef-true` isn't, so it should do the more conventional thing of picking the diagnostic kind in advance. (There's a general principle here that we want it to be possible to enable "too many" warnings and filter them after the fact and still get the same output, as much as possible.)

This is observable in that, with `-Wundef` enabled, the diagnostic for use of `#if true` will display the more precise warning flag `-Wundef-true`.

https://github.com/llvm/llvm-project/pull/128265


More information about the cfe-commits mailing list