compat_compiler.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_COMPAT_COMPILER_H
  6. #define TOR_COMPAT_COMPILER_H
  7. #include "orconfig.h"
  8. #if defined(__has_feature)
  9. # if __has_feature(address_sanitizer)
  10. /* Some of the fancy glibc strcmp() macros include references to memory that
  11. * clang rejects because it is off the end of a less-than-3. Clang hates this,
  12. * even though those references never actually happen. */
  13. # undef strcmp
  14. #endif /* __has_feature(address_sanitizer) */
  15. #endif /* defined(__has_feature) */
  16. #ifndef NULL_REP_IS_ZERO_BYTES
  17. #error "It seems your platform does not represent NULL as zero. We can't cope."
  18. #endif
  19. #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
  20. #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
  21. #endif
  22. #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
  23. #error "It seems that you encode characters in something other than ASCII."
  24. #endif
  25. /* GCC can check printf and scanf types on arbitrary functions. */
  26. #ifdef __GNUC__
  27. #define CHECK_PRINTF(formatIdx, firstArg) \
  28. __attribute__ ((format(printf, formatIdx, firstArg)))
  29. #else
  30. #define CHECK_PRINTF(formatIdx, firstArg)
  31. #endif /* defined(__GNUC__) */
  32. #ifdef __GNUC__
  33. #define CHECK_SCANF(formatIdx, firstArg) \
  34. __attribute__ ((format(scanf, formatIdx, firstArg)))
  35. #else
  36. #define CHECK_SCANF(formatIdx, firstArg)
  37. #endif /* defined(__GNUC__) */
  38. /* What GCC do we have? */
  39. #ifdef __GNUC__
  40. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  41. #else
  42. #define GCC_VERSION 0
  43. #endif
  44. /* Temporarily enable and disable warnings. */
  45. #ifdef __GNUC__
  46. # define PRAGMA_STRINGIFY_(s) #s
  47. # define PRAGMA_JOIN_STRINGIFY_(a,b) PRAGMA_STRINGIFY_(a ## b)
  48. /* Support for macro-generated pragmas (c99) */
  49. # define PRAGMA_(x) _Pragma (#x)
  50. # ifdef __clang__
  51. # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
  52. # else
  53. # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
  54. # endif
  55. # if defined(__clang__) || GCC_VERSION >= 406
  56. /* we have push/pop support */
  57. # define DISABLE_GCC_WARNING(warningopt) \
  58. PRAGMA_DIAGNOSTIC_(push) \
  59. PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
  60. # define ENABLE_GCC_WARNING(warningopt) \
  61. PRAGMA_DIAGNOSTIC_(pop)
  62. #else /* !(defined(__clang__) || GCC_VERSION >= 406) */
  63. /* older version of gcc: no push/pop support. */
  64. # define DISABLE_GCC_WARNING(warningopt) \
  65. PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
  66. # define ENABLE_GCC_WARNING(warningopt) \
  67. PRAGMA_DIAGNOSTIC_(warning PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
  68. #endif /* defined(__clang__) || GCC_VERSION >= 406 */
  69. #else /* !(defined(__GNUC__)) */
  70. /* not gcc at all */
  71. # define DISABLE_GCC_WARNING(warning)
  72. # define ENABLE_GCC_WARNING(warning)
  73. #endif /* defined(__GNUC__) */
  74. /* inline is __inline on windows. */
  75. #ifdef _WIN32
  76. #define inline __inline
  77. #endif
  78. /* Try to get a reasonable __func__ substitute in place. */
  79. #if defined(_MSC_VER)
  80. #define __func__ __FUNCTION__
  81. #else
  82. /* For platforms where autoconf works, make sure __func__ is defined
  83. * sanely. */
  84. #ifndef HAVE_MACRO__func__
  85. #ifdef HAVE_MACRO__FUNCTION__
  86. #define __func__ __FUNCTION__
  87. #elif HAVE_MACRO__FUNC__
  88. #define __func__ __FUNC__
  89. #else
  90. #define __func__ "???"
  91. #endif /* defined(HAVE_MACRO__FUNCTION__) || ... */
  92. #endif /* !defined(HAVE_MACRO__func__) */
  93. #endif /* defined(_MSC_VER) */
  94. #define U64_TO_DBL(x) ((double) (x))
  95. #define DBL_TO_U64(x) ((uint64_t) (x))
  96. #ifdef ENUM_VALS_ARE_SIGNED
  97. #define ENUM_BF(t) unsigned
  98. #else
  99. /** Wrapper for having a bitfield of an enumerated type. Where possible, we
  100. * just use the enumerated type (so the compiler can help us and notice
  101. * problems), but if enumerated types are unsigned, we must use unsigned,
  102. * so that the loss of precision doesn't make large values negative. */
  103. #define ENUM_BF(t) t
  104. #endif /* defined(ENUM_VALS_ARE_SIGNED) */
  105. /* GCC has several useful attributes. */
  106. #if defined(__GNUC__) && __GNUC__ >= 3
  107. #define ATTR_NORETURN __attribute__((noreturn))
  108. #define ATTR_CONST __attribute__((const))
  109. #define ATTR_MALLOC __attribute__((malloc))
  110. #define ATTR_NORETURN __attribute__((noreturn))
  111. #define ATTR_WUR __attribute__((warn_unused_result))
  112. /* Alas, nonnull is not at present a good idea for us. We'd like to get
  113. * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
  114. * spottily), but we don't want to tell the compiler to make optimizations
  115. * with the assumption that the argument can't be NULL (since this would make
  116. * many of our checks go away, and make our code less robust against
  117. * programming errors). Unfortunately, nonnull currently does both of these
  118. * things, and there's no good way to split them up.
  119. *
  120. * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
  121. #define ATTR_NONNULL(x)
  122. #define ATTR_UNUSED __attribute__ ((unused))
  123. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  124. * of <b>exp</b> will probably be true.
  125. *
  126. * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
  127. * except that it tells the compiler that the branch will be taken most of the
  128. * time. This can generate slightly better code with some CPUs.
  129. */
  130. #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
  131. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  132. * of <b>exp</b> will probably be false.
  133. *
  134. * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
  135. * except that it tells the compiler that the branch will usually not be
  136. * taken. This can generate slightly better code with some CPUs.
  137. */
  138. #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
  139. #else /* !(defined(__GNUC__) && __GNUC__ >= 3) */
  140. #define ATTR_NORETURN
  141. #define ATTR_CONST
  142. #define ATTR_MALLOC
  143. #define ATTR_NORETURN
  144. #define ATTR_NONNULL(x)
  145. #define ATTR_UNUSED
  146. #define ATTR_WUR
  147. #define PREDICT_LIKELY(exp) (exp)
  148. #define PREDICT_UNLIKELY(exp) (exp)
  149. #endif /* defined(__GNUC__) && __GNUC__ >= 3 */
  150. /** Expands to a syntactically valid empty statement. */
  151. #define STMT_NIL (void)0
  152. /** Expands to a syntactically valid empty statement, explicitly (void)ing its
  153. * argument. */
  154. #define STMT_VOID(a) while (0) { (void)(a); }
  155. #ifdef __GNUC__
  156. /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
  157. * the macro can be used as if it were a single C statement. */
  158. #define STMT_BEGIN (void) ({
  159. #define STMT_END })
  160. #elif defined(sun) || defined(__sun__)
  161. #define STMT_BEGIN if (1) {
  162. #define STMT_END } else STMT_NIL
  163. #else
  164. #define STMT_BEGIN do {
  165. #define STMT_END } while (0)
  166. #endif /* defined(__GNUC__) || ... */
  167. /* Some tools (like coccinelle) don't like to see operators as macro
  168. * arguments. */
  169. #define OP_LT <
  170. #define OP_GT >
  171. #define OP_GE >=
  172. #define OP_LE <=
  173. #define OP_EQ ==
  174. #define OP_NE !=
  175. #ifdef _MSC_VER
  176. /** Casts the uint64_t value in <b>a</b> to the right type for an argument
  177. * to printf. */
  178. #define U64_PRINTF_ARG(a) (a)
  179. /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
  180. * to scanf. */
  181. #define U64_SCANF_ARG(a) (a)
  182. /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
  183. #define U64_LITERAL(n) (n ## ui64)
  184. #define I64_PRINTF_ARG(a) (a)
  185. #define I64_SCANF_ARG(a) (a)
  186. #define I64_LITERAL(n) (n ## i64)
  187. #else /* !(defined(_MSC_VER)) */
  188. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  189. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  190. #define U64_LITERAL(n) (n ## llu)
  191. #define I64_PRINTF_ARG(a) ((long long signed int)(a))
  192. #define I64_SCANF_ARG(a) ((long long signed int*)(a))
  193. #define I64_LITERAL(n) (n ## ll)
  194. #endif /* defined(_MSC_VER) */
  195. #if defined(__MINGW32__) || defined(__MINGW64__)
  196. #define MINGW_ANY
  197. #endif
  198. #if defined(_MSC_VER) || defined(MINGW_ANY)
  199. /** The formatting string used to put a uint64_t value in a printf() or
  200. * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
  201. #define U64_FORMAT "%I64u"
  202. #define I64_FORMAT "%I64d"
  203. #else /* !(defined(_MSC_VER) || defined(MINGW_ANY)) */
  204. #define U64_FORMAT "%llu"
  205. #define I64_FORMAT "%lld"
  206. #endif /* defined(_MSC_VER) || defined(MINGW_ANY) */
  207. #if (SIZEOF_INTPTR_T == SIZEOF_INT)
  208. #define INTPTR_T_FORMAT "%d"
  209. #define INTPTR_PRINTF_ARG(x) ((int)(x))
  210. #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
  211. #define INTPTR_T_FORMAT "%ld"
  212. #define INTPTR_PRINTF_ARG(x) ((long)(x))
  213. #elif (SIZEOF_INTPTR_T == 8)
  214. #define INTPTR_T_FORMAT I64_FORMAT
  215. #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
  216. #else
  217. #error Unknown: SIZEOF_INTPTR_T
  218. #endif /* (SIZEOF_INTPTR_T == SIZEOF_INT) || ... */
  219. #endif /* !defined(TOR_COMPAT_H) */