History

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.

seyko committed 11 years ago

Commit bd531ec1 parent 367bb6f4 files · +116 −21Browse files

Unified · Split

4 files
  1. tccgen.c +9 −20
  2. tests/tests2/75_array_in_struct_init.c +33 −0
  3. tests/tests2/75_array_in_struct_init.expect +72 −0
  4. tests/tests2/Makefile +2 −1
tccgen.c+9−20
@@ -5546,33 +5546,26 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 5545 unmodified lines
5546 s->c = array_length;5546 s->c = array_length;
5547 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&5547 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5548 (sec || !first || tok == '{')) {5548 (sec || !first || tok == '{')) {
5549 int par_count;
5550 5549
5551 /* NOTE: the previous test is a specific case for automatic5550 /* NOTE: the previous test is a specific case for automatic
5552 struct/union init */5551 struct/union init */
5553 /* XXX: union needs only one init */5552 /* XXX: union needs only one init */
5554 5553
5555 /* XXX: this test is incorrect for local initializers
5556 beginning with ( without {. It would be much more difficult
5557 to do it correctly (ideally, the expression parser should
5558 be used in all cases) */
5559 par_count = 0;
5560 if (tok == '(') {5554 if (tok == '(') {
5561 AttributeDef ad1;5555 AttributeDef ad1;
5562 CType type1;5556 CType type1;
5563 next();5557 next();
5564 while (tok == '(') {5558 if (tok != '(') {
5565 par_count++;5559 if (!parse_btype(&type1, &ad1))
5566 next();5560 expect("cast");
5567 }5561 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5568 if (!parse_btype(&type1, &ad1))
5569 expect("cast");
5570 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5571#if 05562#if 0
5572 if (!is_assignable_types(type, &type1))5563 if (!is_assignable_types(type, &type1))
5573 tcc_error("invalid type for cast");5564 tcc_error("invalid type for cast");
5574#endif5565#endif
5575 skip(')');5566 skip(')');
5567 } else
5568 unget_tok(tok);
5576 }5569 }
5577 no_oblock = 1;5570 no_oblock = 1;
5578 if (first || tok == '{') {5571 if (first || tok == '{') {
@@ -5646,10 +5639,6 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 67 unmodified lines
5646 }5639 }
5647 if (!no_oblock)5640 if (!no_oblock)
5648 skip('}');5641 skip('}');
5649 while (par_count) {
5650 skip(')');
5651 par_count--;
5652 }
5653 } else if (tok == '{') {5642 } else if (tok == '{') {
5654 next();5643 next();
5655 decl_initializer(type, sec, c, first, size_only);5644 decl_initializer(type, sec, c, first, size_only);
tests/tests2/75_array_in_struct_init.c+33−0
@@ -0,0 +1,33 @@
1#include <stdio.h>
2
3/* This test is a snippet from the J interpreter */
4
5typedef long I;
6typedef struct{I c[4];I b,e,k;} PT;
7
8PT cases[] = {
9 ((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,
10 ((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,
11 ((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,
12 ((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,
13 ((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,
14 ((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,
15 ((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,
16 (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,
17 (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,
18};
19
20int main() {
21 int i, j;
22
23 for(j=0; j < sizeof(cases)/sizeof(cases[0]); j++) {
24 for(i=0; i < sizeof(cases->c)/sizeof(cases->c[0]); i++)
25 printf("cases[%d].c[%d]=%ld\n", j, i, cases[j].c[i]);
26
27 printf("cases[%d].b=%ld\n", j, cases[j].b);
28 printf("cases[%d].e=%ld\n", j, cases[j].e);
29 printf("cases[%d].k=%ld\n", j, cases[j].k);
30 printf("\n");
31 }
32 return 0;
33}
tests/tests2/75_array_in_struct_init.expect+72−0
@@ -0,0 +1,72 @@
1cases[0].c[0]=73400320
2cases[0].c[1]=262144
3cases[0].c[2]=805567999
4cases[0].c[3]=-1
5cases[0].b=1
6cases[0].e=2
7cases[0].k=1
8
9cases[1].c[0]=879754751
10cases[1].c[1]=262144
11cases[1].c[2]=262144
12cases[1].c[3]=805567999
13cases[1].b=2
14cases[1].e=3
15cases[1].k=2
16
17cases[2].c[0]=879754751
18cases[2].c[1]=805567999
19cases[2].c[2]=262144
20cases[2].c[3]=805567999
21cases[2].b=1
22cases[2].e=3
23cases[2].k=2
24
25cases[3].c[0]=879754751
26cases[3].c[1]=805830143
27cases[3].c[2]=524288
28cases[3].c[3]=-1
29cases[3].b=1
30cases[3].e=2
31cases[3].k=1
32
33cases[4].c[0]=879754751
34cases[4].c[1]=805830143
35cases[4].c[2]=1048576
36cases[4].c[3]=805830143
37cases[4].b=1
38cases[4].e=3
39cases[4].k=1
40
41cases[5].c[0]=879754751
42cases[5].c[1]=805830143
43cases[5].c[2]=262144
44cases[5].c[3]=262144
45cases[5].b=1
46cases[5].e=3
47cases[5].k=1
48
49cases[6].c[0]=73400320
50cases[6].c[1]=807403007
51cases[6].c[2]=807403007
52cases[6].c[3]=-1
53cases[6].b=1
54cases[6].e=2
55cases[6].k=1
56
57cases[7].c[0]=839122431
58cases[7].c[1]=2097152
59cases[7].c[2]=807403007
60cases[7].c[3]=-1
61cases[7].b=0
62cases[7].e=2
63cases[7].k=1
64
65cases[8].c[0]=67108864
66cases[8].c[1]=807403007
67cases[8].c[2]=134217728
68cases[8].c[3]=-1
69cases[8].b=0
70cases[8].e=2
71cases[8].k=0
72
tests/tests2/Makefile+2−1
@@ -93,7 +93,8 @@ TESTS = \ · 92 unmodified lines
93 71_macro_empty_arg.test \93 71_macro_empty_arg.test \
94 72_long_long_constant.test \94 72_long_long_constant.test \
95 73_arm64.test \95 73_arm64.test \
96 74_nocode_wanted.test96 74_nocode_wanted.test \
97 75_array_in_struct_init.test
97 98
98 99
99# 34_array_assignment.test -- array assignment is not in C standard100# 34_array_assignment.test -- array assignment is not in C standard