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]

Re: PR c/13282: Add a separate -Wmissing-field-initializer


"Joseph S. Myers" <jsm@polyomino.org.uk> writes:
> On Thu, 29 Jul 2004, Richard Sandiford wrote:
>> One of the warnings enabled by -Wextra is for struct initialisers that
>> have fewer fields than the struct itself.  PR c/13282 asks for a separate
>> switch to control the warning, much like we already have with -Wsign-compare.
>> 
>> Bootstrapped & regression tested on i686-pc-linux-gnu.  OK to install?
>
> This patch doesn't test (or document, though that's a pre-existing
> condition) the useful property that (at least for C) the use of designated
> initializers disables the warning for a particular set of braces.
>
>         /* Do not warn if this level of the initializer uses member
>            designators; it is likely to be deliberate.  */

OK, updated version attached.  Only the docs & C tests have changed,
so I rechecked the docs with "make info" and "make dvi" and verified
that the new C tests still pass.

OK to install?

Richard


	PR c/13282
	* c.opt (Wmissing-field-initializers): New option.
	* c-opts.c (c_common_post_options): Make -Wextra turn it on by default.
	* c-typeck.c (pop_init_level): Guard the missing field warning with
	warn_missing_field_initializers rather than extra_warnings.
	* doc/invoke.texi (-Wmissing-field-initializers): Document, moving
	some of the explanation from...
	(-Wextra): ...here.  Say that the missing field warning can be
	seperately controlled by -Wmissing-field-initializers.

cp/
	* typeck2.c (process_init_constructor): Guard the missing field warning
	with warn_missing_field_initializers rather than extra_warnings.

testsuite/
	* gcc.dg/missing-field-init-[12].c: New tests.
	* g++.dg/warn/missing-field-init-[12].C: New tests.

Index: c.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c.opt,v
retrieving revision 1.31
diff -u -p -F^\([(a-zA-Z0-9_]\|#define\) -r1.31 c.opt
--- c.opt	25 Jul 2004 22:52:11 -0000	1.31
+++ c.opt	29 Jul 2004 14:57:53 -0000
@@ -294,6 +294,10 @@ Wmissing-declarations
 C ObjC Var(warn_missing_declarations)
 Warn about global functions without previous declarations
 
+Wmissing-field-initializers
+C ObjC C++ ObjC++ Var(warn_missing_field_initializers) Init(-1)
+Warn about missing fields in struct initializers
+
 Wmissing-format-attribute
 C ObjC C++ ObjC++ Var(warn_missing_format_attribute)
 Warn about functions which might be candidates for format attributes
Index: c-opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-opts.c,v
retrieving revision 1.123
diff -u -p -F^\([(a-zA-Z0-9_]\|#define\) -r1.123 c-opts.c
--- c-opts.c	25 Jul 2004 22:52:10 -0000	1.123
+++ c-opts.c	29 Jul 2004 14:57:54 -0000
@@ -958,10 +958,12 @@ c_common_post_options (const char **pfil
   if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
     flag_exceptions = 1;
 
-  /* -Wextra implies -Wsign-compare, but not if explicitly
-      overridden.  */
+  /* -Wextra implies -Wsign-compare and -Wmissing-field-initializers,
+     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;
 
   /* Special format checking options don't work without -Wformat; warn if
      they are used.  */
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.353
diff -u -p -F^\([(a-zA-Z0-9_]\|#define\) -r1.353 c-typeck.c
--- c-typeck.c	26 Jul 2004 01:33:36 -0000	1.353
+++ c-typeck.c	29 Jul 2004 14:58:02 -0000
@@ -4763,7 +4763,7 @@ pop_init_level (int implicit)
     }
 
   /* Warn when some struct elements are implicitly initialized to zero.  */
-  if (extra_warnings
+  if (warn_missing_field_initializers
       && constructor_type
       && TREE_CODE (constructor_type) == RECORD_TYPE
       && constructor_unfilled_fields)
Index: cp/typeck2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck2.c,v
retrieving revision 1.167
diff -u -p -F^\([(a-zA-Z0-9_]\|#define\) -r1.167 typeck2.c
--- cp/typeck2.c	25 Jul 2004 17:19:39 -0000	1.167
+++ cp/typeck2.c	29 Jul 2004 14:58:04 -0000
@@ -1022,7 +1022,7 @@ process_init_constructor (tree type, tre
 	      next1 = digest_init (TREE_TYPE (field), next1, 0);
 
 	      /* Warn when some struct elements are implicitly initialized.  */
-	      if (extra_warnings
+	      if (warn_missing_field_initializers
 	          && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
 		warning ("missing initializer for member `%D'", field);
 	    }
@@ -1038,7 +1038,7 @@ process_init_constructor (tree type, tre
 
 	      /* Warn when some struct elements are implicitly initialized
 		 to zero.  */
-	      if (extra_warnings
+	      if (warn_missing_field_initializers
 	          && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
 		warning ("missing initializer for member `%D'", field);
 
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.494
diff -u -p -F^\([(a-zA-Z0-9_]\|#define\) -r1.494 invoke.texi
--- doc/invoke.texi	28 Jul 2004 23:57:26 -0000	1.494
+++ doc/invoke.texi	29 Jul 2004 14:58:19 -0000
@@ -222,7 +222,7 @@ in the following sections.
 -Wimport  -Wno-import  -Winit-self  -Winline @gol
 -Wno-invalid-offsetof  -Winvalid-pch @gol
 -Wlarger-than-@var{len}  -Wlong-long @gol
--Wmain  -Wmissing-braces @gol
+-Wmain  -Wmissing-braces  -Wmissing-field-initializers @gol
 -Wmissing-format-attribute  -Wmissing-include-dirs @gol
 -Wmissing-noreturn @gol
 -Wno-multichar  -Wnonnull  -Wpacked  -Wpadded @gol
@@ -2612,13 +2612,8 @@ (But don't warn if @option{-Wno-sign-com
 
 @item
 An aggregate has an initializer which does not initialize all members.
-For example, the following code would cause such a warning, because
-@code{x.h} would be implicitly initialized to zero:
-
-@smallexample
-struct s @{ int f, g, h; @};
-struct s x = @{ 3, 4 @};
-@end smallexample
+This warning can be independently controlled by
+@option{-Wmissing-field-initializers}.
 
 @item
 A function parameter is declared without a type specifier in K&R-style
@@ -2902,6 +2897,29 @@ Do so even if the definition itself prov
 Use this option to detect global functions that are not declared in
 header files.
 
+@item -Wmissing-field-initializers
+@opindex Wmissing-field-initializers
+@opindex W
+Warn if a structure's initializer has some fields missing.  For
+example, the following code would cause such a warning, because
+@code{x.h} is implicitly zero:
+
+@smallexample
+struct s @{ int f, g, h; @};
+struct s x = @{ 3, 4 @};
+@end smallexample
+
+This option does not warn about designated initializers, so the following
+modification would not trigger a warning:
+
+@smallexample
+struct s @{ int f, g, h; @};
+struct s x = @{ .f = 3, .g = 4 @};
+@end smallexample
+
+This warning is included in @option{-Wextra}.  To get other @option{-Wextra}
+warnings without this one, use @samp{-Wextra -Wno-missing-field-initializers}.
+
 @item -Wmissing-noreturn
 @opindex Wmissing-noreturn
 Warn about functions which might be candidates for attribute @code{noreturn}.
--- /dev/null	Fri Apr 23 00:21:55 2004
+++ testsuite/gcc.dg/missing-field-init-1.c	Thu Jul 29 15:55:47 2004
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-Wextra -Wno-missing-field-initializers -std=c99" } */
+
+struct s { int a, b, c; };
+struct s s1 = { 1, 2, 3 };
+struct s s2 = { 1, 2 }; /* { dg-bogus "missing initializer" } */
+struct s s3[] = { { 1, 2 }, { 4, 5 } }; /* { dg-bogus "missing initializer" } */
+struct s s4[] = { 1, 2, 3, 4, 5 }; /* { dg-bogus "missing initializer" } */
+struct s s5[] = { 1, 2, 3, 4, 5, 6 };
+struct s s6 = { .a = 1 }; /* { dg-bogus "missing initializer" } */
--- /dev/null	Fri Apr 23 00:21:55 2004
+++ testsuite/gcc.dg/missing-field-init-2.c	Thu Jul 29 15:55:56 2004
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-Wmissing-field-initializers -std=c99" } */
+
+struct s { int a, b, c; };
+struct s s1 = { 1, 2, 3 };
+struct s s2 = { 1, 2 }; /* { dg-warning "(missing initializer)|(near initialization)" } */
+struct s s3[] = { { 1, 2 }, { 4, 5 } }; /* { dg-warning "(missing initializer)|(near initialization)" } */
+struct s s4[] = { 1, 2, 3, 4, 5 }; /* { dg-warning "(missing initializer)|(near initialization)" } */
+struct s s5[] = { 1, 2, 3, 4, 5, 6 };
+/* Designated initializers produce no warning.  */
+struct s s6 = { .a = 1 }; /* { dg-bogus "missing initializer" } */
--- /dev/null	Fri Apr 23 00:21:55 2004
+++ testsuite/g++.dg/warn/missing-field-init-1.C	Thu Jul 29 12:46:51 2004
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-Wextra -Wno-missing-field-initializers" } */
+
+struct s { int a, b, c; };
+struct s s1 = { 1, 2, 3 };
+struct s s2 = { 1, 2 }; /* { dg-bogus "missing initializer" } */
+struct s s3[] = { { 1, 2 }, { 4, 5 } }; /* { dg-bogus "missing initializer" } */
+struct s s4[] = { 1, 2, 3, 4, 5 }; /* { dg-bogus "missing initializer" } */
+struct s s5[] = { 1, 2, 3, 4, 5, 6 };
--- /dev/null	Fri Apr 23 00:21:55 2004
+++ testsuite/g++.dg/warn/missing-field-init-2.C	Thu Jul 29 12:12:56 2004
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-Wmissing-field-initializers" } */
+
+struct s { int a, b, c; };
+struct s s1 = { 1, 2, 3 };
+struct s s2 = { 1, 2 }; /* { dg-warning "(missing initializer)|(near initialization)" } */
+struct s s3[] = { { 1, 2 }, { 4, 5 } }; /* { dg-warning "(missing initializer)|(near initialization)" } */
+struct s s4[] = { 1, 2, 3, 4, 5 }; /* { dg-warning "(missing initializer)|(near initialization)" } */
+struct s s5[] = { 1, 2, 3, 4, 5, 6 };


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