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 (rebased) #1440

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 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,14 @@ 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;
negative = true;
}
: context(ctx), out(o), negative(d.count() < 0) {

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

// this may overflow and/or the result may not fit in the
// target type.
Expand Down Expand Up @@ -1023,8 +1026,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
4 changes: 2 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2621,7 +2621,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_(-1),
context_(format_str, eh),
parse_funcs_{&parse_format_specs<Args, parse_context_type>...} {}

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

unsigned arg_id_;
int arg_id_;
parse_context_type context_;
parse_func parse_funcs_[num_args > 0 ? num_args : 1];
};
Expand Down
32 changes: 19 additions & 13 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ internal::truncating_iterator<OutputIt> printf(
basic_format_args<Context> args) {
return Context(it, format, args).format();
}

enum
{
ARG_INDEX_SENTINEL = -1
};
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved

} // namespace internal

using internal::printf; // For printing into memory_buffer.
Expand Down Expand Up @@ -335,10 +341,10 @@ template <typename OutputIt, typename Char> class basic_printf_context {

// Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument.
format_arg get_arg(unsigned arg_index = internal::max_value<unsigned>());
format_arg get_arg(int arg_index = internal::ARG_INDEX_SENTINEL);

// Parses argument index, flags and width and returns the argument index.
unsigned parse_header(const Char*& it, const Char* end, format_specs& specs);
int parse_header(const Char*& it, const Char* end, format_specs& specs);

public:
/**
Expand All @@ -355,7 +361,7 @@ template <typename OutputIt, typename Char> class basic_printf_context {
OutputIt out() { return out_; }
void advance_to(OutputIt it) { out_ = it; }

format_arg arg(unsigned id) const { return args_.get(id); }
format_arg arg(int id) const { return args_.get(id); }

basic_format_parse_context<Char>& parse_context() { return parse_ctx_; }

Expand Down Expand Up @@ -397,24 +403,24 @@ void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,

template <typename OutputIt, typename Char>
typename basic_printf_context<OutputIt, Char>::format_arg
basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
if (arg_index == internal::max_value<unsigned>())
basic_printf_context<OutputIt, Char>::get_arg(int arg_index) {
if (arg_index == internal::ARG_INDEX_SENTINEL)
arg_index = parse_ctx_.next_arg_id();
else
parse_ctx_.check_arg_id(--arg_index);
return internal::get_arg(*this, arg_index);
}

template <typename OutputIt, typename Char>
unsigned basic_printf_context<OutputIt, Char>::parse_header(
int basic_printf_context<OutputIt, Char>::parse_header(
const Char*& it, const Char* end, format_specs& specs) {
unsigned arg_index = internal::max_value<unsigned>();
int arg_index = internal::ARG_INDEX_SENTINEL;
char_type c = *it;
if (c >= '0' && c <= '9') {
// Parse an argument index (if followed by '$') or a width possibly
// preceded with '0' flag(s).
internal::error_handler eh;
unsigned value = parse_nonnegative_int(it, end, eh);
int value = parse_nonnegative_int(it, end, eh);
if (it != end && *it == '$') { // value is an argument index
++it;
arg_index = value;
Expand All @@ -436,8 +442,8 @@ unsigned basic_printf_context<OutputIt, Char>::parse_header(
specs.width = parse_nonnegative_int(it, end, eh);
} else if (*it == '*') {
++it;
specs.width = visit_format_arg(
internal::printf_width_handler<char_type>(specs), get_arg());
specs.width = static_cast<int>(visit_format_arg(
internal::printf_width_handler<char_type>(specs), get_arg()));
}
}
return arg_index;
Expand All @@ -464,7 +470,7 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
specs.align = align::right;

// Parse argument index, flags and width.
unsigned arg_index = parse_header(it, end, specs);
int arg_index = parse_header(it, end, specs);
if (arg_index == 0) on_error("argument index out of range");

// Parse precision.
Expand All @@ -473,11 +479,11 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
c = it != end ? *it : 0;
if ('0' <= c && c <= '9') {
internal::error_handler eh;
specs.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
specs.precision = parse_nonnegative_int(it, end, eh);
} else if (c == '*') {
++it;
specs.precision =
visit_format_arg(internal::printf_precision_handler(), get_arg());
static_cast<int>(visit_format_arg(internal::printf_precision_handler(), get_arg()));
} else {
specs.precision = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/format-impl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ TEST(FPTest, GetRoundDirection) {
EXPECT_EQ(fmt::internal::up, get_round_direction(100, 51, 0));
EXPECT_EQ(fmt::internal::down, get_round_direction(100, 40, 10));
EXPECT_EQ(fmt::internal::up, get_round_direction(100, 60, 10));
for (int i = 41; i < 60; ++i)
for (size_t i = 41; i < 60; ++i)
EXPECT_EQ(fmt::internal::unknown, get_round_direction(100, i, 10));
uint64_t max = max_value<uint64_t>();
EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure);
Expand Down
20 changes: 10 additions & 10 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ TEST(FormatTest, Dynamic) {

std::string result = fmt::vformat(
"{} and {} and {}", fmt::basic_format_args<ctx>(
args.data(), static_cast<unsigned>(args.size())));
args.data(), static_cast<int>(args.size())));

EXPECT_EQ("42 and abc1 and 1.5", result);
}
Expand Down Expand Up @@ -2167,12 +2167,12 @@ TEST(FormatTest, WideFormatToN) {
struct test_arg_id_handler {
enum result { NONE, EMPTY, INDEX, NAME, ERROR };
result res = NONE;
unsigned index = 0;
int index = 0;
string_view name;

FMT_CONSTEXPR void operator()() { res = EMPTY; }

FMT_CONSTEXPR void operator()(unsigned i) {
FMT_CONSTEXPR void operator()(int i) {
res = INDEX;
index = i;
}
Expand Down Expand Up @@ -2208,9 +2208,9 @@ struct test_format_specs_handler {

fmt::align_t align = fmt::align::none;
char fill = 0;
unsigned width = 0;
int width = 0;
fmt::internal::arg_ref<char> width_ref;
unsigned precision = 0;
int precision = 0;
fmt::internal::arg_ref<char> precision_ref;
char type = 0;

Expand All @@ -2236,14 +2236,14 @@ struct test_format_specs_handler {
FMT_CONSTEXPR void on_hash() { res = HASH; }
FMT_CONSTEXPR void on_zero() { res = ZERO; }

FMT_CONSTEXPR void on_width(unsigned w) { width = w; }
FMT_CONSTEXPR void on_width(int w) { width = w; }
FMT_CONSTEXPR void on_dynamic_width(fmt::internal::auto_id) {}
FMT_CONSTEXPR void on_dynamic_width(unsigned index) { width_ref = index; }
FMT_CONSTEXPR void on_dynamic_width(int index) { width_ref = index; }
FMT_CONSTEXPR void on_dynamic_width(string_view) {}

FMT_CONSTEXPR void on_precision(unsigned p) { precision = p; }
FMT_CONSTEXPR void on_precision(int p) { precision = p; }
FMT_CONSTEXPR void on_dynamic_precision(fmt::internal::auto_id) {}
FMT_CONSTEXPR void on_dynamic_precision(unsigned index) {
FMT_CONSTEXPR void on_dynamic_precision(int index) {
precision_ref = index;
}
FMT_CONSTEXPR void on_dynamic_precision(string_view) {}
Expand Down Expand Up @@ -2280,7 +2280,7 @@ TEST(FormatTest, ConstexprParseFormatSpecs) {
struct test_parse_context {
typedef char char_type;

FMT_CONSTEXPR unsigned next_arg_id() { return 11; }
FMT_CONSTEXPR int next_arg_id() { return 11; }
template <typename Id> FMT_CONSTEXPR void check_arg_id(Id) {}

FMT_CONSTEXPR const char* begin() { return nullptr; }
Expand Down
14 changes: 8 additions & 6 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
//
// For the license information refer to format.h.

#include "fmt/printf.h"

#include <cctype>
#include <climits>
#include <cstring>

#include "fmt/core.h"
#include "fmt/printf.h"
#include "gtest-extra.h"
#include "util.h"

Expand Down Expand Up @@ -300,15 +301,15 @@ void TestLength(const char* length_spec, U value) {
using fmt::internal::const_check;
if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
signed_value = static_cast<int>(value);
unsigned_value = static_cast<unsigned>(value);
unsigned_value = static_cast<unsigned long long>(value);
} else if (const_check(max <= max_value<unsigned>())) {
signed_value = static_cast<unsigned>(value);
unsigned_value = static_cast<unsigned>(value);
unsigned_value = static_cast<unsigned long long>(value);
}
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
signed_value = static_cast<long long>(value);
unsigned_value =
static_cast<typename std::make_unsigned<unsigned>::type>(value);
unsigned_value = static_cast<unsigned long long>(
static_cast<typename std::make_unsigned<unsigned>::type>(value));
} else {
signed_value = static_cast<typename make_signed<T>::type>(value);
unsigned_value = static_cast<typename std::make_unsigned<T>::type>(value);
Expand Down Expand Up @@ -589,7 +590,8 @@ class custom_printf_arg_formatter : public formatter_t {
if (round(value * pow(10, specs()->precision)) == 0.0) value = 0;
return formatter_t::operator()(value);
}
};
}
;

typedef fmt::basic_format_args<context_t> format_args_t;

Expand Down
15 changes: 8 additions & 7 deletions test/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// For the license information refer to format.h.

#include <array>
#include <cassert>
#include <climits>

#include "fmt/format.h"

Expand Down Expand Up @@ -135,21 +137,20 @@ struct scan_handler : error_handler {
char c = *it++;
if (c < '0' || c > '9') on_error("invalid input");
// TODO: check overflow
value = value * 10 + (c - '0');
value = value * 10 + static_cast<unsigned>(c - '0');
}
scan_ctx_.advance_to(it);
return value;
}

template <typename T = int> T read_int() {
T value = 0;
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
bool negative = it != end && *it == '-';
if (negative) ++it;
scan_ctx_.advance_to(it);
value = read_uint<typename std::make_unsigned<T>::type>();
if (negative) value = -value;
return value;
const auto value = read_uint<typename std::make_unsigned<T>::type>();
if (negative) return -static_cast<T>(value);
return static_cast<T>(value);
}

public:
Expand All @@ -159,7 +160,7 @@ struct scan_handler : error_handler {
const char* pos() const { return scan_ctx_.begin(); }

void on_text(const char* begin, const char* end) {
auto size = end - begin;
auto size = to_unsigned(end - begin);
auto it = scan_ctx_.begin();
if (it + size > scan_ctx_.end() ||
!std::equal(begin, end, make_checked(it, size))) {
Expand Down Expand Up @@ -197,7 +198,7 @@ struct scan_handler : error_handler {
case scan_type::string_view_type: {
auto s = it;
while (it != end && *it != ' ') ++it;
*arg_.string_view = fmt::string_view(s, it - s);
*arg_.string_view = fmt::string_view(s, to_unsigned(it - s));
scan_ctx_.advance_to(it);
break;
}
Expand Down