// Copyright 2007, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Google Test - The Google C++ Testing and Mocking Framework // // This file implements a universal value printer that can print a // value of any type T: // // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr); // // It uses the << operator when possible, and prints the bytes in the // object otherwise. A user can override its behavior for a class // type Foo by defining either operator<<(::std::ostream&, const Foo&) // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that // defines Foo. #include "gtest/gtest-printers.h" #include <stdio.h> #include <cctype> #include <cstdint> #include <cwchar> #include <iomanip> #include <ios> #include <ostream> // NOLINT #include <string> #include <type_traits> #include "gtest/internal/gtest-port.h" #include "src/gtest-internal-inl.h" namespace testing { namespace { ostream; // Prints a segment of bytes in the given object. GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, size_t count, ostream* os) { … } // Prints the bytes in the given value to the given ostream. void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count, ostream* os) { … } // Helpers for widening a character to char32_t. Since the standard does not // specify if char / wchar_t is signed or unsigned, it is important to first // convert it to the unsigned type of the same width before widening it to // char32_t. template <typename CharType> char32_t ToChar32(CharType in) { … } } // namespace namespace internal { // Delegates to PrintBytesInObjectToImpl() to print the bytes in the // given object. The delegation simplifies the implementation, which // uses the << operator and thus is easier done outside of the // ::testing::internal namespace, which contains a << operator that // sometimes conflicts with the one in STL. void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, ostream* os) { … } // Depending on the value of a char (or wchar_t), we print it in one // of three formats: // - as is if it's a printable ASCII (e.g. 'a', '2', ' '), // - as a hexadecimal escape sequence (e.g. '\x7F'), or // - as a special escape sequence (e.g. '\r', '\n'). enum CharFormat { … }; // Returns true if c is a printable ASCII character. We test the // value of c directly instead of calling isprint(), which is buggy on // Windows Mobile. inline bool IsPrintableAscii(char32_t c) { … } // Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a // character literal without the quotes, escaping it when necessary; returns how // c was formatted. template <typename Char> static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) { … } // Prints a char32_t c as if it's part of a string literal, escaping it when // necessary; returns how c was formatted. static CharFormat PrintAsStringLiteralTo(char32_t c, ostream* os) { … } static const char* GetCharWidthPrefix(char) { … } static const char* GetCharWidthPrefix(signed char) { … } static const char* GetCharWidthPrefix(unsigned char) { … } #ifdef __cpp_lib_char8_t static const char* GetCharWidthPrefix(char8_t) { … } #endif static const char* GetCharWidthPrefix(char16_t) { … } static const char* GetCharWidthPrefix(char32_t) { … } static const char* GetCharWidthPrefix(wchar_t) { … } // Prints a char c as if it's part of a string literal, escaping it when // necessary; returns how c was formatted. static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { … } #ifdef __cpp_lib_char8_t static CharFormat PrintAsStringLiteralTo(char8_t c, ostream* os) { … } #endif static CharFormat PrintAsStringLiteralTo(char16_t c, ostream* os) { … } static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) { … } // Prints a character c (of type char, char8_t, char16_t, char32_t, or wchar_t) // and its code. '\0' is printed as "'\\0'", other unprintable characters are // also properly escaped using the standard C++ escape sequence. template <typename Char> void PrintCharAndCodeTo(Char c, ostream* os) { … } void PrintTo(unsigned char c, ::std::ostream* os) { … } void PrintTo(signed char c, ::std::ostream* os) { … } // Prints a wchar_t as a symbol if it is printable or as its internal // code otherwise and also as its code. L'\0' is printed as "L'\\0'". void PrintTo(wchar_t wc, ostream* os) { … } // TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well. void PrintTo(char32_t c, ::std::ostream* os) { … } // gcc/clang __{u,}int128_t #if defined(__SIZEOF_INT128__) void PrintTo(__uint128_t v, ::std::ostream* os) { … } void PrintTo(__int128_t v, ::std::ostream* os) { … } #endif // __SIZEOF_INT128__ // Prints the given array of characters to the ostream. CharType must be either // char, char8_t, char16_t, char32_t, or wchar_t. // The array starts at begin, the length is len, it may include '\0' characters // and may not be NUL-terminated. template <typename CharType> GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat PrintCharsAsStringTo(const CharType* begin, size_t len, ostream* os) { … } // Prints a (const) char/wchar_t array of 'len' elements, starting at address // 'begin'. CharType must be either char or wchar_t. template <typename CharType> GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static void UniversalPrintCharArray(const CharType* begin, size_t len, ostream* os) { … } // Prints a (const) char array of 'len' elements, starting at address 'begin'. void UniversalPrintArray(const char* begin, size_t len, ostream* os) { … } #ifdef __cpp_lib_char8_t // Prints a (const) char8_t array of 'len' elements, starting at address // 'begin'. void UniversalPrintArray(const char8_t* begin, size_t len, ostream* os) { … } #endif // Prints a (const) char16_t array of 'len' elements, starting at address // 'begin'. void UniversalPrintArray(const char16_t* begin, size_t len, ostream* os) { … } // Prints a (const) char32_t array of 'len' elements, starting at address // 'begin'. void UniversalPrintArray(const char32_t* begin, size_t len, ostream* os) { … } // Prints a (const) wchar_t array of 'len' elements, starting at address // 'begin'. void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) { … } namespace { // Prints a null-terminated C-style string to the ostream. template <typename Char> void PrintCStringTo(const Char* s, ostream* os) { … } } // anonymous namespace void PrintTo(const char* s, ostream* os) { … } #ifdef __cpp_lib_char8_t void PrintTo(const char8_t* s, ostream* os) { … } #endif void PrintTo(const char16_t* s, ostream* os) { … } void PrintTo(const char32_t* s, ostream* os) { … } // MSVC compiler can be configured to define whar_t as a typedef // of unsigned short. Defining an overload for const wchar_t* in that case // would cause pointers to unsigned shorts be printed as wide strings, // possibly accessing more memory than intended and causing invalid // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when // wchar_t is implemented as a native type. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) // Prints the given wide C string to the ostream. void PrintTo(const wchar_t* s, ostream* os) { … } #endif // wchar_t is native namespace { bool ContainsUnprintableControlCodes(const char* str, size_t length) { … } bool IsUTF8TrailByte(unsigned char t) { … } bool IsValidUTF8(const char* str, size_t length) { … } void ConditionalPrintAsText(const char* str, size_t length, ostream* os) { … } } // anonymous namespace void PrintStringTo(const ::std::string& s, ostream* os) { … } #ifdef __cpp_lib_char8_t void PrintU8StringTo(const ::std::u8string& s, ostream* os) { … } #endif void PrintU16StringTo(const ::std::u16string& s, ostream* os) { … } void PrintU32StringTo(const ::std::u32string& s, ostream* os) { … } #if GTEST_HAS_STD_WSTRING void PrintWideStringTo(const ::std::wstring& s, ostream* os) { … } #endif // GTEST_HAS_STD_WSTRING } // namespace internal } // namespace testing