diff options
author | Campbell Barton <ideasman42@gmail.com> | 2020-08-06 13:17:44 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2020-08-06 15:49:28 +0300 |
commit | ba20da7214e0a71e843b0a6a8b11e0173599a0f0 (patch) | |
tree | 5fb2cc754fb0c2e2797aa42c1fa9a1ebffe5cb5d /source/blender/blenlib | |
parent | 73a43c9d8acfdb81aa324012bb30cae10c52f186 (diff) |
Cleanup: avoid debug-only includes for BLI_assert.h
Having includes in debug builds makes it possible to accidentally
break release builds.
Avoid this by moving calls to other modules out of BLI_assert.h
into BLI_assert.c
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_alloca.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_assert.h | 43 | ||||
-rw-r--r-- | source/blender/blenlib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_assert.c | 51 |
4 files changed, 63 insertions, 34 deletions
diff --git a/source/blender/blenlib/BLI_alloca.h b/source/blender/blenlib/BLI_alloca.h index 5297296b7ef..258e788ac12 100644 --- a/source/blender/blenlib/BLI_alloca.h +++ b/source/blender/blenlib/BLI_alloca.h @@ -25,6 +25,8 @@ /* BLI_array_alloca / alloca */ +#include <stdlib.h> + #if defined(__GNUC__) || defined(__clang__) # if defined(__cplusplus) && (__cplusplus > 199711L) # define BLI_array_alloca(arr, realsize) (decltype(arr)) alloca(sizeof(*arr) * (realsize)) diff --git a/source/blender/blenlib/BLI_assert.h b/source/blender/blenlib/BLI_assert.h index 603be115b35..7978f04dd9d 100644 --- a/source/blender/blenlib/BLI_assert.h +++ b/source/blender/blenlib/BLI_assert.h @@ -30,58 +30,33 @@ extern "C" { #endif -#ifndef NDEBUG /* for BLI_assert */ -# include <stdio.h> -#endif +/* Utility functions. */ +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); #ifdef _MSC_VER # include <crtdbg.h> /* for _STATIC_ASSERT */ #endif -/* BLI_assert(), default only to print - * for aborting need to define WITH_ASSERT_ABORT - */ -/* For 'abort' only. */ -#include <stdlib.h> - #ifndef NDEBUG -# include "BLI_system.h" /* _BLI_ASSERT_PRINT_POS */ # if defined(__GNUC__) -# define _BLI_ASSERT_PRINT_POS(a) \ - fprintf(stderr, \ - "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", \ - __FILE__, \ - __LINE__, \ - __func__, \ - #a) +# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, # a) # elif defined(_MSC_VER) -# define _BLI_ASSERT_PRINT_POS(a) \ - fprintf(stderr, \ - "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", \ - __FILE__, \ - __LINE__, \ - __FUNCTION__, \ - #a) +# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, # a) # else -# define _BLI_ASSERT_PRINT_POS(a) \ - fprintf(stderr, "BLI_assert failed: %s:%d, at \'%s\'\n", __FILE__, __LINE__, #a) +# define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, "<?>", # a) # endif /* _BLI_ASSERT_ABORT */ # ifdef WITH_ASSERT_ABORT -# ifdef __GNUC__ -/* Cast to remove 'noreturn' attribute since this suppresses missing return statements, - * allowing changes to debug builds to accidentally to break release builds. */ -# define _BLI_ASSERT_ABORT ((void (*)(void))(*(((void **)abort)))) -# else -# define _BLI_ASSERT_ABORT abort -# endif +# define _BLI_ASSERT_ABORT _BLI_assert_abort # else # define _BLI_ASSERT_ABORT() (void)0 # endif /* BLI_assert */ # define BLI_assert(a) \ - (void)((!(a)) ? ((BLI_system_backtrace(stderr), \ + (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \ _BLI_ASSERT_PRINT_POS(a), \ _BLI_ASSERT_ABORT(), \ NULL)) : \ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index d7b279c9bbf..9703c78e19c 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -37,6 +37,7 @@ set(INC_SYS set(SRC intern/BLI_args.c intern/BLI_array.c + intern/BLI_assert.c intern/BLI_dial_2d.c intern/BLI_dynstr.c intern/BLI_filelist.c diff --git a/source/blender/blenlib/intern/BLI_assert.c b/source/blender/blenlib/intern/BLI_assert.c new file mode 100644 index 00000000000..dc22d035459 --- /dev/null +++ b/source/blender/blenlib/intern/BLI_assert.c @@ -0,0 +1,51 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** \file + * \ingroup bli + * + * Helper functions for BLI_assert.h header. + */ + +#include "BLI_assert.h" /* Own include. */ +#include "BLI_system.h" + +#include <stdio.h> +#include <stdlib.h> + +void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id) +{ + fprintf(stderr, "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", file, line, function, id); +} + +void _BLI_assert_print_backtrace(void) +{ +#ifndef NDEBUG + BLI_system_backtrace(stderr); +#endif +} + +/** + * Wrap to remove 'noreturn' attribute since this suppresses missing return statements, + * allowing changes to debug builds to accidentally to break release builds. + * + * For example `BLI_assert(0);` at the end of a function that returns a value, + * will hide that it's missing a return. + */ +void _BLI_assert_abort(void) +{ + abort(); +} |