Skip to content

Commit

Permalink
Update single header
Browse files Browse the repository at this point in the history
  • Loading branch information
tcbrindle authored and github-actions[bot] committed Sep 10, 2024
1 parent fa983e5 commit bf97ba7
Showing 1 changed file with 61 additions and 17 deletions.
78 changes: 61 additions & 17 deletions single_include/flux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@

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

#ifdef __GNUC__
#define FLUX_ALWAYS_INLINE [[gnu::always_inline]]
#if defined(__GNUC__)
# define FLUX_ALWAYS_INLINE [[gnu::always_inline]] inline
#elif defined(_MSC_VER)
# define FLUX_ALWAYS_INLINE __forceinline
#else
#define FLUX_ALWAYS_INLINE
# define FLUX_ALWAYS_INLINE inline
#endif

#define FLUX_NO_UNIQUE_ADDRESS [[no_unique_address]]
Expand Down Expand Up @@ -124,7 +126,8 @@
#include <type_traits>

#define FLUX_ERROR_POLICY_TERMINATE 1
#define FLUX_ERROR_POLICY_UNWIND 2
#define FLUX_ERROR_POLICY_UNWIND 2
#define FLUX_ERROR_POLICY_FAIL_FAST 3

#define FLUX_OVERFLOW_POLICY_ERROR 10
#define FLUX_OVERFLOW_POLICY_WRAP 11
Expand Down Expand Up @@ -158,6 +161,8 @@
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_TERMINATE
#elif defined(FLUX_UNWIND_ON_ERROR)
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_UNWIND
#elif defined(FLUX_FAIL_FAST_ON_ERROR)
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_FAIL_FAST
#else
# define FLUX_ERROR_POLICY FLUX_DEFAULT_ERROR_POLICY
#endif // FLUX_TERMINATE_ON_ERROR
Expand Down Expand Up @@ -234,7 +239,8 @@ namespace flux {
FLUX_EXPORT
enum class error_policy {
terminate = FLUX_ERROR_POLICY_TERMINATE,
unwind = FLUX_ERROR_POLICY_UNWIND
unwind = FLUX_ERROR_POLICY_UNWIND,
fail_fast = FLUX_ERROR_POLICY_FAIL_FAST
};

FLUX_EXPORT
Expand Down Expand Up @@ -294,6 +300,15 @@ inline constexpr bool enable_debug_asserts = FLUX_ENABLE_DEBUG_ASSERTS;
#include <stdexcept>
#include <type_traits>

#if defined(__has_builtin)
# if __has_builtin(__builtin_trap)
# define FLUX_HAS_BUILTIN_TRAP 1
# endif
#elif defined(_MSC_VER)
# include <intrin.h>
# define FLUX_HAS_FASTFAIL 1
#endif

namespace flux {

FLUX_EXPORT
Expand All @@ -304,21 +319,51 @@ struct unrecoverable_error : std::logic_error {
namespace detail {

struct runtime_error_fn {
private:
[[noreturn]]
FLUX_ALWAYS_INLINE
static void fail_fast()
{
#if FLUX_HAS_BUILTIN_TRAP
__builtin_trap();
#elif FLUX_HAS_FASTFAIL
__fastfail(7); // FAST_FAIL_FATAL_APP_EXIT
#else
std::abort();
#endif
}

[[noreturn]]
static void unwind(const char* msg, std::source_location loc)
{
char buf[1024];
std::snprintf(buf, 1024, "%s:%u: Fatal error: %s",
loc.file_name(), loc.line(), msg);
throw unrecoverable_error(buf);
}

[[noreturn]]
static void terminate(const char* msg, std::source_location loc)
{
if constexpr (config::print_error_on_terminate) {
std::fprintf(stderr, "%s:%u: Fatal error: %s\n",
loc.file_name(), loc.line(), msg);
}
std::terminate();
}

public:
[[noreturn]]
FLUX_ALWAYS_INLINE
void operator()(char const* msg,
std::source_location loc = std::source_location::current()) const
{
if constexpr (config::on_error == error_policy::unwind) {
char buf[1024];
std::snprintf(buf, 1024, "%s:%u: Fatal error: %s",
loc.file_name(), loc.line(), msg);
throw unrecoverable_error(buf);
if constexpr (config::on_error == error_policy::fail_fast) {
fail_fast();
} else if constexpr (config::on_error == error_policy::unwind) {
unwind(msg, loc);
} else {
if constexpr (config::print_error_on_terminate) {
std::fprintf(stderr, "%s:%u: Fatal error: %s\n",
loc.file_name(), loc.line(), msg);
}
std::terminate();
terminate(msg, loc);
}
}
};
Expand Down Expand Up @@ -368,8 +413,7 @@ struct indexed_bounds_check_fn {
}
}
#endif
assert_fn{}(idx >= T{0}, "index cannot be negative", loc);
assert_fn{}(idx < limit, "out-of-bounds sequence access", loc);
assert_fn{}(idx >= T{0} && idx < limit, "out-of-bounds sequence access", loc);
}
}
};
Expand Down

0 comments on commit bf97ba7

Please sign in to comment.