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