1 /* PR tree-optimization/86853 - sprintf optimization for wide strings
2 doesn't account for conversion failure​
4 { dg-options "-O2 -Wall -fdump-tree-optimized" } */
6 typedef __SIZE_TYPE__
size_t;
7 typedef __WCHAR_TYPE__
wchar_t;
8 typedef __WINT_TYPE__
wint_t;
10 extern int snprintf (char*, size_t, const char*, ...);
12 #define CONCAT(x, y) x ## y
13 #define CAT(x, y) CONCAT (x, y)
14 #define FAILNAME(name) CAT (call_ ## name ##_on_line_, __LINE__)
16 #define FAIL(name) do { \
17 extern void FAILNAME (name) (void); \
21 /* Macro to emit a call to funcation named
22 call_in_true_branch_not_eliminated_on_line_NNN()
23 for each call that's expected to be eliminated. The dg-final
24 scan-tree-dump-time directive at the bottom of the test verifies
25 that no such call appears in output. */
27 if (!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0
29 /* Macro to emit a call to a function named
30 call_made_in_{true,false}_branch_on_line_NNN()
31 for each call that's expected to be retained. The dg-final
32 scan-tree-dump-time directive at the bottom of the test verifies
33 that the expected number of both kinds of calls appears in output
34 (a pair for each line with the invocation of the KEEP() macro. */
37 FAIL (made_in_true_branch); \
39 FAIL (made_in_false_branch)
45 const wchar_t ws3
[] = L
"12\xff";
47 /* Verify that the following calls are eliminated. */
49 void elim_wide_char_call (void)
51 ELIM (snprintf (0, 0, "%lc", (wint_t)L
'\0'));
52 ELIM (snprintf (0, 0, "%lc", (wint_t)L
'1'));
53 ELIM (snprintf (0, 0, "%lc", (wint_t)L
'a'));
54 ELIM (snprintf (0, 0, "%lc", (wint_t)ws3
[0]));
55 ELIM (snprintf (0, 0, "%lc", (wint_t)ws3
[1]));
56 ELIM (snprintf (0, 0, "%lc", (wint_t)ws3
[3]));
58 ELIM (snprintf (0, 0, "%C", (wint_t)L
'\0'));
59 ELIM (snprintf (0, 0, "%C", (wint_t)L
'9'));
60 ELIM (snprintf (0, 0, "%C", (wint_t)L
'z'));
61 ELIM (snprintf (0, 0, "%C", (wint_t)ws3
[0]));
62 ELIM (snprintf (0, 0, "%C", (wint_t)ws3
[1]));
63 ELIM (snprintf (0, 0, "%C", (wint_t)ws3
[3]));
65 /* Verify an unknown character value within the ASCII range. */
66 if (wc
< 1 || 127 < wc
)
69 ELIM (snprintf (0, 0, "%C", (wint_t)wc
));
70 ELIM (snprintf (0, 0, "%C", (wint_t)wc
));
73 void elim_wide_string_call (void)
75 ELIM (snprintf (0, 0, "%ls", L
""));
81 /* Verify that the following calls are not eliminated. */
83 void keep_wide_char_call (void)
85 KEEP (snprintf (0, 0, "%lc", (wint_t)L
'\xff'));
86 KEEP (snprintf (0, 0, "%lc", (wint_t)L
'\xffff'));
87 KEEP (snprintf (0, 0, "%lc", (wint_t)wc
));
88 KEEP (snprintf (0, 0, "%lc", (wint_t)ws3
[2]));
90 KEEP (snprintf (0, 0, "%C", (wint_t)L
'\xff'));
91 KEEP (snprintf (0, 0, "%C", (wint_t)L
'\xffff'));
92 KEEP (snprintf (0, 0, "%C", (wint_t)wc
));
93 KEEP (snprintf (0, 0, "%C", (wint_t)ws3
[2]));
95 /* Verify an unknown character value outside the ASCII range
96 (with 128 being the only one). */
97 if (wc
< 32 || 128 < wc
)
100 KEEP (snprintf (0, 0, "%lc", (wint_t)wc
));
101 KEEP (snprintf (0, 0, "%C", (wint_t)wc
));
104 void keep_wide_string_call (void)
106 KEEP (snprintf (0, 0, "%ls", L
"\xff"));
107 KEEP (snprintf (0, 0, "%ls", L
"\xffff"));
108 KEEP (snprintf (0, 0, "%ls", ws
));
109 KEEP (snprintf (0, 0, "%ls", ws3
));
111 KEEP (snprintf (0, 0, "%S", L
"\xff"));
112 KEEP (snprintf (0, 0, "%S", L
"\xffff"));
113 KEEP (snprintf (0, 0, "%S", ws
));
114 KEEP (snprintf (0, 0, "%S", ws3
));
117 /* { dg-final { scan-tree-dump-times "call_made_in_true_branch_not_eliminated" 0 "optimized" } }
119 { dg-final { scan-tree-dump-times "call_made_in_true_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 18 "optimized" } }
120 { dg-final { scan-tree-dump-times "call_made_in_false_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 18 "optimized" } } */