From 4fee899c17b6d5821af2f58b8c63e6aa39a397d5 Mon Sep 17 00:00:00 2001 From: Amir Cohen Date: Mon, 14 Aug 2017 16:07:25 +0300 Subject: [PATCH] Implement a STATIC_ASSERT macro Implement STATIC_ASSERT macro to catch errors at compile time. --- api/inc/uvisor_exports.h | 18 +++++++++++++----- tools/uvisor-tests.txt | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/api/inc/uvisor_exports.h b/api/inc/uvisor_exports.h index 7f933eef..996db28f 100644 --- a/api/inc/uvisor_exports.h +++ b/api/inc/uvisor_exports.h @@ -77,11 +77,19 @@ /** Static Assertion Macro * * This macro works from both inside and outside function scope. - * - * FIXME This is currently not implemented. This issue is tracked at - * https://github.com/ARMmbed/uvisor/issues/288 - */ -#define UVISOR_STATIC_ASSERT(cond, msg) + * The implementations differ due to compilation differences, C++ static_assert + * is known from C++11 (__cplusplus > 199711L) while mbed-os compiles with c++98, + * and C _Static_assert is known from GCC version 4.6.0. */ +#define GCC_VERSION (__GNUC__ * 10000 \ + + __GNUC_MINOR__ * 100 \ + + __GNUC_PATCHLEVEL__) +#if (__cplusplus > 199711L) +#define UVISOR_STATIC_ASSERT(cond, msg) static_assert(cond, #msg) +#elif (!__cplusplus && GCC_VERSION > 40600) +#define UVISOR_STATIC_ASSERT(cond, msg) _Static_assert(cond, #msg) +#else +#define UVISOR_STATIC_ASSERT(cond, msg) typedef char STATIC_ASSERT_##msg[(cond)?1:-1] +#endif /* convert macro argument to string */ /* note: this needs one level of indirection, accomplished with the helper macro diff --git a/tools/uvisor-tests.txt b/tools/uvisor-tests.txt index ba93aab5..b6c3fc3e 100644 --- a/tools/uvisor-tests.txt +++ b/tools/uvisor-tests.txt @@ -1 +1 @@ -0ac4c38729d2602bff96d68983a7790143210912 \ No newline at end of file +0fc7c7f55507421463ba46a9692008e87a1ac5f2