[libc-commits] [libc] [libc] fix type generic stdc_leading_zeros for GCC (PR #79917)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Jan 29 16:29:41 PST 2024


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/79917

>From f1ab63dad859dc66e4fd32188b87a7974259d57e Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 29 Jan 2024 16:26:11 -0800
Subject: [PATCH 1/2] [libc] fix type generic stdc_leading_zeros for GCC

GCC does not support _Generic in C++ mode. Use inline function definitions
with function overloading in __cplusplus mode.
---
 libc/include/llvm-libc-macros/stdbit-macros.h | 18 ++++++++++++++++++
 libc/include/stdbit.h.def                     |  3 ++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index febe95fe0a1e3..da0fb1a578f13 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -9,6 +9,23 @@
 #ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 #define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 
+#ifdef __cplusplus
+inline unsigned char stdc_leading_zeros(unsigned char x) {
+  return stdc_leading_zeros_uc(x);
+}
+inline unsigned short stdc_leading_zeros(unsigned short x) {
+  return stdc_leading_zeros_us(x);
+}
+inline unsigned stdc_leading_zeros(unsigned x) {
+  return stdc_leading_zeros_ui(x);
+}
+inline unsigned long stdc_leading_zeros(unsigned long x) {
+  return stdc_leading_zeros_ul(x);
+}
+inline unsigned long long stdc_leading_zeros(unsigned long long x) {
+  return stdc_leading_zeros_ull(x);
+}
+#else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
       unsigned char: stdc_leading_zeros_uc,                                    \
@@ -16,5 +33,6 @@
       unsigned: stdc_leading_zeros_ui,                                         \
       unsigned long: stdc_leading_zeros_ul,                                    \
       unsigned long long: stdc_leading_zeros_ull)(x)
+#endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/include/stdbit.h.def b/libc/include/stdbit.h.def
index cb79ac1caf049..c5a77329fbfe7 100644
--- a/libc/include/stdbit.h.def
+++ b/libc/include/stdbit.h.def
@@ -10,8 +10,9 @@
 #define LLVM_LIBC_STDBIT_H
 
 #include <__llvm-libc-common.h>
-#include <llvm-libc-macros/stdbit-macros.h>
 
 %%public_api()
 
+#include <llvm-libc-macros/stdbit-macros.h>
+
 #endif // LLVM_LIBC_STDBIT_H

>From de547846410bfbb53346fede46211299dff895a1 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 29 Jan 2024 16:29:30 -0800
Subject: [PATCH 2/2] no longer need -Wno-c11-extensions

---
 libc/test/include/CMakeLists.txt | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index ecc32380c596e..fd1c83c1edd40 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -27,13 +27,5 @@ if (LLVM_LIBC_FULL_BUILD)
     DEPENDS
       libc.include.llvm-libc-macros.stdbit_macros
       libc.include.stdbit
-      # Intentionally do not depend on libc.src.stdbit.*. The include test is
-      # simply testing the macros provided by stdbit.h, not the implementation
-      # of the underlying functions which the type generic macros may dispatch
-      # to.
-    COMPILE_OPTIONS
-      # stdbit.h is full of type generic macros implemented via C11 _Generic.
-      # Clang will produce -Wno-c11-extensions when using _Generic in C++ mode.
-      -Wno-c11-extensions
   )
 endif()



More information about the libc-commits mailing list