compat_compiler.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #ifdef ENUM_VALS_ARE_SIGNED
  96. #define ENUM_BF(t) unsigned
  97. #else
  98. /** Wrapper for having a bitfield of an enumerated type. Where possible, we
  99. * just use the enumerated type (so the compiler can help us and notice
  100. * problems), but if enumerated types are unsigned, we must use unsigned,
  101. * so that the loss of precision doesn't make large values negative. */
  102. #define ENUM_BF(t) t
  103. #endif /* defined(ENUM_VALS_ARE_SIGNED) */
  104. /* GCC has several useful attributes. */
  105. #if defined(__GNUC__) && __GNUC__ >= 3
  106. #define ATTR_NORETURN __attribute__((noreturn))
  107. #define ATTR_CONST __attribute__((const))
  108. #define ATTR_MALLOC __attribute__((malloc))
  109. #define ATTR_NORETURN __attribute__((noreturn))
  110. #define ATTR_WUR __attribute__((warn_unused_result))
  111. #define ATTR_UNUSED __attribute__ ((unused))
  112. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  113. * of <b>exp</b> will probably be true.
  114. *
  115. * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
  116. * except that it tells the compiler that the branch will be taken most of the
  117. * time. This can generate slightly better code with some CPUs.
  118. */
  119. #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
  120. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  121. * of <b>exp</b> will probably be false.
  122. *
  123. * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
  124. * except that it tells the compiler that the branch will usually not be
  125. * taken. This can generate slightly better code with some CPUs.
  126. */
  127. #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
  128. #else /* !(defined(__GNUC__) && __GNUC__ >= 3) */
  129. #define ATTR_NORETURN
  130. #define ATTR_CONST
  131. #define ATTR_MALLOC
  132. #define ATTR_NORETURN
  133. #define ATTR_UNUSED
  134. #define ATTR_WUR
  135. #define PREDICT_LIKELY(exp) (exp)
  136. #define PREDICT_UNLIKELY(exp) (exp)
  137. #endif /* defined(__GNUC__) && __GNUC__ >= 3 */
  138. /** Expands to a syntactically valid empty statement. */
  139. #define STMT_NIL (void)0
  140. /** Expands to a syntactically valid empty statement, explicitly (void)ing its
  141. * argument. */
  142. #define STMT_VOID(a) while (0) { (void)(a); }
  143. #ifdef __GNUC__
  144. /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
  145. * the macro can be used as if it were a single C statement. */
  146. #define STMT_BEGIN (void) ({
  147. #define STMT_END })
  148. #elif defined(sun) || defined(__sun__)
  149. #define STMT_BEGIN if (1) {
  150. #define STMT_END } else STMT_NIL
  151. #else
  152. #define STMT_BEGIN do {
  153. #define STMT_END } while (0)
  154. #endif /* defined(__GNUC__) || ... */
  155. /* Some tools (like coccinelle) don't like to see operators as macro
  156. * arguments. */
  157. #define OP_LT <
  158. #define OP_GT >
  159. #define OP_GE >=
  160. #define OP_LE <=
  161. #define OP_EQ ==
  162. #define OP_NE !=
  163. #if defined(__MINGW32__) || defined(__MINGW64__)
  164. #define MINGW_ANY
  165. #endif
  166. /** Macro: yield a pointer to the field at position <b>off</b> within the
  167. * structure <b>st</b>. Example:
  168. * <pre>
  169. * struct a { int foo; int bar; } x;
  170. * off_t bar_offset = offsetof(struct a, bar);
  171. * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
  172. * *bar_p = 3;
  173. * </pre>
  174. */
  175. #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
  176. /** Macro: yield a pointer to an enclosing structure given a pointer to
  177. * a substructure at offset <b>off</b>. Example:
  178. * <pre>
  179. * struct base { ... };
  180. * struct subtype { int x; struct base b; } x;
  181. * struct base *bp = &x.base;
  182. * struct *sp = SUBTYPE_P(bp, struct subtype, b);
  183. * </pre>
  184. */
  185. #define SUBTYPE_P(p, subtype, basemember) \
  186. ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
  187. /** Macro: Yields the number of elements in array x. */
  188. #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
  189. #endif /* !defined(TOR_COMPAT_H) */