1 /* A substitute for POSIX 2008 <stddef.h>, for platforms that have issues.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Eric Blake. */
21 * POSIX 2008 and ISO C 23 <stddef.h> for platforms that have issues.
22 * <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stddef.h.html>
26 @PRAGMA_SYSTEM_HEADER@
30 #if (defined __need_wchar_t || defined __need_size_t \
31 || defined __need_ptrdiff_t || defined __need_NULL \
32 || defined __need_wint_t) \
33 /* Avoid warning triggered by "gcc -std=gnu23 -Wsystem-headers" \
34 in Fedora 40 with gcc 14.0.1. \
35 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114870>. */ \
36 && !@STDDEF_NOT_IDEMPOTENT@
37 /* Special invocation convention inside gcc header files. In
38 particular, <stddef.h> in some ancient versions of GCC blindly
39 redefined NULL when __need_wint_t was defined, even though wint_t
40 is not normally provided by <stddef.h>.
41 (FIXME: It's not clear what GCC versions those were - perhaps so
42 ancient that we can stop worrying about this?)
43 Although glibc 2.26 (2017) and later do not use __need_wint_t,
44 for portability to macOS, Cygwin, Haiku, and older Glibc + GCC,
45 remember if special invocation has ever been used to obtain wint_t,
46 in which case we need to clean up NULL yet again. */
48 # if !(defined _@GUARD_PREFIX@_STDDEF_H && defined _@GUARD_PREFIX@_STDDEF_WINT_T)
50 # define _@GUARD_PREFIX@_STDDEF_WINT_T
52 # @INCLUDE_NEXT@ @NEXT_STDDEF_H@
53 /* On TinyCC, make sure that the macros that indicate the special invocation
54 convention get undefined. */
55 # undef __need_wchar_t
57 # undef __need_ptrdiff_t
63 /* For @STDDEF_NOT_IDEMPOTENT@. */
64 # undef __need_wchar_t
66 # undef __need_ptrdiff_t
70 /* Normal invocation convention. */
72 # ifndef _@GUARD_PREFIX@_STDDEF_H
74 /* On AIX 7.2, with xlc in 64-bit mode, <stddef.h> defines max_align_t to a
75 type with alignment 4, but 'long' has alignment 8. */
76 # if defined _AIX && defined __LP64__ && !@HAVE_MAX_ALIGN_T@
77 # if !GNULIB_defined_max_align_t
79 /* /usr/include/stddef.h has already defined max_align_t. Override it. */
80 typedef long rpl_max_align_t
;
81 # define max_align_t rpl_max_align_t
83 /* Prevent /usr/include/stddef.h from defining max_align_t. */
84 typedef long max_align_t
;
87 # define __CLANG_MAX_ALIGN_T_DEFINED
88 # define GNULIB_defined_max_align_t 1
92 # if !defined _GCC_NULLPTR_T && !@NULLPTR_T_NEEDS_STDDEF@
93 /* Suppress unwanted nullptr_t typedef. See
94 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114869>. */
95 # define _GCC_NULLPTR_T
98 /* The include_next requires a split double-inclusion guard. */
100 # @INCLUDE_NEXT@ @NEXT_STDDEF_H@
102 /* On NetBSD 5.0, the definition of NULL lacks proper parentheses. */
103 # if (@REPLACE_NULL@ \
104 && (!defined _@GUARD_PREFIX@_STDDEF_H || defined _@GUARD_PREFIX@_STDDEF_WINT_T))
107 /* ISO C++ says that the macro NULL must expand to an integer constant
108 expression, hence '((void *) 0)' is not allowed in C++. */
110 /* GNU C++ has a __null macro that behaves like an integer ('int' or
111 'long') but has the same size as a pointer. Use that, to avoid
118 # define NULL ((void *) 0)
122 # ifndef _@GUARD_PREFIX@_STDDEF_H
123 # define _@GUARD_PREFIX@_STDDEF_H
125 /* This file uses _Noreturn, _GL_ATTRIBUTE_NOTHROW. */
126 #if !_GL_CONFIG_H_INCLUDED
127 #error "Please include config.h first."
130 /* _GL_ATTRIBUTE_NOTHROW declares that the function does not throw exceptions.
132 #ifndef _GL_ATTRIBUTE_NOTHROW
133 # if defined __cplusplus
134 # if (__GNUC__ + (__GNUC_MINOR__ >= 8) > 2) || __clang_major__ >= 4
135 # if __cplusplus >= 201103L
136 # define _GL_ATTRIBUTE_NOTHROW noexcept (true)
138 # define _GL_ATTRIBUTE_NOTHROW throw ()
141 # define _GL_ATTRIBUTE_NOTHROW
144 # if (__GNUC__ + (__GNUC_MINOR__ >= 3) > 3) || defined __clang__
145 # define _GL_ATTRIBUTE_NOTHROW __attribute__ ((__nothrow__))
147 # define _GL_ATTRIBUTE_NOTHROW
152 /* Some platforms lack max_align_t. The check for _GCC_MAX_ALIGN_T is
153 a hack in case the configure-time test was done with g++ even though
154 we are currently compiling with gcc.
155 On MSVC, max_align_t is defined only in C++ mode, after <cstddef> was
156 included. Its definition is good since it has an alignment of 8 (on x86
158 Similarly on OS/2 kLIBC. */
159 #if (defined _MSC_VER || (defined __KLIBC__ && !defined __LIBCN__)) \
160 && defined __cplusplus
163 # if ! (@HAVE_MAX_ALIGN_T@ || (defined _GCC_MAX_ALIGN_T && !defined __clang__))
164 # if !GNULIB_defined_max_align_t
165 /* On the x86, the maximum storage alignment of double, long, etc. is 4,
166 but GCC's C11 ABI for x86 says that max_align_t has an alignment of 8,
167 and the C11 standard allows this. Work around this problem by
168 using __alignof__ (which returns 8 for double) rather than _Alignof
169 (which returns 4), and align each union member accordingly. */
170 # if defined __GNUC__ || (__clang_major__ >= 4)
171 # define _GL_STDDEF_ALIGNAS(type) \
172 __attribute__ ((__aligned__ (__alignof__ (type))))
174 # define _GL_STDDEF_ALIGNAS(type) /* */
178 char *__p
_GL_STDDEF_ALIGNAS (char *);
179 double __d
_GL_STDDEF_ALIGNAS (double);
180 long double __ld
_GL_STDDEF_ALIGNAS (long double);
181 long int __i
_GL_STDDEF_ALIGNAS (long int);
183 # define max_align_t rpl_max_align_t
184 # define __CLANG_MAX_ALIGN_T_DEFINED
185 # define GNULIB_defined_max_align_t 1
190 /* ISO C 23 § 7.21.1 The unreachable macro */
193 /* Code borrowed from verify.h. */
194 # ifndef _GL_HAS_BUILTIN_UNREACHABLE
195 # if defined __clang_major__ && __clang_major__ < 5
196 # define _GL_HAS_BUILTIN_UNREACHABLE 0
197 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__) && !defined __clang__
198 # define _GL_HAS_BUILTIN_UNREACHABLE 1
199 # elif defined __has_builtin
200 # define _GL_HAS_BUILTIN_UNREACHABLE __has_builtin (__builtin_unreachable)
202 # define _GL_HAS_BUILTIN_UNREACHABLE 0
206 # if _GL_HAS_BUILTIN_UNREACHABLE
207 # define unreachable() __builtin_unreachable ()
208 # elif 1200 <= _MSC_VER
209 # define unreachable() __assume (0)
211 /* Declare abort(), without including <stdlib.h>. */
213 # if defined __cplusplus
218 # if defined __cplusplus && (__GLIBC__ >= 2)
219 _GL_ATTRIBUTE_NOTHROW
222 # define unreachable() abort ()
227 # endif /* _@GUARD_PREFIX@_STDDEF_H */
228 # endif /* _@GUARD_PREFIX@_STDDEF_H */
229 #endif /* __need_XXX */