From 6f7b9acb4415d3b1ba217face42639c876090b73 Mon Sep 17 00:00:00 2001 From: Miki Rozloznik Date: Fri, 14 Jun 2024 03:03:07 -0400 Subject: [PATCH] Fixed sign-conversion warnings from gcc compiler --- cmake/compiler_utils.cmake | 4 ++++ .../cpp/runtime/src/zserio/BitStreamReader.cpp | 2 +- .../cpp/runtime/src/zserio/BitStreamWriter.cpp | 2 +- .../extensions/cpp/runtime/src/zserio/BitStreamWriter.h | 2 +- .../extensions/cpp/runtime/src/zserio/DeltaContext.h | 6 +++--- compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp | 2 +- compiler/extensions/cpp/runtime/src/zserio/TypeInfo.h | 4 ++-- .../cpp/runtime/test/zserio/BitStreamReaderTest.cpp | 2 +- .../cpp/runtime/test/zserio/BitStreamWriterTest.cpp | 2 +- .../set_cpp_allocator/cpp/ComplexAllocationTest.cpp | 9 ++++++--- .../array_types/cpp/PackedAutoArrayUInt8Test.cpp | 2 +- .../cpp/PackedVariableArrayStructRecursionTest.cpp | 2 +- .../array_types/cpp/PackedVariableArrayUInt8Test.cpp | 2 +- test/language/offsets/cpp/PackedAutoArrayOffsetTest.cpp | 2 +- .../parameterized_types/cpp/ArrayElementParamTest.cpp | 2 +- .../cpp/PackedArrayElementParamTest.cpp | 2 +- 16 files changed, 27 insertions(+), 20 deletions(-) diff --git a/cmake/compiler_utils.cmake b/cmake/compiler_utils.cmake index 4fca09284..59080fd99 100644 --- a/cmake/compiler_utils.cmake +++ b/cmake/compiler_utils.cmake @@ -9,6 +9,10 @@ endfunction() function(compiler_get_warnings_setup VARNAME) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(WARNINGS_SETUP "-Wall -Wextra -pedantic -Wconversion -Wno-long-long") + # gcc 7.5 reports Wsign-conversion even on static_cast, reportedly fixed in gcc 9.3 + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "9.3.0") + set(WARNINGS_SETUP "${WARNINGS_SETUP} -Wsign-conversion") + endif () elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set(WARNINGS_SETUP_LIST "-Weverything" "-Wno-system-headers" diff --git a/compiler/extensions/cpp/runtime/src/zserio/BitStreamReader.cpp b/compiler/extensions/cpp/runtime/src/zserio/BitStreamReader.cpp index 77b17cd74..00578c095 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/BitStreamReader.cpp +++ b/compiler/extensions/cpp/runtime/src/zserio/BitStreamReader.cpp @@ -435,7 +435,7 @@ int64_t BitStreamReader::readSignedBits64(uint8_t numBits) numBits < 64 && (static_cast(value) >= (UINT64_C(1) << (numBits - 1))); if (needsSignExtension) { - value -= UINT64_C(1) << numBits; + value = static_cast(static_cast(value) - (UINT64_C(1) << numBits)); } return value; diff --git a/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.cpp b/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.cpp index e9f5be797..3d26c65e0 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.cpp +++ b/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.cpp @@ -656,7 +656,7 @@ inline void BitStreamWriter::writeVarNum( } const size_t shiftBits = (numVarBytes - (i + 1)) * 7 + ((hasMaxByteRange && hasNextByte) ? 1 : 0); - const uint8_t add = static_cast((value >> shiftBits) & bitMasks[numBits - 1]); + const uint8_t add = static_cast((value >> shiftBits) & bitMasks[numBits - 1U]); byte = static_cast(byte | add); writeUnsignedBits(byte, 8); } diff --git a/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.h b/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.h index 7261528fe..d263ba4c0 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.h +++ b/compiler/extensions/cpp/runtime/src/zserio/BitStreamWriter.h @@ -264,7 +264,7 @@ class BitStreamWriter if (numRestBits > 0) { - writeUnsignedBits(*itEnd >> (8U - numRestBits), numRestBits); + writeUnsignedBits(static_cast(*itEnd) >> (8U - numRestBits), numRestBits); } } diff --git a/compiler/extensions/cpp/runtime/src/zserio/DeltaContext.h b/compiler/extensions/cpp/runtime/src/zserio/DeltaContext.h index bf587daf6..b2f25b0a4 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/DeltaContext.h +++ b/compiler/extensions/cpp/runtime/src/zserio/DeltaContext.h @@ -139,7 +139,7 @@ class DeltaContext } else { - return m_maxBitNumber + (m_maxBitNumber > 0 ? 1 : 0); + return static_cast(m_maxBitNumber) + (m_maxBitNumber > 0 ? 1 : 0); } } @@ -273,8 +273,8 @@ class DeltaContext { if (isFlagSet(IS_PACKED_FLAG)) { - const size_t deltaBitSize = m_maxBitNumber + (m_maxBitNumber > 0 ? 1 : 0); - const size_t packedBitSizeWithDescriptor = 1 + MAX_BIT_NUMBER_BITS + // descriptor + const size_t deltaBitSize = static_cast(m_maxBitNumber) + (m_maxBitNumber > 0 ? 1 : 0); + const size_t packedBitSizeWithDescriptor = 1U + MAX_BIT_NUMBER_BITS + // descriptor m_firstElementBitSize + (m_numElements - 1) * deltaBitSize; const size_t unpackedBitSizeWithDescriptor = 1 + m_unpackedBitSize; if (packedBitSizeWithDescriptor >= unpackedBitSizeWithDescriptor) diff --git a/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp b/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp index 480fc519d..b7b7d92e2 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp +++ b/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp @@ -65,7 +65,7 @@ float convertUInt16ToFloat(uint16_t float16Value) else { // normal number - exponent32 = exponent16 - FLOAT16_EXPONENT_BIAS + FLOAT32_EXPONENT_BIAS; + exponent32 = static_cast(exponent16) - FLOAT16_EXPONENT_BIAS + FLOAT32_EXPONENT_BIAS; } // compose single precision float (float32) diff --git a/compiler/extensions/cpp/runtime/src/zserio/TypeInfo.h b/compiler/extensions/cpp/runtime/src/zserio/TypeInfo.h index 62a8aec88..eb5e5e279 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/TypeInfo.h +++ b/compiler/extensions/cpp/runtime/src/zserio/TypeInfo.h @@ -1440,7 +1440,7 @@ const IBasicTypeInfo& FixedSizeBuiltinTypeInfo::getFixedSignedBitF {"int:63"_sv, SchemaType::FIXED_SIGNED_BITFIELD, CppType::INT64, 63}, {"int:64"_sv, SchemaType::FIXED_SIGNED_BITFIELD, CppType::INT64, 64}}}; - return bitFieldTypeInfoArray[bitSize - 1]; + return bitFieldTypeInfoArray[bitSize - 1U]; } template @@ -1518,7 +1518,7 @@ const IBasicTypeInfo& FixedSizeBuiltinTypeInfo::getFixedUnsignedBi {"bit:63"_sv, SchemaType::FIXED_UNSIGNED_BITFIELD, CppType::UINT64, 63}, {"bit:64"_sv, SchemaType::FIXED_UNSIGNED_BITFIELD, CppType::UINT64, 64}}}; - return bitFieldTypeInfoArray[bitSize - 1]; + return bitFieldTypeInfoArray[static_cast(bitSize - 1)]; } template diff --git a/compiler/extensions/cpp/runtime/test/zserio/BitStreamReaderTest.cpp b/compiler/extensions/cpp/runtime/test/zserio/BitStreamReaderTest.cpp index 6adab2bc1..5a937ae9f 100644 --- a/compiler/extensions/cpp/runtime/test/zserio/BitStreamReaderTest.cpp +++ b/compiler/extensions/cpp/runtime/test/zserio/BitStreamReaderTest.cpp @@ -85,7 +85,7 @@ TEST_F(BitStreamReaderTest, readUnalignedData) for (uint8_t offset = 0; offset <= 64; ++offset) { - BitBuffer buffer(8 + offset); + BitBuffer buffer(8U + offset); // write test value at offset to data buffer buffer.getData()[offset / 8U] = static_cast( diff --git a/compiler/extensions/cpp/runtime/test/zserio/BitStreamWriterTest.cpp b/compiler/extensions/cpp/runtime/test/zserio/BitStreamWriterTest.cpp index 47b498f1b..6a22d42a8 100644 --- a/compiler/extensions/cpp/runtime/test/zserio/BitStreamWriterTest.cpp +++ b/compiler/extensions/cpp/runtime/test/zserio/BitStreamWriterTest.cpp @@ -119,7 +119,7 @@ TEST_F(BitStreamWriterTest, writeUnalignedData) for (uint8_t offset = 0; offset <= 64; ++offset) { - BitBuffer bitBuffer(8 + offset); + BitBuffer bitBuffer(8U + offset); // fill the buffer with 1s to check proper masking std::memset(bitBuffer.getBuffer(), 0xFF, bitBuffer.getByteSize()); diff --git a/test/arguments/set_cpp_allocator/cpp/ComplexAllocationTest.cpp b/test/arguments/set_cpp_allocator/cpp/ComplexAllocationTest.cpp index 2f86fa30d..b162b584a 100644 --- a/test/arguments/set_cpp_allocator/cpp/ComplexAllocationTest.cpp +++ b/test/arguments/set_cpp_allocator/cpp/ComplexAllocationTest.cpp @@ -129,15 +129,18 @@ class ComplexAllocationTest : public ::testing::Test // externalField writer.writeVarSize(EXTERNAL_FIELD_VAR_SIZE); - writer.writeBits(EXTERNAL_FIELD_DATA >> (8U - (EXTERNAL_FIELD_VAR_SIZE % 8U)), EXTERNAL_FIELD_VAR_SIZE); + writer.writeBits(static_cast(EXTERNAL_FIELD_DATA) >> (8U - (EXTERNAL_FIELD_VAR_SIZE % 8U)), + EXTERNAL_FIELD_VAR_SIZE); // externalArray writer.writeVarSize(EXTERNAL_ARRAY_SIZE); writer.writeVarSize(EXTERNAL_ARRAY_ELEMENT0_VAR_SIZE); - writer.writeBits(EXTERNAL_ARRAY_ELEMENT0_DATA >> (8U - (EXTERNAL_ARRAY_ELEMENT0_VAR_SIZE % 8U)), + writer.writeBits(static_cast(EXTERNAL_ARRAY_ELEMENT0_DATA) >> + (8U - (EXTERNAL_ARRAY_ELEMENT0_VAR_SIZE % 8U)), EXTERNAL_ARRAY_ELEMENT0_VAR_SIZE); writer.writeVarSize(EXTERNAL_ARRAY_ELEMENT1_VAR_SIZE); - writer.writeBits(EXTERNAL_ARRAY_ELEMENT1_DATA >> (8U - (EXTERNAL_ARRAY_ELEMENT1_VAR_SIZE % 8U)), + writer.writeBits(static_cast(EXTERNAL_ARRAY_ELEMENT1_DATA) >> + (8U - (EXTERNAL_ARRAY_ELEMENT1_VAR_SIZE % 8U)), EXTERNAL_ARRAY_ELEMENT1_VAR_SIZE); // bytesField diff --git a/test/language/array_types/cpp/PackedAutoArrayUInt8Test.cpp b/test/language/array_types/cpp/PackedAutoArrayUInt8Test.cpp index fa6aad4ef..852e9a32a 100644 --- a/test/language/array_types/cpp/PackedAutoArrayUInt8Test.cpp +++ b/test/language/array_types/cpp/PackedAutoArrayUInt8Test.cpp @@ -38,7 +38,7 @@ class PackedAutoArrayUInt8Test : public ::testing::Test bitSize += 6; // packing descriptor: maxBitNumber } bitSize += 8; // first element - bitSize += (numElements - 1) * (PACKED_ARRAY_MAX_BIT_NUMBER + 1); // all deltas + bitSize += (numElements - 1) * (PACKED_ARRAY_MAX_BIT_NUMBER + 1U); // all deltas return bitSize; } diff --git a/test/language/array_types/cpp/PackedVariableArrayStructRecursionTest.cpp b/test/language/array_types/cpp/PackedVariableArrayStructRecursionTest.cpp index 7782f46dd..c83d63784 100644 --- a/test/language/array_types/cpp/PackedVariableArrayStructRecursionTest.cpp +++ b/test/language/array_types/cpp/PackedVariableArrayStructRecursionTest.cpp @@ -64,7 +64,7 @@ class PackedVariableArrayStructRecursionTest : public ::testing::Test size_t getUnpackedBlockBitSize(uint8_t byteCount, bool isLast) { - size_t bitSize = 8 * byteCount; // dataBytes[byteCount] + size_t bitSize = 8U * byteCount; // dataBytes[byteCount] bitSize += 8; // blockTerminator if (!isLast) { diff --git a/test/language/array_types/cpp/PackedVariableArrayUInt8Test.cpp b/test/language/array_types/cpp/PackedVariableArrayUInt8Test.cpp index 20b31524a..abc07961d 100644 --- a/test/language/array_types/cpp/PackedVariableArrayUInt8Test.cpp +++ b/test/language/array_types/cpp/PackedVariableArrayUInt8Test.cpp @@ -40,7 +40,7 @@ class PackedVariableArrayUInt8Test : public ::testing::Test bitSize += 6; // packing descriptor: maxBitNumber } bitSize += 8; // first element - bitSize += (numElements - 1) * (PACKED_ARRAY_MAX_BIT_NUMBER + 1); // all deltas + bitSize += (numElements - 1) * (PACKED_ARRAY_MAX_BIT_NUMBER + 1U); // all deltas return bitSize; } diff --git a/test/language/offsets/cpp/PackedAutoArrayOffsetTest.cpp b/test/language/offsets/cpp/PackedAutoArrayOffsetTest.cpp index c008a89a5..c09ab3837 100644 --- a/test/language/offsets/cpp/PackedAutoArrayOffsetTest.cpp +++ b/test/language/offsets/cpp/PackedAutoArrayOffsetTest.cpp @@ -59,7 +59,7 @@ class PackedAutoArrayOffsetTest : public ::testing::Test bitSize += 1; // packing descriptor: isPacked bitSize += 6; // packing descriptor: maxBitNumber bitSize += 7; // first element - bitSize += (AUTO_ARRAY_LENGTH - 1) * (PACKED_ARRAY_MAX_BIT_NUMBER + 1); // all deltas + bitSize += (AUTO_ARRAY_LENGTH - 1) * (PACKED_ARRAY_MAX_BIT_NUMBER + 1U); // all deltas return bitSize; } diff --git a/test/language/parameterized_types/cpp/ArrayElementParamTest.cpp b/test/language/parameterized_types/cpp/ArrayElementParamTest.cpp index bdaa4591f..adc74ae70 100644 --- a/test/language/parameterized_types/cpp/ArrayElementParamTest.cpp +++ b/test/language/parameterized_types/cpp/ArrayElementParamTest.cpp @@ -53,7 +53,7 @@ class ArrayElementParamTest : public ::testing::Test const uint16_t numItems = static_cast(reader.readBits(16)); ASSERT_EQ(headers.at(i).getNumItems(), numItems); ASSERT_EQ(expectedOffset, reader.readBits(32)); - expectedOffset += 8 * numItems; + expectedOffset += 8U * numItems; } const auto& blocks = database.getBlocks(); diff --git a/test/language/parameterized_types/cpp/PackedArrayElementParamTest.cpp b/test/language/parameterized_types/cpp/PackedArrayElementParamTest.cpp index 9fae3dd20..9249470cd 100644 --- a/test/language/parameterized_types/cpp/PackedArrayElementParamTest.cpp +++ b/test/language/parameterized_types/cpp/PackedArrayElementParamTest.cpp @@ -43,7 +43,7 @@ class PackedArrayElementParamTest : public ::testing::Test size_t getUnpackedDatabaseBitSize(uint16_t numBlocks) { size_t bitSize = 16; // numBlocks - bitSize += numBlocks * (16 + 32); // headers + bitSize += static_cast(numBlocks) * (16 + 32); // headers for (size_t i = 0; i < numBlocks; ++i) { bitSize += 64 + (i + 1) * 64; // blocks[i]