Home | History | Annotate | Line # | Download | only in lint1
msg_267.c revision 1.6
      1  1.6  rillig /*	$NetBSD: msg_267.c,v 1.6 2022/08/19 19:40:39 rillig Exp $	*/
      2  1.1  rillig # 3 "msg_267.c"
      3  1.1  rillig 
      4  1.6  rillig // Test for message: shift amount %u equals bit-size of '%s' [267]
      5  1.1  rillig 
      6  1.3  rillig int
      7  1.3  rillig shr32(unsigned int x)
      8  1.3  rillig {
      9  1.6  rillig 	/* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
     10  1.4  rillig 	return x >> 32;
     11  1.3  rillig }
     12  1.3  rillig 
     13  1.3  rillig int
     14  1.3  rillig shl32(unsigned int x)
     15  1.3  rillig {
     16  1.6  rillig 	/* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
     17  1.4  rillig 	return x << 32;
     18  1.3  rillig }
     19  1.5  rillig 
     20  1.5  rillig /*
     21  1.5  rillig  * As of 2022-08-19, lint ignores the GCC-specific 'mode' attribute, treating
     22  1.5  rillig  * the tetra-int as a plain single-int, thus having width 32.
     23  1.5  rillig  *
     24  1.5  rillig  * https://gcc.gnu.org/onlinedocs/gccint/Machine-Modes.html
     25  1.5  rillig  */
     26  1.5  rillig unsigned
     27  1.5  rillig function(unsigned __attribute__((mode(TI))) arg)
     28  1.5  rillig {
     29  1.6  rillig 	/* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
     30  1.5  rillig 	return (arg >> 32) & 3;
     31  1.5  rillig }
     32  1.6  rillig 
     33  1.6  rillig unsigned
     34  1.6  rillig shift_bit_field(void)
     35  1.6  rillig {
     36  1.6  rillig 	struct {
     37  1.6  rillig 		unsigned bit_field:18;
     38  1.6  rillig 	} s = { 12345 };
     39  1.6  rillig 
     40  1.6  rillig 	/*
     41  1.6  rillig 	 * A warning may be useful here for '>>' with a shift amount >= 18.
     42  1.6  rillig 	 *
     43  1.6  rillig 	 * For '<<' and bit-size <= 31, a warning only makes sense for shift
     44  1.6  rillig 	 * amounts >= 31, as it is legitimate to rely on the default integer
     45  1.6  rillig 	 * promotions of the left-hand operand. The default integer promotion
     46  1.6  rillig 	 * turns the type into 'int', not 'unsigned int', therefore the 31.
     47  1.6  rillig 	 * Using the same warning text would be confusing though.
     48  1.6  rillig 	 *
     49  1.6  rillig 	 * For '<<' and bit-size == 32, the standard case applies.
     50  1.6  rillig 	 *
     51  1.6  rillig 	 * As of 2022-08-19, Clang-tidy doesn't warn about any of these.
     52  1.6  rillig 	 */
     53  1.6  rillig 	return
     54  1.6  rillig 	    (s.bit_field >> 17) &
     55  1.6  rillig 	    (s.bit_field >> 18) &
     56  1.6  rillig 	    (s.bit_field >> 19) &
     57  1.6  rillig 	    (s.bit_field >> 31) &
     58  1.6  rillig 	    /* XXX: Why 'int:18', not 'unsigned int:18'? */
     59  1.6  rillig 	    /* expect+1: warning: shift amount 32 equals bit-size of 'int:18' [267] */
     60  1.6  rillig 	    (s.bit_field >> 32) &
     61  1.6  rillig 	    /* XXX: Why 'int', not 'unsigned int:18'? */
     62  1.6  rillig 	    /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
     63  1.6  rillig 	    (s.bit_field >> 33) &
     64  1.6  rillig 	    (s.bit_field << 17) &
     65  1.6  rillig 	    (s.bit_field << 18) &
     66  1.6  rillig 	    (s.bit_field << 19) &
     67  1.6  rillig 	    (s.bit_field << 31) &
     68  1.6  rillig 	    /* XXX: Why 'int:18', not 'unsigned int:18'? */
     69  1.6  rillig 	    /* expect+1: warning: shift amount 32 equals bit-size of 'int:18' [267] */
     70  1.6  rillig 	    (s.bit_field << 32) &
     71  1.6  rillig 	    /* XXX: Why 'int', not 'unsigned int:18'? */
     72  1.6  rillig 	    /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
     73  1.6  rillig 	    (s.bit_field << 33) &
     74  1.6  rillig 	    15;
     75  1.6  rillig }
     76