1 // RUN: %clang_cc1 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -fsyntax-only -fcxx-exceptions -verify -std=c++11 -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
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 
id(const T & t)13 template<typename T> constexpr T id(const T &t) { return t; }
min(const T & a,const T & b)14 template<typename T> constexpr T min(const T &a, const T &b) {
15   return a < b ? a : b;
16 }
max(const T & a,const T & b)17 template<typename T> constexpr T max(const T &a, const T &b) {
18   return a < b ? b : a;
19 }
begin(T (& xs)[N])20 template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
end(T (& xs)[N])21 template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
22 
23 struct MemberZero {
zeroMemberZero24   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   int x;
f(int n)99   void f(int n) {
100     switch (n) {
101     case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
102     case id(0): // expected-error {{duplicate case value '0'}}
103       return;
104     case __builtin_constant_p(true) ? (__SIZE_TYPE__)&x : 0:; // expected-error {{constant}}
105     }
106   }
107 }
108 
109 extern int &Recurse1;
110 int &Recurse2 = Recurse1; // expected-note {{declared here}}
111 int &Recurse1 = Recurse2;
112 constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
113 
114 extern const int RecurseA;
115 const int RecurseB = RecurseA; // expected-note {{declared here}}
116 const int RecurseA = 10;
117 constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
118 
119 namespace MemberEnum {
120   struct WithMemberEnum {
121     enum E { A = 42 };
122   } wme;
123 
124   static_assert(wme.A == 42, "");
125 }
126 
127 namespace DefaultArguments {
128 
129 const int z = int();
Sum(int a=0,const int & b=0,const int * c=& z,char d=0)130 constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
131   return a + b + *c + d;
132 }
133 const int four = 4;
134 constexpr int eight = 8;
135 constexpr const int twentyseven = 27;
136 static_assert(Sum() == 0, "");
137 static_assert(Sum(1) == 1, "");
138 static_assert(Sum(1, four) == 5, "");
139 static_assert(Sum(1, eight, &twentyseven) == 36, "");
140 static_assert(Sum(1, 2, &four, eight) == 15, "");
141 
142 }
143 
144 namespace Ellipsis {
145 
146 // Note, values passed through an ellipsis can't actually be used.
F(int a,...)147 constexpr int F(int a, ...) { return a; }
148 static_assert(F(0) == 0, "");
149 static_assert(F(1, 0) == 1, "");
150 static_assert(F(2, "test") == 2, "");
151 static_assert(F(3, &F) == 3, "");
152 int k = 0; // expected-note {{here}}
153 static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
154 
155 }
156 
157 namespace Recursion {
fib(int n)158   constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
159   static_assert(fib(11) == 89, "");
160 
gcd_inner(int a,int b)161   constexpr int gcd_inner(int a, int b) {
162     return b == 0 ? a : gcd_inner(b, a % b);
163   }
gcd(int a,int b)164   constexpr int gcd(int a, int b) {
165     return gcd_inner(max(a, b), min(a, b));
166   }
167 
168   static_assert(gcd(1749237, 5628959) == 7, "");
169 }
170 
171 namespace FunctionCast {
172   // When folding, we allow functions to be cast to different types. Such
173   // cast functions cannot be called, even if they're constexpr.
f()174   constexpr int f() { return 1; }
175   typedef double (*DoubleFn)();
176   typedef int (*IntFn)();
177   int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
178   int b[(int)IntFn(f)()];    // ok
179 }
180 
181 namespace StaticMemberFunction {
182   struct S {
183     static constexpr int k = 42;
fStaticMemberFunction::S184     static constexpr int f(int n) { return n * k + 2; }
185   } s;
186 
187   constexpr int n = s.f(19);
188   static_assert(S::f(19) == 800, "");
189   static_assert(s.f(19) == 800, "");
190   static_assert(n == 800, "");
191 
192   constexpr int (*sf1)(int) = &S::f;
193   constexpr int (*sf2)(int) = &s.f;
194   constexpr const int *sk = &s.k;
195 
196   // Note, out_of_lifetime returns an invalid pointer value, but we don't do
197   // anything with it (other than copy it around), so there's no UB there.
out_of_lifetime(S s)198   constexpr S *out_of_lifetime(S s) { return &s; } // expected-warning {{address of stack}}
199   static_assert(out_of_lifetime({})->k == 42, "");
200   static_assert(out_of_lifetime({})->f(3) == 128, "");
201 
202   // Similarly, using an inactive union member breaks no rules.
203   union U {
204     int n;
205     S s;
206   };
207   constexpr U u = {0};
208   static_assert(u.s.k == 42, "");
209   static_assert(u.s.f(1) == 44, "");
210 
211   // And likewise for a past-the-end pointer.
212   static_assert((&s)[1].k == 42, "");
213   static_assert((&s)[1].f(1) == 44, "");
214 }
215 
216 namespace ParameterScopes {
217 
218   const int k = 42;
ObscureTheTruth(const int & a)219   constexpr const int &ObscureTheTruth(const int &a) { return a; }
MaybeReturnJunk(bool b,const int a)220   constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
221     return ObscureTheTruth(b ? a : k);
222   }
223   static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
224   constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
225 
MaybeReturnNonstaticRef(bool b,const int a)226   constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
227     return ObscureTheTruth(b ? a : k);
228   }
229   static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
230   constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok
231 
InternalReturnJunk(int n)232   constexpr int InternalReturnJunk(int n) {
233     return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}}
234   }
235   constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
236 
LToR(int & n)237   constexpr int LToR(int &n) { return n; }
GrabCallersArgument(bool which,int a,int b)238   constexpr int GrabCallersArgument(bool which, int a, int b) {
239     return LToR(which ? b : a);
240   }
241   static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
242   static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
243 
244 }
245 
246 namespace Pointers {
247 
f(int n,const int * a,const int * b,const int * c)248   constexpr int f(int n, const int *a, const int *b, const int *c) {
249     return n == 0 ? 0 : *a + f(n-1, b, c, a);
250   }
251 
252   const int x = 1, y = 10, z = 100;
253   static_assert(f(23, &x, &y, &z) == 788, "");
254 
g(int n,int a,int b,int c)255   constexpr int g(int n, int a, int b, int c) {
256     return f(n, &a, &b, &c);
257   }
258   static_assert(g(23, x, y, z) == 788, "");
259 
260 }
261 
262 namespace FunctionPointers {
263 
Double(int n)264   constexpr int Double(int n) { return 2 * n; }
Triple(int n)265   constexpr int Triple(int n) { return 3 * n; }
Twice(int (* F)(int),int n)266   constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
Quadruple(int n)267   constexpr int Quadruple(int n) { return Twice(Double, n); }
Select(int n)268   constexpr auto Select(int n) -> int (*)(int) {
269     return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
270   }
Apply(int (* F)(int),int n)271   constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
272 
273   static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
274 
275   constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(nullptr, 0)'}}
276 
277 }
278 
279 namespace PointerComparison {
280 
281 int x, y;
282 static_assert(&x == &y, "false"); // expected-error {{false}}
283 static_assert(&x != &y, "");
284 constexpr bool g1 = &x == &y;
285 constexpr bool g2 = &x != &y;
286 constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
287 constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
288 constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
289 constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
290 
291 struct S { int x, y; } s;
292 static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
293 static_assert(&s.x != &s.y, "");
294 static_assert(&s.x <= &s.y, "");
295 static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
296 static_assert(&s.x < &s.y, "");
297 static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
298 
299 static_assert(0 == &y, "false"); // expected-error {{false}}
300 static_assert(0 != &y, "");
301 constexpr bool n3 = (int*)0 <= &y; // expected-error {{must be initialized by a constant expression}}
302 constexpr bool n4 = (int*)0 >= &y; // expected-error {{must be initialized by a constant expression}}
303 constexpr bool n5 = (int*)0 < &y; // expected-error {{must be initialized by a constant expression}}
304 constexpr bool n6 = (int*)0 > &y; // expected-error {{must be initialized by a constant expression}}
305 
306 static_assert(&x == 0, "false"); // expected-error {{false}}
307 static_assert(&x != 0, "");
308 constexpr bool n9 = &x <= (int*)0; // expected-error {{must be initialized by a constant expression}}
309 constexpr bool n10 = &x >= (int*)0; // expected-error {{must be initialized by a constant expression}}
310 constexpr bool n11 = &x < (int*)0; // expected-error {{must be initialized by a constant expression}}
311 constexpr bool n12 = &x > (int*)0; // expected-error {{must be initialized by a constant expression}}
312 
313 static_assert(&x == &x, "");
314 static_assert(&x != &x, "false"); // expected-error {{false}}
315 static_assert(&x <= &x, "");
316 static_assert(&x >= &x, "");
317 static_assert(&x < &x, "false"); // expected-error {{false}}
318 static_assert(&x > &x, "false"); // expected-error {{false}}
319 
320 constexpr S* sptr = &s;
321 constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
322 
323 struct U {};
324 struct Str {
325   int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
326     expected-warning {{not an integral constant expression}} \
327     expected-note {{dynamic_cast is not allowed in a constant expression}}
328   int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
329     expected-warning {{not an integral constant expression}} \
330     expected-note {{reinterpret_cast is not allowed in a constant expression}}
331   int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
332     expected-warning {{not an integral constant expression}} \
333     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
334   int d : (S*)(42) == (S*)(42); // \
335     expected-warning {{not an integral constant expression}} \
336     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
337   int e : (Str*)(sptr) == (Str*)(sptr); // \
338     expected-warning {{not an integral constant expression}} \
339     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
340   int f : &(U&)(*sptr) == &(U&)(*sptr); // \
341     expected-warning {{not an integral constant expression}} \
342     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
343   int g : (S*)(void*)(sptr) == sptr; // \
344     expected-warning {{not an integral constant expression}} \
345     expected-note {{cast from 'void *' is not allowed in a constant expression}}
346 };
347 
348 extern char externalvar[];
349 constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}}
350 constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}}
351 static_assert(0 != "foo", "");
352 
353 }
354 
355 namespace MaterializeTemporary {
356 
f(const int & r)357 constexpr int f(const int &r) { return r; }
358 constexpr int n = f(1);
359 
same(const int & a,const int & b)360 constexpr bool same(const int &a, const int &b) { return &a == &b; }
sameTemporary(const int & n)361 constexpr bool sameTemporary(const int &n) { return same(n, n); }
362 
363 static_assert(n, "");
364 static_assert(!same(4, 4), "");
365 static_assert(same(n, n), "");
366 static_assert(sameTemporary(9), "");
367 
368 struct A { int &&r; };
369 struct B { A &&a1; A &&a2; };
370 
371 constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}}
372 static_assert(&b1.a1 != &b1.a2, "");
373 static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
374 
375 constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}}
376 static_assert(&b1 != &b2, "");
377 static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
378 
379 constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
foo()380 void foo() {
381   constexpr static B b1 { { 1 }, { 2 } }; // ok
382   constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
383   constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
384 }
385 
386 constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} });
387 static_assert(&b4 != &b2, "");
388 
389 // Proposed DR: copy-elision doesn't trigger lifetime extension.
390 constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
391 
392 namespace NestedNonStatic {
393   // Proposed DR: for a reference constant expression to refer to a static
394   // storage duration temporary, that temporary must itself be initialized
395   // by a constant expression (a core constant expression is not enough).
396   struct A { int &&r; };
397   struct B { A &&a; };
398   constexpr B a = { A{0} }; // ok
399   constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
400 }
401 
402 namespace FakeInitList {
403   struct init_list_3_ints { const int (&x)[3]; };
404   struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; };
405   constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
406 }
407 
408 namespace ConstAddedByReference {
409   const int &r = (0);
410   constexpr int n = r;
411 
operator intMaterializeTemporary::ConstAddedByReference::A412   struct A { constexpr operator int() const { return 0; }};
operator const intMaterializeTemporary::ConstAddedByReference::B413   struct B { constexpr operator const int() const { return 0; }};
414   const int &ra = A();
415   const int &rb = B();
416   constexpr int na = ra;
417   constexpr int nb = rb;
418 }
419 
420 }
421 
strcmp_ce(const char * p,const char * q)422 constexpr int strcmp_ce(const char *p, const char *q) {
423   return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
424 }
425 
426 namespace StringLiteral {
427 
428 template<typename Char>
MangleChars(const Char * p)429 constexpr int MangleChars(const Char *p) {
430   return *p + 3 * (*p ? MangleChars(p+1) : 0);
431 }
432 
433 static_assert(MangleChars("constexpr!") == 1768383, "");
434 static_assert(MangleChars(u8"constexpr!") == 1768383, "");
435 static_assert(MangleChars(L"constexpr!") == 1768383, "");
436 static_assert(MangleChars(u"constexpr!") == 1768383, "");
437 static_assert(MangleChars(U"constexpr!") == 1768383, "");
438 
439 constexpr char c0 = "nought index"[0];
440 constexpr char c1 = "nice index"[10];
441 constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
442 constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-note {{cannot refer to element -1 of array of 15 elements}}
443 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}}
444 
445 constexpr const char *p = "test" + 2;
446 static_assert(*p == 's', "");
447 
max_iter(const char * a,const char * b)448 constexpr const char *max_iter(const char *a, const char *b) {
449   return *a < *b ? b : a;
450 }
max_element(const char * a,const char * b)451 constexpr const char *max_element(const char *a, const char *b) {
452   return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
453 }
454 
455 constexpr char str[] = "the quick brown fox jumped over the lazy dog";
456 constexpr const char *max = max_element(begin(str), end(str));
457 static_assert(*max == 'z', "");
458 static_assert(max == str + 38, "");
459 
460 static_assert(strcmp_ce("hello world", "hello world") == 0, "");
461 static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
462 static_assert(strcmp_ce("constexpr", "test") < 0, "");
463 static_assert(strcmp_ce("", " ") < 0, "");
464 
465 struct S {
466   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}}
467 };
468 
469 struct T {
470   char c[6];
TStringLiteral::T471   constexpr T() : c{"foo"} {}
472 };
473 constexpr T t;
474 
475 static_assert(t.c[0] == 'f', "");
476 static_assert(t.c[1] == 'o', "");
477 static_assert(t.c[2] == 'o', "");
478 static_assert(t.c[3] == 0, "");
479 static_assert(t.c[4] == 0, "");
480 static_assert(t.c[5] == 0, "");
481 static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
482 
483 struct U {
484   wchar_t chars[6];
485   int n;
486 } constexpr u = { { L"test" }, 0 };
487 static_assert(u.chars[2] == L's', "");
488 
489 struct V {
490   char c[4];
VStringLiteral::V491   constexpr V() : c("hi!") {}
492 };
493 static_assert(V().c[1] == "i"[0], "");
494 
495 namespace Parens {
496   constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")},
497                           d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")};
498   static_assert(a[0] == 'f', "");
499   static_assert(b[1] == 'o', "");
500   static_assert(c[2] == 'o', "");
501   static_assert(d[0] == 'f', "");
502   static_assert(e[1] == 'o', "");
503   static_assert(f[2] == 'o', "");
504   static_assert(f[5] == 0, "");
505   static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
506 }
507 
508 }
509 
510 namespace Array {
511 
512 template<typename Iter>
Sum(Iter begin,Iter end)513 constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
514   return begin == end ? 0 : *begin + Sum(begin+1, end);
515 }
516 
517 constexpr int xs[] = { 1, 2, 3, 4, 5 };
518 constexpr int ys[] = { 5, 4, 3, 2, 1 };
519 constexpr int sum_xs = Sum(begin(xs), end(xs));
520 static_assert(sum_xs == 15, "");
521 
ZipFoldR(int (* F)(int x,int y,int c),int n,const int * xs,const int * ys,int c)522 constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
523                        const int *xs, const int *ys, int c) {
524   return n ? F(
525                *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
526                *ys,
527                ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
528       expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
529       expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
530            : c;
531 }
MulAdd(int x,int y,int c)532 constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
533 constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
534 static_assert(InnerProduct == 35, "");
535 
SubMul(int x,int y,int c)536 constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
537 constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
538 static_assert(DiffProd == 8, "");
539 static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
540       expected-error {{constant expression}} \
541       expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
542 
543 constexpr const int *p = xs + 3;
544 constexpr int xs4 = p[1]; // ok
545 constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
546 constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
547 constexpr int xs0 = p[-3]; // ok
548 constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
549 
550 constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
551 static_assert(zs[0][0][0][0] == 1, "");
552 static_assert(zs[1][1][1][1] == 16, "");
553 static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
554 static_assert((&zs[0][0][0][2])[-1] == 2, "");
555 static_assert(**(**(zs + 1) + 1) == 11, "");
556 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}}
557 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
558 constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // \
559 expected-error {{constant expression}} \
560 expected-note {{cannot access array element of pointer past the end}}
561 
fail(const int & p)562 constexpr int fail(const int &p) {
563   return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
564 }
565 static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
566 expected-error {{static_assert expression is not an integral constant expression}} \
567 expected-note {{in call to 'fail(zs[1][0][1][0])'}}
568 
569 constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
SumNonzero(const int * p)570 constexpr int SumNonzero(const int *p) {
571   return *p + (*p ? SumNonzero(p+1) : 0);
572 }
CountZero(const int * p,const int * q)573 constexpr int CountZero(const int *p, const int *q) {
574   return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
575 }
576 static_assert(SumNonzero(arr) == 6, "");
577 static_assert(CountZero(arr, arr + 40) == 36, "");
578 
579 struct ArrayElem {
ArrayElemArray::ArrayElem580   constexpr ArrayElem() : n(0) {}
581   int n;
fArray::ArrayElem582   constexpr int f() const { return n; }
583 };
584 struct ArrayRVal {
ArrayRValArray::ArrayRVal585   constexpr ArrayRVal() {}
586   ArrayElem elems[10];
587 };
588 static_assert(ArrayRVal().elems[3].f() == 0, "");
589 
590 namespace CopyCtor {
591   struct A {
AArray::CopyCtor::A592     constexpr A() {}
AArray::CopyCtor::A593     constexpr A(const A &) {}
594   };
595   struct B {
596     A a;
597     int arr[10];
598   };
599   constexpr B b{{}, {1, 2, 3, 4, 5}};
600   constexpr B c = b;
601   static_assert(c.arr[2] == 3, "");
602   static_assert(c.arr[7] == 0, "");
603 }
604 
605 constexpr int selfref[2][2][2] = {
606   selfref[1][1][1] + 1, selfref[0][0][0] + 1,
607   selfref[1][0][1] + 1, selfref[0][1][0] + 1,
608   selfref[1][0][0] + 1, selfref[0][1][1] + 1 };
609 static_assert(selfref[0][0][0] == 1, "");
610 static_assert(selfref[0][0][1] == 2, "");
611 static_assert(selfref[0][1][0] == 1, "");
612 static_assert(selfref[0][1][1] == 2, "");
613 static_assert(selfref[1][0][0] == 1, "");
614 static_assert(selfref[1][0][1] == 3, "");
615 static_assert(selfref[1][1][0] == 0, "");
616 static_assert(selfref[1][1][1] == 0, "");
617 
618 struct TrivialDefCtor { int n; };
619 typedef TrivialDefCtor TDCArray[2][2];
620 static_assert(TDCArray{}[1][1].n == 0, "");
621 
622 struct NonAggregateTDC : TrivialDefCtor {};
623 typedef NonAggregateTDC NATDCArray[2][2];
624 static_assert(NATDCArray{}[1][1].n == 0, "");
625 
626 }
627 
628 // Per current CWG direction, we reject any cases where pointer arithmetic is
629 // not statically known to be valid.
630 namespace ArrayOfUnknownBound {
631   extern int arr[];
632   constexpr int *a = arr;
633   constexpr int *b = &arr[0];
634   static_assert(a == b, "");
635   constexpr int *c = &arr[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
636   constexpr int *d = &a[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
637   constexpr int *e = a + 1; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
638 
639   struct X {
640     int a;
641     int b[]; // expected-warning {{C99}}
642   };
643   extern X x;
644   constexpr int *xb = x.b; // expected-error {{constant}} expected-note {{not supported}}
645 
646   struct Y { int a; };
647   extern Y yarr[];
648   constexpr Y *p = yarr;
649   constexpr int *q = &p->a;
650 
651   extern const int carr[]; // expected-note {{here}}
652   constexpr int n = carr[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}}
653 
654   constexpr int local_extern[] = {1, 2, 3};
f()655   void f() { extern const int local_extern[]; }
656   static_assert(local_extern[1] == 2, "");
657 }
658 
659 namespace DependentValues {
660 
661 struct I { int n; typedef I V[10]; };
662 I::V x, y;
663 int g(); // expected-note {{declared here}}
664 template<bool B, typename T> struct S : T {
665   int k;
fDependentValues::S666   void f() {
667     I::V &cells = B ? x : y;
668     I &i = cells[k];
669     switch (i.n) {}
670 
671     constexpr int n = g(); // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr function 'g'}}
672 
673     constexpr int m = this->g(); // ok, could be constexpr
674   }
675 };
676 
677 extern const int n;
f()678 template<typename T> void f() {
679   // This is ill-formed, because a hypothetical instantiation at the point of
680   // template definition would be ill-formed due to a construct that does not
681   // depend on a template parameter.
682   constexpr int k = n; // expected-error {{must be initialized by a constant expression}}
683 }
684 // It doesn't matter that the instantiation could later become valid:
685 constexpr int n = 4;
686 template void f<int>();
687 
688 }
689 
690 namespace Class {
691 
AClass::A692 struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
fn(const A & a)693 constexpr int fn(const A &a) { return a.k; }
694 static_assert(fn(A(4,5)) == 9, "");
695 
696 struct B { int n; int m; } constexpr b = { 0, b.n };
697 struct C {
CClass::C698   constexpr C(C *this_) : m(42), n(this_->m) {} // ok
699   int m, n;
700 };
701 struct D {
702   C c;
DClass::D703   constexpr D() : c(&c) {}
704 };
705 static_assert(D().c.n == 42, "");
706 
707 struct E {
EClass::E708   constexpr E() : p(&p) {}
709   void *p;
710 };
711 constexpr const E &e1 = E();
712 // This is a constant expression if we elide the copy constructor call, and
713 // is not a constant expression if we don't! But we do, so it is.
714 constexpr E e2 = E();
715 static_assert(e2.p == &e2.p, "");
716 constexpr E e3;
717 static_assert(e3.p == &e3.p, "");
718 
719 extern const class F f;
720 struct F {
FClass::F721   constexpr F() : p(&f.p) {}
722   const void *p;
723 };
724 constexpr F f;
725 
726 struct G {
727   struct T {
TClass::G::T728     constexpr T(T *p) : u1(), u2(p) {}
729     union U1 {
U1()730       constexpr U1() {}
731       int a, b = 42;
732     } u1;
733     union U2 {
U2(T * p)734       constexpr U2(T *p) : c(p->u1.b) {}
735       int c, d;
736     } u2;
737   } t;
GClass::G738   constexpr G() : t(&t) {}
739 } constexpr g;
740 
741 static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
742 static_assert(g.t.u1.b == 42, "");
743 static_assert(g.t.u2.c == 42, "");
744 static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
745 
746 struct S {
747   int a, b;
748   const S *p;
749   double d;
750   const char *q;
751 
SClass::S752   constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
753 };
754 
755 S global(43, &global);
756 
757 static_assert(S(15, &global).b == 15, "");
758 
CheckS(const S & s)759 constexpr bool CheckS(const S &s) {
760   return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
761 }
762 static_assert(CheckS(S(27, &global)), "");
763 
764 struct Arr {
765   char arr[3];
ArrClass::Arr766   constexpr Arr() : arr{'x', 'y', 'z'} {}
767 };
hash(Arr && a)768 constexpr int hash(Arr &&a) {
769   return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
770 }
771 constexpr int k = hash(Arr());
772 static_assert(k == 0x007a7978, "");
773 
774 
775 struct AggregateInit {
776   const char &c;
777   int n;
778   double d;
779   int arr[5];
780   void *p;
781 };
782 
783 constexpr AggregateInit agg1 = { "hello"[0] };
784 
785 static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
786 static_assert(agg1.n == 0, "");
787 static_assert(agg1.d == 0.0, "");
788 static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
789 static_assert(agg1.arr[0] == 0, "");
790 static_assert(agg1.arr[4] == 0, "");
791 static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
792 static_assert(agg1.p == nullptr, "");
793 
794 static constexpr const unsigned char uc[] = { "foo" };
795 static_assert(uc[0] == 'f', "");
796 static_assert(uc[3] == 0, "");
797 
798 namespace SimpleDerivedClass {
799 
800 struct B {
BClass::SimpleDerivedClass::B801   constexpr B(int n) : a(n) {}
802   int a;
803 };
804 struct D : B {
DClass::SimpleDerivedClass::D805   constexpr D(int n) : B(n) {}
806 };
807 constexpr D d(3);
808 static_assert(d.a == 3, "");
809 
810 }
811 
BottomClass::Bottom812 struct Bottom { constexpr Bottom() {} };
813 struct Base : Bottom {
BaseClass::Base814   constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
815   int a;
816   const char *b;
817 };
818 struct Base2 : Bottom {
Base2Class::Base2819   constexpr Base2(const int &r) : r(r) {}
820   int q = 123;
821   const int &r;
822 };
823 struct Derived : Base, Base2 {
DerivedClass::Derived824   constexpr Derived() : Base(76), Base2(a) {}
825   int c = r + b[1];
826 };
827 
operator ==(const Base & a,const Base & b)828 constexpr bool operator==(const Base &a, const Base &b) {
829   return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
830 }
831 
832 constexpr Base base;
833 constexpr Base base2(76);
834 constexpr Derived derived;
835 static_assert(derived.a == 76, "");
836 static_assert(derived.b[2] == 's', "");
837 static_assert(derived.c == 76 + 'e', "");
838 static_assert(derived.q == 123, "");
839 static_assert(derived.r == 76, "");
840 static_assert(&derived.r == &derived.a, "");
841 
842 static_assert(!(derived == base), "");
843 static_assert(derived == base2, "");
844 
845 constexpr Bottom &bot1 = (Base&)derived;
846 constexpr Bottom &bot2 = (Base2&)derived;
847 static_assert(&bot1 != &bot2, "");
848 
849 constexpr Bottom *pb1 = (Base*)&derived;
850 constexpr Bottom *pb2 = (Base2*)&derived;
851 static_assert(&pb1 != &pb2, "");
852 static_assert(pb1 == &bot1, "");
853 static_assert(pb2 == &bot2, "");
854 
855 constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
856 constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
857 constexpr Base2 &ok2 = (Base2&)bot2;
858 static_assert(&ok2 == &derived, "");
859 
860 constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
861 constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
862 constexpr Base2 *pok2 = (Base2*)pb2;
863 static_assert(pok2 == &derived, "");
864 static_assert(&ok2 == pok2, "");
865 static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
866 static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
867 
868 // Core issue 903: we do not perform constant evaluation when checking for a
869 // null pointer in C++11. Just check for an integer literal with value 0.
870 constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Class::Base *const' with an rvalue of type 'int'}}
871 constexpr Base *nullB1 = 0;
872 static_assert((Bottom*)nullB == 0, "");
873 static_assert((Derived*)nullB == 0, "");
874 static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
875 Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'char'}}
876 Base *nullB3 = (0);
877 Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'bool'}}
878 Base *nullB5 = ((0ULL));
879 Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'double'}}
880 enum Null { kNull };
881 Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'Class::Null'}}
882 static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
883 
884 
885 
886 namespace ConversionOperators {
887 
888 struct T {
TClass::ConversionOperators::T889   constexpr T(int n) : k(5*n - 3) {}
operator intClass::ConversionOperators::T890   constexpr operator int() const { return k; }
891   int k;
892 };
893 
894 struct S {
SClass::ConversionOperators::S895   constexpr S(int n) : k(2*n + 1) {}
operator intClass::ConversionOperators::S896   constexpr operator int() const { return k; }
operator TClass::ConversionOperators::S897   constexpr operator T() const { return T(k); }
898   int k;
899 };
900 
check(T a,T b)901 constexpr bool check(T a, T b) { return a == b.k; }
902 
903 static_assert(S(5) == 11, "");
904 static_assert(check(S(5), 11), "");
905 
906 namespace PR14171 {
907 
908 struct X {
909   constexpr (operator int)() const { return 0; }
910 };
911 static_assert(X() == 0, "");
912 
913 }
914 
915 }
916 
917 struct This {
fClass::This918   constexpr int f() const { return 0; }
gClass::This919   static constexpr int g() { return 0; }
hClass::This920   void h() {
921     constexpr int x = f(); // expected-error {{must be initialized by a constant}}
922     // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
923     constexpr int y = this->f(); // expected-error {{must be initialized by a constant}}
924     // expected-note-re@-1 {{{{^}}use of 'this' pointer}}
925     constexpr int z = g();
926     static_assert(z == 0, "");
927   }
928 };
929 
930 }
931 
932 namespace Temporaries {
933 
934 struct S {
STemporaries::S935   constexpr S() {}
936   constexpr int f() const;
937   constexpr int g() const;
938 };
939 struct T : S {
TTemporaries::T940   constexpr T(int n) : S(), n(n) {}
941   int n;
942 };
f() const943 constexpr int S::f() const {
944   return static_cast<const T*>(this)->n; // expected-note {{cannot cast}}
945 }
g() const946 constexpr int S::g() const {
947   // FIXME: Better diagnostic for this.
948   return this->*(int(S::*))&T::n; // expected-note {{subexpression}}
949 }
950 // The T temporary is implicitly cast to an S subobject, but we can recover the
951 // T full-object via a base-to-derived cast, or a derived-to-base-casted member
952 // pointer.
953 static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->f()'}}
954 static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->g()'}}
955 static_assert(T(3).f() == 3, "");
956 static_assert(T(4).g() == 4, "");
957 
f(const S & s)958 constexpr int f(const S &s) {
959   return static_cast<const T&>(s).n;
960 }
961 constexpr int n = f(T(5));
962 static_assert(f(T(5)) == 5, "");
963 
b(int n)964 constexpr bool b(int n) { return &n; }
965 static_assert(b(0), "");
966 
967 struct NonLiteral {
968   NonLiteral();
969   int f();
970 };
971 constexpr int k = NonLiteral().f(); // expected-error {{constant expression}} expected-note {{non-literal type 'Temporaries::NonLiteral'}}
972 
973 }
974 
975 namespace Union {
976 
977 union U {
978   int a;
979   int b;
980 };
981 
982 constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
983 static_assert(u[0].a == 0, "");
984 static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
985 static_assert(u[1].b == 1, "");
986 static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
987 static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
988 static_assert((&(u[1]) + 1 + 1)->b == 3, "");
989 
990 constexpr U v = {};
991 static_assert(v.a == 0, "");
992 
993 union Empty {};
994 constexpr Empty e = {};
995 
996 // Make sure we handle trivial copy constructors for unions.
997 constexpr U x = {42};
998 constexpr U y = x;
999 static_assert(y.a == 42, "");
1000 static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
1001 
1002 }
1003 
1004 namespace MemberPointer {
1005   struct A {
AMemberPointer::A1006     constexpr A(int n) : n(n) {}
1007     int n;
fMemberPointer::A1008     constexpr int f() const { return n + 3; }
1009   };
1010   constexpr A a(7);
1011   static_assert(A(5).*&A::n == 5, "");
1012   static_assert((&a)->*&A::n == 7, "");
1013   static_assert((A(8).*&A::f)() == 11, "");
1014   static_assert(((&a)->*&A::f)() == 10, "");
1015 
1016   struct B : A {
BMemberPointer::B1017     constexpr B(int n, int m) : A(n), m(m) {}
1018     int m;
gMemberPointer::B1019     constexpr int g() const { return n + m + 1; }
1020   };
1021   constexpr B b(9, 13);
1022   static_assert(B(4, 11).*&A::n == 4, "");
1023   static_assert(B(4, 11).*&B::m == 11, "");
1024   static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
1025   static_assert((&b)->*&A::n == 9, "");
1026   static_assert((&b)->*&B::m == 13, "");
1027   static_assert((&b)->*(int(A::*))&B::m == 13, "");
1028   static_assert((B(4, 11).*&A::f)() == 7, "");
1029   static_assert((B(4, 11).*&B::g)() == 16, "");
1030   static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
1031   static_assert(((&b)->*&A::f)() == 12, "");
1032   static_assert(((&b)->*&B::g)() == 23, "");
1033   static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
1034 
1035   struct S {
SMemberPointer::S1036     constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
1037       m(m), n(n), pf(pf), pn(pn) {}
SMemberPointer::S1038     constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
1039 
fMemberPointer::S1040     constexpr int f() const { return this->*pn; }
1041     virtual int g() const;
1042 
1043     int m, n;
1044     int (S::*pf)() const;
1045     int S::*pn;
1046   };
1047 
1048   constexpr int S::*pm = &S::m;
1049   constexpr int S::*pn = &S::n;
1050   constexpr int (S::*pf)() const = &S::f;
1051   constexpr int (S::*pg)() const = &S::g;
1052 
1053   constexpr S s(2, 5, &S::f, &S::m);
1054 
1055   static_assert((s.*&S::f)() == 2, "");
1056   static_assert((s.*s.pf)() == 2, "");
1057 
1058   static_assert(pf == &S::f, "");
1059   static_assert(pf == s.*&S::pf, "");
1060   static_assert(pm == &S::m, "");
1061   static_assert(pm != pn, "");
1062   static_assert(s.pn != pn, "");
1063   static_assert(s.pn == pm, "");
1064   static_assert(pg != nullptr, "");
1065   static_assert(pf != nullptr, "");
1066   static_assert((int S::*)nullptr == nullptr, "");
1067   static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1068   static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1069 
1070   template<int n> struct T : T<n-1> {};
1071   template<> struct T<0> { int n; };
1072   template<> struct T<30> : T<29> { int m; };
1073 
1074   T<17> t17;
1075   T<30> t30;
1076 
1077   constexpr int (T<10>::*deepn) = &T<0>::n;
1078   static_assert(&(t17.*deepn) == &t17.n, "");
1079   static_assert(deepn == &T<2>::n, "");
1080 
1081   constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
1082   constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
1083   static_assert(&(t30.*deepm) == &t30.m, "");
1084   static_assert(deepm == &T<50>::m, "");
1085   static_assert(deepm != deepn, "");
1086 
1087   constexpr T<5> *p17_5 = &t17;
1088   constexpr T<13> *p17_13 = (T<13>*)p17_5;
1089   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>'}}
1090   static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
1091   static_assert(&(p17_13->*deepn) == &t17.n, "");
1092   constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
1093 
1094   constexpr T<5> *p30_5 = &t30;
1095   constexpr T<23> *p30_23 = (T<23>*)p30_5;
1096   constexpr T<13> *p30_13 = p30_23;
1097   static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
1098   static_assert(&(p30_13->*deepn) == &t30.n, "");
1099   static_assert(&(p30_23->*deepn) == &t30.n, "");
1100   static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
1101   static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
1102   static_assert(&(p30_23->*deepm) == &t30.m, "");
1103 
1104   struct Base { int n; };
1105   template<int N> struct Mid : Base {};
1106   struct Derived : Mid<0>, Mid<1> {};
1107   static_assert(&Mid<0>::n == &Mid<1>::n, "");
1108   static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n !=
1109                 (int Derived::*)(int Mid<1>::*)&Mid<1>::n, "");
1110   static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, "");
1111 }
1112 
1113 namespace ArrayBaseDerived {
1114 
1115   struct Base {
BaseArrayBaseDerived::Base1116     constexpr Base() {}
1117     int n = 0;
1118   };
1119   struct Derived : Base {
DerivedArrayBaseDerived::Derived1120     constexpr Derived() {}
fArrayBaseDerived::Derived1121     constexpr const int *f() const { return &n; }
1122   };
1123 
1124   constexpr Derived a[10];
1125   constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
1126   constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
1127   static_assert(pb3 == pd3, "");
1128 
1129   // pb3 does not point to an array element.
1130   constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
1131   constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1132   constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1133   constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1134   constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1135   constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1136   constexpr Base *pb3a = pb4 - 1;
1137 
1138   // pb4 does not point to a Derived.
1139   constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1140   constexpr Derived *pd3a = (Derived*)pb3a;
1141   constexpr int pd3n = pd3a->n;
1142 
1143   // pd3a still points to the Derived array.
1144   constexpr Derived *pd6 = pd3a + 3;
1145   static_assert(pd6 == &a[6], "");
1146   constexpr Derived *pd9 = pd6 + 3;
1147   constexpr Derived *pd10 = pd6 + 4;
1148   constexpr int pd9n = pd9->n; // ok
1149   constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1150   constexpr int pd0n = pd10[-10].n;
1151   constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1152 
1153   constexpr Base *pb9 = pd9;
1154   constexpr const int *(Base::*pfb)() const =
1155       static_cast<const int *(Base::*)() const>(&Derived::f);
1156   static_assert((pb9->*pfb)() == &a[9].n, "");
1157 }
1158 
1159 namespace Complex {
1160 
1161 class complex {
1162   int re, im;
1163 public:
complex(int re=0,int im=0)1164   constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
complex(const complex & o)1165   constexpr complex(const complex &o) : re(o.re), im(o.im) {}
operator -() const1166   constexpr complex operator-() const { return complex(-re, -im); }
operator +(const complex & l,const complex & r)1167   friend constexpr complex operator+(const complex &l, const complex &r) {
1168     return complex(l.re + r.re, l.im + r.im);
1169   }
operator -(const complex & l,const complex & r)1170   friend constexpr complex operator-(const complex &l, const complex &r) {
1171     return l + -r;
1172   }
operator *(const complex & l,const complex & r)1173   friend constexpr complex operator*(const complex &l, const complex &r) {
1174     return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
1175   }
operator ==(const complex & l,const complex & r)1176   friend constexpr bool operator==(const complex &l, const complex &r) {
1177     return l.re == r.re && l.im == r.im;
1178   }
operator !=(const complex & r) const1179   constexpr bool operator!=(const complex &r) const {
1180     return re != r.re || im != r.im;
1181   }
real() const1182   constexpr int real() const { return re; }
imag() const1183   constexpr int imag() const { return im; }
1184 };
1185 
1186 constexpr complex i = complex(0, 1);
1187 constexpr complex k = (3 + 4*i) * (6 - 4*i);
1188 static_assert(complex(1,0).real() == 1, "");
1189 static_assert(complex(1,0).imag() == 0, "");
1190 static_assert(((complex)1).imag() == 0, "");
1191 static_assert(k.real() == 34, "");
1192 static_assert(k.imag() == 12, "");
1193 static_assert(k - 34 == 12*i, "");
1194 static_assert((complex)1 == complex(1), "");
1195 static_assert((complex)1 != complex(0, 1), "");
1196 static_assert(complex(1) == complex(1), "");
1197 static_assert(complex(1) != complex(0, 1), "");
makeComplex(int re,int im)1198 constexpr complex makeComplex(int re, int im) { return complex(re, im); }
1199 static_assert(makeComplex(1,0) == complex(1), "");
1200 static_assert(makeComplex(1,0) != complex(0, 1), "");
1201 
1202 class complex_wrap : public complex {
1203 public:
complex_wrap(int re,int im=0)1204   constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
complex_wrap(const complex_wrap & o)1205   constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
1206 };
1207 
1208 static_assert((complex_wrap)1 == complex(1), "");
1209 static_assert((complex)1 != complex_wrap(0, 1), "");
1210 static_assert(complex(1) == complex_wrap(1), "");
1211 static_assert(complex_wrap(1) != complex(0, 1), "");
makeComplexWrap(int re,int im)1212 constexpr complex_wrap makeComplexWrap(int re, int im) {
1213   return complex_wrap(re, im);
1214 }
1215 static_assert(makeComplexWrap(1,0) == complex(1), "");
1216 static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1217 
1218 }
1219 
1220 namespace PR11595 {
operator ==PR11595::A1221   struct A { constexpr bool operator==(int x) const { return true; } };
1222   struct B { B(); A& x; };
1223   static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1224 
f(int k)1225   constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}}
1226     return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1227   }
1228 }
1229 
1230 namespace ExprWithCleanups {
1231   struct A { A(); ~A(); int get(); };
get(bool FromA)1232   constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
1233   constexpr int n = get(false);
1234 }
1235 
1236 namespace Volatile {
1237 
1238 volatile constexpr int n1 = 0; // expected-note {{here}}
1239 volatile const int n2 = 0; // expected-note {{here}}
1240 int n3 = 37; // expected-note {{declared here}}
1241 
1242 constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1243 constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1244 constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1245 constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1246 
1247 struct T { int n; };
1248 const T t = { 42 };
1249 
f(volatile int && r)1250 constexpr int f(volatile int &&r) {
1251   return r; // expected-note {{read of volatile-qualified type 'volatile int'}}
1252 }
g(volatile int && r)1253 constexpr int g(volatile int &&r) {
1254   return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1255 }
1256 struct S {
1257   int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1258   int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1259   int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1260   int m : t.n; // expected-warning{{width of bit-field 'm' (42 bits)}} expected-warning{{expression is not an integral constant expression}} expected-note{{read of non-constexpr variable 't' is not allowed}}
1261 };
1262 
1263 }
1264 
1265 namespace ExternConstexpr {
1266   extern constexpr int n = 0;
1267   extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
f()1268   void f() {
1269     extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
1270     constexpr int j = 0;
1271     constexpr int k; // expected-error {{default initialization of an object of const type}}
1272   }
1273 
1274   extern const int q;
g()1275   constexpr int g() { return q; }
1276   constexpr int q = g();
1277   static_assert(q == 0, "zero-initialization should precede static initialization");
1278 
1279   extern int r; // expected-note {{here}}
h()1280   constexpr int h() { return r; } // expected-error {{never produces a constant}} expected-note {{read of non-const}}
1281 
1282   struct S { int n; };
1283   extern const S s;
x()1284   constexpr int x() { return s.n; }
1285   constexpr S s = {x()};
1286   static_assert(s.n == 0, "zero-initialization should precede static initialization");
1287 }
1288 
1289 namespace ComplexConstexpr {
1290   constexpr _Complex float test1 = {};
1291   constexpr _Complex float test2 = {1};
1292   constexpr _Complex double test3 = {1,2};
1293   constexpr _Complex int test4 = {4};
1294   constexpr _Complex int test5 = 4;
1295   constexpr _Complex int test6 = {5,6};
1296   typedef _Complex float fcomplex;
1297   constexpr fcomplex test7 = fcomplex();
1298 
1299   constexpr const double &t2r = __real test3;
1300   constexpr const double &t2i = __imag test3;
1301   static_assert(&t2r + 1 == &t2i, "");
1302   static_assert(t2r == 1.0, "");
1303   static_assert(t2i == 2.0, "");
1304   constexpr const double *t2p = &t2r;
1305   static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1306   static_assert(t2p[0] == 1.0, "");
1307   static_assert(t2p[1] == 2.0, "");
1308   static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1309   static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1310   constexpr _Complex float *p = 0;
1311   constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1312   constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1313   constexpr const _Complex double *q = &test3 + 1;
1314   constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1315   constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1316 
1317   static_assert(__real test6 == 5, "");
1318   static_assert(__imag test6 == 6, "");
1319   static_assert(&__imag test6 == &__real test6 + 1, "");
1320 }
1321 
1322 // _Atomic(T) is exactly like T for the purposes of constant expression
1323 // evaluation..
1324 namespace Atomic {
1325   constexpr _Atomic int n = 3;
1326 
1327   struct S { _Atomic(double) d; };
1328   constexpr S s = { 0.5 };
1329   constexpr double d1 = s.d;
1330   constexpr double d2 = n;
1331   constexpr _Atomic double d3 = n;
1332 
1333   constexpr _Atomic(int) n2 = d3;
1334   static_assert(d1 == 0.5, "");
1335   static_assert(d3 == 3.0, "");
1336 
1337   namespace PR16056 {
1338     struct TestVar {
1339       _Atomic(int) value;
TestVarAtomic::PR16056::TestVar1340       constexpr TestVar(int value) : value(value) {}
1341     };
1342     constexpr TestVar testVar{-1};
1343     static_assert(testVar.value == -1, "");
1344   }
1345 
1346   namespace PR32034 {
1347     struct A {};
1348     struct B { _Atomic(A) a; };
1349     constexpr int n = (B(), B(), 0);
1350 
CAtomic::PR32034::C1351     struct C { constexpr C() {} void *self = this; };
1352     constexpr _Atomic(C) c = C();
1353   }
1354 }
1355 
1356 namespace InstantiateCaseStmt {
f()1357   template<int x> constexpr int f() { return x; }
g(int c)1358   template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; }
gg(int c)1359   int gg(int c) { return g<4>(c); }
1360 }
1361 
1362 namespace ConvertedConstantExpr {
1363   extern int &m;
1364   extern int &n;
1365 
1366   constexpr int k = 4;
1367   int &m = const_cast<int&>(k);
1368 
1369   // If we have nothing more interesting to say, ensure we don't produce a
1370   // useless note and instead just point to the non-constant subexpression.
1371   enum class E {
1372     em = m,
1373     en = n, // expected-error {{not a constant expression}}
1374     eo = (m +
1375           n // expected-error {{not a constant expression}}
1376           ),
1377     eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1378   };
1379 }
1380 
1381 namespace IndirectField {
1382   struct S {
1383     struct { // expected-warning {{GNU extension}}
1384       union { // expected-warning {{declared in an anonymous struct}}
1385         struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1386           int a;
1387           int b;
1388         };
1389         int c;
1390       };
1391       int d;
1392     };
1393     union {
1394       int e;
1395       int f;
1396     };
SIndirectField::S1397     constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {}
SIndirectField::S1398     constexpr S(int c, int d, int f) : c(c), d(d), f(f) {}
1399   };
1400 
1401   constexpr S s1(1, 2, 3, 4);
1402   constexpr S s2(5, 6, 7);
1403 
1404   // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1405   // member is active and which is requested.
1406   static_assert(s1.a == 1, "");
1407   static_assert(s1.b == 2, "");
1408   static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1409   static_assert(s1.d == 3, "");
1410   static_assert(s1.e == 4, "");
1411   static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1412 
1413   static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1414   static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1415   static_assert(s2.c == 5, "");
1416   static_assert(s2.d == 6, "");
1417   static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1418   static_assert(s2.f == 7, "");
1419 }
1420 
1421 // DR1405: don't allow reading mutable members in constant expressions.
1422 namespace MutableMembers {
1423   struct MM {
1424     mutable int n; // expected-note 3{{declared here}}
1425   } constexpr mm = { 4 };
1426   constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1427   int x = (mm.n = 1, 3);
1428   constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1429 
1430   // Here's one reason why allowing this would be a disaster...
1431   template<int n> struct Id { int k = n; };
f()1432   int f() {
1433     constexpr MM m = { 0 };
1434     ++m.n;
1435     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}}
1436   }
1437 
1438   struct A { int n; };
1439   struct B { mutable A a; }; // expected-note {{here}}
1440   struct C { B b; };
1441   constexpr C c[3] = {};
1442   constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1443 
1444   struct D { int x; mutable int y; }; // expected-note {{here}}
1445   constexpr D d1 = { 1, 2 };
1446   int l = ++d1.y;
1447   constexpr D d2 = d1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1448 
1449   struct E {
1450     union {
1451       int a;
1452       mutable int b; // expected-note {{here}}
1453     };
1454   };
1455   constexpr E e1 = {{1}};
1456   constexpr E e2 = e1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1457 
1458   struct F {
1459     union U { };
1460     mutable U u;
1461     struct X { };
1462     mutable X x;
1463     struct Y : X { X x; U u; };
1464     mutable Y y;
1465     int n;
1466   };
1467   // This is OK; we don't actually read any mutable state here.
1468   constexpr F f1 = {};
1469   constexpr F f2 = f1;
1470 
1471   struct G {
1472     struct X {};
1473     union U { X a; };
1474     mutable U u; // expected-note {{here}}
1475   };
1476   constexpr G g1 = {};
1477   constexpr G g2 = g1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1478   constexpr G::U gu1 = {};
1479   constexpr G::U gu2 = gu1;
1480 
1481   union H {
1482     mutable G::X gx; // expected-note {{here}}
1483   };
1484   constexpr H h1 = {};
1485   constexpr H h2 = h1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1486 }
1487 
1488 namespace Fold {
1489 
1490   // This macro forces its argument to be constant-folded, even if it's not
1491   // otherwise a constant expression.
1492   #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
1493 
1494   constexpr int n = (long)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1495   constexpr int m = fold((long)(char*)123); // ok
1496   static_assert(m == 123, "");
1497 
1498   #undef fold
1499 
1500 }
1501 
1502 namespace DR1454 {
1503 
f(const int & n)1504 constexpr const int &f(const int &n) { return n; }
1505 constexpr int k1 = f(0); // ok
1506 
1507 struct Wrap {
1508   const int &value;
1509 };
g(const Wrap & w)1510 constexpr const Wrap &g(const Wrap &w) { return w; }
1511 constexpr int k2 = g({0}).value; // ok
1512 
1513 // The temporary here has static storage duration, so we can bind a constexpr
1514 // reference to it.
1515 constexpr const int &i = 1;
1516 constexpr const int j = i;
1517 static_assert(j == 1, "");
1518 
1519 // The temporary here is not const, so it can't be read outside the expression
1520 // in which it was created (per the C++14 rules, which we use to avoid a C++11
1521 // defect).
1522 constexpr int &&k = 1; // expected-note {{temporary created here}}
1523 constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}}
1524 
f()1525 void f() {
1526   // The temporary here has automatic storage duration, so we can't bind a
1527   // constexpr reference to it.
1528   constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1529 }
1530 
1531 }
1532 
1533 namespace RecursiveOpaqueExpr {
1534   template<typename Iter>
LastNonzero(Iter p,Iter q)1535   constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) {
1536     return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}}
1537   }
1538 
1539   constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1540   static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, "");
1541 
1542   constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1543   static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, "");
1544 
1545   constexpr int arr3[] = {
1546     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,
1547     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,
1548     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,
1549     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,
1550     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,
1551     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,
1552     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,
1553     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 };
1554   static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, "");
1555 }
1556 
1557 namespace VLASizeof {
1558 
f(int k)1559   void f(int k) {
1560     int arr[k]; // expected-warning {{C99}}
1561     constexpr int n = 1 +
1562         sizeof(arr) // expected-error {{constant expression}}
1563         * 3;
1564   }
1565 }
1566 
1567 namespace CompoundLiteral {
1568   // Matching GCC, file-scope array compound literals initialized by constants
1569   // are lifetime-extended.
1570   constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}}
1571   static_assert(*p == 3, "");
1572   static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1573 
1574   // Other kinds are not.
1575   struct X { int a[2]; };
1576   constexpr int *n = (X){1, 2}.a; // expected-warning {{C99}} expected-warning {{temporary}}
1577   // expected-error@-1 {{constant expression}}
1578   // expected-note@-2 {{pointer to subobject of temporary}}
1579   // expected-note@-3 {{temporary created here}}
1580 
f()1581   void f() {
1582     static constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}} expected-warning {{temporary}}
1583     // expected-error@-1 {{constant expression}}
1584     // expected-note@-2 {{pointer to subobject of temporary}}
1585     // expected-note@-3 {{temporary created here}}
1586     static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1587   }
1588 }
1589 
1590 namespace Vector {
1591   typedef int __attribute__((vector_size(16))) VI4;
f(int n)1592   constexpr VI4 f(int n) {
1593     return VI4 { n * 3, n + 4, n - 5, n / 6 };
1594   }
1595   constexpr auto v1 = f(10);
1596 
1597   typedef double __attribute__((vector_size(32))) VD4;
g(int n)1598   constexpr VD4 g(int n) {
1599     return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}}
1600   }
1601   constexpr auto v2 = g(4);
1602 }
1603 
1604 // PR12626, redux
1605 namespace InvalidClasses {
test0()1606   void test0() {
1607     struct X; // expected-note {{forward declaration}}
1608     struct Y { bool b; X x; }; // expected-error {{field has incomplete type}}
1609     Y y;
1610     auto& b = y.b;
1611   }
1612 }
1613 
1614 namespace NamespaceAlias {
f()1615   constexpr int f() {
1616     namespace NS = NamespaceAlias; // expected-warning {{use of this statement in a constexpr function is a C++14 extension}}
1617     return &NS::f != nullptr;
1618   }
1619 }
1620 
1621 // Constructors can be implicitly constexpr, even for a non-literal type.
1622 namespace ImplicitConstexpr {
1623   struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}}
1624   struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; };
1625   struct S { R r; }; // expected-note 3{{here}}
1626   struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; };
1627   struct U { T t; }; // expected-note 3{{here}}
1628   static_assert(!__is_literal_type(Q), "");
1629   static_assert(!__is_literal_type(R), "");
1630   static_assert(!__is_literal_type(S), "");
1631   static_assert(!__is_literal_type(T), "");
1632   static_assert(!__is_literal_type(U), "");
1633   struct Test {
1634     friend Q::Q() noexcept; // expected-error {{follows constexpr}}
1635     friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}}
1636     friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}}
1637     friend S::S() noexcept; // expected-error {{follows constexpr}}
1638     friend S::S(S&&) noexcept; // expected-error {{follows constexpr}}
1639     friend S::S(const S&) noexcept; // expected-error {{follows constexpr}}
1640     friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}}
1641     friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}}
1642     friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}}
1643   };
1644 }
1645 
1646 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1647 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1648 namespace PR12826 {
1649   struct Foo {};
id(Foo x)1650   constexpr Foo id(Foo x) { return x; }
1651   constexpr Foo res(id(Foo()));
1652 }
1653 
1654 namespace PR13273 {
1655   struct U {
1656     int t;
1657     U() = default;
1658   };
1659 
1660   struct S : U {
1661     S() = default;
1662   };
1663 
1664   // S's default constructor isn't constexpr, because U's default constructor
1665   // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1666   // actually call it.
1667   static_assert(S{}.t == 0, "");
1668 }
1669 
1670 namespace PR12670 {
1671   struct S {
SPR12670::S1672     constexpr S(int a0) : m(a0) {}
SPR12670::S1673     constexpr S() : m(6) {}
1674     int m;
1675   };
1676   constexpr S x[3] = { {4}, 5 };
1677   static_assert(x[0].m == 4, "");
1678   static_assert(x[1].m == 5, "");
1679   static_assert(x[2].m == 6, "");
1680 }
1681 
1682 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1683 // when a conditional operator has one argument of type void and where the other
1684 // is a glvalue of class type.
1685 namespace ConditionalLValToRVal {
1686   struct A {
AConditionalLValToRVal::A1687     constexpr A(int a) : v(a) {}
1688     int v;
1689   };
1690 
f(const A & a)1691   constexpr A f(const A &a) {
1692     return a.v == 0 ? throw a : a;
1693   }
1694 
1695   constexpr A a(4);
1696   static_assert(f(a).v == 4, "");
1697 }
1698 
1699 namespace TLS {
1700   __thread int n;
1701   int m;
1702 
1703   constexpr bool b = &n == &n;
1704 
1705   constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1706 
f()1707   constexpr int *f() { return &n; }
1708   constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1709   constexpr bool c = f() == f();
1710 
g()1711   constexpr int *g() { return &m; }
1712   constexpr int *r = g();
1713 }
1714 
1715 namespace Void {
f()1716   constexpr void f() { return; } // expected-error{{constexpr function's return type 'void' is not a literal type}}
1717 
1718   void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}}
1719 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1720   template<typename T, size_t S>
get(T (& a)[S],size_t k)1721   constexpr T get(T (&a)[S], size_t k) {
1722     return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}}
1723   }
1724 #undef ASSERT
1725   template int get(int (&a)[4], size_t);
1726   constexpr int arr[] = { 4, 1, 2, 3, 4 };
1727   static_assert(get(arr, 1) == 1, "");
1728   static_assert(get(arr, 4) == 4, "");
1729   static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1730   // expected-note{{in call to 'get(arr, 0)'}}
1731 }
1732 
1733 namespace std { struct type_info; }
1734 
1735 namespace TypeId {
1736   struct A { virtual ~A(); };
1737   A f();
1738   A &g();
1739   constexpr auto &x = typeid(f());
1740   constexpr auto &y = typeid(g()); // expected-error{{constant expression}} \
1741   // expected-note{{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}} \
1742   // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
1743 }
1744 
1745 namespace PR14203 {
1746   struct duration {
durationPR14203::duration1747     constexpr duration() {}
operator intPR14203::duration1748     constexpr operator int() const { return 0; }
1749   };
1750   // These are valid per P0859R0 (moved as DR).
f()1751   template<typename T> void f() {
1752     constexpr duration d = duration();
1753   }
1754   int n = sizeof(short{duration(duration())});
1755 }
1756 
1757 namespace ArrayEltInit {
1758   struct A {
AArrayEltInit::A1759     constexpr A() : p(&p) {}
1760     void *p;
1761   };
1762   constexpr A a[10];
1763   static_assert(a[0].p == &a[0].p, "");
1764   static_assert(a[9].p == &a[9].p, "");
1765   static_assert(a[0].p != &a[9].p, "");
1766   static_assert(a[9].p != &a[0].p, "");
1767 
1768   constexpr A b[10] = {};
1769   static_assert(b[0].p == &b[0].p, "");
1770   static_assert(b[9].p == &b[9].p, "");
1771   static_assert(b[0].p != &b[9].p, "");
1772   static_assert(b[9].p != &b[0].p, "");
1773 }
1774 
1775 namespace PR15884 {
1776   struct S {};
f()1777   constexpr S f() { return {}; }
1778   constexpr S *p = &f();
1779   // expected-error@-1 {{taking the address of a temporary}}
1780   // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1781   // expected-note@-3 {{pointer to temporary is not a constant expression}}
1782   // expected-note@-4 {{temporary created here}}
1783 }
1784 
1785 namespace AfterError {
1786   // FIXME: Suppress the 'no return statements' diagnostic if the body is invalid.
error()1787   constexpr int error() { // expected-error {{no return statement}}
1788     return foobar; // expected-error {{undeclared identifier}}
1789   }
1790   constexpr int k = error();
1791 }
1792 
1793 namespace std {
1794   typedef decltype(sizeof(int)) size_t;
1795 
1796   template <class _E>
1797   class initializer_list
1798   {
1799     const _E* __begin_;
1800     size_t    __size_;
1801 
initializer_list(const _E * __b,size_t __s)1802     constexpr initializer_list(const _E* __b, size_t __s)
1803       : __begin_(__b),
1804         __size_(__s)
1805     {}
1806 
1807   public:
1808     typedef _E        value_type;
1809     typedef const _E& reference;
1810     typedef const _E& const_reference;
1811     typedef size_t    size_type;
1812 
1813     typedef const _E* iterator;
1814     typedef const _E* const_iterator;
1815 
initializer_list()1816     constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1817 
size() const1818     constexpr size_t    size()  const {return __size_;}
begin() const1819     constexpr const _E* begin() const {return __begin_;}
end() const1820     constexpr const _E* end()   const {return __begin_ + __size_;}
1821   };
1822 }
1823 
1824 namespace InitializerList {
sum(const int * b,const int * e)1825   constexpr int sum(const int *b, const int *e) {
1826     return b != e ? *b + sum(b+1, e) : 0;
1827   }
sum(std::initializer_list<int> ints)1828   constexpr int sum(std::initializer_list<int> ints) {
1829     return sum(ints.begin(), ints.end());
1830   }
1831   static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1832 
1833   static_assert(*std::initializer_list<int>{1, 2, 3}.begin() == 1, "");
1834   static_assert(std::initializer_list<int>{1, 2, 3}.begin()[2] == 3, "");
1835 }
1836 
1837 namespace StmtExpr {
1838   struct A { int k; };
f()1839   void f() {
1840     static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}}
1841     constexpr auto a = ({ A(); }); // expected-warning {{extension}}
1842   }
g(int k)1843   constexpr int g(int k) {
1844     return ({ // expected-warning {{extension}}
1845       const int x = k;
1846       x * x;
1847     });
1848   }
1849   static_assert(g(123) == 15129, "");
h()1850   constexpr int h() { // expected-error {{never produces a constant}}
1851     return ({ // expected-warning {{extension}}
1852       return 0; // expected-note {{not supported}}
1853       1;
1854     });
1855   }
1856 }
1857 
1858 namespace VirtualFromBase {
1859   struct S1 {
1860     virtual int f() const;
1861   };
1862   struct S2 {
1863     virtual int f();
1864   };
1865   template <typename T> struct X : T {
XVirtualFromBase::X1866     constexpr X() {}
1867     double d = 0.0;
fVirtualFromBase::X1868     constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++14}}
1869   };
1870 
1871   // Virtual f(), not OK.
1872   constexpr X<X<S1>> xxs1;
1873   constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
1874   static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function}}
1875 
1876   // Non-virtual f(), OK.
1877   constexpr X<X<S2>> xxs2;
1878   constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
1879   static_assert(q->f() == sizeof(S2), "");
1880 }
1881 
1882 namespace ConstexprConstructorRecovery {
1883   class X {
1884   public:
1885       enum E : short {
1886           headers = 0x1,
1887           middlefile = 0x2,
1888           choices = 0x4
1889       };
X()1890       constexpr X() noexcept {};
1891   protected:
1892       E val{0}; // expected-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}}
1893   };
1894   constexpr X x{};
1895 }
1896 
1897 namespace Lifetime {
f()1898   void f() {
1899     constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1900     constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1901   }
1902 
get(int && n)1903   constexpr int &get(int &&n) { return n; }
get_rv(int && n)1904   constexpr int &&get_rv(int &&n) { return static_cast<int&&>(n); }
1905   struct S {
1906     int &&r;
1907     int &s;
1908     int t;
SLifetime::S1909     constexpr S() : r(get_rv(0)), s(get(0)), t(r) {} // expected-note {{read of object outside its lifetime}}
SLifetime::S1910     constexpr S(int) : r(get_rv(0)), s(get(0)), t(s) {} // expected-note {{read of object outside its lifetime}}
1911   };
1912   constexpr int k1 = S().t; // expected-error {{constant expression}} expected-note {{in call}}
1913   constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}}
1914 
1915   struct Q {
1916     int n = 0;
fLifetime::Q1917     constexpr int f() const { return 0; }
1918   };
out_of_lifetime(Q q)1919   constexpr Q *out_of_lifetime(Q q) { return &q; } // expected-warning {{address of stack}} expected-note 2{{declared here}}
1920   constexpr int k3 = out_of_lifetime({})->n; // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
1921   constexpr int k4 = out_of_lifetime({})->f(); // expected-error {{constant expression}} expected-note {{member call on variable whose lifetime has ended}}
1922 
1923   constexpr int null = ((Q*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
1924 
1925   Q q;
1926   Q qa[3];
1927   constexpr int pte0 = (&q)[0].f(); // ok
1928   constexpr int pte1 = (&q)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1929   constexpr int pte2 = qa[2].f(); // ok
1930   constexpr int pte3 = qa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1931 
1932   constexpr Q cq;
1933   constexpr Q cqa[3];
1934   constexpr int cpte0 = (&cq)[0].f(); // ok
1935   constexpr int cpte1 = (&cq)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1936   constexpr int cpte2 = cqa[2].f(); // ok
1937   constexpr int cpte3 = cqa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1938 
1939   // FIXME: There's no way if we can tell if the first call here is valid; it
1940   // depends on the active union member. Should we reject for that reason?
1941   union U {
1942     int n;
1943     Q q;
1944   };
1945   U u1 = {0};
1946   constexpr U u2 = {0};
1947   constexpr int union_member1 = u1.q.f();
1948   constexpr int union_member2 = u2.q.f(); // expected-error {{constant expression}} expected-note {{member call on member 'q' of union with active member 'n'}}
1949 
1950   struct R { // expected-note {{field init}}
fLifetime::R::Inner1951     struct Inner { constexpr int f() const { return 0; } };
1952     int a = b.f(); // expected-warning {{uninitialized}} expected-note {{member call on object outside its lifetime}}
1953     Inner b;
1954   };
1955   // FIXME: This should be rejected under DR2026.
1956   constexpr R r; // expected-note {{default constructor}}
rf()1957   void rf() {
1958     constexpr R r; // expected-error {{constant expression}} expected-note {{in call}}
1959   }
1960 }
1961 
1962 namespace Bitfields {
1963   struct A {
1964     bool b : 1;
1965     unsigned u : 5;
1966     int n : 5;
1967     bool b2 : 3;
1968     unsigned u2 : 74; // expected-warning {{exceeds the width of its type}}
1969     int n2 : 81; // expected-warning {{exceeds the width of its type}}
1970   };
1971 
1972   constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
1973   static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 &&
1974                 a.u2 + 1 == 0 && a.n2 == 0x7fffffff,
1975                 "bad truncation of bitfield values");
1976 
1977   struct B {
1978     int n : 3;
BBitfields::B1979     constexpr B(int k) : n(k) {}
1980   };
1981   static_assert(B(3).n == 3, "");
1982   static_assert(B(4).n == -4, "");
1983   static_assert(B(7).n == -1, "");
1984   static_assert(B(8).n == 0, "");
1985   static_assert(B(-1).n == -1, "");
1986   static_assert(B(-8889).n == -1, "");
1987 
1988   namespace PR16755 {
1989     struct X {
1990       int x : 1;
fBitfields::PR16755::X1991       constexpr static int f(int x) {
1992         return X{x}.x;
1993       }
1994     };
1995     static_assert(X::f(3) == -1, "3 should truncate to -1");
1996   }
1997 
1998   struct HasUnnamedBitfield {
1999     unsigned a;
2000     unsigned : 20;
2001     unsigned b;
2002 
HasUnnamedBitfieldBitfields::HasUnnamedBitfield2003     constexpr HasUnnamedBitfield() : a(), b() {}
HasUnnamedBitfieldBitfields::HasUnnamedBitfield2004     constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
2005   };
2006 
testUnnamedBitfield()2007   void testUnnamedBitfield() {
2008     const HasUnnamedBitfield zero{};
2009     int a = 1 / zero.b; // expected-warning {{division by zero is undefined}}
2010     const HasUnnamedBitfield oneZero{1, 0};
2011     int b = 1 / oneZero.b; // expected-warning {{division by zero is undefined}}
2012   }
2013 }
2014 
2015 namespace ZeroSizeTypes {
2016   constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
2017   constexpr int k = p2 - p1;
2018   // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
2019   // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}}
2020 
2021   int arr[5][0];
f()2022   constexpr int f() { // expected-error {{never produces a constant expression}}
2023     return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}}
2024   }
2025 }
2026 
2027 namespace BadDefaultInit {
2028   template<int N> struct X { static const int n = N; };
2029 
2030   struct A { // expected-error {{default member initializer for 'k' needed within definition of enclosing class}}
2031     int k = // expected-note {{default member initializer declared here}}
2032         X<A().k>::n; // expected-note {{in evaluation of exception specification for 'BadDefaultInit::A::A' needed here}}
2033   };
2034 
2035   // FIXME: The "constexpr constructor must initialize all members" diagnostic
2036   // here is bogus (we discard the k(k) initializer because the parameter 'k'
2037   // has been marked invalid).
2038   struct B { // expected-note 2{{candidate}}
BBadDefaultInit::B2039     constexpr B( // expected-error {{must initialize all members}} expected-note {{candidate}}
2040         int k = X<B().k>::n) : // expected-error {{no matching constructor}}
2041       k(k) {}
2042     int k; // expected-note {{not initialized}}
2043   };
2044 }
2045 
2046 namespace NeverConstantTwoWays {
2047   // If we see something non-constant but foldable followed by something
2048   // non-constant and not foldable, we want the first diagnostic, not the
2049   // second.
f(int n)2050   constexpr int f(int n) { // expected-error {{never produces a constant expression}}
2051     return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
2052         1 / 0 : // expected-warning {{division by zero}}
2053         0;
2054   }
2055 
2056   constexpr int n = // expected-error {{must be initialized by a constant expression}}
2057       (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
2058         1 / 0 :
2059         0;
2060 }
2061 
2062 namespace PR17800 {
2063   struct A {
operator ()PR17800::A2064     constexpr int operator()() const { return 0; }
2065   };
sink(T...)2066   template <typename ...T> constexpr int sink(T ...) {
2067     return 0;
2068   }
run()2069   template <int ...N> constexpr int run() {
2070     return sink(A()() + N ...);
2071   }
2072   constexpr int k = run<1, 2, 3>();
2073 }
2074 
2075 namespace BuiltinStrlen {
2076   constexpr const char *a = "foo\0quux";
2077   constexpr char b[] = "foo\0quux";
f()2078   constexpr int f() { return 'u'; }
2079   constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
2080 
2081   static_assert(__builtin_strlen("foo") == 3, "");
2082   static_assert(__builtin_strlen("foo\0quux") == 3, "");
2083   static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
2084 
check(const char * p)2085   constexpr bool check(const char *p) {
2086     return __builtin_strlen(p) == 3 &&
2087            __builtin_strlen(p + 1) == 2 &&
2088            __builtin_strlen(p + 2) == 1 &&
2089            __builtin_strlen(p + 3) == 0 &&
2090            __builtin_strlen(p + 4) == 4 &&
2091            __builtin_strlen(p + 5) == 3 &&
2092            __builtin_strlen(p + 6) == 2 &&
2093            __builtin_strlen(p + 7) == 1 &&
2094            __builtin_strlen(p + 8) == 0;
2095   }
2096 
2097   static_assert(check(a), "");
2098   static_assert(check(b), "");
2099   static_assert(check(c), "");
2100 
2101   constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2102   constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2103   constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2104 
2105   constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2106   constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2107   constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2108 
2109   // FIXME: The diagnostic here could be better.
2110   constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
2111   constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2112 }
2113 
2114 namespace PR19010 {
2115   struct Empty {};
2116   struct Empty2 : Empty {};
2117   struct Test : Empty2 {
TestPR19010::Test2118     constexpr Test() {}
2119     Empty2 array[2];
2120   };
test()2121   void test() { constexpr Test t; }
2122 }
2123 
PR21327(int a,int b)2124 void PR21327(int a, int b) {
2125   static_assert(&a + 1 != &b, ""); // expected-error {{constant expression}}
2126 }
2127 
2128 namespace EmptyClass {
2129   struct E1 {} e1;
2130   union E2 {} e2; // expected-note {{here}}
2131   struct E3 : E1 {} e3;
2132 
2133   // The defaulted copy constructor for an empty class does not read any
2134   // members. The defaulted copy constructor for an empty union reads the
2135   // object representation.
2136   constexpr E1 e1b(e1);
2137   constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}
2138   constexpr E3 e3b(e3);
2139 }
2140 
2141 namespace PR21786 {
2142   extern void (*start[])();
2143   extern void (*end[])();
2144   static_assert(&start != &end, ""); // expected-error {{constant expression}}
2145   static_assert(&start != nullptr, "");
2146 
2147   struct Foo;
2148   struct Bar {
2149     static const Foo x;
2150     static const Foo y;
2151   };
2152   static_assert(&Bar::x != nullptr, "");
2153   static_assert(&Bar::x != &Bar::y, "");
2154 }
2155 
2156 namespace PR21859 {
Fun()2157   constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}}
2158   constexpr int Var = Fun();
2159 }
2160 
2161 struct InvalidRedef {
2162   int f; // expected-note{{previous definition is here}}
2163   constexpr int f(void); // expected-error{{redefinition of 'f'}} expected-warning{{will not be implicitly 'const'}}
2164 };
2165 
2166 namespace PR17938 {
f(T const & x)2167   template <typename T> constexpr T const &f(T const &x) { return x; }
2168 
2169   struct X {};
2170   struct Y : X {};
ZPR17938::Z2171   struct Z : Y { constexpr Z() {} };
2172 
2173   static constexpr auto z = f(Z());
2174 }
2175 
2176 namespace PR24597 {
2177   struct A {
2178     int x, *p;
APR24597::A2179     constexpr A() : x(0), p(&x) {}
APR24597::A2180     constexpr A(const A &a) : x(a.x), p(&x) {}
2181   };
f()2182   constexpr A f() { return A(); }
g()2183   constexpr A g() { return f(); }
2184   constexpr int a = *f().p;
2185   constexpr int b = *g().p;
2186 }
2187 
2188 namespace IncompleteClass {
2189   struct XX {
fIncompleteClass::XX2190     static constexpr int f(XX*) { return 1; } // expected-note {{here}}
g(XX *)2191     friend constexpr int g(XX*) { return 2; } // expected-note {{here}}
2192 
2193     static constexpr int i = f(static_cast<XX*>(nullptr)); // expected-error {{constexpr variable 'i' must be initialized by a constant expression}}  expected-note {{undefined function 'f' cannot be used in a constant expression}}
2194     static constexpr int j = g(static_cast<XX*>(nullptr)); // expected-error {{constexpr variable 'j' must be initialized by a constant expression}}  expected-note {{undefined function 'g' cannot be used in a constant expression}}
2195   };
2196 }
2197 
2198 namespace InheritedCtor {
AInheritedCtor::A2199   struct A { constexpr A(int) {} };
2200 
2201   struct B : A { int n; using A::A; }; // expected-note {{here}}
2202   constexpr B b(0); // expected-error {{constant expression}} expected-note {{derived class}}
2203 
2204   struct C : A { using A::A; struct { union { int n, m = 0; }; union { int a = 0; }; int k = 0; }; struct {}; union {}; }; // expected-warning 6{{}}
2205   constexpr C c(0);
2206 
2207   struct D : A {
2208     using A::A; // expected-note {{here}}
2209     struct { // expected-warning {{extension}}
2210       union { // expected-warning {{extension}}
2211         int n;
2212       };
2213     };
2214   };
2215   constexpr D d(0); // expected-error {{constant expression}} expected-note {{derived class}}
2216 
2217   struct E : virtual A { using A::A; }; // expected-note {{here}}
2218   // We wrap a function around this to avoid implicit zero-initialization
2219   // happening first; the zero-initialization step would produce the same
2220   // error and defeat the point of this test.
f()2221   void f() {
2222     constexpr E e(0); // expected-error {{constant expression}} expected-note {{derived class}}
2223   }
2224   // FIXME: This produces a note with no source location.
2225   //constexpr E e(0);
2226 
WInheritedCtor::W2227   struct W { constexpr W(int n) : w(n) {} int w; };
2228   struct X : W { using W::W; int x = 2; };
2229   struct Y : X { using X::X; int y = 3; };
2230   struct Z : Y { using Y::Y; int z = 4; };
2231   constexpr Z z(1);
2232   static_assert(z.w == 1 && z.x == 2 && z.y == 3 && z.z == 4, "");
2233 }
2234 
2235 
2236 namespace PR28366 {
2237 namespace ns1 {
2238 
f(char c)2239 void f(char c) { //expected-note2{{declared here}}
2240   struct X {
2241     static constexpr char f() { //expected-error{{never produces a constant expression}}
2242       return c; //expected-error{{reference to local}} expected-note{{non-const variable}}
2243     }
2244   };
2245   int I = X::f();
2246 }
2247 
g()2248 void g() {
2249   const int c = 'c';
2250   static const int d = 'd';
2251   struct X {
2252     static constexpr int f() {
2253       return c + d;
2254     }
2255   };
2256   static_assert(X::f() == 'c' + 'd',"");
2257 }
2258 
2259 
2260 } // end ns1
2261 
2262 } //end ns PR28366
2263 
2264 namespace PointerArithmeticOverflow {
2265   int n;
2266   int a[1];
2267   constexpr int *b = &n + 1 + (long)-1;
2268   constexpr int *c = &n + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2269   constexpr int *d = &n + 1 - (unsigned long)1;
2270   constexpr int *e = a + 1 + (long)-1;
2271   constexpr int *f = a + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2272   constexpr int *g = a + 1 - (unsigned long)1;
2273 
2274   constexpr int *p = (&n + 1) + (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2275   constexpr int *q = (&n + 1) - (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element -3402}}
2276   constexpr int *r = &(&n + 1)[(unsigned __int128)-1]; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2277 }
2278 
2279 namespace PR40430 {
2280   struct S {
2281     char c[10] = "asdf";
fooPR40430::S2282     constexpr char foo() const { return c[3]; }
2283   };
2284   static_assert(S().foo() == 'f', "");
2285 }
2286 
2287 namespace PR41854 {
2288   struct e { operator int(); };
2289   struct f { e c; };
2290   int a;
2291   f &d = reinterpret_cast<f&>(a);
2292   unsigned b = d.c;
2293 }
2294