diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index efba8728..9f686d94 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -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 diff --git a/libc/include/assert.h b/libc/include/assert.h new file mode 100644 index 00000000..325ae3a0 --- /dev/null +++ b/libc/include/assert.h @@ -0,0 +1,33 @@ +/* assert.h: Debug assertions. */ + +#ifndef _ASSERT_H +#define _ASSERT_H + +#include + +#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 diff --git a/libc/include/bits/attrs.h b/libc/include/bits/attrs.h index c9f6cf58..90de54d0 100644 --- a/libc/include/bits/attrs.h +++ b/libc/include/bits/attrs.h @@ -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 diff --git a/libc/src/assert.cpp b/libc/src/assert.cpp new file mode 100644 index 00000000..f24110b6 --- /dev/null +++ b/libc/src/assert.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +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(); + } +} diff --git a/luna/include/luna/SystemError.h b/luna/include/luna/SystemError.h index 45f4f921..9985897a 100644 --- a/luna/include/luna/SystemError.h +++ b/luna/include/luna/SystemError.h @@ -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