Debian Codegraph @

clang/test/AST/Interp/literals.cpp

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