133965Sjdp/*	$NetBSD: msg_166.c,v 1.6 2024/06/08 06:37:06 rillig Exp $	*/
277298Sobrien# 3 "msg_166.c"
333965Sjdp
433965Sjdp// Test for message: precision lost in bit-field assignment [166]
533965Sjdp
633965Sjdp/* lint1-extra-flags: -hp -X 351 */
733965Sjdp
833965Sjdpstruct bit_set {
933965Sjdp
1033965Sjdp	/*
1133965Sjdp	 * C99 6.7.2p5 and 6.7.2.1p9 footnote 104 say that for bit-fields of
1233965Sjdp	 * underlying type 'int', "it is implementation-defined whether the
1333965Sjdp	 * specifier 'int' designates the same type as 'signed int' or the
1433965Sjdp	 * same type as 'unsigned int'".
1533965Sjdp	 *
1633965Sjdp	 * https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations
1733965Sjdp	 * -and-bit-fields-implementation.html says: "By default it is treated
1833965Sjdp	 * as 'signed int' but this may be changed by the
1933965Sjdp	 * '-funsigned-bitfields' option".
2033965Sjdp	 *
2133965Sjdp	 * Clang doesn't document implementation-defined behavior, see
2233965Sjdp	 * https://bugs.llvm.org/show_bug.cgi?id=11272.
2333965Sjdp	 */
2433965Sjdp
2533965Sjdp	/* expect+1: warning: bit-field of type plain 'int' has implementation-defined signedness [344] */
2677298Sobrien	int minus_1_to_0: 1;
2733965Sjdp	/* expect+1: warning: bit-field of type plain 'int' has implementation-defined signedness [344] */
2833965Sjdp	int minus_8_to_7: 4;
2933965Sjdp	unsigned zero_to_1: 1;
3077298Sobrien	unsigned zero_to_15: 4;
3177298Sobrien};
3277298Sobrien
3333965Sjdpvoid example(void) {
3433965Sjdp	struct bit_set bits;
3533965Sjdp
3633965Sjdp	/* Clang doesn't warn about the 1. */
3733965Sjdp	/* expect+1: warning: precision lost in bit-field assignment [166] */
3833965Sjdp	bits.minus_1_to_0 = -2;
3933965Sjdp	bits.minus_1_to_0 = -1;
4033965Sjdp	bits.minus_1_to_0 = 0;
4133965Sjdp	/* expect+1: warning: precision lost in bit-field assignment [166] */
4233965Sjdp	bits.minus_1_to_0 = 1;
4333965Sjdp	/* expect+1: warning: precision lost in bit-field assignment [166] */
4433965Sjdp	bits.minus_1_to_0 = 2;
4533965Sjdp
4633965Sjdp	/* expect+1: warning: precision lost in bit-field assignment [166] */
4777298Sobrien	bits.minus_8_to_7 = -9;
4833965Sjdp	bits.minus_8_to_7 = -8;
4977298Sobrien	bits.minus_8_to_7 = 7;
5077298Sobrien	/* expect+1: warning: precision lost in bit-field assignment [166] */
5133965Sjdp	bits.minus_8_to_7 = 8;
5277298Sobrien
5377298Sobrien	/* Clang doesn't warn about the -1. */
5477298Sobrien	/* expect+1: warning: assignment of negative constant -2 to unsigned type 'unsigned int:1' [164] */
5577298Sobrien	bits.zero_to_1 = -2;
5633965Sjdp	/* expect+1: warning: assignment of negative constant -1 to unsigned type 'unsigned int:1' [164] */
5777298Sobrien	bits.zero_to_1 = -1;
5833965Sjdp	bits.zero_to_1 = 0;
5977298Sobrien	bits.zero_to_1 = 1;
6077298Sobrien	/* expect+1: warning: precision lost in bit-field assignment [166] */
6133965Sjdp	bits.zero_to_1 = 2;
6277298Sobrien
6377298Sobrien	/* Clang doesn't warn about the -8. */
6477298Sobrien	/* expect+1: warning: assignment of negative constant -9 to unsigned type 'unsigned int:4' [164] */
6577298Sobrien	bits.zero_to_15 = -9;
6633965Sjdp	/* expect+1: warning: assignment of negative constant -8 to unsigned type 'unsigned int:4' [164] */
6777298Sobrien	bits.zero_to_15 = -8;
6833965Sjdp	bits.zero_to_15 = 0;
6977298Sobrien	bits.zero_to_15 = 15;
7077298Sobrien	/* expect+1: warning: precision lost in bit-field assignment [166] */
7133965Sjdp	bits.zero_to_15 = 16;
7277298Sobrien}
7377298Sobrien