xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_166.c (revision 98412b5015f4d494913d9440a5ebd2476000e119)
1 /*	$NetBSD: msg_166.c,v 1.6 2024/06/08 06:37:06 rillig Exp $	*/
2 # 3 "msg_166.c"
3 
4 // Test for message: precision lost in bit-field assignment [166]
5 
6 /* lint1-extra-flags: -hp -X 351 */
7 
8 struct bit_set {
9 
10 	/*
11 	 * C99 6.7.2p5 and 6.7.2.1p9 footnote 104 say that for bit-fields of
12 	 * underlying type 'int', "it is implementation-defined whether the
13 	 * specifier 'int' designates the same type as 'signed int' or the
14 	 * same type as 'unsigned int'".
15 	 *
16 	 * https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations
17 	 * -and-bit-fields-implementation.html says: "By default it is treated
18 	 * as 'signed int' but this may be changed by the
19 	 * '-funsigned-bitfields' option".
20 	 *
21 	 * Clang doesn't document implementation-defined behavior, see
22 	 * https://bugs.llvm.org/show_bug.cgi?id=11272.
23 	 */
24 
25 	/* expect+1: warning: bit-field of type plain 'int' has implementation-defined signedness [344] */
26 	int minus_1_to_0: 1;
27 	/* expect+1: warning: bit-field of type plain 'int' has implementation-defined signedness [344] */
28 	int minus_8_to_7: 4;
29 	unsigned zero_to_1: 1;
30 	unsigned zero_to_15: 4;
31 };
32 
example(void)33 void example(void) {
34 	struct bit_set bits;
35 
36 	/* Clang doesn't warn about the 1. */
37 	/* expect+1: warning: precision lost in bit-field assignment [166] */
38 	bits.minus_1_to_0 = -2;
39 	bits.minus_1_to_0 = -1;
40 	bits.minus_1_to_0 = 0;
41 	/* expect+1: warning: precision lost in bit-field assignment [166] */
42 	bits.minus_1_to_0 = 1;
43 	/* expect+1: warning: precision lost in bit-field assignment [166] */
44 	bits.minus_1_to_0 = 2;
45 
46 	/* expect+1: warning: precision lost in bit-field assignment [166] */
47 	bits.minus_8_to_7 = -9;
48 	bits.minus_8_to_7 = -8;
49 	bits.minus_8_to_7 = 7;
50 	/* expect+1: warning: precision lost in bit-field assignment [166] */
51 	bits.minus_8_to_7 = 8;
52 
53 	/* Clang doesn't warn about the -1. */
54 	/* expect+1: warning: assignment of negative constant -2 to unsigned type 'unsigned int:1' [164] */
55 	bits.zero_to_1 = -2;
56 	/* expect+1: warning: assignment of negative constant -1 to unsigned type 'unsigned int:1' [164] */
57 	bits.zero_to_1 = -1;
58 	bits.zero_to_1 = 0;
59 	bits.zero_to_1 = 1;
60 	/* expect+1: warning: precision lost in bit-field assignment [166] */
61 	bits.zero_to_1 = 2;
62 
63 	/* Clang doesn't warn about the -8. */
64 	/* expect+1: warning: assignment of negative constant -9 to unsigned type 'unsigned int:4' [164] */
65 	bits.zero_to_15 = -9;
66 	/* expect+1: warning: assignment of negative constant -8 to unsigned type 'unsigned int:4' [164] */
67 	bits.zero_to_15 = -8;
68 	bits.zero_to_15 = 0;
69 	bits.zero_to_15 = 15;
70 	/* expect+1: warning: precision lost in bit-field assignment [166] */
71 	bits.zero_to_15 = 16;
72 }
73