Skip to content

Commit 226409c

Browse files
committed
[libc++] Only include_next C library headers when they exist
Some platforms don't provide all C library headers. In practice, libc++ only requires a few C library headers to exist, and only a few functions on those headers. Missing functions that libc++ doesn't need for its own implementation are handled properly by the using_if_exists attribute, however a missing header is currently a hard error when we try to do #include_next. This patch should make libc++ more flexible on platforms that do not provide C headers that libc++ doesn't actually require for its own implementation. The only downside is that it may move some errors from the #include_next point to later in the compilation if we actually try to use something that isn't provided, which could be somewhat confusing. However, these errors should be caught by folks trying to port libc++ over to a new platform (when running the libc++ test suite), not by end users. Differential Revision: https://reviews.llvm.org/D136683
1 parent 20b0e0a commit 226409c

File tree

18 files changed

+60
-32
lines changed

18 files changed

+60
-32
lines changed

libcxx/include/complex.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@
2424
#endif
2525

2626
#ifdef __cplusplus
27-
28-
#include <ccomplex>
29-
30-
#else // __cplusplus
31-
32-
#include_next <complex.h>
33-
34-
#endif // __cplusplus
27+
# include <ccomplex>
28+
#elif __has_include_next(<complex.h>)
29+
# include_next <complex.h>
30+
#endif
3531

3632
#endif // _LIBCPP_COMPLEX_H

libcxx/include/ctype.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ int toupper(int c);
3535
# pragma GCC system_header
3636
#endif
3737

38-
#include_next <ctype.h>
38+
#if __has_include_next(<ctype.h>)
39+
# include_next <ctype.h>
40+
#endif
3941

4042
#ifdef __cplusplus
4143

libcxx/include/errno.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
# pragma GCC system_header
2929
#endif
3030

31-
#include_next <errno.h>
31+
#if __has_include_next(<errno.h>)
32+
# include_next <errno.h>
33+
#endif
3234

3335
#ifdef __cplusplus
3436

libcxx/include/fenv.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ int feupdateenv(const fenv_t* envp);
5656
# pragma GCC system_header
5757
#endif
5858

59-
#include_next <fenv.h>
59+
#if __has_include_next(<fenv.h>)
60+
# include_next <fenv.h>
61+
#endif
6062

6163
#ifdef __cplusplus
6264

libcxx/include/float.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@
7676
# pragma GCC system_header
7777
#endif
7878

79-
#include_next <float.h>
79+
#if __has_include_next(<float.h>)
80+
# include_next <float.h>
81+
#endif
8082

8183
#ifdef __cplusplus
8284

libcxx/include/inttypes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ uintmax_t wcstoumax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int
248248
# define __STDC_FORMAT_MACROS
249249
#endif
250250

251-
#include_next <inttypes.h>
251+
#if __has_include_next(<inttypes.h>)
252+
# include_next <inttypes.h>
253+
#endif
252254

253255
#ifdef __cplusplus
254256

libcxx/include/limits.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
#endif
4545

4646
#ifndef __GNUC__
47-
#include_next <limits.h>
47+
48+
# if __has_include_next(<limits.h>)
49+
# include_next <limits.h>
50+
# endif
51+
4852
#else
4953
// GCC header limits.h recursively includes itself through another header called
5054
// syslimits.h for some reason. This setup breaks down if we directly

libcxx/include/locale.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
# pragma GCC system_header
4444
#endif
4545

46-
#include_next <locale.h>
46+
#if __has_include_next(<locale.h>)
47+
# include_next <locale.h>
48+
#endif
4749

4850
#endif // _LIBCPP_LOCALE_H

libcxx/include/math.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ long double truncl(long double x);
297297
# pragma GCC system_header
298298
#endif
299299

300-
#include_next <math.h>
300+
# if __has_include_next(<math.h>)
301+
# include_next <math.h>
302+
# endif
301303

302304
#ifdef __cplusplus
303305

libcxx/include/setjmp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ void longjmp(jmp_buf env, int val);
3131
# pragma GCC system_header
3232
#endif
3333

34-
#include_next <setjmp.h>
34+
#if __has_include_next(<setjmp.h>)
35+
# include_next <setjmp.h>
36+
#endif
3537

3638
#ifdef __cplusplus
3739

libcxx/include/stdbool.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
# pragma GCC system_header
2525
#endif
2626

27-
#include_next <stdbool.h>
27+
#if __has_include_next(<stdbool.h>)
28+
# include_next <stdbool.h>
29+
#endif
2830

2931
#ifdef __cplusplus
3032
#undef bool

libcxx/include/stddef.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
# pragma GCC system_header
4343
#endif
4444

45-
#include_next <stddef.h>
45+
# if __has_include_next(<stddef.h>)
46+
# include_next <stddef.h>
47+
# endif
4648

4749
#ifdef __cplusplus
4850
typedef decltype(nullptr) nullptr_t;

libcxx/include/stdint.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
# define __STDC_CONSTANT_MACROS
121121
#endif
122122

123-
#include_next <stdint.h>
123+
#if __has_include_next(<stdint.h>)
124+
# include_next <stdint.h>
125+
#endif
124126

125127
#endif // _LIBCPP_STDINT_H

libcxx/include/stdio.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ void perror(const char* s);
104104
# pragma GCC system_header
105105
#endif
106106

107-
#include_next <stdio.h>
107+
# if __has_include_next(<stdio.h>)
108+
# include_next <stdio.h>
109+
# endif
108110

109111
#ifdef __cplusplus
110112

libcxx/include/stdlib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ void *aligned_alloc(size_t alignment, size_t size); // C11
9090
# pragma GCC system_header
9191
#endif
9292

93-
#include_next <stdlib.h>
93+
# if __has_include_next(<stdlib.h>)
94+
# include_next <stdlib.h>
95+
# endif
9496

9597
#ifdef __cplusplus
9698
extern "C++" {

libcxx/include/string.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ size_t strlen(const char* s);
5757
# pragma GCC system_header
5858
#endif
5959

60-
#include_next <string.h>
60+
#if __has_include_next(<string.h>)
61+
# include_next <string.h>
62+
#endif
6163

6264
// MSVCRT, GNU libc and its derivates may already have the correct prototype in
6365
// <string.h>. This macro can be defined by users if their C library provides

libcxx/include/tgmath.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
#endif
2525

2626
#ifdef __cplusplus
27-
28-
#include <ctgmath>
29-
30-
#else // __cplusplus
31-
32-
#include_next <tgmath.h>
33-
34-
#endif // __cplusplus
27+
# include <ctgmath>
28+
#else
29+
# if __has_include_next(<tgmath.h>)
30+
# include_next <tgmath.h>
31+
# endif
32+
#endif
3533

3634
#endif // _LIBCPP_TGMATH_H

libcxx/include/wchar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ size_t wcsrtombs(char* restrict dst, const wchar_t** restrict src, size_t len,
120120
#define __CORRECT_ISO_CPP_WCHAR_H_PROTO
121121
#endif
122122

123-
#include_next <wchar.h>
123+
# if __has_include_next(<wchar.h>)
124+
# include_next <wchar.h>
125+
# endif
124126

125127
// Determine whether we have const-correct overloads for wcschr and friends.
126128
#if defined(_WCHAR_H_CPLUSPLUS_98_CONFORMANCE_)

0 commit comments

Comments
 (0)