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  * @code
81  * MBED_STATIC_ASSERT(MBED_MAJOR_VERSION >= 6,
82  * "The mbed-os library must be at least version 6.0.0");
83  *
84  * int main() {
85  * MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
86  * "An int must be larger than a char");
87  * }
88  * @endcode
89  *
90  * @deprecated This feature is now no longer necessary with the minimum
91  * supported language versions. It will be removed in a forthcoming release.
92  * Use `static_assert` instead. For C this is provided by `<assert.h>`, and
93  * for C++ it is a built-in keyword.
94  */
95 #if defined(__cplusplus)
96 #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
97 #else
98 #define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
99 #endif
100 
101 /** MBED_STRUCT_STATIC_ASSERT
102  * Declare compile-time assertions, results in compile-time error if condition is false
103  *
104  * Previous supported compiler languages would not allow static_assert to be
105  * used within a struct or a class. This is no longer the case. This macro
106  * exists for backwards compatibility.
107  *
108  * @code
109  * struct thing {
110  * MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
111  * "Hopefully the universe is mathematically consistent");
112  * };
113  * @endcode
114  *
115  * @deprecated This feature is now no longer necessary with the minimum
116  * supported language versions. It will be removed in a forthcoming release.
117  * Use `static_assert` instead. For C this is provided by `<assert.h>`, and
118  * for C++ it is a built-in keyword.
119  */
120 #define MBED_STRUCT_STATIC_ASSERT(expr, message) MBED_STATIC_ASSERT(expr, message)
121 
122 #endif
123 
124 /**@}*/
125 
126 /**@}*/
127 
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.