build: avoid type truncation warnings

discovered with clang's -Wshorten-64-to-32
This commit is contained in:
Danny Robson 2016-05-12 17:39:33 +10:00
parent e71bd9f621
commit 1f432c13b7
15 changed files with 30 additions and 26 deletions

View File

@ -11,13 +11,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
* Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/
#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<uint32_t> (ptr - m_cursor);
m_cursor = ptr + bytes;

View File

@ -20,7 +20,7 @@
#include "./debug.hpp"
#include "./exe.hpp"
#include "./io.hpp"
#include "cast.hpp"
#include "./cast.hpp"
#include <sstream>
#include <iomanip>
@ -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<int> (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<char *, decltype(&std::free)> str_t;
str_t names (backtrace_symbols (frames.data (), frames.size ()), ::free);
str_t names (backtrace_symbols (frames.data (), trunc_cast<int> (frames.size ())), ::free);
for (unsigned int i = 0; i < frames.size (); ++i)
os << frames[i] << '\t' << names.get()[i] << '\t' << addr2line (frames[i]);

View File

@ -14,9 +14,10 @@
* Copyright 2013-2016 Danny Robson <danny@nerdcruft.net>
*/
#include "cmdopt.hpp"
#include "./cmdopt.hpp"
#include "debug.hpp"
#include "./cast.hpp"
#include "./debug.hpp"
#include <cstring>
#include <iostream>
@ -407,7 +408,7 @@ parser::print_help (const int argc,
{
return std::get<std::string> (a).size () < std::get<std::string> (b).size ();
});
int longwidth = std::get<std::string> (*largestwidth).size ();
auto longwidth = std::get<std::string> (*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<std::unique_ptr<option::base>> (*largestexample)->example ().size ();
auto longexample = std::get<std::unique_ptr<option::base>> (*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<int> (longwidth));
if (l != std::cend (m_long))
std::cout << std::get<std::string> (*l) << '\t';
else
std::cout << ' ' << '\t';
std::cout << std::setw (longexample) << ptr->example () << '\t'
std::cout << std::setw (trunc_cast<int> (longexample)) << ptr->example () << '\t'
<< std::setw (0) << std::get<std::string> (o)
<< '\n';
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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<const uint32_t*> (data);

View File

@ -86,7 +86,7 @@ util::hash::murmur2::hash_32 (const void *restrict key,
// setup
static const auto m = constants<uint32_t>::m;
uint32_t h = seed ^ len;
uint32_t h = seed ^ uint32_t (len);
// body
auto cursor = reinterpret_cast<const uint32_t*> (key);

View File

@ -21,6 +21,7 @@
#include "term.hpp"
#include "time.hpp"
#include "types.hpp"
#include "cast.hpp"
#include <array>
#include <ctime>
@ -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<int> (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);
}

View File

@ -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

View File

@ -140,7 +140,7 @@ namespace util {
///////////////////////////////////////////////////////////////////////////
/// Returns the number of callbacks connected.
template <typename F, template <typename> class C>
unsigned int
size_t
signal<F,C>::size (void) const
{
return m_children.size ();

View File

@ -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;

View File

@ -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,

View File

@ -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,

View File

@ -260,7 +260,7 @@ util::operator>> (const json::tree::node &node, util::vector<S,T> &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<T> (array[i].as_number ().native ());
v.data[i] = array[i].as<T> ();
return node;
}

View File

@ -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;