forked from fmtlib/fmt
Fix sign conversion warnings
This commit is contained in:
@ -362,6 +362,21 @@ class CharConverter : public fmt::internal::ArgVisitor<CharConverter, void> {
|
|||||||
arg_.int_value = static_cast<char>(value);
|
arg_.int_value = static_cast<char>(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<std::streamsize>::Type UnsignedStreamSize;
|
||||||
|
UnsignedStreamSize max_size =
|
||||||
|
internal::to_unsigned((std::numeric_limits<std::streamsize>::max)());
|
||||||
|
do {
|
||||||
|
UnsignedStreamSize n = size <= max_size ? size : max_size;
|
||||||
|
os.write(data, static_cast<std::streamsize>(n));
|
||||||
|
data += n;
|
||||||
|
size -= n;
|
||||||
|
} while (size != 0);
|
||||||
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
@ -884,10 +899,11 @@ FMT_FUNC void fmt::print(CStringRef format_str, ArgList args) {
|
|||||||
print(stdout, format_str, 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;
|
MemoryWriter w;
|
||||||
w.write(format_str, args);
|
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) {
|
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) {
|
FMT_FUNC int fmt::fprintf(std::ostream &os, CStringRef format, ArgList args) {
|
||||||
MemoryWriter w;
|
MemoryWriter w;
|
||||||
printf(w, format, args);
|
printf(w, format, args);
|
||||||
os.write(w.data(), w.size());
|
write(os, w);
|
||||||
return static_cast<int>(w.size());
|
return static_cast<int>(w.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2021,7 +2021,7 @@ class FormatBuf : public std::basic_streambuf<Char> {
|
|||||||
|
|
||||||
int_type overflow(int_type ch = traits_type::eof()) {
|
int_type overflow(int_type ch = traits_type::eof()) {
|
||||||
if (!traits_type::eq_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_.resize(size);
|
||||||
buffer_.reserve(size * 2);
|
buffer_.reserve(size * 2);
|
||||||
|
|
||||||
@ -2033,7 +2033,7 @@ class FormatBuf : public std::basic_streambuf<Char> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return this->pptr() - start_;
|
return to_unsigned(this->pptr() - start_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
@ -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))));
|
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count))));
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
throw SystemError(errno, "cannot read from file");
|
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) {
|
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))));
|
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count))));
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
throw SystemError(errno, "cannot write to file");
|
throw SystemError(errno, "cannot write to file");
|
||||||
return result;
|
return internal::to_unsigned(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt::File fmt::File::dup(int fd) {
|
fmt::File fmt::File::dup(int fd) {
|
||||||
|
@ -305,7 +305,8 @@ class File {
|
|||||||
// Closes the file.
|
// Closes the file.
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
// Returns the file size.
|
// Returns the file size. The size has signed type for consistency with
|
||||||
|
// stat::st_size.
|
||||||
LongLong size() const;
|
LongLong size() const;
|
||||||
|
|
||||||
// Attempts to read count bytes from the file into the specified buffer.
|
// Attempts to read count bytes from the file into the specified buffer.
|
||||||
|
@ -46,7 +46,7 @@ namespace {
|
|||||||
std::string sanitize(const std::string &s) {
|
std::string sanitize(const std::string &s) {
|
||||||
std::string result;
|
std::string result;
|
||||||
for (std::string::const_iterator i = s.begin(), end = s.end(); i != end; ++i)
|
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<char>(0xff));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,10 +80,10 @@ std::string OutputRedirect::restore_and_read() {
|
|||||||
return content; // Already read.
|
return content; // Already read.
|
||||||
enum { BUFFER_SIZE = 4096 };
|
enum { BUFFER_SIZE = 4096 };
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
std::streamsize count = 0;
|
std::size_t count = 0;
|
||||||
do {
|
do {
|
||||||
count = read_end_.read(buffer, BUFFER_SIZE);
|
count = read_end_.read(buffer, BUFFER_SIZE);
|
||||||
content.append(buffer, static_cast<std::size_t>(count));
|
content.append(buffer, count);
|
||||||
} while (count != 0);
|
} while (count != 0);
|
||||||
read_end_.close();
|
read_end_.close();
|
||||||
return content;
|
return content;
|
||||||
@ -91,8 +91,7 @@ std::string OutputRedirect::restore_and_read() {
|
|||||||
|
|
||||||
std::string read(File &f, std::size_t count) {
|
std::string read(File &f, std::size_t count) {
|
||||||
std::string buffer(count, '\0');
|
std::string buffer(count, '\0');
|
||||||
std::streamsize n = 0;
|
std::size_t n = 0, offset = 0;
|
||||||
std::size_t offset = 0;
|
|
||||||
do {
|
do {
|
||||||
n = f.read(&buffer[offset], count - offset);
|
n = f.read(&buffer[offset], count - offset);
|
||||||
// We can't read more than size_t bytes since count has type size_t.
|
// We can't read more than size_t bytes since count has type size_t.
|
||||||
|
@ -277,8 +277,7 @@ TEST(FileTest, Size) {
|
|||||||
write_file("test", content);
|
write_file("test", content);
|
||||||
File f("test", File::RDONLY);
|
File f("test", File::RDONLY);
|
||||||
EXPECT_GE(f.size(), 0);
|
EXPECT_GE(f.size(), 0);
|
||||||
fmt::ULongLong file_size = f.size();
|
EXPECT_EQ(content.size(), static_cast<fmt::ULongLong>(f.size()));
|
||||||
EXPECT_EQ(content.size(), file_size);
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
fmt::MemoryWriter message;
|
fmt::MemoryWriter message;
|
||||||
fmt::internal::format_windows_error(
|
fmt::internal::format_windows_error(
|
||||||
@ -308,7 +307,7 @@ TEST(FileTest, ReadRetry) {
|
|||||||
write_end.write("test", SIZE);
|
write_end.write("test", SIZE);
|
||||||
write_end.close();
|
write_end.close();
|
||||||
char buffer[SIZE];
|
char buffer[SIZE];
|
||||||
std::streamsize count = 0;
|
std::size_t count = 0;
|
||||||
EXPECT_RETRY(count = read_end.read(buffer, SIZE),
|
EXPECT_RETRY(count = read_end.read(buffer, SIZE),
|
||||||
read, "cannot read from file");
|
read, "cannot read from file");
|
||||||
EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
|
EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
|
||||||
@ -318,7 +317,7 @@ TEST(FileTest, WriteRetry) {
|
|||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
enum { SIZE = 4 };
|
enum { SIZE = 4 };
|
||||||
std::streamsize count = 0;
|
std::size_t count = 0;
|
||||||
EXPECT_RETRY(count = write_end.write("test", SIZE),
|
EXPECT_RETRY(count = write_end.write("test", SIZE),
|
||||||
write, "cannot write to file");
|
write, "cannot write to file");
|
||||||
write_end.close();
|
write_end.close();
|
||||||
|
@ -69,7 +69,7 @@ void write(File &f, fmt::StringRef s) {
|
|||||||
std::size_t num_chars_left = s.size();
|
std::size_t num_chars_left = s.size();
|
||||||
const char *ptr = s.data();
|
const char *ptr = s.data();
|
||||||
do {
|
do {
|
||||||
std::streamsize count = f.write(ptr, num_chars_left);
|
std::size_t count = f.write(ptr, num_chars_left);
|
||||||
ptr += count;
|
ptr += count;
|
||||||
// We can't write more than size_t bytes since num_chars_left
|
// We can't write more than size_t bytes since num_chars_left
|
||||||
// has type size_t.
|
// has type size_t.
|
||||||
@ -236,20 +236,20 @@ File OpenBufferedFile(int &fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, MoveFromTemporaryInCtor) {
|
TEST(FileTest, MoveFromTemporaryInCtor) {
|
||||||
int fd = 0xdeadbeef;
|
int fd = 0xdead;
|
||||||
File f(OpenBufferedFile(fd));
|
File f(OpenBufferedFile(fd));
|
||||||
EXPECT_EQ(fd, f.descriptor());
|
EXPECT_EQ(fd, f.descriptor());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, MoveFromTemporaryInAssignment) {
|
TEST(FileTest, MoveFromTemporaryInAssignment) {
|
||||||
int fd = 0xdeadbeef;
|
int fd = 0xdead;
|
||||||
File f;
|
File f;
|
||||||
f = OpenBufferedFile(fd);
|
f = OpenBufferedFile(fd);
|
||||||
EXPECT_EQ(fd, f.descriptor());
|
EXPECT_EQ(fd, f.descriptor());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
||||||
int fd = 0xdeadbeef;
|
int fd = 0xdead;
|
||||||
File f = open_file();
|
File f = open_file();
|
||||||
int old_fd = f.descriptor();
|
int old_fd = f.descriptor();
|
||||||
f = OpenBufferedFile(fd);
|
f = OpenBufferedFile(fd);
|
||||||
|
@ -292,21 +292,21 @@ SPECIALIZE_MAKE_SIGNED(fmt::ULongLong, fmt::LongLong);
|
|||||||
// Test length format specifier ``length_spec``.
|
// Test length format specifier ``length_spec``.
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
void TestLength(const char *length_spec, U value) {
|
void TestLength(const char *length_spec, U value) {
|
||||||
fmt::LongLong signed_value = value;
|
fmt::LongLong signed_value = 0;
|
||||||
fmt::ULongLong unsigned_value = value;
|
fmt::ULongLong unsigned_value = 0;
|
||||||
// Apply integer promotion to the argument.
|
// Apply integer promotion to the argument.
|
||||||
fmt::ULongLong max = std::numeric_limits<U>::max();
|
fmt::ULongLong max = std::numeric_limits<U>::max();
|
||||||
using fmt::internal::check;
|
using fmt::internal::check;
|
||||||
if (check(max <= static_cast<unsigned>(std::numeric_limits<int>::max()))) {
|
if (check(max <= static_cast<unsigned>(std::numeric_limits<int>::max()))) {
|
||||||
signed_value = static_cast<int>(value);
|
signed_value = static_cast<int>(value);
|
||||||
unsigned_value = static_cast<int>(value);
|
unsigned_value = static_cast<unsigned>(value);
|
||||||
} else if (check(max <= std::numeric_limits<unsigned>::max())) {
|
} else if (check(max <= std::numeric_limits<unsigned>::max())) {
|
||||||
signed_value = static_cast<unsigned>(value);
|
signed_value = static_cast<unsigned>(value);
|
||||||
unsigned_value = static_cast<unsigned>(value);
|
unsigned_value = static_cast<unsigned>(value);
|
||||||
}
|
}
|
||||||
using fmt::internal::MakeUnsigned;
|
using fmt::internal::MakeUnsigned;
|
||||||
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
|
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
|
||||||
signed_value = value;
|
signed_value = static_cast<fmt::LongLong>(value);
|
||||||
unsigned_value = static_cast<typename MakeUnsigned<unsigned>::Type>(value);
|
unsigned_value = static_cast<typename MakeUnsigned<unsigned>::Type>(value);
|
||||||
} else {
|
} else {
|
||||||
signed_value = static_cast<typename MakeSigned<T>::Type>(value);
|
signed_value = static_cast<typename MakeSigned<T>::Type>(value);
|
||||||
@ -382,7 +382,7 @@ TEST(PrintfTest, Bool) {
|
|||||||
TEST(PrintfTest, Int) {
|
TEST(PrintfTest, Int) {
|
||||||
EXPECT_PRINTF("-42", "%d", -42);
|
EXPECT_PRINTF("-42", "%d", -42);
|
||||||
EXPECT_PRINTF("-42", "%i", -42);
|
EXPECT_PRINTF("-42", "%i", -42);
|
||||||
unsigned u = -42;
|
unsigned u = -42u;
|
||||||
EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
|
EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
|
||||||
EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
|
EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
|
||||||
EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
|
EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
|
||||||
|
@ -332,8 +332,9 @@ TEST(MemoryBufferTest, Grow) {
|
|||||||
void grow(std::size_t size) { Base::grow(size); }
|
void grow(std::size_t size) { Base::grow(size); }
|
||||||
} buffer((Allocator(&alloc)));
|
} buffer((Allocator(&alloc)));
|
||||||
buffer.resize(7);
|
buffer.resize(7);
|
||||||
|
using fmt::internal::to_unsigned;
|
||||||
for (int i = 0; i < 7; ++i)
|
for (int i = 0; i < 7; ++i)
|
||||||
buffer[i] = i * i;
|
buffer[to_unsigned(i)] = i * i;
|
||||||
EXPECT_EQ(10u, buffer.capacity());
|
EXPECT_EQ(10u, buffer.capacity());
|
||||||
int mem[20];
|
int mem[20];
|
||||||
mem[7] = 0xdead;
|
mem[7] = 0xdead;
|
||||||
@ -342,7 +343,7 @@ TEST(MemoryBufferTest, Grow) {
|
|||||||
EXPECT_EQ(20u, buffer.capacity());
|
EXPECT_EQ(20u, buffer.capacity());
|
||||||
// Check if size elements have been copied
|
// Check if size elements have been copied
|
||||||
for (int i = 0; i < 7; ++i)
|
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.
|
// and no more than that.
|
||||||
EXPECT_EQ(0xdead, buffer[7]);
|
EXPECT_EQ(0xdead, buffer[7]);
|
||||||
EXPECT_CALL(alloc, deallocate(mem, 20));
|
EXPECT_CALL(alloc, deallocate(mem, 20));
|
||||||
|
Reference in New Issue
Block a user