Skip to content

Commit d90fefe

Browse files
authored
Merge pull request microsoft#851 from JordanMaples/dev/jomaples/exception
prevent exception include when in no-exception mode.
2 parents 4b64391 + 06adf55 commit d90fefe

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

include/gsl/gsl_assert

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,27 @@
1717
#ifndef GSL_CONTRACTS_H
1818
#define GSL_CONTRACTS_H
1919

20+
//
21+
// Temporary until MSVC STL supports no-exceptions mode.
22+
// Currently terminate is a no-op in this mode, so we add termination behavior back
23+
//
24+
#if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
25+
26+
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
27+
#include <intrin.h>
28+
#define RANGE_CHECKS_FAILURE 0
29+
30+
#if defined(__clang__)
31+
#pragma clang diagnostic push
32+
#pragma clang diagnostic ignored "-Winvalid-noreturn"
33+
#endif // defined(__clang__)
34+
35+
#else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
36+
2037
#include <exception>
2138

39+
#endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
40+
2241
//
2342
// make suppress attributes parse for some compilers
2443
// Hopefully temporary until suppression standardization occurs
@@ -33,32 +52,18 @@
3352
#endif // _MSC_VER
3453
#endif // __clang__
3554

36-
//
37-
// Temporary until MSVC STL supports no-exceptions mode.
38-
// Currently terminate is a no-op in this mode, so we add termination behavior back
39-
//
40-
#if defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
41-
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
42-
#include <intrin.h>
43-
#define RANGE_CHECKS_FAILURE 0
44-
45-
#if defined(__clang__)
46-
#pragma clang diagnostic push
47-
#pragma clang diagnostic ignored "-Winvalid-noreturn"
48-
#endif
49-
50-
#endif
51-
5255
#define GSL_STRINGIFY_DETAIL(x) #x
5356
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
5457

5558
#if defined(__clang__) || defined(__GNUC__)
5659
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
5760
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
61+
5862
#else
63+
5964
#define GSL_LIKELY(x) (!!(x))
6065
#define GSL_UNLIKELY(x) (!!(x))
61-
#endif
66+
#endif // defined(__clang__) || defined(__GNUC__)
6267

6368
//
6469
// GSL_ASSUME(cond)
@@ -85,9 +90,11 @@ namespace details
8590
{
8691
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
8792

88-
typedef void (__cdecl *terminate_handler)();
93+
typedef void(__cdecl* terminate_handler)();
8994

95+
// clang-format off
9096
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
97+
// clang-format on
9198
[[noreturn]] inline void __cdecl default_terminate_handler()
9299
{
93100
__fastfail(RANGE_CHECKS_FAILURE);
@@ -99,29 +106,20 @@ namespace details
99106
return handler;
100107
}
101108

102-
#endif
109+
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
103110

104111
[[noreturn]] inline void terminate() noexcept
105112
{
106113
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
107114
(*gsl::details::get_terminate_handler())();
108115
#else
109116
std::terminate();
110-
#endif
117+
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
111118
}
112119

113-
template <typename Exception>
114-
[[deprecated("GSL no longer supports throwing for contract violations. Use gsl::details::terminate() instead.")]]
115-
[[noreturn]] void throw_exception(Exception&&) noexcept
116-
{
117-
gsl::details::terminate();
118-
}
119-
120-
121120
} // namespace details
122121
} // namespace gsl
123122

124-
125123
#define GSL_CONTRACT_CHECK(type, cond) \
126124
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
127125

0 commit comments

Comments
 (0)