diff options
author | Zbigniew Chyla <zbigniew.chyla@nokia.com> | 2015-03-22 21:51:45 +0200 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2015-03-22 16:07:19 -0400 |
commit | fcb8bba28414daa6dd73809b1af31d571dcc9160 (patch) | |
tree | 0c5b7317eba1b97ba1c9febe757fccee2726b982 /compiler.h | |
parent | dfddcbc3b2814aaa15b606a9ce1972d15bd419fa (diff) | |
download | gpsd-fcb8bba28414daa6dd73809b1af31d571dcc9160.tar.gz |
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.
Diffstat (limited to 'compiler.h')
-rw-r--r-- | compiler.h | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -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 { \ |