Blender  V2.93
BLI_assert.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /* Utility functions. */
33 void _BLI_assert_print_pos(const char *file, const int line, const char *function, const char *id);
35 void _BLI_assert_abort(void);
36 void _BLI_assert_unreachable_print(const char *file, const int line, const char *function);
37 
38 #ifdef _MSC_VER
39 # include <crtdbg.h> /* for _STATIC_ASSERT */
40 #endif
41 
42 #ifndef NDEBUG
43 /* _BLI_ASSERT_PRINT_POS */
44 # if defined(__GNUC__)
45 # define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, # a)
46 # elif defined(_MSC_VER)
47 # define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, # a)
48 # else
49 # define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, "<?>", # a)
50 # endif
51 /* _BLI_ASSERT_ABORT */
52 # ifdef WITH_ASSERT_ABORT
53 # define _BLI_ASSERT_ABORT _BLI_assert_abort
54 # else
55 # define _BLI_ASSERT_ABORT() (void)0
56 # endif
57 /* BLI_assert */
58 # define BLI_assert(a) \
59  (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
60  _BLI_ASSERT_PRINT_POS(a), \
61  _BLI_ASSERT_ABORT(), \
62  NULL)) : \
63  NULL)
64 #else
65 # define BLI_assert(a) ((void)0)
66 #endif
67 
68 #if defined(__cplusplus)
69 /* C++11 */
70 # define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
71 #elif defined(_MSC_VER)
72 /* Visual Studio */
73 # if (_MSC_VER > 1910) && !defined(__clang__)
74 # define BLI_STATIC_ASSERT(a, msg) static_assert(a, msg);
75 # else
76 # define BLI_STATIC_ASSERT(a, msg) _STATIC_ASSERT(a);
77 # endif
78 #elif defined(__COVERITY__)
79 /* Workaround error with coverity */
80 # define BLI_STATIC_ASSERT(a, msg)
81 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
82 /* C11 */
83 # define BLI_STATIC_ASSERT(a, msg) _Static_assert(a, msg);
84 #else
85 /* Old unsupported compiler */
86 # define BLI_STATIC_ASSERT(a, msg)
87 #endif
88 
89 #define BLI_STATIC_ASSERT_ALIGN(st, align) \
90  BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")
91 
96 #define BLI_assert_unreachable() \
97  { \
98  _BLI_assert_unreachable_print(__FILE__, __LINE__, __func__); \
99  BLI_assert(!"This line of code is marked to be unreachable."); \
100  } \
101  ((void)0)
102 
103 #ifdef __cplusplus
104 }
105 #endif
void _BLI_assert_print_backtrace(void)
Definition: BLI_assert.c:40
void _BLI_assert_unreachable_print(const char *file, const int line, const char *function)
Definition: BLI_assert.c:34
void _BLI_assert_print_pos(const char *file, const int line, const char *function, const char *id)
Definition: BLI_assert.c:29
void _BLI_assert_abort(void)
Definition: BLI_assert.c:54
FILE * file