From 1f432c13b7dfdf3a0176fd47f61484560823ea62 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 12 May 2016 17:39:33 +1000 Subject: [PATCH] build: avoid type truncation warnings discovered with clang's -Wshorten-64-to-32 --- alloc/stack.cpp | 5 +++-- backtrace_execinfo.cpp | 6 +++--- cmdopt.cpp | 13 +++++++------ crypto/xxtea.cpp | 3 ++- hash/fasthash.cpp | 2 +- hash/murmur/murmur1.cpp | 2 +- hash/murmur/murmur2.cpp | 2 +- log.cpp | 5 +++-- signal.hpp | 2 +- signal.ipp | 2 +- stringid.hpp | 2 +- term.hpp | 4 ++-- uri.hpp | 2 +- vector.cpp | 2 +- version.cpp.rl | 4 ++-- 15 files changed, 30 insertions(+), 26 deletions(-) diff --git a/alloc/stack.cpp b/alloc/stack.cpp index 436aa249..6d7f6f5c 100644 --- a/alloc/stack.cpp +++ b/alloc/stack.cpp @@ -11,13 +11,14 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2016 Danny Robson */ #include "./stack.hpp" #include "../debug.hpp" #include "../pointer.hpp" +#include "../cast.hpp" using util::alloc::stack; @@ -65,7 +66,7 @@ stack::allocate (size_t bytes, size_t alignment) // store the total size and record the new stack head record record; record.as_bytes = ptr - sizeof (record::offset_t); - *record.as_uint32 = ptr - m_cursor; + *record.as_uint32 = trunc_cast (ptr - m_cursor); m_cursor = ptr + bytes; diff --git a/backtrace_execinfo.cpp b/backtrace_execinfo.cpp index c9be021c..36960430 100644 --- a/backtrace_execinfo.cpp +++ b/backtrace_execinfo.cpp @@ -20,7 +20,7 @@ #include "./debug.hpp" #include "./exe.hpp" #include "./io.hpp" -#include "cast.hpp" +#include "./cast.hpp" #include #include @@ -38,7 +38,7 @@ debug::backtrace::backtrace (void): size_t last; size_t size = m_frames.size (); - while ((last = ::backtrace (&m_frames[0], m_frames.size ())) == size) + while ((last = ::backtrace (&m_frames[0], trunc_cast (m_frames.size ()))) == size) m_frames.resize (size = m_frames.size () * 2); CHECK_GT (last, 0); @@ -78,7 +78,7 @@ debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { // We don't use the array form of unique_ptr as clang fails on ambigious constructors typedef std::unique_ptr str_t; - str_t names (backtrace_symbols (frames.data (), frames.size ()), ::free); + str_t names (backtrace_symbols (frames.data (), trunc_cast (frames.size ())), ::free); for (unsigned int i = 0; i < frames.size (); ++i) os << frames[i] << '\t' << names.get()[i] << '\t' << addr2line (frames[i]); diff --git a/cmdopt.cpp b/cmdopt.cpp index e2aab6a2..2502396c 100644 --- a/cmdopt.cpp +++ b/cmdopt.cpp @@ -14,9 +14,10 @@ * Copyright 2013-2016 Danny Robson */ -#include "cmdopt.hpp" +#include "./cmdopt.hpp" -#include "debug.hpp" +#include "./cast.hpp" +#include "./debug.hpp" #include #include @@ -407,7 +408,7 @@ parser::print_help (const int argc, { return std::get (a).size () < std::get (b).size (); }); - int longwidth = std::get (*largestwidth).size (); + auto longwidth = std::get (*largestwidth).size (); // find the longest example text auto largestexample = std::max_element ( @@ -421,7 +422,7 @@ parser::print_help (const int argc, return example_a.size () > example_b.size (); }); - int longexample = std::get> (*largestexample)->example ().size (); + auto longexample = std::get> (*largestexample)->example ().size (); // field width requires an alignment. we don't care about preserving // state as we're about to bail anyway @@ -455,13 +456,13 @@ parser::print_help (const int argc, else std::cout << '\t'; - std::cout << std::setw (longwidth); + std::cout << std::setw (trunc_cast (longwidth)); if (l != std::cend (m_long)) std::cout << std::get (*l) << '\t'; else std::cout << ' ' << '\t'; - std::cout << std::setw (longexample) << ptr->example () << '\t' + std::cout << std::setw (trunc_cast (longexample)) << ptr->example () << '\t' << std::setw (0) << std::get (o) << '\n'; } diff --git a/crypto/xxtea.cpp b/crypto/xxtea.cpp index 99e9a812..65dc47bd 100644 --- a/crypto/xxtea.cpp +++ b/crypto/xxtea.cpp @@ -82,7 +82,8 @@ XXTEA::decrypt (uint32_t *restrict data, size_t count) throw std::invalid_argument ("minimum blocksize is 64 bits"); uint32_t y, z, sum; - uint32_t p, rounds; + uint32_t rounds; + size_t p; rounds = 6 + 52 / count; sum = rounds * MAGIC; diff --git a/hash/fasthash.cpp b/hash/fasthash.cpp index adf298a1..7270e981 100644 --- a/hash/fasthash.cpp +++ b/hash/fasthash.cpp @@ -65,6 +65,6 @@ uint32_t util::hash::fasthash::hash32 (const void *restrict data, size_t len, uint32_t seed) { uint64_t h = hash64 (data, len, seed); - return h - (h >> 32); + return (h & 0xffffffff) - (h >> 32); } diff --git a/hash/murmur/murmur1.cpp b/hash/murmur/murmur1.cpp index 6a888700..38cf2004 100644 --- a/hash/murmur/murmur1.cpp +++ b/hash/murmur/murmur1.cpp @@ -42,7 +42,7 @@ util::hash::murmur1::hash_32 (const void *restrict data, CHECK (data); static const uint32_t m = 0xc6a4a793; - uint32_t h = seed ^ (len * m); + uint32_t h = seed ^ (uint32_t (len) * m); // mix the body auto cursor = reinterpret_cast (data); diff --git a/hash/murmur/murmur2.cpp b/hash/murmur/murmur2.cpp index f052f6ec..2cc80061 100644 --- a/hash/murmur/murmur2.cpp +++ b/hash/murmur/murmur2.cpp @@ -86,7 +86,7 @@ util::hash::murmur2::hash_32 (const void *restrict key, // setup static const auto m = constants::m; - uint32_t h = seed ^ len; + uint32_t h = seed ^ uint32_t (len); // body auto cursor = reinterpret_cast (key); diff --git a/log.cpp b/log.cpp index 1ecf9364..f58752be 100644 --- a/log.cpp +++ b/log.cpp @@ -21,6 +21,7 @@ #include "term.hpp" #include "time.hpp" #include "types.hpp" +#include "cast.hpp" #include #include @@ -211,7 +212,7 @@ util::log (util::level_t level, const std::string &msg) std::cerr << time_string << " [" << level_colour (level) - << std::setw (level_width ()) + << std::setw (trunc_cast (level_width ())) << std::left << level << std::setw (0) @@ -254,5 +255,5 @@ util::scoped_timer::~scoped_timer () auto finish = util::nanoseconds (); auto duration = finish - m_start; - log (m_level, "%fs, %s", duration / 1'000'000'000.f, m_message); + log (m_level, "%fs, %s", float (duration) / 1'000'000'000.f, m_message); } diff --git a/signal.hpp b/signal.hpp index 2169b6d5..e673cffc 100644 --- a/signal.hpp +++ b/signal.hpp @@ -92,7 +92,7 @@ namespace util { void clear (void); /// Returns the number of callbacks connected. - unsigned int size (void) const; + size_t size (void) const; bool empty (void) const; /// Execute all callbacks diff --git a/signal.ipp b/signal.ipp index 26311758..d210c68b 100644 --- a/signal.ipp +++ b/signal.ipp @@ -140,7 +140,7 @@ namespace util { /////////////////////////////////////////////////////////////////////////// /// Returns the number of callbacks connected. template class C> - unsigned int + size_t signal::size (void) const { return m_children.size (); diff --git a/stringid.hpp b/stringid.hpp index e0b8a136..a19813fa 100644 --- a/stringid.hpp +++ b/stringid.hpp @@ -23,7 +23,7 @@ namespace util { class stringid { public: - typedef unsigned id_t; + typedef size_t id_t; id_t add (std::string); id_t find (const std::string&) const; diff --git a/term.hpp b/term.hpp index 70132c70..88ed960c 100644 --- a/term.hpp +++ b/term.hpp @@ -31,12 +31,12 @@ namespace util { namespace term { struct graphics : public code { static constexpr char terminator = 'm'; - enum layer { + enum layer : char { FOREGROUND = 30, BACKGROUND = 40 }; - enum hue { + enum hue : char { BLACK = 0, RED = 1, GREEN = 2, diff --git a/uri.hpp b/uri.hpp index 82464f9f..92f4749a 100644 --- a/uri.hpp +++ b/uri.hpp @@ -34,7 +34,7 @@ namespace util { class parse_error : public std::runtime_error { using runtime_error::runtime_error; }; - enum component : unsigned { + enum component { SCHEME, AUTHORITY, PATH, diff --git a/vector.cpp b/vector.cpp index ab48427f..2fc75632 100644 --- a/vector.cpp +++ b/vector.cpp @@ -260,7 +260,7 @@ util::operator>> (const json::tree::node &node, util::vector &v) // compiler error at this point in release mode, so we dumb it down a // little. for (size_t i = 0; i < array.size (); ++i) - v.data[i] = static_cast (array[i].as_number ().native ()); + v.data[i] = array[i].as (); return node; } diff --git a/version.cpp.rl b/version.cpp.rl index 081dbe4a..13137425 100644 --- a/version.cpp.rl +++ b/version.cpp.rl @@ -168,9 +168,9 @@ util::version::parse (const char *str) { bool version::operator> (const version &rhs) const { - unsigned int count = min (size, rhs.size); + auto count = util::min (size, rhs.size); - for (unsigned int i = 0; i < count; ++i) + for (decltype(count) i = 0; i < count; ++i) if (components[i] < rhs.components[i]) return false;