fixeria has uploaded this change for review.

View Change

utils: use built-in static_assert() if available

Static assertion is part of the C language since C11. It produces
more readable [user supplied] error messages than:

error: size of array ‘dummy_foo_bar’ is negative

and is generally smarter than the current osmo_static_assert()
implementation as it would fail when non-constant expression is
given, whereas osmo_static_assert() would have no effect at all.

See also https://en.cppreference.com/w/c/language/_Static_assert.

Change-Id: I5ca34bc14c05e8c38c721d7df33feb1c6c41c76e
---
M include/osmocom/core/utils.h
1 file changed, 29 insertions(+), 1 deletion(-)

git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/96/31496/1
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index ee7cfa4..482babc 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
+#include <assert.h>

#include <osmocom/core/backtrace.h>
#include <osmocom/core/talloc.h>
@@ -79,7 +80,14 @@

char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));

-#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
+#ifdef static_assert
+/* static_assert() is available in C11 or later */
+#define osmo_static_assert(exp, _name) \
+ static_assert((exp), "(" #exp ")")
+#else
+#define osmo_static_assert(exp, name) \
+ typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
+#endif

void osmo_str2lower(char *out, const char *in)
OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead,"

To view, visit change 31496. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I5ca34bc14c05e8c38c721d7df33feb1c6c41c76e
Gerrit-Change-Number: 31496
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanitskiy@sysmocom.de>
Gerrit-MessageType: newchange