Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / AST / Interp / references.cpp
blob5dc6067db6a642f0fb4f92fdf1430f38243314ac
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
2 // RUN: %clang_cc1 -verify=ref %s
5 constexpr int a = 10;
6 constexpr const int &b = a;
7 static_assert(a == b, "");
9 constexpr int assignToReference() {
10 int a = 20;
11 int &b = a;
13 b = 100;
14 return a;
16 static_assert(assignToReference() == 100, "");
19 constexpr void setValue(int &dest, int val) {
20 dest = val;
23 constexpr int checkSetValue() {
24 int l = 100;
25 setValue(l, 200);
26 return l;
28 static_assert(checkSetValue() == 200, "");
30 constexpr int readLocalRef() {
31 int a = 20;
32 int &b = a;
33 return b;
35 static_assert(readLocalRef() == 20, "");
37 constexpr int incRef() {
38 int a = 0;
39 int &b = a;
41 b = b + 1;
43 return a;
45 static_assert(incRef() == 1, "");
48 template<const int &V>
49 constexpr void Plus3(int &A) {
50 A = V + 3;
52 constexpr int foo = 4;
54 constexpr int callTemplate() {
55 int a = 3;
56 Plus3<foo>(a);
57 return a;
59 static_assert(callTemplate() == 7, "");
62 constexpr int& getValue(int *array, int index) {
63 return array[index];
65 constexpr int testGetValue() {
66 int values[] = {1, 2, 3, 4};
67 getValue(values, 2) = 30;
68 return values[2];
70 static_assert(testGetValue() == 30, "");
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'}}
79 constexpr int LocalMCE() {
80 const int &m = 100;
81 return m;
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'}}
89 struct S {
90 int i, j;
93 constexpr int RefToMemberExpr() {
94 S s{1, 2};
96 int &j = s.i;
97 j = j + 10;
99 return j;
101 static_assert(RefToMemberExpr() == 11, "");
103 struct Ref {
104 int &a;
107 constexpr int RecordWithRef() {
108 int m = 100;
109 Ref r{m};
110 m = 200;
111 return r.a;
113 static_assert(RecordWithRef() == 200, "");
116 struct Ref2 {
117 int &a;
118 constexpr Ref2(int &a) : a(a) {}
121 constexpr int RecordWithRef2() {
122 int m = 100;
123 Ref2 r(m);
124 m = 200;
125 return r.a;
127 static_assert(RecordWithRef2() == 200, "");