1 // RUN: %clang_cc1 -triple i686-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -fsyntax-only -fcxx-exceptions -verify -std=c++11 -pedantic %s -Wno-comment
2 
3 namespace StaticAssertFoldTest {
4 
5 int x;
6 static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7 static_assert(false, "test"); // expected-error {{test}}
8 
9 }
10 
11 typedef decltype(sizeof(char)) size_t;
12 
13 template<typename T> constexpr T id(const T &t) { return t; }
14 template<typename T> constexpr T min(const T &a, const T &b) {
15   return a < b ? a : b;
16 }
17 template<typename T> constexpr T max(const T &a, const T &b) {
18   return a < b ? b : a;
19 }
20 template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
21 template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
22 
23 struct MemberZero {
24   constexpr int zero() const { return 0; }
25 };
26 
27 namespace DerivedToVBaseCast {
28 
29   struct U { int n; };
30   struct V : U { int n; };
31   struct A : virtual V { int n; };
32   struct Aa { int n; };
33   struct B : virtual A, Aa {};
34   struct C : virtual A, Aa {};
35   struct D : B, C {};
36 
37   D d;
38   constexpr B *p = &d;
39   constexpr C *q = &d;
40 
41   static_assert((void*)p != (void*)q, "");
42   static_assert((A*)p == (A*)q, "");
43   static_assert((Aa*)p != (Aa*)q, "");
44 
45   constexpr B &pp = d;
46   constexpr C &qq = d;
47   static_assert((void*)&pp != (void*)&qq, "");
48   static_assert(&(A&)pp == &(A&)qq, "");
49   static_assert(&(Aa&)pp != &(Aa&)qq, "");
50 
51   constexpr V *v = p;
52   constexpr V *w = q;
53   constexpr V *x = (A*)p;
54   static_assert(v == w, "");
55   static_assert(v == x, "");
56 
57   static_assert((U*)&d == p, "");
58   static_assert((U*)&d == q, "");
59   static_assert((U*)&d == v, "");
60   static_assert((U*)&d == w, "");
61   static_assert((U*)&d == x, "");
62 
63   struct X {};
64   struct Y1 : virtual X {};
65   struct Y2 : X {};
66   struct Z : Y1, Y2 {};
67   Z z;
68   static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
69 }
70 
71 namespace ConstCast {
72 
73 constexpr int n1 = 0;
74 constexpr int n2 = const_cast<int&>(n1);
75 constexpr int *n3 = const_cast<int*>(&n1);
76 constexpr int n4 = *const_cast<int*>(&n1);
77 constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
78 constexpr int **n6 = const_cast<int**>(&n3);
79 constexpr int n7 = **n5;
80 constexpr int n8 = **n6;
81 
82 // const_cast from prvalue to xvalue.
83 struct A { int n; };
84 constexpr int n9 = (const_cast<A&&>(A{123})).n;
85 static_assert(n9 == 123, "");
86 
87 }
88 
89 namespace TemplateArgumentConversion {
90   template<int n> struct IntParam {};
91 
92   using IntParam0 = IntParam<0>;
93   using IntParam0 = IntParam<id(0)>;
94   using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
95 }
96 
97 namespace CaseStatements {
98   void f(int n) {
99     switch (n) {
100     case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
101     case id(0): // expected-error {{duplicate case value '0'}}
102       return;
103     }
104   }
105 }
106 
107 extern int &Recurse1;
108 int &Recurse2 = Recurse1; // expected-note {{declared here}}
109 int &Recurse1 = Recurse2;
110 constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
111 
112 extern const int RecurseA;
113 const int RecurseB = RecurseA; // expected-note {{declared here}}
114 const int RecurseA = 10;
115 constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
116 
117 namespace MemberEnum {
118   struct WithMemberEnum {
119     enum E { A = 42 };
120   } wme;
121 
122   static_assert(wme.A == 42, "");
123 }
124 
125 namespace DefaultArguments {
126 
127 const int z = int();
128 constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
129   return a + b + *c + d;
130 }
131 const int four = 4;
132 constexpr int eight = 8;
133 constexpr const int twentyseven = 27;
134 static_assert(Sum() == 0, "");
135 static_assert(Sum(1) == 1, "");
136 static_assert(Sum(1, four) == 5, "");
137 static_assert(Sum(1, eight, &twentyseven) == 36, "");
138 static_assert(Sum(1, 2, &four, eight) == 15, "");
139 
140 }
141 
142 namespace Ellipsis {
143 
144 // Note, values passed through an ellipsis can't actually be used.
145 constexpr int F(int a, ...) { return a; }
146 static_assert(F(0) == 0, "");
147 static_assert(F(1, 0) == 1, "");
148 static_assert(F(2, "test") == 2, "");
149 static_assert(F(3, &F) == 3, "");
150 int k = 0; // expected-note {{here}}
151 static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
152 
153 }
154 
155 namespace Recursion {
156   constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
157   static_assert(fib(11) == 89, "");
158 
159   constexpr int gcd_inner(int a, int b) {
160     return b == 0 ? a : gcd_inner(b, a % b);
161   }
162   constexpr int gcd(int a, int b) {
163     return gcd_inner(max(a, b), min(a, b));
164   }
165 
166   static_assert(gcd(1749237, 5628959) == 7, "");
167 }
168 
169 namespace FunctionCast {
170   // When folding, we allow functions to be cast to different types. Such
171   // cast functions cannot be called, even if they're constexpr.
172   constexpr int f() { return 1; }
173   typedef double (*DoubleFn)();
174   typedef int (*IntFn)();
175   int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
176   int b[(int)IntFn(f)()];    // ok
177 }
178 
179 namespace StaticMemberFunction {
180   struct S {
181     static constexpr int k = 42;
182     static constexpr int f(int n) { return n * k + 2; }
183   } s;
184 
185   constexpr int n = s.f(19);
186   static_assert(S::f(19) == 800, "");
187   static_assert(s.f(19) == 800, "");
188   static_assert(n == 800, "");
189 
190   constexpr int (*sf1)(int) = &S::f;
191   constexpr int (*sf2)(int) = &s.f;
192   constexpr const int *sk = &s.k;
193 }
194 
195 namespace ParameterScopes {
196 
197   const int k = 42;
198   constexpr const int &ObscureTheTruth(const int &a) { return a; }
199   constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
200     return ObscureTheTruth(b ? a : k);
201   }
202   static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
203   constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
204 
205   constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
206     return ObscureTheTruth(b ? a : k);
207   }
208   static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
209   constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok
210 
211   constexpr int InternalReturnJunk(int n) {
212     return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}}
213   }
214   constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
215 
216   constexpr int LToR(int &n) { return n; }
217   constexpr int GrabCallersArgument(bool which, int a, int b) {
218     return LToR(which ? b : a);
219   }
220   static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
221   static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
222 
223 }
224 
225 namespace Pointers {
226 
227   constexpr int f(int n, const int *a, const int *b, const int *c) {
228     return n == 0 ? 0 : *a + f(n-1, b, c, a);
229   }
230 
231   const int x = 1, y = 10, z = 100;
232   static_assert(f(23, &x, &y, &z) == 788, "");
233 
234   constexpr int g(int n, int a, int b, int c) {
235     return f(n, &a, &b, &c);
236   }
237   static_assert(g(23, x, y, z) == 788, "");
238 
239 }
240 
241 namespace FunctionPointers {
242 
243   constexpr int Double(int n) { return 2 * n; }
244   constexpr int Triple(int n) { return 3 * n; }
245   constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
246   constexpr int Quadruple(int n) { return Twice(Double, n); }
247   constexpr auto Select(int n) -> int (*)(int) {
248     return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
249   }
250   constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
251 
252   static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
253 
254   constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
255 
256 }
257 
258 namespace PointerComparison {
259 
260 int x, y;
261 static_assert(&x == &y, "false"); // expected-error {{false}}
262 static_assert(&x != &y, "");
263 constexpr bool g1 = &x == &y;
264 constexpr bool g2 = &x != &y;
265 constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
266 constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
267 constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
268 constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
269 
270 struct S { int x, y; } s;
271 static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
272 static_assert(&s.x != &s.y, "");
273 static_assert(&s.x <= &s.y, "");
274 static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
275 static_assert(&s.x < &s.y, "");
276 static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
277 
278 static_assert(0 == &y, "false"); // expected-error {{false}}
279 static_assert(0 != &y, "");
280 constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
281 constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
282 constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
283 constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
284 
285 static_assert(&x == 0, "false"); // expected-error {{false}}
286 static_assert(&x != 0, "");
287 constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
288 constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
289 constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
290 constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
291 
292 static_assert(&x == &x, "");
293 static_assert(&x != &x, "false"); // expected-error {{false}}
294 static_assert(&x <= &x, "");
295 static_assert(&x >= &x, "");
296 static_assert(&x < &x, "false"); // expected-error {{false}}
297 static_assert(&x > &x, "false"); // expected-error {{false}}
298 
299 constexpr S* sptr = &s;
300 constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
301 
302 struct U {};
303 struct Str {
304   int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
305     expected-warning {{not an integral constant expression}} \
306     expected-note {{dynamic_cast is not allowed in a constant expression}}
307   int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
308     expected-warning {{not an integral constant expression}} \
309     expected-note {{reinterpret_cast is not allowed in a constant expression}}
310   int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
311     expected-warning {{not an integral constant expression}} \
312     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
313   int d : (S*)(42) == (S*)(42); // \
314     expected-warning {{not an integral constant expression}} \
315     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
316   int e : (Str*)(sptr) == (Str*)(sptr); // \
317     expected-warning {{not an integral constant expression}} \
318     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
319   int f : &(U&)(*sptr) == &(U&)(*sptr); // \
320     expected-warning {{not an integral constant expression}} \
321     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
322   int g : (S*)(void*)(sptr) == sptr; // \
323     expected-warning {{not an integral constant expression}} \
324     expected-note {{cast from 'void *' is not allowed in a constant expression}}
325 };
326 
327 extern char externalvar[];
328 constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
329 constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
330 static_assert(0 != "foo", "");
331 
332 }
333 
334 namespace MaterializeTemporary {
335 
336 constexpr int f(const int &r) { return r; }
337 constexpr int n = f(1);
338 
339 constexpr bool same(const int &a, const int &b) { return &a == &b; }
340 constexpr bool sameTemporary(const int &n) { return same(n, n); }
341 
342 static_assert(n, "");
343 static_assert(!same(4, 4), "");
344 static_assert(same(n, n), "");
345 static_assert(sameTemporary(9), "");
346 
347 struct A { int &&r; };
348 struct B { A &&a1; A &&a2; };
349 
350 constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}}
351 static_assert(&b1.a1 != &b1.a2, "");
352 static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
353 
354 constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}}
355 static_assert(&b1 != &b2, "");
356 static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
357 
358 constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
359 void foo() {
360   constexpr static B b1 { { 1 }, { 2 } }; // ok
361   constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
362   constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
363 }
364 
365 constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); // expected-warning 4{{unused}}
366 static_assert(&b4 != &b2, "");
367 
368 // Proposed DR: copy-elision doesn't trigger lifetime extension.
369 constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
370 
371 namespace NestedNonStatic {
372   // Proposed DR: for a reference constant expression to refer to a static
373   // storage duration temporary, that temporary must itself be initialized
374   // by a constant expression (a core constant expression is not enough).
375   struct A { int &&r; };
376   struct B { A &&a; };
377   constexpr B a = { A{0} }; // ok
378   constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
379 }
380 
381 namespace FakeInitList {
382   struct init_list_3_ints { const int (&x)[3]; };
383   struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; };
384   constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
385 }
386 
387 }
388 
389 constexpr int strcmp_ce(const char *p, const char *q) {
390   return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
391 }
392 
393 namespace StringLiteral {
394 
395 template<typename Char>
396 constexpr int MangleChars(const Char *p) {
397   return *p + 3 * (*p ? MangleChars(p+1) : 0);
398 }
399 
400 static_assert(MangleChars("constexpr!") == 1768383, "");
401 static_assert(MangleChars(u8"constexpr!") == 1768383, "");
402 static_assert(MangleChars(L"constexpr!") == 1768383, "");
403 static_assert(MangleChars(u"constexpr!") == 1768383, "");
404 static_assert(MangleChars(U"constexpr!") == 1768383, "");
405 
406 constexpr char c0 = "nought index"[0];
407 constexpr char c1 = "nice index"[10];
408 constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} expected-note {{read of dereferenced one-past-the-end pointer}}
409 constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}}
410 constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}}
411 
412 constexpr const char *p = "test" + 2;
413 static_assert(*p == 's', "");
414 
415 constexpr const char *max_iter(const char *a, const char *b) {
416   return *a < *b ? b : a;
417 }
418 constexpr const char *max_element(const char *a, const char *b) {
419   return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
420 }
421 
422 constexpr char str[] = "the quick brown fox jumped over the lazy dog";
423 constexpr const char *max = max_element(begin(str), end(str));
424 static_assert(*max == 'z', "");
425 static_assert(max == str + 38, "");
426 
427 static_assert(strcmp_ce("hello world", "hello world") == 0, "");
428 static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
429 static_assert(strcmp_ce("constexpr", "test") < 0, "");
430 static_assert(strcmp_ce("", " ") < 0, "");
431 
432 struct S {
433   int n : "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
434 };
435 
436 struct T {
437   char c[6];
438   constexpr T() : c{"foo"} {}
439 };
440 constexpr T t;
441 
442 static_assert(t.c[0] == 'f', "");
443 static_assert(t.c[1] == 'o', "");
444 static_assert(t.c[2] == 'o', "");
445 static_assert(t.c[3] == 0, "");
446 static_assert(t.c[4] == 0, "");
447 static_assert(t.c[5] == 0, "");
448 static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
449 
450 struct U {
451   wchar_t chars[6];
452   int n;
453 } constexpr u = { { L"test" }, 0 };
454 static_assert(u.chars[2] == L's', "");
455 
456 struct V {
457   char c[4];
458   constexpr V() : c("hi!") {}
459 };
460 static_assert(V().c[1] == "i"[0], "");
461 
462 namespace Parens {
463   constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")},
464                           d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")};
465   static_assert(a[0] == 'f', "");
466   static_assert(b[1] == 'o', "");
467   static_assert(c[2] == 'o', "");
468   static_assert(d[0] == 'f', "");
469   static_assert(e[1] == 'o', "");
470   static_assert(f[2] == 'o', "");
471   static_assert(f[5] == 0, "");
472   static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
473 }
474 
475 }
476 
477 namespace Array {
478 
479 template<typename Iter>
480 constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
481   return begin == end ? 0 : *begin + Sum(begin+1, end);
482 }
483 
484 constexpr int xs[] = { 1, 2, 3, 4, 5 };
485 constexpr int ys[] = { 5, 4, 3, 2, 1 };
486 constexpr int sum_xs = Sum(begin(xs), end(xs));
487 static_assert(sum_xs == 15, "");
488 
489 constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
490                        const int *xs, const int *ys, int c) {
491   return n ? F(
492                *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
493                *ys,
494                ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
495       expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
496       expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
497            : c;
498 }
499 constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
500 constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
501 static_assert(InnerProduct == 35, "");
502 
503 constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
504 constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
505 static_assert(DiffProd == 8, "");
506 static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
507       expected-error {{constant expression}} \
508       expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
509 
510 constexpr const int *p = xs + 3;
511 constexpr int xs4 = p[1]; // ok
512 constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
513 constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
514 constexpr int xs0 = p[-3]; // ok
515 constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
516 
517 constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
518 static_assert(zs[0][0][0][0] == 1, "");
519 static_assert(zs[1][1][1][1] == 16, "");
520 static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
521 static_assert((&zs[0][0][0][2])[-1] == 2, "");
522 static_assert(**(**(zs + 1) + 1) == 11, "");
523 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}}
524 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
525 constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}}
526 
527 constexpr int fail(const int &p) {
528   return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
529 }
530 static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
531 expected-error {{static_assert expression is not an integral constant expression}} \
532 expected-note {{in call to 'fail(zs[1][0][1][0])'}}
533 
534 constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
535 constexpr int SumNonzero(const int *p) {
536   return *p + (*p ? SumNonzero(p+1) : 0);
537 }
538 constexpr int CountZero(const int *p, const int *q) {
539   return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
540 }
541 static_assert(SumNonzero(arr) == 6, "");
542 static_assert(CountZero(arr, arr + 40) == 36, "");
543 
544 struct ArrayElem {
545   constexpr ArrayElem() : n(0) {}
546   int n;
547   constexpr int f() const { return n; }
548 };
549 struct ArrayRVal {
550   constexpr ArrayRVal() {}
551   ArrayElem elems[10];
552 };
553 static_assert(ArrayRVal().elems[3].f() == 0, "");
554 
555 constexpr int selfref[2][2][2] = {
556   selfref[1][1][1] + 1, selfref[0][0][0] + 1,
557   selfref[1][0][1] + 1, selfref[0][1][0] + 1,
558   selfref[1][0][0] + 1, selfref[0][1][1] + 1 };
559 static_assert(selfref[0][0][0] == 1, "");
560 static_assert(selfref[0][0][1] == 2, "");
561 static_assert(selfref[0][1][0] == 1, "");
562 static_assert(selfref[0][1][1] == 2, "");
563 static_assert(selfref[1][0][0] == 1, "");
564 static_assert(selfref[1][0][1] == 3, "");
565 static_assert(selfref[1][1][0] == 0, "");
566 static_assert(selfref[1][1][1] == 0, "");
567 
568 struct TrivialDefCtor { int n; };
569 typedef TrivialDefCtor TDCArray[2][2];
570 static_assert(TDCArray{}[1][1].n == 0, "");
571 
572 struct NonAggregateTDC : TrivialDefCtor {};
573 typedef NonAggregateTDC NATDCArray[2][2];
574 static_assert(NATDCArray{}[1][1].n == 0, "");
575 
576 }
577 
578 namespace DependentValues {
579 
580 struct I { int n; typedef I V[10]; };
581 I::V x, y;
582 int g();
583 template<bool B, typename T> struct S : T {
584   int k;
585   void f() {
586     I::V &cells = B ? x : y;
587     I &i = cells[k];
588     switch (i.n) {}
589 
590     // FIXME: We should be able to diagnose this.
591     constexpr int n = g();
592 
593     constexpr int m = this->g(); // ok, could be constexpr
594   }
595 };
596 
597 }
598 
599 namespace Class {
600 
601 struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
602 constexpr int fn(const A &a) { return a.k; }
603 static_assert(fn(A(4,5)) == 9, "");
604 
605 struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
606 struct C {
607   constexpr C(C *this_) : m(42), n(this_->m) {} // ok
608   int m, n;
609 };
610 struct D {
611   C c;
612   constexpr D() : c(&c) {}
613 };
614 static_assert(D().c.n == 42, "");
615 
616 struct E {
617   constexpr E() : p(&p) {}
618   void *p;
619 };
620 constexpr const E &e1 = E();
621 // This is a constant expression if we elide the copy constructor call, and
622 // is not a constant expression if we don't! But we do, so it is.
623 constexpr E e2 = E();
624 static_assert(e2.p == &e2.p, "");
625 constexpr E e3;
626 static_assert(e3.p == &e3.p, "");
627 
628 extern const class F f;
629 struct F {
630   constexpr F() : p(&f.p) {}
631   const void *p;
632 };
633 constexpr F f;
634 
635 struct G {
636   struct T {
637     constexpr T(T *p) : u1(), u2(p) {}
638     union U1 {
639       constexpr U1() {}
640       int a, b = 42;
641     } u1;
642     union U2 {
643       constexpr U2(T *p) : c(p->u1.b) {}
644       int c, d;
645     } u2;
646   } t;
647   constexpr G() : t(&t) {}
648 } constexpr g;
649 
650 static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
651 static_assert(g.t.u1.b == 42, "");
652 static_assert(g.t.u2.c == 42, "");
653 static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
654 
655 struct S {
656   int a, b;
657   const S *p;
658   double d;
659   const char *q;
660 
661   constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
662 };
663 
664 S global(43, &global);
665 
666 static_assert(S(15, &global).b == 15, "");
667 
668 constexpr bool CheckS(const S &s) {
669   return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
670 }
671 static_assert(CheckS(S(27, &global)), "");
672 
673 struct Arr {
674   char arr[3];
675   constexpr Arr() : arr{'x', 'y', 'z'} {}
676 };
677 constexpr int hash(Arr &&a) {
678   return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
679 }
680 constexpr int k = hash(Arr());
681 static_assert(k == 0x007a7978, "");
682 
683 
684 struct AggregateInit {
685   const char &c;
686   int n;
687   double d;
688   int arr[5];
689   void *p;
690 };
691 
692 constexpr AggregateInit agg1 = { "hello"[0] };
693 
694 static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
695 static_assert(agg1.n == 0, "");
696 static_assert(agg1.d == 0.0, "");
697 static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
698 static_assert(agg1.arr[0] == 0, "");
699 static_assert(agg1.arr[4] == 0, "");
700 static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
701 static_assert(agg1.p == nullptr, "");
702 
703 static constexpr const unsigned char uc[] = { "foo" };
704 static_assert(uc[0] == 'f', "");
705 static_assert(uc[3] == 0, "");
706 
707 namespace SimpleDerivedClass {
708 
709 struct B {
710   constexpr B(int n) : a(n) {}
711   int a;
712 };
713 struct D : B {
714   constexpr D(int n) : B(n) {}
715 };
716 constexpr D d(3);
717 static_assert(d.a == 3, "");
718 
719 }
720 
721 struct Bottom { constexpr Bottom() {} };
722 struct Base : Bottom {
723   constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
724   int a;
725   const char *b;
726 };
727 struct Base2 : Bottom {
728   constexpr Base2(const int &r) : r(r) {}
729   int q = 123;
730   const int &r;
731 };
732 struct Derived : Base, Base2 {
733   constexpr Derived() : Base(76), Base2(a) {}
734   int c = r + b[1];
735 };
736 
737 constexpr bool operator==(const Base &a, const Base &b) {
738   return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
739 }
740 
741 constexpr Base base;
742 constexpr Base base2(76);
743 constexpr Derived derived;
744 static_assert(derived.a == 76, "");
745 static_assert(derived.b[2] == 's', "");
746 static_assert(derived.c == 76 + 'e', "");
747 static_assert(derived.q == 123, "");
748 static_assert(derived.r == 76, "");
749 static_assert(&derived.r == &derived.a, "");
750 
751 static_assert(!(derived == base), "");
752 static_assert(derived == base2, "");
753 
754 constexpr Bottom &bot1 = (Base&)derived;
755 constexpr Bottom &bot2 = (Base2&)derived;
756 static_assert(&bot1 != &bot2, "");
757 
758 constexpr Bottom *pb1 = (Base*)&derived;
759 constexpr Bottom *pb2 = (Base2*)&derived;
760 static_assert(&pb1 != &pb2, "");
761 static_assert(pb1 == &bot1, "");
762 static_assert(pb2 == &bot2, "");
763 
764 constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
765 constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
766 constexpr Base2 &ok2 = (Base2&)bot2;
767 static_assert(&ok2 == &derived, "");
768 
769 constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
770 constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
771 constexpr Base2 *pok2 = (Base2*)pb2;
772 static_assert(pok2 == &derived, "");
773 static_assert(&ok2 == pok2, "");
774 static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
775 static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
776 
777 // Core issue 903: we do not perform constant evaluation when checking for a
778 // null pointer in C++11. Just check for an integer literal with value 0.
779 constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Class::Base *const' with an rvalue of type 'int'}}
780 constexpr Base *nullB1 = 0;
781 static_assert((Bottom*)nullB == 0, "");
782 static_assert((Derived*)nullB == 0, "");
783 static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
784 Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'char'}}
785 Base *nullB3 = (0);
786 Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'bool'}}
787 Base *nullB5 = ((0ULL));
788 Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'double'}}
789 enum Null { kNull };
790 Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'Class::Null'}}
791 static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
792 
793 
794 
795 namespace ConversionOperators {
796 
797 struct T {
798   constexpr T(int n) : k(5*n - 3) {}
799   constexpr operator int() const { return k; }
800   int k;
801 };
802 
803 struct S {
804   constexpr S(int n) : k(2*n + 1) {}
805   constexpr operator int() const { return k; }
806   constexpr operator T() const { return T(k); }
807   int k;
808 };
809 
810 constexpr bool check(T a, T b) { return a == b.k; }
811 
812 static_assert(S(5) == 11, "");
813 static_assert(check(S(5), 11), "");
814 
815 namespace PR14171 {
816 
817 struct X {
818   constexpr (operator int)() const { return 0; }
819 };
820 static_assert(X() == 0, "");
821 
822 }
823 
824 }
825 
826 }
827 
828 namespace Temporaries {
829 
830 struct S {
831   constexpr S() {}
832   constexpr int f() const;
833   constexpr int g() const;
834 };
835 struct T : S {
836   constexpr T(int n) : S(), n(n) {}
837   int n;
838 };
839 constexpr int S::f() const {
840   return static_cast<const T*>(this)->n; // expected-note {{cannot cast}}
841 }
842 constexpr int S::g() const {
843   // FIXME: Better diagnostic for this.
844   return this->*(int(S::*))&T::n; // expected-note {{subexpression}}
845 }
846 // The T temporary is implicitly cast to an S subobject, but we can recover the
847 // T full-object via a base-to-derived cast, or a derived-to-base-casted member
848 // pointer.
849 static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->f()'}}
850 static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->g()'}}
851 static_assert(T(3).f() == 3, "");
852 static_assert(T(4).g() == 4, "");
853 
854 constexpr int f(const S &s) {
855   return static_cast<const T&>(s).n;
856 }
857 constexpr int n = f(T(5));
858 static_assert(f(T(5)) == 5, "");
859 
860 constexpr bool b(int n) { return &n; }
861 static_assert(b(0), "");
862 
863 }
864 
865 namespace Union {
866 
867 union U {
868   int a;
869   int b;
870 };
871 
872 constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
873 static_assert(u[0].a == 0, "");
874 static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
875 static_assert(u[1].b == 1, "");
876 static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
877 static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
878 static_assert((&(u[1]) + 1 + 1)->b == 3, "");
879 
880 constexpr U v = {};
881 static_assert(v.a == 0, "");
882 
883 union Empty {};
884 constexpr Empty e = {};
885 
886 // Make sure we handle trivial copy constructors for unions.
887 constexpr U x = {42};
888 constexpr U y = x;
889 static_assert(y.a == 42, "");
890 static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
891 
892 }
893 
894 namespace MemberPointer {
895   struct A {
896     constexpr A(int n) : n(n) {}
897     int n;
898     constexpr int f() const { return n + 3; }
899   };
900   constexpr A a(7);
901   static_assert(A(5).*&A::n == 5, "");
902   static_assert((&a)->*&A::n == 7, "");
903   static_assert((A(8).*&A::f)() == 11, "");
904   static_assert(((&a)->*&A::f)() == 10, "");
905 
906   struct B : A {
907     constexpr B(int n, int m) : A(n), m(m) {}
908     int m;
909     constexpr int g() const { return n + m + 1; }
910   };
911   constexpr B b(9, 13);
912   static_assert(B(4, 11).*&A::n == 4, "");
913   static_assert(B(4, 11).*&B::m == 11, "");
914   static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
915   static_assert((&b)->*&A::n == 9, "");
916   static_assert((&b)->*&B::m == 13, "");
917   static_assert((&b)->*(int(A::*))&B::m == 13, "");
918   static_assert((B(4, 11).*&A::f)() == 7, "");
919   static_assert((B(4, 11).*&B::g)() == 16, "");
920   static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
921   static_assert(((&b)->*&A::f)() == 12, "");
922   static_assert(((&b)->*&B::g)() == 23, "");
923   static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
924 
925   struct S {
926     constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
927       m(m), n(n), pf(pf), pn(pn) {}
928     constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
929 
930     constexpr int f() const { return this->*pn; }
931     virtual int g() const;
932 
933     int m, n;
934     int (S::*pf)() const;
935     int S::*pn;
936   };
937 
938   constexpr int S::*pm = &S::m;
939   constexpr int S::*pn = &S::n;
940   constexpr int (S::*pf)() const = &S::f;
941   constexpr int (S::*pg)() const = &S::g;
942 
943   constexpr S s(2, 5, &S::f, &S::m);
944 
945   static_assert((s.*&S::f)() == 2, "");
946   static_assert((s.*s.pf)() == 2, "");
947 
948   static_assert(pf == &S::f, "");
949   static_assert(pf == s.*&S::pf, "");
950   static_assert(pm == &S::m, "");
951   static_assert(pm != pn, "");
952   static_assert(s.pn != pn, "");
953   static_assert(s.pn == pm, "");
954   static_assert(pg != nullptr, "");
955   static_assert(pf != nullptr, "");
956   static_assert((int S::*)nullptr == nullptr, "");
957   static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
958   static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
959 
960   template<int n> struct T : T<n-1> {};
961   template<> struct T<0> { int n; };
962   template<> struct T<30> : T<29> { int m; };
963 
964   T<17> t17;
965   T<30> t30;
966 
967   constexpr int (T<10>::*deepn) = &T<0>::n;
968   static_assert(&(t17.*deepn) == &t17.n, "");
969   static_assert(deepn == &T<2>::n, "");
970 
971   constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
972   constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
973   static_assert(&(t30.*deepm) == &t30.m, "");
974   static_assert(deepm == &T<50>::m, "");
975   static_assert(deepm != deepn, "");
976 
977   constexpr T<5> *p17_5 = &t17;
978   constexpr T<13> *p17_13 = (T<13>*)p17_5;
979   constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'T<17>' to type 'T<23>'}}
980   static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
981   static_assert(&(p17_13->*deepn) == &t17.n, "");
982   constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
983 
984   constexpr T<5> *p30_5 = &t30;
985   constexpr T<23> *p30_23 = (T<23>*)p30_5;
986   constexpr T<13> *p30_13 = p30_23;
987   static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
988   static_assert(&(p30_13->*deepn) == &t30.n, "");
989   static_assert(&(p30_23->*deepn) == &t30.n, "");
990   static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
991   static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
992   static_assert(&(p30_23->*deepm) == &t30.m, "");
993 
994   struct Base { int n; };
995   template<int N> struct Mid : Base {};
996   struct Derived : Mid<0>, Mid<1> {};
997   static_assert(&Mid<0>::n == &Mid<1>::n, "");
998   static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n !=
999                 (int Derived::*)(int Mid<1>::*)&Mid<1>::n, "");
1000   static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, "");
1001 }
1002 
1003 namespace ArrayBaseDerived {
1004 
1005   struct Base {
1006     constexpr Base() {}
1007     int n = 0;
1008   };
1009   struct Derived : Base {
1010     constexpr Derived() {}
1011     constexpr const int *f() const { return &n; }
1012   };
1013 
1014   constexpr Derived a[10];
1015   constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
1016   constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
1017   static_assert(pb3 == pd3, "");
1018 
1019   // pb3 does not point to an array element.
1020   constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
1021   constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1022   constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1023   constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1024   constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1025   constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1026   constexpr Base *pb3a = pb4 - 1;
1027 
1028   // pb4 does not point to a Derived.
1029   constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1030   constexpr Derived *pd3a = (Derived*)pb3a;
1031   constexpr int pd3n = pd3a->n;
1032 
1033   // pd3a still points to the Derived array.
1034   constexpr Derived *pd6 = pd3a + 3;
1035   static_assert(pd6 == &a[6], "");
1036   constexpr Derived *pd9 = pd6 + 3;
1037   constexpr Derived *pd10 = pd6 + 4;
1038   constexpr int pd9n = pd9->n; // ok
1039   constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1040   constexpr int pd0n = pd10[-10].n;
1041   constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1042 
1043   constexpr Base *pb9 = pd9;
1044   constexpr const int *(Base::*pfb)() const =
1045       static_cast<const int *(Base::*)() const>(&Derived::f);
1046   static_assert((pb9->*pfb)() == &a[9].n, "");
1047 }
1048 
1049 namespace Complex {
1050 
1051 class complex {
1052   int re, im;
1053 public:
1054   constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
1055   constexpr complex(const complex &o) : re(o.re), im(o.im) {}
1056   constexpr complex operator-() const { return complex(-re, -im); }
1057   friend constexpr complex operator+(const complex &l, const complex &r) {
1058     return complex(l.re + r.re, l.im + r.im);
1059   }
1060   friend constexpr complex operator-(const complex &l, const complex &r) {
1061     return l + -r;
1062   }
1063   friend constexpr complex operator*(const complex &l, const complex &r) {
1064     return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
1065   }
1066   friend constexpr bool operator==(const complex &l, const complex &r) {
1067     return l.re == r.re && l.im == r.im;
1068   }
1069   constexpr bool operator!=(const complex &r) const {
1070     return re != r.re || im != r.im;
1071   }
1072   constexpr int real() const { return re; }
1073   constexpr int imag() const { return im; }
1074 };
1075 
1076 constexpr complex i = complex(0, 1);
1077 constexpr complex k = (3 + 4*i) * (6 - 4*i);
1078 static_assert(complex(1,0).real() == 1, "");
1079 static_assert(complex(1,0).imag() == 0, "");
1080 static_assert(((complex)1).imag() == 0, "");
1081 static_assert(k.real() == 34, "");
1082 static_assert(k.imag() == 12, "");
1083 static_assert(k - 34 == 12*i, "");
1084 static_assert((complex)1 == complex(1), "");
1085 static_assert((complex)1 != complex(0, 1), "");
1086 static_assert(complex(1) == complex(1), "");
1087 static_assert(complex(1) != complex(0, 1), "");
1088 constexpr complex makeComplex(int re, int im) { return complex(re, im); }
1089 static_assert(makeComplex(1,0) == complex(1), "");
1090 static_assert(makeComplex(1,0) != complex(0, 1), "");
1091 
1092 class complex_wrap : public complex {
1093 public:
1094   constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
1095   constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
1096 };
1097 
1098 static_assert((complex_wrap)1 == complex(1), "");
1099 static_assert((complex)1 != complex_wrap(0, 1), "");
1100 static_assert(complex(1) == complex_wrap(1), "");
1101 static_assert(complex_wrap(1) != complex(0, 1), "");
1102 constexpr complex_wrap makeComplexWrap(int re, int im) {
1103   return complex_wrap(re, im);
1104 }
1105 static_assert(makeComplexWrap(1,0) == complex(1), "");
1106 static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1107 
1108 }
1109 
1110 namespace PR11595 {
1111   struct A { constexpr bool operator==(int x) const { return true; } };
1112   struct B { B(); A& x; };
1113   static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1114 
1115   constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}}
1116     return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1117   }
1118 }
1119 
1120 namespace ExprWithCleanups {
1121   struct A { A(); ~A(); int get(); };
1122   constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
1123   constexpr int n = get(false);
1124 }
1125 
1126 namespace Volatile {
1127 
1128 volatile constexpr int n1 = 0; // expected-note {{here}}
1129 volatile const int n2 = 0; // expected-note {{here}}
1130 int n3 = 37; // expected-note {{declared here}}
1131 
1132 constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1133 constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1134 constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1135 constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1136 
1137 struct T { int n; };
1138 const T t = { 42 }; // expected-note {{declared here}}
1139 
1140 constexpr int f(volatile int &&r) {
1141   return r; // expected-note {{read of volatile-qualified type 'volatile int'}}
1142 }
1143 constexpr int g(volatile int &&r) {
1144   return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1145 }
1146 struct S {
1147   int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1148   int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1149   int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1150   int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
1151 };
1152 
1153 }
1154 
1155 namespace ExternConstexpr {
1156   extern constexpr int n = 0;
1157   extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
1158   void f() {
1159     extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
1160     constexpr int j = 0;
1161     constexpr int k; // expected-error {{default initialization of an object of const type}}
1162   }
1163 }
1164 
1165 namespace ComplexConstexpr {
1166   constexpr _Complex float test1 = {};
1167   constexpr _Complex float test2 = {1};
1168   constexpr _Complex double test3 = {1,2};
1169   constexpr _Complex int test4 = {4};
1170   constexpr _Complex int test5 = 4;
1171   constexpr _Complex int test6 = {5,6};
1172   typedef _Complex float fcomplex;
1173   constexpr fcomplex test7 = fcomplex();
1174 
1175   constexpr const double &t2r = __real test3;
1176   constexpr const double &t2i = __imag test3;
1177   static_assert(&t2r + 1 == &t2i, "");
1178   static_assert(t2r == 1.0, "");
1179   static_assert(t2i == 2.0, "");
1180   constexpr const double *t2p = &t2r;
1181   static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1182   static_assert(t2p[0] == 1.0, "");
1183   static_assert(t2p[1] == 2.0, "");
1184   static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1185   static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1186   constexpr _Complex float *p = 0;
1187   constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1188   constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1189   constexpr const _Complex double *q = &test3 + 1;
1190   constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1191   constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1192 
1193   static_assert(__real test6 == 5, "");
1194   static_assert(__imag test6 == 6, "");
1195   static_assert(&__imag test6 == &__real test6 + 1, "");
1196 }
1197 
1198 // _Atomic(T) is exactly like T for the purposes of constant expression
1199 // evaluation..
1200 namespace Atomic {
1201   constexpr _Atomic int n = 3;
1202 
1203   struct S { _Atomic(double) d; };
1204   constexpr S s = { 0.5 };
1205   constexpr double d1 = s.d;
1206   constexpr double d2 = n;
1207   constexpr _Atomic double d3 = n;
1208 
1209   constexpr _Atomic(int) n2 = d3;
1210   static_assert(d1 == 0.5, "");
1211   static_assert(d3 == 3.0, "");
1212 
1213   namespace PR16056 {
1214     struct TestVar {
1215       _Atomic(int) value;
1216       constexpr TestVar(int value) : value(value) {}
1217     };
1218     constexpr TestVar testVar{-1};
1219     static_assert(testVar.value == -1, "");
1220   }
1221 }
1222 
1223 namespace InstantiateCaseStmt {
1224   template<int x> constexpr int f() { return x; }
1225   template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; }
1226   int gg(int c) { return g<4>(c); }
1227 }
1228 
1229 namespace ConvertedConstantExpr {
1230   extern int &m;
1231   extern int &n;
1232 
1233   constexpr int k = 4;
1234   int &m = const_cast<int&>(k);
1235 
1236   // If we have nothing more interesting to say, ensure we don't produce a
1237   // useless note and instead just point to the non-constant subexpression.
1238   enum class E {
1239     em = m,
1240     en = n, // expected-error {{not a constant expression}}
1241     eo = (m +
1242           n // expected-error {{not a constant expression}}
1243           ),
1244     eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1245   };
1246 }
1247 
1248 namespace IndirectField {
1249   struct S {
1250     struct { // expected-warning {{GNU extension}}
1251       union { // expected-warning {{declared in an anonymous struct}}
1252         struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1253           int a;
1254           int b;
1255         };
1256         int c;
1257       };
1258       int d;
1259     };
1260     union {
1261       int e;
1262       int f;
1263     };
1264     constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {}
1265     constexpr S(int c, int d, int f) : c(c), d(d), f(f) {}
1266   };
1267 
1268   constexpr S s1(1, 2, 3, 4);
1269   constexpr S s2(5, 6, 7);
1270 
1271   // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1272   // member is active and which is requested.
1273   static_assert(s1.a == 1, "");
1274   static_assert(s1.b == 2, "");
1275   static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1276   static_assert(s1.d == 3, "");
1277   static_assert(s1.e == 4, "");
1278   static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1279 
1280   static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1281   static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1282   static_assert(s2.c == 5, "");
1283   static_assert(s2.d == 6, "");
1284   static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1285   static_assert(s2.f == 7, "");
1286 }
1287 
1288 // DR1405: don't allow reading mutable members in constant expressions.
1289 namespace MutableMembers {
1290   struct MM {
1291     mutable int n; // expected-note 3{{declared here}}
1292   } constexpr mm = { 4 };
1293   constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1294   int x = (mm.n = 1, 3);
1295   constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1296 
1297   // Here's one reason why allowing this would be a disaster...
1298   template<int n> struct Id { int k = n; };
1299   int f() {
1300     constexpr MM m = { 0 };
1301     ++m.n;
1302     return Id<m.n>().k; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1303   }
1304 
1305   struct A { int n; };
1306   struct B { mutable A a; }; // expected-note {{here}}
1307   struct C { B b; };
1308   constexpr C c[3] = {};
1309   constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1310 }
1311 
1312 namespace Fold {
1313 
1314   // This macro forces its argument to be constant-folded, even if it's not
1315   // otherwise a constant expression.
1316   #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
1317 
1318   constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1319   constexpr int m = fold((int)(char*)123); // ok
1320   static_assert(m == 123, "");
1321 
1322   #undef fold
1323 
1324 }
1325 
1326 namespace DR1454 {
1327 
1328 constexpr const int &f(const int &n) { return n; }
1329 constexpr int k1 = f(0); // ok
1330 
1331 struct Wrap {
1332   const int &value;
1333 };
1334 constexpr const Wrap &g(const Wrap &w) { return w; }
1335 constexpr int k2 = g({0}).value; // ok
1336 
1337 // The temporary here has static storage duration, so we can bind a constexpr
1338 // reference to it.
1339 constexpr const int &i = 1;
1340 constexpr const int j = i;
1341 static_assert(j == 1, "");
1342 
1343 // The temporary here is not const, so it can't be read outside the expression
1344 // in which it was created (per the C++14 rules, which we use to avoid a C++11
1345 // defect).
1346 constexpr int &&k = 1; // expected-note {{temporary created here}}
1347 constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}}
1348 
1349 void f() {
1350   // The temporary here has automatic storage duration, so we can't bind a
1351   // constexpr reference to it.
1352   constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1353 }
1354 
1355 }
1356 
1357 namespace RecursiveOpaqueExpr {
1358   template<typename Iter>
1359   constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) {
1360     return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}}
1361   }
1362 
1363   constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1364   static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, "");
1365 
1366   constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1367   static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, "");
1368 
1369   constexpr int arr3[] = {
1370     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1371     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1372     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1373     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1374     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1375     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1376     1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1377     2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1378   static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, "");
1379 }
1380 
1381 namespace VLASizeof {
1382 
1383   void f(int k) {
1384     int arr[k]; // expected-warning {{C99}}
1385     constexpr int n = 1 +
1386         sizeof(arr) // expected-error {{constant expression}}
1387         * 3;
1388   }
1389 }
1390 
1391 namespace CompoundLiteral {
1392   // FIXME:
1393   // We don't model the semantics of this correctly: the compound literal is
1394   // represented as a prvalue in the AST, but actually behaves like an lvalue.
1395   // We treat the compound literal as a temporary and refuse to produce a
1396   // pointer to it. This is OK: we're not required to treat this as a constant
1397   // in C++, and in C we model compound literals as lvalues.
1398   constexpr int *p = (int*)(int[1]){0}; // expected-warning {{C99}} expected-error {{constant expression}} expected-note 2{{temporary}}
1399 }
1400 
1401 namespace Vector {
1402   typedef int __attribute__((vector_size(16))) VI4;
1403   constexpr VI4 f(int n) {
1404     return VI4 { n * 3, n + 4, n - 5, n / 6 };
1405   }
1406   constexpr auto v1 = f(10);
1407 
1408   typedef double __attribute__((vector_size(32))) VD4;
1409   constexpr VD4 g(int n) {
1410     return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}}
1411   }
1412   constexpr auto v2 = g(4);
1413 }
1414 
1415 // PR12626, redux
1416 namespace InvalidClasses {
1417   void test0() {
1418     struct X; // expected-note {{forward declaration}}
1419     struct Y { bool b; X x; }; // expected-error {{field has incomplete type}}
1420     Y y;
1421     auto& b = y.b;
1422   }
1423 }
1424 
1425 namespace NamespaceAlias {
1426   constexpr int f() {
1427     namespace NS = NamespaceAlias; // expected-warning {{use of this statement in a constexpr function is a C++1y extension}}
1428     return &NS::f != nullptr;
1429   }
1430 }
1431 
1432 // Constructors can be implicitly constexpr, even for a non-literal type.
1433 namespace ImplicitConstexpr {
1434   struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}}
1435   struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; };
1436   struct S { R r; }; // expected-note 3{{here}}
1437   struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; };
1438   struct U { T t; }; // expected-note 3{{here}}
1439   static_assert(!__is_literal_type(Q), "");
1440   static_assert(!__is_literal_type(R), "");
1441   static_assert(!__is_literal_type(S), "");
1442   static_assert(!__is_literal_type(T), "");
1443   static_assert(!__is_literal_type(U), "");
1444   struct Test {
1445     friend Q::Q() noexcept; // expected-error {{follows constexpr}}
1446     friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}}
1447     friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}}
1448     friend S::S() noexcept; // expected-error {{follows constexpr}}
1449     friend S::S(S&&) noexcept; // expected-error {{follows constexpr}}
1450     friend S::S(const S&) noexcept; // expected-error {{follows constexpr}}
1451     friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}}
1452     friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}}
1453     friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}}
1454   };
1455 }
1456 
1457 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1458 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1459 namespace PR12826 {
1460   struct Foo {};
1461   constexpr Foo id(Foo x) { return x; }
1462   constexpr Foo res(id(Foo()));
1463 }
1464 
1465 namespace PR13273 {
1466   struct U {
1467     int t;
1468     U() = default;
1469   };
1470 
1471   struct S : U {
1472     S() = default;
1473   };
1474 
1475   // S's default constructor isn't constexpr, because U's default constructor
1476   // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1477   // actually call it.
1478   static_assert(S{}.t == 0, "");
1479 }
1480 
1481 namespace PR12670 {
1482   struct S {
1483     constexpr S(int a0) : m(a0) {}
1484     constexpr S() : m(6) {}
1485     int m;
1486   };
1487   constexpr S x[3] = { {4}, 5 };
1488   static_assert(x[0].m == 4, "");
1489   static_assert(x[1].m == 5, "");
1490   static_assert(x[2].m == 6, "");
1491 }
1492 
1493 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1494 // when a conditional operator has one argument of type void and where the other
1495 // is a glvalue of class type.
1496 namespace ConditionalLValToRVal {
1497   struct A {
1498     constexpr A(int a) : v(a) {}
1499     int v;
1500   };
1501 
1502   constexpr A f(const A &a) {
1503     return a.v == 0 ? throw a : a;
1504   }
1505 
1506   constexpr A a(4);
1507   static_assert(f(a).v == 4, "");
1508 }
1509 
1510 namespace TLS {
1511   __thread int n;
1512   int m;
1513 
1514   constexpr bool b = &n == &n;
1515 
1516   constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1517 
1518   constexpr int *f() { return &n; }
1519   constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1520   constexpr bool c = f() == f();
1521 
1522   constexpr int *g() { return &m; }
1523   constexpr int *r = g();
1524 }
1525 
1526 namespace Void {
1527   constexpr void f() { return; } // expected-error{{constexpr function's return type 'void' is not a literal type}}
1528 
1529   void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}}
1530 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1531   template<typename T, size_t S>
1532   constexpr T get(T (&a)[S], size_t k) {
1533     return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}}
1534   }
1535 #undef ASSERT
1536   template int get(int (&a)[4], size_t);
1537   constexpr int arr[] = { 4, 1, 2, 3, 4 };
1538   static_assert(get(arr, 1) == 1, "");
1539   static_assert(get(arr, 4) == 4, "");
1540   static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1541   // expected-note{{in call to 'get(arr, 0)'}}
1542 }
1543 
1544 namespace std { struct type_info; }
1545 
1546 namespace TypeId {
1547   struct A { virtual ~A(); };
1548   A f();
1549   A &g();
1550   constexpr auto &x = typeid(f());
1551   constexpr auto &y = typeid(g()); // expected-error{{constant expression}} \
1552   // expected-note{{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}}
1553 }
1554 
1555 namespace PR14203 {
1556   struct duration {
1557     constexpr duration() {}
1558     constexpr operator int() const { return 0; }
1559   };
1560   template<typename T> void f() {
1561     // If we want to evaluate this at the point of the template definition, we
1562     // need to trigger the implicit definition of the move constructor at that
1563     // point.
1564     // FIXME: C++ does not permit us to implicitly define it at the appropriate
1565     // times, since it is only allowed to be implicitly defined when it is
1566     // odr-used.
1567     constexpr duration d = duration();
1568   }
1569   // FIXME: It's unclear whether this is valid. On the one hand, we're not
1570   // allowed to generate a move constructor. On the other hand, if we did,
1571   // this would be a constant expression. For now, we generate a move
1572   // constructor here.
1573   int n = sizeof(short{duration(duration())});
1574 }
1575 
1576 namespace ArrayEltInit {
1577   struct A {
1578     constexpr A() : p(&p) {}
1579     void *p;
1580   };
1581   constexpr A a[10];
1582   static_assert(a[0].p == &a[0].p, "");
1583   static_assert(a[9].p == &a[9].p, "");
1584   static_assert(a[0].p != &a[9].p, "");
1585   static_assert(a[9].p != &a[0].p, "");
1586 
1587   constexpr A b[10] = {};
1588   static_assert(b[0].p == &b[0].p, "");
1589   static_assert(b[9].p == &b[9].p, "");
1590   static_assert(b[0].p != &b[9].p, "");
1591   static_assert(b[9].p != &b[0].p, "");
1592 }
1593 
1594 namespace PR15884 {
1595   struct S {};
1596   constexpr S f() { return {}; }
1597   constexpr S *p = &f();
1598   // expected-error@-1 {{taking the address of a temporary}}
1599   // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1600   // expected-note@-3 {{pointer to temporary is not a constant expression}}
1601   // expected-note@-4 {{temporary created here}}
1602 }
1603 
1604 namespace AfterError {
1605   // FIXME: Suppress the 'no return statements' diagnostic if the body is invalid.
1606   constexpr int error() { // expected-error {{no return statement}}
1607     return foobar; // expected-error {{undeclared identifier}}
1608   }
1609   constexpr int k = error(); // expected-error {{must be initialized by a constant expression}}
1610 }
1611 
1612 namespace std {
1613   typedef decltype(sizeof(int)) size_t;
1614 
1615   template <class _E>
1616   class initializer_list
1617   {
1618     const _E* __begin_;
1619     size_t    __size_;
1620 
1621     constexpr initializer_list(const _E* __b, size_t __s)
1622       : __begin_(__b),
1623         __size_(__s)
1624     {}
1625 
1626   public:
1627     typedef _E        value_type;
1628     typedef const _E& reference;
1629     typedef const _E& const_reference;
1630     typedef size_t    size_type;
1631 
1632     typedef const _E* iterator;
1633     typedef const _E* const_iterator;
1634 
1635     constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1636 
1637     constexpr size_t    size()  const {return __size_;}
1638     constexpr const _E* begin() const {return __begin_;}
1639     constexpr const _E* end()   const {return __begin_ + __size_;}
1640   };
1641 }
1642 
1643 namespace InitializerList {
1644   constexpr int sum(const int *b, const int *e) {
1645     return b != e ? *b + sum(b+1, e) : 0;
1646   }
1647   constexpr int sum(std::initializer_list<int> ints) {
1648     return sum(ints.begin(), ints.end());
1649   }
1650   static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1651 }
1652 
1653 namespace StmtExpr {
1654   struct A { int k; };
1655   void f() {
1656     static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}}
1657     constexpr auto a = ({ A(); }); // expected-warning {{extension}}
1658   }
1659   constexpr int g(int k) {
1660     return ({ // expected-warning {{extension}}
1661       const int x = k;
1662       x * x;
1663     });
1664   }
1665   static_assert(g(123) == 15129, "");
1666   constexpr int h() { // expected-error {{never produces a constant}}
1667     return ({ // expected-warning {{extension}}
1668       return 0; // expected-note {{not supported}}
1669       1;
1670     });
1671   }
1672 }
1673 
1674 namespace VirtualFromBase {
1675   struct S1 {
1676     virtual int f() const;
1677   };
1678   struct S2 {
1679     virtual int f();
1680   };
1681   template <typename T> struct X : T {
1682     constexpr X() {}
1683     double d = 0.0;
1684     constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++1y}}
1685   };
1686 
1687   // Virtual f(), not OK.
1688   constexpr X<X<S1>> xxs1;
1689   constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
1690   static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}}
1691 
1692   // Non-virtual f(), OK.
1693   constexpr X<X<S2>> xxs2;
1694   constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
1695   static_assert(q->f() == sizeof(S2), "");
1696 }
1697 
1698 namespace ConstexprConstructorRecovery {
1699   class X {
1700   public:
1701       enum E : short {
1702           headers = 0x1,
1703           middlefile = 0x2,
1704           choices = 0x4
1705       };
1706       constexpr X() noexcept {};
1707   protected:
1708       E val{0}; // expected-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}}
1709   };
1710   constexpr X x{};
1711 }
1712 
1713 namespace Lifetime {
1714   void f() {
1715     constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1716     constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1717   }
1718 
1719   constexpr int &get(int &&n) { return n; }
1720   struct S {
1721     int &&r; // expected-note 2{{declared here}}
1722     int &s;
1723     int t;
1724     constexpr S() : r(0), s(get(0)), t(r) {} // expected-warning {{temporary}}
1725     constexpr S(int) : r(0), s(get(0)), t(s) {} // expected-warning {{temporary}} expected-note {{read of object outside its lifetime}}
1726   };
1727   constexpr int k1 = S().t; // ok, int is lifetime-extended to end of constructor
1728   constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}}
1729 }
1730 
1731 namespace Bitfields {
1732   struct A {
1733     bool b : 1;
1734     unsigned u : 5;
1735     int n : 5;
1736     bool b2 : 3;
1737     unsigned u2 : 74; // expected-warning {{exceeds the size of its type}}
1738     int n2 : 81; // expected-warning {{exceeds the size of its type}}
1739   };
1740 
1741   constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
1742   static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 &&
1743                 a.u2 + 1 == 0 && a.n2 == 0x7fffffff,
1744                 "bad truncation of bitfield values");
1745 
1746   struct B {
1747     int n : 3;
1748     constexpr B(int k) : n(k) {}
1749   };
1750   static_assert(B(3).n == 3, "");
1751   static_assert(B(4).n == -4, "");
1752   static_assert(B(7).n == -1, "");
1753   static_assert(B(8).n == 0, "");
1754   static_assert(B(-1).n == -1, "");
1755   static_assert(B(-8889).n == -1, "");
1756 
1757   namespace PR16755 {
1758     struct X {
1759       int x : 1;
1760       constexpr static int f(int x) {
1761         return X{x}.x;
1762       }
1763     };
1764     static_assert(X::f(3) == -1, "3 should truncate to -1");
1765   }
1766 }
1767 
1768 namespace ZeroSizeTypes {
1769   constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
1770   constexpr int k = p2 - p1;
1771   // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
1772   // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}}
1773 
1774   int arr[5][0];
1775   constexpr int f() { // expected-error {{never produces a constant expression}}
1776     return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}}
1777   }
1778 }
1779 
1780 namespace BadDefaultInit {
1781   template<int N> struct X { static const int n = N; };
1782 
1783   struct A { // expected-note {{subexpression}}
1784     int k = X<A().k>::n; // expected-error {{defaulted default constructor of 'A' cannot be used}} expected-error {{not a constant expression}} expected-note {{in call to 'A()'}}
1785   };
1786 
1787   // FIXME: The "constexpr constructor must initialize all members" diagnostic
1788   // here is bogus (we discard the k(k) initializer because the parameter 'k'
1789   // has been marked invalid).
1790   struct B { // expected-note 2{{candidate}}
1791     constexpr B( // expected-error {{must initialize all members}} expected-note {{candidate}}
1792         int k = X<B().k>::n) : // expected-error {{no matching constructor}}
1793       k(k) {}
1794     int k; // expected-note {{not initialized}}
1795   };
1796 }
1797 
1798 namespace NeverConstantTwoWays {
1799   // If we see something non-constant but foldable followed by something
1800   // non-constant and not foldable, we want the first diagnostic, not the
1801   // second.
1802   constexpr int f(int n) { // expected-error {{never produces a constant expression}}
1803     return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
1804         1 / 0 : // expected-warning {{division by zero}}
1805         0;
1806   }
1807 
1808   // FIXME: We should diagnose the cast to long here, not the division by zero.
1809   constexpr int n = // expected-error {{must be initialized by a constant expression}}
1810       (int *)(long)&n == &n ?
1811         1 / 0 : // expected-warning {{division by zero}} expected-note {{division by zero}}
1812         0;
1813 }
1814 
1815 namespace PR17800 {
1816   struct A {
1817     constexpr int operator()() const { return 0; }
1818   };
1819   template <typename ...T> constexpr int sink(T ...) {
1820     return 0;
1821   }
1822   template <int ...N> constexpr int run() {
1823     return sink(A()() + N ...);
1824   }
1825   constexpr int k = run<1, 2, 3>();
1826 }
1827 
1828 namespace BuiltinStrlen {
1829   constexpr const char *a = "foo\0quux";
1830   constexpr char b[] = "foo\0quux";
1831   constexpr int f() { return 'u'; }
1832   constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
1833 
1834   static_assert(__builtin_strlen("foo") == 3, "");
1835   static_assert(__builtin_strlen("foo\0quux") == 3, "");
1836   static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
1837 
1838   constexpr bool check(const char *p) {
1839     return __builtin_strlen(p) == 3 &&
1840            __builtin_strlen(p + 1) == 2 &&
1841            __builtin_strlen(p + 2) == 1 &&
1842            __builtin_strlen(p + 3) == 0 &&
1843            __builtin_strlen(p + 4) == 4 &&
1844            __builtin_strlen(p + 5) == 3 &&
1845            __builtin_strlen(p + 6) == 2 &&
1846            __builtin_strlen(p + 7) == 1 &&
1847            __builtin_strlen(p + 8) == 0;
1848   }
1849 
1850   static_assert(check(a), "");
1851   static_assert(check(b), "");
1852   static_assert(check(c), "");
1853 
1854   constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1855   constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1856   constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1857 
1858   constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1859   constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1860   constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1861 
1862   // FIXME: The diagnostic here could be better.
1863   constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
1864   constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1865 }
1866