msg_259_c90.c revision 1.4 1 1.4 rillig /* $NetBSD: msg_259_c90.c,v 1.4 2022/06/17 18:54:53 rillig Exp $ */
2 1.1 rillig # 3 "msg_259_c90.c"
3 1.1 rillig
4 1.1 rillig /* Test for message: argument #%d is converted from '%s' to '%s' due to prototype [259] */
5 1.1 rillig
6 1.2 rillig /*
7 1.3 rillig * This warning detects function calls that are translated in very different
8 1.3 rillig * translation environments, one where prototypes are omitted, and one where
9 1.3 rillig * prototypes are active. It is possible to make such code interoperable,
10 1.3 rillig * but that requires that each argument is converted to its proper type by
11 1.3 rillig * the caller of the function.
12 1.3 rillig *
13 1.3 rillig * When lint is run with the '-s' flag, it no longer warns about code with
14 1.3 rillig * incompatibilities between traditional C and C90, therefore this test omits
15 1.3 rillig * all of the options '-t', '-s', '-S' and '-Ac11'.
16 1.3 rillig *
17 1.3 rillig * See also msg_297, which is about lossy integer conversions, but that
18 1.3 rillig * requires the flags -a -p -P, which are not enabled in the default NetBSD
19 1.3 rillig * build.
20 1.2 rillig */
21 1.2 rillig
22 1.1 rillig /* lint1-only-if: lp64 */
23 1.1 rillig /* lint1-flags: -h -w */
24 1.1 rillig
25 1.1 rillig void plain_char(char);
26 1.3 rillig void signed_char(signed char);
27 1.3 rillig void unsigned_char(unsigned char);
28 1.3 rillig void signed_short(signed short);
29 1.3 rillig void unsigned_short(unsigned short);
30 1.1 rillig void signed_int(int);
31 1.1 rillig void unsigned_int(unsigned int);
32 1.1 rillig void signed_long(long);
33 1.1 rillig void unsigned_long(unsigned long);
34 1.1 rillig /* No 'long long' since it requires C99. */
35 1.1 rillig
36 1.1 rillig void
37 1.1 rillig change_in_type_width(char c, int i, long l)
38 1.1 rillig {
39 1.1 rillig plain_char(c);
40 1.1 rillig signed_int(c);
41 1.1 rillig /* No warning 259 on LP64, only on ILP32 */
42 1.1 rillig signed_long(c);
43 1.1 rillig
44 1.1 rillig plain_char(i); /* XXX: why no warning? */
45 1.1 rillig signed_int(i);
46 1.1 rillig /* No warning 259 on LP64, only on ILP32 */
47 1.1 rillig signed_long(i);
48 1.1 rillig
49 1.1 rillig plain_char(l); /* XXX: why no warning? */
50 1.4 rillig /* expect+1: ... from 'long' to 'int' due to prototype [259] */
51 1.1 rillig signed_int(l);
52 1.1 rillig signed_long(l);
53 1.1 rillig }
54 1.1 rillig
55 1.1 rillig /*
56 1.1 rillig * Converting a signed integer type to its corresponding unsigned integer
57 1.1 rillig * type (C99 6.2.5p6) is usually not a problem since the actual values of the
58 1.1 rillig * expressions are usually not anywhere near the maximum signed value. From
59 1.1 rillig * a technical standpoint, it is correct to warn here since even small
60 1.1 rillig * negative numbers may result in very large positive numbers.
61 1.1 rillig *
62 1.1 rillig * A common case where it occurs is when the difference of two pointers is
63 1.1 rillig * converted to size_t. The type ptrdiff_t is defined to be signed, but in
64 1.1 rillig * many practical cases, the expression is '(end - start)', which makes the
65 1.1 rillig * resulting value necessarily positive.
66 1.1 rillig */
67 1.1 rillig void
68 1.3 rillig small_integer_types(char c, signed char sc, unsigned char uc,
69 1.3 rillig signed short ss, unsigned short us,
70 1.3 rillig signed int si, unsigned int ui,
71 1.3 rillig signed long sl, unsigned long ul)
72 1.3 rillig {
73 1.3 rillig plain_char(c);
74 1.3 rillig plain_char(sc);
75 1.3 rillig plain_char(uc);
76 1.3 rillig plain_char(ss);
77 1.3 rillig plain_char(us);
78 1.3 rillig plain_char(si);
79 1.3 rillig plain_char(ui);
80 1.3 rillig plain_char(sl);
81 1.3 rillig plain_char(ul);
82 1.3 rillig
83 1.3 rillig signed_char(c);
84 1.3 rillig signed_char(sc);
85 1.3 rillig signed_char(uc);
86 1.3 rillig signed_char(ss);
87 1.3 rillig signed_char(us);
88 1.3 rillig signed_char(si);
89 1.3 rillig signed_char(ui);
90 1.3 rillig signed_char(sl);
91 1.3 rillig signed_char(ul);
92 1.3 rillig
93 1.3 rillig unsigned_char(c);
94 1.3 rillig unsigned_char(sc);
95 1.3 rillig unsigned_char(uc);
96 1.3 rillig unsigned_char(ss);
97 1.3 rillig unsigned_char(us);
98 1.3 rillig unsigned_char(si);
99 1.3 rillig unsigned_char(ui);
100 1.3 rillig unsigned_char(sl);
101 1.3 rillig unsigned_char(ul);
102 1.3 rillig
103 1.3 rillig signed_short(c);
104 1.3 rillig signed_short(sc);
105 1.3 rillig signed_short(uc);
106 1.3 rillig signed_short(ss);
107 1.3 rillig signed_short(us);
108 1.3 rillig signed_short(si);
109 1.3 rillig signed_short(ui);
110 1.3 rillig signed_short(sl);
111 1.3 rillig signed_short(ul);
112 1.3 rillig
113 1.3 rillig unsigned_short(c);
114 1.3 rillig unsigned_short(sc);
115 1.3 rillig unsigned_short(uc);
116 1.3 rillig unsigned_short(ss);
117 1.3 rillig unsigned_short(us);
118 1.3 rillig unsigned_short(si);
119 1.3 rillig unsigned_short(ui);
120 1.3 rillig unsigned_short(sl);
121 1.3 rillig unsigned_short(ul);
122 1.3 rillig }
123 1.3 rillig
124 1.3 rillig /*
125 1.3 rillig * This function tests, among others, the conversion from a signed integer
126 1.3 rillig * type to its corresponding unsigned integer type. Warning 259 is not
127 1.3 rillig * about lossy integer conversions but about ABI calling conventions.
128 1.3 rillig *
129 1.3 rillig * A common case where a conversion from a signed integer type to its
130 1.3 rillig * corresponding unsigned integer type occurs is when the difference of two
131 1.3 rillig * pointers is converted to size_t. The type ptrdiff_t is defined to be
132 1.3 rillig * signed, but in many practical cases, the expression is '(end - start)',
133 1.3 rillig * which makes the resulting value necessarily positive.
134 1.3 rillig */
135 1.3 rillig void
136 1.1 rillig signed_to_unsigned(int si, long sl)
137 1.1 rillig {
138 1.1 rillig /* expect+1: warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259] */
139 1.1 rillig unsigned_int(si);
140 1.1 rillig
141 1.1 rillig /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned int' due to prototype [259] */
142 1.1 rillig unsigned_int(sl);
143 1.1 rillig
144 1.1 rillig /*
145 1.3 rillig * No warning here. Even though 'unsigned long' is 64 bits wide, it
146 1.3 rillig * cannot represent negative 32-bit values. This lossy conversion is
147 1.3 rillig * covered by message 297 instead, which requires nonstandard flags.
148 1.1 rillig */
149 1.1 rillig unsigned_long(si);
150 1.1 rillig
151 1.1 rillig /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
152 1.1 rillig unsigned_long(sl);
153 1.1 rillig }
154 1.1 rillig
155 1.1 rillig void
156 1.1 rillig unsigned_to_signed(unsigned int ui, unsigned long ul)
157 1.1 rillig {
158 1.1 rillig /* expect+1: warning: argument #1 is converted from 'unsigned int' to 'int' due to prototype [259] */
159 1.1 rillig signed_int(ui);
160 1.1 rillig /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'int' due to prototype [259] */
161 1.1 rillig signed_int(ul);
162 1.1 rillig signed_long(ui);
163 1.1 rillig /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259] */
164 1.1 rillig signed_long(ul);
165 1.1 rillig }
166 1.1 rillig
167 1.1 rillig void
168 1.1 rillig signed_to_signed(signed int si, signed long sl)
169 1.1 rillig {
170 1.1 rillig signed_int(si);
171 1.1 rillig /* expect+1: warning: argument #1 is converted from 'long' to 'int' due to prototype [259] */
172 1.1 rillig signed_int(sl);
173 1.1 rillig signed_long(si);
174 1.1 rillig signed_long(sl);
175 1.1 rillig }
176 1.1 rillig
177 1.1 rillig void
178 1.1 rillig unsigned_to_unsigned(unsigned int ui, unsigned long ul)
179 1.1 rillig {
180 1.1 rillig unsigned_int(ui);
181 1.1 rillig /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
182 1.1 rillig unsigned_int(ul);
183 1.1 rillig unsigned_long(ui);
184 1.1 rillig unsigned_long(ul);
185 1.1 rillig }
186 1.1 rillig
187 1.1 rillig void
188 1.1 rillig pass_sizeof_as_smaller_type(void)
189 1.1 rillig {
190 1.1 rillig /*
191 1.3 rillig * Even though the expression has type size_t, it has a constant
192 1.1 rillig * value that fits effortless into an 'unsigned int', it's so small
193 1.3 rillig * that it would even fit into a 3-bit bit-field, so lint's warning
194 1.3 rillig * may seem wrong here.
195 1.3 rillig *
196 1.3 rillig * This warning 259 is not about lossy integer conversion though but
197 1.3 rillig * instead covers calling conventions that may differ between integer
198 1.3 rillig * types of different sizes, and from that point of view, the
199 1.3 rillig * constant, even though its value would fit in an unsigned int, is
200 1.3 rillig * still passed as size_t.
201 1.1 rillig */
202 1.1 rillig /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
203 1.1 rillig unsigned_int(sizeof(int));
204 1.1 rillig }
205