ansidecl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* ANSI and traditional C compatability macros
  2. Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  3. 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2013
  4. Free Software Foundation, Inc.
  5. This file is part of the GNU C Library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program 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 General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /* ANSI and traditional C compatibility macros
  18. ANSI C is assumed if __STDC__ is #defined.
  19. Macro ANSI C definition Traditional C definition
  20. ----- ---- - ---------- ----------- - ----------
  21. PTR `void *' `char *'
  22. const not defined `'
  23. volatile not defined `'
  24. signed not defined `'
  25. For ease of writing code which uses GCC extensions but needs to be
  26. portable to other compilers, we provide the GCC_VERSION macro that
  27. simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
  28. wrappers around __attribute__. Also, __extension__ will be #defined
  29. to nothing if it doesn't work. See below. */
  30. #ifndef _ANSIDECL_H
  31. #define _ANSIDECL_H 1
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* Every source file includes this file,
  36. so they will all get the switch for lint. */
  37. /* LINTLIBRARY */
  38. /* Using MACRO(x,y) in cpp #if conditionals does not work with some
  39. older preprocessors. Thus we can't define something like this:
  40. #define HAVE_GCC_VERSION(MAJOR, MINOR) \
  41. (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
  42. and then test "#if HAVE_GCC_VERSION(2,7)".
  43. So instead we use the macro below and test it against specific values. */
  44. /* This macro simplifies testing whether we are using gcc, and if it
  45. is of a particular minimum version. (Both major & minor numbers are
  46. significant.) This macro will evaluate to 0 if we are not using
  47. gcc at all. */
  48. #ifndef GCC_VERSION
  49. #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  50. #endif /* GCC_VERSION */
  51. #if defined (__STDC__) || defined(__cplusplus) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32)
  52. /* All known AIX compilers implement these things (but don't always
  53. define __STDC__). The RISC/OS MIPS compiler defines these things
  54. in SVR4 mode, but does not define __STDC__. */
  55. /* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
  56. C++ compilers, does not define __STDC__, though it acts as if this
  57. was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
  58. #define PTR void *
  59. #undef const
  60. #undef volatile
  61. #undef signed
  62. /* inline requires special treatment; it's in C99, and GCC >=2.7 supports
  63. it too, but it's not in C89. */
  64. #undef inline
  65. #if __STDC_VERSION__ >= 199901L || defined(__cplusplus) || (defined(__SUNPRO_C) && defined(__C99FEATURES__))
  66. /* it's a keyword */
  67. #else
  68. # if GCC_VERSION >= 2007
  69. # define inline __inline__ /* __inline__ prevents -pedantic warnings */
  70. # else
  71. # define inline /* nothing */
  72. # endif
  73. #endif
  74. #else /* Not ANSI C. */
  75. #define PTR char *
  76. /* some systems define these in header files for non-ansi mode */
  77. #undef const
  78. #undef volatile
  79. #undef signed
  80. #undef inline
  81. #define const
  82. #define volatile
  83. #define signed
  84. #define inline
  85. #endif /* ANSI C. */
  86. /* Define macros for some gcc attributes. This permits us to use the
  87. macros freely, and know that they will come into play for the
  88. version of gcc in which they are supported. */
  89. #if (GCC_VERSION < 2007)
  90. # define __attribute__(x)
  91. #endif
  92. /* Attribute __malloc__ on functions was valid as of gcc 2.96. */
  93. #ifndef ATTRIBUTE_MALLOC
  94. # if (GCC_VERSION >= 2096)
  95. # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
  96. # else
  97. # define ATTRIBUTE_MALLOC
  98. # endif /* GNUC >= 2.96 */
  99. #endif /* ATTRIBUTE_MALLOC */
  100. /* Attributes on labels were valid as of gcc 2.93 and g++ 4.5. For
  101. g++ an attribute on a label must be followed by a semicolon. */
  102. #ifndef ATTRIBUTE_UNUSED_LABEL
  103. # ifndef __cplusplus
  104. # if GCC_VERSION >= 2093
  105. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
  106. # else
  107. # define ATTRIBUTE_UNUSED_LABEL
  108. # endif
  109. # else
  110. # if GCC_VERSION >= 4005
  111. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED ;
  112. # else
  113. # define ATTRIBUTE_UNUSED_LABEL
  114. # endif
  115. # endif
  116. #endif
  117. /* Similarly to ARG_UNUSED below. Prior to GCC 3.4, the C++ frontend
  118. couldn't parse attributes placed after the identifier name, and now
  119. the entire compiler is built with C++. */
  120. #ifndef ATTRIBUTE_UNUSED
  121. #if GCC_VERSION >= 3004
  122. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  123. #else
  124. #define ATTRIBUTE_UNUSED
  125. #endif
  126. #endif /* ATTRIBUTE_UNUSED */
  127. /* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the
  128. identifier name. */
  129. #if ! defined(__cplusplus) || (GCC_VERSION >= 3004)
  130. # define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED
  131. #else /* !__cplusplus || GNUC >= 3.4 */
  132. # define ARG_UNUSED(NAME) NAME
  133. #endif /* !__cplusplus || GNUC >= 3.4 */
  134. #ifndef ATTRIBUTE_NORETURN
  135. #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  136. #endif /* ATTRIBUTE_NORETURN */
  137. /* Attribute `nonnull' was valid as of gcc 3.3. */
  138. #ifndef ATTRIBUTE_NONNULL
  139. # if (GCC_VERSION >= 3003)
  140. # define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
  141. # else
  142. # define ATTRIBUTE_NONNULL(m)
  143. # endif /* GNUC >= 3.3 */
  144. #endif /* ATTRIBUTE_NONNULL */
  145. /* Attribute `returns_nonnull' was valid as of gcc 4.9. */
  146. #ifndef ATTRIBUTE_RETURNS_NONNULL
  147. # if (GCC_VERSION >= 4009)
  148. # define ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__))
  149. # else
  150. # define ATTRIBUTE_RETURNS_NONNULL
  151. # endif /* GNUC >= 4.9 */
  152. #endif /* ATTRIBUTE_RETURNS_NONNULL */
  153. /* Attribute `pure' was valid as of gcc 3.0. */
  154. #ifndef ATTRIBUTE_PURE
  155. # if (GCC_VERSION >= 3000)
  156. # define ATTRIBUTE_PURE __attribute__ ((__pure__))
  157. # else
  158. # define ATTRIBUTE_PURE
  159. # endif /* GNUC >= 3.0 */
  160. #endif /* ATTRIBUTE_PURE */
  161. /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
  162. This was the case for the `printf' format attribute by itself
  163. before GCC 3.3, but as of 3.3 we need to add the `nonnull'
  164. attribute to retain this behavior. */
  165. #ifndef ATTRIBUTE_PRINTF
  166. #define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m)
  167. #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
  168. #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
  169. #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
  170. #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
  171. #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
  172. #endif /* ATTRIBUTE_PRINTF */
  173. /* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on
  174. a function pointer. Format attributes were allowed on function
  175. pointers as of gcc 3.1. */
  176. #ifndef ATTRIBUTE_FPTR_PRINTF
  177. # if (GCC_VERSION >= 3001)
  178. # define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n)
  179. # else
  180. # define ATTRIBUTE_FPTR_PRINTF(m, n)
  181. # endif /* GNUC >= 3.1 */
  182. # define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2)
  183. # define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3)
  184. # define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4)
  185. # define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5)
  186. # define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6)
  187. #endif /* ATTRIBUTE_FPTR_PRINTF */
  188. /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A
  189. NULL format specifier was allowed as of gcc 3.3. */
  190. #ifndef ATTRIBUTE_NULL_PRINTF
  191. # if (GCC_VERSION >= 3003)
  192. # define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
  193. # else
  194. # define ATTRIBUTE_NULL_PRINTF(m, n)
  195. # endif /* GNUC >= 3.3 */
  196. # define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
  197. # define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
  198. # define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
  199. # define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
  200. # define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
  201. #endif /* ATTRIBUTE_NULL_PRINTF */
  202. /* Attribute `sentinel' was valid as of gcc 3.5. */
  203. #ifndef ATTRIBUTE_SENTINEL
  204. # if (GCC_VERSION >= 3005)
  205. # define ATTRIBUTE_SENTINEL __attribute__ ((__sentinel__))
  206. # else
  207. # define ATTRIBUTE_SENTINEL
  208. # endif /* GNUC >= 3.5 */
  209. #endif /* ATTRIBUTE_SENTINEL */
  210. #ifndef ATTRIBUTE_ALIGNED_ALIGNOF
  211. # if (GCC_VERSION >= 3000)
  212. # define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__ ((__aligned__ (__alignof__ (m))))
  213. # else
  214. # define ATTRIBUTE_ALIGNED_ALIGNOF(m)
  215. # endif /* GNUC >= 3.0 */
  216. #endif /* ATTRIBUTE_ALIGNED_ALIGNOF */
  217. /* Useful for structures whose layout must much some binary specification
  218. regardless of the alignment and padding qualities of the compiler. */
  219. #ifndef ATTRIBUTE_PACKED
  220. # define ATTRIBUTE_PACKED __attribute__ ((packed))
  221. #endif
  222. /* Attribute `hot' and `cold' was valid as of gcc 4.3. */
  223. #ifndef ATTRIBUTE_COLD
  224. # if (GCC_VERSION >= 4003)
  225. # define ATTRIBUTE_COLD __attribute__ ((__cold__))
  226. # else
  227. # define ATTRIBUTE_COLD
  228. # endif /* GNUC >= 4.3 */
  229. #endif /* ATTRIBUTE_COLD */
  230. #ifndef ATTRIBUTE_HOT
  231. # if (GCC_VERSION >= 4003)
  232. # define ATTRIBUTE_HOT __attribute__ ((__hot__))
  233. # else
  234. # define ATTRIBUTE_HOT
  235. # endif /* GNUC >= 4.3 */
  236. #endif /* ATTRIBUTE_HOT */
  237. /* Attribute 'no_sanitize_undefined' was valid as of gcc 4.9. */
  238. #ifndef ATTRIBUTE_NO_SANITIZE_UNDEFINED
  239. # if (GCC_VERSION >= 4009)
  240. # define ATTRIBUTE_NO_SANITIZE_UNDEFINED __attribute__ ((no_sanitize_undefined))
  241. # else
  242. # define ATTRIBUTE_NO_SANITIZE_UNDEFINED
  243. # endif /* GNUC >= 4.9 */
  244. #endif /* ATTRIBUTE_NO_SANITIZE_UNDEFINED */
  245. /* We use __extension__ in some places to suppress -pedantic warnings
  246. about GCC extensions. This feature didn't work properly before
  247. gcc 2.8. */
  248. #if GCC_VERSION < 2008
  249. #define __extension__
  250. #endif
  251. /* This is used to declare a const variable which should be visible
  252. outside of the current compilation unit. Use it as
  253. EXPORTED_CONST int i = 1;
  254. This is because the semantics of const are different in C and C++.
  255. "extern const" is permitted in C but it looks strange, and gcc
  256. warns about it when -Wc++-compat is not used. */
  257. #ifdef __cplusplus
  258. #define EXPORTED_CONST extern const
  259. #else
  260. #define EXPORTED_CONST const
  261. #endif
  262. /* Be conservative and only use enum bitfields with C++ or GCC.
  263. FIXME: provide a complete autoconf test for buggy enum bitfields. */
  264. #ifdef __cplusplus
  265. #define ENUM_BITFIELD(TYPE) enum TYPE
  266. #elif (GCC_VERSION > 2000)
  267. #define ENUM_BITFIELD(TYPE) __extension__ enum TYPE
  268. #else
  269. #define ENUM_BITFIELD(TYPE) unsigned int
  270. #endif
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif /* ansidecl.h */