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