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