ansidecl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* ANSI and traditional C compatability macros
  2. Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. /* ANSI and traditional C compatibility macros
  17. ANSI C is assumed if __STDC__ is #defined.
  18. Macro ANSI C definition Traditional C definition
  19. ----- ---- - ---------- ----------- - ----------
  20. ANSI_PROTOTYPES 1 not defined
  21. PTR `void *' `char *'
  22. PTRCONST `void *const' `char *'
  23. LONG_DOUBLE `long double' `double'
  24. const not defined `'
  25. volatile not defined `'
  26. signed not defined `'
  27. VA_START(ap, var) va_start(ap, var) va_start(ap)
  28. Note that it is safe to write "void foo();" indicating a function
  29. with no return value, in all K+R compilers we have been able to test.
  30. For declaring functions with prototypes, we also provide these:
  31. PARAMS ((prototype))
  32. -- for functions which take a fixed number of arguments. Use this
  33. when declaring the function. When defining the function, write a
  34. K+R style argument list. For example:
  35. char *strcpy PARAMS ((char *dest, char *source));
  36. ...
  37. char *
  38. strcpy (dest, source)
  39. char *dest;
  40. char *source;
  41. { ... }
  42. VPARAMS ((prototype, ...))
  43. -- for functions which take a variable number of arguments. Use
  44. PARAMS to declare the function, VPARAMS to define it. For example:
  45. int printf PARAMS ((const char *format, ...));
  46. ...
  47. int
  48. printf VPARAMS ((const char *format, ...))
  49. {
  50. ...
  51. }
  52. For writing functions which take variable numbers of arguments, we
  53. also provide the VA_OPEN, VA_CLOSE, and VA_FIXEDARG macros. These
  54. hide the differences between K+R <varargs.h> and C89 <stdarg.h> more
  55. thoroughly than the simple VA_START() macro mentioned above.
  56. VA_OPEN and VA_CLOSE are used *instead of* va_start and va_end.
  57. Immediately after VA_OPEN, put a sequence of VA_FIXEDARG calls
  58. corresponding to the list of fixed arguments. Then use va_arg
  59. normally to get the variable arguments, or pass your va_list object
  60. around. You do not declare the va_list yourself; VA_OPEN does it
  61. for you.
  62. Here is a complete example:
  63. int
  64. printf VPARAMS ((const char *format, ...))
  65. {
  66. int result;
  67. VA_OPEN (ap, format);
  68. VA_FIXEDARG (ap, const char *, format);
  69. result = vfprintf (stdout, format, ap);
  70. VA_CLOSE (ap);
  71. return result;
  72. }
  73. You can declare variables either before or after the VA_OPEN,
  74. VA_FIXEDARG sequence. Also, VA_OPEN and VA_CLOSE are the beginning
  75. and end of a block. They must appear at the same nesting level,
  76. and any variables declared after VA_OPEN go out of scope at
  77. VA_CLOSE. Unfortunately, with a K+R compiler, that includes the
  78. argument list. You can have multiple instances of VA_OPEN/VA_CLOSE
  79. pairs in a single function in case you need to traverse the
  80. argument list more than once.
  81. For ease of writing code which uses GCC extensions but needs to be
  82. portable to other compilers, we provide the GCC_VERSION macro that
  83. simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
  84. wrappers around __attribute__. Also, __extension__ will be #defined
  85. to nothing if it doesn't work. See below.
  86. This header also defines a lot of obsolete macros:
  87. CONST, VOLATILE, SIGNED, PROTO, EXFUN, DEFUN, DEFUN_VOID,
  88. AND, DOTS, NOARGS. Don't use them. */
  89. #ifndef _ANSIDECL_H
  90. #define _ANSIDECL_H 1
  91. /* Every source file includes this file,
  92. so they will all get the switch for lint. */
  93. /* LINTLIBRARY */
  94. /* Using MACRO(x,y) in cpp #if conditionals does not work with some
  95. older preprocessors. Thus we can't define something like this:
  96. #define HAVE_GCC_VERSION(MAJOR, MINOR) \
  97. (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
  98. and then test "#if HAVE_GCC_VERSION(2,7)".
  99. So instead we use the macro below and test it against specific values. */
  100. /* This macro simplifies testing whether we are using gcc, and if it
  101. is of a particular minimum version. (Both major & minor numbers are
  102. significant.) This macro will evaluate to 0 if we are not using
  103. gcc at all. */
  104. #ifndef GCC_VERSION
  105. #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  106. #endif /* GCC_VERSION */
  107. #if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32) || (defined(__alpha) && defined(__cplusplus))
  108. /* All known AIX compilers implement these things (but don't always
  109. define __STDC__). The RISC/OS MIPS compiler defines these things
  110. in SVR4 mode, but does not define __STDC__. */
  111. /* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
  112. C++ compilers, does not define __STDC__, though it acts as if this
  113. was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
  114. #define ANSI_PROTOTYPES 1
  115. #define PTR void *
  116. #define PTRCONST void *const
  117. #define LONG_DOUBLE long double
  118. #define PARAMS(ARGS) ARGS
  119. #define VPARAMS(ARGS) ARGS
  120. #define VA_START(VA_LIST, VAR) va_start(VA_LIST, VAR)
  121. /* variadic function helper macros */
  122. /* "struct Qdmy" swallows the semicolon after VA_OPEN/VA_FIXEDARG's
  123. use without inhibiting further decls and without declaring an
  124. actual variable. */
  125. #define VA_OPEN(AP, VAR) { va_list AP; va_start(AP, VAR); { struct Qdmy
  126. #define VA_CLOSE(AP) } va_end(AP); }
  127. #define VA_FIXEDARG(AP, T, N) struct Qdmy
  128. #undef const
  129. #undef volatile
  130. #undef signed
  131. /* inline requires special treatment; it's in C99, and GCC >=2.7 supports
  132. it too, but it's not in C89. */
  133. #undef inline
  134. #if __STDC_VERSION__ > 199901L
  135. /* it's a keyword */
  136. #else
  137. # if GCC_VERSION >= 2007
  138. # define inline __inline__ /* __inline__ prevents -pedantic warnings */
  139. # else
  140. # define inline /* nothing */
  141. # endif
  142. #endif
  143. /* These are obsolete. Do not use. */
  144. #ifndef IN_GCC
  145. #define CONST const
  146. #define VOLATILE volatile
  147. #define SIGNED signed
  148. #define PROTO(type, name, arglist) type name arglist
  149. #define EXFUN(name, proto) name proto
  150. #define DEFUN(name, arglist, args) name(args)
  151. #define DEFUN_VOID(name) name(void)
  152. #define AND ,
  153. #define DOTS , ...
  154. #define NOARGS void
  155. #endif /* ! IN_GCC */
  156. #else /* Not ANSI C. */
  157. #undef ANSI_PROTOTYPES
  158. #define PTR char *
  159. #define PTRCONST PTR
  160. #define LONG_DOUBLE double
  161. #define PARAMS(args) ()
  162. #define VPARAMS(args) (va_alist) va_dcl
  163. #define VA_START(va_list, var) va_start(va_list)
  164. #define VA_OPEN(AP, VAR) { va_list AP; va_start(AP); { struct Qdmy
  165. #define VA_CLOSE(AP) } va_end(AP); }
  166. #define VA_FIXEDARG(AP, TYPE, NAME) TYPE NAME = va_arg(AP, TYPE)
  167. /* some systems define these in header files for non-ansi mode */
  168. #undef const
  169. #undef volatile
  170. #undef signed
  171. #undef inline
  172. #define const
  173. #define volatile
  174. #define signed
  175. #define inline
  176. #ifndef IN_GCC
  177. #define CONST
  178. #define VOLATILE
  179. #define SIGNED
  180. #define PROTO(type, name, arglist) type name ()
  181. #define EXFUN(name, proto) name()
  182. #define DEFUN(name, arglist, args) name arglist args;
  183. #define DEFUN_VOID(name) name()
  184. #define AND ;
  185. #define DOTS
  186. #define NOARGS
  187. #endif /* ! IN_GCC */
  188. #endif /* ANSI C. */
  189. /* Define macros for some gcc attributes. This permits us to use the
  190. macros freely, and know that they will come into play for the
  191. version of gcc in which they are supported. */
  192. #if (GCC_VERSION < 2007)
  193. # define __attribute__(x)
  194. #endif
  195. /* Attribute __malloc__ on functions was valid as of gcc 2.96. */
  196. #ifndef ATTRIBUTE_MALLOC
  197. # if (GCC_VERSION >= 2096)
  198. # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
  199. # else
  200. # define ATTRIBUTE_MALLOC
  201. # endif /* GNUC >= 2.96 */
  202. #endif /* ATTRIBUTE_MALLOC */
  203. /* Attributes on labels were valid as of gcc 2.93. */
  204. #ifndef ATTRIBUTE_UNUSED_LABEL
  205. # if (GCC_VERSION >= 2093)
  206. # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
  207. # else
  208. # define ATTRIBUTE_UNUSED_LABEL
  209. # endif /* GNUC >= 2.93 */
  210. #endif /* ATTRIBUTE_UNUSED_LABEL */
  211. #ifndef ATTRIBUTE_UNUSED
  212. #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  213. #endif /* ATTRIBUTE_UNUSED */
  214. #ifndef ATTRIBUTE_NORETURN
  215. #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  216. #endif /* ATTRIBUTE_NORETURN */
  217. /* Attribute `nonnull' was valid as of gcc 3.3. */
  218. #ifndef ATTRIBUTE_NONNULL
  219. # if (GCC_VERSION >= 3003)
  220. # define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
  221. # else
  222. # define ATTRIBUTE_NONNULL(m)
  223. # endif /* GNUC >= 3.3 */
  224. #endif /* ATTRIBUTE_NONNULL */
  225. /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
  226. This was the case for the `printf' format attribute by itself
  227. before GCC 3.3, but as of 3.3 we need to add the `nonnull'
  228. attribute to retain this behavior. */
  229. #ifndef ATTRIBUTE_PRINTF
  230. #define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m)
  231. #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
  232. #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
  233. #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
  234. #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
  235. #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
  236. #endif /* ATTRIBUTE_PRINTF */
  237. /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A
  238. NULL format specifier was allowed as of gcc 3.3. */
  239. #ifndef ATTRIBUTE_NULL_PRINTF
  240. # if (GCC_VERSION >= 3003)
  241. # define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
  242. # else
  243. # define ATTRIBUTE_NULL_PRINTF(m, n)
  244. # endif /* GNUC >= 3.3 */
  245. # define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
  246. # define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
  247. # define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
  248. # define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
  249. # define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
  250. #endif /* ATTRIBUTE_NULL_PRINTF */
  251. /* We use __extension__ in some places to suppress -pedantic warnings
  252. about GCC extensions. This feature didn't work properly before
  253. gcc 2.8. */
  254. #if GCC_VERSION < 2008
  255. #define __extension__
  256. #endif
  257. #endif /* ansidecl.h */