compat_compiler.h 7.1 KB

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