From bb63a76710554cebbeb20306739a7b832be38c4a Mon Sep 17 00:00:00 2001 From: Andy Getzendanner Date: Fri, 13 Jan 2023 01:22:59 -0800 Subject: Use NullGuard for signed and unsigned char pointer types, and extend volatile pointer type testcase to char pointers. PiperOrigin-RevId: 501781539 Change-Id: I99012cecd960745de8a921b96671cde42e28a3af --- absl/log/internal/nullguard.h | 29 +++++++++++++++++++++++++++++ absl/log/log_format_test.cc | 23 ++++++++++++++--------- 2 files changed, 43 insertions(+), 9 deletions(-) (limited to 'absl') diff --git a/absl/log/internal/nullguard.h b/absl/log/internal/nullguard.h index 147ca814..8ea38356 100644 --- a/absl/log/internal/nullguard.h +++ b/absl/log/internal/nullguard.h @@ -24,6 +24,7 @@ #ifndef ABSL_LOG_INTERNAL_NULLGUARD_H_ #define ABSL_LOG_INTERNAL_NULLGUARD_H_ +#include #include #include "absl/base/config.h" @@ -44,6 +45,34 @@ template <> struct NullGuard final { static const char* Guard(const char* v) { return v ? v : "(null)"; } }; +constexpr std::array kSignedCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; +template <> +struct NullGuard final { + static const signed char* Guard(const signed char* v) { + return v ? v : kSignedCharNull.data(); + } +}; +template <> +struct NullGuard final { + static const signed char* Guard(const signed char* v) { + return v ? v : kSignedCharNull.data(); + } +}; +constexpr std::array kUnsignedCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; +template <> +struct NullGuard final { + static const unsigned char* Guard(const unsigned char* v) { + return v ? v : kUnsignedCharNull.data(); + } +}; +template <> +struct NullGuard final { + static const unsigned char* Guard(const unsigned char* v) { + return v ? v : kUnsignedCharNull.data(); + } +}; template <> struct NullGuard final { static const char* Guard(const std::nullptr_t&) { return "(null)"; } diff --git a/absl/log/log_format_test.cc b/absl/log/log_format_test.cc index 69bdf8d8..dbad5d97 100644 --- a/absl/log/log_format_test.cc +++ b/absl/log/log_format_test.cc @@ -665,11 +665,15 @@ TYPED_TEST(VoidPtrLogFormatTest, NonNull) { } template -class VolatileVoidPtrLogFormatTest : public testing::Test {}; -using VolatileVoidPtrTypes = Types; -TYPED_TEST_SUITE(VolatileVoidPtrLogFormatTest, VolatileVoidPtrTypes); +class VolatilePtrLogFormatTest : public testing::Test {}; +using VolatilePtrTypes = + Types; +TYPED_TEST_SUITE(VolatilePtrLogFormatTest, VolatilePtrTypes); -TYPED_TEST(VolatileVoidPtrLogFormatTest, Null) { +TYPED_TEST(VolatilePtrLogFormatTest, Null) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); const TypeParam value = nullptr; @@ -687,7 +691,7 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, Null) { LOG(INFO) << value; } -TYPED_TEST(VolatileVoidPtrLogFormatTest, NonNull) { +TYPED_TEST(VolatilePtrLogFormatTest, NonNull) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); const TypeParam value = reinterpret_cast(0xdeadbeefLL); @@ -707,7 +711,8 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, NonNull) { template class CharPtrLogFormatTest : public testing::Test {}; -using CharPtrTypes = Types; +using CharPtrTypes = Types; TYPED_TEST_SUITE(CharPtrLogFormatTest, CharPtrTypes); TYPED_TEST(CharPtrLogFormatTest, Null) { @@ -717,7 +722,7 @@ TYPED_TEST(CharPtrLogFormatTest, Null) { // standard library implementations choose to crash. We take measures to log // something useful instead of crashing, even when that differs from the // standard library in use (and thus the behavior of `std::ostream`). - const TypeParam value = nullptr; + TypeParam* const value = nullptr; EXPECT_CALL( test_sink, @@ -733,8 +738,8 @@ TYPED_TEST(CharPtrLogFormatTest, Null) { TYPED_TEST(CharPtrLogFormatTest, NonNull) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - char data[] = "value"; - const TypeParam value = data; + TypeParam data[] = {'v', 'a', 'l', 'u', 'e', '\0'}; + TypeParam* const value = data; auto comparison_stream = ComparisonStream(); comparison_stream << value; -- cgit v1.2.3