Bug 76732 - Improve Woverride-init
Summary: Improve Woverride-init
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 5.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 76733 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-08-15 04:40 UTC by rusty
Modified: 2017-11-30 15:37 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-08-15 00:00:00


Attachments
different testcase (374 bytes, text/plain)
2017-11-30 15:37 UTC, Rainer Orth
Details

Note You need to log in before you can comment on or make changes to this bug.
Description rusty 2016-08-15 04:40:07 UTC
Hit a nasty cut & paste bug in my code (extra comma before +3):

   int x[] = { [0] = 1, +3, [1] = 1 };

This double-initialization of x[1] should cause a warning.  Similarly:

   struct s { int a, b; } s = { .a = 1, .a = 2};

Thanks!
Comment 1 Andrew Pinski 2016-08-15 05:38:30 UTC
*** Bug 76733 has been marked as a duplicate of this bug. ***
Comment 2 Richard Biener 2016-08-15 07:57:05 UTC
Confirmed.
Comment 3 Manuel López-Ibáñez 2016-08-15 10:51:06 UTC
GCC already warns for this (for C)

test.c:1:32: warning: initialized field overwritten [-Woverride-init]
 int x[] = { [0] = 1, +3, [1] = 1 };
                                ^

test.c:1:32: note: (near initialization for ‘x[1]’)
test.c:2:43: warning: initialized field overwritten [-Woverride-init]
 struct s { int a, b; } s = { .a = 1, .a = 2};
                                           ^

test.c:2:43: note: (near initialization for ‘s.a’)

Although the output of Clang is nicer (and they also warn in C++):

prog.cc:1:32: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
int x[] = { [0] = 1, +3, [1] = 1 };  
                               ^
prog.cc:1:22: note: previous initialization is here
int x[] = { [0] = 1, +3, [1] = 1 };  
                     ^~
prog.cc:2:43: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
struct s { int a, b; } s = { .a = 1, .a = 2};
                                          ^
prog.cc:2:35: note: previous initialization is here
struct s { int a, b; } s = { .a = 1, .a = 2};
                                  ^

(And why does GCC have an empty line before the notes?)
Comment 4 Jakub Jelinek 2016-08-15 12:38:47 UTC
(In reply to Manuel López-Ibáñez from comment #3)
> (And why does GCC have an empty line before the notes?)

Can't reproduce that.
Comment 5 Manuel López-Ibáñez 2016-08-15 13:32:49 UTC
(In reply to Jakub Jelinek from comment #4)
> (In reply to Manuel López-Ibáñez from comment #3)
> > (And why does GCC have an empty line before the notes?)
> 
> Can't reproduce that.

Perhaps my build is outdated. I'm stuck with r230753 in the Compile Farm. I haven't managed to build a more recent GCC due to the OS being too old.
Comment 6 rusty 2016-08-17 02:00:21 UTC
"manu at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> writes:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=76732
>
> Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:
>
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                  CC|                            |manu at gcc dot gnu.org
>
> --- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
> GCC already warns for this (for C)
>
> test.c:1:32: warning: initialized field overwritten [-Woverride-init]
>  int x[] = { [0] = 1, +3, [1] = 1 };

Ah, thanks!

Surprised -Wall doesn't set -Woverride-init then; presume this was a
conscious decision?

I'll add it to my CFLAGS.

Thanks again!
Rusty.
Comment 7 Manuel López-Ibáñez 2016-08-17 02:32:54 UTC
> Surprised -Wall doesn't set -Woverride-init then; presume this was a
> conscious decision?

It is enabled by -Wextra. You can find the rationale here: PR24010

Clang warns by default.
Comment 8 Rainer Orth 2017-11-30 15:37:08 UTC
Created attachment 42755 [details]
different testcase

I've been pointed at the attached testcase.

With Oracle Studio cc, I get

ptr_a = hello
ptr_b = (null)
ptr_c = goodbye

while both gcc and clang produce

ptr_a = (null)
ptr_b = (null)
ptr_c = goodbye

It seems Studio cc is wrong according to C11 6.7.9 par. 19.  However clang warns
by default

union.c:40:3: warning: initializer overrides prior initialization of this
      subobject [-Winitializer-overrides]
        .ptr_c = "goodbye"
         ^~~~~
union.c:36:21: note: expanded from macro 'ptr_c'
#define ptr_c   ops_u.ops_v2.v2_c
                     ~^~~~~~
union.c:39:3: note: previous initialization is here
        .ptr_a = "hello",
         ^~~~~~~~~~~~~~~
union.c:34:27: note: expanded from macro 'ptr_a'
#define ptr_a   ops_u.ops_v1.v1_a
                            ^
1 warning generated.

even withou any additional options, which gcc 8.0.0 doesn't even with -Wall -Woverride-init.

Seems gcc should really follow here.