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 /* OVERRIDE and FINAL */
    209 
    210 #if COMPILER_SUPPORTS(CXX_OVERRIDE_CONTROL)
    211 #define OVERRIDE override
    212 
    213 #if COMPILER_QUIRK(FINAL_IS_CALLED_SEALED)
    214 #define FINAL sealed
    215 #else
    216 #define FINAL final
    217 #endif
    218 
    219 #else
    220 #define OVERRIDE
    221 #define FINAL
    222 #endif
    223 
    224 /* REFERENCED_FROM_ASM */
    225 
    226 #ifndef REFERENCED_FROM_ASM
    227 #if COMPILER(GCC)
    228 #define REFERENCED_FROM_ASM __attribute__((used))
    229 #else
    230 #define REFERENCED_FROM_ASM
    231 #endif
    232 #endif
    233 
    234 /* OBJC_CLASS */
    235 
    236 #ifndef OBJC_CLASS
    237 #ifdef __OBJC__
    238 #define OBJC_CLASS @class
    239 #else
    240 #define OBJC_CLASS class
    241 #endif
    242 #endif
    243 
    244 /* ABI */
    245 #if defined(__ARM_EABI__) || defined(__EABI__)
    246 #define WTF_COMPILER_SUPPORTS_EABI 1
    247 #endif
    248 
    249 #endif /* WTF_Compiler_h */
    250