libc: Add assert()

This commit is contained in:
apio 2022-10-15 10:05:48 +02:00
parent 8398b2e2e4
commit d0d6557e99
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,24 @@
#ifndef _ASSERT_H
#define _ASSERT_H
#include <bits/macros.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
__lc_noreturn bool __assertion_failed(const char* file, int line, const char* function, const char* expr);
#ifdef __cplusplus
}
#endif
#ifdef NDEBUG
#define assert(expr) (void)0
#else
#define assert(expr) (bool)(expr) || __assertion_failed(__FILE__, __LINE__, __FUNCTION__, #expr)
#endif
#endif

12
libs/libc/src/assert.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
extern "C"
{
__lc_noreturn bool __assertion_failed(const char* file, int line, const char* function, const char* expr)
{
fprintf(stderr, "%s:%d: %s: Assertion '%s' failed.", file, line, function, expr);
abort();
}
}