Skip to content

Commit 95d4c97

Browse files
authored
[libc][wchar] Move wchar's types to proxy headers. (#109334)
Also protect against extern inline function definitions added when building with gcc: #60481.
1 parent b5cdb03 commit 95d4c97

File tree

16 files changed

+161
-24
lines changed

16 files changed

+161
-24
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,17 @@ add_proxy_header_library(
161161
libc.include.sys_auxv
162162
)
163163

164+
add_header_library(wchar_overlay HDRS wchar_overlay.h)
165+
166+
add_proxy_header_library(
167+
wchar_macros
168+
HDRS
169+
wchar_macros.h
170+
DEPENDS
171+
.wchar_overlay
172+
FULL_BUILD_DEPENDS
173+
libc.include.llvm-libc-macros.wchar_macros
174+
libc.include.wchar
175+
)
176+
164177
add_subdirectory(types)

libc/hdr/types/CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ add_proxy_header_library(
199199
libc.include.setjmp
200200
)
201201

202-
203202
add_proxy_header_library(
204203
struct_msghdr
205204
HDRS
@@ -226,3 +225,25 @@ add_proxy_header_library(
226225
libc.include.llvm-libc-types.socklen_t
227226
libc.include.sys_socket
228227
)
228+
229+
add_proxy_header_library(
230+
wchar_t
231+
HDRS
232+
wchar_t.h
233+
DEPENDS
234+
libc.hdr.wchar_overlay
235+
FULL_BUILD_DEPENDS
236+
libc.include.llvm-libc-types.wchar_t
237+
libc.include.wchar
238+
)
239+
240+
add_proxy_header_library(
241+
wint_t
242+
HDRS
243+
wint_t.h
244+
DEPENDS
245+
libc.hdr.wchar_overlay
246+
FULL_BUILD_DEPENDS
247+
libc.include.llvm-libc-types.wint_t
248+
libc.include.wchar
249+
)

libc/hdr/types/wchar_t.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Definition of wchar_t.h -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_LIBC_HDR_TYPES_WCHAR_T_H
11+
#define LLVM_LIBC_HDR_TYPES_WCHAR_T_H
12+
13+
#ifdef LIBC_FULL_BUILD
14+
15+
#include "include/llvm-libc-types/wchar_t.h"
16+
17+
#else // overlay mode
18+
19+
#include "hdr/wchar_overlay.h"
20+
21+
#endif // LLVM_LIBC_FULL_BUILD
22+
23+
#endif // LLVM_LIBC_HDR_TYPES_WCHAR_T_H

libc/hdr/types/wint_t.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Definition of wint_t.h --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_LIBC_HDR_TYPES_WINT_T_H
11+
#define LLVM_LIBC_HDR_TYPES_WINT_T_H
12+
13+
#ifdef LIBC_FULL_BUILD
14+
15+
#include "include/llvm-libc-types/wint_t.h"
16+
17+
#else // overlay mode
18+
19+
#include "hdr/wchar_overlay.h"
20+
21+
#endif // LLVM_LIBC_FULL_BUILD
22+
23+
#endif // LLVM_LIBC_HDR_TYPES_WINT_T_H

libc/hdr/wchar_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from wchar.h ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_WCHAR_MACROS_H
10+
#define LLVM_LIBC_HDR_WCHAR_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/wchar-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/wchar_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_WCHAR_MACROS_H

libc/hdr/wchar_overlay.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- Including wchar.h in overlay mode ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_WCHAR_OVERLAY_H
10+
#define LLVM_LIBC_HDR_WCHAR_OVERLAY_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#error "This header should only be included in overlay mode"
14+
#endif
15+
16+
// Overlay mode
17+
18+
// glibc <wchar.h header might provide extern inline definitions for few
19+
// functions, causing external alias errors. They are guarded by
20+
// `__USE_EXTERN_INLINES` macro. We temporarily disable `__USE_EXTERN_INLINES`
21+
// macro by defining `__NO_INLINE__` before including <wchar.h>.
22+
// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
23+
// with `_FORTIFY_SOURCE`.
24+
25+
#ifdef _FORTIFY_SOURCE
26+
#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
27+
#undef _FORTIFY_SOURCE
28+
#endif
29+
30+
#ifndef __NO_INLINE__
31+
#define __NO_INLINE__ 1
32+
#define LIBC_SET_NO_INLINE
33+
#endif
34+
35+
#include <wchar.h>
36+
37+
#ifdef LIBC_OLD_FORTIFY_SOURCE
38+
#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
39+
#undef LIBC_OLD_FORTIFY_SOURCE
40+
#endif
41+
42+
#ifdef LIBC_SET_NO_INLINE
43+
#undef __NO_INLINE__
44+
#undef LIBC_SET_NO_INLINE
45+
#endif
46+
47+
#endif // LLVM_LIBC_HDR_WCHAR_OVERLAY_H

libc/include/llvm-libc-types/wchar_t.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
#ifndef LLVM_LIBC_TYPES_WCHAR_T_H
1010
#define LLVM_LIBC_TYPES_WCHAR_T_H
1111

12-
// Since __need_wchar_t is defined, we get the definition of wchar_t from the
13-
// standalone C header stddef.h. Also, because __need_wchar_t is defined,
14-
// including stddef.h will pull only the type wchar_t and nothing else.
15-
#define __need_wchar_t
16-
#include <stddef.h>
17-
#undef __need_wchar_t
12+
typedef __WCHAR_TYPE__ wchar_t;
1813

1914
#endif // LLVM_LIBC_TYPES_WCHAR_T_H

libc/include/llvm-libc-types/wint_t.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
#ifndef LLVM_LIBC_TYPES_WINT_T_H
1010
#define LLVM_LIBC_TYPES_WINT_T_H
1111

12-
// Since __need_wint_t is defined, we get the definition of wint_t from the
13-
// standalone C header stddef.h. Also, because __need_wint_t is defined,
14-
// including stddef.h will pull only the type wint_t and nothing else.
15-
#define __need_wint_t
16-
#include <stddef.h>
17-
#undef __need_wint_t
12+
typedef __WINT_TYPE__ wint_t;
1813

1914
#endif // LLVM_LIBC_TYPES_WINT_T_H

libc/src/__support/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ add_header_library(
117117
wctype_utils
118118
HDRS
119119
wctype_utils.h
120+
DEPENDS
121+
libc.hdr.types.wint_t
120122
)
121123

122124
add_header_library(

libc/src/__support/wctype_utils.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
1010
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
1111

12+
#include "hdr/types/wint_t.h"
1213
#include "src/__support/CPP/optional.h"
1314
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1415
#include "src/__support/macros/config.h"
1516

16-
#define __need_wint_t
17-
#define __need_wchar_t
18-
#include <stddef.h> // needed for wint_t and wchar_t
19-
2017
namespace LIBC_NAMESPACE_DECL {
2118
namespace internal {
2219

libc/src/wchar/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_entrypoint_object(
66
HDRS
77
wctob.h
88
DEPENDS
9-
libc.include.stdio
10-
libc.include.wchar
9+
libc.hdr.types.wint_t
1110
libc.src.__support.wctype_utils
1211
)

libc/src/wchar/btowc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/wctype_utils.h"
1313

1414
#include "hdr/stdio_macros.h" // for EOF.
15+
#include "hdr/types/wint_t.h"
1516

1617
namespace LIBC_NAMESPACE_DECL {
1718

libc/src/wchar/btowc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_WCHAR_BTOWC_H
1010
#define LLVM_LIBC_SRC_WCHAR_BTOWC_H
1111

12+
#include "hdr/types/wint_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <wchar.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/wchar/wctob.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/wctype_utils.h"
1313

1414
#include "hdr/stdio_macros.h" // for EOF.
15+
#include "hdr/types/wint_t.h"
1516

1617
namespace LIBC_NAMESPACE_DECL {
1718

libc/src/wchar/wctob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_WCHAR_WCTOB_H
1010
#define LLVM_LIBC_SRC_WCHAR_WCTOB_H
1111

12+
#include "hdr/types/wint_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <wchar.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/test/src/wchar/btowc_test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <wchar.h> //for WEOF
10-
9+
#include "hdr/wchar_macros.h" // for WEOF
1110
#include "src/wchar/btowc.h"
12-
1311
#include "test/UnitTest/Test.h"
1412

1513
TEST(LlvmLibcBtowc, DefaultLocale) {

0 commit comments

Comments
 (0)