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).
master
Michael Matz 2016-07-13 17:39:15 +02:00
parent b7e0b693a6
commit e034853b38
2 changed files with 22 additions and 0 deletions

View File

@ -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
{

View File

@ -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));
}