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

Fix conversion and other pedantic warnings #983

Closed
wants to merge 7 commits into from
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
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
-Wcast-align -Wnon-virtual-dtor
-Wctor-dtor-privacy -Wdisabled-optimization
-Winvalid-pch -Woverloaded-virtual
-Wconversion
-Wno-ctor-dtor-privacy -Wno-dangling-else
-Wno-format-nonliteral -Wno-sign-conversion -Wno-shadow)
-Wno-format-nonliteral -Wno-shadow -Wno-unused-local-typedefs)
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wnoexcept)
endif ()
Expand All @@ -92,7 +93,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic)
set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wno-sign-conversion)
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
4 changes: 2 additions & 2 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ struct chrono_formatter {
void write(int value, int width) {
typedef typename int_traits<int>::main_type main_type;
main_type n = to_unsigned(value);
int num_digits = static_cast<int>(internal::count_digits(n));
int num_digits = internal::count_digits(n);
if (width > num_digits)
out = std::fill_n(out, width - num_digits, '0');
out = format_decimal<char_type>(out, n, num_digits);
Expand Down Expand Up @@ -377,7 +377,7 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
-> decltype(ctx.begin()) {
auto begin = ctx.begin(), end = ctx.end();
end = parse_chrono_format(begin, end, internal::chrono_format_checker());
format_str = basic_string_view<Char>(&*begin, end - begin);
format_str = basic_string_view<Char>(&*begin, static_cast<size_t>(end - begin));
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved
return end;
}

Expand Down
16 changes: 8 additions & 8 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ struct color_type {
}
FMT_CONSTEXPR color_type(rgb rgb_color) FMT_NOEXCEPT
: is_rgb(true), value{} {
value.rgb_color = (rgb_color.r << 16) + (rgb_color.g << 8) + rgb_color.b;
value.rgb_color = (static_cast<uint32_t>(rgb_color.r) << 16) | (static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b;
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved
}
FMT_CONSTEXPR color_type(terminal_color term_color) FMT_NOEXCEPT
: is_rgb(), value{} {
Expand Down Expand Up @@ -395,22 +395,22 @@ struct ansi_color_escape {
// sequence.
if (!text_color.is_rgb) {
bool is_background = esc == internal::data::BACKGROUND_COLOR;
uint8_t value = text_color.value.term_color;
uint32_t value = text_color.value.term_color;
// Background ASCII codes are the same as the foreground ones but with
// 10 more.
if (is_background)
value += 10;
value += 10u;

std::size_t index = 0;
buffer[index++] = static_cast<Char>('\x1b');
buffer[index++] = static_cast<Char>('[');

if (value >= 100) {
if (value >= 100u) {
buffer[index++] = static_cast<Char>('1');
value %= 100;
value %= 100u;
}
buffer[index++] = static_cast<Char>('0' + value / 10);
buffer[index++] = static_cast<Char>('0' + value % 10);
buffer[index++] = static_cast<Char>('0' + value / 10u);
buffer[index++] = static_cast<Char>('0' + value % 10u);

buffer[index++] = static_cast<Char>('m');
buffer[index++] = static_cast<Char>('\0');
Expand Down Expand Up @@ -452,7 +452,7 @@ struct ansi_color_escape {
FMT_CONSTEXPR operator const Char *() const FMT_NOEXCEPT { return buffer; }

private:
Char buffer[7 + 3 * 4 + 1];
Char buffer[7u + 3u * 4u + 1u];

static FMT_CONSTEXPR void to_esc(uint8_t c, Char *out,
char delimiter) FMT_NOEXCEPT {
Expand Down
6 changes: 4 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class basic_buffer {

protected:
// Don't initialize ptr_ since it is not accessed to save a few cycles.
basic_buffer(std::size_t sz) FMT_NOEXCEPT: size_(sz), capacity_(sz) {}
basic_buffer(std::size_t sz) FMT_NOEXCEPT: ptr_(FMT_NULL), size_(sz), capacity_(sz) {}
Copy link
Contributor

@vitaut vitaut Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ptr_ is not initialized intentionally here. This has measurable performance impact on some benchmarks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - as long as the header does not produce warnings when included in user-level code.


basic_buffer(T *p = FMT_NULL, std::size_t sz = 0, std::size_t cap = 0)
FMT_NOEXCEPT: ptr_(p), size_(sz), capacity_(cap) {}
Expand Down Expand Up @@ -1328,7 +1328,9 @@ struct named_arg_base {
mutable char data[
sizeof(basic_format_arg<typename buffer_context<Char>::type>)];

named_arg_base(basic_string_view<Char> nm) : name(nm) {}
named_arg_base(basic_string_view<Char> nm) : name(nm) {
std::memset(data, 0, sizeof(data));
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved
}

template <typename Context>
basic_format_arg<Context> deserialize() const {
Expand Down
20 changes: 10 additions & 10 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void format_error_code(internal::buffer &out, int error_code,
abs_value = 0 - abs_value;
++error_code_size;
}
error_code_size += internal::count_digits(abs_value);
error_code_size += static_cast<size_t>(internal::count_digits(abs_value));
0x8000-0000 marked this conversation as resolved.
Show resolved Hide resolved
writer w(out);
if (message.size() <= inline_buffer_size - error_code_size) {
w.write(message);
Expand Down Expand Up @@ -464,14 +464,14 @@ FMT_FUNC fp get_cached_power(int min_exponent, int &pow10_exponent) {
}

FMT_FUNC bool grisu2_round(
char *buf, ptrdiff_t &size, size_t max_digits, uint64_t delta,
char *buf, int &size, int max_digits, uint64_t delta,
uint64_t remainder, uint64_t exp, uint64_t diff, int &exp10) {
while (remainder < diff && delta - remainder >= exp &&
(remainder + exp < diff || diff - remainder > remainder + exp - diff)) {
--buf[size - 1];
remainder += exp;
}
if (size > static_cast<ptrdiff_t>(max_digits)) {
if (size > max_digits) {
--size;
++exp10;
if (buf[size] >= '5')
Expand All @@ -482,8 +482,8 @@ FMT_FUNC bool grisu2_round(

// Generates output using Grisu2 digit-gen algorithm.
FMT_FUNC bool grisu2_gen_digits(
char *buf, ptrdiff_t &size, uint32_t hi, uint64_t lo, int &exp,
uint64_t delta, const fp &one, const fp &diff, size_t max_digits) {
char *buf, int &size, uint32_t hi, uint64_t lo, int &exp,
uint64_t delta, const fp &one, const fp &diff, int max_digits) {
// Generate digits for the most significant part (hi).
while (exp > 0) {
uint32_t digit = 0;
Expand All @@ -507,7 +507,7 @@ FMT_FUNC bool grisu2_gen_digits(
buf[size++] = static_cast<char>('0' + digit);
--exp;
uint64_t remainder = (static_cast<uint64_t>(hi) << -one.e) + lo;
if (remainder <= delta || size > static_cast<ptrdiff_t>(max_digits)) {
if (remainder <= delta || size > max_digits) {
return grisu2_round(
buf, size, max_digits, delta, remainder,
static_cast<uint64_t>(data::POWERS_OF_10_32[exp]) << -one.e,
Expand All @@ -523,7 +523,7 @@ FMT_FUNC bool grisu2_gen_digits(
buf[size++] = static_cast<char>('0' + digit);
lo &= one.f - 1;
--exp;
if (lo < delta || size > static_cast<ptrdiff_t>(max_digits)) {
if (lo < delta || size > max_digits) {
return grisu2_round(buf, size, max_digits, delta, lo, one.f,
diff.f * data::POWERS_OF_10_32[-exp], exp);
}
Expand Down Expand Up @@ -704,7 +704,7 @@ FMT_FUNC gen_digits_params process_specs(const core_format_specs &specs,
++num_digits;
break;
}
params.num_digits = to_unsigned(num_digits);
params.num_digits = num_digits;
char_counter counter{num_digits};
grisu2_prettify(params, params.num_digits, exp - num_digits, counter);
buf.resize(to_unsigned(counter.size));
Expand Down Expand Up @@ -739,7 +739,7 @@ FMT_FUNC typename std::enable_if<sizeof(Double) == sizeof(uint64_t), bool>::type
// hi (p1 in Grisu) contains the most significant digits of scaled_upper.
// hi = floor(upper / one).
uint32_t hi = static_cast<uint32_t>(upper.f >> -one.e);
int exp = static_cast<int>(count_digits(hi)); // kappa in Grisu.
int exp = count_digits(hi); // kappa in Grisu.
gen_digits_params params = process_specs(specs, cached_exp + exp, buf);
fp_value.normalize();
fp scaled_value = fp_value * cached_pow;
Expand All @@ -750,7 +750,7 @@ FMT_FUNC typename std::enable_if<sizeof(Double) == sizeof(uint64_t), bool>::type
// lo (p2 in Grisu) contains the least significants digits of scaled_upper.
// lo = supper % one.
uint64_t lo = upper.f & (one.f - 1);
ptrdiff_t size = 0;
int size = 0;
if (!grisu2_gen_digits(buf.data(), size, hi, lo, exp, delta, one, diff,
params.num_digits)) {
buf.clear();
Expand Down
Loading