| 1 |
/* PR tree-optimization/86853 - sprintf optimization for wide strings |
| 2 |
doesn't account for conversion failure​ |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wall -fdump-tree-optimized" } */ |
| 5 |
|
| 6 |
typedef __SIZE_TYPE__ size_t; |
| 7 |
typedef __WCHAR_TYPE__ wchar_t; |
| 8 |
typedef __WINT_TYPE__ wint_t; |
| 9 |
|
| 10 |
extern int snprintf (char*, size_t, const char*, ...); |
| 11 |
|
| 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__) |
| 15 |
|
| 16 |
#define FAIL(name) do { \ |
| 17 |
extern void FAILNAME (name) (void); \ |
| 18 |
FAILNAME (name)(); \ |
| 19 |
} while (0) |
| 20 |
|
| 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-times directive at the bottom of the test verifies |
| 25 |
that no such call appears in output. */ |
| 26 |
#define ELIM(expr) \ |
| 27 |
if (!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0 |
| 28 |
|
| 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-times 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. */ |
| 35 |
#define KEEP(expr) \ |
| 36 |
if (expr) \ |
| 37 |
FAIL (made_in_true_branch); \ |
| 38 |
else \ |
| 39 |
FAIL (made_in_false_branch) |
| 40 |
|
| 41 |
|
| 42 |
extern wchar_t wc; |
| 43 |
extern wchar_t ws[]; |
| 44 |
|
| 45 |
const wchar_t ws3[] = L"12\xff"; |
| 46 |
|
| 47 |
/* Verify that the following calls are eliminated. */ |
| 48 |
|
| 49 |
void elim_wide_char_call (void) |
| 50 |
{ |
| 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])); |
| 57 |
|
| 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])); |
| 64 |
|
| 65 |
/* Verify an unknown character value within the ASCII range. */ |
| 66 |
if (wc < 1 || 127 < wc) |
| 67 |
wc = 0; |
| 68 |
|
| 69 |
ELIM (snprintf (0, 0, "%C", (wint_t)wc)); |
| 70 |
ELIM (snprintf (0, 0, "%C", (wint_t)wc)); |
| 71 |
} |
| 72 |
|
| 73 |
void elim_wide_string_call (void) |
| 74 |
{ |
| 75 |
ELIM (snprintf (0, 0, "%ls", L"")); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
#line 1000 |
| 80 |
|
| 81 |
/* Verify that the following calls are not eliminated. */ |
| 82 |
|
| 83 |
void keep_wide_char_call (void) |
| 84 |
{ |
| 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])); |
| 89 |
|
| 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])); |
| 94 |
|
| 95 |
/* Verify an unknown character value outside the ASCII range |
| 96 |
(with 128 being the only one). */ |
| 97 |
if (wc < 32 || 128 < wc) |
| 98 |
wc = 32; |
| 99 |
|
| 100 |
KEEP (snprintf (0, 0, "%lc", (wint_t)wc)); |
| 101 |
KEEP (snprintf (0, 0, "%C", (wint_t)wc)); |
| 102 |
} |
| 103 |
|
| 104 |
void keep_wide_string_call (void) |
| 105 |
{ |
| 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)); |
| 110 |
|
| 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)); |
| 115 |
} |
| 116 |
|
| 117 |
/* { dg-final { scan-tree-dump-times "call_made_in_true_branch_not_eliminated" 0 "optimized" } } |
| 118 |
|
| 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" } } */ |