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