diff mbox series

[1/4] include/qemu/osdep: Add qemu_build_assert

Message ID 20221024232435.3334600-2-richard.henderson@linaro.org
State Superseded
Headers show
Series atomic: Friendlier assertions, avoidance of __sync | expand

Commit Message

Richard Henderson Oct. 24, 2022, 11:24 p.m. UTC
This differs from assert, in that with optimization enabled it
triggers at build-time.  It differs from QEMU_BUILD_BUG_ON,
aka _Static_assert, in that it is sensitive to control flow
and is subject to dead-code elimination.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 include/qemu/osdep.h | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Philippe Mathieu-Daudé Oct. 25, 2022, 1:15 p.m. UTC | #1
On 25/10/22 01:24, Richard Henderson wrote:
> This differs from assert, in that with optimization enabled it
> triggers at build-time.  It differs from QEMU_BUILD_BUG_ON,
> aka _Static_assert, in that it is sensitive to control flow
> and is subject to dead-code elimination.
> 
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   include/qemu/osdep.h | 8 ++++++++
>   1 file changed, 8 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
diff mbox series

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index b1c161c035..2276094729 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -186,6 +186,14 @@  void QEMU_ERROR("code path is reachable")
 #define qemu_build_not_reached()  g_assert_not_reached()
 #endif
 
+/**
+ * qemu_build_assert()
+ *
+ * The compiler, during optimization, is expected to prove that the
+ * assertion is true.
+ */
+#define qemu_build_assert(test)  while (!(test)) qemu_build_not_reached()
+
 /*
  * According to waitpid man page:
  * WCOREDUMP