From b7ca74577b56190954eb6eeda8c22996b877f3c5 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 1 Aug 2016 22:55:07 +0200 Subject: [PATCH] struct-init: Allow member initialization from qualified lvalues See testcase. --- tccgen.c | 5 ++++- tests/tests2/86-struct-init.c | 28 +++++++++++++++++++++++++--- tests/tests2/86-struct-init.expect | 4 ++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tccgen.c b/tccgen.c index cb7e221b..a833b177 100644 --- a/tccgen.c +++ b/tccgen.c @@ -6012,7 +6012,10 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, if (have_elem && !(type->t & VT_ARRAY) && - is_compatible_types(type, &vtop->type)) { + /* Use i_c_parameter_t, to strip toplevel qualifiers. + The source type might have VT_CONSTANT set, which is + of course assignable to a non-const elements. */ + is_compatible_parameter_types(type, &vtop->type)) { init_putv(type, sec, c); } else if (type->t & VT_ARRAY) { s = type->ref; diff --git a/tests/tests2/86-struct-init.c b/tests/tests2/86-struct-init.c index 6749f984..6ce4e24b 100644 --- a/tests/tests2/86-struct-init.c +++ b/tests/tests2/86-struct-init.c @@ -96,6 +96,19 @@ struct Anon gan = { 10, 11 }; // ... which makes it available here. union UV2 guv4 = {{4,3}}; // and the other inits from above as well */ +struct in6_addr { + union { + u8 u6_addr8[16]; + unsigned short u6_addr16[8]; + } u; +}; +struct flowi6 { + struct in6_addr saddr, daddr; +}; +struct pkthdr { + struct in6_addr daddr, saddr; +}; +struct pkthdr phdr = { { { 6,5,4,3 } }, { { 9,8,7,6 } } }; #include void print_ (const char *name, const u8 *p, long size) { @@ -107,7 +120,7 @@ void print_ (const char *name, const u8 *p, long size) } #define print(x) print_(#x, (u8*)&x, sizeof (x)) #if 1 -void foo (struct W *w) +void foo (struct W *w, struct pkthdr *phdr_) { struct S ls = {1, 2, 3, 4}; struct S ls2 = {1, 2, {3, 4}}; @@ -115,6 +128,9 @@ void foo (struct W *w) struct U lu = {3, 5,6,7,8, 4, "huhu", 43}; struct U lu1 = {3, ls, 4, {"huhu", 43}}; struct U lu2 = {3, (ls), 4, {"huhu", 43}}; + const struct S *pls = &ls; + struct S ls21 = *pls; + struct U lu22 = {3, *pls, 4, {"huhu", 43}}; /* Incomplete bracing. */ struct U lu21 = {3, ls, 4, "huhu", 43}; /* Optional braces around scalar initializers. Accepted, but with @@ -129,20 +145,25 @@ void foo (struct W *w) /* Compund literal */ struct V lv2 = {(struct S)w->t.s, {"hihi", 47}, 48}; /* Parens around compound literal */ - struct V lv3 = {((struct S){7,8,{9,10}}), w->t.t, 50}; + struct V lv3 = {((struct S){7,8,{9,10}}), ((const struct W *)w)->t.t, 50}; + const struct pkthdr *phdr = phdr_; + struct flowi6 flow = { .daddr = phdr->daddr, .saddr = phdr->saddr }; print(ls); print(ls2); print(lt); print(lu); print(lu1); print(lu2); + print(ls21); print(lu21); + print(lu22); print(lu3); print(lu4); print(ls3); print(lv); print(lv2); print(lv3); + print(flow); } #endif @@ -167,7 +188,8 @@ int main() print(guv.b); print(guv2); print(guv3); - foo(&gw); + print(phdr); + foo(&gw, &phdr); //printf("q: %s\n", q); return 0; } diff --git a/tests/tests2/86-struct-init.expect b/tests/tests2/86-struct-init.expect index a5333dce..b827ec45 100644 --- a/tests/tests2/86-struct-init.expect +++ b/tests/tests2/86-struct-init.expect @@ -17,16 +17,20 @@ guv: 6 5 0 0 guv.b: 5 guv2: 8 7 0 0 guv3: 7 8 0 0 +phdr: 6 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0 9 8 7 6 0 0 0 0 0 0 0 0 0 0 0 0 ls: 1 2 3 4 ls2: 1 2 3 4 lt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a lu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b lu1: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b lu2: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +ls21: 1 2 3 4 lu21: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +lu22: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b lu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b lu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c ls3: 1 2 3 4 lv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e lv2: 1 2 3 4 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30 lv3: 7 8 9 a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 +flow: 9 8 7 6 0 0 0 0 0 0 0 0 0 0 0 0 6 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0