compat_compiler.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include <inttypes.h>
  9. #if defined(__has_feature)
  10. # if __has_feature(address_sanitizer)
  11. /* Some of the fancy glibc strcmp() macros include references to memory that
  12. * clang rejects because it is off the end of a less-than-3. Clang hates this,
  13. * even though those references never actually happen. */
  14. # undef strcmp
  15. #endif /* __has_feature(address_sanitizer) */
  16. #endif /* defined(__has_feature) */
  17. #ifndef NULL_REP_IS_ZERO_BYTES
  18. #error "It seems your platform does not represent NULL as zero. We can't cope."
  19. #endif
  20. #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
  21. #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
  22. #endif
  23. #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
  24. #error "It seems that you encode characters in something other than ASCII."
  25. #endif
  26. /* GCC can check printf and scanf types on arbitrary functions. */
  27. #ifdef __GNUC__
  28. #define CHECK_PRINTF(formatIdx, firstArg) \
  29. __attribute__ ((format(printf, formatIdx, firstArg)))
  30. #else
  31. #define CHECK_PRINTF(formatIdx, firstArg)
  32. #endif /* defined(__GNUC__) */
  33. #ifdef __GNUC__
  34. #define CHECK_SCANF(formatIdx, firstArg) \
  35. __attribute__ ((format(scanf, formatIdx, firstArg)))
  36. #else
  37. #define CHECK_SCANF(formatIdx, firstArg)
  38. #endif /* defined(__GNUC__) */
  39. /* What GCC do we have? */
  40. #ifdef __GNUC__
  41. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  42. #else
  43. #define GCC_VERSION 0
  44. #endif
  45. /* Temporarily enable and disable warnings. */
  46. #ifdef __GNUC__
  47. # define PRAGMA_STRINGIFY_(s) #s
  48. # define PRAGMA_JOIN_STRINGIFY_(a,b) PRAGMA_STRINGIFY_(a ## b)
  49. /* Support for macro-generated pragmas (c99) */
  50. # define PRAGMA_(x) _Pragma (#x)
  51. # ifdef __clang__
  52. # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
  53. # else
  54. # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
  55. # endif
  56. # if defined(__clang__) || GCC_VERSION >= 406
  57. /* we have push/pop support */
  58. # define DISABLE_GCC_WARNING(warningopt) \
  59. PRAGMA_DIAGNOSTIC_(push) \
  60. PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
  61. # define ENABLE_GCC_WARNING(warningopt) \
  62. PRAGMA_DIAGNOSTIC_(pop)
  63. #else /* !(defined(__clang__) || GCC_VERSION >= 406) */
  64. /* older version of gcc: no push/pop support. */
  65. # define DISABLE_GCC_WARNING(warningopt) \
  66. PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
  67. # define ENABLE_GCC_WARNING(warningopt) \
  68. PRAGMA_DIAGNOSTIC_(warning PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
  69. #endif /* defined(__clang__) || GCC_VERSION >= 406 */
  70. #else /* !(defined(__GNUC__)) */
  71. /* not gcc at all */
  72. # define DISABLE_GCC_WARNING(warning)
  73. # define ENABLE_GCC_WARNING(warning)
  74. #endif /* defined(__GNUC__) */
  75. /* inline is __inline on windows. */
  76. #ifdef _WIN32
  77. #define inline __inline
  78. #endif
  79. /* Try to get a reasonable __func__ substitute in place. */
  80. #if defined(_MSC_VER)
  81. #define __func__ __FUNCTION__
  82. #else
  83. /* For platforms where autoconf works, make sure __func__ is defined
  84. * sanely. */
  85. #ifndef HAVE_MACRO__func__
  86. #ifdef HAVE_MACRO__FUNCTION__
  87. #define __func__ __FUNCTION__
  88. #elif HAVE_MACRO__FUNC__
  89. #define __func__ __FUNC__
  90. #else
  91. #define __func__ "???"
  92. #endif /* defined(HAVE_MACRO__FUNCTION__) || ... */
  93. #endif /* !defined(HAVE_MACRO__func__) */
  94. #endif /* defined(_MSC_VER) */
  95. #define U64_TO_DBL(x) ((double) (x))
  96. #define DBL_TO_U64(x) ((uint64_t) (x))
  97. #ifdef ENUM_VALS_ARE_SIGNED
  98. #define ENUM_BF(t) unsigned
  99. #else
  100. /** Wrapper for having a bitfield of an enumerated type. Where possible, we
  101. * just use the enumerated type (so the compiler can help us and notice
  102. * problems), but if enumerated types are unsigned, we must use unsigned,
  103. * so that the loss of precision doesn't make large values negative. */
  104. #define ENUM_BF(t) t
  105. #endif /* defined(ENUM_VALS_ARE_SIGNED) */
  106. /* GCC has several useful attributes. */
  107. #if defined(__GNUC__) && __GNUC__ >= 3
  108. #define ATTR_NORETURN __attribute__((noreturn))
  109. #define ATTR_CONST __attribute__((const))
  110. #define ATTR_MALLOC __attribute__((malloc))
  111. #define ATTR_NORETURN __attribute__((noreturn))
  112. #define ATTR_WUR __attribute__((warn_unused_result))
  113. #define ATTR_UNUSED __attribute__ ((unused))
  114. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  115. * of <b>exp</b> will probably be true.
  116. *
  117. * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
  118. * except that it tells the compiler that the branch will be taken most of the
  119. * time. This can generate slightly better code with some CPUs.
  120. */
  121. #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
  122. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  123. * of <b>exp</b> will probably be false.
  124. *
  125. * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
  126. * except that it tells the compiler that the branch will usually not be
  127. * taken. This can generate slightly better code with some CPUs.
  128. */
  129. #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
  130. #else /* !(defined(__GNUC__) && __GNUC__ >= 3) */
  131. #define ATTR_NORETURN
  132. #define ATTR_CONST
  133. #define ATTR_MALLOC
  134. #define ATTR_NORETURN
  135. #define ATTR_UNUSED
  136. #define ATTR_WUR
  137. #define PREDICT_LIKELY(exp) (exp)
  138. #define PREDICT_UNLIKELY(exp) (exp)
  139. #endif /* defined(__GNUC__) && __GNUC__ >= 3 */
  140. /** Expands to a syntactically valid empty statement. */
  141. #define STMT_NIL (void)0
  142. /** Expands to a syntactically valid empty statement, explicitly (void)ing its
  143. * argument. */
  144. #define STMT_VOID(a) while (0) { (void)(a); }
  145. #ifdef __GNUC__
  146. /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
  147. * the macro can be used as if it were a single C statement. */
  148. #define STMT_BEGIN (void) ({
  149. #define STMT_END })
  150. #elif defined(sun) || defined(__sun__)
  151. #define STMT_BEGIN if (1) {
  152. #define STMT_END } else STMT_NIL
  153. #else
  154. #define STMT_BEGIN do {
  155. #define STMT_END } while (0)
  156. #endif /* defined(__GNUC__) || ... */
  157. /* Some tools (like coccinelle) don't like to see operators as macro
  158. * arguments. */
  159. #define OP_LT <
  160. #define OP_GT >
  161. #define OP_GE >=
  162. #define OP_LE <=
  163. #define OP_EQ ==
  164. #define OP_NE !=
  165. #if defined(__MINGW32__) || defined(__MINGW64__)
  166. #define MINGW_ANY
  167. #endif
  168. #define U64_PRINTF_ARG(a) ((uint64_t)a)
  169. #define I64_PRINTF_ARG(a) ((int64_t)a)
  170. #define U64_FORMAT "%"PRIu64
  171. #define I64_FORMAT "%"PRId64
  172. /** Macro: yield a pointer to the field at position <b>off</b> within the
  173. * structure <b>st</b>. Example:
  174. * <pre>
  175. * struct a { int foo; int bar; } x;
  176. * off_t bar_offset = offsetof(struct a, bar);
  177. * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
  178. * *bar_p = 3;
  179. * </pre>
  180. */
  181. #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
  182. /** Macro: yield a pointer to an enclosing structure given a pointer to
  183. * a substructure at offset <b>off</b>. Example:
  184. * <pre>
  185. * struct base { ... };
  186. * struct subtype { int x; struct base b; } x;
  187. * struct base *bp = &x.base;
  188. * struct *sp = SUBTYPE_P(bp, struct subtype, b);
  189. * </pre>
  190. */
  191. #define SUBTYPE_P(p, subtype, basemember) \
  192. ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
  193. /** Macro: Yields the number of elements in array x. */
  194. #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
  195. #endif /* !defined(TOR_COMPAT_H) */