qemu/include/qemu/compiler.h
<<
>>
Prefs
   1/* compiler.h: macros to abstract away compiler specifics
   2 *
   3 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   4 * See the COPYING file in the top-level directory.
   5 */
   6
   7#ifndef COMPILER_H
   8#define COMPILER_H
   9
  10#if defined __clang_analyzer__ || defined __COVERITY__
  11#define QEMU_STATIC_ANALYSIS 1
  12#endif
  13
  14#ifdef __cplusplus
  15#define QEMU_EXTERN_C extern "C"
  16#else
  17#define QEMU_EXTERN_C extern
  18#endif
  19
  20#define QEMU_NORETURN __attribute__ ((__noreturn__))
  21
  22#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  23
  24#define QEMU_SENTINEL __attribute__((sentinel))
  25
  26#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
  27# define QEMU_PACKED __attribute__((gcc_struct, packed))
  28#else
  29# define QEMU_PACKED __attribute__((packed))
  30#endif
  31
  32#define QEMU_ALIGNED(X) __attribute__((aligned(X)))
  33
  34#ifndef glue
  35#define xglue(x, y) x ## y
  36#define glue(x, y) xglue(x, y)
  37#define stringify(s)    tostring(s)
  38#define tostring(s)     #s
  39#endif
  40
  41#ifndef likely
  42#define likely(x)   __builtin_expect(!!(x), 1)
  43#define unlikely(x)   __builtin_expect(!!(x), 0)
  44#endif
  45
  46#ifndef container_of
  47#define container_of(ptr, type, member) ({                      \
  48        const typeof(((type *) 0)->member) *__mptr = (ptr);     \
  49        (type *) ((char *) __mptr - offsetof(type, member));})
  50#endif
  51
  52#define sizeof_field(type, field) sizeof(((type *)0)->field)
  53
  54/*
  55 * Calculate the number of bytes up to and including the given 'field' of
  56 * 'container'.
  57 */
  58#define endof(container, field) \
  59    (offsetof(container, field) + sizeof_field(container, field))
  60
  61/* Convert from a base type to a parent type, with compile time checking.  */
  62#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
  63    char __attribute__((unused)) offset_must_be_zero[ \
  64        -offsetof(type, field)]; \
  65    container_of(dev, type, field);}))
  66
  67#define typeof_field(type, field) typeof(((type *)0)->field)
  68#define type_check(t1,t2) ((t1*)0 - (t2*)0)
  69
  70#define QEMU_BUILD_BUG_ON_STRUCT(x) \
  71    struct { \
  72        int:(x) ? -1 : 1; \
  73    }
  74
  75/* QEMU_BUILD_BUG_MSG() emits the message given if _Static_assert is
  76 * supported; otherwise, it will be omitted from the compiler error
  77 * message (but as it remains present in the source code, it can still
  78 * be useful when debugging). */
  79#if defined(CONFIG_STATIC_ASSERT)
  80#define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
  81#elif defined(__COUNTER__)
  82#define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
  83    glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
  84#else
  85#define QEMU_BUILD_BUG_MSG(x, msg)
  86#endif
  87
  88#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
  89
  90#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
  91                                   sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
  92
  93#if defined(__clang__)
  94/* clang doesn't support gnu_printf, so use printf. */
  95# define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
  96#else
  97/* Use gnu_printf (qemu uses standard format strings). */
  98# define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
  99# if defined(_WIN32)
 100/*
 101 * Map __printf__ to __gnu_printf__ because we want standard format strings even
 102 * when MinGW or GLib include files use __printf__.
 103 */
 104#  define __printf__ __gnu_printf__
 105# endif
 106#endif
 107
 108#ifndef __has_warning
 109#define __has_warning(x) 0 /* compatibility with non-clang compilers */
 110#endif
 111
 112#ifndef __has_feature
 113#define __has_feature(x) 0 /* compatibility with non-clang compilers */
 114#endif
 115
 116#ifndef __has_builtin
 117#define __has_builtin(x) 0 /* compatibility with non-clang compilers */
 118#endif
 119
 120#if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
 121#define HAS_ASSUME_ALIGNED
 122#endif
 123
 124#ifndef __has_attribute
 125#define __has_attribute(x) 0 /* compatibility with older GCC */
 126#endif
 127
 128/*
 129 * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
 130 * versions we support have the "flatten" attribute. Clang may not have the
 131 * "flatten" attribute but always has __has_attribute() to check for it.
 132 */
 133#if __has_attribute(flatten) || !defined(__clang__)
 134# define QEMU_FLATTEN __attribute__((flatten))
 135#else
 136# define QEMU_FLATTEN
 137#endif
 138
 139/*
 140 * If __attribute__((error)) is present, use it to produce an error at
 141 * compile time.  Otherwise, one must wait for the linker to diagnose
 142 * the missing symbol.
 143 */
 144#if __has_attribute(error)
 145# define QEMU_ERROR(X) __attribute__((error(X)))
 146#else
 147# define QEMU_ERROR(X)
 148#endif
 149
 150/*
 151 * The nonstring variable attribute specifies that an object or member
 152 * declaration with type array of char or pointer to char is intended
 153 * to store character arrays that do not necessarily contain a terminating
 154 * NUL character. This is useful in detecting uses of such arrays or pointers
 155 * with functions that expect NUL-terminated strings, and to avoid warnings
 156 * when such an array or pointer is used as an argument to a bounded string
 157 * manipulation function such as strncpy.
 158 */
 159#if __has_attribute(nonstring)
 160# define QEMU_NONSTRING __attribute__((nonstring))
 161#else
 162# define QEMU_NONSTRING
 163#endif
 164
 165/*
 166 * Forced inlining may be desired to encourage constant propagation
 167 * of function parameters.  However, it can also make debugging harder,
 168 * so disable it for a non-optimizing build.
 169 */
 170#if defined(__OPTIMIZE__)
 171#define QEMU_ALWAYS_INLINE  __attribute__((always_inline))
 172#else
 173#define QEMU_ALWAYS_INLINE
 174#endif
 175
 176/* Implement C11 _Generic via GCC builtins.  Example:
 177 *
 178 *    QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x)
 179 *
 180 * The first argument is the discriminator.  The last is the default value.
 181 * The middle ones are tuples in "(type, expansion)" format.
 182 */
 183
 184/* First, find out the number of generic cases.  */
 185#define QEMU_GENERIC(x, ...) \
 186    QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
 187
 188/* There will be extra arguments, but they are not used.  */
 189#define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \
 190    QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
 191
 192/* Two more helper macros, this time to extract items from a parenthesized
 193 * list.
 194 */
 195#define QEMU_FIRST_(a, b) a
 196#define QEMU_SECOND_(a, b) b
 197
 198/* ... and a final one for the common part of the "recursion".  */
 199#define QEMU_GENERIC_IF(x, type_then, else_)                                   \
 200    __builtin_choose_expr(__builtin_types_compatible_p(x,                      \
 201                                                       QEMU_FIRST_ type_then), \
 202                          QEMU_SECOND_ type_then, else_)
 203
 204/* CPP poor man's "recursion".  */
 205#define QEMU_GENERIC1(x, a0, ...) (a0)
 206#define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__))
 207#define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__))
 208#define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__))
 209#define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__))
 210#define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__))
 211#define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__))
 212#define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__))
 213#define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
 214#define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
 215
 216/**
 217 * qemu_build_not_reached()
 218 *
 219 * The compiler, during optimization, is expected to prove that a call
 220 * to this function cannot be reached and remove it.  If the compiler
 221 * supports QEMU_ERROR, this will be reported at compile time; otherwise
 222 * this will be reported at link time due to the missing symbol.
 223 */
 224extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
 225    qemu_build_not_reached_always(void);
 226#if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
 227#define qemu_build_not_reached()  qemu_build_not_reached_always()
 228#else
 229#define qemu_build_not_reached()  g_assert_not_reached()
 230#endif
 231
 232/**
 233 * In most cases, normal "fallthrough" comments are good enough for
 234 * switch-case statements, but sometimes the compiler has problems
 235 * with those. In that case you can use QEMU_FALLTHROUGH instead.
 236 */
 237#if __has_attribute(fallthrough)
 238# define QEMU_FALLTHROUGH __attribute__((fallthrough))
 239#else
 240# define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
 241#endif
 242
 243#ifdef CONFIG_CFI
 244/*
 245 * If CFI is enabled, use an attribute to disable cfi-icall on the following
 246 * function
 247 */
 248#define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
 249#else
 250/* If CFI is not enabled, use an empty define to not change the behavior */
 251#define QEMU_DISABLE_CFI
 252#endif
 253
 254#endif /* COMPILER_H */
 255