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