simde-detect-clang.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Detect Clang Version
  2. * Created by Evan Nemerson <evan@nemerson.com>
  3. *
  4. * To the extent possible under law, the author(s) have dedicated all
  5. * copyright and related and neighboring rights to this software to
  6. * the public domain worldwide. This software is distributed without
  7. * any warranty.
  8. *
  9. * For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. * SPDX-License-Identifier: CC0-1.0
  11. */
  12. /* This file was originally part of SIMDe
  13. * (<https://github.com/simd-everywhere/simde>). You're free to do with it as
  14. * you please, but I do have a few small requests:
  15. *
  16. * * If you make improvements, please submit them back to SIMDe
  17. * (at <https://github.com/simd-everywhere/simde/issues>) so others can
  18. * benefit from them.
  19. * * Please keep a link to SIMDe intact so people know where to submit
  20. * improvements.
  21. * * If you expose it publicly, please change the SIMDE_ prefix to
  22. * something specific to your project.
  23. *
  24. * The version numbers clang exposes (in the ___clang_major__,
  25. * __clang_minor__, and __clang_patchlevel__ macros) are unreliable.
  26. * Vendors such as Apple will define these values to their version
  27. * numbers; for example, "Apple Clang 4.0" is really clang 3.1, but
  28. * __clang_major__ and __clang_minor__ are defined to 4 and 0
  29. * respectively, instead of 3 and 1.
  30. *
  31. * The solution is *usually* to use clang's feature detection macros
  32. * (<https://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros>)
  33. * to determine if the feature you're interested in is available. This
  34. * generally works well, and it should probably be the first thing you
  35. * try. Unfortunately, it's not possible to check for everything. In
  36. * particular, compiler bugs.
  37. *
  38. * This file just uses the feature checking macros to detect features
  39. * added in specific versions of clang to identify which version of
  40. * clang the compiler is based on.
  41. *
  42. * Right now it only goes back to 3.6, but I'm happy to accept patches
  43. * to go back further. And, of course, newer versions are welcome if
  44. * they're not already present, and if you find a way to detect a point
  45. * release that would be great, too!
  46. */
  47. #if !defined(SIMDE_DETECT_CLANG_H)
  48. #define SIMDE_DETECT_CLANG_H 1
  49. /* Attempt to detect the upstream clang version number. I usually only
  50. * worry about major version numbers (at least for 4.0+), but if you
  51. * need more resolution I'm happy to accept patches that are able to
  52. * detect minor versions as well. That said, you'll probably have a
  53. * hard time with detection since AFAIK most minor releases don't add
  54. * anything we can detect. Updated based on
  55. * https://github.com/google/highway/blob/438c705a295176b96a50336527bb3e7ea365ffac/hwy/detect_compiler_arch.h#L73
  56. * - would welcome patches/updates there as well.
  57. */
  58. #if defined(__clang__) && !defined(SIMDE_DETECT_CLANG_VERSION)
  59. # if __has_warning("-Wmissing-designated-field-initializers")
  60. # define SIMDE_DETECT_CLANG_VERSION 190000
  61. # elif __has_warning("-Woverriding-option")
  62. # define SIMDE_DETECT_CLANG_VERSION 180000
  63. # elif __has_attribute(unsafe_buffer_usage) // no new warnings in 17.0
  64. # define SIMDE_DETECT_CLANG_VERSION 170000
  65. # elif __has_attribute(nouwtable) // no new warnings in 16.0
  66. # define SIMDE_DETECT_CLANG_VERSION 160000
  67. # elif __has_warning("-Warray-parameter")
  68. # define SIMDE_DETECT_CLANG_VERSION 150000
  69. # elif __has_warning("-Wbitwise-instead-of-logical")
  70. # define SIMDE_DETECT_CLANG_VERSION 140000
  71. # elif __has_warning("-Waix-compat")
  72. # define SIMDE_DETECT_CLANG_VERSION 130000
  73. # elif __has_warning("-Wformat-insufficient-args")
  74. # define SIMDE_DETECT_CLANG_VERSION 120000
  75. # elif __has_warning("-Wimplicit-const-int-float-conversion")
  76. # define SIMDE_DETECT_CLANG_VERSION 110000
  77. # elif __has_warning("-Wmisleading-indentation")
  78. # define SIMDE_DETECT_CLANG_VERSION 100000
  79. # elif defined(__FILE_NAME__)
  80. # define SIMDE_DETECT_CLANG_VERSION 90000
  81. # elif __has_warning("-Wextra-semi-stmt") || __has_builtin(__builtin_rotateleft32)
  82. # define SIMDE_DETECT_CLANG_VERSION 80000
  83. // For reasons unknown, Xcode 10.3 (Apple LLVM version 10.0.1) is apparently
  84. // based on Clang 7, but does not support the warning we test.
  85. // See https://en.wikipedia.org/wiki/Xcode#Toolchain_versions and
  86. // https://trac.macports.org/wiki/XcodeVersionInfo.
  87. # elif __has_warning("-Wc++98-compat-extra-semi") || \
  88. (defined(__apple_build_version__) && __apple_build_version__ >= 10010000)
  89. # define SIMDE_DETECT_CLANG_VERSION 70000
  90. # elif __has_warning("-Wpragma-pack")
  91. # define SIMDE_DETECT_CLANG_VERSION 60000
  92. # elif __has_warning("-Wbitfield-enum-conversion")
  93. # define SIMDE_DETECT_CLANG_VERSION 50000
  94. # elif __has_attribute(diagnose_if)
  95. # define SIMDE_DETECT_CLANG_VERSION 40000
  96. # elif __has_warning("-Wcomma")
  97. # define SIMDE_DETECT_CLANG_VERSION 39000
  98. # elif __has_warning("-Wdouble-promotion")
  99. # define SIMDE_DETECT_CLANG_VERSION 38000
  100. # elif __has_warning("-Wshift-negative-value")
  101. # define SIMDE_DETECT_CLANG_VERSION 37000
  102. # elif __has_warning("-Wambiguous-ellipsis")
  103. # define SIMDE_DETECT_CLANG_VERSION 36000
  104. # else
  105. # define SIMDE_DETECT_CLANG_VERSION 1
  106. # endif
  107. #endif /* defined(__clang__) && !defined(SIMDE_DETECT_CLANG_VERSION) */
  108. /* The SIMDE_DETECT_CLANG_VERSION_CHECK macro is pretty
  109. * straightforward; it returns true if the compiler is a derivative
  110. * of clang >= the specified version.
  111. *
  112. * Since this file is often (primarily?) useful for working around bugs
  113. * it is also helpful to have a macro which returns true if only if the
  114. * compiler is a version of clang *older* than the specified version to
  115. * make it a bit easier to ifdef regions to add code for older versions,
  116. * such as pragmas to disable a specific warning. */
  117. #if defined(SIMDE_DETECT_CLANG_VERSION)
  118. # define SIMDE_DETECT_CLANG_VERSION_CHECK(major, minor, revision) (SIMDE_DETECT_CLANG_VERSION >= ((major * 10000) + (minor * 1000) + (revision)))
  119. # define SIMDE_DETECT_CLANG_VERSION_NOT(major, minor, revision) (SIMDE_DETECT_CLANG_VERSION < ((major * 10000) + (minor * 1000) + (revision)))
  120. #else
  121. # define SIMDE_DETECT_CLANG_VERSION_CHECK(major, minor, revision) (0)
  122. # define SIMDE_DETECT_CLANG_VERSION_NOT(major, minor, revision) (0)
  123. #endif
  124. #endif /* !defined(SIMDE_DETECT_CLANG_H) */