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