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 /*---------------------------------------------------------------------------- 15 | The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler. 16 | The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h. 17 *----------------------------------------------------------------------------*/ 18 #if defined(__GNUC__) && defined(__GNUC_MINOR__) 19 # define QEMU_GNUC_PREREQ(maj, min) \ 20 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 21 #else 22 # define QEMU_GNUC_PREREQ(maj, min) 0 23 #endif 24 25 #define QEMU_NORETURN __attribute__ ((__noreturn__)) 26 27 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 28 29 #define QEMU_SENTINEL __attribute__((sentinel)) 30 31 #if QEMU_GNUC_PREREQ(4, 3) 32 #define QEMU_ARTIFICIAL __attribute__((always_inline, artificial)) 33 #else 34 #define QEMU_ARTIFICIAL 35 #endif 36 37 #if defined(_WIN32) 38 # define QEMU_PACKED __attribute__((gcc_struct, packed)) 39 #else 40 # define QEMU_PACKED __attribute__((packed)) 41 #endif 42 43 #define QEMU_ALIGNED(X) __attribute__((aligned(X))) 44 45 #ifndef glue 46 #define xglue(x, y) x ## y 47 #define glue(x, y) xglue(x, y) 48 #define stringify(s) tostring(s) 49 #define tostring(s) #s 50 #endif 51 52 #ifndef likely 53 #if __GNUC__ < 3 54 #define __builtin_expect(x, n) (x) 55 #endif 56 57 #define likely(x) __builtin_expect(!!(x), 1) 58 #define unlikely(x) __builtin_expect(!!(x), 0) 59 #endif 60 61 #ifndef container_of 62 #define container_of(ptr, type, member) ({ \ 63 const typeof(((type *) 0)->member) *__mptr = (ptr); \ 64 (type *) ((char *) __mptr - offsetof(type, member));}) 65 #endif 66 67 /* Convert from a base type to a parent type, with compile time checking. */ 68 #ifdef __GNUC__ 69 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ 70 char __attribute__((unused)) offset_must_be_zero[ \ 71 -offsetof(type, field)]; \ 72 container_of(dev, type, field);})) 73 #else 74 #define DO_UPCAST(type, field, dev) container_of(dev, type, field) 75 #endif 76 77 #define typeof_field(type, field) typeof(((type *)0)->field) 78 #define type_check(t1,t2) ((t1*)0 - (t2*)0) 79 80 #define QEMU_BUILD_BUG_ON_STRUCT(x) \ 81 struct { \ 82 int:(x) ? -1 : 1; \ 83 } 84 85 #if defined(CONFIG_STATIC_ASSERT) 86 #define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x) 87 #elif defined(__COUNTER__) 88 #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ 89 glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused)) 90 #else 91 #define QEMU_BUILD_BUG_ON(x) 92 #endif 93 94 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ 95 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x))) 96 97 #if defined __GNUC__ 98 # if !QEMU_GNUC_PREREQ(4, 4) 99 /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */ 100 # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m))) 101 # else 102 /* Use gnu_printf when supported (qemu uses standard format strings). */ 103 # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m))) 104 # if defined(_WIN32) 105 /* Map __printf__ to __gnu_printf__ because we want standard format strings 106 * even when MinGW or GLib include files use __printf__. */ 107 # define __printf__ __gnu_printf__ 108 # endif 109 # endif 110 #else 111 #define GCC_FMT_ATTR(n, m) 112 #endif 113 114 #endif /* COMPILER_H */ 115