1 /* { dg-options "-Wformat -fdiagnostics-show-caret" } */
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. */
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
,
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);
35 { dg-end-multiline-output "" } */
36 sprintf (d
, " %-8x ", ulexpr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int'" } */
37 /* { dg-begin-multiline-output "" }
38 sprintf (d, " %-8x ", ulexpr);
44 { dg-end-multiline-output "" } */
46 sprintf (d
, " %-8x ", llexpr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long long int'" } */
47 /* { dg-begin-multiline-output "" }
48 sprintf (d, " %-8x ", llexpr);
54 { dg-end-multiline-output "" } */
55 sprintf (d
, " %-8x ", ullexpr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long long unsigned int'" } */
56 /* { dg-begin-multiline-output "" }
57 sprintf (d, " %-8x ", ullexpr);
60 | long long unsigned int
63 { dg-end-multiline-output "" } */
65 /* Floating-point arguments. */
67 sprintf (d
, " %-8x ", fexpr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'double'" } */
68 /* { dg-begin-multiline-output "" }
69 sprintf (d, " %-8x ", fexpr);
75 { dg-end-multiline-output "" } */
76 sprintf (d
, " %-8x ", dexpr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'double'" } */
77 /* { dg-begin-multiline-output "" }
78 sprintf (d, " %-8x ", dexpr);
84 { dg-end-multiline-output "" } */
85 sprintf (d
, " %-8x ", ldexpr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long double'" } */
86 /* { dg-begin-multiline-output "" }
87 sprintf (d, " %-8x ", ldexpr);
93 { dg-end-multiline-output "" } */
96 sprintf (d
, " %-8x ", ptr
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'void \\*'" } */
97 /* { dg-begin-multiline-output "" }
98 sprintf (d, " %-8x ", ptr);
104 { dg-end-multiline-output "" } */
106 /* Something unrecognized. */
109 sprintf (d
, " %-8x ", s
); /* { dg-warning "20: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'struct s'" } */
110 /* { dg-begin-multiline-output "" }
111 sprintf (d, " %-8x ", s);
116 { dg-end-multiline-output "" } */
119 /* Tests of "x" with "l", with various param types.
120 Suggestions should preserve the "x" for integer arguments. */
124 int iexpr
, unsigned int uiexpr
,
125 long lexpr
, unsigned long ulexpr
,
126 long long llexpr
, unsigned long long ullexpr
,
127 float fexpr
, double dexpr
, long double ldexpr
)
129 /* Integer arguments. */
131 sprintf (d
, " %-8lx ", iexpr
); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'int'" } */
132 /* { dg-begin-multiline-output "" }
133 sprintf (d, " %-8lx ", iexpr);
139 { dg-end-multiline-output "" } */
140 sprintf (d
, " %-8lx ", uiexpr
); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'unsigned int'" } */
141 /* { dg-begin-multiline-output "" }
142 sprintf (d, " %-8lx ", uiexpr);
148 { dg-end-multiline-output "" } */
150 sprintf (d
, " %-8lx ", lexpr
);
151 sprintf (d
, " %-8lx ", ulexpr
);
153 sprintf (d
, " %-8lx ", llexpr
); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'long long int'" } */
154 /* { dg-begin-multiline-output "" }
155 sprintf (d, " %-8lx ", llexpr);
161 { dg-end-multiline-output "" } */
162 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'" } */
163 /* { dg-begin-multiline-output "" }
164 sprintf (d, " %-8lx ", ullexpr);
167 | long long unsigned int
170 { dg-end-multiline-output "" } */
172 /* Floating-point arguments. */
174 sprintf (d
, " %-8lx ", fexpr
); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'double'" } */
175 /* { dg-begin-multiline-output "" }
176 sprintf (d, " %-8lx ", fexpr);
182 { dg-end-multiline-output "" } */
183 sprintf (d
, " %-8lx ", dexpr
); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'double'" } */
184 /* { dg-begin-multiline-output "" }
185 sprintf (d, " %-8lx ", dexpr);
191 { dg-end-multiline-output "" } */
192 sprintf (d
, " %-8lx ", ldexpr
); /* { dg-warning "21: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'long double'" } */
193 /* { dg-begin-multiline-output "" }
194 sprintf (d, " %-8lx ", ldexpr);
200 { dg-end-multiline-output "" } */
203 /* Tests of "o" without a length modifier, with various param types.
204 Suggestions should preserve the "o" for integer arguments. */
208 int iexpr
, unsigned int uiexpr
,
209 long lexpr
, unsigned long ulexpr
,
210 long long llexpr
, unsigned long long ullexpr
)
212 /* Integer arguments. */
214 sprintf (d
, " %-8o ", iexpr
);
215 sprintf (d
, " %-8o ", uiexpr
);
217 sprintf (d
, " %-8o ", lexpr
); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long int'" } */
218 /* { dg-begin-multiline-output "" }
219 sprintf (d, " %-8o ", lexpr);
225 { dg-end-multiline-output "" } */
226 sprintf (d
, " %-8o ", ulexpr
); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int'" } */
227 /* { dg-begin-multiline-output "" }
228 sprintf (d, " %-8o ", ulexpr);
234 { dg-end-multiline-output "" } */
236 sprintf (d
, " %-8o ", llexpr
); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long long int'" } */
237 /* { dg-begin-multiline-output "" }
238 sprintf (d, " %-8o ", llexpr);
244 { dg-end-multiline-output "" } */
245 sprintf (d
, " %-8o ", ullexpr
); /* { dg-warning "20: format '%o' expects argument of type 'unsigned int', but argument 3 has type 'long long unsigned int'" } */
246 /* { dg-begin-multiline-output "" }
247 sprintf (d, " %-8o ", ullexpr);
250 | long long unsigned int
253 { dg-end-multiline-output "" } */
256 /* Tests of "o" with "l", with various param types.
257 Suggestions should preserve the "o" for integer arguments. */
261 int iexpr
, unsigned int uiexpr
,
262 long lexpr
, unsigned long ulexpr
,
263 long long llexpr
, unsigned long long ullexpr
)
265 /* Integer arguments. */
267 sprintf (d
, " %-8lo ", iexpr
); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'int'" } */
268 /* { dg-begin-multiline-output "" }
269 sprintf (d, " %-8lo ", iexpr);
275 { dg-end-multiline-output "" } */
276 sprintf (d
, " %-8lo ", uiexpr
); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'unsigned int'" } */
277 /* { dg-begin-multiline-output "" }
278 sprintf (d, " %-8lo ", uiexpr);
284 { dg-end-multiline-output "" } */
286 sprintf (d
, " %-8lo ", lexpr
);
287 sprintf (d
, " %-8lo ", ulexpr
);
289 sprintf (d
, " %-8lo ", llexpr
); /* { dg-warning "21: format '%lo' expects argument of type 'long unsigned int', but argument 3 has type 'long long int'" } */
290 /* { dg-begin-multiline-output "" }
291 sprintf (d, " %-8lo ", llexpr);
297 { dg-end-multiline-output "" } */
298 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'" } */
299 /* { dg-begin-multiline-output "" }
300 sprintf (d, " %-8lo ", ullexpr);
303 | long long unsigned int
306 { dg-end-multiline-output "" } */
309 /* Tests of "e" without a length modifier, with various param types.
310 Suggestions should preserve the "e" for float arguments. */
313 test_e (char *d
, int iexpr
, float fexpr
, double dexpr
, long double ldexpr
)
315 /* Integer arguments. */
317 sprintf (d
, " %-8e ", iexpr
); /* { dg-warning "20: format '%e' expects argument of type 'double', but argument 3 has type 'int'" } */
318 /* { dg-begin-multiline-output "" }
319 sprintf (d, " %-8e ", iexpr);
325 { dg-end-multiline-output "" } */
327 /* Floating-point arguments. */
329 sprintf (d
, " %-8e ", fexpr
);
330 sprintf (d
, " %-8e ", dexpr
);
331 sprintf (d
, " %-8e ", ldexpr
); /* { dg-warning "20: format '%e' expects argument of type 'double', but argument 3 has type 'long double'" } */
332 /* { dg-begin-multiline-output "" }
333 sprintf (d, " %-8e ", ldexpr);
339 { dg-end-multiline-output "" } */
342 /* Tests of "e" with "L", with various param types.
343 Suggestions should preserve the "e" for float arguments. */
346 test_Le (char *d
, int iexpr
, float fexpr
, double dexpr
, long double ldexpr
)
348 /* Integer arguments. */
350 sprintf (d
, " %-8Le ", iexpr
); /* { dg-warning "21: format '%Le' expects argument of type 'long double', but argument 3 has type 'int'" } */
351 /* { dg-begin-multiline-output "" }
352 sprintf (d, " %-8Le ", iexpr);
358 { dg-end-multiline-output "" } */
360 /* Floating-point arguments. */
362 sprintf (d
, " %-8Le ", fexpr
); /* { dg-warning "21: format '%Le' expects argument of type 'long double', but argument 3 has type 'double'" } */
363 /* { dg-begin-multiline-output "" }
364 sprintf (d, " %-8Le ", fexpr);
370 { dg-end-multiline-output "" } */
372 sprintf (d
, " %-8Le ", dexpr
); /* { dg-warning "21: format '%Le' expects argument of type 'long double', but argument 3 has type 'double'" } */
373 /* { dg-begin-multiline-output "" }
374 sprintf (d, " %-8Le ", dexpr);
380 { dg-end-multiline-output "" } */
382 sprintf (d
, " %-8Le ", ldexpr
);
385 /* Tests of "E" without a length modifier, with various param types.
386 Suggestions should preserve the "E" for floating-point arguments. */
389 test_E (char *d
, int iexpr
, float fexpr
, double dexpr
, long double ldexpr
)
391 /* Integer arguments. */
393 sprintf (d
, " %-8E ", iexpr
); /* { dg-warning "20: format '%E' expects argument of type 'double', but argument 3 has type 'int'" } */
394 /* { dg-begin-multiline-output "" }
395 sprintf (d, " %-8E ", iexpr);
401 { dg-end-multiline-output "" } */
403 /* Floating-point arguments. */
405 sprintf (d
, " %-8E ", fexpr
);
406 sprintf (d
, " %-8E ", dexpr
);
407 sprintf (d
, " %-8E ", ldexpr
); /* { dg-warning "20: format '%E' expects argument of type 'double', but argument 3 has type 'long double'" } */
408 /* { dg-begin-multiline-output "" }
409 sprintf (d, " %-8E ", ldexpr);
415 { dg-end-multiline-output "" } */
418 /* Tests of "E" with "L", with various param types.
419 Suggestions should preserve the "E" for floating-point arguments. */
422 test_LE (char *d
, int iexpr
, float fexpr
, double dexpr
, long double ldexpr
)
424 /* Integer arguments. */
426 sprintf (d
, " %-8LE ", iexpr
); /* { dg-warning "21: format '%LE' expects argument of type 'long double', but argument 3 has type 'int'" } */
427 /* { dg-begin-multiline-output "" }
428 sprintf (d, " %-8LE ", iexpr);
434 { dg-end-multiline-output "" } */
436 sprintf (d
, " %-8LE ", fexpr
); /* { dg-warning "21: format '%LE' expects argument of type 'long double', but argument 3 has type 'double'" } */
437 /* { dg-begin-multiline-output "" }
438 sprintf (d, " %-8LE ", fexpr);
444 { dg-end-multiline-output "" } */
446 sprintf (d
, " %-8LE ", dexpr
); /* { dg-warning "21: format '%LE' expects argument of type 'long double', but argument 3 has type 'double'" } */
447 /* { dg-begin-multiline-output "" }
448 sprintf (d, " %-8LE ", dexpr);
454 { dg-end-multiline-output "" } */
456 sprintf (d
, " %-8LE ", ldexpr
);
459 /* Test of a suggestion for a conversion specification containing
460 all features (flags, width, precision, length modifier), where
461 all the other arguments have mismatching types. */
464 test_everything (char *d
, long lexpr
)
466 sprintf (d
, "before %-+*.*lld after", lexpr
, lexpr
, lexpr
); /* { dg-line test_everything_sprintf } */
468 /* { dg-warning "26: field width specifier '\\*' expects argument of type 'int', but argument 3 has type 'long int'" "" { target *-*-* } test_everything_sprintf } */
469 /* { dg-begin-multiline-output "" }
470 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr);
474 { dg-end-multiline-output "" } */
476 /* { dg-warning "28: field precision specifier '\\.\\*' expects argument of type 'int', but argument 4 has type 'long int'" "" { target *-*-* } test_everything_sprintf } */
477 /* { dg-begin-multiline-output "" }
478 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr);
482 { dg-end-multiline-output "" } */
484 /* { dg-warning "31: format '%lld' expects argument of type 'long long int', but argument 5 has type 'long int'" "" { target *-*-* } test_everything_sprintf } */
485 /* { dg-begin-multiline-output "" }
486 sprintf (d, "before %-+*.*lld after", lexpr, lexpr, lexpr);
489 long long int long int
491 { dg-end-multiline-output "" } */