Skip to content

Commit

Permalink
Wsign-conversion enabled and remaining warnings fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
OleErikPeistorpet committed Jun 2, 2024
1 parent c9d877a commit 6ca8738
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
8 changes: 4 additions & 4 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_(un)signed

#include <cstring>

Expand Down Expand Up @@ -38,7 +38,7 @@ namespace oel::_detail
{ // Dereference to detect out of range errors if the iterator has internal check
#if OEL_MEM_BOUND_DEBUG_LVL
(void) *src;
(void) *(src + (nElems - 1));
(void) *(src + (as_signed(nElems) - 1));
#endif
std::memcpy(dest, to_pointer_contiguous(src), sizeof(*src) * nElems);
}
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
12 changes: 6 additions & 6 deletions auxi/range_algo_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ 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>)
{
#if OEL_MEM_BOUND_DEBUG_LVL
if (n != 0)
{ // Dereference to detect out of range errors if the iterator has internal check
(void) *dest;
(void) *(dest + (n - 1));
(void) *(dest + (as_signed(n) - 1));
}
#endif
_detail::MemcpyCheck(src, n, to_pointer_contiguous(dest));
return src + n;
_detail::MemcpyCheck(src, as_unsigned(n), to_pointer_contiguous(dest));
return src + as_signed(n);
}
else
{ for (size_t i{}; i < n; ++i)
{ for (Integral i{}; i < n; ++i)
{
dest[i] = *src;
++src;
Expand Down
20 changes: 10 additions & 10 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ 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)
Expand Down Expand Up @@ -482,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 All @@ -509,7 +509,7 @@ class dynarray
}
OEL_CATCH_ALL
{
erase_to_end(begin() + oldSize);
erase_to_end(begin() + as_signed(oldSize));
OEL_RETHROW;
}
return first;
Expand All @@ -524,7 +524,7 @@ 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
Expand Down Expand Up @@ -555,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 @@ -610,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 @@ -641,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 (_spareCapacity() >= count)
{
Expand Down Expand Up @@ -874,7 +874,7 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator pos) _
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 @@ -900,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 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)
8 changes: 4 additions & 4 deletions unit_test/dynarray_mutate_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@ TEST_F(dynarrayTest, insertRTrivial)

TEST_F(dynarrayTest, insertR)
{
size_t const initSize = 2;
constexpr size_t initSize{2};
std::array<TrivialRelocat, 2> const toInsert{TrivialRelocat{-1}, TrivialRelocat{-2}};
for (auto nReserve : {initSize, initSize + toInsert.size()})
for (size_t insertOffset = 0; insertOffset <= initSize; ++insertOffset)
for (ptrdiff_t insertOffset = 0; insertOffset <= oel::as_signed(initSize); ++insertOffset)
for (unsigned countThrow = 0; countThrow <= toInsert.size(); ++countThrow)
{ {
dynarray<TrivialRelocat> dest(oel::reserve, nReserve);
Expand All @@ -465,8 +465,8 @@ TEST_F(dynarrayTest, insertR)
}
if (dest.size() > initSize)
{
for (unsigned i = 0; i < countThrow; ++i)
EXPECT_TRUE( *toInsert[i] == *dest[i + insertOffset] );
for (size_t i{}; i < countThrow; ++i)
EXPECT_TRUE( *toInsert[i] == *dest[i + oel::as_unsigned(insertOffset)] );
}
if (insertOffset == 0)
{
Expand Down
13 changes: 11 additions & 2 deletions view/owning.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ class owning
constexpr bool empty() { return _r.empty(); }

constexpr decltype(auto) operator[](difference_type index)
OEL_REQUIRES(requires{ _r[index]; }) { return _r[index]; }

OEL_REQUIRES(requires{ _r[index]; })
{
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
return _r[index];
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
constexpr Range base() && { return std::move(_r); }
constexpr const Range & base() const & noexcept { return _r; }
void base() const && = delete;
Expand Down

0 comments on commit 6ca8738

Please sign in to comment.