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