2017-08-09 07:17:51 +08:00
|
|
|
// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify -triple x86_64-linux-gnu
|
2011-12-17 03:06:07 +08:00
|
|
|
|
2011-12-19 14:19:21 +08:00
|
|
|
struct S;
|
|
|
|
constexpr int extract(const S &s);
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
struct S {
|
2011-12-19 14:19:21 +08:00
|
|
|
constexpr S() : n(extract(*this)), m(0) {} // expected-note {{in call to 'extract(s1)'}}
|
2011-12-17 03:06:07 +08:00
|
|
|
constexpr S(int k) : n(k), m(extract(*this)) {}
|
|
|
|
int n, m;
|
|
|
|
};
|
|
|
|
|
2013-04-26 22:36:30 +08:00
|
|
|
constexpr int extract(const S &s) { return s.n; } // expected-note {{read of object outside its lifetime is not allowed in a constant expression}}
|
2011-12-17 03:06:07 +08:00
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
constexpr S s1; // ok
|
|
|
|
void f() {
|
|
|
|
constexpr S s1; // expected-error {{constant expression}} expected-note {{in call to 'S()'}}
|
|
|
|
constexpr S s2(10);
|
|
|
|
}
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
typedef __attribute__((vector_size(16))) int vector_int;
|
|
|
|
|
|
|
|
struct T {
|
|
|
|
constexpr T() : arr() {}
|
|
|
|
int arr[4];
|
|
|
|
};
|
|
|
|
struct U : T {
|
|
|
|
constexpr U(const int *p) : T(), another(), p(p) {}
|
|
|
|
constexpr U(const U &u) : T(), another(), p(u.p) {}
|
|
|
|
T another;
|
|
|
|
const int *p;
|
|
|
|
};
|
|
|
|
constexpr U u1(&u1.arr[2]);
|
|
|
|
|
|
|
|
constexpr int test_printing(int a, float b, _Complex int c, _Complex float d,
|
|
|
|
int *e, int &f, vector_int g, U h) {
|
2011-12-21 13:04:46 +08:00
|
|
|
return *e; // expected-note {{read of non-constexpr variable 'u2'}}
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
2011-12-21 13:04:46 +08:00
|
|
|
U u2(0); // expected-note {{here}}
|
2011-12-17 03:06:07 +08:00
|
|
|
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, ""); // \
|
|
|
|
expected-error {{constant expression}} \
|
2013-12-14 09:07:05 +08:00
|
|
|
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]})'}}
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
struct V {
|
2019-08-31 06:52:55 +08:00
|
|
|
int arr[256] = {[255] = 42}; // expected-warning {{C99}}
|
2011-12-17 03:06:07 +08:00
|
|
|
};
|
|
|
|
constexpr V v;
|
2011-12-21 13:04:46 +08:00
|
|
|
constexpr int get(const int *p) { return *p; } // expected-note {{read of dereferenced one-past-the-end pointer}}
|
2011-12-17 03:06:07 +08:00
|
|
|
constexpr int passLargeArray(V v) { return get(v.arr+256); } // expected-note {{in call to 'get(&v.arr[256])'}}
|
2013-12-14 09:07:05 +08:00
|
|
|
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, ...}})'}}
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
union Union {
|
|
|
|
constexpr Union(int n) : b(n) {}
|
|
|
|
constexpr Union(const Union &u) : b(u.b) {}
|
|
|
|
int a, b;
|
|
|
|
};
|
|
|
|
constexpr Union myUnion = 76;
|
|
|
|
|
2011-12-21 13:04:46 +08:00
|
|
|
constexpr int badness(Union u) { return u.a + u.b; } // expected-note {{read of member 'a' of union with active member 'b'}}
|
2011-12-17 03:06:07 +08:00
|
|
|
static_assert(badness(myUnion), ""); // expected-error {{constant expression}} \
|
|
|
|
expected-note {{in call to 'badness({.b = 76})'}}
|
|
|
|
|
|
|
|
struct MemPtrTest {
|
|
|
|
int n;
|
|
|
|
void f();
|
|
|
|
};
|
2011-12-21 13:04:46 +08:00
|
|
|
MemPtrTest mpt; // expected-note {{here}}
|
2011-12-17 03:06:07 +08:00
|
|
|
constexpr int MemPtr(int (MemPtrTest::*a), void (MemPtrTest::*b)(), int &c) {
|
2011-12-21 13:04:46 +08:00
|
|
|
return c; // expected-note {{read of non-constexpr variable 'mpt'}}
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
|
|
|
static_assert(MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.*&MemPtrTest::n), ""); // expected-error {{constant expression}} \
|
|
|
|
expected-note {{in call to 'MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.n)'}}
|
2011-12-31 07:37:31 +08:00
|
|
|
|
|
|
|
template<typename CharT>
|
|
|
|
constexpr CharT get(const CharT *p) { return p[-1]; } // expected-note 5{{}}
|
|
|
|
|
|
|
|
constexpr char c = get("test\0\\\"\t\a\b\234"); // \
|
|
|
|
expected-error {{}} expected-note {{"test\000\\\"\t\a\b\234"}}
|
|
|
|
constexpr char c8 = get(u8"test\0\\\"\t\a\b\234"); // \
|
|
|
|
expected-error {{}} expected-note {{u8"test\000\\\"\t\a\b\234"}}
|
|
|
|
constexpr char16_t c16 = get(u"test\0\\\"\t\a\b\234\u1234"); // \
|
|
|
|
expected-error {{}} expected-note {{u"test\000\\\"\t\a\b\234\u1234"}}
|
2012-01-03 02:14:06 +08:00
|
|
|
constexpr char32_t c32 = get(U"test\0\\\"\t\a\b\234\u1234\U0010ffff"); // \
|
|
|
|
expected-error {{}} expected-note {{U"test\000\\\"\t\a\b\234\u1234\U0010FFFF"}}
|
2012-04-05 08:17:44 +08:00
|
|
|
constexpr wchar_t wc = get(L"test\0\\\"\t\a\b\234\u1234\xffffffff"); // \
|
|
|
|
expected-error {{}} expected-note {{L"test\000\\\"\t\a\b\234\x1234\xFFFFFFFF"}}
|
2012-01-03 02:14:06 +08:00
|
|
|
|
|
|
|
constexpr char32_t c32_err = get(U"\U00110000"); // expected-error {{invalid universal character}}
|
2012-01-06 07:59:40 +08:00
|
|
|
|
2015-12-03 09:36:22 +08:00
|
|
|
#define fold(x) (__builtin_constant_p(x) ? (x) : (x))
|
|
|
|
|
2012-01-06 07:59:40 +08:00
|
|
|
typedef decltype(sizeof(int)) LabelDiffTy;
|
|
|
|
constexpr LabelDiffTy mulBy3(LabelDiffTy x) { return x * 3; } // expected-note {{subexpression}}
|
|
|
|
void LabelDiffTest() {
|
2015-12-03 09:36:22 +08:00
|
|
|
static_assert(mulBy3(fold((LabelDiffTy)&&a-(LabelDiffTy)&&b)) == 3, ""); // expected-error {{constant expression}} expected-note {{call to 'mulBy3(&&a - &&b)'}}
|
2012-01-06 07:59:40 +08:00
|
|
|
a:b:return;
|
|
|
|
}
|
2012-03-24 07:55:39 +08:00
|
|
|
|
|
|
|
constexpr bool test_bool_printing(bool b) { return 1 / !(2*b | !(2*b)); } // expected-note 2{{division by zero}}
|
|
|
|
constexpr bool test_bool_0 = test_bool_printing(false); // expected-error {{constant expr}} expected-note {{in call to 'test_bool_printing(false)'}}
|
|
|
|
constexpr bool test_bool_1 = test_bool_printing(true); // expected-error {{constant expr}} expected-note {{in call to 'test_bool_printing(true)'}}
|