Created attachment 31865 [details] Sparse test case for __attribute__((designated_init)) The Sparse static analyzer provides an extension to force designated initialization of a structure, __attribute__((designated_init)). Given a structure declared with this attribute, Sparse warns if any instance of that structure gets initialized using a positional initializer, rather than a designated initializer. This is useful for structure types likely to change layout or gain additional fields in the future, to make initializers of the structure more resilient. It would help greatly if GCC could support this attribute as well, to make the warnings more generally available and visible to developers who aren't using sparse at compilation time. I've attached Sparse's test case for designated_init, which provides comprehensive coverage of cases that should and shouldn't generate warnings.
Patch here: http://gcc.gnu.org/ml/gcc-patches/2014-01/msg01284.html
Very nice! Looks reasonable to me. I agree with making designated_init on a non-struct an error. Sparse tends to almost never mark anything as an error unless it mangles Sparse's internal state somehow, because errors will suppress subsequent warnings (to avoid cascading issues). GCC should make it an error. In theory, designated_init could be meaningful on a union or array; for the union it would warn on the un-designated initialization of the first member of the union, and for an array it could require numeric designation. However, the latter case seems highly unlikely, and for the former there probably ought to be a general warning -Wpositional-union-init. I'd suggest going with this patch.
Author: tromey Date: Wed Jul 30 15:02:59 2014 New Revision: 213293 URL: https://gcc.gnu.org/viewcvs?rev=213293&root=gcc&view=rev Log: 2014-07-30 Tom Tromey <tromey@redhat.com> PR c/59855 * doc/invoke.texi (Warning Options): Document -Wdesignated-init. * doc/extend.texi (Type Attributes): Document designated_init attribute. 2014-07-30 Tom Tromey <tromey@redhat.com> PR c/59855 * c.opt (Wdesignated-init): New option. * c-common.c (c_common_attribute_table): Add "designated_init". (handle_designated_init): New function. 2014-07-30 Tom Tromey <tromey@redhat.com> * c-typeck.c (struct constructor_stack) <designator_depth>: New field. (really_start_incremental_init, push_init_level): Initialize designator_depth. (pop_init_level): Set global designator_depth. (process_init_element): Check for designated_init attribute. Added: trunk/gcc/testsuite/gcc.dg/Wdesignated-init.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-family/ChangeLog trunk/gcc/c-family/c-common.c trunk/gcc/c-family/c.opt trunk/gcc/c/ChangeLog trunk/gcc/c/c-typeck.c trunk/gcc/doc/extend.texi trunk/gcc/doc/invoke.texi trunk/gcc/testsuite/ChangeLog
Implemented on trunk.
(In reply to Tom Tromey from comment #4) > Implemented on trunk. Thanks! Please consider including anonymous structs and unions in the test case as well: struct S { int a; union { int b; int c; }; } __attribute__((designated_init)); static struct S s = { .a = 0, .b = 0, }; union U { int a; struct { int b; int c; } __attribute__((designated_init)); }; static union U u = { .b = 0, .c = 0, }; Both of the cases above should produce no warnings; analogous positional initializers should produce warnings.
Tom: I can add the testcase(s) above if you want.
Author: mpolacek Date: Sat Aug 2 05:52:30 2014 New Revision: 213515 URL: https://gcc.gnu.org/viewcvs?rev=213515&root=gcc&view=rev Log: PR c/59855 * gcc.dg/Wdesignated-init-2.c: New test. Added: trunk/gcc/testsuite/gcc.dg/Wdesignated-init-2.c Modified: trunk/gcc/testsuite/ChangeLog