public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c-family: Fix up a -Wformat regression [PR104148]
@ 2022-01-21  7:16 Jakub Jelinek
  2022-01-21 21:38 ` Joseph Myers
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2022-01-21  7:16 UTC (permalink / raw)
  To: Joseph S. Myers, Marek Polacek, Jason Merrill; +Cc: gcc-patches, Martin Sebor

Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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.h.jj	2022-01-20 11:30:45.329581659 +0100
+++ gcc/c-family/c-common.h	2022-01-20 19:36:35.195601009 +0100
@@ -853,7 +853,8 @@ extern void check_function_arguments_rec
 					      (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 *,
--- gcc/c-family/c-common.cc.jj	2022-01-20 11:30:45.329581659 +0100
+++ gcc/c-family/c-common.cc	2022-01-20 19:41:02.787901300 +0100
@@ -5592,7 +5592,7 @@ check_function_nonnull (nonnull_arg_ctx
       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
   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
 
 	  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
 
 /* 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 (
     {
       /* 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 (
 	      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 (
 	  /* 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;
 	}
     }
--- gcc/c-family/c-format.cc.jj	2022-01-20 11:30:45.346581418 +0100
+++ gcc/c-family/c-format.cc	2022-01-20 19:46:21.021500071 +0100
@@ -1531,7 +1531,7 @@ check_format_info (function_format_info
   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;
 
--- gcc/testsuite/c-c++-common/Wformat-pr104148.c.jj	2022-01-20 19:53:24.852638396 +0100
+++ gcc/testsuite/c-c++-common/Wformat-pr104148.c	2022-01-20 19:52:57.579015595 +0100
@@ -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 } */
+}

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c-family: Fix up a -Wformat regression [PR104148]
  2022-01-21  7:16 [PATCH] c-family: Fix up a -Wformat regression [PR104148] Jakub Jelinek
@ 2022-01-21 21:38 ` Joseph Myers
  0 siblings, 0 replies; 2+ messages in thread
From: Joseph Myers @ 2022-01-21 21:38 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Marek Polacek, Jason Merrill, gcc-patches

On Fri, 21 Jan 2022, Jakub Jelinek via Gcc-patches wrote:

> 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.

OK.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-21 21:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21  7:16 [PATCH] c-family: Fix up a -Wformat regression [PR104148] Jakub Jelinek
2022-01-21 21:38 ` Joseph Myers

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).