Module Name:    src
Committed By:   rillig
Date:           Sat Oct 15 21:19:15 UTC 2022

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

Log Message:
tests/lint: add more examples for 'extra bits set to 0'

Seen in sys/external/bsd/compiler_rt/dist/lib/builtins/fp_lib.h:88.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_309.c

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.5 src/tests/usr.bin/xlint/lint1/msg_309.c:1.6
--- src/tests/usr.bin/xlint/lint1/msg_309.c:1.5	Fri Jun 17 06:59:16 2022
+++ src/tests/usr.bin/xlint/lint1/msg_309.c	Sat Oct 15 21:19:15 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_309.c,v 1.5 2022/06/17 06:59:16 rillig Exp $	*/
+/*	$NetBSD: msg_309.c,v 1.6 2022/10/15 21:19:15 rillig Exp $	*/
 # 3 "msg_309.c"
 
 // Test for message: extra bits set to 0 in conversion of '%s' to '%s', op '%s' [309]
@@ -25,6 +25,33 @@ scale(unsigned long long x) {
 		return 16;
 
 	/*
+	 * The integer constant is explicitly unsigned.  Even in this case,
+	 * the code may have originated on a platform where 'x' had 32 bits
+	 * originally, and the intention may have been to clear the lower 16
+	 * bits.
+	 */
+	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
+	if ((x & 0xffff0000U) != 0)
+		return 16;
+
+	/*
+	 * Even if the expression is written as '& ~', which makes the
+	 * intention of clearing the lower 16 bits clear, on a 32-bit
+	 * platform the integer constant stays at 32 bits, and when porting
+	 * the code to a 64-bit platform, the upper 32 bits are preserved.
+	 */
+	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
+	if ((x & ~0xffffU) != 0)
+		return 16;
+
+	/*
+	 * Casting the integer constant to the proper type removes all
+	 * ambiguities about the programmer's intention.
+	 */
+	if ((x & (unsigned long long)~0xffffU) != 0)
+		return 16;
+
+	/*
 	 * In the remaining cases, the constant does not have its most
 	 * significant bit set, therefore there is no ambiguity.
 	 */

Reply via email to