On Fri, Aug 15, 2025 at 10:46:04AM -0400, Jason Merrill wrote:
> This name is confusing wrt warn_builtin_macro_redefined; since it seems to
> be used internally to suppress the warning, maybe call it
> suppress_builtin_macro_warning?  Or allow_... if you don't want to reverse
> the sense?

Ok, will go with suppress_builtin_macro_warnings and revert the values.
If all the diagnostics had a guarding -W* option, I could just disable all
those temporarily, but there is -Wbuiltin-macro-redefined (for the less
important builtin macros and only for redefinitions), -Wkeyword-macro (for
keywords, both #define/#undef) and then unconditional warnings/errors for
the rest (more important builtin macros, __STDC* and the newly marked
macros).  I didn't add a -W* option for the last category because I thought
it was intentional that one shouldn't be able to hide the more important
ones.  Seems clang abuses -Wbuiltin-macro-redefined for all #define/#undef
of all the builtin and builtin-like macros (except keywords).

> > @@ -3412,8 +3412,11 @@ warn_of_redefinition (cpp_reader *pfile, 
> > cpp_hashnode *node,
> >     /* Some redefinitions need to be warned about regardless.  */
> >     if (node->flags & NODE_WARN)
> >       {
> > -      /* Ignore NODE_WARN on -Wkeyword-macro registered identifiers 
> > though.  */
> > -      if (!CPP_OPTION (pfile, cpp_warn_keyword_macro) || !cpp_keyword_p 
> > (node))
> > +      /* Ignore NODE_WARN on -Wkeyword-macro registered identifiers though
> > +    or during cpp_define.  */
> > +      if (CPP_OPTION (pfile, warn_builtin_macros)
> 
> Don't we also want to return early if warn_builtin_macros is unset (modulo
> renaming/reversing)?

The point of this spot is return true; only if we should honor NODE_WARN for
the redefinitions.
They shouldn't be honored for cpp_keyword_p - -Wkeyword-macro is diagnosed
earlier and warn_of_redefinition should then act normally for those, only
error if redefining them to something else.
And we should ignore NODE_WARN bits altogether if
CPP_OPTION (pfile, suppress_builtin_macro_warnings) (so not return true
in that case).
The CPP_OPTION (pfile, cpp_warn_keyword_macro) check is there just to
avoid calling cpp_keyword_p (node) uselessly when -Wkeyword-macro isn't
enabled at all (the common case).

So I think
  if (node->flags & NODE_WARN)
    {
      if (!CPP_OPTION (pfile, suppress_builtin_macro_warnings)
          && (!CPP_OPTION (pfile, cpp_warn_keyword_macro)
              || !cpp_keyword_p (node)))
        return true;
    }
is the right condition.  If suppressing the diagnostics, return true
will not be done, otherwise if -Wkeyword-macro and it is a keyword
neither, otherwise it will return true and diagnose redefinition
even if it has the same value.

        Jakub

Reply via email to