Refactor assert(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-10-26 22:46:46 +02:00
parent 9d4bc2a15f
commit d43a044575
3 changed files with 37 additions and 38 deletions

View File

@ -17,7 +17,7 @@ CXXFLAGS=-std=gnu++11 -fno-exceptions -fno-rtti
ASFLAGS= ASFLAGS=
FREEOBJS=\ FREEOBJS=\
assert/_assert.o \ assert/__assert.o \
aux/c++.o \ aux/c++.o \
aux/op-new.o \ aux/op-new.o \
ctype/ctype.o \ ctype/ctype.o \

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012. Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -17,31 +17,33 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>. along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
assert/_assert.cpp assert/__assert.cpp
Reports the occurence of an assertion failure. Reports the occurence of an assertion failure.
*******************************************************************************/ *******************************************************************************/
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#if !defined(__is_sortix_kernel)
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#endif
#if defined(__is_sortix_kernel) #if defined(__is_sortix_kernel)
#include <sortix/kernel/decl.h> #include <sortix/kernel/decl.h>
#include <sortix/kernel/panic.h> #include <sortix/kernel/panic.h>
#endif #endif
void _assert(const char* filename, unsigned int line, const char* functionname, extern "C"
const char* expression) void __assert(const char* filename,
unsigned long line,
const char* function_name,
const char* expression)
{ {
#if !defined(__is_sortix_kernel) #if __is_sortix_kernel
fprintf(stderr, "Assertion failure: %s:%u: %s: %s\n", filename, line, Sortix::PanicF("Assertion failure: %s:%lu: %s: %s\n", filename, line,
functionname, expression); function_name, expression);
abort();
#else #else
Sortix::PanicF("Assertion failure: %s:%u: %s: %s\n", filename, line, fprintf(stderr, "Assertion failure: %s:%lu: %s: %s\n", filename, line,
functionname, expression); function_name, expression);
abort();
#endif #endif
} }

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012. Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -22,21 +22,23 @@
*******************************************************************************/ *******************************************************************************/
#ifndef _ASSERT_H #ifndef INCLUDE_ASSERT_H
#define _ASSERT_H 1 #define INCLUDE_ASSERT_H
#include <sys/cdefs.h> #include <sys/cdefs.h>
/* stdlib.h is not needed, but GCC fixincludes thinks it is, so fool it. */
#if 0
#include <stdlib.h>
#endif
__BEGIN_DECLS __BEGIN_DECLS
/* Determine how the value should be cast to void. */
#if defined __cplusplus
#define __ASSERT_VOID_CAST(x) static_cast<void>(x)
#else
#define __ASSERT_VOID_CAST(x) (void) x
#endif
/* The actual implementation of assert. */ /* The actual implementation of assert. */
void _assert(const char* filename, unsigned int line, const char* functionname, __attribute__((noreturn))
const char* expression) __attribute__ ((noreturn)); void __assert(const char*, unsigned long, const char*, const char*);
__END_DECLS __END_DECLS
@ -47,20 +49,15 @@ __END_DECLS
#undef assert #undef assert
#endif #endif
/* Redefine the assert macro on each <assert.h> inclusion. */ /* If not debugging, we'll declare a no-operation assert macro. */
#ifdef NDEBUG #if defined(NDEBUG)
#define assert(ignore) (__ASSERT_VOID_CAST(0))
#endif
#define assert(ignore) ((void) 0) /* Otherwise, declare the normal assert macro. */
#if !defined(NDEBUG)
#else /* !NDEBUG */
/* Use __builtin_expect to tell the compiler that we don't expect a failure to
happen and thus it can do better branch prediction. Naturally we don't
optimize for the case where the program is about to abort(). */
#define assert(invariant) \ #define assert(invariant) \
if ( __builtin_expect(!(invariant), 0) ) \ ((invariant) \
{ \ ? __ASSERT_VOID_CAST(0) \
_assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, #invariant); \ : __assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, #invariant))
} #endif
#endif /* !NDEBUG */