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