summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2021-06-22 22:50:13 +0200
committerThomas Haller <thaller@redhat.com>2021-06-23 13:09:24 +0200
commit895b7dd95a3e083a984455c4c2059c867f0abd60 (patch)
tree495d1104f3fd44c1d7b968e120ac087183ab2356
parent1e29a7e66b28777692a27e9b9828188147842ec0 (diff)
glib-aux: rework NM_HASH_COMBINE_BOOLS() macro to avoid expression statement
clang 3.4.2-9.el7 dislikes expressions in the form int v; struct { typeof(({ v; })) _field; } x; error: statement expression not allowed at file scope typeof( ({ v; }) ) _field; ^ That is, if the argument to typeof() is an expression statement. But this is what nm_hash_update_val(&h, ..., NM_HASH_COMBINE_BOOLS(...)) expands to. Rework NM_HASH_COMBINE_BOOLS() to avoid the expression statement. We still have the static assertion for the size of the return type. We no longer have the _nm_hash_combine_bools_type typedef. It really wasn't needed, and the current variant is always safe without it. Fixes: 23adeed24463 ('glib-aux: use NM_VA_ARGS_FOREACH() to implement NM_HASH_COMBINE_BOOLS()')
-rw-r--r--src/libnm-core-impl/tests/test-general.c2
-rw-r--r--src/libnm-glib-aux/nm-hash-utils.h18
2 files changed, 8 insertions, 12 deletions
diff --git a/src/libnm-core-impl/tests/test-general.c b/src/libnm-core-impl/tests/test-general.c
index 51411b6feb..172cf15224 100644
--- a/src/libnm-core-impl/tests/test-general.c
+++ b/src/libnm-core-impl/tests/test-general.c
@@ -508,6 +508,8 @@ test_nm_hash(void)
g_assert(((typeof(x)) -1) > 0);
}
#endif
+
+ NM_STATIC_ASSERT_EXPR_VOID(NM_HASH_COMBINE_BOOLS(int, 1, 0, 1) == 5);
}
/*****************************************************************************/
diff --git a/src/libnm-glib-aux/nm-hash-utils.h b/src/libnm-glib-aux/nm-hash-utils.h
index 52e3430503..0a68bde685 100644
--- a/src/libnm-glib-aux/nm-hash-utils.h
+++ b/src/libnm-glib-aux/nm-hash-utils.h
@@ -122,18 +122,12 @@ nm_hash_update_bool(NMHashState *state, bool val)
nm_hash_update(state, &val, sizeof(val));
}
-#define _NM_HASH_COMBINE_BOOLS_OP(x, n) \
- ((x) ? ((_nm_hash_combine_bools_type) NM_BIT((n))) : ((_nm_hash_combine_bools_type) 0))
-
-#define NM_HASH_COMBINE_BOOLS(type, ...) \
- ({ \
- typedef type _nm_hash_combine_bools_type; \
- \
- G_STATIC_ASSERT(NM_NARG(__VA_ARGS__) <= 8 * sizeof(_nm_hash_combine_bools_type)); \
- \
- (_nm_hash_combine_bools_type)( \
- NM_VA_ARGS_FOREACH(, , |, _NM_HASH_COMBINE_BOOLS_OP, __VA_ARGS__)); \
- })
+#define _NM_HASH_COMBINE_BOOLS_OP(x, n) ((x) ? NM_BIT((n)) : 0u)
+
+#define NM_HASH_COMBINE_BOOLS(type, ...) \
+ ((type) (NM_STATIC_ASSERT_EXPR_1(NM_NARG(__VA_ARGS__) <= 8 * sizeof(type)) \
+ ? (NM_VA_ARGS_FOREACH(, , |, _NM_HASH_COMBINE_BOOLS_OP, __VA_ARGS__)) \
+ : 0))
#define nm_hash_update_bools(state, ...) \
nm_hash_update_val(state, NM_HASH_COMBINE_BOOLS(guint8, __VA_ARGS__))