Skip to content

Commit bf97ba7

Browse files
tcbrindlegithub-actions[bot]
authored andcommitted
Update single header
1 parent fa983e5 commit bf97ba7

File tree

1 file changed

+61
-17
lines changed

1 file changed

+61
-17
lines changed

single_include/flux.hpp

+61-17
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@
9191

9292
#define FLUX_DECLVAL(...) ((static_cast<__VA_ARGS__(*)()noexcept>(nullptr))())
9393

94-
#ifdef __GNUC__
95-
#define FLUX_ALWAYS_INLINE [[gnu::always_inline]]
94+
#if defined(__GNUC__)
95+
# define FLUX_ALWAYS_INLINE [[gnu::always_inline]] inline
96+
#elif defined(_MSC_VER)
97+
# define FLUX_ALWAYS_INLINE __forceinline
9698
#else
97-
#define FLUX_ALWAYS_INLINE
99+
# define FLUX_ALWAYS_INLINE inline
98100
#endif
99101

100102
#define FLUX_NO_UNIQUE_ADDRESS [[no_unique_address]]
@@ -124,7 +126,8 @@
124126
#include <type_traits>
125127

126128
#define FLUX_ERROR_POLICY_TERMINATE 1
127-
#define FLUX_ERROR_POLICY_UNWIND 2
129+
#define FLUX_ERROR_POLICY_UNWIND 2
130+
#define FLUX_ERROR_POLICY_FAIL_FAST 3
128131

129132
#define FLUX_OVERFLOW_POLICY_ERROR 10
130133
#define FLUX_OVERFLOW_POLICY_WRAP 11
@@ -158,6 +161,8 @@
158161
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_TERMINATE
159162
#elif defined(FLUX_UNWIND_ON_ERROR)
160163
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_UNWIND
164+
#elif defined(FLUX_FAIL_FAST_ON_ERROR)
165+
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_FAIL_FAST
161166
#else
162167
# define FLUX_ERROR_POLICY FLUX_DEFAULT_ERROR_POLICY
163168
#endif // FLUX_TERMINATE_ON_ERROR
@@ -234,7 +239,8 @@ namespace flux {
234239
FLUX_EXPORT
235240
enum class error_policy {
236241
terminate = FLUX_ERROR_POLICY_TERMINATE,
237-
unwind = FLUX_ERROR_POLICY_UNWIND
242+
unwind = FLUX_ERROR_POLICY_UNWIND,
243+
fail_fast = FLUX_ERROR_POLICY_FAIL_FAST
238244
};
239245

240246
FLUX_EXPORT
@@ -294,6 +300,15 @@ inline constexpr bool enable_debug_asserts = FLUX_ENABLE_DEBUG_ASSERTS;
294300
#include <stdexcept>
295301
#include <type_traits>
296302

303+
#if defined(__has_builtin)
304+
# if __has_builtin(__builtin_trap)
305+
# define FLUX_HAS_BUILTIN_TRAP 1
306+
# endif
307+
#elif defined(_MSC_VER)
308+
# include <intrin.h>
309+
# define FLUX_HAS_FASTFAIL 1
310+
#endif
311+
297312
namespace flux {
298313

299314
FLUX_EXPORT
@@ -304,21 +319,51 @@ struct unrecoverable_error : std::logic_error {
304319
namespace detail {
305320

306321
struct runtime_error_fn {
322+
private:
323+
[[noreturn]]
324+
FLUX_ALWAYS_INLINE
325+
static void fail_fast()
326+
{
327+
#if FLUX_HAS_BUILTIN_TRAP
328+
__builtin_trap();
329+
#elif FLUX_HAS_FASTFAIL
330+
__fastfail(7); // FAST_FAIL_FATAL_APP_EXIT
331+
#else
332+
std::abort();
333+
#endif
334+
}
335+
307336
[[noreturn]]
337+
static void unwind(const char* msg, std::source_location loc)
338+
{
339+
char buf[1024];
340+
std::snprintf(buf, 1024, "%s:%u: Fatal error: %s",
341+
loc.file_name(), loc.line(), msg);
342+
throw unrecoverable_error(buf);
343+
}
344+
345+
[[noreturn]]
346+
static void terminate(const char* msg, std::source_location loc)
347+
{
348+
if constexpr (config::print_error_on_terminate) {
349+
std::fprintf(stderr, "%s:%u: Fatal error: %s\n",
350+
loc.file_name(), loc.line(), msg);
351+
}
352+
std::terminate();
353+
}
354+
355+
public:
356+
[[noreturn]]
357+
FLUX_ALWAYS_INLINE
308358
void operator()(char const* msg,
309359
std::source_location loc = std::source_location::current()) const
310360
{
311-
if constexpr (config::on_error == error_policy::unwind) {
312-
char buf[1024];
313-
std::snprintf(buf, 1024, "%s:%u: Fatal error: %s",
314-
loc.file_name(), loc.line(), msg);
315-
throw unrecoverable_error(buf);
361+
if constexpr (config::on_error == error_policy::fail_fast) {
362+
fail_fast();
363+
} else if constexpr (config::on_error == error_policy::unwind) {
364+
unwind(msg, loc);
316365
} else {
317-
if constexpr (config::print_error_on_terminate) {
318-
std::fprintf(stderr, "%s:%u: Fatal error: %s\n",
319-
loc.file_name(), loc.line(), msg);
320-
}
321-
std::terminate();
366+
terminate(msg, loc);
322367
}
323368
}
324369
};
@@ -368,8 +413,7 @@ struct indexed_bounds_check_fn {
368413
}
369414
}
370415
#endif
371-
assert_fn{}(idx >= T{0}, "index cannot be negative", loc);
372-
assert_fn{}(idx < limit, "out-of-bounds sequence access", loc);
416+
assert_fn{}(idx >= T{0} && idx < limit, "out-of-bounds sequence access", loc);
373417
}
374418
}
375419
};

0 commit comments

Comments
 (0)