Blender V4.3
BLI_assert.h
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
16#include <stddef.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/* Utility functions. */
23
24void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id);
25void _BLI_assert_print_extra(const char *str);
27void _BLI_assert_abort(void);
28void _BLI_assert_unreachable_print(const char *file, int line, const char *function);
29
30#ifdef _MSC_VER
31# include <crtdbg.h> /* for _STATIC_ASSERT */
32#endif
33
34#ifndef NDEBUG
35/* _BLI_ASSERT_PRINT_POS */
36# if defined(__GNUC__)
37# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #a)
38# elif defined(_MSC_VER)
39# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #a)
40# else
41# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, "<?>", #a)
42# endif
43/* _BLI_ASSERT_ABORT */
44# ifdef WITH_ASSERT_ABORT
45# define _BLI_ASSERT_ABORT _BLI_assert_abort
46# else
47# define _BLI_ASSERT_ABORT() (void)0
48# endif
49/* BLI_assert */
50# define BLI_assert(a) \
51 (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
52 _BLI_ASSERT_PRINT_POS(a), \
53 _BLI_ASSERT_ABORT(), \
54 NULL)) : \
55 NULL)
57# define BLI_assert_msg(a, msg) \
58 (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
59 _BLI_ASSERT_PRINT_POS(a), \
60 _BLI_assert_print_extra(msg), \
61 _BLI_ASSERT_ABORT(), \
62 NULL)) : \
63 NULL)
64#else
65# define BLI_assert(a) ((void)0)
66# define BLI_assert_msg(a, msg) ((void)0)
67#endif
68
69#if defined(__cplusplus)
70/* C++11 */
71# define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
72#elif defined(_MSC_VER)
73/* Visual Studio */
74# if !defined(__clang__)
75# define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
76# else
77# define BLI_STATIC_ASSERT(a, msg) _STATIC_ASSERT(a);
78# endif
79#elif defined(__COVERITY__)
80/* Workaround error with COVERITY. */
81# define BLI_STATIC_ASSERT(a, msg)
82#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
83/* C11 */
84# define BLI_STATIC_ASSERT(a, msg) _Static_assert(a, msg);
85#else
86/* Old unsupported compiler */
87# define BLI_STATIC_ASSERT(a, msg)
88#endif
89
90#define BLI_STATIC_ASSERT_ALIGN(st, align) \
91 BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")
92
97#define BLI_assert_unreachable() \
98 { \
99 _BLI_assert_unreachable_print(__FILE__, __LINE__, __func__); \
100 BLI_assert_msg(0, "This line of code is marked to be unreachable."); \
101 } \
102 ((void)0)
103
104#ifdef __cplusplus
105}
106#endif
void _BLI_assert_print_backtrace(void)
Definition BLI_assert.c:36
void _BLI_assert_print_extra(const char *str)
Definition BLI_assert.c:25
void _BLI_assert_abort(void)
Definition BLI_assert.c:43
void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id)
Definition BLI_assert.c:20
void _BLI_assert_unreachable_print(const char *file, int line, const char *function)
Definition BLI_assert.c:30
#define str(s)