[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / AST / ByteCode / records.cpp
blob2eeaafc04c516cb6eaa6b41396c1b0df06d87f57
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++14 -verify=expected,both %s
2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=expected,both %s
3 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -triple i686 -verify=expected,both %s
4 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected,both %s
5 // RUN: %clang_cc1 -verify=ref,both -std=c++14 %s
6 // RUN: %clang_cc1 -verify=ref,both -std=c++17 %s
7 // RUN: %clang_cc1 -verify=ref,both -std=c++17 -triple i686 %s
8 // RUN: %clang_cc1 -verify=ref,both -std=c++20 %s
10 /// Used to crash.
11 struct Empty {};
12 constexpr Empty e = {Empty()};
14 struct BoolPair {
15 bool first;
16 bool second;
19 struct Ints {
20 int a = 20;
21 int b = 30;
22 bool c = true;
23 BoolPair bp = {true, false};
24 int numbers[3] = {1,2,3};
26 static const int five = 5;
27 static constexpr int getFive() {
28 return five;
31 constexpr int getTen() const {
32 return 10;
36 static_assert(Ints::getFive() == 5, "");
38 constexpr Ints ints;
39 static_assert(ints.a == 20, "");
40 static_assert(ints.b == 30, "");
41 static_assert(ints.c, "");
42 static_assert(ints.getTen() == 10, "");
43 static_assert(ints.numbers[0] == 1, "");
44 static_assert(ints.numbers[1] == 2, "");
45 static_assert(ints.numbers[2] == 3, "");
47 constexpr const BoolPair &BP = ints.bp;
48 static_assert(BP.first, "");
49 static_assert(!BP.second, "");
50 static_assert(ints.bp.first, "");
51 static_assert(!ints.bp.second, "");
54 constexpr Ints ints2{-20, -30, false};
55 static_assert(ints2.a == -20, "");
56 static_assert(ints2.b == -30, "");
57 static_assert(!ints2.c, "");
59 constexpr Ints getInts() {
60 return {64, 128, true};
62 constexpr Ints ints3 = getInts();
63 static_assert(ints3.a == 64, "");
64 static_assert(ints3.b == 128, "");
65 static_assert(ints3.c, "");
67 constexpr Ints ints4 = {
68 .a = 40 * 50,
69 .b = 0,
70 .c = (ints.a > 0),
73 static_assert(ints4.a == (40 * 50), "");
74 static_assert(ints4.b == 0, "");
75 static_assert(ints4.c, "");
76 static_assert(ints4.numbers[0] == 1, "");
77 static_assert(ints4.numbers[1] == 2, "");
78 static_assert(ints4.numbers[2] == 3, "");
80 constexpr Ints ints5 = ints4;
81 static_assert(ints5.a == (40 * 50), "");
82 static_assert(ints5.b == 0, "");
83 static_assert(ints5.c, "");
84 static_assert(ints5.numbers[0] == 1, "");
85 static_assert(ints5.numbers[1] == 2, "");
86 static_assert(ints5.numbers[2] == 3, "");
89 struct Ints2 {
90 int a = 10;
91 int b;
93 constexpr Ints2 ints22; // both-error {{without a user-provided default constructor}}
95 constexpr Ints2 I2 = Ints2{12, 25};
96 static_assert(I2.a == 12, "");
97 static_assert(I2.b == 25, "");
99 class C {
100 public:
101 int a;
102 int b;
104 constexpr C() : a(100), b(200) {}
106 constexpr C get() const {
107 return *this;
111 constexpr C c;
112 static_assert(c.a == 100, "");
113 static_assert(c.b == 200, "");
115 constexpr C c2 = C().get();
116 static_assert(c2.a == 100, "");
117 static_assert(c2.b == 200, "");
120 /// A global, composite temporary variable.
121 constexpr const C &c3 = C().get();
123 /// Same, but with a bitfield.
124 class D {
125 public:
126 unsigned a : 4;
127 constexpr D() : a(15) {}
128 constexpr D get() const {
129 return *this;
132 constexpr const D &d4 = D().get();
134 constexpr int getB() {
135 C c;
136 int &j = c.b;
138 j = j * 2;
140 return c.b;
142 static_assert(getB() == 400, "");
144 constexpr int getA(const C &c) {
145 return c.a;
147 static_assert(getA(c) == 100, "");
149 constexpr const C* getPointer() {
150 return &c;
152 static_assert(getPointer()->a == 100, "");
154 constexpr C RVOAndParams(const C *c) {
155 return C();
157 constexpr C RVOAndParamsResult = RVOAndParams(&c);
159 /// Parameter and return value have different types.
160 constexpr C RVOAndParams(int a) {
161 return C();
163 constexpr C RVOAndParamsResult2 = RVOAndParams(12);
165 class Bar { // both-note {{definition of 'Bar' is not complete}}
166 public:
167 constexpr Bar(){}
168 constexpr Bar b; // both-error {{cannot be constexpr}} \
169 // both-error {{has incomplete type 'const Bar'}}
171 constexpr Bar B; // both-error {{must be initialized by a constant expression}}
172 constexpr Bar *pb = nullptr;
174 constexpr int locals() {
175 C c;
176 c.a = 10;
178 // Assignment, not an initializer.
179 c = C();
180 c.a = 10;
183 // Assignment, not an initializer.
184 c = RVOAndParams(&c);
186 return c.a;
188 static_assert(locals() == 100, "");
190 namespace thisPointer {
191 struct S {
192 constexpr int get12() { return 12; }
195 constexpr int foo() { // both-error {{never produces a constant expression}}
196 S *s = nullptr;
197 return s->get12(); // both-note 2{{member call on dereferenced null pointer}}
200 static_assert(foo() == 12, ""); // both-error {{not an integral constant expression}} \
201 // both-note {{in call to 'foo()'}}
204 struct FourBoolPairs {
205 BoolPair v[4] = {
206 {false, false},
207 {false, true},
208 {true, false},
209 {true, true },
212 // Init
213 constexpr FourBoolPairs LT;
214 // Copy ctor
215 constexpr FourBoolPairs LT2 = LT;
216 static_assert(LT2.v[0].first == false, "");
217 static_assert(LT2.v[0].second == false, "");
218 static_assert(LT2.v[2].first == true, "");
219 static_assert(LT2.v[2].second == false, "");
221 class Base {
222 public:
223 int i;
224 constexpr Base() : i(10) {}
225 constexpr Base(int i) : i(i) {}
228 class A : public Base {
229 public:
230 constexpr A() : Base(100) {}
231 constexpr A(int a) : Base(a) {}
233 constexpr A a{};
234 static_assert(a.i == 100, "");
235 constexpr A a2{12};
236 static_assert(a2.i == 12, "");
237 static_assert(a2.i == 200, ""); // both-error {{static assertion failed}} \
238 // both-note {{evaluates to '12 == 200'}}
241 struct S {
242 int a = 0;
243 constexpr int get5() const { return 5; }
244 constexpr void fo() const {
245 this; // both-warning {{expression result unused}}
246 this->a; // both-warning {{expression result unused}}
247 get5();
248 getInts();
251 constexpr int m() const {
252 fo();
253 return 1;
256 constexpr S s;
257 static_assert(s.m() == 1, "");
259 namespace InitializerTemporaries {
260 class Bar {
261 private:
262 int a;
264 public:
265 constexpr Bar() : a(10) {}
266 constexpr int getA() const { return a; }
269 class Foo {
270 public:
271 int a;
273 constexpr Foo() : a(Bar().getA()) {}
275 constexpr Foo F;
276 static_assert(F.a == 10, "");
279 /// Needs constexpr destructors.
280 #if __cplusplus >= 202002L
281 /// Does
282 /// Arr[Pos] = Value;
283 /// ++Pos;
284 /// in its destructor.
285 class BitSetter {
286 private:
287 int *Arr;
288 int &Pos;
289 int Value;
291 public:
292 constexpr BitSetter(int *Arr, int &Pos, int Value) :
293 Arr(Arr), Pos(Pos), Value(Value) {}
295 constexpr int getValue() const { return 0; }
296 constexpr ~BitSetter() {
297 Arr[Pos] = Value;
298 ++Pos;
302 class Test {
303 int a, b, c;
304 public:
305 constexpr Test(int *Arr, int &Pos) :
306 a(BitSetter(Arr, Pos, 1).getValue()),
307 b(BitSetter(Arr, Pos, 2).getValue()),
308 c(BitSetter(Arr, Pos, 3).getValue())
313 constexpr int T(int Index) {
314 int Arr[] = {0, 0, 0};
315 int Pos = 0;
318 Test(Arr, Pos);
319 // End of scope, should destroy Test.
322 return Arr[Index];
324 static_assert(T(0) == 1);
325 static_assert(T(1) == 2);
326 static_assert(T(2) == 3);
328 // Invalid destructor.
329 struct S {
330 constexpr S() {}
331 constexpr ~S() noexcept(false) { throw 12; } // both-error {{cannot use 'throw'}} \
332 // both-error {{never produces a constant expression}} \
333 // both-note 2{{subexpression not valid}}
336 constexpr int f() {
337 S{}; // both-note {{in call to 'S{}.~S()'}}
338 return 12;
340 static_assert(f() == 12); // both-error {{not an integral constant expression}} \
341 // both-note {{in call to 'f()'}}
344 #endif
347 #if __cplusplus >= 201703L
348 namespace BaseInit {
349 class _A {public: int a;};
350 class _B : public _A {};
351 class _C : public _B {};
353 constexpr _C c{12};
354 constexpr const _B &b = c;
355 static_assert(b.a == 12);
357 class A {public: int a;};
358 class B : public A {};
359 class C : public A {};
360 class D : public B, public C {};
362 // This initializes D::B::A::a and not D::C::A::a.
363 constexpr D d{12};
364 static_assert(d.B::a == 12);
365 static_assert(d.C::a == 0);
367 #endif
369 namespace MI {
370 class A {
371 public:
372 int a;
373 constexpr A(int a) : a(a) {}
376 class B {
377 public:
378 int b;
379 constexpr B(int b) : b(b) {}
382 class C : public A, public B {
383 public:
384 constexpr C() : A(10), B(20) {}
386 constexpr C c = {};
387 static_assert(c.a == 10, "");
388 static_assert(c.b == 20, "");
390 constexpr const A *aPointer = &c;
391 constexpr const B *bPointer = &c;
393 class D : private A, private B {
394 public:
395 constexpr D() : A(20), B(30) {}
396 constexpr int getA() const { return a; }
397 constexpr int getB() const { return b; }
399 constexpr D d = {};
400 static_assert(d.getA() == 20, "");
401 static_assert(d.getB() == 30, "");
404 namespace DeriveFailures {
405 #if __cplusplus < 202002L
406 struct Base { // both-note {{declared here}} \
407 // ref-note {{declared here}}
408 int Val;
411 struct Derived : Base {
412 int OtherVal;
414 constexpr Derived(int i) : OtherVal(i) {} // ref-error {{never produces a constant expression}} \
415 // both-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}} \
416 // ref-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}}
419 constexpr Derived D(12); // both-error {{must be initialized by a constant expression}} \
420 // both-note {{in call to 'Derived(12)'}} \
421 // both-note {{declared here}}
423 static_assert(D.Val == 0, ""); // both-error {{not an integral constant expression}} \
424 // both-note {{initializer of 'D' is not a constant expression}}
425 #endif
427 struct AnotherBase {
428 int Val;
429 constexpr AnotherBase(int i) : Val(12 / i) {} // both-note {{division by zero}}
432 struct AnotherDerived : AnotherBase {
433 constexpr AnotherDerived(int i) : AnotherBase(i) {}
435 constexpr AnotherBase Derp(0); // both-error {{must be initialized by a constant expression}} \
436 // both-note {{in call to 'AnotherBase(0)'}}
438 struct YetAnotherBase {
439 int Val;
440 constexpr YetAnotherBase(int i) : Val(i) {}
443 struct YetAnotherDerived : YetAnotherBase {
444 using YetAnotherBase::YetAnotherBase; // both-note {{declared here}}
445 int OtherVal;
447 constexpr bool doit() const { return Val == OtherVal; }
450 constexpr YetAnotherDerived Oops(0); // both-error {{must be initialized by a constant expression}} \
451 // both-note {{constructor inherited from base class 'YetAnotherBase' cannot be used in a constant expression}}
454 namespace EmptyCtor {
455 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
456 constexpr piecewise_construct_t piecewise_construct =
457 piecewise_construct_t();
460 namespace ConditionalInit {
461 struct S { int a; };
463 constexpr S getS(bool b) {
464 return b ? S{12} : S{13};
467 static_assert(getS(true).a == 12, "");
468 static_assert(getS(false).a == 13, "");
470 namespace DeclRefs {
471 struct A{ int m; const int &f = m; };
473 constexpr A a{10};
474 static_assert(a.m == 10, "");
475 static_assert(a.f == 10, "");
477 class Foo {
478 public:
479 int z = 1337;
480 constexpr int a() const {
481 A b{this->z};
483 return b.f;
486 constexpr Foo f;
487 static_assert(f.a() == 1337, "");
490 struct B {
491 A a = A{100};
493 constexpr B b;
494 static_assert(b.a.m == 100, "");
495 static_assert(b.a.f == 100, "");
497 constexpr B b2{};
498 static_assert(b2.a.m == 100, "");
499 static_assert(b2.a.f == 100, "");
500 static_assert(b2.a.f == 101, ""); // both-error {{failed}} \
501 // both-note {{evaluates to '100 == 101'}}
504 namespace PointerArith {
505 struct A {};
506 struct B : A { int n; };
508 B b = {};
509 constexpr A *a1 = &b;
510 constexpr B *b1 = &b + 1;
511 constexpr B *b2 = &b + 0;
513 constexpr A *a2 = &b + 1; // both-error {{must be initialized by a constant expression}} \
514 // both-note {{cannot access base class of pointer past the end of object}}
515 constexpr const int *pn = &(&b + 1)->n; // both-error {{must be initialized by a constant expression}} \
516 // both-note {{cannot access field of pointer past the end of object}}
519 #if __cplusplus >= 202002L
520 namespace VirtualCalls {
521 namespace Obvious {
523 class A {
524 public:
525 constexpr A(){}
526 constexpr virtual int foo() {
527 return 3;
530 class B : public A {
531 public:
532 constexpr int foo() override {
533 return 6;
537 constexpr int getFooB(bool b) {
538 A *a;
539 A myA;
540 B myB;
542 if (b)
543 a = &myA;
544 else
545 a = &myB;
547 return a->foo();
549 static_assert(getFooB(true) == 3, "");
550 static_assert(getFooB(false) == 6, "");
553 namespace MultipleBases {
554 class A {
555 public:
556 constexpr virtual int getInt() const { return 10; }
558 class B {
559 public:
561 class C : public A, public B {
562 public:
563 constexpr int getInt() const override { return 20; }
566 constexpr int callGetInt(const A& a) { return a.getInt(); }
567 static_assert(callGetInt(C()) == 20, "");
568 static_assert(callGetInt(A()) == 10, "");
571 namespace Destructors {
572 class Base {
573 public:
574 int i;
575 constexpr Base(int &i) : i(i) {i++;}
576 constexpr virtual ~Base() {i--;}
579 class Derived : public Base {
580 public:
581 constexpr Derived(int &i) : Base(i) {}
582 constexpr virtual ~Derived() {i--;}
585 constexpr int test() {
586 int i = 0;
587 Derived d(i);
588 return i;
590 static_assert(test() == 1);
592 struct S {
593 constexpr S() {}
594 constexpr ~S() { // both-error {{never produces a constant expression}}
595 int i = 1 / 0; // both-warning {{division by zero}} \
596 // both-note 2{{division by zero}}
599 constexpr int testS() {
600 S{}; // both-note {{in call to 'S{}.~S()'}}
601 return 1;
603 static_assert(testS() == 1); // both-error {{not an integral constant expression}} \
604 // both-note {{in call to 'testS()'}}
607 namespace BaseToDerived {
608 namespace A {
609 struct A {};
610 struct B : A { int n; };
611 struct C : B {};
612 C c = {};
613 constexpr C *pb = (C*)((A*)&c + 1); // both-error {{must be initialized by a constant expression}} \
614 // both-note {{cannot access derived class of pointer past the end of object}}
616 namespace B {
617 struct A {};
618 struct Z {};
619 struct B : Z, A {
620 int n;
621 constexpr B() : n(10) {}
623 struct C : B {
624 constexpr C() : B() {}
627 constexpr C c = {};
628 constexpr const A *pa = &c;
629 constexpr const C *cp = (C*)pa;
630 constexpr const B *cb = (B*)cp;
632 static_assert(cb->n == 10);
633 static_assert(cp->n == 10);
636 namespace C {
637 struct Base { int *a; };
638 struct Base2 : Base { int f[12]; };
640 struct Middle1 { int b[3]; };
641 struct Middle2 : Base2 { char c; };
642 struct Middle3 : Middle2 { char g[3]; };
643 struct Middle4 { int f[3]; };
644 struct Middle5 : Middle4, Middle3 { char g2[3]; };
646 struct NotQuiteDerived : Middle1, Middle5 { bool d; };
647 struct Derived : NotQuiteDerived { int e; };
649 constexpr NotQuiteDerived NQD1 = {};
651 constexpr Middle5 *M4 = (Middle5*)((Base2*)&NQD1);
652 static_assert(M4->a == nullptr);
653 static_assert(M4->g2[0] == 0);
658 namespace VirtualDtors {
659 class A {
660 public:
661 unsigned &v;
662 constexpr A(unsigned &v) : v(v) {}
663 constexpr virtual ~A() {
664 v |= (1 << 0);
667 class B : public A {
668 public:
669 constexpr B(unsigned &v) : A(v) {}
670 constexpr virtual ~B() {
671 v |= (1 << 1);
674 class C : public B {
675 public:
676 constexpr C(unsigned &v) : B(v) {}
677 constexpr virtual ~C() {
678 v |= (1 << 2);
682 constexpr bool foo() {
683 unsigned a = 0;
685 C c(a);
687 return ((a & (1 << 0)) && (a & (1 << 1)) && (a & (1 << 2)));
690 static_assert(foo());
693 namespace QualifiedCalls {
694 class A {
695 public:
696 constexpr virtual int foo() const {
697 return 5;
700 class B : public A {};
701 class C : public B {
702 public:
703 constexpr int foo() const override {
704 return B::foo(); // B doesn't have a foo(), so this should call A::foo().
706 constexpr int foo2() const {
707 return this->A::foo();
710 constexpr C c;
711 static_assert(c.foo() == 5);
712 static_assert(c.foo2() == 5);
715 struct S {
716 int _c = 0;
717 virtual constexpr int foo() const { return 1; }
720 struct SS : S {
721 int a;
722 constexpr SS() {
723 a = S::foo();
725 constexpr int foo() const override {
726 return S::foo();
730 constexpr SS ss;
731 static_assert(ss.a == 1);
734 namespace CtorDtor {
735 struct Base {
736 int i = 0;
737 int j = 0;
739 constexpr Base() : i(func()) {
740 j = func();
742 constexpr Base(int i) : i(i), j(i) {}
744 constexpr virtual int func() const { return 1; }
747 struct Derived : Base {
748 constexpr Derived() {}
749 constexpr Derived(int i) : Base(i) {}
750 constexpr int func() const override { return 2; }
753 struct Derived2 : Derived {
754 constexpr Derived2() : Derived(func()) {} // ref-note {{subexpression not valid in a constant expression}}
755 constexpr int func() const override { return 3; }
758 constexpr Base B;
759 static_assert(B.i == 1 && B.j == 1, "");
761 constexpr Derived D;
762 static_assert(D.i == 1, ""); // expected-error {{static assertion failed}} \
763 // expected-note {{2 == 1}}
764 static_assert(D.j == 1, ""); // expected-error {{static assertion failed}} \
765 // expected-note {{2 == 1}}
767 constexpr Derived2 D2; // ref-error {{must be initialized by a constant expression}} \
768 // ref-note {{in call to 'Derived2()'}} \
769 // ref-note 2{{declared here}}
770 static_assert(D2.i == 3, ""); // ref-error {{not an integral constant expression}} \
771 // ref-note {{initializer of 'D2' is not a constant expression}}
772 static_assert(D2.j == 3, ""); // ref-error {{not an integral constant expression}} \
773 // ref-note {{initializer of 'D2' is not a constant expression}}
777 namespace VirtualFunctionPointers {
778 struct S {
779 virtual constexpr int func() const { return 1; }
782 struct Middle : S {
783 constexpr Middle(int i) : i(i) {}
784 int i;
787 struct Other {
788 constexpr Other(int k) : k(k) {}
789 int k;
792 struct S2 : Middle, Other {
793 int j;
794 constexpr S2(int i, int j, int k) : Middle(i), Other(k), j(j) {}
795 virtual constexpr int func() const { return i + j + k + S::func(); }
798 constexpr S s;
799 constexpr decltype(&S::func) foo = &S::func;
800 constexpr int value = (s.*foo)();
801 static_assert(value == 1);
804 constexpr S2 s2(1, 2, 3);
805 static_assert(s2.i == 1);
806 static_assert(s2.j == 2);
807 static_assert(s2.k == 3);
809 constexpr int value2 = s2.func();
810 constexpr int value3 = (s2.*foo)();
811 static_assert(value3 == 7);
813 constexpr int dynamicDispatch(const S &s) {
814 constexpr decltype(&S::func) SFunc = &S::func;
816 return (s.*SFunc)();
819 static_assert(dynamicDispatch(s) == 1);
820 static_assert(dynamicDispatch(s2) == 7);
824 #endif
826 #if __cplusplus < 202002L
827 namespace VirtualFromBase {
828 struct S1 {
829 virtual int f() const;
831 struct S2 {
832 virtual int f();
834 template <typename T> struct X : T {
835 constexpr X() {}
836 double d = 0.0;
837 constexpr int f() { return sizeof(T); }
840 // Non-virtual f(), OK.
841 constexpr X<X<S1>> xxs1;
842 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
843 static_assert(p->f() == sizeof(S1), "");
845 // Virtual f(), not OK.
846 constexpr X<X<S2>> xxs2;
847 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
848 static_assert(q->f() == sizeof(X<S2>), ""); // both-error {{not an integral constant expression}} \
849 // both-note {{cannot evaluate call to virtual function}}
851 #endif
853 namespace CompositeDefaultArgs {
854 struct Foo {
855 int a;
856 int b;
857 constexpr Foo() : a(12), b(13) {}
860 class Bar {
861 public:
862 bool B = false;
864 constexpr int someFunc(Foo F = Foo()) {
865 this->B = true;
866 return 5;
870 constexpr bool testMe() {
871 Bar B;
872 B.someFunc();
873 return B.B;
875 static_assert(testMe(), "");
878 constexpr bool BPand(BoolPair bp) {
879 return bp.first && bp.second;
881 static_assert(BPand(BoolPair{true, false}) == false, "");
883 namespace TemporaryObjectExpr {
884 struct F {
885 int a;
886 constexpr F() : a(12) {}
888 constexpr int foo(F f) {
889 return 0;
891 static_assert(foo(F()) == 0, "");
894 namespace ZeroInit {
895 struct F {
896 int a;
899 namespace Simple {
900 struct A {
901 char a;
902 bool b;
903 int c[4];
904 float d;
906 constexpr int foo(A x) {
907 return x.a + static_cast<int>(x.b) + x.c[0] + x.c[3] + static_cast<int>(x.d);
909 static_assert(foo(A()) == 0, "");
912 namespace Inheritance {
913 struct F2 : F {
914 float f;
917 constexpr int foo(F2 f) {
918 return (int)f.f + f.a;
920 static_assert(foo(F2()) == 0, "");
923 namespace BitFields {
924 struct F {
925 unsigned a : 6;
927 constexpr int foo(F f) {
928 return f.a;
930 static_assert(foo(F()) == 0, "");
933 namespace Nested {
934 struct F2 {
935 float f;
936 char c;
939 struct F {
940 F2 f2;
941 int i;
944 constexpr int foo(F f) {
945 return f.i + f.f2.f + f.f2.c;
947 static_assert(foo(F()) == 0, "");
950 namespace CompositeArrays {
951 struct F2 {
952 float f;
953 char c;
956 struct F {
957 F2 f2[2];
958 int i;
961 constexpr int foo(F f) {
962 return f.i + f.f2[0].f + f.f2[0].c + f.f2[1].f + f.f2[1].c;
964 static_assert(foo(F()) == 0, "");
967 #if __cplusplus > 201402L
968 namespace Unions {
969 struct F {
970 union {
971 int a;
972 char c[4];
973 float f;
974 } U;
975 int i;
978 constexpr int foo(F f) {
979 return f.i + f.U.f; // both-note {{read of member 'f' of union with active member 'a'}}
981 static_assert(foo(F()) == 0, ""); // both-error {{not an integral constant expression}} \
982 // both-note {{in call to}}
984 #endif
986 #if __cplusplus >= 202002L
987 namespace Failure {
988 struct S {
989 int a;
990 F f{12};
992 constexpr int foo(S x) {
993 return x.a;
995 static_assert(foo(S()) == 0, "");
997 #endif
1000 #if __cplusplus >= 202002L
1001 namespace ParenInit {
1002 struct A {
1003 int a;
1006 struct B : A {
1007 int b;
1010 constexpr B b(A(1),2);
1013 struct O {
1014 int &&j;
1017 /// Not constexpr!
1018 O o1(0);
1019 constinit O o2(0); // both-error {{variable does not have a constant initializer}} \
1020 // both-note {{required by 'constinit' specifier}} \
1021 // both-note {{reference to temporary is not a constant expression}} \
1022 // both-note {{temporary created here}}
1025 /// Initializing an array.
1026 constexpr void bar(int i, int j) {
1027 int arr[4](i, j);
1030 #endif
1032 namespace DelegatingConstructors {
1033 struct S {
1034 int a;
1035 constexpr S() : S(10) {}
1036 constexpr S(int a) : a(a) {}
1038 constexpr S s = {};
1039 static_assert(s.a == 10, "");
1041 struct B {
1042 int a;
1043 int b;
1045 constexpr B(int a) : a(a), b(a + 2) {}
1047 struct A : B {
1048 constexpr A() : B(10) {};
1050 constexpr A d4 = {};
1051 static_assert(d4.a == 10, "");
1052 static_assert(d4.b == 12, "");
1055 namespace AccessOnNullptr {
1056 struct F {
1057 int a;
1060 constexpr int a() { // both-error {{never produces a constant expression}}
1061 F *f = nullptr;
1063 f->a = 0; // both-note 2{{cannot access field of null pointer}}
1064 return f->a;
1066 static_assert(a() == 0, ""); // both-error {{not an integral constant expression}} \
1067 // both-note {{in call to 'a()'}}
1069 constexpr int a2() { // both-error {{never produces a constant expression}}
1070 F *f = nullptr;
1073 const int *a = &(f->a); // both-note 2{{cannot access field of null pointer}}
1074 return f->a;
1076 static_assert(a2() == 0, ""); // both-error {{not an integral constant expression}} \
1077 // both-note {{in call to 'a2()'}}
1080 namespace IndirectFieldInit {
1081 #if __cplusplus >= 202002L
1082 /// Primitive.
1083 struct Nested1 {
1084 struct {
1085 int first;
1087 int x;
1088 constexpr Nested1(int x) : first(12), x() { x = 4; }
1089 constexpr Nested1() : Nested1(42) {}
1091 constexpr Nested1 N1{};
1092 static_assert(N1.first == 12, "");
1094 /// Composite.
1095 struct Nested2 {
1096 struct First { int x = 42; };
1097 struct {
1098 First first;
1100 int x;
1101 constexpr Nested2(int x) : first(12), x() { x = 4; }
1102 constexpr Nested2() : Nested2(42) {}
1104 constexpr Nested2 N2{};
1105 static_assert(N2.first.x == 12, "");
1107 /// Bitfield.
1108 struct Nested3 {
1109 struct {
1110 unsigned first : 2;
1112 int x;
1113 constexpr Nested3(int x) : first(3), x() { x = 4; }
1114 constexpr Nested3() : Nested3(42) {}
1117 constexpr Nested3 N3{};
1118 static_assert(N3.first == 3, "");
1120 /// Test that we get the offset right if the
1121 /// record has a base.
1122 struct Nested4Base {
1123 int a;
1124 int b;
1125 char c;
1127 struct Nested4 : Nested4Base{
1128 struct {
1129 int first;
1131 int x;
1132 constexpr Nested4(int x) : first(123), x() { a = 1; b = 2; c = 3; x = 4; }
1133 constexpr Nested4() : Nested4(42) {}
1135 constexpr Nested4 N4{};
1136 static_assert(N4.first == 123, "");
1138 struct S {
1139 struct {
1140 int x, y;
1143 constexpr S(int x_, int y_) : x(x_), y(y_) {}
1146 constexpr S s(1, 2);
1147 static_assert(s.x == 1 && s.y == 2);
1149 struct S2 {
1150 int a;
1151 struct {
1152 int b;
1153 struct {
1154 int x, y;
1158 constexpr S2(int x_, int y_) : a(3), b(4), x(x_), y(y_) {}
1161 constexpr S2 s2(1, 2);
1162 static_assert(s2.x == 1 && s2.y == 2 && s2.a == 3 && s2.b == 4);
1164 #endif
1167 namespace InheritedConstructor {
1168 namespace PR47555 {
1169 struct A {
1170 int c;
1171 int d;
1172 constexpr A(int c, int d) : c(c), d(d){}
1174 struct B : A { using A::A; };
1176 constexpr B b = {13, 1};
1177 static_assert(b.c == 13, "");
1178 static_assert(b.d == 1, "");
1181 namespace PR47555_2 {
1182 struct A {
1183 int c;
1184 int d;
1185 double e;
1186 constexpr A(int c, int &d, double e) : c(c), d(++d), e(e){}
1188 struct B : A { using A::A; };
1190 constexpr int f() {
1191 int a = 10;
1192 B b = {10, a, 40.0};
1193 return a;
1195 static_assert(f() == 11, "");
1198 namespace AaronsTest {
1199 struct T {
1200 constexpr T(float) {}
1203 struct Base {
1204 constexpr Base(T t = 1.0f) {}
1205 constexpr Base(float) {}
1208 struct FirstMiddle : Base {
1209 using Base::Base;
1210 constexpr FirstMiddle() : Base(2.0f) {}
1213 struct SecondMiddle : Base {
1214 constexpr SecondMiddle() : Base(3.0f) {}
1215 constexpr SecondMiddle(T t) : Base(t) {}
1218 struct S : FirstMiddle, SecondMiddle {
1219 using FirstMiddle::FirstMiddle;
1220 constexpr S(int i) : S(4.0f) {}
1223 constexpr S s(1);
1227 namespace InvalidCtorInitializer {
1228 struct X {
1229 int Y;
1230 constexpr X()
1231 : Y(fo_o_()) {} // both-error {{use of undeclared identifier 'fo_o_'}}
1233 // no crash on evaluating the constexpr ctor.
1234 constexpr int Z = X().Y; // both-error {{constexpr variable 'Z' must be initialized by a constant expression}}
1237 extern int f(); // both-note {{here}}
1238 struct HasNonConstExprMemInit {
1239 int x = f(); // both-note {{non-constexpr function}}
1240 constexpr HasNonConstExprMemInit() {} // both-error {{never produces a constant expression}}
1243 namespace {
1244 template <class Tp, Tp v>
1245 struct integral_constant {
1246 static const Tp value = v;
1249 template <class Tp, Tp v>
1250 const Tp integral_constant<Tp, v>::value;
1252 typedef integral_constant<bool, true> true_type;
1253 typedef integral_constant<bool, false> false_type;
1255 /// This might look innocent, but we get an evaluateAsInitializer call for the
1256 /// static bool member before evaluating the first static_assert, but we do NOT
1257 /// get such a call for the second one. So the second one needs to lazily visit
1258 /// the data member itself.
1259 static_assert(true_type::value, "");
1260 static_assert(true_type::value, "");
1263 #if __cplusplus >= 202002L
1264 namespace {
1265 /// Used to crash because the CXXDefaultInitExpr is of compound type.
1266 struct A {
1267 int &x;
1268 constexpr ~A() { --x; }
1270 struct B {
1271 int &x;
1272 const A &a = A{x};
1274 constexpr int a() {
1275 int x = 1;
1276 int f = B{x}.x;
1277 B{x}; // both-warning {{expression result unused}}
1279 return 1;
1282 #endif
1284 namespace pr18633 {
1285 struct A1 {
1286 static const int sz;
1287 static const int sz2;
1289 const int A1::sz2 = 11;
1290 template<typename T>
1291 void func () {
1292 int arr[A1::sz];
1293 // both-warning@-1 {{variable length arrays in C++ are a Clang extension}}
1294 // both-note@-2 {{initializer of 'sz' is unknown}}
1295 // both-note@-9 {{declared here}}
1297 template<typename T>
1298 void func2 () {
1299 int arr[A1::sz2];
1301 const int A1::sz = 12;
1302 void func2() {
1303 func<int>();
1304 func2<int>();
1308 namespace {
1309 struct F {
1310 static constexpr int Z = 12;
1312 F f;
1313 static_assert(f.Z == 12, "");
1316 namespace UnnamedBitFields {
1317 struct A {
1318 int : 1;
1319 double f;
1320 int : 1;
1321 char c;
1324 constexpr A a = (A){1.0, 'a'};
1325 static_assert(a.f == 1.0, "");
1326 static_assert(a.c == 'a', "");
1329 namespace VirtualBases {
1330 /// This used to crash.
1331 namespace One {
1332 class A {
1333 protected:
1334 int x;
1336 class B : public virtual A {
1337 public:
1338 int getX() { return x; } // both-note {{declared here}}
1341 class DV : virtual public B{};
1343 void foo() {
1344 DV b;
1345 int a[b.getX()]; // both-warning {{variable length arrays}} \
1346 // both-note {{non-constexpr function 'getX' cannot be used}}
1350 namespace Two {
1351 struct U { int n; };
1352 struct A : virtual U { int n; };
1353 struct B : A {};
1354 B a;
1355 static_assert((U*)(A*)(&a) == (U*)(&a), "");
1357 struct C : virtual A {};
1358 struct D : B, C {};
1359 D d;
1360 constexpr B *p = &d;
1361 constexpr C *q = &d;
1362 static_assert((A*)p == (A*)q, ""); // both-error {{failed}}
1365 namespace Three {
1366 struct U { int n; };
1367 struct V : U { int n; };
1368 struct A : virtual V { int n; };
1369 struct Aa { int n; };
1370 struct B : virtual A, Aa {};
1372 struct C : virtual A, Aa {};
1374 struct D : B, C {};
1376 D d;
1378 constexpr B *p = &d;
1379 constexpr C *q = &d;
1381 static_assert((void*)p != (void*)q, "");
1382 static_assert((A*)p == (A*)q, "");
1383 static_assert((Aa*)p != (Aa*)q, "");
1385 constexpr V *v = p;
1386 constexpr V *w = q;
1387 constexpr V *x = (A*)p;
1388 static_assert(v == w, "");
1389 static_assert(v == x, "");
1391 static_assert((U*)&d == p, "");
1392 static_assert((U*)&d == q, "");
1393 static_assert((U*)&d == v, "");
1394 static_assert((U*)&d == w, "");
1395 static_assert((U*)&d == x, "");
1397 struct X {};
1398 struct Y1 : virtual X {};
1399 struct Y2 : X {};
1400 struct Z : Y1, Y2 {};
1401 Z z;
1402 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
1406 namespace ZeroInit {
1407 struct S3 {
1408 S3() = default;
1409 S3(const S3&) = default;
1410 S3(S3&&) = default;
1411 constexpr S3(int n) : n(n) {}
1412 int n;
1414 constexpr S3 s3d; // both-error {{default initialization of an object of const type 'const S3' without a user-provided default constructor}}
1415 static_assert(s3d.n == 0, "");
1417 struct P {
1418 int a = 10;
1420 static_assert(P().a == 10, "");
1423 namespace {
1424 #if __cplusplus >= 202002L
1425 struct C {
1426 template <unsigned N> constexpr C(const char (&)[N]) : n(N) {}
1427 unsigned n;
1429 template <C c>
1430 constexpr auto operator""_c() { return c.n; }
1432 constexpr auto waldo = "abc"_c;
1433 static_assert(waldo == 4, "");
1434 #endif
1438 namespace TemporaryWithInvalidDestructor {
1439 #if __cplusplus >= 202002L
1440 struct A {
1441 bool a = true;
1442 constexpr ~A() noexcept(false) { // both-error {{never produces a constant expression}}
1443 throw; // both-note 2{{not valid in a constant expression}} \
1444 // both-error {{cannot use 'throw' with exceptions disabled}}
1447 static_assert(A().a, ""); // both-error {{not an integral constant expression}} \
1448 // both-note {{in call to}}
1449 #endif
1452 namespace IgnoredCtorWithZeroInit {
1453 struct S {
1454 int a;
1457 bool get_status() {
1458 return (S(), true);
1462 #if __cplusplus >= 202002L
1463 namespace VirtOperator {
1464 /// This used to crash because it's a virtual CXXOperatorCallExpr.
1465 struct B {
1466 virtual constexpr bool operator==(const B&) const { return true; }
1468 struct D : B {
1469 constexpr bool operator==(const B&) const override{ return false; } // both-note {{operator}}
1471 constexpr bool cmp_base_derived = D() == D(); // both-warning {{ambiguous}}
1474 namespace FloatAPValue {
1475 struct ClassTemplateArg {
1476 int a;
1477 float f;
1479 template<ClassTemplateArg A> struct ClassTemplateArgTemplate {
1480 static constexpr const ClassTemplateArg &Arg = A;
1482 ClassTemplateArgTemplate<ClassTemplateArg{1, 2.0f}> ClassTemplateArgObj;
1483 template<const ClassTemplateArg&> struct ClassTemplateArgRefTemplate {};
1484 ClassTemplateArgRefTemplate<ClassTemplateArgObj.Arg> ClassTemplateArgRefObj;
1486 #endif
1488 namespace LocalWithThisPtrInit {
1489 struct S {
1490 int i;
1491 int *p = &i;
1493 constexpr int foo() {
1494 S s{2};
1495 return *s.p;
1497 static_assert(foo() == 2, "");
1500 namespace OnePastEndAndBack {
1501 struct Base {
1502 constexpr Base() {}
1503 int n = 0;
1506 constexpr Base a;
1507 constexpr const Base *c = &a + 1;
1508 constexpr const Base *d = c - 1;
1509 static_assert(d == &a, "");
1512 namespace BitSet {
1513 class Bitset {
1514 unsigned Bit = 0;
1516 public:
1517 constexpr Bitset() {
1518 int Init[2] = {1,2};
1519 for (auto I : Init)
1520 set(I);
1522 constexpr void set(unsigned I) {
1523 this->Bit++;
1524 this->Bit = 1u << 1;
1528 struct ArchInfo {
1529 Bitset DefaultExts;
1532 constexpr ArchInfo ARMV8A = {
1533 Bitset()
1537 namespace ArrayInitChain {
1538 struct StringLiteral {
1539 const char *S;
1542 struct CustomOperandVal {
1543 StringLiteral Str;
1544 unsigned Width;
1545 unsigned Mask = Width + 1;
1548 constexpr CustomOperandVal A[] = {
1550 {{"depctr_hold_cnt"}, 12, 13},
1552 static_assert(A[0].Str.S == nullptr, "");
1553 static_assert(A[0].Width == 0, "");
1554 static_assert(A[0].Mask == 1, "");
1556 static_assert(A[1].Width == 12, "");
1557 static_assert(A[1].Mask == 13, "");
1560 #if __cplusplus >= 202002L
1561 namespace ctorOverrider {
1562 // Ensure that we pick the right final overrider during construction.
1563 struct A {
1564 virtual constexpr char f() const { return 'A'; }
1565 char a = f();
1568 struct Covariant1 {
1569 A d;
1572 constexpr Covariant1 cb;
1574 #endif
1576 #if __cplusplus >= 202002L
1577 namespace VirtDtor {
1578 struct X { char *p; constexpr ~X() { *p++ = 'X'; } };
1579 struct Y : X { int y; virtual constexpr ~Y() { *p++ = 'Y'; } };
1580 struct Z : Y { int z; constexpr ~Z() override { *p++ = 'Z'; } };
1582 union VU {
1583 constexpr VU() : z() {}
1584 constexpr ~VU() {}
1585 Z z;
1588 constexpr char virt_dtor(int mode, const char *expected) {
1589 char buff[4] = {};
1590 VU vu;
1591 vu.z.p = buff;
1593 ((Y&)vu.z).~Y();
1594 return true;
1596 static_assert(virt_dtor(0, "ZYX"));
1599 namespace DtorDestroysFieldsAfterSelf {
1600 struct S {
1601 int a = 10;
1602 constexpr ~S() {
1603 a = 0;
1607 struct F {
1608 S s;
1609 int a;
1610 int &b;
1611 constexpr F(int a, int &b) : a(a), b(b) {}
1612 constexpr ~F() {
1613 b += s.a;
1617 constexpr int foo() {
1618 int a = 10;
1619 int b = 5;
1621 F f(a, b);
1624 return b;
1627 static_assert(foo() == 15);
1629 #endif
1631 namespace ExprWithCleanups {
1632 struct A { A(); ~A(); int get(); };
1633 constexpr int get() {return false ? A().get() : 1;}
1634 static_assert(get() == 1, "");
1637 struct S {
1638 int V;
1639 constexpr S(int V) : V(V) {}
1640 constexpr int get() {
1641 return V;
1644 constexpr int get(bool b) {
1645 S a = b ? S(1) : S(2);
1647 return a.get();
1649 static_assert(get(true) == 1, "");
1650 static_assert(get(false) == 2, "");
1653 constexpr auto F = true ? 1i : 2i;
1654 static_assert(F == 1i, "");
1657 namespace NullptrUpcast {
1658 struct A {};
1659 struct B : A { int n; };
1660 constexpr B *nb = nullptr;
1661 constexpr A &ra = *nb; // both-error {{constant expression}} \
1662 // both-note {{cannot access base class of null pointer}}
1665 namespace NonConst {
1666 template <int I>
1667 struct S {
1668 static constexpr int Size = I;
1669 constexpr int getSize() const { return I; }
1670 explicit S(int a) {}
1673 void func() {
1674 int a,b ;
1675 const S<10> s{a};
1676 static_assert(s.getSize() == 10, "");