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