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