1/*	$NetBSD: d_bltinoffsetof.c,v 1.3 2024/01/09 23:46:54 rillig Exp $	*/
2# 3 "d_bltinoffsetof.c"
3
4struct 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
21typedef int first[-(int)__builtin_offsetof(struct foo, u.s.first)];
22typedef int first_a[-(int)__builtin_offsetof(struct foo, u.s.first.a)];
23/* expect+1: ... (-4) ... */
24typedef int first_b[-(int)__builtin_offsetof(struct foo, u.s.first.b)];
25/* expect+1: ... (-8) ... */
26typedef int second[-(int)__builtin_offsetof(struct foo, u.s.second)];
27
28/* expect+1: ... (-1000) ... */
29typedef int array[-(int)__builtin_offsetof(struct foo, array)];
30/* expect+1: ... (-1000) ... */
31typedef int array_0_a[-(int)__builtin_offsetof(struct foo, array[0].a)];
32/* expect+1: ... (-1000) ... */
33typedef int array_0_b[-(int)__builtin_offsetof(struct foo, array[0].b)];
34/* expect+1: ... (-1008) ... */
35typedef 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) ... */
40typedef int array_50_a[-(int)__builtin_offsetof(struct foo, array[50].a)];
41/* expect+1: ... (-1400) ... */
42typedef 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) ... */
48typedef int array_51_a[-(int)__builtin_offsetof(struct foo, array[51].a)];
49