Home | History | Annotate | Line # | Download | only in lint1
msg_309.c revision 1.6
      1  1.6  rillig /*	$NetBSD: msg_309.c,v 1.6 2022/10/15 21:19:15 rillig Exp $	*/
      2  1.1  rillig # 3 "msg_309.c"
      3  1.1  rillig 
      4  1.4  rillig // Test for message: extra bits set to 0 in conversion of '%s' to '%s', op '%s' [309]
      5  1.1  rillig 
      6  1.3  rillig int
      7  1.3  rillig scale(unsigned long long x) {
      8  1.3  rillig 
      9  1.3  rillig 	/*
     10  1.3  rillig 	 * Both operands of '&' have the same type, therefore no conversion
     11  1.3  rillig 	 * is necessary and no bits can get lost.
     12  1.3  rillig 	 */
     13  1.3  rillig 	if ((x & 0xffffffff00000000ULL) != 0)
     14  1.3  rillig 		return 32;
     15  1.3  rillig 
     16  1.3  rillig 	/*
     17  1.3  rillig 	 * The constant has type 'unsigned 32-bit'.  The usual arithmetic
     18  1.3  rillig 	 * conversions of '&' convert this constant to unsigned 64-bit.
     19  1.3  rillig 	 * The programmer may or may not have intended to sign-extend the
     20  1.3  rillig 	 * bit mask here.  This situation may occur during migration from a
     21  1.3  rillig 	 * 32-bit to a 64-bit platform.
     22  1.3  rillig 	 */
     23  1.5  rillig 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
     24  1.5  rillig 	if ((x & 0xffff0000) != 0)
     25  1.3  rillig 		return 16;
     26  1.3  rillig 
     27  1.3  rillig 	/*
     28  1.6  rillig 	 * The integer constant is explicitly unsigned.  Even in this case,
     29  1.6  rillig 	 * the code may have originated on a platform where 'x' had 32 bits
     30  1.6  rillig 	 * originally, and the intention may have been to clear the lower 16
     31  1.6  rillig 	 * bits.
     32  1.6  rillig 	 */
     33  1.6  rillig 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
     34  1.6  rillig 	if ((x & 0xffff0000U) != 0)
     35  1.6  rillig 		return 16;
     36  1.6  rillig 
     37  1.6  rillig 	/*
     38  1.6  rillig 	 * Even if the expression is written as '& ~', which makes the
     39  1.6  rillig 	 * intention of clearing the lower 16 bits clear, on a 32-bit
     40  1.6  rillig 	 * platform the integer constant stays at 32 bits, and when porting
     41  1.6  rillig 	 * the code to a 64-bit platform, the upper 32 bits are preserved.
     42  1.6  rillig 	 */
     43  1.6  rillig 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
     44  1.6  rillig 	if ((x & ~0xffffU) != 0)
     45  1.6  rillig 		return 16;
     46  1.6  rillig 
     47  1.6  rillig 	/*
     48  1.6  rillig 	 * Casting the integer constant to the proper type removes all
     49  1.6  rillig 	 * ambiguities about the programmer's intention.
     50  1.6  rillig 	 */
     51  1.6  rillig 	if ((x & (unsigned long long)~0xffffU) != 0)
     52  1.6  rillig 		return 16;
     53  1.6  rillig 
     54  1.6  rillig 	/*
     55  1.3  rillig 	 * In the remaining cases, the constant does not have its most
     56  1.3  rillig 	 * significant bit set, therefore there is no ambiguity.
     57  1.3  rillig 	 */
     58  1.3  rillig 	if ((x & 0xff00) != 0)
     59  1.3  rillig 		return 8;
     60  1.3  rillig 	if ((x & 0xf0) != 0)
     61  1.3  rillig 		return 4;
     62  1.3  rillig 	if ((x & 0xc) != 0)
     63  1.3  rillig 		return 2;
     64  1.3  rillig 	if ((x & 0x2) != 0)
     65  1.3  rillig 		return 1;
     66  1.3  rillig 	return (int)(x & 0x1);
     67  1.3  rillig }
     68