From d641796b21cef4730efa5bac268c28662fd67d80 Mon Sep 17 00:00:00 2001 From: Rian Quinn Date: Thu, 3 Nov 2016 19:38:32 -0600 Subject: [PATCH] Cleanup additional GCC warnings 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 --- gsl/multi_span | 5 +++-- gsl/span | 2 +- gsl/string_span | 2 +- tests/CMakeLists.txt | 4 ++-- tests/bounds_tests.cpp | 4 ++-- tests/multi_span_tests.cpp | 10 +++++----- tests/notnull_tests.cpp | 2 +- tests/span_tests.cpp | 8 ++++---- tests/strided_span_tests.cpp | 2 +- tests/utils_tests.cpp | 2 +- 10 files changed, 21 insertions(+), 20 deletions(-) diff --git a/gsl/multi_span b/gsl/multi_span index 2186c7b..d91f02a 100644 --- a/gsl/multi_span +++ b/gsl/multi_span @@ -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(sizeof(value_type)) * this->size(); } // length of the multi_span in bytes constexpr size_type length_bytes() const noexcept { return this->size_bytes(); } @@ -1653,7 +1653,8 @@ constexpr auto as_multi_span(multi_span s) noexcept - ConstByteSpan::bounds_type::static_size % narrow_cast(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(sizeof(U))) == 0 && + (s.size_bytes() / narrow_cast(sizeof(U))) < PTRDIFF_MAX); return {reinterpret_cast(s.data()), s.size_bytes() / narrow_cast(sizeof(U))}; } diff --git a/gsl/span b/gsl/span index 601580c..3a3fb52 100644 --- a/gsl/span +++ b/gsl/span @@ -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(sizeof(element_type)); } constexpr bool empty() const noexcept { return size() == 0; } // [span.elem], span element access diff --git a/gsl/string_span b/gsl/string_span index 703bc01..b5512a2 100644 --- a/gsl/string_span +++ b/gsl/string_span @@ -331,7 +331,7 @@ public: // Container signature should work for basic_string after C++17 version exists template constexpr basic_string_span(std::basic_string& str) - : span_(&str[0], str.length()) + : span_(&str[0], narrow_cast(str.length())) { } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 44db32d..c60f597 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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() diff --git a/tests/bounds_tests.cpp b/tests/bounds_tests.cpp index d10bf6d..a4024d1 100644 --- a/tests/bounds_tests.cpp +++ b/tests/bounds_tests.cpp @@ -19,7 +19,7 @@ #include using namespace std; -using namespace gsl;; +using namespace gsl; namespace { @@ -37,7 +37,7 @@ SUITE(bounds_test) j++) { use(j); - use(point[j]); + use(point[static_cast(j)]); } } } diff --git a/tests/multi_span_tests.cpp b/tests/multi_span_tests.cpp index c0240ea..8dbe151 100644 --- a/tests/multi_span_tests.cpp +++ b/tests/multi_span_tests.cpp @@ -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; @@ -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; @@ -1650,7 +1650,7 @@ SUITE(multi_span_tests) { multi_span av = a; auto wav = as_writeable_bytes(av); - CHECK(wav.data() == (byte*) &a[0]); + CHECK(wav.data() == reinterpret_cast(&a[0])); CHECK(wav.length() == sizeof(a)); } } diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index 526b074..201b797 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -83,7 +83,7 @@ SUITE(NotNullTests) not_null s = reinterpret_cast(p); #endif not_null t = reinterpret_cast(p.get()); - CHECK((void*)p.get() == (void*)t.get()); + CHECK(reinterpret_cast(p.get()) == reinterpret_cast(t.get())); } TEST(TestNotNullAssignment) diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 35e6b03..9ef2721 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -1336,8 +1336,8 @@ SUITE(span_tests) #endif { auto f = [&]() { - span s4 = {arr2, 2}; - static_cast(s4); + span _s4 = {arr2, 2}; + static_cast(_s4); }; CHECK_THROW(f(), fail_fast); } @@ -1345,8 +1345,8 @@ SUITE(span_tests) // this should fail - we are trying to assign a small dynamic span to a fixed_size larger one span av = arr2; auto f = [&]() { - span s4 = av; - static_cast(s4); + span _s4 = av; + static_cast(_s4); }; CHECK_THROW(f(), fail_fast); } diff --git a/tests/strided_span_tests.cpp b/tests/strided_span_tests.cpp index 86666d1..de10910 100644 --- a/tests/strided_span_tests.cpp +++ b/tests/strided_span_tests.cpp @@ -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(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)); diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 9f4ba02..781f692 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -67,7 +67,7 @@ SUITE(utils_tests) } int j = 0; - void g() { j += 1; }; + void g() { j += 1; } TEST(finally_function_ptr) { j = 0;