From 947dd4c5d3b83f68bec18cbb633654163db8a62b Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 6 Aug 2019 03:08:36 +0000 Subject: [PATCH] added static assertion macros --- lib/hcl-cmn.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/hcl-cmn.h b/lib/hcl-cmn.h index 0ceae09..2e06b25 100644 --- a/lib/hcl-cmn.h +++ b/lib/hcl-cmn.h @@ -799,6 +799,12 @@ typedef struct hcl_t hcl_t; # undef HCL_HAVE_INLINE #endif +#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)) +# define HCL_UNUSED __attribute__((__unused__)) +#else +# define HCL_UNUSED +#endif + /** * The HCL_TYPE_IS_SIGNED() macro determines if a type is signed. * \code @@ -1011,5 +1017,20 @@ typedef struct hcl_t hcl_t; # define HCL_UNLIKELY(x) (x) #endif +/* ========================================================================= + * STATIC ASSERTION + * =========================================================================*/ +#define HCL_STATIC_JOIN_INNER(x, y) x ## y +#define HCL_STATIC_JOIN(x, y) HCL_STATIC_JOIN_INNER(x, y) + +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +# define HCL_STATIC_ASSERT(expr) _Static_assert (expr, "invalid assertion") +#elif defined(__cplusplus) && (__cplusplus >= 201103L) +# define HCL_STATIC_ASSERT(expr) static_assert (expr, "invalid assertion") +#else +# define HCL_STATIC_ASSERT(expr) typedef char HCL_STATIC_JOIN(HCL_STATIC_ASSERT_T_, __LINE__)[(expr)? 1: -1] HCL_UNUSED +#endif + +#define HCL_STATIC_ASSERT_EXPR(expr) ((void)HCL_SIZEOF(char[(expr)? 1: -1])) #endif