Tor  0.4.8.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-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file compat_compiler.h
8  * \brief Utility macros to handle different features and behavior in different
9  * compilers.
10  **/
11 
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(__MINGW32__) || defined(__MINGW64__)
19 #define MINGW_ANY
20 #endif
21 
22 #ifdef MINGW_ANY
23 /* We need this for __MINGW_PRINTF_FORMAT, alas. */
24 #include <stdio.h>
25 #endif
26 
27 #if defined(__has_feature)
28 # if __has_feature(address_sanitizer)
29 /* Some of the fancy glibc strcmp() macros include references to memory that
30  * clang rejects because it is off the end of a less-than-3. Clang hates this,
31  * even though those references never actually happen. */
32 # undef strcmp
33 #endif /* __has_feature(address_sanitizer) */
34 #endif /* defined(__has_feature) */
35 
36 #ifndef NULL_REP_IS_ZERO_BYTES
37 #error "Your platform does not represent NULL as zero. We can't cope."
38 #endif
39 
40 #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
41 #error "Your platform does not represent 0.0 as zeros. We can't cope."
42 #endif
43 
44 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
45 #error "It seems that you encode characters in something other than ASCII."
46 #endif
47 
48 /* Use the right magic attribute on mingw, which might be printf, gnu_printf,
49  * or ms_printf, depending on how we're set up to build.
50  */
51 #ifdef __MINGW_PRINTF_FORMAT
52 #define PRINTF_FORMAT_ATTR __MINGW_PRINTF_FORMAT
53 #else
54 #define PRINTF_FORMAT_ATTR printf
55 #endif
56 #ifdef __MINGW_SCANF_FORMAT
57 #define SCANF_FORMAT_ATTR __MINGW_SCANF_FORMAT
58 #else
59 #define SCANF_FORMAT_ATTR scanf
60 #endif
61 
62 /* GCC can check printf and scanf types on arbitrary functions. */
63 #ifdef __GNUC__
64 #define CHECK_PRINTF(formatIdx, firstArg) \
65  __attribute__ ((format(PRINTF_FORMAT_ATTR, formatIdx, firstArg)))
66 #else
67 #define CHECK_PRINTF(formatIdx, firstArg)
68 #endif /* defined(__GNUC__) */
69 #ifdef __GNUC__
70 #define CHECK_SCANF(formatIdx, firstArg) \
71  __attribute__ ((format(SCANF_FORMAT_ATTR, formatIdx, firstArg)))
72 #else
73 #define CHECK_SCANF(formatIdx, firstArg)
74 #endif /* defined(__GNUC__) */
75 
76 #if defined(HAVE_ATTR_FALLTHROUGH)
77 #define FALLTHROUGH __attribute__((fallthrough))
78 #else
79 #define FALLTHROUGH
80 #endif
81 
82 /* What GCC do we have? */
83 #ifdef __GNUC__
84 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
85 #else
86 #define GCC_VERSION 0
87 #endif
88 
89 /* Temporarily enable and disable warnings. */
90 #ifdef __GNUC__
91 /* Support for macro-generated pragmas (c99) */
92 # define PRAGMA_(x) _Pragma (#x)
93 # ifdef __clang__
94 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
95 # else
96 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
97 # endif
98 # if defined(__clang__) || GCC_VERSION >= 406
99 /* we have push/pop support */
100 # define DISABLE_GCC_WARNING(warningopt) \
101  PRAGMA_DIAGNOSTIC_(push) \
102  PRAGMA_DIAGNOSTIC_(ignored warningopt)
103 # define ENABLE_GCC_WARNING(warningopt) \
104  PRAGMA_DIAGNOSTIC_(pop)
105 #else /* !(defined(__clang__) || GCC_VERSION >= 406) */
106 /* older version of gcc: no push/pop support. */
107 # define DISABLE_GCC_WARNING(warningopt) \
108  PRAGMA_DIAGNOSTIC_(ignored warningopt)
109 # define ENABLE_GCC_WARNING(warningopt) \
110  PRAGMA_DIAGNOSTIC_(warning warningopt)
111 #endif /* defined(__clang__) || GCC_VERSION >= 406 */
112 #else /* !defined(__GNUC__) */
113 /* not gcc at all */
114 # define DISABLE_GCC_WARNING(warning)
115 # define ENABLE_GCC_WARNING(warning)
116 #endif /* defined(__GNUC__) */
117 
118 /* inline is __inline on windows. */
119 #ifdef _WIN32
120 #define inline __inline
121 #endif
122 
123 /* Try to get a reasonable __func__ substitute in place. */
124 #if defined(_MSC_VER)
125 
126 #define __func__ __FUNCTION__
127 
128 #else
129 /* For platforms where autoconf works, make sure __func__ is defined
130  * sanely. */
131 #ifndef HAVE_MACRO__func__
132 #ifdef HAVE_MACRO__FUNCTION__
133 #define __func__ __FUNCTION__
134 #elif HAVE_MACRO__FUNC__
135 #define __func__ __FUNC__
136 #else
137 #define __func__ "???"
138 #endif /* defined(HAVE_MACRO__FUNCTION__) || ... */
139 #endif /* !defined(HAVE_MACRO__func__) */
140 #endif /* defined(_MSC_VER) */
141 
142 #ifdef ENUM_VALS_ARE_SIGNED
143 #define ENUM_BF(t) unsigned
144 #else
145 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
146  * just use the enumerated type (so the compiler can help us and notice
147  * problems), but if enumerated types are unsigned, we must use unsigned,
148  * so that the loss of precision doesn't make large values negative. */
149 #define ENUM_BF(t) t
150 #endif /* defined(ENUM_VALS_ARE_SIGNED) */
151 
152 /* GCC has several useful attributes. */
153 #if defined(__GNUC__) && __GNUC__ >= 3
154 #define ATTR_NORETURN __attribute__((noreturn))
155 #define ATTR_CONST __attribute__((const))
156 #define ATTR_MALLOC __attribute__((malloc))
157 #define ATTR_NORETURN __attribute__((noreturn))
158 #define ATTR_WUR __attribute__((warn_unused_result))
159 #define ATTR_UNUSED __attribute__ ((unused))
160 
161 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
162  * of <b>exp</b> will probably be true.
163  *
164  * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
165  * except that it tells the compiler that the branch will be taken most of the
166  * time. This can generate slightly better code with some CPUs.
167  */
168 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
169 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
170  * of <b>exp</b> will probably be false.
171  *
172  * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
173  * except that it tells the compiler that the branch will usually not be
174  * taken. This can generate slightly better code with some CPUs.
175  */
176 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
177 #else /* !(defined(__GNUC__) && __GNUC__ >= 3) */
178 #define ATTR_NORETURN
179 #define ATTR_CONST
180 #define ATTR_MALLOC
181 #define ATTR_NORETURN
182 #define ATTR_UNUSED
183 #define ATTR_WUR
184 #define PREDICT_LIKELY(exp) (exp)
185 #define PREDICT_UNLIKELY(exp) (exp)
186 #endif /* defined(__GNUC__) && __GNUC__ >= 3 */
187 
188 /** Expands to a syntactically valid empty statement. */
189 #define STMT_NIL (void)0
190 
191 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
192  * argument. */
193 #define STMT_VOID(a) while (0) { (void)(a); }
194 
195 #ifdef __GNUC__
196 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
197  * the macro can be used as if it were a single C statement. */
198 #define STMT_BEGIN (void) ({
199 #define STMT_END })
200 #elif defined(sun) || defined(__sun__)
201 #define STMT_BEGIN if (1) {
202 #define STMT_END } else STMT_NIL
203 #else
204 #define STMT_BEGIN do {
205 #define STMT_END } while (0)
206 #endif /* defined(__GNUC__) || ... */
207 
208 /* Some tools (like coccinelle) don't like to see operators as macro
209  * arguments. */
210 #define OP_LT <
211 #define OP_GT >
212 #define OP_GE >=
213 #define OP_LE <=
214 #define OP_EQ ==
215 #define OP_NE !=
216 
217 /** Macro: yield a pointer to the field at position <b>off</b> within the
218  * structure <b>st</b>. Example:
219  * <pre>
220  * struct a_t { int foo; int bar; } x;
221  * ptrdiff_t bar_offset = offsetof(struct a_t, bar);
222  * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
223  * *bar_p = 3;
224  * </pre>
225  */
226 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
227 
228 /** Macro: yield a pointer to an enclosing structure given a pointer to
229  * a substructure at offset <b>off</b>. Example:
230  * <pre>
231  * struct base_t { ... };
232  * struct subtype_t { int x; struct base_t b; } x;
233  * struct base_t *bp = &x.base;
234  * struct *sp = SUBTYPE_P(bp, struct subtype_t, b);
235  * </pre>
236  */
237 #define SUBTYPE_P(p, subtype, basemember) \
238  ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
239 
240 /** Macro: Yields the number of elements in array x. */
241 #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
242 
243 /**
244  * "Eat" a semicolon that somebody puts at the end of a top-level macro.
245  *
246  * Frequently, we want to declare a macro that people will use at file scope,
247  * and we want to allow people to put a semicolon after the macro.
248  *
249  * This declaration of a struct can be repeated any number of times, and takes
250  * a trailing semicolon afterwards.
251  **/
252 #define EAT_SEMICOLON \
253  struct dummy_semicolon_eater__
254 
255 /**
256  * Tell our static analysis tool to believe that (clang's scan-build or
257  * coverity scan) that an expression might be true. We use this to suppress
258  * dead-code warnings.
259  **/
260 #if defined(__COVERITY__) || defined(__clang_analyzer__)
261 /* By calling getenv, we force the analyzer not to conclude that 'expr' is
262  * false. */
263 #define POSSIBLE(expr) ((expr) || getenv("STATIC_ANALYZER_DEADCODE_DUMMY_"))
264 #else
265 #define POSSIBLE(expr) (expr)
266 #endif /* defined(__COVERITY__) || defined(__clang_analyzer__) */
267 
268 #endif /* !defined(TOR_COMPAT_COMPILER_H) */