Skip to content

Commit 94433ef

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Fix various warnings for _WIN32.
Bug: chromium:1292951 PiperOrigin-RevId: 481757795 Change-Id: I03c808222c6c4d3d7052576ab4b36141e5f1ebbc
1 parent 6b2d248 commit 94433ef

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

Diff for: absl/log/internal/log_format.cc

+26-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ ABSL_NAMESPACE_BEGIN
4646
namespace log_internal {
4747
namespace {
4848

49+
// This templated function avoids compiler warnings about tautological
50+
// comparisons when log_internal::Tid is unsigned. It can be replaced with a
51+
// constexpr if once the minimum C++ version Abseil suppports is C++17.
52+
template <typename T>
53+
inline std::enable_if_t<!std::is_signed<T>::value>
54+
PutLeadingWhitespace(T tid, char*& p) {
55+
if (tid < 10) *p++ = ' ';
56+
if (tid < 100) *p++ = ' ';
57+
if (tid < 1000) *p++ = ' ';
58+
if (tid < 10000) *p++ = ' ';
59+
if (tid < 100000) *p++ = ' ';
60+
if (tid < 1000000) *p++ = ' ';
61+
}
62+
63+
template <typename T>
64+
inline std::enable_if_t<std::is_signed<T>::value>
65+
PutLeadingWhitespace(T tid, char*& p) {
66+
if (tid >= 0 && tid < 10) *p++ = ' ';
67+
if (tid > -10 && tid < 100) *p++ = ' ';
68+
if (tid > -100 && tid < 1000) *p++ = ' ';
69+
if (tid > -1000 && tid < 10000) *p++ = ' ';
70+
if (tid > -10000 && tid < 100000) *p++ = ' ';
71+
if (tid > -100000 && tid < 1000000) *p++ = ' ';
72+
}
73+
4974
// The fields before the filename are all fixed-width except for the thread ID,
5075
// which is of bounded width.
5176
size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
@@ -110,13 +135,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
110135
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(usecs % 100), p);
111136
p += 2;
112137
*p++ = ' ';
113-
constexpr bool unsigned_tid_t = !std::is_signed<log_internal::Tid>::value;
114-
if ((unsigned_tid_t || tid >= 0) && tid < 10) *p++ = ' ';
115-
if ((unsigned_tid_t || tid > -10) && tid < 100) *p++ = ' ';
116-
if ((unsigned_tid_t || tid > -100) && tid < 1000) *p++ = ' ';
117-
if ((unsigned_tid_t || tid > -1000) && tid < 10000) *p++ = ' ';
118-
if ((unsigned_tid_t || tid > -10000) && tid < 100000) *p++ = ' ';
119-
if ((unsigned_tid_t || tid > -100000) && tid < 1000000) *p++ = ' ';
138+
PutLeadingWhitespace(tid, p);
120139
p = absl::numbers_internal::FastIntToBuffer(tid, p);
121140
*p++ = ' ';
122141
const size_t bytes_formatted = static_cast<size_t>(p - buf.data());

Diff for: absl/log/internal/test_helpers.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bool DiedOfFatal(int exit_status) {
4646
// Depending on NDEBUG and (configuration?) MSVC's abort either results
4747
// in error code 3 (SIGABRT) or error code 0x80000003 (breakpoint
4848
// triggered).
49-
return ::testing::ExitedWithCode(3)(exit_status & ~0x80000000);
49+
return ::testing::ExitedWithCode(3)(exit_status & 0x7fffffff);
5050
#elif defined(__Fuchsia__)
5151
// The Fuchsia death test implementation kill()'s the process when it detects
5252
// an exception, so it should exit with the corresponding code. See

Diff for: absl/log/log_entry_test.cc

+8-2
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,13 @@ TEST(LogEntryTest, EmptyFields) {
208208
}
209209

210210
TEST(LogEntryTest, NegativeFields) {
211+
// When Abseil's minimum C++ version is C++17, this conditional can be
212+
// converted to a constexpr if and the static_cast below removed.
211213
if (std::is_signed<absl::LogEntry::tid_t>::value) {
212214
LogEntryTestPeer entry("foo.cc", -1234, kUsePrefix,
213215
absl::LogSeverity::kInfo, "2020-01-02T03:04:05.6789",
214-
-451, "hello world");
216+
static_cast<absl::LogEntry::tid_t>(-451),
217+
"hello world");
215218
EXPECT_THAT(entry.FormatLogMessage(),
216219
Eq("I0102 03:04:05.678900 -451 foo.cc:-1234] hello world"));
217220
EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
@@ -313,12 +316,15 @@ TEST(LogEntryTest, LongFields) {
313316
}
314317

315318
TEST(LogEntryTest, LongNegativeFields) {
319+
// When Abseil's minimum C++ version is C++17, this conditional can be
320+
// converted to a constexpr if and the static_cast below removed.
316321
if (std::is_signed<absl::LogEntry::tid_t>::value) {
317322
LogEntryTestPeer entry(
318323
"I am the very model of a modern Major-General / "
319324
"I've information vegetable, animal, and mineral.",
320325
-2147483647, kUsePrefix, absl::LogSeverity::kInfo,
321-
"2020-01-02T03:04:05.678967896789", -2147483647,
326+
"2020-01-02T03:04:05.678967896789",
327+
static_cast<absl::LogEntry::tid_t>(-2147483647),
322328
"I know the kings of England, and I quote the fights historical / "
323329
"From Marathon to Waterloo, in order categorical.");
324330
EXPECT_THAT(

Diff for: absl/log/log_modifier_methods_test.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ TEST(TailCallsModifiesTest, WithTimestamp) {
130130
TEST(TailCallsModifiesTest, WithThreadID) {
131131
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
132132

133-
EXPECT_CALL(test_sink, Send(AllOf(ThreadID(Eq(1234)))));
133+
EXPECT_CALL(test_sink,
134+
Send(AllOf(ThreadID(Eq(absl::LogEntry::tid_t{1234})))));
134135

135136
test_sink.StartCapturingLogs();
136137
LOG(INFO).WithThreadID(1234) << "hello world";
@@ -152,7 +153,8 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) {
152153
Send(AllOf(SourceFilename(Eq("fake/file")), SourceBasename(Eq("file")),
153154
SourceLine(Eq(123)), Prefix(IsFalse()),
154155
LogSeverity(Eq(absl::LogSeverity::kWarning)),
155-
Timestamp(Eq(absl::UnixEpoch())), ThreadID(Eq(456)),
156+
Timestamp(Eq(absl::UnixEpoch())),
157+
ThreadID(Eq(absl::LogEntry::tid_t{456})),
156158
TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)),
157159
ENCODED_MESSAGE(
158160
EqualsProto(R"pb(value { literal: "forwarded: " }

Diff for: absl/log/scoped_mock_log_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST(ScopedMockLogTest, LogMockCatchAndMatchSendExpectations) {
9898
log,
9999
Send(AllOf(SourceFilename(Eq("/my/very/very/very_long_source_file.cc")),
100100
SourceBasename(Eq("very_long_source_file.cc")),
101-
SourceLine(Eq(777)), ThreadID(Eq(1234)),
101+
SourceLine(Eq(777)), ThreadID(Eq(absl::LogEntry::tid_t{1234})),
102102
TextMessageWithPrefix(Truly([](absl::string_view msg) {
103103
return absl::EndsWith(
104104
msg, " very_long_source_file.cc:777] Info message");

0 commit comments

Comments
 (0)