[libc++] Only include_next C library headers when they exist
authorLouis Dionne <ldionne.2@gmail.com>
Tue, 15 Nov 2022 22:08:01 +0000 (17:08 -0500)
committerLouis Dionne <ldionne.2@gmail.com>
Thu, 17 Nov 2022 15:30:20 +0000 (10:30 -0500)
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.

NOTE: This is a reapplicaton of 226409, which was reverted in 674729813
      because it broke the build. The issue has now been fixed with
      https://reviews.llvm.org/D138062.

Differential Revision: https://reviews.llvm.org/D136683

18 files changed:
libcxx/include/complex.h
libcxx/include/ctype.h
libcxx/include/errno.h
libcxx/include/fenv.h
libcxx/include/float.h
libcxx/include/inttypes.h
libcxx/include/limits.h
libcxx/include/locale.h
libcxx/include/math.h
libcxx/include/setjmp.h
libcxx/include/stdbool.h
libcxx/include/stddef.h
libcxx/include/stdint.h
libcxx/include/stdio.h
libcxx/include/stdlib.h
libcxx/include/string.h
libcxx/include/tgmath.h
libcxx/include/wchar.h

index a281466..a3da21c 100644 (file)
 #endif
 
 #ifdef __cplusplus
-
-#include <ccomplex>
-
-#else  // __cplusplus
-
-#include_next <complex.h>
-
-#endif // __cplusplus
+#  include <ccomplex>
+#elif __has_include_next(<complex.h>)
+#  include_next <complex.h>
+#endif
 
 #endif // _LIBCPP_COMPLEX_H
index ba09250..728173e 100644 (file)
@@ -35,7 +35,9 @@ int toupper(int c);
 #  pragma GCC system_header
 #endif
 
-#include_next <ctype.h>
+#if __has_include_next(<ctype.h>)
+#  include_next <ctype.h>
+#endif
 
 #ifdef __cplusplus
 
index ea0559f..7b02d2b 100644 (file)
@@ -28,7 +28,9 @@ Macros:
 #  pragma GCC system_header
 #endif
 
-#include_next <errno.h>
+#if __has_include_next(<errno.h>)
+#  include_next <errno.h>
+#endif
 
 #ifdef __cplusplus
 
index a9ba680..15e4156 100644 (file)
@@ -56,7 +56,9 @@ int feupdateenv(const fenv_t* envp);
 #  pragma GCC system_header
 #endif
 
-#include_next <fenv.h>
+#if __has_include_next(<fenv.h>)
+#  include_next <fenv.h>
+#endif
 
 #ifdef __cplusplus
 
index 6060812..9e5f711 100644 (file)
@@ -76,7 +76,9 @@ Macros:
 #  pragma GCC system_header
 #endif
 
-#include_next <float.h>
+#if __has_include_next(<float.h>)
+#  include_next <float.h>
+#endif
 
 #ifdef __cplusplus
 
index e0fd71f..b755526 100644 (file)
@@ -248,7 +248,9 @@ uintmax_t wcstoumax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int
 #   define __STDC_FORMAT_MACROS
 #endif
 
-#include_next <inttypes.h>
+#if __has_include_next(<inttypes.h>)
+#  include_next <inttypes.h>
+#endif
 
 #ifdef __cplusplus
 
index 3e1e85a..032a909 100644 (file)
@@ -44,7 +44,11 @@ Macros:
 #endif
 
 #ifndef __GNUC__
-#include_next <limits.h>
+
+#  if __has_include_next(<limits.h>)
+#    include_next <limits.h>
+#  endif
+
 #else
 // GCC header limits.h recursively includes itself through another header called
 // syslimits.h for some reason. This setup breaks down if we directly
index 17c0a70..3fb8120 100644 (file)
@@ -43,6 +43,8 @@ Functions:
 #  pragma GCC system_header
 #endif
 
-#include_next <locale.h>
+#if __has_include_next(<locale.h>)
+#  include_next <locale.h>
+#endif
 
 #endif // _LIBCPP_LOCALE_H
index d5a7095..d609430 100644 (file)
@@ -297,7 +297,9 @@ long double    truncl(long double x);
 #  pragma GCC system_header
 #endif
 
-#include_next <math.h>
+#  if __has_include_next(<math.h>)
+#    include_next <math.h>
+#  endif
 
 #ifdef __cplusplus
 
index de4f9ed..f4a2bbc 100644 (file)
@@ -31,7 +31,9 @@ void longjmp(jmp_buf env, int val);
 #  pragma GCC system_header
 #endif
 
-#include_next <setjmp.h>
+#if __has_include_next(<setjmp.h>)
+#  include_next <setjmp.h>
+#endif
 
 #ifdef __cplusplus
 
index 0bc1aa8..5bba005 100644 (file)
@@ -24,7 +24,9 @@ Macros:
 #  pragma GCC system_header
 #endif
 
-#include_next <stdbool.h>
+#if __has_include_next(<stdbool.h>)
+#  include_next <stdbool.h>
+#endif
 
 #ifdef __cplusplus
 #undef bool
index 19e344f..f1725db 100644 (file)
@@ -42,7 +42,9 @@ Types:
 #  pragma GCC system_header
 #endif
 
-#include_next <stddef.h>
+#  if __has_include_next(<stddef.h>)
+#    include_next <stddef.h>
+#  endif
 
 #ifdef __cplusplus
     typedef decltype(nullptr) nullptr_t;
index ee71f62..fa7b011 100644 (file)
@@ -120,6 +120,8 @@ Macros:
 #   define __STDC_CONSTANT_MACROS
 #endif
 
-#include_next <stdint.h>
+#if __has_include_next(<stdint.h>)
+#  include_next <stdint.h>
+#endif
 
 #endif // _LIBCPP_STDINT_H
index ad1b4c0..cea43aa 100644 (file)
@@ -104,7 +104,9 @@ void perror(const char* s);
 #  pragma GCC system_header
 #endif
 
-#include_next <stdio.h>
+#  if __has_include_next(<stdio.h>)
+#    include_next <stdio.h>
+#  endif
 
 #ifdef __cplusplus
 
index e4dce9c..64581b6 100644 (file)
@@ -90,7 +90,9 @@ void *aligned_alloc(size_t alignment, size_t size);                       // C11
 #  pragma GCC system_header
 #endif
 
-#include_next <stdlib.h>
+#  if __has_include_next(<stdlib.h>)
+#    include_next <stdlib.h>
+#  endif
 
 #ifdef __cplusplus
 extern "C++" {
index 082c632..627cbae 100644 (file)
@@ -57,7 +57,9 @@ size_t strlen(const char* s);
 #  pragma GCC system_header
 #endif
 
-#include_next <string.h>
+#if __has_include_next(<string.h>)
+#  include_next <string.h>
+#endif
 
 // MSVCRT, GNU libc and its derivates may already have the correct prototype in
 // <string.h>. This macro can be defined by users if their C library provides
index c650917..e6f0a4a 100644 (file)
 #endif
 
 #ifdef __cplusplus
-
-#include <ctgmath>
-
-#else  // __cplusplus
-
-#include_next <tgmath.h>
-
-#endif // __cplusplus
+#  include <ctgmath>
+#else
+#  if __has_include_next(<tgmath.h>)
+#    include_next <tgmath.h>
+#  endif
+#endif
 
 #endif // _LIBCPP_TGMATH_H
index 0fba53b..c684508 100644 (file)
@@ -120,7 +120,9 @@ size_t wcsrtombs(char* restrict dst, const wchar_t** restrict src, size_t len,
 #define __CORRECT_ISO_CPP_WCHAR_H_PROTO
 #endif
 
-#include_next <wchar.h>
+#  if __has_include_next(<wchar.h>)
+#    include_next <wchar.h>
+#  endif
 
 // Determine whether we have const-correct overloads for wcschr and friends.
 #if defined(_WCHAR_H_CPLUSPLUS_98_CONFORMANCE_)