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!
*** Bug 76733 has been marked as a duplicate of this bug. ***
Confirmed.
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?)
(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.
(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.
"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.
> 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.
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.