[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / consteval-builtin.cpp
blob3ba95b4dbd9b505a9dfa6c938d85a9fb09805525
1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -Wno-unused %s -verify=cxx20-cxx26
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wno-unused %s -verify=cxx20,cxx20-cxx26
3 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -Wno-unused %s -verify=precxx20,cxx11-cxx17
4 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -Wno-unused %s -verify=precxx20,cxx11-cxx17
5 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-unused %s -verify=precxx20,cxx11-cxx17
6 // RUN: %clang_cc1 -std=c++03 -fsyntax-only -Wno-unused %s -verify=precxx20
7 // RUN: %clang_cc1 -std=c++98 -fsyntax-only -Wno-unused %s -verify=precxx20
8 // RUN: %clang_cc1 -x c -std=c23 -fsyntax-only -Wno-unused %s -verify=c
10 #if __has_builtin(__builtin_is_within_lifetime)
11 #error has the builtin
12 #else
13 #error does not have the builtin
14 #endif
15 // cxx20-cxx26-error@-4 {{has the builtin}}
16 // precxx20-error@-3 {{does not have the builtin}}
17 // c-error@-4 {{does not have the builtin}}
19 #if __has_constexpr_builtin(__builtin_is_within_lifetime)
20 #error has the constexpr builtin
21 #else
22 #error does not have the constexpr builtin
23 #endif
24 // cxx20-cxx26-error@-4 {{has the constexpr builtin}}
25 // precxx20-error@-3 {{does not have the constexpr builtin}}
26 // c-error@-4 {{does not have the constexpr builtin}}
28 #if __cplusplus < 201103L
29 #define static_assert __extension__ _Static_assert
30 #define CONSTEXPR11
31 #else
32 #define CONSTEXPR11 constexpr
33 #endif
35 static const int i1 = 0;
36 static_assert(__builtin_is_within_lifetime(&i1), "");
37 // precxx20-error@-1 {{use of undeclared identifier '__builtin_is_within_lifetime'}}
38 // c-error@-2 {{use of undeclared identifier '__builtin_is_within_lifetime'}}
40 #if !defined(__cplusplus) || __cplusplus >= 201102L
41 constexpr int i2 = 0;
42 static_assert(__builtin_is_within_lifetime(&i2), "");
43 // cxx11-cxx17-error@-1 {{use of undeclared identifier '__builtin_is_within_lifetime'}}
44 // c-error@-2 {{use of undeclared identifier '__builtin_is_within_lifetime'}}
45 #endif
47 #ifdef __cplusplus
48 template<typename T>
49 CONSTEXPR11 bool f1(T i) { // #f1
50 return __builtin_is_within_lifetime(&i); // #f1-consteval-call
53 bool(&fp1)(int) = f1<int>;
54 // cxx20-cxx26-error@-1 {{cannot take address of immediate function 'f1<int>' outside of an immediate invocation}}
55 // cxx20-cxx26-note@#f1 {{declared here}}
56 // cxx20-cxx26-note@#f1-consteval-call {{'f1<int>' is an immediate function because its body contains a call to a consteval function '__builtin_is_within_lifetime' and that call is not a constant expression}}
57 // precxx20-error@#f1-consteval-call {{use of undeclared identifier '__builtin_is_within_lifetime'}}
58 // precxx20-note@-5 {{in instantiation of function template specialization 'f1<int>' requested here}}
59 #else
60 void f1(int i) {
61 __builtin_is_within_lifetime(&i);
62 // c-error@-1 {{use of undeclared identifier '__builtin_is_within_lifetime'}}
64 #endif
66 #if __cplusplus >= 202002L
67 constexpr void f2() {
68 int i = 0;
69 if consteval { // cxx20-warning {{consteval if}}
70 __builtin_is_within_lifetime(&i);
73 void(&fp2)() = f2;
75 constexpr void f3() {
76 __builtin_is_within_lifetime(&i1);
78 void(&fp3)() = f3;
80 constexpr void f4() {
81 &__builtin_is_within_lifetime;
82 // cxx20-cxx26-error@-1 {{builtin functions must be directly called}}
83 // cxx20-cxx26-error@-2 {{cannot take address of consteval function '__builtin_is_within_lifetime' outside of an immediate invocation}}
84 __builtin_is_within_lifetime();
85 // cxx20-cxx26-error@-1 {{too few arguments to function call, expected 1, have 0}}
86 // cxx20-cxx26-error@-2 {{cannot take address of consteval function '__builtin_is_within_lifetime' outside of an immediate invocation}}
87 int* not_constexpr;
88 __builtin_is_within_lifetime(not_constexpr);
89 // cxx20-cxx26-error@-1 {{call to consteval function '__builtin_is_within_lifetime' is not a constant expression}}
90 // cxx20-cxx26-note@-2 {{read of non-constexpr variable 'not_constexpr' is not allowed in a constant expression}}
91 // cxx20-cxx26-note@-4 {{declared here}}
93 #endif