From 9e86ebee944e7fc480fe49bf4f4f406aab672200 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 1 Aug 2016 22:11:49 +0200 Subject: struct-init: Correctly parse unnamed member initializers For union U { struct {int a,b}; int c; }; union U u = {{ 1, 2, }}; The unnamed first member of union U needs to actually exist in the structure so initializer parsing isn't confused about the double braces. That means also the a and b members must be part of _that_, not of union U directly. Which in turn means we need to do a bit more work for field lookup. See the testcase extension for more things that need to work. --- tests/tests2/86-struct-init.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests/tests2/86-struct-init.c') diff --git a/tests/tests2/86-struct-init.c b/tests/tests2/86-struct-init.c index fa0d967..6749f98 100644 --- a/tests/tests2/86-struct-init.c +++ b/tests/tests2/86-struct-init.c @@ -77,6 +77,25 @@ struct SU { }; struct SU gsu = {5,6}; +/* Unnamed struct/union members aren't ISO C, but it's a widely accepted + extension. See below for further extensions to that under -fms-extension.*/ +union UV { + struct {u8 a,b;}; + struct S s; +}; +union UV guv = {{6,5}}; +union UV guv2 = {{.b = 7, .a = 8}}; +union UV guv3 = {.b = 8, .a = 7}; + +/* Under -fms-extensions also the following is valid: +union UV2 { + struct Anon {u8 a,b;}; // unnamed member, but tagged struct, ... + struct S s; +}; +struct Anon gan = { 10, 11 }; // ... which makes it available here. +union UV2 guv4 = {{4,3}}; // and the other inits from above as well +*/ + #include void print_ (const char *name, const u8 *p, long size) { @@ -144,6 +163,10 @@ int main() print(sinit16); print(gw); print(gsu); + print(guv); + print(guv.b); + print(guv2); + print(guv3); foo(&gw); //printf("q: %s\n", q); return 0; -- cgit v1.3.1