1 /* Verify that tests for the result of calls to fprintf, printf, vfprintf,
2 and vprintf are not eliminated, even if it is possible to determine
3 their value on success (the calls may fail and return a negative value).
5 { dg-options "-O2 -fdump-tree-optimized" } */
7 typedef struct FILE FILE;
8 typedef __builtin_va_list
va_list;
10 extern int printf (const char *, ...);
11 extern int printf_unlocked (const char *, ...);
12 extern int vprintf (const char *, va_list);
14 extern int fprintf (FILE*, const char *, ...);
15 extern int fprintf_unlocked (FILE*, const char *, ...);
16 extern int vfprintf (FILE*, const char *, va_list);
18 #define fprintf_chk __builtin___fprintf_chk
19 #define printf_chk __builtin___printf_chk
20 #define vfprintf_chk __builtin___vfprintf_chk
21 #define vprintf_chk __builtin___vprintf_chk
23 #define CAT(s, n) s ## n
25 #define KEEP(func, line) CAT (func ## _test_on_line_, line)
27 /* Emit one call to a function named call_on_line_NNN when the result
28 of the call FUNC ARGS is less than zero, zero, or greater than zero.
29 This verifies that the expression is not eliminated.
31 For known output it is possible to bound the return value to
32 [INT_MIN, -1] U [0, N] with N being the size of the output, but
33 that optimization isn't implemented (yet). */
35 #define T(func, args) \
37 extern void KEEP (func, __LINE__)(const char*); \
38 if ((func args) < 0) KEEP (func, __LINE__)("< 0"); \
39 if ((func args) >= 0) KEEP (func, __LINE__)(">= 0"); \
42 void test_fprintf (FILE *f
, const char *s
)
44 /* Here the result is in [INT_MIN, 0], i.e., it cannot be positive.
45 It might be a useful enhancement to implement this optimization. */
47 T (fprintf
, (f
, "1"));
48 T (fprintf
, (f
, "123"));
51 T (fprintf
, (f
, "%c", 0));
52 T (fprintf
, (f
, "%c", '1'));
53 T (fprintf
, (f
, "%c", *s
));
55 T (fprintf
, (f
, "%s", ""));
56 T (fprintf
, (f
, "%s", "1"));
57 T (fprintf
, (f
, "%.0s", ""));
58 T (fprintf
, (f
, "%.0s", s
));
60 /* { dg-final { scan-tree-dump-times " fprintf_test_on_line_" 22 "optimized"} } */
64 void test_fprintf_unlocked (FILE *f
, const char *s
)
66 T (fprintf_unlocked
, (f
, ""));
67 T (fprintf_unlocked
, (f
, "1"));
68 T (fprintf_unlocked
, (f
, "123"));
69 T (fprintf_unlocked
, (f
, s
));
71 T (fprintf_unlocked
, (f
, "%c", 0));
72 T (fprintf_unlocked
, (f
, "%c", '1'));
73 T (fprintf_unlocked
, (f
, "%c", *s
));
75 T (fprintf_unlocked
, (f
, "%s", ""));
76 T (fprintf_unlocked
, (f
, "%s", "1"));
77 T (fprintf_unlocked
, (f
, "%.0s", ""));
78 T (fprintf_unlocked
, (f
, "%.0s", s
));
80 /* { dg-final { scan-tree-dump-times " fprintf_unlocked_test_on_line_" 22 "optimized"} } */
84 void test_fprintf_chk (FILE *f
, const char *s
)
86 T (fprintf_chk
, (f
, 0, ""));
87 T (fprintf_chk
, (f
, 0, "1"));
88 T (fprintf_chk
, (f
, 0, "123"));
89 T (fprintf_chk
, (f
, 0, s
));
91 T (fprintf_chk
, (f
, 0, "%c", 0));
92 T (fprintf_chk
, (f
, 0, "%c", '1'));
93 T (fprintf_chk
, (f
, 0, "%c", *s
));
95 T (fprintf_chk
, (f
, 0, "%s", ""));
96 T (fprintf_chk
, (f
, 0, "%s", "1"));
97 T (fprintf_chk
, (f
, 0, "%.0s", ""));
98 T (fprintf_chk
, (f
, 0, "%.0s", s
));
100 /* { dg-final { scan-tree-dump-times " __builtin___fprintf_chk_test_on_line_" 22 "optimized"} } */
104 void test_vfprintf (FILE *f
, va_list va
)
106 T (vfprintf
, (f
, "", va
));
107 T (vfprintf
, (f
, "123", va
));
109 T (vfprintf
, (f
, "%c", va
));
111 T (vfprintf
, (f
, "%.0s", va
));
113 /* { dg-final { scan-tree-dump-times " vfprintf_test_on_line_" 8 "optimized"} } */
117 void test_vfprintf_chk (FILE *f
, va_list va
)
119 T (vfprintf_chk
, (f
, 0, "", va
));
120 T (vfprintf_chk
, (f
, 0, "123", va
));
122 T (vfprintf_chk
, (f
, 0, "%c", va
));
124 T (vfprintf_chk
, (f
, 0, "%.0s", va
));
126 /* { dg-final { scan-tree-dump-times " __builtin___vfprintf_chk_test_on_line_" 8 "optimized"} } */
130 void test_printf (const char *s
)
137 T (printf
, ("%c", 0));
138 T (printf
, ("%c", '1'));
139 T (printf
, ("%c", *s
));
141 T (printf
, ("%s", ""));
142 T (printf
, ("%s", "1"));
143 T (printf
, ("%.0s", ""));
144 T (printf
, ("%.0s", s
));
146 /* { dg-final { scan-tree-dump-times " printf_test_on_line_" 22 "optimized"} } */
150 void test_printf_unlocked (const char *s
)
152 T (printf_unlocked
, (""));
153 T (printf_unlocked
, ("1"));
154 T (printf_unlocked
, ("123"));
155 T (printf_unlocked
, (s
));
157 T (printf_unlocked
, ("%c", 0));
158 T (printf_unlocked
, ("%c", '1'));
159 T (printf_unlocked
, ("%c", *s
));
161 T (printf_unlocked
, ("%s", ""));
162 T (printf_unlocked
, ("%s", "1"));
163 T (printf_unlocked
, ("%.0s", ""));
164 T (printf_unlocked
, ("%.0s", s
));
166 /* { dg-final { scan-tree-dump-times " printf_unlocked_test_on_line_" 22 "optimized"} } */
170 void test_printf_chk (const char *s
)
172 T (printf_chk
, (0, ""));
173 T (printf_chk
, (0, "1"));
174 T (printf_chk
, (0, "123"));
175 T (printf_chk
, (0, s
));
177 T (printf_chk
, (0, "%c", 0));
178 T (printf_chk
, (0, "%c", '1'));
179 T (printf_chk
, (0, "%c", *s
));
181 T (printf_chk
, (0, "%s", ""));
182 T (printf_chk
, (0, "%s", "1"));
183 T (printf_chk
, (0, "%.0s", ""));
184 T (printf_chk
, (0, "%.0s", s
));
186 /* { dg-final { scan-tree-dump-times " __builtin___printf_chk_test_on_line_" 22 "optimized"} } */
190 void test_vprintf (va_list va
)
192 T (vprintf
, ("", va
));
193 T (vprintf
, ("123", va
));
195 T (vprintf
, ("%c", va
));
197 T (vprintf
, ("%.0s", va
));
199 /* { dg-final { scan-tree-dump-times " vprintf_test_on_line_" 8 "optimized"} } */
203 void test_vprintf_chk (va_list va
)
205 T (vprintf_chk
, (0, "", va
));
206 T (vprintf_chk
, (0, "123", va
));
208 T (vprintf_chk
, (0, "%c", va
));
210 T (vprintf_chk
, (0, "%.0s", va
));
212 /* { dg-final { scan-tree-dump-times " __builtin___vprintf_chk_test_on_line_" 8 "optimized"} } */