[Bf-blender-cvs] [433fb9326ec] master: BLI: add macro to indicate unreachable code

Jacques Lucke noreply at git.blender.org
Tue Mar 23 16:51:08 CET 2021


Commit: 433fb9326ece7b0b84f2f548e43059881cca68b5
Author: Jacques Lucke
Date:   Tue Mar 23 16:45:44 2021 +0100
Branches: master
https://developer.blender.org/rB433fb9326ece7b0b84f2f548e43059881cca68b5

BLI: add macro to indicate unreachable code

This adds a `BLI_assert_unreachable()` macro, that should be used instead
of `BLI_assert(false)` in many places.

* `BLI_assert_unreachable` provides more information than `BLI_assert(false)`
  to people reading the code.
* `BLI_assert_unreachable` will print an error to `stderr` in a release build.
  This makes it more likely that we will get bug reports when the assumptions
  of a developer were wrong.

Differential Revision: https://developer.blender.org/D10780

===================================================================

M	source/blender/blenlib/BLI_assert.h
M	source/blender/blenlib/intern/BLI_assert.c

===================================================================

diff --git a/source/blender/blenlib/BLI_assert.h b/source/blender/blenlib/BLI_assert.h
index 172a2fb44ca..685f526b4ad 100644
--- a/source/blender/blenlib/BLI_assert.h
+++ b/source/blender/blenlib/BLI_assert.h
@@ -33,6 +33,7 @@ extern "C" {
 void _BLI_assert_print_pos(const char *file, const int line, const char *function, const char *id);
 void _BLI_assert_print_backtrace(void);
 void _BLI_assert_abort(void);
+void _BLI_assert_unreachable_print(const char *file, const int line, const char *function);
 
 #ifdef _MSC_VER
 #  include <crtdbg.h> /* for _STATIC_ASSERT */
@@ -88,6 +89,17 @@ void _BLI_assert_abort(void);
 #define BLI_STATIC_ASSERT_ALIGN(st, align) \
   BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")
 
+/**
+ * Indicates that this line of code should never be executed. If it is reached, it will abort in
+ * debug builds and print an error in release builds.
+ */
+#define BLI_assert_unreachable() \
+  { \
+    _BLI_assert_unreachable_print(__FILE__, __LINE__, __func__); \
+    BLI_assert(!"This line of code is marked to be unreachable."); \
+  } \
+  ((void)0)
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenlib/intern/BLI_assert.c b/source/blender/blenlib/intern/BLI_assert.c
index f397cdcd8a9..887f583242f 100644
--- a/source/blender/blenlib/intern/BLI_assert.c
+++ b/source/blender/blenlib/intern/BLI_assert.c
@@ -31,6 +31,12 @@ void _BLI_assert_print_pos(const char *file, const int line, const char *functio
   fprintf(stderr, "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", file, line, function, id);
 }
 
+void _BLI_assert_unreachable_print(const char *file, const int line, const char *function)
+{
+  fprintf(stderr, "Code marked as unreachable has been executed. Please report this as a bug.\n");
+  fprintf(stderr, "Error found at %s:%d in %s.\n", file, line, function);
+}
+
 void _BLI_assert_print_backtrace(void)
 {
 #ifndef NDEBUG



More information about the Bf-blender-cvs mailing list