rui314 / chibicc

A small C compiler
MIT License
9.57k stars 869 forks source link

Assertion when using empty structs #36

Open edubart opened 3 years ago

edubart commented 3 years ago

I am trying to use chibicc with some tool that generates C code with empty structs and I get some assertions when compiling, here is a small test case:

typedef struct empty_struct {} empty_struct;

int f(empty_struct a) {
  return 0;
}

int main() {
  f((empty_struct){});
  return 0;
}

Output:

./chibicc a.c -o a
chibicc: codegen.c:1568: emit_text: Assertion `depth == 0' failed.
rui314 commented 3 years ago

As per the C spec, program's behavior is undefined if a struct is empty. So you generally should avoid using it.

https://www.sigbus.info/n1570#6.7.2.1p8

That being said, gcc and clang handle empty structs as a one-byte object, and chibicc should mimic that behavior for the compatibility with the compilers.