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