summaryrefslogtreecommitdiff
path: root/tests/usr.bin/xlint/lint1/msg_169.c
blob: 98f4b816ce35458cf010bf89fa676ae13e068579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*	$NetBSD: msg_169.c,v 1.8 2023/07/07 06:03:31 rillig Exp $	*/
# 3 "msg_169.c"

// Test for message: precedence confusion possible: parenthesize! [169]

/* lint1-flags: -g -h -S -w -X 191,351 */

typedef _Bool bool;

void
confusing_shift_arith(unsigned a, unsigned b, unsigned c, unsigned char ch)
{
	unsigned con, okl, okr;

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a + b << c;
	okl = (a + b) << c;
	okr = a + (b << c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a << b + c;
	okl = (a << b) + c;
	okr = a << (b + c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a - b >> c;
	okl = (a - b) >> c;
	okr = a - (b >> c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a >> b - c;
	okl = (a >> b) - c;
	okr = a >> (b - c);

	// Parenthesizing the inner operands has no effect on the warning.
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = (a) + b << c;
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a + (b) << c;
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a + b << (c);

	// The usual arithmetic promotions have no effect on the warning.
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = ch + b << c;
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a + ch << c;
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a + b << ch;
}

void
confusing_logical(bool a, bool b, bool c)
{
	bool con, okl, okr, eql;

	eql = a && b && c;
	eql = a || b || c;

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a && b || c;
	okl = (a && b) || c;
	okr = a && (b || c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a || b && c;
	okl = (a || b) && c;
	okr = a || (b && c);
}

void
confusing_bitwise(unsigned a, unsigned b, unsigned c)
{
	bool con, okl, okr, eql;

	eql = a & b & c;
	eql = a | b | c;
	eql = a ^ b ^ c;

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a | b ^ c;
	okl = (a | b) ^ c;
	okr = a | (b ^ c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a | b & c;
	okl = (a | b) & c;
	okr = a | (b & c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a ^ b | c;
	okl = (a ^ b) | c;
	okr = a ^ (b | c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a ^ b & c;
	okl = (a ^ b) & c;
	okr = a ^ (b & c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a & b | c;
	okl = (a & b) ^ c;
	okr = a & (b ^ c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a & b ^ c;
	okl = (a & b) ^ c;
	okr = a & (b ^ c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a & b + c;
	okl = (a & b) + c;
	okr = a & (b + c);

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a - b | c;
	okl = (a - b) | c;
	okr = a - (b | c);

	// This looks like a binomial formula but isn't.
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = a ^ 2 - 2 * a * b + b ^ 2;

	// This isn't a binomial formula either since '^' means xor.
	con = (a ^ 2) - 2 * a * b + (b ^ 2);
}

void
constant_expressions(void)
{
	unsigned con;

	// The check for confusing precedence happens after constant folding.
	// Therefore the following lines do not generate warnings.
	con = 1 & 2 | 3;
	con = 4 << 5 + 6;
	con = 7 ^ 8 & 9;
}

void
cast_expressions(char a, char b, char c)
{
	unsigned con;

	// Adding casts to the leaf nodes doesn't change anything about the
	// confusing precedence.
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = (unsigned)a | (unsigned)b & (unsigned)c;
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = (unsigned)a & (unsigned)b | (unsigned)c;

	// Adding a cast around the whole calculation doesn't change the
	// precedence as well.
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	con = (unsigned)(a | b & c);

	// Adding a cast around an intermediate result groups the operands
	// of the main node, which prevents any confusion about precedence.
	con = (unsigned)a | (unsigned)(b & c);
	con = a | (unsigned)(b & c);
	con = (unsigned)(a | b) & (unsigned)c;
	con = (unsigned)(a | b) & c;
}

void
expected_precedence(int a, int b, int c)
{
	int ok;

	ok = a + b * c;
}

void
implicit_conversion_to_long(long la, int a)
{
	int ok;

	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	ok = a & a | la;

	/*
	 * Before tree.c 1.132 from 2021-01-04, there was a typo in
	 * check_precedence_confusion that prevented the right-hand operand
	 * from being flagged as possibly confusing if there was an implicit
	 * conversion or an explicit cast between the main operator ('|') and
	 * the nested operator ('&').
	 */
	/* expect+1: warning: precedence confusion possible: parenthesize! [169] */
	ok = la | a & a;

	ok = (a & a) | la;	/* always ok */
	ok = la | (a & a);	/* always ok */

	/*
	 * Before tree.c 1.132, this expression didn't generate a warning
	 * because the right-hand operand was CVT, and there is no confusing
	 * precedence between BITOR and CVT.
	 *
	 * Since tree.c 1.132, this expression doesn't generate a warning
	 * because the right-hand operand is parenthesized.  There is no way
	 * to have the right operand casted and at the same time not
	 * parenthesized since the cast operator has higher precedence.
	 *
	 * In summary, there is no visible change, but the implementation is
	 * now works as intended.
	 */
	ok = la | (int)(a & a);	/* always ok */
}