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

via libc-commits libc-commits at lists.llvm.org
Mon Jan 29 16:40:07 PST 2024


Author: Nick Desaulniers
Date: 2024-01-29T16:40:03-08:00
New Revision: 5a7a8f73c71b9aaf6d2f7cb548807046f19c4498

URL: https://github.com/llvm/llvm-project/commit/5a7a8f73c71b9aaf6d2f7cb548807046f19c4498
DIFF: https://github.com/llvm/llvm-project/commit/5a7a8f73c71b9aaf6d2f7cb548807046f19c4498.diff

LOG: [libc] fix type generic stdc_leading_zeros for GCC (#79917)

GCC does not support _Generic in C++ mode. Use inline function definitions with
function overloading in __cplusplus mode.

Added: 
    

Modified: 
    libc/include/llvm-libc-macros/stdbit-macros.h
    libc/include/stdbit.h.def
    libc/test/include/CMakeLists.txt

Removed: 
    


################################################################################
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

diff  --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index ecc32380c596e..6a586cd715cf8 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -31,9 +31,5 @@ if (LLVM_LIBC_FULL_BUILD)
       # 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