ansidecl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Compiler compatibility macros
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* For ease of writing code which uses GCC extensions but needs to be
  16. portable to other compilers, we provide the GCC_VERSION macro that
  17. simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
  18. wrappers around __attribute__. Also, __extension__ will be #defined
  19. to nothing if it doesn't work. See below. */
  20. #ifndef _ANSIDECL_H
  21. #define _ANSIDECL_H 1
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Every source file includes this file,
  26. so they will all get the switch for lint. */
  27. /* LINTLIBRARY */
  28. /* Using MACRO(x,y) in cpp #if conditionals does not work with some
  29. older preprocessors. Thus we can't define something like this:
  30. #define HAVE_GCC_VERSION(MAJOR, MINOR) \
  31. (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
  32. and then test "#if HAVE_GCC_VERSION(2,7)".
  33. So instead we use the macro below and test it against specific values. */
  34. /* This macro simplifies testing whether we are using gcc, and if it
  35. is of a particular minimum version. (Both major & minor numbers are
  36. significant.) This macro will evaluate to 0 if we are not using
  37. gcc at all. */
  38. #ifndef GCC_VERSION
  39. #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  40. #endif /* GCC_VERSION */
  41. /* inline requires special treatment; it's in C99, and GCC >=2.7 supports
  42. it too, but it's not in C89. */
  43. #undef inline
  44. #if (!defined(__cplusplus) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) || (defined(__SUNPRO_C) && defined(__C99FEATURES__))
  45. /* it's a keyword */
  46. #else
  47. # if GCC_VERSION >= 2007
  48. # define inline __inline__ /* __inline__ prevents -pedantic warnings */
  49. # else
  50. # define inline /* nothing */
  51. # endif
  52. #endif
  53. /* Define macros for some gcc attributes. This permits us to use the
  54. macros freely, and know that they will come into play for the
  55. version of gcc in which they are supported. */
  56. #if (GCC_VERSION < 2007)
  57. # define __attribute__(x)
  58. #endif
  59. /* Attribute __malloc__ on functions was valid as of gcc 2.96. */
  60. #ifndef ATTRIBUTE_MALLOC
  61. # if (GCC_VERSION >= 2096)
  62. # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
  63. # else
  64. # define ATTRIBUTE_MALLOC
  65. # endif /* GNUC >= 2.96 */
  66. #endif /* ATTRIBUTE_MALLOC */
  67. /* Attributes on labels were valid as of gcc 2.93 and g++ 4.5. For
  68. g++ an attribute on a label must be followed by a semicolon. */
  69. #ifndef ATTRIBUTE_UNUSED_LABEL
  70. # ifndef __cplusplus
  71. # if GCC_VERSION >= 2093
  72. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
  73. # else
  74. # define ATTRIBUTE_UNUSED_LABEL
  75. # endif
  76. # else
  77. # if GCC_VERSION >= 4005
  78. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED ;
  79. # else
  80. # define ATTRIBUTE_UNUSED_LABEL
  81. # endif
  82. # endif
  83. #endif
  84. /* Similarly to ARG_UNUSED below. Prior to GCC 3.4, the C++ frontend
  85. couldn't parse attributes placed after the identifier name, and now
  86. the entire compiler is built with C++. */
  87. #ifndef ATTRIBUTE_UNUSED
  88. #if GCC_VERSION >= 3004
  89. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  90. #else
  91. #define ATTRIBUTE_UNUSED
  92. #endif
  93. #endif /* ATTRIBUTE_UNUSED */
  94. /* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the
  95. identifier name. */
  96. #if ! defined(__cplusplus) || (GCC_VERSION >= 3004)
  97. # define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED
  98. #else /* !__cplusplus || GNUC >= 3.4 */
  99. # define ARG_UNUSED(NAME) NAME
  100. #endif /* !__cplusplus || GNUC >= 3.4 */
  101. #ifndef ATTRIBUTE_NORETURN
  102. #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  103. #endif /* ATTRIBUTE_NORETURN */
  104. /* Attribute `nonnull' was valid as of gcc 3.3. */
  105. #ifndef ATTRIBUTE_NONNULL
  106. # if (GCC_VERSION >= 3003)
  107. # define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
  108. # else
  109. # define ATTRIBUTE_NONNULL(m)
  110. # endif /* GNUC >= 3.3 */
  111. #endif /* ATTRIBUTE_NONNULL */
  112. /* Attribute `returns_nonnull' was valid as of gcc 4.9. */
  113. #ifndef ATTRIBUTE_RETURNS_NONNULL
  114. # if (GCC_VERSION >= 4009)
  115. # define ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__))
  116. # else
  117. # define ATTRIBUTE_RETURNS_NONNULL
  118. # endif /* GNUC >= 4.9 */
  119. #endif /* ATTRIBUTE_RETURNS_NONNULL */
  120. /* Attribute `pure' was valid as of gcc 3.0. */
  121. #ifndef ATTRIBUTE_PURE
  122. # if (GCC_VERSION >= 3000)
  123. # define ATTRIBUTE_PURE __attribute__ ((__pure__))
  124. # else
  125. # define ATTRIBUTE_PURE
  126. # endif /* GNUC >= 3.0 */
  127. #endif /* ATTRIBUTE_PURE */
  128. /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
  129. This was the case for the `printf' format attribute by itself
  130. before GCC 3.3, but as of 3.3 we need to add the `nonnull'
  131. attribute to retain this behavior. */
  132. #ifndef ATTRIBUTE_PRINTF
  133. #define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (gnu_printf, m, n))) ATTRIBUTE_NONNULL(m)
  134. #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
  135. #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
  136. #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
  137. #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
  138. #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
  139. #endif /* ATTRIBUTE_PRINTF */
  140. /* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on
  141. a function pointer. Format attributes were allowed on function
  142. pointers as of gcc 3.1. */
  143. #ifndef ATTRIBUTE_FPTR_PRINTF
  144. # if (GCC_VERSION >= 3001)
  145. # define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n)
  146. # else
  147. # define ATTRIBUTE_FPTR_PRINTF(m, n)
  148. # endif /* GNUC >= 3.1 */
  149. # define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2)
  150. # define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3)
  151. # define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4)
  152. # define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5)
  153. # define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6)
  154. #endif /* ATTRIBUTE_FPTR_PRINTF */
  155. /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A
  156. NULL format specifier was allowed as of gcc 3.3. */
  157. #ifndef ATTRIBUTE_NULL_PRINTF
  158. # if (GCC_VERSION >= 3003)
  159. # define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (gnu_printf, m, n)))
  160. # else
  161. # define ATTRIBUTE_NULL_PRINTF(m, n)
  162. # endif /* GNUC >= 3.3 */
  163. # define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
  164. # define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
  165. # define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
  166. # define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
  167. # define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
  168. #endif /* ATTRIBUTE_NULL_PRINTF */
  169. /* Attribute `sentinel' was valid as of gcc 3.5. */
  170. #ifndef ATTRIBUTE_SENTINEL
  171. # if (GCC_VERSION >= 3005)
  172. # define ATTRIBUTE_SENTINEL __attribute__ ((__sentinel__))
  173. # else
  174. # define ATTRIBUTE_SENTINEL
  175. # endif /* GNUC >= 3.5 */
  176. #endif /* ATTRIBUTE_SENTINEL */
  177. #ifndef ATTRIBUTE_ALIGNED_ALIGNOF
  178. # if (GCC_VERSION >= 3000)
  179. # define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__ ((__aligned__ (__alignof__ (m))))
  180. # else
  181. # define ATTRIBUTE_ALIGNED_ALIGNOF(m)
  182. # endif /* GNUC >= 3.0 */
  183. #endif /* ATTRIBUTE_ALIGNED_ALIGNOF */
  184. /* Useful for structures whose layout must match some binary specification
  185. regardless of the alignment and padding qualities of the compiler. */
  186. #ifndef ATTRIBUTE_PACKED
  187. # define ATTRIBUTE_PACKED __attribute__ ((packed))
  188. #endif
  189. /* Attribute `hot' and `cold' was valid as of gcc 4.3. */
  190. #ifndef ATTRIBUTE_COLD
  191. # if (GCC_VERSION >= 4003)
  192. # define ATTRIBUTE_COLD __attribute__ ((__cold__))
  193. # else
  194. # define ATTRIBUTE_COLD
  195. # endif /* GNUC >= 4.3 */
  196. #endif /* ATTRIBUTE_COLD */
  197. #ifndef ATTRIBUTE_HOT
  198. # if (GCC_VERSION >= 4003)
  199. # define ATTRIBUTE_HOT __attribute__ ((__hot__))
  200. # else
  201. # define ATTRIBUTE_HOT
  202. # endif /* GNUC >= 4.3 */
  203. #endif /* ATTRIBUTE_HOT */
  204. /* Attribute 'no_sanitize_undefined' was valid as of gcc 4.9. */
  205. #ifndef ATTRIBUTE_NO_SANITIZE_UNDEFINED
  206. # if (GCC_VERSION >= 4009)
  207. # define ATTRIBUTE_NO_SANITIZE_UNDEFINED __attribute__ ((no_sanitize_undefined))
  208. # else
  209. # define ATTRIBUTE_NO_SANITIZE_UNDEFINED
  210. # endif /* GNUC >= 4.9 */
  211. #endif /* ATTRIBUTE_NO_SANITIZE_UNDEFINED */
  212. /* Attribute 'nonstring' was valid as of gcc 8. */
  213. #ifndef ATTRIBUTE_NONSTRING
  214. # if GCC_VERSION >= 8000
  215. # define ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__))
  216. # else
  217. # define ATTRIBUTE_NONSTRING
  218. # endif
  219. #endif
  220. /* Attribute `alloc_size' was valid as of gcc 4.3. */
  221. #ifndef ATTRIBUTE_RESULT_SIZE_1
  222. # if (GCC_VERSION >= 4003)
  223. # define ATTRIBUTE_RESULT_SIZE_1 __attribute__ ((alloc_size (1)))
  224. # else
  225. # define ATTRIBUTE_RESULT_SIZE_1
  226. #endif
  227. #endif
  228. #ifndef ATTRIBUTE_RESULT_SIZE_2
  229. # if (GCC_VERSION >= 4003)
  230. # define ATTRIBUTE_RESULT_SIZE_2 __attribute__ ((alloc_size (2)))
  231. # else
  232. # define ATTRIBUTE_RESULT_SIZE_2
  233. #endif
  234. #endif
  235. #ifndef ATTRIBUTE_RESULT_SIZE_1_2
  236. # if (GCC_VERSION >= 4003)
  237. # define ATTRIBUTE_RESULT_SIZE_1_2 __attribute__ ((alloc_size (1, 2)))
  238. # else
  239. # define ATTRIBUTE_RESULT_SIZE_1_2
  240. #endif
  241. #endif
  242. /* Attribute `warn_unused_result' was valid as of gcc 3.3. */
  243. #ifndef ATTRIBUTE_WARN_UNUSED_RESULT
  244. # if GCC_VERSION >= 3003
  245. # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
  246. # else
  247. # define ATTRIBUTE_WARN_UNUSED_RESULT
  248. # endif
  249. #endif
  250. /* We use __extension__ in some places to suppress -pedantic warnings
  251. about GCC extensions. This feature didn't work properly before
  252. gcc 2.8. */
  253. #if GCC_VERSION < 2008
  254. #define __extension__
  255. #endif
  256. /* This is used to declare a const variable which should be visible
  257. outside of the current compilation unit. Use it as
  258. EXPORTED_CONST int i = 1;
  259. This is because the semantics of const are different in C and C++.
  260. "extern const" is permitted in C but it looks strange, and gcc
  261. warns about it when -Wc++-compat is not used. */
  262. #ifdef __cplusplus
  263. #define EXPORTED_CONST extern const
  264. #else
  265. #define EXPORTED_CONST const
  266. #endif
  267. /* Be conservative and only use enum bitfields with C++ or GCC.
  268. FIXME: provide a complete autoconf test for buggy enum bitfields. */
  269. #ifdef __cplusplus
  270. #define ENUM_BITFIELD(TYPE) enum TYPE
  271. #elif (GCC_VERSION > 2000)
  272. #define ENUM_BITFIELD(TYPE) __extension__ enum TYPE
  273. #else
  274. #define ENUM_BITFIELD(TYPE) unsigned int
  275. #endif
  276. #if defined(__cplusplus) && __cpp_constexpr >= 200704
  277. #define CONSTEXPR constexpr
  278. #else
  279. #define CONSTEXPR
  280. #endif
  281. /* A macro to disable the copy constructor and assignment operator.
  282. When building with C++11 and above, the methods are explicitly
  283. deleted, causing a compile-time error if something tries to copy.
  284. For C++03, this just declares the methods, causing a link-time
  285. error if the methods end up called (assuming you don't
  286. define them). For C++03, for best results, place the macro
  287. under the private: access specifier, like this,
  288. class name_lookup
  289. {
  290. private:
  291. DISABLE_COPY_AND_ASSIGN (name_lookup);
  292. };
  293. so that most attempts at copy are caught at compile-time. */
  294. #if defined(__cplusplus) && __cplusplus >= 201103
  295. #define DISABLE_COPY_AND_ASSIGN(TYPE) \
  296. TYPE (const TYPE&) = delete; \
  297. void operator= (const TYPE &) = delete
  298. #else
  299. #define DISABLE_COPY_AND_ASSIGN(TYPE) \
  300. TYPE (const TYPE&); \
  301. void operator= (const TYPE &)
  302. #endif /* __cplusplus >= 201103 */
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #endif /* ansidecl.h */