Commit 94433ef0 by Abseil Team Committed by Copybara-Service

Fix various warnings for _WIN32.

Bug: chromium:1292951
PiperOrigin-RevId: 481757795
Change-Id: I03c808222c6c4d3d7052576ab4b36141e5f1ebbc
parent 6b2d248d
...@@ -46,6 +46,31 @@ ABSL_NAMESPACE_BEGIN ...@@ -46,6 +46,31 @@ ABSL_NAMESPACE_BEGIN
namespace log_internal { namespace log_internal {
namespace { namespace {
// This templated function avoids compiler warnings about tautological
// comparisons when log_internal::Tid is unsigned. It can be replaced with a
// constexpr if once the minimum C++ version Abseil suppports is C++17.
template <typename T>
inline std::enable_if_t<!std::is_signed<T>::value>
PutLeadingWhitespace(T tid, char*& p) {
if (tid < 10) *p++ = ' ';
if (tid < 100) *p++ = ' ';
if (tid < 1000) *p++ = ' ';
if (tid < 10000) *p++ = ' ';
if (tid < 100000) *p++ = ' ';
if (tid < 1000000) *p++ = ' ';
}
template <typename T>
inline std::enable_if_t<std::is_signed<T>::value>
PutLeadingWhitespace(T tid, char*& p) {
if (tid >= 0 && tid < 10) *p++ = ' ';
if (tid > -10 && tid < 100) *p++ = ' ';
if (tid > -100 && tid < 1000) *p++ = ' ';
if (tid > -1000 && tid < 10000) *p++ = ' ';
if (tid > -10000 && tid < 100000) *p++ = ' ';
if (tid > -100000 && tid < 1000000) *p++ = ' ';
}
// The fields before the filename are all fixed-width except for the thread ID, // The fields before the filename are all fixed-width except for the thread ID,
// which is of bounded width. // which is of bounded width.
size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp, size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
...@@ -110,13 +135,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp, ...@@ -110,13 +135,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(usecs % 100), p); absl::numbers_internal::PutTwoDigits(static_cast<size_t>(usecs % 100), p);
p += 2; p += 2;
*p++ = ' '; *p++ = ' ';
constexpr bool unsigned_tid_t = !std::is_signed<log_internal::Tid>::value; PutLeadingWhitespace(tid, p);
if ((unsigned_tid_t || tid >= 0) && tid < 10) *p++ = ' ';
if ((unsigned_tid_t || tid > -10) && tid < 100) *p++ = ' ';
if ((unsigned_tid_t || tid > -100) && tid < 1000) *p++ = ' ';
if ((unsigned_tid_t || tid > -1000) && tid < 10000) *p++ = ' ';
if ((unsigned_tid_t || tid > -10000) && tid < 100000) *p++ = ' ';
if ((unsigned_tid_t || tid > -100000) && tid < 1000000) *p++ = ' ';
p = absl::numbers_internal::FastIntToBuffer(tid, p); p = absl::numbers_internal::FastIntToBuffer(tid, p);
*p++ = ' '; *p++ = ' ';
const size_t bytes_formatted = static_cast<size_t>(p - buf.data()); const size_t bytes_formatted = static_cast<size_t>(p - buf.data());
......
...@@ -46,7 +46,7 @@ bool DiedOfFatal(int exit_status) { ...@@ -46,7 +46,7 @@ bool DiedOfFatal(int exit_status) {
// Depending on NDEBUG and (configuration?) MSVC's abort either results // Depending on NDEBUG and (configuration?) MSVC's abort either results
// in error code 3 (SIGABRT) or error code 0x80000003 (breakpoint // in error code 3 (SIGABRT) or error code 0x80000003 (breakpoint
// triggered). // triggered).
return ::testing::ExitedWithCode(3)(exit_status & ~0x80000000); return ::testing::ExitedWithCode(3)(exit_status & 0x7fffffff);
#elif defined(__Fuchsia__) #elif defined(__Fuchsia__)
// The Fuchsia death test implementation kill()'s the process when it detects // The Fuchsia death test implementation kill()'s the process when it detects
// an exception, so it should exit with the corresponding code. See // an exception, so it should exit with the corresponding code. See
......
...@@ -208,10 +208,13 @@ TEST(LogEntryTest, EmptyFields) { ...@@ -208,10 +208,13 @@ TEST(LogEntryTest, EmptyFields) {
} }
TEST(LogEntryTest, NegativeFields) { TEST(LogEntryTest, NegativeFields) {
// When Abseil's minimum C++ version is C++17, this conditional can be
// converted to a constexpr if and the static_cast below removed.
if (std::is_signed<absl::LogEntry::tid_t>::value) { if (std::is_signed<absl::LogEntry::tid_t>::value) {
LogEntryTestPeer entry("foo.cc", -1234, kUsePrefix, LogEntryTestPeer entry("foo.cc", -1234, kUsePrefix,
absl::LogSeverity::kInfo, "2020-01-02T03:04:05.6789", absl::LogSeverity::kInfo, "2020-01-02T03:04:05.6789",
-451, "hello world"); static_cast<absl::LogEntry::tid_t>(-451),
"hello world");
EXPECT_THAT(entry.FormatLogMessage(), EXPECT_THAT(entry.FormatLogMessage(),
Eq("I0102 03:04:05.678900 -451 foo.cc:-1234] hello world")); Eq("I0102 03:04:05.678900 -451 foo.cc:-1234] hello world"));
EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000), EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
...@@ -313,12 +316,15 @@ TEST(LogEntryTest, LongFields) { ...@@ -313,12 +316,15 @@ TEST(LogEntryTest, LongFields) {
} }
TEST(LogEntryTest, LongNegativeFields) { TEST(LogEntryTest, LongNegativeFields) {
// When Abseil's minimum C++ version is C++17, this conditional can be
// converted to a constexpr if and the static_cast below removed.
if (std::is_signed<absl::LogEntry::tid_t>::value) { if (std::is_signed<absl::LogEntry::tid_t>::value) {
LogEntryTestPeer entry( LogEntryTestPeer entry(
"I am the very model of a modern Major-General / " "I am the very model of a modern Major-General / "
"I've information vegetable, animal, and mineral.", "I've information vegetable, animal, and mineral.",
-2147483647, kUsePrefix, absl::LogSeverity::kInfo, -2147483647, kUsePrefix, absl::LogSeverity::kInfo,
"2020-01-02T03:04:05.678967896789", -2147483647, "2020-01-02T03:04:05.678967896789",
static_cast<absl::LogEntry::tid_t>(-2147483647),
"I know the kings of England, and I quote the fights historical / " "I know the kings of England, and I quote the fights historical / "
"From Marathon to Waterloo, in order categorical."); "From Marathon to Waterloo, in order categorical.");
EXPECT_THAT( EXPECT_THAT(
......
...@@ -130,7 +130,8 @@ TEST(TailCallsModifiesTest, WithTimestamp) { ...@@ -130,7 +130,8 @@ TEST(TailCallsModifiesTest, WithTimestamp) {
TEST(TailCallsModifiesTest, WithThreadID) { TEST(TailCallsModifiesTest, WithThreadID) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
EXPECT_CALL(test_sink, Send(AllOf(ThreadID(Eq(1234))))); EXPECT_CALL(test_sink,
Send(AllOf(ThreadID(Eq(absl::LogEntry::tid_t{1234})))));
test_sink.StartCapturingLogs(); test_sink.StartCapturingLogs();
LOG(INFO).WithThreadID(1234) << "hello world"; LOG(INFO).WithThreadID(1234) << "hello world";
...@@ -152,7 +153,8 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) { ...@@ -152,7 +153,8 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) {
Send(AllOf(SourceFilename(Eq("fake/file")), SourceBasename(Eq("file")), Send(AllOf(SourceFilename(Eq("fake/file")), SourceBasename(Eq("file")),
SourceLine(Eq(123)), Prefix(IsFalse()), SourceLine(Eq(123)), Prefix(IsFalse()),
LogSeverity(Eq(absl::LogSeverity::kWarning)), LogSeverity(Eq(absl::LogSeverity::kWarning)),
Timestamp(Eq(absl::UnixEpoch())), ThreadID(Eq(456)), Timestamp(Eq(absl::UnixEpoch())),
ThreadID(Eq(absl::LogEntry::tid_t{456})),
TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)), TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)),
ENCODED_MESSAGE( ENCODED_MESSAGE(
EqualsProto(R"pb(value { literal: "forwarded: " } EqualsProto(R"pb(value { literal: "forwarded: " }
......
...@@ -98,7 +98,7 @@ TEST(ScopedMockLogTest, LogMockCatchAndMatchSendExpectations) { ...@@ -98,7 +98,7 @@ TEST(ScopedMockLogTest, LogMockCatchAndMatchSendExpectations) {
log, log,
Send(AllOf(SourceFilename(Eq("/my/very/very/very_long_source_file.cc")), Send(AllOf(SourceFilename(Eq("/my/very/very/very_long_source_file.cc")),
SourceBasename(Eq("very_long_source_file.cc")), SourceBasename(Eq("very_long_source_file.cc")),
SourceLine(Eq(777)), ThreadID(Eq(1234)), SourceLine(Eq(777)), ThreadID(Eq(absl::LogEntry::tid_t{1234})),
TextMessageWithPrefix(Truly([](absl::string_view msg) { TextMessageWithPrefix(Truly([](absl::string_view msg) {
return absl::EndsWith( return absl::EndsWith(
msg, " very_long_source_file.cc:777] Info message"); msg, " very_long_source_file.cc:777] Info message");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment