mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
1c37688ccd
As always with feature tests, they can only be used to aggressively adopt new features even before all compilers we need have the features -- the test lets us write a vestigial workaround for downllevel compilers, to be removed as soon as all compilers support the feature.
142 lines
4.1 KiB
Plaintext
142 lines
4.1 KiB
Plaintext
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// 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
|
|
|
|
#include <atomic>
|
|
|
|
//
|
|
// 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
|
|
|
|
|
|
class contract_group {
|
|
public:
|
|
#ifdef __cpp_noexcept_function_type
|
|
using handler = void (*)() noexcept;
|
|
#else
|
|
using handler = void (*)();
|
|
#endif
|
|
|
|
contract_group (handler h) { set_handler(h); }
|
|
auto set_handler(handler h) -> handler { return chandler.exchange(h ? h : []()noexcept{}); }
|
|
auto get_handler() -> handler { return chandler; }
|
|
|
|
constexpr void expects (bool b) { assertion(b); }
|
|
constexpr void ensures (bool b) { assertion(b); }
|
|
private:
|
|
constexpr void assertion(bool b) { if (!b) chandler.load()(); }
|
|
|
|
#ifdef __cpp_guaranteed_copy_elision
|
|
std::atomic<handler> chandler; // current handler
|
|
#else
|
|
handler chandler;
|
|
#endif
|
|
};
|
|
|
|
auto static Default = contract_group( &gsl::details::terminate );
|
|
auto static Bounds = contract_group( Default.get_handler() );
|
|
auto static Null = contract_group( Default.get_handler() );
|
|
auto static Testing = contract_group( Default.get_handler() );
|
|
|
|
} // namespace gsl
|
|
|
|
#define Expects(cond, kind) kind.expects(cond)
|
|
#define Ensures(cond, kind) kind.ensures(cond)
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
|
|
#pragma clang diagnostic pop
|
|
#endif
|
|
|
|
#endif // GSL_CONTRACTS_H
|