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 build time
60  *
61  * If the compiler cannot prove that the condition is true, the build
62  * will fail with an error message.
63  */
64 #undef static_assert
65 #define static_assert(x) _Static_assert( x, #x )
66 
67 /**
68  * Assert a condition at build time (after dead code elimination)
69  *
70  * If the compiler cannot prove that the condition is true, the build
71  * will fail with an error message.
72  *
73  * This macro is iPXE-specific. Do not use this macro in code
74  * intended to be portable.
75  */
76 #define build_assert( condition ) \
77  do { \
78  if ( ! (condition) ) { \
79  extern void __attribute__ (( warning ( \
80  "build_assert(" #condition ") failed" \
81  ) )) _C2 ( build_assert_, __LINE__ ) ( void ); \
82  _C2 ( build_assert_, __LINE__ ) (); \
83  } \
84  } while ( 0 )
85 
86 #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