Path: blob/master/dep/googletest/src/gtest-printers.cc
4804 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Test - The Google C++ Testing and Mocking Framework30//31// This file implements a universal value printer that can print a32// value of any type T:33//34// void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);35//36// It uses the << operator when possible, and prints the bytes in the37// object otherwise. A user can override its behavior for a class38// type Foo by defining either operator<<(::std::ostream&, const Foo&)39// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that40// defines Foo.4142#include "gtest/gtest-printers.h"4344#include <stdio.h>4546#include <cctype>47#include <cstdint>48#include <cwchar>49#include <iomanip>50#include <ios>51#include <ostream> // NOLINT52#include <string>53#include <type_traits>5455#include "gtest/internal/gtest-port.h"56#include "src/gtest-internal-inl.h"5758namespace testing {5960namespace {6162using ::std::ostream;6364// Prints a segment of bytes in the given object.65GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_66GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_67GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_68GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_69void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,70size_t count, ostream* os) {71char text[5] = "";72for (size_t i = 0; i != count; i++) {73const size_t j = start + i;74if (i != 0) {75// Organizes the bytes into groups of 2 for easy parsing by76// human.77if ((j % 2) == 0)78*os << ' ';79else80*os << '-';81}82GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);83*os << text;84}85}8687// Prints the bytes in the given value to the given ostream.88void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,89ostream* os) {90// Tells the user how big the object is.91*os << count << "-byte object <";9293const size_t kThreshold = 132;94const size_t kChunkSize = 64;95// If the object size is bigger than kThreshold, we'll have to omit96// some details by printing only the first and the last kChunkSize97// bytes.98if (count < kThreshold) {99PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);100} else {101PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);102*os << " ... ";103// Rounds up to 2-byte boundary.104const size_t resume_pos = (count - kChunkSize + 1) / 2 * 2;105PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);106}107*os << ">";108}109110// Helpers for widening a character to char32_t. Since the standard does not111// specify if char / wchar_t is signed or unsigned, it is important to first112// convert it to the unsigned type of the same width before widening it to113// char32_t.114template <typename CharType>115char32_t ToChar32(CharType in) {116return static_cast<char32_t>(117static_cast<typename std::make_unsigned<CharType>::type>(in));118}119120} // namespace121122namespace internal {123124// Delegates to PrintBytesInObjectToImpl() to print the bytes in the125// given object. The delegation simplifies the implementation, which126// uses the << operator and thus is easier done outside of the127// ::testing::internal namespace, which contains a << operator that128// sometimes conflicts with the one in STL.129void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,130ostream* os) {131PrintBytesInObjectToImpl(obj_bytes, count, os);132}133134// Depending on the value of a char (or wchar_t), we print it in one135// of three formats:136// - as is if it's a printable ASCII (e.g. 'a', '2', ' '),137// - as a hexadecimal escape sequence (e.g. '\x7F'), or138// - as a special escape sequence (e.g. '\r', '\n').139enum CharFormat { kAsIs, kHexEscape, kSpecialEscape };140141// Returns true if c is a printable ASCII character. We test the142// value of c directly instead of calling isprint(), which is buggy on143// Windows Mobile.144inline bool IsPrintableAscii(char32_t c) { return 0x20 <= c && c <= 0x7E; }145146// Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a147// character literal without the quotes, escaping it when necessary; returns how148// c was formatted.149template <typename Char>150static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {151const char32_t u_c = ToChar32(c);152switch (u_c) {153case L'\0':154*os << "\\0";155break;156case L'\'':157*os << "\\'";158break;159case L'\\':160*os << "\\\\";161break;162case L'\a':163*os << "\\a";164break;165case L'\b':166*os << "\\b";167break;168case L'\f':169*os << "\\f";170break;171case L'\n':172*os << "\\n";173break;174case L'\r':175*os << "\\r";176break;177case L'\t':178*os << "\\t";179break;180case L'\v':181*os << "\\v";182break;183default:184if (IsPrintableAscii(u_c)) {185*os << static_cast<char>(c);186return kAsIs;187} else {188ostream::fmtflags flags = os->flags();189*os << "\\x" << std::hex << std::uppercase << static_cast<int>(u_c);190os->flags(flags);191return kHexEscape;192}193}194return kSpecialEscape;195}196197// Prints a char32_t c as if it's part of a string literal, escaping it when198// necessary; returns how c was formatted.199static CharFormat PrintAsStringLiteralTo(char32_t c, ostream* os) {200switch (c) {201case L'\'':202*os << "'";203return kAsIs;204case L'"':205*os << "\\\"";206return kSpecialEscape;207default:208return PrintAsCharLiteralTo(c, os);209}210}211212static const char* GetCharWidthPrefix(char) { return ""; }213214static const char* GetCharWidthPrefix(signed char) { return ""; }215216static const char* GetCharWidthPrefix(unsigned char) { return ""; }217218#ifdef __cpp_lib_char8_t219static const char* GetCharWidthPrefix(char8_t) { return "u8"; }220#endif221222static const char* GetCharWidthPrefix(char16_t) { return "u"; }223224static const char* GetCharWidthPrefix(char32_t) { return "U"; }225226static const char* GetCharWidthPrefix(wchar_t) { return "L"; }227228// Prints a char c as if it's part of a string literal, escaping it when229// necessary; returns how c was formatted.230static CharFormat PrintAsStringLiteralTo(char c, ostream* os) {231return PrintAsStringLiteralTo(ToChar32(c), os);232}233234#ifdef __cpp_lib_char8_t235static CharFormat PrintAsStringLiteralTo(char8_t c, ostream* os) {236return PrintAsStringLiteralTo(ToChar32(c), os);237}238#endif239240static CharFormat PrintAsStringLiteralTo(char16_t c, ostream* os) {241return PrintAsStringLiteralTo(ToChar32(c), os);242}243244static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {245return PrintAsStringLiteralTo(ToChar32(c), os);246}247248// Prints a character c (of type char, char8_t, char16_t, char32_t, or wchar_t)249// and its code. '\0' is printed as "'\\0'", other unprintable characters are250// also properly escaped using the standard C++ escape sequence.251template <typename Char>252void PrintCharAndCodeTo(Char c, ostream* os) {253// First, print c as a literal in the most readable form we can find.254*os << GetCharWidthPrefix(c) << "'";255const CharFormat format = PrintAsCharLiteralTo(c, os);256*os << "'";257258// To aid user debugging, we also print c's code in decimal, unless259// it's 0 (in which case c was printed as '\\0', making the code260// obvious).261if (c == 0) return;262*os << " (" << static_cast<int>(c);263264// For more convenience, we print c's code again in hexadecimal,265// unless c was already printed in the form '\x##' or the code is in266// [1, 9].267if (format == kHexEscape || (1 <= c && c <= 9)) {268// Do nothing.269} else {270*os << ", 0x" << String::FormatHexInt(static_cast<int>(c));271}272*os << ")";273}274275void PrintTo(unsigned char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); }276void PrintTo(signed char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); }277278// Prints a wchar_t as a symbol if it is printable or as its internal279// code otherwise and also as its code. L'\0' is printed as "L'\\0'".280void PrintTo(wchar_t wc, ostream* os) { PrintCharAndCodeTo(wc, os); }281282// TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well.283void PrintTo(char32_t c, ::std::ostream* os) {284*os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4)285<< static_cast<uint32_t>(c);286}287288// gcc/clang __{u,}int128_t289#if defined(__SIZEOF_INT128__)290void PrintTo(__uint128_t v, ::std::ostream* os) {291if (v == 0) {292*os << "0";293return;294}295296// Buffer large enough for ceil(log10(2^128))==39 and the null terminator297char buf[40];298char* p = buf + sizeof(buf);299300// Some configurations have a __uint128_t, but no support for built in301// division. Do manual long division instead.302303uint64_t high = static_cast<uint64_t>(v >> 64);304uint64_t low = static_cast<uint64_t>(v);305306*--p = 0;307while (high != 0 || low != 0) {308uint64_t high_mod = high % 10;309high = high / 10;310// This is the long division algorithm specialized for a divisor of 10 and311// only two elements.312// Notable values:313// 2^64 / 10 == 1844674407370955161314// 2^64 % 10 == 6315const uint64_t carry = 6 * high_mod + low % 10;316low = low / 10 + high_mod * 1844674407370955161 + carry / 10;317318char digit = static_cast<char>(carry % 10);319*--p = static_cast<char>('0' + digit);320}321*os << p;322}323void PrintTo(__int128_t v, ::std::ostream* os) {324__uint128_t uv = static_cast<__uint128_t>(v);325if (v < 0) {326*os << "-";327uv = -uv;328}329PrintTo(uv, os);330}331#endif // __SIZEOF_INT128__332333// Prints the given array of characters to the ostream. CharType must be either334// char, char8_t, char16_t, char32_t, or wchar_t.335// The array starts at begin, the length is len, it may include '\0' characters336// and may not be NUL-terminated.337template <typename CharType>338GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_339GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_340GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat341PrintCharsAsStringTo(const CharType* begin, size_t len, ostream* os) {342const char* const quote_prefix = GetCharWidthPrefix(*begin);343*os << quote_prefix << "\"";344bool is_previous_hex = false;345CharFormat print_format = kAsIs;346for (size_t index = 0; index < len; ++index) {347const CharType cur = begin[index];348if (is_previous_hex && IsXDigit(cur)) {349// Previous character is of '\x..' form and this character can be350// interpreted as another hexadecimal digit in its number. Break string to351// disambiguate.352*os << "\" " << quote_prefix << "\"";353}354is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;355// Remember if any characters required hex escaping.356if (is_previous_hex) {357print_format = kHexEscape;358}359}360*os << "\"";361return print_format;362}363364// Prints a (const) char/wchar_t array of 'len' elements, starting at address365// 'begin'. CharType must be either char or wchar_t.366template <typename CharType>367GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_368GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_369GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static void370UniversalPrintCharArray(const CharType* begin, size_t len,371ostream* os) {372// The code373// const char kFoo[] = "foo";374// generates an array of 4, not 3, elements, with the last one being '\0'.375//376// Therefore when printing a char array, we don't print the last element if377// it's '\0', such that the output matches the string literal as it's378// written in the source code.379if (len > 0 && begin[len - 1] == '\0') {380PrintCharsAsStringTo(begin, len - 1, os);381return;382}383384// If, however, the last element in the array is not '\0', e.g.385// const char kFoo[] = { 'f', 'o', 'o' };386// we must print the entire array. We also print a message to indicate387// that the array is not NUL-terminated.388PrintCharsAsStringTo(begin, len, os);389*os << " (no terminating NUL)";390}391392// Prints a (const) char array of 'len' elements, starting at address 'begin'.393void UniversalPrintArray(const char* begin, size_t len, ostream* os) {394UniversalPrintCharArray(begin, len, os);395}396397#ifdef __cpp_lib_char8_t398// Prints a (const) char8_t array of 'len' elements, starting at address399// 'begin'.400void UniversalPrintArray(const char8_t* begin, size_t len, ostream* os) {401UniversalPrintCharArray(begin, len, os);402}403#endif404405// Prints a (const) char16_t array of 'len' elements, starting at address406// 'begin'.407void UniversalPrintArray(const char16_t* begin, size_t len, ostream* os) {408UniversalPrintCharArray(begin, len, os);409}410411// Prints a (const) char32_t array of 'len' elements, starting at address412// 'begin'.413void UniversalPrintArray(const char32_t* begin, size_t len, ostream* os) {414UniversalPrintCharArray(begin, len, os);415}416417// Prints a (const) wchar_t array of 'len' elements, starting at address418// 'begin'.419void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {420UniversalPrintCharArray(begin, len, os);421}422423namespace {424425// Prints a null-terminated C-style string to the ostream.426template <typename Char>427void PrintCStringTo(const Char* s, ostream* os) {428if (s == nullptr) {429*os << "NULL";430} else {431*os << ImplicitCast_<const void*>(s) << " pointing to ";432PrintCharsAsStringTo(s, std::char_traits<Char>::length(s), os);433}434}435436} // anonymous namespace437438void PrintTo(const char* s, ostream* os) { PrintCStringTo(s, os); }439440#ifdef __cpp_lib_char8_t441void PrintTo(const char8_t* s, ostream* os) { PrintCStringTo(s, os); }442#endif443444void PrintTo(const char16_t* s, ostream* os) { PrintCStringTo(s, os); }445446void PrintTo(const char32_t* s, ostream* os) { PrintCStringTo(s, os); }447448// MSVC compiler can be configured to define whar_t as a typedef449// of unsigned short. Defining an overload for const wchar_t* in that case450// would cause pointers to unsigned shorts be printed as wide strings,451// possibly accessing more memory than intended and causing invalid452// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when453// wchar_t is implemented as a native type.454#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)455// Prints the given wide C string to the ostream.456void PrintTo(const wchar_t* s, ostream* os) { PrintCStringTo(s, os); }457#endif // wchar_t is native458459namespace {460461bool ContainsUnprintableControlCodes(const char* str, size_t length) {462const unsigned char* s = reinterpret_cast<const unsigned char*>(str);463464for (size_t i = 0; i < length; i++) {465unsigned char ch = *s++;466if (std::iscntrl(ch)) {467switch (ch) {468case '\t':469case '\n':470case '\r':471break;472default:473return true;474}475}476}477return false;478}479480bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t <= 0xbf; }481482bool IsValidUTF8(const char* str, size_t length) {483const unsigned char* s = reinterpret_cast<const unsigned char*>(str);484485for (size_t i = 0; i < length;) {486unsigned char lead = s[i++];487488if (lead <= 0x7f) {489continue; // single-byte character (ASCII) 0..7F490}491if (lead < 0xc2) {492return false; // trail byte or non-shortest form493} else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) {494++i; // 2-byte character495} else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&496IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) &&497// check for non-shortest form and surrogate498(lead != 0xe0 || s[i] >= 0xa0) &&499(lead != 0xed || s[i] < 0xa0)) {500i += 2; // 3-byte character501} else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&502IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) &&503IsUTF8TrailByte(s[i + 2]) &&504// check for non-shortest form505(lead != 0xf0 || s[i] >= 0x90) &&506(lead != 0xf4 || s[i] < 0x90)) {507i += 3; // 4-byte character508} else {509return false;510}511}512return true;513}514515void ConditionalPrintAsText(const char* str, size_t length, ostream* os) {516if (!ContainsUnprintableControlCodes(str, length) &&517IsValidUTF8(str, length)) {518*os << "\n As Text: \"" << str << "\"";519}520}521522} // anonymous namespace523524void PrintStringTo(const ::std::string& s, ostream* os) {525if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {526if (GTEST_FLAG_GET(print_utf8)) {527ConditionalPrintAsText(s.data(), s.size(), os);528}529}530}531532#ifdef __cpp_lib_char8_t533void PrintU8StringTo(const ::std::u8string& s, ostream* os) {534PrintCharsAsStringTo(s.data(), s.size(), os);535}536#endif537538void PrintU16StringTo(const ::std::u16string& s, ostream* os) {539PrintCharsAsStringTo(s.data(), s.size(), os);540}541542void PrintU32StringTo(const ::std::u32string& s, ostream* os) {543PrintCharsAsStringTo(s.data(), s.size(), os);544}545546#if GTEST_HAS_STD_WSTRING547void PrintWideStringTo(const ::std::wstring& s, ostream* os) {548PrintCharsAsStringTo(s.data(), s.size(), os);549}550#endif // GTEST_HAS_STD_WSTRING551552} // namespace internal553554} // namespace testing555556557