d_c99_bool_strict.c revision 1.49 1 1.49 rillig /* $NetBSD: d_c99_bool_strict.c,v 1.49 2024/05/12 11:46:14 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.36 rillig do_nothing();
413 1.36 rillig /* expect-1: warning: statement not reached [193] */
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.36 rillig do_nothing();
425 1.36 rillig /* expect-1: warning: statement not reached [193] */
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 /* expect+1: error: controlling expression must be bool, not 'int' [333] */
480 1.49 rillig } while (0);
481 1.8 rillig }
482 1.1 rillig
483 1.8 rillig /*
484 1.49 rillig * strict-bool-operand-unary
485 1.8 rillig */
486 1.8 rillig
487 1.8 rillig void
488 1.8 rillig strict_bool_operand_unary_not(void)
489 1.8 rillig {
490 1.8 rillig bool b = __lint_false;
491 1.8 rillig
492 1.8 rillig b = !b;
493 1.8 rillig b = !!!b;
494 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
495 1.44 rillig /* expect+1: warning: constant operand to '!' [239] */
496 1.36 rillig b = !__lint_false;
497 1.36 rillig /* expect+2: warning: constant in conditional context [161] */
498 1.44 rillig /* expect+1: warning: constant operand to '!' [239] */
499 1.36 rillig b = !__lint_true;
500 1.8 rillig
501 1.8 rillig int i = 0;
502 1.8 rillig
503 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
504 1.36 rillig i = !i;
505 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
506 1.36 rillig i = !!!i;
507 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
508 1.36 rillig i = !0;
509 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
510 1.36 rillig i = !1;
511 1.8 rillig }
512 1.8 rillig
513 1.8 rillig void
514 1.8 rillig strict_bool_operand_unary_address(void)
515 1.8 rillig {
516 1.8 rillig bool b = __lint_false;
517 1.8 rillig
518 1.8 rillig /* Taking the address of a bool lvalue. */
519 1.8 rillig bool *bp;
520 1.8 rillig bp = &b;
521 1.8 rillig *bp = b;
522 1.8 rillig b = *bp;
523 1.1 rillig }
524 1.1 rillig
525 1.29 rillig /* see strict_bool_operand_unary_all below for the other unary operators. */
526 1.29 rillig
527 1.8 rillig /*
528 1.49 rillig * strict-bool-operand-binary
529 1.8 rillig */
530 1.8 rillig
531 1.8 rillig /*
532 1.8 rillig * Ensure that bool members can be accessed as usual.
533 1.8 rillig */
534 1.1 rillig void
535 1.8 rillig strict_bool_operand_binary_dot_arrow(void)
536 1.1 rillig {
537 1.8 rillig struct bool_struct {
538 1.8 rillig bool b;
539 1.8 rillig };
540 1.8 rillig
541 1.8 rillig /* Initialize and assign using boolean constants. */
542 1.8 rillig bool b = __lint_false;
543 1.8 rillig b = __lint_true;
544 1.1 rillig
545 1.8 rillig /* Access a struct member using the '.' operator. */
546 1.8 rillig struct bool_struct bs = { __lint_true };
547 1.8 rillig b = bs.b;
548 1.8 rillig bs.b = b;
549 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
550 1.36 rillig bs.b = 0;
551 1.1 rillig
552 1.8 rillig /* Access a struct member using the '->' operator. */
553 1.8 rillig struct bool_struct *bsp = &bs;
554 1.8 rillig b = bsp->b;
555 1.8 rillig bsp->b = b;
556 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
557 1.36 rillig bsp->b = 0;
558 1.1 rillig }
559 1.1 rillig
560 1.4 rillig int
561 1.8 rillig strict_bool_operand_binary(bool b, int i)
562 1.1 rillig {
563 1.1 rillig
564 1.4 rillig /* The right-hand sides of these assignments are ok. */
565 1.1 rillig b = !b;
566 1.1 rillig b = b && b;
567 1.1 rillig b = b || b;
568 1.1 rillig
569 1.4 rillig /*
570 1.4 rillig * The right-hand sides of these assignments implicitly convert from
571 1.4 rillig * scalar to bool.
572 1.4 rillig */
573 1.36 rillig /* expect+1: error: operand of '!' must be bool, not 'int' [330] */
574 1.36 rillig b = !i;
575 1.36 rillig /* expect+2: error: left operand of '&&' must be bool, not 'int' [331] */
576 1.36 rillig /* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
577 1.36 rillig b = i && i;
578 1.36 rillig /* expect+2: error: left operand of '||' must be bool, not 'int' [331] */
579 1.36 rillig /* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
580 1.36 rillig b = i || i;
581 1.36 rillig
582 1.36 rillig /* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
583 1.36 rillig b = b && 0;
584 1.36 rillig /* expect+1: error: left operand of '&&' must be bool, not 'int' [331] */
585 1.36 rillig b = 0 && b;
586 1.36 rillig /* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
587 1.36 rillig b = b || 0;
588 1.36 rillig /* expect+1: error: left operand of '||' must be bool, not 'int' [331] */
589 1.36 rillig b = 0 || b;
590 1.4 rillig
591 1.4 rillig return i;
592 1.1 rillig }
593 1.1 rillig
594 1.1 rillig void
595 1.29 rillig strict_bool_operand_unary_all(bool b)
596 1.1 rillig {
597 1.8 rillig b = !b;
598 1.36 rillig /* expect+1: error: operand of '~' must not be bool [335] */
599 1.36 rillig b = ~b;
600 1.36 rillig /* expect+1: error: operand of '++x' must not be bool [335] */
601 1.36 rillig ++b;
602 1.36 rillig /* expect+1: error: operand of '--x' must not be bool [335] */
603 1.36 rillig --b;
604 1.36 rillig /* expect+1: error: operand of 'x++' must not be bool [335] */
605 1.36 rillig b++;
606 1.36 rillig /* expect+1: error: operand of 'x--' must not be bool [335] */
607 1.36 rillig b--;
608 1.36 rillig /* expect+1: error: operand of '+' must not be bool [335] */
609 1.36 rillig b = +b;
610 1.36 rillig /* expect+1: error: operand of '-' must not be bool [335] */
611 1.36 rillig b = -b;
612 1.29 rillig }
613 1.4 rillig
614 1.29 rillig void
615 1.29 rillig strict_bool_operand_binary_all(bool b, unsigned u)
616 1.29 rillig {
617 1.36 rillig /* expect+2: error: left operand of '*' must not be bool [336] */
618 1.36 rillig /* expect+1: error: right operand of '*' must not be bool [337] */
619 1.36 rillig b = b * b;
620 1.36 rillig /* expect+2: error: left operand of '/' must not be bool [336] */
621 1.36 rillig /* expect+1: error: right operand of '/' must not be bool [337] */
622 1.36 rillig b = b / b;
623 1.36 rillig /* expect+2: error: left operand of '%' must not be bool [336] */
624 1.36 rillig /* expect+1: error: right operand of '%' must not be bool [337] */
625 1.36 rillig b = b % b;
626 1.36 rillig /* expect+2: error: left operand of '+' must not be bool [336] */
627 1.36 rillig /* expect+1: error: right operand of '+' must not be bool [337] */
628 1.36 rillig b = b + b;
629 1.36 rillig /* expect+2: error: left operand of '-' must not be bool [336] */
630 1.36 rillig /* expect+1: error: right operand of '-' must not be bool [337] */
631 1.36 rillig b = b - b;
632 1.36 rillig /* expect+2: error: left operand of '<<' must not be bool [336] */
633 1.36 rillig /* expect+1: error: right operand of '<<' must not be bool [337] */
634 1.36 rillig b = b << b;
635 1.36 rillig /* expect+2: error: left operand of '>>' must not be bool [336] */
636 1.36 rillig /* expect+1: error: right operand of '>>' must not be bool [337] */
637 1.36 rillig b = b >> b;
638 1.36 rillig
639 1.36 rillig /* expect+2: error: left operand of '<' must not be bool [336] */
640 1.36 rillig /* expect+1: error: right operand of '<' must not be bool [337] */
641 1.36 rillig b = b < b;
642 1.36 rillig /* expect+2: error: left operand of '<=' must not be bool [336] */
643 1.36 rillig /* expect+1: error: right operand of '<=' must not be bool [337] */
644 1.36 rillig b = b <= b;
645 1.36 rillig /* expect+2: error: left operand of '>' must not be bool [336] */
646 1.36 rillig /* expect+1: error: right operand of '>' must not be bool [337] */
647 1.36 rillig b = b > b;
648 1.36 rillig /* expect+2: error: left operand of '>=' must not be bool [336] */
649 1.36 rillig /* expect+1: error: right operand of '>=' must not be bool [337] */
650 1.36 rillig b = b >= b;
651 1.8 rillig b = b == b;
652 1.8 rillig b = b != b;
653 1.1 rillig
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;
659 1.8 rillig b = b ? b : b;
660 1.1 rillig
661 1.8 rillig b = b;
662 1.36 rillig /* expect+2: error: left operand of '*=' must not be bool [336] */
663 1.36 rillig /* expect+1: error: right operand of '*=' must not be bool [337] */
664 1.36 rillig b *= b;
665 1.36 rillig /* expect+2: error: left operand of '/=' must not be bool [336] */
666 1.36 rillig /* expect+1: error: right operand of '/=' must not be bool [337] */
667 1.36 rillig b /= b;
668 1.36 rillig /* expect+2: error: left operand of '%=' must not be bool [336] */
669 1.36 rillig /* expect+1: error: right operand of '%=' must not be bool [337] */
670 1.36 rillig b %= b;
671 1.36 rillig /* expect+2: error: left operand of '+=' must not be bool [336] */
672 1.36 rillig /* expect+1: error: right operand of '+=' must not be bool [337] */
673 1.36 rillig b += b;
674 1.36 rillig /* expect+2: error: left operand of '-=' must not be bool [336] */
675 1.36 rillig /* expect+1: error: right operand of '-=' must not be bool [337] */
676 1.36 rillig b -= b;
677 1.36 rillig /* expect+2: error: left operand of '<<=' must not be bool [336] */
678 1.36 rillig /* expect+1: error: right operand of '<<=' must not be bool [337] */
679 1.36 rillig b <<= b;
680 1.36 rillig /* expect+2: error: left operand of '>>=' must not be bool [336] */
681 1.36 rillig /* expect+1: error: right operand of '>>=' must not be bool [337] */
682 1.36 rillig b >>= b;
683 1.8 rillig b &= b;
684 1.8 rillig b ^= b;
685 1.8 rillig b |= b;
686 1.1 rillig
687 1.1 rillig /* Operations with mixed types. */
688 1.36 rillig /* expect+1: error: left operand of '*' must not be bool [336] */
689 1.36 rillig u = b * u;
690 1.36 rillig /* expect+1: error: right operand of '*' must not be bool [337] */
691 1.36 rillig u = u * b;
692 1.36 rillig /* expect+1: error: left operand of '/' must not be bool [336] */
693 1.36 rillig u = b / u;
694 1.36 rillig /* expect+1: error: right operand of '/' must not be bool [337] */
695 1.36 rillig u = u / b;
696 1.36 rillig /* expect+1: error: left operand of '%' must not be bool [336] */
697 1.36 rillig u = b % u;
698 1.36 rillig /* expect+1: error: right operand of '%' must not be bool [337] */
699 1.36 rillig u = u % b;
700 1.36 rillig /* expect+1: error: left operand of '+' must not be bool [336] */
701 1.36 rillig u = b + u;
702 1.36 rillig /* expect+1: error: right operand of '+' must not be bool [337] */
703 1.36 rillig u = u + b;
704 1.36 rillig /* expect+1: error: left operand of '-' must not be bool [336] */
705 1.36 rillig u = b - u;
706 1.36 rillig /* expect+1: error: right operand of '-' must not be bool [337] */
707 1.36 rillig u = u - b;
708 1.36 rillig /* expect+1: error: left operand of '<<' must not be bool [336] */
709 1.36 rillig u = b << u;
710 1.36 rillig /* expect+1: error: right operand of '<<' must not be bool [337] */
711 1.36 rillig u = u << b;
712 1.36 rillig /* expect+1: error: left operand of '>>' must not be bool [336] */
713 1.36 rillig u = b >> u;
714 1.36 rillig /* expect+1: error: right operand of '>>' must not be bool [337] */
715 1.36 rillig u = u >> b;
716 1.8 rillig u = b ? u : u;
717 1.38 rillig /* expect+1: error: operands of ':' have incompatible types '_Bool' and 'unsigned int' [107] */
718 1.36 rillig u = b ? b : u;
719 1.38 rillig /* expect+1: error: operands of ':' have incompatible types 'unsigned int' and '_Bool' [107] */
720 1.36 rillig u = b ? u : b;
721 1.1 rillig }
722 1.1 rillig
723 1.8 rillig bool
724 1.8 rillig strict_bool_operand_binary_comma(bool b, int i)
725 1.1 rillig {
726 1.36 rillig /* expect+1: warning: expression has null effect [129] */
727 1.36 rillig b = (b, !b);
728 1.36 rillig /* expect+1: warning: expression has null effect [129] */
729 1.36 rillig i = (i, i + 1);
730 1.8 rillig return b;
731 1.1 rillig }
732 1.1 rillig
733 1.8 rillig /*
734 1.49 rillig * strict-bool-operator-result
735 1.8 rillig */
736 1.1 rillig
737 1.8 rillig void
738 1.8 rillig strict_bool_operator_result(bool b)
739 1.8 rillig {
740 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'char' and '_Bool' [107] */
741 1.36 rillig char c = b;
742 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'int' and '_Bool' [107] */
743 1.36 rillig int i = b;
744 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'double' and '_Bool' [107] */
745 1.36 rillig double d = b;
746 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types 'pointer' and '_Bool' [107] */
747 1.36 rillig void *p = b;
748 1.1 rillig
749 1.8 rillig /* The right-hand sides of these assignments are all ok. */
750 1.8 rillig b = !b;
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 = i > i;
757 1.8 rillig b = b && b;
758 1.8 rillig b = b || b;
759 1.1 rillig
760 1.1 rillig /*
761 1.8 rillig * The right-hand sides of these assignments are not ok, they
762 1.8 rillig * implicitly convert from bool to int.
763 1.1 rillig */
764 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
765 1.36 rillig i = !b;
766 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
767 1.36 rillig i = i == i;
768 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
769 1.36 rillig i = i != i;
770 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
771 1.36 rillig i = i < i;
772 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
773 1.36 rillig i = i <= i;
774 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
775 1.36 rillig i = i >= i;
776 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
777 1.36 rillig i = i > i;
778 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
779 1.36 rillig i = b && b;
780 1.38 rillig /* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
781 1.36 rillig i = b || b;
782 1.8 rillig }
783 1.1 rillig
784 1.2 rillig
785 1.3 rillig /*
786 1.49 rillig * strict-bool-bitwise-and
787 1.3 rillig */
788 1.3 rillig
789 1.3 rillig enum Flags {
790 1.3 rillig FLAG0 = 1 << 0,
791 1.3 rillig FLAG1 = 1 << 1,
792 1.3 rillig FLAG28 = 1 << 28
793 1.2 rillig };
794 1.2 rillig
795 1.43 rillig /* expect+2: warning: parameter 'flags' unused in function 'strict_bool_bitwise_and_enum' [231] */
796 1.3 rillig void
797 1.36 rillig strict_bool_bitwise_and_enum(enum Flags flags)
798 1.3 rillig {
799 1.3 rillig bool b;
800 1.3 rillig
801 1.3 rillig /*
802 1.4 rillig * FLAG0 has the value 1 and thus can be stored in a bool variable
803 1.4 rillig * without truncation. Nevertheless this special case is not allowed
804 1.4 rillig * because it would be too confusing if FLAG0 would work and all the
805 1.4 rillig * other flags wouldn't.
806 1.3 rillig */
807 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
808 1.36 rillig b = flags & FLAG0;
809 1.3 rillig
810 1.3 rillig /*
811 1.3 rillig * Assuming that FLAG1 is set in flags, a _Bool variable stores this
812 1.4 rillig * as 1, as defined by C99 6.3.1.2. A uint8_t variable would store
813 1.4 rillig * it as 2, as that is the integer value of FLAG1. Since FLAG1 fits
814 1.4 rillig * in a uint8_t, no truncation takes place.
815 1.3 rillig */
816 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
817 1.36 rillig b = flags & FLAG1;
818 1.3 rillig
819 1.3 rillig /*
820 1.4 rillig * In a _Bool variable, FLAG28 is stored as 1, since it is unequal to
821 1.4 rillig * zero. In a uint8_t, the stored value would be 0 since bit 28 is
822 1.4 rillig * out of range for a uint8_t and thus gets truncated.
823 1.3 rillig */
824 1.38 rillig /* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
825 1.36 rillig b = flags & FLAG28;
826 1.3 rillig }
827 1.3 rillig
828 1.4 rillig /*
829 1.8 rillig * Demonstrate idiomatic code to query flags from an enum bit set.
830 1.8 rillig *
831 1.8 rillig * In all the controlling expressions in this function, the result of the
832 1.8 rillig * operator '&' is compared against 0. This makes this pattern work, no
833 1.8 rillig * matter whether the bits are in the low-value range or in the high-value
834 1.8 rillig * range (such as FLAG28, which has the value 1073741824, which is more than
835 1.8 rillig * what would fit into an unsigned char). Even if an enum could be extended
836 1.8 rillig * to larger types than int, this pattern would work.
837 1.4 rillig */
838 1.47 rillig bool
839 1.8 rillig query_flag_from_enum_bit_set(enum Flags flags)
840 1.4 rillig {
841 1.8 rillig if (flags & FLAG0)
842 1.8 rillig println("FLAG0 is set");
843 1.4 rillig
844 1.8 rillig if ((flags & FLAG1) != 0)
845 1.8 rillig println("FLAG1 is set");
846 1.4 rillig
847 1.8 rillig if ((flags & (FLAG0 | FLAG1)) == (FLAG0 | FLAG1))
848 1.8 rillig println("FLAG0 and FLAG1 are both set");
849 1.4 rillig
850 1.8 rillig if (flags & FLAG0 && flags & FLAG1)
851 1.8 rillig println("FLAG0 and FLAG1 are both set");
852 1.4 rillig
853 1.8 rillig if ((flags & (FLAG0 | FLAG1)) != 0)
854 1.8 rillig println("At least one of FLAG0 and FLAG1 is set");
855 1.4 rillig
856 1.8 rillig if (flags & FLAG28)
857 1.8 rillig println("FLAG28 is set");
858 1.47 rillig
859 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
860 1.47 rillig bool b0 = flags & FLAG0;
861 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
862 1.47 rillig bool b1 = flags & FLAG1;
863 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
864 1.47 rillig bool b28 = flags & FLAG28;
865 1.47 rillig return b0 || b1 || b28;
866 1.7 rillig }
867 1.10 rillig
868 1.47 rillig bool
869 1.46 rillig query_flag_from_int(int flags)
870 1.46 rillig {
871 1.47 rillig
872 1.46 rillig if (flags & FLAG0)
873 1.46 rillig println("FLAG0 is set");
874 1.46 rillig
875 1.46 rillig if ((flags & FLAG1) != 0)
876 1.46 rillig println("FLAG1 is set");
877 1.46 rillig
878 1.46 rillig if ((flags & (FLAG0 | FLAG1)) == (FLAG0 | FLAG1))
879 1.46 rillig println("FLAG0 and FLAG1 are both set");
880 1.46 rillig
881 1.46 rillig if (flags & FLAG0 && flags & FLAG1)
882 1.46 rillig println("FLAG0 and FLAG1 are both set");
883 1.46 rillig
884 1.46 rillig if ((flags & (FLAG0 | FLAG1)) != 0)
885 1.46 rillig println("At least one of FLAG0 and FLAG1 is set");
886 1.46 rillig
887 1.46 rillig if (flags & FLAG28)
888 1.46 rillig println("FLAG28 is set");
889 1.47 rillig
890 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
891 1.47 rillig bool b0 = flags & FLAG0;
892 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
893 1.47 rillig bool b1 = flags & FLAG1;
894 1.47 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
895 1.47 rillig bool b28 = flags & FLAG28;
896 1.47 rillig return b0 || b1 || b28;
897 1.46 rillig }
898 1.46 rillig
899 1.10 rillig
900 1.12 rillig void
901 1.10 rillig strict_bool_operator_eq_bool_int(void)
902 1.10 rillig {
903 1.38 rillig /* expect+1: error: operands of '==' have incompatible types '_Bool' and 'int' [107] */
904 1.36 rillig (void)(strict_bool_conversion_return_false() == 0);
905 1.10 rillig }
906 1.13 rillig
907 1.13 rillig void
908 1.13 rillig strict_bool_assign_bit_field_then_compare(void)
909 1.13 rillig {
910 1.13 rillig struct s {
911 1.13 rillig bool flag: 1;
912 1.13 rillig };
913 1.13 rillig
914 1.13 rillig struct s s = { __lint_false };
915 1.13 rillig
916 1.36 rillig /* expect+1: warning: expression has null effect [129] */
917 1.36 rillig (void)((s.flag = s.flag) != __lint_false);
918 1.13 rillig }
919 1.18 rillig
920 1.18 rillig void
921 1.18 rillig bool_as_array_index(bool cond)
922 1.18 rillig {
923 1.18 rillig static const char *repr[] = { "no", "yes" };
924 1.18 rillig /*
925 1.18 rillig * The '+' in the error message reveals that lint internally
926 1.18 rillig * translates 'arr[ind]' to '*(arr + ind)' in an early stage of
927 1.18 rillig * parsing.
928 1.18 rillig */
929 1.36 rillig /* expect+1: error: right operand of '+' must not be bool [337] */
930 1.36 rillig println(repr[cond]);
931 1.18 rillig println(cond ? "yes" : "no");
932 1.18 rillig }
933 1.20 rillig
934 1.20 rillig void
935 1.23 rillig initialization(void)
936 1.23 rillig {
937 1.23 rillig struct {
938 1.23 rillig _Bool b;
939 1.23 rillig } var[] = {
940 1.23 rillig { __lint_false },
941 1.23 rillig { __lint_true },
942 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
943 1.36 rillig { 0 },
944 1.38 rillig /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
945 1.36 rillig { 1 },
946 1.23 rillig };
947 1.23 rillig }
948