Home | History | Annotate | Line # | Download | only in lint1
msg_259.c revision 1.15
      1 /*	$NetBSD: msg_259.c,v 1.15 2021/08/31 18:59:26 rillig Exp $	*/
      2 # 3 "msg_259.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 /* lint1-extra-flags: -h */
      8 
      9 void plain_char(char);
     10 void signed_int(int);
     11 void unsigned_int(unsigned int);
     12 void signed_long(long);
     13 void unsigned_long(unsigned long);
     14 void signed_long_long(long long);
     15 void unsigned_long_long(unsigned long long);
     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, long long sll)
     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 	/* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned int' due to prototype [259] */
     58 	unsigned_int(sll);
     59 
     60 	/*
     61 	 * XXX: Why no warning?  Even though 'unsigned long' is 64 bits
     62 	 * wide, it cannot represent negative 32-bit values.
     63 	 */
     64 	unsigned_long(si);
     65 
     66 	/* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
     67 	unsigned_long(sl);
     68 	/* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned long' due to prototype [259] */
     69 	unsigned_long(sll);
     70 
     71 	/*
     72 	 * XXX: Why no warning?  Even though 'unsigned long long' is 64 bits
     73 	 * wide, it cannot represent negative 32-bit values.
     74 	 */
     75 	unsigned_long_long(si);
     76 
     77 	/* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long long' due to prototype [259] */
     78 	unsigned_long_long(sl);
     79 
     80 	/* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned long long' due to prototype [259] */
     81 	unsigned_long_long(sll);
     82 }
     83 
     84 void
     85 unsigned_to_signed(unsigned int ui, unsigned long ul, unsigned long long ull)
     86 {
     87 	/* expect+1: warning: argument #1 is converted from 'unsigned int' to 'int' due to prototype [259] */
     88 	signed_int(ui);
     89 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'int' due to prototype [259] */
     90 	signed_int(ul);
     91 	/* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'int' due to prototype [259] */
     92 	signed_int(ull);
     93 	signed_long(ui);
     94 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259] */
     95 	signed_long(ul);
     96 	/* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'long' due to prototype [259] */
     97 	signed_long(ull);
     98 	signed_long_long(ui);
     99 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long long' due to prototype [259] */
    100 	signed_long_long(ul);
    101 	/* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'long long' due to prototype [259] */
    102 	signed_long_long(ull);
    103 }
    104 
    105 void
    106 signed_to_signed(signed int si, signed long sl, signed long long sll)
    107 {
    108 	signed_int(si);
    109 	/* expect+1: warning: argument #1 is converted from 'long' to 'int' due to prototype [259] */
    110 	signed_int(sl);
    111 	/* expect+1: warning: argument #1 is converted from 'long long' to 'int' due to prototype [259] */
    112 	signed_int(sll);
    113 	signed_long(si);
    114 	signed_long(sl);
    115 	/* expect+1: warning: argument #1 is converted from 'long long' to 'long' due to prototype [259] */
    116 	signed_long(sll);
    117 	signed_long_long(si);
    118 	/* expect+1: warning: argument #1 is converted from 'long' to 'long long' due to prototype [259] */
    119 	signed_long_long(sl);
    120 	signed_long_long(sll);
    121 }
    122 
    123 void
    124 unsigned_to_unsigned(unsigned int ui, unsigned long ul, unsigned long long ull)
    125 {
    126 	unsigned_int(ui);
    127 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
    128 	unsigned_int(ul);
    129 	/* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'unsigned int' due to prototype [259] */
    130 	unsigned_int(ull);
    131 	unsigned_long(ui);
    132 	unsigned_long(ul);
    133 	/* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'unsigned long' due to prototype [259] */
    134 	unsigned_long(ull);
    135 	unsigned_long_long(ui);
    136 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned long long' due to prototype [259] */
    137 	unsigned_long_long(ul);
    138 	unsigned_long_long(ull);
    139 }
    140 
    141 void
    142 pass_sizeof_as_smaller_type(void)
    143 {
    144 	/*
    145 	 * XXX: Even though the expression has type size_t, it has a constant
    146 	 * value that fits effortless into an 'unsigned int', it's so small
    147 	 * that it would even fit into a 3-bit bit-field, so lint should not
    148 	 * warn here.
    149 	 */
    150 	/* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
    151 	unsigned_int(sizeof(int));
    152 }
    153