Skip to content

Commit

Permalink
Cleanup additional GCC warnings
Browse files Browse the repository at this point in the history
When turning on the following flags, several additional warnings
were generated, which have been cleaned up in this patch. The
flags included:

-Wextra
-Wpedantic
-Wconversion
-Wsign-conversion
-Wctor-dtor-privacy
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-align
-Woverloaded-virtual
  • Loading branch information
rianquinn authored and Neil MacIntosh committed Nov 4, 2016
1 parent 38f453d commit d641796
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 20 deletions.
5 changes: 3 additions & 2 deletions gsl/multi_span
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ public:
constexpr size_type length() const noexcept { return this->size(); }

// length of the multi_span in bytes
constexpr size_type size_bytes() const noexcept { return sizeof(value_type) * this->size(); }
constexpr size_type size_bytes() const noexcept { return narrow_cast<size_type>(sizeof(value_type)) * this->size(); }

// length of the multi_span in bytes
constexpr size_type length_bytes() const noexcept { return this->size_bytes(); }
Expand Down Expand Up @@ -1653,7 +1653,8 @@ constexpr auto as_multi_span(multi_span<const byte, Dimensions...> s) noexcept -
ConstByteSpan::bounds_type::static_size % narrow_cast<std::ptrdiff_t>(sizeof(U)) == 0),
"Target type must be a trivial type and its size must match the byte array size");

Expects((s.size_bytes() % sizeof(U)) == 0 && (s.size_bytes() / sizeof(U)) < PTRDIFF_MAX);
Expects((s.size_bytes() % narrow_cast<std::ptrdiff_t>(sizeof(U))) == 0 &&
(s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))) < PTRDIFF_MAX);
return {reinterpret_cast<const U*>(s.data()),
s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))};
}
Expand Down
2 changes: 1 addition & 1 deletion gsl/span
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public:
constexpr index_type length() const noexcept { return size(); }
constexpr index_type size() const noexcept { return storage_.size(); }
constexpr index_type length_bytes() const noexcept { return size_bytes(); }
constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
constexpr index_type size_bytes() const noexcept { return size() * narrow_cast<index_type>(sizeof(element_type)); }
constexpr bool empty() const noexcept { return size() == 0; }

// [span.elem], span element access
Expand Down
2 changes: 1 addition & 1 deletion gsl/string_span
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public:
// Container signature should work for basic_string after C++17 version exists
template <class Traits, class Allocator>
constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
: span_(&str[0], str.length())
: span_(&str[0], narrow_cast<std::ptrdiff_t>(str.length()))
{
}

Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ else()
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -std=c++14 -O3 -Wall -Wno-missing-braces")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -std=c++14 -O3 -Wall -Wextra -Wpedantic -Wno-missing-braces -Wconversion -Wsign-conversion -Wctor-dtor-privacy -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Woverloaded-virtual")
elseif(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -std=c++11 -O3 -Wall -Wno-missing-braces")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -std=c++11 -O3 -Wall -Wextra -Wpedantic -Wno-missing-braces -Wconversion -Wsign-conversion -Wctor-dtor-privacy -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Woverloaded-virtual")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
Expand Down
4 changes: 2 additions & 2 deletions tests/bounds_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <vector>

using namespace std;
using namespace gsl;;
using namespace gsl;

namespace
{
Expand All @@ -37,7 +37,7 @@ SUITE(bounds_test)
j++)
{
use(j);
use(point[j]);
use(point[static_cast<size_t>(j)]);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/multi_span_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,14 +1427,14 @@ SUITE(multi_span_tests)
}
}

size_t check_sum = 0;
auto check_sum = 0;
for (auto i = 0; i < length; ++i) {
check_sum += av[i][1];
}

{
auto idx = 0;
size_t sum = 0;
auto sum = 0;
for (auto num : section) {
CHECK(num == av[idx][1]);
sum += num;
Expand All @@ -1444,8 +1444,8 @@ SUITE(multi_span_tests)
CHECK(sum == check_sum);
}
{
size_t idx = length - 1;
size_t sum = 0;
auto idx = length - 1;
auto sum = 0;
for (auto iter = section.rbegin(); iter != section.rend(); ++iter) {
CHECK(*iter == av[idx][1]);
sum += *iter;
Expand Down Expand Up @@ -1650,7 +1650,7 @@ SUITE(multi_span_tests)
{
multi_span<int, dynamic_range> av = a;
auto wav = as_writeable_bytes(av);
CHECK(wav.data() == (byte*) &a[0]);
CHECK(wav.data() == reinterpret_cast<byte*>(&a[0]));
CHECK(wav.length() == sizeof(a));
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/notnull_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ SUITE(NotNullTests)
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif
not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
CHECK((void*)p.get() == (void*)t.get());
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
}

TEST(TestNotNullAssignment)
Expand Down
8 changes: 4 additions & 4 deletions tests/span_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,17 +1336,17 @@ SUITE(span_tests)
#endif
{
auto f = [&]() {
span<int, 4> s4 = {arr2, 2};
static_cast<void>(s4);
span<int, 4> _s4 = {arr2, 2};
static_cast<void>(_s4);
};
CHECK_THROW(f(), fail_fast);
}

// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
span<int> av = arr2;
auto f = [&]() {
span<int, 4> s4 = av;
static_cast<void>(s4);
span<int, 4> _s4 = av;
static_cast<void>(_s4);
};
CHECK_THROW(f(), fail_fast);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/strided_span_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ SUITE(strided_span_tests)

int s = sizeof(int) / sizeof(byte);
auto d2 = 3 * s;
auto d1 = sizeof(int) * 12 / d2;
auto d1 = narrow_cast<int>(sizeof(int)) * 12 / d2;

// convert to 4x12 array of bytes
auto av = as_multi_span(as_bytes(as_multi_span(arr, 4)), dim(d1), dim(d2));
Expand Down
2 changes: 1 addition & 1 deletion tests/utils_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ SUITE(utils_tests)
}

int j = 0;
void g() { j += 1; };
void g() { j += 1; }
TEST(finally_function_ptr)
{
j = 0;
Expand Down

0 comments on commit d641796

Please sign in to comment.