001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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