xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_102.c (revision e50dbdf451c05cd4203d32971385809406ec02e0)
1 /*	$NetBSD: msg_102.c,v 1.6 2023/06/30 21:06:18 rillig Exp $	*/
2 # 3 "msg_102.c"
3 
4 // Test for message: illegal use of member '%s' [102]
5 
6 // Anonymous members are defined in C11 6.7.2.1p2.
7 
8 struct unrelated {
9 	union {
10 		struct {
11 			unsigned bit_0:1;
12 			unsigned bit_1:1;
13 		};
14 		unsigned bits;
15 	};
16 };
17 
18 struct bit_fields_and_bits {
19 	union {
20 		struct {
21 			unsigned bf_bit_0:1;
22 			unsigned bf_bit_1:1;
23 		};
24 		unsigned bf_bits;
25 	};
26 };
27 
28 static struct unrelated *u1, *u2;
29 static struct bit_fields_and_bits *b1, *b2;
30 
31 static inline _Bool
eq(int x)32 eq(int x)
33 {
34 	if (x == 0)
35 		/* Accessing a member from an unnamed struct member. */
36 		return u1->bits == u2->bits;
37 
38 	/*
39 	 * The struct does not have a member named 'bits'.  There's another
40 	 * struct with a member of that name, and in traditional C, it was
41 	 * possible but discouraged to access members of other structs via
42 	 * their plain name.
43 	 */
44 	/* expect+2: error: illegal use of member 'bits' [102] */
45 	/* expect+1: error: illegal use of member 'bits' [102] */
46 	return b1->bits == b2->bits;
47 }
48