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