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