foot/debug.h
Craig Barnes cb9c13800e Fix performance regression caused by xassert() macro
Before this commit, the xassert() macro still expanded to e.g.:

    if (unlikely(!(cond)))
        __builtin_unreachable();

...even when NDEBUG was set.

If `cond` happened to contain a function call that the compiler couldn't
prove to be side-effect free, this call would still have to be emitted
in the generated code, even when everything else could be optimized out.

This commit changes xassert() to be more in-keeping with the standard
assert() macro when NDEBUG is set; specifically, it causes the entire
statement to expand to nothing, which thus guarantees that any function
calls in the condition will be dropped, regardless of whether or not
they have side-effects.
2021-02-06 00:53:29 +00:00

30 lines
858 B
C

#pragma once
#include "macros.h"
#define BUG(...) bug(__FILE__, __LINE__, __func__, __VA_ARGS__)
#ifdef NDEBUG
#define xassert(x)
#else
#define xassert(x) do { \
IGNORE_WARNING("-Wtautological-compare") \
if (unlikely(!(x))) { \
BUG("assertion failed: '%s'", #x); \
} \
UNIGNORE_WARNINGS \
} while (0)
#endif
#ifndef static_assert
#if __STDC_VERSION__ >= 201112L
#define static_assert(x, msg) _Static_assert((x), msg)
#elif GNUC_AT_LEAST(4, 6) || HAS_EXTENSION(c_static_assert)
#define static_assert(x, msg) __extension__ _Static_assert((x), msg)
#else
#define static_assert(x, msg)
#endif
#endif
noreturn void fatal_error(const char *msg, int err) COLD;
noreturn void bug(const char *file, int line, const char *func, const char *fmt, ...) PRINTF(4) COLD;