1 1.4 mrg /* Compiler compatibility macros 2 1.4 mrg Copyright (C) 1991-2024 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.4 mrg /* For ease of writing code which uses GCC extensions but needs to be 20 1.1 mrg portable to other compilers, we provide the GCC_VERSION macro that 21 1.1 mrg simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various 22 1.1 mrg wrappers around __attribute__. Also, __extension__ will be #defined 23 1.2 christos to nothing if it doesn't work. See below. */ 24 1.1 mrg 25 1.1 mrg #ifndef _ANSIDECL_H 26 1.1 mrg #define _ANSIDECL_H 1 27 1.1 mrg 28 1.1 mrg #ifdef __cplusplus 29 1.1 mrg extern "C" { 30 1.1 mrg #endif 31 1.1 mrg 32 1.1 mrg /* Every source file includes this file, 33 1.1 mrg so they will all get the switch for lint. */ 34 1.1 mrg /* LINTLIBRARY */ 35 1.1 mrg 36 1.1 mrg /* Using MACRO(x,y) in cpp #if conditionals does not work with some 37 1.1 mrg older preprocessors. Thus we can't define something like this: 38 1.1 mrg 39 1.1 mrg #define HAVE_GCC_VERSION(MAJOR, MINOR) \ 40 1.1 mrg (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR))) 41 1.1 mrg 42 1.1 mrg and then test "#if HAVE_GCC_VERSION(2,7)". 43 1.1 mrg 44 1.1 mrg So instead we use the macro below and test it against specific values. */ 45 1.1 mrg 46 1.1 mrg /* This macro simplifies testing whether we are using gcc, and if it 47 1.1 mrg is of a particular minimum version. (Both major & minor numbers are 48 1.1 mrg significant.) This macro will evaluate to 0 if we are not using 49 1.1 mrg gcc at all. */ 50 1.1 mrg #ifndef GCC_VERSION 51 1.1 mrg #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__) 52 1.1 mrg #endif /* GCC_VERSION */ 53 1.1 mrg 54 1.1 mrg /* inline requires special treatment; it's in C99, and GCC >=2.7 supports 55 1.1 mrg it too, but it's not in C89. */ 56 1.1 mrg #undef inline 57 1.3 mrg #if (!defined(__cplusplus) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) || (defined(__SUNPRO_C) && defined(__C99FEATURES__)) 58 1.1 mrg /* it's a keyword */ 59 1.1 mrg #else 60 1.1 mrg # if GCC_VERSION >= 2007 61 1.1 mrg # define inline __inline__ /* __inline__ prevents -pedantic warnings */ 62 1.1 mrg # else 63 1.1 mrg # define inline /* nothing */ 64 1.1 mrg # endif 65 1.1 mrg #endif 66 1.1 mrg 67 1.1 mrg /* Define macros for some gcc attributes. This permits us to use the 68 1.1 mrg macros freely, and know that they will come into play for the 69 1.1 mrg version of gcc in which they are supported. */ 70 1.1 mrg 71 1.1 mrg #if (GCC_VERSION < 2007) 72 1.1 mrg # define __attribute__(x) 73 1.1 mrg #endif 74 1.1 mrg 75 1.1 mrg /* Attribute __malloc__ on functions was valid as of gcc 2.96. */ 76 1.1 mrg #ifndef ATTRIBUTE_MALLOC 77 1.1 mrg # if (GCC_VERSION >= 2096) 78 1.1 mrg # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) 79 1.1 mrg # else 80 1.1 mrg # define ATTRIBUTE_MALLOC 81 1.1 mrg # endif /* GNUC >= 2.96 */ 82 1.1 mrg #endif /* ATTRIBUTE_MALLOC */ 83 1.1 mrg 84 1.1 mrg /* Attributes on labels were valid as of gcc 2.93 and g++ 4.5. For 85 1.1 mrg g++ an attribute on a label must be followed by a semicolon. */ 86 1.1 mrg #ifndef ATTRIBUTE_UNUSED_LABEL 87 1.1 mrg # ifndef __cplusplus 88 1.1 mrg # if GCC_VERSION >= 2093 89 1.1 mrg # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED 90 1.1 mrg # else 91 1.1 mrg # define ATTRIBUTE_UNUSED_LABEL 92 1.1 mrg # endif 93 1.1 mrg # else 94 1.1 mrg # if GCC_VERSION >= 4005 95 1.1 mrg # define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED ; 96 1.1 mrg # else 97 1.1 mrg # define ATTRIBUTE_UNUSED_LABEL 98 1.1 mrg # endif 99 1.1 mrg # endif 100 1.1 mrg #endif 101 1.1 mrg 102 1.2 christos /* Similarly to ARG_UNUSED below. Prior to GCC 3.4, the C++ frontend 103 1.2 christos couldn't parse attributes placed after the identifier name, and now 104 1.2 christos the entire compiler is built with C++. */ 105 1.1 mrg #ifndef ATTRIBUTE_UNUSED 106 1.2 christos #if GCC_VERSION >= 3004 107 1.2 christos # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) 108 1.2 christos #else 109 1.2 christos #define ATTRIBUTE_UNUSED 110 1.2 christos #endif 111 1.1 mrg #endif /* ATTRIBUTE_UNUSED */ 112 1.1 mrg 113 1.1 mrg /* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the 114 1.1 mrg identifier name. */ 115 1.1 mrg #if ! defined(__cplusplus) || (GCC_VERSION >= 3004) 116 1.1 mrg # define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED 117 1.1 mrg #else /* !__cplusplus || GNUC >= 3.4 */ 118 1.1 mrg # define ARG_UNUSED(NAME) NAME 119 1.1 mrg #endif /* !__cplusplus || GNUC >= 3.4 */ 120 1.1 mrg 121 1.1 mrg #ifndef ATTRIBUTE_NORETURN 122 1.1 mrg #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) 123 1.1 mrg #endif /* ATTRIBUTE_NORETURN */ 124 1.1 mrg 125 1.1 mrg /* Attribute `nonnull' was valid as of gcc 3.3. */ 126 1.1 mrg #ifndef ATTRIBUTE_NONNULL 127 1.1 mrg # if (GCC_VERSION >= 3003) 128 1.1 mrg # define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m))) 129 1.1 mrg # else 130 1.1 mrg # define ATTRIBUTE_NONNULL(m) 131 1.1 mrg # endif /* GNUC >= 3.3 */ 132 1.1 mrg #endif /* ATTRIBUTE_NONNULL */ 133 1.1 mrg 134 1.2 christos /* Attribute `returns_nonnull' was valid as of gcc 4.9. */ 135 1.2 christos #ifndef ATTRIBUTE_RETURNS_NONNULL 136 1.2 christos # if (GCC_VERSION >= 4009) 137 1.2 christos # define ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__)) 138 1.2 christos # else 139 1.2 christos # define ATTRIBUTE_RETURNS_NONNULL 140 1.2 christos # endif /* GNUC >= 4.9 */ 141 1.2 christos #endif /* ATTRIBUTE_RETURNS_NONNULL */ 142 1.2 christos 143 1.1 mrg /* Attribute `pure' was valid as of gcc 3.0. */ 144 1.1 mrg #ifndef ATTRIBUTE_PURE 145 1.1 mrg # if (GCC_VERSION >= 3000) 146 1.1 mrg # define ATTRIBUTE_PURE __attribute__ ((__pure__)) 147 1.1 mrg # else 148 1.1 mrg # define ATTRIBUTE_PURE 149 1.1 mrg # endif /* GNUC >= 3.0 */ 150 1.1 mrg #endif /* ATTRIBUTE_PURE */ 151 1.1 mrg 152 1.1 mrg /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL. 153 1.1 mrg This was the case for the `printf' format attribute by itself 154 1.1 mrg before GCC 3.3, but as of 3.3 we need to add the `nonnull' 155 1.1 mrg attribute to retain this behavior. */ 156 1.1 mrg #ifndef ATTRIBUTE_PRINTF 157 1.1 mrg #define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m) 158 1.1 mrg #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2) 159 1.1 mrg #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3) 160 1.1 mrg #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4) 161 1.1 mrg #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5) 162 1.1 mrg #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6) 163 1.1 mrg #endif /* ATTRIBUTE_PRINTF */ 164 1.1 mrg 165 1.1 mrg /* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on 166 1.1 mrg a function pointer. Format attributes were allowed on function 167 1.1 mrg pointers as of gcc 3.1. */ 168 1.1 mrg #ifndef ATTRIBUTE_FPTR_PRINTF 169 1.1 mrg # if (GCC_VERSION >= 3001) 170 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n) 171 1.1 mrg # else 172 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF(m, n) 173 1.1 mrg # endif /* GNUC >= 3.1 */ 174 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2) 175 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3) 176 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4) 177 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5) 178 1.1 mrg # define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6) 179 1.1 mrg #endif /* ATTRIBUTE_FPTR_PRINTF */ 180 1.1 mrg 181 1.1 mrg /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL. A 182 1.1 mrg NULL format specifier was allowed as of gcc 3.3. */ 183 1.1 mrg #ifndef ATTRIBUTE_NULL_PRINTF 184 1.1 mrg # if (GCC_VERSION >= 3003) 185 1.1 mrg # define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) 186 1.1 mrg # else 187 1.1 mrg # define ATTRIBUTE_NULL_PRINTF(m, n) 188 1.1 mrg # endif /* GNUC >= 3.3 */ 189 1.1 mrg # define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2) 190 1.1 mrg # define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3) 191 1.1 mrg # define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4) 192 1.1 mrg # define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5) 193 1.1 mrg # define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6) 194 1.1 mrg #endif /* ATTRIBUTE_NULL_PRINTF */ 195 1.1 mrg 196 1.1 mrg /* Attribute `sentinel' was valid as of gcc 3.5. */ 197 1.1 mrg #ifndef ATTRIBUTE_SENTINEL 198 1.1 mrg # if (GCC_VERSION >= 3005) 199 1.1 mrg # define ATTRIBUTE_SENTINEL __attribute__ ((__sentinel__)) 200 1.1 mrg # else 201 1.1 mrg # define ATTRIBUTE_SENTINEL 202 1.1 mrg # endif /* GNUC >= 3.5 */ 203 1.1 mrg #endif /* ATTRIBUTE_SENTINEL */ 204 1.1 mrg 205 1.1 mrg 206 1.1 mrg #ifndef ATTRIBUTE_ALIGNED_ALIGNOF 207 1.1 mrg # if (GCC_VERSION >= 3000) 208 1.1 mrg # define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__ ((__aligned__ (__alignof__ (m)))) 209 1.1 mrg # else 210 1.1 mrg # define ATTRIBUTE_ALIGNED_ALIGNOF(m) 211 1.1 mrg # endif /* GNUC >= 3.0 */ 212 1.1 mrg #endif /* ATTRIBUTE_ALIGNED_ALIGNOF */ 213 1.1 mrg 214 1.2 christos /* Useful for structures whose layout must match some binary specification 215 1.1 mrg regardless of the alignment and padding qualities of the compiler. */ 216 1.1 mrg #ifndef ATTRIBUTE_PACKED 217 1.1 mrg # define ATTRIBUTE_PACKED __attribute__ ((packed)) 218 1.1 mrg #endif 219 1.1 mrg 220 1.1 mrg /* Attribute `hot' and `cold' was valid as of gcc 4.3. */ 221 1.1 mrg #ifndef ATTRIBUTE_COLD 222 1.1 mrg # if (GCC_VERSION >= 4003) 223 1.1 mrg # define ATTRIBUTE_COLD __attribute__ ((__cold__)) 224 1.1 mrg # else 225 1.1 mrg # define ATTRIBUTE_COLD 226 1.1 mrg # endif /* GNUC >= 4.3 */ 227 1.1 mrg #endif /* ATTRIBUTE_COLD */ 228 1.1 mrg #ifndef ATTRIBUTE_HOT 229 1.1 mrg # if (GCC_VERSION >= 4003) 230 1.1 mrg # define ATTRIBUTE_HOT __attribute__ ((__hot__)) 231 1.1 mrg # else 232 1.1 mrg # define ATTRIBUTE_HOT 233 1.1 mrg # endif /* GNUC >= 4.3 */ 234 1.1 mrg #endif /* ATTRIBUTE_HOT */ 235 1.1 mrg 236 1.2 christos /* Attribute 'no_sanitize_undefined' was valid as of gcc 4.9. */ 237 1.2 christos #ifndef ATTRIBUTE_NO_SANITIZE_UNDEFINED 238 1.2 christos # if (GCC_VERSION >= 4009) 239 1.2 christos # define ATTRIBUTE_NO_SANITIZE_UNDEFINED __attribute__ ((no_sanitize_undefined)) 240 1.2 christos # else 241 1.2 christos # define ATTRIBUTE_NO_SANITIZE_UNDEFINED 242 1.2 christos # endif /* GNUC >= 4.9 */ 243 1.2 christos #endif /* ATTRIBUTE_NO_SANITIZE_UNDEFINED */ 244 1.2 christos 245 1.2 christos /* Attribute 'nonstring' was valid as of gcc 8. */ 246 1.2 christos #ifndef ATTRIBUTE_NONSTRING 247 1.2 christos # if GCC_VERSION >= 8000 248 1.2 christos # define ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__)) 249 1.2 christos # else 250 1.2 christos # define ATTRIBUTE_NONSTRING 251 1.2 christos # endif 252 1.2 christos #endif 253 1.2 christos 254 1.2 christos /* Attribute `alloc_size' was valid as of gcc 4.3. */ 255 1.2 christos #ifndef ATTRIBUTE_RESULT_SIZE_1 256 1.2 christos # if (GCC_VERSION >= 4003) 257 1.2 christos # define ATTRIBUTE_RESULT_SIZE_1 __attribute__ ((alloc_size (1))) 258 1.2 christos # else 259 1.2 christos # define ATTRIBUTE_RESULT_SIZE_1 260 1.2 christos #endif 261 1.2 christos #endif 262 1.2 christos 263 1.2 christos #ifndef ATTRIBUTE_RESULT_SIZE_2 264 1.2 christos # if (GCC_VERSION >= 4003) 265 1.2 christos # define ATTRIBUTE_RESULT_SIZE_2 __attribute__ ((alloc_size (2))) 266 1.2 christos # else 267 1.2 christos # define ATTRIBUTE_RESULT_SIZE_2 268 1.2 christos #endif 269 1.2 christos #endif 270 1.2 christos 271 1.2 christos #ifndef ATTRIBUTE_RESULT_SIZE_1_2 272 1.2 christos # if (GCC_VERSION >= 4003) 273 1.2 christos # define ATTRIBUTE_RESULT_SIZE_1_2 __attribute__ ((alloc_size (1, 2))) 274 1.2 christos # else 275 1.2 christos # define ATTRIBUTE_RESULT_SIZE_1_2 276 1.2 christos #endif 277 1.2 christos #endif 278 1.2 christos 279 1.2 christos /* Attribute `warn_unused_result' was valid as of gcc 3.3. */ 280 1.2 christos #ifndef ATTRIBUTE_WARN_UNUSED_RESULT 281 1.2 christos # if GCC_VERSION >= 3003 282 1.4 mrg # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__)) 283 1.2 christos # else 284 1.2 christos # define ATTRIBUTE_WARN_UNUSED_RESULT 285 1.2 christos # endif 286 1.2 christos #endif 287 1.2 christos 288 1.1 mrg /* We use __extension__ in some places to suppress -pedantic warnings 289 1.1 mrg about GCC extensions. This feature didn't work properly before 290 1.1 mrg gcc 2.8. */ 291 1.2 christos #if GCC_VERSION < 2008 && !defined(__extension__) 292 1.1 mrg #define __extension__ 293 1.1 mrg #endif 294 1.1 mrg 295 1.1 mrg /* This is used to declare a const variable which should be visible 296 1.1 mrg outside of the current compilation unit. Use it as 297 1.1 mrg EXPORTED_CONST int i = 1; 298 1.1 mrg This is because the semantics of const are different in C and C++. 299 1.1 mrg "extern const" is permitted in C but it looks strange, and gcc 300 1.1 mrg warns about it when -Wc++-compat is not used. */ 301 1.1 mrg #ifdef __cplusplus 302 1.1 mrg #define EXPORTED_CONST extern const 303 1.1 mrg #else 304 1.1 mrg #define EXPORTED_CONST const 305 1.1 mrg #endif 306 1.1 mrg 307 1.2 christos /* Be conservative and only use enum bitfields with C++ or GCC. 308 1.2 christos FIXME: provide a complete autoconf test for buggy enum bitfields. */ 309 1.2 christos 310 1.2 christos #ifdef __cplusplus 311 1.2 christos #define ENUM_BITFIELD(TYPE) enum TYPE 312 1.2 christos #elif (GCC_VERSION > 2000) 313 1.2 christos #define ENUM_BITFIELD(TYPE) __extension__ enum TYPE 314 1.2 christos #else 315 1.2 christos #define ENUM_BITFIELD(TYPE) unsigned int 316 1.2 christos #endif 317 1.2 christos 318 1.3 mrg #if defined(__cplusplus) && __cpp_constexpr >= 200704 319 1.2 christos #define CONSTEXPR constexpr 320 1.2 christos #else 321 1.2 christos #define CONSTEXPR 322 1.2 christos #endif 323 1.2 christos 324 1.2 christos /* A macro to disable the copy constructor and assignment operator. 325 1.2 christos When building with C++11 and above, the methods are explicitly 326 1.2 christos deleted, causing a compile-time error if something tries to copy. 327 1.2 christos For C++03, this just declares the methods, causing a link-time 328 1.2 christos error if the methods end up called (assuming you don't 329 1.2 christos define them). For C++03, for best results, place the macro 330 1.2 christos under the private: access specifier, like this, 331 1.2 christos 332 1.2 christos class name_lookup 333 1.2 christos { 334 1.2 christos private: 335 1.2 christos DISABLE_COPY_AND_ASSIGN (name_lookup); 336 1.2 christos }; 337 1.2 christos 338 1.2 christos so that most attempts at copy are caught at compile-time. */ 339 1.2 christos 340 1.3 mrg #if defined(__cplusplus) && __cplusplus >= 201103 341 1.2 christos #define DISABLE_COPY_AND_ASSIGN(TYPE) \ 342 1.2 christos TYPE (const TYPE&) = delete; \ 343 1.2 christos void operator= (const TYPE &) = delete 344 1.2 christos #else 345 1.2 christos #define DISABLE_COPY_AND_ASSIGN(TYPE) \ 346 1.2 christos TYPE (const TYPE&); \ 347 1.2 christos void operator= (const TYPE &) 348 1.2 christos #endif /* __cplusplus >= 201103 */ 349 1.2 christos 350 1.1 mrg #ifdef __cplusplus 351 1.1 mrg } 352 1.1 mrg #endif 353 1.1 mrg 354 1.1 mrg #endif /* ansidecl.h */ 355