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#if GCC_VERSION_AT_LEAST(7, 0, 0)
209#define FALLTHROUGH __attribute__ ((fallthrough))
210#endif
211#endif
212
213#endif // !defined(FALLTHROUGH) && defined(__cplusplus) && defined(__has_cpp_attribute)
214
215#if !defined(FALLTHROUGH)
216#define FALLTHROUGH
217#endif
218
219/* LIKELY */
220
221#if !defined(LIKELY) && COMPILER(GCC_COMPATIBLE)
222#define LIKELY(x) __builtin_expect(!!(x), 1)
223#endif
224
225#if !defined(LIKELY)
226#define LIKELY(x) (x)
227#endif
228
229/* NEVER_INLINE */
230
231#if !defined(NEVER_INLINE) && COMPILER(GCC_COMPATIBLE)
232#define NEVER_INLINE __attribute__((__noinline__))
233#endif
234
235#if !defined(NEVER_INLINE) && COMPILER(MSVC)
236#define NEVER_INLINE __declspec(noinline)
237#endif
238
239#if !defined(NEVER_INLINE)
240#define NEVER_INLINE
241#endif
242
243/* NO_RETURN */
244
245#if !defined(NO_RETURN) && COMPILER(GCC_COMPATIBLE)
246#define NO_RETURN __attribute((__noreturn__))
247#endif
248
249#if !defined(NO_RETURN) && COMPILER(MSVC)
250#define NO_RETURN __declspec(noreturn)
251#endif
252
253#if !defined(NO_RETURN)
254#define NO_RETURN
255#endif
256
257/* NOT_TAIL_CALLED */
258
259#if !defined(NOT_TAIL_CALLED) && defined(__has_attribute)
260#if __has_attribute(not_tail_called)
261#define NOT_TAIL_CALLED __attribute__((not_tail_called))
262#endif
263#endif
264
265#if !defined(NOT_TAIL_CALLED)
266#define NOT_TAIL_CALLED
267#endif
268
269/* RETURNS_NONNULL */
270#if !defined(RETURNS_NONNULL) && COMPILER(GCC_COMPATIBLE)
271#define RETURNS_NONNULL __attribute__((returns_nonnull))
272#endif
273
274#if !defined(RETURNS_NONNULL)
275#define RETURNS_NONNULL
276#endif
277
278/* NO_RETURN_WITH_VALUE */
279
280#if !defined(NO_RETURN_WITH_VALUE) && !COMPILER(MSVC)
281#define NO_RETURN_WITH_VALUE NO_RETURN
282#endif
283
284#if !defined(NO_RETURN_WITH_VALUE)
285#define NO_RETURN_WITH_VALUE
286#endif
287
288/* OBJC_CLASS */
289
290#if !defined(OBJC_CLASS) && defined(__OBJC__)
291#define OBJC_CLASS @class
292#endif
293
294#if !defined(OBJC_CLASS)
295#define OBJC_CLASS class
296#endif
297
298/* OBJC_PROTOCOL */
299
300#if !defined(OBJC_PROTOCOL) && defined(__OBJC__)
301/* This forward-declares a protocol, then also creates a type of the same name based on NSObject.
302 * This allows us to use "NSObject<MyProtocol> *" or "MyProtocol *" more-or-less interchangably. */
303#define OBJC_PROTOCOL(protocolName) @protocol protocolName; using protocolName = NSObject<protocolName>
304#endif
305
306#if !defined(OBJC_PROTOCOL)
307#define OBJC_PROTOCOL(protocolName) class protocolName
308#endif
309
310/* PURE_FUNCTION */
311
312#if !defined(PURE_FUNCTION) && COMPILER(GCC_COMPATIBLE)
313#define PURE_FUNCTION __attribute__((__pure__))
314#endif
315
316#if !defined(PURE_FUNCTION)
317#define PURE_FUNCTION
318#endif
319
320/* UNUSED_FUNCTION */
321
322#if !defined(UNUSED_FUNCTION) && COMPILER(GCC_COMPATIBLE)
323#define UNUSED_FUNCTION __attribute__((unused))
324#endif
325
326#if !defined(UNUSED_FUNCTION)
327#define UNUSED_FUNCTION
328#endif
329
330/* REFERENCED_FROM_ASM */
331
332#if !defined(REFERENCED_FROM_ASM) && COMPILER(GCC_COMPATIBLE)
333#define REFERENCED_FROM_ASM __attribute__((__used__))
334#endif
335
336#if !defined(REFERENCED_FROM_ASM)
337#define REFERENCED_FROM_ASM
338#endif
339
340/* UNLIKELY */
341
342#if !defined(UNLIKELY) && COMPILER(GCC_COMPATIBLE)
343#define UNLIKELY(x) __builtin_expect(!!(x), 0)
344#endif
345
346#if !defined(UNLIKELY)
347#define UNLIKELY(x) (x)
348#endif
349
350/* UNUSED_LABEL */
351
352/* Keep the compiler from complaining for a local label that is defined but not referenced. */
353/* Helpful when mixing hand-written and autogenerated code. */
354
355#if !defined(UNUSED_LABEL) && COMPILER(MSVC)
356#define UNUSED_LABEL(label) if (false) goto label
357#endif
358
359#if !defined(UNUSED_LABEL)
360#define UNUSED_LABEL(label) UNUSED_PARAM(&& label)
361#endif
362
363/* UNUSED_PARAM */
364
365#if !defined(UNUSED_PARAM)
366#define UNUSED_PARAM(variable) (void)variable
367#endif
368
369/* UNUSED_VARIABLE */
370#if !defined(UNUSED_VARIABLE)
371#define UNUSED_VARIABLE(variable) UNUSED_PARAM(variable)
372#endif
373
374/* WARN_UNUSED_RETURN */
375
376#if !defined(WARN_UNUSED_RETURN) && COMPILER(GCC_COMPATIBLE)
377#define WARN_UNUSED_RETURN __attribute__((__warn_unused_result__))
378#endif
379
380#if !defined(WARN_UNUSED_RETURN)
381#define WARN_UNUSED_RETURN
382#endif
383
384#if !defined(__has_include) && COMPILER(MSVC)
385#define __has_include(path) 0
386#endif
387
388/* IGNORE_WARNINGS */
389
390/* Can't use WTF_CONCAT() and STRINGIZE() because they are defined in
391 * StdLibExtras.h, which includes this file. */
392#define _COMPILER_CONCAT_I(a, b) a ## b
393#define _COMPILER_CONCAT(a, b) _COMPILER_CONCAT_I(a, b)
394
395#define _COMPILER_STRINGIZE(exp) #exp
396
397#define _COMPILER_WARNING_NAME(warning) "-W" warning
398
399#if COMPILER(GCC) || COMPILER(CLANG)
400#define IGNORE_WARNINGS_BEGIN_COND(cond, compiler, warning) \
401 _Pragma(_COMPILER_STRINGIZE(compiler diagnostic push)) \
402 _COMPILER_CONCAT(IGNORE_WARNINGS_BEGIN_IMPL_, cond)(compiler, warning)
403
404#define IGNORE_WARNINGS_BEGIN_IMPL_1(compiler, warning) \
405 _Pragma(_COMPILER_STRINGIZE(compiler diagnostic ignored warning))
406#define IGNORE_WARNINGS_BEGIN_IMPL_0(compiler, warning)
407#define IGNORE_WARNINGS_BEGIN_IMPL_(compiler, warning)
408
409
410#define IGNORE_WARNINGS_END_IMPL(compiler) _Pragma(_COMPILER_STRINGIZE(compiler diagnostic pop))
411
412#if defined(__has_warning)
413#define _IGNORE_WARNINGS_BEGIN_IMPL(compiler, warning) \
414 IGNORE_WARNINGS_BEGIN_COND(__has_warning(warning), compiler, warning)
415#else
416#define _IGNORE_WARNINGS_BEGIN_IMPL(compiler, warning) IGNORE_WARNINGS_BEGIN_COND(1, compiler, warning)
417#endif
418
419#define IGNORE_WARNINGS_BEGIN_IMPL(compiler, warning) \
420 _IGNORE_WARNINGS_BEGIN_IMPL(compiler, _COMPILER_WARNING_NAME(warning))
421
422#endif // COMPILER(GCC) || COMPILER(CLANG)
423
424
425#if COMPILER(GCC)
426#define IGNORE_GCC_WARNINGS_BEGIN(warning) IGNORE_WARNINGS_BEGIN_IMPL(GCC, warning)
427#define IGNORE_GCC_WARNINGS_END IGNORE_WARNINGS_END_IMPL(GCC)
428#else
429#define IGNORE_GCC_WARNINGS_BEGIN(warning)
430#define IGNORE_GCC_WARNINGS_END
431#endif
432
433#if COMPILER(CLANG)
434#define IGNORE_CLANG_WARNINGS_BEGIN(warning) IGNORE_WARNINGS_BEGIN_IMPL(clang, warning)
435#define IGNORE_CLANG_WARNINGS_END IGNORE_WARNINGS_END_IMPL(clang)
436#else
437#define IGNORE_CLANG_WARNINGS_BEGIN(warning)
438#define IGNORE_CLANG_WARNINGS_END
439#endif
440
441#if COMPILER(GCC) || COMPILER(CLANG)
442#define IGNORE_WARNINGS_BEGIN(warning) IGNORE_WARNINGS_BEGIN_IMPL(GCC, warning)
443#define IGNORE_WARNINGS_END IGNORE_WARNINGS_END_IMPL(GCC)
444#else
445#define IGNORE_WARNINGS_BEGIN(warning)
446#define IGNORE_WARNINGS_END
447#endif
448
449#define ALLOW_DEPRECATED_DECLARATIONS_BEGIN IGNORE_WARNINGS_BEGIN("deprecated-declarations")
450#define ALLOW_DEPRECATED_DECLARATIONS_END IGNORE_WARNINGS_END
451
452#define ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN IGNORE_CLANG_WARNINGS_BEGIN("unguarded-availability-new")
453#define ALLOW_NEW_API_WITHOUT_GUARDS_END IGNORE_CLANG_WARNINGS_END
454
455#define ALLOW_UNUSED_PARAMETERS_BEGIN IGNORE_WARNINGS_BEGIN("unused-parameter")
456#define ALLOW_UNUSED_PARAMETERS_END IGNORE_WARNINGS_END
457
458#define ALLOW_NONLITERAL_FORMAT_BEGIN IGNORE_WARNINGS_BEGIN("format-nonliteral")
459#define ALLOW_NONLITERAL_FORMAT_END IGNORE_WARNINGS_END
460
461#define IGNORE_RETURN_TYPE_WARNINGS_BEGIN IGNORE_WARNINGS_BEGIN("return-type")
462#define IGNORE_RETURN_TYPE_WARNINGS_END IGNORE_WARNINGS_END
463
464#define IGNORE_NULL_CHECK_WARNINGS_BEGIN IGNORE_WARNINGS_BEGIN("nonnull")
465#define IGNORE_NULL_CHECK_WARNINGS_END IGNORE_WARNINGS_END
466