ansidecl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* ANSI and traditional C compatability 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. /* ANSI and traditional C compatibility macros
  16. ANSI C is assumed if __STDC__ is #defined.
  17. Macro ANSI C definition Traditional C definition
  18. ----- ---- - ---------- ----------- - ----------
  19. PTR `void *' `char *'
  20. const not defined `'
  21. volatile not defined `'
  22. signed not defined `'
  23. For ease of writing code which uses GCC extensions but needs to be
  24. portable to other compilers, we provide the GCC_VERSION macro that
  25. simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
  26. wrappers around __attribute__. Also, __extension__ will be #defined
  27. to nothing if it doesn't work. See below. */
  28. #ifndef _ANSIDECL_H
  29. #define _ANSIDECL_H 1
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Every source file includes this file,
  34. so they will all get the switch for lint. */
  35. /* LINTLIBRARY */
  36. /* Using MACRO(x,y) in cpp #if conditionals does not work with some
  37. older preprocessors. Thus we can't define something like this:
  38. #define HAVE_GCC_VERSION(MAJOR, MINOR) \
  39. (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
  40. and then test "#if HAVE_GCC_VERSION(2,7)".
  41. So instead we use the macro below and test it against specific values. */
  42. /* This macro simplifies testing whether we are using gcc, and if it
  43. is of a particular minimum version. (Both major & minor numbers are
  44. significant.) This macro will evaluate to 0 if we are not using
  45. gcc at all. */
  46. #ifndef GCC_VERSION
  47. #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  48. #endif /* GCC_VERSION */
  49. #if defined (__STDC__) || defined(__cplusplus) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32)
  50. /* All known AIX compilers implement these things (but don't always
  51. define __STDC__). The RISC/OS MIPS compiler defines these things
  52. in SVR4 mode, but does not define __STDC__. */
  53. /* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
  54. C++ compilers, does not define __STDC__, though it acts as if this
  55. was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
  56. #define PTR void *
  57. #undef const
  58. #undef volatile
  59. #undef signed
  60. /* inline requires special treatment; it's in C99, and GCC >=2.7 supports
  61. it too, but it's not in C89. */
  62. #undef inline
  63. #if (!defined(__cplusplus) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) || (defined(__SUNPRO_C) && defined(__C99FEATURES__))
  64. /* it's a keyword */
  65. #else
  66. # if GCC_VERSION >= 2007
  67. # define inline __inline__ /* __inline__ prevents -pedantic warnings */
  68. # else
  69. # define inline /* nothing */
  70. # endif
  71. #endif
  72. #else /* Not ANSI C. */
  73. #define PTR char *
  74. /* some systems define these in header files for non-ansi mode */
  75. #undef const
  76. #undef volatile
  77. #undef signed
  78. #undef inline
  79. #define const
  80. #define volatile
  81. #define signed
  82. #define inline
  83. #endif /* ANSI C. */
  84. /* Define macros for some gcc attributes. This permits us to use the
  85. macros freely, and know that they will come into play for the
  86. version of gcc in which they are supported. */
  87. #if (GCC_VERSION < 2007)
  88. # define __attribute__(x)
  89. #endif
  90. /* Attribute __malloc__ on functions was valid as of gcc 2.96. */
  91. #ifndef ATTRIBUTE_MALLOC
  92. # if (GCC_VERSION >= 2096)
  93. # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
  94. # else
  95. # define ATTRIBUTE_MALLOC
  96. # endif /* GNUC >= 2.96 */
  97. #endif /* ATTRIBUTE_MALLOC */
  98. /* Attributes on labels were valid as of gcc 2.93 and g++ 4.5. For
  99. g++ an attribute on a label must be followed by a semicolon. */
  100. #ifndef ATTRIBUTE_UNUSED_LABEL
  101. # ifndef __cplusplus
  102. # if GCC_VERSION >= 2093
  103. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
  104. # else
  105. # define ATTRIBUTE_UNUSED_LABEL
  106. # endif
  107. # else
  108. # if GCC_VERSION >= 4005
  109. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED ;
  110. # else
  111. # define ATTRIBUTE_UNUSED_LABEL
  112. # endif
  113. # endif
  114. #endif
  115. /* Similarly to ARG_UNUSED below. Prior to GCC 3.4, the C++ frontend
  116. couldn't parse attributes placed after the identifier name, and now
  117. the entire compiler is built with C++. */
  118. #ifndef ATTRIBUTE_UNUSED
  119. #if GCC_VERSION >= 3004
  120. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  121. #else
  122. #define ATTRIBUTE_UNUSED
  123. #endif
  124. #endif /* ATTRIBUTE_UNUSED */
  125. /* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the
  126. identifier name. */
  127. #if ! defined(__cplusplus) || (GCC_VERSION >= 3004)
  128. # define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED
  129. #else /* !__cplusplus || GNUC >= 3.4 */
  130. # define ARG_UNUSED(NAME) NAME
  131. #endif /* !__cplusplus || GNUC >= 3.4 */
  132. #ifndef ATTRIBUTE_NORETURN
  133. #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  134. #endif /* ATTRIBUTE_NORETURN */
  135. /* Attribute `nonnull' was valid as of gcc 3.3. */
  136. #ifndef ATTRIBUTE_NONNULL
  137. # if (GCC_VERSION >= 3003)
  138. # define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
  139. # else
  140. # define ATTRIBUTE_NONNULL(m)
  141. # endif /* GNUC >= 3.3 */
  142. #endif /* ATTRIBUTE_NONNULL */
  143. /* Attribute `returns_nonnull' was valid as of gcc 4.9. */
  144. #ifndef ATTRIBUTE_RETURNS_NONNULL
  145. # if (GCC_VERSION >= 4009)
  146. # define ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__))
  147. # else
  148. # define ATTRIBUTE_RETURNS_NONNULL
  149. # endif /* GNUC >= 4.9 */
  150. #endif /* ATTRIBUTE_RETURNS_NONNULL */
  151. /* Attribute `pure' was valid as of gcc 3.0. */
  152. #ifndef ATTRIBUTE_PURE
  153. # if (GCC_VERSION >= 3000)
  154. # define ATTRIBUTE_PURE __attribute__ ((__pure__))
  155. # else
  156. # define ATTRIBUTE_PURE
  157. # endif /* GNUC >= 3.0 */
  158. #endif /* ATTRIBUTE_PURE */
  159. /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
  160. This was the case for the `printf' format attribute by itself
  161. before GCC 3.3, but as of 3.3 we need to add the `nonnull'
  162. attribute to retain this behavior. */
  163. #ifndef ATTRIBUTE_PRINTF
  164. #define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m)
  165. #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
  166. #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
  167. #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
  168. #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
  169. #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
  170. #endif /* ATTRIBUTE_PRINTF */
  171. /* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on
  172. a function pointer. Format attributes were allowed on function
  173. pointers as of gcc 3.1. */
  174. #ifndef ATTRIBUTE_FPTR_PRINTF
  175. # if (GCC_VERSION >= 3001)
  176. # define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n)
  177. # else
  178. # define ATTRIBUTE_FPTR_PRINTF(m, n)
  179. # endif /* GNUC >= 3.1 */
  180. # define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2)
  181. # define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3)
  182. # define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4)
  183. # define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5)
  184. # define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6)
  185. #endif /* ATTRIBUTE_FPTR_PRINTF */
  186. /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A
  187. NULL format specifier was allowed as of gcc 3.3. */
  188. #ifndef ATTRIBUTE_NULL_PRINTF
  189. # if (GCC_VERSION >= 3003)
  190. # define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
  191. # else
  192. # define ATTRIBUTE_NULL_PRINTF(m, n)
  193. # endif /* GNUC >= 3.3 */
  194. # define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
  195. # define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
  196. # define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
  197. # define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
  198. # define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
  199. #endif /* ATTRIBUTE_NULL_PRINTF */
  200. /* Attribute `sentinel' was valid as of gcc 3.5. */
  201. #ifndef ATTRIBUTE_SENTINEL
  202. # if (GCC_VERSION >= 3005)
  203. # define ATTRIBUTE_SENTINEL __attribute__ ((__sentinel__))
  204. # else
  205. # define ATTRIBUTE_SENTINEL
  206. # endif /* GNUC >= 3.5 */
  207. #endif /* ATTRIBUTE_SENTINEL */
  208. #ifndef ATTRIBUTE_ALIGNED_ALIGNOF
  209. # if (GCC_VERSION >= 3000)
  210. # define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__ ((__aligned__ (__alignof__ (m))))
  211. # else
  212. # define ATTRIBUTE_ALIGNED_ALIGNOF(m)
  213. # endif /* GNUC >= 3.0 */
  214. #endif /* ATTRIBUTE_ALIGNED_ALIGNOF */
  215. /* Useful for structures whose layout must match some binary specification
  216. regardless of the alignment and padding qualities of the compiler. */
  217. #ifndef ATTRIBUTE_PACKED
  218. # define ATTRIBUTE_PACKED __attribute__ ((packed))
  219. #endif
  220. /* Attribute `hot' and `cold' was valid as of gcc 4.3. */
  221. #ifndef ATTRIBUTE_COLD
  222. # if (GCC_VERSION >= 4003)
  223. # define ATTRIBUTE_COLD __attribute__ ((__cold__))
  224. # else
  225. # define ATTRIBUTE_COLD
  226. # endif /* GNUC >= 4.3 */
  227. #endif /* ATTRIBUTE_COLD */
  228. #ifndef ATTRIBUTE_HOT
  229. # if (GCC_VERSION >= 4003)
  230. # define ATTRIBUTE_HOT __attribute__ ((__hot__))
  231. # else
  232. # define ATTRIBUTE_HOT
  233. # endif /* GNUC >= 4.3 */
  234. #endif /* ATTRIBUTE_HOT */
  235. /* Attribute 'no_sanitize_undefined' was valid as of gcc 4.9. */
  236. #ifndef ATTRIBUTE_NO_SANITIZE_UNDEFINED
  237. # if (GCC_VERSION >= 4009)
  238. # define ATTRIBUTE_NO_SANITIZE_UNDEFINED __attribute__ ((no_sanitize_undefined))
  239. # else
  240. # define ATTRIBUTE_NO_SANITIZE_UNDEFINED
  241. # endif /* GNUC >= 4.9 */
  242. #endif /* ATTRIBUTE_NO_SANITIZE_UNDEFINED */
  243. /* Attribute 'nonstring' was valid as of gcc 8. */
  244. #ifndef ATTRIBUTE_NONSTRING
  245. # if GCC_VERSION >= 8000
  246. # define ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__))
  247. # else
  248. # define ATTRIBUTE_NONSTRING
  249. # endif
  250. #endif
  251. /* Attribute `alloc_size' was valid as of gcc 4.3. */
  252. #ifndef ATTRIBUTE_RESULT_SIZE_1
  253. # if (GCC_VERSION >= 4003)
  254. # define ATTRIBUTE_RESULT_SIZE_1 __attribute__ ((alloc_size (1)))
  255. # else
  256. # define ATTRIBUTE_RESULT_SIZE_1
  257. #endif
  258. #endif
  259. #ifndef ATTRIBUTE_RESULT_SIZE_2
  260. # if (GCC_VERSION >= 4003)
  261. # define ATTRIBUTE_RESULT_SIZE_2 __attribute__ ((alloc_size (2)))
  262. # else
  263. # define ATTRIBUTE_RESULT_SIZE_2
  264. #endif
  265. #endif
  266. #ifndef ATTRIBUTE_RESULT_SIZE_1_2
  267. # if (GCC_VERSION >= 4003)
  268. # define ATTRIBUTE_RESULT_SIZE_1_2 __attribute__ ((alloc_size (1, 2)))
  269. # else
  270. # define ATTRIBUTE_RESULT_SIZE_1_2
  271. #endif
  272. #endif
  273. /* Attribute `warn_unused_result' was valid as of gcc 3.3. */
  274. #ifndef ATTRIBUTE_WARN_UNUSED_RESULT
  275. # if GCC_VERSION >= 3003
  276. # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
  277. # else
  278. # define ATTRIBUTE_WARN_UNUSED_RESULT
  279. # endif
  280. #endif
  281. /* We use __extension__ in some places to suppress -pedantic warnings
  282. about GCC extensions. This feature didn't work properly before
  283. gcc 2.8. */
  284. #if GCC_VERSION < 2008
  285. #define __extension__
  286. #endif
  287. /* This is used to declare a const variable which should be visible
  288. outside of the current compilation unit. Use it as
  289. EXPORTED_CONST int i = 1;
  290. This is because the semantics of const are different in C and C++.
  291. "extern const" is permitted in C but it looks strange, and gcc
  292. warns about it when -Wc++-compat is not used. */
  293. #ifdef __cplusplus
  294. #define EXPORTED_CONST extern const
  295. #else
  296. #define EXPORTED_CONST const
  297. #endif
  298. /* Be conservative and only use enum bitfields with C++ or GCC.
  299. FIXME: provide a complete autoconf test for buggy enum bitfields. */
  300. #ifdef __cplusplus
  301. #define ENUM_BITFIELD(TYPE) enum TYPE
  302. #elif (GCC_VERSION > 2000)
  303. #define ENUM_BITFIELD(TYPE) __extension__ enum TYPE
  304. #else
  305. #define ENUM_BITFIELD(TYPE) unsigned int
  306. #endif
  307. #if defined(__cplusplus) && __cpp_constexpr >= 200704
  308. #define CONSTEXPR constexpr
  309. #else
  310. #define CONSTEXPR
  311. #endif
  312. /* C++11 adds the ability to add "override" after an implementation of a
  313. virtual function in a subclass, to:
  314. (A) document that this is an override of a virtual function
  315. (B) allow the compiler to issue a warning if it isn't (e.g. a mismatch
  316. of the type signature).
  317. Similarly, it allows us to add a "final" to indicate that no subclass
  318. may subsequently override the vfunc.
  319. Provide OVERRIDE and FINAL as macros, allowing us to get these benefits
  320. when compiling with C++11 support, but without requiring C++11.
  321. For gcc, use "-std=c++11" to enable C++11 support; gcc 6 onwards enables
  322. this by default (actually GNU++14). */
  323. #if defined __cplusplus
  324. # if __cplusplus >= 201103
  325. /* C++11 claims to be available: use it. Final/override were only
  326. implemented in 4.7, though. */
  327. # if GCC_VERSION < 4007
  328. # define OVERRIDE
  329. # define FINAL
  330. # else
  331. # define OVERRIDE override
  332. # define FINAL final
  333. # endif
  334. # elif GCC_VERSION >= 4007
  335. /* G++ 4.7 supports __final in C++98. */
  336. # define OVERRIDE
  337. # define FINAL __final
  338. # else
  339. /* No C++11 support; leave the macros empty. */
  340. # define OVERRIDE
  341. # define FINAL
  342. # endif
  343. #else
  344. /* No C++11 support; leave the macros empty. */
  345. # define OVERRIDE
  346. # define FINAL
  347. #endif
  348. /* A macro to disable the copy constructor and assignment operator.
  349. When building with C++11 and above, the methods are explicitly
  350. deleted, causing a compile-time error if something tries to copy.
  351. For C++03, this just declares the methods, causing a link-time
  352. error if the methods end up called (assuming you don't
  353. define them). For C++03, for best results, place the macro
  354. under the private: access specifier, like this,
  355. class name_lookup
  356. {
  357. private:
  358. DISABLE_COPY_AND_ASSIGN (name_lookup);
  359. };
  360. so that most attempts at copy are caught at compile-time. */
  361. #if defined(__cplusplus) && __cplusplus >= 201103
  362. #define DISABLE_COPY_AND_ASSIGN(TYPE) \
  363. TYPE (const TYPE&) = delete; \
  364. void operator= (const TYPE &) = delete
  365. #else
  366. #define DISABLE_COPY_AND_ASSIGN(TYPE) \
  367. TYPE (const TYPE&); \
  368. void operator= (const TYPE &)
  369. #endif /* __cplusplus >= 201103 */
  370. #ifdef __cplusplus
  371. }
  372. #endif
  373. #endif /* ansidecl.h */