toolchain: common: Merge build assert macros
In order to de-duplicate 2 macros with the same use, merge BUILD_ASSERT(), BUILD_ASSERT_MSG() into one macro. Make BUILD_ASSERT_MSG() deprecated. Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
This commit is contained in:
parent
a457681b1c
commit
974aa3add4
3 changed files with 21 additions and 20 deletions
|
@ -123,8 +123,8 @@ Build Assertions
|
||||||
Zephyr provides two macros for performing build-time assertion checks.
|
Zephyr provides two macros for performing build-time assertion checks.
|
||||||
These are evaluated completely at compile-time, and are always checked.
|
These are evaluated completely at compile-time, and are always checked.
|
||||||
|
|
||||||
BUILD_ASSERT_MSG()
|
BUILD_ASSERT()
|
||||||
------------------
|
--------------
|
||||||
|
|
||||||
This has the same semantics as C's ``_Static_assert`` or C++'s
|
This has the same semantics as C's ``_Static_assert`` or C++'s
|
||||||
``static_assert``. If the evaluation fails, a build error will be generated by
|
``static_assert``. If the evaluation fails, a build error will be generated by
|
||||||
|
@ -138,8 +138,7 @@ For example, suppose this check fails:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
BUILD_ASSERT_MSG(FOO == 2000,
|
BUILD_ASSERT(FOO == 2000, "Invalid value of FOO");
|
||||||
"Invalid value of FOO");
|
|
||||||
|
|
||||||
With GCC, the output resembles:
|
With GCC, the output resembles:
|
||||||
|
|
||||||
|
@ -147,17 +146,16 @@ With GCC, the output resembles:
|
||||||
|
|
||||||
tests/kernel/fatal/src/main.c: In function 'test_main':
|
tests/kernel/fatal/src/main.c: In function 'test_main':
|
||||||
include/toolchain/gcc.h:28:37: error: static assertion failed: "Invalid value of FOO"
|
include/toolchain/gcc.h:28:37: error: static assertion failed: "Invalid value of FOO"
|
||||||
#define BUILD_ASSERT_MSG(EXPR, MSG) _Static_assert(EXPR, MSG)
|
#define BUILD_ASSERT(EXPR, MSG) _Static_assert(EXPR, "" MSG)
|
||||||
^~~~~~~~~~~~~~
|
^~~~~~~~~~~~~~
|
||||||
tests/kernel/fatal/src/main.c:370:2: note: in expansion of macro 'BUILD_ASSERT_MSG'
|
tests/kernel/fatal/src/main.c:370:2: note: in expansion of macro 'BUILD_ASSERT'
|
||||||
BUILD_ASSERT_MSG(FOO == 2000,
|
BUILD_ASSERT(FOO == 2000,
|
||||||
^~~~~~~~~~~~~~~~
|
^~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
BUILD_ASSERT()
|
BUILD_ASSERT_MSG()
|
||||||
--------------
|
------------------
|
||||||
|
|
||||||
This works just like ``BUILD_ASSERT_MSG()`` except there is no supplemental
|
This macro is identical to ``BUILD_ASSERT()``. Its use is deprecated.
|
||||||
message provided, and like ``__ASSERT_NO_MSG()`` its use is discouraged.
|
|
||||||
|
|
||||||
Kernel Oops
|
Kernel Oops
|
||||||
===========
|
===========
|
||||||
|
|
|
@ -138,15 +138,17 @@
|
||||||
#define __subsystem
|
#define __subsystem
|
||||||
|
|
||||||
#ifndef BUILD_ASSERT
|
#ifndef BUILD_ASSERT
|
||||||
/* compile-time assertion that makes the build fail */
|
/* Compile-time assertion that makes the build to fail.
|
||||||
#define BUILD_ASSERT(EXPR) \
|
* Common implementation swallows the message.
|
||||||
|
*/
|
||||||
|
#define BUILD_ASSERT(EXPR, MSG) \
|
||||||
enum _CONCAT(__build_assert_enum, __COUNTER__) { \
|
enum _CONCAT(__build_assert_enum, __COUNTER__) { \
|
||||||
_CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
|
_CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef BUILD_ASSERT_MSG
|
#ifndef BUILD_ASSERT_MSG
|
||||||
/* build assertion with message -- common implementation swallows message. */
|
#define BUILD_ASSERT_MSG(EXPR, MSG) __DEPRECATED_MACRO BUILD_ASSERT(EXPR, MSG)
|
||||||
#define BUILD_ASSERT_MSG(EXPR, MSG) BUILD_ASSERT(EXPR)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -50,16 +50,17 @@
|
||||||
|
|
||||||
/* C++11 has static_assert built in */
|
/* C++11 has static_assert built in */
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#define BUILD_ASSERT(EXPR) static_assert(EXPR, "")
|
#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
|
||||||
#define BUILD_ASSERT_MSG(EXPR, MSG) static_assert(EXPR, MSG)
|
#define BUILD_ASSERT_MSG(EXPR, MSG) __DEPRECATED_MACRO BUILD_ASSERT(EXPR, MSG)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* GCC 4.6 and higher have the C11 _Static_assert built in, and its
|
* GCC 4.6 and higher have the C11 _Static_assert built in, and its
|
||||||
* output is easier to understand than the common BUILD_ASSERT macros.
|
* output is easier to understand than the common BUILD_ASSERT macros.
|
||||||
*/
|
*/
|
||||||
#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
|
#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
|
||||||
(__STDC_VERSION__) >= 201100
|
(__STDC_VERSION__) >= 201100
|
||||||
#define BUILD_ASSERT(EXPR) _Static_assert(EXPR, "")
|
#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
|
||||||
#define BUILD_ASSERT_MSG(EXPR, MSG) _Static_assert(EXPR, MSG)
|
#define BUILD_ASSERT_MSG(EXPR, MSG) __DEPRECATED_MACRO BUILD_ASSERT(EXPR, MSG)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <toolchain/common.h>
|
#include <toolchain/common.h>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue