diff options
| author | Michael Brown | 2024-01-16 14:24:29 +0100 |
|---|---|---|
| committer | Michael Brown | 2024-01-16 14:35:08 +0100 |
| commit | 4b7d9a6af08cb704ce77eadba2a7bb1b06c1554c (patch) | |
| tree | 9ee6e4fab3807cea2b6b4c5fa8f1984510e6107a /src/include/assert.h | |
| parent | [libc] Make static_assert() available via assert.h (diff) | |
| download | ipxe-4b7d9a6af08cb704ce77eadba2a7bb1b06c1554c.tar.gz ipxe-4b7d9a6af08cb704ce77eadba2a7bb1b06c1554c.tar.xz ipxe-4b7d9a6af08cb704ce77eadba2a7bb1b06c1554c.zip | |
[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>
Diffstat (limited to 'src/include/assert.h')
| -rw-r--r-- | src/include/assert.h | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/include/assert.h b/src/include/assert.h index 6d0531801..b3a9b1fe0 100644 --- a/src/include/assert.h +++ b/src/include/assert.h @@ -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 */ |
