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#if __STDC_VERSION__ >= 201112L && !defined __cplusplus
45#define static_assert _Static_assert
46#endif
47/** \endcond */
48
49#ifdef NDEBUG
50# define assert(e) ((void)0)
51# define assert_msg(e, m) ((void)0)
52#else
53
54/* This bit of magic borrowed from Newlib's assert.h... */
55/* \cond */
56#ifndef __ASSERT_FUNC
57#if defined(__cplusplus)
58# define __ASSERT_FUNC __PRETTY_FUNCTION__
59#elif __STDC_VERSION__ >= 199901L
60# define __ASSERT_FUNC __func__
61#elif __GNUC__ >= 2
62# define __ASSERT_FUNC __FUNCTION__
63#else
64# define __ASSERT_FUNC ((char *)0)
65#endif
66#endif
67/* \endcond */
68
69/** \brief Standard C assertion macro.
70
71 This macro does a standard C assertion, wherein the expression is evaluated,
72 and if false, the program is ultimately aborted using abort(). If the
73 expression evaluates to true, the macro does nothing (other than any side
74 effects of evaluating the expression).
75
76 \param e A value or expression to be evaluated as true or
77 false.
78*/
79# define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, NULL, __ASSERT_FUNC))
80
81/** \brief assert() with a custom message.
82
83 This macro acts the same as the assert() macro, but allows you to specify a
84 custom message to be printed out if the assertion fails.
85
86 \param e A value or expression to be evaluated as true or
87 false.
88 \param m A message (const char *).
89*/
90# define assert_msg(e, m) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, m, __ASSERT_FUNC))
91#endif
92
93/* \cond */
94/* Defined in assert.c */
95void __assert(const char *file, int line, const char *expr,
96 const char *msg, const char *func);
97/* \endcond */
98
99/** \brief Assertion handler type.
100
101 The user can provide their own assertion handler with this type. If none is
102 provided, a default is used which ultimately prints out the location of the
103 failed assertion and calls abort().
104
105 \param file The filename where the assertion happened.
106 \param line The line number where the assertion happened.
107 \param expr The expression that raised the assertion.
108 \param msg A custom message for why the assertion happened.
109 \param func The function name from which the assertion happened.
110
111 \see assert_set_handler
112*/
113typedef void (*assert_handler_t)(const char * file, int line, const char * expr,
114 const char * msg, const char * func);
115
116/** \brief Set an assertion handler to call on a failed assertion.
117
118 The default assertion handler simply will print a message and call abort().
119 NULL is a valid value and will cause nothing to happen on an assert.
120
121 \return The old assertion handler so it may be restored
122 later if appropriate.
123
124 \see assert_handler_t
125*/
127
128/** @} */
129
130__END_DECLS
131
132#endif /* __ASSERT_H */
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:113
assert_handler_t assert_set_handler(assert_handler_t hnd)
Set an assertion handler to call on a failed assertion.