xref: /netbsd-src/tests/usr.bin/xlint/lint1/d_c99_bool.c (revision 06dfa8449cb5e76c0044ec0f3badf7d5180af0f5)
1 /*	$NetBSD: d_c99_bool.c,v 1.10 2023/03/28 14:44:34 rillig Exp $	*/
2 # 3 "d_c99_bool.c"
3 
4 /*
5  * C99 6.3.1.2 says: "When any scalar value is converted to _Bool, the result
6  * is 0 if the value compares equal to 0; otherwise the result is 1."
7  *
8  * This is different from the other integer types, which get truncated or
9  * invoke undefined behavior.
10  */
11 
12 /* lint1-extra-flags: -X 351 */
13 
14 /* Below, each false statement produces "negative array dimension" [20]. */
15 
16 int int_0_converts_to_false[(_Bool)0 ? -1 : 1];
17 /* expect+1: error: negative array dimension (-1) [20] */
18 int int_0_converts_to_true_[(_Bool)0 ? 1 : -1];
19 
20 /* expect+1: error: negative array dimension (-1) [20] */
21 int int_1_converts_to_false[(_Bool)1 ? -1 : 1];
22 int int_1_converts_to_true_[(_Bool)1 ? 1 : -1];
23 
24 /* expect+1: error: negative array dimension (-1) [20] */
25 int int_2_converts_to_false[(_Bool)2 ? -1 : 1];
26 int int_2_converts_to_true_[(_Bool)2 ? 1 : -1];
27 
28 /* expect+1: error: negative array dimension (-1) [20] */
29 int int_256_converts_to_false[(_Bool)256 ? -1 : 1];
30 int int_256_converts_to_true_[(_Bool)256 ? 1 : -1];
31 
32 int null_pointer_converts_to_false[(_Bool)(void *)0 ? -1 : 1];
33 /* expect+1: error: negative array dimension (-1) [20] */
34 int null_pointer_converts_to_true_[(_Bool)(void *)0 ? 1 : -1];
35 
36 /*
37  * XXX: lint does not treat the address of a global variable as a constant
38  * expression.  This goes against C99 6.6p7 but is probably not too relevant
39  * in practice.
40  *
41  * In such a case, to_int_constant(tn, false) in cgram.y:array_size_opt
42  * returns 1 for the array size.  This is why neither of the following array
43  * declarations generates an error message.
44  */
45 char ch;
46 int nonnull_pointer_converts_to_false[(_Bool)&ch ? -1 : 1];
47 int nonnull_pointer_converts_to_true_[(_Bool)&ch ? 1 : -1];
48 
49 /* expect+1: error: negative array dimension (-1) [20] */
50 int double_minus_1_0_converts_to_false[(_Bool)-1.0 ? -1 : 1];
51 int double_minus_1_0_converts_to_true_[(_Bool)-1.0 ? 1 : -1];
52 
53 /* expect+1: error: negative array dimension (-1) [20] */
54 int double_minus_0_5_converts_to_false[(_Bool)-0.5 ? -1 : 1];
55 int double_minus_0_5_converts_to_true_[(_Bool)-0.5 ? 1 : -1];
56 
57 int double_minus_0_0_converts_to_false[(_Bool)-0.0 ? -1 : 1];
58 /* expect+1: error: negative array dimension (-1) [20] */
59 int double_minus_0_0_converts_to_true_[(_Bool)-0.0 ? 1 : -1];
60 
61 int double_0_0_converts_to_false[(_Bool)0.0 ? -1 : 1];
62 /* expect+1: error: negative array dimension (-1) [20] */
63 int double_0_0_converts_to_true_[(_Bool)0.0 ? 1 : -1];
64 
65 /* The C99 rationale explains in 6.3.1.2 why (_Bool)0.5 is true. */
66 /* expect+1: error: negative array dimension (-1) [20] */
67 int double_0_5_converts_to_false[(_Bool)0.5 ? -1 : 1];
68 int double_0_5_converts_to_true_[(_Bool)0.5 ? 1 : -1];
69 
70 /* expect+1: error: negative array dimension (-1) [20] */
71 int double_1_0_converts_to_false[(_Bool)1.0 ? -1 : 1];
72 int double_1_0_converts_to_true_[(_Bool)1.0 ? 1 : -1];
73 
74 _Bool
75 bool_to_bool(_Bool b)
76 {
77 	return b;
78 }
79 
80 _Bool
81 char_to_bool(char c)
82 {
83 	return c;
84 }
85 
86 _Bool
87 int_to_bool(int i)
88 {
89 	return i;
90 }
91 
92 _Bool
93 double_to_bool(double d)
94 {
95 	return d;
96 }
97 
98 enum color {
99 	RED
100 };
101 
102 _Bool
103 enum_to_bool(enum color e)
104 {
105 	return e;
106 }
107 
108 _Bool
109 pointer_to_bool(const char *p)
110 {
111 	return p;
112 }
113 
114 _Bool
115 function_pointer_to_bool(void (*f)(void))
116 {
117 	return f;
118 }
119 
120 _Bool
121 complex_to_bool(double _Complex c)
122 {
123 	return c;
124 }
125