Skip to content

Commit

Permalink
[#625] Fixed sign-conversion warnings from gcc compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jun 19, 2024
1 parent d9cb919 commit 6f4de4d
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 20 deletions.
4 changes: 4 additions & 0 deletions cmake/compiler_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ int64_t BitStreamReader::readSignedBits64(uint8_t numBits)
numBits < 64 && (static_cast<uint64_t>(value) >= (UINT64_C(1) << (numBits - 1)));
if (needsSignExtension)
{
value -= UINT64_C(1) << numBits;
value = static_cast<int64_t>(static_cast<uint64_t>(value) - (UINT64_C(1) << numBits));
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>((value >> shiftBits) & bitMasks[numBits - 1]);
const uint8_t add = static_cast<uint8_t>((value >> shiftBits) & bitMasks[numBits - 1U]);
byte = static_cast<uint8_t>(byte | add);
writeUnsignedBits(byte, 8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class BitStreamWriter

if (numRestBits > 0)
{
writeUnsignedBits(*itEnd >> (8U - numRestBits), numRestBits);
writeUnsignedBits(static_cast<uint32_t>(*itEnd) >> (8U - numRestBits), numRestBits);
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/extensions/cpp/runtime/src/zserio/DeltaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class DeltaContext
}
else
{
return m_maxBitNumber + (m_maxBitNumber > 0 ? 1 : 0);
return static_cast<size_t>(m_maxBitNumber) + (m_maxBitNumber > 0 ? 1 : 0);
}
}

Expand Down Expand Up @@ -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<size_t>(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)
Expand Down
2 changes: 1 addition & 1 deletion compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ float convertUInt16ToFloat(uint16_t float16Value)
else
{
// normal number
exponent32 = exponent16 - FLOAT16_EXPONENT_BIAS + FLOAT32_EXPONENT_BIAS;
exponent32 = static_cast<uint32_t>(exponent16) - FLOAT16_EXPONENT_BIAS + FLOAT32_EXPONENT_BIAS;
}

// compose single precision float (float32)
Expand Down
4 changes: 2 additions & 2 deletions compiler/extensions/cpp/runtime/src/zserio/TypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ const IBasicTypeInfo<ALLOC>& FixedSizeBuiltinTypeInfo<ALLOC>::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 <typename ALLOC>
Expand Down Expand Up @@ -1518,7 +1518,7 @@ const IBasicTypeInfo<ALLOC>& FixedSizeBuiltinTypeInfo<ALLOC>::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<size_t>(bitSize - 1)];
}

template <typename ALLOC>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(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<uint32_t>(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<uint32_t>(EXTERNAL_ARRAY_ELEMENT1_DATA) >>
(8U - (EXTERNAL_ARRAY_ELEMENT1_VAR_SIZE % 8U)),
EXTERNAL_ARRAY_ELEMENT1_VAR_SIZE);

// bytesField
Expand Down
2 changes: 1 addition & 1 deletion test/language/array_types/cpp/PackedAutoArrayUInt8Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/language/offsets/cpp/PackedAutoArrayOffsetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ArrayElementParamTest : public ::testing::Test
const uint16_t numItems = static_cast<uint16_t>(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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(numBlocks) * (16 + 32); // headers
for (size_t i = 0; i < numBlocks; ++i)
{
bitSize += 64 + (i + 1) * 64; // blocks[i]
Expand Down

0 comments on commit 6f4de4d

Please sign in to comment.