Mbed OS Reference
Loading...
Searching...
No Matches
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
24extern "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 */
43MBED_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. Code inside an MBED_ASSERT block
54 * will not be executed at all in Release builds.
55 *
56 * @code
57 *
58 * int Configure(serial_t *obj) {
59 * MBED_ASSERT(obj);
60 * }
61 * @endcode
62 */
63#ifdef NDEBUG
64#define MBED_ASSERT(expr) ((void)0)
65
66#else
67#define MBED_ASSERT(expr) \
68do { \
69 if (!(expr)) { \
70 mbed_assert_internal(#expr, MBED_FILENAME, __LINE__); \
71 } \
72} while (0)
73#endif
74
75// ARM Compiler 6 currently fails to define `static_assert` in assert.h; correct for this
76#if !defined __cplusplus && !defined static_assert
77#define static_assert _Static_assert
78#endif
79
80/** MBED_STATIC_ASSERT
81 * Declare compile-time assertions, results in compile-time error if condition is false
82 *
83 * The assertion acts as a declaration that can be placed at file scope, in a
84 * code block (except after a label), or as a member of a C++ class/struct/union.
85 *
86 * @code
87 * MBED_STATIC_ASSERT(MBED_MAJOR_VERSION >= 6,
88 * "The mbed-os library must be at least version 6.0.0");
89 *
90 * int main() {
91 * MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
92 * "An int must be larger than a char");
93 * }
94 * @endcode
95 *
96 * @deprecated This feature is now no longer necessary with the minimum
97 * supported language versions. It will be removed in a forthcoming release.
98 * Use `static_assert` instead. For C this is provided by `<assert.h>`, and
99 * for C++ it is a built-in keyword.
100 */
101#if defined(__cplusplus)
102#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
103#else
104#define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
105#endif
106
107/** MBED_STRUCT_STATIC_ASSERT
108 * Declare compile-time assertions, results in compile-time error if condition is false
109 *
110 * Previous supported compiler languages would not allow static_assert to be
111 * used within a struct or a class. This is no longer the case. This macro
112 * exists for backwards compatibility.
113 *
114 * @code
115 * struct thing {
116 * MBED_STRUCT_STATIC_ASSERT(2 + 2 == 4,
117 * "Hopefully the universe is mathematically consistent");
118 * };
119 * @endcode
120 *
121 * @deprecated This feature is now no longer necessary with the minimum
122 * supported language versions. It will be removed in a forthcoming release.
123 * Use `static_assert` instead. For C this is provided by `<assert.h>`, and
124 * for C++ it is a built-in keyword.
125 */
126#define MBED_STRUCT_STATIC_ASSERT(expr, message) MBED_STATIC_ASSERT(expr, message)
127
128#endif
129
130/**@}*/
131
132/**@}*/
133
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.