diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-09-12 18:07:47 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-09-12 18:07:47 +0100 |
commit | 84ba4304dceef2b8ac08e445c5ce5b3b5da43409 (patch) | |
tree | 4f60715976f3968026921b4f0e56251d1a693283 | |
parent | Rewrite UriParse in modern C++ (diff) | |
download | libadhocutil-84ba4304dceef2b8ac08e445c5ce5b3b5da43409.tar.bz2 libadhocutil-84ba4304dceef2b8ac08e445c5ce5b3b5da43409.tar.xz libadhocutil-84ba4304dceef2b8ac08e445c5ce5b3b5da43409.zip |
Add -Wconversion and -Wsign-conversion
-rw-r--r-- | Jamroot.jam | 2 | ||||
-rw-r--r-- | libadhocutil/buffer.cpp | 4 | ||||
-rw-r--r-- | libadhocutil/curlMultiHandle.cpp | 2 | ||||
-rw-r--r-- | libadhocutil/curlStream.cpp | 11 | ||||
-rw-r--r-- | libadhocutil/curlStream.h | 4 | ||||
-rw-r--r-- | libadhocutil/lexer-regex.cpp | 7 | ||||
-rw-r--r-- | libadhocutil/lexer-regex.h | 1 | ||||
-rw-r--r-- | libadhocutil/nvpParse.ll | 1 | ||||
-rw-r--r-- | libadhocutil/resourcePool.h | 12 | ||||
-rw-r--r-- | libadhocutil/resourcePool.impl.h | 12 | ||||
-rw-r--r-- | libadhocutil/semaphore.cpp | 4 | ||||
-rw-r--r-- | libadhocutil/semaphore.h | 6 | ||||
-rw-r--r-- | libadhocutil/unittests/testLazyPointer.cpp | 2 | ||||
-rw-r--r-- | libadhocutil/unittests/testLexer.cpp | 1 | ||||
-rw-r--r-- | libadhocutil/unittests/testMapFinds.cpp | 2 | ||||
-rw-r--r-- | libadhocutil/unittests/testOptionals.cpp | 4 | ||||
-rw-r--r-- | 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 <variant>debug:<warnings>extra <variant>debug:<warnings-as-errors>on <variant>debug:<cflags>-Wold-style-cast + <variant>debug:<cflags>-Wconversion + <variant>debug:<cflags>-Wsign-conversion <variant>coverage:<coverage>on <toolset>tidy:<exclude>bin/sys.h <toolset>tidy:<exclude>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<CStringFragment>(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<std::streamsize>(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<int>(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<size_t>(buflen, targetSize); - memcpy(target, buf, bytes); + auto bytes = std::min(buflen, targetSize); + memcpy(target, buf, static_cast<size_t>(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<CurlStreamSource *>(css)->recv(data, sz * nm); + return static_cast<size_t>( + static_cast<CurlStreamSource *>(css)->recv(data, static_cast<std::streamsize>(sz * nm))); } - size_t - CurlStreamSource::recv(void * data, size_t datalen) + std::streamsize + CurlStreamSource::recv(void * data, std::streamsize datalen) { buf = static_cast<char *>(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 <cassert> #include <cstddef> #include <memory> #include <optional> @@ -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<gssize>(length), static_cast<gint>(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<size_t>(end - start); } std::optional<Glib::ustring> 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 <glib.h> #include <glibmm/ustring.h> #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<typename R> - unsigned int + std::size_t ResourceHandle<R>::handleCount() const { BOOST_ASSERT(resource); // InUse has one, we don't count that - return resource.use_count() - 1; + return static_cast<std::size_t>(resource.use_count() - 1); } template<typename R> @@ -108,7 +108,7 @@ namespace AdHoc { // ResourcePool // - template<typename R> ResourcePool<R>::ResourcePool(unsigned int max, unsigned int k) : poolSize(max), keep(k) { } + template<typename R> ResourcePool<R>::ResourcePool(std::size_t max, std::size_t k) : poolSize(max), keep(k) { } template<typename R> ResourcePool<R>::~ResourcePool() { @@ -130,7 +130,7 @@ namespace AdHoc { } template<typename R> - unsigned int + std::size_t ResourcePool<R>::inUseCount() const { SharedLock(lock); @@ -138,7 +138,7 @@ namespace AdHoc { } template<typename R> - unsigned int + std::size_t ResourcePool<R>::availableCount() const { SharedLock(lock); @@ -146,7 +146,7 @@ namespace AdHoc { } template<typename R> - unsigned int + std::size_t ResourcePool<R>::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 <chrono> 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<int>(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 <functional> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wsign-conversion" #include <glib.h> #include <glibmm/ustring.h> #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<std::string>("Key not found: %d", key)) { } + explicit NotFound(int key) : std::runtime_error("Key not found: %d" + std::to_string(key)) { } }; const std::map<int, std::string> 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<char, BUFSIZ> 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); } |