[libc][wchar] Move wchar's types to proxy headers. (#109334)

Also protect against extern inline function definitions added when
building with gcc: https://github.com/llvm/llvm-project/issues/60481.
This commit is contained in:
lntue 2024-09-19 22:23:51 -04:00 committed by GitHub
parent b5cdb03971
commit 95d4c97a20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 161 additions and 24 deletions

View File

@ -161,4 +161,17 @@ add_proxy_header_library(
libc.include.sys_auxv
)
add_header_library(wchar_overlay HDRS wchar_overlay.h)
add_proxy_header_library(
wchar_macros
HDRS
wchar_macros.h
DEPENDS
.wchar_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.wchar_macros
libc.include.wchar
)
add_subdirectory(types)

View File

@ -199,7 +199,6 @@ add_proxy_header_library(
libc.include.setjmp
)
add_proxy_header_library(
struct_msghdr
HDRS
@ -226,3 +225,25 @@ add_proxy_header_library(
libc.include.llvm-libc-types.socklen_t
libc.include.sys_socket
)
add_proxy_header_library(
wchar_t
HDRS
wchar_t.h
DEPENDS
libc.hdr.wchar_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.wchar_t
libc.include.wchar
)
add_proxy_header_library(
wint_t
HDRS
wint_t.h
DEPENDS
libc.hdr.wchar_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.wint_t
libc.include.wchar
)

23
libc/hdr/types/wchar_t.h Normal file
View File

@ -0,0 +1,23 @@
//===-- 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
#include "hdr/wchar_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD
#endif // LLVM_LIBC_HDR_TYPES_WCHAR_T_H

23
libc/hdr/types/wint_t.h Normal file
View File

@ -0,0 +1,23 @@
//===-- 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
#include "hdr/wchar_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD
#endif // LLVM_LIBC_HDR_TYPES_WINT_T_H

22
libc/hdr/wchar_macros.h Normal file
View File

@ -0,0 +1,22 @@
//===-- 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
#include "hdr/wchar_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD
#endif // LLVM_LIBC_HDR_WCHAR_MACROS_H

47
libc/hdr/wchar_overlay.h Normal file
View File

@ -0,0 +1,47 @@
//===-- Including wchar.h in overlay mode ---------------------------------===//
//
// 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_OVERLAY_H
#define LLVM_LIBC_HDR_WCHAR_OVERLAY_H
#ifdef LIBC_FULL_BUILD
#error "This header should only be included in overlay mode"
#endif
// 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_HDR_WCHAR_OVERLAY_H

View File

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

View File

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

View File

@ -117,6 +117,8 @@ add_header_library(
wctype_utils
HDRS
wctype_utils.h
DEPENDS
libc.hdr.types.wint_t
)
add_header_library(

View File

@ -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 {

View File

@ -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
)

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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) {