Blender V5.3
BLI_assert.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
15
16#include <stddef.h>
17
18/* Utility functions. */
19
20void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id);
21void _BLI_assert_print_extra(const char *str);
24void _BLI_assert_unreachable_print(const char *file, int line, const char *function);
25
26#ifdef _MSC_VER
27# include <crtdbg.h> /* for _STATIC_ASSERT */
28#endif
29
30#ifndef NDEBUG
31/* _BLI_ASSERT_PRINT_POS */
32# if defined(__GNUC__)
33# define _BLI_ASSERT_PRINT_POS(a) \
34 _BLI_assert_print_pos( \
35 __FILE__, __LINE__, __func__, #a) // NOLINT(bugprone-lambda-function-name)
36# elif defined(_MSC_VER)
37# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #a)
38# else
39# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, "<?>", #a)
40# endif
41/* _BLI_ASSERT_ABORT */
42# ifdef WITH_ASSERT_ABORT
43# define _BLI_ASSERT_ABORT _BLI_assert_abort
44# else
45# define _BLI_ASSERT_ABORT() (void)0
46# endif
47/* BLI_assert */
48# define BLI_assert(a) \
49 (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
50 _BLI_ASSERT_PRINT_POS(a), \
51 _BLI_ASSERT_ABORT(), \
52 NULL)) : \
53 NULL)
55# define BLI_assert_msg(a, msg) \
56 (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
57 _BLI_ASSERT_PRINT_POS(a), \
58 _BLI_assert_print_extra(msg), \
59 _BLI_ASSERT_ABORT(), \
60 NULL)) : \
61 NULL)
62#else
63# define BLI_assert(a) ((void)0)
64# define BLI_assert_msg(a, msg) ((void)0)
65#endif
66
67/* C++11 */
68#define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
69
70#define BLI_STATIC_ASSERT_ALIGN(st, align) \
71 BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")
72
77#define BLI_assert_unreachable() \
78 { \
79 _BLI_assert_unreachable_print(__FILE__, __LINE__, __func__); \
80 BLI_assert_msg(0, "This line of code is marked to be unreachable."); \
81 } \
82 ((void)0)
83
89#define BLI_assert_unreachable_static() \
90 static_assert([]<bool flag = false>() { return flag; }(), \
91 "Unreachable code path reached at compile-time!")
92
93template<class T> inline constexpr bool _bli_always_false = false;
94
99#define BLI_assert_unreachable_static_t(T) \
100 static_assert(_bli_always_false<T>, "Unreachable code path reached at compile-time!")
void _BLI_assert_print_extra(const char *str)
Definition BLI_assert.cc:25
constexpr bool _bli_always_false
Definition BLI_assert.hh:93
void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id)
Definition BLI_assert.cc:20
void _BLI_assert_print_backtrace()
Definition BLI_assert.cc:36
void _BLI_assert_abort()
Definition BLI_assert.cc:43
void _BLI_assert_unreachable_print(const char *file, int line, const char *function)
Definition BLI_assert.cc:30
#define str(s)