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