From 755e528f969e0f9ef9061479e0ed2b13bcf0f9f2 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Thu, 7 May 2015 22:14:27 +0000 Subject: [PATCH] re PR c/64918 (invalid (?) warning when initializing structure) PR c/64918 * c.opt (Woverride-init-side-effects): New option. * c-typeck.c (add_pending_init): Use OPT_Woverride_init_side_effects. (output_init_element): Likewise. * doc/invoke.texi: Document -Woverride-init-side-effects. * gcc.dg/Woverride-init-side-effects-1.c: New test. * gcc.dg/Woverride-init-side-effects-2.c: New test. From-SVN: r222894 --- gcc/ChangeLog | 5 ++++ gcc/c-family/ChangeLog | 5 ++++ gcc/c-family/c.opt | 4 +++ gcc/c/ChangeLog | 6 +++++ gcc/c/c-typeck.c | 6 ++--- gcc/doc/invoke.texi | 8 ++++++ gcc/testsuite/ChangeLog | 6 +++++ .../gcc.dg/Woverride-init-side-effects-1.c | 25 +++++++++++++++++++ .../gcc.dg/Woverride-init-side-effects-2.c | 25 +++++++++++++++++++ 9 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Woverride-init-side-effects-1.c create mode 100644 gcc/testsuite/gcc.dg/Woverride-init-side-effects-2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f1c685aa8d8..d38918de3a7 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2015-05-08 Marek Polacek + + PR c/64918 + * doc/invoke.texi: Document -Woverride-init-side-effects. + 2015-05-07 Marek Polacek PR c/65179 diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 6f141215c29..57f83c97700 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2015-05-08 Marek Polacek + + PR c/64918 + * c.opt (Woverride-init-side-effects): New option. + 2015-05-07 Marek Polacek PR c/65179 diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 48947b4f2b4..3774a89088b 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -709,6 +709,10 @@ Woverride-init C ObjC Var(warn_override_init) Warning EnabledBy(Wextra) Warn about overriding initializers without side effects +Woverride-init-side-effects +C ObjC Var(warn_override_init_side_effects) Init(1) Warning +Warn about overriding initializers with side effects + Wpacked-bitfield-compat C ObjC C++ ObjC++ Var(warn_packed_bitfield_compat) Init(-1) Warning Warn about packed bit-fields whose offset changed in GCC 4.4 diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index ccd1e7291cf..3b8f491ff84 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2015-05-08 Marek Polacek + + PR c/64918 + * c-typeck.c (add_pending_init): Use OPT_Woverride_init_side_effects. + (output_init_element): Likewise. + 2015-05-07 Marek Polacek PR c/65179 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 73275aa106b..3fcb7c2c5e7 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -7968,7 +7968,7 @@ add_pending_init (location_t loc, tree purpose, tree value, tree origtype, if (!implicit) { if (TREE_SIDE_EFFECTS (p->value)) - warning_init (loc, 0, + warning_init (loc, OPT_Woverride_init_side_effects, "initialized field with side-effects " "overwritten"); else if (warn_override_init) @@ -7998,7 +7998,7 @@ add_pending_init (location_t loc, tree purpose, tree value, tree origtype, if (!implicit) { if (TREE_SIDE_EFFECTS (p->value)) - warning_init (loc, 0, + warning_init (loc, OPT_Woverride_init_side_effects, "initialized field with side-effects " "overwritten"); else if (warn_override_init) @@ -8530,7 +8530,7 @@ output_init_element (location_t loc, tree value, tree origtype, if (!implicit) { if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) - warning_init (loc, 0, + warning_init (loc, OPT_Woverride_init_side_effects, "initialized field with side-effects overwritten"); else if (warn_override_init) warning_init (loc, OPT_Woverride_init, diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 934edd6efae..cb5ae17311b 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -266,6 +266,7 @@ Objective-C and Objective-C++ Dialects}. -Wmissing-field-initializers -Wmissing-include-dirs @gol -Wno-multichar -Wnonnull -Wnormalized=@r{[}none@r{|}id@r{|}nfc@r{|}nfkc@r{]} @gol -Wodr -Wno-overflow -Wopenmp-simd @gol +-Woverride-init-side-effects @gol -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded @gol -Wparentheses -Wpedantic-ms-format -Wno-pedantic-ms-format @gol -Wpointer-arith -Wno-pointer-to-int-cast @gol @@ -5197,6 +5198,13 @@ This warning is included in @option{-Wextra}. To get other @option{-Wextra} warnings without this one, use @option{-Wextra -Wno-override-init}. +@item -Woverride-init-side-effects @r{(C and Objective-C only)} +@opindex Woverride-init-side-effects +@opindex Wno-override-init-side-effects +Warn if an initialized field with side effects is overridden when +using designated initializers (@pxref{Designated Inits, , Designated +Initializers}). This warning is enabled by default. + @item -Wpacked @opindex Wpacked @opindex Wno-packed diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 4ffc42857ef..79b2aa262f2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2015-05-08 Marek Polacek + + PR c/64918 + * gcc.dg/Woverride-init-side-effects-1.c: New test. + * gcc.dg/Woverride-init-side-effects-2.c: New test. + 2015-05-07 Marek Polacek PR c/65179 diff --git a/gcc/testsuite/gcc.dg/Woverride-init-side-effects-1.c b/gcc/testsuite/gcc.dg/Woverride-init-side-effects-1.c new file mode 100644 index 00000000000..50f198d7fa0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Woverride-init-side-effects-1.c @@ -0,0 +1,25 @@ +/* PR c/64918 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +struct S { int m, n; }; +union U { short s; long int l; }; + +void +foo (int i) +{ + int a[] = { + [0] = ++i, + [1] = i, + [0] = 42 /* { dg-warning "initialized field with side-effects overwritten" } */ + }; + struct S s = { + .n = ++i, + .m = i, + .n = i /* { dg-warning "initialized field with side-effects overwritten" } */ + }; + union U u = { + .s = i--, + .l = 42 /* { dg-warning "initialized field with side-effects overwritten" } */ + }; +} diff --git a/gcc/testsuite/gcc.dg/Woverride-init-side-effects-2.c b/gcc/testsuite/gcc.dg/Woverride-init-side-effects-2.c new file mode 100644 index 00000000000..5927189f00b --- /dev/null +++ b/gcc/testsuite/gcc.dg/Woverride-init-side-effects-2.c @@ -0,0 +1,25 @@ +/* PR c/64918 */ +/* { dg-do compile } */ +/* { dg-options "-Wno-override-init-side-effects" } */ + +struct S { int m, n; }; +union U { short s; long int l; }; + +void +foo (int i) +{ + int a[] = { + [0] = ++i, + [1] = i, + [0] = 42 + }; + struct S s = { + .n = ++i, + .m = i, + .n = i + }; + union U u = { + .s = i--, + .l = 42 + }; +} -- 2.30.2