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