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 <assert.h>
21 #include "platform/mbed_toolchain.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /** \addtogroup platform-public-api */
28 /** @{*/
29 
30 /**
31  * \defgroup platform_Assert Assert macros
32  * @{
33  */
34 
35 /** Internal mbed assert function which is invoked when MBED_ASSERT macro fails.
36  * This function is active only if NDEBUG is not defined prior to including this
37  * assert header file.
38  * In case of MBED_ASSERT failing condition, error() is called with the assertation message.
39  * @param expr Expression to be checked.
40  * @param file File where assertation failed.
41  * @param line Failing assertation line number.
42  */
43 MBED_NORETURN void mbed_assert_internal(const char *expr, const char *file, int line);
44 
45 #ifdef __cplusplus
46 }
47 #endif
48 
49 /** MBED_ASSERT
50  * Declare runtime assertions: results in runtime error if condition is false
51  *
52  * @note
53  * Use of MBED_ASSERT is limited to Debug and Develop builds.
54  *
55  * @code
56  *
57  * int Configure(serial_t *obj) {
58  * MBED_ASSERT(obj);
59  * }
60  * @endcode
61  */
62 #ifdef NDEBUG
63 #define MBED_ASSERT(expr) ((void)0)
64 
65 #else
66 #define MBED_ASSERT(expr) \
67 do { \
68  if (!(expr)) { \
69  mbed_assert_internal(#expr, MBED_FILENAME, __LINE__); \
70  } \
71 } while (0)
72 #endif
73 
74 // ARM Compiler 6 currently fails to define `static_assert` in assert.h; correct for this
75 #if !defined __cplusplus && !defined static_assert
76 #define static_assert _Static_assert
77 #endif
78 
79 /** MBED_STATIC_ASSERT
80  * Declare compile-time assertions, results in compile-time error if condition is false
81  *
82  * The assertion acts as a declaration that can be placed at file scope, in a
83  * code block (except after a label), or as a member of a C++ class/struct/union.
84  *
85  * @code
86  * MBED_STATIC_ASSERT(MBED_MAJOR_VERSION >= 6,
87  * "The mbed-os library must be at least version 6.0.0");
88  *
89  * int main() {
90  * MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
91  * "An int must be larger than a char");
92  * }
93  * @endcode
94  *
95  * @deprecated This feature is now no longer necessary with the minimum
96  * supported language versions. It will be removed in a forthcoming release.
97  * Use `static_assert` instead. For C this is provided by `<assert.h>`, and
98  * for C++ it is a built-in keyword.
99  */
100 #if defined(__cplusplus)
101 #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
102 #else
103 #define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
104 #endif
105 
106 /** MBED_STRUCT_STATIC_ASSERT
107  * Declare compile-time assertions, results in compile-time error if condition is false
108  *
109  * Previous supported compiler languages would not allow static_assert to be
110  * used within a struct or a class. This is no longer the case. This macro
111  * exists for backwards compatibility.
112  *
113  * @code
114  * struct thing {
115  * MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
116  * "Hopefully the universe is mathematically consistent");
117  * };
118  * @endcode
119  *
120  * @deprecated This feature is now no longer necessary with the minimum
121  * supported language versions. It will be removed in a forthcoming release.
122  * Use `static_assert` instead. For C this is provided by `<assert.h>`, and
123  * for C++ it is a built-in keyword.
124  */
125 #define MBED_STRUCT_STATIC_ASSERT(expr, message) MBED_STATIC_ASSERT(expr, message)
126 
127 #endif
128 
129 /**@}*/
130 
131 /**@}*/
132 
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.