1 /* compiler.h: macros to abstract away compiler specifics
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.
10 #if defined __clang_analyzer__ || defined __COVERITY__
11 #define QEMU_STATIC_ANALYSIS 1
14 /*----------------------------------------------------------------------------
15 | The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler.
16 | The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h.
17 *----------------------------------------------------------------------------*/
18 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
19 # define QEMU_GNUC_PREREQ(maj, min) \
20 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
22 # define QEMU_GNUC_PREREQ(maj, min) 0
25 #define QEMU_NORETURN __attribute__ ((__noreturn__))
27 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
29 #define QEMU_SENTINEL __attribute__((sentinel))
31 #if QEMU_GNUC_PREREQ(4, 3)
32 #define QEMU_ARTIFICIAL __attribute__((always_inline, artificial))
34 #define QEMU_ARTIFICIAL
38 # define QEMU_PACKED __attribute__((gcc_struct, packed))
40 # define QEMU_PACKED __attribute__((packed))
43 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
46 #define xglue(x, y) x ## y
47 #define glue(x, y) xglue(x, y)
48 #define stringify(s) tostring(s)
49 #define tostring(s) #s
54 #define __builtin_expect(x, n) (x)
57 #define likely(x) __builtin_expect(!!(x), 1)
58 #define unlikely(x) __builtin_expect(!!(x), 0)
62 #define container_of(ptr, type, member) ({ \
63 const typeof(((type *) 0)->member) *__mptr = (ptr); \
64 (type *) ((char *) __mptr - offsetof(type, member));})
67 /* Convert from a base type to a parent type, with compile time checking. */
69 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
70 char __attribute__((unused)) offset_must_be_zero[ \
71 -offsetof(type, field)]; \
72 container_of(dev, type, field);}))
74 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
77 #define typeof_field(type, field) typeof(((type *)0)->field)
78 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
80 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
85 #if defined(CONFIG_STATIC_ASSERT)
86 #define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x)
87 #elif defined(__COUNTER__)
88 #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
89 glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
91 #define QEMU_BUILD_BUG_ON(x)
94 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
95 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
98 # if !QEMU_GNUC_PREREQ(4, 4)
99 /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
100 # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
102 /* Use gnu_printf when supported (qemu uses standard format strings). */
103 # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
105 /* Map __printf__ to __gnu_printf__ because we want standard format strings
106 * even when MinGW or GLib include files use __printf__. */
107 # define __printf__ __gnu_printf__
111 #define GCC_FMT_ATTR(n, m)
114 #endif /* COMPILER_H */