From e034853b38907440142107eb689c5dd6c6621fca Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Wed, 13 Jul 2016 17:39:15 +0200 Subject: [PATCH] Fix parsing array typedefs of unknown size This must compile: typedef int arrtype1[]; arrtype1 sinit19 = {1}; arrtype1 sinit20 = {2,3}; and generate two arrays of one resp. two elements. Before the fix the determined size of the first array was encoded in the type directly, so sinit20 couldn't be parsed anymore (because arrtype1 was thought to be only one element long). --- tccgen.c | 8 ++++++++ tests/tcctest.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/tccgen.c b/tccgen.c index c006f719..6d72ab99 100644 --- a/tccgen.c +++ b/tccgen.c @@ -6657,6 +6657,14 @@ static int decl0(int l, int is_for_loop_init) } while (1) { /* iterate thru each declaration */ type = btype; + /* If the base type itself was an array type of unspecified + size (like in 'typedef int arr[]; arr x = {1};') then + we will overwrite the unknown size by the real one for + this decl. We need to unshare the ref symbol holding + that size. */ + if ((type.t & VT_ARRAY) && type.ref->c < 0) { + type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c); + } type_decl(&type, &ad, &v, TYPE_DIRECT); #if 0 { diff --git a/tests/tcctest.c b/tests/tcctest.c index 025d12d8..0b894aa9 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -1446,6 +1446,13 @@ struct complexinit2 cix22 = { .b = { 4001, 4002, 4003, 4004, 4005, 4006 } }; +typedef int arrtype1[]; +arrtype1 sinit19 = {1}; +arrtype1 sinit20 = {2,3}; +typedef int arrtype2[3]; +arrtype2 sinit21 = {4}; +arrtype2 sinit22 = {5,6,7}; + void init_test(void) { int linit1 = 2; @@ -1546,6 +1553,13 @@ void init_test(void) cix[0].b[2].a, cix[0].b[2].b); printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]); printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22); + + printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]); + printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20)); + printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]); + printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]); + printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22)); + printf("arrtype6: %d\n", sizeof(arrtype2)); }