public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-6810] c-family: Fix up a -Wformat regression [PR104148]
@ 2022-01-21 21:45 Jakub Jelinek
0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-01-21 21:45 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:f36efe71bef8ddf72306aca313d28759434cf97a
commit r12-6810-gf36efe71bef8ddf72306aca313d28759434cf97a
Author: Jakub Jelinek <jakub@redhat.com>
Date: Fri Jan 21 22:44:53 2022 +0100
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.
Diff:
---
gcc/c-family/c-common.cc | 25 ++++++++++++--------
gcc/c-family/c-common.h | 3 ++-
gcc/c-family/c-format.cc | 2 +-
gcc/testsuite/c-c++-common/Wformat-pr104148.c | 33 +++++++++++++++++++++++++++
4 files changed, 51 insertions(+), 12 deletions(-)
diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index f56f6df8fe3..bf0749bf498 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -5592,7 +5592,7 @@ check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
firstarg = 1;
if (!closure)
check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[0],
- firstarg);
+ firstarg, OPT_Wnonnull);
}
tree attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx.fntype));
@@ -5611,7 +5611,7 @@ check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
if (a != NULL_TREE)
for (int i = firstarg; i < nargs; i++)
check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
- i + 1);
+ i + 1, OPT_Wnonnull);
else
{
/* Walk the argument list. If we encounter an argument number we
@@ -5627,7 +5627,8 @@ check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
if (a != NULL_TREE)
check_function_arguments_recurse (check_nonnull_arg, &ctx,
- argarray[i], i + 1);
+ argarray[i], i + 1,
+ OPT_Wnonnull);
}
}
return ctx.warned_p;
@@ -6095,14 +6096,16 @@ check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
/* Generic argument checking recursion routine. PARAM is the argument to
be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
- once the argument is resolved. CTX is context for the callback. */
+ once the argument is resolved. CTX is context for the callback.
+ OPT is the warning for which this is done. */
void
check_function_arguments_recurse (void (*callback)
(void *, tree, unsigned HOST_WIDE_INT),
void *ctx, tree param,
- unsigned HOST_WIDE_INT param_num)
+ unsigned HOST_WIDE_INT param_num,
+ opt_code opt)
{
- if (warning_suppressed_p (param))
+ if (opt != OPT_Wformat_ && warning_suppressed_p (param))
return;
if (CONVERT_EXPR_P (param)
@@ -6111,7 +6114,8 @@ check_function_arguments_recurse (void (*callback)
{
/* Strip coercion. */
check_function_arguments_recurse (callback, ctx,
- TREE_OPERAND (param, 0), param_num);
+ TREE_OPERAND (param, 0), param_num,
+ opt);
return;
}
@@ -6148,7 +6152,8 @@ check_function_arguments_recurse (void (*callback)
if (i == format_num)
{
check_function_arguments_recurse (callback, ctx,
- inner_arg, param_num);
+ inner_arg, param_num,
+ opt);
found_format_arg = true;
break;
}
@@ -6170,10 +6175,10 @@ check_function_arguments_recurse (void (*callback)
/* Check both halves of the conditional expression. */
check_function_arguments_recurse (callback, ctx,
TREE_OPERAND (param, 1),
- param_num);
+ param_num, opt);
check_function_arguments_recurse (callback, ctx,
TREE_OPERAND (param, 2),
- param_num);
+ param_num, opt);
return;
}
}
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 28c73fe2847..a8d6f82bb2c 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -853,7 +853,8 @@ extern void check_function_arguments_recurse (void (*)
(void *, tree,
unsigned HOST_WIDE_INT),
void *, tree,
- unsigned HOST_WIDE_INT);
+ unsigned HOST_WIDE_INT,
+ opt_code);
extern bool check_builtin_function_arguments (location_t, vec<location_t>,
tree, tree, int, tree *);
extern void check_function_format (const_tree, tree, int, tree *,
diff --git a/gcc/c-family/c-format.cc b/gcc/c-family/c-format.cc
index a2affdacd7c..98f28c0dcc6 100644
--- a/gcc/c-family/c-format.cc
+++ b/gcc/c-family/c-format.cc
@@ -1531,7 +1531,7 @@ check_format_info (function_format_info *info, tree params,
format_ctx.arglocs = arglocs;
check_function_arguments_recurse (check_format_arg, &format_ctx,
- format_tree, arg_num);
+ format_tree, arg_num, OPT_Wformat_);
location_t loc = format_ctx.res->format_string_loc;
diff --git a/gcc/testsuite/c-c++-common/Wformat-pr104148.c b/gcc/testsuite/c-c++-common/Wformat-pr104148.c
new file mode 100644
index 00000000000..6786463bf66
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wformat-pr104148.c
@@ -0,0 +1,33 @@
+/* PR c++/104148 */
+/* { dg-do compile } */
+/* { dg-options "-Wformat" } */
+
+char *foo (const char *) __attribute__((format_arg(1)));
+void bar (const char *, ...) __attribute__((format(printf, 1, 2)));
+
+void
+baz (int x)
+{
+ bar ("%ld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar (x ? "%ld" : "%ld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar (x ? "%ld" : "%lld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */
+ bar (foo ("%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar (x ? foo ("%ld") : "%ld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar (x ? foo ("%ld") : "%lld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */
+ bar (foo (x ? "%ld" : "%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar (foo (x ? "%ld" : "%lld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */
+ bar (("%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar ((x ? "%ld" : "%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar ((x ? "%ld" : "%lld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */
+ bar ((foo ("%ld")), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar ((x ? foo ("%ld") : "%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar ((x ? foo ("%ld") : "%lld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */
+ bar ((foo (x ? "%ld" : "%ld")), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ bar ((foo (x ? "%ld" : "%lld")), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */
+ /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-01-21 21:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 21:45 [gcc r12-6810] c-family: Fix up a -Wformat regression [PR104148] Jakub Jelinek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).