Unified BUILD_BUG_ON and STATIC_ASSERT into STATIC_ASSERT.

This commit is contained in:
mikeller 2018-08-19 01:41:26 +12:00
parent 54dd79ac69
commit 70ac9423c7
10 changed files with 36 additions and 33 deletions

View File

@ -20,8 +20,6 @@
#pragma once #pragma once
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
#ifdef UNIT_TEST #ifdef UNIT_TEST
// make these visible to unit test // make these visible to unit test
#define STATIC_UNIT_TESTED #define STATIC_UNIT_TESTED

View File

@ -59,7 +59,9 @@
#endif #endif
#define STATIC_ASSERT(condition, name) \ #define STATIC_ASSERT(condition, name) \
typedef char assert_failed_ ## name [(condition) ? 1 : -1 ] __attribute__((unused)) enum { assert_failed_ ## name = sizeof(char [(condition) ? 1 : -1 ]) }
// If it is decided to ditch the -Wpedantic build flag, this should be switched to:
// _Static_assert((condition), #name)
#define BIT(x) (1 << (x)) #define BIT(x) (1 << (x))

View File

@ -86,12 +86,12 @@ typedef struct {
void initEEPROM(void) void initEEPROM(void)
{ {
// Verify that this architecture packs as expected. // Verify that this architecture packs as expected.
BUILD_BUG_ON(offsetof(packingTest_t, byte) != 0); STATIC_ASSERT(offsetof(packingTest_t, byte) == 0, byte_packing_test_failed);
BUILD_BUG_ON(offsetof(packingTest_t, word) != 1); STATIC_ASSERT(offsetof(packingTest_t, word) == 1, word_packing_test_failed);
BUILD_BUG_ON(sizeof(packingTest_t) != 5); STATIC_ASSERT(sizeof(packingTest_t) == 5, overall_packing_test_failed);
BUILD_BUG_ON(sizeof(configFooter_t) != 2); STATIC_ASSERT(sizeof(configFooter_t) == 2, footer_size_failed);
BUILD_BUG_ON(sizeof(configRecord_t) != 6); STATIC_ASSERT(sizeof(configRecord_t) == 6, record_size_failed);
} }
bool isEEPROMVersionValid(void) bool isEEPROMVersionValid(void)

View File

@ -37,12 +37,12 @@ __STATIC_INLINE DMA_TypeDef *LL_EX_DMA_Stream_to_DMA(DMA_Stream_TypeDef *DMAx_St
__STATIC_INLINE uint32_t LL_EX_DMA_Stream_to_Stream(DMA_Stream_TypeDef *DMAx_Streamy) __STATIC_INLINE uint32_t LL_EX_DMA_Stream_to_Stream(DMA_Stream_TypeDef *DMAx_Streamy)
{ {
STATIC_ASSERT(DMA1_Stream0_BASE - DMA1_BASE == sizeof(DMA_TypeDef), DMA_TypeDef_has_padding);
STATIC_ASSERT(DMA1_Stream1_BASE - DMA1_Stream0_BASE == sizeof(DMA_Stream_TypeDef), DMA_Stream_TypeDef_has_padding);
const size_t firstStreamOffset = sizeof(DMA_TypeDef); const size_t firstStreamOffset = sizeof(DMA_TypeDef);
const size_t streamSize = sizeof(DMA_Stream_TypeDef); const size_t streamSize = sizeof(DMA_Stream_TypeDef);
STATIC_ASSERT(DMA1_Stream0_BASE - DMA1_BASE == firstStreamOffset, DMA_TypeDef_has_padding);
STATIC_ASSERT(DMA1_Stream1_BASE - DMA1_Stream0_BASE == streamSize, DMA_Stream_TypeDef_has_padding);
return (((uint32_t) DMAx_Streamy & DMA_STREAM_MASK) - firstStreamOffset) / streamSize; return (((uint32_t) DMAx_Streamy & DMA_STREAM_MASK) - firstStreamOffset) / streamSize;
} }

View File

@ -236,7 +236,7 @@ static FAST_RAM_ZERO_INIT pt1Filter_t antiGravityThrottleLpf;
void pidInitFilters(const pidProfile_t *pidProfile) void pidInitFilters(const pidProfile_t *pidProfile)
{ {
BUILD_BUG_ON(FD_YAW != 2); // ensure yaw axis is 2 STATIC_ASSERT(FD_YAW == 2, FD_YAW_incorrect); // ensure yaw axis is 2
if (targetPidLooptime == 0) { if (targetPidLooptime == 0) {
// no looptime set, so set all the filters to null // no looptime set, so set all the filters to null

View File

@ -35,9 +35,10 @@
#include "common/axis.h" #include "common/axis.h"
#include "common/bitarray.h" #include "common/bitarray.h"
#include "common/color.h" #include "common/color.h"
#include "common/huffman.h"
#include "common/maths.h" #include "common/maths.h"
#include "common/streambuf.h" #include "common/streambuf.h"
#include "common/huffman.h" #include "common/utils.h"
#include "config/config_eeprom.h" #include "config/config_eeprom.h"
#include "config/feature.h" #include "config/feature.h"
@ -338,7 +339,7 @@ enum compressionType_e {
static void serializeDataflashReadReply(sbuf_t *dst, uint32_t address, const uint16_t size, bool useLegacyFormat, bool allowCompression) static void serializeDataflashReadReply(sbuf_t *dst, uint32_t address, const uint16_t size, bool useLegacyFormat, bool allowCompression)
{ {
BUILD_BUG_ON(MSP_PORT_DATAFLASH_INFO_SIZE < 16); STATIC_ASSERT(MSP_PORT_DATAFLASH_INFO_SIZE >= 16, MSP_PORT_DATAFLASH_INFO_SIZE_invalid);
uint16_t readLen = size; uint16_t readLen = size;
const int bytesRemainingInBuf = sbufBytesRemaining(dst) - MSP_PORT_DATAFLASH_INFO_SIZE; const int bytesRemainingInBuf = sbufBytesRemaining(dst) - MSP_PORT_DATAFLASH_INFO_SIZE;
@ -596,25 +597,27 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce
} }
case MSP_VOLTAGE_METER_CONFIG: case MSP_VOLTAGE_METER_CONFIG:
// by using a sensor type and a sub-frame length it's possible to configure any type of voltage meter, {
// e.g. an i2c/spi/can sensor or any sensor not built directly into the FC such as ESC/RX/SPort/SBus that has // by using a sensor type and a sub-frame length it's possible to configure any type of voltage meter,
// different configuration requirements. // e.g. an i2c/spi/can sensor or any sensor not built directly into the FC such as ESC/RX/SPort/SBus that has
BUILD_BUG_ON(VOLTAGE_SENSOR_ADC_VBAT != 0); // VOLTAGE_SENSOR_ADC_VBAT should be the first index, // different configuration requirements.
sbufWriteU8(dst, MAX_VOLTAGE_SENSOR_ADC); // voltage meters in payload STATIC_ASSERT(VOLTAGE_SENSOR_ADC_VBAT == 0, VOLTAGE_SENSOR_ADC_VBAT_incorrect); // VOLTAGE_SENSOR_ADC_VBAT should be the first index
for (int i = VOLTAGE_SENSOR_ADC_VBAT; i < MAX_VOLTAGE_SENSOR_ADC; i++) { sbufWriteU8(dst, MAX_VOLTAGE_SENSOR_ADC); // voltage meters in payload
const uint8_t adcSensorSubframeLength = 1 + 1 + 1 + 1 + 1; // length of id, type, vbatscale, vbatresdivval, vbatresdivmultipler, in bytes for (int i = VOLTAGE_SENSOR_ADC_VBAT; i < MAX_VOLTAGE_SENSOR_ADC; i++) {
sbufWriteU8(dst, adcSensorSubframeLength); // ADC sensor sub-frame length const uint8_t adcSensorSubframeLength = 1 + 1 + 1 + 1 + 1; // length of id, type, vbatscale, vbatresdivval, vbatresdivmultipler, in bytes
sbufWriteU8(dst, adcSensorSubframeLength); // ADC sensor sub-frame length
sbufWriteU8(dst, voltageMeterADCtoIDMap[i]); // id of the sensor sbufWriteU8(dst, voltageMeterADCtoIDMap[i]); // id of the sensor
sbufWriteU8(dst, VOLTAGE_SENSOR_TYPE_ADC_RESISTOR_DIVIDER); // indicate the type of sensor that the next part of the payload is for sbufWriteU8(dst, VOLTAGE_SENSOR_TYPE_ADC_RESISTOR_DIVIDER); // indicate the type of sensor that the next part of the payload is for
sbufWriteU8(dst, voltageSensorADCConfig(i)->vbatscale); sbufWriteU8(dst, voltageSensorADCConfig(i)->vbatscale);
sbufWriteU8(dst, voltageSensorADCConfig(i)->vbatresdivval); sbufWriteU8(dst, voltageSensorADCConfig(i)->vbatresdivval);
sbufWriteU8(dst, voltageSensorADCConfig(i)->vbatresdivmultiplier); sbufWriteU8(dst, voltageSensorADCConfig(i)->vbatresdivmultiplier);
}
// if we had any other voltage sensors, this is where we would output any needed configuration
} }
// if we had any other voltage sensors, this is where we would output any needed configuration
break;
break;
case MSP_CURRENT_METER_CONFIG: { case MSP_CURRENT_METER_CONFIG: {
// the ADC and VIRTUAL sensors have the same configuration requirements, however this API reflects // the ADC and VIRTUAL sensors have the same configuration requirements, however this API reflects
// that this situation may change and allows us to support configuration of any current sensor with // that this situation may change and allows us to support configuration of any current sensor with

View File

@ -1124,5 +1124,5 @@ const clivalue_t valueTable[] = {
const uint16_t valueTableEntryCount = ARRAYLEN(valueTable); const uint16_t valueTableEntryCount = ARRAYLEN(valueTable);
void settingsBuildCheck() { void settingsBuildCheck() {
BUILD_BUG_ON(LOOKUP_TABLE_COUNT != ARRAYLEN(lookupTables)); STATIC_ASSERT(LOOKUP_TABLE_COUNT == ARRAYLEN(lookupTables), LOOKUP_TABLE_COUNT_incorrect);
} }

View File

@ -165,7 +165,7 @@ void pgResetFn_ledStripConfig(ledStripConfig_t *ledStripConfig)
memset(ledStripConfig->ledConfigs, 0, LED_MAX_STRIP_LENGTH * sizeof(ledConfig_t)); memset(ledStripConfig->ledConfigs, 0, LED_MAX_STRIP_LENGTH * sizeof(ledConfig_t));
// copy hsv colors as default // copy hsv colors as default
memset(ledStripConfig->colors, 0, ARRAYLEN(hsv) * sizeof(hsvColor_t)); memset(ledStripConfig->colors, 0, ARRAYLEN(hsv) * sizeof(hsvColor_t));
BUILD_BUG_ON(LED_CONFIGURABLE_COLOR_COUNT < ARRAYLEN(hsv)); STATIC_ASSERT(LED_CONFIGURABLE_COLOR_COUNT >= ARRAYLEN(hsv), LED_CONFIGURABLE_COLOR_COUNT_invalid);
for (unsigned colorIndex = 0; colorIndex < ARRAYLEN(hsv); colorIndex++) { for (unsigned colorIndex = 0; colorIndex < ARRAYLEN(hsv); colorIndex++) {
ledStripConfig->colors[colorIndex] = hsv[colorIndex]; ledStripConfig->colors[colorIndex] = hsv[colorIndex];
} }

View File

@ -1095,7 +1095,7 @@ void osdInit(displayPort_t *osdDisplayPortToUse)
return; return;
} }
BUILD_BUG_ON(OSD_POS_MAX != OSD_POS(31,31)); STATIC_ASSERT(OSD_POS_MAX == OSD_POS(31,31), OSD_POS_MAX_incorrect);
osdDisplayPort = osdDisplayPortToUse; osdDisplayPort = osdDisplayPortToUse;
#ifdef USE_CMS #ifdef USE_CMS

View File

@ -63,7 +63,7 @@ static protocolSetRcDataFromPayloadFnPtr protocolSetRcDataFromPayload;
STATIC_UNIT_TESTED uint16_t rxSpiReadRawRC(const rxRuntimeConfig_t *rxRuntimeConfig, uint8_t channel) STATIC_UNIT_TESTED uint16_t rxSpiReadRawRC(const rxRuntimeConfig_t *rxRuntimeConfig, uint8_t channel)
{ {
BUILD_BUG_ON(NRF24L01_MAX_PAYLOAD_SIZE > RX_SPI_MAX_PAYLOAD_SIZE); STATIC_ASSERT(NRF24L01_MAX_PAYLOAD_SIZE <= RX_SPI_MAX_PAYLOAD_SIZE, NRF24L01_MAX_PAYLOAD_SIZE_larger_than_RX_SPI_MAX_PAYLOAD_SIZE);
if (channel >= rxRuntimeConfig->channelCount) { if (channel >= rxRuntimeConfig->channelCount) {
return 0; return 0;