1/*
2 * Copyright (C) 2011, 2012, 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28/* COMPILER() - the compiler being used to build the project */
29#define COMPILER(WTF_FEATURE) (defined WTF_COMPILER_##WTF_FEATURE && WTF_COMPILER_##WTF_FEATURE)
30
31/* COMPILER_SUPPORTS() - whether the compiler being used to build the project supports the given feature. */
32#define COMPILER_SUPPORTS(WTF_COMPILER_FEATURE) (defined WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE && WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE)
33
34/* COMPILER_QUIRK() - whether the compiler being used to build the project requires a given quirk. */
35#define COMPILER_QUIRK(WTF_COMPILER_QUIRK) (defined WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK && WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK)
36
37/* COMPILER_HAS_CLANG_BUILTIN() - whether the compiler supports a particular clang builtin. */
38#ifdef __has_builtin
39#define COMPILER_HAS_CLANG_BUILTIN(x) __has_builtin(x)
40#else
41#define COMPILER_HAS_CLANG_BUILTIN(x) 0
42#endif
43
44/* COMPILER_HAS_CLANG_FEATURE() - whether the compiler supports a particular language or library feature. */
45/* http://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension */
46#ifdef __has_feature
47#define COMPILER_HAS_CLANG_FEATURE(x) __has_feature(x)
48#else
49#define COMPILER_HAS_CLANG_FEATURE(x) 0
50#endif
51
52/* COMPILER_HAS_CLANG_DECLSPEC() - whether the compiler supports a Microsoft style __declspec attribute. */
53/* https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute */
54#ifdef __has_declspec_attribute
55#define COMPILER_HAS_CLANG_DECLSPEC(x) __has_declspec_attribute(x)
56#else
57#define COMPILER_HAS_CLANG_DECLSPEC(x) 0
58#endif
59
60/* ==== COMPILER() - primary detection of the compiler being used to build the project, in alphabetical order ==== */
61
62/* COMPILER(CLANG) - Clang */
63
64#if defined(__clang__)
65#define WTF_COMPILER_CLANG 1
66#define WTF_COMPILER_SUPPORTS_BLOCKS COMPILER_HAS_CLANG_FEATURE(blocks)
67#define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT COMPILER_HAS_CLANG_FEATURE(c_static_assert)
68#define WTF_COMPILER_SUPPORTS_CXX_EXCEPTIONS COMPILER_HAS_CLANG_FEATURE(cxx_exceptions)
69#define WTF_COMPILER_SUPPORTS_BUILTIN_IS_TRIVIALLY_COPYABLE COMPILER_HAS_CLANG_FEATURE(is_trivially_copyable)
70
71#ifdef __cplusplus
72#if __cplusplus <= 201103L
73#define WTF_CPP_STD_VER 11
74#elif __cplusplus <= 201402L
75#define WTF_CPP_STD_VER 14
76#elif __cplusplus <= 201703L
77#define WTF_CPP_STD_VER 17
78#endif
79#endif
80
81#endif // defined(__clang__)
82
83/* COMPILER(GCC_COMPATIBLE) - GNU Compiler Collection or compatibles */
84#if defined(__GNUC__)
85#define WTF_COMPILER_GCC_COMPATIBLE 1
86#endif
87
88/* COMPILER(GCC) - GNU Compiler Collection */
89/* Note: This section must come after the Clang section since we check !COMPILER(CLANG) here. */
90#if COMPILER(GCC_COMPATIBLE) && !COMPILER(CLANG)
91#define WTF_COMPILER_GCC 1
92
93#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
94#define GCC_VERSION_AT_LEAST(major, minor, patch) (GCC_VERSION >= (major * 10000 + minor * 100 + patch))
95
96#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
97#define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT 1
98#endif
99
100#endif /* COMPILER(GCC) */
101
102#if COMPILER(GCC_COMPATIBLE) && defined(NDEBUG) && !defined(__OPTIMIZE__) && !defined(RELEASE_WITHOUT_OPTIMIZATIONS)
103#error "Building release without compiler optimizations: WebKit will be slow. Set -DRELEASE_WITHOUT_OPTIMIZATIONS if this is intended."
104#endif
105
106/* COMPILER(MINGW) - MinGW GCC */
107
108#if defined(__MINGW32__)
109#define WTF_COMPILER_MINGW 1
110#include <_mingw.h>
111#endif
112
113/* COMPILER(MINGW64) - mingw-w64 GCC - used as additional check to exclude mingw.org specific functions */
114
115/* Note: This section must come after the MinGW section since we check COMPILER(MINGW) here. */
116
117#if COMPILER(MINGW) && defined(__MINGW64_VERSION_MAJOR) /* best way to check for mingw-w64 vs mingw.org */
118#define WTF_COMPILER_MINGW64 1
119#endif
120
121/* COMPILER(MSVC) - Microsoft Visual C++ */
122
123#if defined(_MSC_VER)
124
125#define WTF_COMPILER_MSVC 1
126
127#if _MSC_VER < 1910
128#error "Please use a newer version of Visual Studio. WebKit requires VS2017 or newer to compile."
129#endif
130
131#endif
132
133#if !COMPILER(CLANG) && !COMPILER(MSVC)
134#define WTF_COMPILER_QUIRK_CONSIDERS_UNREACHABLE_CODE 1
135#endif
136
137/* ==== COMPILER_SUPPORTS - additional compiler feature detection, in alphabetical order ==== */
138
139/* COMPILER_SUPPORTS(EABI) */
140
141#if defined(__ARM_EABI__) || defined(__EABI__)
142#define WTF_COMPILER_SUPPORTS_EABI 1
143#endif
144
145/* ASAN_ENABLED and SUPPRESS_ASAN */
146
147#ifdef __SANITIZE_ADDRESS__
148#define ASAN_ENABLED 1
149#else
150#define ASAN_ENABLED COMPILER_HAS_CLANG_FEATURE(address_sanitizer)
151#endif
152
153#if ASAN_ENABLED
154#define SUPPRESS_ASAN __attribute__((no_sanitize_address))
155#else
156#define SUPPRESS_ASAN
157#endif
158
159/* ==== Compiler-independent macros for various compiler features, in alphabetical order ==== */
160
161/* ALWAYS_INLINE */
162
163/* In GCC functions marked with no_sanitize_address cannot call functions that are marked with always_inline and not marked with no_sanitize_address.
164 * Therefore we need to give up on the enforcement of ALWAYS_INLINE when bulding with ASAN. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 */
165#if !defined(ALWAYS_INLINE) && COMPILER(GCC_COMPATIBLE) && defined(NDEBUG) && !COMPILER(MINGW) && !(COMPILER(GCC) && ASAN_ENABLED)
166#define ALWAYS_INLINE inline __attribute__((__always_inline__))
167#endif
168
169#if !defined(ALWAYS_INLINE) && COMPILER(MSVC) && defined(NDEBUG)
170#define ALWAYS_INLINE __forceinline
171#endif
172
173#if !defined(ALWAYS_INLINE)
174#define ALWAYS_INLINE inline
175#endif
176
177#if COMPILER(MSVC)
178#define ALWAYS_INLINE_EXCEPT_MSVC inline
179#else
180#define ALWAYS_INLINE_EXCEPT_MSVC ALWAYS_INLINE
181#endif
182
183/* WTF_EXTERN_C_{BEGIN, END} */
184
185#ifdef __cplusplus
186#define WTF_EXTERN_C_BEGIN extern "C" {
187#define WTF_EXTERN_C_END }
188#else
189#define WTF_EXTERN_C_BEGIN
190#define WTF_EXTERN_C_END
191#endif
192
193/* FALLTHROUGH */
194
195#if !defined(FALLTHROUGH) && defined(__cplusplus) && defined(__has_cpp_attribute)
196
197#if __has_cpp_attribute(fallthrough)
198#define FALLTHROUGH [[fallthrough]]
199#elif __has_cpp_attribute(clang::fallthrough)
200#define FALLTHROUGH [[clang::fallthrough]]
201#elif __has_cpp_attribute(gnu::fallthrough)
202#define FALLTHROUGH [[gnu::fallthrough]]
203#endif
204
205#elif !defined(FALLTHROUGH) && !defined(__cplusplus)
206
207#if COMPILER(GCC)
208#define FALLTHROUGH __attribute__ ((fallthrough))
209#endif
210
211#endif // !defined(FALLTHROUGH) && defined(__cplusplus) && defined(__has_cpp_attribute)
212
213#if !defined(FALLTHROUGH)
214#define FALLTHROUGH
215#endif
216
217/* LIKELY */
218
219#if !defined(LIKELY) && COMPILER(GCC_COMPATIBLE)
220#define LIKELY(x) __builtin_expect(!!(x), 1)
221#endif
222
223#if !defined(LIKELY)
224#define LIKELY(x) (x)
225#endif
226
227/* NEVER_INLINE */
228
229#if !defined(NEVER_INLINE) && COMPILER(GCC_COMPATIBLE)
230#define NEVER_INLINE __attribute__((__noinline__))
231#endif
232
233#if !defined(NEVER_INLINE) && COMPILER(MSVC)
234#define NEVER_INLINE __declspec(noinline)
235#endif
236
237#if !defined(NEVER_INLINE)
238#define NEVER_INLINE
239#endif
240
241/* NO_RETURN */
242
243#if !defined(NO_RETURN) && COMPILER(GCC_COMPATIBLE)
244#define NO_RETURN __attribute((__noreturn__))
245#endif
246
247#if !defined(NO_RETURN) && COMPILER(MSVC)
248#define NO_RETURN __declspec(noreturn)
249#endif
250
251#if !defined(NO_RETURN)
252#define NO_RETURN
253#endif
254
255/* NOT_TAIL_CALLED */
256
257#if !defined(NOT_TAIL_CALLED) && defined(__has_attribute)
258#if __has_attribute(not_tail_called)
259#define NOT_TAIL_CALLED __attribute__((not_tail_called))
260#endif
261#endif
262
263#if !defined(NOT_TAIL_CALLED)
264#define NOT_TAIL_CALLED
265#endif
266
267/* RETURNS_NONNULL */
268#if !defined(RETURNS_NONNULL) && COMPILER(GCC_COMPATIBLE)
269#define RETURNS_NONNULL __attribute__((returns_nonnull))
270#endif
271
272#if !defined(RETURNS_NONNULL)
273#define RETURNS_NONNULL
274#endif
275
276/* NO_RETURN_WITH_VALUE */
277
278#if !defined(NO_RETURN_WITH_VALUE) && !COMPILER(MSVC)
279#define NO_RETURN_WITH_VALUE NO_RETURN
280#endif
281
282#if !defined(NO_RETURN_WITH_VALUE)
283#define NO_RETURN_WITH_VALUE
284#endif
285
286/* OBJC_CLASS */
287
288#if !defined(OBJC_CLASS) && defined(__OBJC__)
289#define OBJC_CLASS @class
290#endif
291
292#if !defined(OBJC_CLASS)
293#define OBJC_CLASS class
294#endif
295
296/* OBJC_PROTOCOL */
297
298#if !defined(OBJC_PROTOCOL) && defined(__OBJC__)
299/* This forward-declares a protocol, then also creates a type of the same name based on NSObject.
300 * This allows us to use "NSObject<MyProtocol> *" or "MyProtocol *" more-or-less interchangably. */
301#define OBJC_PROTOCOL(protocolName) @protocol protocolName; using protocolName = NSObject<protocolName>
302#endif
303
304#if !defined(OBJC_PROTOCOL)
305#define OBJC_PROTOCOL(protocolName) class protocolName
306#endif
307
308/* PURE_FUNCTION */
309
310#if !defined(PURE_FUNCTION) && COMPILER(GCC_COMPATIBLE)
311#define PURE_FUNCTION __attribute__((__pure__))
312#endif
313
314#if !defined(PURE_FUNCTION)
315#define PURE_FUNCTION
316#endif
317
318/* UNUSED_FUNCTION */
319
320#if !defined(UNUSED_FUNCTION) && COMPILER(GCC_COMPATIBLE)
321#define UNUSED_FUNCTION __attribute__((unused))
322#endif
323
324#if !defined(UNUSED_FUNCTION)
325#define UNUSED_FUNCTION
326#endif
327
328/* REFERENCED_FROM_ASM */
329
330#if !defined(REFERENCED_FROM_ASM) && COMPILER(GCC_COMPATIBLE)
331#define REFERENCED_FROM_ASM __attribute__((__used__))
332#endif
333
334#if !defined(REFERENCED_FROM_ASM)
335#define REFERENCED_FROM_ASM
336#endif
337
338/* UNLIKELY */
339
340#if !defined(UNLIKELY) && COMPILER(GCC_COMPATIBLE)
341#define UNLIKELY(x) __builtin_expect(!!(x), 0)
342#endif
343
344#if !defined(UNLIKELY)
345#define UNLIKELY(x) (x)
346#endif
347
348/* UNUSED_LABEL */
349
350/* Keep the compiler from complaining for a local label that is defined but not referenced. */
351/* Helpful when mixing hand-written and autogenerated code. */
352
353#if !defined(UNUSED_LABEL) && COMPILER(MSVC)
354#define UNUSED_LABEL(label) if (false) goto label
355#endif
356
357#if !defined(UNUSED_LABEL)
358#define UNUSED_LABEL(label) UNUSED_PARAM(&& label)
359#endif
360
361/* UNUSED_PARAM */
362
363#if !defined(UNUSED_PARAM)
364#define UNUSED_PARAM(variable) (void)variable
365#endif
366
367/* UNUSED_VARIABLE */
368#if !defined(UNUSED_VARIABLE)
369#define UNUSED_VARIABLE(variable) UNUSED_PARAM(variable)
370#endif
371
372/* WARN_UNUSED_RETURN */
373
374#if !defined(WARN_UNUSED_RETURN) && COMPILER(GCC_COMPATIBLE)
375#define WARN_UNUSED_RETURN __attribute__((__warn_unused_result__))
376#endif
377
378#if !defined(WARN_UNUSED_RETURN)
379#define WARN_UNUSED_RETURN
380#endif
381
382#if !defined(__has_include) && COMPILER(MSVC)
383#define __has_include(path) 0
384#endif
385
386/* IGNORE_WARNINGS */
387
388/* Can't use WTF_CONCAT() and STRINGIZE() because they are defined in
389 * StdLibExtras.h, which includes this file. */
390#define _COMPILER_CONCAT_I(a, b) a ## b
391#define _COMPILER_CONCAT(a, b) _COMPILER_CONCAT_I(a, b)
392
393#define _COMPILER_STRINGIZE(exp) #exp
394
395#define _COMPILER_WARNING_NAME(warning) "-W" warning
396
397#if COMPILER(GCC) || COMPILER(CLANG)
398#define IGNORE_WARNINGS_BEGIN_COND(cond, compiler, warning) \
399 _Pragma(_COMPILER_STRINGIZE(compiler diagnostic push)) \
400 _COMPILER_CONCAT(IGNORE_WARNINGS_BEGIN_IMPL_, cond)(compiler, warning)
401
402#define IGNORE_WARNINGS_BEGIN_IMPL_1(compiler, warning) \
403 _Pragma(_COMPILER_STRINGIZE(compiler diagnostic ignored warning))
404#define IGNORE_WARNINGS_BEGIN_IMPL_0(compiler, warning)
405#define IGNORE_WARNINGS_BEGIN_IMPL_(compiler, warning)
406
407
408#define IGNORE_WARNINGS_END_IMPL(compiler) _Pragma(_COMPILER_STRINGIZE(compiler diagnostic pop))
409
410#if defined(__has_warning)
411#define _IGNORE_WARNINGS_BEGIN_IMPL(compiler, warning) \
412 IGNORE_WARNINGS_BEGIN_COND(__has_warning(warning), compiler, warning)
413#else
414#define _IGNORE_WARNINGS_BEGIN_IMPL(compiler, warning) IGNORE_WARNINGS_BEGIN_COND(1, compiler, warning)
415#endif
416
417#define IGNORE_WARNINGS_BEGIN_IMPL(compiler, warning) \
418 _IGNORE_WARNINGS_BEGIN_IMPL(compiler, _COMPILER_WARNING_NAME(warning))
419
420#endif // COMPILER(GCC) || COMPILER(CLANG)
421
422
423#if COMPILER(GCC)
424#define IGNORE_GCC_WARNINGS_BEGIN(warning) IGNORE_WARNINGS_BEGIN_IMPL(GCC, warning)
425#define IGNORE_GCC_WARNINGS_END IGNORE_WARNINGS_END_IMPL(GCC)
426#else
427#define IGNORE_GCC_WARNINGS_BEGIN(warning)
428#define IGNORE_GCC_WARNINGS_END
429#endif
430
431#if COMPILER(CLANG)
432#define IGNORE_CLANG_WARNINGS_BEGIN(warning) IGNORE_WARNINGS_BEGIN_IMPL(clang, warning)
433#define IGNORE_CLANG_WARNINGS_END IGNORE_WARNINGS_END_IMPL(clang)
434#else
435#define IGNORE_CLANG_WARNINGS_BEGIN(warning)
436#define IGNORE_CLANG_WARNINGS_END
437#endif
438
439#if COMPILER(GCC) || COMPILER(CLANG)
440#define IGNORE_WARNINGS_BEGIN(warning) IGNORE_WARNINGS_BEGIN_IMPL(GCC, warning)
441#define IGNORE_WARNINGS_END IGNORE_WARNINGS_END_IMPL(GCC)
442#else
443#define IGNORE_WARNINGS_BEGIN(warning)
444#define IGNORE_WARNINGS_END
445#endif
446
447#define ALLOW_DEPRECATED_DECLARATIONS_BEGIN IGNORE_WARNINGS_BEGIN("deprecated-declarations")
448#define ALLOW_DEPRECATED_DECLARATIONS_END IGNORE_WARNINGS_END
449
450#define ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN IGNORE_WARNINGS_BEGIN("deprecated-implementations")
451#define ALLOW_DEPRECATED_IMPLEMENTATIONS_END IGNORE_WARNINGS_END
452
453#define ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN IGNORE_CLANG_WARNINGS_BEGIN("unguarded-availability-new")
454#define ALLOW_NEW_API_WITHOUT_GUARDS_END IGNORE_CLANG_WARNINGS_END
455
456#define ALLOW_UNUSED_PARAMETERS_BEGIN IGNORE_WARNINGS_BEGIN("unused-parameter")
457#define ALLOW_UNUSED_PARAMETERS_END IGNORE_WARNINGS_END
458
459#define ALLOW_NONLITERAL_FORMAT_BEGIN IGNORE_WARNINGS_BEGIN("format-nonliteral")
460#define ALLOW_NONLITERAL_FORMAT_END IGNORE_WARNINGS_END
461
462#define IGNORE_RETURN_TYPE_WARNINGS_BEGIN IGNORE_WARNINGS_BEGIN("return-type")
463#define IGNORE_RETURN_TYPE_WARNINGS_END IGNORE_WARNINGS_END
464
465#define IGNORE_NULL_CHECK_WARNINGS_BEGIN IGNORE_WARNINGS_BEGIN("nonnull")
466#define IGNORE_NULL_CHECK_WARNINGS_END IGNORE_WARNINGS_END
467