Skip to content

Commit 79bdf3b

Browse files
suertreuscopybara-github
authored andcommitted
When printing CHECK_XX failures and both types are unprintable, don't bother printing " (UNPRINTABLE vs. UNPRINTABLE)".
PiperOrigin-RevId: 802287138 Change-Id: I8b4815fdc089427be9f276e10f9a72b398ed8f80
1 parent 4dd0ffd commit 79bdf3b

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

absl/log/check_test_impl.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ TEST(CHECKDeathTest, TestBinaryChecksWithUnprintable) {
292292
ExampleTypeThatHasNoStreamOperator a{true};
293293
ExampleTypeThatHasNoStreamOperator b{false};
294294
ABSL_TEST_CHECK_EQ(a, a);
295-
EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
296-
"Check failed: a == b \\(UNPRINTABLE vs. UNPRINTABLE\\)");
295+
EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), "Check failed: a == b");
297296
ABSL_TEST_CHECK_EQ(a, true);
298297
EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, false),
299298
"Check failed: a == false \\(UNPRINTABLE vs. 0\\)");

absl/log/internal/check_op.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ void MakeCheckOpValueString(std::ostream& os, const void* p) {
101101
}
102102
}
103103

104-
void MakeCheckOpUnprintableString(std::ostream& os) { os << "UNPRINTABLE"; }
104+
std::ostream& operator<<(std::ostream& os, UnprintableWrapper) {
105+
return os << "UNPRINTABLE";
106+
}
105107

106108
// Helper functions for string comparisons.
107109
#define DEFINE_CHECK_STROP_IMPL(name, func, expected) \

absl/log/internal/check_op.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,12 @@ void MakeCheckOpValueString(std::ostream& os, signed char v);
225225
void MakeCheckOpValueString(std::ostream& os, unsigned char v);
226226
void MakeCheckOpValueString(std::ostream& os, const void* absl_nullable p);
227227

228-
void MakeCheckOpUnprintableString(std::ostream& os);
229-
230228
// A wrapper for types that have no operator<<.
231229
struct UnprintableWrapper {
232230
template <typename T>
233231
explicit UnprintableWrapper(const T&) {}
234232

235-
friend std::ostream& operator<<(std::ostream& os, const UnprintableWrapper&) {
236-
MakeCheckOpUnprintableString(os);
237-
return os;
238-
}
233+
friend std::ostream& operator<<(std::ostream& os, UnprintableWrapper);
239234
};
240235

241236
namespace detect_specialization {
@@ -400,10 +395,16 @@ ABSL_ATTRIBUTE_RETURNS_NONNULL const char* absl_nonnull MakeCheckOpString(
400395
template <typename T1, typename T2>
401396
const char* absl_nonnull MakeCheckOpString(T1 v1, T2 v2,
402397
const char* absl_nonnull exprtext) {
403-
CheckOpMessageBuilder comb(exprtext);
404-
MakeCheckOpValueString(comb.ForVar1(), v1);
405-
MakeCheckOpValueString(comb.ForVar2(), v2);
406-
return comb.NewString();
398+
if constexpr (std::is_same_v<CheckOpStreamType<T1>, UnprintableWrapper> &&
399+
std::is_same_v<CheckOpStreamType<T2>, UnprintableWrapper>) {
400+
// No sense printing " (UNPRINTABLE vs. UNPRINTABLE)"
401+
return exprtext;
402+
} else {
403+
CheckOpMessageBuilder comb(exprtext);
404+
MakeCheckOpValueString(comb.ForVar1(), v1);
405+
MakeCheckOpValueString(comb.ForVar2(), v2);
406+
return comb.NewString();
407+
}
407408
}
408409

409410
// Add a few commonly used instantiations as extern to reduce size of objects

0 commit comments

Comments
 (0)