msg_267.c revision 1.8 1 1.8 rillig /* $NetBSD: msg_267.c,v 1.8 2024/03/12 07:56:08 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.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 * As of 2022-08-19, lint ignores the GCC-specific 'mode' attribute, treating
24 1.5 rillig * the tetra-int as a plain single-int, thus having width 32.
25 1.5 rillig *
26 1.5 rillig * https://gcc.gnu.org/onlinedocs/gccint/Machine-Modes.html
27 1.5 rillig */
28 1.5 rillig unsigned
29 1.5 rillig function(unsigned __attribute__((mode(TI))) arg)
30 1.5 rillig {
31 1.6 rillig /* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
32 1.5 rillig return (arg >> 32) & 3;
33 1.5 rillig }
34 1.6 rillig
35 1.6 rillig unsigned
36 1.6 rillig shift_bit_field(void)
37 1.6 rillig {
38 1.6 rillig struct {
39 1.6 rillig unsigned bit_field:18;
40 1.6 rillig } s = { 12345 };
41 1.6 rillig
42 1.6 rillig /*
43 1.6 rillig * A warning may be useful here for '>>' with a shift amount >= 18.
44 1.6 rillig *
45 1.6 rillig * For '<<' and bit-size <= 31, a warning only makes sense for shift
46 1.6 rillig * amounts >= 31, as it is legitimate to rely on the default integer
47 1.6 rillig * promotions of the left-hand operand. The default integer promotion
48 1.6 rillig * turns the type into 'int', not 'unsigned int', therefore the 31.
49 1.6 rillig * Using the same warning text would be confusing though.
50 1.6 rillig *
51 1.6 rillig * For '<<' and bit-size == 32, the standard case applies.
52 1.6 rillig *
53 1.6 rillig * As of 2022-08-19, Clang-tidy doesn't warn about any of these.
54 1.6 rillig */
55 1.6 rillig return
56 1.6 rillig (s.bit_field >> 17) &
57 1.6 rillig (s.bit_field >> 18) &
58 1.6 rillig (s.bit_field >> 19) &
59 1.6 rillig (s.bit_field >> 31) &
60 1.8 rillig // When promoting 'unsigned int:18', the target type is 'int', as
61 1.8 rillig // it can represent all possible values; this is a bit misleading
62 1.8 rillig // as its sign bit is always 0.
63 1.8 rillig /* expect+1: warning: shift amount 32 equals bit-size of 'int:19' [267] */
64 1.6 rillig (s.bit_field >> 32) &
65 1.8 rillig // When promoting 'unsigned int:18', the target type is 'int', as
66 1.8 rillig // it can represent all possible values; this is a bit misleading
67 1.8 rillig // as its sign bit is always 0.
68 1.6 rillig /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
69 1.6 rillig (s.bit_field >> 33) &
70 1.6 rillig (s.bit_field << 17) &
71 1.6 rillig (s.bit_field << 18) &
72 1.6 rillig (s.bit_field << 19) &
73 1.6 rillig (s.bit_field << 31) &
74 1.8 rillig // When promoting 'unsigned int:18', the target type is 'int', as
75 1.8 rillig // it can represent all possible values; this is a bit misleading
76 1.8 rillig // as its sign bit is always 0.
77 1.8 rillig /* expect+1: warning: shift amount 32 equals bit-size of 'int:19' [267] */
78 1.6 rillig (s.bit_field << 32) &
79 1.8 rillig // When promoting 'unsigned int:18', the target type is 'int', as
80 1.8 rillig // it can represent all possible values; this is a bit misleading
81 1.8 rillig // as its sign bit is always 0.
82 1.6 rillig /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
83 1.6 rillig (s.bit_field << 33) &
84 1.6 rillig 15;
85 1.6 rillig }
86