diff --git a/compiler/cheaders.v b/compiler/cheaders.v index 83b4973a42..2004f5fcc4 100644 --- a/compiler/cheaders.v +++ b/compiler/cheaders.v @@ -40,9 +40,17 @@ CommonCHeaders = ' #define EMPTY_STRUCT_DECLARATION #define EMPTY_STRUCT_INITIALIZATION 0 +// Due to a tcc bug, the length of an array needs to be specified, but GCC crashes if it is... +#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[]) +#define TCCSKIP(x) x + #ifdef __TINYC__ #undef EMPTY_STRUCT_INITIALIZATION #define EMPTY_STRUCT_INITIALIZATION +#undef EMPTY_ARRAY_OF_ELEMS +#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n]) +#undef TCCSKIP +#define TCCSKIP(x) #endif #define OPTION_CAST(x) (x) diff --git a/compiler/gen_c.v b/compiler/gen_c.v index 5a620463c0..33faf6a8c4 100644 --- a/compiler/gen_c.v +++ b/compiler/gen_c.v @@ -317,18 +317,15 @@ fn (p mut Parser) gen_array_init(typ string, no_alloc bool, new_arr_ph int, nr_e if no_alloc { new_arr += '_no_alloc' } - if nr_elems == 0 && p.pref.ccompiler != 'tcc' { - p.gen(' 0 })') + if nr_elems == 0 { + p.gen(' TCCSKIP(0) })') } else { p.gen(' })') } // Need to do this in the second pass, otherwise it goes to the very top of the out.c file - if !p.first_pass() { - // Due to a tcc bug, the length needs to be specified. - // GCC crashes if it is. - cast := if p.pref.ccompiler == 'tcc' { '($typ[$nr_elems])' } else { '($typ[])' } - p.cgen.set_placeholder(new_arr_ph, - '$new_arr($nr_elems, $nr_elems, sizeof($typ), $cast { ') + if !p.first_pass() { + p.cgen.set_placeholder(new_arr_ph, + '$new_arr($nr_elems, $nr_elems, sizeof($typ), EMPTY_ARRAY_OF_ELEMS( $typ, $nr_elems ) { ') } }