diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 11 | ||||
-rw-r--r-- | gcc/c-opts.c | 6 | ||||
-rw-r--r-- | gcc/c-typeck.c | 6 | ||||
-rw-r--r-- | gcc/c.opt | 4 | ||||
-rw-r--r-- | gcc/doc/invoke.texi | 18 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Woverride-init-1.c | 28 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Woverride-init-2.c | 28 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Woverride-init-3.c | 29 |
9 files changed, 134 insertions, 2 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 7a0e026c281..e7fc16ffab9 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +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. + 2006-09-30 Eric Botcazou <ebotcazou@libertysurf.fr> PR rtl-optimization/28096 diff --git a/gcc/c-opts.c b/gcc/c-opts.c index 5821deabb96..b74ce773088 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -1016,12 +1016,14 @@ c_common_post_options (const char **pfilename) 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 --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 00537ac3f4c..89bfed1db64 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -5737,6 +5737,8 @@ add_pending_init (tree purpose, tree value) { 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 value) { 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 strict_string, tree type, tree field, 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 --git a/gcc/c.opt b/gcc/c.opt index 12510d9697f..0aa0a3ad30c 100644 --- a/gcc/c.opt +++ b/gcc/c.opt @@ -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 --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index d5a3b6bac6b..6d7bc1e1eec 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -2876,6 +2876,12 @@ This warning can be independently controlled by @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 --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c5c774a6670..51599a7c544 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +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. + 2006-09-30 Eric Botcazou <ebotcazou@libertysurf.fr> * gcc.c-torture/execute/20060930-1.c: New test. diff --git a/gcc/testsuite/gcc.dg/Woverride-init-1.c b/gcc/testsuite/gcc.dg/Woverride-init-1.c new file mode 100644 index 00000000000..29eca3095cd --- /dev/null +++ b/gcc/testsuite/gcc.dg/Woverride-init-1.c @@ -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 --git a/gcc/testsuite/gcc.dg/Woverride-init-2.c b/gcc/testsuite/gcc.dg/Woverride-init-2.c new file mode 100644 index 00000000000..c5490b5ad35 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Woverride-init-2.c @@ -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 --git a/gcc/testsuite/gcc.dg/Woverride-init-3.c b/gcc/testsuite/gcc.dg/Woverride-init-3.c new file mode 100644 index 00000000000..3fec7385d04 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Woverride-init-3.c @@ -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 +}; |