xref: /qemu/include/qemu/compiler.h (revision 49f95221)
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_NORETURN __attribute__ ((__noreturn__))
26 
27 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
28 # define QEMU_PACKED __attribute__((gcc_struct, packed))
29 #else
30 # define QEMU_PACKED __attribute__((packed))
31 #endif
32 
33 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
34 
35 #ifndef glue
36 #define xglue(x, y) x ## y
37 #define glue(x, y) xglue(x, y)
38 #define stringify(s)	tostring(s)
39 #define tostring(s)	#s
40 #endif
41 
42 #ifndef likely
43 #define likely(x)   __builtin_expect(!!(x), 1)
44 #define unlikely(x)   __builtin_expect(!!(x), 0)
45 #endif
46 
47 #ifndef container_of
48 #define container_of(ptr, type, member) ({                      \
49         const typeof(((type *) 0)->member) *__mptr = (ptr);     \
50         (type *) ((char *) __mptr - offsetof(type, member));})
51 #endif
52 
53 #define sizeof_field(type, field) sizeof(((type *)0)->field)
54 
55 /*
56  * Calculate the number of bytes up to and including the given 'field' of
57  * 'container'.
58  */
59 #define endof(container, field) \
60     (offsetof(container, field) + sizeof_field(container, field))
61 
62 /* Convert from a base type to a parent type, with compile time checking.  */
63 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
64     char __attribute__((unused)) offset_must_be_zero[ \
65         -offsetof(type, field)]; \
66     container_of(dev, type, field);}))
67 
68 #define typeof_field(type, field) typeof(((type *)0)->field)
69 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
70 
71 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
72     struct { \
73         int:(x) ? -1 : 1; \
74     }
75 
76 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
77 
78 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
79 
80 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
81                                    sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
82 
83 #if !defined(__clang__) && defined(_WIN32)
84 /*
85  * Map __printf__ to __gnu_printf__ because we want standard format strings even
86  * when MinGW or GLib include files use __printf__.
87  */
88 # define __printf__ __gnu_printf__
89 #endif
90 
91 #ifndef __has_warning
92 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
93 #endif
94 
95 #ifndef __has_feature
96 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
97 #endif
98 
99 #ifndef __has_builtin
100 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
101 #endif
102 
103 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
104 #define HAS_ASSUME_ALIGNED
105 #endif
106 
107 #ifndef __has_attribute
108 #define __has_attribute(x) 0 /* compatibility with older GCC */
109 #endif
110 
111 /*
112  * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
113  * versions we support have the "flatten" attribute. Clang may not have the
114  * "flatten" attribute but always has __has_attribute() to check for it.
115  */
116 #if __has_attribute(flatten) || !defined(__clang__)
117 # define QEMU_FLATTEN __attribute__((flatten))
118 #else
119 # define QEMU_FLATTEN
120 #endif
121 
122 /*
123  * If __attribute__((error)) is present, use it to produce an error at
124  * compile time.  Otherwise, one must wait for the linker to diagnose
125  * the missing symbol.
126  */
127 #if __has_attribute(error)
128 # define QEMU_ERROR(X) __attribute__((error(X)))
129 #else
130 # define QEMU_ERROR(X)
131 #endif
132 
133 /*
134  * The nonstring variable attribute specifies that an object or member
135  * declaration with type array of char or pointer to char is intended
136  * to store character arrays that do not necessarily contain a terminating
137  * NUL character. This is useful in detecting uses of such arrays or pointers
138  * with functions that expect NUL-terminated strings, and to avoid warnings
139  * when such an array or pointer is used as an argument to a bounded string
140  * manipulation function such as strncpy.
141  */
142 #if __has_attribute(nonstring)
143 # define QEMU_NONSTRING __attribute__((nonstring))
144 #else
145 # define QEMU_NONSTRING
146 #endif
147 
148 /*
149  * Forced inlining may be desired to encourage constant propagation
150  * of function parameters.  However, it can also make debugging harder,
151  * so disable it for a non-optimizing build.
152  */
153 #if defined(__OPTIMIZE__)
154 #define QEMU_ALWAYS_INLINE  __attribute__((always_inline))
155 #else
156 #define QEMU_ALWAYS_INLINE
157 #endif
158 
159 /**
160  * qemu_build_not_reached()
161  *
162  * The compiler, during optimization, is expected to prove that a call
163  * to this function cannot be reached and remove it.  If the compiler
164  * supports QEMU_ERROR, this will be reported at compile time; otherwise
165  * this will be reported at link time due to the missing symbol.
166  */
167 extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
168     qemu_build_not_reached_always(void);
169 #if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
170 #define qemu_build_not_reached()  qemu_build_not_reached_always()
171 #else
172 #define qemu_build_not_reached()  g_assert_not_reached()
173 #endif
174 
175 /**
176  * In most cases, normal "fallthrough" comments are good enough for
177  * switch-case statements, but sometimes the compiler has problems
178  * with those. In that case you can use QEMU_FALLTHROUGH instead.
179  */
180 #if __has_attribute(fallthrough)
181 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
182 #else
183 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
184 #endif
185 
186 #ifdef CONFIG_CFI
187 /*
188  * If CFI is enabled, use an attribute to disable cfi-icall on the following
189  * function
190  */
191 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
192 #else
193 /* If CFI is not enabled, use an empty define to not change the behavior */
194 #define QEMU_DISABLE_CFI
195 #endif
196 
197 #endif /* COMPILER_H */
198