From 700410049c40d3414af6842bf9f87e7110f3e771 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Mon, 1 Apr 2019 15:29:19 +0300 Subject: [PATCH 1/3] MBED_STRUCT_STATIC_ASSERT - avoid alignment problem The `int : 0` bitfield this produced could force integer alignment onto the structure it was placed in, making a structure that should be 1 byte be 4 bytes. Change `int` to `bool` to minimise alignment impact - should be to nothing. Alignment/size problem was revealed in a `sizeof` check in an `Atomic` test. --- platform/mbed_assert.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/platform/mbed_assert.h b/platform/mbed_assert.h index a748f3acebf..a9bbc8c6965 100644 --- a/platform/mbed_assert.h +++ b/platform/mbed_assert.h @@ -24,6 +24,7 @@ #ifndef MBED_ASSERT_H #define MBED_ASSERT_H +#include #include "mbed_preprocessor.h" #include "mbed_toolchain.h" @@ -124,7 +125,7 @@ do { \ * }; * @endcode */ -#define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1 +#define MBED_STRUCT_STATIC_ASSERT(expr, msg) bool : (expr) ? 0 : -1 #endif From 56a043cbd1a24dfeb9f8a3edd4d91ba868104cc4 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 13 Jun 2019 15:11:03 +0300 Subject: [PATCH 2/3] MBED_STRUCT_STATIC_ASSERT: Use standard C++11/C11 If available, we can use standard static_assert. --- platform/mbed_assert.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/platform/mbed_assert.h b/platform/mbed_assert.h index a9bbc8c6965..7b2be2cfaac 100644 --- a/platform/mbed_assert.h +++ b/platform/mbed_assert.h @@ -24,7 +24,6 @@ #ifndef MBED_ASSERT_H #define MBED_ASSERT_H -#include #include "mbed_preprocessor.h" #include "mbed_toolchain.h" @@ -125,8 +124,14 @@ do { \ * }; * @endcode */ +#if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L) +#define MBED_STRUCT_STATIC_ASSERT(expr, msg) static_assert(expr, msg) +#elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L +#define MBED_STRUCT_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) +#else +#include #define MBED_STRUCT_STATIC_ASSERT(expr, msg) bool : (expr) ? 0 : -1 - +#endif #endif From 703af8df0684788380872778a6b6dcdb364944eb Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 13 Jun 2019 12:16:16 +0300 Subject: [PATCH 3/3] mbed_toolchain: Use C++11/C11 attributes Newer language standards have standard forms for `MBED_NORETURN` and `MBED_ALIGN` attributes. Use them when available. C++14 also adds `[[deprecated]]`, but as it needs to go in the middle of structure definitions as `class [[deprecated]] MyClass`, it's not a total drop-in-replacemend for `MBED_DEPRECATED`, so that is not attempted here. Using standard forms increases the chances that code analysis tools such Coverity will recognise them - particularly important for "no return". --- platform/mbed_toolchain.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/platform/mbed_toolchain.h b/platform/mbed_toolchain.h index b513c71fef5..0b51f77a69a 100644 --- a/platform/mbed_toolchain.h +++ b/platform/mbed_toolchain.h @@ -72,7 +72,11 @@ * @endcode */ #ifndef MBED_ALIGN -#if defined(__ICCARM__) +#if __cplusplus >= 201103 && !defined __CC_ARM +#define MBED_ALIGN(N) alignas(N) +#elif __STDC_VERSION__ >= 201112 && !defined __CC_ARM +#define MBED_ALIGN(N) _Alignas(N) +#elif defined(__ICCARM__) #define MBED_ALIGN(N) _Pragma(MBED_STRINGIFY(data_alignment=N)) #else #define MBED_ALIGN(N) __attribute__((aligned(N))) @@ -298,7 +302,11 @@ * @endcode */ #ifndef MBED_NORETURN -#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) +#if __cplusplus >= 201103 +#define MBED_NORETURN [[noreturn]] +#elif __STDC_VERSION__ >= 201112 +#define MBED_NORETURN _Noreturn +#elif defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) #define MBED_NORETURN __attribute__((noreturn)) #elif defined(__ICCARM__) #define MBED_NORETURN __noreturn