forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_compiler.h
373 lines (331 loc) · 10.8 KB
/
my_compiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#ifndef MY_COMPILER_INCLUDED
#define MY_COMPILER_INCLUDED
/* Copyright (c) 2010, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file include/my_compiler.h
Header for compiler-dependent features.
Intended to contain a set of reusable wrappers for preprocessor
macros, attributes, pragmas, and any other features that are
specific to a target compiler.
*/
#ifndef MYSQL_ABI_CHECK
#include <assert.h>
#include <stddef.h> /* size_t */
#endif
#include "my_config.h"
#include "mysql/attribute.h"
/*
The macros below are borrowed from include/linux/compiler.h in the
Linux kernel. Use them to indicate the likelihood of the truthfulness
of a condition. This serves two purposes - newer versions of gcc will be
able to optimize for branch predication, which could yield significant
performance gains in frequently executed sections of the code, and the
other reason to use them is for documentation
*/
#ifdef HAVE_BUILTIN_EXPECT
// likely/unlikely are likely to clash with other symbols, do not #define
#if defined(__cplusplus)
constexpr bool likely(bool expr) { return __builtin_expect(expr, true); }
constexpr bool unlikely(bool expr) { return __builtin_expect(expr, false); }
#else
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#endif
#else /* HAVE_BUILTIN_EXPECT */
#if defined(__cplusplus)
constexpr bool likely(bool expr) { return expr; }
constexpr bool unlikely(bool expr) { return expr; }
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#endif /* HAVE_BUILTIN_EXPECT */
/* Communicate to the compiler the unreachability of the code. */
#ifdef HAVE_BUILTIN_UNREACHABLE
#define MY_ASSERT_UNREACHABLE() __builtin_unreachable()
#else
#define MY_ASSERT_UNREACHABLE() \
do { \
assert(0); \
} while (0)
#endif
/* Visual Studio requires '__inline' for C code */
#if !defined(__cplusplus) && defined(_MSC_VER)
#define inline __inline
#endif
/* Provide __func__ macro definition for Visual Studio. */
#if defined(_MSC_VER)
#define __func__ __FUNCTION__
#endif
#if defined(_MSC_VER)
#define ALWAYS_INLINE __forceinline
#else
#define ALWAYS_INLINE __attribute__((always_inline)) inline
#endif
#if defined(_MSC_VER)
#define NO_INLINE __declspec(noinline)
#else
#define NO_INLINE __attribute__((noinline))
#endif
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
#ifndef SUPPRESS_UBSAN
// clang -fsanitize=undefined
#if defined(HAVE_UBSAN) && defined(__clang__)
#define SUPPRESS_UBSAN MY_ATTRIBUTE((no_sanitize("undefined")))
// gcc -fsanitize=undefined
#elif defined(HAVE_UBSAN) && __has_attribute(no_sanitize_undefined)
#define SUPPRESS_UBSAN MY_ATTRIBUTE((no_sanitize_undefined))
#else
#define SUPPRESS_UBSAN
#endif
#endif /* SUPPRESS_UBSAN */
#ifndef SUPPRESS_TSAN
#if defined(HAVE_TSAN) && defined(__clang__)
#define SUPPRESS_TSAN MY_ATTRIBUTE((no_sanitize("thread")))
#elif defined(HAVE_TSAN) && __has_attribute(no_sanitize_thread)
#define SUPPRESS_TSAN MY_ATTRIBUTE((no_sanitize_thread))
#else
#define SUPPRESS_TSAN
#endif
#endif /* SUPPRESS_TSAN */
#ifdef _WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif
/**
* stringify parameters for C99/C++11 _Pragma().
*/
#define MY_COMPILER_CPP11_PRAGMA(X) _Pragma(#X)
/**
* pass parameters to MSVC's __pragma() as is.
*/
#define MY_COMPILER_MSVC_PRAGMA(X) __pragma(X)
// enable compiler specified 'diagnostic' pragmas.
//
// 1. clang on windows defines both clang and msvc pragmas and generates the
// same warnings
// 2. clang defines both __clang__ and __GNUC__, but doesn't support all GCC
// warnings with the same name
//
// +---------------------+
// | enabled diagnostics |
// +------+-------+------+
// | gcc | clang | msvc |
// +-------+------+-------+------+
// | gcc | x | - | - |
// | clang | - | x | x |
// | msvc | - | - | x |
// +-------+------+-------+------+
// ^^^
// +----- current compiler
//
// suppressions that aren't supported by the compiler are disabled to avoid
// "unsupported pragmas" warnings:
//
// @code
// // on GCC, clang-specific diagnostic pragmas are disabled
// MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Wdocumentation")
// @endcode
#if defined(__clang__)
#define MY_COMPILER_CLANG_DIAGNOSTIC_PUSH() \
MY_COMPILER_CPP11_PRAGMA(clang diagnostic push)
#define MY_COMPILER_CLANG_DIAGNOSTIC_POP() \
MY_COMPILER_CPP11_PRAGMA(clang diagnostic pop)
/**
* ignore a compiler warning.
*
* @param X warning option to disable, must be quoted like "-Wdocumentation"
*/
#define MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE(X) \
MY_COMPILER_CPP11_PRAGMA(clang diagnostic ignored X)
/**
* turn a compiler warning into an error.
*
* @param X warning option to turn into an error, must be a quoted string like
* "-Wdocumentation"
*/
#define MY_COMPILER_CLANG_DIAGNOSTIC_ERROR(X) \
MY_COMPILER_CPP11_PRAGMA(clang diagnostic error X)
#elif defined(__GNUC__)
#define MY_COMPILER_GCC_DIAGNOSTIC_PUSH() \
MY_COMPILER_CPP11_PRAGMA(GCC diagnostic push)
#define MY_COMPILER_GCC_DIAGNOSTIC_POP() \
MY_COMPILER_CPP11_PRAGMA(GCC diagnostic pop)
/**
* ignore a compiler warning.
*
* @param X warning option to disable, must be quoted like "-Wdocumentation"
*/
#define MY_COMPILER_GCC_DIAGNOSTIC_IGNORE(X) \
MY_COMPILER_CPP11_PRAGMA(GCC diagnostic ignored X)
/**
* turn a compiler warning into an error.
*
* @param X warning option to turn into an error, must be quoted like
* "-Wdocumentation"
*/
#define MY_COMPILER_GCC_DIAGNOSTIC_ERROR(X) \
MY_COMPILER_CPP11_PRAGMA(GCC diagnostic error X)
#endif // defined(__GNUC__)
#if defined(_MSC_VER)
#define MY_COMPILER_MSVC_DIAGNOSTIC_PUSH() \
MY_COMPILER_MSVC_PRAGMA(warning(push))
#define MY_COMPILER_MSVC_DIAGNOSTIC_POP() MY_COMPILER_MSVC_PRAGMA(warning(pop))
/**
* ignore a compiler warning.
*
* @param X warning number to disable
*/
#define MY_COMPILER_MSVC_DIAGNOSTIC_IGNORE(X) \
MY_COMPILER_MSVC_PRAGMA(warning(disable : X))
#define MY_COMPILER_MSVC_DIAGNOSTIC_ERROR(X) \
MY_COMPILER_MSVC_PRAGMA(warning(error : X))
#endif // defined(_MSC_VER)
#if !defined(MY_COMPILER_CLANG_DIAGNOSTIC_ERROR)
#define MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE(X)
#define MY_COMPILER_CLANG_DIAGNOSTIC_ERROR(X)
#endif
#if !defined(MY_COMPILER_GCC_DIAGNOSTIC_ERROR)
#define MY_COMPILER_GCC_DIAGNOSTIC_IGNORE(X)
#define MY_COMPILER_GCC_DIAGNOSTIC_ERROR(X)
#endif
#if !defined(MY_COMPILER_MSVC_DIAGNOSTIC_ERROR)
#define MY_COMPILER_MSVC_DIAGNOSTIC_IGNORE(X)
#define MY_COMPILER_MSVC_DIAGNOSTIC_ERROR(X)
#endif
/**
* @def MY_COMPILER_DIAGNOSTIC_PUSH()
*
* save the compiler's diagnostic (enabled warnings, errors, ...) state
*
* @see MY_COMPILER_DIAGNOSTIC_POP()
*/
/**
* @def MY_COMPILER_DIAGNOSTIC_POP()
*
* restore the compiler's diagnostic (enabled warnings, errors, ...) state
*
* @see MY_COMPILER_DIAGNOSTIC_PUSH()
*/
#if defined(__clang__)
#define MY_COMPILER_DIAGNOSTIC_PUSH() MY_COMPILER_CLANG_DIAGNOSTIC_PUSH()
#define MY_COMPILER_DIAGNOSTIC_POP() MY_COMPILER_CLANG_DIAGNOSTIC_POP()
#elif defined(__GNUC__)
#define MY_COMPILER_DIAGNOSTIC_PUSH() MY_COMPILER_GCC_DIAGNOSTIC_PUSH()
#define MY_COMPILER_DIAGNOSTIC_POP() MY_COMPILER_GCC_DIAGNOSTIC_POP()
#elif defined(_MSC_VER)
#define MY_COMPILER_DIAGNOSTIC_PUSH() MY_COMPILER_MSVC_DIAGNOSTIC_PUSH()
#define MY_COMPILER_DIAGNOSTIC_POP() MY_COMPILER_MSVC_DIAGNOSTIC_POP()
#else
#define MY_COMPILER_DIAGNOSTIC_PUSH()
#define MY_COMPILER_DIAGNOSTIC_POP()
#endif
/**
* ignore -Wdocumentation compiler warnings for \@tparam.
*
* @code
* MY_COMPILER_DIAGNOSTIC_PUSH()
* MY_COMPILER_CLANG_WORKAROUND_TPARAM_DOCBUG()
* ...
* MY_COMPILER_DIAGNOSTIC_POP()
* @endcode
*
* @see MY_COMPILER_DIAGNOSTIC_PUSH()
* @see MY_COMPILER_DIAGNOSTIC_POP()
*
* allows to work around false positives -Wdocumentation warnings like:
*
* - \@tparam and explicitly instantiated templates
* https://bugs.llvm.org/show_bug.cgi?id=35144
*
*/
#define MY_COMPILER_CLANG_WORKAROUND_TPARAM_DOCBUG() \
MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Wdocumentation")
/**
* ignore -Wdocumentation compiler warnings for \@see \@ref
*
* @code
* MY_COMPILER_DIAGNOSTIC_PUSH()
* MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
* ...
* MY_COMPILER_DIAGNOSTIC_POP()
* @endcode
*
* @see MY_COMPILER_DIAGNOSTIC_PUSH()
* @see MY_COMPILER_DIAGNOSTIC_POP()
*
* allows to work around false positives -Wdocumentation warnings like:
*
* - \@sa \@ref
* - \@see \@ref
* - \@return \@ref
* https://bugs.llvm.org/show_bug.cgi?id=38905
*
*/
#define MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG() \
MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Wdocumentation")
/**
* ignore -Wunused-variable compiler warnings for \@see \@ref
*
* @code
* MY_COMPILER_DIAGNOSTIC_PUSH()
* MY_COMPILER_CLANG_WORKAROUND_FALSE_POSITIVE_UNUSED_VARIABLE_WARNING()
* ...
* MY_COMPILER_DIAGNOSTIC_POP()
* @endcode
*
* @see MY_COMPILER_DIAGNOSTIC_PUSH()
* @see MY_COMPILER_DIAGNOSTIC_POP()
*
* allows to work around false positives -Wunused-variable warnings like:
*
* - \@sa \@ref
* - \@see \@ref
* - \@return \@ref
* https://bugs.llvm.org/show_bug.cgi?id=46035
*
*/
#define MY_COMPILER_CLANG_WORKAROUND_FALSE_POSITIVE_UNUSED_VARIABLE_WARNING() \
MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Wunused-variable")
/**
* ignore -Wsuggest-attribute=format compiler warnings for \@see \@ref
*
* @code
* MY_COMPILER_DIAGNOSTIC_PUSH()
* MY_COMPILER_GCC_WORKAROUND_FALSE_POSITIVE_SUGGEST_ATTRIBUTE_FORMAT()
* ...
* MY_COMPILER_DIAGNOSTIC_POP()
* @endcode
*
* allows to work around false positives -Wsuggest-attribute=format warnings
* like:
*
* - \@sa \@ref
* - \@see \@ref
* - \@return \@ref
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116954
*
*/
#define MY_COMPILER_GCC_WORKAROUND_FALSE_POSITIVE_SUGGEST_ATTRIBUTE_FORMAT() \
MY_COMPILER_GCC_DIAGNOSTIC_IGNORE("-Wsuggest-attribute=format")
#endif /* MY_COMPILER_INCLUDED */