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
13FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
14FILE_SECBOOT ( PERMITTED );
15
16#ifndef ASSERTING
17#ifdef NDEBUG
18#define ASSERTING 0
19#else
20#define ASSERTING 1
21#endif
22#endif
23
24extern 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 */
38extern int __attribute__ (( format ( printf, 1, 2 ) ))
39assert_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 when debugging is enabled for the
46 * object in which the assertion is compiled (see DBG()).
47 *
48 * Assertions are non-terminating even when enabled. Execution will
49 * continue, since in a debug build this is often more useful to the
50 * developer than terminating immediately (and, as a bootloader, there
51 * is often no defined way to terminate the program other than by
52 * rebooting the system). However, all behaviour after the first
53 * assertion has triggered should be considered to be undefined.
54 *
55 * Assertion conditions will always be compile-checked even when
56 * debugging is not enabled, and can therefore serve as consumers of
57 * otherwise unused variables. The assertion condition is removed by
58 * dead code elimination when debugging is disabled. Assertion
59 * conditions must therefore not have side effects.
60 */
61#define assert( condition ) \
62 do { \
63 if ( ASSERTING && ! (condition) ) { \
64 assert_printf ( "assert(%s) failed at %s line %d\n", \
65 #condition, __FILE__, __LINE__ ); \
66 assertion_failures++; \
67 } \
68 } while ( 0 )
69
70/**
71 * Assert a condition at build time
72 *
73 * If the compiler cannot prove that the condition is true, the build
74 * will fail with an error message.
75 */
76#undef static_assert
77#define static_assert(x) _Static_assert( x, #x )
78
79/**
80 * Assert a condition at build time (after dead code elimination)
81 *
82 * If the compiler cannot prove that the condition is true, the build
83 * will fail with an error message.
84 *
85 * This macro is iPXE-specific. Do not use this macro in code
86 * intended to be portable.
87 */
88#define build_assert( condition ) \
89 do { \
90 if ( ! (condition) ) { \
91 extern void __attribute__ (( warning ( \
92 "build_assert(" #condition ") failed" \
93 ) )) _C2 ( build_assert_, __LINE__ ) ( void ); \
94 _C2 ( build_assert_, __LINE__ ) (); \
95 } \
96 } while ( 0 )
97
98#endif /* _ASSERT_H */
register unsigned long reg2 asm(XEN_REG2)
unsigned int assertion_failures
Number of assertion failures triggered.
Definition assert.c:35
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define __attribute__(x)
Definition compiler.h:10
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:485
int ssize_t const char * fmt
Definition vsprintf.h:73
int const char * format
Definition xfer.h:105