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