From 3ecad55910a6da4be3555394349259b4ea91bab6 Mon Sep 17 00:00:00 2001 From: vitaut Date: Wed, 2 Mar 2016 07:53:14 -0800 Subject: [PATCH] Fix sign conversion warnings --- cppformat/format.cc | 22 +++++++++++++++++++--- cppformat/format.h | 4 ++-- cppformat/posix.cc | 4 ++-- cppformat/posix.h | 3 ++- test/gtest-extra-test.cc | 2 +- test/gtest-extra.cc | 7 +++---- test/posix-mock-test.cc | 7 +++---- test/posix-test.cc | 8 ++++---- test/printf-test.cc | 10 +++++----- test/util-test.cc | 5 +++-- 10 files changed, 44 insertions(+), 28 deletions(-) diff --git a/cppformat/format.cc b/cppformat/format.cc index 908a5156..f30d5f2d 100644 --- a/cppformat/format.cc +++ b/cppformat/format.cc @@ -362,6 +362,21 @@ class CharConverter : public fmt::internal::ArgVisitor { arg_.int_value = static_cast(value); } }; + +// Write the content of w to os. +void write(std::ostream &os, fmt::MemoryWriter &w) { + const char *data = w.data(); + std::size_t size = w.size(); + typedef internal::MakeUnsigned::Type UnsignedStreamSize; + UnsignedStreamSize max_size = + internal::to_unsigned((std::numeric_limits::max)()); + do { + UnsignedStreamSize n = size <= max_size ? size : max_size; + os.write(data, static_cast(n)); + data += n; + size -= n; + } while (size != 0); +} } // namespace namespace internal { @@ -884,10 +899,11 @@ FMT_FUNC void fmt::print(CStringRef format_str, ArgList args) { print(stdout, format_str, args); } -FMT_FUNC void fmt::print(std::ostream &os, CStringRef format_str, ArgList args) { +FMT_FUNC void fmt::print(std::ostream &os, CStringRef format_str, + ArgList args) { MemoryWriter w; w.write(format_str, args); - os.write(w.data(), w.size()); + write(os, w); } FMT_FUNC void fmt::print_colored(Color c, CStringRef format, ArgList args) { @@ -908,7 +924,7 @@ FMT_FUNC int fmt::fprintf(std::FILE *f, CStringRef format, ArgList args) { FMT_FUNC int fmt::fprintf(std::ostream &os, CStringRef format, ArgList args) { MemoryWriter w; printf(w, format, args); - os.write(w.data(), w.size()); + write(os, w); return static_cast(w.size()); } diff --git a/cppformat/format.h b/cppformat/format.h index 22be015b..cc60d52c 100644 --- a/cppformat/format.h +++ b/cppformat/format.h @@ -2021,7 +2021,7 @@ class FormatBuf : public std::basic_streambuf { int_type overflow(int_type ch = traits_type::eof()) { if (!traits_type::eq_int_type(ch, traits_type::eof())) { - size_t size = this->pptr() - start_; + size_t size = this->size(); buffer_.resize(size); buffer_.reserve(size * 2); @@ -2033,7 +2033,7 @@ class FormatBuf : public std::basic_streambuf { } size_t size() const { - return this->pptr() - start_; + return to_unsigned(this->pptr() - start_); } }; } // namespace internal diff --git a/cppformat/posix.cc b/cppformat/posix.cc index 756281a0..c6c2ae2c 100644 --- a/cppformat/posix.cc +++ b/cppformat/posix.cc @@ -173,7 +173,7 @@ std::size_t fmt::File::read(void *buffer, std::size_t count) { FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count)))); if (result < 0) throw SystemError(errno, "cannot read from file"); - return result; + return internal::to_unsigned(result); } std::size_t fmt::File::write(const void *buffer, std::size_t count) { @@ -181,7 +181,7 @@ std::size_t fmt::File::write(const void *buffer, std::size_t count) { FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count)))); if (result < 0) throw SystemError(errno, "cannot write to file"); - return result; + return internal::to_unsigned(result); } fmt::File fmt::File::dup(int fd) { diff --git a/cppformat/posix.h b/cppformat/posix.h index 0c8a89f0..bfbd3851 100644 --- a/cppformat/posix.h +++ b/cppformat/posix.h @@ -305,7 +305,8 @@ class File { // Closes the file. void close(); - // Returns the file size. + // Returns the file size. The size has signed type for consistency with + // stat::st_size. LongLong size() const; // Attempts to read count bytes from the file into the specified buffer. diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index d1c3ccdf..1990f311 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -46,7 +46,7 @@ namespace { std::string sanitize(const std::string &s) { std::string result; for (std::string::const_iterator i = s.begin(), end = s.end(); i != end; ++i) - result.push_back(*i & 0xff); + result.push_back(*i & static_cast(0xff)); return result; } diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index c8c4ac54..f7c29630 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -80,10 +80,10 @@ std::string OutputRedirect::restore_and_read() { return content; // Already read. enum { BUFFER_SIZE = 4096 }; char buffer[BUFFER_SIZE]; - std::streamsize count = 0; + std::size_t count = 0; do { count = read_end_.read(buffer, BUFFER_SIZE); - content.append(buffer, static_cast(count)); + content.append(buffer, count); } while (count != 0); read_end_.close(); return content; @@ -91,8 +91,7 @@ std::string OutputRedirect::restore_and_read() { std::string read(File &f, std::size_t count) { std::string buffer(count, '\0'); - std::streamsize n = 0; - std::size_t offset = 0; + std::size_t n = 0, offset = 0; do { n = f.read(&buffer[offset], count - offset); // We can't read more than size_t bytes since count has type size_t. diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index 4550bc65..2ccd3301 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -277,8 +277,7 @@ TEST(FileTest, Size) { write_file("test", content); File f("test", File::RDONLY); EXPECT_GE(f.size(), 0); - fmt::ULongLong file_size = f.size(); - EXPECT_EQ(content.size(), file_size); + EXPECT_EQ(content.size(), static_cast(f.size())); #ifdef _WIN32 fmt::MemoryWriter message; fmt::internal::format_windows_error( @@ -308,7 +307,7 @@ TEST(FileTest, ReadRetry) { write_end.write("test", SIZE); write_end.close(); char buffer[SIZE]; - std::streamsize count = 0; + std::size_t count = 0; EXPECT_RETRY(count = read_end.read(buffer, SIZE), read, "cannot read from file"); EXPECT_EQ_POSIX(static_cast(SIZE), count); @@ -318,7 +317,7 @@ TEST(FileTest, WriteRetry) { File read_end, write_end; File::pipe(read_end, write_end); enum { SIZE = 4 }; - std::streamsize count = 0; + std::size_t count = 0; EXPECT_RETRY(count = write_end.write("test", SIZE), write, "cannot write to file"); write_end.close(); diff --git a/test/posix-test.cc b/test/posix-test.cc index f6e3b99d..7fe78e40 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -69,7 +69,7 @@ void write(File &f, fmt::StringRef s) { std::size_t num_chars_left = s.size(); const char *ptr = s.data(); do { - std::streamsize count = f.write(ptr, num_chars_left); + std::size_t count = f.write(ptr, num_chars_left); ptr += count; // We can't write more than size_t bytes since num_chars_left // has type size_t. @@ -236,20 +236,20 @@ File OpenBufferedFile(int &fd) { } TEST(FileTest, MoveFromTemporaryInCtor) { - int fd = 0xdeadbeef; + int fd = 0xdead; File f(OpenBufferedFile(fd)); EXPECT_EQ(fd, f.descriptor()); } TEST(FileTest, MoveFromTemporaryInAssignment) { - int fd = 0xdeadbeef; + int fd = 0xdead; File f; f = OpenBufferedFile(fd); EXPECT_EQ(fd, f.descriptor()); } TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) { - int fd = 0xdeadbeef; + int fd = 0xdead; File f = open_file(); int old_fd = f.descriptor(); f = OpenBufferedFile(fd); diff --git a/test/printf-test.cc b/test/printf-test.cc index 7c57136c..c0e3626e 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -292,21 +292,21 @@ SPECIALIZE_MAKE_SIGNED(fmt::ULongLong, fmt::LongLong); // Test length format specifier ``length_spec``. template void TestLength(const char *length_spec, U value) { - fmt::LongLong signed_value = value; - fmt::ULongLong unsigned_value = value; + fmt::LongLong signed_value = 0; + fmt::ULongLong unsigned_value = 0; // Apply integer promotion to the argument. fmt::ULongLong max = std::numeric_limits::max(); using fmt::internal::check; if (check(max <= static_cast(std::numeric_limits::max()))) { signed_value = static_cast(value); - unsigned_value = static_cast(value); + unsigned_value = static_cast(value); } else if (check(max <= std::numeric_limits::max())) { signed_value = static_cast(value); unsigned_value = static_cast(value); } using fmt::internal::MakeUnsigned; if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) { - signed_value = value; + signed_value = static_cast(value); unsigned_value = static_cast::Type>(value); } else { signed_value = static_cast::Type>(value); @@ -382,7 +382,7 @@ TEST(PrintfTest, Bool) { TEST(PrintfTest, Int) { EXPECT_PRINTF("-42", "%d", -42); EXPECT_PRINTF("-42", "%i", -42); - unsigned u = -42; + unsigned u = -42u; EXPECT_PRINTF(fmt::format("{}", u), "%u", -42); EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42); EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42); diff --git a/test/util-test.cc b/test/util-test.cc index 7d816e87..38b52e0f 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -332,8 +332,9 @@ TEST(MemoryBufferTest, Grow) { void grow(std::size_t size) { Base::grow(size); } } buffer((Allocator(&alloc))); buffer.resize(7); + using fmt::internal::to_unsigned; for (int i = 0; i < 7; ++i) - buffer[i] = i * i; + buffer[to_unsigned(i)] = i * i; EXPECT_EQ(10u, buffer.capacity()); int mem[20]; mem[7] = 0xdead; @@ -342,7 +343,7 @@ TEST(MemoryBufferTest, Grow) { EXPECT_EQ(20u, buffer.capacity()); // Check if size elements have been copied for (int i = 0; i < 7; ++i) - EXPECT_EQ(i * i, buffer[i]); + EXPECT_EQ(i * i, buffer[to_unsigned(i)]); // and no more than that. EXPECT_EQ(0xdead, buffer[7]); EXPECT_CALL(alloc, deallocate(mem, 20));