Bug 59855 - Support sparse-style __attribute__((designated_init)) on structures, requiring designated initializers
Summary: Support sparse-style __attribute__((designated_init)) on structures, requirin...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: sparse
  Show dependency treegraph
 
Reported: 2014-01-17 08:37 UTC by Josh Triplett
Modified: 2022-09-23 12:36 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Sparse test case for __attribute__((designated_init)) (719 bytes, text/x-csrc)
2014-01-17 08:37 UTC, Josh Triplett
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Josh Triplett 2014-01-17 08:37:02 UTC
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.
Comment 1 Tom Tromey 2014-01-20 20:43:32 UTC
Patch here: http://gcc.gnu.org/ml/gcc-patches/2014-01/msg01284.html
Comment 2 Josh Triplett 2014-01-20 23:01:27 UTC
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.
Comment 3 Tom Tromey 2014-07-30 15:03:34 UTC
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
Comment 4 Tom Tromey 2014-07-30 15:06:22 UTC
Implemented on trunk.
Comment 5 Josh Triplett 2014-07-31 18:08:30 UTC
(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.
Comment 6 Marek Polacek 2014-07-31 18:12:05 UTC
Tom: I can add the testcase(s) above if you want.
Comment 7 Marek Polacek 2014-08-02 05:53:03 UTC
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