From 0b6d2755f397e6c3a7422c40530f5061723f1b84 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 9 Feb 2025 08:18:18 +0100 Subject: [PATCH] mbsstr, mbscasestr, mbspcasecmp: Use const-improved function macros. * lib/string.in.h (mbsstr, mbspcasecmp, mbscasestr): Define as macros that cast the result to 'const char *' when the first argument is a 'const char *'. * lib/mbsstr.c: Define _GL_NO_CONST_GENERICS. * lib/mbscasestr.c: Likewise. * lib/mbspcasecmp.c: Likewise. --- ChangeLog | 10 ++++++++++ lib/mbscasestr.c | 3 +++ lib/mbspcasecmp.c | 3 +++ lib/mbsstr.c | 3 +++ lib/string.in.h | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/ChangeLog b/ChangeLog index 73350f9f99..be4e2425d3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2025-02-09 Bruno Haible + + mbsstr, mbscasestr, mbspcasecmp: Use const-improved function macros. + * lib/string.in.h (mbsstr, mbspcasecmp, mbscasestr): Define as macros + that cast the result to 'const char *' when the first argument is a + 'const char *'. + * lib/mbsstr.c: Define _GL_NO_CONST_GENERICS. + * lib/mbscasestr.c: Likewise. + * lib/mbspcasecmp.c: Likewise. + 2025-02-09 Bruno Haible stdckdint-h tests: Fix compilation error (regression 2025-02-07). diff --git a/lib/mbscasestr.c b/lib/mbscasestr.c index 7a186e0f98..843229666f 100644 --- a/lib/mbscasestr.c +++ b/lib/mbscasestr.c @@ -15,6 +15,9 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ +/* Don't use the const-improved function macros in this compilation unit. */ +#define _GL_NO_CONST_GENERICS + #include /* Specification. */ diff --git a/lib/mbspcasecmp.c b/lib/mbspcasecmp.c index f57337f01e..0717cd893a 100644 --- a/lib/mbspcasecmp.c +++ b/lib/mbspcasecmp.c @@ -15,6 +15,9 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ +/* Don't use the const-improved function macros in this compilation unit. */ +#define _GL_NO_CONST_GENERICS + #include /* Specification. */ diff --git a/lib/mbsstr.c b/lib/mbsstr.c index 579bcd054d..8e8eb73d55 100644 --- a/lib/mbsstr.c +++ b/lib/mbsstr.c @@ -15,6 +15,9 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ +/* Don't use the const-improved function macros in this compilation unit. */ +#define _GL_NO_CONST_GENERICS + #include /* Specification. */ diff --git a/lib/string.in.h b/lib/string.in.h index ce48829900..6705967c59 100644 --- a/lib/string.in.h +++ b/lib/string.in.h @@ -1178,6 +1178,19 @@ _GL_CXXALIASWARN (mbsrchr); _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); +/* Don't silently convert a 'const char *' to a 'char *'. Programmers want + compiler warnings for 'const' related mistakes. */ +# if !(defined _GL_NO_CONST_GENERICS || defined __cplusplus) +# if __STDC_VERSION__ >= 202311 +# define mbsstr(h,n) (typeof(h)) mbsstr ((h), (n)) +# elif ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \ + || defined __ICC || defined __TINYC__) +# define mbsstr(h,n) \ + _Generic ((h), \ + char const *: (char const *) mbsstr ((h), (n)), \ + default : mbsstr ((h), (n))) +# endif +# endif #endif #if @GNULIB_MBSCASECMP@ @@ -1219,6 +1232,19 @@ _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n) _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); +/* Don't silently convert a 'const char *' to a 'char *'. Programmers want + compiler warnings for 'const' related mistakes. */ +# if !(defined _GL_NO_CONST_GENERICS || defined __cplusplus) +# if __STDC_VERSION__ >= 202311 +# define mbspcasecmp(s,p) (typeof(s)) mbspcasecmp ((s), (p)) +# elif ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \ + || defined __ICC || defined __TINYC__) +# define mbspcasecmp(s,p) \ + _Generic ((s), \ + char const *: (char const *) mbspcasecmp ((s), (p)), \ + default : mbspcasecmp ((s), (p))) +# endif +# endif #endif #if @GNULIB_MBSCASESTR@ @@ -1230,6 +1256,19 @@ _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix) _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); +/* Don't silently convert a 'const char *' to a 'char *'. Programmers want + compiler warnings for 'const' related mistakes. */ +# if !(defined _GL_NO_CONST_GENERICS || defined __cplusplus) +# if __STDC_VERSION__ >= 202311 +# define mbscasestr(h,n) (typeof(h)) mbscasestr ((h), (n)) +# elif ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \ + || defined __ICC || defined __TINYC__) +# define mbscasestr(h,n) \ + _Generic ((h), \ + char const *: (char const *) mbscasestr ((h), (n)), \ + default : mbscasestr ((h), (n))) +# endif +# endif #endif #if @GNULIB_MBSCSPN@ -- 2.39.5