d_c99_bool_strict.c revision 1.51 1 1.51 rillig /* $NetBSD: d_c99_bool_strict.c,v 1.51 2024/11/13 04:32:49 rillig Exp $ */
2 1.1 rillig # 3 "d_c99_bool_strict.c"
3 1.1 rillig
4 1.1 rillig /*
5 1.4 rillig * The option -T treats _Bool as incompatible with all other scalar types.
6 1.8 rillig * This is implemented by the following rules:
7 1.1 rillig *
8 1.8 rillig * strict-bool-typedef:
9 1.8 rillig * The type _Bool is compatible with any typedef of _Bool.
10 1.1 rillig *
11 1.8 rillig * Note: Since <stdbool.h> defines bool as textual alias of _Bool,
12 1.8 rillig * having another typedef for bool is unusual.
13 1.1 rillig *
14 1.8 rillig * strict-bool-constant:
15 1.8 rillig * There are 2 bool constants named false and true.
16 1.8 rillig * No other constants are compatible with type _Bool.
17 1.1 rillig *
18 1.8 rillig * Note: Internally these constants are named __lint_false and
19 1.8 rillig * __lint_true.
20 1.1 rillig *
21 1.8 rillig * strict-bool-bit-field:
22 1.8 rillig * A struct or union member that is a bit field with underlying type
23 1.8 rillig * bool is compatible with plain bool.
24 1.1 rillig *
25 1.8 rillig * strict-bool-conversion:
26 1.8 rillig * There is no implicit conversion between _Bool and any other type.
27 1.3 rillig *
28 1.8 rillig * strict-bool-controlling-expression:
29 1.8 rillig * Controlling expressions in 'if', 'while', 'for', '?:' must be of
30 1.49 rillig * type bool, except for a literal 0 in a do-while loop.
31 1.3 rillig *
32 1.8 rillig * strict-bool-operand-unary:
33 1.8 rillig * Operator bool? scalar?
34 1.25 rillig * ! yes -
35 1.25 rillig * & yes yes
36 1.25 rillig * The other unary operators do not accept bool operands.
37 1.8 rillig *
38 1.8 rillig * strict-bool-operand-binary:
39 1.8 rillig * Operator left: bool? other? right: bool? other?
40 1.8 rillig * . - yes yes yes
41 1.8 rillig * -> - yes yes yes
42 1.11 rillig * <=, <, >=, > - yes - yes
43 1.8 rillig * ==, != yes yes yes yes
44 1.8 rillig * & yes yes yes yes
45 1.8 rillig * ^ yes yes yes yes
46 1.8 rillig * | yes yes yes yes
47 1.8 rillig * && yes - yes -
48 1.8 rillig * || yes - yes -
49 1.8 rillig * ? yes - yes yes
50 1.8 rillig * : yes yes yes yes
51 1.8 rillig * = yes yes yes yes
52 1.8 rillig * &=, ^=, |= yes yes yes yes
53 1.8 rillig * , yes yes yes yes
54 1.8 rillig * The other binary operators do not accept bool operands.
55 1.8 rillig *
56 1.8 rillig * strict-bool-operator-result:
57 1.8 rillig * The result type of the operators '!', '<', '<=', '>', '>=',
58 1.8 rillig * '==', '!=', '&&', '||' is _Bool instead of int.
59 1.8 rillig *
60 1.8 rillig * strict-bool-bitwise-and:
61 1.8 rillig * Expressions of the form "flags & FLAG" are compatible with _Bool if
62 1.48 rillig * the resulting value is used in a context where it is implicitly and
63 1.48 rillig * immediately compared to zero.
64 1.8 rillig *
65 1.8 rillig * Note: Examples for such contexts are controlling expressions or the
66 1.8 rillig * operands of the operators '!', '&&', '||'.
67 1.8 rillig *
68 1.49 rillig * Note: Counterexamples for contexts are assignments to a bool variable,
69 1.49 rillig * as without the conversion from C99 6.3.1.2, converting an integer to a
70 1.49 rillig * "bool-like" integer type truncated the value instead of comparing it
71 1.49 rillig * to 0.
72 1.49 rillig *
73 1.49 rillig * Note: These rules ensure that conforming code behaves the same in both
74 1.49 rillig * C99 and in environments that emulate a boolean type using a small
75 1.49 rillig * integer type.
76 1.8 rillig */
77 1.8 rillig
78 1.8 rillig /*
79 1.8 rillig * The header <stdbool.h> defines the macros bool = _Bool, false = 0 and
80 1.8 rillig * true = 1. Without further hacks, this would mean that constant expressions
81 1.8 rillig * of integer type have to be regarded as possible boolean constants if their
82 1.8 rillig * value is either 0 or 1.
83 1.8 rillig *
84 1.8 rillig * This would not help in migrating old code to use bool consistently.
85 1.8 rillig * Therefore lint provides its own <stdbool.h> header that expands false to
86 1.8 rillig * __lint_false and true to __lint_true, two predefined constant expressions.
87 1.1 rillig */
88 1.1 rillig
89 1.40 rillig /* lint1-extra-flags: -hT -X 351 */
90 1.1 rillig
91 1.1 rillig /*
92 1.8 rillig * strict-bool-typedef
93 1.1 rillig */
94 1.1 rillig
95 1.1 rillig /*
96 1.1 rillig * Using a typedef for bool does not hurt the checks, they all use the
97 1.1 rillig * underlying basic type (see tspec_t), which is BOOL.
98 1.1 rillig */
99 1.1 rillig typedef _Bool bool;
100 1.1 rillig
101 1.8 rillig extern void accept_bool(bool);
102 1.8 rillig extern void println(const char *);
103 1.8 rillig extern void take_arguments(bool, int, const char *, ...);
104 1.8 rillig extern void do_nothing(void);
105 1.8 rillig
106 1.8 rillig /*
107 1.8 rillig * strict-bool-constant
108 1.8 rillig */
109 1.8 rillig
110 1.8 rillig void
111 1.8 rillig strict_bool_constant(void)
112 1.8 rillig {
113 1.8 rillig accept_bool(__lint_false);
114 1.8 rillig accept_bool(__lint_true);
115 1.44 rillig /* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
116 1.36 rillig accept_bool(0);
117 1.44 rillig /* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
118 1.36 rillig accept_bool(1);
119 1.44 rillig /* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
120 1.36 rillig accept_bool(2);
121 1.8 rillig }
122 1.8 rillig
123 1.8 rillig enum strict_bool_constant_expressions {
124 1.8 rillig /* Ok: __lint_false is a boolean constant expression. */
125 1.36 rillig /* expect+1: warning: constant in conditional context [161] */
126 1.36 rillig FALSE = __lint_false ? 100 : 101,
127 1.8 rillig
128 1.8 rillig /* Ok: __lint_true is a boolean constant expression. */
129 1.36 rillig /* expect+1: warning: constant in conditional context [161] */
130 1.36 rillig TRUE = __lint_true ? 100 : 101,
131 1.8 rillig
132 1.8 rillig /* Not ok: an integer is not a boolean constant expression. */
133 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
134 1.36 rillig INT0 = 0 ? 100 : 101,
135 1.8 rillig
136 1.8 rillig /* Not ok: an integer is not a boolean constant expression. */
137 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
138 1.36 rillig INT1 = 1 ? 100 : 101,
139 1.8 rillig
140 1.8 rillig /* Not ok: 2 is not a boolean constant. */
141 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
142 1.36 rillig INT2 = 2 ? 100 : 101,
143 1.8 rillig
144 1.8 rillig /* Not ok: compound integer expressions are not bool. */
145 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
146 1.36 rillig ARITH = (2 - 2) ? 100 : 101,
147 1.8 rillig
148 1.8 rillig /*
149 1.8 rillig * Without strict bool mode, these two variants of an expression can
150 1.8 rillig * occur when a preprocessor macro is either defined to 1 or left
151 1.45 rillig * empty (since C99).
152 1.8 rillig *
153 1.27 rillig * In strict bool mode, the resulting expression can be compared
154 1.27 rillig * against 0 to achieve the same effect (so +0 != 0 or 1 + 0 != 0).
155 1.8 rillig */
156 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
157 1.36 rillig BINARY_PLUS = (1 + 0) ? 100 : 101,
158 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
159 1.36 rillig UNARY_PLUS = (+0) ? 100 : 101,
160 1.8 rillig
161 1.8 rillig /* The main operator '>' has return type bool. */
162 1.36 rillig /* expect+1: warning: constant in conditional context [161] */
163 1.36 rillig Q1 = (13 > 12) ? 100 : 101,
164 1.8 rillig
165 1.8 rillig /*
166 1.8 rillig * The parenthesized expression has type int and thus cannot be
167 1.8 rillig * used as the controlling expression in the '?:' operator.
168 1.8 rillig */
169 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
170 1.36 rillig /* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
171 1.36 rillig Q2 = (13 > 12 ? 1 : 7) ? 100 : 101,
172 1.8 rillig
173 1.36 rillig /* expect+1: error: integral constant expression expected [55] */
174 1.36 rillig BINAND_BOOL = __lint_false & __lint_true,
175 1.8 rillig BINAND_INT = 0 & 1,
176 1.8 rillig
177 1.36 rillig /* expect+1: error: integral constant expression expected [55] */
178 1.36 rillig BINXOR_BOOL = __lint_false ^ __lint_true,
179 1.8 rillig BINXOR_INT = 0 ^ 1,
180 1.8 rillig
181 1.36 rillig /* expect+1: error: integral constant expression expected [55] */
182 1.36 rillig BINOR_BOOL = __lint_false | __lint_true,
183 1.8 rillig BINOR_INT = 0 | 1,
184 1.8 rillig
185 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
186 1.36 rillig /* expect+1: error: integral constant expression expected [55] */
187 1.36 rillig LOGOR_BOOL = __lint_false || __lint_true,
188 1.36 rillig /* expect+2: error: left operand of '||' must be bool, not 'int' [331] */
189 1.36 rillig /* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
190 1.36 rillig LOGOR_INT = 0 || 1,
191 1.36 rillig
192 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
193 1.36 rillig /* expect+1: error: integral constant expression expected [55] */
194 1.36 rillig LOGAND_BOOL = __lint_false && __lint_true,
195 1.36 rillig /* expect+2: error: left operand of '&&' must be bool, not 'int' [331] */
196 1.36 rillig /* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
197 1.36 rillig LOGAND_INT = 0 && 1,
198 1.8 rillig };
199 1.8 rillig
200 1.8 rillig /*
201 1.8 rillig * strict-bool-bit-fields
202 1.8 rillig */
203 1.8 rillig
204 1.8 rillig void
205 1.8 rillig strict_bool_bit_fields(void)
206 1.8 rillig {
207 1.8 rillig struct flags {
208 1.8 rillig bool bool_flag: 1;
209 1.8 rillig unsigned uint_flag: 1;
210 1.8 rillig };
211 1.8 rillig
212 1.8 rillig struct flags flags = { __lint_false, 0 };
213 1.8 rillig struct flags *flags_ptr = &flags;
214 1.8 rillig bool b;
215 1.8 rillig
216 1.8 rillig b = flags.bool_flag;
217 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'unsigned int' [107] */
218 1.36 rillig b = flags.uint_flag;
219 1.8 rillig flags.bool_flag = b;
220 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'unsigned int' and '_Bool' [107] */
221 1.36 rillig flags.uint_flag = b;
222 1.8 rillig
223 1.8 rillig b = flags_ptr->bool_flag;
224 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'unsigned int' [107] */
225 1.36 rillig b = flags_ptr->uint_flag;
226 1.8 rillig flags_ptr->bool_flag = b;
227 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'unsigned int' and '_Bool' [107] */
228 1.36 rillig flags_ptr->uint_flag = b;
229 1.8 rillig }
230 1.8 rillig
231 1.8 rillig void
232 1.8 rillig strict_bool_bit_fields_operand_conversion(void)
233 1.8 rillig {
234 1.8 rillig struct s {
235 1.8 rillig bool ordinary;
236 1.8 rillig bool bit_field: 1;
237 1.8 rillig };
238 1.8 rillig
239 1.24 rillig struct s s = { 0 > 0 };
240 1.8 rillig
241 1.8 rillig s.ordinary = s.ordinary | s.ordinary;
242 1.15 rillig s.bit_field = s.bit_field | s.bit_field;
243 1.8 rillig }
244 1.8 rillig
245 1.8 rillig /*
246 1.8 rillig * strict-bool-conversion
247 1.8 rillig */
248 1.8 rillig
249 1.8 rillig bool
250 1.8 rillig strict_bool_conversion_return_false(void)
251 1.8 rillig {
252 1.8 rillig return __lint_false;
253 1.8 rillig }
254 1.8 rillig
255 1.8 rillig bool
256 1.8 rillig strict_bool_conversion_return_true(void)
257 1.8 rillig {
258 1.8 rillig return __lint_true;
259 1.8 rillig }
260 1.8 rillig
261 1.8 rillig bool
262 1.8 rillig strict_bool_conversion_return_bool(bool b)
263 1.8 rillig {
264 1.8 rillig return b;
265 1.8 rillig }
266 1.8 rillig
267 1.8 rillig bool
268 1.8 rillig strict_bool_conversion_return_0(void)
269 1.8 rillig {
270 1.39 rillig /* expect+1: error: function has return type '_Bool' but returns 'int' [211] */
271 1.36 rillig return 0;
272 1.8 rillig }
273 1.8 rillig
274 1.8 rillig bool
275 1.8 rillig strict_bool_conversion_return_1(void)
276 1.8 rillig {
277 1.39 rillig /* expect+1: error: function has return type '_Bool' but returns 'int' [211] */
278 1.36 rillig return 1;
279 1.8 rillig }
280 1.8 rillig
281 1.8 rillig bool
282 1.8 rillig strict_bool_conversion_return_2(void)
283 1.8 rillig {
284 1.39 rillig /* expect+1: error: function has return type '_Bool' but returns 'int' [211] */
285 1.36 rillig return 2;
286 1.8 rillig }
287 1.8 rillig
288 1.43 rillig /* expect+2: warning: parameter 'p' unused in function 'strict_bool_conversion_return_pointer' [231] */
289 1.8 rillig bool
290 1.36 rillig strict_bool_conversion_return_pointer(const void *p)
291 1.8 rillig {
292 1.39 rillig /* expect+1: error: function has return type '_Bool' but returns 'pointer' [211] */
293 1.36 rillig return p;
294 1.8 rillig }
295 1.8 rillig
296 1.8 rillig char
297 1.8 rillig strict_bool_conversion_return_false_as_char(void)
298 1.8 rillig {
299 1.39 rillig /* expect+1: error: function has return type 'char' but returns '_Bool' [211] */
300 1.36 rillig return __lint_false;
301 1.8 rillig }
302 1.8 rillig
303 1.8 rillig char
304 1.8 rillig strict_bool_conversion_return_true_as_char(void)
305 1.8 rillig {
306 1.39 rillig /* expect+1: error: function has return type 'char' but returns '_Bool' [211] */
307 1.36 rillig return __lint_true;
308 1.8 rillig }
309 1.8 rillig
310 1.8 rillig
311 1.1 rillig void
312 1.8 rillig strict_bool_conversion_function_argument(void)
313 1.1 rillig {
314 1.8 rillig accept_bool(__lint_false);
315 1.8 rillig accept_bool(__lint_true);
316 1.8 rillig }
317 1.8 rillig
318 1.8 rillig void
319 1.8 rillig strict_bool_conversion_function_argument_pass(bool b, int i, const char *p)
320 1.8 rillig {
321 1.8 rillig /* No conversion necessary. */
322 1.8 rillig take_arguments(b, i, p);
323 1.8 rillig
324 1.8 rillig /* Implicitly converting bool to other scalar types. */
325 1.44 rillig /* expect+2: error: parameter 2 expects 'int', gets passed '_Bool' [334] */
326 1.44 rillig /* expect+1: error: parameter 3 expects 'pointer', gets passed '_Bool' [334] */
327 1.36 rillig take_arguments(b, b, b);
328 1.8 rillig
329 1.8 rillig /* Implicitly converting int to bool (arg #1). */
330 1.44 rillig /* expect+2: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
331 1.39 rillig /* expect+1: warning: illegal combination of pointer 'pointer to const char' and integer 'int', arg #3 [154] */
332 1.36 rillig take_arguments(i, i, i);
333 1.8 rillig
334 1.8 rillig /* Implicitly converting pointer to bool (arg #1). */
335 1.44 rillig /* expect+2: error: parameter 1 expects '_Bool', gets passed 'pointer' [334] */
336 1.39 rillig /* expect+1: warning: illegal combination of integer 'int' and pointer 'pointer to const char', arg #2 [154] */
337 1.36 rillig take_arguments(p, p, p);
338 1.8 rillig
339 1.8 rillig /* Passing bool as vararg. */
340 1.36 rillig /* TODO: maybe expect+1: arg#4 should not be bool but scalar */
341 1.36 rillig take_arguments(b, i, p, b, i, p);
342 1.8 rillig
343 1.8 rillig /* Passing a bool constant. */
344 1.8 rillig take_arguments(__lint_false, i, p);
345 1.8 rillig
346 1.8 rillig /* Passing a bool constant. */
347 1.8 rillig take_arguments(__lint_true, i, p);
348 1.8 rillig
349 1.8 rillig /* Trying to pass integer constants. */
350 1.44 rillig /* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
351 1.36 rillig take_arguments(0, i, p);
352 1.44 rillig /* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
353 1.36 rillig take_arguments(1, i, p);
354 1.44 rillig /* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
355 1.36 rillig take_arguments(2, i, p);
356 1.8 rillig }
357 1.1 rillig
358 1.8 rillig void
359 1.8 rillig strict_bool_conversion_between_bool_and_int(void)
360 1.8 rillig {
361 1.8 rillig bool b;
362 1.8 rillig int i;
363 1.1 rillig
364 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
365 1.36 rillig b = 0;
366 1.9 rillig b = __lint_false;
367 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
368 1.36 rillig b = 1;
369 1.8 rillig b = __lint_true;
370 1.1 rillig
371 1.8 rillig i = 0;
372 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
373 1.36 rillig i = __lint_false;
374 1.8 rillig i = 1;
375 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
376 1.36 rillig i = __lint_true;
377 1.8 rillig
378 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
379 1.36 rillig i = b;
380 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
381 1.36 rillig b = i;
382 1.8 rillig }
383 1.8 rillig
384 1.43 rillig /* expect+2: warning: parameter 'b' unused in function 'strict_bool_conversion_from_bool_to_scalar' [231] */
385 1.8 rillig void
386 1.36 rillig strict_bool_conversion_from_bool_to_scalar(bool b)
387 1.8 rillig {
388 1.8 rillig int i;
389 1.8 rillig unsigned u;
390 1.8 rillig double d;
391 1.8 rillig void *p;
392 1.8 rillig
393 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
394 1.36 rillig i = b;
395 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'unsigned int' and '_Bool' [107] */
396 1.36 rillig u = b;
397 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'double' and '_Bool' [107] */
398 1.36 rillig d = b;
399 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'pointer' and '_Bool' [107] */
400 1.36 rillig p = b;
401 1.8 rillig }
402 1.8 rillig
403 1.8 rillig /*
404 1.49 rillig * strict-bool-controlling-expression
405 1.8 rillig */
406 1.8 rillig
407 1.8 rillig void
408 1.8 rillig strict_bool_controlling_expression(bool b, int i, double d, const void *p)
409 1.8 rillig {
410 1.36 rillig /* expect+1: warning: constant in conditional context [161] */
411 1.36 rillig if (__lint_false)
412 1.51 rillig /* expect+1: warning: 'call' statement not reached [193] */
413 1.36 rillig do_nothing();
414 1.8 rillig
415 1.36 rillig /* expect+1: warning: constant in conditional context [161] */
416 1.36 rillig if (__lint_true)
417 1.8 rillig do_nothing();
418 1.8 rillig
419 1.8 rillig if (b)
420 1.8 rillig do_nothing();
421 1.8 rillig
422 1.36 rillig /* expect+1: error: controlling expression must be bool, not 'int' [333] */
423 1.36 rillig if (/*CONSTCOND*/0)
424 1.51 rillig /* expect+1: warning: 'call' statement not reached [193] */
425 1.36 rillig do_nothing();
426 1.8 rillig
427 1.36 rillig /* expect+1: error: controlling expression must be bool, not 'int' [333] */
428 1.36 rillig if (/*CONSTCOND*/1)
429 1.8 rillig do_nothing();
430 1.8 rillig
431 1.36 rillig /* expect+1: error: controlling expression must be bool, not 'int' [333] */
432 1.36 rillig if (/*CONSTCOND*/2)
433 1.8 rillig do_nothing();
434 1.1 rillig
435 1.1 rillig /* Not allowed: There is no implicit conversion from scalar to bool. */
436 1.36 rillig /* expect+1: error: controlling expression must be bool, not 'int' [333] */
437 1.36 rillig if (i)
438 1.8 rillig do_nothing();
439 1.1 rillig if (i != 0)
440 1.8 rillig do_nothing();
441 1.1 rillig
442 1.1 rillig /* Not allowed: There is no implicit conversion from scalar to bool. */
443 1.36 rillig /* expect+1: error: controlling expression must be bool, not 'double' [333] */
444 1.36 rillig if (d)
445 1.8 rillig do_nothing();
446 1.1 rillig if (d != 0.0)
447 1.8 rillig do_nothing();
448 1.1 rillig
449 1.1 rillig /* Not allowed: There is no implicit conversion from scalar to bool. */
450 1.36 rillig /* expect+1: error: controlling expression must be bool, not 'pointer' [333] */
451 1.36 rillig if (p)
452 1.8 rillig do_nothing();
453 1.1 rillig if (p != (void *)0)
454 1.8 rillig do_nothing();
455 1.49 rillig
456 1.49 rillig // An endless loop. The preferred form is 'for (;;)' instead.
457 1.49 rillig do {
458 1.49 rillig /* expect+1: warning: constant in conditional context [161] */
459 1.49 rillig } while (__lint_true);
460 1.49 rillig
461 1.49 rillig // A do-once "loop", often used in statement macros.
462 1.49 rillig /* expect+1: warning: loop not entered at top [207] */
463 1.49 rillig do {
464 1.49 rillig } while (__lint_false);
465 1.49 rillig
466 1.49 rillig // This form is too unusual to be allowed in strict bool mode.
467 1.49 rillig do {
468 1.49 rillig /* expect+2: error: controlling expression must be bool, not 'int' [333] */
469 1.49 rillig /* expect+1: warning: constant in conditional context [161] */
470 1.49 rillig } while (1);
471 1.49 rillig
472 1.49 rillig // Even though 0 is an integer instead of a bool, this idiom is so
473 1.49 rillig // common that it is frequently used in system headers. Since the
474 1.49 rillig // Clang preprocessor does not mark each token as coming from a system
475 1.49 rillig // header or from user code, this idiom can only be allowed everywhere
476 1.49 rillig // or nowhere.
477 1.49 rillig /* expect+1: warning: loop not entered at top [207] */
478 1.49 rillig do {
479 1.49 rillig } while (0);
480 1.8 rillig }
481 1.1 rillig
482 1.8 rillig /*
483 1.49 rillig * strict-bool-operand-unary
484 1.8 rillig */
485 1.8 rillig
486 1.8 rillig void
487 1.8 rillig strict_bool_operand_unary_not(void)
488 1.8 rillig {
489 1.8 rillig bool b = __lint_false;
490 1.8 rillig
491 1.8 rillig b = !b;
492 1.8 rillig b = !!!b;
493 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
494 1.44 rillig /* expect+1: warning: constant operand to '!' [239] */
495 1.36 rillig b = !__lint_false;
496 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
497 1.44 rillig /* expect+1: warning: constant operand to '!' [239] */
498 1.36 rillig b = !__lint_true;
499 1.8 rillig
500 1.8 rillig int i = 0;
501 1.8 rillig
502 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
503 1.36 rillig i = !i;
504 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
505 1.36 rillig i = !!!i;
506 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
507 1.36 rillig i = !0;
508 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
509 1.36 rillig i = !1;
510 1.8 rillig }
511 1.8 rillig
512 1.8 rillig void
513 1.8 rillig strict_bool_operand_unary_address(void)
514 1.8 rillig {
515 1.8 rillig bool b = __lint_false;
516 1.8 rillig
517 1.8 rillig /* Taking the address of a bool lvalue. */
518 1.8 rillig bool *bp;
519 1.8 rillig bp = &b;
520 1.8 rillig *bp = b;
521 1.8 rillig b = *bp;
522 1.1 rillig }
523 1.1 rillig
524 1.29 rillig /* see strict_bool_operand_unary_all below for the other unary operators. */
525 1.29 rillig
526 1.8 rillig /*
527 1.49 rillig * strict-bool-operand-binary
528 1.8 rillig */
529 1.8 rillig
530 1.8 rillig /*
531 1.8 rillig * Ensure that bool members can be accessed as usual.
532 1.8 rillig */
533 1.1 rillig void
534 1.8 rillig strict_bool_operand_binary_dot_arrow(void)
535 1.1 rillig {
536 1.8 rillig struct bool_struct {
537 1.8 rillig bool b;
538 1.8 rillig };
539 1.8 rillig
540 1.8 rillig /* Initialize and assign using boolean constants. */
541 1.8 rillig bool b = __lint_false;
542 1.8 rillig b = __lint_true;
543 1.1 rillig
544 1.8 rillig /* Access a struct member using the '.' operator. */
545 1.8 rillig struct bool_struct bs = { __lint_true };
546 1.8 rillig b = bs.b;
547 1.8 rillig bs.b = b;
548 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
549 1.36 rillig bs.b = 0;
550 1.1 rillig
551 1.8 rillig /* Access a struct member using the '->' operator. */
552 1.8 rillig struct bool_struct *bsp = &bs;
553 1.8 rillig b = bsp->b;
554 1.8 rillig bsp->b = b;
555 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
556 1.36 rillig bsp->b = 0;
557 1.1 rillig }
558 1.1 rillig
559 1.4 rillig int
560 1.8 rillig strict_bool_operand_binary(bool b, int i)
561 1.1 rillig {
562 1.1 rillig
563 1.4 rillig /* The right-hand sides of these assignments are ok. */
564 1.1 rillig b = !b;
565 1.1 rillig b = b && b;
566 1.1 rillig b = b || b;
567 1.1 rillig
568 1.4 rillig /*
569 1.4 rillig * The right-hand sides of these assignments implicitly convert from
570 1.4 rillig * scalar to bool.
571 1.4 rillig */
572 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
573 1.36 rillig b = !i;
574 1.36 rillig /* expect+2: error: left operand of '&&' must be bool, not 'int' [331] */
575 1.36 rillig /* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
576 1.36 rillig b = i && i;
577 1.36 rillig /* expect+2: error: left operand of '||' must be bool, not 'int' [331] */
578 1.36 rillig /* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
579 1.36 rillig b = i || i;
580 1.36 rillig
581 1.36 rillig /* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
582 1.36 rillig b = b && 0;
583 1.36 rillig /* expect+1: error: left operand of '&&' must be bool, not 'int' [331] */
584 1.36 rillig b = 0 && b;
585 1.36 rillig /* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
586 1.36 rillig b = b || 0;
587 1.36 rillig /* expect+1: error: left operand of '||' must be bool, not 'int' [331] */
588 1.36 rillig b = 0 || b;
589 1.4 rillig
590 1.4 rillig return i;
591 1.1 rillig }
592 1.1 rillig
593 1.1 rillig void
594 1.29 rillig strict_bool_operand_unary_all(bool b)
595 1.1 rillig {
596 1.8 rillig b = !b;
597 1.36 rillig /* expect+1: error: operand of '~' must not be bool [335] */
598 1.36 rillig b = ~b;
599 1.36 rillig /* expect+1: error: operand of '++x' must not be bool [335] */
600 1.36 rillig ++b;
601 1.36 rillig /* expect+1: error: operand of '--x' must not be bool [335] */
602 1.36 rillig --b;
603 1.36 rillig /* expect+1: error: operand of 'x++' must not be bool [335] */
604 1.36 rillig b++;
605 1.36 rillig /* expect+1: error: operand of 'x--' must not be bool [335] */
606 1.36 rillig b--;
607 1.36 rillig /* expect+1: error: operand of '+' must not be bool [335] */
608 1.36 rillig b = +b;
609 1.36 rillig /* expect+1: error: operand of '-' must not be bool [335] */
610 1.36 rillig b = -b;
611 1.29 rillig }
612 1.4 rillig
613 1.29 rillig void
614 1.29 rillig strict_bool_operand_binary_all(bool b, unsigned u)
615 1.29 rillig {
616 1.36 rillig /* expect+2: error: left operand of '*' must not be bool [336] */
617 1.36 rillig /* expect+1: error: right operand of '*' must not be bool [337] */
618 1.36 rillig b = b * b;
619 1.36 rillig /* expect+2: error: left operand of '/' must not be bool [336] */
620 1.36 rillig /* expect+1: error: right operand of '/' must not be bool [337] */
621 1.36 rillig b = b / b;
622 1.36 rillig /* expect+2: error: left operand of '%' must not be bool [336] */
623 1.36 rillig /* expect+1: error: right operand of '%' must not be bool [337] */
624 1.36 rillig b = b % b;
625 1.36 rillig /* expect+2: error: left operand of '+' must not be bool [336] */
626 1.36 rillig /* expect+1: error: right operand of '+' must not be bool [337] */
627 1.36 rillig b = b + b;
628 1.36 rillig /* expect+2: error: left operand of '-' must not be bool [336] */
629 1.36 rillig /* expect+1: error: right operand of '-' must not be bool [337] */
630 1.36 rillig b = b - b;
631 1.36 rillig /* expect+2: error: left operand of '<<' must not be bool [336] */
632 1.36 rillig /* expect+1: error: right operand of '<<' must not be bool [337] */
633 1.36 rillig b = b << b;
634 1.36 rillig /* expect+2: error: left operand of '>>' must not be bool [336] */
635 1.36 rillig /* expect+1: error: right operand of '>>' must not be bool [337] */
636 1.36 rillig b = b >> b;
637 1.36 rillig
638 1.36 rillig /* expect+2: error: left operand of '<' must not be bool [336] */
639 1.36 rillig /* expect+1: error: right operand of '<' must not be bool [337] */
640 1.36 rillig b = b < b;
641 1.36 rillig /* expect+2: error: left operand of '<=' must not be bool [336] */
642 1.36 rillig /* expect+1: error: right operand of '<=' must not be bool [337] */
643 1.36 rillig b = b <= b;
644 1.36 rillig /* expect+2: error: left operand of '>' must not be bool [336] */
645 1.36 rillig /* expect+1: error: right operand of '>' must not be bool [337] */
646 1.36 rillig b = b > b;
647 1.36 rillig /* expect+2: error: left operand of '>=' must not be bool [336] */
648 1.36 rillig /* expect+1: error: right operand of '>=' must not be bool [337] */
649 1.36 rillig b = b >= b;
650 1.8 rillig b = b == b;
651 1.8 rillig b = b != b;
652 1.1 rillig
653 1.8 rillig b = b & b;
654 1.8 rillig b = b ^ b;
655 1.8 rillig b = b | b;
656 1.8 rillig b = b && b;
657 1.8 rillig b = b || b;
658 1.8 rillig b = b ? b : b;
659 1.1 rillig
660 1.8 rillig b = b;
661 1.36 rillig /* expect+2: error: left operand of '*=' must not be bool [336] */
662 1.36 rillig /* expect+1: error: right operand of '*=' must not be bool [337] */
663 1.36 rillig b *= b;
664 1.36 rillig /* expect+2: error: left operand of '/=' must not be bool [336] */
665 1.36 rillig /* expect+1: error: right operand of '/=' must not be bool [337] */
666 1.36 rillig b /= b;
667 1.36 rillig /* expect+2: error: left operand of '%=' must not be bool [336] */
668 1.36 rillig /* expect+1: error: right operand of '%=' must not be bool [337] */
669 1.36 rillig b %= b;
670 1.36 rillig /* expect+2: error: left operand of '+=' must not be bool [336] */
671 1.36 rillig /* expect+1: error: right operand of '+=' must not be bool [337] */
672 1.36 rillig b += b;
673 1.36 rillig /* expect+2: error: left operand of '-=' must not be bool [336] */
674 1.36 rillig /* expect+1: error: right operand of '-=' must not be bool [337] */
675 1.36 rillig b -= b;
676 1.36 rillig /* expect+2: error: left operand of '<<=' must not be bool [336] */
677 1.36 rillig /* expect+1: error: right operand of '<<=' must not be bool [337] */
678 1.36 rillig b <<= b;
679 1.36 rillig /* expect+2: error: left operand of '>>=' must not be bool [336] */
680 1.36 rillig /* expect+1: error: right operand of '>>=' must not be bool [337] */
681 1.36 rillig b >>= b;
682 1.8 rillig b &= b;
683 1.8 rillig b ^= b;
684 1.8 rillig b |= b;
685 1.1 rillig
686 1.1 rillig /* Operations with mixed types. */
687 1.36 rillig /* expect+1: error: left operand of '*' must not be bool [336] */
688 1.36 rillig u = b * u;
689 1.36 rillig /* expect+1: error: right operand of '*' must not be bool [337] */
690 1.36 rillig u = u * b;
691 1.36 rillig /* expect+1: error: left operand of '/' must not be bool [336] */
692 1.36 rillig u = b / u;
693 1.36 rillig /* expect+1: error: right operand of '/' must not be bool [337] */
694 1.36 rillig u = u / b;
695 1.36 rillig /* expect+1: error: left operand of '%' must not be bool [336] */
696 1.36 rillig u = b % u;
697 1.36 rillig /* expect+1: error: right operand of '%' must not be bool [337] */
698 1.36 rillig u = u % b;
699 1.36 rillig /* expect+1: error: left operand of '+' must not be bool [336] */
700 1.36 rillig u = b + u;
701 1.36 rillig /* expect+1: error: right operand of '+' must not be bool [337] */
702 1.36 rillig u = u + b;
703 1.36 rillig /* expect+1: error: left operand of '-' must not be bool [336] */
704 1.36 rillig u = b - u;
705 1.36 rillig /* expect+1: error: right operand of '-' must not be bool [337] */
706 1.36 rillig u = u - b;
707 1.36 rillig /* expect+1: error: left operand of '<<' must not be bool [336] */
708 1.36 rillig u = b << u;
709 1.36 rillig /* expect+1: error: right operand of '<<' must not be bool [337] */
710 1.36 rillig u = u << b;
711 1.36 rillig /* expect+1: error: left operand of '>>' must not be bool [336] */
712 1.36 rillig u = b >> u;
713 1.36 rillig /* expect+1: error: right operand of '>>' must not be bool [337] */
714 1.36 rillig u = u >> b;
715 1.8 rillig u = b ? u : u;
716 1.38 rillig /* expect+1: error: operands of ':' have incompatible types '_Bool' and 'unsigned int' [107] */
717 1.36 rillig u = b ? b : u;
718 1.38 rillig /* expect+1: error: operands of ':' have incompatible types 'unsigned int' and '_Bool' [107] */
719 1.36 rillig u = b ? u : b;
720 1.1 rillig }
721 1.1 rillig
722 1.8 rillig bool
723 1.8 rillig strict_bool_operand_binary_comma(bool b, int i)
724 1.1 rillig {
725 1.36 rillig /* expect+1: warning: expression has null effect [129] */
726 1.36 rillig b = (b, !b);
727 1.36 rillig /* expect+1: warning: expression has null effect [129] */
728 1.36 rillig i = (i, i + 1);
729 1.8 rillig return b;
730 1.1 rillig }
731 1.1 rillig
732 1.8 rillig /*
733 1.49 rillig * strict-bool-operator-result
734 1.8 rillig */
735 1.1 rillig
736 1.8 rillig void
737 1.8 rillig strict_bool_operator_result(bool b)
738 1.8 rillig {
739 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'char' and '_Bool' [107] */
740 1.36 rillig char c = b;
741 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'int' and '_Bool' [107] */
742 1.36 rillig int i = b;
743 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'double' and '_Bool' [107] */
744 1.36 rillig double d = b;
745 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'pointer' and '_Bool' [107] */
746 1.36 rillig void *p = b;
747 1.1 rillig
748 1.8 rillig /* The right-hand sides of these assignments are all ok. */
749 1.8 rillig b = !b;
750 1.8 rillig b = i == i;
751 1.8 rillig b = i != i;
752 1.8 rillig b = i < i;
753 1.8 rillig b = i <= i;
754 1.8 rillig b = i >= i;
755 1.8 rillig b = i > i;
756 1.8 rillig b = b && b;
757 1.8 rillig b = b || b;
758 1.1 rillig
759 1.1 rillig /*
760 1.8 rillig * The right-hand sides of these assignments are not ok, they
761 1.8 rillig * implicitly convert from bool to int.
762 1.1 rillig */
763 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
764 1.36 rillig i = !b;
765 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
766 1.36 rillig i = i == i;
767 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
768 1.36 rillig i = i != i;
769 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
770 1.36 rillig i = i < i;
771 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
772 1.36 rillig i = i <= i;
773 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
774 1.36 rillig i = i >= i;
775 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
776 1.36 rillig i = i > i;
777 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
778 1.36 rillig i = b && b;
779 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
780 1.36 rillig i = b || b;
781 1.8 rillig }
782 1.1 rillig
783 1.2 rillig
784 1.3 rillig /*
785 1.49 rillig * strict-bool-bitwise-and
786 1.3 rillig */
787 1.3 rillig
788 1.3 rillig enum Flags {
789 1.3 rillig FLAG0 = 1 << 0,
790 1.3 rillig FLAG1 = 1 << 1,
791 1.3 rillig FLAG28 = 1 << 28
792 1.2 rillig };
793 1.2 rillig
794 1.43 rillig /* expect+2: warning: parameter 'flags' unused in function 'strict_bool_bitwise_and_enum' [231] */
795 1.3 rillig void
796 1.36 rillig strict_bool_bitwise_and_enum(enum Flags flags)
797 1.3 rillig {
798 1.3 rillig bool b;
799 1.3 rillig
800 1.3 rillig /*
801 1.4 rillig * FLAG0 has the value 1 and thus can be stored in a bool variable
802 1.4 rillig * without truncation. Nevertheless this special case is not allowed
803 1.4 rillig * because it would be too confusing if FLAG0 would work and all the
804 1.4 rillig * other flags wouldn't.
805 1.3 rillig */
806 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
807 1.36 rillig b = flags & FLAG0;
808 1.3 rillig
809 1.3 rillig /*
810 1.3 rillig * Assuming that FLAG1 is set in flags, a _Bool variable stores this
811 1.4 rillig * as 1, as defined by C99 6.3.1.2. A uint8_t variable would store
812 1.4 rillig * it as 2, as that is the integer value of FLAG1. Since FLAG1 fits
813 1.4 rillig * in a uint8_t, no truncation takes place.
814 1.3 rillig */
815 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
816 1.36 rillig b = flags & FLAG1;
817 1.3 rillig
818 1.3 rillig /*
819 1.4 rillig * In a _Bool variable, FLAG28 is stored as 1, since it is unequal to
820 1.4 rillig * zero. In a uint8_t, the stored value would be 0 since bit 28 is
821 1.4 rillig * out of range for a uint8_t and thus gets truncated.
822 1.3 rillig */
823 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
824 1.36 rillig b = flags & FLAG28;
825 1.3 rillig }
826 1.3 rillig
827 1.4 rillig /*
828 1.8 rillig * Demonstrate idiomatic code to query flags from an enum bit set.
829 1.8 rillig *
830 1.8 rillig * In all the controlling expressions in this function, the result of the
831 1.8 rillig * operator '&' is compared against 0. This makes this pattern work, no
832 1.8 rillig * matter whether the bits are in the low-value range or in the high-value
833 1.8 rillig * range (such as FLAG28, which has the value 1073741824, which is more than
834 1.8 rillig * what would fit into an unsigned char). Even if an enum could be extended
835 1.8 rillig * to larger types than int, this pattern would work.
836 1.4 rillig */
837 1.47 rillig bool
838 1.8 rillig query_flag_from_enum_bit_set(enum Flags flags)
839 1.4 rillig {
840 1.8 rillig if (flags & FLAG0)
841 1.8 rillig println("FLAG0 is set");
842 1.4 rillig
843 1.8 rillig if ((flags & FLAG1) != 0)
844 1.8 rillig println("FLAG1 is set");
845 1.4 rillig
846 1.8 rillig if ((flags & (FLAG0 | FLAG1)) == (FLAG0 | FLAG1))
847 1.8 rillig println("FLAG0 and FLAG1 are both set");
848 1.4 rillig
849 1.8 rillig if (flags & FLAG0 && flags & FLAG1)
850 1.8 rillig println("FLAG0 and FLAG1 are both set");
851 1.4 rillig
852 1.8 rillig if ((flags & (FLAG0 | FLAG1)) != 0)
853 1.8 rillig println("At least one of FLAG0 and FLAG1 is set");
854 1.4 rillig
855 1.8 rillig if (flags & FLAG28)
856 1.8 rillig println("FLAG28 is set");
857 1.47 rillig
858 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
859 1.47 rillig bool b0 = flags & FLAG0;
860 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
861 1.47 rillig bool b1 = flags & FLAG1;
862 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
863 1.47 rillig bool b28 = flags & FLAG28;
864 1.47 rillig return b0 || b1 || b28;
865 1.7 rillig }
866 1.10 rillig
867 1.47 rillig bool
868 1.46 rillig query_flag_from_int(int flags)
869 1.46 rillig {
870 1.47 rillig
871 1.46 rillig if (flags & FLAG0)
872 1.46 rillig println("FLAG0 is set");
873 1.46 rillig
874 1.46 rillig if ((flags & FLAG1) != 0)
875 1.46 rillig println("FLAG1 is set");
876 1.46 rillig
877 1.46 rillig if ((flags & (FLAG0 | FLAG1)) == (FLAG0 | FLAG1))
878 1.46 rillig println("FLAG0 and FLAG1 are both set");
879 1.46 rillig
880 1.46 rillig if (flags & FLAG0 && flags & FLAG1)
881 1.46 rillig println("FLAG0 and FLAG1 are both set");
882 1.46 rillig
883 1.46 rillig if ((flags & (FLAG0 | FLAG1)) != 0)
884 1.46 rillig println("At least one of FLAG0 and FLAG1 is set");
885 1.46 rillig
886 1.46 rillig if (flags & FLAG28)
887 1.46 rillig println("FLAG28 is set");
888 1.47 rillig
889 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
890 1.47 rillig bool b0 = flags & FLAG0;
891 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
892 1.47 rillig bool b1 = flags & FLAG1;
893 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
894 1.47 rillig bool b28 = flags & FLAG28;
895 1.47 rillig return b0 || b1 || b28;
896 1.46 rillig }
897 1.46 rillig
898 1.10 rillig
899 1.12 rillig void
900 1.10 rillig strict_bool_operator_eq_bool_int(void)
901 1.10 rillig {
902 1.38 rillig /* expect+1: error: operands of '==' have incompatible types '_Bool' and 'int' [107] */
903 1.36 rillig (void)(strict_bool_conversion_return_false() == 0);
904 1.10 rillig }
905 1.13 rillig
906 1.13 rillig void
907 1.13 rillig strict_bool_assign_bit_field_then_compare(void)
908 1.13 rillig {
909 1.13 rillig struct s {
910 1.13 rillig bool flag: 1;
911 1.13 rillig };
912 1.13 rillig
913 1.13 rillig struct s s = { __lint_false };
914 1.13 rillig
915 1.36 rillig /* expect+1: warning: expression has null effect [129] */
916 1.36 rillig (void)((s.flag = s.flag) != __lint_false);
917 1.13 rillig }
918 1.18 rillig
919 1.18 rillig void
920 1.18 rillig bool_as_array_index(bool cond)
921 1.18 rillig {
922 1.18 rillig static const char *repr[] = { "no", "yes" };
923 1.18 rillig /*
924 1.18 rillig * The '+' in the error message reveals that lint internally
925 1.18 rillig * translates 'arr[ind]' to '*(arr + ind)' in an early stage of
926 1.18 rillig * parsing.
927 1.18 rillig */
928 1.36 rillig /* expect+1: error: right operand of '+' must not be bool [337] */
929 1.36 rillig println(repr[cond]);
930 1.18 rillig println(cond ? "yes" : "no");
931 1.18 rillig }
932 1.20 rillig
933 1.20 rillig void
934 1.23 rillig initialization(void)
935 1.23 rillig {
936 1.23 rillig struct {
937 1.23 rillig _Bool b;
938 1.23 rillig } var[] = {
939 1.23 rillig { __lint_false },
940 1.23 rillig { __lint_true },
941 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
942 1.36 rillig { 0 },
943 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
944 1.36 rillig { 1 },
945 1.23 rillig };
946 1.23 rillig }
947