diff --git a/src/cli/cli_uart.cpp b/src/cli/cli_uart.cpp index a1a4d9327..c35b34dcc 100644 --- a/src/cli/cli_uart.cpp +++ b/src/cli/cli_uart.cpp @@ -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 @@ -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 { diff --git a/src/core/Makefile.am b/src/core/Makefile.am index d462752db..7e5fe3da9 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -462,7 +462,6 @@ HEADERS_COMMON = \ utils/otns.hpp \ utils/parse_cmdline.hpp \ utils/slaac_address.hpp \ - utils/static_assert.hpp \ $(NULL) noinst_HEADERS = \ diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index 4606e3905..87697c10a 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -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(OT_ALIGN(mBuffer.mHead.mData, kHelpDataAlignment)); } diff --git a/src/core/common/code_utils.hpp b/src/core/common/code_utils.hpp index 063753efa..72e7045a7 100644 --- a/src/core/common/code_utils.hpp +++ b/src/core/common/code_utils.hpp @@ -40,8 +40,6 @@ #include -#include "utils/static_assert.hpp" - /** * This macro calculates the number of elements in an array. * diff --git a/src/core/mac/channel_mask.hpp b/src/core/mac/channel_mask.hpp index 2c7fc8247..b90ad65a1 100644 --- a/src/core/mac/channel_mask.hpp +++ b/src/core/mac/channel_mask.hpp @@ -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; }; diff --git a/src/core/mac/data_poll_handler.hpp b/src/core/mac/data_poll_handler.hpp index 91762e4b2..bf66724d1 100644 --- a/src/core/mac/data_poll_handler.hpp +++ b/src/core/mac/data_poll_handler.hpp @@ -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!"); }; /** diff --git a/src/core/meshcop/meshcop.cpp b/src/core/meshcop/meshcop.cpp index 0bb5906ec..8046ea0bd 100644 --- a/src/core/meshcop/meshcop.cpp +++ b/src/core/meshcop/meshcop.cpp @@ -193,8 +193,8 @@ bool IsPskdValid(const char *aPskd) bool valid = false; size_t pskdLength = StringLength(aPskd, kPskdMaxLength + 1); - OT_STATIC_ASSERT(static_cast(kPskdMaxLength) <= static_cast(Dtls::kPskMaxLength), - "The maximum length of DTLS PSK is smaller than joiner PSKd"); + static_assert(static_cast(kPskdMaxLength) <= static_cast(Dtls::kPskMaxLength), + "The maximum length of DTLS PSK is smaller than joiner PSKd"); VerifyOrExit(pskdLength >= kPskdMinLength && pskdLength <= kPskdMaxLength, OT_NOOP); diff --git a/src/core/radio/radio.hpp b/src/core/radio/radio.hpp index d20a57c2e..0ebc15a38 100644 --- a/src/core/radio/radio.hpp +++ b/src/core/radio/radio.hpp @@ -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`. diff --git a/src/core/thread/indirect_sender.hpp b/src/core/thread/indirect_sender.hpp index 98e2c53df..d6fd63bcd 100644 --- a/src/core/thread/indirect_sender.hpp +++ b/src/core/thread/indirect_sender.hpp @@ -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!"); }; /** diff --git a/src/core/thread/topology.hpp b/src/core/thread/topology.hpp index 20bfc2418..425a4fcbc 100644 --- a/src/core/thread/topology.hpp +++ b/src/core/thread/topology.hpp @@ -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!"); }; /** diff --git a/src/core/utils/heap.hpp b/src/core/utils/heap.hpp index a7d2a1f68..60f22443e 100644 --- a/src/core/utils/heap.hpp +++ b/src/core/utils/heap.hpp @@ -41,7 +41,6 @@ #include #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. diff --git a/src/core/utils/slaac_address.cpp b/src/core/utils/slaac_address.cpp index 70a23e7de..101b48084 100644 --- a/src/core/utils/slaac_address.cpp +++ b/src/core/utils/slaac_address.cpp @@ -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); diff --git a/src/core/utils/static_assert.hpp b/src/core/utils/static_assert.hpp deleted file mode 100644 index de25c704a..000000000 --- a/src/core/utils/static_assert.hpp +++ /dev/null @@ -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 struct StaticAssertError; -template <> struct StaticAssertError -{ - 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_ diff --git a/src/ncp/changed_props_set.cpp b/src/ncp/changed_props_set.cpp index c266053f7..8a167e331 100644 --- a/src/ncp/changed_props_set.cpp +++ b/src/ncp/changed_props_set.cpp @@ -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); } diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index aecffeea2..ceeb45b43 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -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 diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index 7ffe78e8e..7dda234f2 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -854,7 +854,7 @@ template <> otError NcpBase::HandlePropertyInsert(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(dataLen); SuccessOrExit(error = mDecoder.ReadBool(stable)); @@ -864,7 +864,7 @@ template <> otError NcpBase::HandlePropertyInsert(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(dataLen); SuccessOrExit(error = otServerAddService(mInstance, &cfg)); diff --git a/src/ncp/ncp_spi.cpp b/src/ncp/ncp_spi.cpp index 286b18fb4..b8a931b02 100644 --- a/src/ncp/ncp_spi.cpp +++ b/src/ncp/ncp_spi.cpp @@ -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 { diff --git a/src/ncp/ncp_uart.cpp b/src/ncp/ncp_uart.cpp index 3ef967f98..09247b4b9 100644 --- a/src/ncp/ncp_uart.cpp +++ b/src/ncp/ncp_uart.cpp @@ -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 { diff --git a/tests/unit/test_toolchain.cpp b/tests/unit/test_toolchain.cpp index a66317a41..c43ce1458 100644 --- a/tests/unit/test_toolchain.cpp +++ b/tests/unit/test_toolchain.cpp @@ -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"); }