libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / builtin-printf-warn-2.c
blob31d16bc99692ea49387e687da1428454de378d63
1 /* PR middle-end/88993 - GCC 9 -Wformat-overflow=2 should reflect real
2 libc limits
3 Verify that -Wformat-overflow=2 "may exceed" warnings are not issued
4 for printf family of functions.
5 { dg-do compile }
6 { dg-options "-O -Wformat -Wformat-overflow=2 -ftrack-macro-expansion=0" }
7 { dg-require-effective-target int32plus } */
10 #define INT_MAX __INT_MAX__
12 typedef __SIZE_TYPE__ size_t;
14 #if !__cplusplus
15 typedef __WCHAR_TYPE__ wchar_t;
16 #endif
18 #define T(...) __builtin_printf (__VA_ARGS__)
20 /* Exercise the "%c" directive with constant arguments. */
22 void test_printf_c_const (int width)
24 /* Verify that a warning is only issued when the output is definitely
25 exceeded but not when exceeding it is possible but not inevitable. */
26 T ("%2147483647c", '1');
27 T ("X%2147483647c", '2'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
28 T ("%2147483647cY", '3'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
30 T ("%2147483648c", '1'); /* { dg-warning ".%2147483648c. directive output of 2147483648 bytes exceeds .INT_MAX." } */
31 T ("X%2147483649c", '2'); /* { dg-warning ".%2147483649c. directive output of 2147483649 bytes exceeds .INT_MAX." } */
32 T ("%2147483650cY", '3'); /* { dg-warning ".%2147483650c. directive output of 2147483650 bytes exceeds .INT_MAX." } */
34 T ("%*c", INT_MAX, '1');
35 T ("X%*c", INT_MAX, '1'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
36 T ("%*cY", INT_MAX, '1'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
38 T ("X%*c", INT_MAX - 1, '1');
39 T ("%*cY", INT_MAX - 1, '1');
41 T ("%*cY", INT_MAX, '1'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
42 T ("X%*c", INT_MAX, '1'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
44 if (width > INT_MAX - 1)
45 width = INT_MAX - 1;
47 T ("%*c", width, '1');
48 T ("X%*c", width, '1');
49 T ("%*cY", width, '1');
51 T ("%*c", width, '1');
52 T ("X%*c", width, '1');
53 T ("%*cY", width, '1');
55 T ("%*c%*c", width, '1', width, '2');
56 T ("X%*cY%*cZ", width, '1', width, '2');
58 if (width < 4096)
59 width = 4096;
61 T ("%*c", width, '1');
62 T ("X%*c", width, '1');
63 T ("%*cY", width, '1');
65 if (width < INT_MAX - 1)
66 width = INT_MAX - 1;
68 T ("%*c", width, '1');
69 T ("X%*c", width, '2');
70 T ("%*cY", width, '3');
71 T ("X%*cY", width, '4'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
75 /* Exercise the "%s" directive with constant arguments. */
77 void test_printf_s_const (int width, const char *s)
79 T ("%2147483647s", s);
80 T ("X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
81 T ("%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
83 T ("%2147483648s", s); /* { dg-warning "%2147483648s. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
84 T ("X%2147483649s", s); /* { dg-warning "%2147483649s. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
85 T ("%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
87 T ("%*s", INT_MAX, s);
88 T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
89 T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
91 T ("X%*s", INT_MAX - 1, s);
92 T ("%*sY", INT_MAX - 1, s);
94 T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
95 T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
97 if (width > INT_MAX - 1)
98 width = INT_MAX - 1;
100 T ("%*s", width, s);
101 T ("X%*s", width, s);
102 T ("%*sY", width, s);
104 T ("%*s", width, s);
105 T ("X%*s", width, s);
106 T ("%*sY", width, s);
108 T ("%*s%*s", width, s, width, s);
109 T ("X%*sY%*sZ", width, s, width, s);
111 if (width < 4096)
112 width = 4096;
114 T ("%*s", width, s);
115 T ("X%*s", width, s);
116 T ("%*sY", width, s);
118 if (width < INT_MAX - 1)
119 width = INT_MAX - 1;
121 T ("%*s", width, s);
122 T ("X%*s", width, s);
123 T ("%*sY", width, s);
124 T ("X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
127 /* Exercise the "%ls" directive with constant arguments. */
129 void test_printf_ls_const (int width, const wchar_t *s)
131 T ("%2147483647ls", s);
132 T ("X%2147483647ls", s); /* { dg-warning ".%2147483647ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
133 T ("%2147483647lsY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
135 T ("%2147483648ls", s); /* { dg-warning "%2147483648ls. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
136 T ("X%2147483649ls", s); /* { dg-warning "%2147483649ls. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
137 T ("%2147483650lsY", s); /* { dg-warning ".%2147483650ls. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
139 T ("%*ls", INT_MAX, s);
140 T ("X%*ls", INT_MAX, s); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
141 T ("%*lsY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
143 T ("X%*ls", INT_MAX - 1, s);
144 T ("%*lsY", INT_MAX - 1, s);
146 T ("%*lsY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
147 T ("X%*ls", INT_MAX, s); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
149 if (width > INT_MAX - 1)
150 width = INT_MAX - 1;
152 T ("%*ls", width, s);
153 T ("X%*ls", width, s);
154 T ("%*lsY", width, s);
156 T ("%*ls", width, s);
157 T ("X%*ls", width, s);
158 T ("%*lsY", width, s);
160 T ("%*ls%*ls", width, s, width, s);
161 T ("X%*lsY%*lsZ", width, s, width, s);
163 if (width < 4096)
164 width = 4096;
166 T ("%*ls", width, s);
167 T ("X%*ls", width, s);
168 T ("%*lsY", width, s);
170 if (width < INT_MAX - 1)
171 width = INT_MAX - 1;
173 T ("%*ls", width, s);
174 T ("X%*ls", width, s);
175 T ("%*lsY", width, s);
176 T ("X%*lsY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
180 /* Also exercise printf_chk. */
182 #undef T
183 #define T(...) __builtin___printf_chk (__VA_ARGS__)
185 void test_printf_chk_s_const (int width)
187 const char *s = "0123456789";
189 T (0, "%2147483647s", s);
190 T (0, "X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
191 T (0, "%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
193 T (0, "%2147483648s", s); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
194 T (0, "X%2147483649s", s); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
195 T (0, "%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
197 T (0, "%*s", INT_MAX, s);
198 T (0, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
199 T (0, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
201 T (0, "X%*s", INT_MAX - 1, s);
202 T (0, "%*sY", INT_MAX - 1, s);
204 T (0, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
205 T (0, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
207 if (width > INT_MAX - 1)
208 width = INT_MAX - 1;
210 T (0, "%*s", width, s);
211 T (0, "X%*s", width, s);
212 T (0, "%*sY", width, s);
214 T (0, "%*s", width, s);
215 T (0, "X%*s", width, s);
216 T (0, "%*sY", width, s);
218 T (0, "%*s%*s", width, s, width, s);
219 T (0, "X%*sY%*sZ", width, s, width, s);
221 if (width < 4096)
222 width = 4096;
224 T (0, "%*s", width, s);
225 T (0, "X%*s", width, s);
226 T (0, "%*sY", width, s);
228 if (width < INT_MAX - 1)
229 width = INT_MAX - 1;
231 T (0, "%*s", width, s);
232 T (0, "X%*s", width, s);
233 T (0, "%*sY", width, s);
234 T (0, "X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
238 /* And finally exercise printf_unlocked. */
240 #undef T
241 #define T(...) __builtin_printf_unlocked (__VA_ARGS__)
243 void test_printf_unlocked_s_const (int width)
245 const char *s = "0123456789";
247 T ("%2147483647s", s);
248 T ("X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
249 T ("%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
251 T ("%2147483648s", s); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
252 T ("X%2147483649s", s); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
253 T ("%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
255 T ("%*s", INT_MAX, s);
256 T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
257 T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
259 T ("X%*s", INT_MAX - 1, s);
260 T ("%*sY", INT_MAX - 1, s);
262 T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
263 T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
265 if (width > INT_MAX - 1)
266 width = INT_MAX - 1;
268 T ("%*s", width, s);
269 T ("X%*s", width, s);
270 T ("%*sY", width, s);
272 T ("%*s", width, s);
273 T ("X%*s", width, s);
274 T ("%*sY", width, s);
276 T ("%*s%*s", width, s, width, s);
277 T ("X%*sY%*sZ", width, s, width, s);
279 if (width < 4096)
280 width = 4096;
282 T ("%*s", width, s);
283 T ("X%*s", width, s);
284 T ("%*sY", width, s);
286 if (width < INT_MAX - 1)
287 width = INT_MAX - 1;
289 T ("%*s", width, s);
290 T ("X%*s", width, s);
291 T ("%*sY", width, s);
292 T ("X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */