iPXE
assert.h
Go to the documentation of this file.
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_OR_UBDL );
14 
15 #ifndef ASSERTING
16 #ifdef NDEBUG
17 #define ASSERTING 0
18 #else
19 #define ASSERTING 1
20 #endif
21 #endif
22 
23 extern unsigned int assertion_failures;
24 
25 #define ASSERTED ( ASSERTING && ( assertion_failures != 0 ) )
26 
27 /** printf() for assertions
28  *
29  * This function exists so that the assert() macro can expand to
30  * printf() calls without dragging the printf() prototype into scope.
31  *
32  * As far as the compiler is concerned, assert_printf() and printf() are
33  * completely unrelated calls; it's only at the assembly stage that
34  * references to the assert_printf symbol are collapsed into references
35  * to the printf symbol.
36  */
37 extern int __attribute__ (( format ( printf, 1, 2 ) ))
38 assert_printf ( const char *fmt, ... ) asm ( "printf" );
39 
40 /**
41  * Assert a condition at run-time.
42  *
43  * If the condition is not true, a debug message will be printed.
44  * Assertions only take effect in debug-enabled builds (see DBG()).
45  *
46  * @todo Make an assertion failure abort the program
47  *
48  */
49 #define assert( condition ) \
50  do { \
51  if ( ASSERTING && ! (condition) ) { \
52  assertion_failures++; \
53  assert_printf ( "assert(%s) failed at %s line %d\n", \
54  #condition, __FILE__, __LINE__ ); \
55  } \
56  } while ( 0 )
57 
58 /**
59  * Assert a condition at link-time.
60  *
61  * If the condition is not true, the link will fail with an unresolved
62  * symbol (error_symbol).
63  *
64  * This macro is iPXE-specific. Do not use this macro in code
65  * intended to be portable.
66  *
67  */
68 #define linker_assert( condition, error_symbol ) \
69  if ( ! (condition) ) { \
70  extern void error_symbol ( void ); \
71  error_symbol(); \
72  }
73 
74 #endif /* _ASSERT_H */
int asm("printf")
int __attribute__((format(printf, 1, 2))) assert_printf(const char *fmt
printf() for assertions
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
unsigned int assertion_failures
Number of assertion failures triggered.
Definition: assert.c:35
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int ssize_t const char * fmt
Definition: vsprintf.h:72
int const char * format
Definition: xfer.h:104