Avoid warning about unused value with VLAs in statement expressions (PR c/49120)

Submitted by Jakub Jelinek on May 23, 2011, 4:51 p.m.

Details

Message ID 20110523165153.GC17079@tyan-ft48-01.lab.bos.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek May 23, 2011, 4:51 p.m.
Hi!

This patch shuts up a warning about VLA bounds expression being unused.
start_decl adds the (usually SAVE_EXPR) expression to statements to make
sure it is evaluated, but if that happens in statement expression where
unused value warnings are postponed until we know what is the return value,
it will be warned on.  Fixed by casting it to void.

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

2011-05-23  Jakub Jelinek  <jakub@redhat.com>

	PR c/49120
	* c-decl.c (start_decl): Convert expr to void_type_node.

	* gcc.dg/pr49120.c: New test.


	Jakub

Comments

Joseph S. Myers May 23, 2011, 5:07 p.m.
On Mon, 23 May 2011, Jakub Jelinek wrote:

> Hi!
> 
> This patch shuts up a warning about VLA bounds expression being unused.
> start_decl adds the (usually SAVE_EXPR) expression to statements to make
> sure it is evaluated, but if that happens in statement expression where
> unused value warnings are postponed until we know what is the return value,
> it will be warned on.  Fixed by casting it to void.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/4.6?
> 
> 2011-05-23  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c/49120
> 	* c-decl.c (start_decl): Convert expr to void_type_node.
> 
> 	* gcc.dg/pr49120.c: New test.

OK.

Patch hide | download patch | download mbox

--- gcc/c-decl.c.jj	2011-05-11 19:39:04.000000000 +0200
+++ gcc/c-decl.c	2011-05-23 14:23:58.000000000 +0200
@@ -3942,7 +3942,7 @@  start_decl (struct c_declarator *declara
     return 0;
 
   if (expr)
-    add_stmt (expr);
+    add_stmt (fold_convert (void_type_node, expr));
 
   if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
     warning (OPT_Wmain, "%q+D is usually a function", decl);
--- gcc/testsuite/gcc.dg/pr49120.c.jj	2011-05-23 14:28:53.000000000 +0200
+++ gcc/testsuite/gcc.dg/pr49120.c	2011-05-23 14:28:26.000000000 +0200
@@ -0,0 +1,11 @@ 
+/* PR c/49120 */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+int
+main ()
+{
+  int a = 1;
+  int c = ({ char b[a + 1]; b[0] = 0; b[0]; });
+  return c;
+}