diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-23 18:45:44 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-23 18:45:44 +0300 |
commit | 433fb9326ece7b0b84f2f548e43059881cca68b5 (patch) | |
tree | fdfaf85e5e41b2f2c7d1182b68fc985dc436a221 /source/blender | |
parent | 7046e37eded55ac8879835500788a1e734437bbf (diff) |
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
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_assert.h | 12 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_assert.c | 6 |
2 files changed, 18 insertions, 0 deletions
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 |