Home | History | Annotate | Line # | Download | only in lint1
msg_259_c90.c revision 1.1
      1 /*	$NetBSD: msg_259_c90.c,v 1.1 2021/08/31 18:59:26 rillig Exp $	*/
      2 # 3 "msg_259_c90.c"
      3 
      4 /* Test for message: argument #%d is converted from '%s' to '%s' due to prototype [259] */
      5 
      6 /* lint1-only-if: lp64 */
      7 /* XXX: The flag '-s' suppresses all warnings.  Why? */
      8 /* lint1-flags: -h -w */
      9 
     10 void plain_char(char);
     11 void signed_int(int);
     12 void unsigned_int(unsigned int);
     13 void signed_long(long);
     14 void unsigned_long(unsigned long);
     15 /* No 'long long' since it requires C99. */
     16 
     17 void
     18 change_in_type_width(char c, int i, long l)
     19 {
     20 	plain_char(c);
     21 	signed_int(c);
     22 	/* No warning 259 on LP64, only on ILP32 */
     23 	signed_long(c);
     24 
     25 	plain_char(i);		/* XXX: why no warning? */
     26 	signed_int(i);
     27 	/* No warning 259 on LP64, only on ILP32 */
     28 	signed_long(i);
     29 
     30 	plain_char(l);		/* XXX: why no warning? */
     31 	/* expect+1: from 'long' to 'int' due to prototype [259] */
     32 	signed_int(l);
     33 	signed_long(l);
     34 }
     35 
     36 /*
     37  * Converting a signed integer type to its corresponding unsigned integer
     38  * type (C99 6.2.5p6) is usually not a problem since the actual values of the
     39  * expressions are usually not anywhere near the maximum signed value.  From
     40  * a technical standpoint, it is correct to warn here since even small
     41  * negative numbers may result in very large positive numbers.
     42  *
     43  * A common case where it occurs is when the difference of two pointers is
     44  * converted to size_t.  The type ptrdiff_t is defined to be signed, but in
     45  * many practical cases, the expression is '(end - start)', which makes the
     46  * resulting value necessarily positive.
     47  */
     48 void
     49 signed_to_unsigned(int si, long sl)
     50 {
     51 	/* expect+1: warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259] */
     52 	unsigned_int(si);
     53 
     54 	/* expect+1: warning: argument #1 is converted from 'long' to 'unsigned int' due to prototype [259] */
     55 	unsigned_int(sl);
     56 
     57 	/*
     58 	 * XXX: Why no warning?  Even though 'unsigned long' is 64 bits
     59 	 * wide, it cannot represent negative 32-bit values.
     60 	 */
     61 	unsigned_long(si);
     62 
     63 	/* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
     64 	unsigned_long(sl);
     65 }
     66 
     67 void
     68 unsigned_to_signed(unsigned int ui, unsigned long ul)
     69 {
     70 	/* expect+1: warning: argument #1 is converted from 'unsigned int' to 'int' due to prototype [259] */
     71 	signed_int(ui);
     72 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'int' due to prototype [259] */
     73 	signed_int(ul);
     74 	signed_long(ui);
     75 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259] */
     76 	signed_long(ul);
     77 }
     78 
     79 void
     80 signed_to_signed(signed int si, signed long sl)
     81 {
     82 	signed_int(si);
     83 	/* expect+1: warning: argument #1 is converted from 'long' to 'int' due to prototype [259] */
     84 	signed_int(sl);
     85 	signed_long(si);
     86 	signed_long(sl);
     87 }
     88 
     89 void
     90 unsigned_to_unsigned(unsigned int ui, unsigned long ul)
     91 {
     92 	unsigned_int(ui);
     93 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
     94 	unsigned_int(ul);
     95 	unsigned_long(ui);
     96 	unsigned_long(ul);
     97 }
     98 
     99 void
    100 pass_sizeof_as_smaller_type(void)
    101 {
    102 	/*
    103 	 * XXX: Even though the expression has type size_t, it has a constant
    104 	 * value that fits effortless into an 'unsigned int', it's so small
    105 	 * that it would even fit into a 3-bit bit-field, so lint should not
    106 	 * warn here.
    107 	 */
    108 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
    109 	unsigned_int(sizeof(int));
    110 }
    111