xref: /qemu/include/qemu/compiler.h (revision 138ca49a)
1 /* compiler.h: macros to abstract away compiler specifics
2  *
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.
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 #define QEMU_NORETURN __attribute__ ((__noreturn__))
15 
16 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
17 
18 #define QEMU_SENTINEL __attribute__((sentinel))
19 
20 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
21 # define QEMU_PACKED __attribute__((gcc_struct, packed))
22 #else
23 # define QEMU_PACKED __attribute__((packed))
24 #endif
25 
26 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
27 
28 #ifndef glue
29 #define xglue(x, y) x ## y
30 #define glue(x, y) xglue(x, y)
31 #define stringify(s)	tostring(s)
32 #define tostring(s)	#s
33 #endif
34 
35 #ifndef likely
36 #define likely(x)   __builtin_expect(!!(x), 1)
37 #define unlikely(x)   __builtin_expect(!!(x), 0)
38 #endif
39 
40 #ifndef container_of
41 #define container_of(ptr, type, member) ({                      \
42         const typeof(((type *) 0)->member) *__mptr = (ptr);     \
43         (type *) ((char *) __mptr - offsetof(type, member));})
44 #endif
45 
46 #define sizeof_field(type, field) sizeof(((type *)0)->field)
47 
48 /*
49  * Calculate the number of bytes up to and including the given 'field' of
50  * 'container'.
51  */
52 #define endof(container, field) \
53     (offsetof(container, field) + sizeof_field(container, field))
54 
55 /* Convert from a base type to a parent type, with compile time checking.  */
56 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
57     char __attribute__((unused)) offset_must_be_zero[ \
58         -offsetof(type, field)]; \
59     container_of(dev, type, field);}))
60 
61 #define typeof_field(type, field) typeof(((type *)0)->field)
62 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
63 
64 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
65     struct { \
66         int:(x) ? -1 : 1; \
67     }
68 
69 /* QEMU_BUILD_BUG_MSG() emits the message given if _Static_assert is
70  * supported; otherwise, it will be omitted from the compiler error
71  * message (but as it remains present in the source code, it can still
72  * be useful when debugging). */
73 #if defined(CONFIG_STATIC_ASSERT)
74 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
75 #elif defined(__COUNTER__)
76 #define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
77     glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
78 #else
79 #define QEMU_BUILD_BUG_MSG(x, msg)
80 #endif
81 
82 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
83 
84 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
85                                    sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
86 
87 #if defined(__clang__)
88 /* clang doesn't support gnu_printf, so use printf. */
89 # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
90 #else
91 /* Use gnu_printf (qemu uses standard format strings). */
92 # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
93 # if defined(_WIN32)
94 /*
95  * Map __printf__ to __gnu_printf__ because we want standard format strings even
96  * when MinGW or GLib include files use __printf__.
97  */
98 #  define __printf__ __gnu_printf__
99 # endif
100 #endif
101 
102 #ifndef __has_warning
103 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
104 #endif
105 
106 #ifndef __has_feature
107 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
108 #endif
109 
110 #ifndef __has_builtin
111 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
112 #endif
113 
114 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
115 #define HAS_ASSUME_ALIGNED
116 #endif
117 
118 #ifndef __has_attribute
119 #define __has_attribute(x) 0 /* compatibility with older GCC */
120 #endif
121 
122 /*
123  * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
124  * versions we support have the "flatten" attribute. Clang may not have the
125  * "flatten" attribute but always has __has_attribute() to check for it.
126  */
127 #if __has_attribute(flatten) || !defined(__clang__)
128 # define QEMU_FLATTEN __attribute__((flatten))
129 #else
130 # define QEMU_FLATTEN
131 #endif
132 
133 /*
134  * If __attribute__((error)) is present, use it to produce an error at
135  * compile time.  Otherwise, one must wait for the linker to diagnose
136  * the missing symbol.
137  */
138 #if __has_attribute(error)
139 # define QEMU_ERROR(X) __attribute__((error(X)))
140 #else
141 # define QEMU_ERROR(X)
142 #endif
143 
144 /*
145  * The nonstring variable attribute specifies that an object or member
146  * declaration with type array of char or pointer to char is intended
147  * to store character arrays that do not necessarily contain a terminating
148  * NUL character. This is useful in detecting uses of such arrays or pointers
149  * with functions that expect NUL-terminated strings, and to avoid warnings
150  * when such an array or pointer is used as an argument to a bounded string
151  * manipulation function such as strncpy.
152  */
153 #if __has_attribute(nonstring)
154 # define QEMU_NONSTRING __attribute__((nonstring))
155 #else
156 # define QEMU_NONSTRING
157 #endif
158 
159 /*
160  * Forced inlining may be desired to encourage constant propagation
161  * of function parameters.  However, it can also make debugging harder,
162  * so disable it for a non-optimizing build.
163  */
164 #if defined(__OPTIMIZE__)
165 #define QEMU_ALWAYS_INLINE  __attribute__((always_inline))
166 #else
167 #define QEMU_ALWAYS_INLINE
168 #endif
169 
170 /* Implement C11 _Generic via GCC builtins.  Example:
171  *
172  *    QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x)
173  *
174  * The first argument is the discriminator.  The last is the default value.
175  * The middle ones are tuples in "(type, expansion)" format.
176  */
177 
178 /* First, find out the number of generic cases.  */
179 #define QEMU_GENERIC(x, ...) \
180     QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
181 
182 /* There will be extra arguments, but they are not used.  */
183 #define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \
184     QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
185 
186 /* Two more helper macros, this time to extract items from a parenthesized
187  * list.
188  */
189 #define QEMU_FIRST_(a, b) a
190 #define QEMU_SECOND_(a, b) b
191 
192 /* ... and a final one for the common part of the "recursion".  */
193 #define QEMU_GENERIC_IF(x, type_then, else_)                                   \
194     __builtin_choose_expr(__builtin_types_compatible_p(x,                      \
195                                                        QEMU_FIRST_ type_then), \
196                           QEMU_SECOND_ type_then, else_)
197 
198 /* CPP poor man's "recursion".  */
199 #define QEMU_GENERIC1(x, a0, ...) (a0)
200 #define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__))
201 #define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__))
202 #define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__))
203 #define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__))
204 #define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__))
205 #define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__))
206 #define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__))
207 #define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
208 #define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
209 
210 /**
211  * qemu_build_not_reached()
212  *
213  * The compiler, during optimization, is expected to prove that a call
214  * to this function cannot be reached and remove it.  If the compiler
215  * supports QEMU_ERROR, this will be reported at compile time; otherwise
216  * this will be reported at link time due to the missing symbol.
217  */
218 #if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
219 extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
220     qemu_build_not_reached(void);
221 #else
222 #define qemu_build_not_reached()  g_assert_not_reached()
223 #endif
224 
225 /**
226  * In most cases, normal "fallthrough" comments are good enough for
227  * switch-case statements, but sometimes the compiler has problems
228  * with those. In that case you can use QEMU_FALLTHROUGH instead.
229  */
230 #if __has_attribute(fallthrough)
231 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
232 #else
233 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
234 #endif
235 
236 #ifdef CONFIG_CFI
237 /*
238  * If CFI is enabled, use an attribute to disable cfi-icall on the following
239  * function
240  */
241 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
242 #else
243 /* If CFI is not enabled, use an empty define to not change the behavior */
244 #define QEMU_DISABLE_CFI
245 #endif
246 
247 #endif /* COMPILER_H */
248