[core] use C++11 static_assert (#5127)

This commit uses C++11 `static_assert()` replacing the existing
`OT_STATIC_ASSERT()` macro definitions (intended to support build-time
assertion in C++003 (oder) tool-chains).
This commit is contained in:
Abtin Keshavarzian
2020-06-20 18:09:14 -07:00
committed by GitHub
parent 0cf84da100
commit 53d23b511d
19 changed files with 44 additions and 130 deletions
+6 -7
View File
@@ -51,7 +51,6 @@
#include "common/logging.hpp"
#include "common/new.hpp"
#include "common/tasklet.hpp"
#include "utils/static_assert.hpp"
#if OPENTHREAD_CONFIG_ENABLE_DEBUG_UART
#include <openthread/platform/debug_uart.h>
@@ -90,14 +89,14 @@
#endif // OT_CLI_UART_LOCK_HDR_FILE
#if OPENTHREAD_CONFIG_DIAG_ENABLE
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <= OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE,
"diag output buffer should be smaller than CLI UART tx buffer");
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
"diag command line should be smaller than CLI UART rx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <= OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE,
"diag output buffer should be smaller than CLI UART tx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
"diag command line should be smaller than CLI UART rx buffer");
#endif
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH <= OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
"command line should be should be smaller than CLI rx buffer");
static_assert(OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH <= OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
"command line should be should be smaller than CLI rx buffer");
namespace ot {
namespace Cli {
-1
View File
@@ -462,7 +462,6 @@ HEADERS_COMMON = \
utils/otns.hpp \
utils/parse_cmdline.hpp \
utils/slaac_address.hpp \
utils/static_assert.hpp \
$(NULL)
noinst_HEADERS = \
+2 -3
View File
@@ -42,7 +42,6 @@
#include "common/code_utils.hpp"
#include "common/encoding.hpp"
#include "common/message.hpp"
#include "utils/static_assert.hpp"
namespace ot {
@@ -639,8 +638,8 @@ private:
const HelpData &GetHelpData(void) const
{
OT_STATIC_ASSERT(sizeof(mBuffer.mHead.mMetadata) + sizeof(HelpData) + kHelpDataAlignment <= sizeof(mBuffer),
"Insufficient buffer size for CoAP processing!");
static_assert(sizeof(mBuffer.mHead.mMetadata) + sizeof(HelpData) + kHelpDataAlignment <= sizeof(mBuffer),
"Insufficient buffer size for CoAP processing!");
return *static_cast<const HelpData *>(OT_ALIGN(mBuffer.mHead.mData, kHelpDataAlignment));
}
-2
View File
@@ -40,8 +40,6 @@
#include <openthread/error.h>
#include "utils/static_assert.hpp"
/**
* This macro calculates the number of elements in an array.
*
+2 -3
View File
@@ -41,7 +41,6 @@
#include "common/string.hpp"
#include "radio/radio.hpp"
#include "utils/static_assert.hpp"
namespace ot {
namespace Mac {
@@ -252,8 +251,8 @@ public:
InfoString ToString(void) const;
private:
OT_STATIC_ASSERT((Radio::kChannelMin < 32) && (Radio::kChannelMax < 32),
"The channel number is larger than 32. `ChannelMask` uses 32 bit mask.");
static_assert((Radio::kChannelMin < 32) && (Radio::kChannelMax < 32),
"The channel number is larger than 32. `ChannelMask` uses 32 bit mask.");
uint32_t mMask;
};
+1 -1
View File
@@ -122,7 +122,7 @@ public:
bool mFramePurgePending : 1; // Indicates a pending purge request for the current indirect frame.
bool mFrameReplacePending : 1; // Indicates a pending replace request for the current indirect frame.
OT_STATIC_ASSERT(kMaxPollTriggeredTxAttempts < (1 << 5), "mIndirectTxAttempts cannot fit max!");
static_assert(kMaxPollTriggeredTxAttempts < (1 << 5), "mIndirectTxAttempts cannot fit max!");
};
/**
+2 -2
View File
@@ -193,8 +193,8 @@ bool IsPskdValid(const char *aPskd)
bool valid = false;
size_t pskdLength = StringLength(aPskd, kPskdMaxLength + 1);
OT_STATIC_ASSERT(static_cast<uint8_t>(kPskdMaxLength) <= static_cast<uint8_t>(Dtls::kPskMaxLength),
"The maximum length of DTLS PSK is smaller than joiner PSKd");
static_assert(static_cast<uint8_t>(kPskdMaxLength) <= static_cast<uint8_t>(Dtls::kPskMaxLength),
"The maximum length of DTLS PSK is smaller than joiner PSKd");
VerifyOrExit(pskdLength >= kPskdMinLength && pskdLength <= kPskdMaxLength, OT_NOOP);
+3 -4
View File
@@ -41,7 +41,6 @@
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "mac/mac_frame.hpp"
#include "utils/static_assert.hpp"
namespace ot {
@@ -91,9 +90,9 @@ public:
#endif
};
OT_STATIC_ASSERT((OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT || OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT),
"OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT or OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT "
"must be set to 1 to specify the radio mode");
static_assert((OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT || OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT),
"OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT or OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT "
"must be set to 1 to specify the radio mode");
/**
* This class defines the callbacks from `Radio`.
+2 -2
View File
@@ -120,8 +120,8 @@ public:
bool mUseShortAddress : 1; // Indicates whether to use short or extended address.
bool mSourceMatchPending : 1; // Indicates whether or not pending to add to src match table.
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS < (1UL << 14),
"mQueuedMessageCount cannot fit max required!");
static_assert(OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS < (1UL << 14),
"mQueuedMessageCount cannot fit max required!");
};
/**
+1 -1
View File
@@ -769,7 +769,7 @@ private:
uint16_t mSecondsSinceSupervision; ///< Number of seconds since last supervision of the child.
#endif
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS < 8192, "mQueuedMessageCount cannot fit max required!");
static_assert(OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS < 8192, "mQueuedMessageCount cannot fit max required!");
};
/**
+1 -2
View File
@@ -41,7 +41,6 @@
#include <stdint.h>
#include "common/non_copyable.hpp"
#include "utils/static_assert.hpp"
namespace ot {
namespace Utils {
@@ -246,7 +245,7 @@ private:
kGuardBlockOffset = kMemorySize - sizeof(uint16_t), ///< Offset of the guard block.
};
OT_STATIC_ASSERT(kMemorySize % kAlignSize == 0, "The heap memory size is not aligned to kAlignSize!");
static_assert(kMemorySize % kAlignSize == 0, "The heap memory size is not aligned to kAlignSize!");
/**
* This method returns the block at offset @p aOffset.
+2 -2
View File
@@ -286,8 +286,8 @@ otError Slaac::GenerateIid(Ip6::NetifUnicastAddress &aAddress,
Crypto::Sha256 sha256;
uint8_t hash[Crypto::Sha256::kHashSize];
OT_STATIC_ASSERT(sizeof(hash) >= Ip6::Address::kInterfaceIdentifierSize,
"SHA-256 hash size is too small to use as IPv6 address IID");
static_assert(sizeof(hash) >= Ip6::Address::kInterfaceIdentifierSize,
"SHA-256 hash size is too small to use as IPv6 address IID");
GetIidSecretKey(secretKey);
-74
View File
@@ -1,74 +0,0 @@
/*
* Copyright (c) 2019, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file includes macros for static assert for C++03.
*/
#ifndef OPENTHREAD_STATIC_ASSERT_HPP_
#define OPENTHREAD_STATIC_ASSERT_HPP_
#ifdef __cplusplus
/**
* Some compilers such as Keil MDK-ARM does not support __COUNTER__ macro.
*/
#ifndef __COUNTER__
#define __COUNTER__ __LINE__
#endif
namespace ot {
template <int> struct StaticAssertError;
template <> struct StaticAssertError<true>
{
StaticAssertError(...);
};
} // namespace ot
#define __OT_STATIC_ASSERT_ERROR(aError, aLine) aError##aLine
#define _OT_STATIC_ASSERT_ERROR(aLine) __OT_STATIC_ASSERT_ERROR(StaticAssertError, aLine)
/**
* This macro does static assert.
*
* @param[in] aExpression An expression to assert.
* @param[in] aMessage A message to describe what is wrong when @p aExpression is false.
*
*/
#define OT_STATIC_ASSERT(aExpression, aMessage) \
enum \
{ \
_OT_STATIC_ASSERT_ERROR(__COUNTER__) = sizeof(ot::StaticAssertError<(aExpression) != 0>(aMessage)), \
}
#endif // __cplusplus
#endif // OPENTHREAD_STATIC_ASSERT_HPP_
+2 -2
View File
@@ -99,8 +99,8 @@ const ChangedPropsSet::Entry ChangedPropsSet::mSupportedProps[] = {
uint8_t ChangedPropsSet::GetNumEntries(void) const
{
OT_STATIC_ASSERT(OT_ARRAY_LENGTH(mSupportedProps) <= sizeof(mChangedSet) * CHAR_BIT,
"Changed set size is smaller than number of entries in `mSupportedProps[]` array");
static_assert(OT_ARRAY_LENGTH(mSupportedProps) <= sizeof(mChangedSet) * CHAR_BIT,
"Changed set size is smaller than number of entries in `mSupportedProps[]` array");
return OT_ARRAY_LENGTH(mSupportedProps);
}
+3 -4
View File
@@ -55,7 +55,6 @@
#include "lib/spinel/spinel_buffer.hpp"
#include "lib/spinel/spinel_decoder.hpp"
#include "lib/spinel/spinel_encoder.hpp"
#include "utils/static_assert.hpp"
namespace ot {
namespace Ncp {
@@ -416,9 +415,9 @@ protected:
otError HandlePropertySet_SPINEL_PROP_HOST_POWER_STATE(uint8_t aHeader);
#if OPENTHREAD_CONFIG_DIAG_ENABLE
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <=
OPENTHREAD_CONFIG_NCP_TX_BUFFER_SIZE - kSpinelCmdHeaderSize - kSpinelPropIdSize,
"diag output buffer should be smaller than NCP UART tx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <=
OPENTHREAD_CONFIG_NCP_TX_BUFFER_SIZE - kSpinelCmdHeaderSize - kSpinelPropIdSize,
"diag output buffer should be smaller than NCP UART tx buffer");
otError HandlePropertySet_SPINEL_PROP_NEST_STREAM_MFG(uint8_t aHeader);
#endif
+2 -2
View File
@@ -854,7 +854,7 @@ template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_SERVER_SERVICES>(v
VerifyOrExit((dataLen <= sizeof(cfg.mServiceData)), error = OT_ERROR_INVALID_ARGS);
memcpy(cfg.mServiceData, data, dataLen);
OT_STATIC_ASSERT((sizeof(cfg.mServiceData) <= UINT8_MAX), "Cannot handle full range of buffer length");
static_assert((sizeof(cfg.mServiceData) <= UINT8_MAX), "Cannot handle full range of buffer length");
cfg.mServiceDataLength = static_cast<uint8_t>(dataLen);
SuccessOrExit(error = mDecoder.ReadBool(stable));
@@ -864,7 +864,7 @@ template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_SERVER_SERVICES>(v
VerifyOrExit((dataLen <= sizeof(cfg.mServerConfig.mServerData)), error = OT_ERROR_INVALID_ARGS);
memcpy(cfg.mServerConfig.mServerData, data, dataLen);
OT_STATIC_ASSERT((sizeof(cfg.mServerConfig.mServerData) <= UINT8_MAX), "Cannot handle full range of buffer length");
static_assert((sizeof(cfg.mServerConfig.mServerData) <= UINT8_MAX), "Cannot handle full range of buffer length");
cfg.mServerConfig.mServerDataLength = static_cast<uint8_t>(dataLen);
SuccessOrExit(error = otServerAddService(mInstance, &cfg));
+6 -7
View File
@@ -42,17 +42,16 @@
#include "common/instance.hpp"
#include "common/new.hpp"
#include "net/ip6.hpp"
#include "utils/static_assert.hpp"
#if OPENTHREAD_CONFIG_NCP_SPI_ENABLE
#if OPENTHREAD_CONFIG_DIAG_ENABLE
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <=
OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE - ot::Ncp::NcpBase::kSpinelCmdHeaderSize -
ot::Ncp::NcpBase::kSpinelPropIdSize - ot::Ncp::SpiFrame::kHeaderSize,
"diag output should be smaller than NCP SPI tx buffer");
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE,
"diag command line should be smaller than NCP SPI rx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <=
OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE - ot::Ncp::NcpBase::kSpinelCmdHeaderSize -
ot::Ncp::NcpBase::kSpinelPropIdSize - ot::Ncp::SpiFrame::kHeaderSize,
"diag output should be smaller than NCP SPI tx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE,
"diag command line should be smaller than NCP SPI rx buffer");
#endif
namespace ot {
+6 -7
View File
@@ -45,18 +45,17 @@
#include "common/instance.hpp"
#include "common/new.hpp"
#include "net/ip6.hpp"
#include "utils/static_assert.hpp"
#if OPENTHREAD_CONFIG_NCP_UART_ENABLE
#if OPENTHREAD_CONFIG_DIAG_ENABLE
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <= OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE -
ot::Ncp::NcpBase::kSpinelCmdHeaderSize -
ot::Ncp::NcpBase::kSpinelPropIdSize,
"diag output should be smaller than NCP UART rx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <= OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE -
ot::Ncp::NcpBase::kSpinelCmdHeaderSize -
ot::Ncp::NcpBase::kSpinelPropIdSize,
"diag output should be smaller than NCP UART rx buffer");
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE,
"diag command line should be smaller than NCP UART rx buffer");
static_assert(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE,
"diag command line should be smaller than NCP UART rx buffer");
#endif
namespace ot {
+3 -4
View File
@@ -34,7 +34,6 @@
#include "test_util.h"
#include "thread/topology.hpp"
#include "utils/static_assert.hpp"
extern "C" {
uint32_t otNetifAddress_Size_c();
@@ -52,7 +51,7 @@ void test_packed1()
uint16_t mShort;
} OT_TOOL_PACKED_END;
OT_STATIC_ASSERT(sizeof(packed_t) == 7, "packed_t should be packed to 7 bytes");
static_assert(sizeof(packed_t) == 7, "packed_t should be packed to 7 bytes");
VerifyOrQuit(sizeof(packed_t) == 7, "Toolchain::OT_TOOL_PACKED failed 1");
}
@@ -66,7 +65,7 @@ void test_packed2()
uint8_t mByte;
} OT_TOOL_PACKED_END;
OT_STATIC_ASSERT(sizeof(packed_t) == 4, "packed_t should be packed to 4 bytes");
static_assert(sizeof(packed_t) == 4, "packed_t should be packed to 4 bytes");
VerifyOrQuit(sizeof(packed_t) == 4, "Toolchain::OT_TOOL_PACKED failed 2");
}
@@ -89,7 +88,7 @@ void test_packed_union()
} OT_TOOL_PACKED_FIELD;
} OT_TOOL_PACKED_END;
OT_STATIC_ASSERT(sizeof(packed_t) == 5, "packed_t should be packed to 5 bytes");
static_assert(sizeof(packed_t) == 5, "packed_t should be packed to 5 bytes");
VerifyOrQuit(sizeof(packed_t) == 5, "Toolchain::OT_TOOL_PACKED failed 3");
}