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);
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);
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);
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);
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);
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);
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);
72 { dg-end-multiline-output "" } */
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);
80 { dg-end-multiline-output "" } */
82 /* Something unrecognized. */
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);
89 { dg-end-multiline-output "" } */
92 /* Tests of "x" with "l", with various param types.
93 Suggestions should preserve the "x" for integer arguments. */
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);
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);
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);
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);
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);
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);
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);
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. */
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);
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);
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);
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);
193 { dg-end-multiline-output "" } */
196 /* Tests of "o" with "l", with various param types.
197 Suggestions should preserve the "o" for integer arguments. */
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);
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);
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);
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);
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. */
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);
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);
261 { dg-end-multiline-output "" } */
264 /* Tests of "e" with "L", with various param types.
265 Suggestions should preserve the "e" for float arguments. */
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);
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);
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);
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. */
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);
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);
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. */
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);
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);
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);
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. */
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);
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);
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);
383 { dg-end-multiline-output "" } */