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