2016-07-20 16:17:47 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
2015-11-20 19:03:00 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef GSL_CONTRACTS_H
|
|
|
|
#define GSL_CONTRACTS_H
|
|
|
|
|
2018-03-15 19:00:08 -04:00
|
|
|
//
|
|
|
|
// Temporary until MSVC STL supports no-exceptions mode.
|
|
|
|
// Currently terminate is a no-op in this mode, so we add termination behavior back
|
|
|
|
//
|
2020-03-12 15:32:32 -04:00
|
|
|
#if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
|
2020-03-06 14:17:58 -05:00
|
|
|
|
2018-03-15 19:00:08 -04:00
|
|
|
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
|
2018-07-31 20:30:43 -04:00
|
|
|
#include <intrin.h>
|
|
|
|
#define RANGE_CHECKS_FAILURE 0
|
2019-01-15 13:27:34 -05:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Winvalid-noreturn"
|
2020-03-06 14:17:58 -05:00
|
|
|
#endif // defined(__clang__)
|
2020-03-12 15:32:32 -04:00
|
|
|
|
|
|
|
#else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
|
|
|
|
|
2020-03-03 20:04:46 -05:00
|
|
|
#include <exception>
|
2020-03-12 15:32:32 -04:00
|
|
|
|
|
|
|
#endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
|
2018-03-15 19:00:08 -04:00
|
|
|
|
2020-03-06 14:22:09 -05:00
|
|
|
//
|
|
|
|
// make suppress attributes parse for some compilers
|
|
|
|
// Hopefully temporary until suppression standardization occurs
|
|
|
|
//
|
|
|
|
#if defined(__clang__)
|
|
|
|
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
|
|
|
#else
|
2020-08-05 17:30:24 -04:00
|
|
|
#if defined(_MSC_VER) && ! defined(__INTEL_COMPILER)
|
2020-03-06 14:22:09 -05:00
|
|
|
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
|
|
|
#else
|
|
|
|
#define GSL_SUPPRESS(x)
|
|
|
|
#endif // _MSC_VER
|
|
|
|
#endif // __clang__
|
|
|
|
|
2015-11-20 19:03:00 -05:00
|
|
|
#define GSL_STRINGIFY_DETAIL(x) #x
|
|
|
|
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
|
|
|
|
|
2016-09-28 11:43:13 -04:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
2017-04-20 10:51:37 -04:00
|
|
|
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
|
|
|
|
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
2020-03-06 14:17:58 -05:00
|
|
|
|
2016-09-28 11:43:13 -04:00
|
|
|
#else
|
2020-03-06 14:17:58 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#define GSL_LIKELY(x) (!!(x))
|
|
|
|
#define GSL_UNLIKELY(x) (!!(x))
|
2020-03-06 14:17:58 -05:00
|
|
|
#endif // defined(__clang__) || defined(__GNUC__)
|
2017-03-20 12:30:14 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// GSL_ASSUME(cond)
|
|
|
|
//
|
|
|
|
// Tell the optimizer that the predicate cond must hold. It is unspecified
|
|
|
|
// whether or not cond is actually evaluated.
|
|
|
|
//
|
|
|
|
#ifdef _MSC_VER
|
2017-04-20 10:51:37 -04:00
|
|
|
#define GSL_ASSUME(cond) __assume(cond)
|
2017-03-20 12:30:14 -04:00
|
|
|
#elif defined(__GNUC__)
|
2017-04-20 10:51:37 -04:00
|
|
|
#define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
|
2017-03-20 12:30:14 -04:00
|
|
|
#else
|
2018-02-11 15:16:39 -05:00
|
|
|
#define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
|
2016-09-28 11:43:13 -04:00
|
|
|
#endif
|
|
|
|
|
2015-11-20 19:03:00 -05:00
|
|
|
//
|
|
|
|
// GSL.assert: assertions
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace gsl
|
|
|
|
{
|
2018-03-15 19:00:08 -04:00
|
|
|
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
|
2020-03-06 14:17:58 -05:00
|
|
|
typedef void(__cdecl* terminate_handler)();
|
2019-01-15 13:27:34 -05:00
|
|
|
|
2020-03-06 14:17:58 -05:00
|
|
|
// clang-format off
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
|
2020-03-06 14:17:58 -05:00
|
|
|
// clang-format on
|
2018-07-31 20:30:43 -04:00
|
|
|
[[noreturn]] inline void __cdecl default_terminate_handler()
|
|
|
|
{
|
|
|
|
__fastfail(RANGE_CHECKS_FAILURE);
|
|
|
|
}
|
2018-03-15 19:00:08 -04:00
|
|
|
|
|
|
|
inline gsl::details::terminate_handler& get_terminate_handler() noexcept
|
|
|
|
{
|
2018-07-31 20:30:43 -04:00
|
|
|
static terminate_handler handler = &default_terminate_handler;
|
2018-03-15 19:00:08 -04:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2020-03-06 14:17:58 -05:00
|
|
|
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
2018-03-15 19:00:08 -04:00
|
|
|
|
|
|
|
[[noreturn]] inline void terminate() noexcept
|
|
|
|
{
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
(*gsl::details::get_terminate_handler())();
|
|
|
|
#else
|
|
|
|
std::terminate();
|
2020-03-06 14:17:58 -05:00
|
|
|
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
2018-03-15 19:00:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
} // namespace gsl
|
2015-11-20 19:03:00 -05:00
|
|
|
|
2018-03-15 19:00:08 -04:00
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) \
|
|
|
|
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
|
2015-11-20 19:03:00 -05:00
|
|
|
|
2017-03-20 12:30:14 -04:00
|
|
|
#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
|
|
|
|
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
|
|
|
|
|
2019-01-15 13:27:34 -05:00
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2015-11-20 19:03:00 -05:00
|
|
|
#endif // GSL_CONTRACTS_H
|