Rizin
unix-like reverse engineering framework and cli tools
ansidecl.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 
4 /* ANSI and traditional C compatibility macros
5  Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
6  Free Software Foundation, Inc.
7  This file is part of the GNU C Library.
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 /* ANSI and traditional C compatibility macros
24 
25  ANSI C is assumed if __STDC__ is #defined.
26 
27  Macro ANSI C definition Traditional C definition
28  ----- ---- - ---------- ----------- - ----------
29  ANSI_PROTOTYPES 1 not defined
30  PTR `void *' `char *'
31  PTRCONST `void *const' `char *'
32  LONG_DOUBLE `long double' `double'
33  const not defined `'
34  volatile not defined `'
35  signed not defined `'
36  VA_START(ap, var) va_start(ap, var) va_start(ap)
37 
38  Note that it is safe to write "void foo();" indicating a function
39  with no return value, in all K+R compilers we have been able to test.
40 
41  For declaring functions with prototypes, we also provide these:
42 
43  PARAMS ((prototype))
44  -- for functions which take a fixed number of arguments. Use this
45  when declaring the function. When defining the function, write a
46  K+R style argument list. For example:
47 
48  char *strcpy PARAMS ((char *dest, char *source));
49  ...
50  char *
51  strcpy (dest, source)
52  char *dest;
53  char *source;
54  { ... }
55 
56 
57  VPARAMS ((prototype, ...))
58  -- for functions which take a variable number of arguments. Use
59  PARAMS to declare the function, VPARAMS to define it. For example:
60 
61  int printf PARAMS ((const char *format, ...));
62  ...
63  int
64  printf VPARAMS ((const char *format, ...))
65  {
66  ...
67  }
68 
69  For writing functions which take variable numbers of arguments, we
70  also provide the VA_OPEN, VA_CLOSE, and VA_FIXEDARG macros. These
71  hide the differences between K+R <varargs.h> and C89 <stdarg.h> more
72  thoroughly than the simple VA_START() macro mentioned above.
73 
74  VA_OPEN and VA_CLOSE are used *instead of* va_start and va_end.
75  Immediately after VA_OPEN, put a sequence of VA_FIXEDARG calls
76  corresponding to the list of fixed arguments. Then use va_arg
77  normally to get the variable arguments, or pass your va_list object
78  around. You do not declare the va_list yourself; VA_OPEN does it
79  for you.
80 
81  Here is a complete example:
82 
83  int
84  printf VPARAMS ((const char *format, ...))
85  {
86  int result;
87 
88  VA_OPEN (ap, format);
89  VA_FIXEDARG (ap, const char *, format);
90 
91  result = vfprintf (stdout, format, ap);
92  VA_CLOSE (ap);
93 
94  return result;
95  }
96 
97 
98  You can declare variables either before or after the VA_OPEN,
99  VA_FIXEDARG sequence. Also, VA_OPEN and VA_CLOSE are the beginning
100  and end of a block. They must appear at the same nesting level,
101  and any variables declared after VA_OPEN go out of scope at
102  VA_CLOSE. Unfortunately, with a K+R compiler, that includes the
103  argument list. You can have multiple instances of VA_OPEN/VA_CLOSE
104  pairs in a single function in case you need to traverse the
105  argument list more than once.
106 
107  For ease of writing code which uses GCC extensions but needs to be
108  portable to other compilers, we provide the GCC_VERSION macro that
109  simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
110  wrappers around __attribute__. Also, __extension__ will be #defined
111  to nothing if it doesn't work. See below.
112 
113  This header also defines a lot of obsolete macros:
114  CONST, VOLATILE, SIGNED, PROTO, EXFUN, DEFUN, DEFUN_VOID,
115  AND, DOTS, NOARGS. Don't use them. */
116 
117 #ifndef _ANSIDECL_H
118 #define _ANSIDECL_H
119 
120 /* Every source file includes this file,
121  so they will all get the switch for lint. */
122 /* LINTLIBRARY */
123 
124 /* Using MACRO(x,y) in cpp #if conditionals does not work with some
125  older preprocessors. Thus we can't define something like this:
126 
127 #define HAVE_GCC_VERSION(MAJOR, MINOR) \
128  (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
129 
130 and then test "#if HAVE_GCC_VERSION(2,7)".
131 
132 So instead we use the macro below and test it against specific values. */
133 
134 /* This macro simplifies testing whether we are using gcc, and if it
135  is of a particular minimum version. (Both major & minor numbers are
136  significant.) This macro will evaluate to 0 if we are not using
137  gcc at all. */
138 #ifndef GCC_VERSION
139 #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
140 #endif /* GCC_VERSION */
141 
142 #if defined(__STDC__) || defined(_AIX) || (defined(__mips) && defined(_SYSTYPE_SVR4)) || defined(_WIN32) || defined(__WINDOWS__) || (defined(__alpha) && defined(__cplusplus))
143 /* All known AIX compilers implement these things (but don't always
144  define __STDC__). The RISC/OS MIPS compiler defines these things
145  in SVR4 mode, but does not define __STDC__. */
146 /* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
147  C++ compilers, does not define __STDC__, though it acts as if this
148  was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
149 
150 #define ANSI_PROTOTYPES 1
151 #define PTR void *
152 #define PTRCONST void *const
153 #define LONG_DOUBLE long double
154 
155 /* PARAMS is often defined elsewhere (e.g. by libintl.h), so wrap it in
156  a #ifndef. */
157 #ifndef PARAMS
158 #define PARAMS(ARGS) ARGS
159 #endif
160 
161 #define VPARAMS(ARGS) ARGS
162 #define VA_START(VA_LIST, VAR) va_start(VA_LIST, VAR)
163 
164 /* variadic function helper macros */
165 /* "struct Qdmy" swallows the semicolon after VA_OPEN/VA_FIXEDARG's
166  use without inhibiting further decls and without declaring an
167  actual variable. */
168 #define VA_OPEN(AP, VAR) \
169  { \
170  va_list AP; \
171  va_start(AP, VAR); \
172  { \
173  struct Qdmy
174 #define VA_CLOSE(AP) \
175  } \
176  va_end(AP); \
177  }
178 #define VA_FIXEDARG(AP, T, N) struct Qdmy
179 
180 #undef const
181 #undef volatile
182 #undef signed
183 
184 /* inline requires special treatment; it's in C99, and GCC >=2.7 supports
185  it too, but it's not in C89. */
186 #undef inline
187 #if __STDC_VERSION__ > 199901L
188 /* it's a keyword */
189 #else
190 #if GCC_VERSION >= 2007
191 #define inline __inline__ /* __inline__ prevents -pedantic warnings */
192 #else
193 #define inline /* nothing */
194 #endif
195 #endif
196 
197 /* These are obsolete. Do not use. */
198 #ifndef IN_GCC
199 #define CONST const
200 #define VOLATILE volatile
201 #define SIGNED signed
202 
203 #define PROTO(type, name, arglist) type name arglist
204 #define EXFUN(name, proto) name proto
205 #define DEFUN(name, arglist, args) name(args)
206 #define DEFUN_VOID(name) name(void)
207 #define AND ,
208 #define DOTS , ...
209 #define NOARGS void
210 #endif /* ! IN_GCC */
211 
212 #else /* Not ANSI C. */
213 
214 #undef ANSI_PROTOTYPES
215 #define PTR char *
216 #define PTRCONST PTR
217 #define LONG_DOUBLE double
218 
219 #define PARAMS(args) ()
220 #define VPARAMS(args) (va_alist) va_dcl
221 #define VA_START(va_list, var) va_start(va_list)
222 
223 #define VA_OPEN(AP, VAR) \
224  { \
225  va_list AP; \
226  va_start(AP); \
227  { \
228  struct Qdmy
229 #define VA_CLOSE(AP) \
230  } \
231  va_end(AP); \
232  }
233 #define VA_FIXEDARG(AP, TYPE, NAME) TYPE NAME = va_arg(AP, TYPE)
234 
235 /* some systems define these in header files for non-ansi mode */
236 #undef const
237 #undef volatile
238 #undef signed
239 #undef inline
240 #define const
241 #define volatile
242 #define signed
243 #define inline
244 
245 #ifndef IN_GCC
246 #define CONST
247 #define VOLATILE
248 #define SIGNED
249 
250 #define PROTO(type, name, arglist) type name()
251 #define EXFUN(name, proto) name()
252 #define DEFUN(name, arglist, args) name arglist args;
253 #define DEFUN_VOID(name) name()
254 #define AND ;
255 #define DOTS
256 #define NOARGS
257 #endif /* ! IN_GCC */
258 
259 #endif /* ANSI C. */
260 
261 /* Define macros for some gcc attributes. This permits us to use the
262  macros freely, and know that they will come into play for the
263  version of gcc in which they are supported. */
264 
265 #if (GCC_VERSION < 2007)
266 #define __attribute__(x)
267 #endif
268 
269 /* Attribute __malloc__ on functions was valid as of gcc 2.96. */
270 #ifndef ATTRIBUTE_MALLOC
271 #if (GCC_VERSION >= 2096)
272 #define ATTRIBUTE_MALLOC __attribute__((__malloc__))
273 #else
274 #define ATTRIBUTE_MALLOC
275 #endif /* GNUC >= 2.96 */
276 #endif /* ATTRIBUTE_MALLOC */
277 
278 /* Attributes on labels were valid as of gcc 2.93. */
279 #ifndef ATTRIBUTE_UNUSED_LABEL
280 #if (!defined(__cplusplus) && GCC_VERSION >= 2093)
281 #define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
282 #else
283 #define ATTRIBUTE_UNUSED_LABEL
284 #endif /* !__cplusplus && GNUC >= 2.93 */
285 #endif /* ATTRIBUTE_UNUSED_LABEL */
286 
287 #ifndef ATTRIBUTE_UNUSED
288 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
289 #endif /* ATTRIBUTE_UNUSED */
290 
291 /* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the
292  identifier name. */
293 #if !defined(__cplusplus) || (GCC_VERSION >= 3004)
294 #define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED
295 #else /* !__cplusplus || GNUC >= 3.4 */
296 #define ARG_UNUSED(NAME) NAME
297 #endif /* !__cplusplus || GNUC >= 3.4 */
298 
299 #ifndef ATTRIBUTE_NORETURN
300 #define ATTRIBUTE_NORETURN __attribute__((__noreturn__))
301 #endif /* ATTRIBUTE_NORETURN */
302 
303 /* Attribute `nonnull' was valid as of gcc 3.3. */
304 #ifndef ATTRIBUTE_NONNULL
305 #if (GCC_VERSION >= 3003)
306 #define ATTRIBUTE_NONNULL(m) __attribute__((__nonnull__(m)))
307 #else
308 #define ATTRIBUTE_NONNULL(m)
309 #endif /* GNUC >= 3.3 */
310 #endif /* ATTRIBUTE_NONNULL */
311 
312 /* Attribute `pure' was valid as of gcc 3.0. */
313 #ifndef ATTRIBUTE_PURE
314 #if (GCC_VERSION >= 3000)
315 #define ATTRIBUTE_PURE __attribute__((__pure__))
316 #else
317 #define ATTRIBUTE_PURE
318 #endif /* GNUC >= 3.0 */
319 #endif /* ATTRIBUTE_PURE */
320 
321 /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
322  This was the case for the `printf' format attribute by itself
323  before GCC 3.3, but as of 3.3 we need to add the `nonnull'
324  attribute to retain this behavior. */
325 #ifndef ATTRIBUTE_PRINTF
326 #define ATTRIBUTE_PRINTF(m, n) __attribute__((__format__(__printf__, m, n))) ATTRIBUTE_NONNULL(m)
327 #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
328 #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
329 #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
330 #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
331 #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
332 #endif /* ATTRIBUTE_PRINTF */
333 
334 /* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on
335  a function pointer. Format attributes were allowed on function
336  pointers as of gcc 3.1. */
337 #ifndef ATTRIBUTE_FPTR_PRINTF
338 #if (GCC_VERSION >= 3001)
339 #define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n)
340 #else
341 #define ATTRIBUTE_FPTR_PRINTF(m, n)
342 #endif /* GNUC >= 3.1 */
343 #define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2)
344 #define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3)
345 #define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4)
346 #define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5)
347 #define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6)
348 #endif /* ATTRIBUTE_FPTR_PRINTF */
349 
350 /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A
351  NULL format specifier was allowed as of gcc 3.3. */
352 #ifndef ATTRIBUTE_NULL_PRINTF
353 #if (GCC_VERSION >= 3003)
354 #define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__((__format__(__printf__, m, n)))
355 #else
356 #define ATTRIBUTE_NULL_PRINTF(m, n)
357 #endif /* GNUC >= 3.3 */
358 #define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
359 #define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
360 #define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
361 #define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
362 #define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
363 #endif /* ATTRIBUTE_NULL_PRINTF */
364 
365 /* Attribute `sentinel' was valid as of gcc 3.5. */
366 #ifndef ATTRIBUTE_SENTINEL
367 #if (GCC_VERSION >= 3005)
368 #define ATTRIBUTE_SENTINEL __attribute__((__sentinel__))
369 #else
370 #define ATTRIBUTE_SENTINEL
371 #endif /* GNUC >= 3.5 */
372 #endif /* ATTRIBUTE_SENTINEL */
373 
374 #ifndef ATTRIBUTE_ALIGNED_ALIGNOF
375 #if (GCC_VERSION >= 3000)
376 #define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__((__aligned__(__alignof__(m))))
377 #else
378 #define ATTRIBUTE_ALIGNED_ALIGNOF(m)
379 #endif /* GNUC >= 3.0 */
380 #endif /* ATTRIBUTE_ALIGNED_ALIGNOF */
381 
382 /* Useful for structures whose layout must much some binary specification
383  regardless of the alignment and padding qualities of the compiler. */
384 #ifndef ATTRIBUTE_PACKED
385 #define ATTRIBUTE_PACKED __attribute__((packed))
386 #endif
387 
388 /* Attribute `hot' and `cold' was valid as of gcc 4.3. */
389 #ifndef ATTRIBUTE_COLD
390 #if (GCC_VERSION >= 4003)
391 #define ATTRIBUTE_COLD __attribute__((__cold__))
392 #else
393 #define ATTRIBUTE_COLD
394 #endif /* GNUC >= 4.3 */
395 #endif /* ATTRIBUTE_COLD */
396 #ifndef ATTRIBUTE_HOT
397 #if (GCC_VERSION >= 4003)
398 #define ATTRIBUTE_HOT __attribute__((__hot__))
399 #else
400 #define ATTRIBUTE_HOT
401 #endif /* GNUC >= 4.3 */
402 #endif /* ATTRIBUTE_HOT */
403 
404 /* We use __extension__ in some places to suppress -pedantic warnings
405  about GCC extensions. This feature didn't work properly before
406  gcc 2.8. */
407 #if GCC_VERSION < 2008
408 #define __extension__
409 #endif
410 
411 #endif /* ansidecl.h */