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(); +}