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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.