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.
This commit is contained in:
herman ten brugge
2025-08-30 07:13:49 +02:00
parent 8c59fd3cb6
commit e6ea0d0424
6 changed files with 71 additions and 3 deletions

View File

@@ -1263,7 +1263,7 @@ static void patch_type(Sym *sym, CType *type)
} else {
if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) {
/* set array size if it was omitted in extern declaration */
sym->type.ref = type->ref;
sym->type.ref->c = type->ref->c;
}
if ((type->t ^ sym->type.t) & VT_STATIC)
tcc_warning("storage mismatch for redefinition of '%s'",
@@ -4890,6 +4890,8 @@ static int parse_btype(CType *type, AttributeDef *ad, int ignore_label)
sym_to_attr(ad, s);
typespec_found = 1;
st = bt = -2;
if (type->ref && (t & VT_ARRAY) && type->ref->c < 0)
type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
break;
}
type_found = 1;
@@ -4992,7 +4994,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td)
type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT | TYPE_PARAM);
if ((pt.t & VT_BTYPE) == VT_VOID)
tcc_error("parameter declared as void");
if (n == 0)
if (local_scope > 1 || n == 0)
n = SYM_FIELD;
} else {
n = tok;
@@ -5000,7 +5002,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td)
pt.ref = NULL;
next();
}
if (n < TOK_UIDENT)
if (local_scope == 1 && n < TOK_UIDENT)
expect("identifier");
convert_parameter_type(&pt);
arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
@@ -8685,7 +8687,9 @@ static int decl(int l)
sym = type.ref;
if (sym->f.func_type == FUNC_OLD && l == VT_CONST) {
func_vt = type;
++local_scope;
decl(VT_CMP);
--local_scope;
}
if ((type.t & (VT_EXTERN|VT_INLINE)) == (VT_EXTERN|VT_INLINE)) {
/* always_inline functions must be handled as if they

View File

@@ -76,11 +76,48 @@ struct st func(void)
return st;
}
/* --------------------------------------------- */
static void func2(char *(*md)(char *md))
{
(*md)("test");
}
static char *a(char *a)
{
printf("%s\n", a);
return a;
}
int main_4(void)
{
func2(a);
return 0;
}
/* --------------------------------------------- */
int b[3];
int f(void);
int main_5(void)
{
extern int b[3];
b[2]=10;
printf("%d\n", f());
return 0;
}
int f(void)
{
return b[2]==10 ? 1 : 0;
}
/* --------------------------------------------- */
int main()
{
main_1();
main_2();
main_3();
main_4();
main_5();
return 0;
}

View File

@@ -21,3 +21,5 @@
129_scopes.c:43: ok : "!in"
129_scopes.c:59: ok : "c == 'a'"
129_scopes.c:69: ok : "st.a == 10"
test
1

View File

@@ -8,6 +8,16 @@ float x;
void func(float a);
void func3(struct p { int a; int b; } *q) {
}
void func4(q)
struct p { int a; int b; int c; } *q;
{
}
struct p { int a; int b; int c; int d; };
int
main(void)
{

View File

@@ -12,8 +12,11 @@ typedef struct FunStruct MyFunStruct;
typedef MyFunStruct *MoreFunThanEver;
typedef int t[];
int main()
{
int i, *p;
MyInt a = 1;
printf("%d\n", a);
@@ -25,6 +28,14 @@ int main()
MoreFunThanEver c = &b;
printf("%d,%d\n", c->i, c->j);
p = (t){1,2,3};
for (i = 0; i < 3 ; i++) printf("%d ", *p++); printf("\n");
p = (t){1,2,3,4};
for (i = 0; i < 4 ; i++) printf("%d ", *p++); printf("\n");
printf("%d\n", (int)sizeof((t){1,2,3}));
printf("%d\n", (int)sizeof((t){1,2,3,4}));
return 0;
}

View File

@@ -1,3 +1,7 @@
1
12,34
12,34
1 2 3
1 2 3 4
12
16