xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_309.c (revision ec42194bb18c332affeb5a21a15291e1e8a43bb0)
1 /*	$NetBSD: msg_309.c,v 1.5 2022/06/17 06:59:16 rillig Exp $	*/
2 # 3 "msg_309.c"
3 
4 // Test for message: extra bits set to 0 in conversion of '%s' to '%s', op '%s' [309]
5 
6 int
7 scale(unsigned long long x) {
8 
9 	/*
10 	 * Both operands of '&' have the same type, therefore no conversion
11 	 * is necessary and no bits can get lost.
12 	 */
13 	if ((x & 0xffffffff00000000ULL) != 0)
14 		return 32;
15 
16 	/*
17 	 * The constant has type 'unsigned 32-bit'.  The usual arithmetic
18 	 * conversions of '&' convert this constant to unsigned 64-bit.
19 	 * The programmer may or may not have intended to sign-extend the
20 	 * bit mask here.  This situation may occur during migration from a
21 	 * 32-bit to a 64-bit platform.
22 	 */
23 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
24 	if ((x & 0xffff0000) != 0)
25 		return 16;
26 
27 	/*
28 	 * In the remaining cases, the constant does not have its most
29 	 * significant bit set, therefore there is no ambiguity.
30 	 */
31 	if ((x & 0xff00) != 0)
32 		return 8;
33 	if ((x & 0xf0) != 0)
34 		return 4;
35 	if ((x & 0xc) != 0)
36 		return 2;
37 	if ((x & 0x2) != 0)
38 		return 1;
39 	return (int)(x & 0x1);
40 }
41