| 1 |
/* PR tree-optimization/86274 - SEGFAULT when logging std::to_string(NAN) |
| 2 |
{ dg-do compile } |
| 3 |
{ dg-require-effective-target double64 } |
| 4 |
{ dg-skip-if "not IEEE float layout" { "pdp11-*-*" } } |
| 5 |
{ dg-options "-O2 -Wall -fdump-tree-optimized" } */ |
| 6 |
|
| 7 |
typedef __SIZE_TYPE__ size_t; |
| 8 |
extern int sprintf (char*, const char*, ...); |
| 9 |
extern int snprintf (char*, size_t, const char*, ...); |
| 10 |
|
| 11 |
#define CAT(x, y) x ## y |
| 12 |
#define CONCAT(x, y) CAT (x, y) |
| 13 |
#define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__) |
| 14 |
|
| 15 |
#define FAIL(name) do { \ |
| 16 |
extern void FAILNAME (name) (void); \ |
| 17 |
FAILNAME (name)(); \ |
| 18 |
} while (0) |
| 19 |
|
| 20 |
/* Macro to emit a call to funcation named |
| 21 |
call_in_true_branch_not_eliminated_on_line_NNN() |
| 22 |
for each expression that's expected to fold to false but that |
| 23 |
GCC does not fold. The dg-final scan-tree-dump-times directive |
| 24 |
at the bottom of the test verifies that no such call appears |
| 25 |
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 |
extern void sink (int, ...); |
| 42 |
#define sink(...) sink (0, __VA_ARGS__) |
| 43 |
|
| 44 |
#define WARN(N, expr) \ |
| 45 |
do { \ |
| 46 |
char a[N]; \ |
| 47 |
expr; \ |
| 48 |
sink (a); \ |
| 49 |
} while (0) |
| 50 |
|
| 51 |
|
| 52 |
void test_elim (double x) |
| 53 |
{ |
| 54 |
ELIM (snprintf (0, 0, "%a", x) < 3); |
| 55 |
ELIM (snprintf (0, 0, "%e", x) < 3); |
| 56 |
ELIM (snprintf (0, 0, "%f", x) < 3); |
| 57 |
ELIM (snprintf (0, 0, "%g", x) < 1); |
| 58 |
|
| 59 |
/* Verify that snprintf knows that NaN cannot result in fewer |
| 60 |
than three characters on output. */ |
| 61 |
const double nan = __builtin_nan ("0"); |
| 62 |
ELIM (snprintf (0, 0, "%a", nan) < 3); |
| 63 |
ELIM (snprintf (0, 0, "%e", nan) < 3); |
| 64 |
ELIM (snprintf (0, 0, "%f", nan) < 3); |
| 65 |
ELIM (snprintf (0, 0, "%g", nan) < 3); |
| 66 |
} |
| 67 |
|
| 68 |
void test_keep (int p, double x) |
| 69 |
{ |
| 70 |
KEEP (snprintf (0, 0, "%a", x) == 3); |
| 71 |
KEEP (snprintf (0, 0, "%e", x) == 3); |
| 72 |
|
| 73 |
KEEP (snprintf (0, 0, "%f", x) == 3); |
| 74 |
KEEP (snprintf (0, 0, "%.*f", p, x) < 3); |
| 75 |
|
| 76 |
KEEP (snprintf (0, 0, "%g", x) == 1); |
| 77 |
KEEP (snprintf (0, 0, "%g", x) == 3); |
| 78 |
} |
| 79 |
|
| 80 |
void test_warn_sprintf_f (double x) |
| 81 |
{ |
| 82 |
WARN (4, sprintf (a, "%a", x)); /* { dg-warning "between 3 and 24 bytes" } */ |
| 83 |
WARN (4, sprintf (a, "%e", x)); /* { dg-warning "between 3 and 14 bytes" } */ |
| 84 |
WARN (4, sprintf (a, "%f", x)); /* { dg-warning "between 3 and 317 bytes" } */ |
| 85 |
WARN (4, sprintf (a, "%g", x)); /* { dg-warning "between 1 and 13 bytes" } */ |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/* { dg-final { scan-tree-dump-times "call_in_true_branch_not_eliminated_" 0 "optimized" } } |
| 90 |
{ dg-final { scan-tree-dump-times "call_made_in_true_branch_" 6 "optimized" } } |
| 91 |
{ dg-final { scan-tree-dump-times "call_made_in_false_branch_" 6 "optimized" } } */ |