]>
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 #if defined __clang_analyzer__ || defined __COVERITY__
11 #define QEMU_STATIC_ANALYSIS 1
15 #define QEMU_EXTERN_C extern "C"
17 #define QEMU_EXTERN_C extern
20 #define QEMU_NORETURN __attribute__ ((__noreturn__))
22 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
24 #define QEMU_SENTINEL __attribute__((sentinel))
26 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
27 # define QEMU_PACKED __attribute__((gcc_struct, packed))
29 # define QEMU_PACKED __attribute__((packed))
32 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
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
42 #define likely(x) __builtin_expect(!!(x), 1)
43 #define unlikely(x) __builtin_expect(!!(x), 0)
47 #define container_of(ptr, type, member) ({ \
48 const typeof(((type *) 0)->member) *__mptr = (ptr); \
49 (type *) ((char *) __mptr - offsetof(type, member));})
52 #define sizeof_field(type, field) sizeof(((type *)0)->field)
55 * Calculate the number of bytes up to and including the given 'field' of
58 #define endof(container, field) \
59 (offsetof(container, field) + sizeof_field(container, field))
61 /* Convert from a base type to a parent type, with compile time checking. */
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);}))
67 #define typeof_field(type, field) typeof(((type *)0)->field)
68 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
70 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
75 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
77 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
79 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
80 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
82 #if !defined(__clang__) && defined(_WIN32)
84 * Map __printf__ to __gnu_printf__ because we want standard format strings even
85 * when MinGW or GLib include files use __printf__.
87 # define __printf__ __gnu_printf__
91 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
95 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
99 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
102 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
103 #define HAS_ASSUME_ALIGNED
106 #ifndef __has_attribute
107 #define __has_attribute(x) 0 /* compatibility with older GCC */
111 * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
112 * versions we support have the "flatten" attribute. Clang may not have the
113 * "flatten" attribute but always has __has_attribute() to check for it.
115 #if __has_attribute(flatten) || !defined(__clang__)
116 # define QEMU_FLATTEN __attribute__((flatten))
118 # define QEMU_FLATTEN
122 * If __attribute__((error)) is present, use it to produce an error at
123 * compile time. Otherwise, one must wait for the linker to diagnose
124 * the missing symbol.
126 #if __has_attribute(error)
127 # define QEMU_ERROR(X) __attribute__((error(X)))
129 # define QEMU_ERROR(X)
133 * The nonstring variable attribute specifies that an object or member
134 * declaration with type array of char or pointer to char is intended
135 * to store character arrays that do not necessarily contain a terminating
136 * NUL character. This is useful in detecting uses of such arrays or pointers
137 * with functions that expect NUL-terminated strings, and to avoid warnings
138 * when such an array or pointer is used as an argument to a bounded string
139 * manipulation function such as strncpy.
141 #if __has_attribute(nonstring)
142 # define QEMU_NONSTRING __attribute__((nonstring))
144 # define QEMU_NONSTRING
148 * Forced inlining may be desired to encourage constant propagation
149 * of function parameters. However, it can also make debugging harder,
150 * so disable it for a non-optimizing build.
152 #if defined(__OPTIMIZE__)
153 #define QEMU_ALWAYS_INLINE __attribute__((always_inline))
155 #define QEMU_ALWAYS_INLINE
159 * qemu_build_not_reached()
161 * The compiler, during optimization, is expected to prove that a call
162 * to this function cannot be reached and remove it. If the compiler
163 * supports QEMU_ERROR, this will be reported at compile time; otherwise
164 * this will be reported at link time due to the missing symbol.
166 extern void QEMU_NORETURN
QEMU_ERROR("code path is reachable")
167 qemu_build_not_reached_always(void);
168 #if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
169 #define qemu_build_not_reached() qemu_build_not_reached_always()
171 #define qemu_build_not_reached() g_assert_not_reached()
175 * In most cases, normal "fallthrough" comments are good enough for
176 * switch-case statements, but sometimes the compiler has problems
177 * with those. In that case you can use QEMU_FALLTHROUGH instead.
179 #if __has_attribute(fallthrough)
180 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
182 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
187 * If CFI is enabled, use an attribute to disable cfi-icall on the following
190 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
192 /* If CFI is not enabled, use an empty define to not change the behavior */
193 #define QEMU_DISABLE_CFI
196 #endif /* COMPILER_H */