Skip to contentSkip to conversation

osdep: Work around MinGW assert

PATCH Applied Richard Henderson 3 participants 4 messages Oct 22, 2018 – Oct 23, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 osdep: Work around MinGW assert

In several places we use assert(FEATURE), and assume that if FEATURE is disabled, all following code is removed as unreachable. Which allows us to compile-out functions that are only present with FEATURE, and have a link-time failure if the functions remain used.

MinGW does not mark its internal function _assert() as noreturn, so the compiler cannot see when code is unreachable, which leads to link errors for this host that are not present elsewhere.

The current build-time failure concerns 62823083b8a2, but I remember having seen this same error before. Fix it once and for all for MinGW.

---
 include/qemu/osdep.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/qemu/osdep.h +12 -0
index 4f8559e550..0c1e335a43 100644
@@ -122,6 +122,18 @@ extern int daemon(int, int);
 #include "glib-compat.h"
 #include "qemu/typedefs.h"
 
+/*
+ * For mingw, as of v6.0.0, the function implementing the assert macro is
+ * not marked a noreturn, so the compiler cannot delete code following an
+ * assert(false) as unused.  We rely on this within the code base to delete
+ * code that is unreachable when features are disabled.
+ * All supported versions of Glib's g_assert() satisfy this requirement.
+ */
+#ifdef __MINGW32__
+#undef assert
+#define assert(x)  g_assert(x)
+#endif
+
 /*
  * According to waitpid man page:
  * WCOREDUMP
Signature · 1 line
2.17.2
#1 lore raw
Quoted 1 line · Richard Henderson

+ * not marked a noreturn, so the compiler cannot delete code following an

Bah. Peter, if you apply this directly, can you please fix the grammar around "marked a return" (either s/a/as/ or s/a// sound equally plausible for me).

r~

Reviewed-by on v1 1/1
#2 lore raw
In reply to Richard Henderson
Quoted 5 lines · Richard Henderson

In several places we use assert(FEATURE), and assume that if FEATURE is disabled, all following code is removed as unreachable. Which allows us to compile-out functions that are only present with FEATURE, and have a link-time failure if the functions remain used.

Quoted 8 lines

MinGW does not mark its internal function _assert() as noreturn, so the compiler cannot see when code is unreachable, which leads to link errors for this host that are not present elsewhere.

The current build-time failure concerns 62823083b8a2, but I remember having seen this same error before. Fix it once and for all for MinGW.

Quoted 28 lines
---
  include/qemu/osdep.h | 12 ++++++++++++
  1 file changed, 12 insertions(+)
include/qemu/osdep.h +12 -0
index 4f8559e550..0c1e335a43 100644
@@ -122,6 +122,18 @@ extern int daemon(int, int);
  #include "glib-compat.h"
  #include "qemu/typedefs.h"
 
+/*
+ * For mingw, as of v6.0.0, the function implementing the assert macro is
+ * not marked a noreturn, so the compiler cannot delete code following an
+ * assert(false) as unused.  We rely on this within the code base to delete
+ * code that is unreachable when features are disabled.
+ * All supported versions of Glib's g_assert() satisfy this requirement.
+ */
+#ifdef __MINGW32__
+#undef assert
+#define assert(x)  g_assert(x)
+#endif
+
  /*
   * According to waitpid man page:
   * WCOREDUMP
#3 lore raw
In reply to Richard Henderson
Quoted 5 lines · Richard Henderson

In several places we use assert(FEATURE), and assume that if FEATURE is disabled, all following code is removed as unreachable. Which allows us to compile-out functions that are only present with FEATURE, and have a link-time failure if the functions remain used.

Quoted 7 lines

MinGW does not mark its internal function _assert() as noreturn, so the compiler cannot see when code is unreachable, which leads to link errors for this host that are not present elsewhere.

The current build-time failure concerns 62823083b8a2, but I remember having seen this same error before. Fix it once and for all for MinGW.

Applied to master (with the typo fixed), thanks.

-- PMM