safe-ctype.h: Fix building with latest libc++

Message ID 20230413194442.3851955-1-manojgupta@google.com
State New
Headers
Series safe-ctype.h: Fix building with latest libc++ |

Commit Message

Manoj Gupta April 13, 2023, 7:44 p.m. UTC
  Latest libc++[1] causes transitive include to <locale> when
<mutex> or <thread> header is included. This causes
gdb to not build[2] since <locale> defines isupper/islower etc.
functions that are explicitly macroed-out in safe-ctype.h to
prevent their use.
Use the suggestion from libc++ to include <locale> internally when
building in C++ mode to avoid build errors.

[1]: https://reviews.llvm.org/D144331
[2]: https://issuetracker.google.com/issues/277967395
---
 include/safe-ctype.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
  

Comments

Tom Tromey April 14, 2023, 1:29 p.m. UTC | #1
>>>>> Manoj Gupta via Gdb-patches <gdb-patches@sourceware.org> writes:

> Latest libc++[1] causes transitive include to <locale> when
> <mutex> or <thread> header is included. This causes
> gdb to not build[2] since <locale> defines isupper/islower etc.
> functions that are explicitly macroed-out in safe-ctype.h to
> prevent their use.
> Use the suggestion from libc++ to include <locale> internally when
> building in C++ mode to avoid build errors.

> [1]: https://reviews.llvm.org/D144331
> [2]: https://issuetracker.google.com/issues/277967395
> ---
>  include/safe-ctype.h | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)

This file is canonically maintained in gcc, so changes have to go there
first.

Alternatively you could perhaps change gdbsupport/gdb-safe-ctype.h,
though that might require fixing up the odd #include elsewhere.

Tom
  

Patch

diff --git a/include/safe-ctype.h b/include/safe-ctype.h
index a3cb1858efc..843ba98571c 100644
--- a/include/safe-ctype.h
+++ b/include/safe-ctype.h
@@ -117,9 +117,16 @@  extern const unsigned char  _sch_tolower[256];
    detecting that ctype.h has been included.  But this was causing
    trouble as ctype.h might get indirectly included as a result of
    including another system header (for instance gnulib's stdint.h).
-   So we include ctype.h here and then immediately redefine its macros.  */
+   So we include ctype.h here and then immediately redefine its macros.
+   When compiling in C++ mode, aldo add <locale> which also defines
+   is* functions.
+*/
 
 #include <ctype.h>
+#ifdef __cplusplus
+#include <locale>
+#endif
+
 #undef isalpha
 #define isalpha(c) do_not_use_isalpha_with_safe_ctype
 #undef isalnum