Skip to content

Commit

Permalink
Error out for incomplete type initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrippling committed Nov 26, 2023
1 parent be8f894 commit fb164e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tccgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -7996,6 +7996,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
toplevel array or the last member of the toplevel struct */

if (size < 0) {
// error out except for top-level incomplete arrays
// (arrays of incomplete types are handled in array parsing)
if (!(type->t & VT_ARRAY))
tcc_error("initialization of incomplete type");

/* If the base type itself was an array type of unspecified size
(like in 'typedef int arr[]; arr x = {1};') then we will
overwrite the unknown size by the real one for this decl.
Expand Down
13 changes: 13 additions & 0 deletions tests/tests2/60_errors_and_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,17 @@ int main() {
#error \123\\
456

#elif defined test_error_incomplete_type
struct A;
void f(struct A *);

int main()
{
f(&(struct A){});
}

struct A {
int x;
};

#endif
5 changes: 4 additions & 1 deletion tests/tests2/60_errors_and_warnings.expect
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
[returns 1]

[test_61_undefined_enum]
60_errors_and_warnings.c:46: error: unknown type size
60_errors_and_warnings.c:46: error: initialization of incomplete type

[test_74_non_const_init]
60_errors_and_warnings.c:49: error: initializer element is not constant
Expand Down Expand Up @@ -228,3 +228,6 @@ arg[1] = "Y"

[test_error_string]
60_errors_and_warnings.c:464: error: #error \123\456

[test_error_incomplete_type]
60_errors_and_warnings.c:472: error: initialization of incomplete type

0 comments on commit fb164e0

Please sign in to comment.