1 /* $NetBSD: d_c99_bool.c,v 1.9 2022/01/16 08:40:31 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 /* Below, each false statement produces "negative array dimension" [20]. */ 13 14 int int_0_converts_to_false[(_Bool)0 ? -1 : 1]; 15 /* expect+1: error: negative array dimension (-1) [20] */ 16 int int_0_converts_to_true_[(_Bool)0 ? 1 : -1]; 17 18 /* expect+1: error: negative array dimension (-1) [20] */ 19 int int_1_converts_to_false[(_Bool)1 ? -1 : 1]; 20 int int_1_converts_to_true_[(_Bool)1 ? 1 : -1]; 21 22 /* expect+1: error: negative array dimension (-1) [20] */ 23 int int_2_converts_to_false[(_Bool)2 ? -1 : 1]; 24 int int_2_converts_to_true_[(_Bool)2 ? 1 : -1]; 25 26 /* expect+1: error: negative array dimension (-1) [20] */ 27 int int_256_converts_to_false[(_Bool)256 ? -1 : 1]; 28 int int_256_converts_to_true_[(_Bool)256 ? 1 : -1]; 29 30 int null_pointer_converts_to_false[(_Bool)(void *)0 ? -1 : 1]; 31 /* expect+1: error: negative array dimension (-1) [20] */ 32 int null_pointer_converts_to_true_[(_Bool)(void *)0 ? 1 : -1]; 33 34 /* 35 * XXX: lint does not treat the address of a global variable as a constant 36 * expression. This goes against C99 6.6p7 but is probably not too relevant 37 * in practice. 38 * 39 * In such a case, to_int_constant(tn, false) in cgram.y:array_size_opt 40 * returns 1 for the array size. This is why neither of the following array 41 * declarations generates an error message. 42 */ 43 char ch; 44 int nonnull_pointer_converts_to_false[(_Bool)&ch ? -1 : 1]; 45 int nonnull_pointer_converts_to_true_[(_Bool)&ch ? 1 : -1]; 46 47 /* expect+1: error: negative array dimension (-1) [20] */ 48 int double_minus_1_0_converts_to_false[(_Bool)-1.0 ? -1 : 1]; 49 int double_minus_1_0_converts_to_true_[(_Bool)-1.0 ? 1 : -1]; 50 51 /* expect+1: error: negative array dimension (-1) [20] */ 52 int double_minus_0_5_converts_to_false[(_Bool)-0.5 ? -1 : 1]; 53 int double_minus_0_5_converts_to_true_[(_Bool)-0.5 ? 1 : -1]; 54 55 int double_minus_0_0_converts_to_false[(_Bool)-0.0 ? -1 : 1]; 56 /* expect+1: error: negative array dimension (-1) [20] */ 57 int double_minus_0_0_converts_to_true_[(_Bool)-0.0 ? 1 : -1]; 58 59 int double_0_0_converts_to_false[(_Bool)0.0 ? -1 : 1]; 60 /* expect+1: error: negative array dimension (-1) [20] */ 61 int double_0_0_converts_to_true_[(_Bool)0.0 ? 1 : -1]; 62 63 /* The C99 rationale explains in 6.3.1.2 why (_Bool)0.5 is true. */ 64 /* expect+1: error: negative array dimension (-1) [20] */ 65 int double_0_5_converts_to_false[(_Bool)0.5 ? -1 : 1]; 66 int double_0_5_converts_to_true_[(_Bool)0.5 ? 1 : -1]; 67 68 /* expect+1: error: negative array dimension (-1) [20] */ 69 int double_1_0_converts_to_false[(_Bool)1.0 ? -1 : 1]; 70 int double_1_0_converts_to_true_[(_Bool)1.0 ? 1 : -1]; 71 72 _Bool 73 bool_to_bool(_Bool b) 74 { 75 return b; 76 } 77 78 _Bool 79 char_to_bool(char c) 80 { 81 return c; 82 } 83 84 _Bool 85 int_to_bool(int i) 86 { 87 return i; 88 } 89 90 _Bool 91 double_to_bool(double d) 92 { 93 return d; 94 } 95 96 enum color { 97 RED 98 }; 99 100 _Bool 101 enum_to_bool(enum color e) 102 { 103 return e; 104 } 105 106 _Bool 107 pointer_to_bool(const char *p) 108 { 109 return p; 110 } 111 112 _Bool 113 function_pointer_to_bool(void (*f)(void)) 114 { 115 return f; 116 } 117 118 _Bool 119 complex_to_bool(double _Complex c) 120 { 121 return c; 122 } 123