94 lines · cpp
1// RUN: %clang_cc1 -std=c++23 -fsyntax-only -Wno-unused %s -verify=cxx20-cxx262// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wno-unused %s -verify=cxx20,cxx20-cxx263// RUN: %clang_cc1 -std=c++17 -fsyntax-only -Wno-unused %s -verify=precxx20,cxx11-cxx174// RUN: %clang_cc1 -std=c++14 -fsyntax-only -Wno-unused %s -verify=precxx20,cxx11-cxx175// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-unused %s -verify=precxx20,cxx11-cxx176// RUN: %clang_cc1 -std=c++03 -fsyntax-only -Wno-unused %s -verify=precxx207// RUN: %clang_cc1 -std=c++98 -fsyntax-only -Wno-unused %s -verify=precxx208// RUN: %clang_cc1 -x c -std=c23 -fsyntax-only -Wno-unused %s -verify=c9 10#if __has_builtin(__builtin_is_within_lifetime)11#error has the builtin12#else13#error does not have the builtin14#endif15// 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}}18 19#if __has_constexpr_builtin(__builtin_is_within_lifetime)20#error has the constexpr builtin21#else22#error does not have the constexpr builtin23#endif24// 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}}27 28#if __cplusplus < 201103L29#define static_assert __extension__ _Static_assert30#define CONSTEXPR1131#else32#define CONSTEXPR11 constexpr33#endif34 35static const int i1 = 0;36static_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'}}39 40#if !defined(__cplusplus) || __cplusplus >= 201102L41constexpr int i2 = 0;42static_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#endif46 47#ifdef __cplusplus48template<typename T>49CONSTEXPR11 bool f1(T i) { // #f150 return __builtin_is_within_lifetime(&i); // #f1-consteval-call51}52 53bool(&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#else60void f1(int i) {61 __builtin_is_within_lifetime(&i);62 // c-error@-1 {{use of undeclared identifier '__builtin_is_within_lifetime'}}63}64#endif65 66#if __cplusplus >= 202002L67constexpr void f2() {68 int i = 0;69 if consteval { // cxx20-warning {{consteval if}}70 __builtin_is_within_lifetime(&i);71 }72}73void(&fp2)() = f2;74 75constexpr void f3() {76 __builtin_is_within_lifetime(&i1);77}78void(&fp3)() = f3;79 80constexpr 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}}92}93#endif94