KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 assert.h
4 Copyright (C) 2002, 2004 Megan Potter
5
6*/
7
8#ifndef __ASSERT_H
9#define __ASSERT_H
10
11#include <kos/cdefs.h>
12__BEGIN_DECLS
13
14/**
15 \file assert.h
16 \brief Standard C Assertions
17 \ingroup assertions
18
19 This file contains the standard C assertions to raise an assertion or to
20 change the assertion handler.
21
22 \author Megan Potter
23*/
24
25/** \defgroup assertions Assertions
26 \brief assert() management and custom handlers
27 \ingroup debugging
28
29 KOS maps the standard C assert() mechanism to a default implementation
30 which logs the failed expression as well as the source code context.
31 A secondary assertion mechanism, assert_msg() is also provided for adding
32 a cusom message. You may also override KOS's assertion handler and replace
33 it with your own via assert_set_handler().
34
35 @{
36*/
37
38/* This is nice and simple, modeled after the BSD one like most of KOS;
39 the addition here is assert_msg(), which allows you to provide an
40 error message. */
41/** \cond */
42#define _assert(e) assert(e)
43
44/* 1) C11-C17: _Static_assert is a reserved word, and <assert.h> adds a macro,
45 static_assert which maps to it.
46 2) C23+: static_assert becomes a reserved word, _Static_assert becomes deprecated.
47 3) C++11+: static_assert is a reserved word.
48*/
49#if __STDC_VERSION__ >= 201112L && __STDC_VERSION__ <= 201710L && !defined __cplusplus
50# define static_assert _Static_assert
51#endif
52/** \endcond */
53
54#ifdef NDEBUG
55# define assert(e) ((void)0)
56# define assert_msg(e, m) ((void)0)
57#else
58
59/* This bit of magic borrowed from Newlib's assert.h... */
60/* \cond */
61#ifndef __ASSERT_FUNC
62#if defined(__cplusplus)
63# define __ASSERT_FUNC __PRETTY_FUNCTION__
64#elif __STDC_VERSION__ >= 199901L
65# define __ASSERT_FUNC __func__
66#elif __GNUC__ >= 2
67# define __ASSERT_FUNC __FUNCTION__
68#else
69# define __ASSERT_FUNC ((char *)0)
70#endif
71#endif
72/* \endcond */
73
74/** \brief Standard C assertion macro.
75
76 This macro does a standard C assertion, wherein the expression is evaluated,
77 and if false, the program is ultimately aborted using abort(). If the
78 expression evaluates to true, the macro does nothing (other than any side
79 effects of evaluating the expression).
80
81 \param e A value or expression to be evaluated as true or
82 false.
83*/
84# define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, NULL, __ASSERT_FUNC))
85
86/** \brief assert() with a custom message.
87
88 This macro acts the same as the assert() macro, but allows you to specify a
89 custom message to be printed out if the assertion fails.
90
91 \param e A value or expression to be evaluated as true or
92 false.
93 \param m A message (const char *).
94*/
95# define assert_msg(e, m) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, m, __ASSERT_FUNC))
96#endif
97
98/* \cond */
99/* Defined in assert.c */
100void __assert(const char *file, int line, const char *expr,
101 const char *msg, const char *func);
102/* \endcond */
103
104/** \brief Assertion handler type.
105
106 The user can provide their own assertion handler with this type. If none is
107 provided, a default is used which ultimately prints out the location of the
108 failed assertion and calls abort().
109
110 \param file The filename where the assertion happened.
111 \param line The line number where the assertion happened.
112 \param expr The expression that raised the assertion.
113 \param msg A custom message for why the assertion happened.
114 \param func The function name from which the assertion happened.
115
116 \see assert_set_handler
117*/
118typedef void (*assert_handler_t)(const char * file, int line, const char * expr,
119 const char * msg, const char * func);
120
121/** \brief Set an assertion handler to call on a failed assertion.
122
123 The default assertion handler simply will print a message and call abort().
124 NULL is a valid value and will cause nothing to happen on an assert.
125
126 \return The old assertion handler so it may be restored
127 later if appropriate.
128
129 \see assert_handler_t
130*/
132
133/** @} */
134
135__END_DECLS
136
137#endif /* __ASSERT_H */
void hnd(const char *file, int line, const char *expr, const char *msg, const char *func)
Definition asserthnd.c:53
Various common macros used throughout the codebase.
void(* assert_handler_t)(const char *file, int line, const char *expr, const char *msg, const char *func)
Assertion handler type.
Definition assert.h:118
assert_handler_t assert_set_handler(assert_handler_t hnd)
Set an assertion handler to call on a failed assertion.
__noinline void func(void)
Definition stacktrace.c:13