1
2
3
4
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
56
57
58#define endof(container, field) \
59 (offsetof(container, field) + sizeof_field(container, field))
60
61
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#define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
76
77#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
78
79#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
80 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
81
82#if defined(__clang__)
83
84# define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
85#else
86
87# define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
88# if defined(_WIN32)
89
90
91
92
93# define __printf__ __gnu_printf__
94# endif
95#endif
96
97#ifndef __has_warning
98#define __has_warning(x) 0
99#endif
100
101#ifndef __has_feature
102#define __has_feature(x) 0
103#endif
104
105#ifndef __has_builtin
106#define __has_builtin(x) 0
107#endif
108
109#if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
110#define HAS_ASSUME_ALIGNED
111#endif
112
113#ifndef __has_attribute
114#define __has_attribute(x) 0
115#endif
116
117
118
119
120
121
122#if __has_attribute(flatten) || !defined(__clang__)
123# define QEMU_FLATTEN __attribute__((flatten))
124#else
125# define QEMU_FLATTEN
126#endif
127
128
129
130
131
132
133#if __has_attribute(error)
134# define QEMU_ERROR(X) __attribute__((error(X)))
135#else
136# define QEMU_ERROR(X)
137#endif
138
139
140
141
142
143
144
145
146
147
148#if __has_attribute(nonstring)
149# define QEMU_NONSTRING __attribute__((nonstring))
150#else
151# define QEMU_NONSTRING
152#endif
153
154
155
156
157
158
159#if defined(__OPTIMIZE__)
160#define QEMU_ALWAYS_INLINE __attribute__((always_inline))
161#else
162#define QEMU_ALWAYS_INLINE
163#endif
164
165
166
167
168
169
170
171
172
173extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
174 qemu_build_not_reached_always(void);
175#if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
176#define qemu_build_not_reached() qemu_build_not_reached_always()
177#else
178#define qemu_build_not_reached() g_assert_not_reached()
179#endif
180
181
182
183
184
185
186#if __has_attribute(fallthrough)
187# define QEMU_FALLTHROUGH __attribute__((fallthrough))
188#else
189# define QEMU_FALLTHROUGH do {} while (0)
190#endif
191
192#ifdef CONFIG_CFI
193
194
195
196
197#define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
198#else
199
200#define QEMU_DISABLE_CFI
201#endif
202
203#endif
204