Skip to content

Commit cdc5437

Browse files
author
Daniel Kroening
committed
better location for compound members
1 parent 032f33b commit cdc5437

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/ansi-c/c_typecheck_type.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -874,8 +874,13 @@ void c_typecheck_baset::typecheck_compound_body(
874874
{
875875
struct_union_typet::componentt new_component;
876876

877-
new_component.add_source_location()=
878-
declarator.source_location();
877+
// There may be a declarator, which we use as location for
878+
// the component. Otherwise, use location of the declaration.
879+
const source_locationt source_location =
880+
declarator.get_name().empty() ? declaration.source_location()
881+
: declarator.source_location();
882+
883+
new_component.add_source_location() = source_location;
879884
new_component.set(ID_name, declarator.get_base_name());
880885
new_component.set(ID_pretty_name, declarator.get_base_name());
881886
new_component.type()=declaration.full_type(declarator);
@@ -886,7 +891,7 @@ void c_typecheck_baset::typecheck_compound_body(
886891
(new_component.type().id()!=ID_array ||
887892
!to_array_type(new_component.type()).is_incomplete()))
888893
{
889-
error().source_location=new_component.type().source_location();
894+
error().source_location = source_location;
890895
error() << "incomplete type not permitted here" << eom;
891896
throw 0;
892897
}

src/ansi-c/parser.y

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,8 @@ member_declaration:
16341634
| member_default_declaring_list ';'
16351635
| ';' /* empty declaration */
16361636
{
1637-
init($$, ID_declaration);
1637+
$$=$1; // the ';' becomes the location of the declaration
1638+
stack($$).id(ID_declaration);
16381639
}
16391640
| static_assert_declaration ';'
16401641
;
@@ -1651,6 +1652,7 @@ member_default_declaring_list:
16511652

16521653
init($$, ID_declaration);
16531654
to_ansi_c_declaration(stack($$)).set_is_member(true);
1655+
stack($$).add_source_location()=stack($2).source_location();
16541656
stack($$).type().swap(stack($2));
16551657
PARSER.add_declarator(stack($$), stack($3));
16561658
}
@@ -1686,6 +1688,7 @@ member_declaring_list:
16861688

16871689
init($$, ID_declaration);
16881690
to_ansi_c_declaration(stack($$)).set_is_member(true);
1691+
stack($$).add_source_location()=stack($2).source_location();
16891692
stack($$).type().swap(stack($2));
16901693
PARSER.add_declarator(stack($$), stack($3));
16911694
}

0 commit comments

Comments
 (0)