xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_267.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: msg_267.c,v 1.7 2023/07/07 19:45:22 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  * 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
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
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 	    /* XXX: Why 'int:18', not 'unsigned int:18'? */
61 	    /* expect+1: warning: shift amount 32 equals bit-size of 'int:18' [267] */
62 	    (s.bit_field >> 32) &
63 	    /* XXX: Why 'int', not 'unsigned int:18'? */
64 	    /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [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 	    /* XXX: Why 'int:18', not 'unsigned int:18'? */
71 	    /* expect+1: warning: shift amount 32 equals bit-size of 'int:18' [267] */
72 	    (s.bit_field << 32) &
73 	    /* XXX: Why 'int', not 'unsigned int:18'? */
74 	    /* expect+1: warning: shift amount 33 is greater than bit-size 32 of 'int' [122] */
75 	    (s.bit_field << 33) &
76 	    15;
77 }
78