From 957db32f46ca468043e1699817273a3e228c1ffb Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 18 Apr 2025 07:14:27 -0600 Subject: libmisc: LM_ARRAY_LEN: Compiler-error if not array --- libmisc/include/libmisc/assert.h | 11 ++++++----- libmisc/include/libmisc/macro.h | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'libmisc/include') diff --git a/libmisc/include/libmisc/assert.h b/libmisc/include/libmisc/assert.h index 8cf0735..0563a58 100644 --- a/libmisc/include/libmisc/assert.h +++ b/libmisc/include/libmisc/assert.h @@ -1,6 +1,6 @@ /* libmisc/assert.h - More assertions * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -16,9 +16,10 @@ const char *msg); #endif -#define assert_msg(expr, msg) __assert_msg(expr, #expr, msg) /* libmisc */ -#define assert(expr) __assert_msg(expr, #expr, 0) /* C89, POSIX-2001 */ -#define assert_notreached(msg) do { __assert_msg(0, "notreached", msg); __builtin_unreachable(); } while (0) /* libmisc */ -#define static_assert _Static_assert /* C11 */ +#define assert_msg(expr, msg) __assert_msg(expr, #expr, msg) /* libmisc */ +#define assert(expr) __assert_msg(expr, #expr, 0) /* C89, POSIX-2001 */ +#define assert_notreached(msg) do { __assert_msg(0, "notreached", msg); __builtin_unreachable(); } while (0) /* libmisc */ +#define static_assert _Static_assert /* C11 */ +#define static_assert_as_expr(...) (sizeof(struct {static_assert(__VA_ARGS__);})) /* libmisc */ #endif /* _LIBMISC_ASSERT_H_ */ diff --git a/libmisc/include/libmisc/macro.h b/libmisc/include/libmisc/macro.h index 6cb15fb..a95ac82 100644 --- a/libmisc/include/libmisc/macro.h +++ b/libmisc/include/libmisc/macro.h @@ -7,6 +7,8 @@ #ifndef _LIBMISC_MACRO_H_ #define _LIBMISC_MACRO_H_ +#include + /* for function definitions */ #define LM_UNUSED(argname) @@ -16,7 +18,9 @@ /* types */ -#define LM_ARRAY_LEN(ary) (sizeof(ary)/sizeof((ary)[0])) +/* If it's a pointer instead of an array, then typeof(&ptr[0]) == typeof(ptr) */ +#define _LM_IS_ARRAY(ary) (!__builtin_types_compatible_p(typeof(&(ary)[0]), typeof(ary))) +#define LM_ARRAY_LEN(ary) ( (sizeof(ary)/sizeof((ary)[0])) + static_assert_as_expr(_LM_IS_ARRAY(ary)) ) #define LM_CAST_FIELD_TO_STRUCT(STRUCT_TYP, FIELD_NAME, PTR_TO_FIELD) ({ \ /* The _fptr assignment is to get the compiler to do type checking. */ \ -- cgit v1.2.3-2-g168b From 0c70b9a94982f4b75d3b450920e82e2390a153c1 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 18 Apr 2025 07:34:59 -0600 Subject: Tidy `do { ... } while (0)` macros --- libmisc/include/libmisc/assert.h | 6 +++++- libmisc/include/libmisc/linkedlist.h | 10 +++++----- libmisc/include/libmisc/map.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) (limited to 'libmisc/include') diff --git a/libmisc/include/libmisc/assert.h b/libmisc/include/libmisc/assert.h index 0563a58..ccdb288 100644 --- a/libmisc/include/libmisc/assert.h +++ b/libmisc/include/libmisc/assert.h @@ -10,7 +10,11 @@ #ifdef NDEBUG # define __assert_msg(expr, expr_str, msg) ((void)0) #else -# define __assert_msg(expr, expr_str, msg) do { if (!(expr)) __assert_msg_fail(expr_str, __FILE__, __LINE__, __func__, msg); } while (0) +# define __assert_msg(expr, expr_str, msg) \ + do { \ + if (!(expr)) \ + __assert_msg_fail(expr_str, __FILE__, __LINE__, __func__, msg); \ + } while (0) [[noreturn]] void __assert_msg_fail(const char *expr, const char *file, unsigned int line, const char *func, const char *msg); diff --git a/libmisc/include/libmisc/linkedlist.h b/libmisc/include/libmisc/linkedlist.h index e8c65db..b6ff688 100644 --- a/libmisc/include/libmisc/linkedlist.h +++ b/libmisc/include/libmisc/linkedlist.h @@ -56,14 +56,14 @@ void _dlist_pop_from_front(struct _dlist_root *root); typeof(*_rootp->front) *_nodep = NODE; \ _slist_push_to_rear((struct _slist_root *)_rootp, \ (struct _slist_node *)_nodep); \ -} while(0) +} while (0) #define slist_pop_from_front(ROOT) { \ /* This temporary variables are to get the compiler to check \ * the type. */ \ typeof(*(ROOT)->_slist_root_typ[0]) *_rootp = ROOT; \ _slist_pop_from_front((struct _slist_root *)_rootp); \ -} while(0) +} while (0) /* doubly linked list (non-intrusive) *****************************************/ @@ -87,7 +87,7 @@ void _dlist_pop_from_front(struct _dlist_root *root); typeof(*_rootp->front) *_nodep = NODE; \ _dlist_push_to_rear((struct _dlist_root *)_rootp, \ (struct _dlist_node *)_nodep); \ -} while(0) +} while (0) #define dlist_remove(ROOT, NODE) { \ /* These temporary variables are to get the compiler to check \ @@ -96,13 +96,13 @@ void _dlist_pop_from_front(struct _dlist_root *root); typeof(*_rootp->front) *_nodep = NODE; \ _dlist_remove((struct _dlist_root *)_rootp, \ (struct _dlist_node *)_nodep); \ -} while(0) +} while (0) #define dlist_pop_from_front(ROOT) { \ /* This temporary variables are to get the compiler to check \ * the type. */ \ typeof(*(ROOT)->_dlist_root_typ[0]) *_rootp = ROOT; \ _dlist_pop_from_front((struct _dlist_root *)_rootp); \ -} while(0) +} while (0) #endif /* _LIBMISC_LINKEDLIST_H_ */ diff --git a/libmisc/include/libmisc/map.h b/libmisc/include/libmisc/map.h index 41ac069..6622595 100644 --- a/libmisc/include/libmisc/map.h +++ b/libmisc/include/libmisc/map.h @@ -52,7 +52,7 @@ struct _map { (M)->core.offsetof_k = offsetof(typeof((M)->kv_typ[0]), val.key); \ (M)->core.offsetof_v = offsetof(typeof((M)->kv_typ[0]), val.val); \ } \ -} while(0) +} while (0) /* Methods ********************************************************************/ -- cgit v1.2.3-2-g168b