openat: don’t close (-1)
[gnulib.git] / lib / stddef.in.h
blobe39268c60c90d3d2ebbfaed5410ffcaff584aded
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>
25 #if __GNUC__ >= 3
26 @PRAGMA_SYSTEM_HEADER@
27 #endif
28 @PRAGMA_COLUMNS@
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)
49 # ifdef __need_wint_t
50 # define _@GUARD_PREFIX@_STDDEF_WINT_T
51 # endif
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
56 # undef __need_size_t
57 # undef __need_ptrdiff_t
58 # undef __need_NULL
59 # undef __need_wint_t
60 # endif
62 #else
63 /* For @STDDEF_NOT_IDEMPOTENT@. */
64 # undef __need_wchar_t
65 # undef __need_size_t
66 # undef __need_ptrdiff_t
67 # undef __need_NULL
68 # undef __need_wint_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
78 # ifdef _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
82 # else
83 /* Prevent /usr/include/stddef.h from defining max_align_t. */
84 typedef long max_align_t;
85 # define _MAX_ALIGN_T
86 # endif
87 # define __CLANG_MAX_ALIGN_T_DEFINED
88 # define GNULIB_defined_max_align_t 1
89 # endif
90 # endif
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
96 # endif
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))
105 # undef NULL
106 # ifdef __cplusplus
107 /* ISO C++ says that the macro NULL must expand to an integer constant
108 expression, hence '((void *) 0)' is not allowed in C++. */
109 # if __GNUG__ >= 3
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
112 warnings. */
113 # define NULL __null
114 # else
115 # define NULL 0L
116 # endif
117 # else
118 # define NULL ((void *) 0)
119 # endif
120 # endif
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."
128 #endif
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)
137 # else
138 # define _GL_ATTRIBUTE_NOTHROW throw ()
139 # endif
140 # else
141 # define _GL_ATTRIBUTE_NOTHROW
142 # endif
143 # else
144 # if (__GNUC__ + (__GNUC_MINOR__ >= 3) > 3) || defined __clang__
145 # define _GL_ATTRIBUTE_NOTHROW __attribute__ ((__nothrow__))
146 # else
147 # define _GL_ATTRIBUTE_NOTHROW
148 # endif
149 # endif
150 #endif
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
157 and x86_64).
158 Similarly on OS/2 kLIBC. */
159 #if (defined _MSC_VER || (defined __KLIBC__ && !defined __LIBCN__)) \
160 && defined __cplusplus
161 # include <cstddef>
162 #else
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))))
173 # else
174 # define _GL_STDDEF_ALIGNAS(type) /* */
175 # endif
176 typedef union
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);
182 } rpl_max_align_t;
183 # define max_align_t rpl_max_align_t
184 # define __CLANG_MAX_ALIGN_T_DEFINED
185 # define GNULIB_defined_max_align_t 1
186 # endif
187 # endif
188 #endif
190 /* ISO C 23 § 7.21.1 The unreachable macro */
191 #ifndef unreachable
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)
201 # else
202 # define _GL_HAS_BUILTIN_UNREACHABLE 0
203 # endif
204 # endif
206 # if _GL_HAS_BUILTIN_UNREACHABLE
207 # define unreachable() __builtin_unreachable ()
208 # elif 1200 <= _MSC_VER
209 # define unreachable() __assume (0)
210 # else
211 /* Declare abort(), without including <stdlib.h>. */
212 extern
213 # if defined __cplusplus
215 # endif
216 _Noreturn
217 void abort (void)
218 # if defined __cplusplus && (__GLIBC__ >= 2)
219 _GL_ATTRIBUTE_NOTHROW
220 # endif
222 # define unreachable() abort ()
223 # endif
225 #endif
227 # endif /* _@GUARD_PREFIX@_STDDEF_H */
228 # endif /* _@GUARD_PREFIX@_STDDEF_H */
229 #endif /* __need_XXX */