xref: /aosp_15_r20/external/clang/test/SemaCXX/constexpr-printing.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify -triple x86_64-linux-gnu
2*67e74705SXin Li 
3*67e74705SXin Li struct S;
4*67e74705SXin Li constexpr int extract(const S &s);
5*67e74705SXin Li 
6*67e74705SXin Li struct S {
SS7*67e74705SXin Li   constexpr S() : n(extract(*this)), m(0) {} // expected-note {{in call to 'extract(s1)'}}
SS8*67e74705SXin Li   constexpr S(int k) : n(k), m(extract(*this)) {}
9*67e74705SXin Li   int n, m;
10*67e74705SXin Li };
11*67e74705SXin Li 
extract(const S & s)12*67e74705SXin Li constexpr int extract(const S &s) { return s.n; } // expected-note {{read of object outside its lifetime is not allowed in a constant expression}}
13*67e74705SXin Li 
14*67e74705SXin Li constexpr S s1; // ok
f()15*67e74705SXin Li void f() {
16*67e74705SXin Li   constexpr S s1; // expected-error {{constant expression}} expected-note {{in call to 'S()'}}
17*67e74705SXin Li   constexpr S s2(10);
18*67e74705SXin Li }
19*67e74705SXin Li 
20*67e74705SXin Li typedef __attribute__((vector_size(16))) int vector_int;
21*67e74705SXin Li 
22*67e74705SXin Li struct T {
TT23*67e74705SXin Li   constexpr T() : arr() {}
24*67e74705SXin Li   int arr[4];
25*67e74705SXin Li };
26*67e74705SXin Li struct U : T {
UU27*67e74705SXin Li   constexpr U(const int *p) : T(), another(), p(p) {}
UU28*67e74705SXin Li   constexpr U(const U &u) : T(), another(), p(u.p) {}
29*67e74705SXin Li   T another;
30*67e74705SXin Li   const int *p;
31*67e74705SXin Li };
32*67e74705SXin Li constexpr U u1(&u1.arr[2]);
33*67e74705SXin Li 
test_printing(int a,float b,_Complex int c,_Complex float d,int * e,int & f,vector_int g,U h)34*67e74705SXin Li constexpr int test_printing(int a, float b, _Complex int c, _Complex float d,
35*67e74705SXin Li                             int *e, int &f, vector_int g, U h) {
36*67e74705SXin Li   return *e; // expected-note {{read of non-constexpr variable 'u2'}}
37*67e74705SXin Li }
38*67e74705SXin Li U u2(0); // expected-note {{here}}
39*67e74705SXin Li static_assert(test_printing(12, 39.762, 3 + 4i, 12.9 + 3.6i, &u2.arr[4], u2.another.arr[2], (vector_int){5, 1, 2, 3}, u1) == 0, ""); // \
40*67e74705SXin Li expected-error {{constant expression}} \
41*67e74705SXin Li expected-note {{in call to 'test_printing(12, 3.976200e+01, 3+4i, 1.290000e+01+3.600000e+00i, &u2.T::arr[4], u2.another.arr[2], {5, 1, 2, 3}, {{{}}, {{}}, &u1.T::arr[2]})'}}
42*67e74705SXin Li 
43*67e74705SXin Li struct V {
44*67e74705SXin Li   // FIXME: when we can generate these as constexpr constructors, remove the
45*67e74705SXin Li   // explicit definitions.
VV46*67e74705SXin Li   constexpr V() : arr{[255] = 42} {}
VV47*67e74705SXin Li   constexpr V(const V &v) : arr{[255] = 42} {}
48*67e74705SXin Li   int arr[256];
49*67e74705SXin Li };
50*67e74705SXin Li constexpr V v;
get(const int * p)51*67e74705SXin Li constexpr int get(const int *p) { return *p; } // expected-note {{read of dereferenced one-past-the-end pointer}}
passLargeArray(V v)52*67e74705SXin Li constexpr int passLargeArray(V v) { return get(v.arr+256); } // expected-note {{in call to 'get(&v.arr[256])'}}
53*67e74705SXin Li static_assert(passLargeArray(v) == 0, ""); // expected-error {{constant expression}} expected-note {{in call to 'passLargeArray({{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}})'}}
54*67e74705SXin Li 
55*67e74705SXin Li union Union {
Union(int n)56*67e74705SXin Li   constexpr Union(int n) : b(n) {}
Union(const Union & u)57*67e74705SXin Li   constexpr Union(const Union &u) : b(u.b) {}
58*67e74705SXin Li   int a, b;
59*67e74705SXin Li };
60*67e74705SXin Li constexpr Union myUnion = 76;
61*67e74705SXin Li 
badness(Union u)62*67e74705SXin Li constexpr int badness(Union u) { return u.a + u.b; } // expected-note {{read of member 'a' of union with active member 'b'}}
63*67e74705SXin Li static_assert(badness(myUnion), ""); // expected-error {{constant expression}} \
64*67e74705SXin Li         expected-note {{in call to 'badness({.b = 76})'}}
65*67e74705SXin Li 
66*67e74705SXin Li struct MemPtrTest {
67*67e74705SXin Li   int n;
68*67e74705SXin Li   void f();
69*67e74705SXin Li };
70*67e74705SXin Li MemPtrTest mpt; // expected-note {{here}}
MemPtr(int (MemPtrTest::* a),void (MemPtrTest::* b)(),int & c)71*67e74705SXin Li constexpr int MemPtr(int (MemPtrTest::*a), void (MemPtrTest::*b)(), int &c) {
72*67e74705SXin Li   return c; // expected-note {{read of non-constexpr variable 'mpt'}}
73*67e74705SXin Li }
74*67e74705SXin Li static_assert(MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.*&MemPtrTest::n), ""); // expected-error {{constant expression}} \
75*67e74705SXin Li expected-note {{in call to 'MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.n)'}}
76*67e74705SXin Li 
77*67e74705SXin Li template<typename CharT>
get(const CharT * p)78*67e74705SXin Li constexpr CharT get(const CharT *p) { return p[-1]; } // expected-note 5{{}}
79*67e74705SXin Li 
80*67e74705SXin Li constexpr char c = get("test\0\\\"\t\a\b\234"); // \
81*67e74705SXin Li   expected-error {{}} expected-note {{"test\000\\\"\t\a\b\234"}}
82*67e74705SXin Li constexpr char c8 = get(u8"test\0\\\"\t\a\b\234"); // \
83*67e74705SXin Li   expected-error {{}} expected-note {{u8"test\000\\\"\t\a\b\234"}}
84*67e74705SXin Li constexpr char16_t c16 = get(u"test\0\\\"\t\a\b\234\u1234"); // \
85*67e74705SXin Li   expected-error {{}} expected-note {{u"test\000\\\"\t\a\b\234\u1234"}}
86*67e74705SXin Li constexpr char32_t c32 = get(U"test\0\\\"\t\a\b\234\u1234\U0010ffff"); // \
87*67e74705SXin Li   expected-error {{}} expected-note {{U"test\000\\\"\t\a\b\234\u1234\U0010FFFF"}}
88*67e74705SXin Li constexpr wchar_t wc = get(L"test\0\\\"\t\a\b\234\u1234\xffffffff"); // \
89*67e74705SXin Li   expected-error {{}} expected-note {{L"test\000\\\"\t\a\b\234\x1234\xFFFFFFFF"}}
90*67e74705SXin Li 
91*67e74705SXin Li constexpr char32_t c32_err = get(U"\U00110000"); // expected-error {{invalid universal character}}
92*67e74705SXin Li 
93*67e74705SXin Li #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
94*67e74705SXin Li 
95*67e74705SXin Li typedef decltype(sizeof(int)) LabelDiffTy;
mulBy3(LabelDiffTy x)96*67e74705SXin Li constexpr LabelDiffTy mulBy3(LabelDiffTy x) { return x * 3; } // expected-note {{subexpression}}
LabelDiffTest()97*67e74705SXin Li void LabelDiffTest() {
98*67e74705SXin Li   static_assert(mulBy3(fold((LabelDiffTy)&&a-(LabelDiffTy)&&b)) == 3, ""); // expected-error {{constant expression}} expected-note {{call to 'mulBy3(&&a - &&b)'}}
99*67e74705SXin Li   a:b:return;
100*67e74705SXin Li }
101*67e74705SXin Li 
test_bool_printing(bool b)102*67e74705SXin Li constexpr bool test_bool_printing(bool b) { return 1 / !(2*b | !(2*b)); } // expected-note 2{{division by zero}}
103*67e74705SXin Li constexpr bool test_bool_0 = test_bool_printing(false); // expected-error {{constant expr}} expected-note {{in call to 'test_bool_printing(false)'}}
104*67e74705SXin Li constexpr bool test_bool_1 = test_bool_printing(true); // expected-error {{constant expr}} expected-note {{in call to 'test_bool_printing(true)'}}
105