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