Skip to content

Commit 310c3ea

Browse files
committed
[libc++] Remove explicit mentions of __need_FOO macros
This change has a long history. It was first attempted naively in https://reviews.llvm.org/D131425, which didn't work because we broke the ability for code to include e.g. <stdio.h> multiple times and get different definitions based on the pre-defined macros. However, in #86843 we managed to simplify <stddef.h> by including the underlying system header outside of any include guards, which worked. This patch applies the same simplification we did to <stddef.h> to the other headers that currently mention __need_FOO macros explicitly.
1 parent 7ea1fe7 commit 310c3ea

File tree

4 files changed

+60
-63
lines changed

4 files changed

+60
-63
lines changed

libcxx/include/module.modulemap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,15 +2271,15 @@ module std_stdbool_h [system] {
22712271
textual header "stdbool.h"
22722272
}
22732273
module std_stddef_h [system] {
2274-
// <stddef.h>'s __need_* macros require textual inclusion.
2274+
// <stddef.h> supports being included multiple times with different pre-defined macros
22752275
textual header "stddef.h"
22762276
}
22772277
module std_stdio_h [system] {
2278-
// <stdio.h>'s __need_* macros require textual inclusion.
2278+
// <stdio.h> supports being included multiple times with different pre-defined macros
22792279
textual header "stdio.h"
22802280
}
22812281
module std_stdlib_h [system] {
2282-
// <stdlib.h>'s __need_* macros require textual inclusion.
2282+
// <stdlib.h> supports being included multiple times with different pre-defined macros
22832283
textual header "stdlib.h"
22842284
}
22852285
module std_string_h [system] {
@@ -2295,7 +2295,7 @@ module std_uchar_h [system] {
22952295
export *
22962296
}
22972297
module std_wchar_h [system] {
2298-
// <wchar.h>'s __need_* macros require textual inclusion.
2298+
// <wchar.h> supports being included multiple times with different pre-defined macros
22992299
textual header "wchar.h"
23002300
}
23012301
module std_wctype_h [system] {

libcxx/include/stdio.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#if defined(__need_FILE) || defined(__need___FILE)
11-
12-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
13-
# pragma GCC system_header
14-
# endif
15-
16-
# include_next <stdio.h>
17-
18-
#elif !defined(_LIBCPP_STDIO_H)
19-
# define _LIBCPP_STDIO_H
20-
2110
/*
2211
stdio.h synopsis
2312
@@ -98,13 +87,23 @@ int ferror(FILE* stream);
9887
void perror(const char* s);
9988
*/
10089

101-
# if 0
102-
# else // 0
103-
# include <__config>
90+
#if 0
91+
#else // 0
92+
# include <__config>
10493

105-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
106-
# pragma GCC system_header
107-
# endif
94+
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
95+
# pragma GCC system_header
96+
# endif
97+
98+
// The inclusion of the system's <stdio.h> is intentionally done once outside of any include
99+
// guards because some code expects to be able to include the underlying system header multiple
100+
// times to get different definitions based on the macros that are set before inclusion.
101+
# if __has_include_next(<stdio.h>)
102+
# include_next <stdio.h>
103+
# endif
104+
105+
# ifndef _LIBCPP_STDIO_H
106+
# define _LIBCPP_STDIO_H
108107

109108
# if __has_include_next(<stdio.h>)
110109
# include_next <stdio.h>

libcxx/include/stdlib.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#if defined(__need_malloc_and_calloc)
11-
12-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
13-
# pragma GCC system_header
14-
# endif
15-
16-
# include_next <stdlib.h>
17-
18-
#elif !defined(_LIBCPP_STDLIB_H)
19-
# define _LIBCPP_STDLIB_H
20-
2110
/*
2211
stdlib.h synopsis
2312
@@ -84,13 +73,23 @@ void *aligned_alloc(size_t alignment, size_t size); // C11
8473
8574
*/
8675

87-
# if 0
88-
# else // 0
89-
# include <__config>
76+
#if 0
77+
#else // 0
78+
# include <__config>
9079

91-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
92-
# pragma GCC system_header
93-
# endif
80+
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
81+
# pragma GCC system_header
82+
# endif
83+
84+
// The inclusion of the system's <stdlib.h> is intentionally done once outside of any include
85+
// guards because some code expects to be able to include the underlying system header multiple
86+
// times to get different definitions based on the macros that are set before inclusion.
87+
# if __has_include_next(<stdlib.h>)
88+
# include_next <stdlib.h>
89+
# endif
90+
91+
# if !defined(_LIBCPP_STDLIB_H)
92+
# define _LIBCPP_STDLIB_H
9493

9594
# if __has_include_next(<stdlib.h>)
9695
# include_next <stdlib.h>
@@ -148,7 +147,7 @@ inline _LIBCPP_HIDE_FROM_ABI lldiv_t div(long long __x, long long __y) _NOEXCEPT
148147
# endif
149148
# endif // _LIBCPP_MSVCRT
150149
} // extern "C++"
151-
# endif // __cplusplus
152-
# endif // 0
150+
# endif // __cplusplus
151+
# endif // 0
153152

154153
#endif // _LIBCPP_STDLIB_H

libcxx/include/wchar.h

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#if defined(__need_wint_t) || defined(__need_mbstate_t)
11-
12-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
13-
# pragma GCC system_header
14-
# endif
15-
16-
# include_next <wchar.h>
17-
18-
#elif !defined(_LIBCPP_WCHAR_H)
19-
# define _LIBCPP_WCHAR_H
20-
2110
/*
2211
wchar.h synopsis
2312
@@ -105,25 +94,35 @@ size_t wcsrtombs(char* restrict dst, const wchar_t** restrict src, size_t len,
10594
10695
*/
10796

108-
# if 0
109-
# else // 0
110-
# include <__config>
111-
# include <stddef.h>
97+
#if 0
98+
#else // 0
99+
# include <__config>
112100

113-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
114-
# pragma GCC system_header
115-
# endif
101+
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
102+
# pragma GCC system_header
103+
# endif
116104

117105
// We define this here to support older versions of glibc <wchar.h> that do
118106
// not define this for clang.
119-
# ifdef __cplusplus
120-
# define __CORRECT_ISO_CPP_WCHAR_H_PROTO
121-
# endif
107+
# if defined(__cplusplus) && !defined(__CORRECT_ISO_CPP_WCHAR_H_PROTO)
108+
# define __CORRECT_ISO_CPP_WCHAR_H_PROTO
109+
# endif
110+
111+
// The inclusion of the system's <wchar.h> is intentionally done once outside of any include
112+
// guards because some code expects to be able to include the underlying system header multiple
113+
// times to get different definitions based on the macros that are set before inclusion.
114+
# if __has_include_next(<wchar.h>)
115+
# include_next <wchar.h>
116+
# endif
117+
118+
# ifndef _LIBCPP_WCHAR_H
119+
# define _LIBCPP_WCHAR_H
120+
121+
# include <__mbstate_t.h> // provide mbstate_t
122+
# include <stddef.h> // provide size_t
122123

123124
# if __has_include_next(<wchar.h>)
124125
# include_next <wchar.h>
125-
# else
126-
# include <__mbstate_t.h> // make sure we have mbstate_t regardless of the existence of <wchar.h>
127126
# endif
128127

129128
// Determine whether we have const-correct overloads for wcschr and friends.

0 commit comments

Comments
 (0)