This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch for bug 24010


This patch fixes regression bug 24010, concerning the lack of a
warning for overriding a previous initializer (in the presence of
designated initializers), by adding an option to enable such a warning
and enabling it at -Wextra.  (GCC already warns unconditionally in the
case that the overridden initializer has side effects, since it's
unspecified whether those side effects occur in that case.)

Bootstrapped with no regressions on i686-pc-linux-gnu.  Applied to
mainline.  I don't consider this sort of patch, adding warnings for
valid code, to be suitable for previous release branches.

2006-09-30  Joseph S. Myers  <joseph@codesourcery.com>

	PR c/24010
	* c.opt (Woverride-init): New.
	* c-opts.c (c_common_post_options): Default warn_override_init to
	extra_warnings.
	* c-typeck.c (add_pending_init, output_init_element): Warn for
	overriding initializers without side effects.
	* doc/invoke.texi (-Woverride-init): Document.
	(-Wextra): Update.

testsuite:
2006-09-30  Joseph S. Myers  <joseph@codesourcery.com>

	PR c/24010
	* gcc.dg/Woverride-init-1.c, gcc.dg/Woverride-init-2.c,
	gcc.dg/Woverride-init-3.c: New tests.

diff -rupN GCC.orig/gcc/c-opts.c GCC/gcc/c-opts.c
--- GCC.orig/gcc/c-opts.c	2006-09-17 18:24:26.000000000 +0000
+++ GCC/gcc/c-opts.c	2006-09-30 16:25:40.000000000 +0000
@@ -1016,12 +1016,14 @@ c_common_post_options (const char **pfil
   if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
     flag_exceptions = 1;
 
-  /* -Wextra implies -Wsign-compare and -Wmissing-field-initializers,
-     but not if explicitly overridden.  */
+  /* -Wextra implies -Wsign-compare, -Wmissing-field-initializers and
+     -Woverride-init, but not if explicitly overridden.  */
   if (warn_sign_compare == -1)
     warn_sign_compare = extra_warnings;
   if (warn_missing_field_initializers == -1)
     warn_missing_field_initializers = extra_warnings;
+  if (warn_override_init == -1)
+    warn_override_init = extra_warnings;
 
   /* -Wpointer_sign is disabled by default, but it is enabled if any
      of -Wall or -pedantic are given.  */
diff -rupN GCC.orig/gcc/c-typeck.c GCC/gcc/c-typeck.c
--- GCC.orig/gcc/c-typeck.c	2006-09-24 11:31:33.000000000 +0000
+++ GCC/gcc/c-typeck.c	2006-09-30 16:27:45.000000000 +0000
@@ -5737,6 +5737,8 @@ add_pending_init (tree purpose, tree val
 	    {
 	      if (TREE_SIDE_EFFECTS (p->value))
 		warning_init ("initialized field with side-effects overwritten");
+	      else if (warn_override_init)
+		warning_init ("initialized field overwritten");
 	      p->value = value;
 	      return;
 	    }
@@ -5758,6 +5760,8 @@ add_pending_init (tree purpose, tree val
 	    {
 	      if (TREE_SIDE_EFFECTS (p->value))
 		warning_init ("initialized field with side-effects overwritten");
+	      else if (warn_override_init)
+		warning_init ("initialized field overwritten");
 	      p->value = value;
 	      return;
 	    }
@@ -6230,6 +6234,8 @@ output_init_element (tree value, bool st
       if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
 				       constructor_elements)->value))
 	warning_init ("initialized field with side-effects overwritten");
+      else if (warn_override_init)
+	warning_init ("initialized field overwritten");
 
       /* We can have just one union field set.  */
       constructor_elements = 0;
diff -rupN GCC.orig/gcc/c.opt GCC/gcc/c.opt
--- GCC.orig/gcc/c.opt	2006-05-19 10:23:14.000000000 +0000
+++ GCC/gcc/c.opt	2006-09-30 16:24:22.000000000 +0000
@@ -319,6 +319,10 @@ Woverloaded-virtual
 C++ ObjC++ Var(warn_overloaded_virtual)
 Warn about overloaded virtual function names
 
+Woverride-init
+C ObjC Var(warn_override_init) Init(-1)
+Warn about overriding initializers without side effects
+
 Wparentheses
 C ObjC C++ ObjC++ Var(warn_parentheses)
 Warn about possibly missing parentheses
diff -rupN GCC.orig/gcc/doc/invoke.texi GCC/gcc/doc/invoke.texi
--- GCC.orig/gcc/doc/invoke.texi	2006-09-26 11:23:45.000000000 +0000
+++ GCC/gcc/doc/invoke.texi	2006-09-30 16:34:03.000000000 +0000
@@ -2876,6 +2876,12 @@ This warning can be independently contro
 @option{-Wmissing-field-initializers}.
 
 @item
+An initialized field without side effects is overridden when using
+designated initializers (@pxref{Designated Inits, , Designated
+Initializers}).  This warning can be independently controlled by
+@option{-Woverride-init}.
+
+@item
 A function parameter is declared without a type specifier in K&R-style
 functions:
 
@@ -3302,6 +3308,18 @@ attribute.
 @opindex Wno-overflow
 Do not warn about compile-time overflow in constant expressions.
 
+@item -Woverride-init
+@opindex Woverride-init
+@opindex W
+@opindex Wextra
+Warn if an initialized field without side effects is overridden when
+using designated initializers (@pxref{Designated Inits, , Designated
+Initializers}).
+
+This warning is included in @option{-Wextra}.  To get other
+@option{-Wextra} warnings without this one, use @samp{-Wextra
+-Wno-override-init}.
+
 @item -Wpacked
 @opindex Wpacked
 Warn if a structure is given the packed attribute, but the packed
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/Woverride-init-1.c GCC/gcc/testsuite/gcc.dg/Woverride-init-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/Woverride-init-1.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/Woverride-init-1.c	2006-09-30 19:08:30.000000000 +0000
@@ -0,0 +1,28 @@
+/* Test for warnings for overriding designated initializers:
+   -Woverride-init.  Bug 24010.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-Woverride-init" } */
+
+struct s { int a; int b; int c; };
+union u { char a; long long b; };
+
+struct s s0 = {
+  .a = 1,
+  .b = 2,
+  .a = 3, /* { dg-warning "initialized field overwritten|near init" } */
+  4, /* { dg-warning "initialized field overwritten|near init" } */
+  5
+};
+
+union u u0 = {
+  .a = 1,
+  .b = 2, /* { dg-warning "initialized field overwritten|near init" } */
+  .a = 3 }; /* { dg-warning "initialized field overwritten|near init" } */
+
+int a[5] = {
+  [0] = 1,
+  [1] = 2,
+  [0] = 3, /* { dg-warning "initialized field overwritten|near init" } */
+  [2] = 4
+};
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/Woverride-init-2.c GCC/gcc/testsuite/gcc.dg/Woverride-init-2.c
--- GCC.orig/gcc/testsuite/gcc.dg/Woverride-init-2.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/Woverride-init-2.c	2006-09-30 19:08:42.000000000 +0000
@@ -0,0 +1,28 @@
+/* Test for warnings for overriding designated initializers:
+   -Wextra.  Bug 24010.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-Wextra" } */
+
+struct s { int a; int b; int c; };
+union u { char a; long long b; };
+
+struct s s0 = {
+  .a = 1,
+  .b = 2,
+  .a = 3, /* { dg-warning "initialized field overwritten|near init" } */
+  4, /* { dg-warning "initialized field overwritten|near init" } */
+  5
+};
+
+union u u0 = {
+  .a = 1,
+  .b = 2, /* { dg-warning "initialized field overwritten|near init" } */
+  .a = 3 }; /* { dg-warning "initialized field overwritten|near init" } */
+
+int a[5] = {
+  [0] = 1,
+  [1] = 2,
+  [0] = 3, /* { dg-warning "initialized field overwritten|near init" } */
+  [2] = 4
+};
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/Woverride-init-3.c GCC/gcc/testsuite/gcc.dg/Woverride-init-3.c
--- GCC.orig/gcc/testsuite/gcc.dg/Woverride-init-3.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/Woverride-init-3.c	2006-09-30 16:40:23.000000000 +0000
@@ -0,0 +1,29 @@
+/* Test for warnings for overriding designated initializers: not
+   warned for with -Wextra -Wno-override-init.  Bug 24010.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-Wextra -Wno-override-init" } */
+
+struct s { int a; int b; int c; };
+union u { char a; long long b; };
+
+struct s s0 = {
+  .a = 1,
+  .b = 2,
+  .a = 3,
+  4,
+  5
+};
+
+union u u0 = {
+  .a = 1,
+  .b = 2,
+  .a = 3
+};
+
+int a[5] = {
+  [0] = 1,
+  [1] = 2,
+  [0] = 3,
+  [2] = 4
+};


-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]