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_CONTRACTS_H
0018 #define GSL_CONTRACTS_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)
0052 #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
0053 #else
0054 #define GSL_SUPPRESS(x)
0055 #endif // _MSC_VER
0056 #endif // __clang__
0057
0058 #define GSL_STRINGIFY_DETAIL(x) #x
0059 #define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
0060
0061 #if defined(__clang__) || defined(__GNUC__)
0062 #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
0063 #define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
0064
0065 #else
0066
0067 #define GSL_LIKELY(x) (!!(x))
0068 #define GSL_UNLIKELY(x) (!!(x))
0069 #endif // defined(__clang__) || defined(__GNUC__)
0070
0071 //
0072 // GSL_ASSUME(cond)
0073 //
0074 // Tell the optimizer that the predicate cond must hold. It is unspecified
0075 // whether or not cond is actually evaluated.
0076 //
0077 #ifdef _MSC_VER
0078 #define GSL_ASSUME(cond) __assume(cond)
0079 #elif defined(__GNUC__)
0080 #define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
0081 #else
0082 #define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
0083 #endif
0084
0085 //
0086 // GSL.assert: assertions
0087 //
0088
0089 namespace gsl
0090 {
0091
0092 namespace details
0093 {
0094 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0095
0096 typedef void(__cdecl* terminate_handler)();
0097
0098 // clang-format off
0099 GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
0100 // clang-format on
0101 [[noreturn]] inline void __cdecl default_terminate_handler()
0102 {
0103 __fastfail(RANGE_CHECKS_FAILURE);
0104 }
0105
0106 inline gsl::details::terminate_handler& get_terminate_handler() noexcept
0107 {
0108 static terminate_handler handler = &default_terminate_handler;
0109 return handler;
0110 }
0111
0112 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0113
0114 [[noreturn]] inline void terminate() noexcept
0115 {
0116 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0117 (*gsl::details::get_terminate_handler())();
0118 #else
0119 std::terminate();
0120 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0121 }
0122
0123 } // namespace details
0124 } // namespace gsl
0125
0126 #define GSL_CONTRACT_CHECK(type, cond) \
0127 (GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
0128
0129 #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
0130 #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
0131
0132 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
0133 #pragma clang diagnostic pop
0134 #endif
0135
0136 #endif // GSL_CONTRACTS_H