]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/compiler.h
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 #if QEMU_GNUC_PREREQ(3, 4)
28 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
30 #define QEMU_WARN_UNUSED_RESULT
33 #if QEMU_GNUC_PREREQ(4, 0)
34 #define QEMU_SENTINEL __attribute__((sentinel))
39 #if QEMU_GNUC_PREREQ(4, 3)
40 #define QEMU_ARTIFICIAL __attribute__((always_inline, artificial))
42 #define QEMU_ARTIFICIAL
46 # define QEMU_PACKED __attribute__((gcc_struct, packed))
48 # define QEMU_PACKED __attribute__((packed))
51 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
54 #define xglue(x, y) x ## y
55 #define glue(x, y) xglue(x, y)
56 #define stringify(s) tostring(s)
57 #define tostring(s) #s
62 #define __builtin_expect(x, n) (x)
65 #define likely(x) __builtin_expect(!!(x), 1)
66 #define unlikely(x) __builtin_expect(!!(x), 0)
70 #define container_of(ptr, type, member) ({ \
71 const typeof(((type *) 0)->member) *__mptr = (ptr); \
72 (type *) ((char *) __mptr - offsetof(type, member));})
75 /* Convert from a base type to a parent type, with compile time checking. */
77 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
78 char __attribute__((unused)) offset_must_be_zero[ \
79 -offsetof(type, field)]; \
80 container_of(dev, type, field);}))
82 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
85 #define typeof_field(type, field) typeof(((type *)0)->field)
86 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
88 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
94 #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
95 glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
97 #define QEMU_BUILD_BUG_ON(x)
100 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
101 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
104 # if !QEMU_GNUC_PREREQ(4, 4)
105 /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
106 # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
108 /* Use gnu_printf when supported (qemu uses standard format strings). */
109 # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
111 /* Map __printf__ to __gnu_printf__ because we want standard format strings
112 * even when MinGW or GLib include files use __printf__. */
113 # define __printf__ __gnu_printf__
117 #define GCC_FMT_ATTR(n, m)
120 #endif /* COMPILER_H */