assert 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef GSL_ASSERT_H
  17. #define GSL_ASSERT_H
  18. //
  19. // Temporary until MSVC STL supports no-exceptions mode.
  20. // Currently terminate is a no-op in this mode, so we add termination behavior back
  21. //
  22. #if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
  23. #define GSL_KERNEL_MODE
  24. #define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
  25. #include <intrin.h>
  26. #define RANGE_CHECKS_FAILURE 0
  27. #if defined(__clang__)
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Winvalid-noreturn"
  30. #endif // defined(__clang__)
  31. #else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
  32. // !_HAS_EXCEPTIONS))
  33. #include <exception>
  34. #endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
  35. // !_HAS_EXCEPTIONS))
  36. //
  37. // make suppress attributes parse for some compilers
  38. // Hopefully temporary until suppression standardization occurs
  39. //
  40. #if defined(__clang__)
  41. #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]]
  42. #else
  43. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__NVCC__)
  44. #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
  45. #else
  46. #define GSL_SUPPRESS(x)
  47. #endif // _MSC_VER
  48. #endif // __clang__
  49. #if defined(__clang__) || defined(__GNUC__)
  50. #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
  51. #define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
  52. #else
  53. #define GSL_LIKELY(x) (!!(x))
  54. #define GSL_UNLIKELY(x) (!!(x))
  55. #endif // defined(__clang__) || defined(__GNUC__)
  56. //
  57. // GSL_ASSUME(cond)
  58. //
  59. // Tell the optimizer that the predicate cond must hold. It is unspecified
  60. // whether or not cond is actually evaluated.
  61. //
  62. #ifdef _MSC_VER
  63. #define GSL_ASSUME(cond) __assume(cond)
  64. #elif defined(__GNUC__)
  65. #define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
  66. #else
  67. #define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
  68. #endif
  69. //
  70. // GSL.assert: assertions
  71. //
  72. namespace gsl
  73. {
  74. namespace details
  75. {
  76. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  77. typedef void(__cdecl* terminate_handler)();
  78. // clang-format off
  79. GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
  80. // clang-format on
  81. [[noreturn]] inline void __cdecl default_terminate_handler()
  82. {
  83. __fastfail(RANGE_CHECKS_FAILURE);
  84. }
  85. inline gsl::details::terminate_handler& get_terminate_handler() noexcept
  86. {
  87. static terminate_handler handler = &default_terminate_handler;
  88. return handler;
  89. }
  90. #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  91. [[noreturn]] inline void terminate() noexcept
  92. {
  93. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  94. (*gsl::details::get_terminate_handler())();
  95. #else
  96. std::terminate();
  97. #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  98. }
  99. } // namespace details
  100. } // namespace gsl
  101. #define GSL_CONTRACT_CHECK(type, cond) \
  102. (GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
  103. #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
  104. #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
  105. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
  106. #pragma clang diagnostic pop
  107. #endif
  108. #endif // GSL_ASSERT_H