Skip to content

Commit e6ea0d0

Browse files
committed
tccgen: fix several problems
Fix 'void func2(char *(*md)(char *md))' declaration. Fix global array and local extern array problems. Fix scope problem with old function declaration. Fix 'typedef int t[]' declaration. Empty size should remain.
1 parent 8c59fd3 commit e6ea0d0

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

tccgen.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ static void patch_type(Sym *sym, CType *type)
12631263
} else {
12641264
if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) {
12651265
/* set array size if it was omitted in extern declaration */
1266-
sym->type.ref = type->ref;
1266+
sym->type.ref->c = type->ref->c;
12671267
}
12681268
if ((type->t ^ sym->type.t) & VT_STATIC)
12691269
tcc_warning("storage mismatch for redefinition of '%s'",
@@ -4890,6 +4890,8 @@ static int parse_btype(CType *type, AttributeDef *ad, int ignore_label)
48904890
sym_to_attr(ad, s);
48914891
typespec_found = 1;
48924892
st = bt = -2;
4893+
if (type->ref && (t & VT_ARRAY) && type->ref->c < 0)
4894+
type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
48934895
break;
48944896
}
48954897
type_found = 1;
@@ -4992,15 +4994,15 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td)
49924994
type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT | TYPE_PARAM);
49934995
if ((pt.t & VT_BTYPE) == VT_VOID)
49944996
tcc_error("parameter declared as void");
4995-
if (n == 0)
4997+
if (local_scope > 1 || n == 0)
49964998
n = SYM_FIELD;
49974999
} else {
49985000
n = tok;
49995001
pt.t = VT_INT | VT_EXTERN; /* default type */
50005002
pt.ref = NULL;
50015003
next();
50025004
}
5003-
if (n < TOK_UIDENT)
5005+
if (local_scope == 1 && n < TOK_UIDENT)
50045006
expect("identifier");
50055007
convert_parameter_type(&pt);
50065008
arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
@@ -8685,7 +8687,9 @@ static int decl(int l)
86858687
sym = type.ref;
86868688
if (sym->f.func_type == FUNC_OLD && l == VT_CONST) {
86878689
func_vt = type;
8690+
++local_scope;
86888691
decl(VT_CMP);
8692+
--local_scope;
86898693
}
86908694
if ((type.t & (VT_EXTERN|VT_INLINE)) == (VT_EXTERN|VT_INLINE)) {
86918695
/* always_inline functions must be handled as if they

tests/tests2/129_scopes.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,48 @@ struct st func(void)
7676
return st;
7777
}
7878

79+
/* --------------------------------------------- */
80+
static void func2(char *(*md)(char *md))
81+
{
82+
(*md)("test");
83+
}
84+
85+
static char *a(char *a)
86+
{
87+
printf("%s\n", a);
88+
return a;
89+
}
90+
91+
int main_4(void)
92+
{
93+
func2(a);
94+
return 0;
95+
}
96+
97+
/* --------------------------------------------- */
98+
int b[3];
99+
int f(void);
100+
101+
int main_5(void)
102+
{
103+
extern int b[3];
104+
b[2]=10;
105+
printf("%d\n", f());
106+
return 0;
107+
}
108+
109+
int f(void)
110+
{
111+
return b[2]==10 ? 1 : 0;
112+
}
113+
79114
/* --------------------------------------------- */
80115
int main()
81116
{
82117
main_1();
83118
main_2();
84119
main_3();
120+
main_4();
121+
main_5();
85122
return 0;
86123
}

tests/tests2/129_scopes.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
129_scopes.c:43: ok : "!in"
2222
129_scopes.c:59: ok : "c == 'a'"
2323
129_scopes.c:69: ok : "st.a == 10"
24+
test
25+
1

tests/tests2/133_old_func.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ float x;
88

99
void func(float a);
1010

11+
void func3(struct p { int a; int b; } *q) {
12+
}
13+
14+
void func4(q)
15+
struct p { int a; int b; int c; } *q;
16+
{
17+
}
18+
19+
struct p { int a; int b; int c; int d; };
20+
1121
int
1222
main(void)
1323
{

tests/tests2/39_typedef.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ typedef struct FunStruct MyFunStruct;
1212

1313
typedef MyFunStruct *MoreFunThanEver;
1414

15+
typedef int t[];
16+
1517
int main()
1618
{
19+
int i, *p;
1720
MyInt a = 1;
1821
printf("%d\n", a);
1922

@@ -25,6 +28,14 @@ int main()
2528
MoreFunThanEver c = &b;
2629
printf("%d,%d\n", c->i, c->j);
2730

31+
p = (t){1,2,3};
32+
for (i = 0; i < 3 ; i++) printf("%d ", *p++); printf("\n");
33+
p = (t){1,2,3,4};
34+
for (i = 0; i < 4 ; i++) printf("%d ", *p++); printf("\n");
35+
36+
printf("%d\n", (int)sizeof((t){1,2,3}));
37+
printf("%d\n", (int)sizeof((t){1,2,3,4}));
38+
2839
return 0;
2940
}
3041

tests/tests2/39_typedef.expect

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
1
22
12,34
33
12,34
4+
1 2 3
5+
1 2 3 4
6+
12
7+
16

0 commit comments

Comments
 (0)