d_bltinoffsetof.c revision 1.3 1 /* $NetBSD: d_bltinoffsetof.c,v 1.3 2024/01/09 23:46:54 rillig Exp $ */
2 # 3 "d_bltinoffsetof.c"
3
4 struct foo {
5 union {
6 struct {
7 struct {
8 int a;
9 int b;
10 } first;
11 char *second;
12 } s;
13 unsigned char padding[1000];
14 } u;
15 union {
16 int a;
17 double b;
18 } array[50];
19 };
20
21 typedef int first[-(int)__builtin_offsetof(struct foo, u.s.first)];
22 typedef int first_a[-(int)__builtin_offsetof(struct foo, u.s.first.a)];
23 /* expect+1: ... (-4) ... */
24 typedef int first_b[-(int)__builtin_offsetof(struct foo, u.s.first.b)];
25 /* expect+1: ... (-8) ... */
26 typedef int second[-(int)__builtin_offsetof(struct foo, u.s.second)];
27
28 /* expect+1: ... (-1000) ... */
29 typedef int array[-(int)__builtin_offsetof(struct foo, array)];
30 /* expect+1: ... (-1000) ... */
31 typedef int array_0_a[-(int)__builtin_offsetof(struct foo, array[0].a)];
32 /* expect+1: ... (-1000) ... */
33 typedef int array_0_b[-(int)__builtin_offsetof(struct foo, array[0].b)];
34 /* expect+1: ... (-1008) ... */
35 typedef int array_1_a[-(int)__builtin_offsetof(struct foo, array[1].a)];
36
37 // There is no element array[50], but pointing right behind the last element
38 // may be fine.
39 /* expect+1: ... (-1400) ... */
40 typedef int array_50_a[-(int)__builtin_offsetof(struct foo, array[50].a)];
41 /* expect+1: ... (-1400) ... */
42 typedef int sizeof_foo[-(int)sizeof(struct foo)];
43
44
45 // 51 is out of bounds, as it is larger than the size of the struct.
46 // No warning though, maybe later.
47 /* expect+1: ... (-1408) ... */
48 typedef int array_51_a[-(int)__builtin_offsetof(struct foo, array[51].a)];
49