1 | // RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu |
2 | |
3 | struct S { |
4 | // dummy ctor to make this a literal type |
5 | constexpr S(int); |
6 | |
7 | S(); |
8 | |
9 | int arr[10]; |
10 | |
11 | constexpr int &get(int n) { return arr[n]; } |
12 | constexpr const int &get(int n) const { return arr[n]; } |
13 | }; |
14 | |
15 | S s = S(); |
16 | const S &sr = s; |
17 | static_assert(&s.get(4) - &sr.get(2) == 2, ""); |
18 | |
19 | // Compound-statements can be used in constexpr functions. |
20 | constexpr int e() {{{{}} return 5; }} |
21 | static_assert(e() == 5, ""); |
22 | |
23 | // Types can be defined in constexpr functions. |
24 | constexpr int f() { |
25 | enum E { e1, e2, e3 }; |
26 | |
27 | struct S { |
28 | constexpr S(E e) : e(e) {} |
29 | constexpr int get() { return e; } |
30 | E e; |
31 | }; |
32 | |
33 | return S(e2).get(); |
34 | } |
35 | static_assert(f() == 1, ""); |
36 | |
37 | // Variables can be declared in constexpr functions. |
38 | constexpr int g(int k) { |
39 | const int n = 9; |
40 | int k2 = k * k; |
41 | int k3 = k2 * k; |
42 | return 3 * k3 + 5 * k2 + n * k - 20; |
43 | } |
44 | static_assert(g(2) == 42, ""); |
45 | constexpr int h(int n) { |
46 | static const int m = n; // expected-error {{static variable not permitted in a constexpr function}} |
47 | return m; |
48 | } |
49 | constexpr int i(int n) { |
50 | thread_local const int m = n; // expected-error {{thread_local variable not permitted in a constexpr function}} |
51 | return m; |
52 | } |
53 | |
54 | // if-statements can be used in constexpr functions. |
55 | constexpr int j(int k) { |
56 | if (k == 5) |
57 | return 1; |
58 | if (k == 1) |
59 | return 5; |
60 | else { |
61 | if (int n = 2 * k - 4) { |
62 | return n + 1; |
63 | return 2; |
64 | } |
65 | } |
66 | } // expected-note 2{{control reached end of constexpr function}} |
67 | static_assert(j(0) == -3, ""); |
68 | static_assert(j(1) == 5, ""); |
69 | static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}} |
70 | static_assert(j(3) == 3, ""); |
71 | static_assert(j(4) == 5, ""); |
72 | static_assert(j(5) == 1, ""); |
73 | |
74 | // There can be 0 return-statements. |
75 | constexpr void k() { |
76 | } |
77 | |
78 | // If the return type is not 'void', no return statements => never a constant |
79 | // expression, so still diagnose that case. |
80 | [[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}} |
81 | fn(); |
82 | } |
83 | |
84 | // We evaluate the body of a constexpr constructor, to check for side-effects. |
85 | struct U { |
86 | constexpr U(int n) { |
87 | if (j(n)) {} // expected-note {{in call to 'j(2)'}} |
88 | } |
89 | }; |
90 | constexpr U u1{1}; |
91 | constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}} |
92 | |
93 | // We allow expression-statements. |
94 | constexpr int l(bool b) { |
95 | if (b) |
96 | throw "invalid value for b!"; // expected-note {{subexpression not valid}} |
97 | return 5; |
98 | } |
99 | static_assert(l(false) == 5, ""); |
100 | static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}} |
101 | |
102 | // Potential constant expression checking is still applied where possible. |
103 | constexpr int htonl(int x) { // expected-error {{never produces a constant expression}} |
104 | typedef unsigned char uchar; |
105 | uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; |
106 | return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} |
107 | } |
108 | |
109 | constexpr int maybe_htonl(bool isBigEndian, int x) { |
110 | if (isBigEndian) |
111 | return x; |
112 | |
113 | typedef unsigned char uchar; |
114 | uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; |
115 | return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} |
116 | } |
117 | |
118 | constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}} |
119 | |
120 | namespace NS { |
121 | constexpr int n = 0; |
122 | } |
123 | constexpr int namespace_alias() { |
124 | namespace N = NS; |
125 | return N::n; |
126 | } |
127 | |
128 | namespace assign { |
129 | constexpr int a = 0; |
130 | const int b = 0; |
131 | int c = 0; // expected-note {{here}} |
132 | |
133 | constexpr void set(const int &a, int b) { |
134 | const_cast<int&>(a) = b; // expected-note 3{{constant expression cannot modify an object that is visible outside that expression}} |
135 | } |
136 | constexpr int wrap(int a, int b) { |
137 | set(a, b); |
138 | return a; |
139 | } |
140 | |
141 | static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}} |
142 | static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}} |
143 | static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(c, 1)'}} |
144 | |
145 | static_assert(wrap(a, 1) == 1, ""); |
146 | static_assert(wrap(b, 1) == 1, ""); |
147 | static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} |
148 | } |
149 | |
150 | namespace string_assign { |
151 | template<typename T> |
152 | constexpr void swap(T &a, T &b) { |
153 | T tmp = a; |
154 | a = b; |
155 | b = tmp; |
156 | } |
157 | template<typename Iterator> |
158 | constexpr void reverse(Iterator begin, Iterator end) { |
159 | while (begin != end && begin != --end) |
160 | swap(*begin++, *end); |
161 | } |
162 | template<typename Iterator1, typename Iterator2> |
163 | constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) { |
164 | while (a != ae && b != be) |
165 | if (*a++ != *b++) |
166 | return false; |
167 | return a == ae && b == be; |
168 | } |
169 | constexpr bool test1(int n) { |
170 | char stuff[100] = "foobarfoo"; |
171 | const char stuff2[100] = "oofraboof"; |
172 | reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}} |
173 | return equal(stuff, stuff + n, stuff2, stuff2 + n); |
174 | } |
175 | static_assert(!test1(1), ""); |
176 | static_assert(test1(3), ""); |
177 | static_assert(!test1(6), ""); |
178 | static_assert(test1(9), ""); |
179 | static_assert(!test1(100), ""); |
180 | static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}} |
181 | |
182 | constexpr void f() { // expected-error{{constexpr function never produces a constant expression}} expected-note@+2{{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}} |
183 | char foo[10] = { "z" }; // expected-note {{here}} |
184 | foo[10] = 'x'; // expected-warning {{past the end}} |
185 | } |
186 | } |
187 | |
188 | namespace array_resize { |
189 | constexpr int do_stuff(int k1, int k2) { |
190 | int arr[1234] = { 1, 2, 3, 4 }; |
191 | arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}} |
192 | return arr[k2]; |
193 | } |
194 | static_assert(do_stuff(1, 2) == 3, ""); |
195 | static_assert(do_stuff(0, 0) == 5, ""); |
196 | static_assert(do_stuff(1233, 1233) == 5, ""); |
197 | static_assert(do_stuff(1233, 0) == 1, ""); |
198 | static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
199 | static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
200 | static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
201 | } |
202 | |
203 | namespace potential_const_expr { |
204 | constexpr void set(int &n) { n = 1; } |
205 | constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error |
206 | constexpr int div_zero_2() { // expected-error {{never produces a constant expression}} |
207 | int z = 0; |
208 | return 100 / (set(z), 0); // expected-note {{division by zero}} |
209 | } |
210 | int n; // expected-note {{declared here}} |
211 | constexpr int ref() { // expected-error {{never produces a constant expression}} |
212 | int &r = n; |
213 | return r; // expected-note {{read of non-const variable 'n'}} |
214 | } |
215 | } |
216 | |
217 | namespace subobject { |
218 | union A { constexpr A() : y(5) {} int x, y; }; |
219 | struct B { A a; }; |
220 | struct C : B {}; |
221 | union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; }; |
222 | constexpr void f(D &d) { |
223 | d.c.a.y = 3; |
224 | // expected-note@-1 {{cannot modify an object that is visible outside}} |
225 | // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}} |
226 | } |
227 | constexpr bool check(D &d) { return d.c.a.y == 3; } |
228 | |
229 | constexpr bool g() { D d; f(d); return d.c.a.y == 3; } |
230 | static_assert(g(), ""); |
231 | |
232 | D d; |
233 | constexpr bool h() { f(d); return check(d); } // expected-note {{in call}} |
234 | static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
235 | |
236 | constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}} |
237 | static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
238 | |
239 | constexpr bool j() { D d; d.c.a.x = 3; return check(d); } // expected-note {{assignment to member 'x' of union with active member 'y'}} |
240 | static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
241 | } |
242 | |
243 | namespace lifetime { |
244 | constexpr int &&id(int &&n) { return static_cast<int&&>(n); } |
245 | constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}} |
246 | constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}} |
247 | static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
248 | } |
249 | |
250 | namespace const_modify { |
251 | constexpr int modify(int &n) { return n = 1; } // expected-note 2 {{modification of object of const-qualified type 'const int'}} |
252 | constexpr int test1() { int k = 0; return modify(k); } |
253 | constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note 2 {{in call}} |
254 | static_assert(test1() == 1, ""); |
255 | static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
256 | constexpr int i = test2(); // expected-error {{constant expression}} expected-note {{in call}} |
257 | } |
258 | |
259 | namespace null { |
260 | constexpr int test(int *p) { |
261 | return *p = 123; // expected-note {{assignment to dereferenced null pointer}} |
262 | } |
263 | static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}} |
264 | } |
265 | |
266 | namespace incdec { |
267 | template<typename T> constexpr T &ref(T &&r) { return r; } |
268 | template<typename T> constexpr T postinc(T &&r) { return (r++, r); } |
269 | template<typename T> constexpr T postdec(T &&r) { return (r--, r); } |
270 | |
271 | static_assert(++ref(0) == 1, ""); |
272 | static_assert(ref(0)++ == 0, ""); |
273 | static_assert(postinc(0) == 1, ""); |
274 | static_assert(--ref(0) == -1, ""); |
275 | static_assert(ref(0)-- == 0, ""); |
276 | static_assert(postdec(0) == -1, ""); |
277 | |
278 | constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}} |
279 | constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++; |
280 | constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}} |
281 | constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe); |
282 | |
283 | // inc/dec on short can't overflow because we promote to int first |
284 | static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, ""); |
285 | static_assert(--ref<short>(0x8000) == 0x7fff, ""); |
286 | |
287 | // inc on bool sets to true |
288 | static_assert(++ref(false), ""); // expected-warning {{deprecated}} |
289 | static_assert(++ref(true), ""); // expected-warning {{deprecated}} |
290 | |
291 | int arr[10]; |
292 | static_assert(++ref(&arr[0]) == &arr[1], ""); |
293 | static_assert(++ref(&arr[9]) == &arr[10], ""); |
294 | static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} |
295 | static_assert(ref(&arr[0])++ == &arr[0], ""); |
296 | static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} |
297 | static_assert(postinc(&arr[0]) == &arr[1], ""); |
298 | static_assert(--ref(&arr[10]) == &arr[9], ""); |
299 | static_assert(--ref(&arr[1]) == &arr[0], ""); |
300 | static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} |
301 | static_assert(ref(&arr[1])-- == &arr[1], ""); |
302 | static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} |
303 | static_assert(postdec(&arr[1]) == &arr[0], ""); |
304 | |
305 | int x; |
306 | static_assert(++ref(&x) == &x + 1, ""); |
307 | |
308 | static_assert(++ref(0.0) == 1.0, ""); |
309 | static_assert(ref(0.0)++ == 0.0, ""); |
310 | static_assert(postinc(0.0) == 1.0, ""); |
311 | static_assert(--ref(0.0) == -1.0, ""); |
312 | static_assert(ref(0.0)-- == 0.0, ""); |
313 | static_assert(postdec(0.0) == -1.0, ""); |
314 | |
315 | static_assert(++ref(1e100) == 1e100, ""); |
316 | static_assert(--ref(1e100) == 1e100, ""); |
317 | |
318 | union U { |
319 | int a, b; |
320 | }; |
321 | constexpr int f(U u) { |
322 | return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}} |
323 | } |
324 | constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}} |
325 | constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}} |
326 | |
327 | constexpr int incr(int k) { |
328 | int x = k; |
329 | if (x++ == 100) |
330 | return x; |
331 | return incr(x); |
332 | } |
333 | static_assert(incr(0) == 101, ""); |
334 | } |
335 | |
336 | namespace compound_assign { |
337 | constexpr bool test_int() { |
338 | int a = 3; |
339 | a += 6; |
340 | if (a != 9) return false; |
341 | a -= 2; |
342 | if (a != 7) return false; |
343 | a *= 3; |
344 | if (a != 21) return false; |
345 | if (&(a /= 10) != &a) return false; |
346 | if (a != 2) return false; |
347 | a <<= 3; |
348 | if (a != 16) return false; |
349 | a %= 6; |
350 | if (a != 4) return false; |
351 | a >>= 1; |
352 | if (a != 2) return false; |
353 | a ^= 10; |
354 | if (a != 8) return false; |
355 | a |= 5; |
356 | if (a != 13) return false; |
357 | a &= 14; |
358 | if (a != 12) return false; |
359 | a += -1.2; |
360 | if (a != 10) return false; |
361 | a -= 3.1; |
362 | if (a != 6) return false; |
363 | a *= 2.2; |
364 | if (a != 13) return false; |
365 | if (&(a /= 1.5) != &a) return false; |
366 | if (a != 8) return false; |
367 | return true; |
368 | } |
369 | static_assert(test_int(), ""); |
370 | |
371 | constexpr bool test_float() { |
372 | float f = 123.; |
373 | f *= 2; |
374 | if (f != 246.) return false; |
375 | if ((f -= 0.5) != 245.5) return false; |
376 | if (f != 245.5) return false; |
377 | f /= 0.5; |
378 | if (f != 491.) return false; |
379 | f += -40; |
380 | if (f != 451.) return false; |
381 | return true; |
382 | } |
383 | static_assert(test_float(), ""); |
384 | |
385 | constexpr bool test_bool() { |
386 | bool b = false; |
387 | b |= 2; |
388 | if (b != true) return false; |
389 | b <<= 1; |
390 | if (b != true) return false; |
391 | b *= 2; |
392 | if (b != true) return false; |
393 | b -= 1; |
394 | if (b != false) return false; |
395 | b -= 1; |
396 | if (b != true) return false; |
397 | b += -1; |
398 | if (b != false) return false; |
399 | b += 1; |
400 | if (b != true) return false; |
401 | b += 1; |
402 | if (b != true) return false; |
403 | b ^= b; |
404 | if (b != false) return false; |
405 | return true; |
406 | } |
407 | static_assert(test_bool(), ""); |
408 | |
409 | constexpr bool test_ptr() { |
410 | int arr[123] = {}; |
411 | int *p = arr; |
412 | if ((p += 4) != &arr[4]) return false; |
413 | if (p != &arr[4]) return false; |
414 | p += -1; |
415 | if (p != &arr[3]) return false; |
416 | if ((p -= -10) != &arr[13]) return false; |
417 | if (p != &arr[13]) return false; |
418 | p -= 11; |
419 | if (p != &arr[2]) return false; |
420 | return true; |
421 | } |
422 | static_assert(test_ptr(), ""); |
423 | |
424 | template<typename T> |
425 | constexpr bool test_overflow() { |
426 | T a = 1; |
427 | while (a != a / 2) |
428 | a *= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }} expected-note {{floating point arithmetic produces an infinity}} |
429 | return true; |
430 | } |
431 | |
432 | static_assert(test_overflow<int>(), ""); // expected-error {{constant}} expected-note {{call}} |
433 | static_assert(test_overflow<unsigned>(), ""); // ok, unsigned overflow is defined |
434 | static_assert(test_overflow<short>(), ""); // ok, short is promoted to int before multiplication |
435 | static_assert(test_overflow<unsigned short>(), ""); // ok |
436 | static_assert(test_overflow<unsigned long long>(), ""); // ok |
437 | static_assert(test_overflow<long long>(), ""); // expected-error {{constant}} expected-note {{call}} |
438 | static_assert(test_overflow<float>(), ""); // expected-error {{constant}} expected-note {{call}} |
439 | |
440 | constexpr short test_promotion(short k) { |
441 | short s = k; |
442 | s *= s; |
443 | return s; |
444 | } |
445 | static_assert(test_promotion(100) == 10000, ""); |
446 | static_assert(test_promotion(200) == -25536, ""); |
447 | static_assert(test_promotion(256) == 0, ""); |
448 | |
449 | constexpr const char *test_bounds(const char *p, int o) { |
450 | return p += o; // expected-note {{element 5 of}} expected-note {{element -1 of}} expected-note {{element 1000 of}} |
451 | } |
452 | static_assert(test_bounds("foo", 0)[0] == 'f', ""); |
453 | static_assert(test_bounds("foo", 3)[0] == 0, ""); |
454 | static_assert(test_bounds("foo", 4)[-3] == 'o', ""); |
455 | static_assert(test_bounds(&"foo"[4], -4)[0] == 'f', ""); |
456 | static_assert(test_bounds("foo", 5) != 0, ""); // expected-error {{constant}} expected-note {{call}} |
457 | static_assert(test_bounds("foo", -1) != 0, ""); // expected-error {{constant}} expected-note {{call}} |
458 | static_assert(test_bounds("foo", 1000) != 0, ""); // expected-error {{constant}} expected-note {{call}} |
459 | } |
460 | |
461 | namespace loops { |
462 | constexpr int fib_loop(int a) { |
463 | int f_k = 0, f_k_plus_one = 1; |
464 | for (int k = 1; k != a; ++k) { |
465 | int f_k_plus_two = f_k + f_k_plus_one; |
466 | f_k = f_k_plus_one; |
467 | f_k_plus_one = f_k_plus_two; |
468 | } |
469 | return f_k_plus_one; |
470 | } |
471 | static_assert(fib_loop(46) == 1836311903, ""); |
472 | |
473 | constexpr bool breaks_work() { |
474 | int a = 0; |
475 | for (int n = 0; n != 100; ++n) { |
476 | ++a; |
477 | if (a == 5) continue; |
478 | if ((a % 5) == 0) break; |
479 | } |
480 | |
481 | int b = 0; |
482 | while (b != 17) { |
483 | ++b; |
484 | if (b == 6) continue; |
485 | if ((b % 6) == 0) break; |
486 | } |
487 | |
488 | int c = 0; |
489 | do { |
490 | ++c; |
491 | if (c == 7) continue; |
492 | if ((c % 7) == 0) break; |
493 | } while (c != 21); |
494 | |
495 | return a == 10 && b == 12 && c == 14; |
496 | } |
497 | static_assert(breaks_work(), ""); |
498 | |
499 | void not_constexpr(); |
500 | constexpr bool no_cont_after_break() { |
501 | for (;;) { |
502 | break; |
503 | not_constexpr(); |
504 | } |
505 | while (true) { |
506 | break; |
507 | not_constexpr(); |
508 | } |
509 | do { |
510 | break; |
511 | not_constexpr(); |
512 | } while (true); |
513 | return true; |
514 | } |
515 | static_assert(no_cont_after_break(), ""); |
516 | |
517 | constexpr bool cond() { |
518 | for (int a = 1; bool b = a != 3; ++a) { |
519 | if (!b) |
520 | return false; |
521 | } |
522 | while (bool b = true) { |
523 | b = false; |
524 | break; |
525 | } |
526 | return true; |
527 | } |
528 | static_assert(cond(), ""); |
529 | |
530 | constexpr int range_for() { |
531 | int arr[] = { 1, 2, 3, 4, 5 }; |
532 | int sum = 0; |
533 | for (int x : arr) |
534 | sum += x; |
535 | return sum; |
536 | } |
537 | static_assert(range_for() == 15, ""); |
538 | |
539 | template<int...N> struct ints {}; |
540 | template<typename A, typename B> struct join_ints; |
541 | template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> { |
542 | using type = ints<As..., sizeof...(As) + Bs...>; |
543 | }; |
544 | template<unsigned N> struct make_ints { |
545 | using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type; |
546 | }; |
547 | template<> struct make_ints<0> { using type = ints<>; }; |
548 | template<> struct make_ints<1> { using type = ints<0>; }; |
549 | |
550 | struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} }; |
551 | |
552 | template<typename T, unsigned N> struct array { |
553 | constexpr array() : arr{} {} |
554 | template<typename ...X> |
555 | constexpr array(X ...x) : arr{} { |
556 | init(typename make_ints<sizeof...(X)>::type{}, x...); |
557 | } |
558 | template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) { |
559 | ignore{arr[I] = x ...}; |
560 | } |
561 | T arr[N]; |
562 | struct iterator { |
563 | T *p; |
564 | constexpr explicit iterator(T *p) : p(p) {} |
565 | constexpr bool operator!=(iterator o) { return p != o.p; } |
566 | constexpr iterator &operator++() { ++p; return *this; } |
567 | constexpr T &operator*() { return *p; } |
568 | }; |
569 | constexpr iterator begin() { return iterator(arr); } |
570 | constexpr iterator end() { return iterator(arr + N); } |
571 | }; |
572 | |
573 | constexpr int range_for_2() { |
574 | array<int, 5> arr { 1, 2, 3, 4, 5 }; |
575 | int sum = 0; |
576 | for (int k : arr) { |
577 | sum += k; |
578 | if (sum > 8) break; |
579 | } |
580 | return sum; |
581 | } |
582 | static_assert(range_for_2() == 10, ""); |
583 | } |
584 | |
585 | namespace assignment_op { |
586 | struct A { |
587 | constexpr A() : n(5) {} |
588 | int n; |
589 | struct B { |
590 | int k = 1; |
591 | union U { |
592 | constexpr U() : y(4) {} |
593 | int x; |
594 | int y; |
595 | } u; |
596 | } b; |
597 | }; |
598 | constexpr bool testA() { |
599 | A a, b; |
600 | a.n = 7; |
601 | a.b.u.y = 5; |
602 | b = a; |
603 | return b.n == 7 && b.b.u.y == 5 && b.b.k == 1; |
604 | } |
605 | static_assert(testA(), ""); |
606 | |
607 | struct B { |
608 | bool assigned = false; |
609 | constexpr B &operator=(const B&) { |
610 | assigned = true; |
611 | return *this; |
612 | } |
613 | }; |
614 | struct C : B { |
615 | B b; |
616 | int n = 5; |
617 | }; |
618 | constexpr bool testC() { |
619 | C c, d; |
620 | c.n = 7; |
621 | d = c; |
622 | c.n = 3; |
623 | return d.n == 7 && d.assigned && d.b.assigned; |
624 | } |
625 | static_assert(testC(), ""); |
626 | } |
627 | |
628 | namespace switch_stmt { |
629 | constexpr int f(char k) { |
630 | bool b = false; |
631 | int z = 6; |
632 | switch (k) { |
633 | return -1; |
634 | case 0: |
635 | if (false) { |
636 | case 1: |
637 | z = 1; |
638 | for (; b;) { |
639 | return 5; |
640 | while (0) |
641 | case 2: return 2; |
642 | case 7: z = 7; |
643 | do case 6: { |
644 | return z; |
645 | if (false) |
646 | case 3: return 3; |
647 | case 4: z = 4; |
648 | } while (1); |
649 | case 5: b = true; |
650 | case 9: z = 9; |
651 | } |
652 | return z; |
653 | } else if (false) case 8: z = 8; |
654 | else if (false) { |
655 | case 10: |
656 | z = -10; |
657 | break; |
658 | } |
659 | else z = 0; |
660 | return z; |
661 | default: |
662 | return -1; |
663 | } |
664 | return -z; |
665 | } |
666 | static_assert(f(0) == 0, ""); |
667 | static_assert(f(1) == 1, ""); |
668 | static_assert(f(2) == 2, ""); |
669 | static_assert(f(3) == 3, ""); |
670 | static_assert(f(4) == 4, ""); |
671 | static_assert(f(5) == 5, ""); |
672 | static_assert(f(6) == 6, ""); |
673 | static_assert(f(7) == 7, ""); |
674 | static_assert(f(8) == 8, ""); |
675 | static_assert(f(9) == 9, ""); |
676 | static_assert(f(10) == 10, ""); |
677 | |
678 | // Check that we can continue an outer loop from within a switch. |
679 | constexpr bool contin() { |
680 | for (int n = 0; n != 10; ++n) { |
681 | switch (n) { |
682 | case 0: |
683 | ++n; |
684 | continue; |
685 | case 1: |
686 | return false; |
687 | case 2: |
688 | return true; |
689 | } |
690 | } |
691 | return false; |
692 | } |
693 | static_assert(contin(), ""); |
694 | |
695 | constexpr bool switch_into_for() { |
696 | int n = 0; |
697 | switch (n) { |
698 | for (; n == 1; ++n) { |
699 | return n == 1; |
700 | case 0: ; |
701 | } |
702 | } |
703 | return false; |
704 | } |
705 | static_assert(switch_into_for(), ""); |
706 | |
707 | constexpr void duff_copy(char *a, const char *b, int n) { |
708 | switch ((n - 1) % 8 + 1) { |
709 | for ( ; n; n = (n - 1) & ~7) { |
710 | case 8: a[n-8] = b[n-8]; |
711 | case 7: a[n-7] = b[n-7]; |
712 | case 6: a[n-6] = b[n-6]; |
713 | case 5: a[n-5] = b[n-5]; |
714 | case 4: a[n-4] = b[n-4]; |
715 | case 3: a[n-3] = b[n-3]; |
716 | case 2: a[n-2] = b[n-2]; |
717 | case 1: a[n-1] = b[n-1]; |
718 | } |
719 | case 0: ; |
720 | } |
721 | } |
722 | |
723 | constexpr bool test_copy(const char *str, int n) { |
724 | char buffer[16] = {}; |
725 | duff_copy(buffer, str, n); |
726 | for (int i = 0; i != sizeof(buffer); ++i) |
727 | if (buffer[i] != (i < n ? str[i] : 0)) |
728 | return false; |
729 | return true; |
730 | } |
731 | static_assert(test_copy("foo", 0), ""); |
732 | static_assert(test_copy("foo", 1), ""); |
733 | static_assert(test_copy("foo", 2), ""); |
734 | static_assert(test_copy("hello world", 0), ""); |
735 | static_assert(test_copy("hello world", 7), ""); |
736 | static_assert(test_copy("hello world", 8), ""); |
737 | static_assert(test_copy("hello world", 9), ""); |
738 | static_assert(test_copy("hello world", 10), ""); |
739 | static_assert(test_copy("hello world", 10), ""); |
740 | } |
741 | |
742 | namespace deduced_return_type { |
743 | constexpr auto f() { return 0; } |
744 | template<typename T> constexpr auto g(T t) { return t; } |
745 | static_assert(f() == 0, ""); |
746 | static_assert(g(true), ""); |
747 | } |
748 | |
749 | namespace modify_temporary_during_construction { |
750 | struct A { int &&temporary; int x; int y; }; |
751 | constexpr int f(int &r) { r *= 9; return r - 12; } |
752 | constexpr A a = { 6, f(a.temporary), a.temporary }; // expected-note {{temporary created here}} |
753 | static_assert(a.x == 42, ""); |
754 | static_assert(a.y == 54, ""); |
755 | constexpr int k = a.temporary++; // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} |
756 | } |
757 | |
758 | namespace std { |
759 | typedef decltype(sizeof(int)) size_t; |
760 | |
761 | template <class _E> |
762 | class initializer_list |
763 | { |
764 | const _E* __begin_; |
765 | size_t __size_; |
766 | |
767 | constexpr initializer_list(const _E* __b, size_t __s) |
768 | : __begin_(__b), |
769 | __size_(__s) |
770 | {} |
771 | |
772 | public: |
773 | typedef _E value_type; |
774 | typedef const _E& reference; |
775 | typedef const _E& const_reference; |
776 | typedef size_t size_type; |
777 | |
778 | typedef const _E* iterator; |
779 | typedef const _E* const_iterator; |
780 | |
781 | constexpr initializer_list() : __begin_(nullptr), __size_(0) {} |
782 | |
783 | constexpr size_t size() const {return __size_;} |
784 | constexpr const _E* begin() const {return __begin_;} |
785 | constexpr const _E* end() const {return __begin_ + __size_;} |
786 | }; |
787 | } |
788 | |
789 | namespace InitializerList { |
790 | constexpr int sum(std::initializer_list<int> ints) { |
791 | int total = 0; |
792 | for (int n : ints) total += n; |
793 | return total; |
794 | } |
795 | static_assert(sum({1, 2, 3, 4, 5}) == 15, ""); |
796 | } |
797 | |
798 | namespace StmtExpr { |
799 | constexpr int f(int k) { |
800 | switch (k) { |
801 | case 0: |
802 | return 0; |
803 | |
804 | ({ |
805 | case 1: // expected-note {{not supported}} |
806 | return 1; |
807 | }); |
808 | } |
809 | } |
810 | static_assert(f(1) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
811 | |
812 | constexpr int g() { // expected-error {{never produces a constant}} |
813 | return ({ int n; n; }); // expected-note {{object of type 'int' is not initialized}} |
814 | } |
815 | |
816 | // FIXME: We should handle the void statement expression case. |
817 | constexpr int h() { // expected-error {{never produces a constant}} |
818 | ({ if (true) {} }); // expected-note {{not supported}} |
819 | return 0; |
820 | } |
821 | } |
822 | |
823 | namespace VirtualFromBase { |
824 | struct S1 { |
825 | virtual int f() const; |
826 | }; |
827 | struct S2 { |
828 | virtual int f(); |
829 | }; |
830 | template <typename T> struct X : T { |
831 | constexpr X() {} |
832 | double d = 0.0; |
833 | constexpr int f() { return sizeof(T); } |
834 | }; |
835 | |
836 | // Non-virtual f(), OK. |
837 | constexpr X<X<S1>> xxs1; |
838 | constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1); |
839 | static_assert(p->f() == sizeof(S1), ""); |
840 | |
841 | // Virtual f(), not OK. |
842 | constexpr X<X<S2>> xxs2; |
843 | constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2); |
844 | static_assert(q->f() == sizeof(X<S2>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}} |
845 | } |
846 | |
847 | namespace Lifetime { |
848 | constexpr int &get(int &&r) { return r; } |
849 | constexpr int f() { |
850 | int &r = get(123); |
851 | return r; // expected-note {{read of object outside its lifetime}} |
852 | } |
853 | static_assert(f() == 123, ""); // expected-error {{constant expression}} expected-note {{in call}} |
854 | |
855 | constexpr int g() { |
856 | int *p = 0; |
857 | { |
858 | int n = 0; |
859 | p = &n; |
860 | n = 42; |
861 | } |
862 | *p = 123; // expected-note {{assignment to object outside its lifetime}} |
863 | return *p; |
864 | } |
865 | static_assert(g() == 42, ""); // expected-error {{constant expression}} expected-note {{in call}} |
866 | |
867 | constexpr int h(int n) { |
868 | int *p[4] = {}; |
869 | int &&r = 1; |
870 | p[0] = &r; |
871 | while (int a = 1) { |
872 | p[1] = &a; |
873 | for (int b = 1; int c = 1; ) { |
874 | p[2] = &b, p[3] = &c; |
875 | break; |
876 | } |
877 | break; |
878 | } |
879 | *p[n] = 0; // expected-note 3{{assignment to object outside its lifetime}} |
880 | return *p[n]; |
881 | } |
882 | static_assert(h(0) == 0, ""); // ok, lifetime-extended |
883 | static_assert(h(1) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}} |
884 | static_assert(h(2) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}} |
885 | static_assert(h(3) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}} |
886 | |
887 | constexpr void lifetime_versus_loops() { |
888 | int *p = 0; |
889 | for (int i = 0; i != 2; ++i) { |
890 | int *q = p; |
891 | int n = 0; |
892 | p = &n; |
893 | if (i) |
894 | // This modifies the 'n' from the previous iteration of the loop outside |
895 | // its lifetime. |
896 | ++*q; // expected-note {{increment of object outside its lifetime}} |
897 | } |
898 | } |
899 | static_assert((lifetime_versus_loops(), true), ""); // expected-error {{constant expression}} expected-note {{in call}} |
900 | } |
901 | |
902 | namespace Bitfields { |
903 | struct A { |
904 | bool b : 1; |
905 | int n : 4; |
906 | unsigned u : 5; |
907 | }; |
908 | constexpr bool test() { |
909 | A a {}; |
910 | a.b += 2; |
911 | --a.n; |
912 | --a.u; |
913 | a.n = -a.n * 3; |
914 | return a.b == true && a.n == 3 && a.u == 31; |
915 | } |
916 | static_assert(test(), ""); |
917 | } |
918 | |
919 | namespace PR17615 { |
920 | struct A { |
921 | int &&r; |
922 | constexpr A(int &&r) : r(static_cast<int &&>(r)) {} |
923 | constexpr A() : A(0) { |
924 | (void)+r; // expected-note {{outside its lifetime}} |
925 | } |
926 | }; |
927 | constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}} |
928 | } |
929 | |
930 | namespace PR17331 { |
931 | template<typename T, unsigned int N> |
932 | constexpr T sum(const T (&arr)[N]) { |
933 | T result = 0; |
934 | for (T i : arr) |
935 | result += i; |
936 | return result; |
937 | } |
938 | |
939 | constexpr int ARR[] = { 1, 2, 3, 4, 5 }; |
940 | static_assert(sum(ARR) == 15, ""); |
941 | } |
942 | |
943 | namespace EmptyClass { |
944 | struct E1 {} e1; |
945 | union E2 {} e2; // expected-note 4{{here}} |
946 | struct E3 : E1 {} e3; |
947 | |
948 | template<typename E> |
949 | constexpr int f(E &a, int kind) { |
950 | switch (kind) { |
951 | case 0: { E e(a); return 0; } // expected-note {{read}} expected-note {{in call}} |
952 | case 1: { E e(static_cast<E&&>(a)); return 0; } // expected-note {{read}} expected-note {{in call}} |
953 | case 2: { E e; e = a; return 0; } // expected-note {{read}} expected-note {{in call}} |
954 | case 3: { E e; e = static_cast<E&&>(a); return 0; } // expected-note {{read}} expected-note {{in call}} |
955 | } |
956 | } |
957 | constexpr int test1 = f(e1, 0); |
958 | constexpr int test2 = f(e2, 0); // expected-error {{constant expression}} expected-note {{in call}} |
959 | constexpr int test3 = f(e3, 0); |
960 | constexpr int test4 = f(e1, 1); |
961 | constexpr int test5 = f(e2, 1); // expected-error {{constant expression}} expected-note {{in call}} |
962 | constexpr int test6 = f(e3, 1); |
963 | constexpr int test7 = f(e1, 2); |
964 | constexpr int test8 = f(e2, 2); // expected-error {{constant expression}} expected-note {{in call}} |
965 | constexpr int test9 = f(e3, 2); |
966 | constexpr int testa = f(e1, 3); |
967 | constexpr int testb = f(e2, 3); // expected-error {{constant expression}} expected-note {{in call}} |
968 | constexpr int testc = f(e3, 3); |
969 | } |
970 | |
971 | namespace SpeculativeEvalWrites { |
972 | // Ensure that we don't try to speculatively evaluate writes. |
973 | constexpr int f() { |
974 | int i = 0; |
975 | int a = 0; |
976 | // __builtin_object_size speculatively evaluates its first argument. |
977 | __builtin_object_size((i = 1, &a), 0); |
978 | return i; |
979 | } |
980 | |
981 | static_assert(!f(), ""); |
982 | } |
983 | |
984 | namespace PR27989 { |
985 | constexpr int f(int n) { |
986 | int a = (n = 1, 0); |
987 | return n; |
988 | } |
989 | static_assert(f(0) == 1, ""); |
990 | } |
991 | |
992 | namespace const_char { |
993 | template <int N> |
994 | constexpr int sum(const char (&Arr)[N]) { |
995 | int S = 0; |
996 | for (unsigned I = 0; I != N; ++I) |
997 | S += Arr[I]; // expected-note 2{{read of non-constexpr variable 'Cs' is not allowed}} |
998 | return S; |
999 | } |
1000 | |
1001 | // As an extension, we support evaluating some things that are `const` as though |
1002 | // they were `constexpr` when folding, but it should not be allowed in normal |
1003 | // constexpr evaluation. |
1004 | const char Cs[] = {'a', 'b'}; |
1005 | void foo() __attribute__((enable_if(sum(Cs) == 'a' + 'b', ""))); |
1006 | void run() { foo(); } |
1007 | |
1008 | static_assert(sum(Cs) == 'a' + 'b', ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'sum(Cs)'}} |
1009 | constexpr int S = sum(Cs); // expected-error{{must be initialized by a constant expression}} expected-note{{in call}} |
1010 | } |
1011 | |
1012 | constexpr void PR28739(int n) { // expected-error {{never produces a constant}} |
1013 | int *p = &n; |
1014 | p += (__int128)(unsigned long)-1; // expected-note {{cannot refer to element 18446744073709551615 of non-array object in a constant expression}} |
1015 | } |
1016 | |
1017 | constexpr void Void(int n) { |
1018 | void(n + 1); |
1019 | void(); |
1020 | } |
1021 | constexpr int void_test = (Void(0), 1); |
1022 | |
1023 | namespace PR19741 { |
1024 | constexpr void addone(int &m) { m++; } |
1025 | |
1026 | struct S { |
1027 | int m = 0; |
1028 | constexpr S() { addone(m); } |
1029 | }; |
1030 | constexpr bool evalS() { |
1031 | constexpr S s; |
1032 | return s.m == 1; |
1033 | } |
1034 | static_assert(evalS(), ""); |
1035 | |
1036 | struct Nested { |
1037 | struct First { int x = 42; }; |
1038 | union { |
1039 | First first; |
1040 | int second; |
1041 | }; |
1042 | int x; |
1043 | constexpr Nested(int x) : first(), x(x) { x = 4; } |
1044 | constexpr Nested() : Nested(42) { |
1045 | addone(first.x); |
1046 | x = 3; |
1047 | } |
1048 | }; |
1049 | constexpr bool evalNested() { |
1050 | constexpr Nested N; |
1051 | return N.first.x == 43; |
1052 | } |
1053 | static_assert(evalNested(), ""); |
1054 | } // namespace PR19741 |
1055 | |
1056 | namespace Mutable { |
1057 | struct A { mutable int n; }; // expected-note 2{{here}} |
1058 | constexpr int k = A{123}.n; // ok |
1059 | static_assert(k == 123, ""); |
1060 | |
1061 | struct Q { A &&a; int b = a.n; }; |
1062 | constexpr Q q = { A{456} }; // expected-note {{temporary}} |
1063 | static_assert(q.b == 456, ""); |
1064 | static_assert(q.a.n == 456, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} |
1065 | |
1066 | constexpr A a = {123}; |
1067 | constexpr int m = a.n; // expected-error {{constant expression}} expected-note {{mutable}} |
1068 | |
1069 | constexpr Q r = { static_cast<A&&>(const_cast<A&>(a)) }; // expected-error {{constant expression}} expected-note@-8 {{mutable}} |
1070 | |
1071 | struct B { |
1072 | mutable int n; // expected-note {{here}} |
1073 | int m; |
1074 | constexpr B() : n(1), m(n) {} // ok |
1075 | }; |
1076 | constexpr B b; |
1077 | constexpr int p = b.n; // expected-error {{constant expression}} expected-note {{mutable}} |
1078 | } |
1079 | |
1080 | namespace IndirectFields { |
1081 | |
1082 | // Reference indirect field. |
1083 | struct A { |
1084 | struct { |
1085 | union { |
1086 | int x = x = 3; // expected-note {{outside its lifetime}} |
1087 | }; |
1088 | }; |
1089 | constexpr A() {} |
1090 | }; |
1091 | static_assert(A().x == 3, ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'A()'}} |
1092 | |
1093 | // Reference another indirect field, with different 'this'. |
1094 | struct B { |
1095 | struct { |
1096 | union { |
1097 | int x = 3; |
1098 | }; |
1099 | int y = x; |
1100 | }; |
1101 | constexpr B() {} |
1102 | }; |
1103 | static_assert(B().y == 3, ""); |
1104 | |
1105 | // Nested evaluation of indirect field initializers. |
1106 | struct C { |
1107 | union { |
1108 | int x = 1; |
1109 | }; |
1110 | }; |
1111 | struct D { |
1112 | struct { |
1113 | C c; |
1114 | int y = c.x + 1; |
1115 | }; |
1116 | }; |
1117 | static_assert(D().y == 2, ""); |
1118 | |
1119 | // Explicit 'this'. |
1120 | struct E { |
1121 | int n = 0; |
1122 | struct { |
1123 | void *x = this; |
1124 | }; |
1125 | void *y = this; |
1126 | }; |
1127 | constexpr E e1 = E(); |
1128 | static_assert(e1.x != e1.y, ""); |
1129 | constexpr E e2 = E{0}; |
1130 | static_assert(e2.x != e2.y, ""); |
1131 | |
1132 | } // namespace IndirectFields |
1133 | |
1134 | constexpr bool indirect_builtin_constant_p(const char *__s) { |
1135 | return __builtin_constant_p(*__s); |
1136 | } |
1137 | constexpr bool n = indirect_builtin_constant_p("a"); |
1138 | |
1139 | __attribute__((enable_if(indirect_builtin_constant_p("a") == n, "OK"))) |
1140 | int test_in_enable_if() { return 0; } |
1141 | int n2 = test_in_enable_if(); |
1142 | |
1143 | template <bool n = indirect_builtin_constant_p("a")> |
1144 | int test_in_template_param() { return 0; } |
1145 | int n3 = test_in_template_param(); |
1146 | |
1147 | void test_in_case(int n) { |
1148 | switch (n) { |
1149 | case indirect_builtin_constant_p("abc"): |
1150 | break; |
1151 | } |
1152 | } |
1153 | enum InEnum1 { |
1154 | ONE = indirect_builtin_constant_p("abc") |
1155 | }; |
1156 | enum InEnum2 : int { |
1157 | TWO = indirect_builtin_constant_p("abc") |
1158 | }; |
1159 | enum class InEnum3 { |
1160 | THREE = indirect_builtin_constant_p("abc") |
1161 | }; |
1162 | |