PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / pr79327-2.c
blob4c9d60625c02cb50e2937415e36dc4df16021bff
1 /* PR tree-optimization/79327 - wrong code at -O2 and -fprintf-return-value
2 { dg-do compile }
3 { dg-options "-O2 -fdump-tree-optimized" } */
5 #define CAT(s, n) s ## n
6 #define FAIL(line) CAT (failure_on_line_, line)
8 /* Emit a call to a function named failure_on_line_NNN when EXPR is false. */
9 #define ASSERT(expr) \
10 do { \
11 extern void FAIL (__LINE__)(void); \
12 if (!(expr)) FAIL (__LINE__)(); \
13 } while (0)
15 #define KEEP(line) CAT (keep_call_on_line_, line)
17 /* Emit a call to a function named keep_call_on_line_NNN when EXPR is true.
18 Used to verify that the expression need not be the only one that holds. */
19 #define ASSERT_MAYBE(expr) \
20 do { \
21 extern void KEEP (__LINE__)(void); \
22 if (expr) KEEP (__LINE__)(); \
23 } while (0)
25 void test_hho_cst (void)
27 ASSERT (1 == __builtin_snprintf (0, 0, "%#.1hho", 0));
28 ASSERT (2 == __builtin_snprintf (0, 0, "%#.2hho", 0));
30 ASSERT (2 == __builtin_snprintf (0, 0, "%#.1hho", 1));
31 ASSERT (2 == __builtin_snprintf (0, 0, "%#.2hho", 1));
32 ASSERT (3 == __builtin_snprintf (0, 0, "%#.3hho", 1));
35 int test_hho_var (int i)
37 int n = __builtin_snprintf (0, 0, "%#hho", i);
39 ASSERT (0 < n && n < 5);
41 ASSERT_MAYBE (1 == n);
42 ASSERT_MAYBE (2 == n);
43 ASSERT_MAYBE (3 == n);
44 ASSERT_MAYBE (4 == n);
46 return n;
49 int test_ho_var (int i)
51 int n = __builtin_snprintf (0, 0, "%#ho", i);
53 ASSERT (0 < n && n < 8);
55 ASSERT_MAYBE (1 == n);
56 ASSERT_MAYBE (2 == n);
57 ASSERT_MAYBE (3 == n);
58 ASSERT_MAYBE (4 == n);
59 ASSERT_MAYBE (5 == n);
60 ASSERT_MAYBE (6 == n);
61 ASSERT_MAYBE (7 == n);
63 return n;
66 int test_o_var (int i)
68 int n = __builtin_snprintf (0, 0, "%#o", i);
70 ASSERT (0 < n && n < 13);
72 ASSERT_MAYBE (1 == n);
73 ASSERT_MAYBE (2 == n);
74 ASSERT_MAYBE (3 == n);
75 ASSERT_MAYBE (4 == n);
76 ASSERT_MAYBE (5 == n);
77 ASSERT_MAYBE (6 == n);
78 ASSERT_MAYBE (7 == n);
79 ASSERT_MAYBE (8 == n);
80 ASSERT_MAYBE (9 == n);
81 ASSERT_MAYBE (10 == n);
82 ASSERT_MAYBE (11 == n);
83 ASSERT_MAYBE (12 == n);
85 return n;
88 void test_hhx_cst (void)
90 ASSERT (1 == __builtin_snprintf (0, 0, "%#.1hhx", 0));
91 ASSERT (2 == __builtin_snprintf (0, 0, "%#.2hhx", 0));
93 ASSERT (3 == __builtin_snprintf (0, 0, "%#.1hhx", 1));
94 ASSERT (4 == __builtin_snprintf (0, 0, "%#.2hhx", 1));
95 ASSERT (5 == __builtin_snprintf (0, 0, "%#.3hhx", 1));
98 int test_hhx_var (int i)
100 int n = __builtin_snprintf (0, 0, "%#hhx", i);
102 ASSERT (0 < n && n < 5);
104 ASSERT_MAYBE (1 == n);
105 ASSERT_MAYBE (2 == n);
106 ASSERT_MAYBE (3 == n);
107 ASSERT_MAYBE (4 == n);
109 return n;
112 void test_hx_cst (void)
114 ASSERT (1 == __builtin_snprintf (0, 0, "%#.1hx", 0));
115 ASSERT (2 == __builtin_snprintf (0, 0, "%#.2hx", 0));
117 ASSERT (3 == __builtin_snprintf (0, 0, "%#.1hx", 1));
118 ASSERT (4 == __builtin_snprintf (0, 0, "%#.2hx", 1));
119 ASSERT (5 == __builtin_snprintf (0, 0, "%#.3hx", 1));
122 int test_hx_var (int i)
124 int n = __builtin_snprintf (0, 0, "%#hx", i);
126 ASSERT (0 < n && n < 7);
128 ASSERT_MAYBE (1 == n);
129 ASSERT_MAYBE (2 == n);
130 ASSERT_MAYBE (3 == n);
131 ASSERT_MAYBE (4 == n);
132 ASSERT_MAYBE (5 == n);
133 ASSERT_MAYBE (6 == n);
135 return n;
138 int test_x_var (int i)
140 int n = __builtin_snprintf (0, 0, "%#x", i);
142 ASSERT (0 < n && n < 11);
144 ASSERT_MAYBE (1 == n);
145 ASSERT_MAYBE (2 == n);
146 ASSERT_MAYBE (3 == n);
147 ASSERT_MAYBE (4 == n);
148 ASSERT_MAYBE (5 == n);
149 ASSERT_MAYBE (6 == n);
150 ASSERT_MAYBE (7 == n);
151 ASSERT_MAYBE (8 == n);
152 ASSERT_MAYBE (9 == n);
153 ASSERT_MAYBE (10 == n);
155 return n;
158 /* { dg-final { scan-tree-dump-not "failure_on_line" "optimized"} }
159 { dg-final { scan-tree-dump-times "keep_call_on_line" 43 "optimized"} } */