xref: /qemu/include/qemu/compiler.h (revision 2c533c54)
1 /* public domain */
2 
3 #ifndef COMPILER_H
4 #define COMPILER_H
5 
6 #if defined __clang_analyzer__ || defined __COVERITY__
7 #define QEMU_STATIC_ANALYSIS 1
8 #endif
9 
10 /*----------------------------------------------------------------------------
11 | The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler.
12 | The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h.
13 *----------------------------------------------------------------------------*/
14 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
15 # define QEMU_GNUC_PREREQ(maj, min) \
16          ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
17 #else
18 # define QEMU_GNUC_PREREQ(maj, min) 0
19 #endif
20 
21 #define QEMU_NORETURN __attribute__ ((__noreturn__))
22 
23 #if QEMU_GNUC_PREREQ(3, 4)
24 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
25 #else
26 #define QEMU_WARN_UNUSED_RESULT
27 #endif
28 
29 #if QEMU_GNUC_PREREQ(4, 0)
30 #define QEMU_SENTINEL __attribute__((sentinel))
31 #else
32 #define QEMU_SENTINEL
33 #endif
34 
35 #if QEMU_GNUC_PREREQ(4, 3)
36 #define QEMU_ARTIFICIAL __attribute__((always_inline, artificial))
37 #else
38 #define QEMU_ARTIFICIAL
39 #endif
40 
41 #if defined(_WIN32)
42 # define QEMU_PACKED __attribute__((gcc_struct, packed))
43 #else
44 # define QEMU_PACKED __attribute__((packed))
45 #endif
46 
47 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
48 
49 #ifndef glue
50 #define xglue(x, y) x ## y
51 #define glue(x, y) xglue(x, y)
52 #define stringify(s)	tostring(s)
53 #define tostring(s)	#s
54 #endif
55 
56 #ifndef likely
57 #if __GNUC__ < 3
58 #define __builtin_expect(x, n) (x)
59 #endif
60 
61 #define likely(x)   __builtin_expect(!!(x), 1)
62 #define unlikely(x)   __builtin_expect(!!(x), 0)
63 #endif
64 
65 #ifndef container_of
66 #define container_of(ptr, type, member) ({                      \
67         const typeof(((type *) 0)->member) *__mptr = (ptr);     \
68         (type *) ((char *) __mptr - offsetof(type, member));})
69 #endif
70 
71 /* Convert from a base type to a parent type, with compile time checking.  */
72 #ifdef __GNUC__
73 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
74     char __attribute__((unused)) offset_must_be_zero[ \
75         -offsetof(type, field)]; \
76     container_of(dev, type, field);}))
77 #else
78 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
79 #endif
80 
81 #define typeof_field(type, field) typeof(((type *)0)->field)
82 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
83 
84 #define QEMU_BUILD_BUG_ON(x) \
85     typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused));
86 
87 #if defined __GNUC__
88 # if !QEMU_GNUC_PREREQ(4, 4)
89    /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
90 #  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
91 # else
92    /* Use gnu_printf when supported (qemu uses standard format strings). */
93 #  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
94 #  if defined(_WIN32)
95     /* Map __printf__ to __gnu_printf__ because we want standard format strings
96      * even when MinGW or GLib include files use __printf__. */
97 #   define __printf__ __gnu_printf__
98 #  endif
99 # endif
100 #else
101 #define GCC_FMT_ATTR(n, m)
102 #endif
103 
104 #endif /* COMPILER_H */
105