blob: e6cf209819a2e7e61d160315933407ed5e1e69bc [file] [log] [blame]
Richard Smith08d6e032011-12-16 19:06:07 +00001// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify
2
Richard Smith099e7f62011-12-19 06:19:21 +00003struct S;
4constexpr int extract(const S &s);
Richard Smith08d6e032011-12-16 19:06:07 +00005
6struct S {
Richard Smith099e7f62011-12-19 06:19:21 +00007 constexpr S() : n(extract(*this)), m(0) {} // expected-note {{in call to 'extract(s1)'}}
Richard Smith08d6e032011-12-16 19:06:07 +00008 constexpr S(int k) : n(k), m(extract(*this)) {}
9 int n, m;
10};
11
Richard Smith7098cbd2011-12-21 05:04:46 +000012constexpr int extract(const S &s) { return s.n; } // expected-note {{read of uninitialized object is not allowed in a constant expression}}
Richard Smith08d6e032011-12-16 19:06:07 +000013
Rafael Espindolaf8c2a332011-12-30 03:11:50 +000014constexpr S s1; // expected-error {{constant expression}} expected-note {{in call to 'S()'}}
15constexpr S s2(10);
Richard Smith08d6e032011-12-16 19:06:07 +000016
17typedef __attribute__((vector_size(16))) int vector_int;
18
19struct T {
20 constexpr T() : arr() {}
21 int arr[4];
22};
23struct U : T {
24 constexpr U(const int *p) : T(), another(), p(p) {}
25 constexpr U(const U &u) : T(), another(), p(u.p) {}
26 T another;
27 const int *p;
28};
29constexpr U u1(&u1.arr[2]);
30
31constexpr int test_printing(int a, float b, _Complex int c, _Complex float d,
32 int *e, int &f, vector_int g, U h) {
Richard Smith7098cbd2011-12-21 05:04:46 +000033 return *e; // expected-note {{read of non-constexpr variable 'u2'}}
Richard Smith08d6e032011-12-16 19:06:07 +000034}
Richard Smith7098cbd2011-12-21 05:04:46 +000035U u2(0); // expected-note {{here}}
Richard Smith08d6e032011-12-16 19:06:07 +000036static_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, ""); // \
37expected-error {{constant expression}} \
38expected-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]})'}}
39
40struct V {
41 // FIXME: when we can generate these as constexpr constructors, remove the
42 // explicit definitions.
43 constexpr V() : arr{[255] = 42} {}
44 constexpr V(const V &v) : arr{[255] = 42} {}
45 int arr[256];
46};
47constexpr V v;
Richard Smith7098cbd2011-12-21 05:04:46 +000048constexpr int get(const int *p) { return *p; } // expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith08d6e032011-12-16 19:06:07 +000049constexpr int passLargeArray(V v) { return get(v.arr+256); } // expected-note {{in call to 'get(&v.arr[256])'}}
50static_assert(passLargeArray(v) == 0, ""); // expected-error {{constant expression}} expected-note {{in call to 'passLargeArray({{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}})'}}
51
52union Union {
53 constexpr Union(int n) : b(n) {}
54 constexpr Union(const Union &u) : b(u.b) {}
55 int a, b;
56};
57constexpr Union myUnion = 76;
58
Richard Smith7098cbd2011-12-21 05:04:46 +000059constexpr int badness(Union u) { return u.a + u.b; } // expected-note {{read of member 'a' of union with active member 'b'}}
Richard Smith08d6e032011-12-16 19:06:07 +000060static_assert(badness(myUnion), ""); // expected-error {{constant expression}} \
61 expected-note {{in call to 'badness({.b = 76})'}}
62
63struct MemPtrTest {
64 int n;
65 void f();
66};
Richard Smith7098cbd2011-12-21 05:04:46 +000067MemPtrTest mpt; // expected-note {{here}}
Richard Smith08d6e032011-12-16 19:06:07 +000068constexpr int MemPtr(int (MemPtrTest::*a), void (MemPtrTest::*b)(), int &c) {
Richard Smith7098cbd2011-12-21 05:04:46 +000069 return c; // expected-note {{read of non-constexpr variable 'mpt'}}
Richard Smith08d6e032011-12-16 19:06:07 +000070}
71static_assert(MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.*&MemPtrTest::n), ""); // expected-error {{constant expression}} \
72expected-note {{in call to 'MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.n)'}}