Home | History | Annotate | Line # | Download | only in lint1
msg_267.c revision 1.8.2.1
      1  1.8.2.1  perseant /*	$NetBSD: msg_267.c,v 1.8.2.1 2025/08/02 05:58:17 perseant 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.7    rillig /* lint1-extra-flags: -X 351 */
      7      1.7    rillig 
      8      1.3    rillig int
      9      1.3    rillig shr32(unsigned int x)
     10      1.3    rillig {
     11      1.6    rillig 	/* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
     12      1.4    rillig 	return x >> 32;
     13      1.3    rillig }
     14      1.3    rillig 
     15      1.3    rillig int
     16      1.3    rillig shl32(unsigned int x)
     17      1.3    rillig {
     18      1.6    rillig 	/* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
     19      1.4    rillig 	return x << 32;
     20      1.3    rillig }
     21      1.5    rillig 
     22      1.5    rillig /*
     23      1.5    rillig  * https://gcc.gnu.org/onlinedocs/gccint/Machine-Modes.html
     24      1.5    rillig  */
     25      1.5    rillig unsigned
     26      1.5    rillig function(unsigned __attribute__((mode(TI))) arg)
     27      1.5    rillig {
     28      1.5    rillig 	return (arg >> 32) & 3;
     29      1.5    rillig }
     30      1.6    rillig 
     31      1.6    rillig unsigned
     32      1.6    rillig shift_bit_field(void)
     33      1.6    rillig {
     34      1.6    rillig 	struct {
     35      1.6    rillig 		unsigned bit_field:18;
     36      1.6    rillig 	} s = { 12345 };
     37      1.6    rillig 
     38      1.6    rillig 	/*
     39      1.6    rillig 	 * A warning may be useful here for '>>' with a shift amount >= 18.
     40      1.6    rillig 	 *
     41      1.6    rillig 	 * For '<<' and bit-size <= 31, a warning only makes sense for shift
     42      1.6    rillig 	 * amounts >= 31, as it is legitimate to rely on the default integer
     43      1.6    rillig 	 * promotions of the left-hand operand. The default integer promotion
     44      1.6    rillig 	 * turns the type into 'int', not 'unsigned int', therefore the 31.
     45      1.6    rillig 	 * Using the same warning text would be confusing though.
     46      1.6    rillig 	 *
     47      1.6    rillig 	 * For '<<' and bit-size == 32, the standard case applies.
     48      1.6    rillig 	 *
     49      1.6    rillig 	 * As of 2022-08-19, Clang-tidy doesn't warn about any of these.
     50      1.6    rillig 	 */
     51      1.6    rillig 	return
     52      1.6    rillig 	    (s.bit_field >> 17) &
     53      1.6    rillig 	    (s.bit_field >> 18) &
     54      1.6    rillig 	    (s.bit_field >> 19) &
     55      1.6    rillig 	    (s.bit_field >> 31) &
     56      1.8    rillig 	    // When promoting 'unsigned int:18', the target type is 'int', as
     57      1.8    rillig 	    // it can represent all possible values; this is a bit misleading
     58      1.8    rillig 	    // as its sign bit is always 0.
     59      1.8    rillig 	    /* expect+1: warning: shift amount 32 equals bit-size of 'int:19' [267] */
     60      1.6    rillig 	    (s.bit_field >> 32) &
     61      1.8    rillig 	    // When promoting 'unsigned int:18', the target type is 'int', as
     62      1.8    rillig 	    // it can represent all possible values; this is a bit misleading
     63      1.8    rillig 	    // as its sign bit is always 0.
     64      1.6    rillig 	    /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
     65      1.6    rillig 	    (s.bit_field >> 33) &
     66      1.6    rillig 	    (s.bit_field << 17) &
     67      1.6    rillig 	    (s.bit_field << 18) &
     68      1.6    rillig 	    (s.bit_field << 19) &
     69      1.6    rillig 	    (s.bit_field << 31) &
     70      1.8    rillig 	    // When promoting 'unsigned int:18', the target type is 'int', as
     71      1.8    rillig 	    // it can represent all possible values; this is a bit misleading
     72      1.8    rillig 	    // as its sign bit is always 0.
     73      1.8    rillig 	    /* expect+1: warning: shift amount 32 equals bit-size of 'int:19' [267] */
     74      1.6    rillig 	    (s.bit_field << 32) &
     75      1.8    rillig 	    // When promoting 'unsigned int:18', the target type is 'int', as
     76      1.8    rillig 	    // it can represent all possible values; this is a bit misleading
     77      1.8    rillig 	    // as its sign bit is always 0.
     78      1.6    rillig 	    /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
     79      1.6    rillig 	    (s.bit_field << 33) &
     80      1.6    rillig 	    15;
     81      1.6    rillig }
     82