libdrgn: factor is_array() macro out of array_size()

It'll be used for another macro in the next commit.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
This commit is contained in:
Omar Sandoval 2024-01-04 13:51:18 -08:00
parent 630d55aaaf
commit ea93f5743d
2 changed files with 7 additions and 5 deletions

View File

@ -25,11 +25,10 @@
*
* @hideinitializer
*/
#define array_size(arr) \
static_assert_expression( \
!types_compatible((arr), &(arr)[0]), "not an array", \
sizeof(arr) / sizeof((arr)[0]) \
)
#define array_size(arr) \
static_assert_expression(is_array(arr), \
"not an array", \
sizeof(arr) / sizeof((arr)[0]))
/**
* Iterate over every element in an array.

View File

@ -68,6 +68,9 @@
/** Return whether two types or expressions have compatible types. */
#define types_compatible(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
/** Return whether an expression is an array. */
#define is_array(x) (!types_compatible(x, &(x)[0]))
/**
* `static_assert(assert_expression, message)` as an expression that evaluates
* to `eval_expression`.