Blender V5.0
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
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);
23void _BLI_assert_abort(void);
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) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #a)
34# elif defined(_MSC_VER)
35# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #a)
36# else
37# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, "<?>", #a)
38# endif
39/* _BLI_ASSERT_ABORT */
40# ifdef WITH_ASSERT_ABORT
41# define _BLI_ASSERT_ABORT _BLI_assert_abort
42# else
43# define _BLI_ASSERT_ABORT() (void)0
44# endif
45/* BLI_assert */
46# define BLI_assert(a) \
47 (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
48 _BLI_ASSERT_PRINT_POS(a), \
49 _BLI_ASSERT_ABORT(), \
50 NULL)) : \
51 NULL)
52
53# define BLI_assert_msg(a, msg) \
54 (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
55 _BLI_ASSERT_PRINT_POS(a), \
56 _BLI_assert_print_extra(msg), \
57 _BLI_ASSERT_ABORT(), \
58 NULL)) : \
59 NULL)
60#else
61# define BLI_assert(a) ((void)0)
62# define BLI_assert_msg(a, msg) ((void)0)
63#endif
64
65#if defined(__cplusplus)
66/* C++11 */
67# define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
68#elif defined(_MSC_VER)
69/* Visual Studio */
70# if !defined(__clang__)
71# define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
72# else
73# define BLI_STATIC_ASSERT(a, msg) _STATIC_ASSERT(a);
74# endif
75#elif defined(__COVERITY__)
76/* Workaround error with COVERITY. */
77# define BLI_STATIC_ASSERT(a, msg)
78#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
79/* C11 */
80# define BLI_STATIC_ASSERT(a, msg) _Static_assert(a, msg);
81#else
82/* Old unsupported compiler */
83# define BLI_STATIC_ASSERT(a, msg)
84#endif
85
86#define BLI_STATIC_ASSERT_ALIGN(st, align) \
87 BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")
88
93#define BLI_assert_unreachable() \
94 { \
95 _BLI_assert_unreachable_print(__FILE__, __LINE__, __func__); \
96 BLI_assert_msg(0, "This line of code is marked to be unreachable."); \
97 } \
98 ((void)0)
void _BLI_assert_print_backtrace(void)
Definition BLI_assert.cc:36
void _BLI_assert_print_extra(const char *str)
Definition BLI_assert.cc:25
void _BLI_assert_abort(void)
Definition BLI_assert.cc:43
void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id)
Definition BLI_assert.cc:20
void _BLI_assert_unreachable_print(const char *file, int line, const char *function)
Definition BLI_assert.cc:30
#define str(s)