> On Tue, Jan 20, 2015 at 11:14 AM, Jonathan Gray <[email protected]> wrote: > So it seems at some point after gcc 4.4 --std=c89 and --std=c99 > starting allowing anonymous unions/structs when this behaviour > was otherwise only accepted with --std=gnu99, --std=c99 -fms-extensions > or omitting a --std option entirely.
The gcc docs say that "When a base standard is specified, the compiler accepts all programs following that standard *plus* those using GNU extensions that do not contradict it." (see https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html). So I guess someone decided after gcc 4.4 that anonymous structs/unions don't conflict with the c99 standard. The concept seems ok to me after finding that blurb although I haven't had a chance to test the patch yet. > clang also has the same behaviour as recent gcc. > > struct foo { > union { > int a; > }; > }; > > int > main(void) > { > struct foo f; > f.a = 0; > return (0); > } > > gcc 4.2.1, egcc 4.8.3, clang 3.4.2 > > $ gcc --std=c99 t.c > t.c:4: warning: declaration does not declare anything > t.c: In function 'main': > t.c:11: error: 'struct foo' has no member named 'a' > $ gcc --std=gnu99 t.c > $ gcc --std=c99 -fms-extensions t.c > $ egcc --std=c89 t.c > $ egcc --std=c99 t.c > $ clang --std=c89 t.c > $ clang --std=c99 t.c > $ clang --std=c99 -pedantic t.c > t.c:2:2: warning: anonymous unions are a C11 extension [-Wc11-extensions] > union { > ^ > 1 warning generated. > $ egcc --std=c99 -pedantic t.c > t.c:4:3: warning: ISO C99 doesn't support unnamed structs/unions [-Wpedantic] > }; > ^ > t.c:1:8: warning: struct has no named members [-Wpedantic] > struct foo { > ^ > > I'd like to change the defaults for the in tree versions > of gcc to match to avoid problems like those mentioned here: > https://bugs.freedesktop.org/show_bug.cgi?id=88467 > > Index: gcc/gcc/c-decl.c > =================================================================== > RCS file: /cvs/src/gnu/gcc/gcc/c-decl.c,v > retrieving revision 1.2 > diff -u -p -r1.2 c-decl.c > --- gcc/gcc/c-decl.c 29 Apr 2010 18:37:37 -0000 1.2 > +++ gcc/gcc/c-decl.c 20 Jan 2015 15:35:34 -0000 > @@ -5350,7 +5350,7 @@ grokfield (struct c_declarator *declarat > if (flag_ms_extensions) > ok = true; > else if (flag_iso) > - ok = false; > + ok = true; > else if (TYPE_NAME (type) == NULL) > ok = true; > else > Index: usr.bin/gcc/gcc/c-decl.c > =================================================================== > RCS file: /cvs/src/gnu/usr.bin/gcc/gcc/c-decl.c,v > retrieving revision 1.1.1.2 > diff -u -p -r1.1.1.2 c-decl.c > --- usr.bin/gcc/gcc/c-decl.c 24 Dec 2004 23:51:28 -0000 1.1.1.2 > +++ usr.bin/gcc/gcc/c-decl.c 20 Jan 2015 15:36:41 -0000 > @@ -5093,7 +5093,7 @@ grokfield (filename, line, declarator, d > if (flag_ms_extensions) > ; /* ok */ > else if (flag_iso) > - goto warn_unnamed_field; > + ; /* ok */ > else if (TYPE_NAME (type) == NULL) > ; /* ok */ > else >
