diff options
author | Andy Getzendanner <durandal@google.com> | 2023-01-13 01:22:59 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-01-13 01:23:57 -0800 |
commit | bb63a76710554cebbeb20306739a7b832be38c4a (patch) | |
tree | 8ba428e9cb17d9e766c89bc97c625f5f5d075b67 | |
parent | 49081b8d71e4e60667110dd085f3cb8c3f554c8a (diff) |
Use NullGuard for signed and unsigned char pointer types, and extend volatile pointer type testcase to char pointers.
PiperOrigin-RevId: 501781539
Change-Id: I99012cecd960745de8a921b96671cde42e28a3af
-rw-r--r-- | absl/log/internal/nullguard.h | 29 | ||||
-rw-r--r-- | absl/log/log_format_test.cc | 23 |
2 files changed, 43 insertions, 9 deletions
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 <array> #include <cstddef> #include "absl/base/config.h" @@ -44,6 +45,34 @@ template <> struct NullGuard<const char*> final { static const char* Guard(const char* v) { return v ? v : "(null)"; } }; +constexpr std::array<signed char, 7> kSignedCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; +template <> +struct NullGuard<signed char*> final { + static const signed char* Guard(const signed char* v) { + return v ? v : kSignedCharNull.data(); + } +}; +template <> +struct NullGuard<const signed char*> final { + static const signed char* Guard(const signed char* v) { + return v ? v : kSignedCharNull.data(); + } +}; +constexpr std::array<unsigned char, 7> kUnsignedCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; +template <> +struct NullGuard<unsigned char*> final { + static const unsigned char* Guard(const unsigned char* v) { + return v ? v : kUnsignedCharNull.data(); + } +}; +template <> +struct NullGuard<const unsigned char*> final { + static const unsigned char* Guard(const unsigned char* v) { + return v ? v : kUnsignedCharNull.data(); + } +}; template <> struct NullGuard<std::nullptr_t> 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 <typename T> -class VolatileVoidPtrLogFormatTest : public testing::Test {}; -using VolatileVoidPtrTypes = Types<volatile void *, const volatile void *>; -TYPED_TEST_SUITE(VolatileVoidPtrLogFormatTest, VolatileVoidPtrTypes); +class VolatilePtrLogFormatTest : public testing::Test {}; +using VolatilePtrTypes = + Types<volatile void*, const volatile void*, volatile char*, + const volatile char*, volatile signed char*, + const volatile signed char*, volatile unsigned char*, + const volatile unsigned char*>; +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<TypeParam>(0xdeadbeefLL); @@ -707,7 +711,8 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, NonNull) { template <typename T> class CharPtrLogFormatTest : public testing::Test {}; -using CharPtrTypes = Types<char *, const char *>; +using CharPtrTypes = Types<char, const char, signed char, const signed char, + unsigned char, const unsigned char>; 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; |