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