libc: Add assert.h
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-22 12:51:30 +01:00
parent 34a9b35037
commit cb59a4e0e3
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 52 additions and 2 deletions

View File

@ -7,6 +7,7 @@ set(SOURCES
src/unistd.cpp
src/errno.cpp
src/string.cpp
src/assert.cpp
src/atexit.cpp
src/ctype.cpp
src/time.cpp

33
libc/include/assert.h Normal file
View File

@ -0,0 +1,33 @@
/* assert.h: Debug assertions. */
#ifndef _ASSERT_H
#define _ASSERT_H
#include <bits/attrs.h>
#ifdef __cplusplus
extern "C"
{
#endif
__noreturn void __assertion_failed(const char* file, int line, const char* function, const char* expr);
#ifdef __cplusplus
}
#endif
#if !defined(__cplusplus) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define static_assert _Static_assert
#endif
#ifdef NDEBUG
#define assert(expr)
#else
#define assert(expr) ((expr) ? (void)0 : __assertion_failed(__FILE__, __LINE__, __func__, #expr))
#endif
#ifdef _LUNA_SOURCE
#define release_assert(expr) ((expr) ? (void)0 : __assertion_failed(__FILE__, __LINE__, __func__, #expr))
#endif
#endif

View File

@ -3,7 +3,7 @@
#ifndef _BITS_ATTRS_H
#define _BITS_ATTRS_H
#if !defined(_STDLIB_H) && !defined(_STRING_H)
#if !defined(_STDLIB_H) && !defined(_STRING_H) && !defined(_ASSERT_H)
#error "Never include bits/attrs.h directly; use one of the standard library headers."
#endif

14
libc/src/assert.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <assert.h>
#include <luna/Attributes.h>
#include <stdio.h>
#include <stdlib.h>
extern "C"
{
_weak [[noreturn]] void __assertion_failed(const char* file, int line, const char* function, const char* expr)
{
// FIXME: Output to standard error instead of standard output.
printf("%s:%d: %s: Assertion '%s' failed.\n", file, line, function, expr);
abort();
}
}

View File

@ -54,10 +54,12 @@
#define ETIMEDOUT 110 // Connection timed out
#define EALREADY 114 // Operation already in progress
#if defined(IN_MOON) || defined(USE_FREESTANDING) || defined(_LUNA_SYSTEM_ERROR_EXTENSIONS)
#if defined(IN_MOON) || defined(USE_FREESTANDING) || defined(_LUNA_SYSTEM_ERROR_EXTENSIONS) || defined(_LUNA_SOURCE)
// This one is Luna-specific.
#define EFIXME 342 // Functionality not yet implemented
#ifndef _LUNA_SOURCE
const char* error_string(int error);
#endif
#endif