check empty struct while use sizeof

pull/14007/head
chengqinglin 2022-04-11 18:06:02 +08:00
parent 75ff76a999
commit e6cd556205
4 changed files with 28 additions and 3 deletions

View File

@ -307,4 +307,8 @@ fn test_array_append_empty_struct() {
mut names := []XYZ{cap: 2} mut names := []XYZ{cap: 2}
names << XYZ{} names << XYZ{}
assert (XYZ{} in names) == true assert (XYZ{} in names) == true
// test fixed array
array := [XYZ{}]
assert (XYZ{} in names) == true
} }

View File

@ -151,7 +151,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
} else { } else {
g.write('0, ') g.write('0, ')
} }
if elem_type.unaliased_sym.kind == .function { if elem_type.unaliased_sym.kind == .function || g.is_empty_struct(elem_type) {
g.write('sizeof(voidptr), ') g.write('sizeof(voidptr), ')
} else { } else {
g.write('sizeof($elem_styp), ') g.write('sizeof($elem_styp), ')
@ -217,7 +217,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
} else { } else {
g.write('0, ') g.write('0, ')
} }
if elem_type.unaliased_sym.kind == .function { if elem_type.unaliased_sym.kind == .function || g.is_empty_struct(elem_type) {
g.write('sizeof(voidptr), ') g.write('sizeof(voidptr), ')
} else { } else {
g.write('sizeof($elem_styp), ') g.write('sizeof($elem_styp), ')
@ -251,6 +251,8 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
len := node.exprs.len len := node.exprs.len
if elem_type.unaliased_sym.kind == .function { if elem_type.unaliased_sym.kind == .function {
g.write('new_array_from_c_array($len, $len, sizeof(voidptr), _MOV((voidptr[$len]){') g.write('new_array_from_c_array($len, $len, sizeof(voidptr), _MOV((voidptr[$len]){')
} else if g.is_empty_struct(elem_type) {
g.write('new_array_from_c_array${noscan}($len, $len, sizeof(voidptr), _MOV(($elem_styp[$len]){')
} else { } else {
g.write('new_array_from_c_array${noscan}($len, $len, sizeof($elem_styp), _MOV(($elem_styp[$len]){') g.write('new_array_from_c_array${noscan}($len, $len, sizeof($elem_styp), _MOV(($elem_styp[$len]){')
} }

View File

@ -258,7 +258,7 @@ static void __closure_destroy(void *closure) {
const c_common_macros = ' const c_common_macros = '
#define EMPTY_VARG_INITIALIZATION 0 #define EMPTY_VARG_INITIALIZATION 0
#define EMPTY_STRUCT_DECLARATION char _dummy #define EMPTY_STRUCT_DECLARATION
#define EMPTY_STRUCT_INITIALIZATION #define EMPTY_STRUCT_INITIALIZATION
// Due to a tcc bug, the length of an array needs to be specified, but GCC crashes if it is... // 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 EMPTY_ARRAY_OF_ELEMS(x,n) (x[])
@ -300,6 +300,8 @@ const c_common_macros = '
#endif #endif
#ifdef __TINYC__ #ifdef __TINYC__
#undef EMPTY_STRUCT_DECLARATION
#define EMPTY_STRUCT_DECLARATION char _dummy
#undef EMPTY_ARRAY_OF_ELEMS #undef EMPTY_ARRAY_OF_ELEMS
#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n]) #define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n])
#undef __NOINLINE #undef __NOINLINE
@ -574,8 +576,10 @@ voidptr memdup(voidptr src, int sz);
#define _Atomic volatile #define _Atomic volatile
// MSVC cannot parse some things properly // MSVC cannot parse some things properly
#undef EMPTY_STRUCT_DECLARATION
#undef OPTION_CAST #undef OPTION_CAST
#define EMPTY_STRUCT_DECLARATION char __pad
#define OPTION_CAST(x) #define OPTION_CAST(x)
#undef __NOINLINE #undef __NOINLINE
#undef __IRQHANDLER #undef __IRQHANDLER

View File

@ -305,3 +305,18 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
} }
return true return true
} }
fn (mut g Gen) is_empty_struct(t Type) bool {
sym := t.unaliased_sym
match sym.info {
ast.Struct {
if sym.info.fields.len > 0 || sym.info.embeds.len > 0 {
return false
}
return true
}
else {
return false
}
}
}