From b76ba4bbfd818ea4b84ede04b386fb7233f17222 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:32:26 +0100 Subject: [PATCH 1/7] pkg/littlefs: use static_assert() instead of BUILD_BUG_ON() --- pkg/littlefs/fs/littlefs_fs.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/littlefs/fs/littlefs_fs.c b/pkg/littlefs/fs/littlefs_fs.c index daf616bdc1..6e5ca07e8a 100644 --- a/pkg/littlefs/fs/littlefs_fs.c +++ b/pkg/littlefs/fs/littlefs_fs.c @@ -18,6 +18,7 @@ * @} */ +#include #include #include #include @@ -25,8 +26,6 @@ #include "fs/littlefs_fs.h" -#include "kernel_defines.h" - #define ENABLE_DEBUG 0 #include @@ -170,8 +169,10 @@ static int _mount(vfs_mount_t *mountp) { /* if one of the lines below fail to compile you probably need to adjust vfs buffer sizes ;) */ - BUILD_BUG_ON(VFS_DIR_BUFFER_SIZE < sizeof(lfs_dir_t)); - BUILD_BUG_ON(VFS_FILE_BUFFER_SIZE < sizeof(lfs_file_t)); + static_assert(VFS_DIR_BUFFER_SIZE >= sizeof(lfs_dir_t), + "lfs_dir_t must fit in VFS_DIR_BUFFER_SIZE"); + static_assert(VFS_FILE_BUFFER_SIZE >= sizeof(lfs_file_t), + "lfs_file_t must fit in VFS_FILE_BUFFER_SIZE"); littlefs_desc_t *fs = mountp->private_data; From 906d73107ac4ca3bb8023888859b16e48695c816 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:33:05 +0100 Subject: [PATCH 2/7] pkg/littlefs2: use static_assert() instead of BUILD_BUG_ON() --- pkg/littlefs2/fs/littlefs2_fs.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/littlefs2/fs/littlefs2_fs.c b/pkg/littlefs2/fs/littlefs2_fs.c index eca79576a9..7e7a5605af 100644 --- a/pkg/littlefs2/fs/littlefs2_fs.c +++ b/pkg/littlefs2/fs/littlefs2_fs.c @@ -18,6 +18,7 @@ * @} */ +#include #include #include #include @@ -25,8 +26,6 @@ #include "fs/littlefs2_fs.h" -#include "kernel_defines.h" - #define ENABLE_DEBUG 0 #include @@ -176,8 +175,10 @@ static int _mount(vfs_mount_t *mountp) { /* if one of the lines below fail to compile you probably need to adjust vfs buffer sizes ;) */ - BUILD_BUG_ON(VFS_DIR_BUFFER_SIZE < sizeof(lfs_dir_t)); - BUILD_BUG_ON(VFS_FILE_BUFFER_SIZE < sizeof(lfs_file_t)); + static_assert(VFS_DIR_BUFFER_SIZE >= sizeof(lfs_dir_t), + "lfs_dir_t must fit in VFS_DIR_BUFFER_SIZE"); + static_assert(VFS_FILE_BUFFER_SIZE >= sizeof(lfs_file_t), + "lfs_file_t must fit in VFS_FILE_BUFFER_SIZE"); littlefs2_desc_t *fs = mountp->private_data; From 92d75695b5b0d46565d8f4e5db37cb2aafe09f0f Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:33:19 +0100 Subject: [PATCH 3/7] pkg/fatfs: use static_assert() instead of BUILD_BUG_ON() --- pkg/fatfs/fatfs_vfs/fatfs_vfs.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/fatfs/fatfs_vfs/fatfs_vfs.c b/pkg/fatfs/fatfs_vfs/fatfs_vfs.c index 55224c1e05..2ce45fd84e 100644 --- a/pkg/fatfs/fatfs_vfs/fatfs_vfs.c +++ b/pkg/fatfs/fatfs_vfs/fatfs_vfs.c @@ -18,6 +18,7 @@ * @} */ +#include #include #include #include @@ -26,7 +27,6 @@ #include "fs/fatfs.h" -#include "kernel_defines.h" /* needed for BUILD_BUG_ON */ #include "time.h" #define ENABLE_DEBUG 0 @@ -50,8 +50,10 @@ static int _mount(vfs_mount_t *mountp) { /* if one of the lines below fail to compile you probably need to adjust vfs buffer sizes ;) */ - BUILD_BUG_ON(VFS_DIR_BUFFER_SIZE < sizeof(DIR)); - BUILD_BUG_ON(VFS_FILE_BUFFER_SIZE < sizeof(fatfs_file_desc_t)); + static_assert(VFS_DIR_BUFFER_SIZE >= sizeof(DIR), + "DIR must fit into VFS_DIR_BUFFER_SIZE"); + static_assert(VFS_FILE_BUFFER_SIZE >= sizeof(fatfs_file_desc_t), + "fatfs_file_desc_t must fit into VFS_FILE_BUFFER_SIZE"); fatfs_desc_t *fs_desc = (fatfs_desc_t *)mountp->private_data; From 362b964ed2ddcb2baf2f2e037525f71951447f6c Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:36:21 +0100 Subject: [PATCH 4/7] drivers/sps30: use static_assert() instead of BUILD_BUG_ON() --- drivers/sps30/sps30.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/sps30/sps30.c b/drivers/sps30/sps30.c index a3813b1603..e11bec1a2d 100644 --- a/drivers/sps30/sps30.c +++ b/drivers/sps30/sps30.c @@ -223,7 +223,8 @@ int sps30_read_measurement(const sps30_t *dev, sps30_data_t *data) { /* This compile time check is needed to ensure the below method used for endianness conversion will work as expected */ - BUILD_BUG_ON(sizeof(sps30_data_t) != (sizeof(float) * 10)); + static_assert(sizeof(sps30_data_t) == (sizeof(float) * 10), + "sps30_data_t must be sized 10 floats"); assert(dev && data); /* The target buffer is also used for storing the raw data temporarily */ From f022f88629d193b95053b554b2249edf2266eb49 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:36:43 +0100 Subject: [PATCH 5/7] drivers/sps30: make cppcheck happy --- drivers/sps30/sps30.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/sps30/sps30.c b/drivers/sps30/sps30.c index e11bec1a2d..f6934f0a43 100644 --- a/drivers/sps30/sps30.c +++ b/drivers/sps30/sps30.c @@ -104,7 +104,7 @@ static inline void _cpy_add_crc(uint8_t *data, size_t len, uint8_t *crcd_data) * @return true if all CRCs are valid * @return false if at least one CRC is invalid */ -static inline bool _cpy_check_crc(uint8_t *data, size_t len, uint8_t *crcd_data) +static inline bool _cpy_check_crc(uint8_t *data, size_t len, const uint8_t *crcd_data) { for (size_t elem = 0; elem < len / 2; elem++) { int idx = (elem << 1); From 674fca2023219beec5ede3eaef0e0c7c9697eb7e Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:46:45 +0100 Subject: [PATCH 6/7] sys/arduino: use static_assert() instead of BUILD_BUG_ON() --- sys/arduino/SPI.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sys/arduino/SPI.cpp b/sys/arduino/SPI.cpp index fd67aa83cb..d4f263c060 100644 --- a/sys/arduino/SPI.cpp +++ b/sys/arduino/SPI.cpp @@ -66,8 +66,10 @@ SPISettings::SPISettings(uint32_t clock_hz, uint8_t bitOrder, uint8_t dataMode) SPIClass::SPIClass(spi_t spi_dev) { - /* Check if default SPI interface is valid */ - BUILD_BUG_ON(ARDUINO_SPI_INTERFACE >= SPI_NUMOF); + /* Check if default SPI interface is valid. Casting to int to avoid + * bogus type-limits warning here. */ + static_assert((int)ARDUINO_SPI_INTERFACE <= (int)SPI_NUMOF, + "spi_dev out of bounds"); this->spi_dev = spi_dev; this->settings = SPISettings(); this->is_transaction = false; From 8608104fe599293a5a2d64b4d340baf3295e69ce Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 25 Nov 2021 09:46:57 +0100 Subject: [PATCH 7/7] core/kernel_defines.h: drop BUILD_BUG_ON() This provides the same functionality as `static_assert()` provided by C11 and has no advantages compared to it. Hence, encourage users to use standard C functionality instead. --- core/include/kernel_defines.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/core/include/kernel_defines.h b/core/include/kernel_defines.h index 4ade63792e..12f3ea410b 100644 --- a/core/include/kernel_defines.h +++ b/core/include/kernel_defines.h @@ -132,18 +132,6 @@ extern "C" { #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0])) #endif -/** - * @def BUILD_BUG_ON(condition) - * @brief Forces a compilation error if condition is true. - * This trick is only needed if the condition can't be evaluated - * before compile time (i.e. sizeof(sometype_t) < 42 ) - * For more details on this see for example: - * https://git.kernel.org/pub/scm/linux/kernel/git/stable/ - * linux-stable.git/tree/include/linux/bug.h - * @param[in] condition A condition that will be evaluated at compile time - */ -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)])) - /** * @def IS_ACTIVE(macro) * @brief Allows to verify a macro definition outside the preprocessor.