Module Name: src
Committed By: rillig
Date: Sun May 16 10:08:02 UTC 2021
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_309.c msg_309.exp
Log Message:
tests/lint: add test for warning about zero-bits in '&'
To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_309.c \
src/tests/usr.bin/xlint/lint1/msg_309.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_309.c
diff -u src/tests/usr.bin/xlint/lint1/msg_309.c:1.2 src/tests/usr.bin/xlint/lint1/msg_309.c:1.3
--- src/tests/usr.bin/xlint/lint1/msg_309.c:1.2 Sun Feb 21 09:07:58 2021
+++ src/tests/usr.bin/xlint/lint1/msg_309.c Sun May 16 10:08:01 2021
@@ -1,7 +1,39 @@
-/* $NetBSD: msg_309.c,v 1.2 2021/02/21 09:07:58 rillig Exp $ */
+/* $NetBSD: msg_309.c,v 1.3 2021/05/16 10:08:01 rillig Exp $ */
# 3 "msg_309.c"
// Test for message: extra bits set to 0 in conversion of '%s' to '%s', op %s [309]
-TODO: "Add example code that triggers the above message." /* expect: 249 */
-TODO: "Add example code that almost triggers the above message."
+int
+scale(unsigned long long x) {
+
+ /*
+ * Both operands of '&' have the same type, therefore no conversion
+ * is necessary and no bits can get lost.
+ */
+ if ((x & 0xffffffff00000000ULL) != 0)
+ return 32;
+
+ /*
+ * The constant has type 'unsigned 32-bit'. The usual arithmetic
+ * conversions of '&' convert this constant to unsigned 64-bit.
+ * The programmer may or may not have intended to sign-extend the
+ * bit mask here. This situation may occur during migration from a
+ * 32-bit to a 64-bit platform.
+ */
+ if ((x & 0xffff0000) != 0) /* expect: 309 */
+ return 16;
+
+ /*
+ * In the remaining cases, the constant does not have its most
+ * significant bit set, therefore there is no ambiguity.
+ */
+ if ((x & 0xff00) != 0)
+ return 8;
+ if ((x & 0xf0) != 0)
+ return 4;
+ if ((x & 0xc) != 0)
+ return 2;
+ if ((x & 0x2) != 0)
+ return 1;
+ return (int)(x & 0x1);
+}
Index: src/tests/usr.bin/xlint/lint1/msg_309.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_309.exp:1.2 src/tests/usr.bin/xlint/lint1/msg_309.exp:1.3
--- src/tests/usr.bin/xlint/lint1/msg_309.exp:1.2 Sun Mar 21 20:45:00 2021
+++ src/tests/usr.bin/xlint/lint1/msg_309.exp Sun May 16 10:08:01 2021
@@ -1 +1 @@
-msg_309.c(6): error: syntax error ':' [249]
+msg_309.c(23): warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op & [309]