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