Bump copyright date to 2019
[tor.git] / src / lib / cc / compat_compiler.h
blob3a0f307186416e1bb5ddaf7b747db35de9356ee7
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 */
6 /**
7 * \file compat_compiler.h
8 * \brief Utility macros to handle different features and behavior in different
9 * compilers.
10 **/
12 #ifndef TOR_COMPAT_COMPILER_H
13 #define TOR_COMPAT_COMPILER_H
15 #include "orconfig.h"
16 #include <inttypes.h>
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) */
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
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
35 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
36 #error "It seems that you encode characters in something other than ASCII."
37 #endif
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__) */
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
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__) */
91 /* inline is __inline on windows. */
92 #ifdef _WIN32
93 #define inline __inline
94 #endif
96 /* Try to get a reasonable __func__ substitute in place. */
97 #if defined(_MSC_VER)
99 #define __func__ __FUNCTION__
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) */
115 #ifdef ENUM_VALS_ARE_SIGNED
116 #define ENUM_BF(t) unsigned
117 #else
118 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
119 * just use the enumerated type (so the compiler can help us and notice
120 * problems), but if enumerated types are unsigned, we must use unsigned,
121 * so that the loss of precision doesn't make large values negative. */
122 #define ENUM_BF(t) t
123 #endif /* defined(ENUM_VALS_ARE_SIGNED) */
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))
134 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
135 * of <b>exp</b> will probably be true.
137 * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
138 * except that it tells the compiler that the branch will be taken most of the
139 * time. This can generate slightly better code with some CPUs.
141 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
142 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
143 * of <b>exp</b> will probably be false.
145 * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
146 * except that it tells the compiler that the branch will usually not be
147 * taken. This can generate slightly better code with some CPUs.
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 */
161 /** Expands to a syntactically valid empty statement. */
162 #define STMT_NIL (void)0
164 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
165 * argument. */
166 #define STMT_VOID(a) while (0) { (void)(a); }
168 #ifdef __GNUC__
169 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
170 * the macro can be used as if it were a single C statement. */
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__) || ... */
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 !=
190 #if defined(__MINGW32__) || defined(__MINGW64__)
191 #define MINGW_ANY
192 #endif
194 /** Macro: yield a pointer to the field at position <b>off</b> within the
195 * structure <b>st</b>. Example:
196 * <pre>
197 * struct a { int foo; int bar; } x;
198 * off_t bar_offset = offsetof(struct a, bar);
199 * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
200 * *bar_p = 3;
201 * </pre>
203 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
205 /** Macro: yield a pointer to an enclosing structure given a pointer to
206 * a substructure at offset <b>off</b>. Example:
207 * <pre>
208 * struct base { ... };
209 * struct subtype { int x; struct base b; } x;
210 * struct base *bp = &x.base;
211 * struct *sp = SUBTYPE_P(bp, struct subtype, b);
212 * </pre>
214 #define SUBTYPE_P(p, subtype, basemember) \
215 ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
217 /** Macro: Yields the number of elements in array x. */
218 #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
220 #endif /* !defined(TOR_COMPAT_H) */