Fix ASSERT macros to not over-expand

The code reuse in the definitions of the ASSERT and VERIFY macros result
in expansion of their arguments before they are stringified, which
produces ugly and undesirable output.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Paul Dagnelie <pcd@delphix.com>
Closes #7884
This commit is contained in:
Paul Dagnelie 2018-10-03 20:16:45 -07:00 committed by Brian Behlendorf
parent 95542372e6
commit 6e8b268875
2 changed files with 103 additions and 33 deletions

View File

@ -63,22 +63,59 @@ void spl_dumpstack(void);
spl_panic(__FILE__, __FUNCTION__, __LINE__, \
"%s", "VERIFY(" #cond ") failed\n"))
#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) do { \
TYPE _verify3_left = (TYPE)(LEFT); \
TYPE _verify3_right = (TYPE)(RIGHT); \
#define VERIFY3B(LEFT, OP, RIGHT) do { \
boolean_t _verify3_left = (boolean_t)(LEFT); \
boolean_t _verify3_right = (boolean_t)(RIGHT); \
if (!(_verify3_left OP _verify3_right)) \
spl_panic(__FILE__, __FUNCTION__, __LINE__, \
"VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
"failed (" FMT " " #OP " " FMT ")\n", \
CAST (_verify3_left), CAST (_verify3_right)); \
"VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
"failed (%d " #OP " %d)\n", \
(boolean_t) (_verify3_left), \
(boolean_t) (_verify3_right)); \
} while (0)
#define VERIFY3B(x,y,z) VERIFY3_IMPL(x, y, z, boolean_t, "%d", (boolean_t))
#define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
#define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
(unsigned long long))
#define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
#define VERIFY0(x) VERIFY3_IMPL(0, ==, x, int64_t, "%lld", (long long))
#define VERIFY3S(LEFT, OP, RIGHT) do { \
int64_t _verify3_left = (int64_t)(LEFT); \
int64_t _verify3_right = (int64_t)(RIGHT); \
if (!(_verify3_left OP _verify3_right)) \
spl_panic(__FILE__, __FUNCTION__, __LINE__, \
"VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
"failed (%lld " #OP " %lld)\n", \
(long long) (_verify3_left), \
(long long) (_verify3_right)); \
} while (0)
#define VERIFY3U(LEFT, OP, RIGHT) do { \
uint64_t _verify3_left = (uint64_t)(LEFT); \
uint64_t _verify3_right = (uint64_t)(RIGHT); \
if (!(_verify3_left OP _verify3_right)) \
spl_panic(__FILE__, __FUNCTION__, __LINE__, \
"VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
"failed (%llu " #OP " %llu)\n", \
(unsigned long long) (_verify3_left), \
(unsigned long long) (_verify3_right)); \
} while (0)
#define VERIFY3P(LEFT, OP, RIGHT) do { \
uintptr_t _verify3_left = (uintptr_t)(LEFT); \
uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
if (!(_verify3_left OP _verify3_right)) \
spl_panic(__FILE__, __FUNCTION__, __LINE__, \
"VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
"failed (%p " #OP " %p)\n", \
(void *) (_verify3_left), \
(void *) (_verify3_right)); \
} while (0)
#define VERIFY0(RIGHT) do { \
int64_t _verify3_left = (int64_t)(0); \
int64_t _verify3_right = (int64_t)(RIGHT); \
if (!(_verify3_left == _verify3_right)) \
spl_panic(__FILE__, __FUNCTION__, __LINE__, \
"VERIFY3(0 == " #RIGHT ") " \
"failed (0 == %lld)\n", \
(long long) (_verify3_right)); \
} while (0)
#define CTASSERT_GLOBAL(x) _CTASSERT(x, __LINE__)
#define CTASSERT(x) { _CTASSERT(x, __LINE__); }
@ -107,13 +144,13 @@ void spl_dumpstack(void);
*/
#else
#define ASSERT(cond) VERIFY(cond)
#define ASSERT3B VERIFY3B
#define ASSERT3S VERIFY3S
#define ASSERT3U VERIFY3U
#define ASSERT3P VERIFY3P
#define ASSERT0 VERIFY0
#define ASSERT VERIFY
#define ASSERTV(x) x
#define ASSERT3B(x,y,z) VERIFY3B(x, y, z)
#define ASSERT3S(x,y,z) VERIFY3S(x, y, z)
#define ASSERT3U(x,y,z) VERIFY3U(x, y, z)
#define ASSERT3P(x,y,z) VERIFY3P(x, y, z)
#define ASSERT0(x) VERIFY0(x)
#define IMPLY(A, B) \
((void)(((!(A)) || (B)) || \
spl_panic(__FILE__, __FUNCTION__, __LINE__, \

View File

@ -66,21 +66,54 @@ libspl_assertf(const char *file, const char *func, int line, char *format, ...)
(void) ((!(cond)) && \
libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) \
#define VERIFY3B(LEFT, OP, RIGHT) \
do { \
const TYPE __left = (TYPE)(LEFT); \
const TYPE __right = (TYPE)(RIGHT); \
const boolean_t __left = (boolean_t)(LEFT); \
const boolean_t __right = (boolean_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
"%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
} while (0)
#define VERIFY3B(x, y, z) VERIFY3_IMPL(x, y, z, boolean_t)
#define VERIFY3S(x, y, z) VERIFY3_IMPL(x, y, z, int64_t)
#define VERIFY3U(x, y, z) VERIFY3_IMPL(x, y, z, uint64_t)
#define VERIFY3P(x, y, z) VERIFY3_IMPL(x, y, z, uintptr_t)
#define VERIFY0(x) VERIFY3_IMPL(x, ==, 0, uint64_t)
#define VERIFY3S(LEFT, OP, RIGHT) \
do { \
const int64_t __left = (int64_t)(LEFT); \
const int64_t __right = (int64_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
"%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
} while (0)
#define VERIFY3U(LEFT, OP, RIGHT) \
do { \
const uint64_t __left = (uint64_t)(LEFT); \
const uint64_t __right = (uint64_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
"%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
} while (0)
#define VERIFY3P(LEFT, OP, RIGHT) \
do { \
const uintptr_t __left = (uintptr_t)(LEFT); \
const uintptr_t __right = (uintptr_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
"%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
} while (0)
#define VERIFY0(LEFT) \
do { \
const uint64_t __left = (uint64_t)(LEFT); \
if (!(__left == 0)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
"%s == 0 (0x%llx == 0)", #LEFT, \
(u_longlong_t)__left); \
} while (0)
#ifdef assert
#undef assert
@ -106,13 +139,13 @@ do { \
#define IMPLY(A, B) ((void)0)
#define EQUIV(A, B) ((void)0)
#else
#define ASSERT3B(x, y, z) VERIFY3B(x, y, z)
#define ASSERT3S(x, y, z) VERIFY3S(x, y, z)
#define ASSERT3U(x, y, z) VERIFY3U(x, y, z)
#define ASSERT3P(x, y, z) VERIFY3P(x, y, z)
#define ASSERT0(x) VERIFY0(x)
#define ASSERT(x) VERIFY(x)
#define assert(x) VERIFY(x)
#define ASSERT3B VERIFY3B
#define ASSERT3S VERIFY3S
#define ASSERT3U VERIFY3U
#define ASSERT3P VERIFY3P
#define ASSERT0 VERIFY0
#define ASSERT VERIFY
#define assert VERIFY
#define ASSERTV(x) x
#define IMPLY(A, B) \
((void)(((!(A)) || (B)) || \