mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Refactor assert(3).
This commit is contained in:
parent
9d4bc2a15f
commit
d43a044575
3 changed files with 37 additions and 38 deletions
|
@ -17,7 +17,7 @@ CXXFLAGS=-std=gnu++11 -fno-exceptions -fno-rtti
|
|||
ASFLAGS=
|
||||
|
||||
FREEOBJS=\
|
||||
assert/_assert.o \
|
||||
assert/__assert.o \
|
||||
aux/c++.o \
|
||||
aux/op-new.o \
|
||||
ctype/ctype.o \
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
@ -17,31 +17,33 @@
|
|||
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/>.
|
||||
|
||||
assert/_assert.cpp
|
||||
assert/__assert.cpp
|
||||
Reports the occurence of an assertion failure.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#if !defined(__is_sortix_kernel)
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#if defined(__is_sortix_kernel)
|
||||
#include <sortix/kernel/decl.h>
|
||||
#include <sortix/kernel/panic.h>
|
||||
#endif
|
||||
|
||||
void _assert(const char* filename, unsigned int line, const char* functionname,
|
||||
const char* expression)
|
||||
extern "C"
|
||||
void __assert(const char* filename,
|
||||
unsigned long line,
|
||||
const char* function_name,
|
||||
const char* expression)
|
||||
{
|
||||
#if !defined(__is_sortix_kernel)
|
||||
fprintf(stderr, "Assertion failure: %s:%u: %s: %s\n", filename, line,
|
||||
functionname, expression);
|
||||
abort();
|
||||
#if __is_sortix_kernel
|
||||
Sortix::PanicF("Assertion failure: %s:%lu: %s: %s\n", filename, line,
|
||||
function_name, expression);
|
||||
#else
|
||||
Sortix::PanicF("Assertion failure: %s:%u: %s: %s\n", filename, line,
|
||||
functionname, expression);
|
||||
fprintf(stderr, "Assertion failure: %s:%lu: %s: %s\n", filename, line,
|
||||
function_name, expression);
|
||||
abort();
|
||||
#endif
|
||||
}
|
|
@ -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.
|
||||
|
||||
|
@ -22,21 +22,23 @@
|
|||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _ASSERT_H
|
||||
#define _ASSERT_H 1
|
||||
#ifndef INCLUDE_ASSERT_H
|
||||
#define INCLUDE_ASSERT_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
|
||||
|
||||
/* 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. */
|
||||
void _assert(const char* filename, unsigned int line, const char* functionname,
|
||||
const char* expression) __attribute__ ((noreturn));
|
||||
__attribute__((noreturn))
|
||||
void __assert(const char*, unsigned long, const char*, const char*);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
|
@ -47,20 +49,15 @@ __END_DECLS
|
|||
#undef assert
|
||||
#endif
|
||||
|
||||
/* Redefine the assert macro on each <assert.h> inclusion. */
|
||||
#ifdef NDEBUG
|
||||
/* If not debugging, we'll declare a no-operation assert macro. */
|
||||
#if defined(NDEBUG)
|
||||
#define assert(ignore) (__ASSERT_VOID_CAST(0))
|
||||
#endif
|
||||
|
||||
#define assert(ignore) ((void) 0)
|
||||
|
||||
#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(). */
|
||||
/* Otherwise, declare the normal assert macro. */
|
||||
#if !defined(NDEBUG)
|
||||
#define assert(invariant) \
|
||||
if ( __builtin_expect(!(invariant), 0) ) \
|
||||
{ \
|
||||
_assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, #invariant); \
|
||||
}
|
||||
|
||||
#endif /* !NDEBUG */
|
||||
((invariant) \
|
||||
? __ASSERT_VOID_CAST(0) \
|
||||
: __assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, #invariant))
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue