tor  0.4.2.0-alpha-dev
compat_compiler.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
12 #ifndef TOR_COMPAT_COMPILER_H
13 #define TOR_COMPAT_COMPILER_H
14 
15 #include "orconfig.h"
16 #include <inttypes.h>
17 
18 #if defined(__has_feature)
19 # if __has_feature(address_sanitizer)
20 /* Some of the fancy glibc strcmp() macros include references to memory that
21  * clang rejects because it is off the end of a less-than-3. Clang hates this,
22  * even though those references never actually happen. */
23 # undef strcmp
24 #endif /* __has_feature(address_sanitizer) */
25 #endif /* defined(__has_feature) */
26 
27 #ifndef NULL_REP_IS_ZERO_BYTES
28 #error "It seems your platform does not represent NULL as zero. We can't cope."
29 #endif
30 
31 #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
32 #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
33 #endif
34 
35 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
36 #error "It seems that you encode characters in something other than ASCII."
37 #endif
38 
39 /* GCC can check printf and scanf types on arbitrary functions. */
40 #ifdef __GNUC__
41 #define CHECK_PRINTF(formatIdx, firstArg) \
42  __attribute__ ((format(printf, formatIdx, firstArg)))
43 #else
44 #define CHECK_PRINTF(formatIdx, firstArg)
45 #endif /* defined(__GNUC__) */
46 #ifdef __GNUC__
47 #define CHECK_SCANF(formatIdx, firstArg) \
48  __attribute__ ((format(scanf, formatIdx, firstArg)))
49 #else
50 #define CHECK_SCANF(formatIdx, firstArg)
51 #endif /* defined(__GNUC__) */
52 
53 /* What GCC do we have? */
54 #ifdef __GNUC__
55 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
56 #else
57 #define GCC_VERSION 0
58 #endif
59 
60 /* Temporarily enable and disable warnings. */
61 #ifdef __GNUC__
62 # define PRAGMA_STRINGIFY_(s) #s
63 # define PRAGMA_JOIN_STRINGIFY_(a,b) PRAGMA_STRINGIFY_(a ## b)
64 /* Support for macro-generated pragmas (c99) */
65 # define PRAGMA_(x) _Pragma (#x)
66 # ifdef __clang__
67 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
68 # else
69 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
70 # endif
71 # if defined(__clang__) || GCC_VERSION >= 406
72 /* we have push/pop support */
73 # define DISABLE_GCC_WARNING(warningopt) \
74  PRAGMA_DIAGNOSTIC_(push) \
75  PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
76 # define ENABLE_GCC_WARNING(warningopt) \
77  PRAGMA_DIAGNOSTIC_(pop)
78 #else /* !(defined(__clang__) || GCC_VERSION >= 406) */
79 /* older version of gcc: no push/pop support. */
80 # define DISABLE_GCC_WARNING(warningopt) \
81  PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
82 # define ENABLE_GCC_WARNING(warningopt) \
83  PRAGMA_DIAGNOSTIC_(warning PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
84 #endif /* defined(__clang__) || GCC_VERSION >= 406 */
85 #else /* !(defined(__GNUC__)) */
86 /* not gcc at all */
87 # define DISABLE_GCC_WARNING(warning)
88 # define ENABLE_GCC_WARNING(warning)
89 #endif /* defined(__GNUC__) */
90 
91 /* inline is __inline on windows. */
92 #ifdef _WIN32
93 #define inline __inline
94 #endif
95 
96 /* Try to get a reasonable __func__ substitute in place. */
97 #if defined(_MSC_VER)
98 
99 #define __func__ __FUNCTION__
100 
101 #else
102 /* For platforms where autoconf works, make sure __func__ is defined
103  * sanely. */
104 #ifndef HAVE_MACRO__func__
105 #ifdef HAVE_MACRO__FUNCTION__
106 #define __func__ __FUNCTION__
107 #elif HAVE_MACRO__FUNC__
108 #define __func__ __FUNC__
109 #else
110 #define __func__ "???"
111 #endif /* defined(HAVE_MACRO__FUNCTION__) || ... */
112 #endif /* !defined(HAVE_MACRO__func__) */
113 #endif /* defined(_MSC_VER) */
114 
115 #ifdef ENUM_VALS_ARE_SIGNED
116 #define ENUM_BF(t) unsigned
117 #else
118 
122 #define ENUM_BF(t) t
123 #endif /* defined(ENUM_VALS_ARE_SIGNED) */
124 
125 /* GCC has several useful attributes. */
126 #if defined(__GNUC__) && __GNUC__ >= 3
127 #define ATTR_NORETURN __attribute__((noreturn))
128 #define ATTR_CONST __attribute__((const))
129 #define ATTR_MALLOC __attribute__((malloc))
130 #define ATTR_NORETURN __attribute__((noreturn))
131 #define ATTR_WUR __attribute__((warn_unused_result))
132 #define ATTR_UNUSED __attribute__ ((unused))
133 
141 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
142 
149 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
150 #else /* !(defined(__GNUC__) && __GNUC__ >= 3) */
151 #define ATTR_NORETURN
152 #define ATTR_CONST
153 #define ATTR_MALLOC
154 #define ATTR_NORETURN
155 #define ATTR_UNUSED
156 #define ATTR_WUR
157 #define PREDICT_LIKELY(exp) (exp)
158 #define PREDICT_UNLIKELY(exp) (exp)
159 #endif /* defined(__GNUC__) && __GNUC__ >= 3 */
160 
162 #define STMT_NIL (void)0
163 
166 #define STMT_VOID(a) while (0) { (void)(a); }
167 
168 #ifdef __GNUC__
169 
171 #define STMT_BEGIN (void) ({
172 #define STMT_END })
173 #elif defined(sun) || defined(__sun__)
174 #define STMT_BEGIN if (1) {
175 #define STMT_END } else STMT_NIL
176 #else
177 #define STMT_BEGIN do {
178 #define STMT_END } while (0)
179 #endif /* defined(__GNUC__) || ... */
180 
181 /* Some tools (like coccinelle) don't like to see operators as macro
182  * arguments. */
183 #define OP_LT <
184 #define OP_GT >
185 #define OP_GE >=
186 #define OP_LE <=
187 #define OP_EQ ==
188 #define OP_NE !=
189 
190 #if defined(__MINGW32__) || defined(__MINGW64__)
191 #define MINGW_ANY
192 #endif
193 
203 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
204 
214 #define SUBTYPE_P(p, subtype, basemember) \
215  ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
216 
218 #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
219 
229 #define EAT_SEMICOLON \
230  struct dummy_semicolon_eater__
231 
232 #endif /* !defined(TOR_COMPAT_COMPILER_H) */