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