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