Debian Codegraph @

src/3rdparty/gsl/assert

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #ifndef GSL_ASSERT_H
18 #define GSL_ASSERT_H
19
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 #define GSL_KERNEL_MODE
26
27 #define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
28 #include <intrin.h>
29 #define RANGE_CHECKS_FAILURE 0
30
31 #if defined(__clang__)
32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Winvalid-noreturn"
34 #endif // defined(__clang__)
35
36 #else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
37 // !_HAS_EXCEPTIONS))
38
39 #include <exception>
40
41 #endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
42 // !_HAS_EXCEPTIONS))
43
44 //
45 // make suppress attributes parse for some compilers
46 // Hopefully temporary until suppression standardization occurs
47 //
48 #if defined(__clang__)
49 #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]]
50 #elif defined(_MSC_VER) && _MSC_VER >= 1950
51 // Visual Studio versions after 2022 (_MSC_VER > 1944) support the justification message.
52 #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]]
53 #elif defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__NVCC__)
54 #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
55 #else
56 #define GSL_SUPPRESS(x)
57 #endif // defined(__clang__)
58
59 #if defined(__clang__) || defined(__GNUC__)
60 #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
61 #define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
62
63 #else
64
65 #define GSL_LIKELY(x) (!!(x))
66 #define GSL_UNLIKELY(x) (!!(x))
67 #endif // defined(__clang__) || defined(__GNUC__)
68
69 //
70 // GSL_ASSUME(cond)
71 //
72 // Tell the optimizer that the predicate cond must hold. It is unspecified
73 // whether or not cond is actually evaluated.
74 //
75 #ifdef _MSC_VER
76 #define GSL_ASSUME(cond) __assume(cond)
77 #elif defined(__GNUC__)
78 #define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
79 #else
80 #define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
81 #endif
82
83 //
84 // GSL.assert: assertions
85 //
86
87 namespace gsl
88 {
89
90 namespace details
91 {
92 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
93
94 typedef void(__cdecl* terminate_handler)();
95
96 // clang-format off
97 GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
98 // clang-format on
99 [[noreturn]] inline void __cdecl default_terminate_handler()
100 {
101 __fastfail(RANGE_CHECKS_FAILURE);
102 }
103
104 inline gsl::details::terminate_handler& get_terminate_handler() noexcept
105 {
106 static terminate_handler handler = &default_terminate_handler;
107 return handler;
108 }
109
110 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
111
112 [[noreturn]] inline void terminate() noexcept
113 {
114 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
115 (*gsl::details::get_terminate_handler())();
116 #else
117 std::terminate();
118 #endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
119 }
120
121 } // namespace details
122 } // namespace gsl
123
124 #define GSL_CONTRACT_CHECK(type, cond) \
125 (GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
126
127 #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
128 #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
129
130 #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
131 #pragma clang diagnostic pop
132 #endif
133
134 #endif // GSL_ASSERT_H