1130812Smarcel/* ANSI and traditional C compatability macros 2130812Smarcel Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001 3130812Smarcel Free Software Foundation, Inc. 4130812Smarcel This file is part of the GNU C Library. 5130812Smarcel 6130812SmarcelThis program is free software; you can redistribute it and/or modify 7130812Smarcelit under the terms of the GNU General Public License as published by 8130812Smarcelthe Free Software Foundation; either version 2 of the License, or 9130812Smarcel(at your option) any later version. 10130812Smarcel 11130812SmarcelThis program is distributed in the hope that it will be useful, 12130812Smarcelbut WITHOUT ANY WARRANTY; without even the implied warranty of 13130812SmarcelMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14130812SmarcelGNU General Public License for more details. 15130812Smarcel 16130812SmarcelYou should have received a copy of the GNU General Public License 17130812Smarcelalong with this program; if not, write to the Free Software 18130812SmarcelFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19130812Smarcel 20130812Smarcel/* ANSI and traditional C compatibility macros 21130812Smarcel 22130812Smarcel ANSI C is assumed if __STDC__ is #defined. 23130812Smarcel 24130812Smarcel Macro ANSI C definition Traditional C definition 25130812Smarcel ----- ---- - ---------- ----------- - ---------- 26130812Smarcel ANSI_PROTOTYPES 1 not defined 27130812Smarcel PTR `void *' `char *' 28130812Smarcel PTRCONST `void *const' `char *' 29130812Smarcel LONG_DOUBLE `long double' `double' 30130812Smarcel const not defined `' 31130812Smarcel volatile not defined `' 32130812Smarcel signed not defined `' 33130812Smarcel VA_START(ap, var) va_start(ap, var) va_start(ap) 34130812Smarcel 35130812Smarcel Note that it is safe to write "void foo();" indicating a function 36130812Smarcel with no return value, in all K+R compilers we have been able to test. 37130812Smarcel 38130812Smarcel For declaring functions with prototypes, we also provide these: 39130812Smarcel 40130812Smarcel PARAMS ((prototype)) 41130812Smarcel -- for functions which take a fixed number of arguments. Use this 42130812Smarcel when declaring the function. When defining the function, write a 43130812Smarcel K+R style argument list. For example: 44130812Smarcel 45130812Smarcel char *strcpy PARAMS ((char *dest, char *source)); 46130812Smarcel ... 47130812Smarcel char * 48130812Smarcel strcpy (dest, source) 49130812Smarcel char *dest; 50130812Smarcel char *source; 51130812Smarcel { ... } 52130812Smarcel 53130812Smarcel 54130812Smarcel VPARAMS ((prototype, ...)) 55130812Smarcel -- for functions which take a variable number of arguments. Use 56130812Smarcel PARAMS to declare the function, VPARAMS to define it. For example: 57130812Smarcel 58130812Smarcel int printf PARAMS ((const char *format, ...)); 59130812Smarcel ... 60130812Smarcel int 61130812Smarcel printf VPARAMS ((const char *format, ...)) 62130812Smarcel { 63130812Smarcel ... 64130812Smarcel } 65130812Smarcel 66130812Smarcel For writing functions which take variable numbers of arguments, we 67130812Smarcel also provide the VA_OPEN, VA_CLOSE, and VA_FIXEDARG macros. These 68130812Smarcel hide the differences between K+R <varargs.h> and C89 <stdarg.h> more 69130812Smarcel thoroughly than the simple VA_START() macro mentioned above. 70130812Smarcel 71130812Smarcel VA_OPEN and VA_CLOSE are used *instead of* va_start and va_end. 72130812Smarcel Immediately after VA_OPEN, put a sequence of VA_FIXEDARG calls 73130812Smarcel corresponding to the list of fixed arguments. Then use va_arg 74130812Smarcel normally to get the variable arguments, or pass your va_list object 75130812Smarcel around. You do not declare the va_list yourself; VA_OPEN does it 76130812Smarcel for you. 77130812Smarcel 78130812Smarcel Here is a complete example: 79130812Smarcel 80130812Smarcel int 81130812Smarcel printf VPARAMS ((const char *format, ...)) 82130812Smarcel { 83130812Smarcel int result; 84130812Smarcel 85130812Smarcel VA_OPEN (ap, format); 86130812Smarcel VA_FIXEDARG (ap, const char *, format); 87130812Smarcel 88130812Smarcel result = vfprintf (stdout, format, ap); 89130812Smarcel VA_CLOSE (ap); 90130812Smarcel 91130812Smarcel return result; 92130812Smarcel } 93130812Smarcel 94130812Smarcel 95130812Smarcel You can declare variables either before or after the VA_OPEN, 96130812Smarcel VA_FIXEDARG sequence. Also, VA_OPEN and VA_CLOSE are the beginning 97130812Smarcel and end of a block. They must appear at the same nesting level, 98130812Smarcel and any variables declared after VA_OPEN go out of scope at 99130812Smarcel VA_CLOSE. Unfortunately, with a K+R compiler, that includes the 100130812Smarcel argument list. You can have multiple instances of VA_OPEN/VA_CLOSE 101130812Smarcel pairs in a single function in case you need to traverse the 102130812Smarcel argument list more than once. 103130812Smarcel 104130812Smarcel For ease of writing code which uses GCC extensions but needs to be 105130812Smarcel portable to other compilers, we provide the GCC_VERSION macro that 106130812Smarcel simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various 107130812Smarcel wrappers around __attribute__. Also, __extension__ will be #defined 108130812Smarcel to nothing if it doesn't work. See below. 109130812Smarcel 110130812Smarcel This header also defines a lot of obsolete macros: 111130812Smarcel CONST, VOLATILE, SIGNED, PROTO, EXFUN, DEFUN, DEFUN_VOID, 112130812Smarcel AND, DOTS, NOARGS. Don't use them. */ 113130812Smarcel 114130812Smarcel#ifndef _ANSIDECL_H 115130812Smarcel#define _ANSIDECL_H 1 116130812Smarcel 117130812Smarcel/* Every source file includes this file, 118130812Smarcel so they will all get the switch for lint. */ 119130812Smarcel/* LINTLIBRARY */ 120130812Smarcel 121130812Smarcel/* Using MACRO(x,y) in cpp #if conditionals does not work with some 122130812Smarcel older preprocessors. Thus we can't define something like this: 123130812Smarcel 124130812Smarcel#define HAVE_GCC_VERSION(MAJOR, MINOR) \ 125130812Smarcel (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR))) 126130812Smarcel 127130812Smarceland then test "#if HAVE_GCC_VERSION(2,7)". 128130812Smarcel 129130812SmarcelSo instead we use the macro below and test it against specific values. */ 130130812Smarcel 131130812Smarcel/* This macro simplifies testing whether we are using gcc, and if it 132130812Smarcel is of a particular minimum version. (Both major & minor numbers are 133130812Smarcel significant.) This macro will evaluate to 0 if we are not using 134130812Smarcel gcc at all. */ 135130812Smarcel#ifndef GCC_VERSION 136130812Smarcel#define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__) 137130812Smarcel#endif /* GCC_VERSION */ 138130812Smarcel 139130812Smarcel#if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32) || (defined(__alpha) && defined(__cplusplus)) 140130812Smarcel/* All known AIX compilers implement these things (but don't always 141130812Smarcel define __STDC__). The RISC/OS MIPS compiler defines these things 142130812Smarcel in SVR4 mode, but does not define __STDC__. */ 143130812Smarcel/* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other 144130812Smarcel C++ compilers, does not define __STDC__, though it acts as if this 145130812Smarcel was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */ 146130812Smarcel 147130812Smarcel#define ANSI_PROTOTYPES 1 148130812Smarcel#define PTR void * 149130812Smarcel#define PTRCONST void *const 150130812Smarcel#define LONG_DOUBLE long double 151130812Smarcel 152130812Smarcel#define PARAMS(ARGS) ARGS 153130812Smarcel#define VPARAMS(ARGS) ARGS 154130812Smarcel#define VA_START(VA_LIST, VAR) va_start(VA_LIST, VAR) 155130812Smarcel 156130812Smarcel/* variadic function helper macros */ 157130812Smarcel/* "struct Qdmy" swallows the semicolon after VA_OPEN/VA_FIXEDARG's 158130812Smarcel use without inhibiting further decls and without declaring an 159130812Smarcel actual variable. */ 160130812Smarcel#define VA_OPEN(AP, VAR) { va_list AP; va_start(AP, VAR); { struct Qdmy 161130812Smarcel#define VA_CLOSE(AP) } va_end(AP); } 162130812Smarcel#define VA_FIXEDARG(AP, T, N) struct Qdmy 163130812Smarcel 164130812Smarcel#undef const 165130812Smarcel#undef volatile 166130812Smarcel#undef signed 167130812Smarcel 168130812Smarcel/* inline requires special treatment; it's in C99, and GCC >=2.7 supports 169130812Smarcel it too, but it's not in C89. */ 170130812Smarcel#undef inline 171130812Smarcel#if __STDC_VERSION__ > 199901L 172130812Smarcel/* it's a keyword */ 173130812Smarcel#else 174130812Smarcel# if GCC_VERSION >= 2007 175130812Smarcel# define inline __inline__ /* __inline__ prevents -pedantic warnings */ 176130812Smarcel# else 177130812Smarcel# define inline /* nothing */ 178130812Smarcel# endif 179130812Smarcel#endif 180130812Smarcel 181130812Smarcel/* These are obsolete. Do not use. */ 182130812Smarcel#ifndef IN_GCC 183130812Smarcel#define CONST const 184130812Smarcel#define VOLATILE volatile 185130812Smarcel#define SIGNED signed 186130812Smarcel 187130812Smarcel#define PROTO(type, name, arglist) type name arglist 188130812Smarcel#define EXFUN(name, proto) name proto 189130812Smarcel#define DEFUN(name, arglist, args) name(args) 190130812Smarcel#define DEFUN_VOID(name) name(void) 191130812Smarcel#define AND , 192130812Smarcel#define DOTS , ... 193130812Smarcel#define NOARGS void 194130812Smarcel#endif /* ! IN_GCC */ 195130812Smarcel 196130812Smarcel#else /* Not ANSI C. */ 197130812Smarcel 198130812Smarcel#undef ANSI_PROTOTYPES 199130812Smarcel#define PTR char * 200130812Smarcel#define PTRCONST PTR 201130812Smarcel#define LONG_DOUBLE double 202130812Smarcel 203130812Smarcel#define PARAMS(args) () 204130812Smarcel#define VPARAMS(args) (va_alist) va_dcl 205130812Smarcel#define VA_START(va_list, var) va_start(va_list) 206130812Smarcel 207130812Smarcel#define VA_OPEN(AP, VAR) { va_list AP; va_start(AP); { struct Qdmy 208130812Smarcel#define VA_CLOSE(AP) } va_end(AP); } 209130812Smarcel#define VA_FIXEDARG(AP, TYPE, NAME) TYPE NAME = va_arg(AP, TYPE) 210130812Smarcel 211130812Smarcel/* some systems define these in header files for non-ansi mode */ 212130812Smarcel#undef const 213130812Smarcel#undef volatile 214130812Smarcel#undef signed 215130812Smarcel#undef inline 216130812Smarcel#define const 217130812Smarcel#define volatile 218130812Smarcel#define signed 219130812Smarcel#define inline 220130812Smarcel 221130812Smarcel#ifndef IN_GCC 222130812Smarcel#define CONST 223130812Smarcel#define VOLATILE 224130812Smarcel#define SIGNED 225130812Smarcel 226130812Smarcel#define PROTO(type, name, arglist) type name () 227130812Smarcel#define EXFUN(name, proto) name() 228130812Smarcel#define DEFUN(name, arglist, args) name arglist args; 229130812Smarcel#define DEFUN_VOID(name) name() 230130812Smarcel#define AND ; 231130812Smarcel#define DOTS 232130812Smarcel#define NOARGS 233130812Smarcel#endif /* ! IN_GCC */ 234130812Smarcel 235130812Smarcel#endif /* ANSI C. */ 236130812Smarcel 237130812Smarcel/* Define macros for some gcc attributes. This permits us to use the 238130812Smarcel macros freely, and know that they will come into play for the 239130812Smarcel version of gcc in which they are supported. */ 240130812Smarcel 241130812Smarcel#if (GCC_VERSION < 2007) 242130812Smarcel# define __attribute__(x) 243130812Smarcel#endif 244130812Smarcel 245130812Smarcel/* Attribute __malloc__ on functions was valid as of gcc 2.96. */ 246130812Smarcel#ifndef ATTRIBUTE_MALLOC 247130812Smarcel# if (GCC_VERSION >= 2096) 248130812Smarcel# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) 249130812Smarcel# else 250130812Smarcel# define ATTRIBUTE_MALLOC 251130812Smarcel# endif /* GNUC >= 2.96 */ 252130812Smarcel#endif /* ATTRIBUTE_MALLOC */ 253130812Smarcel 254130812Smarcel/* Attributes on labels were valid as of gcc 2.93. */ 255130812Smarcel#ifndef ATTRIBUTE_UNUSED_LABEL 256130812Smarcel# if (GCC_VERSION >= 2093) 257130812Smarcel# define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED 258130812Smarcel# else 259130812Smarcel# define ATTRIBUTE_UNUSED_LABEL 260130812Smarcel# endif /* GNUC >= 2.93 */ 261130812Smarcel#endif /* ATTRIBUTE_UNUSED_LABEL */ 262130812Smarcel 263130812Smarcel#ifndef ATTRIBUTE_UNUSED 264130812Smarcel#define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) 265130812Smarcel#endif /* ATTRIBUTE_UNUSED */ 266130812Smarcel 267130812Smarcel#ifndef ATTRIBUTE_NORETURN 268130812Smarcel#define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) 269130812Smarcel#endif /* ATTRIBUTE_NORETURN */ 270130812Smarcel 271130812Smarcel/* Attribute `nonnull' was valid as of gcc 3.3. */ 272130812Smarcel#ifndef ATTRIBUTE_NONNULL 273130812Smarcel# if (GCC_VERSION >= 3003) 274130812Smarcel# define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m))) 275130812Smarcel# else 276130812Smarcel# define ATTRIBUTE_NONNULL(m) 277130812Smarcel# endif /* GNUC >= 3.3 */ 278130812Smarcel#endif /* ATTRIBUTE_NONNULL */ 279130812Smarcel 280130812Smarcel/* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL. 281130812Smarcel This was the case for the `printf' format attribute by itself 282130812Smarcel before GCC 3.3, but as of 3.3 we need to add the `nonnull' 283130812Smarcel attribute to retain this behavior. */ 284130812Smarcel#ifndef ATTRIBUTE_PRINTF 285130812Smarcel#define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m) 286130812Smarcel#define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2) 287130812Smarcel#define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3) 288130812Smarcel#define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4) 289130812Smarcel#define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5) 290130812Smarcel#define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6) 291130812Smarcel#endif /* ATTRIBUTE_PRINTF */ 292130812Smarcel 293130812Smarcel/* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A 294130812Smarcel NULL format specifier was allowed as of gcc 3.3. */ 295130812Smarcel#ifndef ATTRIBUTE_NULL_PRINTF 296130812Smarcel# if (GCC_VERSION >= 3003) 297130812Smarcel# define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) 298130812Smarcel# else 299130812Smarcel# define ATTRIBUTE_NULL_PRINTF(m, n) 300130812Smarcel# endif /* GNUC >= 3.3 */ 301130812Smarcel# define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2) 302130812Smarcel# define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3) 303130812Smarcel# define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4) 304130812Smarcel# define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5) 305130812Smarcel# define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6) 306130812Smarcel#endif /* ATTRIBUTE_NULL_PRINTF */ 307130812Smarcel 308130812Smarcel/* We use __extension__ in some places to suppress -pedantic warnings 309130812Smarcel about GCC extensions. This feature didn't work properly before 310130812Smarcel gcc 2.8. */ 311130812Smarcel#if GCC_VERSION < 2008 312130812Smarcel#define __extension__ 313130812Smarcel#endif 314130812Smarcel 315130812Smarcel#endif /* ansidecl.h */ 316