1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-11-28 23:44:09 +03:00
Files
glibc/string/tst-const.c
Joseph Myers cd748a63ab Implement C23 const-preserving standard library macros
C23 makes various standard library functions, that return a pointer
into an input array, into macros that return a pointer to const when
the relevant argument passed to the macro is a pointer to const.  (The
requirement is for macros, with the existing function types applying
when macro expansion is suppressed.  When a null pointer constant is
passed, such as integer 0, that's the same as a pointer to non-const.)

Implement this feature.  This only applies to C, not C++, since such
macros are not an appropriate way of doing this for C++ and all the
affected functions other than bsearch have overloads to implement an
equivalent feature for C++ anyway.  Nothing is done to apply such a
change to any non-C23 functions with the same property of returning a
pointer into an input array.

The feature is also disabled when _LIBC is defined, since there are
various places in glibc that either redefine these identifiers as
macros, or define the functions themselves, and would need changing to
work in the presence of these macro definitions.  A natural question
is whether we should in fact change those places and not disable the
macro definitions for _LIBC.  If so, we'd need a solution for the
places in glibc that define the macro *before* including the relevant
header (in order in effect to disable the header declaration of the
function by renaming that declaration).

One testcase has #undef added to avoid conflicting with this feature
and another has const added; -Wno-discarded-qualifiers is added for
building zic (but could be removed once there's a new upstream tzcode
release that's const-safe with this C23 change and glibc has updated
to code from that new release).  Probably other places in glibc proper
would need const added if we remove the _LIBC conditionals.

Another question would be whether some GCC extension should be added
to support this feature better with macros that only expand each
argument once (as well as reducing duplication of diagnostics for bad
usages such as non-pointer and pointer-to-volatile-qualfied
arguments).

Tested for x86_64.
2025-11-20 19:31:04 +00:00

108 lines
3.9 KiB
C

/* Test <string.h> const-generic macros.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <libc-diag.h>
void *vp;
const void *cvp;
int *ip;
const int *cip;
char *cp;
const char *ccp;
int c;
size_t sz;
#define CHECK_TYPE(EXPR, TYPE) \
_Static_assert (_Generic (EXPR, TYPE: 1), "type check")
static int
do_test (void)
{
/* This is a compilation test. */
CHECK_TYPE (memchr (vp, c, sz), void *);
CHECK_TYPE (memchr (cvp, c, sz), const void *);
CHECK_TYPE (memchr (ip, c, sz), void *);
CHECK_TYPE (memchr (cip, c, sz), const void *);
CHECK_TYPE (memchr (cp, c, sz), void *);
CHECK_TYPE (memchr (ccp, c, sz), const void *);
DIAG_PUSH_NEEDS_COMMENT;
/* This deliberately tests the type of the result with a null
pointer constant argument. */
DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
CHECK_TYPE (memchr (0, c, sz), void *);
CHECK_TYPE (memchr ((void *) 0, c, sz), void *);
DIAG_POP_NEEDS_COMMENT;
CHECK_TYPE ((memchr) (cvp, c, sz), void *);
CHECK_TYPE (strchr (vp, c), char *);
CHECK_TYPE (strchr (cvp, c), const char *);
CHECK_TYPE (strchr (cp, c), char *);
CHECK_TYPE (strchr (ccp, c), const char *);
DIAG_PUSH_NEEDS_COMMENT;
DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
CHECK_TYPE (strchr (0, c), char *);
CHECK_TYPE (strchr ((void *) 0, c), char *);
DIAG_POP_NEEDS_COMMENT;
CHECK_TYPE ((strchr) (ccp, c), char *);
CHECK_TYPE (strpbrk (vp, vp), char *);
CHECK_TYPE (strpbrk (vp, cvp), char *);
CHECK_TYPE (strpbrk (cvp, vp), const char *);
CHECK_TYPE (strpbrk (cvp, cvp), const char *);
CHECK_TYPE (strpbrk (cp, cp), char *);
CHECK_TYPE (strpbrk (cp, ccp), char *);
CHECK_TYPE (strpbrk (ccp, cp), const char *);
CHECK_TYPE (strpbrk (ccp, ccp), const char *);
DIAG_PUSH_NEEDS_COMMENT;
DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
CHECK_TYPE (strpbrk (0, cp), char *);
CHECK_TYPE (strpbrk (0, ccp), char *);
CHECK_TYPE (strpbrk ((void *) 0, cp), char *);
CHECK_TYPE (strpbrk ((void *) 0, ccp), char *);
DIAG_POP_NEEDS_COMMENT;
CHECK_TYPE ((strpbrk) (ccp, ccp), char *);
CHECK_TYPE (strrchr (vp, c), char *);
CHECK_TYPE (strrchr (cvp, c), const char *);
CHECK_TYPE (strrchr (cp, c), char *);
CHECK_TYPE (strrchr (ccp, c), const char *);
DIAG_PUSH_NEEDS_COMMENT;
DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
CHECK_TYPE (strrchr (0, c), char *);
CHECK_TYPE (strrchr ((void *) 0, c), char *);
DIAG_POP_NEEDS_COMMENT;
CHECK_TYPE ((strrchr) (ccp, c), char *);
CHECK_TYPE (strstr (vp, vp), char *);
CHECK_TYPE (strstr (vp, cvp), char *);
CHECK_TYPE (strstr (cvp, vp), const char *);
CHECK_TYPE (strstr (cvp, cvp), const char *);
CHECK_TYPE (strstr (cp, cp), char *);
CHECK_TYPE (strstr (cp, ccp), char *);
CHECK_TYPE (strstr (ccp, cp), const char *);
CHECK_TYPE (strstr (ccp, ccp), const char *);
DIAG_PUSH_NEEDS_COMMENT;
DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
CHECK_TYPE (strstr (0, cp), char *);
CHECK_TYPE (strstr (0, ccp), char *);
CHECK_TYPE (strstr ((void *) 0, cp), char *);
CHECK_TYPE (strstr ((void *) 0, ccp), char *);
DIAG_POP_NEEDS_COMMENT;
CHECK_TYPE ((strstr) (ccp, ccp), char *);
return 0;
}
#include <support/test-driver.c>