A right fix for the array in struct initialization w/o '{'

Parse a type if there is only one '(' before a type token.
Otherwise a recursion will perform a job.
This commit is contained in:
seyko 2015-03-23 08:27:16 +03:00
parent 367bb6f4b7
commit bd531ec1fd
4 changed files with 116 additions and 21 deletions

View File

@ -5546,33 +5546,26 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c,
s->c = array_length;
} else if ((type->t & VT_BTYPE) == VT_STRUCT &&
(sec || !first || tok == '{')) {
int par_count;
/* NOTE: the previous test is a specific case for automatic
struct/union init */
/* XXX: union needs only one init */
/* XXX: this test is incorrect for local initializers
beginning with ( without {. It would be much more difficult
to do it correctly (ideally, the expression parser should
be used in all cases) */
par_count = 0;
if (tok == '(') {
AttributeDef ad1;
CType type1;
next();
while (tok == '(') {
par_count++;
next();
}
if (!parse_btype(&type1, &ad1))
expect("cast");
type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
if (tok != '(') {
if (!parse_btype(&type1, &ad1))
expect("cast");
type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
#if 0
if (!is_assignable_types(type, &type1))
tcc_error("invalid type for cast");
if (!is_assignable_types(type, &type1))
tcc_error("invalid type for cast");
#endif
skip(')');
skip(')');
} else
unget_tok(tok);
}
no_oblock = 1;
if (first || tok == '{') {
@ -5646,10 +5639,6 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c,
}
if (!no_oblock)
skip('}');
while (par_count) {
skip(')');
par_count--;
}
} else if (tok == '{') {
next();
decl_initializer(type, sec, c, first, size_only);

View File

@ -0,0 +1,33 @@
#include <stdio.h>
/* This test is a snippet from the J interpreter */
typedef long I;
typedef struct{I c[4];I b,e,k;} PT;
PT cases[] = {
((I)4194304L +(I)2097152L +(I)67108864L), (I)262144L, (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), -1L, 1,2,1,
((I)+4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L, (I)262144L, (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), 2,3,2,
((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)262144L, (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), 1,3,2,
((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)524288L, -1L, 1,2,1,
((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)1048576L, (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), 1,3,1,
((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)262144L, (I)262144L, 1,3,1,
((I)4194304L +(I)2097152L +(I)67108864L), ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), -1L, 1,2,1,
(I)33554432L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)2097152L, ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), -1L, 0,2,1,
(I)67108864L, ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)134217728L, -1L, 0,2,0,
};
int main() {
int i, j;
for(j=0; j < sizeof(cases)/sizeof(cases[0]); j++) {
for(i=0; i < sizeof(cases->c)/sizeof(cases->c[0]); i++)
printf("cases[%d].c[%d]=%ld\n", j, i, cases[j].c[i]);
printf("cases[%d].b=%ld\n", j, cases[j].b);
printf("cases[%d].e=%ld\n", j, cases[j].e);
printf("cases[%d].k=%ld\n", j, cases[j].k);
printf("\n");
}
return 0;
}

View File

@ -0,0 +1,72 @@
cases[0].c[0]=73400320
cases[0].c[1]=262144
cases[0].c[2]=805567999
cases[0].c[3]=-1
cases[0].b=1
cases[0].e=2
cases[0].k=1
cases[1].c[0]=879754751
cases[1].c[1]=262144
cases[1].c[2]=262144
cases[1].c[3]=805567999
cases[1].b=2
cases[1].e=3
cases[1].k=2
cases[2].c[0]=879754751
cases[2].c[1]=805567999
cases[2].c[2]=262144
cases[2].c[3]=805567999
cases[2].b=1
cases[2].e=3
cases[2].k=2
cases[3].c[0]=879754751
cases[3].c[1]=805830143
cases[3].c[2]=524288
cases[3].c[3]=-1
cases[3].b=1
cases[3].e=2
cases[3].k=1
cases[4].c[0]=879754751
cases[4].c[1]=805830143
cases[4].c[2]=1048576
cases[4].c[3]=805830143
cases[4].b=1
cases[4].e=3
cases[4].k=1
cases[5].c[0]=879754751
cases[5].c[1]=805830143
cases[5].c[2]=262144
cases[5].c[3]=262144
cases[5].b=1
cases[5].e=3
cases[5].k=1
cases[6].c[0]=73400320
cases[6].c[1]=807403007
cases[6].c[2]=807403007
cases[6].c[3]=-1
cases[6].b=1
cases[6].e=2
cases[6].k=1
cases[7].c[0]=839122431
cases[7].c[1]=2097152
cases[7].c[2]=807403007
cases[7].c[3]=-1
cases[7].b=0
cases[7].e=2
cases[7].k=1
cases[8].c[0]=67108864
cases[8].c[1]=807403007
cases[8].c[2]=134217728
cases[8].c[3]=-1
cases[8].b=0
cases[8].e=2
cases[8].k=0

View File

@ -93,7 +93,8 @@ TESTS = \
71_macro_empty_arg.test \
72_long_long_constant.test \
73_arm64.test \
74_nocode_wanted.test
74_nocode_wanted.test \
75_array_in_struct_init.test
# 34_array_assignment.test -- array assignment is not in C standard