From 21462410eef2ad8c5b31aa5796a8319b54872c6c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 25 Sep 2014 13:05:42 +1000 Subject: [PATCH] Move typecheck macros into own header --- .../blender/blenlib/BLI_compiler_typecheck.h | 74 +++++++++++++++++++ source/blender/blenlib/BLI_utildefines.h | 49 ++---------- source/blender/blenlib/CMakeLists.txt | 1 + 3 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 source/blender/blenlib/BLI_compiler_typecheck.h diff --git a/source/blender/blenlib/BLI_compiler_typecheck.h b/source/blender/blenlib/BLI_compiler_typecheck.h new file mode 100644 index 00000000000..93dcc6ee866 --- /dev/null +++ b/source/blender/blenlib/BLI_compiler_typecheck.h @@ -0,0 +1,74 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __BLI_COMPILER_TYPECHECK_H__ +#define __BLI_COMPILER_TYPECHECK_H__ + +/** \file BLI_compiler_typecheck.h + * \ingroup bli + * + * Type checking macros (often used to ensure valid use of macro args). + * These depend on compiler extensions and c11 in some cases. + */ + +/* Causes warning: + * incompatible types when assigning to type 'Foo' from type 'Bar' + * ... the compiler optimizes away the temp var */ +#ifdef __GNUC__ +#define CHECK_TYPE(var, type) { \ + typeof(var) *__tmp; \ + __tmp = (type *)NULL; \ + (void)__tmp; \ +} (void)0 + +#define CHECK_TYPE_PAIR(var_a, var_b) { \ + typeof(var_a) *__tmp; \ + __tmp = (typeof(var_b) *)NULL; \ + (void)__tmp; \ +} (void)0 + +#define CHECK_TYPE_PAIR_INLINE(var_a, var_b) ((void)({ \ + typeof(var_a) *__tmp; \ + __tmp = (typeof(var_b) *)NULL; \ + (void)__tmp; \ +})) + +#else +# define CHECK_TYPE(var, type) +# define CHECK_TYPE_PAIR(var_a, var_b) +# define CHECK_TYPE_PAIR_INLINE(var_a, var_b) (void)0 +#endif + +/* can be used in simple macros */ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +# define CHECK_TYPE_INLINE(val, type) \ + (void)((void)(((type)0) != (0 ? (val) : ((type)0))), \ + _Generic((val), type: 0, const type: 0)) +#else +# define CHECK_TYPE_INLINE(val, type) \ + ((void)(((type)0) != (0 ? (val) : ((type)0)))) +#endif + +#define CHECK_TYPE_NONCONST(var) { \ + void *non_const = 0 ? (var) : NULL; \ + (void)non_const; \ +} (void)0 + +#endif /* __BLI_COMPILER_TYPECHECK_H__ */ diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 5724f0e6ad4..a829cc60106 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -54,8 +54,8 @@ #define _VA_NARGS_COUNT_MAX32(...) _VA_NARGS_EXPAND((__VA_ARGS__, \ 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, \ 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, \ - 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, \ - 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)) + 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, \ + 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)) #define _VA_NARGS_OVERLOAD_MACRO2(name, count) name##count #define _VA_NARGS_OVERLOAD_MACRO1(name, count) _VA_NARGS_OVERLOAD_MACRO2(name, count) #define _VA_NARGS_OVERLOAD_MACRO(name, count) _VA_NARGS_OVERLOAD_MACRO1(name, count) @@ -71,6 +71,9 @@ # define MAX2(x, y) (_TYPECHECK(x, y), (((x) > (y) ? (x) : (y)))) #endif +/* include after _VA_NARGS macro */ +#include "BLI_compiler_typecheck.h" + /* min/max */ #if defined(__GNUC__) || defined(__clang__) @@ -155,48 +158,6 @@ /* some math and copy defines */ -/* Causes warning: - * incompatible types when assigning to type 'Foo' from type 'Bar' - * ... the compiler optimizes away the temp var */ -#ifdef __GNUC__ -#define CHECK_TYPE(var, type) { \ - typeof(var) *__tmp; \ - __tmp = (type *)NULL; \ - (void)__tmp; \ -} (void)0 - -#define CHECK_TYPE_PAIR(var_a, var_b) { \ - typeof(var_a) *__tmp; \ - __tmp = (typeof(var_b) *)NULL; \ - (void)__tmp; \ -} (void)0 - -#define CHECK_TYPE_PAIR_INLINE(var_a, var_b) ((void)({ \ - typeof(var_a) *__tmp; \ - __tmp = (typeof(var_b) *)NULL; \ - (void)__tmp; \ -})) - -#else -# define CHECK_TYPE(var, type) -# define CHECK_TYPE_PAIR(var_a, var_b) -# define CHECK_TYPE_PAIR_INLINE(var_a, var_b) (void)0 -#endif - -/* can be used in simple macros */ -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) -# define CHECK_TYPE_INLINE(val, type) \ - (void)((void)(((type)0) != (0 ? (val) : ((type)0))), \ - _Generic((val), type: 0, const type: 0)) -#else -# define CHECK_TYPE_INLINE(val, type) \ - ((void)(((type)0) != (0 ? (val) : ((type)0)))) -#endif - -#define CHECK_TYPE_NONCONST(var) { \ - void *non_const = 0 ? (var) : NULL; \ - (void)non_const; \ -} (void)0 #define SWAP(type, a, b) { \ type sw_ap; \ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index dd02bcf22a9..9efa20da13e 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -118,6 +118,7 @@ set(SRC BLI_callbacks.h BLI_compiler_attrs.h BLI_compiler_compat.h + BLI_compiler_typecheck.h BLI_convexhull2d.h BLI_dial.h BLI_dlrbTree.h