msg_166.c revision 1.3
1/*	$NetBSD: msg_166.c,v 1.3 2021/05/16 11:11:37 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 */
7
8struct 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	int minus_1_to_0: 1;		/* expect: 344 */
26	int minus_8_to_7: 4;		/* expect: 344 */
27	unsigned zero_to_1: 1;
28	unsigned zero_to_15: 4;
29};
30
31void example(void) {
32	struct bit_set bits;
33
34	/* Clang doesn't warn about the 1. */
35	bits.minus_1_to_0 = -2;		/* expect: 166 */
36	bits.minus_1_to_0 = -1;
37	bits.minus_1_to_0 = 0;
38	bits.minus_1_to_0 = 1;		/* expect: 166 */
39	bits.minus_1_to_0 = 2;		/* expect: 166 */
40
41	bits.minus_8_to_7 = -9;		/* expect: 166 */
42	bits.minus_8_to_7 = -8;
43	bits.minus_8_to_7 = 7;
44	bits.minus_8_to_7 = 8;		/* expect: 166 */
45
46	/* Clang doesn't warn about the -1. */
47	bits.zero_to_1 = -2;		/* expect: 164 */
48	bits.zero_to_1 = -1;		/* expect: 164 */
49	bits.zero_to_1 = 0;
50	bits.zero_to_1 = 1;
51	bits.zero_to_1 = 2;		/* expect: 166 */
52
53	/* Clang doesn't warn about the -8. */
54	bits.zero_to_15 = -9;		/* expect: 164 */
55	bits.zero_to_15 = -8;		/* expect: 164 */
56	bits.zero_to_15 = 0;
57	bits.zero_to_15 = 15;
58	bits.zero_to_15 = 16;		/* expect: 166 */
59}
60