xref: /freebsd/tools/build/cross-build/include/common/sys/cdefs.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2018-2020 Alex Richardson <arichardson@FreeBSD.org>
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory (Department of Computer Science and
8  * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9  * DARPA SSITH research programme.
10  *
11  * This software was developed by SRI International and the University of
12  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
13  * ("CTSRD"), as part of the DARPA CRASH research programme.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD$
37  */
38 #pragma once
39 /* musl libc does not provide a sys/cdefs.h header */
40 #if __has_include_next(<sys/cdefs.h>)
41 #include_next <sys/cdefs.h>
42 #else
43 /* No sys/cdefs.h header exists so we have to provide some basic macros */
44 #ifdef __cplusplus
45 #define __BEGIN_DECLS extern "C" {
46 #define __END_DECLS }
47 #else
48 #define __BEGIN_DECLS
49 #define __END_DECLS
50 #endif
51 
52 #endif
53 
54 #ifndef __FBSDID
55 #define __FBSDID(id)
56 #endif
57 
58 #ifndef __IDSTRING
59 #define __IDSTRING(name, string)
60 #endif
61 
62 #ifndef __pure
63 #define __pure __attribute__((__pure__))
64 #endif
65 #ifndef __packed
66 #define __packed __attribute__((__packed__))
67 #endif
68 #ifndef __dead2
69 #define __dead2 __attribute__((__noreturn__))
70 #endif
71 #ifndef __pure2
72 #define __pure2 __attribute__((__const__))
73 #endif
74 #ifndef __used
75 #define __used __attribute__((__used__))
76 #endif
77 #ifndef __aligned
78 #define __aligned(x) __attribute__((__aligned__(x)))
79 #endif
80 #ifndef __section
81 #define __section(x) __attribute__((__section__(x)))
82 #endif
83 
84 #ifndef __alloc_size
85 #define __alloc_size(...) __attribute__((__alloc_size__(__VA_ARGS__)))
86 #endif
87 #ifndef __alloc_size2
88 #define __alloc_size2(n, x) __attribute__((__alloc_size__(n, x)))
89 #endif
90 #ifndef __alloc_align
91 #define __alloc_align(x) __attribute__((__alloc_align__(x)))
92 #endif
93 #ifndef __result_use_check
94 #define __result_use_check __attribute__((__warn_unused_result__))
95 #endif
96 #ifndef __printflike
97 #define __printflike(fmtarg, firstvararg) \
98 	__attribute__((__format__(__printf__, fmtarg, firstvararg)))
99 #endif
100 #ifndef __printf0like
101 #define __printf0like(fmtarg, firstvararg) \
102 	__attribute__((__format__(__printf0__, fmtarg, firstvararg)))
103 #endif
104 
105 #ifndef __predict_true
106 #define __predict_true(exp) __builtin_expect((exp), 1)
107 #endif
108 #ifndef __predict_false
109 #define __predict_false(exp) __builtin_expect((exp), 0)
110 #endif
111 
112 #ifndef __weak_symbol
113 #define __weak_symbol __attribute__((__weak__))
114 #endif
115 #ifndef __weak_reference
116 #ifdef __ELF__
117 #define __weak_reference(sym, alias) \
118 	__asm__(".weak " #alias);    \
119 	__asm__(".equ " #alias ", " #sym)
120 #else
121 #define __weak_reference(sym, alias) \
122 	static int alias() __attribute__((weakref(#sym)));
123 #endif
124 #endif
125 
126 /* Some files built as part of the bootstrap libegacy use these macros, but
127  * since we aren't actually building libc.so, we can defined them to be empty */
128 #ifndef __sym_compat
129 #define __sym_compat(sym, impl, verid) /* not needed for bootstrapping */
130 #endif
131 #ifndef __sym_default
132 #define __sym_default(sym, impl, verid) /* not needed for bootstrapping */
133 #endif
134 #ifndef __sym_default
135 #define __warn_references(sym, msg) /* not needed for bootstrapping */
136 #endif
137 
138 #ifndef __malloc_like
139 #define __malloc_like __attribute__((__malloc__))
140 #endif
141 
142 #ifndef __min_size
143 #if !defined(__cplusplus)
144 #define __min_size(x) static(x)
145 #else
146 #define __min_size(x) (x)
147 #endif
148 #endif
149 
150 #ifndef __unused
151 #define __unused __attribute__((unused))
152 #endif
153 #define __format_arg(fmtarg) __attribute__((__format_arg__(fmtarg)))
154 
155 #ifndef __exported
156 #define __exported __attribute__((__visibility__("default")))
157 #endif
158 #ifndef __hidden
159 #define __hidden __attribute__((__visibility__("hidden")))
160 #endif
161 
162 #ifndef __unreachable
163 #define __unreachable() __builtin_unreachable()
164 #endif
165 
166 #ifndef __clang__
167 /* GCC doesn't like the printf0 format specifier. Clang treats it the same as
168  * printf so add the compatibility macro here. */
169 #define __printf0__ __printf__
170 #endif
171 
172 /* On MacOS __CONCAT is defined as x ## y, which won't expand macros */
173 #undef __CONCAT
174 #define __CONCAT1(x, y) x##y
175 #define __CONCAT(x, y) __CONCAT1(x, y)
176 
177 #ifndef __STRING
178 #define __STRING(x) #x /* stringify without expanding x */
179 #endif
180 #ifndef __XSTRING
181 #define __XSTRING(x) __STRING(x) /* expand x, then stringify */
182 #endif
183 
184 #ifndef __has_feature
185 #define __has_feature(...) 0
186 #endif
187 
188 #ifndef __has_builtin
189 #define __has_builtin(...) 0
190 #endif
191 
192 /*
193  * Nullability qualifiers: currently only supported by Clang.
194  */
195 #if !(defined(__clang__) && __has_feature(nullability))
196 #define _Nonnull
197 #define _Nullable
198 #define _Null_unspecified
199 #define __NULLABILITY_PRAGMA_PUSH
200 #define __NULLABILITY_PRAGMA_POP
201 #else
202 #define __NULLABILITY_PRAGMA_PUSH        \
203 	_Pragma("clang diagnostic push") \
204 	    _Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
205 #define __NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
206 #endif
207 
208 #ifndef __offsetof
209 #define __offsetof(type, field) __builtin_offsetof(type, field)
210 #endif
211 
212 #define __rangeof(type, start, end) \
213 	(__offsetof(type, end) - __offsetof(type, start))
214 
215 #ifndef __containerof
216 #define __containerof(x, s, m)                                           \
217 	({                                                               \
218 		const volatile __typeof(((s *)0)->m) *__x = (x);         \
219 		__DEQUALIFY(                                             \
220 		    s *, (const volatile char *)__x - __offsetof(s, m)); \
221 	})
222 #endif
223 
224 #ifndef __RCSID
225 #define __RCSID(x)
226 #endif
227 #ifndef __FBSDID
228 #define __FBSDID(x)
229 #endif
230 #ifndef __RCSID
231 #define __RCSID(x)
232 #endif
233 #ifndef __RCSID_SOURCE
234 #define __RCSID_SOURCE(x)
235 #endif
236 #ifndef __SCCSID
237 #define __SCCSID(x)
238 #endif
239 #ifndef __COPYRIGHT
240 #define __COPYRIGHT(x)
241 #endif
242 #ifndef __DECONST
243 #define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var))
244 #endif
245 
246 #ifndef __DEVOLATILE
247 #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
248 #endif
249 
250 #ifndef __DEQUALIFY
251 #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
252 #endif
253 
254 
255 /* Expose all declarations when using FreeBSD headers */
256 #define	__POSIX_VISIBLE		200809
257 #define	__XSI_VISIBLE		700
258 #define	__BSD_VISIBLE		1
259 #define	__ISO_C_VISIBLE		2011
260 #define	__EXT1_VISIBLE		1
261 
262 /* Alignment builtins for better type checking and improved code generation. */
263 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
264 #if !__has_builtin(__builtin_is_aligned)
265 #define __builtin_is_aligned(x, align)	\
266 	(((__uintptr_t)x & ((align) - 1)) == 0)
267 #endif
268 #if !__has_builtin(__builtin_align_up)
269 #define __builtin_align_up(x, align)	\
270 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
271 #endif
272 #if !__has_builtin(__builtin_align_down)
273 #define __builtin_align_down(x, align)	\
274 	((__typeof__(x))((x)&(~((align)-1))))
275 #endif
276 
277 #define __align_up(x, y) __builtin_align_up(x, y)
278 #define __align_down(x, y) __builtin_align_down(x, y)
279 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
280