Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu |
| 2 | |
| 3 | struct S { |
| 4 | // dummy ctor to make this a literal type |
| 5 | constexpr S(int); |
| 6 | |
| 7 | S(); |
| 8 | |
| 9 | int arr[10]; |
| 10 | |
| 11 | constexpr int &get(int n) { return arr[n]; } |
| 12 | constexpr const int &get(int n) const { return arr[n]; } |
| 13 | }; |
| 14 | |
| 15 | S s = S(); |
| 16 | const S &sr = s; |
| 17 | static_assert(&s.get(4) - &sr.get(2) == 2, ""); |
| 18 | |
| 19 | // Compound-statements can be used in constexpr functions. |
| 20 | constexpr int e() {{{{}} return 5; }} |
| 21 | static_assert(e() == 5, ""); |
| 22 | |
| 23 | // Types can be defined in constexpr functions. |
| 24 | constexpr int f() { |
| 25 | enum E { e1, e2, e3 }; |
| 26 | |
| 27 | struct S { |
| 28 | constexpr S(E e) : e(e) {} |
| 29 | constexpr int get() { return e; } |
| 30 | E e; |
| 31 | }; |
| 32 | |
| 33 | return S(e2).get(); |
| 34 | } |
| 35 | static_assert(f() == 1, ""); |
| 36 | |
| 37 | // Variables can be declared in constexpr functions. |
| 38 | constexpr int g(int k) { |
| 39 | const int n = 9; |
| 40 | int k2 = k * k; |
| 41 | int k3 = k2 * k; |
| 42 | return 3 * k3 + 5 * k2 + n * k - 20; |
| 43 | } |
| 44 | static_assert(g(2) == 42, ""); |
| 45 | constexpr int h(int n) { |
| 46 | static const int m = n; // expected-error {{static variable not permitted in a constexpr function}} |
| 47 | return m; |
| 48 | } |
| 49 | constexpr int i(int n) { |
| 50 | thread_local const int m = n; // expected-error {{thread_local variable not permitted in a constexpr function}} |
| 51 | return m; |
| 52 | } |
| 53 | |
| 54 | // if-statements can be used in constexpr functions. |
| 55 | constexpr int j(int k) { |
| 56 | if (k == 5) |
| 57 | return 1; |
| 58 | if (k == 1) |
| 59 | return 5; |
| 60 | else { |
| 61 | if (int n = 2 * k - 4) { |
| 62 | return n + 1; |
| 63 | return 2; |
| 64 | } |
| 65 | } |
| 66 | } // expected-note 2{{control reached end of constexpr function}} |
| 67 | static_assert(j(0) == -3, ""); |
| 68 | static_assert(j(1) == 5, ""); |
| 69 | static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}} |
| 70 | static_assert(j(3) == 3, ""); |
| 71 | static_assert(j(4) == 5, ""); |
| 72 | static_assert(j(5) == 1, ""); |
| 73 | |
| 74 | // There can be 0 return-statements. |
| 75 | constexpr void k() { |
| 76 | } |
| 77 | |
| 78 | // If the return type is not 'void', no return statements => never a constant |
| 79 | // expression, so still diagnose that case. |
| 80 | [[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}} |
| 81 | fn(); |
| 82 | } |
| 83 | |
| 84 | // We evaluate the body of a constexpr constructor, to check for side-effects. |
| 85 | struct U { |
| 86 | constexpr U(int n) { |
| 87 | if (j(n)) {} // expected-note {{in call to 'j(2)'}} |
| 88 | } |
| 89 | }; |
| 90 | constexpr U u1{1}; |
| 91 | constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}} |
| 92 | |
| 93 | // We allow expression-statements. |
| 94 | constexpr int l(bool b) { |
| 95 | if (b) |
| 96 | throw "invalid value for b!"; // expected-note {{subexpression not valid}} |
| 97 | return 5; |
| 98 | } |
| 99 | static_assert(l(false) == 5, ""); |
| 100 | static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}} |
| 101 | |
| 102 | // Potential constant expression checking is still applied where possible. |
| 103 | constexpr int htonl(int x) { // expected-error {{never produces a constant expression}} |
| 104 | typedef unsigned char uchar; |
| 105 | uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; |
| 106 | return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} |
| 107 | } |
| 108 | |
| 109 | constexpr int maybe_htonl(bool isBigEndian, int x) { |
| 110 | if (isBigEndian) |
| 111 | return x; |
| 112 | |
| 113 | typedef unsigned char uchar; |
| 114 | uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; |
| 115 | return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} |
| 116 | } |
| 117 | |
| 118 | constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}} |
| 119 | |
| 120 | namespace NS { |
| 121 | constexpr int n = 0; |
| 122 | } |
| 123 | constexpr int namespace_alias() { |
| 124 | namespace N = NS; |
| 125 | return N::n; |
| 126 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 127 | |
| 128 | namespace assign { |
| 129 | constexpr int a = 0; |
| 130 | const int b = 0; |
| 131 | int c = 0; // expected-note 2{{here}} |
| 132 | |
| 133 | constexpr void set(const int &a, int b) { |
| 134 | const_cast<int&>(a) = b; // expected-note 2{{constant expression cannot modify an object that is visible outside that expression}} |
| 135 | } |
| 136 | constexpr int wrap(int a, int b) { |
| 137 | set(a, b); |
| 138 | return a; |
| 139 | } |
| 140 | |
| 141 | static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}} |
| 142 | static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}} |
| 143 | static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} |
| 144 | |
| 145 | static_assert(wrap(a, 1) == 1, ""); |
| 146 | static_assert(wrap(b, 1) == 1, ""); |
| 147 | static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} |
| 148 | } |
| 149 | |
| 150 | namespace string_assign { |
| 151 | template<typename T> |
| 152 | constexpr void swap(T &a, T &b) { |
| 153 | T tmp = a; |
| 154 | a = b; |
| 155 | b = tmp; |
| 156 | } |
| 157 | template<typename Iterator> |
| 158 | constexpr void reverse(Iterator begin, Iterator end) { |
| 159 | #if 0 // FIXME: once implementation is complete... |
| 160 | while (begin != end && begin != --end) |
| 161 | swap(*begin++, *end); |
| 162 | #else |
| 163 | if (begin != end) { |
| 164 | end = end - 1; |
| 165 | if (begin == end) |
| 166 | return; |
| 167 | swap(*begin, *end); |
| 168 | begin = begin + 1; |
| 169 | reverse(begin, end); |
| 170 | } |
| 171 | #endif |
| 172 | } |
| 173 | template<typename Iterator1, typename Iterator2> |
| 174 | constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) { |
| 175 | #if 0 // FIXME: once implementation is complete... |
| 176 | while (a != ae && b != be) { |
| 177 | if (*a != *b) |
| 178 | return false; |
| 179 | ++a, ++b; |
| 180 | } |
| 181 | #else |
| 182 | if (a != ae && b != be) { |
| 183 | if (*a != *b) |
| 184 | return false; |
| 185 | a = a + 1; |
| 186 | b = b + 1; |
| 187 | return equal(a, ae, b, be); |
| 188 | } |
| 189 | #endif |
| 190 | return a == ae && b == be; |
| 191 | } |
| 192 | constexpr bool test1(int n) { |
| 193 | char stuff[100] = "foobarfoo"; |
| 194 | const char stuff2[100] = "oofraboof"; |
| 195 | reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}} |
| 196 | return equal(stuff, stuff + n, stuff2, stuff2 + n); |
| 197 | } |
| 198 | static_assert(!test1(1), ""); |
| 199 | static_assert(test1(3), ""); |
| 200 | static_assert(!test1(6), ""); |
| 201 | static_assert(test1(9), ""); |
| 202 | static_assert(!test1(100), ""); |
| 203 | static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}} |
| 204 | |
| 205 | // FIXME: We should be able to reject this before it's called |
| 206 | constexpr void f() { |
| 207 | char foo[10] = { "z" }; // expected-note {{here}} |
| 208 | foo[10] = 'x'; // expected-warning {{past the end}} expected-note {{assignment to dereferenced one-past-the-end pointer}} |
| 209 | } |
| 210 | constexpr int k = (f(), 0); // expected-error {{constant expression}} expected-note {{in call}} |
| 211 | } |
| 212 | |
| 213 | namespace array_resize { |
| 214 | constexpr int do_stuff(int k1, int k2) { |
| 215 | int arr[1234] = { 1, 2, 3, 4 }; |
| 216 | arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}} |
| 217 | return arr[k2]; |
| 218 | } |
| 219 | static_assert(do_stuff(1, 2) == 3, ""); |
| 220 | static_assert(do_stuff(0, 0) == 5, ""); |
| 221 | static_assert(do_stuff(1233, 1233) == 5, ""); |
| 222 | static_assert(do_stuff(1233, 0) == 1, ""); |
| 223 | static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 224 | static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 225 | static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 226 | } |
| 227 | |
| 228 | namespace potential_const_expr { |
| 229 | constexpr void set(int &n) { n = 1; } |
| 230 | constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error |
| 231 | constexpr int div_zero_2() { // expected-error {{never produces a constant expression}} |
| 232 | int z = 0; |
| 233 | return 100 / (set(z), 0); // expected-note {{division by zero}} |
| 234 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame^] | 235 | int n; // expected-note {{declared here}} |
| 236 | constexpr int ref() { // expected-error {{never produces a constant expression}} |
| 237 | int &r = n; |
| 238 | return r; // expected-note {{read of non-const variable 'n'}} |
| 239 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | namespace subobject { |
| 243 | union A { constexpr A() : y(5) {} int x, y; }; |
| 244 | struct B { A a; }; |
| 245 | struct C : B {}; |
| 246 | union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; }; |
| 247 | constexpr void f(D &d) { |
| 248 | d.c.a.y = 3; |
| 249 | // expected-note@-1 {{cannot modify an object that is visible outside}} |
| 250 | // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}} |
| 251 | } |
| 252 | constexpr bool check(D &d) { return d.c.a.y == 3; } |
| 253 | |
| 254 | constexpr bool g() { D d; f(d); return d.c.a.y == 3; } |
| 255 | static_assert(g(), ""); |
| 256 | |
| 257 | D d; |
| 258 | constexpr bool h() { f(d); return check(d); } // expected-note {{in call}} |
| 259 | static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 260 | |
| 261 | constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}} |
| 262 | static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 263 | |
| 264 | constexpr bool j() { D d; d.c.a.x = 3; return check(d); } // expected-note {{assignment to member 'x' of union with active member 'y'}} |
| 265 | static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 266 | } |
| 267 | |
| 268 | namespace lifetime { |
| 269 | constexpr int &&id(int &&n) { return static_cast<int&&>(n); } |
| 270 | constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}} |
| 271 | constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}} |
| 272 | static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 273 | } |
| 274 | |
| 275 | namespace const_modify { |
| 276 | constexpr int modify(int &n) { return n = 1; } // expected-note {{modification of object of const-qualified type 'const int'}} |
| 277 | constexpr int test1() { int k = 0; return modify(k); } |
| 278 | constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note {{in call}} |
| 279 | static_assert(test1() == 1, ""); |
| 280 | static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 281 | } |
| 282 | |
| 283 | namespace null { |
| 284 | constexpr int test(int *p) { |
| 285 | return *p = 123; // expected-note {{assignment to dereferenced null pointer}} |
| 286 | } |
| 287 | static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 288 | } |