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