kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

assert.h (1315B)


      1 /* assert.h -- C11 7.2 -- Diagnostics
      2  *
      3  * <assert.h> is a hosted header in C11: a faithful assert() writes to
      4  * stderr and calls abort(), neither of which exists freestanding. kit
      5  * provides a slimmer contract -- on failure, assert() calls a
      6  * user-supplied handler:
      7  *
      8  *     _Noreturn void __kit_assert_fail(const char *expr,
      9  *                                        const char *file,
     10  *                                        int         line,
     11  *                                        const char *func);
     12  *
     13  * The user (or environment) must define this. It must not return.
     14  *
     15  * NOTE: this header has no include guard by design. C11 7.2 requires the
     16  * `assert` macro to reflect the current state of NDEBUG on every
     17  * inclusion, so re-including <assert.h> after #define/#undef NDEBUG must
     18  * flip its definition.
     19  */
     20 
     21 #undef assert
     22 
     23 #ifdef NDEBUG
     24 #define assert(ignore) ((void)0)
     25 #else
     26 
     27 _Noreturn void __kit_assert_fail(const char* expr, const char* file, int line,
     28                                  const char* func);
     29 
     30 #define assert(expr) \
     31   ((expr) ? (void)0 : __kit_assert_fail(#expr, __FILE__, __LINE__, __func__))
     32 
     33 #endif /* NDEBUG */
     34 
     35 /* static_assert: identical re-definition across re-inclusions is allowed
     36    by C11 6.10.3p2 and produces no diagnostic. */
     37 #define static_assert _Static_assert