| 1 |
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s |
| 2 |
// RUN: %clang_cc1 -verify=ref %s |
| 3 |
|
| 4 |
|
| 5 |
constexpr int a = 10; |
| 6 |
constexpr const int &b = a; |
| 7 |
static_assert(a == b, ""); |
| 8 |
|
| 9 |
constexpr int assignToReference() { |
| 10 |
int a = 20; |
| 11 |
int &b = a; |
| 12 |
|
| 13 |
b = 100; |
| 14 |
return a; |
| 15 |
} |
| 16 |
static_assert(assignToReference() == 100, ""); |
| 17 |
|
| 18 |
|
| 19 |
constexpr void setValue(int &dest, int val) { |
| 20 |
dest = val; |
| 21 |
} |
| 22 |
|
| 23 |
constexpr int checkSetValue() { |
| 24 |
int l = 100; |
| 25 |
setValue(l, 200); |
| 26 |
return l; |
| 27 |
} |
| 28 |
static_assert(checkSetValue() == 200, ""); |
| 29 |
|
| 30 |
constexpr int readLocalRef() { |
| 31 |
int a = 20; |
| 32 |
int &b = a; |
| 33 |
return b; |
| 34 |
} |
| 35 |
static_assert(readLocalRef() == 20, ""); |
| 36 |
|
| 37 |
constexpr int incRef() { |
| 38 |
int a = 0; |
| 39 |
int &b = a; |
| 40 |
|
| 41 |
b = b + 1; |
| 42 |
|
| 43 |
return a; |
| 44 |
} |
| 45 |
static_assert(incRef() == 1, ""); |
| 46 |
|
| 47 |
|
| 48 |
template<const int &V> |
| 49 |
constexpr void Plus3(int &A) { |
| 50 |
A = V + 3; |
| 51 |
} |
| 52 |
constexpr int foo = 4; |
| 53 |
|
| 54 |
constexpr int callTemplate() { |
| 55 |
int a = 3; |
| 56 |
Plus3<foo>(a); |
| 57 |
return a; |
| 58 |
} |
| 59 |
static_assert(callTemplate() == 7, ""); |
| 60 |
|
| 61 |
|
| 62 |
constexpr int& getValue(int *array, int index) { |
| 63 |
return array[index]; |
| 64 |
} |
| 65 |
constexpr int testGetValue() { |
| 66 |
int values[] = {1, 2, 3, 4}; |
| 67 |
getValue(values, 2) = 30; |
| 68 |
return values[2]; |
| 69 |
} |
| 70 |
static_assert(testGetValue() == 30, ""); |
| 71 |
|
| 72 |
constexpr const int &MCE = 20; |
| 73 |
static_assert(MCE == 20, ""); |
| 74 |
static_assert(MCE == 30, ""); // expected-error {{static assertion failed}} \ |
| 75 |
// expected-note {{evaluates to '20 == 30'}} \ |
| 76 |
// ref-error {{static assertion failed}} \ |
| 77 |
// ref-note {{evaluates to '20 == 30'}} |
| 78 |
|
| 79 |
constexpr int LocalMCE() { |
| 80 |
const int &m = 100; |
| 81 |
return m; |
| 82 |
} |
| 83 |
static_assert(LocalMCE() == 100, ""); |
| 84 |
static_assert(LocalMCE() == 200, ""); // expected-error {{static assertion failed}} \ |
| 85 |
// expected-note {{evaluates to '100 == 200'}} \ |
| 86 |
// ref-error {{static assertion failed}} \ |
| 87 |
// ref-note {{evaluates to '100 == 200'}} |
| 88 |
|
| 89 |
struct S { |
| 90 |
int i, j; |
| 91 |
}; |
| 92 |
|
| 93 |
constexpr int RefToMemberExpr() { |
| 94 |
S s{1, 2}; |
| 95 |
|
| 96 |
int &j = s.i; |
| 97 |
j = j + 10; |
| 98 |
|
| 99 |
return j; |
| 100 |
} |
| 101 |
static_assert(RefToMemberExpr() == 11, ""); |
| 102 |
|
| 103 |
struct Ref { |
| 104 |
int &a; |
| 105 |
}; |
| 106 |
|
| 107 |
constexpr int RecordWithRef() { |
| 108 |
int m = 100; |
| 109 |
Ref r{m}; |
| 110 |
m = 200; |
| 111 |
return r.a; |
| 112 |
} |
| 113 |
static_assert(RecordWithRef() == 200, ""); |
| 114 |
|
| 115 |
|
| 116 |
struct Ref2 { |
| 117 |
int &a; |
| 118 |
constexpr Ref2(int &a) : a(a) {} |
| 119 |
}; |
| 120 |
|
| 121 |
constexpr int RecordWithRef2() { |
| 122 |
int m = 100; |
| 123 |
Ref2 r(m); |
| 124 |
m = 200; |
| 125 |
return r.a; |
| 126 |
} |
| 127 |
static_assert(RecordWithRef2() == 200, ""); |
| 128 |
|
| 129 |
const char (&nonextended_string_ref)[3] = {"hi"}; |
| 130 |
static_assert(nonextended_string_ref[0] == 'h', ""); |
| 131 |
static_assert(nonextended_string_ref[1] == 'i', ""); |
| 132 |
static_assert(nonextended_string_ref[2] == '\0', ""); |
| 133 |
|
| 134 |
/// This isa non-constant context. Reading A is not allowed, |
| 135 |
/// but taking its address is. |
| 136 |
int &&A = 12; |
| 137 |
int arr[!&A]; |