xref: /qemu/subprojects/libvduse/include/compiler.h (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
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 #define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
11 
12 /* HOST_LONG_BITS is the size of a native pointer in bits. */
13 #define HOST_LONG_BITS (__SIZEOF_POINTER__ * 8)
14 
15 #if defined __clang_analyzer__ || defined __COVERITY__
16 #define QEMU_STATIC_ANALYSIS 1
17 #endif
18 
19 #ifdef __cplusplus
20 #define QEMU_EXTERN_C extern "C"
21 #else
22 #define QEMU_EXTERN_C extern
23 #endif
24 
25 #define QEMU_PACKED __attribute__((packed))
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 /* Expands into an identifier stemN, where N is another number each time */
36 #define MAKE_IDENTIFIER(stem) glue(stem, __COUNTER__)
37 
38 #ifndef likely
39 #define likely(x)   __builtin_expect(!!(x), 1)
40 #define unlikely(x)   __builtin_expect(!!(x), 0)
41 #endif
42 
43 #ifndef container_of
44 #define container_of(ptr, type, member) ({                      \
45         const typeof(((type *) 0)->member) *__mptr = (ptr);     \
46         (type *) ((char *) __mptr - offsetof(type, member));})
47 #endif
48 
49 #define sizeof_field(type, field) sizeof(((type *)0)->field)
50 
51 /*
52  * Calculate the number of bytes up to and including the given 'field' of
53  * 'container'.
54  */
55 #define endof(container, field) \
56     (offsetof(container, field) + sizeof_field(container, field))
57 
58 /* Convert from a base type to a parent type, with compile time checking.  */
59 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
60     char __attribute__((unused)) offset_must_be_zero[ \
61         -offsetof(type, field)]; \
62     container_of(dev, type, field);}))
63 
64 #define typeof_field(type, field) typeof(((type *)0)->field)
65 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
66 
67 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
68     struct { \
69         int:(x) ? -1 : 1; \
70     }
71 
72 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
73 
74 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
75 
76 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
77                                    sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
78 
79 #if !defined(__clang__) && defined(_WIN32)
80 /*
81  * Map __printf__ to __gnu_printf__ because we want standard format strings even
82  * when MinGW or GLib include files use __printf__.
83  */
84 # define __printf__ __gnu_printf__
85 #endif
86 
87 #ifndef __has_warning
88 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
89 #endif
90 
91 #ifndef __has_feature
92 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
93 #endif
94 
95 #ifndef __has_builtin
96 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
97 #endif
98 
99 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
100 #define HAS_ASSUME_ALIGNED
101 #endif
102 
103 #ifndef __has_attribute
104 #define __has_attribute(x) 0 /* compatibility with older GCC */
105 #endif
106 
107 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
108 # define QEMU_SANITIZE_ADDRESS 1
109 #endif
110 
111 #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
112 # define QEMU_SANITIZE_THREAD 1
113 #endif
114 
115 /*
116  * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
117  * versions we support have the "flatten" attribute. Clang may not have the
118  * "flatten" attribute but always has __has_attribute() to check for it.
119  */
120 #if __has_attribute(flatten) || !defined(__clang__)
121 # define QEMU_FLATTEN __attribute__((flatten))
122 #else
123 # define QEMU_FLATTEN
124 #endif
125 
126 /*
127  * If __attribute__((error)) is present, use it to produce an error at
128  * compile time.  Otherwise, one must wait for the linker to diagnose
129  * the missing symbol.
130  */
131 #if __has_attribute(error)
132 # define QEMU_ERROR(X) __attribute__((error(X)))
133 #else
134 # define QEMU_ERROR(X)
135 #endif
136 
137 /*
138  * The nonstring variable attribute specifies that an object or member
139  * declaration with type array of char or pointer to char is intended
140  * to store character arrays that do not necessarily contain a terminating
141  * NUL character. This is useful in detecting uses of such arrays or pointers
142  * with functions that expect NUL-terminated strings, and to avoid warnings
143  * when such an array or pointer is used as an argument to a bounded string
144  * manipulation function such as strncpy.
145  */
146 #if __has_attribute(nonstring)
147 # define QEMU_NONSTRING __attribute__((nonstring))
148 #else
149 # define QEMU_NONSTRING
150 #endif
151 
152 /*
153  * Forced inlining may be desired to encourage constant propagation
154  * of function parameters.  However, it can also make debugging harder,
155  * so disable it for a non-optimizing build.
156  */
157 #if defined(__OPTIMIZE__)
158 #define QEMU_ALWAYS_INLINE  __attribute__((always_inline))
159 #else
160 #define QEMU_ALWAYS_INLINE
161 #endif
162 
163 /**
164  * In most cases, normal "fallthrough" comments are good enough for
165  * switch-case statements, but sometimes the compiler has problems
166  * with those. In that case you can use QEMU_FALLTHROUGH instead.
167  */
168 #if __has_attribute(fallthrough)
169 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
170 #else
171 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
172 #endif
173 
174 #ifdef CONFIG_CFI
175 /*
176  * If CFI is enabled, use an attribute to disable cfi-icall on the following
177  * function
178  */
179 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
180 #else
181 /* If CFI is not enabled, use an empty define to not change the behavior */
182 #define QEMU_DISABLE_CFI
183 #endif
184 
185 /*
186  * Apple clang version 14 has a bug in its __builtin_subcll(); define
187  * BUILTIN_SUBCLL_BROKEN for the offending versions so we can avoid it.
188  * When a version of Apple clang which has this bug fixed is released
189  * we can add an upper bound to this check.
190  * See https://gitlab.com/qemu-project/qemu/-/issues/1631
191  * and https://gitlab.com/qemu-project/qemu/-/issues/1659 for details.
192  * The bug never made it into any upstream LLVM releases, only Apple ones.
193  */
194 #if defined(__apple_build_version__) && __clang_major__ >= 14
195 #define BUILTIN_SUBCLL_BROKEN
196 #endif
197 
198 #if __has_attribute(annotate)
199 #define QEMU_ANNOTATE(x) __attribute__((annotate(x)))
200 #else
201 #define QEMU_ANNOTATE(x)
202 #endif
203 
204 #if __has_attribute(used)
205 # define QEMU_USED __attribute__((used))
206 #else
207 # define QEMU_USED
208 #endif
209 
210 /*
211  * Ugly CPP trick that is like "defined FOO", but also works in C
212  * code.  Useful to replace #ifdef with "if" statements; assumes
213  * the symbol was defined with Meson's "config.set()", so it is empty
214  * if defined.
215  */
216 #define IS_ENABLED(x)                  IS_EMPTY(x)
217 
218 #define IS_EMPTY_JUNK_                 junk,
219 #define IS_EMPTY(value)                IS_EMPTY_(IS_EMPTY_JUNK_##value)
220 
221 /* Expands to either SECOND_ARG(junk, 1, 0) or SECOND_ARG(IS_EMPTY_JUNK_CONFIG_FOO 1, 0)  */
222 #define SECOND_ARG(first, second, ...) second
223 #define IS_EMPTY_(junk_maybecomma)     SECOND_ARG(junk_maybecomma 1, 0)
224 
225 #ifndef __cplusplus
226 /*
227  * Useful in macros that need to declare temporary variables.  For example,
228  * the variable that receives the old value of an atomically-accessed
229  * variable must be non-qualified, because atomic builtins return values
230  * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
231  *
232  * This macro has to handle types smaller than int manually, because of
233  * implicit promotion.  int and larger types, as well as pointers, can be
234  * converted to a non-qualified type just by applying a binary operator.
235  */
236 #define typeof_strip_qual(expr)                                                    \
237   typeof(                                                                          \
238     __builtin_choose_expr(                                                         \
239       __builtin_types_compatible_p(typeof(expr), bool) ||                          \
240         __builtin_types_compatible_p(typeof(expr), const bool) ||                  \
241         __builtin_types_compatible_p(typeof(expr), volatile bool) ||               \
242         __builtin_types_compatible_p(typeof(expr), const volatile bool),           \
243         (bool)1,                                                                   \
244     __builtin_choose_expr(                                                         \
245       __builtin_types_compatible_p(typeof(expr), signed char) ||                   \
246         __builtin_types_compatible_p(typeof(expr), const signed char) ||           \
247         __builtin_types_compatible_p(typeof(expr), volatile signed char) ||        \
248         __builtin_types_compatible_p(typeof(expr), const volatile signed char),    \
249         (signed char)1,                                                            \
250     __builtin_choose_expr(                                                         \
251       __builtin_types_compatible_p(typeof(expr), unsigned char) ||                 \
252         __builtin_types_compatible_p(typeof(expr), const unsigned char) ||         \
253         __builtin_types_compatible_p(typeof(expr), volatile unsigned char) ||      \
254         __builtin_types_compatible_p(typeof(expr), const volatile unsigned char),  \
255         (unsigned char)1,                                                          \
256     __builtin_choose_expr(                                                         \
257       __builtin_types_compatible_p(typeof(expr), signed short) ||                  \
258         __builtin_types_compatible_p(typeof(expr), const signed short) ||          \
259         __builtin_types_compatible_p(typeof(expr), volatile signed short) ||       \
260         __builtin_types_compatible_p(typeof(expr), const volatile signed short),   \
261         (signed short)1,                                                           \
262     __builtin_choose_expr(                                                         \
263       __builtin_types_compatible_p(typeof(expr), unsigned short) ||                \
264         __builtin_types_compatible_p(typeof(expr), const unsigned short) ||        \
265         __builtin_types_compatible_p(typeof(expr), volatile unsigned short) ||     \
266         __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
267         (unsigned short)1,                                                         \
268       (expr)+0))))))
269 #endif
270 
271 #endif /* COMPILER_H */
272