Skip to content

Commit 5936a3d

Browse files
committed
GCC supports -> in offsetof when forming a member designator
This is undocumented, but available as of GCC > 4.4: gcc-mirror/gcc@f0d0d84
1 parent bcdd322 commit 5936a3d

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stddef.h>
2+
3+
struct S
4+
{
5+
int x;
6+
union {
7+
int y;
8+
struct S2
9+
{
10+
int z;
11+
} s[1];
12+
} u[2];
13+
};
14+
15+
int main()
16+
{
17+
int A[offsetof(struct S, u[0].y)==sizeof(int)?1:-1];
18+
#if defined(__GNUC__) && !defined(__clang__)
19+
int B[offsetof(struct S, u->y)==sizeof(int)?1:-1];
20+
int C[offsetof(struct S, u->s[0].z)==sizeof(int)?1:-1];
21+
#endif
22+
return 0;
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

src/ansi-c/parser.y

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern char *yyansi_ctext;
2222

2323
#include "parser_static.inc"
2424

25+
#include "literals/convert_integer_literal.h"
26+
2527
#include "ansi_c_y.tab.h"
2628

2729
// statements have right recursion, deep nesting of statements thus
@@ -425,6 +427,17 @@ offsetof_member_designator:
425427
mto($2, $3);
426428
mto($$, $2);
427429
}
430+
| offsetof_member_designator TOK_ARROW member_name
431+
{
432+
$$=$1;
433+
set($2, ID_index);
434+
exprt tmp=convert_integer_literal("0");
435+
stack($2).move_to_operands(tmp);
436+
mto($$, $2);
437+
set($2, ID_member);
438+
stack($2).set(ID_component_name, stack($3).get(ID_C_base_name));
439+
mto($$, $2);
440+
}
428441
;
429442

430443
quantifier_expression:

0 commit comments

Comments
 (0)