1 /* $NetBSD: d_c99_bool.c,v 1.8 2022/01/15 14:22:03 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 * The call to constant(tn, 0) defaults to 1, then. This is why neither of 40 * the following array declarations generates an error message. 41 */ 42 char ch; 43 int nonnull_pointer_converts_to_false[(_Bool)&ch ? -1 : 1]; 44 int nonnull_pointer_converts_to_true_[(_Bool)&ch ? 1 : -1]; 45 46 /* expect+1: error: negative array dimension (-1) [20] */ 47 int double_minus_1_0_converts_to_false[(_Bool)-1.0 ? -1 : 1]; 48 int double_minus_1_0_converts_to_true_[(_Bool)-1.0 ? 1 : -1]; 49 50 /* expect+1: error: negative array dimension (-1) [20] */ 51 int double_minus_0_5_converts_to_false[(_Bool)-0.5 ? -1 : 1]; 52 int double_minus_0_5_converts_to_true_[(_Bool)-0.5 ? 1 : -1]; 53 54 int double_minus_0_0_converts_to_false[(_Bool)-0.0 ? -1 : 1]; 55 /* expect+1: error: negative array dimension (-1) [20] */ 56 int double_minus_0_0_converts_to_true_[(_Bool)-0.0 ? 1 : -1]; 57 58 int double_0_0_converts_to_false[(_Bool)0.0 ? -1 : 1]; 59 /* expect+1: error: negative array dimension (-1) [20] */ 60 int double_0_0_converts_to_true_[(_Bool)0.0 ? 1 : -1]; 61 62 /* The C99 rationale explains in 6.3.1.2 why (_Bool)0.5 is true. */ 63 /* expect+1: error: negative array dimension (-1) [20] */ 64 int double_0_5_converts_to_false[(_Bool)0.5 ? -1 : 1]; 65 int double_0_5_converts_to_true_[(_Bool)0.5 ? 1 : -1]; 66 67 /* expect+1: error: negative array dimension (-1) [20] */ 68 int double_1_0_converts_to_false[(_Bool)1.0 ? -1 : 1]; 69 int double_1_0_converts_to_true_[(_Bool)1.0 ? 1 : -1]; 70 71 _Bool 72 bool_to_bool(_Bool b) 73 { 74 return b; 75 } 76 77 _Bool 78 char_to_bool(char c) 79 { 80 return c; 81 } 82 83 _Bool 84 int_to_bool(int i) 85 { 86 return i; 87 } 88 89 _Bool 90 double_to_bool(double d) 91 { 92 return d; 93 } 94 95 enum color { 96 RED 97 }; 98 99 _Bool 100 enum_to_bool(enum color e) 101 { 102 return e; 103 } 104 105 _Bool 106 pointer_to_bool(const char *p) 107 { 108 return p; 109 } 110 111 _Bool 112 function_pointer_to_bool(void (*f)(void)) 113 { 114 return f; 115 } 116 117 _Bool 118 complex_to_bool(double _Complex c) 119 { 120 return c; 121 } 122