[clang] 2f2b931 - [C23] Correct the type for INFINITY and NAN in freestanding

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 8 06:10:35 PDT 2024


Author: Aaron Ballman
Date: 2024-07-08T09:10:25-04:00
New Revision: 2f2b931e1296aebe6c03fd969363683b637973e5

URL: https://github.com/llvm/llvm-project/commit/2f2b931e1296aebe6c03fd969363683b637973e5
DIFF: https://github.com/llvm/llvm-project/commit/2f2b931e1296aebe6c03fd969363683b637973e5.diff

LOG: [C23] Correct the type for INFINITY and NAN in freestanding

This amends bcb7c38af7de59f3b2201734ee11987839cd7bbe to correct the
type use for the two macros to be float rather than double. Also adds
additional test coverage.

https://github.com/llvm/llvm-project/issues/98018 was filed to track
the duplicate diagnostic issue that was discovered.

Added: 
    

Modified: 
    clang/lib/Headers/float.h
    clang/test/Headers/float.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Headers/float.h b/clang/lib/Headers/float.h
index a565a33243df1..49d4212414d64 100644
--- a/clang/lib/Headers/float.h
+++ b/clang/lib/Headers/float.h
@@ -164,8 +164,8 @@
 #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(""))
+#  define INFINITY (__builtin_inff())
+#  define NAN (__builtin_nanf(""))
 #endif
 
 #ifdef __STDC_WANT_IEC_60559_TYPES_EXT__

diff  --git a/clang/test/Headers/float.c b/clang/test/Headers/float.c
index b9e6e971545e5..218ab58ba62ef 100644
--- a/clang/test/Headers/float.c
+++ b/clang/test/Headers/float.c
@@ -2,11 +2,14 @@
 // 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=finite -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
+// NOTE: C++23 wasn't based on top of C23, so it gets no diagnostics for
+//       finite-math-only mode as happens in C. When C++ rebased onto C23, that
+//       is when we'll issue diagnostics for INFINITY and NAN use.
 // RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding -ffinite-math-only %s
 // expected-no-diagnostics
 
@@ -218,6 +221,10 @@
   #ifndef NAN
     #error "Mandatory macro NAN is missing."
   #endif
+  // FIXME: the NAN diagnostic should only be issued once, not twice.
+  _Static_assert(_Generic(INFINITY, float : 1, default : 0), ""); // finite-warning {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}
+  _Static_assert(_Generic(NAN, float : 1, default : 0), ""); // finite-warning {{use of NaN is undefined behavior due to the currently enabled floating-point options}} \
+                                                                finite-warning {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}
 #else
   #ifdef INFINITY
     #error "Macro INFINITY should not be defined."


        


More information about the cfe-commits mailing list