Skip to content

Commit 2e84af4

Browse files
authored
Merge pull request #2997 from diffblue/member-location
better location for compound members
2 parents f1a47f9 + 0f43b25 commit 2e84af4

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
typedef struct incomplete_struct t;
2+
3+
struct
4+
{
5+
t x;
6+
};
7+
8+
int main()
9+
{
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
incomplete_component.c
3+
4+
^EXIT=(1|64)$
5+
^SIGNAL=0$
6+
^CONVERSION ERROR$
7+
^incomplete_component\.c:5:1: error: incomplete type not permitted here$
8+
--
9+
^warning: ignoring

src/ansi-c/c_typecheck_type.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,13 @@ void c_typecheck_baset::typecheck_compound_body(
872872
struct_union_typet::componentt new_component(
873873
declarator.get_base_name(), declaration.full_type(declarator));
874874

875-
new_component.add_source_location()=
876-
declarator.source_location();
875+
// There may be a declarator, which we use as location for
876+
// the component. Otherwise, use location of the declaration.
877+
const source_locationt source_location =
878+
declarator.get_name().empty() ? declaration.source_location()
879+
: declarator.source_location();
880+
881+
new_component.add_source_location() = source_location;
877882
new_component.set_pretty_name(declarator.get_base_name());
878883

879884
typecheck_type(new_component.type());
@@ -882,7 +887,7 @@ void c_typecheck_baset::typecheck_compound_body(
882887
(new_component.type().id()!=ID_array ||
883888
!to_array_type(new_component.type()).is_incomplete()))
884889
{
885-
error().source_location=new_component.type().source_location();
890+
error().source_location = source_location;
886891
error() << "incomplete type not permitted here" << eom;
887892
throw 0;
888893
}

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)