]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/compiler.h
1 /* compiler.h: macros to abstract away compiler specifics
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.
10 #define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
12 /* HOST_LONG_BITS is the size of a native pointer in bits. */
13 #define HOST_LONG_BITS (__SIZEOF_POINTER__ * 8)
15 #if defined __clang_analyzer__ || defined __COVERITY__
16 #define QEMU_STATIC_ANALYSIS 1
20 #define QEMU_EXTERN_C extern "C"
22 #define QEMU_EXTERN_C extern
25 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
26 # define QEMU_PACKED __attribute__((gcc_struct, packed))
28 # define QEMU_PACKED __attribute__((packed))
31 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
34 #define xglue(x, y) x ## y
35 #define glue(x, y) xglue(x, y)
36 #define stringify(s) tostring(s)
37 #define tostring(s) #s
40 /* Expands into an identifier stemN, where N is another number each time */
41 #define MAKE_IDENTFIER(stem) glue(stem, __COUNTER__)
44 #define likely(x) __builtin_expect(!!(x), 1)
45 #define unlikely(x) __builtin_expect(!!(x), 0)
49 #define container_of(ptr, type, member) ({ \
50 const typeof(((type *) 0)->member) *__mptr = (ptr); \
51 (type *) ((char *) __mptr - offsetof(type, member));})
54 #define sizeof_field(type, field) sizeof(((type *)0)->field)
57 * Calculate the number of bytes up to and including the given 'field' of
60 #define endof(container, field) \
61 (offsetof(container, field) + sizeof_field(container, field))
63 /* Convert from a base type to a parent type, with compile time checking. */
64 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
65 char __attribute__((unused)) offset_must_be_zero[ \
66 -offsetof(type, field)]; \
67 container_of(dev, type, field);}))
69 #define typeof_field(type, field) typeof(((type *)0)->field)
70 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
72 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
77 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
79 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
81 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
82 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
84 #if !defined(__clang__) && defined(_WIN32)
86 * Map __printf__ to __gnu_printf__ because we want standard format strings even
87 * when MinGW or GLib include files use __printf__.
89 # define __printf__ __gnu_printf__
93 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
97 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
100 #ifndef __has_builtin
101 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
104 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
105 #define HAS_ASSUME_ALIGNED
108 #ifndef __has_attribute
109 #define __has_attribute(x) 0 /* compatibility with older GCC */
112 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
113 # define QEMU_SANITIZE_ADDRESS 1
116 #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
117 # define QEMU_SANITIZE_THREAD 1
121 * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
122 * versions we support have the "flatten" attribute. Clang may not have the
123 * "flatten" attribute but always has __has_attribute() to check for it.
125 #if __has_attribute(flatten) || !defined(__clang__)
126 # define QEMU_FLATTEN __attribute__((flatten))
128 # define QEMU_FLATTEN
132 * If __attribute__((error)) is present, use it to produce an error at
133 * compile time. Otherwise, one must wait for the linker to diagnose
134 * the missing symbol.
136 #if __has_attribute(error)
137 # define QEMU_ERROR(X) __attribute__((error(X)))
139 # define QEMU_ERROR(X)
143 * The nonstring variable attribute specifies that an object or member
144 * declaration with type array of char or pointer to char is intended
145 * to store character arrays that do not necessarily contain a terminating
146 * NUL character. This is useful in detecting uses of such arrays or pointers
147 * with functions that expect NUL-terminated strings, and to avoid warnings
148 * when such an array or pointer is used as an argument to a bounded string
149 * manipulation function such as strncpy.
151 #if __has_attribute(nonstring)
152 # define QEMU_NONSTRING __attribute__((nonstring))
154 # define QEMU_NONSTRING
158 * Forced inlining may be desired to encourage constant propagation
159 * of function parameters. However, it can also make debugging harder,
160 * so disable it for a non-optimizing build.
162 #if defined(__OPTIMIZE__)
163 #define QEMU_ALWAYS_INLINE __attribute__((always_inline))
165 #define QEMU_ALWAYS_INLINE
169 * In most cases, normal "fallthrough" comments are good enough for
170 * switch-case statements, but sometimes the compiler has problems
171 * with those. In that case you can use QEMU_FALLTHROUGH instead.
173 #if __has_attribute(fallthrough)
174 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
176 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
181 * If CFI is enabled, use an attribute to disable cfi-icall on the following
184 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
186 /* If CFI is not enabled, use an empty define to not change the behavior */
187 #define QEMU_DISABLE_CFI
191 * Apple clang version 14 has a bug in its __builtin_subcll(); define
192 * BUILTIN_SUBCLL_BROKEN for the offending versions so we can avoid it.
193 * When a version of Apple clang which has this bug fixed is released
194 * we can add an upper bound to this check.
195 * See https://gitlab.com/qemu-project/qemu/-/issues/1631
196 * and https://gitlab.com/qemu-project/qemu/-/issues/1659 for details.
197 * The bug never made it into any upstream LLVM releases, only Apple ones.
199 #if defined(__apple_build_version__) && __clang_major__ >= 14
200 #define BUILTIN_SUBCLL_BROKEN
203 #if __has_attribute(annotate)
204 #define QEMU_ANNOTATE(x) __attribute__((annotate(x)))
206 #define QEMU_ANNOTATE(x)
209 #endif /* COMPILER_H */