PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / gcc.dg / format / pr72858.c
blobb8c5829ed23fcdd08c9654567aa6c82b03f73aa6
1 /* { dg-options "-Wformat -fdiagnostics-show-caret" } */
3 #include "format.h"
5 /* Various format tests, some containing type mismatches. Verify that for
6 the type mismatch cases that we offer "good" suggestions. Specifically,
7 any suggestions should preserve flags characters, field width and precision,
8 and, if possible, the conversion specifier character, whilst giving a
9 corrected length modifier appropriate to the argument type. */
11 /* Tests of "x" without a length modifier, with various param types.
12 Suggestions should preserve the "x" for integer arguments. */
14 void
15 test_x (char *d,
16 int iexpr, unsigned int uiexpr,
17 long lexpr, unsigned long ulexpr,
18 long long llexpr, unsigned long long ullexpr,
19 float fexpr, double dexpr, long double ldexpr,
20 void *ptr)
22 /* Integer arguments. */
24 sprintf (d, " %-8x ", iexpr);
25 sprintf (d, " %-8x ", uiexpr);
27 sprintf (d, " %-8x ", lexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long int'" } */
28 /* { dg-begin-multiline-output "" }
29 sprintf (d, " %-8x ", lexpr);
30 ~~~^ ~~~~~
31 %-8lx
32 { dg-end-multiline-output "" } */
33 sprintf (d, " %-8x ", ulexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int'" } */
34 /* { dg-begin-multiline-output "" }
35 sprintf (d, " %-8x ", ulexpr);
36 ~~~^ ~~~~~~
37 %-8lx
38 { dg-end-multiline-output "" } */
40 sprintf (d, " %-8x ", llexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long long int'" } */
41 /* { dg-begin-multiline-output "" }
42 sprintf (d, " %-8x ", llexpr);
43 ~~~^ ~~~~~~
44 %-8llx
45 { dg-end-multiline-output "" } */
46 sprintf (d, " %-8x ", ullexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long long unsigned int'" } */
47 /* { dg-begin-multiline-output "" }
48 sprintf (d, " %-8x ", ullexpr);
49 ~~~^ ~~~~~~~
50 %-8llx
51 { dg-end-multiline-output "" } */
53 /* Floating-point arguments. */
55 sprintf (d, " %-8x ", fexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'double'" } */
56 /* { dg-begin-multiline-output "" }
57 sprintf (d, " %-8x ", fexpr);
58 ~~~^ ~~~~~
59 %-8f
60 { dg-end-multiline-output "" } */
61 sprintf (d, " %-8x ", dexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'double'" } */
62 /* { dg-begin-multiline-output "" }
63 sprintf (d, " %-8x ", dexpr);
64 ~~~^ ~~~~~
65 %-8f
66 { dg-end-multiline-output "" } */
67 sprintf (d, " %-8x ", ldexpr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long double'" } */
68 /* { dg-begin-multiline-output "" }
69 sprintf (d, " %-8x ", ldexpr);
70 ~~~^ ~~~~~~
71 %-8Lf
72 { dg-end-multiline-output "" } */
74 /* Pointer. */
75 sprintf (d, " %-8x ", ptr); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'void \\*'" } */
76 /* { dg-begin-multiline-output "" }
77 sprintf (d, " %-8x ", ptr);
78 ~~~^ ~~~
79 %-8p
80 { dg-end-multiline-output "" } */
82 /* Something unrecognized. */
83 struct s { int i; };
84 struct s s;
85 sprintf (d, " %-8x ", s); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'struct s'" } */
86 /* { dg-begin-multiline-output "" }
87 sprintf (d, " %-8x ", s);
88 ~~~^ ~
89 { dg-end-multiline-output "" } */
92 /* Tests of "x" with "l", with various param types.
93 Suggestions should preserve the "x" for integer arguments. */
95 void
96 test_lx (char *d,
97 int iexpr, unsigned int uiexpr,
98 long lexpr, unsigned long ulexpr,
99 long long llexpr, unsigned long long ullexpr,
100 float fexpr, double dexpr, long double ldexpr)
102 /* Integer arguments. */
104 sprintf (d, " %-8lx ", iexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'int'" } */
105 /* { dg-begin-multiline-output "" }
106 sprintf (d, " %-8lx ", iexpr);
107 ~~~~^ ~~~~~
108 %-8x
109 { dg-end-multiline-output "" } */
110 sprintf (d, " %-8lx ", uiexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'unsigned int'" } */
111 /* { dg-begin-multiline-output "" }
112 sprintf (d, " %-8lx ", uiexpr);
113 ~~~~^ ~~~~~~
114 %-8x
115 { dg-end-multiline-output "" } */
117 sprintf (d, " %-8lx ", lexpr);
118 sprintf (d, " %-8lx ", ulexpr);
120 sprintf (d, " %-8lx ", llexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'long long int'" } */
121 /* { dg-begin-multiline-output "" }
122 sprintf (d, " %-8lx ", llexpr);
123 ~~~~^ ~~~~~~
124 %-8llx
125 { dg-end-multiline-output "" } */
126 sprintf (d, " %-8lx ", ullexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'long long unsigned int'" } */
127 /* { dg-begin-multiline-output "" }
128 sprintf (d, " %-8lx ", ullexpr);
129 ~~~~^ ~~~~~~~
130 %-8llx
131 { dg-end-multiline-output "" } */
133 /* Floating-point arguments. */
135 sprintf (d, " %-8lx ", fexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'double'" } */
136 /* { dg-begin-multiline-output "" }
137 sprintf (d, " %-8lx ", fexpr);
138 ~~~~^ ~~~~~
139 %-8f
140 { dg-end-multiline-output "" } */
141 sprintf (d, " %-8lx ", dexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'double'" } */
142 /* { dg-begin-multiline-output "" }
143 sprintf (d, " %-8lx ", dexpr);
144 ~~~~^ ~~~~~
145 %-8f
146 { dg-end-multiline-output "" } */
147 sprintf (d, " %-8lx ", ldexpr); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'long double'" } */
148 /* { dg-begin-multiline-output "" }
149 sprintf (d, " %-8lx ", ldexpr);
150 ~~~~^ ~~~~~~
151 %-8Lf
152 { dg-end-multiline-output "" } */
155 /* Tests of "o" without a length modifier, with various param types.
156 Suggestions should preserve the "o" for integer arguments. */
158 void
159 test_o (char *d,
160 int iexpr, unsigned int uiexpr,
161 long lexpr, unsigned long ulexpr,
162 long long llexpr, unsigned long long ullexpr)
164 /* Integer arguments. */
166 sprintf (d, " %-8o ", iexpr);
167 sprintf (d, " %-8o ", uiexpr);
169 sprintf (d, " %-8o ", lexpr); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long int'" } */
170 /* { dg-begin-multiline-output "" }
171 sprintf (d, " %-8o ", lexpr);
172 ~~~^ ~~~~~
173 %-8lo
174 { dg-end-multiline-output "" } */
175 sprintf (d, " %-8o ", ulexpr); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int'" } */
176 /* { dg-begin-multiline-output "" }
177 sprintf (d, " %-8o ", ulexpr);
178 ~~~^ ~~~~~~
179 %-8lo
180 { dg-end-multiline-output "" } */
182 sprintf (d, " %-8o ", llexpr); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long long int'" } */
183 /* { dg-begin-multiline-output "" }
184 sprintf (d, " %-8o ", llexpr);
185 ~~~^ ~~~~~~
186 %-8llo
187 { dg-end-multiline-output "" } */
188 sprintf (d, " %-8o ", ullexpr); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long long unsigned int'" } */
189 /* { dg-begin-multiline-output "" }
190 sprintf (d, " %-8o ", ullexpr);
191 ~~~^ ~~~~~~~
192 %-8llo
193 { dg-end-multiline-output "" } */
196 /* Tests of "o" with "l", with various param types.
197 Suggestions should preserve the "o" for integer arguments. */
199 void
200 test_lo (char *d,
201 int iexpr, unsigned int uiexpr,
202 long lexpr, unsigned long ulexpr,
203 long long llexpr, unsigned long long ullexpr)
205 /* Integer arguments. */
207 sprintf (d, " %-8lo ", iexpr); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'int'" } */
208 /* { dg-begin-multiline-output "" }
209 sprintf (d, " %-8lo ", iexpr);
210 ~~~~^ ~~~~~
211 %-8o
212 { dg-end-multiline-output "" } */
213 sprintf (d, " %-8lo ", uiexpr); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'unsigned int'" } */
214 /* { dg-begin-multiline-output "" }
215 sprintf (d, " %-8lo ", uiexpr);
216 ~~~~^ ~~~~~~
217 %-8o
218 { dg-end-multiline-output "" } */
220 sprintf (d, " %-8lo ", lexpr);
221 sprintf (d, " %-8lo ", ulexpr);
223 sprintf (d, " %-8lo ", llexpr); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'long long int'" } */
224 /* { dg-begin-multiline-output "" }
225 sprintf (d, " %-8lo ", llexpr);
226 ~~~~^ ~~~~~~
227 %-8llo
228 { dg-end-multiline-output "" } */
229 sprintf (d, " %-8lo ", ullexpr); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'long long unsigned int'" } */
230 /* { dg-begin-multiline-output "" }
231 sprintf (d, " %-8lo ", ullexpr);
232 ~~~~^ ~~~~~~~
233 %-8llo
234 { dg-end-multiline-output "" } */
237 /* Tests of "e" without a length modifier, with various param types.
238 Suggestions should preserve the "e" for float arguments. */
240 void
241 test_e (char *d, int iexpr, float fexpr, double dexpr, long double ldexpr)
243 /* Integer arguments. */
245 sprintf (d, " %-8e ", iexpr); /* { dg-warning "20: format '%e' expects argument of type 'double', but argument 3 has type 'int'" } */
246 /* { dg-begin-multiline-output "" }
247 sprintf (d, " %-8e ", iexpr);
248 ~~~^ ~~~~~
249 %-8d
250 { dg-end-multiline-output "" } */
252 /* Floating-point arguments. */
254 sprintf (d, " %-8e ", fexpr);
255 sprintf (d, " %-8e ", dexpr);
256 sprintf (d, " %-8e ", ldexpr); /* { dg-warning "20: format '%e' expects argument of type 'double', but argument 3 has type 'long double'" } */
257 /* { dg-begin-multiline-output "" }
258 sprintf (d, " %-8e ", ldexpr);
259 ~~~^ ~~~~~~
260 %-8Le
261 { dg-end-multiline-output "" } */
264 /* Tests of "e" with "L", with various param types.
265 Suggestions should preserve the "e" for float arguments. */
267 void
268 test_Le (char *d, int iexpr, float fexpr, double dexpr, long double ldexpr)
270 /* Integer arguments. */
272 sprintf (d, " %-8Le ", iexpr); /* { dg-warning "21: format '%Le' expects argument of type 'long double', but argument 3 has type 'int'" } */
273 /* { dg-begin-multiline-output "" }
274 sprintf (d, " %-8Le ", iexpr);
275 ~~~~^ ~~~~~
276 %-8d
277 { dg-end-multiline-output "" } */
279 /* Floating-point arguments. */
281 sprintf (d, " %-8Le ", fexpr); /* { dg-warning "21: format '%Le' expects argument of type 'long double', but argument 3 has type 'double'" } */
282 /* { dg-begin-multiline-output "" }
283 sprintf (d, " %-8Le ", fexpr);
284 ~~~~^ ~~~~~
285 %-8e
286 { dg-end-multiline-output "" } */
288 sprintf (d, " %-8Le ", dexpr); /* { dg-warning "21: format '%Le' expects argument of type 'long double', but argument 3 has type 'double'" } */
289 /* { dg-begin-multiline-output "" }
290 sprintf (d, " %-8Le ", dexpr);
291 ~~~~^ ~~~~~
292 %-8e
293 { dg-end-multiline-output "" } */
295 sprintf (d, " %-8Le ", ldexpr);
298 /* Tests of "E" without a length modifier, with various param types.
299 Suggestions should preserve the "E" for floating-point arguments. */
301 void
302 test_E (char *d, int iexpr, float fexpr, double dexpr, long double ldexpr)
304 /* Integer arguments. */
306 sprintf (d, " %-8E ", iexpr); /* { dg-warning "20: format '%E' expects argument of type 'double', but argument 3 has type 'int'" } */
307 /* { dg-begin-multiline-output "" }
308 sprintf (d, " %-8E ", iexpr);
309 ~~~^ ~~~~~
310 %-8d
311 { dg-end-multiline-output "" } */
313 /* Floating-point arguments. */
315 sprintf (d, " %-8E ", fexpr);
316 sprintf (d, " %-8E ", dexpr);
317 sprintf (d, " %-8E ", ldexpr); /* { dg-warning "20: format '%E' expects argument of type 'double', but argument 3 has type 'long double'" } */
318 /* { dg-begin-multiline-output "" }
319 sprintf (d, " %-8E ", ldexpr);
320 ~~~^ ~~~~~~
321 %-8LE
322 { dg-end-multiline-output "" } */
325 /* Tests of "E" with "L", with various param types.
326 Suggestions should preserve the "E" for floating-point arguments. */
328 void
329 test_LE (char *d, int iexpr, float fexpr, double dexpr, long double ldexpr)
331 /* Integer arguments. */
333 sprintf (d, " %-8LE ", iexpr); /* { dg-warning "21: format '%LE' expects argument of type 'long double', but argument 3 has type 'int'" } */
334 /* { dg-begin-multiline-output "" }
335 sprintf (d, " %-8LE ", iexpr);
336 ~~~~^ ~~~~~
337 %-8d
338 { dg-end-multiline-output "" } */
340 sprintf (d, " %-8LE ", fexpr); /* { dg-warning "21: format '%LE' expects argument of type 'long double', but argument 3 has type 'double'" } */
341 /* { dg-begin-multiline-output "" }
342 sprintf (d, " %-8LE ", fexpr);
343 ~~~~^ ~~~~~
344 %-8E
345 { dg-end-multiline-output "" } */
347 sprintf (d, " %-8LE ", dexpr); /* { dg-warning "21: format '%LE' expects argument of type 'long double', but argument 3 has type 'double'" } */
348 /* { dg-begin-multiline-output "" }
349 sprintf (d, " %-8LE ", dexpr);
350 ~~~~^ ~~~~~
351 %-8E
352 { dg-end-multiline-output "" } */
354 sprintf (d, " %-8LE ", ldexpr);
357 /* Test of a suggestion for a conversion specification containing
358 all features (flags, width, precision, length modifier), where
359 all the other arguments have mismatching types. */
361 void
362 test_everything (char *d, long lexpr)
364 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr); /* { dg-line test_everything_sprintf } */
366 /* { dg-warning "26: field width specifier '\\*' expects argument of type 'int', but argument 3 has type 'long int'" "" { target *-*-* } test_everything_sprintf } */
367 /* { dg-begin-multiline-output "" }
368 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr);
369 ~~~^~~~~~ ~~~~~
370 { dg-end-multiline-output "" } */
372 /* { dg-warning "28: field precision specifier '\\.\\*' expects argument of type 'int', but argument 4 has type 'long int'" "" { target *-*-* } test_everything_sprintf } */
373 /* { dg-begin-multiline-output "" }
374 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr);
375 ~~~~~^~~~ ~~~~~
376 { dg-end-multiline-output "" } */
378 /* { dg-warning "31: format '%lld' expects argument of type 'long long int', but argument 5 has type 'long int'" "" { target *-*-* } test_everything_sprintf } */
379 /* { dg-begin-multiline-output "" }
380 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr);
381 ~~~~~~~~^ ~~~~~
382 %-+*.*ld
383 { dg-end-multiline-output "" } */