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