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