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