diff mbox

[C] Pipe down bogus missing initializer warning (PR c/64709)

Message ID 20150129205045.GF4074@redhat.com
State New
Headers show

Commit Message

Marek Polacek Jan. 29, 2015, 8:50 p.m. UTC
Here the problem is that we issue a bogus "missing initializer" warning
even for zero-initializer "{ 0 }".  So in case we're sure we have such
a zero-initializer, suppress the warning.  If the constructor has a single
element which is not zero, don't set anything; it might be a constructor
from another init level, which is checked at a different time.

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

2015-01-29  Marek Polacek  <polacek@redhat.com>

	PR c/64709
	* c-typeck.c (pop_init_level): If constructor_elements has
	exactly one element with integer_zerop value, set constructor_zeroinit
	to 1.  Remove braces around warning_init call.

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


	Marek

Comments

Jakub Jelinek Jan. 29, 2015, 8:59 p.m. UTC | #1
On Thu, Jan 29, 2015 at 09:50:45PM +0100, Marek Polacek wrote:
> Here the problem is that we issue a bogus "missing initializer" warning
> even for zero-initializer "{ 0 }".  So in case we're sure we have such
> a zero-initializer, suppress the warning.  If the constructor has a single
> element which is not zero, don't set anything; it might be a constructor
> from another init level, which is checked at a different time.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2015-01-29  Marek Polacek  <polacek@redhat.com>
> 
> 	PR c/64709
> 	* c-typeck.c (pop_init_level): If constructor_elements has
> 	exactly one element with integer_zerop value, set constructor_zeroinit
> 	to 1.  Remove braces around warning_init call.
> 
> 	* gcc.dg/pr64709.c: New test.

Ok, thanks.

	Jakub
diff mbox

Patch

--- gcc/c/c-typeck.c.jj	2015-01-27 09:13:05.091753264 +0100
+++ gcc/c/c-typeck.c	2015-01-29 19:01:30.841668899 +0100
@@ -7557,20 +7557,28 @@  pop_init_level (location_t loc, int impl
 	}
     }
 
-  /* Initialization with { } counts as zeroinit.  */
-  if (vec_safe_length (constructor_elements) == 0)
-    constructor_zeroinit = 1;
-  /* If the constructor has more than one element, it can't be { 0 }.  */
-  else if (vec_safe_length (constructor_elements) != 1)
-    constructor_zeroinit = 0;
+  switch (vec_safe_length (constructor_elements))
+    {
+    case 0:
+      /* Initialization with { } counts as zeroinit.  */
+      constructor_zeroinit = 1;
+      break;
+    case 1:
+      /* This might be zeroinit as well.  */
+      if (integer_zerop ((*constructor_elements)[0].value))
+	constructor_zeroinit = 1;
+      break;
+    default:
+      /* If the constructor has more than one element, it can't be { 0 }.  */
+      constructor_zeroinit = 0;
+      break;
+    }
 
   /* Warn when some structs are initialized with direct aggregation.  */
   if (!implicit && found_missing_braces && warn_missing_braces
       && !constructor_zeroinit)
-    {
-      warning_init (loc, OPT_Wmissing_braces,
-		    "missing braces around initializer");
-    }
+    warning_init (loc, OPT_Wmissing_braces,
+		  "missing braces around initializer");
 
   /* Warn when some struct elements are implicitly initialized to zero.  */
   if (warn_missing_field_initializers
--- gcc/testsuite/gcc.dg/pr64709.c.jj	2015-01-29 19:00:37.816566266 +0100
+++ gcc/testsuite/gcc.dg/pr64709.c	2015-01-29 19:00:32.675653268 +0100
@@ -0,0 +1,10 @@ 
+/* PR c/64709 */
+/* { dg-do compile } */
+/* { dg-options "-Wmissing-field-initializers" } */
+
+struct S { int a, b; };
+void
+foo (void)
+{
+  struct S s[] = { { 1, 2 }, { 0 } }; /* { dg-bogus "missing initializer for field" } */
+}