Skip to content

Commit

Permalink
Wsign-conversion enabled and resulting warnings fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
OleErikPeistorpet committed Jun 2, 2024
1 parent f119f1c commit c98bc98
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 82 deletions.
6 changes: 3 additions & 3 deletions auxi/impl_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


#include "contiguous_iterator_to_ptr.h"
#include "range_traits.h"
#include "util.h" // for as_unsigned

#include <cstring>

Expand Down Expand Up @@ -88,11 +88,11 @@ namespace oel::_detail

if constexpr (std::is_trivial_v<T> and sizeof...(Args) == 0)
{
std::memset(first, 0, sizeof(T) * (last - first));
std::memset(first, 0, sizeof(T) * as_unsigned(last - first));
}
else if constexpr (isByte)
{
std::memset(first, static_cast<int>(args)..., last - first);
std::memset(first, static_cast<int>(args)..., as_unsigned(last - first));
}
else
{ T *const init = first;
Expand Down
8 changes: 4 additions & 4 deletions auxi/range_algo_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ namespace oel::_detail

////////////////////////////////////////////////////////////////////////////////

template< typename InputIter, typename RandomAccessIter >
InputIter CopyUnsf(InputIter src, size_t const n, RandomAccessIter const dest)
template< typename InputIter, typename Integral, typename RandomAccessIter >
InputIter CopyUnsf(InputIter src, Integral const n, RandomAccessIter const dest)
{
if constexpr (can_memmove_with<RandomAccessIter, InputIter>)
{
Expand All @@ -64,11 +64,11 @@ namespace oel::_detail
(void) *(dest + (n - 1));
}
#endif
_detail::MemcpyCheck(src, n, to_pointer_contiguous(dest));
_detail::MemcpyCheck(src, as_unsigned(n), to_pointer_contiguous(dest));
return src + n;
}
else
{ for (size_t i{}; i < n; ++i)
{ for (Integral i{}; i < n; ++i)
{
dest[i] = *src;
++src;
Expand Down
77 changes: 38 additions & 39 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ class dynarray

[[nodiscard]] bool empty() const noexcept { return _m.data == _m.end; }

size_type size() const noexcept { return _m.end - _m.data; }
size_type size() const noexcept { return static_cast<size_t>(_m.end - _m.data); }

size_type capacity() const noexcept { return _m.reservEnd - _m.data; }
size_type capacity() const noexcept { return static_cast<size_t>(_m.reservEnd - _m.data); }

constexpr size_type max_size() const noexcept { return _alloTrait::max_size(_m) - _allocateWrap::sizeForHeader; }

Expand Down Expand Up @@ -242,15 +242,21 @@ class dynarray
T & back() noexcept { return *_detail::MakeDynarrIter (_m, _m.end - 1); }
const T & back() const noexcept { return *_detail::MakeDynarrIter<const T *>(_m, _m.end - 1); }

T & operator[](size_type index) noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }
const T & operator[](size_type index) const noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }

T & at(size_type index) OEL_ALWAYS_INLINE
{
const auto & cSelf = *this;
return const_cast<T &>(cSelf.at(index));
}
const T & at(size_type index) const;

T & operator[](size_type index) noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }
const T & operator[](size_type index) const noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }
const T & at(size_type index) const
{
if (index < size()) // would be unsafe with signed size_type
return _m.data[index];
else
_detail::OutOfRange::raise("Bad index dynarray::at");
}

friend bool operator==(const dynarray & left, const dynarray & right)
{
Expand Down Expand Up @@ -290,7 +296,7 @@ class dynarray
: ::oel::_detail::DynarrBase<value_type *>{}, _usedAlloc_7KQw{std::move(a)} {
}

_memOwner(_memOwner && other) noexcept
constexpr _memOwner(_memOwner && other) noexcept
: ::oel::_detail::DynarrBase<value_type *>{other}, _usedAlloc_7KQw{std::move(other)}
{
other.reservEnd = other.end = other.data = nullptr;
Expand All @@ -299,7 +305,10 @@ class dynarray
~_memOwner()
{
if (data)
::oel::_detail::DebugAllocateWrapper<_usedAlloc_7KQw, value_type *>::dealloc(*this, data, reservEnd - data);
{
::oel::_detail::DebugAllocateWrapper<_usedAlloc_7KQw, value_type *>
::dealloc(*this, data, static_cast<size_t>(reservEnd - data));
}
}
}
_m; // the only non-static data member
Expand Down Expand Up @@ -330,7 +339,7 @@ class dynarray

size_type _unusedCapacity() const
{
return _m.reservEnd - _m.end;
return static_cast<size_t>(_m.reservEnd - _m.end);
}

size_type _calcCapUnchecked(size_type const newSize) const
Expand Down Expand Up @@ -375,13 +384,13 @@ class dynarray
{
if constexpr (allocator_can_realloc<allocator_type>)
{
T *const p = _allocateWrap::realloc(_m, _m.data, newCap);
auto const p = _allocateWrap::realloc(_m, _m.data, newCap);
_m.data = p;
_m.end = p + oldSize;
_m.reservEnd = p + newCap;
}
else
{ T *const newData = _allocateWrap::allocate(_m, newCap);
{ auto const newData = _allocateWrap::allocate(_m, newCap);
_m.end = _detail::Relocate(_m.data, oldSize, newData);
_resetData(newData, newCap);
}
Expand Down Expand Up @@ -437,22 +446,22 @@ class dynarray
}
_detail::MemcpyCheck(src, count, _m.data);

return src + count;
return src + as_signed(count);
}
else
{ auto cpy = [](InputIter src_, T *__restrict dest, T * dLast)
{
while (dest != dLast)
{
*dest = *src_;
++src_; ++dest;
++dest; ++src_;
}
return src_;
};
T * newEnd;
if (capacity() < count)
{
T *const newData = _allocateChecked(count);
auto const newData = _allocateChecked(count);
// Old elements might hold some limited resource, probably good to destroy them before constructing new
_detail::Destroy(_m.data, _m.end);
_resetData(newData, count);
Expand All @@ -473,7 +482,7 @@ class dynarray
while (_m.end < newEnd)
{ // each iteration updates _m.end for exception safety
_alloTrait::construct(_m, _m.end, *src);
++src; ++_m.end;
++_m.end; ++src;
}
return src;
}
Expand Down Expand Up @@ -515,12 +524,12 @@ class dynarray
if constexpr (can_memmove_with<T *, InputIter>)
{
_detail::MemcpyCheck(src, count, _m.end);
src += count;
src += as_signed(count);
_m.end += count;
}
else
{ T *__restrict dest = _m.end;
T *const dLast = dest + count;
auto const dLast = dest + count;
OEL_TRY_
{
while (dest != dLast)
Expand All @@ -546,8 +555,8 @@ class dynarray
{
auto const newData = _allocateWrap::allocate(_m, newCap);
// Exception free from here
auto const nBefore = pos - _m.data;
auto const nAfter = _m.end - pos;
auto const nBefore = as_unsigned(pos - _m.data);
auto const nAfter = as_unsigned(_m.end - pos);
T *const newPos = _detail::Relocate(_m.data, nBefore, newData);
_m.end = _detail::Relocate(pos, nAfter, newPos + count);

Expand Down Expand Up @@ -601,7 +610,7 @@ typename dynarray<T, Alloc>::iterator
_alloTrait::construct(_m, reinterpret_cast<T *>(&tmp), static_cast<Args &&>(args)...);
if (_m.end < _m.reservEnd)
{ // Relocate [pos, end) to [pos + 1, end + 1)
size_t const bytesAfterPos{sizeof(T) * (_m.end - pPos)};
auto const bytesAfterPos = sizeof(T) * as_unsigned(_m.end - pPos);
std::memmove(
static_cast<void *>(pPos + 1),
static_cast<const void *>(pPos),
Expand Down Expand Up @@ -632,7 +641,7 @@ typename dynarray<T, Alloc>::iterator
static_assert( std::is_same_v<decltype(count), size_t const>,
"insert_range requires that source models std::ranges::forward_range or that source.size() is valid" );

size_t const bytesAfterPos{sizeof(T) * (_m.end - pPos)};
auto const bytesAfterPos = sizeof(T) * as_unsigned(_m.end - pPos);
T * dLast;
if (_unusedCapacity() >= count)
{
Expand Down Expand Up @@ -800,7 +809,7 @@ inline void dynarray<T, Alloc>::append(size_type count, const T &__restrict val)
if (_unusedCapacity() < count)
_growBy(count);

T *const pos = _m.end;
auto const pos = _m.end;
_uninitFill::template call< _detail::ForwardT<const T &> >(pos, pos + count, _m, val);

_debugSizeUpdater guard{_m};
Expand All @@ -821,7 +830,7 @@ inline void dynarray<T, Alloc>::pop_back() noexcept
template< typename T, typename Alloc >
void dynarray<T, Alloc>::erase_to_end(iterator first) noexcept
{
T *const newEnd = to_pointer_contiguous(first);
T *const newEnd{to_pointer_contiguous(first)};
OEL_ASSERT(_m.data <= newEnd and newEnd <= _m.end);

_detail::Destroy(newEnd, _m.end);
Expand Down Expand Up @@ -856,16 +865,16 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator pos) _
{
_debugSizeUpdater guard{_m};

T *const ptr = to_pointer_contiguous(pos);
T *const ptr{to_pointer_contiguous(pos)};
OEL_ASSERT(_m.data <= ptr and ptr < _m.end);
if constexpr (is_trivially_relocatable<T>::value)
{
ptr-> ~T();
T *const next = ptr + 1;
auto const next = ptr + 1;
std::memmove( // relocate [pos + 1, end) to [pos, end - 1)
static_cast<void *>(ptr),
static_cast<const void *>(next),
sizeof(T) * (_m.end - next) );
sizeof(T) * as_unsigned(_m.end - next) );
--_m.end;
}
else
Expand All @@ -880,8 +889,8 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator first,
{
_debugSizeUpdater guard{_m};

T * dest = to_pointer_contiguous(first);
const T *const pLast = to_pointer_contiguous(last);
T * dest{to_pointer_contiguous(first)};
const T *const pLast{to_pointer_contiguous(last)};
OEL_ASSERT(_m.data <= dest and dest <= pLast and pLast <= _m.end);

if constexpr (is_trivially_relocatable<T>::value)
Expand All @@ -891,7 +900,7 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator first,
std::memmove( // relocate [last, end) to [first, first + nAfter)
static_cast<void *>(dest),
static_cast<const void *>(pLast),
sizeof(T) * nAfter );
sizeof(T) * as_unsigned(nAfter) );
_m.end = dest + nAfter;
}
else if (dest < pLast) // must avoid self-move-assigning the elements
Expand All @@ -904,16 +913,6 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator first,
}


template< typename T, typename Alloc >
const T & dynarray<T, Alloc>::at(size_type i) const
{
if (i < size()) // would be unsafe with signed size_type
return _m.data[i];
else
_detail::OutOfRange::raise("Bad index dynarray::at");
}


template< typename InputRange, typename Alloc = allocator<> >
explicit dynarray(InputRange &&, Alloc = {})
-> dynarray<
Expand Down
2 changes: 1 addition & 1 deletion unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ else()
if(MEM_BOUND_DEBUG)
target_compile_options(oel-test PRIVATE -fno-strict-aliasing)
endif()
target_compile_options(oel-test PRIVATE -Wall -Wextra -Wold-style-cast -Wshadow -Wconversion -Wno-sign-conversion)
target_compile_options(oel-test PRIVATE -Wall -Wextra -Wold-style-cast -Wshadow -Wconversion -Wsign-conversion)
endif()

target_link_libraries(oel-test gtest_main)
14 changes: 7 additions & 7 deletions unit_test/dynarray_construct_assignop_swap_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ TEST_F(dynarrayConstructTest, constructRangeNoCopyAssign)

TEST_F(dynarrayConstructTest, constructForwardRangeNoSize)
{
for (size_t const n : {0, 1, 59})
for (auto const n : {0u, 1u, 59u})
{
std::forward_list<int> li(n, -6);
dynarray<int> d(li);
Expand Down Expand Up @@ -373,12 +373,12 @@ void testConstructMoveElements()
g_allocCount.clear();
T::clearCount();
// not propagating, not equal, cannot steal the memory
for (auto const na : {0, 1, 101})
for (auto const na : {0u, 1u, 101u})
{
using Alloc = StatefulAllocator<T, false, false>;
dynarray<T, Alloc> a(reserve, na, Alloc{1});

for (int i = 0; i < na; ++i)
for (unsigned i{}; i < na; ++i)
a.emplace_back(i + 0.5);

auto const capBefore = a.capacity();
Expand All @@ -398,7 +398,7 @@ void testConstructMoveElements()

EXPECT_EQ(capBefore, a.capacity());
ASSERT_EQ(na, ssize(b));
for (int i = 0; i < na; ++i)
for (unsigned i{}; i < na; ++i)
EXPECT_TRUE(b[i].hasValue() and *b[i] == i + 0.5);
}
EXPECT_EQ(T::nConstructions, T::nDestruct);
Expand Down Expand Up @@ -471,8 +471,8 @@ void testAssignMoveElements()
for (auto const nb : {0, 1, 2})
{
using Alloc = StatefulAllocator<T, false, false>;
dynarray<T, Alloc> a(reserve, na, Alloc{1});
dynarray<T, Alloc> b(reserve, nb, Alloc{2});
dynarray<T, Alloc> a(reserve, as_unsigned(na), Alloc{1});
dynarray<T, Alloc> b(reserve, as_unsigned(nb), Alloc{2});

ASSERT_FALSE(a.get_allocator() == b.get_allocator());

Expand All @@ -497,7 +497,7 @@ void testAssignMoveElements()

EXPECT_EQ(capBefore, a.capacity());
ASSERT_EQ(na, ssize(b));
for (int i = 0; i < na; ++i)
for (size_t i{}; i < as_unsigned(na); ++i)
EXPECT_TRUE(b[i].hasValue() and *b[i] == i + 0.5);
}
EXPECT_EQ(T::nConstructions, T::nDestruct);
Expand Down
Loading

0 comments on commit c98bc98

Please sign in to comment.