Compiler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #ifndef WTF_Compiler_h
  26. #define WTF_Compiler_h
  27. /* COMPILER() - the compiler being used to build the project */
  28. #define COMPILER(WTF_FEATURE) (defined WTF_COMPILER_##WTF_FEATURE && WTF_COMPILER_##WTF_FEATURE)
  29. /* COMPILER_SUPPORTS() - whether the compiler being used to build the project supports the given feature. */
  30. #define COMPILER_SUPPORTS(WTF_COMPILER_FEATURE) (defined WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE && WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE)
  31. /* COMPILER_QUIRK() - whether the compiler being used to build the project requires a given quirk. */
  32. #define COMPILER_QUIRK(WTF_COMPILER_QUIRK) (defined WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK && WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK)
  33. /* ==== COMPILER() - the compiler being used to build the project ==== */
  34. /* COMPILER(CLANG) - Clang */
  35. #if defined(__clang__)
  36. #define WTF_COMPILER_CLANG 1
  37. #define CLANG_PRAGMA(PRAGMA) _Pragma(PRAGMA)
  38. /* Specific compiler features */
  39. #define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES __has_feature(cxx_variadic_templates)
  40. /* There is a bug in clang that comes with Xcode 4.2 where AtomicStrings can't be implicitly converted to Strings
  41. in the presence of move constructors and/or move assignment operators. This bug has been fixed in Xcode 4.3 clang, so we
  42. check for both cxx_rvalue_references as well as the unrelated cxx_nonstatic_member_init feature which we know was added in 4.3 */
  43. #define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES __has_feature(cxx_rvalue_references) && __has_feature(cxx_nonstatic_member_init)
  44. #define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS __has_feature(cxx_deleted_functions)
  45. #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR __has_feature(cxx_nullptr)
  46. #define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS __has_feature(cxx_explicit_conversions)
  47. #define WTF_COMPILER_SUPPORTS_BLOCKS __has_feature(blocks)
  48. #define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT __has_feature(c_static_assert)
  49. #define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT __has_feature(cxx_static_assert)
  50. #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL __has_feature(cxx_override_control)
  51. #define WTF_COMPILER_SUPPORTS_HAS_TRIVIAL_DESTRUCTOR __has_feature(has_trivial_destructor)
  52. #define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS __has_feature(cxx_strong_enums)
  53. #define WTF_COMPILER_SUPPORTS_CXX_REFERENCE_QUALIFIED_FUNCTIONS __has_feature(cxx_reference_qualified_functions)
  54. #endif
  55. #ifndef CLANG_PRAGMA
  56. #define CLANG_PRAGMA(PRAGMA)
  57. #endif
  58. #if defined(__SNC__)
  59. #define WTF_COMPILER_SNC 1
  60. /* Specific compiler features */
  61. #define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES __has_feature(cxx_variadic_templates)
  62. #define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES __has_feature(cxx_rvalue_references)
  63. #define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS 0
  64. #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR __has_feature(cxx_nullptr)
  65. #define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS __has_feature(cxx_explicit_conversions)
  66. #define WTF_COMPILER_SUPPORTS_BLOCKS __has_feature(blocks)
  67. #define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT __has_feature(c_static_assert)
  68. #define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT __has_feature(cxx_static_assert)
  69. #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL __has_feature(cxx_override_control)
  70. #define WTF_COMPILER_SUPPORTS_HAS_TRIVIAL_DESTRUCTOR __has_feature(has_trivial_destructor)
  71. #define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS __has_feature(cxx_strong_enums)
  72. #define WTF_COMPILER_SUPPORTS_CXX_REFERENCE_QUALIFIED_FUNCTIONS __has_feature(cxx_reference_qualified_functions)
  73. #endif
  74. /* COMPILER(MSVC) - Microsoft Visual C++ */
  75. /* COMPILER(MSVC9_OR_LOWER) - Microsoft Visual C++ 2008 or lower*/
  76. #if defined(_MSC_VER)
  77. #define WTF_COMPILER_MSVC 1
  78. #if _MSC_VER < 1600
  79. #define WTF_COMPILER_MSVC9_OR_LOWER 1
  80. #endif
  81. /* Specific compiler features */
  82. #if !COMPILER(CLANG) && _MSC_VER >= 1600
  83. #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR 1
  84. #endif
  85. #if !COMPILER(CLANG)
  86. #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL 1
  87. #define WTF_COMPILER_QUIRK_FINAL_IS_CALLED_SEALED 1
  88. #endif
  89. /* Check for VS2010 or newer */
  90. #if _MSC_VER >= 1600
  91. #define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES 1
  92. #define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT 1
  93. #endif
  94. #endif /* defined(_MSC_VER) */
  95. /* COMPILER(RVCT) - ARM RealView Compilation Tools */
  96. #if defined(__CC_ARM) || defined(__ARMCC__)
  97. #define WTF_COMPILER_RVCT 1
  98. #define RVCT_VERSION_AT_LEAST(major, minor, patch, build) (__ARMCC_VERSION >= (major * 100000 + minor * 10000 + patch * 1000 + build))
  99. #else
  100. /* Define this for !RVCT compilers, just so we can write things like RVCT_VERSION_AT_LEAST(3, 0, 0, 0). */
  101. #define RVCT_VERSION_AT_LEAST(major, minor, patch, build) 0
  102. #endif
  103. /* COMPILER(GCCE) - GNU Compiler Collection for Embedded */
  104. #if defined(__GCCE__)
  105. #define WTF_COMPILER_GCCE 1
  106. #define GCCE_VERSION (__GCCE__ * 10000 + __GCCE_MINOR__ * 100 + __GCCE_PATCHLEVEL__)
  107. #define GCCE_VERSION_AT_LEAST(major, minor, patch) (GCCE_VERSION >= (major * 10000 + minor * 100 + patch))
  108. #endif
  109. /* COMPILER(GCC) - GNU Compiler Collection */
  110. /* --gnu option of the RVCT compiler also defines __GNUC__ */
  111. #if defined(__GNUC__) && !COMPILER(RVCT)
  112. #define WTF_COMPILER_GCC 1
  113. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  114. #define GCC_VERSION_AT_LEAST(major, minor, patch) (GCC_VERSION >= (major * 10000 + minor * 100 + patch))
  115. #else
  116. /* Define this for !GCC compilers, just so we can write things like GCC_VERSION_AT_LEAST(4, 1, 0). */
  117. #define GCC_VERSION_AT_LEAST(major, minor, patch) 0
  118. #endif
  119. /* Specific compiler features */
  120. #if COMPILER(GCC) && !COMPILER(CLANG)
  121. #if GCC_VERSION_AT_LEAST(4, 8, 0)
  122. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  123. #endif
  124. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  125. /* C11 support */
  126. #define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT 1
  127. #endif
  128. #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(__cplusplus) && __cplusplus >= 201103L)
  129. /* C++11 support */
  130. #if GCC_VERSION_AT_LEAST(4, 3, 0)
  131. #define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES 1
  132. #define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT 1
  133. #define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES 1
  134. #endif
  135. #if GCC_VERSION_AT_LEAST(4, 4, 0)
  136. #define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS 1
  137. #endif
  138. #if GCC_VERSION_AT_LEAST(4, 5, 0)
  139. #define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS 1
  140. #endif
  141. #if GCC_VERSION_AT_LEAST(4, 6, 0)
  142. #define WTF_COMPILER_SUPPORTS_CXX_NULLPTR 1
  143. /* Strong enums should work from gcc 4.4, but doesn't seem to support some operators */
  144. #define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS 1
  145. #endif
  146. #if GCC_VERSION_AT_LEAST(4, 7, 0)
  147. #define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL 1
  148. #endif
  149. #endif /* defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(__cplusplus) && __cplusplus >= 201103L) */
  150. #endif /* COMPILER(GCC) */
  151. /* COMPILER(MINGW) - MinGW GCC */
  152. /* COMPILER(MINGW64) - mingw-w64 GCC - only used as additional check to exclude mingw.org specific functions */
  153. #if defined(__MINGW32__)
  154. #define WTF_COMPILER_MINGW 1
  155. #include <_mingw.h> /* private MinGW header */
  156. #if defined(__MINGW64_VERSION_MAJOR) /* best way to check for mingw-w64 vs mingw.org */
  157. #define WTF_COMPILER_MINGW64 1
  158. #endif /* __MINGW64_VERSION_MAJOR */
  159. #endif /* __MINGW32__ */
  160. /* COMPILER(INTEL) - Intel C++ Compiler */
  161. #if defined(__INTEL_COMPILER)
  162. #define WTF_COMPILER_INTEL 1
  163. #endif
  164. /* COMPILER(SUNCC) */
  165. #if defined(__SUNPRO_CC) || defined(__SUNPRO_C)
  166. #define WTF_COMPILER_SUNCC 1
  167. #endif
  168. /* ABI */
  169. #if defined(__ARM_EABI__) || defined(__EABI__)
  170. #define WTF_COMPILER_SUPPORTS_EABI 1
  171. #endif
  172. /* ==== Compiler features ==== */
  173. /* ALWAYS_INLINE */
  174. #ifndef ALWAYS_INLINE
  175. #if COMPILER(GCC) && defined(NDEBUG) && !COMPILER(MINGW)
  176. #define ALWAYS_INLINE inline __attribute__((__always_inline__))
  177. #elif (COMPILER(MSVC) || COMPILER(RVCT)) && defined(NDEBUG)
  178. #define ALWAYS_INLINE __forceinline
  179. #else
  180. #define ALWAYS_INLINE inline
  181. #endif
  182. #endif
  183. /* NEVER_INLINE */
  184. #ifndef NEVER_INLINE
  185. #if COMPILER(GCC)
  186. #define NEVER_INLINE __attribute__((__noinline__))
  187. #elif COMPILER(RVCT)
  188. #define NEVER_INLINE __declspec(noinline)
  189. #else
  190. #define NEVER_INLINE
  191. #endif
  192. #endif
  193. /* UNLIKELY */
  194. #ifndef UNLIKELY
  195. #if COMPILER(GCC) || (COMPILER(RVCT) && defined(__GNUC__))
  196. #define UNLIKELY(x) __builtin_expect((x), 0)
  197. #else
  198. #define UNLIKELY(x) (x)
  199. #endif
  200. #endif
  201. /* LIKELY */
  202. #ifndef LIKELY
  203. #if COMPILER(GCC) || (COMPILER(RVCT) && defined(__GNUC__))
  204. #define LIKELY(x) __builtin_expect((x), 1)
  205. #else
  206. #define LIKELY(x) (x)
  207. #endif
  208. #endif
  209. /* NO_RETURN */
  210. #ifndef NO_RETURN
  211. #if COMPILER(GCC)
  212. #define NO_RETURN __attribute((__noreturn__))
  213. #elif COMPILER(MSVC) || COMPILER(RVCT)
  214. #define NO_RETURN __declspec(noreturn)
  215. #else
  216. #define NO_RETURN
  217. #endif
  218. #endif
  219. /* NO_RETURN_WITH_VALUE */
  220. #ifndef NO_RETURN_WITH_VALUE
  221. #if !COMPILER(MSVC)
  222. #define NO_RETURN_WITH_VALUE NO_RETURN
  223. #else
  224. #define NO_RETURN_WITH_VALUE
  225. #endif
  226. #endif
  227. /* WARN_UNUSED_RETURN */
  228. #if COMPILER(GCC)
  229. #define WARN_UNUSED_RETURN __attribute__ ((warn_unused_result))
  230. #else
  231. #define WARN_UNUSED_RETURN
  232. #endif
  233. /* OVERRIDE and FINAL */
  234. #if COMPILER_SUPPORTS(CXX_OVERRIDE_CONTROL)
  235. #define OVERRIDE override
  236. #if COMPILER_QUIRK(FINAL_IS_CALLED_SEALED)
  237. #define FINAL sealed
  238. #else
  239. #define FINAL final
  240. #endif
  241. #else
  242. #define OVERRIDE
  243. #define FINAL
  244. #endif
  245. #if COMPILER_SUPPORTS(CXX_DELETED_FUNCTIONS)
  246. #define WTF_DELETED_FUNCTION = delete
  247. #else
  248. #define WTF_DELETED_FUNCTION
  249. #endif
  250. /* REFERENCED_FROM_ASM */
  251. #ifndef REFERENCED_FROM_ASM
  252. #if COMPILER(GCC)
  253. #define REFERENCED_FROM_ASM __attribute__((used))
  254. #else
  255. #define REFERENCED_FROM_ASM
  256. #endif
  257. #endif
  258. /* OBJC_CLASS */
  259. #ifndef OBJC_CLASS
  260. #ifdef __OBJC__
  261. #define OBJC_CLASS @class
  262. #else
  263. #define OBJC_CLASS class
  264. #endif
  265. #endif
  266. /* UNUSED_PARAM */
  267. #if COMPILER(INTEL) && !(defined(WIN32) || defined(_WIN32)) || COMPILER(RVCT)
  268. template<typename T>
  269. inline void unusedParam(T& x) { (void)x; }
  270. #define UNUSED_PARAM(variable) unusedParam(variable)
  271. #elif COMPILER(MSVC)
  272. #define UNUSED_PARAM(variable) (void)&variable
  273. #else
  274. #define UNUSED_PARAM(variable) (void)variable
  275. #endif
  276. /* UNUSED_LABEL */
  277. /* This is to keep the compiler from complaining when for local labels are
  278. declared but not referenced. For example, this can happen with code that
  279. works with auto-generated code.
  280. */
  281. #if COMPILER(MSVC)
  282. #define UNUSED_LABEL(label) if (false) goto label
  283. #else
  284. #define UNUSED_LABEL(label) UNUSED_PARAM(&& label)
  285. #endif
  286. #endif /* WTF_Compiler_h */