From 1f76fbd168a9db5ffee39e33b751da81704bbc8d Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Tue, 31 Jul 2018 17:30:43 -0700 Subject: [PATCH 1/2] Dev/annagrin/failfast in noexcept mode windows (#710) made terminate call __fastfail in noexcept mode when using msvc --- include/gsl/gsl_assert | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/gsl/gsl_assert b/include/gsl/gsl_assert index 131fa8b..be4676b 100644 --- a/include/gsl/gsl_assert +++ b/include/gsl/gsl_assert @@ -26,6 +26,8 @@ // #if defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS #define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND +#include +#define RANGE_CHECKS_FAILURE 0 #endif // @@ -82,10 +84,15 @@ namespace details #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) typedef void (__cdecl *terminate_handler)(); + + [[noreturn]] inline void __cdecl default_terminate_handler() + { + __fastfail(RANGE_CHECKS_FAILURE); + } inline gsl::details::terminate_handler& get_terminate_handler() noexcept { - static terminate_handler handler = &abort; + static terminate_handler handler = &default_terminate_handler; return handler; } From f4a715816ceb2dc136e86a68b71f05a8773c8f2b Mon Sep 17 00:00:00 2001 From: beinhaerter <34543625+beinhaerter@users.noreply.github.com> Date: Wed, 1 Aug 2018 02:53:00 +0200 Subject: [PATCH 2/2] Suppress warnings on VS (#642) - gsl::narrow, gsl::narrow_cast and gsl::at are the safe variants suggested by CppCoreGuideline. It does not make sense to let VS warn inside the implementation of these functions that unsafe static_cast is used and that the safe variants shall be used. - Suppress warning that throw_exception can be declared noexcept (for the GSL_TERMINATE_ON_CONTRACT_VIOLATION case) --- include/gsl/gsl_assert | 4 ++++ include/gsl/gsl_util | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/gsl/gsl_assert b/include/gsl/gsl_assert index be4676b..70a6eba 100644 --- a/include/gsl/gsl_assert +++ b/include/gsl/gsl_assert @@ -110,6 +110,10 @@ namespace details #if defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) template +#if defined(__clang__) || defined(__GNUC__) +#else + [[gsl::suppress(f.6)]] +#endif [[noreturn]] void throw_exception(Exception&&) { gsl::details::terminate(); diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index 25f8502..ee61711 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -88,6 +88,10 @@ final_action finally(F&& f) noexcept // narrow_cast(): a searchable way to do narrowing casts of values template +#if defined(__clang__) || defined(__GNUC__) +#else +[[gsl::suppress(type.1)]] +#endif constexpr T narrow_cast(U&& u) noexcept { return static_cast(std::forward(u)); @@ -108,6 +112,10 @@ namespace details // narrow() : a checked version of narrow_cast() that throws if the cast changed the value template +#if defined(__clang__) || defined(__GNUC__) +#else +[[gsl::suppress(type.1)]] +#endif T narrow(U u) { T t = narrow_cast(u); @@ -121,6 +129,10 @@ T narrow(U u) // at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector // template +#if defined(__clang__) || defined(__GNUC__) +#else +[[gsl::suppress(type.1,bounds.2,bounds.4)]] +#endif constexpr T& at(T (&arr)[N], const index i) { Expects(i >= 0 && i < narrow_cast(N)); @@ -128,6 +140,10 @@ constexpr T& at(T (&arr)[N], const index i) } template +#if defined(__clang__) || defined(__GNUC__) +#else +[[gsl::suppress(type.1,bounds.4)]] +#endif constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()]) { Expects(i >= 0 && i < narrow_cast(cont.size()));