From eaac467945c87292784a1773c911f649e340136a Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Fri, 6 Aug 2004 11:23:23 +0000 Subject: [PATCH] re PR c/13282 (A "-Wno..." switch to turn off "missing initializer" warnings) 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. From-SVN: r85638 --- gcc/ChangeLog | 12 +++++++ gcc/c-opts.c | 6 ++-- gcc/c-typeck.c | 2 +- gcc/c.opt | 4 +++ gcc/cp/ChangeLog | 5 +++ gcc/cp/typeck2.c | 4 +-- gcc/doc/invoke.texi | 35 ++++++++++++++----- gcc/testsuite/ChangeLog | 5 +++ .../g++.dg/warn/missing-field-init-1.C | 9 +++++ .../g++.dg/warn/missing-field-init-2.C | 9 +++++ gcc/testsuite/gcc.dg/missing-field-init-1.c | 10 ++++++ gcc/testsuite/gcc.dg/missing-field-init-2.c | 11 ++++++ 12 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/missing-field-init-1.C create mode 100644 gcc/testsuite/g++.dg/warn/missing-field-init-2.C create mode 100644 gcc/testsuite/gcc.dg/missing-field-init-1.c create mode 100644 gcc/testsuite/gcc.dg/missing-field-init-2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a6ac8d53c01..0c189c61c95 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,15 @@ +2004-08-06 Richard Sandiford + + 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. + 2004-08-06 Paolo Bonzini * expr.c (expand_expr_real_1) : Remove. diff --git a/gcc/c-opts.c b/gcc/c-opts.c index faa6023b1b9..92a6c7273a3 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -958,10 +958,12 @@ c_common_post_options (const char **pfilename) 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. */ diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 9f88d6fc05b..44b30123c80 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -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) diff --git a/gcc/c.opt b/gcc/c.opt index 2f85c8cb35f..b84c5ba8408 100644 --- a/gcc/c.opt +++ b/gcc/c.opt @@ -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 diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3edd2477ab9..be759c5840d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2004-08-06 Richard Sandiford + + * typeck2.c (process_init_constructor): Guard the missing field warning + with warn_missing_field_initializers rather than extra_warnings. + 2004-08-06 Paolo Bonzini * class.c (instantiate_type) : Do not handle. diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 3c018285fc1..87e05efa092 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -1022,7 +1022,7 @@ process_init_constructor (tree type, tree init, tree* elts) 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, tree init, tree* elts) /* 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); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 6332839e070..154dc976fb2 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -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 @@ incorrect result when the signed value is converted to unsigned. @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,30 @@ Do so even if the definition itself provides a prototype. Use this option to detect global functions that are not declared in header files. +@item -Wmissing-field-initializers +@opindex Wmissing-field-initializers +@opindex W +@opindex Wextra +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}. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 480bdcf24e1..87d1ddfc440 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2004-08-06 Richard Sandiford + + * gcc.dg/missing-field-init-[12].c: New tests. + * g++.dg/warn/missing-field-init-[12].C: New tests. + 2004-08-06 Mark Mitchell * gcc.dg/enum2.c: New test. diff --git a/gcc/testsuite/g++.dg/warn/missing-field-init-1.C b/gcc/testsuite/g++.dg/warn/missing-field-init-1.C new file mode 100644 index 00000000000..f28418f0c60 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/missing-field-init-1.C @@ -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 }; diff --git a/gcc/testsuite/g++.dg/warn/missing-field-init-2.C b/gcc/testsuite/g++.dg/warn/missing-field-init-2.C new file mode 100644 index 00000000000..7f6ec060f25 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/missing-field-init-2.C @@ -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 }; diff --git a/gcc/testsuite/gcc.dg/missing-field-init-1.c b/gcc/testsuite/gcc.dg/missing-field-init-1.c new file mode 100644 index 00000000000..5f32b021f0a --- /dev/null +++ b/gcc/testsuite/gcc.dg/missing-field-init-1.c @@ -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" } */ diff --git a/gcc/testsuite/gcc.dg/missing-field-init-2.c b/gcc/testsuite/gcc.dg/missing-field-init-2.c new file mode 100644 index 00000000000..581eb30d89b --- /dev/null +++ b/gcc/testsuite/gcc.dg/missing-field-init-2.c @@ -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" } */ -- 2.30.2