Merge pull request #3113 from geky/static-assert

Add static assert macro
pull/3220/head
Sam Grove 2016-11-07 10:25:17 -06:00 committed by GitHub
commit 074555b715
9 changed files with 246 additions and 12 deletions

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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;
@ -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;
}

View File

@ -281,7 +281,8 @@ protected:
// 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;
}

View File

@ -715,7 +715,8 @@ private:
&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;
}
@ -1421,7 +1422,8 @@ private:
&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;
}
@ -2127,7 +2129,8 @@ private:
&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;
}
@ -2833,7 +2836,8 @@ private:
&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;
}
@ -3539,7 +3543,8 @@ private:
&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;
}
@ -4245,7 +4250,8 @@ private:
&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;
}

View File

@ -19,6 +19,8 @@
#ifndef MBED_ASSERT_H
#define MBED_ASSERT_H
#include "mbed_preprocessor.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -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
* 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
/** @}*/

View File

@ -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
/** @}*/

View File

@ -19,6 +19,8 @@
#ifndef MBED_TOOLCHAIN_H
#define MBED_TOOLCHAIN_H
#include "mbed_preprocessor.h"
// Warning for unsupported compilers
#if !defined(__GNUC__) /* GCC */ \
@ -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