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
6 #define INT_MIN (~__INT_MAX__)
7 #define INT_MAX __INT_MAX__
9 typedef __INTPTR_TYPE__
intptr_t;
10 typedef __PTRDIFF_TYPE__
ptrdiff_t;
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}}
20 constexpr void* v
= nullptr;
21 static_assert(__null
== v
, "");
23 constexpr int number
= 10;
24 static_assert(number
== 10, "");
25 static_assert(number
!= 10, ""); // both-error{{failed}} \
26 // both-note{{evaluates to}}
28 static_assert(__objc_yes
, "");
29 static_assert(!__objc_no
, "");
31 constexpr bool b
= number
;
33 constexpr int one
= true;
34 static_assert(one
== 1, "");
36 constexpr bool b2
= bool();
37 static_assert(!b2
, "");
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}}
48 const int x
= *(volatile int*)0x1234;
49 static_assert((void{}, true), "");
51 namespace ScalarTypes
{
52 constexpr int ScalarInitInt
= int();
53 static_assert(ScalarInitInt
== 0, "");
54 constexpr float ScalarInitFloat
= float();
55 static_assert(ScalarInitFloat
== 0.0f
, "");
57 static_assert(decltype(nullptr)() == nullptr, "");
60 constexpr T
getScalar() { return T(); }
62 static_assert(getScalar
<const int>() == 0, "");
63 static_assert(getScalar
<const double>() == 0.0, "");
65 static_assert(getScalar
<void*>() == nullptr, "");
66 static_assert(getScalar
<void(*)(void)>() == nullptr, "");
71 static_assert(getScalar
<E
>() == First
, "");
76 constexpr int S::* MemberPtr
= &S::v
;
77 static_assert(getScalar
<decltype(MemberPtr
)>() == nullptr, "");
79 #if __cplusplus >= 201402L
80 constexpr void Void(int n
) {
84 constexpr int void_test
= (Void(0), 1);
85 static_assert(void_test
== 1, "");
89 namespace IntegralCasts
{
91 constexpr unsigned int ui
= i
;
92 static_assert(ui
== 12, "");
93 constexpr unsigned int ub
= !false;
94 static_assert(ub
== 1, "");
96 constexpr int si
= ui
;
97 static_assert(si
== 12, "");
98 constexpr int sb
= true;
99 static_assert(sb
== 1, "");
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
, "");
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}}
116 constexpr bool getTrue() { return true; }
117 constexpr bool getFalse() { return false; }
118 constexpr void* getNull() { return nullptr; }
120 constexpr int neg(int m
) { return -m
; }
121 constexpr bool inv(bool b
) { return !b
; }
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}}
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
, "");
142 static_assert(-(1 << 31), ""); // both-error {{not an integral constant expression}} \
143 // both-note {{outside the range of representable values}}
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, "");
156 constexpr E e
= static_cast<E
>(0);
157 static_assert(~e
== -1, "");
160 constexpr int m
= 10;
161 constexpr const int *p
= &m
;
162 static_assert(p
!= nullptr, "");
163 static_assert(*p
== 10, "");
165 constexpr const int* getIntPointer() {
168 static_assert(getIntPointer() == &m
, "");
169 static_assert(*getIntPointer() == 10, "");
171 constexpr int gimme(int k
) {
174 static_assert(gimme(5) == 5, "");
176 namespace PointerToBool
{
178 constexpr void *N
= nullptr;
179 constexpr bool B
= N
;
180 static_assert(!B
, "");
181 static_assert(!N
, "");
183 constexpr float F
= 1.0;
184 constexpr const float *FP
= &F
;
185 static_assert(FP
, "");
186 static_assert(!!FP
, "");
189 namespace PointerComparison
{
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 'nullptr' and '&s.a' has unspecified value}}
199 constexpr bool v3
= null
== pv
; // ok
200 constexpr bool v4
= qv
== pv
; // ok
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 '&s.b' and 'nullptr' has unspecified value}}
207 constexpr bool v7
= qv
<= (void*)&s
.b
; // ok
209 constexpr ptrdiff_t m
= &m
- &m
;
210 static_assert(m
== 0, "");
212 constexpr ptrdiff_t m2
= (&m2
+ 1) - (&m2
+ 1);
213 static_assert(m2
== 0, "");
215 constexpr long m3
= (&m3
+ 1) - (&m3
);
216 static_assert(m3
== 1, "");
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}}
223 static_assert(alignof(char&) == 1, "");
225 constexpr int soint
= sizeof(int);
226 constexpr int souint
= sizeof(unsigned int);
227 static_assert(soint
== souint
, "");
229 static_assert(sizeof(&soint
) == sizeof(void*), "");
230 static_assert(sizeof(&soint
) == sizeof(nullptr), "");
232 static_assert(sizeof(long) == sizeof(unsigned long), "");
233 static_assert(sizeof(char) == sizeof(unsigned char), "");
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]), "");
240 constexpr bool arrB
[N
] = {true, true, true, true};
241 static_assert(sizeof(arrB
) == N
* sizeof(bool), "");
243 static_assert(sizeof(bool) == 1, "");
244 static_assert(sizeof(char) == 1, "");
246 constexpr int F
= sizeof(void); // both-error{{incomplete type 'void'}}
248 constexpr int F2
= sizeof(gimme
); // both-error{{to a function type}}
254 constexpr void (S::*Func
)() = &S::func
;
255 static_assert(sizeof(Func
) == sizeof(&S::func
), "");
260 constexpr int oofda
= sizeof(int[n
++]); // both-error {{must be initialized by a constant expression}}
263 #if __cplusplus >= 201402L
264 constexpr int IgnoredRejected() { // ref-error {{never produces a constant expression}}
266 sizeof(int[n
++]); // both-warning {{expression result unused}} \
267 // ref-note 2{{subexpression not valid in a constant expression}}
270 /// FIXME: This is rejected because the parameter so sizeof() is not constant.
271 /// produce a proper diagnostic.
272 static_assert(IgnoredRejected() == 0, ""); // both-error {{not an integral constant expression}} \
273 // ref-note {{in call to 'IgnoredRejected()'}}
277 #if __cplusplus >= 202002L
278 /// FIXME: The following code should be accepted.
279 consteval
int foo(int n
) { // ref-error {{consteval function never produces a constant expression}}
280 return sizeof(int[n
]); // ref-note 3{{not valid in a constant expression}}
282 constinit
int var
= foo(5); // ref-error {{not a constant expression}} \
283 // ref-note 2{{in call to}} \
284 // ref-error {{does not have a constant initializer}} \
285 // ref-note {{required by 'constinit' specifier}} \
286 // expected-error {{is not a constant expression}} \
287 // expected-error {{does not have a constant initializer}} \
288 // expected-note {{required by 'constinit' specifier}} \
294 static_assert(2 % 2 == 0, "");
295 static_assert(2 % 1 == 0, "");
296 static_assert(-3 % 4 == -3, "");
297 static_assert(4 % -2 == 0, "");
298 static_assert(-3 % -4 == -3, "");
300 constexpr int zero() { return 0; }
301 static_assert(10 % zero() == 20, ""); // both-error {{not an integral constant expression}} \
302 // both-note {{division by zero}}
304 static_assert(true % true == 0, "");
305 static_assert(false % true == 0, "");
306 static_assert(true % false == 10, ""); // both-error {{not an integral constant expression}} \
307 // both-note {{division by zero}}
308 constexpr int x
= INT_MIN
% - 1; // both-error {{must be initialized by a constant expression}} \
309 // both-note {{value 2147483648 is outside the range}}
313 constexpr int zero() { return 0; }
314 static_assert(12 / 3 == 4, "");
315 static_assert(12 / zero() == 12, ""); // both-error {{not an integral constant expression}} \
316 // both-note {{division by zero}}
317 static_assert(12 / -3 == -4, "");
318 static_assert(-12 / 3 == -4, "");
321 constexpr int LHS
= 12;
322 constexpr long unsigned RHS
= 3;
323 static_assert(LHS
/ RHS
== 4, "");
325 constexpr int x
= INT_MIN
/ - 1; // both-error {{must be initialized by a constant expression}} \
326 // both-note {{value 2147483648 is outside the range}}
330 constexpr bool isEven(int n
) {
331 return n
% 2 == 0 ? true : false;
333 static_assert(isEven(2), "");
334 static_assert(!isEven(3), "");
335 static_assert(isEven(100), "");
337 constexpr int M
= 5 ? 10 : 20;
338 static_assert(M
== 10, "");
340 static_assert(5 ? 13 : 16 == 13, "");
341 static_assert(0 ? 13 : 16 == 16, "");
343 static_assert(number
?: -15 == number
, "");
344 static_assert(0 ?: 100 == 100 , "");
346 #if __cplusplus >= 201402L
347 constexpr int N
= 20;
348 constexpr int foo() {
349 int m
= N
> 0 ? 5 : 10;
351 return m
== 5 ? isEven(m
) : true;
353 static_assert(foo() == false, "");
355 constexpr int dontCallMe(unsigned m
) {
356 if (m
== 0) return 0;
357 return dontCallMe(m
- 2);
360 // Can't call this because it will run into infinite recursion.
361 constexpr int assertNotReached() {
362 return dontCallMe(3);
365 constexpr int testCond() {
366 return true ? 5 : assertNotReached();
369 constexpr int testCond2() {
370 return false ? assertNotReached() : 10;
373 static_assert(testCond() == 5, "");
374 static_assert(testCond2() == 10, "");
381 static_assert((10 & 1) == 0, "");
382 static_assert((10 & 10) == 10, "");
384 static_assert((1337 & -1) == 1337, "");
385 static_assert((0 & gimme(12)) == 0, "");
389 static_assert((10 | 1) == 11, "");
390 static_assert((10 | 10) == 10, "");
392 static_assert((1337 | -1) == -1, "");
393 static_assert((0 | gimme(12)) == 12, "");
394 static_assert((12 | true) == 13, "");
398 #pragma clang diagnostic push
399 #pragma clang diagnostic ignored "-Wxor-used-as-pow"
400 static_assert((10 ^ 1) == 11, "");
401 static_assert((10 ^ 10) == 0, "");
407 static_assert((1337 ^ -1) == -1338, "");
408 static_assert((0 | gimme(12)) == 12, "");
409 static_assert((12 ^ true) == 13, "");
410 static_assert((12 ^ ONE
) == 13, "");
411 #pragma clang diagnostic pop
414 #if __cplusplus >= 201402L
415 constexpr bool IgnoredUnary() {
417 !bo
; // both-warning {{expression result unused}}
420 static_assert(IgnoredUnary(), "");
424 constexpr const char *S
= "abc";
425 static_assert(S
[0] == 97, "");
426 static_assert(S
[1] == 98, "");
427 static_assert(S
[2] == 99, "");
428 static_assert(S
[3] == 0, "");
430 static_assert("foobar"[2] == 'o', "");
431 static_assert(2["foobar"] == 'o', "");
433 constexpr const wchar_t *wide
= L
"bar";
434 static_assert(wide
[0] == L
'b', "");
436 constexpr const char32_t
*u32
= U
"abc";
437 static_assert(u32
[1] == U
'b', "");
439 constexpr char32_t c
= U
'\U0001F60E';
440 static_assert(c
== 0x0001F60EL
, "");
442 constexpr char k
= -1;
443 static_assert(k
== -1, "");
445 static_assert('\N{LATIN CAPITAL LETTER E}' == 'E', "");
446 static_assert('\t' == 9, "");
448 #pragma clang diagnostic push
449 #pragma clang diagnostic ignored "-Wmultichar"
450 constexpr int mc
= 'abc';
451 static_assert(mc
== 'abc', "");
452 __WCHAR_TYPE__ wm
= L
'abc'; // both-error{{wide character literals may not contain multiple characters}}
453 __WCHAR_TYPE__ wu
= u
'abc'; // both-error{{Unicode character literals may not contain multiple characters}}
454 __WCHAR_TYPE__ wU
= U
'abc'; // both-error{{Unicode character literals may not contain multiple characters}}
455 #if __cplusplus > 201103L
456 __WCHAR_TYPE__ wu8
= u8
'abc'; // both-error{{Unicode character literals may not contain multiple characters}}
459 #pragma clang diagnostic pop
461 constexpr char foo
[12] = "abc";
462 static_assert(foo
[0] == 'a', "");
463 static_assert(foo
[1] == 'b', "");
464 static_assert(foo
[2] == 'c', "");
465 static_assert(foo
[3] == 0, "");
466 static_assert(foo
[11] == 0, "");
468 constexpr char foo2
[] = "abc\0def";
469 static_assert(foo2
[0] == 'a', "");
470 static_assert(foo2
[3] == '\0', "");
471 static_assert(foo2
[6] == 'f', "");
472 static_assert(foo2
[7] == '\0', "");
473 static_assert(foo2
[8] == '\0', ""); // both-error {{not an integral constant expression}} \
474 // both-note {{read of dereferenced one-past-the-end pointer}}
476 constexpr char foo3
[4] = "abc";
477 static_assert(foo3
[3] == '\0', "");
478 static_assert(foo3
[4] == '\0', ""); // both-error {{not an integral constant expression}} \
479 // both-note {{read of dereferenced one-past-the-end pointer}}
481 constexpr char foo4
[2] = "abcd"; // both-error {{initializer-string for char array is too long}}
482 static_assert(foo4
[0] == 'a', "");
483 static_assert(foo4
[1] == 'b', "");
484 static_assert(foo4
[2] == '\0', ""); // both-error {{not an integral constant expression}} \
485 // both-note {{read of dereferenced one-past-the-end pointer}}
487 constexpr char foo5
[12] = "abc\xff";
488 #if defined(__CHAR_UNSIGNED__) || __CHAR_BIT__ > 8
489 static_assert(foo5
[3] == 255, "");
491 static_assert(foo5
[3] == -1, "");
495 #if __cplusplus > 201402L
497 constexpr int zero() {
505 static_assert(zero() == 0, "");
507 constexpr int preInc() {
511 static_assert(preInc() == 1, "");
513 constexpr int postInc() {
517 static_assert(postInc() == 0, "");
519 constexpr int preDec() {
523 static_assert(preDec() == -1, "");
525 constexpr int postDec() {
529 static_assert(postDec() == 0, "");
531 constexpr int three() {
533 return ++a
+ ++a
; // both-warning {{multiple unsequenced modifications to 'a'}}
535 static_assert(three() == 3, "");
537 constexpr bool incBool() {
539 return ++b
; // both-error {{ISO C++17 does not allow incrementing expression of type bool}}
541 static_assert(incBool(), "");
543 /// FIXME: The diagnostics for pre-inc/dec of pointers doesn't match the
544 /// current interpreter. But they are stil OK.
545 template<typename T
, bool Inc
, bool Pre
>
546 constexpr int uninit() {
550 ++a
; // ref-note 3{{increment of uninitialized}} \
551 // expected-note 2{{increment of uninitialized}} \
552 // expected-note {{read of uninitialized}}
554 a
++; // ref-note 2{{increment of uninitialized}} \
555 // expected-note 2{{increment of uninitialized}}
558 --a
; // ref-note 3{{decrement of uninitialized}} \
559 // expected-note 2{{decrement of uninitialized}} \
560 // expected-note {{read of uninitialized}}
562 a
--; // ref-note 2{{decrement of uninitialized}} \
563 // expected-note 2{{decrement of uninitialized}}
567 static_assert(uninit
<int, true, true>(), ""); // both-error {{not an integral constant expression}} \
568 // both-note {{in call to 'uninit<int, true, true>()'}}
569 static_assert(uninit
<int, false, true>(), ""); // both-error {{not an integral constant expression}} \
570 // both-note {{in call to 'uninit<int, false, true>()'}}
572 static_assert(uninit
<float, true, true>(), ""); // both-error {{not an integral constant expression}} \
573 // both-note {{in call to 'uninit<float, true, true>()'}}
574 static_assert(uninit
<float, false, true>(), ""); // both-error {{not an integral constant expression}} \
575 // both-note {{in call to 'uninit<float, false, true>()'}}
576 static_assert(uninit
<float, true, false>(), ""); // both-error {{not an integral constant expression}} \
577 // both-note {{in call to 'uninit<float, true, false>()'}}
578 static_assert(uninit
<float, false, false>(), ""); // both-error {{not an integral constant expression}} \
579 // both-note {{in call to 'uninit<float, false, false>()'}}
581 static_assert(uninit
<int*, true, true>(), ""); // both-error {{not an integral constant expression}} \
582 // both-note {{in call to 'uninit<int *, true, true>()'}}
583 static_assert(uninit
<int*, false, true>(), ""); // both-error {{not an integral constant expression}} \
584 // both-note {{in call to 'uninit<int *, false, true>()'}}
585 static_assert(uninit
<int*, true, false>(), ""); // both-error {{not an integral constant expression}} \
586 // both-note {{in call to 'uninit<int *, true, false>()'}}
587 static_assert(uninit
<int*, false, false>(), ""); // both-error {{not an integral constant expression}} \
588 // both-note {{in call to 'uninit<int *, false, false>()'}}
590 constexpr int OverFlow() { // both-error {{never produces a constant expression}}
592 ++a
; // both-note 2{{is outside the range}}
595 static_assert(OverFlow() == -1, ""); // both-error {{not an integral constant expression}} \
596 // both-note {{in call to 'OverFlow()'}}
598 constexpr int UnderFlow() { // both-error {{never produces a constant expression}}
600 --a
; // both-note 2{{is outside the range}}
603 static_assert(UnderFlow() == -1, ""); // both-error {{not an integral constant expression}} \
604 // both-note {{in call to 'UnderFlow()'}}
606 constexpr int getTwo() {
610 static_assert(getTwo() == 2, "");
612 constexpr int sub(int a
) {
615 static_assert(sub(7) == 5, "");
617 constexpr int add(int a
, int b
) {
618 a
+= b
; // both-note {{is outside the range of representable values}}
621 static_assert(add(1, 2) == 3, "");
622 static_assert(add(INT_MAX
, 1) == 0, ""); // both-error {{not an integral constant expression}} \
623 // both-note {{in call to 'add}}
625 constexpr int sub(int a
, int b
) {
626 a
-= b
; // both-note {{is outside the range of representable values}}
629 static_assert(sub(10, 20) == -10, "");
630 static_assert(sub(INT_MIN
, 1) == 0, ""); // both-error {{not an integral constant expression}} \
631 // both-note {{in call to 'sub}}
633 constexpr int subAll(int a
) {
636 static_assert(subAll(213) == 0, "");
638 constexpr bool BoolOr(bool b1
, bool b2
) {
644 static_assert(BoolOr(true, true), "");
645 static_assert(BoolOr(true, false), "");
646 static_assert(BoolOr(false, true), "");
647 static_assert(!BoolOr(false, false), "");
649 constexpr int IntOr(unsigned a
, unsigned b
) {
655 static_assert(IntOr(10, 1) == 11, "");
656 static_assert(IntOr(1337, -1) == -1, "");
657 static_assert(IntOr(0, 12) == 12, "");
659 constexpr bool BoolAnd(bool b1
, bool b2
) {
665 static_assert(BoolAnd(true, true), "");
666 static_assert(!BoolAnd(true, false), "");
667 static_assert(!BoolAnd(false, true), "");
668 static_assert(!BoolAnd(false, false), "");
670 constexpr int IntAnd(unsigned a
, unsigned b
) {
676 static_assert(IntAnd(10, 1) == 0, "");
677 static_assert(IntAnd(1337, -1) == 1337, "");
678 static_assert(IntAnd(0, 12) == 0, "");
680 constexpr bool BoolXor(bool b1
, bool b2
) {
686 static_assert(!BoolXor(true, true), "");
687 static_assert(BoolXor(true, false), "");
688 static_assert(BoolXor(false, true), "");
689 static_assert(!BoolXor(false, false), "");
691 constexpr int IntXor(unsigned a
, unsigned b
) {
697 static_assert(IntXor(10, 1) == 11, "");
698 static_assert(IntXor(10, 10) == 0, "");
699 static_assert(IntXor(12, true) == 13, "");
701 constexpr bool BoolRem(bool b1
, bool b2
) {
707 static_assert(!BoolRem(true, true), "");
708 static_assert(!BoolRem(false, true), "");
710 constexpr int IntRem(int a
, int b
) {
713 r
%= b
; // both-note {{division by zero}} \
714 // both-note {{outside the range of representable values}}
717 static_assert(IntRem(2, 2) == 0, "");
718 static_assert(IntRem(2, 1) == 0, "");
719 static_assert(IntRem(9, 7) == 2, "");
720 static_assert(IntRem(5, 0) == 0, ""); // both-error {{not an integral constant expression}} \
721 // both-note {{in call to 'IntRem(5, 0)'}}
723 static_assert(IntRem(INT_MIN
, -1) == 0, ""); // both-error {{not an integral constant expression}} \
724 // both-note {{in call to 'IntRem}}
726 constexpr bool BoolDiv(bool b1
, bool b2
) {
732 static_assert(BoolDiv(true, true), "");
733 static_assert(!BoolDiv(false, true), "");
735 constexpr int IntDiv(int a
, int b
) {
738 r
/= b
; // both-note {{division by zero}} \
739 // both-note {{outside the range of representable values}}
742 static_assert(IntDiv(2, 2) == 1, "");
743 static_assert(IntDiv(12, 20) == 0, "");
744 static_assert(IntDiv(2, 1) == 2, "");
745 static_assert(IntDiv(9, 7) == 1, "");
746 static_assert(IntDiv(5, 0) == 0, ""); // both-error {{not an integral constant expression}} \
747 // both-note {{in call to 'IntDiv(5, 0)'}}
749 static_assert(IntDiv(INT_MIN
, -1) == 0, ""); // both-error {{not an integral constant expression}} \
750 // both-note {{in call to 'IntDiv}}
752 constexpr bool BoolMul(bool b1
, bool b2
) {
758 static_assert(BoolMul(true, true), "");
759 static_assert(!BoolMul(true, false), "");
760 static_assert(!BoolMul(false, true), "");
761 static_assert(!BoolMul(false, false), "");
763 constexpr int IntMul(int a
, int b
) {
766 r
*= b
; // both-note {{is outside the range of representable values of type 'int'}}
769 static_assert(IntMul(2, 2) == 4, "");
770 static_assert(IntMul(12, 20) == 240, "");
771 static_assert(IntMul(2, 1) == 2, "");
772 static_assert(IntMul(9, 7) == 63, "");
773 static_assert(IntMul(INT_MAX
, 2) == 0, ""); // both-error {{not an integral constant expression}} \
774 // both-note {{in call to 'IntMul}}
775 constexpr int arr
[] = {1,2,3};
776 constexpr int ptrInc1() {
781 static_assert(ptrInc1() == 3, "");
783 constexpr int ptrInc2() {
787 static_assert(ptrInc2() == 2, "");
789 constexpr int ptrInc3() { // both-error {{never produces a constant expression}}
791 p
+= 12; // both-note {{cannot refer to element 12 of array of 3 elements}}
795 constexpr int ptrIncDec1() {
801 static_assert(ptrIncDec1() == 2, "");
803 constexpr int ptrDec1() { // both-error {{never produces a constant expression}}
805 p
-= 1; // both-note {{cannot refer to element -1 of array of 3 elements}}
809 /// This used to leave a 0 on the stack instead of the previous
811 constexpr int bug1Inc() {
816 static_assert(bug1Inc() == 3);
818 constexpr int bug1Dec() {
823 static_assert(bug1Dec() == 3);
829 // RHS should be evaluated before LHS, so this should
835 static_assert(f() == 3, "");
837 int nonconst(int a
) { // both-note 4{{declared here}}
838 static_assert(a
++, ""); // both-error {{not an integral constant expression}} \
839 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
840 static_assert(a
--, ""); // both-error {{not an integral constant expression}} \
841 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
842 static_assert(++a
, ""); // both-error {{not an integral constant expression}} \
843 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
844 static_assert(--a
, ""); // both-error {{not an integral constant expression}} \
845 // both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
851 namespace CompoundLiterals
{
852 constexpr int get5() {
853 return (int[]){1,2,3,4,5}[4];
855 static_assert(get5() == 5, "");
857 constexpr int get6(int f
= (int[]){1,2,6}[2]) { // ref-note {{subexpression not valid in a constant expression}} \
858 // ref-note {{declared here}}
861 static_assert(get6(6) == 6, "");
862 // FIXME: Who's right here?
863 static_assert(get6() == 6, ""); // ref-error {{not an integral constant expression}}
865 constexpr int x
= (int){3};
866 static_assert(x
== 3, "");
867 #if __cplusplus >= 201402L
868 constexpr int getX() {
873 static_assert(getX() == 5, "");
876 #if __cplusplus >= 202002L
877 constexpr int get3() {
882 static_assert(get3() == 3, "");
886 namespace TypeTraits
{
887 static_assert(__is_trivial(int), "");
888 static_assert(__is_trivial(float), "");
889 static_assert(__is_trivial(E
), "");
891 static_assert(__is_trivial(S
), "");
895 static_assert(!__is_trivial(S2
), "");
897 template <typename T
>
899 constexpr bool foo() const { return __is_trivial(T
); }
905 static_assert(S3
<U
>{}.foo(), "");
906 static_assert(!S3
<T
>{}.foo(), "");
909 typedef Int IntAr
[10];
910 typedef const IntAr ConstIntAr
;
911 typedef ConstIntAr ConstIntArAr
[4];
913 static_assert(__array_rank(IntAr
) == 1, "");
914 static_assert(__array_rank(ConstIntArAr
) == 2, "");
916 static_assert(__array_extent(IntAr
, 0) == 10, "");
917 static_assert(__array_extent(ConstIntArAr
, 0) == 4, "");
918 static_assert(__array_extent(ConstIntArAr
, 1) == 10, "");
921 #if __cplusplus >= 201402L
922 constexpr int ignoredDecls() {
923 static_assert(true, "");
931 static_assert(ignoredDecls() == 12, "");
933 namespace DiscardExprs
{
934 #pragma clang diagnostic push
935 #pragma clang diagnostic ignored "-Wunused-value"
936 typedef struct _GUID
{
937 __UINT32_TYPE__ Data1
;
938 __UINT16_TYPE__ Data2
;
939 __UINT16_TYPE__ Data3
;
940 __UINT8_TYPE__ Data4
[8];
942 class __declspec(uuid("000000A0-0000-0000-C000-000000000049")) GuidType
;
945 constexpr int ignoredExprs() {
952 /// Ignored MaterializeTemporaryExpr.
953 struct B
{ const int &a
; };
973 __builtin_offsetof(A
, a
);
980 __uuidof(number
); // both-error {{cannot call operator __uuidof on a type with no GUID}}
986 static_assert(ignoredExprs() == 0, "");
988 constexpr int oh_my(int x
) {
992 static_assert(oh_my(0) == 1, "");
994 constexpr int oh_my2(int x
) {
999 static_assert(oh_my2(0) == 1, "");
1002 /// Ignored comma expressions still have their
1003 /// expressions evaluated.
1004 constexpr int Comma(int start
) {
1008 (void)i
++,(void)i
++;
1011 constexpr int Value
= Comma(5);
1012 static_assert(Value
== 8, "");
1014 /// Ignored MemberExprs need to still evaluate the Base
1016 constexpr A
callme(int &i
) {
1020 constexpr int ignoredMemberExpr() {
1025 static_assert(ignoredMemberExpr() == 1, "");
1028 constexpr int foo() {
1032 static_assert(foo
<3>() == 3, "");
1035 consteval ATemp
ret_a() const { return ATemp
{}; }
1039 int k
= (ATemp().ret_a(), 0);
1042 #pragma clang diagnostic pop
1046 namespace PredefinedExprs
{
1047 #if __cplusplus >= 201402L
1048 template<typename CharT
>
1049 constexpr bool strings_match(const CharT
*str1
, const CharT
*str2
) {
1050 while (*str1
&& *str2
) {
1051 if (*str1
++ != *str2
++)
1055 return *str1
== *str2
;
1059 static_assert(strings_match(__FUNCSIG__
, "void __cdecl PredefinedExprs::foo(void)"), "");
1060 static_assert(strings_match(L__FUNCSIG__
, L
"void __cdecl PredefinedExprs::foo(void)"), "");
1061 static_assert(strings_match(L__FUNCTION__
, L
"foo"), "");
1062 static_assert(strings_match(__FUNCTION__
, "foo"), "");
1063 static_assert(strings_match(__func__
, "foo"), "");
1064 static_assert(strings_match(__PRETTY_FUNCTION__
, "void PredefinedExprs::foo()"), "");
1067 constexpr char heh(unsigned index
) {
1068 __FUNCTION__
; // both-warning {{result unused}}
1069 __extension__ __FUNCTION__
; // both-warning {{result unused}}
1070 return __FUNCTION__
[index
];
1072 static_assert(heh(0) == 'h', "");
1073 static_assert(heh(1) == 'e', "");
1074 static_assert(heh(2) == 'h', "");
1079 constexpr int foo() noexcept
{
1082 static_assert(noexcept(foo()), "");
1083 constexpr int foo2() {
1086 static_assert(!noexcept(foo2()), "");
1088 #if __cplusplus > 201402L
1091 (void)noexcept(++b
); // both-warning {{expression with side effects has no effect in an unevaluated context}}
1095 static_assert(a() == 0, "");
1099 namespace PointerCasts
{
1100 constexpr int M
= 10;
1101 constexpr const int *P
= &M
;
1102 constexpr intptr_t A
= (intptr_t)P
; // both-error {{must be initialized by a constant expression}} \
1103 // both-note {{cast that performs the conversions of a reinterpret_cast}}
1105 int array
[(intptr_t)(char*)0]; // both-warning {{variable length array folded to constant array}}
1108 namespace InvalidDeclRefs
{
1109 bool b00
; // both-note {{declared here}}
1110 static_assert(b00
, ""); // both-error {{not an integral constant expression}} \
1111 // both-note {{read of non-const variable}}
1113 float b01
; // both-note {{declared here}}
1114 static_assert(b01
, ""); // both-error {{not an integral constant expression}} \
1115 // both-note {{read of non-constexpr variable}}
1117 extern const int b02
; // both-note {{declared here}}
1118 static_assert(b02
, ""); // both-error {{not an integral constant expression}} \
1119 // both-note {{initializer of 'b02' is unknown}}
1121 int b03
= 3; // both-note {{declared here}}
1122 static_assert(b03
, ""); // both-error {{not an integral constant expression}} \
1123 // both-note {{read of non-const variable}}
1126 constexpr int *varp
= &var
; // Ok.
1129 namespace NonConstReads
{
1130 void *p
= nullptr; // both-note {{declared here}}
1131 static_assert(!p
, ""); // both-error {{not an integral constant expression}} \
1132 // both-note {{read of non-constexpr variable 'p'}}
1134 int arr
[!p
]; // both-error {{variable length array}}
1136 int z
; // both-note {{declared here}}
1137 static_assert(z
== 0, ""); // both-error {{not an integral constant expression}} \
1138 // both-note {{read of non-const variable 'z'}}
1141 /// This test passes a MaterializedTemporaryExpr to evaluateAsRValue.
1142 /// That needs to return a null pointer after the lvalue-to-rvalue conversion.
1143 /// We used to fail to do that.
1144 namespace rdar8769025
{
1145 __attribute__((nonnull
)) void f1(int * const &p
);
1147 f1(0); // both-warning{{null passed to a callee that requires a non-null argument}}
1151 namespace nullptrsub
{
1153 char *f
= (char *)0;
1154 f
= (char *)((char *)0 - (char *)0);
1158 namespace incdecbool
{
1159 #if __cplusplus >= 201402L
1160 constexpr bool incb(bool c
) {
1164 #if __cplusplus >= 202002L
1165 // both-error@-3 {{ISO C++17 does not allow incrementing expression of type bool}}
1166 // both-error@-3 2{{ISO C++17 does not allow incrementing expression of type bool}}
1168 // both-warning@-6 {{incrementing expression of type bool is deprecated and incompatible with C++17}}
1172 static_assert(incb(false), "");
1173 static_assert(incb(true), "");
1174 static_assert(incb(true) == 1, "");
1178 #if __cplusplus == 201103L
1179 constexpr bool foo() { // both-error {{never produces a constant expression}}
1180 bool b
= true; // both-warning {{variable declaration in a constexpr function is a C++14 extension}}
1181 b
++; // both-warning {{incrementing expression of type bool is deprecated and incompatible with C++17}} \
1182 // both-warning {{use of this statement in a constexpr function is a C++14 extension}} \
1183 // both-note 2{{subexpression not valid in a constant expression}}
1187 static_assert(foo() == 1, ""); // both-error {{not an integral constant expression}} \
1188 // both-note {{in call to}}
1195 #if __cplusplus >= 201402L
1196 constexpr int externvar1() { // both-error {{never produces a constant expression}}
1197 extern char arr
[]; // both-note {{declared here}}
1198 return arr
[0]; // both-note {{read of non-constexpr variable 'arr'}}
1200 namespace externarr
{
1202 constexpr int *externarrindex
= &arr
[0]; /// No diagnostic.
1206 namespace StmtExprs
{
1207 constexpr int foo() {
1210 for (i
= 0; i
< 76; i
++) {}
1211 i
; // both-warning {{expression result unused}}
1215 static_assert(foo() == 76, "");
1217 namespace CrossFuncLabelDiff
{
1218 constexpr long a(bool x
) { return x
? 0 : (intptr_t)&&lbl
+ (0 && ({lbl
: 0;})); }
1224 constexpr extern char Oops
= 1;
1225 static_assert(Oops
== 1, "");
1227 #if __cplusplus >= 201402L
1232 constexpr NonLiteral
&ExternNonLiteralVarDecl() {
1233 extern NonLiteral nl
;
1236 static_assert(&ExternNonLiteralVarDecl() == &nl
, "");
1243 extern constexpr A a
{12};
1244 static_assert(a
.b
== 12, "");
1247 #if __cplusplus >= 201402L
1248 constexpr int StmtExprEval() {
1257 static_assert(StmtExprEval() == 2, "");
1259 constexpr int ReturnInStmtExpr() { // both-error {{never produces a constant expression}}
1261 return 1; // both-note 2{{this use of statement expressions is not supported in a constant expression}}
1265 static_assert(ReturnInStmtExpr() == 1, ""); // both-error {{not an integral constant expression}} \
1266 // both-note {{in call to}}
1270 namespace ComparisonAgainstOnePastEnd
{
1272 static_assert(&a
+ 1 == &b
, ""); // both-error {{not an integral constant expression}} \
1273 // both-note {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}}
1274 static_assert(&a
== &b
+ 1, ""); // both-error {{not an integral constant expression}} \
1275 // both-note {{comparison against pointer '&b + 1' that points past the end of a complete object has unspecified value}}
1277 static_assert(&a
+ 1 == &b
+ 1, ""); // both-error {{static assertion failed}}
1281 template <typename _Tp
, unsigned _Nm
>
1283 size(const _Tp (&)[_Nm
]) noexcept
1287 static int write_padding() {
1288 static const char Chars
[] = {C
};
1294 #if __cplusplus >= 201402L
1295 namespace UnaryOpError
{
1296 constexpr int foo() {
1298 ++g
; // both-error {{use of undeclared identifier 'g'}}
1304 namespace VolatileReads
{
1305 const volatile int b
= 1;
1306 static_assert(b
, ""); // both-error {{not an integral constant expression}} \
1307 // both-note {{read of volatile-qualified type 'const volatile int' is not allowed in a constant expression}}
1309 #if __cplusplus >= 201703L
1315 template <const C
*p
> void f() {
1316 const auto &[c
] = *p
;
1317 &c
; // both-warning {{expression result unused}}