Module Name:    src
Committed By:   rillig
Date:           Sun Jan 31 13:11:08 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_166.c msg_166.exp

Log Message:
lint: add test for lossy assignments to bit-fields (164, 166)


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/xlint/lint1/msg_166.c \
    src/tests/usr.bin/xlint/lint1/msg_166.exp

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/msg_166.c
diff -u src/tests/usr.bin/xlint/lint1/msg_166.c:1.1 src/tests/usr.bin/xlint/lint1/msg_166.c:1.2
--- src/tests/usr.bin/xlint/lint1/msg_166.c:1.1	Sat Jan  2 10:22:43 2021
+++ src/tests/usr.bin/xlint/lint1/msg_166.c	Sun Jan 31 13:11:08 2021
@@ -1,7 +1,59 @@
-/*	$NetBSD: msg_166.c,v 1.1 2021/01/02 10:22:43 rillig Exp $	*/
+/*	$NetBSD: msg_166.c,v 1.2 2021/01/31 13:11:08 rillig Exp $	*/
 # 3 "msg_166.c"
 
 // Test for message: precision lost in bit-field assignment [166]
 
-TODO: "Add example code that triggers the above message."
-TODO: "Add example code that almost triggers the above message."
+/* lint1-extra-flags: -hp */
+
+struct bit_set {
+
+	/*
+	 * C99 6.7.2p5 and 6.7.2.1p9 footnote 104 say that for bit-fields of
+	 * underlying type 'int', "it is implementation-defined whether the
+	 * specifier 'int' designates the same type as 'signed int' or the
+	 * same type as 'unsigned int'".
+	 *
+	 * https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations
+	 * -and-bit-fields-implementation.html says: "By default it is treated
+	 * as 'signed int' but this may be changed by the
+	 * '-funsigned-bitfields' option".
+	 *
+	 * Clang doesn't document implementation-defined behavior, see
+	 * https://bugs.llvm.org/show_bug.cgi?id=11272.
+	 */
+
+	int minus_1_to_0: 1;		/* expect: 34 */
+	int minus_8_to_7: 4;		/* expect: 34 */
+	unsigned zero_to_1: 1;
+	unsigned zero_to_15: 4;
+};
+
+void example(void) {
+	struct bit_set bits;
+
+	/* Clang doesn't warn about the 1. */
+	bits.minus_1_to_0 = -2;		/* expect: 166 */
+	bits.minus_1_to_0 = -1;
+	bits.minus_1_to_0 = 0;
+	bits.minus_1_to_0 = 1;		/* expect: 166 */
+	bits.minus_1_to_0 = 2;		/* expect: 166 */
+
+	bits.minus_8_to_7 = -9;		/* expect: 166 */
+	bits.minus_8_to_7 = -8;
+	bits.minus_8_to_7 = 7;
+	bits.minus_8_to_7 = 8;		/* expect: 166 */
+
+	/* Clang doesn't warn about the -1. */
+	bits.zero_to_1 = -2;		/* expect: 164 */
+	bits.zero_to_1 = -1;		/* expect: 164 */
+	bits.zero_to_1 = 0;
+	bits.zero_to_1 = 1;
+	bits.zero_to_1 = 2;		/* expect: 166 */
+
+	/* Clang doesn't warn about the -8. */
+	bits.zero_to_15 = -9;		/* expect: 164 */
+	bits.zero_to_15 = -8;		/* expect: 164 */
+	bits.zero_to_15 = 0;
+	bits.zero_to_15 = 15;
+	bits.zero_to_15 = 16;		/* expect: 166 */
+}
Index: src/tests/usr.bin/xlint/lint1/msg_166.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_166.exp:1.1 src/tests/usr.bin/xlint/lint1/msg_166.exp:1.2
--- src/tests/usr.bin/xlint/lint1/msg_166.exp:1.1	Sat Jan  2 10:22:43 2021
+++ src/tests/usr.bin/xlint/lint1/msg_166.exp	Sun Jan 31 13:11:08 2021
@@ -1 +1,13 @@
-msg_166.c(6): syntax error ':' [249]
+msg_166.c(25): warning: nonportable bit-field type [34]
+msg_166.c(26): warning: nonportable bit-field type [34]
+msg_166.c(35): warning: precision lost in bit-field assignment [166]
+msg_166.c(38): warning: precision lost in bit-field assignment [166]
+msg_166.c(39): warning: precision lost in bit-field assignment [166]
+msg_166.c(41): warning: precision lost in bit-field assignment [166]
+msg_166.c(44): warning: precision lost in bit-field assignment [166]
+msg_166.c(47): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(48): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(51): warning: precision lost in bit-field assignment [166]
+msg_166.c(54): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(55): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(58): warning: precision lost in bit-field assignment [166]

Reply via email to