From a80493d221eea6393fc919008d752063fa02b7a3 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sun, 26 Jun 2022 19:11:09 -0600 Subject: [PATCH] LibC: Align _ctype_ to newlib's implementation newlib has an extra character slot at the beginning to enable some macro tricks that cause a warning when someone passes a type that's not "int" into a ctype function. Our deviation from this causes issues for LLVM. --- Userland/Libraries/LibC/ctype.cpp | 3 ++- Userland/Libraries/LibC/ctype.h | 35 +++++++++++++++++++------------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibC/ctype.cpp b/Userland/Libraries/LibC/ctype.cpp index 81a69a8bce0b43..c866ed6aaf93f5 100644 --- a/Userland/Libraries/LibC/ctype.cpp +++ b/Userland/Libraries/LibC/ctype.cpp @@ -8,7 +8,8 @@ extern "C" { -char const _ctype_[256] = { +char const _ctype_[1 + 256] = { + 0, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C | _S, _C | _S, _C | _S, _C | _S, _C | _S, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, diff --git a/Userland/Libraries/LibC/ctype.h b/Userland/Libraries/LibC/ctype.h index 62bbbbb866a967..1947f3416b9c58 100644 --- a/Userland/Libraries/LibC/ctype.h +++ b/Userland/Libraries/LibC/ctype.h @@ -10,6 +10,10 @@ __BEGIN_DECLS +#ifndef EOF +# define EOF (-1) +#endif + /* Do what newlib does to appease GCC's --with-newlib option. */ #define _U 01 #define _L 02 @@ -20,16 +24,21 @@ __BEGIN_DECLS #define _X 0100 #define _B 0200 -extern char const _ctype_[256] __attribute__((visibility("default"))); +/** + * newlib has a 257 byte _ctype_ array to enable compiler tricks to catch + * people passing char instead of int. We don't engage in those tricks, + * but still claim to be newlib to the toolchains + */ +extern char const _ctype_[1 + 256] __attribute__((visibility("default"))); static inline int __inline_isalnum(int c) { - return _ctype_[(unsigned char)(c)] & (_U | _L | _N); + return _ctype_[(unsigned char)(c) + 1] & (_U | _L | _N); } static inline int __inline_isalpha(int c) { - return _ctype_[(unsigned char)(c)] & (_U | _L); + return _ctype_[(unsigned char)(c) + 1] & (_U | _L); } static inline int __inline_isascii(int c) @@ -39,52 +48,52 @@ static inline int __inline_isascii(int c) static inline int __inline_iscntrl(int c) { - return _ctype_[(unsigned char)(c)] & (_C); + return _ctype_[(unsigned char)(c) + 1] & (_C); } static inline int __inline_isdigit(int c) { - return _ctype_[(unsigned char)(c)] & (_N); + return _ctype_[(unsigned char)(c) + 1] & (_N); } static inline int __inline_isxdigit(int c) { - return _ctype_[(unsigned char)(c)] & (_N | _X); + return _ctype_[(unsigned char)(c) + 1] & (_N | _X); } static inline int __inline_isspace(int c) { - return _ctype_[(unsigned char)(c)] & (_S); + return _ctype_[(unsigned char)(c) + 1] & (_S); } static inline int __inline_ispunct(int c) { - return _ctype_[(unsigned char)(c)] & (_P); + return _ctype_[(unsigned char)(c) + 1] & (_P); } static inline int __inline_isprint(int c) { - return _ctype_[(unsigned char)(c)] & (_P | _U | _L | _N | _B); + return _ctype_[(unsigned char)(c) + 1] & (_P | _U | _L | _N | _B); } static inline int __inline_isgraph(int c) { - return _ctype_[(unsigned char)(c)] & (_P | _U | _L | _N); + return _ctype_[(unsigned char)(c) + 1] & (_P | _U | _L | _N); } static inline int __inline_islower(int c) { - return _ctype_[(unsigned char)(c)] & (_L); + return _ctype_[(unsigned char)(c) + 1] & (_L); } static inline int __inline_isupper(int c) { - return _ctype_[(unsigned char)(c)] & (_U); + return _ctype_[(unsigned char)(c) + 1] & (_U); } static inline int __inline_isblank(int c) { - return _ctype_[(unsigned char)(c)] & (_B) || (c == '\t'); + return _ctype_[(unsigned char)(c) + 1] & (_B) || (c == '\t'); } static inline int __inline_toascii(int c)