mirror of
https://github.com/ipxe/ipxe
synced 2026-02-02 19:57:34 +03:00
[libc] Replace linker_assert() with build_assert()
We currently implement build-time assertions via a mechanism that generates a call to an undefined external function that will cause the link to fail unless the compiler can prove that the asserted condition is true (and thereby eliminate the undefined function call). This assertion mechanism can be used for conditions that are not amenable to the use of static_assert(), since static_assert() will not allow for proofs via dead code elimination. Add __attribute__((error(...))) to the undefined external function, so that the error is raised at compile time rather than at link time. This allows us to provide a more meaningful error message (which will include the file name and line number, as with any other compile-time error), and avoids the need for the caller to specify a unique symbol name for the external function. Change the name from linker_assert() to build_assert(), since the assertion now takes place at compile time rather than at link time. Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -65,19 +65,22 @@ assert_printf ( const char *fmt, ... ) asm ( "printf" );
|
||||
#define static_assert(x) _Static_assert( x, #x )
|
||||
|
||||
/**
|
||||
* Assert a condition at link-time.
|
||||
* Assert a condition at build time (after dead code elimination)
|
||||
*
|
||||
* If the condition is not true, the link will fail with an unresolved
|
||||
* symbol (error_symbol).
|
||||
* If the compiler cannot prove that the condition is true, the build
|
||||
* will fail with an error message.
|
||||
*
|
||||
* This macro is iPXE-specific. Do not use this macro in code
|
||||
* intended to be portable.
|
||||
*
|
||||
*/
|
||||
#define linker_assert( condition, error_symbol ) \
|
||||
if ( ! (condition) ) { \
|
||||
extern void error_symbol ( void ); \
|
||||
error_symbol(); \
|
||||
}
|
||||
#define build_assert( condition ) \
|
||||
do { \
|
||||
if ( ! (condition) ) { \
|
||||
extern void __attribute__ (( error ( \
|
||||
"build_assert(" #condition ") failed" \
|
||||
) )) _C2 ( build_assert_, __LINE__ ) ( void ); \
|
||||
_C2 ( build_assert_, __LINE__ ) (); \
|
||||
} \
|
||||
} while ( 0 )
|
||||
|
||||
#endif /* _ASSERT_H */
|
||||
|
||||
Reference in New Issue
Block a user