diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c720e47dbe35b..fd643efe4efa0 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -337,6 +337,11 @@ C23 Feature Support - Properly promote bit-fields of bit-precise integer types to the field's type rather than to ``int``. #GH87641 +- Added the ``INFINITY`` and ``NAN`` macros to Clang's ```` + freestanding implementation; these macros were defined in ```` in C99 + but C23 added them to ```` in + `WG14 N2848 `_. + Non-comprehensive list of changes in this release ------------------------------------------------- diff --git a/clang/lib/Headers/float.h b/clang/lib/Headers/float.h index 642c8f06cc938..a565a33243df1 100644 --- a/clang/lib/Headers/float.h +++ b/clang/lib/Headers/float.h @@ -88,6 +88,12 @@ # endif #endif +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \ + !defined(__STRICT_ANSI__) +# undef INFINITY +# undef NAN +#endif + /* Characteristics of floating point types, C99 5.2.4.2.2 */ #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ @@ -155,6 +161,13 @@ # define LDBL_HAS_SUBNORM __LDBL_HAS_DENORM__ #endif +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \ + !defined(__STRICT_ANSI__) + /* C23 5.2.5.3.3p29-30 */ +# define INFINITY (__builtin_inf()) +# define NAN (__builtin_nan("")) +#endif + #ifdef __STDC_WANT_IEC_60559_TYPES_EXT__ # define FLT16_MANT_DIG __FLT16_MANT_DIG__ # define FLT16_DECIMAL_DIG __FLT16_DECIMAL_DIG__ diff --git a/clang/test/Headers/float.c b/clang/test/Headers/float.c index 70c11b0537537..b9e6e971545e5 100644 --- a/clang/test/Headers/float.c +++ b/clang/test/Headers/float.c @@ -1,9 +1,13 @@ // RUN: %clang_cc1 -fsyntax-only -verify -std=c89 -ffreestanding %s // RUN: %clang_cc1 -fsyntax-only -verify -std=c99 -ffreestanding %s // RUN: %clang_cc1 -fsyntax-only -verify -std=c11 -ffreestanding %s +// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -ffreestanding %s +// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -ffreestanding -ffinite-math-only %s // RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++11 -ffreestanding %s // RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++14 -ffreestanding %s // RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++17 -ffreestanding %s +// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding %s +// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding -ffinite-math-only %s // expected-no-diagnostics /* Basic floating point conformance checks against: @@ -207,6 +211,21 @@ #error "Mandatory macros {FLT,DBL,LDBL}_MAX_10_EXP are invalid." #endif +#if __STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__) + #ifndef INFINITY + #error "Mandatory macro INFINITY is missing." + #endif + #ifndef NAN + #error "Mandatory macro NAN is missing." + #endif +#else + #ifdef INFINITY + #error "Macro INFINITY should not be defined." + #endif + #ifdef NAN + #error "Macro NAN should not be defined." + #endif +#endif /* Internal consistency checks */ _Static_assert(FLT_RADIX == __FLT_RADIX__, ""); @@ -244,3 +263,9 @@ _Static_assert(LDBL_MAX_EXP == __LDBL_MAX_EXP__, ""); _Static_assert(FLT_MAX_10_EXP == __FLT_MAX_10_EXP__, ""); _Static_assert(DBL_MAX_10_EXP == __DBL_MAX_10_EXP__, ""); _Static_assert(LDBL_MAX_10_EXP == __LDBL_MAX_10_EXP__, ""); + +#if (__STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__)) && __FINITE_MATH_ONLY__ == 0 +// Ensure INFINITY and NAN are suitable for use in a constant expression. +float f1 = INFINITY; +float f2 = NAN; +#endif diff --git a/clang/www/c_status.html b/clang/www/c_status.html index 84cd8e836006c..36d3637048fce 100644 --- a/clang/www/c_status.html +++ b/clang/www/c_status.html @@ -990,7 +990,7 @@

C23 implementation status

Contradiction about INFINITY macro N2848 - Unknown + Clang 19 Require exact-width integer type interfaces