diff --git a/regression/ansi-c/Incomplete_Type1/incomplete_component.c b/regression/ansi-c/Incomplete_Type1/incomplete_component.c new file mode 100644 index 00000000000..fdb3b2b6a2f --- /dev/null +++ b/regression/ansi-c/Incomplete_Type1/incomplete_component.c @@ -0,0 +1,10 @@ +typedef struct incomplete_struct t; + +struct +{ + t x; +}; + +int main() +{ +} diff --git a/regression/ansi-c/Incomplete_Type1/incomplete_component.desc b/regression/ansi-c/Incomplete_Type1/incomplete_component.desc new file mode 100644 index 00000000000..92066edfe4e --- /dev/null +++ b/regression/ansi-c/Incomplete_Type1/incomplete_component.desc @@ -0,0 +1,9 @@ +CORE +incomplete_component.c + +^EXIT=(1|64)$ +^SIGNAL=0$ +^CONVERSION ERROR$ +^incomplete_component\.c:5:1: error: incomplete type not permitted here$ +-- +^warning: ignoring diff --git a/src/ansi-c/c_typecheck_type.cpp b/src/ansi-c/c_typecheck_type.cpp index 2bb1dc370d1..1958480cbc7 100644 --- a/src/ansi-c/c_typecheck_type.cpp +++ b/src/ansi-c/c_typecheck_type.cpp @@ -872,8 +872,13 @@ void c_typecheck_baset::typecheck_compound_body( struct_union_typet::componentt new_component( declarator.get_base_name(), declaration.full_type(declarator)); - new_component.add_source_location()= - declarator.source_location(); + // There may be a declarator, which we use as location for + // the component. Otherwise, use location of the declaration. + const source_locationt source_location = + declarator.get_name().empty() ? declaration.source_location() + : declarator.source_location(); + + new_component.add_source_location() = source_location; new_component.set_pretty_name(declarator.get_base_name()); typecheck_type(new_component.type()); @@ -882,7 +887,7 @@ void c_typecheck_baset::typecheck_compound_body( (new_component.type().id()!=ID_array || !to_array_type(new_component.type()).is_incomplete())) { - error().source_location=new_component.type().source_location(); + error().source_location = source_location; error() << "incomplete type not permitted here" << eom; throw 0; } diff --git a/src/ansi-c/parser.y b/src/ansi-c/parser.y index e02ae3bd571..539e8494b00 100644 --- a/src/ansi-c/parser.y +++ b/src/ansi-c/parser.y @@ -1634,7 +1634,8 @@ member_declaration: | member_default_declaring_list ';' | ';' /* empty declaration */ { - init($$, ID_declaration); + $$=$1; // the ';' becomes the location of the declaration + stack($$).id(ID_declaration); } | static_assert_declaration ';' ; @@ -1651,6 +1652,7 @@ member_default_declaring_list: init($$, ID_declaration); to_ansi_c_declaration(stack($$)).set_is_member(true); + stack($$).add_source_location()=stack($2).source_location(); stack($$).type().swap(stack($2)); PARSER.add_declarator(stack($$), stack($3)); } @@ -1686,6 +1688,7 @@ member_declaring_list: init($$, ID_declaration); to_ansi_c_declaration(stack($$)).set_is_member(true); + stack($$).add_source_location()=stack($2).source_location(); stack($$).type().swap(stack($2)); PARSER.add_declarator(stack($$), stack($3)); }