From f8304d762e0ed119d3d1c72deb1976dc80029db5 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 5 Jan 2021 12:21:27 -0500 Subject: Googletest export Add support for printing incomplete types in the universal printer. PiperOrigin-RevId: 350154637 --- googletest/include/gtest/gtest-printers.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'googletest/include/gtest/gtest-printers.h') diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 463f0aff..d3292247 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -261,16 +261,23 @@ struct ConvertibleToStringViewPrinter { GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, ::std::ostream* os); -struct FallbackPrinter { - template +struct RawBytesPrinter { + // SFINAE on `sizeof` to make sure we have a complete type. + template static void PrintValue(const T& value, ::std::ostream* os) { PrintBytesInObjectTo( - static_cast( - reinterpret_cast(std::addressof(value))), + reinterpret_cast(std::addressof(value)), sizeof(value), os); } }; +struct FallbackPrinter { + template + static void PrintValue(const T&, ::std::ostream* os) { + *os << "(incomplete type)"; + } +}; + // Try every printer in order and return the first one that works. template struct FindFirstPrinter : FindFirstPrinter {}; @@ -297,7 +304,7 @@ void PrintWithFallback(const T& value, ::std::ostream* os) { T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, internal_stream_operator_without_lexical_name_lookup::StreamPrinter, ProtobufPrinter, ConvertibleToIntegerPrinter, - ConvertibleToStringViewPrinter, FallbackPrinter>::type; + ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type; Printer::PrintValue(value, os); } -- cgit v1.2.1