cdefs.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /* libc/sys/linux/sys/cdefs.h - Helper macros for K&R vs. ANSI C compat. */
  2. /* Written 2000 by Werner Almesberger */
  3. /*-
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Copyright (c) 1991, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * This code is derived from software contributed to Berkeley by
  10. * Berkeley Software Design, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * @(#)cdefs.h 8.8 (Berkeley) 1/9/95
  37. * $FreeBSD$
  38. */
  39. #ifndef _SYS_CDEFS_H_
  40. #define _SYS_CDEFS_H_
  41. #include <machine/_default_types.h>
  42. #include <sys/features.h>
  43. #include <stddef.h>
  44. #define __PMT(args) args
  45. #define __DOTS , ...
  46. #define __THROW
  47. #ifdef __GNUC__
  48. # define __ASMNAME(cname) __XSTRING (__USER_LABEL_PREFIX__) cname
  49. #endif
  50. #define __ptr_t void *
  51. #define __long_double_t long double
  52. #define __attribute_malloc__
  53. #define __attribute_pure__
  54. #define __attribute_format_strfmon__(a,b)
  55. #define __flexarr [0]
  56. #ifndef __BOUNDED_POINTERS__
  57. # define __bounded /* nothing */
  58. # define __unbounded /* nothing */
  59. # define __ptrvalue /* nothing */
  60. #endif
  61. /*
  62. * Testing against Clang-specific extensions.
  63. */
  64. #ifndef __has_attribute
  65. #define __has_attribute(x) 0
  66. #endif
  67. #ifndef __has_extension
  68. #define __has_extension __has_feature
  69. #endif
  70. #ifndef __has_feature
  71. #define __has_feature(x) 0
  72. #endif
  73. #ifndef __has_include
  74. #define __has_include(x) 0
  75. #endif
  76. #ifndef __has_builtin
  77. #define __has_builtin(x) 0
  78. #endif
  79. #if defined(__cplusplus)
  80. #define __BEGIN_DECLS extern "C" {
  81. #define __END_DECLS }
  82. #else
  83. #define __BEGIN_DECLS
  84. #define __END_DECLS
  85. #endif
  86. /*
  87. * This code has been put in place to help reduce the addition of
  88. * compiler specific defines in FreeBSD code. It helps to aid in
  89. * having a compiler-agnostic source tree.
  90. */
  91. #if defined(__GNUC__) || defined(__INTEL_COMPILER)
  92. #if __GNUC__ >= 3 || defined(__INTEL_COMPILER)
  93. #define __GNUCLIKE_ASM 3
  94. #define __GNUCLIKE_MATH_BUILTIN_CONSTANTS
  95. #else
  96. #define __GNUCLIKE_ASM 2
  97. #endif
  98. #define __GNUCLIKE___TYPEOF 1
  99. #define __GNUCLIKE___OFFSETOF 1
  100. #define __GNUCLIKE___SECTION 1
  101. #ifndef __INTEL_COMPILER
  102. #define __GNUCLIKE_CTOR_SECTION_HANDLING 1
  103. #endif
  104. #define __GNUCLIKE_BUILTIN_CONSTANT_P 1
  105. #if defined(__INTEL_COMPILER) && defined(__cplusplus) && \
  106. __INTEL_COMPILER < 800
  107. #undef __GNUCLIKE_BUILTIN_CONSTANT_P
  108. #endif
  109. #if (__GNUC_MINOR__ > 95 || __GNUC__ >= 3)
  110. #define __GNUCLIKE_BUILTIN_VARARGS 1
  111. #define __GNUCLIKE_BUILTIN_STDARG 1
  112. #define __GNUCLIKE_BUILTIN_VAALIST 1
  113. #endif
  114. #if defined(__GNUC__)
  115. #define __GNUC_VA_LIST_COMPATIBILITY 1
  116. #endif
  117. /*
  118. * Compiler memory barriers, specific to gcc and clang.
  119. */
  120. #if defined(__GNUC__)
  121. #define __compiler_membar() __asm __volatile(" " : : : "memory")
  122. #endif
  123. #ifndef __INTEL_COMPILER
  124. #define __GNUCLIKE_BUILTIN_NEXT_ARG 1
  125. #define __GNUCLIKE_MATH_BUILTIN_RELOPS
  126. #endif
  127. #define __GNUCLIKE_BUILTIN_MEMCPY 1
  128. /* XXX: if __GNUC__ >= 2: not tested everywhere originally, where replaced */
  129. #define __CC_SUPPORTS_INLINE 1
  130. #define __CC_SUPPORTS___INLINE 1
  131. #define __CC_SUPPORTS___INLINE__ 1
  132. #define __CC_SUPPORTS___FUNC__ 1
  133. #define __CC_SUPPORTS_WARNING 1
  134. #define __CC_SUPPORTS_VARADIC_XXX 1 /* see varargs.h */
  135. #define __CC_SUPPORTS_DYNAMIC_ARRAY_INIT 1
  136. #endif /* __GNUC__ || __INTEL_COMPILER */
  137. /*
  138. * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
  139. * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
  140. * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
  141. * mode -- there must be no spaces between its arguments, and for nested
  142. * __CONCAT's, all the __CONCAT's must be at the left. __CONCAT can also
  143. * concatenate double-quoted strings produced by the __STRING macro, but
  144. * this only works with ANSI C.
  145. *
  146. * __XSTRING is like __STRING, but it expands any macros in its argument
  147. * first. It is only available with ANSI C.
  148. */
  149. #if defined(__STDC__) || defined(__cplusplus)
  150. #define __P(protos) protos /* full-blown ANSI C */
  151. #define __CONCAT1(x,y) x ## y
  152. #define __CONCAT(x,y) __CONCAT1(x,y)
  153. #define __STRING(x) #x /* stringify without expanding x */
  154. #define __XSTRING(x) __STRING(x) /* expand x, then stringify */
  155. #define __const const /* define reserved names to standard */
  156. #define __signed signed
  157. #define __volatile volatile
  158. #if defined(__cplusplus)
  159. #define __inline inline /* convert to C++ keyword */
  160. #else
  161. #if !(defined(__CC_SUPPORTS___INLINE))
  162. #define __inline /* delete GCC keyword */
  163. #endif /* ! __CC_SUPPORTS___INLINE */
  164. #endif /* !__cplusplus */
  165. #else /* !(__STDC__ || __cplusplus) */
  166. #define __P(protos) () /* traditional C preprocessor */
  167. #define __CONCAT(x,y) x/**/y
  168. #define __STRING(x) "x"
  169. #if !defined(__CC_SUPPORTS___INLINE)
  170. #define __const /* delete pseudo-ANSI C keywords */
  171. #define __inline
  172. #define __signed
  173. #define __volatile
  174. /*
  175. * In non-ANSI C environments, new programs will want ANSI-only C keywords
  176. * deleted from the program and old programs will want them left alone.
  177. * When using a compiler other than gcc, programs using the ANSI C keywords
  178. * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
  179. * When using "gcc -traditional", we assume that this is the intent; if
  180. * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
  181. */
  182. #ifndef NO_ANSI_KEYWORDS
  183. #define const /* delete ANSI C keywords */
  184. #define inline
  185. #define signed
  186. #define volatile
  187. #endif /* !NO_ANSI_KEYWORDS */
  188. #endif /* !__CC_SUPPORTS___INLINE */
  189. #endif /* !(__STDC__ || __cplusplus) */
  190. /*
  191. * Compiler-dependent macros to help declare dead (non-returning) and
  192. * pure (no side effects) functions, and unused variables. They are
  193. * null except for versions of gcc that are known to support the features
  194. * properly (old versions of gcc-2 supported the dead and pure features
  195. * in a different (wrong) way). If we do not provide an implementation
  196. * for a given compiler, let the compile fail if it is told to use
  197. * a feature that we cannot live without.
  198. */
  199. #define __weak_symbol __attribute__((__weak__))
  200. #if !__GNUC_PREREQ__(2, 5) && !defined(__INTEL_COMPILER)
  201. #define __dead2
  202. #define __pure2
  203. #define __unused
  204. #endif
  205. #if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7 && !defined(__INTEL_COMPILER)
  206. #define __dead2 __attribute__((__noreturn__))
  207. #define __pure2 __attribute__((__const__))
  208. #define __unused
  209. /* XXX Find out what to do for __packed, __aligned and __section */
  210. #endif
  211. #if __GNUC_PREREQ__(2, 7) || defined(__INTEL_COMPILER)
  212. #define __dead2 __attribute__((__noreturn__))
  213. #define __pure2 __attribute__((__const__))
  214. #define __unused __attribute__((__unused__))
  215. #define __used __attribute__((__used__))
  216. #define __packed __attribute__((__packed__))
  217. #define __aligned(x) __attribute__((__aligned__(x)))
  218. #define __section(x) __attribute__((__section__(x)))
  219. #endif
  220. #if __GNUC_PREREQ__(4, 3) || __has_attribute(__alloc_size__)
  221. #define __alloc_size(x) __attribute__((__alloc_size__(x)))
  222. #define __alloc_size2(n, x) __attribute__((__alloc_size__(n, x)))
  223. #else
  224. #define __alloc_size(x)
  225. #define __alloc_size2(n, x)
  226. #endif
  227. #if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__)
  228. #define __alloc_align(x) __attribute__((__alloc_align__(x)))
  229. #else
  230. #define __alloc_align(x)
  231. #endif
  232. #if !__GNUC_PREREQ__(2, 95)
  233. #define __alignof(x) __offsetof(struct { char __a; x __b; }, __b)
  234. #endif
  235. /*
  236. * Keywords added in C11.
  237. */
  238. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
  239. #if !__has_extension(c_alignas)
  240. #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
  241. __has_extension(cxx_alignas)
  242. #define _Alignas(x) alignas(x)
  243. #else
  244. /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
  245. #define _Alignas(x) __aligned(x)
  246. #endif
  247. #endif
  248. #if defined(__cplusplus) && __cplusplus >= 201103L
  249. #define _Alignof(x) alignof(x)
  250. #else
  251. #define _Alignof(x) __alignof(x)
  252. #endif
  253. #if !defined(__cplusplus) && !__has_extension(c_atomic) && \
  254. !__has_extension(cxx_atomic)
  255. /*
  256. * No native support for _Atomic(). Place object in structure to prevent
  257. * most forms of direct non-atomic access.
  258. */
  259. #define _Atomic(T) struct { T volatile __val; }
  260. #endif
  261. #if defined(__cplusplus) && __cplusplus >= 201103L
  262. #define _Noreturn [[noreturn]]
  263. #else
  264. #define _Noreturn __dead2
  265. #endif
  266. #if !__has_extension(c_static_assert)
  267. #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
  268. __has_extension(cxx_static_assert)
  269. #define _Static_assert(x, y) static_assert(x, y)
  270. #elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
  271. /* Nothing, gcc 4.6 and higher has _Static_assert built-in */
  272. #elif defined(__COUNTER__)
  273. #define _Static_assert(x, y) __Static_assert(x, __COUNTER__)
  274. #define __Static_assert(x, y) ___Static_assert(x, y)
  275. #define ___Static_assert(x, y) typedef char __assert_ ## y[(x) ? 1 : -1] \
  276. __unused
  277. #else
  278. #define _Static_assert(x, y) struct __hack
  279. #endif
  280. #endif
  281. #if !__has_extension(c_thread_local)
  282. /*
  283. * XXX: Some compilers (Clang 3.3, GCC 4.7) falsely announce C++11 mode
  284. * without actually supporting the thread_local keyword. Don't check for
  285. * the presence of C++11 when defining _Thread_local.
  286. */
  287. #if /* (defined(__cplusplus) && __cplusplus >= 201103L) || */ \
  288. __has_extension(cxx_thread_local)
  289. #define _Thread_local thread_local
  290. #else
  291. #define _Thread_local __thread
  292. #endif
  293. #endif
  294. #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
  295. /*
  296. * Emulation of C11 _Generic(). Unlike the previously defined C11
  297. * keywords, it is not possible to implement this using exactly the same
  298. * syntax. Therefore implement something similar under the name
  299. * __generic(). Unlike _Generic(), this macro can only distinguish
  300. * between a single type, so it requires nested invocations to
  301. * distinguish multiple cases.
  302. */
  303. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
  304. __has_extension(c_generic_selections)
  305. #define __generic(expr, t, yes, no) \
  306. _Generic(expr, t: yes, default: no)
  307. #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
  308. #define __generic(expr, t, yes, no) \
  309. __builtin_choose_expr( \
  310. __builtin_types_compatible_p(__typeof(expr), t), yes, no)
  311. #endif
  312. /*
  313. * C99 Static array indices in function parameter declarations. Syntax such as:
  314. * void bar(int myArray[static 10]);
  315. * is allowed in C99 but not in C++. Define __min_size appropriately so
  316. * headers using it can be compiled in either language. Use like this:
  317. * void bar(int myArray[__min_size(10)]);
  318. */
  319. #if !defined(__cplusplus) && \
  320. (defined(__clang__) || __GNUC_PREREQ__(4, 6)) && \
  321. (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
  322. #define __min_size(x) static (x)
  323. #else
  324. #define __min_size(x) (x)
  325. #endif
  326. #if __GNUC_PREREQ__(2, 96)
  327. #define __malloc_like __attribute__((__malloc__))
  328. #define __pure __attribute__((__pure__))
  329. #else
  330. #define __malloc_like
  331. #define __pure
  332. #endif
  333. #if __GNUC_PREREQ__(3, 1) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
  334. #define __always_inline __inline__ __attribute__((__always_inline__))
  335. #else
  336. #define __always_inline
  337. #endif
  338. #if __GNUC_PREREQ__(3, 1)
  339. #define __noinline __attribute__ ((__noinline__))
  340. #else
  341. #define __noinline
  342. #endif
  343. #if __GNUC_PREREQ__(3, 3)
  344. #define __nonnull(x) __attribute__((__nonnull__ x))
  345. #define __nonnull_all __attribute__((__nonnull__))
  346. #else
  347. #define __nonnull(x)
  348. #define __nonnull_all
  349. #endif
  350. #if __GNUC_PREREQ__(3, 4)
  351. #define __fastcall __attribute__((__fastcall__))
  352. #define __result_use_check __attribute__((__warn_unused_result__))
  353. #else
  354. #define __fastcall
  355. #define __result_use_check
  356. #endif
  357. #if __GNUC_PREREQ__(4, 1)
  358. #define __returns_twice __attribute__((__returns_twice__))
  359. #else
  360. #define __returns_twice
  361. #endif
  362. #if __GNUC_PREREQ__(4, 6) || __has_builtin(__builtin_unreachable)
  363. #define __unreachable() __builtin_unreachable()
  364. #else
  365. #define __unreachable() ((void)0)
  366. #endif
  367. /* XXX: should use `#if __STDC_VERSION__ < 199901'. */
  368. #if !__GNUC_PREREQ__(2, 7) && !defined(__INTEL_COMPILER)
  369. #define __func__ NULL
  370. #endif
  371. /*
  372. * GCC 2.95 provides `__restrict' as an extension to C90 to support the
  373. * C99-specific `restrict' type qualifier. We happen to use `__restrict' as
  374. * a way to define the `restrict' type qualifier without disturbing older
  375. * software that is unaware of C99 keywords.
  376. */
  377. #if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
  378. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
  379. #define __restrict
  380. #else
  381. #define __restrict restrict
  382. #endif
  383. #endif
  384. /*
  385. * GNU C version 2.96 adds explicit branch prediction so that
  386. * the CPU back-end can hint the processor and also so that
  387. * code blocks can be reordered such that the predicted path
  388. * sees a more linear flow, thus improving cache behavior, etc.
  389. *
  390. * The following two macros provide us with a way to utilize this
  391. * compiler feature. Use __predict_true() if you expect the expression
  392. * to evaluate to true, and __predict_false() if you expect the
  393. * expression to evaluate to false.
  394. *
  395. * A few notes about usage:
  396. *
  397. * * Generally, __predict_false() error condition checks (unless
  398. * you have some _strong_ reason to do otherwise, in which case
  399. * document it), and/or __predict_true() `no-error' condition
  400. * checks, assuming you want to optimize for the no-error case.
  401. *
  402. * * Other than that, if you don't know the likelihood of a test
  403. * succeeding from empirical or other `hard' evidence, don't
  404. * make predictions.
  405. *
  406. * * These are meant to be used in places that are run `a lot'.
  407. * It is wasteful to make predictions in code that is run
  408. * seldomly (e.g. at subsystem initialization time) as the
  409. * basic block reordering that this affects can often generate
  410. * larger code.
  411. */
  412. #if __GNUC_PREREQ__(2, 96)
  413. #define __predict_true(exp) __builtin_expect((exp), 1)
  414. #define __predict_false(exp) __builtin_expect((exp), 0)
  415. #else
  416. #define __predict_true(exp) (exp)
  417. #define __predict_false(exp) (exp)
  418. #endif
  419. #if __GNUC_PREREQ__(4, 0)
  420. #define __null_sentinel __attribute__((__sentinel__))
  421. #define __exported __attribute__((__visibility__("default")))
  422. /* Only default visibility is supported on PE/COFF targets. */
  423. #ifndef __CYGWIN__
  424. #define __hidden __attribute__((__visibility__("hidden")))
  425. #else
  426. #define __hidden
  427. #endif
  428. #else
  429. #define __null_sentinel
  430. #define __exported
  431. #define __hidden
  432. #endif
  433. #define __offsetof(type, field) offsetof(type, field)
  434. #define __rangeof(type, start, end) \
  435. (__offsetof(type, end) - __offsetof(type, start))
  436. /*
  437. * Given the pointer x to the member m of the struct s, return
  438. * a pointer to the containing structure. When using GCC, we first
  439. * assign pointer x to a local variable, to check that its type is
  440. * compatible with member m.
  441. */
  442. #if __GNUC_PREREQ__(3, 1)
  443. #define __containerof(x, s, m) ({ \
  444. const volatile __typeof(((s *)0)->m) *__x = (x); \
  445. __DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
  446. })
  447. #else
  448. #define __containerof(x, s, m) \
  449. __DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
  450. #endif
  451. /*
  452. * Compiler-dependent macros to declare that functions take printf-like
  453. * or scanf-like arguments. They are null except for versions of gcc
  454. * that are known to support the features properly (old versions of gcc-2
  455. * didn't permit keeping the keywords out of the application namespace).
  456. */
  457. #if !__GNUC_PREREQ__(2, 7) && !defined(__INTEL_COMPILER)
  458. #define __printflike(fmtarg, firstvararg)
  459. #define __scanflike(fmtarg, firstvararg)
  460. #define __format_arg(fmtarg)
  461. #define __strfmonlike(fmtarg, firstvararg)
  462. #define __strftimelike(fmtarg, firstvararg)
  463. #else
  464. #define __printflike(fmtarg, firstvararg) \
  465. __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
  466. #define __scanflike(fmtarg, firstvararg) \
  467. __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
  468. #define __format_arg(fmtarg) __attribute__((__format_arg__ (fmtarg)))
  469. #define __strfmonlike(fmtarg, firstvararg) \
  470. __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
  471. #define __strftimelike(fmtarg, firstvararg) \
  472. __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
  473. #endif
  474. /* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
  475. #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
  476. defined(__GNUC__) && !defined(__INTEL_COMPILER)
  477. #define __printf0like(fmtarg, firstvararg) \
  478. __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
  479. #else
  480. #define __printf0like(fmtarg, firstvararg)
  481. #endif
  482. #if defined(__GNUC__) || defined(__INTEL_COMPILER)
  483. #ifndef __INTEL_COMPILER
  484. #define __strong_reference(sym,aliassym) \
  485. extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
  486. #endif
  487. #ifdef __ELF__
  488. #ifdef __STDC__
  489. #define __weak_reference(sym,alias) \
  490. __asm__(".weak " #alias); \
  491. __asm__(".equ " #alias ", " #sym)
  492. #define __warn_references(sym,msg) \
  493. __asm__(".section .gnu.warning." #sym); \
  494. __asm__(".asciz \"" msg "\""); \
  495. __asm__(".previous")
  496. #define __sym_compat(sym,impl,verid) \
  497. __asm__(".symver " #impl ", " #sym "@" #verid)
  498. #define __sym_default(sym,impl,verid) \
  499. __asm__(".symver " #impl ", " #sym "@@" #verid)
  500. #else
  501. #define __weak_reference(sym,alias) \
  502. __asm__(".weak alias"); \
  503. __asm__(".equ alias, sym")
  504. #define __warn_references(sym,msg) \
  505. __asm__(".section .gnu.warning.sym"); \
  506. __asm__(".asciz \"msg\""); \
  507. __asm__(".previous")
  508. #define __sym_compat(sym,impl,verid) \
  509. __asm__(".symver impl, sym@verid")
  510. #define __sym_default(impl,sym,verid) \
  511. __asm__(".symver impl, sym@@verid")
  512. #endif /* __STDC__ */
  513. #else /* !__ELF__ */
  514. #ifdef __STDC__
  515. #define __weak_reference(sym,alias) \
  516. __asm__(".stabs \"_" #alias "\",11,0,0,0"); \
  517. __asm__(".stabs \"_" #sym "\",1,0,0,0")
  518. #define __warn_references(sym,msg) \
  519. __asm__(".stabs \"" msg "\",30,0,0,0"); \
  520. __asm__(".stabs \"_" #sym "\",1,0,0,0")
  521. #else
  522. #define __weak_reference(sym,alias) \
  523. __asm__(".stabs \"_/**/alias\",11,0,0,0"); \
  524. __asm__(".stabs \"_/**/sym\",1,0,0,0")
  525. #define __warn_references(sym,msg) \
  526. __asm__(".stabs msg,30,0,0,0"); \
  527. __asm__(".stabs \"_/**/sym\",1,0,0,0")
  528. #endif /* __STDC__ */
  529. #endif /* __ELF__ */
  530. #endif /* __GNUC__ || __INTEL_COMPILER */
  531. #ifndef __FBSDID
  532. #define __FBSDID(s) struct __hack
  533. #endif
  534. #ifndef __RCSID
  535. #define __RCSID(s) struct __hack
  536. #endif
  537. #ifndef __RCSID_SOURCE
  538. #define __RCSID_SOURCE(s) struct __hack
  539. #endif
  540. #ifndef __SCCSID
  541. #define __SCCSID(s) struct __hack
  542. #endif
  543. #ifndef __COPYRIGHT
  544. #define __COPYRIGHT(s) struct __hack
  545. #endif
  546. #ifndef __DECONST
  547. #define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var))
  548. #endif
  549. #ifndef __DEVOLATILE
  550. #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
  551. #endif
  552. #ifndef __DEQUALIFY
  553. #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
  554. #endif
  555. /*
  556. * Nullability qualifiers: currently only supported by Clang.
  557. */
  558. #if !(defined(__clang__) && __has_feature(nullability))
  559. #define _Nonnull
  560. #define _Nullable
  561. #define _Null_unspecified
  562. #define __NULLABILITY_PRAGMA_PUSH
  563. #define __NULLABILITY_PRAGMA_POP
  564. #else
  565. #define __NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push") \
  566. _Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
  567. #define __NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
  568. #endif
  569. /*
  570. * Type Safety Checking
  571. *
  572. * Clang provides additional attributes to enable checking type safety
  573. * properties that cannot be enforced by the C type system.
  574. */
  575. #if __has_attribute(__argument_with_type_tag__) && \
  576. __has_attribute(__type_tag_for_datatype__)
  577. #define __arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
  578. __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
  579. #define __datatype_type_tag(kind, type) \
  580. __attribute__((__type_tag_for_datatype__(kind, type)))
  581. #else
  582. #define __arg_type_tag(arg_kind, arg_idx, type_tag_idx)
  583. #define __datatype_type_tag(kind, type)
  584. #endif
  585. /*
  586. * Lock annotations.
  587. *
  588. * Clang provides support for doing basic thread-safety tests at
  589. * compile-time, by marking which locks will/should be held when
  590. * entering/leaving a functions.
  591. *
  592. * Furthermore, it is also possible to annotate variables and structure
  593. * members to enforce that they are only accessed when certain locks are
  594. * held.
  595. */
  596. #if __has_extension(c_thread_safety_attributes)
  597. #define __lock_annotate(x) __attribute__((x))
  598. #else
  599. #define __lock_annotate(x)
  600. #endif
  601. /* Structure implements a lock. */
  602. /* FIXME: Use __lockable__, etc. to avoid colliding with user namespace macros,
  603. * once clang is fixed: https://bugs.llvm.org/show_bug.cgi?id=34319 */
  604. #define __lockable __lock_annotate(lockable)
  605. /* Function acquires an exclusive or shared lock. */
  606. #define __locks_exclusive(...) \
  607. __lock_annotate(exclusive_lock_function(__VA_ARGS__))
  608. #define __locks_shared(...) \
  609. __lock_annotate(shared_lock_function(__VA_ARGS__))
  610. /* Function attempts to acquire an exclusive or shared lock. */
  611. #define __trylocks_exclusive(...) \
  612. __lock_annotate(exclusive_trylock_function(__VA_ARGS__))
  613. #define __trylocks_shared(...) \
  614. __lock_annotate(shared_trylock_function(__VA_ARGS__))
  615. /* Function releases a lock. */
  616. #define __unlocks(...) __lock_annotate(unlock_function(__VA_ARGS__))
  617. /* Function asserts that an exclusive or shared lock is held. */
  618. #define __asserts_exclusive(...) \
  619. __lock_annotate(assert_exclusive_lock(__VA_ARGS__))
  620. #define __asserts_shared(...) \
  621. __lock_annotate(assert_shared_lock(__VA_ARGS__))
  622. /* Function requires that an exclusive or shared lock is or is not held. */
  623. #define __requires_exclusive(...) \
  624. __lock_annotate(exclusive_locks_required(__VA_ARGS__))
  625. #define __requires_shared(...) \
  626. __lock_annotate(shared_locks_required(__VA_ARGS__))
  627. #define __requires_unlocked(...) \
  628. __lock_annotate(locks_excluded(__VA_ARGS__))
  629. /* Function should not be analyzed. */
  630. #define __no_lock_analysis __lock_annotate(no_thread_safety_analysis)
  631. /* Guard variables and structure members by lock. */
  632. #define __guarded_by(x) __lock_annotate(guarded_by(x))
  633. #define __pt_guarded_by(x) __lock_annotate(pt_guarded_by(x))
  634. #endif /* !_SYS_CDEFS_H_ */