From 84ba4304dceef2b8ac08e445c5ce5b3b5da43409 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 12 Sep 2021 18:07:47 +0100 Subject: Add -Wconversion and -Wsign-conversion --- Jamroot.jam | 2 ++ libadhocutil/buffer.cpp | 4 ++-- libadhocutil/curlMultiHandle.cpp | 2 +- libadhocutil/curlStream.cpp | 11 ++++++----- libadhocutil/curlStream.h | 4 ++-- libadhocutil/lexer-regex.cpp | 7 +++++-- libadhocutil/lexer-regex.h | 1 + libadhocutil/nvpParse.ll | 1 + libadhocutil/resourcePool.h | 12 ++++++------ libadhocutil/resourcePool.impl.h | 12 ++++++------ libadhocutil/semaphore.cpp | 4 ++-- libadhocutil/semaphore.h | 6 +++--- libadhocutil/unittests/testLazyPointer.cpp | 2 +- libadhocutil/unittests/testLexer.cpp | 1 + libadhocutil/unittests/testMapFinds.cpp | 2 +- libadhocutil/unittests/testOptionals.cpp | 4 ++-- libadhocutil/unittests/testProcessPipes.cpp | 12 +++++++----- 17 files changed, 49 insertions(+), 38 deletions(-) diff --git a/Jamroot.jam b/Jamroot.jam index 527b990..526b535 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -18,6 +18,8 @@ project debug:extra debug:on debug:-Wold-style-cast + debug:-Wconversion + debug:-Wsign-conversion coverage:on tidy:bin/sys.h tidy:bin/net.h diff --git a/libadhocutil/buffer.cpp b/libadhocutil/buffer.cpp index bade74e..26e15fc 100644 --- a/libadhocutil/buffer.cpp +++ b/libadhocutil/buffer.cpp @@ -155,7 +155,7 @@ namespace AdHoc { Buffer::vappendf(const char * fmt, va_list args) { char * frag; - size_t len = vasprintf(&frag, fmt, args); + const auto len = vasprintf(&frag, fmt, args); if (len > 0) { content.push_back(std::make_shared(frag, Free, len)); } @@ -299,7 +299,7 @@ std::ostream & std::operator<<(std::ostream & os, const AdHoc::Buffer & b) { for (const auto & f : b.content) { - os.write(f->c_str(), f->length()); + os.write(f->c_str(), static_cast(f->length())); } return os; } diff --git a/libadhocutil/curlMultiHandle.cpp b/libadhocutil/curlMultiHandle.cpp index 3f73ad7..49a58b3 100644 --- a/libadhocutil/curlMultiHandle.cpp +++ b/libadhocutil/curlMultiHandle.cpp @@ -59,7 +59,7 @@ namespace AdHoc::Net { while (!curls.empty() && running.size() < 5) { addRunner(curlm, running, curls); } - int act = running.size(); + int act = static_cast(running.size()); while (act) { while (curl_multi_perform(curlm, &act) == CURLM_CALL_MULTI_PERFORM) { } // Has anything finished diff --git a/libadhocutil/curlStream.cpp b/libadhocutil/curlStream.cpp index be7f162..051bbcf 100644 --- a/libadhocutil/curlStream.cpp +++ b/libadhocutil/curlStream.cpp @@ -23,8 +23,8 @@ namespace AdHoc::Net { return 0; } } - size_t bytes = std::min(buflen, targetSize); - memcpy(target, buf, bytes); + auto bytes = std::min(buflen, targetSize); + memcpy(target, buf, static_cast(bytes)); buflen -= bytes; buf += bytes; return bytes; @@ -42,11 +42,12 @@ namespace AdHoc::Net { size_t CurlStreamSource::recvWrapper(void * data, size_t sz, size_t nm, void * css) { - return static_cast(css)->recv(data, sz * nm); + return static_cast( + static_cast(css)->recv(data, static_cast(sz * nm))); } - size_t - CurlStreamSource::recv(void * data, size_t datalen) + std::streamsize + CurlStreamSource::recv(void * data, std::streamsize datalen) { buf = static_cast(data); buflen = datalen; diff --git a/libadhocutil/curlStream.h b/libadhocutil/curlStream.h index 83b7d7a..6425a2c 100644 --- a/libadhocutil/curlStream.h +++ b/libadhocutil/curlStream.h @@ -37,9 +37,9 @@ namespace AdHoc { DLL_PRIVATE void callback() override; DLL_PRIVATE static size_t recvWrapper(void * data, size_t sz, size_t nm, void * css); - DLL_PRIVATE size_t recv(void * data, size_t datalen); + DLL_PRIVATE std::streamsize recv(void * data, std::streamsize datalen); - size_t buflen; + std::streamsize buflen; char * buf; CURLcode res; }; diff --git a/libadhocutil/lexer-regex.cpp b/libadhocutil/lexer-regex.cpp index 239f953..6e17a5d 100644 --- a/libadhocutil/lexer-regex.cpp +++ b/libadhocutil/lexer-regex.cpp @@ -1,5 +1,6 @@ #include "lexer-regex.h" #include "c++11Helpers.h" +#include #include #include #include @@ -38,7 +39,8 @@ namespace AdHoc::LexerMatchers { if (info) { g_match_info_free(info); } - g_regex_match_full(regex, string, length, position, G_REGEX_MATCH_ANCHORED, &info, &err); + g_regex_match_full(regex, string, static_cast(length), static_cast(position), + G_REGEX_MATCH_ANCHORED, &info, &err); if (err) { auto msg = std::string("Failed to create GRegex: ") + err->message; g_error_free(err); @@ -53,7 +55,8 @@ namespace AdHoc::LexerMatchers { { gint start, end; g_match_info_fetch_pos(info, 0, &start, &end); - return end - start; + assert(start <= end); + return static_cast(end - start); } std::optional diff --git a/libadhocutil/lexer-regex.h b/libadhocutil/lexer-regex.h index 33dbc77..a469987 100644 --- a/libadhocutil/lexer-regex.h +++ b/libadhocutil/lexer-regex.h @@ -5,6 +5,7 @@ #include "visibility.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wsign-conversion" #include #include #pragma GCC diagnostic pop diff --git a/libadhocutil/nvpParse.ll b/libadhocutil/nvpParse.ll index 9e8b87c..af082ce 100644 --- a/libadhocutil/nvpParse.ll +++ b/libadhocutil/nvpParse.ll @@ -8,6 +8,7 @@ %{ #include "nvpParse.h" +#pragma GCC diagnostic ignored "-Wconversion" #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" #pragma GCC diagnostic ignored "-Wold-style-cast" diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 0b9e0ab..e8d8597 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -42,7 +42,7 @@ namespace AdHoc { explicit operator bool() const noexcept; /// Get number of handles to this resource. - [[nodiscard]] unsigned int handleCount() const; + [[nodiscard]] std::size_t handleCount() const; private: DLL_PRIVATE void decRef() noexcept; @@ -58,7 +58,7 @@ namespace AdHoc { /// Create a new resource pool. /// @param maxSize The upper limit of how many concurrent active resources there can be. /// @param keep The number of resources to cache for reuse. - ResourcePool(unsigned int maxSize, unsigned int keep); + ResourcePool(std::size_t maxSize, std::size_t keep); virtual ~ResourcePool(); /// Standard move/copy support @@ -75,11 +75,11 @@ namespace AdHoc { void idle(); /// Get number of active resources. - unsigned int inUseCount() const; + std::size_t inUseCount() const; /// Get number of available cached resources. - unsigned int availableCount() const; + std::size_t availableCount() const; /// Get number of free slots. - unsigned int freeCount() const; + std::size_t freeCount() const; protected: /// Create a new resource instance to add to the pool. @@ -101,7 +101,7 @@ namespace AdHoc { mutable std::shared_mutex lock; Semaphore poolSize; - unsigned int keep; + std::size_t keep; Available available; InUse inUse; }; diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index fe77857..31f0dbf 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -23,12 +23,12 @@ namespace AdHoc { } template - unsigned int + std::size_t ResourceHandle::handleCount() const { BOOST_ASSERT(resource); // InUse has one, we don't count that - return resource.use_count() - 1; + return static_cast(resource.use_count() - 1); } template @@ -108,7 +108,7 @@ namespace AdHoc { // ResourcePool // - template ResourcePool::ResourcePool(unsigned int max, unsigned int k) : poolSize(max), keep(k) { } + template ResourcePool::ResourcePool(std::size_t max, std::size_t k) : poolSize(max), keep(k) { } template ResourcePool::~ResourcePool() { @@ -130,7 +130,7 @@ namespace AdHoc { } template - unsigned int + std::size_t ResourcePool::inUseCount() const { SharedLock(lock); @@ -138,7 +138,7 @@ namespace AdHoc { } template - unsigned int + std::size_t ResourcePool::availableCount() const { SharedLock(lock); @@ -146,7 +146,7 @@ namespace AdHoc { } template - unsigned int + std::size_t ResourcePool::freeCount() const { SharedLock(lock); diff --git a/libadhocutil/semaphore.cpp b/libadhocutil/semaphore.cpp index 163a53b..e6ec7d0 100644 --- a/libadhocutil/semaphore.cpp +++ b/libadhocutil/semaphore.cpp @@ -2,7 +2,7 @@ #include namespace AdHoc { - Semaphore::Semaphore(unsigned int initial) : count(initial) { } + Semaphore::Semaphore(std::size_t initial) : count(initial) { } void Semaphore::notify() @@ -36,7 +36,7 @@ namespace AdHoc { return true; } - unsigned int + std::size_t Semaphore::freeCount() const { return count; diff --git a/libadhocutil/semaphore.h b/libadhocutil/semaphore.h index b3633b0..b551661 100644 --- a/libadhocutil/semaphore.h +++ b/libadhocutil/semaphore.h @@ -13,7 +13,7 @@ namespace AdHoc { class DLL_PUBLIC Semaphore { public: /// Construct a new semaphore with optional initial count. - explicit Semaphore(unsigned int initial = 0); + explicit Semaphore(std::size_t initial = 0); /// Notify one waiting thread. void notify(); @@ -23,12 +23,12 @@ namespace AdHoc { /// @param ms Timeout in milliseconds. bool wait(unsigned int ms); /// Free - [[nodiscard]] unsigned int freeCount() const; + [[nodiscard]] std::size_t freeCount() const; private: std::mutex mutex; std::condition_variable condition; - unsigned long count; + std::size_t count; }; } diff --git a/libadhocutil/unittests/testLazyPointer.cpp b/libadhocutil/unittests/testLazyPointer.cpp index f2440bd..c3bbbfe 100644 --- a/libadhocutil/unittests/testLazyPointer.cpp +++ b/libadhocutil/unittests/testLazyPointer.cpp @@ -108,7 +108,7 @@ BOOST_AUTO_TEST_CASE(rawPointerNonNull) int * rawFactory(const std::string & s) { - return new int(s.length()); + return new int(static_cast(s.length())); } BOOST_AUTO_TEST_CASE(rawPointerFactory) diff --git a/libadhocutil/unittests/testLexer.cpp b/libadhocutil/unittests/testLexer.cpp index 6267dda..837eaf3 100644 --- a/libadhocutil/unittests/testLexer.cpp +++ b/libadhocutil/unittests/testLexer.cpp @@ -4,6 +4,7 @@ #include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wsign-conversion" #include #include #pragma GCC diagnostic pop diff --git a/libadhocutil/unittests/testMapFinds.cpp b/libadhocutil/unittests/testMapFinds.cpp index 4b9f642..3372526 100644 --- a/libadhocutil/unittests/testMapFinds.cpp +++ b/libadhocutil/unittests/testMapFinds.cpp @@ -12,7 +12,7 @@ using namespace AdHoc; class NotFound : std::runtime_error { public: - explicit NotFound(int key) : std::runtime_error(boost::lexical_cast("Key not found: %d", key)) { } + explicit NotFound(int key) : std::runtime_error("Key not found: %d" + std::to_string(key)) { } }; const std::map sample {{1, "one"}, {2, "two"}, {4, "four"}, {8, "eight"}, {16, "sixteen"}}; diff --git a/libadhocutil/unittests/testOptionals.cpp b/libadhocutil/unittests/testOptionals.cpp index 6680558..4404568 100644 --- a/libadhocutil/unittests/testOptionals.cpp +++ b/libadhocutil/unittests/testOptionals.cpp @@ -27,8 +27,8 @@ BOOST_AUTO_TEST_CASE(general) BOOST_REQUIRE_EQUAL(2.3, *a1); auto a2 = x / 10; BOOST_REQUIRE_EQUAL(10, a2); - auto a3 = ix / 11; - BOOST_REQUIRE_EQUAL(11, a3); + auto a3 = ix / 11.F; + BOOST_REQUIRE_EQUAL(11.F, a3); auto a4 = iy / 11; BOOST_REQUIRE_EQUAL(4, a4); diff --git a/libadhocutil/unittests/testProcessPipes.cpp b/libadhocutil/unittests/testProcessPipes.cpp index 0fd3a26..90b0354 100644 --- a/libadhocutil/unittests/testProcessPipes.cpp +++ b/libadhocutil/unittests/testProcessPipes.cpp @@ -19,12 +19,14 @@ BOOST_AUTO_TEST_CASE(readfind) BOOST_REQUIRE_EQUAL(pp.fdIn(), -1); BOOST_REQUIRE_NE(pp.fdOut(), -1); BOOST_REQUIRE_NE(pp.fdError(), -1); - std::string buf(BUFSIZ, '\0'); - ssize_t bytes = read(pp.fdOut(), buf.data(), BUFSIZ); + std::array buf {}; + const auto bytes = read(pp.fdOut(), buf.data(), BUFSIZ); BOOST_REQUIRE_MESSAGE(bytes > 0, "bytes = " << bytes); - buf[bytes] = '\0'; - const char * lnf = strstr(buf.data(), "testProcessPipes.cpp"); - BOOST_REQUIRE_MESSAGE(lnf, buf); + std::string_view str {buf.data(), buf.data() + bytes}; + BOOST_TEST_CONTEXT(str) { + const auto lnf = str.find("testProcessPipes.cpp"); + BOOST_REQUIRE_NE(lnf, std::string_view::npos); + } int status; waitpid(pp.pid(), &status, 0); } -- cgit v1.2.3