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
13 #error does not have the builtin
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
22 #error does not have the constexpr builtin
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
32 #define CONSTEXPR11 constexpr
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
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'}}
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}}
61 __builtin_is_within_lifetime(&i
);
62 // c-error@-1 {{use of undeclared identifier '__builtin_is_within_lifetime'}}
66 #if __cplusplus >= 202002L
69 if consteval
{ // cxx20-warning {{consteval if}}
70 __builtin_is_within_lifetime(&i
);
76 __builtin_is_within_lifetime(&i1
);
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}}
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}}