Skip to content

Commit 953326b

Browse files
author
Daniel Kroening
committed
better location for compound members
1 parent a21cd68 commit 953326b

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
@@ -873,8 +873,13 @@ void c_typecheck_baset::typecheck_compound_body(
873873
{
874874
struct_union_typet::componentt new_component;
875875

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

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)