common/include: extract STATIC_ASSERT() macro
We already have two use cases for static assertions (and soon we'll have
yet another). Namely:
- STATIC_ASSERT_UNSIGNED_INT() in "checked-overflow.h". Here, we use our
own trick, based on a negative-sized array typedef that's named with
NBDKIT_UNIQUE_NAME.
- static_assert() in "test-array-size.c". This uses the C11 macro called
static_assert() from <assert.h>, which wraps the C11 _Static_assert().
This is not really great: our baseline is C99, not C11 (per commit
762f7c9e5166, "tests: Set minimum compiler to ISO C99.", 2021-04-08) --
which is why the same assertions are repeated in the code as normal
runtime assert() calls, in case static_assert() is not defined.
Factor out our own STATIC_ASSERT(), from STATIC_ASSERT_UNSIGNED_INT().
Put it to use in "test-array-size.c", replacing both the runtime assert()s
and the compile-time static_assert()s. Note that for the latter, in order
to remain consistent with STATIC_ASSERT_UNSIGNED_INT(), we no longer
provide the *complaint* that we want the compiler to emit upon assertion
failure, but an identifier that stands for the predicate that we *expect*.
When uncommenting the negative test case in "test-array-size.c", the
resultant wall of compiler diagnostics includes the following entry:
> test-array-size.c:83:39: error: size of array
> ‘_array_size_macro_is_applied_to_array13’ is negative
This patch will have to be ported to nbdkit. (IMO we can implement the
change in libnbd first -- the "common" subdir is meant to be common, so no
particular precedence should be assumed.)
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
(cherry picked from libnbd commit
c593baab3c9c8b17317daece0694ec8b5fc6fb46)
Message-Id: <
20230303075145.177338-7-lersek@redhat.com>
Acked-by: Richard W.M. Jones <rjones@redhat.com>
Acked-by: Eric Blake <eblake@redhat.com>