1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-vla -fms-extensions -std=c++11 -verify=expected,both %s 2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-vla -fms-extensions -std=c++20 -verify=expected,both %s 3 // RUN: %clang_cc1 -std=c++11 -fms-extensions -Wno-vla -verify=ref,both %s 4 // RUN: %clang_cc1 -std=c++20 -fms-extensions -Wno-vla -verify=ref,both %s 5 6 #define INT_MIN (~__INT_MAX__) 7 #define INT_MAX __INT_MAX__ 8 9 typedef __INTPTR_TYPE__ intptr_t; 10 typedef __PTRDIFF_TYPE__ ptrdiff_t; 11 12 13 static_assert(true, ""); 14 static_assert(false, ""); // both-error{{failed}} 15 static_assert(nullptr == nullptr, ""); 16 static_assert(__null == __null, ""); 17 static_assert(1 == 1, ""); 18 static_assert(1 == 3, ""); // both-error{{failed}} 19 20 constexpr void* v = nullptr; 21 static_assert(__null == v, ""); 22 23 constexpr int number = 10; 24 static_assert(number == 10, ""); 25 static_assert(number != 10, ""); // both-error{{failed}} \ 26 // both-note{{evaluates to}} 27 28 static_assert(__objc_yes, ""); 29 static_assert(!__objc_no, ""); 30 31 constexpr bool b = number; 32 static_assert(b, ""); 33 constexpr int one = true; 34 static_assert(one == 1, ""); 35 36 constexpr bool b2 = bool(); 37 static_assert(!b2, ""); 38 39 constexpr int Failed1 = 1 / 0; // both-error {{must be initialized by a constant expression}} \ 40 // both-note {{division by zero}} \ 41 // both-note {{declared here}} 42 constexpr int Failed2 = Failed1 + 1; // both-error {{must be initialized by a constant expression}} \ 43 // both-note {{declared here}} \ 44 // both-note {{initializer of 'Failed1' is not a constant expression}} 45 static_assert(Failed2 == 0, ""); // both-error {{not an integral constant expression}} \ 46 // both-note {{initializer of 'Failed2' is not a constant expression}} 47 48 const int x = *(volatile int*)0x1234; 49 static_assert((void{}, true), ""); 50 51 namespace ScalarTypes { 52 constexpr int ScalarInitInt = int(); 53 static_assert(ScalarInitInt == 0, ""); 54 constexpr float ScalarInitFloat = float(); 55 static_assert(ScalarInitFloat == 0.0f, ""); 56 57 static_assert(decltype(nullptr)() == nullptr, ""); 58 59 template<typename T> 60 constexpr T getScalar() { return T(); } 61 62 static_assert(getScalar<const int>() == 0, ""); 63 static_assert(getScalar<const double>() == 0.0, ""); 64 65 static_assert(getScalar<void*>() == nullptr, ""); 66 static_assert(getScalar<void(*)(void)>() == nullptr, ""); 67 68 enum E { 69 First = 0, 70 }; 71 static_assert(getScalar<E>() == First, ""); 72 73 struct S { 74 int v; 75 }; 76 constexpr int S::* MemberPtr = &S::v; 77 static_assert(getScalar<decltype(MemberPtr)>() == nullptr, ""); 78 79 #if __cplusplus >= 201402L 80 constexpr void Void(int n) { 81 void(n + 1); 82 void(); 83 } 84 constexpr int void_test = (Void(0), 1); 85 static_assert(void_test == 1, ""); 86 #endif 87 } 88 89 namespace IntegralCasts { 90 constexpr int i = 12; 91 constexpr unsigned int ui = i; 92 static_assert(ui == 12, ""); 93 constexpr unsigned int ub = !false; 94 static_assert(ub == 1, ""); 95 96 constexpr int si = ui; 97 static_assert(si == 12, ""); 98 constexpr int sb = true; 99 static_assert(sb == 1, ""); 100 101 constexpr int zero = 0; 102 constexpr unsigned int uzero = 0; 103 constexpr bool bs = i; 104 static_assert(bs, ""); 105 constexpr bool bu = ui; 106 static_assert(bu, ""); 107 constexpr bool ns = zero; 108 static_assert(!ns, ""); 109 constexpr bool nu = uzero; 110 static_assert(!nu, ""); 111 }; 112 113 constexpr int UninitI; // both-error {{must be initialized by a constant expression}} 114 constexpr int *UninitPtr; // both-error {{must be initialized by a constant expression}} 115 116 constexpr bool getTrue() { return true; } 117 constexpr bool getFalse() { return false; } 118 constexpr void* getNull() { return nullptr; } 119 120 constexpr int neg(int m) { return -m; } 121 constexpr bool inv(bool b) { return !b; } 122 123 static_assert(12, ""); 124 static_assert(12 == -(-(12)), ""); 125 static_assert(!false, ""); 126 static_assert(!!true, ""); 127 static_assert(!!true == !false, ""); 128 static_assert(true == 1, ""); 129 static_assert(false == 0, ""); 130 static_assert(!5 == false, ""); 131 static_assert(!0, ""); 132 static_assert(-true, ""); 133 static_assert(-false, ""); //both-error{{failed}} 134 135 static_assert(~0 == -1, ""); 136 static_assert(~1 == -2, ""); 137 static_assert(~-1 == 0, ""); 138 static_assert(~255 == -256, ""); 139 static_assert(~INT_MIN == INT_MAX, ""); 140 static_assert(~INT_MAX == INT_MIN, ""); 141 142 static_assert(-(1 << 31), ""); // both-error {{not an integral constant expression}} \ 143 // both-note {{outside the range of representable values}} 144 145 namespace PrimitiveEmptyInitList { 146 constexpr int a = {}; 147 static_assert(a == 0, ""); 148 constexpr bool b = {}; 149 static_assert(!b, ""); 150 constexpr double d = {}; 151 static_assert(d == 0.0, ""); 152 } 153 154 155 enum E {}; 156 constexpr E e = static_cast<E>(0); 157 static_assert(~e == -1, ""); 158 159 160 constexpr int m = 10; 161 constexpr const int *p = &m; 162 static_assert(p != nullptr, ""); 163 static_assert(*p == 10, ""); 164 165 constexpr const int* getIntPointer() { 166 return &m; 167 } 168 static_assert(getIntPointer() == &m, ""); 169 static_assert(*getIntPointer() == 10, ""); 170 171 constexpr int gimme(int k) { 172 return k; 173 } 174 static_assert(gimme(5) == 5, ""); 175 176 namespace PointerToBool { 177 178 constexpr void *N = nullptr; 179 constexpr bool B = N; 180 static_assert(!B, ""); 181 static_assert(!N, ""); 182 183 constexpr float F = 1.0; 184 constexpr const float *FP = &F; 185 static_assert(FP, ""); 186 static_assert(!!FP, ""); 187 } 188 189 namespace PointerComparison { 190 191 struct S { int a, b; } s; 192 constexpr void *null = 0; 193 constexpr void *pv = (void*)&s.a; 194 constexpr void *qv = (void*)&s.b; 195 constexpr bool v1 = null < (int*)0; 196 constexpr bool v2 = null < pv; // both-error {{must be initialized by a constant expression}} \ 197 // both-note {{comparison between pointers to unrelated objects 'nullptr' and '&s.a' has unspecified value}} 198 199 constexpr bool v3 = null == pv; // ok 200 constexpr bool v4 = qv == pv; // ok 201 202 constexpr bool v5 = qv >= pv; 203 constexpr bool v8 = qv > (void*)&s.a; 204 constexpr bool v6 = qv > null; // both-error {{must be initialized by a constant expression}} \ 205 // both-note {{comparison between pointers to unrelated objects '&s.b' and 'nullptr' has unspecified value}} 206 207 constexpr bool v7 = qv <= (void*)&s.b; // ok 208 209 constexpr ptrdiff_t m = &m - &m; 210 static_assert(m == 0, ""); 211 212 constexpr ptrdiff_t m2 = (&m2 + 1) - (&m2 + 1); 213 static_assert(m2 == 0, ""); 214 215 constexpr long m3 = (&m3 + 1) - (&m3); 216 static_assert(m3 == 1, ""); 217 218 constexpr long m4 = &m4 + 2 - &m4; // both-error {{must be initialized by a constant expression}} \ 219 // both-note {{cannot refer to element 2 of non-array object}} 220 } 221 222 namespace SizeOf { 223 static_assert(alignof(char&) == 1, ""); 224 225 constexpr int soint = sizeof(int); 226 constexpr int souint = sizeof(unsigned int); 227 static_assert(soint == souint, ""); 228 229 static_assert(sizeof(&soint) == sizeof(void*), ""); 230 static_assert(sizeof(&soint) == sizeof(nullptr), ""); 231 232 static_assert(sizeof(long) == sizeof(unsigned long), ""); 233 static_assert(sizeof(char) == sizeof(unsigned char), ""); 234 235 constexpr int N = 4; 236 constexpr int arr[N] = {1,2,3,4}; 237 static_assert(sizeof(arr) == N * sizeof(int), ""); 238 static_assert(sizeof(arr) == N * sizeof(arr[0]), ""); 239 240 constexpr bool arrB[N] = {true, true, true, true}; 241 static_assert(sizeof(arrB) == N * sizeof(bool), ""); 242 243 static_assert(sizeof(bool) == 1, ""); 244 static_assert(sizeof(char) == 1, ""); 245 246 constexpr int F = sizeof(void); // both-error{{incomplete type 'void'}} 247 248 constexpr int F2 = sizeof(gimme); // both-error{{to a function type}} 249 250 251 struct S { 252 void func(); 253 }; 254 constexpr void (S::*Func)() = &S::func; 255 static_assert(sizeof(Func) == sizeof(&S::func), ""); 256 257 258 void func() { 259 int n = 12; 260 constexpr int oofda = sizeof(int[n++]); // both-error {{must be initialized by a constant expression}} 261 } 262 263 #if __cplusplus >= 201402L 264 constexpr int IgnoredRejected() { // both-error {{never produces a constant expression}} 265 int n = 0; 266 sizeof(int[n++]); // both-warning {{expression result unused}} \ 267 // both-note 2{{subexpression not valid in a constant expression}} 268 return n; 269 } 270 static_assert(IgnoredRejected() == 0, ""); // both-error {{not an integral constant expression}} \ 271 // both-note {{in call to 'IgnoredRejected()'}} 272 #endif 273 274 275 #if __cplusplus >= 202002L 276 /// FIXME: The following code should be accepted. 277 consteval int foo(int n) { // both-error {{consteval function never produces a constant expression}} 278 return sizeof(int[n]); // both-note 3{{not valid in a constant expression}} 279 } 280 constinit int var = foo(5); // both-error {{not a constant expression}} \ 281 // both-note 2{{in call to}} \ 282 // both-error {{does not have a constant initializer}} \ 283 // both-note {{required by 'constinit' specifier}} 284 285 #endif 286 }; 287 288 namespace rem { 289 static_assert(2 % 2 == 0, ""); 290 static_assert(2 % 1 == 0, ""); 291 static_assert(-3 % 4 == -3, ""); 292 static_assert(4 % -2 == 0, ""); 293 static_assert(-3 % -4 == -3, ""); 294 295 constexpr int zero() { return 0; } 296 static_assert(10 % zero() == 20, ""); // both-error {{not an integral constant expression}} \ 297 // both-note {{division by zero}} 298 299 static_assert(true % true == 0, ""); 300 static_assert(false % true == 0, ""); 301 static_assert(true % false == 10, ""); // both-error {{not an integral constant expression}} \ 302 // both-note {{division by zero}} 303 constexpr int x = INT_MIN % - 1; // both-error {{must be initialized by a constant expression}} \ 304 // both-note {{value 2147483648 is outside the range}} 305 }; 306 307 namespace div { 308 constexpr int zero() { return 0; } 309 static_assert(12 / 3 == 4, ""); 310 static_assert(12 / zero() == 12, ""); // both-error {{not an integral constant expression}} \ 311 // both-note {{division by zero}} 312 static_assert(12 / -3 == -4, ""); 313 static_assert(-12 / 3 == -4, ""); 314 315 316 constexpr int LHS = 12; 317 constexpr long unsigned RHS = 3; 318 static_assert(LHS / RHS == 4, ""); 319 320 constexpr int x = INT_MIN / - 1; // both-error {{must be initialized by a constant expression}} \ 321 // both-note {{value 2147483648 is outside the range}} 322 }; 323 324 namespace cond { 325 constexpr bool isEven(int n) { 326 return n % 2 == 0 ? true : false; 327 } 328 static_assert(isEven(2), ""); 329 static_assert(!isEven(3), ""); 330 static_assert(isEven(100), ""); 331 332 constexpr int M = 5 ? 10 : 20; 333 static_assert(M == 10, ""); 334 335 static_assert(5 ? 13 : 16 == 13, ""); 336 static_assert(0 ? 13 : 16 == 16, ""); 337 338 static_assert(number ?: -15 == number, ""); 339 static_assert(0 ?: 100 == 100 , ""); 340 341 #if __cplusplus >= 201402L 342 constexpr int N = 20; 343 constexpr int foo() { 344 int m = N > 0 ? 5 : 10; 345 346 return m == 5 ? isEven(m) : true; 347 } 348 static_assert(foo() == false, ""); 349 350 constexpr int dontCallMe(unsigned m) { 351 if (m == 0) return 0; 352 return dontCallMe(m - 2); 353 } 354 355 // Can't call this because it will run into infinite recursion. 356 constexpr int assertNotReached() { 357 return dontCallMe(3); 358 } 359 360 constexpr int testCond() { 361 return true ? 5 : assertNotReached(); 362 } 363 364 constexpr int testCond2() { 365 return false ? assertNotReached() : 10; 366 } 367 368 static_assert(testCond() == 5, ""); 369 static_assert(testCond2() == 10, ""); 370 371 #endif 372 373 }; 374 375 namespace band { 376 static_assert((10 & 1) == 0, ""); 377 static_assert((10 & 10) == 10, ""); 378 379 static_assert((1337 & -1) == 1337, ""); 380 static_assert((0 & gimme(12)) == 0, ""); 381 }; 382 383 namespace bitOr { 384 static_assert((10 | 1) == 11, ""); 385 static_assert((10 | 10) == 10, ""); 386 387 static_assert((1337 | -1) == -1, ""); 388 static_assert((0 | gimme(12)) == 12, ""); 389 static_assert((12 | true) == 13, ""); 390 }; 391 392 namespace bitXor { 393 #pragma clang diagnostic push 394 #pragma clang diagnostic ignored "-Wxor-used-as-pow" 395 static_assert((10 ^ 1) == 11, ""); 396 static_assert((10 ^ 10) == 0, ""); 397 398 enum { 399 ONE = 1, 400 }; 401 402 static_assert((1337 ^ -1) == -1338, ""); 403 static_assert((0 | gimme(12)) == 12, ""); 404 static_assert((12 ^ true) == 13, ""); 405 static_assert((12 ^ ONE) == 13, ""); 406 #pragma clang diagnostic pop 407 }; 408 409 #if __cplusplus >= 201402L 410 constexpr bool IgnoredUnary() { 411 bool bo = true; 412 !bo; // both-warning {{expression result unused}} 413 return bo; 414 } 415 static_assert(IgnoredUnary(), ""); 416 #endif 417 418 namespace strings { 419 constexpr const char *S = "abc"; 420 static_assert(S[0] == 97, ""); 421 static_assert(S[1] == 98, ""); 422 static_assert(S[2] == 99, ""); 423 static_assert(S[3] == 0, ""); 424 425 static_assert("foobar"[2] == 'o', ""); 426 static_assert(2["foobar"] == 'o', ""); 427 428 constexpr const wchar_t *wide = L"bar"; 429 static_assert(wide[0] == L'b', ""); 430 431 constexpr const char32_t *u32 = U"abc"; 432 static_assert(u32[1] == U'b', ""); 433 434 constexpr char32_t c = U'\U0001F60E'; 435 static_assert(c == 0x0001F60EL, ""); 436 437 constexpr char k = -1; 438 static_assert(k == -1, ""); 439 440 static_assert('\N{LATIN CAPITAL LETTER E}' == 'E', ""); 441 static_assert('\t' == 9, ""); 442 443 #pragma clang diagnostic push 444 #pragma clang diagnostic ignored "-Wmultichar" 445 constexpr int mc = 'abc'; 446 static_assert(mc == 'abc', ""); 447 __WCHAR_TYPE__ wm = L'abc'; // both-error{{wide character literals may not contain multiple characters}} 448 __WCHAR_TYPE__ wu = u'abc'; // both-error{{Unicode character literals may not contain multiple characters}} 449 __WCHAR_TYPE__ wU = U'abc'; // both-error{{Unicode character literals may not contain multiple characters}} 450 #if __cplusplus > 201103L 451 __WCHAR_TYPE__ wu8 = u8'abc'; // both-error{{Unicode character literals may not contain multiple characters}} 452 #endif 453 454 #pragma clang diagnostic pop 455 456 constexpr char foo[12] = "abc"; 457 static_assert(foo[0] == 'a', ""); 458 static_assert(foo[1] == 'b', ""); 459 static_assert(foo[2] == 'c', ""); 460 static_assert(foo[3] == 0, ""); 461 static_assert(foo[11] == 0, ""); 462 463 constexpr char foo2[] = "abc\0def"; 464 static_assert(foo2[0] == 'a', ""); 465 static_assert(foo2[3] == '\0', ""); 466 static_assert(foo2[6] == 'f', ""); 467 static_assert(foo2[7] == '\0', ""); 468 static_assert(foo2[8] == '\0', ""); // both-error {{not an integral constant expression}} \ 469 // both-note {{read of dereferenced one-past-the-end pointer}} 470 471 constexpr char foo3[4] = "abc"; 472 static_assert(foo3[3] == '\0', ""); 473 static_assert(foo3[4] == '\0', ""); // both-error {{not an integral constant expression}} \ 474 // both-note {{read of dereferenced one-past-the-end pointer}} 475 476 constexpr char foo4[2] = "abcd"; // both-error {{initializer-string for char array is too long}} 477 static_assert(foo4[0] == 'a', ""); 478 static_assert(foo4[1] == 'b', ""); 479 static_assert(foo4[2] == '\0', ""); // both-error {{not an integral constant expression}} \ 480 // both-note {{read of dereferenced one-past-the-end pointer}} 481 482 constexpr char foo5[12] = "abc\xff"; 483 #if defined(__CHAR_UNSIGNED__) || __CHAR_BIT__ > 8 484 static_assert(foo5[3] == 255, ""); 485 #else 486 static_assert(foo5[3] == -1, ""); 487 #endif 488 }; 489 490 #if __cplusplus > 201402L 491 namespace IncDec { 492 constexpr int zero() { 493 int a = 0; 494 a++; 495 ++a; 496 a--; 497 --a; 498 return a; 499 } 500 static_assert(zero() == 0, ""); 501 502 constexpr int preInc() { 503 int a = 0; 504 return ++a; 505 } 506 static_assert(preInc() == 1, ""); 507 508 constexpr int postInc() { 509 int a = 0; 510 return a++; 511 } 512 static_assert(postInc() == 0, ""); 513 514 constexpr int preDec() { 515 int a = 0; 516 return --a; 517 } 518 static_assert(preDec() == -1, ""); 519 520 constexpr int postDec() { 521 int a = 0; 522 return a--; 523 } 524 static_assert(postDec() == 0, ""); 525 526 constexpr int three() { 527 int a = 0; 528 return ++a + ++a; // both-warning {{multiple unsequenced modifications to 'a'}} 529 } 530 static_assert(three() == 3, ""); 531 532 constexpr bool incBool() { 533 bool b = false; 534 return ++b; // both-error {{ISO C++17 does not allow incrementing expression of type bool}} 535 } 536 static_assert(incBool(), ""); 537 538 /// FIXME: The diagnostics for pre-inc/dec of pointers doesn't match the 539 /// current interpreter. But they are stil OK. 540 template<typename T, bool Inc, bool Pre> 541 constexpr int uninit() { 542 T a; 543 if constexpr (Inc) { 544 if (Pre) 545 ++a; // ref-note 3{{increment of uninitialized}} \ 546 // expected-note 2{{increment of uninitialized}} \ 547 // expected-note {{read of uninitialized}} 548 else 549 a++; // ref-note 2{{increment of uninitialized}} \ 550 // expected-note 2{{increment of uninitialized}} 551 } else { 552 if (Pre) 553 --a; // ref-note 3{{decrement of uninitialized}} \ 554 // expected-note 2{{decrement of uninitialized}} \ 555 // expected-note {{read of uninitialized}} 556 else 557 a--; // ref-note 2{{decrement of uninitialized}} \ 558 // expected-note 2{{decrement of uninitialized}} 559 } 560 return 1; 561 } 562 static_assert(uninit<int, true, true>(), ""); // both-error {{not an integral constant expression}} \ 563 // both-note {{in call to 'uninit<int, true, true>()'}} 564 static_assert(uninit<int, false, true>(), ""); // both-error {{not an integral constant expression}} \ 565 // both-note {{in call to 'uninit<int, false, true>()'}} 566 567 static_assert(uninit<float, true, true>(), ""); // both-error {{not an integral constant expression}} \ 568 // both-note {{in call to 'uninit<float, true, true>()'}} 569 static_assert(uninit<float, false, true>(), ""); // both-error {{not an integral constant expression}} \ 570 // both-note {{in call to 'uninit<float, false, true>()'}} 571 static_assert(uninit<float, true, false>(), ""); // both-error {{not an integral constant expression}} \ 572 // both-note {{in call to 'uninit<float, true, false>()'}} 573 static_assert(uninit<float, false, false>(), ""); // both-error {{not an integral constant expression}} \ 574 // both-note {{in call to 'uninit<float, false, false>()'}} 575 576 static_assert(uninit<int*, true, true>(), ""); // both-error {{not an integral constant expression}} \ 577 // both-note {{in call to 'uninit<int *, true, true>()'}} 578 static_assert(uninit<int*, false, true>(), ""); // both-error {{not an integral constant expression}} \ 579 // both-note {{in call to 'uninit<int *, false, true>()'}} 580 static_assert(uninit<int*, true, false>(), ""); // both-error {{not an integral constant expression}} \ 581 // both-note {{in call to 'uninit<int *, true, false>()'}} 582 static_assert(uninit<int*, false, false>(), ""); // both-error {{not an integral constant expression}} \ 583 // both-note {{in call to 'uninit<int *, false, false>()'}} 584 585 constexpr int OverFlow() { // both-error {{never produces a constant expression}} 586 int a = INT_MAX; 587 ++a; // both-note 2{{is outside the range}} 588 return -1; 589 } 590 static_assert(OverFlow() == -1, ""); // both-error {{not an integral constant expression}} \ 591 // both-note {{in call to 'OverFlow()'}} 592 593 constexpr int UnderFlow() { // both-error {{never produces a constant expression}} 594 int a = INT_MIN; 595 --a; // both-note 2{{is outside the range}} 596 return -1; 597 } 598 static_assert(UnderFlow() == -1, ""); // both-error {{not an integral constant expression}} \ 599 // both-note {{in call to 'UnderFlow()'}} 600 601 constexpr int getTwo() { 602 int i = 1; 603 return (i += 1); 604 } 605 static_assert(getTwo() == 2, ""); 606 607 constexpr int sub(int a) { 608 return (a -= 2); 609 } 610 static_assert(sub(7) == 5, ""); 611 612 constexpr int add(int a, int b) { 613 a += b; // both-note {{is outside the range of representable values}} 614 return a; 615 } 616 static_assert(add(1, 2) == 3, ""); 617 static_assert(add(INT_MAX, 1) == 0, ""); // both-error {{not an integral constant expression}} \ 618 // both-note {{in call to 'add}} 619 620 constexpr int sub(int a, int b) { 621 a -= b; // both-note {{is outside the range of representable values}} 622 return a; 623 } 624 static_assert(sub(10, 20) == -10, ""); 625 static_assert(sub(INT_MIN, 1) == 0, ""); // both-error {{not an integral constant expression}} \ 626 // both-note {{in call to 'sub}} 627 628 constexpr int subAll(int a) { 629 return (a -= a); 630 } 631 static_assert(subAll(213) == 0, ""); 632 633 constexpr bool BoolOr(bool b1, bool b2) { 634 bool a; 635 a = b1; 636 a |= b2; 637 return a; 638 } 639 static_assert(BoolOr(true, true), ""); 640 static_assert(BoolOr(true, false), ""); 641 static_assert(BoolOr(false, true), ""); 642 static_assert(!BoolOr(false, false), ""); 643 644 constexpr int IntOr(unsigned a, unsigned b) { 645 unsigned r; 646 r = a; 647 r |= b; 648 return r; 649 } 650 static_assert(IntOr(10, 1) == 11, ""); 651 static_assert(IntOr(1337, -1) == -1, ""); 652 static_assert(IntOr(0, 12) == 12, ""); 653 654 constexpr bool BoolAnd(bool b1, bool b2) { 655 bool a; 656 a = b1; 657 a &= b2; 658 return a; 659 } 660 static_assert(BoolAnd(true, true), ""); 661 static_assert(!BoolAnd(true, false), ""); 662 static_assert(!BoolAnd(false, true), ""); 663 static_assert(!BoolAnd(false, false), ""); 664 665 constexpr int IntAnd(unsigned a, unsigned b) { 666 unsigned r; 667 r = a; 668 r &= b; 669 return r; 670 } 671 static_assert(IntAnd(10, 1) == 0, ""); 672 static_assert(IntAnd(1337, -1) == 1337, ""); 673 static_assert(IntAnd(0, 12) == 0, ""); 674 675 constexpr bool BoolXor(bool b1, bool b2) { 676 bool a; 677 a = b1; 678 a ^= b2; 679 return a; 680 } 681 static_assert(!BoolXor(true, true), ""); 682 static_assert(BoolXor(true, false), ""); 683 static_assert(BoolXor(false, true), ""); 684 static_assert(!BoolXor(false, false), ""); 685 686 constexpr int IntXor(unsigned a, unsigned b) { 687 unsigned r; 688 r = a; 689 r ^= b; 690 return r; 691 } 692 static_assert(IntXor(10, 1) == 11, ""); 693 static_assert(IntXor(10, 10) == 0, ""); 694 static_assert(IntXor(12, true) == 13, ""); 695 696 constexpr bool BoolRem(bool b1, bool b2) { 697 bool a; 698 a = b1; 699 a %= b2; 700 return a; 701 } 702 static_assert(!BoolRem(true, true), ""); 703 static_assert(!BoolRem(false, true), ""); 704 705 constexpr int IntRem(int a, int b) { 706 int r; 707 r = a; 708 r %= b; // both-note {{division by zero}} \ 709 // both-note {{outside the range of representable values}} 710 return r; 711 } 712 static_assert(IntRem(2, 2) == 0, ""); 713 static_assert(IntRem(2, 1) == 0, ""); 714 static_assert(IntRem(9, 7) == 2, ""); 715 static_assert(IntRem(5, 0) == 0, ""); // both-error {{not an integral constant expression}} \ 716 // both-note {{in call to 'IntRem(5, 0)'}} 717 718 static_assert(IntRem(INT_MIN, -1) == 0, ""); // both-error {{not an integral constant expression}} \ 719 // both-note {{in call to 'IntRem}} 720 721 constexpr bool BoolDiv(bool b1, bool b2) { 722 bool a; 723 a = b1; 724 a /= b2; 725 return a; 726 } 727 static_assert(BoolDiv(true, true), ""); 728 static_assert(!BoolDiv(false, true), ""); 729 730 constexpr int IntDiv(int a, int b) { 731 int r; 732 r = a; 733 r /= b; // both-note {{division by zero}} \ 734 // both-note {{outside the range of representable values}} 735 return r; 736 } 737 static_assert(IntDiv(2, 2) == 1, ""); 738 static_assert(IntDiv(12, 20) == 0, ""); 739 static_assert(IntDiv(2, 1) == 2, ""); 740 static_assert(IntDiv(9, 7) == 1, ""); 741 static_assert(IntDiv(5, 0) == 0, ""); // both-error {{not an integral constant expression}} \ 742 // both-note {{in call to 'IntDiv(5, 0)'}} 743 744 static_assert(IntDiv(INT_MIN, -1) == 0, ""); // both-error {{not an integral constant expression}} \ 745 // both-note {{in call to 'IntDiv}} 746 747 constexpr bool BoolMul(bool b1, bool b2) { 748 bool a; 749 a = b1; 750 a *= b2; 751 return a; 752 } 753 static_assert(BoolMul(true, true), ""); 754 static_assert(!BoolMul(true, false), ""); 755 static_assert(!BoolMul(false, true), ""); 756 static_assert(!BoolMul(false, false), ""); 757 758 constexpr int IntMul(int a, int b) { 759 int r; 760 r = a; 761 r *= b; // both-note {{is outside the range of representable values of type 'int'}} 762 return r; 763 } 764 static_assert(IntMul(2, 2) == 4, ""); 765 static_assert(IntMul(12, 20) == 240, ""); 766 static_assert(IntMul(2, 1) == 2, ""); 767 static_assert(IntMul(9, 7) == 63, ""); 768 static_assert(IntMul(INT_MAX, 2) == 0, ""); // both-error {{not an integral constant expression}} \ 769 // both-note {{in call to 'IntMul}} 770 constexpr int arr[] = {1,2,3}; 771 constexpr int ptrInc1() { 772 const int *p = arr; 773 p += 2; 774 return *p; 775 } 776 static_assert(ptrInc1() == 3, ""); 777 778 constexpr int ptrInc2() { 779 const int *p = arr; 780 return *(p += 1); 781 } 782 static_assert(ptrInc2() == 2, ""); 783 784 constexpr int ptrInc3() { // both-error {{never produces a constant expression}} 785 const int *p = arr; 786 p += 12; // both-note {{cannot refer to element 12 of array of 3 elements}} 787 return *p; 788 } 789 790 constexpr int ptrIncDec1() { 791 const int *p = arr; 792 p += 2; 793 p -= 1; 794 return *p; 795 } 796 static_assert(ptrIncDec1() == 2, ""); 797 798 constexpr int ptrDec1() { // both-error {{never produces a constant expression}} 799 const int *p = arr; 800 p -= 1; // both-note {{cannot refer to element -1 of array of 3 elements}} 801 return *p; 802 } 803 804 /// This used to leave a 0 on the stack instead of the previous 805 /// value of a. 806 constexpr int bug1Inc() { 807 int a = 3; 808 int b = a++; 809 return b; 810 } 811 static_assert(bug1Inc() == 3); 812 813 constexpr int bug1Dec() { 814 int a = 3; 815 int b = a--; 816 return b; 817 } 818 static_assert(bug1Dec() == 3); 819 820 constexpr int f() { 821 int a[] = {1,2}; 822 int i = 0; 823 824 // RHS should be evaluated before LHS, so this should 825 // write to a[1]; 826 a[i++] += ++i; 827 828 return a[1]; 829 } 830 static_assert(f() == 3, ""); 831 832 int nonconst(int a) { // both-note 4{{declared here}} 833 static_assert(a++, ""); // both-error {{not an integral constant expression}} \ 834 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} 835 static_assert(a--, ""); // both-error {{not an integral constant expression}} \ 836 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} 837 static_assert(++a, ""); // both-error {{not an integral constant expression}} \ 838 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} 839 static_assert(--a, ""); // both-error {{not an integral constant expression}} \ 840 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} 841 } 842 843 }; 844 #endif 845 846 namespace CompoundLiterals { 847 constexpr int get5() { 848 return (int[]){1,2,3,4,5}[4]; 849 } 850 static_assert(get5() == 5, ""); 851 852 constexpr int get6(int f = (int[]){1,2,6}[2]) { // ref-note {{subexpression not valid in a constant expression}} \ 853 // ref-note {{declared here}} 854 return f; 855 } 856 static_assert(get6(6) == 6, ""); 857 // FIXME: Who's right here? 858 static_assert(get6() == 6, ""); // ref-error {{not an integral constant expression}} 859 860 constexpr int x = (int){3}; 861 static_assert(x == 3, ""); 862 #if __cplusplus >= 201402L 863 constexpr int getX() { 864 int x = (int){3}; 865 x = (int){5}; 866 return x; 867 } 868 static_assert(getX() == 5, ""); 869 #endif 870 871 #if __cplusplus >= 202002L 872 constexpr int get3() { 873 int m; 874 m = (int){3}; 875 return m; 876 } 877 static_assert(get3() == 3, ""); 878 #endif 879 }; 880 881 namespace TypeTraits { 882 static_assert(__is_trivial(int), ""); 883 static_assert(__is_trivial(float), ""); 884 static_assert(__is_trivial(E), ""); 885 struct S{}; 886 static_assert(__is_trivial(S), ""); 887 struct S2 { 888 S2() {} 889 }; 890 static_assert(!__is_trivial(S2), ""); 891 892 template <typename T> 893 struct S3 { 894 constexpr bool foo() const { return __is_trivial(T); } 895 }; 896 struct T { 897 ~T() {} 898 }; 899 struct U {}; 900 static_assert(S3<U>{}.foo(), ""); 901 static_assert(!S3<T>{}.foo(), ""); 902 903 typedef int Int; 904 typedef Int IntAr[10]; 905 typedef const IntAr ConstIntAr; 906 typedef ConstIntAr ConstIntArAr[4]; 907 908 static_assert(__array_rank(IntAr) == 1, ""); 909 static_assert(__array_rank(ConstIntArAr) == 2, ""); 910 911 static_assert(__array_extent(IntAr, 0) == 10, ""); 912 static_assert(__array_extent(ConstIntArAr, 0) == 4, ""); 913 static_assert(__array_extent(ConstIntArAr, 1) == 10, ""); 914 } 915 916 #if __cplusplus >= 201402L 917 constexpr int ignoredDecls() { 918 static_assert(true, ""); 919 struct F { int a; }; 920 enum E { b }; 921 using A = int; 922 typedef int Z; 923 924 return F{12}.a; 925 } 926 static_assert(ignoredDecls() == 12, ""); 927 928 namespace DiscardExprs { 929 #pragma clang diagnostic push 930 #pragma clang diagnostic ignored "-Wunused-value" 931 typedef struct _GUID { 932 __UINT32_TYPE__ Data1; 933 __UINT16_TYPE__ Data2; 934 __UINT16_TYPE__ Data3; 935 __UINT8_TYPE__ Data4[8]; 936 } GUID; 937 class __declspec(uuid("000000A0-0000-0000-C000-000000000049")) GuidType; 938 939 struct A{ int a; }; 940 constexpr int ignoredExprs() { 941 (void)(1 / 2); 942 A a{12}; 943 a; 944 (void)a; 945 (a); 946 947 /// Ignored MaterializeTemporaryExpr. 948 struct B{ const int &a; }; 949 (void)B{12}; 950 951 (void)5, (void)6; 952 953 1 ? 0 : 1; 954 __is_trivial(int); 955 956 (int){1}; 957 (int[]){1,2,3}; 958 int arr[] = {1,2,3}; 959 arr[0]; 960 "a"; 961 'b'; 962 sizeof(int); 963 alignof(int); 964 965 (short)5; 966 (bool)1; 967 __null; 968 __builtin_offsetof(A, a); 969 1,2; 970 (int)1.0; 971 (float)1; 972 (double)1.0f; 973 (signed)4u; 974 __uuidof(GuidType); 975 __uuidof(number); // both-error {{cannot call operator __uuidof on a type with no GUID}} 976 977 requires{false;}; 978 constexpr int *p = nullptr; 979 p - p; 980 981 return 0; 982 } 983 static_assert(ignoredExprs() == 0, ""); 984 985 constexpr int oh_my(int x) { 986 (int){ x++ }; 987 return x; 988 } 989 static_assert(oh_my(0) == 1, ""); 990 991 constexpr int oh_my2(int x) { 992 int y{x++}; 993 return x; 994 } 995 996 static_assert(oh_my2(0) == 1, ""); 997 998 999 /// Ignored comma expressions still have their 1000 /// expressions evaluated. 1001 constexpr int Comma(int start) { 1002 int i = start; 1003 1004 (void)i++; 1005 (void)i++,(void)i++; 1006 return i; 1007 } 1008 constexpr int Value = Comma(5); 1009 static_assert(Value == 8, ""); 1010 1011 /// Ignored MemberExprs need to still evaluate the Base 1012 /// expr. 1013 constexpr A callme(int &i) { 1014 ++i; 1015 return A{}; 1016 } 1017 constexpr int ignoredMemberExpr() { 1018 int i = 0; 1019 callme(i).a; 1020 return i; 1021 } 1022 static_assert(ignoredMemberExpr() == 1, ""); 1023 1024 template <int I> 1025 constexpr int foo() { 1026 I; 1027 return I; 1028 } 1029 static_assert(foo<3>() == 3, ""); 1030 1031 struct ATemp { 1032 consteval ATemp ret_a() const { return ATemp{}; } 1033 }; 1034 1035 void test() { 1036 int k = (ATemp().ret_a(), 0); 1037 } 1038 1039 #pragma clang diagnostic pop 1040 } 1041 #endif 1042 1043 namespace PredefinedExprs { 1044 #if __cplusplus >= 201402L 1045 template<typename CharT> 1046 constexpr bool strings_match(const CharT *str1, const CharT *str2) { 1047 while (*str1 && *str2) { 1048 if (*str1++ != *str2++) 1049 return false; 1050 }; 1051 1052 return *str1 == *str2; 1053 } 1054 1055 void foo() { 1056 static_assert(strings_match(__FUNCSIG__, "void __cdecl PredefinedExprs::foo(void)"), ""); 1057 static_assert(strings_match(L__FUNCSIG__, L"void __cdecl PredefinedExprs::foo(void)"), ""); 1058 static_assert(strings_match(L__FUNCTION__, L"foo"), ""); 1059 static_assert(strings_match(__FUNCTION__, "foo"), ""); 1060 static_assert(strings_match(__func__, "foo"), ""); 1061 static_assert(strings_match(__PRETTY_FUNCTION__, "void PredefinedExprs::foo()"), ""); 1062 } 1063 1064 constexpr char heh(unsigned index) { 1065 __FUNCTION__; // both-warning {{result unused}} 1066 __extension__ __FUNCTION__; // both-warning {{result unused}} 1067 return __FUNCTION__[index]; 1068 } 1069 static_assert(heh(0) == 'h', ""); 1070 static_assert(heh(1) == 'e', ""); 1071 static_assert(heh(2) == 'h', ""); 1072 #endif 1073 } 1074 1075 namespace NE { 1076 constexpr int foo() noexcept { 1077 return 1; 1078 } 1079 static_assert(noexcept(foo()), ""); 1080 constexpr int foo2() { 1081 return 1; 1082 } 1083 static_assert(!noexcept(foo2()), ""); 1084 1085 #if __cplusplus > 201402L 1086 constexpr int a() { 1087 int b = 0; 1088 (void)noexcept(++b); // both-warning {{expression with side effects has no effect in an unevaluated context}} 1089 1090 return b; 1091 } 1092 static_assert(a() == 0, ""); 1093 #endif 1094 } 1095 1096 namespace PointerCasts { 1097 constexpr int M = 10; 1098 constexpr const int *P = &M; 1099 constexpr intptr_t A = (intptr_t)P; // both-error {{must be initialized by a constant expression}} \ 1100 // both-note {{cast that performs the conversions of a reinterpret_cast}} 1101 1102 int array[(intptr_t)(char*)0]; // both-warning {{variable length array folded to constant array}} 1103 } 1104 1105 namespace InvalidDeclRefs { 1106 bool b00; // both-note {{declared here}} 1107 static_assert(b00, ""); // both-error {{not an integral constant expression}} \ 1108 // both-note {{read of non-const variable}} 1109 1110 float b01; // both-note {{declared here}} 1111 static_assert(b01, ""); // both-error {{not an integral constant expression}} \ 1112 // both-note {{read of non-constexpr variable}} 1113 1114 extern const int b02; // both-note {{declared here}} 1115 static_assert(b02, ""); // both-error {{not an integral constant expression}} \ 1116 // both-note {{initializer of 'b02' is unknown}} 1117 1118 int b03 = 3; // both-note {{declared here}} 1119 static_assert(b03, ""); // both-error {{not an integral constant expression}} \ 1120 // both-note {{read of non-const variable}} 1121 1122 extern int var; 1123 constexpr int *varp = &var; // Ok. 1124 } 1125 1126 namespace NonConstReads { 1127 void *p = nullptr; // both-note {{declared here}} 1128 static_assert(!p, ""); // both-error {{not an integral constant expression}} \ 1129 // both-note {{read of non-constexpr variable 'p'}} 1130 1131 int arr[!p]; // both-error {{variable length array}} 1132 1133 int z; // both-note {{declared here}} 1134 static_assert(z == 0, ""); // both-error {{not an integral constant expression}} \ 1135 // both-note {{read of non-const variable 'z'}} 1136 } 1137 1138 /// This test passes a MaterializedTemporaryExpr to evaluateAsRValue. 1139 /// That needs to return a null pointer after the lvalue-to-rvalue conversion. 1140 /// We used to fail to do that. 1141 namespace rdar8769025 { 1142 __attribute__((nonnull)) void f1(int * const &p); 1143 void test_f1() { 1144 f1(0); // both-warning{{null passed to a callee that requires a non-null argument}} 1145 } 1146 } 1147 1148 namespace nullptrsub { 1149 void a() { 1150 char *f = (char *)0; 1151 f = (char *)((char *)0 - (char *)0); 1152 } 1153 } 1154 1155 namespace incdecbool { 1156 #if __cplusplus >= 201402L 1157 constexpr bool incb(bool c) { 1158 if (!c) 1159 ++c; 1160 else {++c; c++; } 1161 #if __cplusplus >= 202002L 1162 // both-error@-3 {{ISO C++17 does not allow incrementing expression of type bool}} 1163 // both-error@-3 2{{ISO C++17 does not allow incrementing expression of type bool}} 1164 #else 1165 // both-warning@-6 {{incrementing expression of type bool is deprecated and incompatible with C++17}} 1166 #endif 1167 return c; 1168 } 1169 static_assert(incb(false), ""); 1170 static_assert(incb(true), ""); 1171 static_assert(incb(true) == 1, ""); 1172 #endif 1173 1174 1175 #if __cplusplus == 201103L 1176 constexpr bool foo() { // both-error {{never produces a constant expression}} 1177 bool b = true; // both-warning {{variable declaration in a constexpr function is a C++14 extension}} 1178 b++; // both-warning {{incrementing expression of type bool is deprecated and incompatible with C++17}} \ 1179 // both-warning {{use of this statement in a constexpr function is a C++14 extension}} \ 1180 // both-note 2{{subexpression not valid in a constant expression}} 1181 1182 return b; 1183 } 1184 static_assert(foo() == 1, ""); // both-error {{not an integral constant expression}} \ 1185 // both-note {{in call to}} 1186 #endif 1187 1188 1189 1190 } 1191 1192 #if __cplusplus >= 201402L 1193 constexpr int externvar1() { // both-error {{never produces a constant expression}} 1194 extern char arr[]; // both-note {{declared here}} 1195 return arr[0]; // both-note {{read of non-constexpr variable 'arr'}} 1196 } 1197 namespace externarr { 1198 extern int arr[]; 1199 constexpr int *externarrindex = &arr[0]; /// No diagnostic. 1200 } 1201 1202 1203 namespace StmtExprs { 1204 constexpr int foo() { 1205 ({ 1206 int i; 1207 for (i = 0; i < 76; i++) {} 1208 i; // both-warning {{expression result unused}} 1209 }); 1210 return 76; 1211 } 1212 static_assert(foo() == 76, ""); 1213 1214 namespace CrossFuncLabelDiff { 1215 constexpr long a(bool x) { return x ? 0 : (intptr_t)&&lbl + (0 && ({lbl: 0;})); } 1216 } 1217 } 1218 #endif 1219 1220 namespace Extern { 1221 constexpr extern char Oops = 1; 1222 static_assert(Oops == 1, ""); 1223 1224 #if __cplusplus >= 201402L 1225 struct NonLiteral { 1226 NonLiteral() {} 1227 }; 1228 NonLiteral nl; 1229 constexpr NonLiteral &ExternNonLiteralVarDecl() { 1230 extern NonLiteral nl; 1231 return nl; 1232 } 1233 static_assert(&ExternNonLiteralVarDecl() == &nl, ""); 1234 #endif 1235 1236 struct A { 1237 int b; 1238 }; 1239 1240 extern constexpr A a{12}; 1241 static_assert(a.b == 12, ""); 1242 } 1243 1244 #if __cplusplus >= 201402L 1245 constexpr int StmtExprEval() { 1246 if (({ 1247 while (0); 1248 true; 1249 })) { 1250 return 2; 1251 } 1252 return 1; 1253 } 1254 static_assert(StmtExprEval() == 2, ""); 1255 1256 constexpr int ReturnInStmtExpr() { // both-error {{never produces a constant expression}} 1257 return ({ 1258 return 1; // both-note 2{{this use of statement expressions is not supported in a constant expression}} 1259 2; 1260 }); 1261 } 1262 static_assert(ReturnInStmtExpr() == 1, ""); // both-error {{not an integral constant expression}} \ 1263 // both-note {{in call to}} 1264 1265 #endif 1266 1267 namespace ComparisonAgainstOnePastEnd { 1268 int a, b; 1269 static_assert(&a + 1 == &b, ""); // both-error {{not an integral constant expression}} \ 1270 // both-note {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}} 1271 static_assert(&a == &b + 1, ""); // both-error {{not an integral constant expression}} \ 1272 // both-note {{comparison against pointer '&b + 1' that points past the end of a complete object has unspecified value}} 1273 1274 static_assert(&a + 1 == &b + 1, ""); // both-error {{static assertion failed}} 1275 }; 1276 1277 namespace NTTP { 1278 template <typename _Tp, unsigned _Nm> 1279 constexpr unsigned 1280 size(const _Tp (&)[_Nm]) noexcept 1281 { return _Nm; } 1282 1283 template <char C> 1284 static int write_padding() { 1285 static const char Chars[] = {C}; 1286 1287 return size(Chars); 1288 } 1289 } 1290 1291 #if __cplusplus >= 201402L 1292 namespace UnaryOpError { 1293 constexpr int foo() { 1294 int f = 0; 1295 ++g; // both-error {{use of undeclared identifier 'g'}} 1296 return f; 1297 } 1298 } 1299 #endif 1300 1301 namespace VolatileReads { 1302 const volatile int b = 1; 1303 static_assert(b, ""); // both-error {{not an integral constant expression}} \ 1304 // both-note {{read of volatile-qualified type 'const volatile int' is not allowed in a constant expression}} 1305 } 1306 #if __cplusplus >= 201703L 1307 namespace { 1308 struct C { 1309 int x; 1310 }; 1311 1312 template <const C *p> void f() { 1313 const auto &[c] = *p; 1314 &c; // both-warning {{expression result unused}} 1315 } 1316 } 1317 #endif 1318 1319 void localConstexpr() { 1320 constexpr int a = 1/0; // both-error {{must be initialized by a constant expression}} \ 1321 // both-note {{division by zero}} \ 1322 // both-warning {{division by zero is undefined}} \ 1323 // both-note {{declared here}} 1324 static_assert(a == 0, ""); // both-error {{not an integral constant expression}} \ 1325 // both-note {{initializer of 'a' is not a constant expression}} 1326 } 1327