[libc-commits] [libc] [libc][wchar] Move wchar's types to proxy headers. (PR #109334)
via libc-commits
libc-commits at lists.llvm.org
Thu Sep 19 13:54:48 PDT 2024
https://github.com/lntue created https://github.com/llvm/llvm-project/pull/109334
Also protect against extern inline function definitions added when building with gcc: https://github.com/llvm/llvm-project/issues/60481.
>From ee996b189db4497deeb34b8abdd3feb4183304ff Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 19 Sep 2024 20:43:03 +0000
Subject: [PATCH] [libc][wchar] Move wchar's types to proxy headers and protect
against extern inline function definitions added when building with gcc.
---
libc/hdr/CMakeLists.txt | 9 +++++
libc/hdr/types/CMakeLists.txt | 18 ++++++++++
libc/hdr/types/wchar_t.h | 50 ++++++++++++++++++++++++++
libc/hdr/types/wint_t.h | 50 ++++++++++++++++++++++++++
libc/hdr/wchar_macros.h | 49 +++++++++++++++++++++++++
libc/include/llvm-libc-types/wchar_t.h | 7 +---
libc/include/llvm-libc-types/wint_t.h | 7 +---
libc/src/__support/CMakeLists.txt | 2 ++
libc/src/__support/wctype_utils.h | 5 +--
libc/src/wchar/CMakeLists.txt | 3 +-
libc/src/wchar/btowc.cpp | 1 +
libc/src/wchar/btowc.h | 2 +-
libc/src/wchar/wctob.cpp | 1 +
libc/src/wchar/wctob.h | 2 +-
libc/test/src/wchar/btowc_test.cpp | 4 +--
15 files changed, 187 insertions(+), 23 deletions(-)
create mode 100644 libc/hdr/types/wchar_t.h
create mode 100644 libc/hdr/types/wint_t.h
create mode 100644 libc/hdr/wchar_macros.h
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index e0b65b7c2eb02d..67c13ec4e19c27 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -161,4 +161,13 @@ add_proxy_header_library(
libc.include.sys_auxv
)
+add_proxy_header_library(
+ wchar_macros
+ HDRS
+ wchar_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.wchar_macros
+ libc.include.wchar
+)
+
add_subdirectory(types)
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 12641c4d93ffe8..4db60f2d470fee 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -199,3 +199,21 @@ add_proxy_header_library(
libc.include.setjmp
)
+add_proxy_header_library(
+ wchar_t
+ HDRS
+ wchar_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.wchar_t
+ libc.include.wchar
+)
+
+add_proxy_header_library(
+ wint_t
+ HDRS
+ wint_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.wint_t
+ libc.include.wchar
+)
+
diff --git a/libc/hdr/types/wchar_t.h b/libc/hdr/types/wchar_t.h
new file mode 100644
index 00000000000000..dda612afbcce34
--- /dev/null
+++ b/libc/hdr/types/wchar_t.h
@@ -0,0 +1,50 @@
+//===-- Definition of wchar_t.h -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_WCHAR_T_H
+#define LLVM_LIBC_HDR_TYPES_WCHAR_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/wchar_t.h"
+
+#else // overlay mode
+
+// glibc <wchar.h header might provide extern inline definitions for few
+// functions, causing external alias errors. They are guarded by
+// `__USE_EXTERN_INLINES` macro. We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <wchar.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <wchar.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_WCHAR_T_H
diff --git a/libc/hdr/types/wint_t.h b/libc/hdr/types/wint_t.h
new file mode 100644
index 00000000000000..4a133d82207da0
--- /dev/null
+++ b/libc/hdr/types/wint_t.h
@@ -0,0 +1,50 @@
+//===-- Definition of wint_t.h --------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_WINT_T_H
+#define LLVM_LIBC_HDR_TYPES_WINT_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/wint_t.h"
+
+#else // overlay mode
+
+// glibc <wchar.h header might provide extern inline definitions for few
+// functions, causing external alias errors. They are guarded by
+// `__USE_EXTERN_INLINES` macro. We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <wchar.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <wchar.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_WINT_T_H
diff --git a/libc/hdr/wchar_macros.h b/libc/hdr/wchar_macros.h
new file mode 100644
index 00000000000000..b471f6d31e4cce
--- /dev/null
+++ b/libc/hdr/wchar_macros.h
@@ -0,0 +1,49 @@
+//===-- Definition of macros from wchar.h ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_WCHAR_MACROS_H
+#define LLVM_LIBC_HDR_WCHAR_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/wchar-macros.h"
+
+#else // Overlay mode
+
+// glibc <wchar.h header might provide extern inline definitions for few
+// functions, causing external alias errors. They are guarded by
+// `__USE_EXTERN_INLINES` macro. We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <wchar.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <wchar.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_WCHAR_MACROS_H
diff --git a/libc/include/llvm-libc-types/wchar_t.h b/libc/include/llvm-libc-types/wchar_t.h
index 3e9a70b8afe6a6..faa40d96efc4d2 100644
--- a/libc/include/llvm-libc-types/wchar_t.h
+++ b/libc/include/llvm-libc-types/wchar_t.h
@@ -9,11 +9,6 @@
#ifndef LLVM_LIBC_TYPES_WCHAR_T_H
#define LLVM_LIBC_TYPES_WCHAR_T_H
-// Since __need_wchar_t is defined, we get the definition of wchar_t from the
-// standalone C header stddef.h. Also, because __need_wchar_t is defined,
-// including stddef.h will pull only the type wchar_t and nothing else.
-#define __need_wchar_t
-#include <stddef.h>
-#undef __need_wchar_t
+typedef __WCHAR_TYPE__ wchar_t;
#endif // LLVM_LIBC_TYPES_WCHAR_T_H
diff --git a/libc/include/llvm-libc-types/wint_t.h b/libc/include/llvm-libc-types/wint_t.h
index 2758685a084575..a53c6e3cb622c9 100644
--- a/libc/include/llvm-libc-types/wint_t.h
+++ b/libc/include/llvm-libc-types/wint_t.h
@@ -9,11 +9,6 @@
#ifndef LLVM_LIBC_TYPES_WINT_T_H
#define LLVM_LIBC_TYPES_WINT_T_H
-// Since __need_wint_t is defined, we get the definition of wint_t from the
-// standalone C header stddef.h. Also, because __need_wint_t is defined,
-// including stddef.h will pull only the type wint_t and nothing else.
-#define __need_wint_t
-#include <stddef.h>
-#undef __need_wint_t
+typedef __WINT_TYPE__ wint_t;
#endif // LLVM_LIBC_TYPES_WINT_T_H
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 0302ad64f8b5df..4785895b562b5e 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -117,6 +117,8 @@ add_header_library(
wctype_utils
HDRS
wctype_utils.h
+ DEPENDS
+ libc.hdr.types.wint_t
)
add_header_library(
diff --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 469d81250b535a..aa137c278323e3 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -9,14 +9,11 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
+#include "hdr/types/wint_t.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
-#define __need_wint_t
-#define __need_wchar_t
-#include <stddef.h> // needed for wint_t and wchar_t
-
namespace LIBC_NAMESPACE_DECL {
namespace internal {
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 1d62399c1fe9d7..c89b05e80e5d82 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -6,7 +6,6 @@ add_entrypoint_object(
HDRS
wctob.h
DEPENDS
- libc.include.stdio
- libc.include.wchar
+ libc.hdr.types.wint_t
libc.src.__support.wctype_utils
)
diff --git a/libc/src/wchar/btowc.cpp b/libc/src/wchar/btowc.cpp
index 6e67f1ba02975c..2129ba557603fc 100644
--- a/libc/src/wchar/btowc.cpp
+++ b/libc/src/wchar/btowc.cpp
@@ -12,6 +12,7 @@
#include "src/__support/wctype_utils.h"
#include "hdr/stdio_macros.h" // for EOF.
+#include "hdr/types/wint_t.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/wchar/btowc.h b/libc/src/wchar/btowc.h
index 1f41bd68f9c175..ef70ee26cc197c 100644
--- a/libc/src/wchar/btowc.h
+++ b/libc/src/wchar/btowc.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_WCHAR_BTOWC_H
#define LLVM_LIBC_SRC_WCHAR_BTOWC_H
+#include "hdr/types/wint_t.h"
#include "src/__support/macros/config.h"
-#include <wchar.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/wchar/wctob.cpp b/libc/src/wchar/wctob.cpp
index 33f67feb90b8d5..45240d6052eb4e 100644
--- a/libc/src/wchar/wctob.cpp
+++ b/libc/src/wchar/wctob.cpp
@@ -12,6 +12,7 @@
#include "src/__support/wctype_utils.h"
#include "hdr/stdio_macros.h" // for EOF.
+#include "hdr/types/wint_t.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/wchar/wctob.h b/libc/src/wchar/wctob.h
index 95f349ed8e4485..8ae89fa3aa6596 100644
--- a/libc/src/wchar/wctob.h
+++ b/libc/src/wchar/wctob.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_WCHAR_WCTOB_H
#define LLVM_LIBC_SRC_WCHAR_WCTOB_H
+#include "hdr/types/wint_t.h"
#include "src/__support/macros/config.h"
-#include <wchar.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/test/src/wchar/btowc_test.cpp b/libc/test/src/wchar/btowc_test.cpp
index b5fcb451bd07b0..8479d541a20ee9 100644
--- a/libc/test/src/wchar/btowc_test.cpp
+++ b/libc/test/src/wchar/btowc_test.cpp
@@ -6,10 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#include <wchar.h> //for WEOF
-
+#include "hdr/wchar_macros.h" // for WEOF
#include "src/wchar/btowc.h"
-
#include "test/UnitTest/Test.h"
TEST(LlvmLibcBtowc, DefaultLocale) {
More information about the libc-commits
mailing list