|
1 | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 -triple x86_64-unknown-unknown %s |
2 | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 -triple x86_64-unknown-unknown %s -fexperimental-new-constant-interpreter |
3 | 3 |
|
4 | | -constexpr int arr[10] = {}; |
5 | | - |
6 | 4 | constexpr bool test_constexpr_valid() { |
| 5 | + constexpr int arr[10] = {}; |
7 | 6 | __builtin_assume_dereferenceable(arr, 40); |
8 | 7 | return true; |
9 | 8 | } |
10 | 9 | static_assert(test_constexpr_valid(), ""); |
11 | 10 |
|
12 | 11 | constexpr bool test_constexpr_partial() { |
| 12 | + constexpr int arr[10] = {}; |
13 | 13 | __builtin_assume_dereferenceable(&arr[5], 20); |
14 | 14 | return true; |
15 | 15 | } |
16 | 16 | static_assert(test_constexpr_partial(), ""); |
17 | 17 |
|
18 | | -constexpr bool test_constexpr_nullptr() { |
19 | | - __builtin_assume_dereferenceable(nullptr, 4); |
| 18 | +constexpr bool test_constexpr_nullptr() { // expected-error {{constexpr function never produces a constant expression}} |
| 19 | + __builtin_assume_dereferenceable(nullptr, 4); // expected-note 2{{read of dereferenced null pointer is not allowed in a constant expression}} |
20 | 20 | return true; |
21 | 21 | } |
22 | | -static_assert(test_constexpr_nullptr(), ""); // expected-error {{not an integral constant expression}} |
| 22 | +static_assert(test_constexpr_nullptr(), ""); // expected-error {{not an integral constant expression}} expected-note {{in call to}} |
23 | 23 |
|
24 | | -constexpr bool test_constexpr_too_large() { |
25 | | - __builtin_assume_dereferenceable(arr, 100); |
| 24 | +constexpr bool test_constexpr_too_large() { // expected-error {{constexpr function never produces a constant expression}} |
| 25 | + constexpr int arr[10] = {}; |
| 26 | + __builtin_assume_dereferenceable(arr, 100); // expected-note 2{{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} |
26 | 27 | return true; |
27 | 28 | } |
28 | | -static_assert(test_constexpr_too_large(), ""); // expected-error {{not an integral constant expression}} |
| 29 | +static_assert(test_constexpr_too_large(), ""); // expected-error {{not an integral constant expression}} expected-note {{in call to}} |
29 | 30 |
|
30 | | -constexpr int single_var = 42; |
31 | 31 | constexpr bool test_single_var() { |
| 32 | + constexpr int single_var = 42; |
32 | 33 | __builtin_assume_dereferenceable(&single_var, 4); |
33 | 34 | return true; |
34 | 35 | } |
35 | 36 | static_assert(test_single_var(), ""); |
36 | 37 |
|
37 | 38 | constexpr bool test_exact_boundary() { |
| 39 | + constexpr int arr[10] = {}; |
38 | 40 | __builtin_assume_dereferenceable(&arr[9], 4); |
39 | 41 | return true; |
40 | 42 | } |
41 | 43 | static_assert(test_exact_boundary(), ""); |
42 | 44 |
|
43 | | -constexpr bool test_one_over() { |
44 | | - __builtin_assume_dereferenceable(&arr[9], 5); |
| 45 | +constexpr bool test_one_over() { // expected-error {{constexpr function never produces a constant expression}} |
| 46 | + constexpr int arr[10] = {}; |
| 47 | + __builtin_assume_dereferenceable(&arr[9], 5); // expected-note 2{{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} |
45 | 48 | return true; |
46 | 49 | } |
47 | | -static_assert(test_one_over(), ""); // expected-error {{not an integral constant expression}} |
| 50 | +static_assert(test_one_over(), ""); // expected-error {{not an integral constant expression}} expected-note {{in call to}} |
48 | 51 |
|
49 | 52 | constexpr bool test_zero_size() { |
| 53 | + constexpr int arr[10] = {}; |
50 | 54 | __builtin_assume_dereferenceable(arr, 0); |
51 | 55 | return true; |
52 | 56 | } |
53 | | -static_assert(test_zero_size(), ""); // expected-error {{not an integral constant expression}} |
| 57 | +static_assert(test_zero_size(), ""); |
54 | 58 |
|
55 | | -struct S { |
56 | | - int x; |
57 | | - int y; |
58 | | -}; |
59 | | -constexpr S s = {1, 2}; |
60 | 59 | constexpr bool test_struct_member() { |
| 60 | + struct S { |
| 61 | + int x; |
| 62 | + int y; |
| 63 | + }; |
| 64 | + constexpr S s = {1, 2}; |
61 | 65 | __builtin_assume_dereferenceable(&s.x, 4); |
62 | 66 | return true; |
63 | 67 | } |
64 | 68 | static_assert(test_struct_member(), ""); |
| 69 | + |
| 70 | +constexpr bool test_range_valid() { |
| 71 | + constexpr int range_data[5] = {1, 2, 3, 4, 5}; |
| 72 | + __builtin_assume_dereferenceable(range_data, 5 * sizeof(int)); |
| 73 | + return range_data[0] == 1; |
| 74 | +} |
| 75 | +static_assert(test_range_valid(), ""); |
| 76 | + |
| 77 | +constexpr bool test_range_invalid() { // expected-error {{constexpr function never produces a constant expression}} |
| 78 | + constexpr int range_data[5] = {1, 2, 3, 4, 5}; |
| 79 | + __builtin_assume_dereferenceable(range_data, 6 * sizeof(int)); // expected-note 2{{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} |
| 80 | + return true; |
| 81 | +} |
| 82 | +static_assert(test_range_invalid(), ""); // expected-error {{not an integral constant expression}} expected-note {{in call to}} |
| 83 | + |
| 84 | +constexpr int arr1[10] = {}; |
| 85 | +constexpr int valid = (__builtin_assume_dereferenceable(arr1, 40), 12); |
| 86 | + |
| 87 | +constexpr int invalid = (__builtin_assume_dereferenceable((int*)123, 4), 12); // expected-error {{constexpr variable 'invalid' must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
| 88 | + |
| 89 | +constexpr int arr2[5] = {1, 2, 3, 4, 5}; |
| 90 | +constexpr int too_large = (__builtin_assume_dereferenceable(arr2, 6 * sizeof(int)), 12); // expected-error {{constexpr variable 'too_large' must be initialized by a constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} |
| 91 | + |
| 92 | +constexpr int null = (__builtin_assume_dereferenceable(nullptr, 4), 12); // expected-error {{constexpr variable 'null' must be initialized by a constant expression}} expected-note {{read of dereferenced null pointer is not allowed in a constant expression}} |
0 commit comments