Skip to content

Commit ce4ac99

Browse files
authored
[libc++] Remove explicit mentions of __need_FOO macros (#119025)
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 17b3dd0 commit ce4ac99

File tree

5 files changed

+66
-63
lines changed

5 files changed

+66
-63
lines changed

libcxx/include/module.modulemap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,15 +2286,15 @@ module std_stdbool_h [system] {
22862286
textual header "stdbool.h"
22872287
}
22882288
module std_stddef_h [system] {
2289-
// <stddef.h>'s __need_* macros require textual inclusion.
2289+
// <stddef.h> supports being included multiple times with different pre-defined macros
22902290
textual header "stddef.h"
22912291
}
22922292
module std_stdio_h [system] {
2293-
// <stdio.h>'s __need_* macros require textual inclusion.
2293+
// <stdio.h> supports being included multiple times with different pre-defined macros
22942294
textual header "stdio.h"
22952295
}
22962296
module std_stdlib_h [system] {
2297-
// <stdlib.h>'s __need_* macros require textual inclusion.
2297+
// <stdlib.h> supports being included multiple times with different pre-defined macros
22982298
textual header "stdlib.h"
22992299
}
23002300
module std_string_h [system] {
@@ -2310,7 +2310,7 @@ module std_uchar_h [system] {
23102310
export *
23112311
}
23122312
module std_wchar_h [system] {
2313-
// <wchar.h>'s __need_* macros require textual inclusion.
2313+
// <wchar.h> supports being included multiple times with different pre-defined macros
23142314
textual header "wchar.h"
23152315
}
23162316
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.

libcxx/test/libcxx/clang_modules_include.gen.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
// TODO: Investigate why this doesn't work on Picolibc once the locale base API is refactored
5050
// UNSUPPORTED: LIBCXX-PICOLIBC-FIXME
5151
52+
// TODO: Fix seemingly circular inclusion or <wchar.h> on AIX
53+
// UNSUPPORTED: LIBCXX-AIX-FIXME
54+
5255
{lit_header_restrictions.get(header, '')}
5356
{lit_header_undeprecations.get(header, '')}
5457
@@ -83,6 +86,9 @@
8386
// TODO: Investigate why this doesn't work on Picolibc once the locale base API is refactored
8487
// UNSUPPORTED: LIBCXX-PICOLIBC-FIXME
8588
89+
// TODO: Fix seemingly circular inclusion or <wchar.h> on AIX
90+
// UNSUPPORTED: LIBCXX-AIX-FIXME
91+
8692
@import std;
8793
8894
"""

0 commit comments

Comments
 (0)