1 | #ifndef _ASSERT_H |
---|
2 | #define _ASSERT_H |
---|
3 | |
---|
4 | /** @file |
---|
5 | * |
---|
6 | * Assertions |
---|
7 | * |
---|
8 | * This file provides two assertion macros: assert() (for run-time |
---|
9 | * assertions) and linker_assert() (for link-time assertions). |
---|
10 | * |
---|
11 | */ |
---|
12 | |
---|
13 | FILE_LICENCE ( GPL2_OR_LATER ); |
---|
14 | |
---|
15 | #ifdef NDEBUG |
---|
16 | #define ASSERTING 0 |
---|
17 | #else |
---|
18 | #define ASSERTING 1 |
---|
19 | #endif |
---|
20 | |
---|
21 | /** printf() for assertions |
---|
22 | * |
---|
23 | * This function exists so that the assert() macro can expand to |
---|
24 | * printf() calls without dragging the printf() prototype into scope. |
---|
25 | * |
---|
26 | * As far as the compiler is concerned, assert_printf() and printf() are |
---|
27 | * completely unrelated calls; it's only at the assembly stage that |
---|
28 | * references to the assert_printf symbol are collapsed into references |
---|
29 | * to the printf symbol. |
---|
30 | */ |
---|
31 | extern int __attribute__ (( format ( printf, 1, 2 ) )) |
---|
32 | assert_printf ( const char *fmt, ... ) asm ( "printf" ); |
---|
33 | |
---|
34 | /** |
---|
35 | * Assert a condition at run-time. |
---|
36 | * |
---|
37 | * If the condition is not true, a debug message will be printed. |
---|
38 | * Assertions only take effect in debug-enabled builds (see DBG()). |
---|
39 | * |
---|
40 | * @todo Make an assertion failure abort the program |
---|
41 | * |
---|
42 | */ |
---|
43 | #define assert( condition ) \ |
---|
44 | do { \ |
---|
45 | if ( ASSERTING && ! (condition) ) { \ |
---|
46 | assert_printf ( "assert(%s) failed at %s line %d\n", \ |
---|
47 | #condition, __FILE__, __LINE__ ); \ |
---|
48 | } \ |
---|
49 | } while ( 0 ) |
---|
50 | |
---|
51 | /** |
---|
52 | * Assert a condition at link-time. |
---|
53 | * |
---|
54 | * If the condition is not true, the link will fail with an unresolved |
---|
55 | * symbol (error_symbol). |
---|
56 | * |
---|
57 | * This macro is gPXE-specific. Do not use this macro in code |
---|
58 | * intended to be portable. |
---|
59 | * |
---|
60 | */ |
---|
61 | #define linker_assert( condition, error_symbol ) \ |
---|
62 | if ( ! (condition) ) { \ |
---|
63 | extern void error_symbol ( void ); \ |
---|
64 | error_symbol(); \ |
---|
65 | } |
---|
66 | |
---|
67 | #endif /* _ASSERT_H */ |
---|