gsl_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_CONTRACTS_H
  17. #define GSL_CONTRACTS_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_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
  24. #include <intrin.h>
  25. #define RANGE_CHECKS_FAILURE 0
  26. #if defined(__clang__)
  27. #pragma clang diagnostic push
  28. #pragma clang diagnostic ignored "-Winvalid-noreturn"
  29. #endif // defined(__clang__)
  30. #else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
  31. #include <exception>
  32. #endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
  33. //
  34. // make suppress attributes parse for some compilers
  35. // Hopefully temporary until suppression standardization occurs
  36. //
  37. #if defined(__clang__)
  38. #define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
  39. #else
  40. #if defined(_MSC_VER)
  41. #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
  42. #else
  43. #define GSL_SUPPRESS(x)
  44. #endif // _MSC_VER
  45. #endif // __clang__
  46. #define GSL_STRINGIFY_DETAIL(x) #x
  47. #define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
  48. #if defined(__clang__) || defined(__GNUC__)
  49. #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
  50. #define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
  51. #else
  52. #define GSL_LIKELY(x) (!!(x))
  53. #define GSL_UNLIKELY(x) (!!(x))
  54. #endif // defined(__clang__) || defined(__GNUC__)
  55. //
  56. // GSL_ASSUME(cond)
  57. //
  58. // Tell the optimizer that the predicate cond must hold. It is unspecified
  59. // whether or not cond is actually evaluated.
  60. //
  61. #ifdef _MSC_VER
  62. #define GSL_ASSUME(cond) __assume(cond)
  63. #elif defined(__GNUC__)
  64. #define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
  65. #else
  66. #define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
  67. #endif
  68. //
  69. // GSL.assert: assertions
  70. //
  71. namespace gsl
  72. {
  73. namespace details
  74. {
  75. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  76. typedef void(__cdecl* terminate_handler)();
  77. // clang-format off
  78. GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
  79. // clang-format on
  80. [[noreturn]] inline void __cdecl default_terminate_handler()
  81. {
  82. __fastfail(RANGE_CHECKS_FAILURE);
  83. }
  84. inline gsl::details::terminate_handler& get_terminate_handler() noexcept
  85. {
  86. static terminate_handler handler = &default_terminate_handler;
  87. return handler;
  88. }
  89. #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  90. [[noreturn]] inline void terminate() noexcept
  91. {
  92. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  93. (*gsl::details::get_terminate_handler())();
  94. #else
  95. std::terminate();
  96. #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  97. }
  98. } // namespace details
  99. } // namespace gsl
  100. #define GSL_CONTRACT_CHECK(type, cond) \
  101. (GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
  102. #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
  103. #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
  104. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
  105. #pragma clang diagnostic pop
  106. #endif
  107. #endif // GSL_CONTRACTS_H