Warning, /include/gsl/assert is written in an unsupported language. File is not indexed.
0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0004 //
0005 // This code is licensed under the MIT License (MIT).
0006 //
0007 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0008 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0009 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0010 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0011 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0012 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0013 // THE SOFTWARE.
0014 //
0015 ///////////////////////////////////////////////////////////////////////////////
0016
0017 #ifndef GSL_ASSERT_H
0018 #define GSL_ASSERT_H
0019
0020 //
0021 // Temporary until MSVC STL supports no-exceptions mode.
0022 // Currently terminate is a no-op in this mode, so we add termination behavior back
0023 //
0024 #if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
0025 #define GSL_KERNEL_MODE
0026
0027 #define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
0028 #include <intrin.h>
0029 #define RANGE_CHECKS_FAILURE 0
0030
0031 #if defined(__clang__)
0032 #pragma clang diagnostic push
0033 #pragma clang diagnostic ignored "-Winvalid-noreturn"
0034 #endif // defined(__clang__)
0035
0036 #else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
0037 // !_HAS_EXCEPTIONS))
0038
0039 #include <exception>
0040
0041 #endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
0042 // !_HAS_EXCEPTIONS))
0043
0044 //
0045 // make suppress attributes parse for some compilers
0046 // Hopefully temporary until suppression standardization occurs
0047 //
0048 #if defined(__clang__)
0049 #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]]
0050 #else
0051 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__NVCC__)
0052 #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
0053 #else
0054 #define GSL_SUPPRESS(x)
0055 #endif // _MSC_VER
0056 #endif // __clang__
0057
0058 #if defined(__clang__) || defined(__GNUC__)
0059 #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
0060 #define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
0061
0062 #else
0063
0064 #define GSL_LIKELY(x) (!!(x))
0065 #define GSL_UNLIKELY(x) (!!(x))
0066 #endif // defined(__clang__) || defined(__GNUC__)
0067
0068 //
0069 // GSL_ASSUME(cond)
0070 //
0071 // Tell the optimizer that the predicate cond must hold. It is unspecified
0072 // whether or not cond is actually evaluated.
0073 //
0074 #ifdef _MSC_VER
0075 #define GSL_ASSUME(cond) __assume(cond)
0076 #elif defined(__GNUC__)
0077 #define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
0078 #else
0079 #define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
0080 #endif
0081
0082 //
0083 // GSL.assert: assertions
0084 //
0085
0086 namespace gsl
0087 {
0088
0089 namespace details
0090 {
0091 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0092
0093 typedef void(__cdecl* terminate_handler)();
0094
0095 // clang-format off
0096 GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
0097 // clang-format on
0098 [[noreturn]] inline void __cdecl default_terminate_handler()
0099 {
0100 __fastfail(RANGE_CHECKS_FAILURE);
0101 }
0102
0103 inline gsl::details::terminate_handler& get_terminate_handler() noexcept
0104 {
0105 static terminate_handler handler = &default_terminate_handler;
0106 return handler;
0107 }
0108
0109 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0110
0111 [[noreturn]] inline void terminate() noexcept
0112 {
0113 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0114 (*gsl::details::get_terminate_handler())();
0115 #else
0116 std::terminate();
0117 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0118 }
0119
0120 } // namespace details
0121 } // namespace gsl
0122
0123 #define GSL_CONTRACT_CHECK(type, cond) \
0124 (GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
0125
0126 #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
0127 #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
0128
0129 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
0130 #pragma clang diagnostic pop
0131 #endif
0132
0133 #endif // GSL_ASSERT_H