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