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