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 #if __clang_major__ >= 22
0035 #pragma clang diagnostic ignored "-Wunique-object-duplication"
0036 #endif // __clang_major >= 22
0037 #endif // defined(__clang__)
0038
0039 #else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
0040 // !_HAS_EXCEPTIONS))
0041
0042 #include <exception>
0043
0044 #endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
0045 // !_HAS_EXCEPTIONS))
0046
0047 //
0048 // make suppress attributes parse for some compilers
0049 // Hopefully temporary until suppression standardization occurs
0050 //
0051 #if defined(__clang__)
0052 #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]]
0053 #elif defined(_MSC_VER) && _MSC_VER >= 1950
0054 // Visual Studio versions after 2022 (_MSC_VER > 1944) support the justification message.
0055 #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]]
0056 #elif defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__NVCC__)
0057 #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
0058 #else
0059 #define GSL_SUPPRESS(x)
0060 #endif // defined(__clang__)
0061
0062 #if defined(__clang__) || defined(__GNUC__)
0063 #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
0064 #define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
0065
0066 #else
0067
0068 #define GSL_LIKELY(x) (!!(x))
0069 #define GSL_UNLIKELY(x) (!!(x))
0070 #endif // defined(__clang__) || defined(__GNUC__)
0071
0072 //
0073 // GSL_ASSUME(cond)
0074 //
0075 // Tell the optimizer that the predicate cond must hold. It is unspecified
0076 // whether or not cond is actually evaluated.
0077 //
0078 #ifdef _MSC_VER
0079 #define GSL_ASSUME(cond) __assume(cond)
0080 #elif defined(__GNUC__)
0081 #define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
0082 #else
0083 #define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
0084 #endif
0085
0086 //
0087 // GSL.assert: assertions
0088 //
0089
0090 namespace gsl
0091 {
0092
0093 namespace details
0094 {
0095 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0096
0097 typedef void(__cdecl* terminate_handler)();
0098
0099 GSL_SUPPRESS(f.6)
0100 [[noreturn]] inline void __cdecl default_terminate_handler()
0101 {
0102 __fastfail(RANGE_CHECKS_FAILURE);
0103 }
0104
0105 inline gsl::details::terminate_handler& get_terminate_handler() noexcept
0106 {
0107 static terminate_handler handler = &default_terminate_handler;
0108 return handler;
0109 }
0110
0111 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0112
0113 [[noreturn]] inline void terminate() noexcept
0114 {
0115 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0116 (*gsl::details::get_terminate_handler())();
0117 #else
0118 std::terminate();
0119 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
0120 }
0121
0122 } // namespace details
0123 } // namespace gsl
0124
0125 #define GSL_CONTRACT_CHECK(type, cond) \
0126 (GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
0127
0128 #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
0129 #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
0130
0131 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
0132 #pragma clang diagnostic pop
0133 #endif
0134
0135 #endif // GSL_ASSERT_H