Skip to content

Add static assert macro #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions TESTS/mbedmicro-mbed/static_assert/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdint.h>

#include "toolchain.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

using namespace utest::v1;

void no_test() {}

utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(5, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Compilation test", no_test),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
39 changes: 39 additions & 0 deletions TESTS/mbedmicro-mbed/static_assert/test_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "mbed_assert.h"
#define THE_ANSWER 42

// Tests for static asserts in different contexts
// multiple asserts are used to garuntee no conflicts occur in generated labels

// Test for static asserts in global context
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");

struct test {
int dummy;

// Test for static asserts in struct context
MBED_STRUCT_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STRUCT_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");
};

MBED_STATIC_ASSERT(sizeof(struct test) == sizeof(int),
"Static assertions should not change the size of a struct");

void doit_c(void) {
// Test for static asserts in function context
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");
}

46 changes: 46 additions & 0 deletions TESTS/mbedmicro-mbed/static_assert/test_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "mbed_assert.h"
#define THE_ANSWER 42

// Tests for static asserts in different contexts
// multiple asserts are used to garuntee no conflicts occur in generated labels

// Test for static asserts in global context
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");

struct test {
int dummy;

// Test for static asserts in struct context
MBED_STRUCT_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STRUCT_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");

MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");
};

MBED_STATIC_ASSERT(sizeof(struct test) == sizeof(int),
"Static assertions should not change the size of a struct");

void doit_c(void) {
// Test for static asserts in function context
MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
"An int must be larger than char");
MBED_STATIC_ASSERT(2 + 2 == 4,
"Hopefully the universe is mathematically consistent");
MBED_STATIC_ASSERT(THE_ANSWER == 42,
"Said Deep Thought, with infinite majesty and calm");
}

9 changes: 6 additions & 3 deletions events/equeue/equeue_mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ static void equeue_tick_update() {
}

static void equeue_tick_init() {
MBED_ASSERT(sizeof(equeue_timer) >= sizeof(Timer));
MBED_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker));
MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(Timer),
"The equeue_timer buffer must fit the class Timer");
MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker),
"The equeue_ticker buffer must fit the class Ticker");
new (equeue_timer) Timer;
new (equeue_ticker) Ticker;

Expand Down Expand Up @@ -78,7 +80,8 @@ void equeue_mutex_unlock(equeue_mutex_t *m) {
#ifdef MBED_CONF_RTOS_PRESENT

int equeue_sema_create(equeue_sema_t *s) {
MBED_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore));
MBED_STATIC_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore),
"The equeue_sema_t must fit the class Semaphore");
new (s) Semaphore(0);
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion features/netsocket/NetworkStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ class NetworkStackWrapper : public NetworkStack
// Conversion function for network stacks
NetworkStack *nsapi_create_stack(nsapi_stack_t *stack)
{
MBED_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper));
MBED_STATIC_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper),
"The nsapi_stack_t stack buffer must fit a NetworkStackWrapper");
return new (stack->_stack_buffer) NetworkStackWrapper;
}

Expand Down
18 changes: 12 additions & 6 deletions platform/Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,8 @@ class Callback<R()> {
&Callback::function_dtor<F>,
};

MBED_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F));
MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
"Type F must not exceed the size of the Callback class");
new (this) F(f);
_ops = &ops;
}
Expand Down Expand Up @@ -1421,7 +1422,8 @@ class Callback<R(A0)> {
&Callback::function_dtor<F>,
};

MBED_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F));
MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
"Type F must not exceed the size of the Callback class");
new (this) F(f);
_ops = &ops;
}
Expand Down Expand Up @@ -2127,7 +2129,8 @@ class Callback<R(A0, A1)> {
&Callback::function_dtor<F>,
};

MBED_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F));
MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
"Type F must not exceed the size of the Callback class");
new (this) F(f);
_ops = &ops;
}
Expand Down Expand Up @@ -2833,7 +2836,8 @@ class Callback<R(A0, A1, A2)> {
&Callback::function_dtor<F>,
};

MBED_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F));
MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
"Type F must not exceed the size of the Callback class");
new (this) F(f);
_ops = &ops;
}
Expand Down Expand Up @@ -3539,7 +3543,8 @@ class Callback<R(A0, A1, A2, A3)> {
&Callback::function_dtor<F>,
};

MBED_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F));
MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
"Type F must not exceed the size of the Callback class");
new (this) F(f);
_ops = &ops;
}
Expand Down Expand Up @@ -4245,7 +4250,8 @@ class Callback<R(A0, A1, A2, A3, A4)> {
&Callback::function_dtor<F>,
};

MBED_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F));
MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
"Type F must not exceed the size of the Callback class");
new (this) F(f);
_ops = &ops;
}
Expand Down
59 changes: 59 additions & 0 deletions platform/mbed_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef MBED_ASSERT_H
#define MBED_ASSERT_H

#include "mbed_preprocessor.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -49,6 +51,63 @@ do { \
} while (0)
#endif


/** MBED_STATIC_ASSERT
* Declare 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to enhance the documentation by explaining that this macro is valid in C and C++ at file scope and in a code block. Then split the structure case in two:

  • C++: this macro is valid.
  • C: should use MBED_STRUCT_STATIC_ASSERT which is also valid for C++ code.

* code block (except after a label), or as a member of a C++ class/struct/union.
*
* @note
* Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
* - In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
* - In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and
* MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid
* in C and C++ class/struct/union scope.
*
* @code
* MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
* "The mbed library must be at least version 120");
*
* int main() {
* MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
* "An int must be larger than a char");
* }
* @endcode
*/
#if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
#elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
#define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
#elif defined(__cplusplus) && defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) \
&& (__GNUC__*100 + __GNUC_MINOR__) > 403L
#define MBED_STATIC_ASSERT(expr, msg) __extension__ static_assert(expr, msg)
#elif !defined(__cplusplus) && defined(__GNUC__) && !defined(__CC_ARM) \
&& (__GNUC__*100 + __GNUC_MINOR__) > 406L
#define MBED_STATIC_ASSERT(expr, msg) __extension__ _Static_assert(expr, msg)
#elif defined(__ICCARM__)
#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
#else
#define MBED_STATIC_ASSERT(expr, msg) \
enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}
#endif

/** MBED_STRUCT_STATIC_ASSERT
* Declare compile-time assertions, results in compile-time error if condition is false
*
* Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used
* as a member of a C/C++ class/struct/union.
*
* @code
* struct thing {
* MBED_STATIC_ASSERT(2 + 2 == 4,
* "Hopefully the universe is mathematically consistent");
* };
* @endcode
*/
#define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1


#endif

/** @}*/
53 changes: 53 additions & 0 deletions platform/mbed_preprocessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/** \addtogroup platform */
/** @{*/
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_PREPROCESSOR_H
#define MBED_PREPROCESSOR_H


/** MBED_CONCAT
* Concatenate tokens together
*
* @note
* Expands tokens before concatenation
*
* @code
* // Creates a unique label based on the line number
* int MBED_CONCAT(UNIQUE_LABEL_, __LINE__) = 1;
* @endcode
*/
#define MBED_CONCAT(a, b) MBED_CONCAT_(a, b)
#define MBED_CONCAT_(a, b) a##b

/** MBED_STRINGIFY
* Converts tokens into strings
*
* @note
* Expands tokens before stringification
*
* @code
* // Creates a string based on the parameters
* const char *c = MBED_STRINGIFY(This is a ridiculous way to create a string)
* @endcode
*/
#define MBED_STRINGIFY(a) MBED_STRINGIFY_(a)
#define MBED_STRINGIFY_(a) #a


#endif

/** @}*/
5 changes: 3 additions & 2 deletions platform/toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef MBED_TOOLCHAIN_H
#define MBED_TOOLCHAIN_H

#include "mbed_preprocessor.h"


// Warning for unsupported compilers
#if !defined(__GNUC__) /* GCC */ \
Expand Down Expand Up @@ -65,8 +67,7 @@
*/
#ifndef MBED_ALIGN
#if defined(__ICCARM__)
#define _MBED_ALIGN(N) _Pragma(#N)
#define MBED_ALIGN(N) _MBED_ALIGN(data_alignment=N)
#define MBED_ALIGN(N) _Pragma(MBED_STRINGIFY(data_alignment=N))
#else
#define MBED_ALIGN(N) __attribute__((aligned(N)))
#endif
Expand Down