Mistake on this page?
Report an issue in GitHub or email us
mbed_assert.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_ASSERT_H
18 #define MBED_ASSERT_H
19 
20 #include "platform/mbed_toolchain.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /** \addtogroup platform-public-api */
27 /** @{*/
28 
29 /**
30  * \defgroup platform_Assert Assert macros
31  * @{
32  */
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 #if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
128 #define MBED_STRUCT_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
129 #elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
130 #define MBED_STRUCT_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
131 #else
132 #include <stdbool.h>
133 #define MBED_STRUCT_STATIC_ASSERT(expr, msg) bool : (expr) ? 0 : -1
134 #endif
135 
136 #endif
137 
138 /**@}*/
139 
140 /**@}*/
141 
MBED_NORETURN void mbed_assert_internal(const char *expr, const char *file, int line)
Internal mbed assert function which is invoked when MBED_ASSERT macro fails.
#define MBED_NORETURN
MBED_NORETURN Declare a function that will never return.
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.