#pragma once #include "defs.hpp" #include #include #ifdef __GNUG__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-macros" #define FM_KILL_WARN_double_promotion1() \ _Pragma( "GCC diagnostic push" ) \ _Pragma( "GCC diagnostic ignored \"-Wdouble-promotion\"" ) #define FM_KILL_WARN_double_promotion2() _Pragma( "GCC diagnostic pop" ) #else #define FM_KILL_WARN_double_promotion1() #define FM_KILL_WARN_double_promotion2() #endif namespace floormat { [[noreturn]] [[maybe_unused]] static inline void _fm_abort() { *(volatile int*)nullptr = 0; for (;;) ; } } // namespace floormat #define fm_EMIT_DEBUG2(pfx, ...) \ do { \ if (!std::is_constant_evaluated()) { \ if constexpr (sizeof(pfx) > 1) \ std::fputs((pfx), stderr); \ std::fprintf(stderr, __VA_ARGS__); \ } \ } while (false) #define fm_EMIT_DEBUG(pfx, ...) \ do { \ FM_KILL_WARN_double_promotion1() \ fm_EMIT_DEBUG2(pfx, __VA_ARGS__); \ FM_KILL_WARN_double_promotion2() \ std::fputc('\n', stderr); \ std::fflush(stderr); \ } while (false) #define fm_abort(...) \ do { \ fm_EMIT_DEBUG2("fatal: ", __VA_ARGS__); \ fm_EMIT_DEBUG("", " in %s:%d", __FILE__, __LINE__); \ ::floormat::_fm_abort(); \ } while (false) #define fm_assert(...) \ do { \ if (!(__VA_ARGS__)) { \ fm_EMIT_DEBUG("", "assertion failed: %s in %s:%d", \ #__VA_ARGS__, __FILE__, __LINE__); \ ::floormat::_fm_abort(); \ } \ } while(false) #ifndef FM_NO_DEBUG #define fm_debug_assert(...) fm_assert(__VA_ARGS__) #else #define fm_debug_assert(...) void() #endif #define ASSERT_EXPR(var, expr, cond) \ ([&] { \ decltype(auto) var = (expr); \ fm_assert(cond); \ return (var); \ })() #define fm_warn(...) fm_EMIT_DEBUG("warning: ", __VA_ARGS__) #define fm_error(...) fm_EMIT_DEBUG("error: ", __VA_ARGS__) #define fm_log(...) fm_EMIT_DEBUG("", __VA_ARGS__) #define fm_debug(...) fm_EMIT_DEBUG("", __VA_ARGS__) #define fm_warn_once(...) do { \ static bool _fm_once_flag = false; \ if (!_fm_once_flag) { \ _fm_once_flag = true; \ fm_warn(__VA_ARGS__); \ } \ } while (false) #ifdef __GNUG__ # pragma GCC diagnostic pop #endif