Debian Codegraph @

clang/test/AST/Interp/functions.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -pedantic -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++14 -fexperimental-new-constant-interpreter -pedantic -verify=expected,both %s
3 // RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -pedantic -verify=expected,both %s
4 // RUN: %clang_cc1 -pedantic -verify=ref,both %s
5 // RUN: %clang_cc1 -pedantic -std=c++14 -verify=ref,both %s
6 // RUN: %clang_cc1 -pedantic -std=c++20 -verify=ref,both %s
7
8 constexpr void doNothing() {}
9 constexpr int gimme5() {
10 doNothing();
11 return 5;
12 }
13 static_assert(gimme5() == 5, "");
14
15
16 template<typename T> constexpr T identity(T t) {
17 static_assert(true, "");
18 return t;
19 }
20 static_assert(identity(true), "");
21 static_assert(identity(true), ""); /// Compiled bytecode should be cached
22 static_assert(!identity(false), "");
23
24 template<typename A, typename B>
25 constexpr bool sameSize() {
26 static_assert(sizeof(A) == sizeof(B), ""); // both-error {{static assertion failed}} \
27 // both-note {{evaluates to}}
28 return true;
29 }
30 static_assert(sameSize<int, int>(), "");
31 static_assert(sameSize<unsigned int, int>(), "");
32 static_assert(sameSize<char, long>(), ""); // both-note {{in instantiation of function template specialization}}
33
34
35 constexpr auto add(int a, int b) -> int {
36 return identity(a) + identity(b);
37 }
38
39 constexpr int sub(int a, int b) {
40 return a - b;
41 }
42 static_assert(sub(5, 2) == 3, "");
43 static_assert(sub(0, 5) == -5, "");
44
45 constexpr int norm(int n) {
46 if (n >= 0) {
47 return identity(n);
48 }
49 return -identity(n);
50 }
51 static_assert(norm(5) == norm(-5), "");
52
53 constexpr int square(int n) {
54 return norm(n) * norm(n);
55 }
56 static_assert(square(2) == 4, "");
57
58 constexpr int add_second(int a, int b, bool doAdd = true) {
59 if (doAdd)
60 return a + b;
61 return a;
62 }
63 static_assert(add_second(10, 3, true) == 13, "");
64 static_assert(add_second(10, 3) == 13, "");
65 static_assert(add_second(300, -20, false) == 300, "");
66
67
68 constexpr int sub(int a, int b, int c) {
69 return a - b - c;
70 }
71 static_assert(sub(10, 8, 2) == 0, "");
72
73
74 constexpr int recursion(int i) {
75 doNothing();
76 i = i - 1;
77 if (i == 0)
78 return identity(0);
79
80 return recursion(i);
81 }
82 static_assert(recursion(10) == 0, "");
83
84 template<int N = 5>
85 constexpr decltype(N) getNum() {
86 return N;
87 }
88 static_assert(getNum<-2>() == -2, "");
89 static_assert(getNum<10>() == 10, "");
90 static_assert(getNum() == 5, "");
91
92 constexpr int f(); // both-note {{declared here}}
93 static_assert(f() == 5, ""); // both-error {{not an integral constant expression}} \
94 // both-note {{undefined function 'f'}}
95 constexpr int a() {
96 return f();
97 }
98 constexpr int f() {
99 return 5;
100 }
101 static_assert(a() == 5, "");
102
103 constexpr int invalid() {
104 // Invalid expression in visit().
105 while(huh) {} // both-error {{use of undeclared identifier}}
106 return 0;
107 }
108
109 constexpr void invalid2() {
110 int i = 0;
111 // Invalid expression in discard().
112 huh(); // both-error {{use of undeclared identifier}}
113 }
114
115 namespace FunctionPointers {
116 constexpr int add(int a, int b) {
117 return a + b;
118 }
119
120 struct S { int a; };
121 constexpr S getS() {
122 return S{12};
123 }
124
125 constexpr int applyBinOp(int a, int b, int (*op)(int, int)) {
126 return op(a, b); // both-note {{evaluates to a null function pointer}}
127 }
128 static_assert(applyBinOp(1, 2, add) == 3, "");
129 static_assert(applyBinOp(1, 2, nullptr) == 3, ""); // both-error {{not an integral constant expression}} \
130 // both-note {{in call to}}
131
132
133 constexpr int ignoreReturnValue() {
134 int (*foo)(int, int) = add;
135
136 foo(1, 2);
137 return 1;
138 }
139 static_assert(ignoreReturnValue() == 1, "");
140
141 constexpr int createS(S (*gimme)()) {
142 gimme(); // Ignored return value
143 return gimme().a;
144 }
145 static_assert(createS(getS) == 12, "");
146
147 namespace FunctionReturnType {
148 typedef int (*ptr)(int*);
149 typedef ptr (*pm)();
150
151 constexpr int fun1(int* y) {
152 return *y + 10;
153 }
154 constexpr ptr fun() {
155 return &fun1;
156 }
157 static_assert(fun() == nullptr, ""); // both-error {{static assertion failed}}
158
159 constexpr int foo() {
160 int (*f)(int *) = fun();
161 int m = 0;
162
163 m = f(&m);
164
165 return m;
166 }
167 static_assert(foo() == 10, "");
168
169 struct S {
170 int i;
171 void (*fp)();
172 };
173
174 constexpr S s{ 12 };
175 static_assert(s.fp == nullptr, ""); // zero-initialized function pointer.
176
177 constexpr int (*op)(int, int) = add;
178 constexpr bool b = op;
179 static_assert(op, "");
180 static_assert(!!op, "");
181 constexpr int (*op2)(int, int) = nullptr;
182 static_assert(!op2, "");
183
184 int m() { return 5;} // both-note {{declared here}}
185 constexpr int (*invalidFnPtr)() = m;
186 static_assert(invalidFnPtr() == 5, ""); // both-error {{not an integral constant expression}} \
187 // both-note {{non-constexpr function 'm'}}
188
189
190 namespace ToBool {
191 void mismatched(int x) {}
192 typedef void (*callback_t)(int);
193 void foo() {
194 callback_t callback = (callback_t)mismatched; // warns
195 /// Casts a function pointer to a boolean and then back to a function pointer.
196 /// This is extracted from test/Sema/callingconv-cast.c
197 callback = (callback_t)!mismatched; // both-warning {{address of function 'mismatched' will always evaluate to 'true'}} \
198 // both-note {{prefix with the address-of operator to silence this warning}}
199 }
200 }
201
202
203 }
204
205 namespace Comparison {
206 void f(), g();
207 constexpr void (*pf)() = &f, (*pg)() = &g;
208
209 constexpr bool u13 = pf < pg; // both-warning {{ordered comparison of function pointers}} \
210 // both-error {{must be initialized by a constant expression}} \
211 // both-note {{comparison between '&f' and '&g' has unspecified value}}
212
213 constexpr bool u14 = pf < (void(*)())nullptr; // both-warning {{ordered comparison of function pointers}} \
214 // both-error {{must be initialized by a constant expression}} \
215 // both-note {{comparison between '&f' and 'nullptr' has unspecified value}}
216
217
218
219 static_assert(pf != pg, "");
220 static_assert(pf == &f, "");
221 static_assert(pg == &g, "");
222 }
223
224 }
225
226 struct F {
227 constexpr bool ok() const {
228 return okRecurse();
229 }
230 constexpr bool okRecurse() const {
231 return true;
232 }
233 };
234
235 struct BodylessMemberFunction {
236 constexpr int first() const {
237 return second();
238 }
239 constexpr int second() const {
240 return 1;
241 }
242 };
243
244 constexpr int nyd(int m);
245 constexpr int doit() { return nyd(10); }
246 constexpr int nyd(int m) { return m; }
247 static_assert(doit() == 10, "");
248
249 namespace InvalidCall {
250 struct S {
251 constexpr int a() const { // both-error {{never produces a constant expression}}
252 return 1 / 0; // both-note 2{{division by zero}} \
253 // both-warning {{is undefined}}
254 }
255 };
256 constexpr S s;
257 static_assert(s.a() == 1, ""); // both-error {{not an integral constant expression}} \
258 // both-note {{in call to}}
259
260 /// This used to cause an assertion failure in the new constant interpreter.
261 constexpr void func(); // both-note {{declared here}}
262 struct SS {
263 constexpr SS() { func(); } // both-note {{undefined function }}
264 };
265 constexpr SS ss; // both-error {{must be initialized by a constant expression}} \
266 // both-note {{in call to 'SS()'}}
267
268
269 /// This should not emit a diagnostic.
270 constexpr int f();
271 constexpr int a() {
272 return f();
273 }
274 constexpr int f() {
275 return 5;
276 }
277 static_assert(a() == 5, "");
278
279 }
280
281 namespace CallWithArgs {
282 /// This used to call problems during checkPotentialConstantExpression() runs.
283 constexpr void g(int a) {}
284 constexpr void f() {
285 g(0);
286 }
287 }
288
289 namespace ReturnLocalPtr {
290 constexpr int *p() {
291 int a = 12;
292 return &a; // both-warning {{address of stack memory}}
293 }
294
295 /// GCC rejects the expression below, just like the new interpreter. The current interpreter
296 /// however accepts it and only warns about the function above returning an address to stack
297 /// memory. If we change the condition to 'p() != nullptr', it even succeeds.
298 static_assert(p() == nullptr, ""); // ref-error {{static assertion failed}} \
299 // expected-error {{not an integral constant expression}}
300
301 /// FIXME: The current interpreter emits diagnostics in the reference case below, but the
302 /// new one does not.
303 constexpr const int &p2() {
304 int a = 12; // ref-note {{declared here}}
305 return a; // both-warning {{reference to stack memory associated with local variable}}
306 }
307
308 static_assert(p2() == 12, ""); // both-error {{not an integral constant expression}} \
309 // ref-note {{read of variable whose lifetime has ended}}
310 }
311
312 namespace VoidReturn {
313 /// ReturnStmt with an expression in a void function used to cause problems.
314 constexpr void bar() {}
315 constexpr void foo() {
316 return bar();
317 }
318 static_assert((foo(),1) == 1, "");
319 }
320
321 namespace InvalidReclRefs {
322 void param(bool b) { // both-note {{declared here}}
323 static_assert(b, ""); // both-error {{not an integral constant expression}} \
324 // both-note {{function parameter 'b' with unknown value}}
325 static_assert(true ? true : b, "");
326 }
327
328 #if __cplusplus >= 202002L
329 consteval void param2(bool b) { // both-note {{declared here}}
330 static_assert(b, ""); // both-error {{not an integral constant expression}} \
331 // both-note {{function parameter 'b' with unknown value}}
332 }
333 #endif
334 }
335
336 namespace TemplateUndefined {
337 template<typename T> constexpr int consume(T);
338 // ok, not a constant expression.
339 const int k = consume(0);
340
341 template<typename T> constexpr int consume(T) { return 0; }
342 // ok, constant expression.
343 constexpr int l = consume(0);
344 static_assert(l == 0, "");
345 }
346
347 namespace PtrReturn {
348 constexpr void *a() {
349 return nullptr;
350 }
351 static_assert(a() == nullptr, "");
352 }
353
354 namespace Variadic {
355 struct S { int a; bool b; };
356
357 constexpr void variadic_function(int a, ...) {}
358 constexpr int f1() {
359 variadic_function(1, S{'a', false});
360 return 1;
361 }
362 static_assert(f1() == 1, "");
363
364 constexpr int variadic_function2(...) {
365 return 12;
366 }
367 static_assert(variadic_function2() == 12, "");
368 static_assert(variadic_function2(1, 2, 3, 4, 5) == 12, "");
369 static_assert(variadic_function2(1, variadic_function2()) == 12, "");
370
371 constexpr int (*VFP)(...) = variadic_function2;
372 static_assert(VFP() == 12, "");
373
374 /// Member functions
375 struct Foo {
376 int a = 0;
377 constexpr void bla(...) {}
378 constexpr S bla2(...) {
379 return S{12, true};
380 }
381 constexpr Foo(...) : a(1337) {}
382 constexpr Foo(void *c, bool b, void*p, ...) : a('a' + b) {}
383 constexpr Foo(int a, const S* s, ...) : a(a) {}
384 };
385
386 constexpr int foo2() {
387 Foo f(1, nullptr);
388 auto s = f.bla2(1, 2, S{1, false});
389 return s.a + s.b;
390 }
391 static_assert(foo2() == 13, "");
392
393 constexpr Foo _f = 123;
394 static_assert(_f.a == 1337, "");
395
396 constexpr Foo __f(nullptr, false, nullptr, nullptr, 'a', Foo());
397 static_assert(__f.a == 'a', "");
398
399
400 #if __cplusplus >= 202002L
401 namespace VariadicVirtual {
402 class A {
403 public:
404 constexpr virtual void foo(int &a, ...) {
405 a = 1;
406 }
407 };
408
409 class B : public A {
410 public:
411 constexpr void foo(int &a, ...) override {
412 a = 2;
413 }
414 };
415
416 constexpr int foo() {
417 B b;
418 int a;
419 b.foo(a, 1,2,nullptr);
420 return a;
421 }
422 static_assert(foo() == 2, "");
423 } // VariadicVirtual
424
425 namespace VariadicQualified {
426 class A {
427 public:
428 constexpr virtual int foo(...) const {
429 return 5;
430 }
431 };
432 class B : public A {};
433 class C : public B {
434 public:
435 constexpr int foo(...) const override {
436 return B::foo(1,2,3); // B doesn't have a foo(), so this should call A::foo().
437 }
438 constexpr int foo2() const {
439 return this->A::foo(1,2,3,this);
440 }
441 };
442 constexpr C c;
443 static_assert(c.foo() == 5);
444 static_assert(c.foo2() == 5);
445 } // VariadicQualified
446 #endif
447
448 }
449
450 namespace Packs {
451 template<typename...T>
452 constexpr int foo() { return sizeof...(T); }
453 static_assert(foo<int, char>() == 2, "");
454 static_assert(foo<>() == 0, "");
455 }
456
457 namespace AddressOf {
458 struct S {} s;
459 static_assert(__builtin_addressof(s) == &s, "");
460
461 struct T { constexpr T *operator&() const { return nullptr; } int n; } t;
462 constexpr T *pt = __builtin_addressof(t);
463 static_assert(&pt->n == &t.n, "");
464
465 struct U { int n : 5; } u;
466 int *pbf = __builtin_addressof(u.n); // both-error {{address of bit-field requested}}
467
468 S *ptmp = __builtin_addressof(S{}); // both-error {{taking the address of a temporary}} \
469 // both-warning {{temporary whose address is used as value of local variable 'ptmp' will be destroyed at the end of the full-expression}}
470
471 constexpr int foo() {return 1;}
472 static_assert(__builtin_addressof(foo) == foo, "");
473
474 constexpr _Complex float F = {3, 4}; // both-warning {{'_Complex' is a C99 extension}}
475 static_assert(__builtin_addressof(F) == &F, "");
476
477 void testAddressof(int x) {
478 static_assert(&x == __builtin_addressof(x), "");
479 }
480 }
481
482 namespace std {
483 template <typename T> struct remove_reference { using type = T; };
484 template <typename T> struct remove_reference<T &> { using type = T; };
485 template <typename T> struct remove_reference<T &&> { using type = T; };
486 template <typename T>
487 constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept {
488 return static_cast<typename std::remove_reference<T>::type &&>(t);
489 }
490 }
491 /// The std::move declaration above gets translated to a builtin function.
492 namespace Move {
493 #if __cplusplus >= 202002L
494 consteval int f_eval() { // both-note 12{{declared here}}
495 return 0;
496 }
497
498 /// From test/SemaCXX/cxx2a-consteval.
499 struct Copy {
500 int(*ptr)();
501 constexpr Copy(int(*p)() = nullptr) : ptr(p) {}
502 consteval Copy(const Copy&) = default;
503 };
504
505 constexpr const Copy &to_lvalue_ref(const Copy &&a) {
506 return a;
507 }
508
509 void test() {
510 constexpr const Copy C;
511 // there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision.
512 // so we need to test with both prvalue and xvalues.
513 { Copy c(C); }
514 { Copy c((Copy(&f_eval))); } // both-error {{cannot take address of consteval}}
515 { Copy c(std::move(C)); }
516 { Copy c(std::move(Copy(&f_eval))); } // both-error {{is not a constant expression}} \
517 // both-note {{to a consteval}}
518 { Copy c(to_lvalue_ref((Copy(&f_eval)))); } // both-error {{is not a constant expression}} \
519 // both-note {{to a consteval}}
520 { Copy c(to_lvalue_ref(std::move(C))); }
521 { Copy c(to_lvalue_ref(std::move(Copy(&f_eval)))); } // both-error {{is not a constant expression}} \
522 // both-note {{to a consteval}}
523 { Copy c = Copy(C); }
524 { Copy c = Copy(Copy(&f_eval)); } // both-error {{cannot take address of consteval}}
525 { Copy c = Copy(std::move(C)); }
526 { Copy c = Copy(std::move(Copy(&f_eval))); } // both-error {{is not a constant expression}} \
527 // both-note {{to a consteval}}
528 { Copy c = Copy(to_lvalue_ref(Copy(&f_eval))); } // both-error {{is not a constant expression}} \
529 // both-note {{to a consteval}}
530 { Copy c = Copy(to_lvalue_ref(std::move(C))); }
531 { Copy c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); } // both-error {{is not a constant expression}} \
532 // both-note {{to a consteval}}
533 { Copy c; c = Copy(C); }
534 { Copy c; c = Copy(Copy(&f_eval)); } // both-error {{cannot take address of consteval}}
535 { Copy c; c = Copy(std::move(C)); }
536 { Copy c; c = Copy(std::move(Copy(&f_eval))); } // both-error {{is not a constant expression}} \
537 // both-note {{to a consteval}}
538 { Copy c; c = Copy(to_lvalue_ref(Copy(&f_eval))); } // both-error {{is not a constant expression}} \
539 // both-note {{to a consteval}}
540 { Copy c; c = Copy(to_lvalue_ref(std::move(C))); }
541 { Copy c; c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); } // both-error {{is not a constant expression}} \
542 // both-note {{to a consteval}}
543 }
544 #endif
545 constexpr int A = std::move(5);
546 static_assert(A == 5, "");
547 }
548
549 namespace StaticLocals {
550 void test() {
551 static int j; // both-note {{declared here}}
552 static_assert(&j != nullptr, ""); // both-warning {{always true}}
553
554 static_assert(j == 0, ""); // both-error {{not an integral constant expression}} \
555 // both-note {{read of non-const variable 'j'}}
556
557 static int k = 0; // both-note {{declared here}}
558 static_assert(k == 0, ""); // both-error {{not an integral constant expression}} \
559 // both-note {{read of non-const variable 'k'}}
560
561 static const int l = 12;
562 static_assert(l == 12, "");
563
564 static const int m; // both-error {{default initialization}}
565 static_assert(m == 0, "");
566 }
567 }
568
569 namespace Local {
570 /// We used to run into infinite recursin here because we were
571 /// trying to evaluate t's initializer while evaluating t's initializer.
572 int a() {
573 const int t=t;
574 return t;
575 }
576 }
577
578 namespace VariadicOperator {
579 struct Callable {
580 float& operator()(...);
581 };
582
583 void test_callable(Callable c) {
584 float &fr = c(10);
585 }
586 }
587
588 namespace WeakCompare {
589 [[gnu::weak]]void weak_method();
590 static_assert(weak_method != nullptr, ""); // both-error {{not an integral constant expression}} \
591 // both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
592
593 constexpr auto A = &weak_method;
594 static_assert(A != nullptr, ""); // both-error {{not an integral constant expression}} \
595 // both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
596 }
597
598 namespace FromIntegral {
599 #if __cplusplus >= 202002L
600 typedef double (*DoubleFn)();
601 int a[(int)DoubleFn((void*)-1)()]; // both-error {{not allowed at file scope}} \
602 // both-warning {{variable length arrays}}
603 int b[(int)DoubleFn((void*)(-1 + 1))()]; // both-error {{not allowed at file scope}} \
604 // expected-note {{evaluates to a null function pointer}} \
605 // both-warning {{variable length arrays}}
606 #endif
607 }
608
609 namespace {
610 template <typename T> using id = T;
611 template <typename T>
612 constexpr void g() {
613 constexpr id<void (T)> f;
614 }
615
616 static_assert((g<int>(), true), "");
617 }
618
619 namespace {
620 /// The InitListExpr here is of void type.
621 void bir [[clang::annotate("B", {1, 2, 3, 4})]] (); // both-error {{'annotate' attribute requires parameter 1 to be a constant expression}} \
622 // both-note {{subexpression not valid in a constant expression}}
623 }
624
625 namespace FuncPtrParam {
626 void foo(int(&a)()) {
627 *a; // both-warning {{expression result unused}}
628 }
629 }
630
631 namespace {
632 void f() noexcept;
633 void (&r)() = f;
634 void (&cond3)() = r;
635 }
636
637 namespace FunctionCast {
638 // When folding, we allow functions to be cast to different types. Such
639 // cast functions cannot be called, even if they're constexpr.
640 constexpr int f() { return 1; }
641 typedef double (*DoubleFn)();
642 typedef int (*IntFn)();
643 int a[(int)DoubleFn(f)()]; // both-error {{variable length array}} \
644 // both-warning {{are a Clang extension}}
645 int b[(int)IntFn(f)()]; // ok
646 }
647
648 #if __cplusplus >= 202002L
649 namespace StableAddress {
650 template<unsigned N> struct str {
651 char arr[N];
652 };
653 // FIXME: Deduction guide not needed with P1816R0.
654 template<unsigned N> str(const char (&)[N]) -> str<N>;
655
656 template<str s> constexpr int sum() {
657 int n = 0;
658 for (char c : s.arr)
659 n += c;
660 return n;
661 }
662 static_assert(sum<str{"$hello $world."}>() == 1234, "");
663 }
664 #endif