c-family: Fix up a -Wformat regression [PR104148]
authorJakub Jelinek <jakub@redhat.com>
Fri, 21 Jan 2022 21:44:53 +0000 (22:44 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 21 Jan 2022 21:44:53 +0000 (22:44 +0100)
commitf36efe71bef8ddf72306aca313d28759434cf97a
treed455223d95ef696c7357eadff83475ba4fd986f7
parent2e01fde49eab8dfb7ba48b22c2da596b16571525
c-family: Fix up a -Wformat regression [PR104148]

As can be seen on the testcase, GCC 11 no longer warns if the format
string is wrapped inside of ()s.
This regressed with r11-2457-gdf5cf47a978, which added
if (TREE_NO_WARNING (param)) return;
to check_function_arguments_recurse.  That function is used with a callback
for two cases, for -Wformat and for -Wnonnull.  For the latter it is
desirable to not warn in parameters or their subexpressions where that
warning is suppressed, but for -Wformat the function is used solely
to discover the string literals if any so that the c-format.cc code can
diagnose them.  I believe no warning suppression should stand in the
way of that, -Wformat* warnings should be decided from warning suppression
on the CALL_EXPR only.
In the PR Martin argued that now that we have specialized
warning_suppressed_p we should use it, so instead of adding a bool
arg to check_function_arguments_recurse I've added opt_code to the
function, but will defer the warning_suppressed_p change to him.
For OPT_Wformat_ we don't want to call it anyway at all (as I said,
I think there should be no suppression for it during the string discovery,
there isn't just one -Wformat= option, there are many and
warning_suppression_p even with no_warnings actually tests the
TREE_NO_WARNING bit).
Initially, I thought I'd restrict also call to fn with format_arg attribute
handling in check_function_arguments_recurse to OPT_Wformat_ only, but
after looking around, it perhaps is intentional that way, most functions
with format_arg attribute don't have nonnull attribute for that arg too,
various gettext implementations handle NULL argument by passing it through,
but when result of gettext (NULL) etc. is passed to non-NULL argument, it
makes sense to warn.

2022-01-21  Jakub Jelinek  <jakub@redhat.com>

PR c++/104148
* c-common.h (check_function_arguments_recurse): Add for_format
arg.
* c-common.cc (check_function_nonnull): Pass false to
check_function_arguments_recurse's last argument.
(check_function_arguments_recurse): Add for_format argument,
if true, don't stop on warning_suppressed_p.
* c-format.cc (check_format_info): Pass true to
check_function_arguments_recurse's last argument.

* c-c++-common/Wformat-pr104148.c: New test.
gcc/c-family/c-common.cc
gcc/c-family/c-common.h
gcc/c-family/c-format.cc
gcc/testsuite/c-c++-common/Wformat-pr104148.c [new file with mode: 0644]