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).
4    { dg-do compile }
5    { dg-options "-O2 -fdump-tree-optimized" } */
6 
7 typedef struct FILE FILE;
8 typedef __builtin_va_list va_list;
9 
10 extern int printf (const char *, ...);
11 extern int printf_unlocked (const char *, ...);
12 extern int vprintf (const char *, va_list);
13 
14 extern int fprintf (FILE*, const char *, ...);
15 extern int fprintf_unlocked (FILE*, const char *, ...);
16 extern int vfprintf (FILE*, const char *, va_list);
17 
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
22 
23 #define CAT(s, n)   s ## n
24 
25 #define KEEP(func, line)   CAT (func ## _test_on_line_, line)
26 
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.
30 
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).  */
34 
35 #define T(func, args)						\
36   do {								\
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");	\
40   } while (0)
41 
test_fprintf(FILE * f,const char * s)42 void test_fprintf (FILE *f, const char *s)
43 {
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.  */
46   T (fprintf, (f, ""));
47   T (fprintf, (f, "1"));
48   T (fprintf, (f, "123"));
49   T (fprintf, (f, s));
50 
51   T (fprintf, (f, "%c", 0));
52   T (fprintf, (f, "%c", '1'));
53   T (fprintf, (f, "%c", *s));
54 
55   T (fprintf, (f, "%s", ""));
56   T (fprintf, (f, "%s", "1"));
57   T (fprintf, (f, "%.0s", ""));
58   T (fprintf, (f, "%.0s", s));
59 
60   /* { dg-final { scan-tree-dump-times " fprintf_test_on_line_" 22 "optimized"} } */
61 }
62 
63 
test_fprintf_unlocked(FILE * f,const char * s)64 void test_fprintf_unlocked (FILE *f, const char *s)
65 {
66   T (fprintf_unlocked, (f, ""));
67   T (fprintf_unlocked, (f, "1"));
68   T (fprintf_unlocked, (f, "123"));
69   T (fprintf_unlocked, (f, s));
70 
71   T (fprintf_unlocked, (f, "%c", 0));
72   T (fprintf_unlocked, (f, "%c", '1'));
73   T (fprintf_unlocked, (f, "%c", *s));
74 
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));
79 
80   /* { dg-final { scan-tree-dump-times " fprintf_unlocked_test_on_line_" 22 "optimized"} } */
81 }
82 
83 
test_fprintf_chk(FILE * f,const char * s)84 void test_fprintf_chk (FILE *f, const char *s)
85 {
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));
90 
91   T (fprintf_chk, (f, 0, "%c", 0));
92   T (fprintf_chk, (f, 0, "%c", '1'));
93   T (fprintf_chk, (f, 0, "%c", *s));
94 
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));
99 
100   /* { dg-final { scan-tree-dump-times " __builtin___fprintf_chk_test_on_line_" 22 "optimized"} } */
101 }
102 
103 
test_vfprintf(FILE * f,va_list va)104 void test_vfprintf (FILE *f, va_list va)
105 {
106   T (vfprintf, (f, "", va));
107   T (vfprintf, (f, "123", va));
108 
109   T (vfprintf, (f, "%c", va));
110 
111   T (vfprintf, (f, "%.0s", va));
112 
113   /* { dg-final { scan-tree-dump-times " vfprintf_test_on_line_" 8 "optimized"} } */
114 }
115 
116 
test_vfprintf_chk(FILE * f,va_list va)117 void test_vfprintf_chk (FILE *f, va_list va)
118 {
119   T (vfprintf_chk, (f, 0, "", va));
120   T (vfprintf_chk, (f, 0, "123", va));
121 
122   T (vfprintf_chk, (f, 0, "%c", va));
123 
124   T (vfprintf_chk, (f, 0, "%.0s", va));
125 
126   /* { dg-final { scan-tree-dump-times " __builtin___vfprintf_chk_test_on_line_" 8 "optimized"} } */
127 }
128 
129 
test_printf(const char * s)130 void test_printf (const char *s)
131 {
132   T (printf, (""));
133   T (printf, ("1"));
134   T (printf, ("123"));
135   T (printf, (s));
136 
137   T (printf, ("%c", 0));
138   T (printf, ("%c", '1'));
139   T (printf, ("%c", *s));
140 
141   T (printf, ("%s", ""));
142   T (printf, ("%s", "1"));
143   T (printf, ("%.0s", ""));
144   T (printf, ("%.0s", s));
145 
146 /* { dg-final { scan-tree-dump-times " printf_test_on_line_" 22 "optimized"} } */
147 }
148 
149 
test_printf_unlocked(const char * s)150 void test_printf_unlocked (const char *s)
151 {
152   T (printf_unlocked, (""));
153   T (printf_unlocked, ("1"));
154   T (printf_unlocked, ("123"));
155   T (printf_unlocked, (s));
156 
157   T (printf_unlocked, ("%c", 0));
158   T (printf_unlocked, ("%c", '1'));
159   T (printf_unlocked, ("%c", *s));
160 
161   T (printf_unlocked, ("%s", ""));
162   T (printf_unlocked, ("%s", "1"));
163   T (printf_unlocked, ("%.0s", ""));
164   T (printf_unlocked, ("%.0s", s));
165 
166 /* { dg-final { scan-tree-dump-times " printf_unlocked_test_on_line_" 22 "optimized"} } */
167 }
168 
169 
test_printf_chk(const char * s)170 void test_printf_chk (const char *s)
171 {
172   T (printf_chk, (0, ""));
173   T (printf_chk, (0, "1"));
174   T (printf_chk, (0, "123"));
175   T (printf_chk, (0, s));
176 
177   T (printf_chk, (0, "%c", 0));
178   T (printf_chk, (0, "%c", '1'));
179   T (printf_chk, (0, "%c", *s));
180 
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));
185 
186 /* { dg-final { scan-tree-dump-times " __builtin___printf_chk_test_on_line_" 22 "optimized"} } */
187 }
188 
189 
test_vprintf(va_list va)190 void test_vprintf (va_list va)
191 {
192   T (vprintf, ("", va));
193   T (vprintf, ("123", va));
194 
195   T (vprintf, ("%c", va));
196 
197   T (vprintf, ("%.0s", va));
198 
199   /* { dg-final { scan-tree-dump-times " vprintf_test_on_line_" 8 "optimized"} } */
200 }
201 
202 
test_vprintf_chk(va_list va)203 void test_vprintf_chk (va_list va)
204 {
205   T (vprintf_chk, (0, "", va));
206   T (vprintf_chk, (0, "123", va));
207 
208   T (vprintf_chk, (0, "%c", va));
209 
210   T (vprintf_chk, (0, "%.0s", va));
211 
212   /* { dg-final { scan-tree-dump-times " __builtin___vprintf_chk_test_on_line_" 8 "optimized"} } */
213 }
214