Tor  0.4.7.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(__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 "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 "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 #if defined(HAVE_ATTR_FALLTHROUGH)
54 #define FALLTHROUGH __attribute__((fallthrough))
55 #else
56 #define FALLTHROUGH
57 #endif
58 
59 /* What GCC do we have? */
60 #ifdef __GNUC__
61 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
62 #else
63 #define GCC_VERSION 0
64 #endif
65 
66 /* Temporarily enable and disable warnings. */
67 #ifdef __GNUC__
68 /* Support for macro-generated pragmas (c99) */
69 # define PRAGMA_(x) _Pragma (#x)
70 # ifdef __clang__
71 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
72 # else
73 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
74 # endif
75 # if defined(__clang__) || GCC_VERSION >= 406
76 /* we have push/pop support */
77 # define DISABLE_GCC_WARNING(warningopt) \
78  PRAGMA_DIAGNOSTIC_(push) \
79  PRAGMA_DIAGNOSTIC_(ignored warningopt)
80 # define ENABLE_GCC_WARNING(warningopt) \
81  PRAGMA_DIAGNOSTIC_(pop)
82 #else /* !(defined(__clang__) || GCC_VERSION >= 406) */
83 /* older version of gcc: no push/pop support. */
84 # define DISABLE_GCC_WARNING(warningopt) \
85  PRAGMA_DIAGNOSTIC_(ignored warningopt)
86 # define ENABLE_GCC_WARNING(warningopt) \
87  PRAGMA_DIAGNOSTIC_(warning warningopt)
88 #endif /* defined(__clang__) || GCC_VERSION >= 406 */
89 #else /* !defined(__GNUC__) */
90 /* not gcc at all */
91 # define DISABLE_GCC_WARNING(warning)
92 # define ENABLE_GCC_WARNING(warning)
93 #endif /* defined(__GNUC__) */
94 
95 /* inline is __inline on windows. */
96 #ifdef _WIN32
97 #define inline __inline
98 #endif
99 
100 /* Try to get a reasonable __func__ substitute in place. */
101 #if defined(_MSC_VER)
102 
103 #define __func__ __FUNCTION__
104 
105 #else
106 /* For platforms where autoconf works, make sure __func__ is defined
107  * sanely. */
108 #ifndef HAVE_MACRO__func__
109 #ifdef HAVE_MACRO__FUNCTION__
110 #define __func__ __FUNCTION__
111 #elif HAVE_MACRO__FUNC__
112 #define __func__ __FUNC__
113 #else
114 #define __func__ "???"
115 #endif /* defined(HAVE_MACRO__FUNCTION__) || ... */
116 #endif /* !defined(HAVE_MACRO__func__) */
117 #endif /* defined(_MSC_VER) */
118 
119 #ifdef ENUM_VALS_ARE_SIGNED
120 #define ENUM_BF(t) unsigned
121 #else
122 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
123  * just use the enumerated type (so the compiler can help us and notice
124  * problems), but if enumerated types are unsigned, we must use unsigned,
125  * so that the loss of precision doesn't make large values negative. */
126 #define ENUM_BF(t) t
127 #endif /* defined(ENUM_VALS_ARE_SIGNED) */
128 
129 /* GCC has several useful attributes. */
130 #if defined(__GNUC__) && __GNUC__ >= 3
131 #define ATTR_NORETURN __attribute__((noreturn))
132 #define ATTR_CONST __attribute__((const))
133 #define ATTR_MALLOC __attribute__((malloc))
134 #define ATTR_NORETURN __attribute__((noreturn))
135 #define ATTR_WUR __attribute__((warn_unused_result))
136 #define ATTR_UNUSED __attribute__ ((unused))
137 
138 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
139  * of <b>exp</b> will probably be true.
140  *
141  * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
142  * except that it tells the compiler that the branch will be taken most of the
143  * time. This can generate slightly better code with some CPUs.
144  */
145 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
146 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
147  * of <b>exp</b> will probably be false.
148  *
149  * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
150  * except that it tells the compiler that the branch will usually not be
151  * taken. This can generate slightly better code with some CPUs.
152  */
153 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
154 #else /* !(defined(__GNUC__) && __GNUC__ >= 3) */
155 #define ATTR_NORETURN
156 #define ATTR_CONST
157 #define ATTR_MALLOC
158 #define ATTR_NORETURN
159 #define ATTR_UNUSED
160 #define ATTR_WUR
161 #define PREDICT_LIKELY(exp) (exp)
162 #define PREDICT_UNLIKELY(exp) (exp)
163 #endif /* defined(__GNUC__) && __GNUC__ >= 3 */
164 
165 /** Expands to a syntactically valid empty statement. */
166 #define STMT_NIL (void)0
167 
168 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
169  * argument. */
170 #define STMT_VOID(a) while (0) { (void)(a); }
171 
172 #ifdef __GNUC__
173 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
174  * the macro can be used as if it were a single C statement. */
175 #define STMT_BEGIN (void) ({
176 #define STMT_END })
177 #elif defined(sun) || defined(__sun__)
178 #define STMT_BEGIN if (1) {
179 #define STMT_END } else STMT_NIL
180 #else
181 #define STMT_BEGIN do {
182 #define STMT_END } while (0)
183 #endif /* defined(__GNUC__) || ... */
184 
185 /* Some tools (like coccinelle) don't like to see operators as macro
186  * arguments. */
187 #define OP_LT <
188 #define OP_GT >
189 #define OP_GE >=
190 #define OP_LE <=
191 #define OP_EQ ==
192 #define OP_NE !=
193 
194 #if defined(__MINGW32__) || defined(__MINGW64__)
195 #define MINGW_ANY
196 #endif
197 
198 /** Macro: yield a pointer to the field at position <b>off</b> within the
199  * structure <b>st</b>. Example:
200  * <pre>
201  * struct a_t { int foo; int bar; } x;
202  * ptrdiff_t bar_offset = offsetof(struct a_t, bar);
203  * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
204  * *bar_p = 3;
205  * </pre>
206  */
207 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
208 
209 /** Macro: yield a pointer to an enclosing structure given a pointer to
210  * a substructure at offset <b>off</b>. Example:
211  * <pre>
212  * struct base_t { ... };
213  * struct subtype_t { int x; struct base_t b; } x;
214  * struct base_t *bp = &x.base;
215  * struct *sp = SUBTYPE_P(bp, struct subtype_t, b);
216  * </pre>
217  */
218 #define SUBTYPE_P(p, subtype, basemember) \
219  ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
220 
221 /** Macro: Yields the number of elements in array x. */
222 #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
223 
224 /**
225  * "Eat" a semicolon that somebody puts at the end of a top-level macro.
226  *
227  * Frequently, we want to declare a macro that people will use at file scope,
228  * and we want to allow people to put a semicolon after the macro.
229  *
230  * This declaration of a struct can be repeated any number of times, and takes
231  * a trailing semicolon afterwards.
232  **/
233 #define EAT_SEMICOLON \
234  struct dummy_semicolon_eater__
235 
236 /**
237  * Tell our static analysis tool to believe that (clang's scan-build or
238  * coverity scan) that an expression might be true. We use this to suppress
239  * dead-code warnings.
240  **/
241 #if defined(__COVERITY__) || defined(__clang_analyzer__)
242 /* By calling getenv, we force the analyzer not to conclude that 'expr' is
243  * false. */
244 #define POSSIBLE(expr) ((expr) || getenv("STATIC_ANALYZER_DEADCODE_DUMMY_"))
245 #else
246 #define POSSIBLE(expr) (expr)
247 #endif /* defined(__COVERITY__) || defined(__clang_analyzer__) */
248 
249 #endif /* !defined(TOR_COMPAT_COMPILER_H) */