]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gcc.dg/c2x-constexpr-1.c
c: Update checks on constexpr floating-point initializers
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / c2x-constexpr-1.c
CommitLineData
b556d177
JM
1/* Test C2x constexpr. Valid code, compilation tests. */
2/* { dg-do compile } */
3/* { dg-options "-std=c2x -pedantic-errors" } */
4
5#include <float.h>
6
7constexpr int v1 = 1;
8static_assert (v1 == 1);
9extern typeof (v1) *pci;
10extern const int *pci;
11extern typeof (&(constexpr int) {}) pci;
12/* Redeclaring a constexpr object is OK (although it can't be declared before
13 the definition without undefined behavior). */
14extern const int v1;
15static_assert (v1 == 1);
16unsigned int constexpr v2 = 2;
17static_assert (v2 == 2);
18extern typeof (v2) *pcui;
19extern const unsigned int *pcui;
20static constexpr char v3 = 3;
21static_assert (v3 == 3);
22extern typeof (v3) *pcc;
23extern const char *pcc;
24constexpr void *v4 = 0;
25extern typeof (v4) *pcpv;
26extern void *const *pcpv;
27constexpr int *v5 = nullptr;
28extern typeof (v5) *pcpi;
29extern int *const *pcpi;
30constexpr double v6 = 3.5;
31extern typeof (v6) *pcd;
32extern const double *pcd;
33auto constexpr v7 = 1.0;
34extern typeof (v7) *pcd;
35constexpr auto v8 = 1.5f;
36extern typeof (v8) *pcf;
37extern const float *pcf;
38constexpr static long v9 = 2ULL;
39static_assert (v9 == 2);
40extern typeof (v9) *pcl;
41extern const long *pcl;
42const short *v10 = &(constexpr short) { 10 };
43/* Qualifiers that aren't permitted on a constexpr object itself are OK in a
44 pointer target. */
45constexpr volatile int *v11 = nullptr;
46extern typeof (v11) *pcpvi;
47extern volatile int *const *pcpvi;
48constexpr _Atomic int *v12 = nullptr;
49extern typeof (v12) *pcpai;
50extern _Atomic int *const *pcpai;
51constexpr int *restrict *v13 = nullptr;
52extern typeof (v13) cprpi;
53extern int *restrict *const cprpi;
54typedef int *P;
55constexpr restrict P *v14 = nullptr;
56extern typeof (v14) cprpi;
57struct s15 { volatile int a; _Atomic int b; int *restrict p; };
58constexpr struct s15 *v16 = nullptr;
59constexpr char v17[3] = { 1, 2, 3 };
60struct s18 { int a; int *b; double c; };
61constexpr struct s18 v19 = { 12345ULL, 0, 19.0L };
62static_assert (v19.a == 12345);
63union u20 { int a; float b; };
64constexpr union u20 v21 = { 1 };
65static_assert (v21.a == 1);
66constexpr union u20 v22 = { .b = 23.0 };
67constexpr float v23 = (float) (1.0f / 3.0f);
68constexpr double v24 = (double) (1.0 / 3.0);
69constexpr struct s18 v25 = { 0, 0, (double) (1.0 / 3.0) };
70static_assert (v25.a == 0);
71constexpr char v26[] = "abc\xfe";
72constexpr unsigned char v27[] = u8"xyz\xff";
73constexpr unsigned char v28[] = "\x12\x7f";
74constexpr signed char v29[] = "\x34\x66";
75constexpr double v30 = (int) (double) 3.0 - (long) (double) 2.0;
76constexpr int v31 = 1 + 2 + (int) 3.0;
77static_assert (v31 == 6);
78constexpr typeof (nullptr) v32 = nullptr;
79constexpr _Complex double v33 = __builtin_complex (1.0f, 3.0f / 2.0f);
80constexpr float v34 = 1234.0L;
81constexpr char v35 = 127ULL;
82#if FLT_MIN_EXP == -125 && FLT_MANT_DIG == 24
83constexpr float v36 = 0x1p-149;
84constexpr float _Complex v37 = __builtin_complex (0x1p-149, 0x1p127);
85constexpr float v38 = 0xffffffUL;
86constexpr float v39 = -0xffffffL;
87constexpr float v40 = 0xffffff0L;
88constexpr float v41 = 1ULL << 63;
89#endif
90#if DBL_MIN_EXP == -1021 && DBL_MANT_DIG == 53
91constexpr double v42 = 0x1p-1074L;
92constexpr _Complex double v43 = __builtin_complex (0x1p1023L, 0x1p-1074L);
93constexpr double v44 = 0x1fffffffffffffULL;
94constexpr double v45 = -0x1fffffffffffffLL;
95constexpr double v46 = 0x3ffffffffffffeULL;
96constexpr double v47 = 1ULL << 63;
97#endif
98constexpr void *v48 = (void *) 0;
99constexpr int *v49 = (void *) 0L;
100constexpr long *v50 = 0LL;
101constexpr int v51 = {};
102static_assert (v51 == 0);
103constexpr float v52 = {};
104constexpr long double v53 = {};
105constexpr int *v54 = {};
106constexpr void *v55 = {};
107constexpr typeof (nullptr) v56 = {};
108struct s57 { int *p; };
109union u58 { int *p; };
110constexpr int *v59 = 0;
111constexpr int *v60 = { 0 };
112constexpr struct s57 v61 = { 0 };
113constexpr struct s57 v62 = { { } }; /* { dg-warning "braces around scalar initializer" } */
114constexpr struct s57 v63 = { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
115constexpr union u58 v64 = { 0 };
116constexpr union u58 v65 = { { } }; /* { dg-warning "braces around scalar initializer" } */
117constexpr union u58 v66 = { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
118struct s67 { int a; float b; void *c; int *d; typeof (nullptr) e; int f; int g[2]; };
119struct s68 { struct s67 x; };
120union u69 { int a; float b; void *c; int *d; struct s68 e; };
121struct s70 { union u69 x; };
122constexpr struct s67 v71 = { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } };
123static_assert (v71.a == 1);
124static_assert (v71.f == 7);
125constexpr struct s67 v72 = v71;
126static_assert (v72.a == 1);
127static_assert (v72.f == 7);
128extern const struct s67 v71;
129constexpr auto v73 = v71;
130static_assert (v73.a == 1);
131static_assert (v73.f == 7);
132auto v74 = v71;
133constexpr struct s68 v75 = { v72 };
134static_assert (v75.x.a == 1);
135static_assert (v75.x.f == 7);
136constexpr union u69 v76 = { };
137static_assert (v76.a == 0);
138constexpr union u69 v77 = { .e = v75 };
139static_assert (v77.e.x.a == 1);
140static_assert (v77.e.x.f == 7);
141constexpr union u69 v78 = { .a = 1 };
142static_assert (v78.a == 1);
143constexpr union u69 v79 = { .e = { v72 } };
144static_assert (v79.e.x.a == 1);
145static_assert (v79.e.x.f == 7);
146enum e80 { E80 = v79.e.x.f };
147static_assert (E80 == 7);
148constexpr struct s70 v81 = { v79 };
149static_assert (v81.x.e.x.f == 7);
150constexpr struct s68 v82 = { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } };
151static_assert (v82.x.a == 5);
152static_assert (v82.x.f == 9);
153constexpr auto v83 = (constexpr int) { (constexpr int) { 0 } };
154/* These are null pointers but not null pointer constants. */
155constexpr typeof (nullptr) v84 = nullptr;
156constexpr void *v85 = 0;
157int *v86 = v85;
158int *v87 = v84;
159typeof (1 ? v85 : (int *) 0) v88;
160extern void *v88;
161typeof (1 ? (void *) 0 : (int *) 0) v89;
162extern int *v89;
163constexpr struct s68 v90 = { };
164static_assert (v90.x.a == 0);
165static_assert (v90.x.f == 0);
166constexpr int v91 = { 123 };
167static_assert (v91 == 123);
168constexpr int v92 = { v91 };
169static_assert (v92 == 123);
170/* Verify that constexpr values can be used in various contexts requiring
171 (integer) constant expressions. */
172struct s93 { int x : v79.e.x.f; };
173constexpr int v94 = alignof (int);
174alignas (v94) int v95;
175constexpr int v97[100] = { [v82.x.f] = 7 };
176static int v98[v94];
66337ff8
JM
177constexpr _Complex double v99 = 1.0;
178constexpr _Complex float v100 = 12345;
b556d177
JM
179
180void
181f0 ()
182{
183 constexpr int fv0 = 3;
184 static_assert (fv0 == 3);
3db5bee0 185 auto constexpr fv1 = 4;
b556d177
JM
186 static_assert (fv1 == 4);
187 register constexpr float fv2 = 1.0;
3db5bee0 188 constexpr auto fv3 = 123;
b556d177
JM
189 static_assert (fv3 == 123);
190 constexpr register void *fv4 = (void *) 0;
191 const int *fv5 = &(constexpr int) { 234 };
192 const int *fv6 = &(constexpr static int) { 234 };
193 const int *fv7 = &(static constexpr int) { 234 };
194 typeof ((constexpr register int) { 234 }) *fv8;
195 typeof ((register constexpr int) { 234 }) *fv9;
196 int fv10 = (constexpr int) { 1 } + sizeof (struct fs *);
197 constexpr auto fv11 = (constexpr int) { (constexpr int) { 0 } };
198 static_assert (fv11 == 0);
199 constexpr char fv12[3] = { 1, 2, 3 };
200 (constexpr short [4]) { 9, 8, 7, -6 };
201 constexpr struct s18 fv13 = { 1234ULL, 0, 13.0f };
202 (constexpr struct s18) { 123, (void *) 0, 11 };
203 constexpr union u20 fv14 = { 2 };
204 (constexpr union u20) { 5 };
205 constexpr union u20 fv15 = { .b = 15.0 };
206 (constexpr union u20) { .b = 20 };
207 (constexpr float) { (float) (1.0f / 3.0f) };
208 (constexpr double) { (double) (1.0 / 3.0) };
209 (constexpr struct s18) { 0, 0, (double) (1.0 / 3.0) };
210 (constexpr char []) { "abc\xfe" };
211 (constexpr unsigned char []) { u8"xyz\xff" };
212 (constexpr unsigned char []) { "\x12\x7f" };
213 (constexpr signed char []) { "\x34\x66" };
214 (constexpr double) { (int) (double) 3.0 - (long) (double) 2.0 };
215 (constexpr int) { 1 + 2 + (int) 3.0 };
216 (constexpr typeof (nullptr)) { nullptr };
217 (constexpr _Complex double) { __builtin_complex (1.0f, 3.0f / 2.0f) };
218 (constexpr float) { 1234.0L };
219 (constexpr char) { 127ULL };
220#if FLT_MIN_EXP == -125 && FLT_MANT_DIG == 24
221 (constexpr float) { 0x1p-149 };
222 (constexpr float _Complex) { __builtin_complex (0x1p-149, 0x1p127) };
223 (constexpr float) { 0xffffffUL };
224 (constexpr float) { -0xffffffL };
225 (constexpr float) { 0xffffff0L };
226 (constexpr float) { 1ULL << 63 };
227#endif
228#if DBL_MIN_EXP == -1021 && DBL_MANT_DIG == 53
229 (constexpr double) { 0x1p-1074L };
230 (constexpr _Complex double) { __builtin_complex (0x1p1023L, 0x1p-1074L) };
231 (constexpr double) { 0x1fffffffffffffULL };
232 (constexpr double) { -0x1fffffffffffffLL };
233 (constexpr double) { 0x3ffffffffffffeULL };
234 (constexpr double) { 1ULL << 63 };
235#endif
236 (constexpr void *) { (void *) 0 };
237 (constexpr int *) { (void *) 0L };
238 (constexpr long *) { 0LL };
239 (constexpr int) {};
240 (constexpr float) {};
241 (constexpr long double) {};
242 (constexpr int *) {};
243 (constexpr void *) {};
244 (constexpr typeof (nullptr)) {};
245 (constexpr int *) { 0 };
246 (constexpr struct s57) { 0 };
247 (constexpr struct s57) { { } }; /* { dg-warning "braces around scalar initializer" } */
248 (constexpr struct s57) { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
249 (constexpr union u58) { 0 };
250 (constexpr union u58) { { } }; /* { dg-warning "braces around scalar initializer" } */
251 (constexpr union u58) { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
66337ff8
JM
252 (constexpr _Complex double) { 1.0 };
253 (constexpr _Complex float) { 12345 };
b556d177
JM
254 /* It's not entirely clear if constexpr declarations are allowed in this
255 position in a for loop; presume they are, as implicitly auto just as if no
256 storage class specifiers were used. */
257 for (constexpr int fv16 = 1;;)
258 break;
259 constexpr struct s67 fv17 = { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } };
260 static_assert (fv17.a == 1);
261 static_assert (fv17.f == 7);
262 constexpr struct s67 fv18 = fv17;
263 static_assert (fv18.a == 1);
264 static_assert (fv18.f == 7);
265 constexpr auto fv19 = fv17;
266 static_assert (fv19.a == 1);
267 static_assert (fv19.f == 7);
268 auto fv20 = fv17;
269 constexpr struct s68 fv21 = { fv18 };
270 static_assert (fv21.x.a == 1);
271 static_assert (fv21.x.f == 7);
272 constexpr union u69 fv22 = { };
273 static_assert (fv22.a == 0);
274 constexpr union u69 fv23 = { .e = fv21 };
275 static_assert (fv23.e.x.a == 1);
276 static_assert (fv23.e.x.f == 7);
277 constexpr union u69 fv24 = { .a = 1 };
278 static_assert (fv24.a == 1);
279 constexpr union u69 fv25 = { .e = { fv18 } };
280 static_assert (fv25.e.x.a == 1);
281 static_assert (fv25.e.x.f == 7);
282 enum fe80 { FE80 = fv25.e.x.f };
283 static_assert (FE80 == 7);
284 constexpr struct s70 fv26 = { fv25 };
285 static_assert (fv26.x.e.x.f == 7);
286 constexpr struct s68 fv27 = { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } };
287 static_assert (fv27.x.a == 5);
288 static_assert (fv27.x.f == 9);
289 constexpr struct s68 fv28 = { };
290 static_assert (fv28.x.a == 0);
291 static_assert (fv28.x.f == 0);
292 constexpr int fv29 = { 123 };
293 static_assert (fv29 == 123);
294 constexpr int fv30 = { fv29 };
295 static_assert (fv30 == 123);
296 static_assert ((constexpr struct s67) { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } }.f == 7);
297 static_assert ((constexpr struct s68) { fv18 }.x.a == 1);
298 static_assert ((constexpr union u69) { }.a == 0);
299 static_assert ((constexpr union u69) { .e = fv21 }.e.x.f == 7);
300 static_assert ((constexpr union u69) { .a = 1 }.a == 1);
301 static_assert ((constexpr union u69) { .e = { fv18 } }.e.x.a == 1);
302 static_assert ((constexpr struct s70) { fv25 }.x.e.x.f == 7);
303 static_assert ((constexpr struct s68) { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } }.x.f == 9);
304 static_assert ((constexpr struct s68) { }.x.f == 0);
305 /* Verify that constexpr values can be used in various contexts requiring
306 (integer) constant expressions. */
307 struct fs93 { int x : fv25.e.x.f; };
308 constexpr int fv31 = alignof (int);
309 alignas (fv31) int fv32;
310 constexpr int fv33[100] = { [fv27.x.f] = 7 };
311 static int fv34[fv31];
312 switch (fv0)
313 {
314 case fv27.x.f: ;
315 }
316}