From 676601b894df9c08a48eb76f02bd68f5ba92aba3 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 16 May 2021 18:05:56 +0200 Subject: [PATCH] assert: add a partial assert implementation (LDEPS) This currently does not link because fprintf, stderr and abort are missing on most platforms. But the code is there. --- CMakeLists.txt | 1 + include/assert.h | 27 +++++++++++++++++++++++++++ src/libc/assert/assert.c | 11 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 include/assert.h create mode 100644 src/libc/assert/assert.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 44541eb..032f57e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ endif() # Building set(SOURCES + src/libc/assert/assert.c src/libc/stdio/vsnprintf.c src/libc/stdio/sprintf.c src/libc/stdio/dprintf.c diff --git a/include/assert.h b/include/assert.h new file mode 100644 index 0000000..e375e46 --- /dev/null +++ b/include/assert.h @@ -0,0 +1,27 @@ +/* +** assert.h> can be included several times and the assert macro is redefined +** every time to match the definition of NDEBUG (7.2ยง1) +*/ +#ifdef __ASSERT_H__ +# undef __ASSERT_H__ +# undef assert +#endif + +#define __ASSERT_H__ + +/* Internal function that prints error message and aborts */ +void __assert_fail(char const *__file, int __line, char const *__function, + char const *__expression); + +#ifdef NDEBUG +# define assert(ignore) ((void)0) +#else +# define assert(expression) \ + ((expression) \ + ? (void)0 : \ + : __assert_fail(__FILE__, __LINE__, __PRETTY_FUNCTION__, #expression)) +#endif + +#ifndef __cplusplus +# define static_assert _Static_assert +#endif diff --git a/src/libc/assert/assert.c b/src/libc/assert/assert.c new file mode 100644 index 0000000..cb23a89 --- /dev/null +++ b/src/libc/assert/assert.c @@ -0,0 +1,11 @@ +#include +#include +#include + +void __assert_fail(char const *file, int line, char const *func, char const + *expression) +{ + fprintf(stderr, "%s:%d:%s: assertion failed: %s\n", + file, line, func, expression); + abort(); +}