StuBS
assert.h File Reference

Contains several macros usable for making assertions. More...

This graph shows which files directly or indirectly include this file:

Macros

#define STRINGIFY(S)   #S
 Converts a macro parameter into a string.
 
#define assert_size(TYPE, SIZE)    static_assert(sizeof(TYPE) == (SIZE), "Wrong size for " STRINGIFY(TYPE))
 Statically ensure (at compile time) that a data type (or variable) has the expected size.
 
#define assert(EXP)
 Ensure (at execution time) an expression evaluates to true, print an error message and stop the CPU otherwise.
 

Functions

void assertion_failed (const char *exp, const char *func, const char *file, int line)
 Handles a failed assertion.
 

Detailed Description

Contains several macros usable for making assertions.

Depending on the type of assertion (either static or at runtime), a failing assertion will trigger an error. For static assertion, this error will be shown at compile time and abort compilation. Runtime assertions will trigger a message containing details about the error occurred and will make the CPU die.

Macro Definition Documentation

◆ STRINGIFY

#define STRINGIFY (   S)    #S

Converts a macro parameter into a string.

Parameters
SExpression to be converted
Returns
stringified version of S

◆ assert_size

#define assert_size (   TYPE,
  SIZE 
)     static_assert(sizeof(TYPE) == (SIZE), "Wrong size for " STRINGIFY(TYPE))

Statically ensure (at compile time) that a data type (or variable) has the expected size.

Parameters
TYPEThe type to be checked
SIZEExpected size in bytes

◆ assert

#define assert (   EXP)
Value:
do { \
if (__builtin_expect(!((EXP)), 0)) { \
assertion_failed(STRINGIFY(EXP), __func__, __FILE__, __LINE__); \
} \
} while (false)
#define STRINGIFY(S)
Converts a macro parameter into a string.
Definition assert.h:18

Ensure (at execution time) an expression evaluates to true, print an error message and stop the CPU otherwise.

Parameters
EXPThe expression to be checked

Function Documentation

◆ assertion_failed()

void assertion_failed ( const char *  exp,
const char *  func,
const char *  file,
int  line 
)

Handles a failed assertion.

This function will print a message containing further information about the failed assertion and stops the current CPU permanently.

Note
This function should never be called directly, but only via the macro assert.
Todo:
Implement Remainder of Method (output & CPU stopping)
Parameters
expExpression that did not hold
funcName of the function in which the assertion failed
fileName of the file in which the assertion failed
lineLine in which the assertion failed