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.
This commit is contained in:
Lephenixnoir 2021-05-16 18:05:56 +02:00
parent 97d52ff0b1
commit 676601b894
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
3 changed files with 39 additions and 0 deletions

View file

@ -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

27
include/assert.h Normal file
View file

@ -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

11
src/libc/assert/assert.c Normal file
View file

@ -0,0 +1,11 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
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();
}