Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean-up sign-conversion warnings #1435

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion
-Wno-sign-conversion -Wdeprecated -Wweak-vtables)
-Wsign-conversion -Wdeprecated -Wweak-vtables)
check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING)
if (HAS_NULLPTR_WARNING)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
Expand Down
19 changes: 12 additions & 7 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ inline int to_nonnegative_int(T value, int upper) {

template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline T mod(T x, int y) {
return x % y;
return x % static_cast<T>(y);
}
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
inline T mod(T x, int y) {
Expand Down Expand Up @@ -793,11 +793,16 @@ struct chrono_formatter {

explicit chrono_formatter(FormatContext& ctx, OutputIt o,
std::chrono::duration<Rep, Period> d)
: context(ctx), out(o), val(d.count()), negative(false) {
if (d.count() < 0) {
val = 0 - val;
: context(ctx), out(o), val(static_cast<rep>(std::abs(d.count()))), negative(d.count() < 0) {
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved

if (d.count() < 0) {
val = static_cast<rep>(- d.count());
negative = true;
}
}
else {
val = static_cast<rep>(d.count());
negative = false;
}

// this may overflow and/or the result may not fit in the
// target type.
Expand Down Expand Up @@ -1023,8 +1028,8 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
void on_error(const char* msg) { FMT_THROW(format_error(msg)); }
void on_fill(Char fill) { f.specs.fill[0] = fill; }
void on_align(align_t align) { f.specs.align = align; }
void on_width(unsigned width) { f.specs.width = width; }
void on_precision(unsigned _precision) { f.precision = _precision; }
void on_width(int width) { f.specs.width = width; }
void on_precision(int _precision) { f.precision = _precision; }
void end_precision() {}

template <typename Id> void on_dynamic_width(Id arg_id) {
Expand Down
12 changes: 5 additions & 7 deletions include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ template <typename Char> struct format_part {

kind part_kind;
union value {
unsigned arg_index;
int arg_index;
basic_string_view<Char> str;
replacement repl;

FMT_CONSTEXPR value(unsigned index = 0) : arg_index(index) {}
FMT_CONSTEXPR value(int index = 0) : arg_index(index) {}
FMT_CONSTEXPR value(basic_string_view<Char> s) : str(s) {}
FMT_CONSTEXPR value(replacement r) : repl(r) {}
} val;
Expand All @@ -40,7 +40,7 @@ template <typename Char> struct format_part {
FMT_CONSTEXPR format_part(kind k = kind::arg_index, value v = {})
: part_kind(k), val(v) {}

static FMT_CONSTEXPR format_part make_arg_index(unsigned index) {
static FMT_CONSTEXPR format_part make_arg_index(int index) {
return format_part(kind::arg_index, index);
}
static FMT_CONSTEXPR format_part make_arg_name(basic_string_view<Char> name) {
Expand All @@ -62,7 +62,7 @@ template <typename Char> struct part_counter {
}

FMT_CONSTEXPR void on_arg_id() { ++num_parts; }
FMT_CONSTEXPR void on_arg_id(unsigned) { ++num_parts; }
FMT_CONSTEXPR void on_arg_id(int) { ++num_parts; }
FMT_CONSTEXPR void on_arg_id(basic_string_view<Char>) { ++num_parts; }

FMT_CONSTEXPR void on_replacement_field(const Char*) {}
Expand Down Expand Up @@ -119,7 +119,7 @@ class format_string_compiler : public error_handler {
part_ = part::make_arg_index(parse_context_.next_arg_id());
}

FMT_CONSTEXPR void on_arg_id(unsigned id) {
FMT_CONSTEXPR void on_arg_id(int id) {
parse_context_.check_arg_id(id);
part_ = part::make_arg_index(id);
}
Expand Down Expand Up @@ -512,8 +512,6 @@ template <typename CompiledFormat, typename... Args,
CompiledFormat>::value)>
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
basic_memory_buffer<Char> buffer;
using range = buffer_range<Char>;
using context = buffer_context<Char>;
cf.format(std::back_inserter(buffer), args...);
return to_string(buffer);
}
Expand Down
77 changes: 38 additions & 39 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstdarg>
#include <cstring> // for std::memmove
#include <cwchar>
#include <memory> // for std::uninitialized_fill_n
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
# include <locale>
#endif
Expand Down Expand Up @@ -481,9 +482,8 @@ inline fp operator*(fp x, fp y) { return {multiply(x.f, y.f), x.e + y.e + 64}; }
FMT_FUNC fp get_cached_power(int min_exponent, int& pow10_exponent) {
const uint64_t one_over_log2_10 = 0x4d104d42; // round(pow(2, 32) / log2(10))
int index = static_cast<int>(
static_cast<int64_t>(
(min_exponent + fp::significand_size - 1) * one_over_log2_10 +
((uint64_t(1) << 32) - 1) // ceil
(static_cast<uint64_t>(min_exponent + fp::significand_size - 1) * one_over_log2_10 +
((1ULL << 32) - 1) // ceil
) >>
32 // arithmetic shift
);
Expand Down Expand Up @@ -531,14 +531,14 @@ class bigint {

friend struct formatter<bigint>;

void subtract_bigits(int index, bigit other, bigit& borrow) {
void subtract_bigits(size_t index, bigit other, bigit& borrow) {
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved
auto result = static_cast<double_bigit>(bigits_[index]) - other - borrow;
bigits_[index] = static_cast<bigit>(result);
borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
}

void remove_leading_zeros() {
int num_bigits = static_cast<int>(bigits_.size()) - 1;
size_t num_bigits = bigits_.size() - 1;
while (num_bigits > 0 && bigits_[num_bigits] == 0) --num_bigits;
bigits_.resize(num_bigits + 1);
}
Expand All @@ -548,8 +548,8 @@ class bigint {
FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
FMT_ASSERT(compare(*this, other) >= 0, "");
bigit borrow = 0;
int i = other.exp_ - exp_;
for (int j = 0, n = static_cast<int>(other.bigits_.size()); j != n;
size_t i = static_cast<size_t>(other.exp_ - exp_);
for (size_t j = 0, n = other.bigits_.size(); j != n;
++i, ++j) {
subtract_bigits(i, other.bigits_[j], borrow);
}
Expand Down Expand Up @@ -601,7 +601,7 @@ class bigint {
}

void assign(uint64_t n) {
int num_bigits = 0;
size_t num_bigits = 0;
do {
bigits_[num_bigits++] = n & ~bigit(0);
n >>= bigit_bits;
Expand Down Expand Up @@ -637,11 +637,11 @@ class bigint {
int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits();
if (num_lhs_bigits != num_rhs_bigits)
return num_lhs_bigits > num_rhs_bigits ? 1 : -1;
int i = static_cast<int>(lhs.bigits_.size()) - 1;
int j = static_cast<int>(rhs.bigits_.size()) - 1;
int end = i - j;
if (end < 0) end = 0;
for (; i >= end; --i, --j) {
size_t i = lhs.bigits_.size();
size_t j = rhs.bigits_.size();
while ((i != 0) && (j != 0)) {
-- i;
-- j;
bigit lhs_bigit = lhs.bigits_[i], rhs_bigit = rhs.bigits_[j];
if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
}
Expand All @@ -657,7 +657,7 @@ class bigint {
if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
if (max_lhs_bigits > num_rhs_bigits) return 1;
auto get_bigit = [](const bigint& n, int i) -> bigit {
return i >= n.exp_ && i < n.num_bigits() ? n.bigits_[i - n.exp_] : 0;
return i >= n.exp_ && i < n.num_bigits() ? n.bigits_[static_cast<size_t>(i - n.exp_)] : 0;
};
double_bigit borrow = 0;
int min_exp = (std::min)((std::min)(lhs1.exp_, lhs2.exp_), rhs.exp_);
Expand Down Expand Up @@ -695,25 +695,25 @@ class bigint {

void square() {
basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
int num_bigits = static_cast<int>(bigits_.size());
int num_result_bigits = 2 * num_bigits;
size_t num_bigits = bigits_.size();
size_t num_result_bigits = 2 * num_bigits;
bigits_.resize(num_result_bigits);
using accumulator_t = conditional_t<FMT_USE_INT128, uint128_t, accumulator>;
auto sum = accumulator_t();
for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
for (size_t bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
// Compute bigit at position bigit_index of the result by adding
// cross-product terms n[i] * n[j] such that i + j == bigit_index.
for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
for (size_t i = 0; i <= bigit_index; ++ i) {
// Most terms are multiplied twice which can be optimized in the future.
sum += static_cast<double_bigit>(n[i]) * n[j];
sum += static_cast<double_bigit>(n[i]) * n[bigit_index - i];
}
bigits_[bigit_index] = static_cast<bigit>(sum);
sum >>= bits<bigit>::value; // Compute the carry.
}
// Do the same for the top half.
for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
for (size_t bigit_index = num_bigits; bigit_index < num_result_bigits;
++bigit_index) {
for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
for (size_t j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
sum += static_cast<double_bigit>(n[i++]) * n[j--];
bigits_[bigit_index] = static_cast<bigit>(sum);
sum >>= bits<bigit>::value;
Expand All @@ -728,16 +728,15 @@ class bigint {
int divmod_assign(const bigint& divisor) {
FMT_ASSERT(this != &divisor, "");
if (compare(*this, divisor) < 0) return 0;
int num_bigits = static_cast<int>(bigits_.size());
size_t num_bigits = bigits_.size();
FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1] != 0, "");
int exp_difference = exp_ - divisor.exp_;
if (exp_difference > 0) {
if (exp_ > divisor.exp_) {
const auto exp_difference = static_cast<size_t>(exp_ - divisor.exp_);
// Align bigints by adding trailing zeros to simplify subtraction.
bigits_.resize(num_bigits + exp_difference);
for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
bigits_[j] = bigits_[i];
std::memmove(&bigits_[exp_difference], &bigits_[0], num_bigits * sizeof(bigits_[0]));
std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
exp_ -= exp_difference;
exp_ = divisor.exp_;
}
int quotient = 0;
do {
Expand Down Expand Up @@ -1012,7 +1011,7 @@ void fallback_format(Double d, buffer<char>& buf, int& exp10) {
if (!upper) upper = &lower;
// Invariant: value == (numerator / denominator) * pow(10, exp10).
bool even = (value.f & 1) == 0;
int num_digits = 0;
size_t num_digits = 0;
char* data = buf.data();
for (;;) {
int digit = numerator.divmod_assign(denominator);
Expand All @@ -1030,7 +1029,7 @@ void fallback_format(Double d, buffer<char>& buf, int& exp10) {
++data[num_digits - 1];
}
buf.resize(num_digits);
exp10 -= num_digits - 1;
exp10 -= static_cast<int>(num_digits - 1);
return;
}
numerator *= 10;
Expand Down Expand Up @@ -1072,15 +1071,15 @@ int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
fixed_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
return snprintf_float(value, precision, specs, buf);
int num_digits = handler.size;
auto num_digits = static_cast<size_t>(handler.size);
if (!fixed) {
// Remove trailing zeros.
while (num_digits > 0 && buf[num_digits - 1] == '0') {
--num_digits;
++exp;
}
}
buf.resize(to_unsigned(num_digits));
buf.resize(num_digits);
} else {
fp fp_value;
auto boundaries = specs.binary32
Expand Down Expand Up @@ -1179,10 +1178,10 @@ int snprintf_float(T value, int precision, float_specs specs,
do {
--p;
} while (is_digit(*p));
int fraction_size = static_cast<int>(end - p - 1);
std::memmove(p, p + 1, fraction_size);
auto fraction_size = end - p - 1;
std::memmove(p, p + 1, static_cast<size_t>(fraction_size));
buf.resize(size - 1);
return -fraction_size;
return static_cast<int>(-fraction_size);
}
if (specs.format == float_format::hex) {
buf.resize(size + offset);
Expand All @@ -1195,24 +1194,24 @@ int snprintf_float(T value, int precision, float_specs specs,
} while (*exp_pos != 'e');
char sign = exp_pos[1];
assert(sign == '+' || sign == '-');
int exp = 0;
size_t exp = 0;
auto p = exp_pos + 2; // Skip 'e' and sign.
do {
assert(is_digit(*p));
exp = exp * 10 + (*p++ - '0');
exp = exp * 10 + static_cast<size_t>(*p++ - '0');
} while (p != end);
if (sign == '-') exp = -exp;
int fraction_size = 0;
size_t fraction_size = 0;
if (exp_pos != begin + 1) {
// Remove trailing zeros.
auto fraction_end = exp_pos - 1;
while (*fraction_end == '0') --fraction_end;
// Move the fractional part left to get rid of the decimal point.
fraction_size = static_cast<int>(fraction_end - begin - 1);
fraction_size = static_cast<size_t>(fraction_end - begin - 1);
std::memmove(begin + 1, begin + 2, fraction_size);
}
buf.resize(fraction_size + offset + 1);
return exp - fraction_size;
return static_cast<int>(exp - fraction_size);
}
}
} // namespace internal
Expand Down
6 changes: 4 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2620,7 +2620,7 @@ class format_string_checker {
public:
explicit FMT_CONSTEXPR format_string_checker(
basic_string_view<Char> format_str, ErrorHandler eh)
: arg_id_(max_value<unsigned>()),
: arg_id_(ARG_ID_SENTINEL),
context_(format_str, eh),
parse_funcs_{&parse_format_specs<Args, parse_context_type>...} {}

Expand Down Expand Up @@ -2661,7 +2661,9 @@ class format_string_checker {
// Format specifier parsing function.
using parse_func = const Char* (*)(parse_context_type&);

unsigned arg_id_;
enum { ARG_ID_SENTINEL = -1 };

int arg_id_;
parse_context_type context_;
parse_func parse_funcs_[num_args > 0 ? num_args : 1];
};
Expand Down
Loading