constexpr-printing.cpp revision 08d6e032a2a0a8656d12b3b7b93942987bb12eb7
1// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify
2
3constexpr int extract(struct S &s);
4
5struct S {
6  constexpr S() : n(extract(*this)), m(0) {}
7  constexpr S(int k) : n(k), m(extract(*this)) {}
8  int n, m;
9};
10
11constexpr int extract(S &s) { return s.n; }
12
13// FIXME: once we produce notes for constexpr variable declarations, this should
14// produce a note indicating that S.n is used uninitialized.
15constexpr S s1; // expected-error {{constant expression}}
16constexpr S s2(10);
17
18typedef __attribute__((vector_size(16))) int vector_int;
19
20struct T {
21  constexpr T() : arr() {}
22  int arr[4];
23};
24struct U : T {
25  constexpr U(const int *p) : T(), another(), p(p) {}
26  constexpr U(const U &u) : T(), another(), p(u.p) {}
27  T another;
28  const int *p;
29};
30constexpr U u1(&u1.arr[2]);
31
32constexpr int test_printing(int a, float b, _Complex int c, _Complex float d,
33                            int *e, int &f, vector_int g, U h) {
34  return *e; // expected-note {{subexpression}}
35}
36U u2(0);
37static_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, ""); // \
38expected-error {{constant expression}} \
39expected-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]})'}}
40
41struct V {
42  // FIXME: when we can generate these as constexpr constructors, remove the
43  // explicit definitions.
44  constexpr V() : arr{[255] = 42} {}
45  constexpr V(const V &v) : arr{[255] = 42} {}
46  int arr[256];
47};
48constexpr V v;
49constexpr int get(const int *p) { return *p; } // expected-note {{subexpression}}
50constexpr int passLargeArray(V v) { return get(v.arr+256); } // expected-note {{in call to 'get(&v.arr[256])'}}
51static_assert(passLargeArray(v) == 0, ""); // expected-error {{constant expression}} expected-note {{in call to 'passLargeArray({{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}})'}}
52
53union Union {
54  constexpr Union(int n) : b(n) {}
55  constexpr Union(const Union &u) : b(u.b) {}
56  int a, b;
57};
58constexpr Union myUnion = 76;
59
60constexpr int badness(Union u) { return u.a + u.b; } // expected-note {{subexpression}}
61static_assert(badness(myUnion), ""); // expected-error {{constant expression}} \
62        expected-note {{in call to 'badness({.b = 76})'}}
63
64struct MemPtrTest {
65  int n;
66  void f();
67};
68MemPtrTest mpt;
69constexpr int MemPtr(int (MemPtrTest::*a), void (MemPtrTest::*b)(), int &c) {
70  return c; // expected-note {{subexpression}}
71}
72static_assert(MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.*&MemPtrTest::n), ""); // expected-error {{constant expression}} \
73expected-note {{in call to 'MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.n)'}}
74