Commit bb63a767 by Andy Getzendanner Committed by Copybara-Service

Use NullGuard for signed and unsigned char pointer types, and extend volatile…

Use NullGuard for signed and unsigned char pointer types, and extend volatile pointer type testcase to char pointers.

PiperOrigin-RevId: 501781539
Change-Id: I99012cecd960745de8a921b96671cde42e28a3af
parent 49081b8d
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#ifndef ABSL_LOG_INTERNAL_NULLGUARD_H_ #ifndef ABSL_LOG_INTERNAL_NULLGUARD_H_
#define ABSL_LOG_INTERNAL_NULLGUARD_H_ #define ABSL_LOG_INTERNAL_NULLGUARD_H_
#include <array>
#include <cstddef> #include <cstddef>
#include "absl/base/config.h" #include "absl/base/config.h"
...@@ -44,6 +45,34 @@ template <> ...@@ -44,6 +45,34 @@ template <>
struct NullGuard<const char*> final { struct NullGuard<const char*> final {
static const char* Guard(const char* v) { return v ? v : "(null)"; } 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 <> template <>
struct NullGuard<std::nullptr_t> final { struct NullGuard<std::nullptr_t> final {
static const char* Guard(const std::nullptr_t&) { return "(null)"; } static const char* Guard(const std::nullptr_t&) { return "(null)"; }
......
...@@ -665,11 +665,15 @@ TYPED_TEST(VoidPtrLogFormatTest, NonNull) { ...@@ -665,11 +665,15 @@ TYPED_TEST(VoidPtrLogFormatTest, NonNull) {
} }
template <typename T> template <typename T>
class VolatileVoidPtrLogFormatTest : public testing::Test {}; class VolatilePtrLogFormatTest : public testing::Test {};
using VolatileVoidPtrTypes = Types<volatile void *, const volatile void *>; using VolatilePtrTypes =
TYPED_TEST_SUITE(VolatileVoidPtrLogFormatTest, VolatileVoidPtrTypes); 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); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const TypeParam value = nullptr; const TypeParam value = nullptr;
...@@ -687,7 +691,7 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, Null) { ...@@ -687,7 +691,7 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, Null) {
LOG(INFO) << value; LOG(INFO) << value;
} }
TYPED_TEST(VolatileVoidPtrLogFormatTest, NonNull) { TYPED_TEST(VolatilePtrLogFormatTest, NonNull) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const TypeParam value = reinterpret_cast<TypeParam>(0xdeadbeefLL); const TypeParam value = reinterpret_cast<TypeParam>(0xdeadbeefLL);
...@@ -707,7 +711,8 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, NonNull) { ...@@ -707,7 +711,8 @@ TYPED_TEST(VolatileVoidPtrLogFormatTest, NonNull) {
template <typename T> template <typename T>
class CharPtrLogFormatTest : public testing::Test {}; 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_SUITE(CharPtrLogFormatTest, CharPtrTypes);
TYPED_TEST(CharPtrLogFormatTest, Null) { TYPED_TEST(CharPtrLogFormatTest, Null) {
...@@ -717,7 +722,7 @@ TYPED_TEST(CharPtrLogFormatTest, Null) { ...@@ -717,7 +722,7 @@ TYPED_TEST(CharPtrLogFormatTest, Null) {
// standard library implementations choose to crash. We take measures to log // standard library implementations choose to crash. We take measures to log
// something useful instead of crashing, even when that differs from the // something useful instead of crashing, even when that differs from the
// standard library in use (and thus the behavior of `std::ostream`). // standard library in use (and thus the behavior of `std::ostream`).
const TypeParam value = nullptr; TypeParam* const value = nullptr;
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
...@@ -733,8 +738,8 @@ TYPED_TEST(CharPtrLogFormatTest, Null) { ...@@ -733,8 +738,8 @@ TYPED_TEST(CharPtrLogFormatTest, Null) {
TYPED_TEST(CharPtrLogFormatTest, NonNull) { TYPED_TEST(CharPtrLogFormatTest, NonNull) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
char data[] = "value"; TypeParam data[] = {'v', 'a', 'l', 'u', 'e', '\0'};
const TypeParam value = data; TypeParam* const value = data;
auto comparison_stream = ComparisonStream(); auto comparison_stream = ComparisonStream();
comparison_stream << value; comparison_stream << value;
......
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