Skip to content

Commit 9d78f83

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 llvm#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 7f7f540 commit 9d78f83

File tree

4 files changed

+53
-56
lines changed

4 files changed

+53
-56
lines changed

libcxx/include/module.modulemap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,15 +2265,15 @@ module std_stdbool_h [system] {
22652265
textual header "stdbool.h"
22662266
}
22672267
module std_stddef_h [system] {
2268-
// <stddef.h>'s __need_* macros require textual inclusion.
2268+
// <stddef.h> supports being included multiple times with different pre-defined macros
22692269
textual header "stddef.h"
22702270
}
22712271
module std_stdio_h [system] {
2272-
// <stdio.h>'s __need_* macros require textual inclusion.
2272+
// <stdio.h> supports being included multiple times with different pre-defined macros
22732273
textual header "stdio.h"
22742274
}
22752275
module std_stdlib_h [system] {
2276-
// <stdlib.h>'s __need_* macros require textual inclusion.
2276+
// <stdlib.h> supports being included multiple times with different pre-defined macros
22772277
textual header "stdlib.h"
22782278
}
22792279
module std_string_h [system] {
@@ -2289,7 +2289,7 @@ module std_uchar_h [system] {
22892289
export *
22902290
}
22912291
module std_wchar_h [system] {
2292-
// <wchar.h>'s __need_* macros require textual inclusion.
2292+
// <wchar.h> supports being included multiple times with different pre-defined macros
22932293
textual header "wchar.h"
22942294
}
22952295
module std_wctype_h [system] {

libcxx/include/stdio.h

Lines changed: 14 additions & 15 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,11 +87,21 @@ int ferror(FILE* stream);
9887
void perror(const char* s);
9988
*/
10089

101-
# include <__config>
90+
#include <__config>
10291

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

107106
# if __has_include_next(<stdio.h>)
108107
# include_next <stdio.h>

libcxx/include/stdlib.h

Lines changed: 15 additions & 16 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,11 +73,21 @@ void *aligned_alloc(size_t alignment, size_t size); // C11
8473
8574
*/
8675

87-
# include <__config>
76+
#include <__config>
8877

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

9392
# if __has_include_next(<stdlib.h>)
9493
# include_next <stdlib.h>
@@ -146,6 +145,6 @@ inline _LIBCPP_HIDE_FROM_ABI lldiv_t div(long long __x, long long __y) _NOEXCEPT
146145
# endif
147146
# endif // _LIBCPP_MSVCRT
148147
} // extern "C++"
149-
# endif // __cplusplus
148+
# endif // __cplusplus
150149

151150
#endif // _LIBCPP_STDLIB_H

libcxx/include/wchar.h

Lines changed: 20 additions & 21 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,23 +94,33 @@ size_t wcsrtombs(char* restrict dst, const wchar_t** restrict src, size_t len,
10594
10695
*/
10796

108-
# include <__config>
109-
# include <stddef.h>
97+
#include <__config>
11098

111-
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
112-
# pragma GCC system_header
113-
# endif
99+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
100+
# pragma GCC system_header
101+
#endif
114102

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

121122
# if __has_include_next(<wchar.h>)
122123
# include_next <wchar.h>
123-
# else
124-
# include <__mbstate_t.h> // make sure we have mbstate_t regardless of the existence of <wchar.h>
125124
# endif
126125

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

0 commit comments

Comments
 (0)