xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_329.c (revision e6298b924c5ba98f3a22919b56dab04a87cdbb1c)
1 /*	$NetBSD: msg_329.c,v 1.5 2023/07/07 19:45:22 rillig Exp $	*/
2 # 3 "msg_329.c"
3 
4 // Test for message: type '%s' is not a member of '%s' [329]
5 
6 /* lint1-extra-flags: -X 351 */
7 
8 union u {
9 	int i1;
10 	int i2;
11 	void *vp;
12 };
13 
14 void
example(void)15 example(void)
16 {
17 	/*
18 	 * A type cast to a union type is valid if the source type is any
19 	 * member type of the union.  Since all union members with the same
20 	 * type have the same representation, the name of the union member
21 	 * doesn't matter.
22 	 *
23 	 * XXX: could there be padding bits or other tricky details that are
24 	 * settable per-member?  These could make the type alone insufficient
25 	 * for determining the exact representation.
26 	 *
27 	 * C99 6.5.4 "Cast operators" does not mention a union cast.  On the
28 	 * contrary, it says that the type name shall specify a scalar type.
29 	 *
30 	 * C11 6.5.4 "Cast operators" differs from C99 but still requires
31 	 * scalar types for both the target type and the source value.
32 	 *
33 	 * This is a GCC extension.
34 	 * See https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html.
35 	 */
36 	union u u_i1 = (union u)3;
37 	union u u_vp = (union u)(void *)0;
38 	/* expect+1: error: type 'pointer to char' is not a member of 'union u' [329] */
39 	union u u_cp = (union u)(char *)0;
40 }
41