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
This commit is contained in:
Jacques Lucke 2021-03-23 16:45:44 +01:00
parent 7046e37ede
commit 433fb9326e
2 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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