compiler.h 3.4 KB

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