From 19fdef46f960dd72fb5883514df25501db5b2d4e Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Fri, 15 Aug 2025 09:59:11 +0200 Subject: [PATCH] Allow mixing old/new function prototypes This fixes another external testsuite failure --- tccgen.c | 23 +++++++++++++++++++---- tests/tests2/133_old_func.c | 8 ++++++++ tests/tests2/133_old_func.expect | 1 + 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tccgen.c b/tccgen.c index afb1567a..b6dbad2d 100644 --- a/tccgen.c +++ b/tccgen.c @@ -8517,7 +8517,7 @@ static int decl(int l) { int v, has_init, r, oldint; CType type, btype; - Sym *sym; + Sym *sym, *s; AttributeDef ad, adbase; ElfSym *esym; @@ -8589,6 +8589,21 @@ static int decl(int l) if (sym->f.func_type == FUNC_OLD && l == VT_CONST) { func_vt = type; decl(VT_CMP); + + /* Allow mixing old/new prototypes + * void func(float a); + * int main(void) { func(1.0); } + * void func(a) float a; { printf("%g\n", a); } + */ + s = sym_find(v); + if (type.ref->next && // skip old func(); definitions + s && s->type.ref && + s->type.ref->f.func_type == FUNC_NEW) { + sym->f.func_type = FUNC_NEW; + if (!is_compatible_types(&s->type, &type)) + tcc_error("incompatible redefinition of '%s'", + get_tok_str(v, NULL)); + } } if ((type.t & (VT_EXTERN|VT_INLINE)) == (VT_EXTERN|VT_INLINE)) { @@ -8646,6 +8661,9 @@ static int decl(int l) while ((sym = sym->next) != NULL) { if (!(sym->v & ~SYM_FIELD)) expect("identifier"); + if (type.ref->f.func_type == FUNC_OLD && + sym->type.t == VT_FLOAT) + sym->type.t = VT_DOUBLE; if (sym->type.t == VT_VOID) sym->type = int_type; } @@ -8693,9 +8711,6 @@ static int decl(int l) if (sym->type.t != VT_VOID) tcc_error("redefinition of parameter '%s'", get_tok_str(v, NULL)); - if (func_vt.ref->f.func_type == FUNC_OLD && - type.t == VT_FLOAT) - type.t = VT_DOUBLE; convert_parameter_type(&type); sym->type = type; } else if (type.t & VT_TYPEDEF) { diff --git a/tests/tests2/133_old_func.c b/tests/tests2/133_old_func.c index 81b9b75a..cb702194 100644 --- a/tests/tests2/133_old_func.c +++ b/tests/tests2/133_old_func.c @@ -6,6 +6,8 @@ float x; return 2.0 * x; } +void func(float a); + int main(void) { @@ -13,6 +15,7 @@ main(void) printf("%g %g\n", fx(2.0), fy(10.0)); printf("%g %g\n", fx(2.0f), fy(10.0f)); + func(1); } float fy(x) @@ -21,3 +24,8 @@ float x; return 3.0 * x; } +void func(a) +float a; +{ + printf("%g\n", a); +} diff --git a/tests/tests2/133_old_func.expect b/tests/tests2/133_old_func.expect index e93760b6..525ba06f 100644 --- a/tests/tests2/133_old_func.expect +++ b/tests/tests2/133_old_func.expect @@ -1,2 +1,3 @@ 4 30 4 30 +1