Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2011, 2012 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 #ifndef WTF_Compiler_h
     27 #define WTF_Compiler_h
     28 
     29 /* COMPILER() - the compiler being used to build the project */
     30 #define COMPILER(WTF_FEATURE) (defined WTF_COMPILER_##WTF_FEATURE  && WTF_COMPILER_##WTF_FEATURE)
     31 
     32 /* COMPILER_SUPPORTS() - whether the compiler being used to build the project supports the given feature. */
     33 #define COMPILER_SUPPORTS(WTF_COMPILER_FEATURE) (defined WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE  && WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE)
     34 
     35 /* COMPILER_QUIRK() - whether the compiler being used to build the project requires a given quirk. */
     36 #define COMPILER_QUIRK(WTF_COMPILER_QUIRK) (defined WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK  && WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK)
     37 
     38 /* ==== COMPILER() - the compiler being used to build the project ==== */
     39 
     40 /* COMPILER(CLANG) - Clang  */
     41 #if defined(__clang__)
     42 #define WTF_COMPILER_CLANG 1
     43 
     44 #define CLANG_PRAGMA(PRAGMA) _Pragma(PRAGMA)
     45 
     46 /* Specific compiler features */
     47 #define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES __has_extension(cxx_variadic_templates)
     48 
     49 /* There is a bug in clang that comes with Xcode 4.2 where AtomicStrings can't be implicitly converted to Strings
     50    in the presence of move constructors and/or move assignment operators. This bug has been fixed in Xcode 4.3 clang, so we
     51    check for both cxx_rvalue_references as well as the unrelated cxx_nonstatic_member_init feature which we know was added in 4.3 */
     52 #define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES __has_extension(cxx_rvalue_references) && __has_extension(cxx_nonstatic_member_init)
     53 
     54 #define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS __has_extension(cxx_deleted_functions)
     55 #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR __has_feature(cxx_nullptr)
     56 #define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS __has_feature(cxx_explicit_conversions)
     57 #define WTF_COMPILER_SUPPORTS_BLOCKS __has_feature(blocks)
     58 #define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT __has_extension(c_static_assert)
     59 #define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT __has_extension(cxx_static_assert)
     60 #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL __has_extension(cxx_override_control)
     61 #define WTF_COMPILER_SUPPORTS_HAS_TRIVIAL_DESTRUCTOR __has_extension(has_trivial_destructor)
     62 #define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS __has_extension(cxx_strong_enums)
     63 
     64 #endif
     65 
     66 #ifndef CLANG_PRAGMA
     67 #define CLANG_PRAGMA(PRAGMA)
     68 #endif
     69 
     70 /* COMPILER(MSVC) - Microsoft Visual C++ */
     71 #if defined(_MSC_VER)
     72 #define WTF_COMPILER_MSVC 1
     73 
     74 /* Specific compiler features */
     75 #if !COMPILER(CLANG) && _MSC_VER >= 1600
     76 #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR 1
     77 #endif
     78 
     79 #if !COMPILER(CLANG)
     80 #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL 1
     81 #define WTF_COMPILER_QUIRK_FINAL_IS_CALLED_SEALED 1
     82 #endif
     83 
     84 #endif
     85 
     86 /* COMPILER(GCC) - GNU Compiler Collection */
     87 #if defined(__GNUC__)
     88 #define WTF_COMPILER_GCC 1
     89 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
     90 #define GCC_VERSION_AT_LEAST(major, minor, patch) (GCC_VERSION >= (major * 10000 + minor * 100 + patch))
     91 #else
     92 /* Define this for !GCC compilers, just so we can write things like GCC_VERSION_AT_LEAST(4, 1, 0). */
     93 #define GCC_VERSION_AT_LEAST(major, minor, patch) 0
     94 #endif
     95 
     96 /* Specific compiler features */
     97 #if COMPILER(GCC) && !COMPILER(CLANG)
     98 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
     99 /* C11 support */
    100 #define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT 1
    101 #endif
    102 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(__cplusplus) && __cplusplus >= 201103L)
    103 /* C++11 support */
    104 #if GCC_VERSION_AT_LEAST(4, 3, 0)
    105 #define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES 1
    106 #define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT 1
    107 #define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES 1
    108 #endif
    109 #if GCC_VERSION_AT_LEAST(4, 4, 0)
    110 #define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS 1
    111 #endif
    112 #if GCC_VERSION_AT_LEAST(4, 5, 0)
    113 #define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS 1
    114 #endif
    115 #if GCC_VERSION_AT_LEAST(4, 6, 0)
    116 #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR 1
    117 /* Strong enums should work from gcc 4.4, but doesn't seem to support some operators */
    118 #define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS 1
    119 #endif
    120 #if GCC_VERSION_AT_LEAST(4, 7, 0)
    121 #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL 1
    122 #endif
    123 #endif /* defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(__cplusplus) && __cplusplus >= 201103L) */
    124 #endif /* COMPILER(GCC) */
    125 
    126 /* ==== Compiler features ==== */
    127 
    128 
    129 /* ALWAYS_INLINE */
    130 
    131 #ifndef ALWAYS_INLINE
    132 #if COMPILER(GCC) && defined(NDEBUG) && !COMPILER(MINGW)
    133 #define ALWAYS_INLINE inline __attribute__((__always_inline__))
    134 #elif COMPILER(MSVC) && defined(NDEBUG)
    135 #define ALWAYS_INLINE __forceinline
    136 #else
    137 #define ALWAYS_INLINE inline
    138 #endif
    139 #endif
    140 
    141 
    142 /* NEVER_INLINE */
    143 
    144 #ifndef NEVER_INLINE
    145 #if COMPILER(GCC)
    146 #define NEVER_INLINE __attribute__((__noinline__))
    147 #else
    148 #define NEVER_INLINE
    149 #endif
    150 #endif
    151 
    152 
    153 /* UNLIKELY */
    154 
    155 #ifndef UNLIKELY
    156 #if COMPILER(GCC)
    157 #define UNLIKELY(x) __builtin_expect((x), 0)
    158 #else
    159 #define UNLIKELY(x) (x)
    160 #endif
    161 #endif
    162 
    163 
    164 /* LIKELY */
    165 
    166 #ifndef LIKELY
    167 #if COMPILER(GCC)
    168 #define LIKELY(x) __builtin_expect((x), 1)
    169 #else
    170 #define LIKELY(x) (x)
    171 #endif
    172 #endif
    173 
    174 
    175 /* NO_RETURN */
    176 
    177 
    178 #ifndef NO_RETURN
    179 #if COMPILER(GCC)
    180 #define NO_RETURN __attribute((__noreturn__))
    181 #elif COMPILER(MSVC)
    182 #define NO_RETURN __declspec(noreturn)
    183 #else
    184 #define NO_RETURN
    185 #endif
    186 #endif
    187 
    188 
    189 /* NO_RETURN_WITH_VALUE */
    190 
    191 #ifndef NO_RETURN_WITH_VALUE
    192 #if !COMPILER(MSVC)
    193 #define NO_RETURN_WITH_VALUE NO_RETURN
    194 #else
    195 #define NO_RETURN_WITH_VALUE
    196 #endif
    197 #endif
    198 
    199 
    200 /* WARN_UNUSED_RETURN */
    201 
    202 #if COMPILER(GCC)
    203 #define WARN_UNUSED_RETURN __attribute__ ((warn_unused_result))
    204 #else
    205 #define WARN_UNUSED_RETURN
    206 #endif
    207 
    208 /* ALLOW_UNUSED */
    209 
    210 #if COMPILER(GCC)
    211 #define ALLOW_UNUSED __attribute__((unused))
    212 #else
    213 #define ALLOW_UNUSED
    214 #endif
    215 
    216 /* OVERRIDE and FINAL */
    217 
    218 #if COMPILER_SUPPORTS(CXX_OVERRIDE_CONTROL)
    219 #define OVERRIDE override
    220 
    221 #if COMPILER_QUIRK(FINAL_IS_CALLED_SEALED)
    222 #define FINAL sealed
    223 #else
    224 #define FINAL final
    225 #endif
    226 
    227 #else
    228 #define OVERRIDE
    229 #define FINAL
    230 #endif
    231 
    232 /* WTF_DELETED_FUNCTION */
    233 
    234 #if COMPILER_SUPPORTS(CXX_DELETED_FUNCTIONS)
    235 #define WTF_DELETED_FUNCTION = delete
    236 #else
    237 #define WTF_DELETED_FUNCTION
    238 #endif
    239 
    240 /* REFERENCED_FROM_ASM */
    241 
    242 #ifndef REFERENCED_FROM_ASM
    243 #if COMPILER(GCC)
    244 #define REFERENCED_FROM_ASM __attribute__((used))
    245 #else
    246 #define REFERENCED_FROM_ASM
    247 #endif
    248 #endif
    249 
    250 /* OBJC_CLASS */
    251 
    252 #ifndef OBJC_CLASS
    253 #ifdef __OBJC__
    254 #define OBJC_CLASS @class
    255 #else
    256 #define OBJC_CLASS class
    257 #endif
    258 #endif
    259 
    260 /* ABI */
    261 #if defined(__ARM_EABI__) || defined(__EABI__)
    262 #define WTF_COMPILER_SUPPORTS_EABI 1
    263 #endif
    264 
    265 /* WTF_PRETTY_FUNCTION */
    266 
    267 #if COMPILER(GCC)
    268 #define WTF_COMPILER_SUPPORTS_PRETTY_FUNCTION 1
    269 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
    270 #elif COMPILER(MSVC)
    271 #define WTF_COMPILER_SUPPORTS_PRETTY_FUNCTION 1
    272 #define WTF_PRETTY_FUNCTION __FUNCDNAME__
    273 #else
    274 #define WTF_PRETTY_FUNCTION __FUNCTION__
    275 #endif
    276 
    277 #endif /* WTF_Compiler_h */
    278