blob: 726d1770faefaecd9a767dd1b679e30f1f1a7055 [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 Smith099e7f62011-12-19 06:19:21 +000012constexpr int extract(const S &s) { return s.n; } // expected-note {{subexpression}}
Richard Smith08d6e032011-12-16 19:06:07 +000013
14// FIXME: once we produce notes for constexpr variable declarations, this should
15// produce a note indicating that S.n is used uninitialized.
Richard Smith099e7f62011-12-19 06:19:21 +000016constexpr S s1; // expected-error {{constant expression}} expected-note {{in call to 'S()'}}
Richard Smith08d6e032011-12-16 19:06:07 +000017constexpr S s2(10);
18
19typedef __attribute__((vector_size(16))) int vector_int;
20
21struct T {
22 constexpr T() : arr() {}
23 int arr[4];
24};
25struct U : T {
26 constexpr U(const int *p) : T(), another(), p(p) {}
27 constexpr U(const U &u) : T(), another(), p(u.p) {}
28 T another;
29 const int *p;
30};
31constexpr U u1(&u1.arr[2]);
32
33constexpr 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 {{subexpression}}
36}
37U u2(0);
38static_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, ""); // \
39expected-error {{constant expression}} \
40expected-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
42struct V {
43 // FIXME: when we can generate these as constexpr constructors, remove the
44 // explicit definitions.
45 constexpr V() : arr{[255] = 42} {}
46 constexpr V(const V &v) : arr{[255] = 42} {}
47 int arr[256];
48};
49constexpr V v;
50constexpr int get(const int *p) { return *p; } // expected-note {{subexpression}}
51constexpr int passLargeArray(V v) { return get(v.arr+256); } // expected-note {{in call to 'get(&v.arr[256])'}}
52static_assert(passLargeArray(v) == 0, ""); // expected-error {{constant expression}} expected-note {{in call to 'passLargeArray({{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}})'}}
53
54union Union {
55 constexpr Union(int n) : b(n) {}
56 constexpr Union(const Union &u) : b(u.b) {}
57 int a, b;
58};
59constexpr Union myUnion = 76;
60
61constexpr int badness(Union u) { return u.a + u.b; } // expected-note {{subexpression}}
62static_assert(badness(myUnion), ""); // expected-error {{constant expression}} \
63 expected-note {{in call to 'badness({.b = 76})'}}
64
65struct MemPtrTest {
66 int n;
67 void f();
68};
69MemPtrTest mpt;
70constexpr int MemPtr(int (MemPtrTest::*a), void (MemPtrTest::*b)(), int &c) {
71 return c; // expected-note {{subexpression}}
72}
73static_assert(MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.*&MemPtrTest::n), ""); // expected-error {{constant expression}} \
74expected-note {{in call to 'MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.n)'}}