[352] | 1 |
|
---|
| 2 | /** \addtogroup platform */
|
---|
| 3 | /** @{*/
|
---|
| 4 | /* mbed Microcontroller Library
|
---|
| 5 | * Copyright (c) 2006-2013 ARM Limited
|
---|
| 6 | *
|
---|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 8 | * you may not use this file except in compliance with the License.
|
---|
| 9 | * You may obtain a copy of the License at
|
---|
| 10 | *
|
---|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 12 | *
|
---|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 16 | * See the License for the specific language governing permissions and
|
---|
| 17 | * limitations under the License.
|
---|
| 18 | */
|
---|
| 19 | #ifndef MBED_ASSERT_H
|
---|
| 20 | #define MBED_ASSERT_H
|
---|
| 21 |
|
---|
| 22 | #include "mbed_preprocessor.h"
|
---|
| 23 |
|
---|
| 24 | #ifdef __cplusplus
|
---|
| 25 | extern "C" {
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
| 28 | /** Internal mbed assert function which is invoked when MBED_ASSERT macro failes.
|
---|
| 29 | * This function is active only if NDEBUG is not defined prior to including this
|
---|
| 30 | * assert header file.
|
---|
| 31 | * In case of MBED_ASSERT failing condition, error() is called with the assertation message.
|
---|
| 32 | * @param expr Expresion to be checked.
|
---|
| 33 | * @param file File where assertation failed.
|
---|
| 34 | * @param line Failing assertation line number.
|
---|
| 35 | */
|
---|
| 36 | void mbed_assert_internal(const char *expr, const char *file, int line);
|
---|
| 37 |
|
---|
| 38 | #ifdef __cplusplus
|
---|
| 39 | }
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | #ifdef NDEBUG
|
---|
| 43 | #define MBED_ASSERT(expr) ((void)0)
|
---|
| 44 |
|
---|
| 45 | #else
|
---|
| 46 | #define MBED_ASSERT(expr) \
|
---|
| 47 | do { \
|
---|
| 48 | if (!(expr)) { \
|
---|
| 49 | mbed_assert_internal(#expr, __FILE__, __LINE__); \
|
---|
| 50 | } \
|
---|
| 51 | } while (0)
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | /** MBED_STATIC_ASSERT
|
---|
| 56 | * Declare compile-time assertions, results in compile-time error if condition is false
|
---|
| 57 | *
|
---|
| 58 | * The assertion acts as a declaration that can be placed at file scope, in a
|
---|
| 59 | * code block (except after a label), or as a member of a C++ class/struct/union.
|
---|
| 60 | *
|
---|
| 61 | * @note
|
---|
| 62 | * Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
|
---|
| 63 | * - In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
|
---|
| 64 | * - In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and
|
---|
| 65 | * MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid
|
---|
| 66 | * in C and C++ class/struct/union scope.
|
---|
| 67 | *
|
---|
| 68 | * @code
|
---|
| 69 | * MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
|
---|
| 70 | * "The mbed library must be at least version 120");
|
---|
| 71 | *
|
---|
| 72 | * int main() {
|
---|
| 73 | * MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
|
---|
| 74 | * "An int must be larger than a char");
|
---|
| 75 | * }
|
---|
| 76 | * @endcode
|
---|
| 77 | */
|
---|
| 78 | #if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
|
---|
| 79 | #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
|
---|
| 80 | #elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
|
---|
| 81 | #define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
|
---|
| 82 | #elif defined(__cplusplus) && defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) \
|
---|
| 83 | && (__GNUC__*100 + __GNUC_MINOR__) > 403L
|
---|
| 84 | #define MBED_STATIC_ASSERT(expr, msg) __extension__ static_assert(expr, msg)
|
---|
| 85 | #elif !defined(__cplusplus) && defined(__GNUC__) && !defined(__CC_ARM) \
|
---|
| 86 | && (__GNUC__*100 + __GNUC_MINOR__) > 406L
|
---|
| 87 | #define MBED_STATIC_ASSERT(expr, msg) __extension__ _Static_assert(expr, msg)
|
---|
| 88 | #elif defined(__ICCARM__)
|
---|
| 89 | #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
|
---|
| 90 | #else
|
---|
| 91 | #define MBED_STATIC_ASSERT(expr, msg) \
|
---|
| 92 | enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}
|
---|
| 93 | #endif
|
---|
| 94 |
|
---|
| 95 | /** MBED_STRUCT_STATIC_ASSERT
|
---|
| 96 | * Declare compile-time assertions, results in compile-time error if condition is false
|
---|
| 97 | *
|
---|
| 98 | * Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used
|
---|
| 99 | * as a member of a C/C++ class/struct/union.
|
---|
| 100 | *
|
---|
| 101 | * @code
|
---|
| 102 | * struct thing {
|
---|
| 103 | * MBED_STATIC_ASSERT(2 + 2 == 4,
|
---|
| 104 | * "Hopefully the universe is mathematically consistent");
|
---|
| 105 | * };
|
---|
| 106 | * @endcode
|
---|
| 107 | */
|
---|
| 108 | #define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | #endif
|
---|
| 112 |
|
---|
| 113 | /** @}*/
|
---|