From fcb8bba28414daa6dd73809b1af31d571dcc9160 Mon Sep 17 00:00:00 2001 From: Zbigniew Chyla Date: Sun, 22 Mar 2015 21:51:45 +0200 Subject: Add compile-time check for arg type to NITEMS() If a pointer (not array) is passed to NITEMS() macro, gcc will emit compilation warning. The actual check is based on newly added COMPILE_CHECK_IS_ARRAY macro. The change doesn't affect generated binary code. --- compiler.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'compiler.h') diff --git a/compiler.h b/compiler.h index 8159d776..4fde42db 100644 --- a/compiler.h +++ b/compiler.h @@ -37,6 +37,27 @@ #define UNUSED #endif +/* + * Macro for compile-time checking if argument is an array. + * It expands to constant expression with int value 0. + */ +#if defined(__GNUC__) +#define COMPILE_CHECK_IS_ARRAY(arr) ( \ + 0 * (int) sizeof(({ \ + struct { \ + int unused_int; \ + typeof(arr) unused_arr; \ + } zero_init = {0}; \ + typeof(arr) arg_is_not_array UNUSED = { \ + zero_init.unused_arr[0], \ + }; \ + 1; \ + })) \ +) +#else +#define COMPILE_CHECK_IS_ARRAY(arr) 0 +#endif + /* Needed because 4.x versions of GCC are really annoying */ #define ignore_return(funcall) \ do { \ -- cgit v1.2.1