2020-10-29 20:38:48 -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.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef GSL_CONTRACTS_H
|
|
|
|
#define GSL_CONTRACTS_H
|
|
|
|
|
2020-11-27 19:33:12 -05:00
|
|
|
|
2020-10-29 20:38:48 -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
|
|
|
|
//
|
|
|
|
#if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
|
|
|
|
|
|
|
|
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
|
|
|
|
#include <intrin.h>
|
|
|
|
#define RANGE_CHECKS_FAILURE 0
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Winvalid-noreturn"
|
|
|
|
#endif // defined(__clang__)
|
|
|
|
|
|
|
|
#else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
|
|
|
|
// !_HAS_EXCEPTIONS))
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
|
|
|
|
#endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
|
|
|
|
// !_HAS_EXCEPTIONS))
|
|
|
|
|
|
|
|
//
|
|
|
|
// make suppress attributes parse for some compilers
|
|
|
|
// Hopefully temporary until suppression standardization occurs
|
|
|
|
//
|
|
|
|
#if defined(__clang__)
|
|
|
|
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
|
|
|
#else
|
|
|
|
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
|
|
|
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
|
|
|
#else
|
|
|
|
#define GSL_SUPPRESS(x)
|
|
|
|
#endif // _MSC_VER
|
|
|
|
#endif // __clang__
|
|
|
|
|
|
|
|
#define GSL_STRINGIFY_DETAIL(x) #x
|
|
|
|
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
|
|
|
|
|
|
|
|
//
|
|
|
|
// GSL.assert: assertions
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace gsl
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
|
|
|
|
typedef void(__cdecl* terminate_handler)();
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
|
|
|
|
// clang-format on
|
|
|
|
[[noreturn]] inline void __cdecl default_terminate_handler()
|
|
|
|
{
|
|
|
|
__fastfail(RANGE_CHECKS_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline gsl::details::terminate_handler& get_terminate_handler() noexcept
|
|
|
|
{
|
|
|
|
static terminate_handler handler = &default_terminate_handler;
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
|
|
|
|
[[noreturn]] inline void terminate() noexcept
|
|
|
|
{
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
(*gsl::details::get_terminate_handler())();
|
|
|
|
#else
|
|
|
|
std::terminate();
|
|
|
|
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
|
2020-11-27 19:33:12 -05:00
|
|
|
class contract_group {
|
|
|
|
public:
|
2020-12-18 16:16:40 -05:00
|
|
|
#if defined __cpp_noexcept_function_type
|
2020-12-18 14:20:03 -05:00
|
|
|
using handler = void (*)() noexcept;
|
2020-12-18 15:18:23 -05:00
|
|
|
#else // VESTIGIAL, remove when no longer needed for downlevel compilers
|
2020-12-18 14:20:03 -05:00
|
|
|
using handler = void (*)();
|
|
|
|
#endif
|
2020-11-27 19:33:12 -05:00
|
|
|
|
2020-12-18 15:45:05 -05:00
|
|
|
constexpr contract_group (handler h = nullptr) : chandler(sanitize(h)) { }
|
2020-12-18 15:34:07 -05:00
|
|
|
constexpr auto set_handler(handler h) -> handler { auto old = chandler; chandler = sanitize(h); return old; }
|
2020-12-19 15:58:31 -05:00
|
|
|
constexpr auto get_handler() const -> handler { return chandler; }
|
2020-11-27 19:33:12 -05:00
|
|
|
|
2020-12-18 14:40:50 -05:00
|
|
|
constexpr void expects (bool b) { assertion(b); }
|
|
|
|
constexpr void ensures (bool b) { assertion(b); }
|
2020-11-27 19:33:12 -05:00
|
|
|
private:
|
2020-12-18 14:40:50 -05:00
|
|
|
constexpr void assertion(bool b) { if (!b) chandler(); }
|
2020-12-18 15:34:07 -05:00
|
|
|
constexpr auto sanitize(handler h) -> handler { return h ? h : []()noexcept{}; }
|
2020-12-18 14:20:03 -05:00
|
|
|
handler chandler;
|
2020-11-27 19:33:12 -05:00
|
|
|
};
|
|
|
|
|
2020-12-19 15:58:31 -05:00
|
|
|
#if !defined GSL_PER_CPP_CONTRACT_VIOLATION && defined __cpp_inline_variables
|
|
|
|
#define GSL_CONTRACT_VIOLATION_GRANULARITY inline
|
|
|
|
#else
|
|
|
|
#define GSL_CONTRACT_VIOLATION_GRANULARITY static
|
2020-12-18 16:16:40 -05:00
|
|
|
#endif
|
|
|
|
|
2020-12-19 15:58:31 -05:00
|
|
|
auto GSL_CONTRACT_VIOLATION_GRANULARITY Default = contract_group(
|
2020-12-18 15:53:36 -05:00
|
|
|
#if defined GSL_UNENFORCED_ON_CONTRACT_VIOLATION
|
2020-12-18 15:45:05 -05:00
|
|
|
// use default == null handler
|
2020-12-18 15:53:36 -05:00
|
|
|
#else // if defined GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
|
|
|
&gsl::details::terminate
|
2020-12-18 15:45:05 -05:00
|
|
|
#endif
|
|
|
|
);
|
2020-12-19 15:58:31 -05:00
|
|
|
auto GSL_CONTRACT_VIOLATION_GRANULARITY Bounds = Default;
|
|
|
|
auto GSL_CONTRACT_VIOLATION_GRANULARITY Null = Default;
|
|
|
|
auto GSL_CONTRACT_VIOLATION_GRANULARITY Testing = Default;
|
2020-11-27 19:33:12 -05:00
|
|
|
|
|
|
|
} // namespace gsl
|
|
|
|
|
2020-11-27 20:06:01 -05:00
|
|
|
#define Expects(cond, kind) kind.expects(cond)
|
|
|
|
#define Ensures(cond, kind) kind.ensures(cond)
|
2020-10-29 20:38:48 -04:00
|
|
|
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // GSL_CONTRACTS_H
|