From 2eee382a62a909d5a3f2f5eda94f30fe68fd5335 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Fri, 20 Jan 2012 13:41:10 +0000 Subject: initial import of the C runtime --- src/runtime/c/gu/assert.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/runtime/c/gu/assert.h (limited to 'src/runtime/c/gu/assert.h') diff --git a/src/runtime/c/gu/assert.h b/src/runtime/c/gu/assert.h new file mode 100644 index 000000000..9d7ecc15f --- /dev/null +++ b/src/runtime/c/gu/assert.h @@ -0,0 +1,61 @@ +#ifndef GU_ASSERT_H_ +#define GU_ASSERT_H_ + +#include + +typedef enum { + GU_ASSERT_PRECOND, + GU_ASSERT_ASSERTION, + GU_ASSERT_POSTCOND, + GU_ASSERT_NEVER +} GuAssertMode; + +void +gu_abort_v_(GuAssertMode mode, + const char* file, const char* func, int line, + const char* msg_fmt, va_list args); + +void +gu_abort_(GuAssertMode mode, + const char* file, const char* func, int line, + const char* msg_fmt, ...); + +#ifndef NDEBUG +#define gu_assertion_(mode_, expr_, ...) \ + GU_BEGIN \ + if (!(expr_)) { \ + gu_abort_(mode_, __FILE__, __func__, __LINE__, __VA_ARGS__); \ + } \ + GU_END +#else +// this should prevent unused variable warnings when a variable is only used +// in an assertion +#define gu_assertion_(mode_, expr_, ...) \ + GU_BEGIN \ + (void) (sizeof (expr_)); \ + GU_END +#endif + + +#define gu_require(expr) \ + gu_assertion_(GU_ASSERT_PRECOND, expr, "%s", #expr) + +#define gu_assert_msg(expr, ...) \ + gu_assertion_(GU_ASSERT_ASSERTION, expr, __VA_ARGS__) + +#define gu_assert(expr) \ + gu_assert_msg(expr, "%s", #expr) + +#define gu_ensure(expr) \ + gu_assertion_(GU_ASSERT_POSTCOND, expr, "%s", #expr) + +#define gu_impossible_msg(...) \ + gu_assertion_(GU_ASSERT_ASSERTION, false, __VA_ARGS__) + +#define gu_impossible() \ + gu_impossible_msg(NULL) + +void +gu_fatal(const char* fmt, ...); + +#endif /* GU_ASSERT_H_ */ -- cgit v1.2.3