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