020-Include-safe-ctype.h-after-C-standard-headers-to-avo.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. From 9970b576b7e4ae337af1268395ff221348c4b34a Mon Sep 17 00:00:00 2001
  2. From: Francois-Xavier Coudert <[email protected]>
  3. Date: Thu, 7 Mar 2024 14:36:03 +0100
  4. Subject: [PATCH] Include safe-ctype.h after C++ standard headers, to avoid
  5. over-poisoning
  6. When building gcc's C++ sources against recent libc++, the poisoning of
  7. the ctype macros due to including safe-ctype.h before including C++
  8. standard headers such as <list>, <map>, etc, causes many compilation
  9. errors, similar to:
  10. In file included from /home/dim/src/gcc/master/gcc/gensupport.cc:23:
  11. In file included from /home/dim/src/gcc/master/gcc/system.h:233:
  12. In file included from /usr/include/c++/v1/vector:321:
  13. In file included from
  14. /usr/include/c++/v1/__format/formatter_bool.h:20:
  15. In file included from
  16. /usr/include/c++/v1/__format/formatter_integral.h:32:
  17. In file included from /usr/include/c++/v1/locale:202:
  18. /usr/include/c++/v1/__locale:546:5: error: '__abi_tag__' attribute
  19. only applies to structs, variables, functions, and namespaces
  20. 546 | _LIBCPP_INLINE_VISIBILITY
  21. | ^
  22. /usr/include/c++/v1/__config:813:37: note: expanded from macro
  23. '_LIBCPP_INLINE_VISIBILITY'
  24. 813 | # define _LIBCPP_INLINE_VISIBILITY _LIBCPP_HIDE_FROM_ABI
  25. | ^
  26. /usr/include/c++/v1/__config:792:26: note: expanded from macro
  27. '_LIBCPP_HIDE_FROM_ABI'
  28. 792 |
  29. __attribute__((__abi_tag__(_LIBCPP_TOSTRING(
  30. _LIBCPP_VERSIONED_IDENTIFIER))))
  31. | ^
  32. In file included from /home/dim/src/gcc/master/gcc/gensupport.cc:23:
  33. In file included from /home/dim/src/gcc/master/gcc/system.h:233:
  34. In file included from /usr/include/c++/v1/vector:321:
  35. In file included from
  36. /usr/include/c++/v1/__format/formatter_bool.h:20:
  37. In file included from
  38. /usr/include/c++/v1/__format/formatter_integral.h:32:
  39. In file included from /usr/include/c++/v1/locale:202:
  40. /usr/include/c++/v1/__locale:547:37: error: expected ';' at end of
  41. declaration list
  42. 547 | char_type toupper(char_type __c) const
  43. | ^
  44. /usr/include/c++/v1/__locale:553:48: error: too many arguments
  45. provided to function-like macro invocation
  46. 553 | const char_type* toupper(char_type* __low, const
  47. char_type* __high) const
  48. | ^
  49. /home/dim/src/gcc/master/gcc/../include/safe-ctype.h:146:9: note:
  50. macro 'toupper' defined here
  51. 146 | #define toupper(c) do_not_use_toupper_with_safe_ctype
  52. | ^
  53. This is because libc++ uses different transitive includes than
  54. libstdc++, and some of those transitive includes pull in various ctype
  55. declarations (typically via <locale>).
  56. There was already a special case for including <string> before
  57. safe-ctype.h, so move the rest of the C++ standard header includes to
  58. the same location, to fix the problem.
  59. gcc/ChangeLog:
  60. * system.h: Include safe-ctype.h after C++ standard headers.
  61. Signed-off-by: Dimitry Andric <[email protected]>
  62. ---
  63. gcc/system.h | 39 ++++++++++++++++++---------------------
  64. 1 file changed, 18 insertions(+), 21 deletions(-)
  65. diff --git a/gcc/system.h b/gcc/system.h
  66. index b0edab02885..ab29fc19776 100644
  67. --- a/gcc/system.h
  68. +++ b/gcc/system.h
  69. @@ -194,27 +194,8 @@ extern int fprintf_unlocked (FILE *, const char *, ...);
  70. #undef fread_unlocked
  71. #undef fwrite_unlocked
  72. -/* Include <string> before "safe-ctype.h" to avoid GCC poisoning
  73. - the ctype macros through safe-ctype.h */
  74. -
  75. -#ifdef __cplusplus
  76. -#ifdef INCLUDE_STRING
  77. -# include <string>
  78. -#endif
  79. -#endif
  80. -
  81. -/* There are an extraordinary number of issues with <ctype.h>.
  82. - The last straw is that it varies with the locale. Use libiberty's
  83. - replacement instead. */
  84. -#include "safe-ctype.h"
  85. -
  86. -#include <sys/types.h>
  87. -
  88. -#include <errno.h>
  89. -
  90. -#if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
  91. -extern int errno;
  92. -#endif
  93. +/* Include C++ standard headers before "safe-ctype.h" to avoid GCC
  94. + poisoning the ctype macros through safe-ctype.h */
  95. #ifdef __cplusplus
  96. #if defined (INCLUDE_ALGORITHM) || !defined (HAVE_SWAP_IN_UTILITY)
  97. @@ -229,6 +210,9 @@ extern int errno;
  98. #ifdef INCLUDE_SET
  99. # include <set>
  100. #endif
  101. +#ifdef INCLUDE_STRING
  102. +# include <string>
  103. +#endif
  104. #ifdef INCLUDE_VECTOR
  105. # include <vector>
  106. #endif
  107. @@ -245,6 +229,19 @@ extern int errno;
  108. # include <type_traits>
  109. #endif
  110. +/* There are an extraordinary number of issues with <ctype.h>.
  111. + The last straw is that it varies with the locale. Use libiberty's
  112. + replacement instead. */
  113. +#include "safe-ctype.h"
  114. +
  115. +#include <sys/types.h>
  116. +
  117. +#include <errno.h>
  118. +
  119. +#if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
  120. +extern int errno;
  121. +#endif
  122. +
  123. /* Some of glibc's string inlines cause warnings. Plus we'd rather
  124. rely on (and therefore test) GCC's string builtins. */
  125. #define __NO_STRING_INLINES
  126. --
  127. 2.39.3