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) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame^] | 164 | if (begin == --end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 165 | return; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame^] | 166 | swap(*begin++, *end); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 167 | reverse(begin, end); |
| 168 | } |
| 169 | #endif |
| 170 | } |
| 171 | template<typename Iterator1, typename Iterator2> |
| 172 | constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) { |
| 173 | #if 0 // FIXME: once implementation is complete... |
| 174 | while (a != ae && b != be) { |
| 175 | if (*a != *b) |
| 176 | return false; |
| 177 | ++a, ++b; |
| 178 | } |
| 179 | #else |
| 180 | if (a != ae && b != be) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame^] | 181 | if (*a++ != *b++) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 182 | return false; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 183 | return equal(a, ae, b, be); |
| 184 | } |
| 185 | #endif |
| 186 | return a == ae && b == be; |
| 187 | } |
| 188 | constexpr bool test1(int n) { |
| 189 | char stuff[100] = "foobarfoo"; |
| 190 | const char stuff2[100] = "oofraboof"; |
| 191 | reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}} |
| 192 | return equal(stuff, stuff + n, stuff2, stuff2 + n); |
| 193 | } |
| 194 | static_assert(!test1(1), ""); |
| 195 | static_assert(test1(3), ""); |
| 196 | static_assert(!test1(6), ""); |
| 197 | static_assert(test1(9), ""); |
| 198 | static_assert(!test1(100), ""); |
| 199 | static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}} |
| 200 | |
| 201 | // FIXME: We should be able to reject this before it's called |
| 202 | constexpr void f() { |
| 203 | char foo[10] = { "z" }; // expected-note {{here}} |
| 204 | foo[10] = 'x'; // expected-warning {{past the end}} expected-note {{assignment to dereferenced one-past-the-end pointer}} |
| 205 | } |
| 206 | constexpr int k = (f(), 0); // expected-error {{constant expression}} expected-note {{in call}} |
| 207 | } |
| 208 | |
| 209 | namespace array_resize { |
| 210 | constexpr int do_stuff(int k1, int k2) { |
| 211 | int arr[1234] = { 1, 2, 3, 4 }; |
| 212 | arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}} |
| 213 | return arr[k2]; |
| 214 | } |
| 215 | static_assert(do_stuff(1, 2) == 3, ""); |
| 216 | static_assert(do_stuff(0, 0) == 5, ""); |
| 217 | static_assert(do_stuff(1233, 1233) == 5, ""); |
| 218 | static_assert(do_stuff(1233, 0) == 1, ""); |
| 219 | static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 220 | static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 221 | static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 222 | } |
| 223 | |
| 224 | namespace potential_const_expr { |
| 225 | constexpr void set(int &n) { n = 1; } |
| 226 | constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error |
| 227 | constexpr int div_zero_2() { // expected-error {{never produces a constant expression}} |
| 228 | int z = 0; |
| 229 | return 100 / (set(z), 0); // expected-note {{division by zero}} |
| 230 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 231 | int n; // expected-note {{declared here}} |
| 232 | constexpr int ref() { // expected-error {{never produces a constant expression}} |
| 233 | int &r = n; |
| 234 | return r; // expected-note {{read of non-const variable 'n'}} |
| 235 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | namespace subobject { |
| 239 | union A { constexpr A() : y(5) {} int x, y; }; |
| 240 | struct B { A a; }; |
| 241 | struct C : B {}; |
| 242 | union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; }; |
| 243 | constexpr void f(D &d) { |
| 244 | d.c.a.y = 3; |
| 245 | // expected-note@-1 {{cannot modify an object that is visible outside}} |
| 246 | // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}} |
| 247 | } |
| 248 | constexpr bool check(D &d) { return d.c.a.y == 3; } |
| 249 | |
| 250 | constexpr bool g() { D d; f(d); return d.c.a.y == 3; } |
| 251 | static_assert(g(), ""); |
| 252 | |
| 253 | D d; |
| 254 | constexpr bool h() { f(d); return check(d); } // expected-note {{in call}} |
| 255 | static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 256 | |
| 257 | constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}} |
| 258 | static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 259 | |
| 260 | 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'}} |
| 261 | static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 262 | } |
| 263 | |
| 264 | namespace lifetime { |
| 265 | constexpr int &&id(int &&n) { return static_cast<int&&>(n); } |
| 266 | constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}} |
| 267 | constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}} |
| 268 | static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 269 | } |
| 270 | |
| 271 | namespace const_modify { |
| 272 | constexpr int modify(int &n) { return n = 1; } // expected-note {{modification of object of const-qualified type 'const int'}} |
| 273 | constexpr int test1() { int k = 0; return modify(k); } |
| 274 | constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note {{in call}} |
| 275 | static_assert(test1() == 1, ""); |
| 276 | static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 277 | } |
| 278 | |
| 279 | namespace null { |
| 280 | constexpr int test(int *p) { |
| 281 | return *p = 123; // expected-note {{assignment to dereferenced null pointer}} |
| 282 | } |
| 283 | static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}} |
| 284 | } |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame^] | 285 | |
| 286 | namespace incdec { |
| 287 | template<typename T> constexpr T &ref(T &&r) { return r; } |
| 288 | template<typename T> constexpr T postinc(T &&r) { return (r++, r); } |
| 289 | template<typename T> constexpr T postdec(T &&r) { return (r--, r); } |
| 290 | |
| 291 | static_assert(++ref(0) == 1, ""); |
| 292 | static_assert(ref(0)++ == 0, ""); |
| 293 | static_assert(postinc(0) == 1, ""); |
| 294 | static_assert(--ref(0) == -1, ""); |
| 295 | static_assert(ref(0)-- == 0, ""); |
| 296 | static_assert(postdec(0) == -1, ""); |
| 297 | |
| 298 | constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}} |
| 299 | constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++; |
| 300 | constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}} |
| 301 | constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe); |
| 302 | |
| 303 | // inc/dec on short can't overflow because we promote to int first |
| 304 | static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, ""); |
| 305 | static_assert(--ref<short>(0x8000) == 0x7fff, ""); |
| 306 | |
| 307 | // inc on bool sets to true |
| 308 | static_assert(++ref(false), ""); // expected-warning {{deprecated}} |
| 309 | static_assert(++ref(true), ""); // expected-warning {{deprecated}} |
| 310 | |
| 311 | int arr[10]; |
| 312 | static_assert(++ref(&arr[0]) == &arr[1], ""); |
| 313 | static_assert(++ref(&arr[9]) == &arr[10], ""); |
| 314 | static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} |
| 315 | static_assert(ref(&arr[0])++ == &arr[0], ""); |
| 316 | static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} |
| 317 | static_assert(postinc(&arr[0]) == &arr[1], ""); |
| 318 | static_assert(--ref(&arr[10]) == &arr[9], ""); |
| 319 | static_assert(--ref(&arr[1]) == &arr[0], ""); |
| 320 | static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} |
| 321 | static_assert(ref(&arr[1])-- == &arr[1], ""); |
| 322 | static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} |
| 323 | static_assert(postdec(&arr[1]) == &arr[0], ""); |
| 324 | |
| 325 | int x; |
| 326 | static_assert(++ref(&x) == &x + 1, ""); |
| 327 | |
| 328 | static_assert(++ref(0.0) == 1.0, ""); |
| 329 | static_assert(ref(0.0)++ == 0.0, ""); |
| 330 | static_assert(postinc(0.0) == 1.0, ""); |
| 331 | static_assert(--ref(0.0) == -1.0, ""); |
| 332 | static_assert(ref(0.0)-- == 0.0, ""); |
| 333 | static_assert(postdec(0.0) == -1.0, ""); |
| 334 | |
| 335 | static_assert(++ref(1e100) == 1e100, ""); |
| 336 | static_assert(--ref(1e100) == 1e100, ""); |
| 337 | |
| 338 | union U { |
| 339 | int a, b; |
| 340 | }; |
| 341 | constexpr int f(U u) { |
| 342 | return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}} |
| 343 | } |
| 344 | constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}} |
| 345 | constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}} |
| 346 | |
| 347 | constexpr int incr(int k) { |
| 348 | int x = k; |
| 349 | if (x++ == 100) |
| 350 | return x; |
| 351 | return incr(x); |
| 352 | } |
| 353 | static_assert(incr(0) == 101, ""); |
| 354 | } |