This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Patch to add -Wmissing-field-initializers
- From: Richard Sandiford <rsandifo at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: 03 May 2002 18:32:50 +0100
- Subject: Patch to add -Wmissing-field-initializers
This patch just gives a name to one of the -W warnings, so that you can
turn it on and off individually. rth approved it some time ago, but
I'll leave a few days for objections before committing.
Bootstrapped & regression tested on i686-pc-linux-gnu.
Richard
* toplev.c (documented_lang_options): Add -Wmissing-field-initializers
and -Wno-missing-field-initializers.
* c-tree.h (warn_missing_field_initializers): Declare.
* c-decl.c (warn_missing_field_initializers): Define.
(c_decode_option): Check for the new options.
* c-typeck.c (pop_init_level): Make the missing initializer warning
depend on the new options as well as -W.
* doc/invoke.texi: Document the new options.
[testsuite/]
* gcc-dg/missing-field-init-1.c.
* gcc-dg/missing-field-init-2.c: New tests.
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.619
diff -c -d -p -r1.619 toplev.c
*** toplev.c 2 May 2002 17:51:48 -0000 1.619
--- toplev.c 3 May 2002 09:40:15 -0000
*************** documented_lang_options[] =
*** 1281,1286 ****
--- 1281,1289 ----
{ "-Wmissing-declarations",
N_("Warn about global funcs without previous declarations") },
{ "-Wno-missing-declarations", "" },
+ { "-Wmissing-field-initializers",
+ N_("Warn about missing fields in struct initializers") },
+ { "-Wno-missing-field-initializers", "" },
{ "-Wmissing-prototypes",
N_("Warn about global funcs without prototypes") },
{ "-Wno-missing-prototypes", "" },
Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.133
diff -c -d -p -r1.133 c-common.h
*** c-common.h 27 Apr 2002 06:53:05 -0000 1.133
--- c-common.h 3 May 2002 09:40:15 -0000
*************** extern int warn_missing_format_attribute
*** 424,429 ****
--- 424,436 ----
extern int warn_pointer_arith;
+ /* Controls whether we warn about missing fields when initializing a
+ structure. The value is -1 for -Wno-missing-field-initializers, 1
+ for -Wmissing-field-initializers, or 0 for the default (which is to
+ warn when -W is passed). */
+
+ extern int warn_missing_field_initializers;
+
/* Nonzero means to warn about compile-time division by zero. */
extern int warn_div_by_zero;
Index: c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.322
diff -c -d -p -r1.322 c-decl.c
*** c-decl.c 28 Apr 2002 18:31:28 -0000 1.322
--- c-decl.c 3 May 2002 09:40:15 -0000
*************** int warn_missing_prototypes;
*** 380,385 ****
--- 380,392 ----
int warn_missing_declarations;
+ /* Controls whether we warn about missing fields when initializing a
+ structure. The value is -1 for -Wno-missing-field-initializers, 1
+ for -Wmissing-field-initializers, or 0 for the default (which is to
+ warn when -W is passed). */
+
+ int warn_missing_field_initializers;
+
/* Nonzero means warn about multiple (redundant) decls for the same single
variable or function. */
*************** c_decode_option (argc, argv)
*** 488,493 ****
--- 495,501 ----
{ "main", &warn_main },
{ "missing-braces", &warn_missing_braces },
{ "missing-declarations", &warn_missing_declarations },
+ { "missing-field-initializers", &warn_missing_field_initializers },
{ "missing-format-attribute", &warn_missing_format_attribute },
{ "missing-prototypes", &warn_missing_prototypes },
{ "multichar", &warn_multichar },
*************** c_decode_option (argc, argv)
*** 658,663 ****
--- 666,673 ----
warn_implicit_int = 0, mesg_implicit_function_declaration = 0;
else if (!strcmp (p, "-Wno-main"))
warn_main = -1;
+ else if (!strcmp (p, "-Wno-missing-field-initializers"))
+ warn_missing_field_initializers = -1;
else if (!strcmp (p, "-Wunknown-pragmas"))
/* Set to greater than 1, so that even unknown pragmas in system
headers will be warned about. */
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.194
diff -c -d -p -r1.194 c-typeck.c
*** c-typeck.c 26 Apr 2002 21:56:55 -0000 1.194
--- c-typeck.c 3 May 2002 09:40:16 -0000
*************** pop_init_level (implicit)
*** 5486,5492 ****
}
/* Warn when some struct elements are implicitly initialized to zero. */
! if (extra_warnings
&& constructor_type
&& TREE_CODE (constructor_type) == RECORD_TYPE
&& constructor_unfilled_fields)
--- 5486,5492 ----
}
/* Warn when some struct elements are implicitly initialized to zero. */
! if (warn_missing_field_initializers >= (extra_warnings? 0 : 1)
&& constructor_type
&& TREE_CODE (constructor_type) == RECORD_TYPE
&& constructor_unfilled_fields)
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.141
diff -c -d -p -r1.141 invoke.texi
*** doc/invoke.texi 30 Apr 2002 16:47:41 -0000 1.141
--- doc/invoke.texi 3 May 2002 09:40:17 -0000
*************** in the following sections.
*** 223,228 ****
--- 223,229 ----
-Wimport -Winline -Wno-endif-labels @gol
-Wlarger-than-@var{len} -Wlong-long @gol
-Wmain -Wmissing-braces -Wmissing-declarations @gol
+ -Wmissing-field-initializers @gol
-Wmissing-format-attribute -Wmissing-noreturn @gol
-Wno-multichar -Wno-format-extra-args -Wno-format-y2k @gol
-Wno-import -Wpacked -Wpadded @gol
*************** struct t x = @{ 1, 2, 3 @};
*** 2267,2279 ****
@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
@end itemize
@item -Wno-div-by-zero
--- 2268,2276 ----
@item
An aggregate has an initializer which does not initialize all members.
! You can control this warning separately using
! @option{-Wmissing-field-initializers} and
! @option{-Wno-missing-field-initializers}.
@end itemize
@item -Wno-div-by-zero
*************** be taken to manually verify functions ac
*** 2503,2508 ****
--- 2500,2520 ----
adding the @code{noreturn} attribute, otherwise subtle code generation
bugs could be introduced. You will not get a warning for @code{main} in
hosted C environments.
+
+ @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 warning is included in @option{-W}. To get other @option{-W}
+ warnings without this one, use @samp{-W -Wno-missing-field-initializers}.
@item -Wmissing-format-attribute
@opindex Wmissing-format-attribute
*** /dev/null Tue Nov 14 21:44:43 2000
--- testsuite/gcc.dg/missing-field-init-1.c Tue Apr 23 11:26:59 2002
***************
*** 0 ****
--- 1,9 ----
+ /* { dg-do compile } */
+ /* { dg-options "-W -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 Tue Nov 14 21:44:43 2000
--- testsuite/gcc.dg/missing-field-init-2.c Tue Apr 23 11:26:59 2002
***************
*** 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 };