Skip to content

Commit 182c6a2

Browse files
committed
Added static assert macro
Added MBED_STATIC_ASSERT for compile-time assertions, results in compile-time error if condition is false The assertion acts as a declaration that can be placed at file scope, in a code block (except after a label), or as a member of a C++ class/struct/union. Unfortunately, there does not exist a backup construct for use in C class/struct/union contexts. An alternative macro, MBED_STRUCT_STATIC_ASSERT provides this ability to avoid disabling static assertions for the majority of mbed-supported C compilers.
1 parent d1a71eb commit 182c6a2

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
4+
#include "toolchain.h"
5+
#include "greentea-client/test_env.h"
6+
#include "unity.h"
7+
#include "utest.h"
8+
9+
using namespace utest::v1;
10+
11+
void no_test() {}
12+
13+
utest::v1::status_t test_setup(const size_t number_of_cases) {
14+
GREENTEA_SETUP(5, "default_auto");
15+
return verbose_test_setup_handler(number_of_cases);
16+
}
17+
18+
Case cases[] = {
19+
Case("Compilation test", no_test),
20+
};
21+
22+
Specification specification(test_setup, cases);
23+
24+
int main() {
25+
return !Harness::run(specification);
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "mbed_assert.h"
2+
#define THE_ANSWER 42
3+
4+
// Tests for static asserts in different contexts
5+
// multiple asserts are used to garuntee no conflicts occur in generated labels
6+
7+
// Test for static asserts in global context
8+
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
9+
"An int must be larger than char");
10+
MBED_STATIC_ASSERT(2 + 2 == 4,
11+
"Hopefully the universe is mathematically consistent");
12+
MBED_STATIC_ASSERT(THE_ANSWER == 42,
13+
"Said Deep Thought, with infinite majesty and calm");
14+
15+
struct test {
16+
int dummy;
17+
18+
// Test for static asserts in struct context
19+
MBED_STRUCT_STATIC_ASSERT(sizeof(int) >= sizeof(char),
20+
"An int must be larger than char");
21+
MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
22+
"Hopefully the universe is mathematically consistent");
23+
MBED_STRUCT_STATIC_ASSERT(THE_ANSWER == 42,
24+
"Said Deep Thought, with infinite majesty and calm");
25+
};
26+
27+
MBED_STATIC_ASSERT(sizeof(struct test) == sizeof(int),
28+
"Static assertions should not change the size of a struct");
29+
30+
void doit_c(void) {
31+
// Test for static asserts in function context
32+
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
33+
"An int must be larger than char");
34+
MBED_STATIC_ASSERT(2 + 2 == 4,
35+
"Hopefully the universe is mathematically consistent");
36+
MBED_STATIC_ASSERT(THE_ANSWER == 42,
37+
"Said Deep Thought, with infinite majesty and calm");
38+
}
39+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "mbed_assert.h"
2+
#define THE_ANSWER 42
3+
4+
// Tests for static asserts in different contexts
5+
// multiple asserts are used to garuntee no conflicts occur in generated labels
6+
7+
// Test for static asserts in global context
8+
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
9+
"An int must be larger than char");
10+
MBED_STATIC_ASSERT(2 + 2 == 4,
11+
"Hopefully the universe is mathematically consistent");
12+
MBED_STATIC_ASSERT(THE_ANSWER == 42,
13+
"Said Deep Thought, with infinite majesty and calm");
14+
15+
struct test {
16+
int dummy;
17+
18+
// Test for static asserts in struct context
19+
MBED_STRUCT_STATIC_ASSERT(sizeof(int) >= sizeof(char),
20+
"An int must be larger than char");
21+
MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
22+
"Hopefully the universe is mathematically consistent");
23+
MBED_STRUCT_STATIC_ASSERT(THE_ANSWER == 42,
24+
"Said Deep Thought, with infinite majesty and calm");
25+
26+
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
27+
"An int must be larger than char");
28+
MBED_STATIC_ASSERT(2 + 2 == 4,
29+
"Hopefully the universe is mathematically consistent");
30+
MBED_STATIC_ASSERT(THE_ANSWER == 42,
31+
"Said Deep Thought, with infinite majesty and calm");
32+
};
33+
34+
MBED_STATIC_ASSERT(sizeof(struct test) == sizeof(int),
35+
"Static assertions should not change the size of a struct");
36+
37+
void doit_c(void) {
38+
// Test for static asserts in function context
39+
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
40+
"An int must be larger than char");
41+
MBED_STATIC_ASSERT(2 + 2 == 4,
42+
"Hopefully the universe is mathematically consistent");
43+
MBED_STATIC_ASSERT(THE_ANSWER == 42,
44+
"Said Deep Thought, with infinite majesty and calm");
45+
}
46+

platform/mbed_assert.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,66 @@ do { \
4949
} while (0)
5050
#endif
5151

52+
53+
#define _MBED_CONCAT_(a, b) a##b
54+
#define _MBED_CONCAT(a, b) _MBED_CONCAT_(a, b)
55+
#define _MBED_ASSERTION _MBED_CONCAT(_MBED_ASSERTION_AT_, __LINE__)
56+
57+
/** MBED_STATIC_ASSERT
58+
* Declare compile-time assertions, results in compile-time error if condition is false
59+
*
60+
* The assertion acts as a declaration that can be placed at file scope, in a
61+
* code block (except after a label), or as a member of a C++ class/struct/union.
62+
*
63+
* @note
64+
* Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
65+
* - In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
66+
* - In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and
67+
* MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid
68+
* in C and C++ class/struct/union scope.
69+
*
70+
* @code
71+
* MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
72+
* "The mbed library must be at least version 120");
73+
*
74+
* int main() {
75+
* MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
76+
* "An int must be larger than a char");
77+
* }
78+
* @endcode
79+
*/
80+
#if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
81+
#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
82+
#elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
83+
#define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
84+
#elif defined(__cplusplus) && defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) \
85+
&& (__GNUC__*100 + __GNUC_MINOR__) > 403L
86+
#define MBED_STATIC_ASSERT(expr, msg) __extension__ static_assert(expr, msg)
87+
#elif !defined(__cplusplus) && defined(__GNUC__) && !defined(__CC_ARM) \
88+
&& (__GNUC__*100 + __GNUC_MINOR__) > 406L
89+
#define MBED_STATIC_ASSERT(expr, msg) __extension__ _Static_assert(expr, msg)
90+
#elif defined(__ICCARM__)
91+
#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
92+
#else
93+
#define MBED_STATIC_ASSERT(expr, msg) enum {_MBED_ASSERTION = sizeof(char[(expr) ? 1 : -1])}
94+
#endif
95+
96+
/** MBED_STRUCT_STATIC_ASSERT
97+
* Declare compile-time assertions, results in compile-time error if condition is false
98+
*
99+
* Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used
100+
* as a member of a C/C++ class/struct/union.
101+
*
102+
* @code
103+
* struct thing {
104+
* MBED_STATIC_ASSERT(2 + 2 == 4,
105+
* "Hopefully the universe is mathematically consistent");
106+
* };
107+
* @endcode
108+
*/
109+
#define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1
110+
111+
52112
#endif
53113

54114
/** @}*/

0 commit comments

Comments
 (0)