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