gtest-printers.cc (14661B)
1 // Copyright 2007, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 31 // Google Test - The Google C++ Testing and Mocking Framework 32 // 33 // This file implements a universal value printer that can print a 34 // value of any type T: 35 // 36 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr); 37 // 38 // It uses the << operator when possible, and prints the bytes in the 39 // object otherwise. A user can override its behavior for a class 40 // type Foo by defining either operator<<(::std::ostream&, const Foo&) 41 // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that 42 // defines Foo. 43 44 #include "gtest/gtest-printers.h" 45 #include <stdio.h> 46 #include <cctype> 47 #include <cwchar> 48 #include <ostream> // NOLINT 49 #include <string> 50 #include "gtest/internal/gtest-port.h" 51 #include "src/gtest-internal-inl.h" 52 53 namespace testing { 54 55 namespace { 56 57 using ::std::ostream; 58 59 // Prints a segment of bytes in the given object. 60 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ 61 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ 62 GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 63 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ 64 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, 65 size_t count, ostream* os) { 66 char text[5] = ""; 67 for (size_t i = 0; i != count; i++) { 68 const size_t j = start + i; 69 if (i != 0) { 70 // Organizes the bytes into groups of 2 for easy parsing by 71 // human. 72 if ((j % 2) == 0) 73 *os << ' '; 74 else 75 *os << '-'; 76 } 77 GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]); 78 *os << text; 79 } 80 } 81 82 // Prints the bytes in the given value to the given ostream. 83 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count, 84 ostream* os) { 85 // Tells the user how big the object is. 86 *os << count << "-byte object <"; 87 88 const size_t kThreshold = 132; 89 const size_t kChunkSize = 64; 90 // If the object size is bigger than kThreshold, we'll have to omit 91 // some details by printing only the first and the last kChunkSize 92 // bytes. 93 if (count < kThreshold) { 94 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); 95 } else { 96 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); 97 *os << " ... "; 98 // Rounds up to 2-byte boundary. 99 const size_t resume_pos = (count - kChunkSize + 1)/2*2; 100 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); 101 } 102 *os << ">"; 103 } 104 105 } // namespace 106 107 namespace internal { 108 109 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the 110 // given object. The delegation simplifies the implementation, which 111 // uses the << operator and thus is easier done outside of the 112 // ::testing::internal namespace, which contains a << operator that 113 // sometimes conflicts with the one in STL. 114 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, 115 ostream* os) { 116 PrintBytesInObjectToImpl(obj_bytes, count, os); 117 } 118 119 // Depending on the value of a char (or wchar_t), we print it in one 120 // of three formats: 121 // - as is if it's a printable ASCII (e.g. 'a', '2', ' '), 122 // - as a hexadecimal escape sequence (e.g. '\x7F'), or 123 // - as a special escape sequence (e.g. '\r', '\n'). 124 enum CharFormat { 125 kAsIs, 126 kHexEscape, 127 kSpecialEscape 128 }; 129 130 // Returns true if c is a printable ASCII character. We test the 131 // value of c directly instead of calling isprint(), which is buggy on 132 // Windows Mobile. 133 inline bool IsPrintableAscii(wchar_t c) { 134 return 0x20 <= c && c <= 0x7E; 135 } 136 137 // Prints a wide or narrow char c as a character literal without the 138 // quotes, escaping it when necessary; returns how c was formatted. 139 // The template argument UnsignedChar is the unsigned version of Char, 140 // which is the type of c. 141 template <typename UnsignedChar, typename Char> 142 static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) { 143 wchar_t w_c = static_cast<wchar_t>(c); 144 switch (w_c) { 145 case L'\0': 146 *os << "\\0"; 147 break; 148 case L'\'': 149 *os << "\\'"; 150 break; 151 case L'\\': 152 *os << "\\\\"; 153 break; 154 case L'\a': 155 *os << "\\a"; 156 break; 157 case L'\b': 158 *os << "\\b"; 159 break; 160 case L'\f': 161 *os << "\\f"; 162 break; 163 case L'\n': 164 *os << "\\n"; 165 break; 166 case L'\r': 167 *os << "\\r"; 168 break; 169 case L'\t': 170 *os << "\\t"; 171 break; 172 case L'\v': 173 *os << "\\v"; 174 break; 175 default: 176 if (IsPrintableAscii(w_c)) { 177 *os << static_cast<char>(c); 178 return kAsIs; 179 } else { 180 ostream::fmtflags flags = os->flags(); 181 *os << "\\x" << std::hex << std::uppercase 182 << static_cast<int>(static_cast<UnsignedChar>(c)); 183 os->flags(flags); 184 return kHexEscape; 185 } 186 } 187 return kSpecialEscape; 188 } 189 190 // Prints a wchar_t c as if it's part of a string literal, escaping it when 191 // necessary; returns how c was formatted. 192 static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) { 193 switch (c) { 194 case L'\'': 195 *os << "'"; 196 return kAsIs; 197 case L'"': 198 *os << "\\\""; 199 return kSpecialEscape; 200 default: 201 return PrintAsCharLiteralTo<wchar_t>(c, os); 202 } 203 } 204 205 // Prints a char c as if it's part of a string literal, escaping it when 206 // necessary; returns how c was formatted. 207 static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { 208 return PrintAsStringLiteralTo( 209 static_cast<wchar_t>(static_cast<unsigned char>(c)), os); 210 } 211 212 // Prints a wide or narrow character c and its code. '\0' is printed 213 // as "'\\0'", other unprintable characters are also properly escaped 214 // using the standard C++ escape sequence. The template argument 215 // UnsignedChar is the unsigned version of Char, which is the type of c. 216 template <typename UnsignedChar, typename Char> 217 void PrintCharAndCodeTo(Char c, ostream* os) { 218 // First, print c as a literal in the most readable form we can find. 219 *os << ((sizeof(c) > 1) ? "L'" : "'"); 220 const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os); 221 *os << "'"; 222 223 // To aid user debugging, we also print c's code in decimal, unless 224 // it's 0 (in which case c was printed as '\\0', making the code 225 // obvious). 226 if (c == 0) 227 return; 228 *os << " (" << static_cast<int>(c); 229 230 // For more convenience, we print c's code again in hexadecimal, 231 // unless c was already printed in the form '\x##' or the code is in 232 // [1, 9]. 233 if (format == kHexEscape || (1 <= c && c <= 9)) { 234 // Do nothing. 235 } else { 236 *os << ", 0x" << String::FormatHexInt(static_cast<int>(c)); 237 } 238 *os << ")"; 239 } 240 241 void PrintTo(unsigned char c, ::std::ostream* os) { 242 PrintCharAndCodeTo<unsigned char>(c, os); 243 } 244 void PrintTo(signed char c, ::std::ostream* os) { 245 PrintCharAndCodeTo<unsigned char>(c, os); 246 } 247 248 // Prints a wchar_t as a symbol if it is printable or as its internal 249 // code otherwise and also as its code. L'\0' is printed as "L'\\0'". 250 void PrintTo(wchar_t wc, ostream* os) { 251 PrintCharAndCodeTo<wchar_t>(wc, os); 252 } 253 254 // Prints the given array of characters to the ostream. CharType must be either 255 // char or wchar_t. 256 // The array starts at begin, the length is len, it may include '\0' characters 257 // and may not be NUL-terminated. 258 template <typename CharType> 259 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ 260 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ 261 GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 262 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ 263 static CharFormat PrintCharsAsStringTo( 264 const CharType* begin, size_t len, ostream* os) { 265 const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\""; 266 *os << kQuoteBegin; 267 bool is_previous_hex = false; 268 CharFormat print_format = kAsIs; 269 for (size_t index = 0; index < len; ++index) { 270 const CharType cur = begin[index]; 271 if (is_previous_hex && IsXDigit(cur)) { 272 // Previous character is of '\x..' form and this character can be 273 // interpreted as another hexadecimal digit in its number. Break string to 274 // disambiguate. 275 *os << "\" " << kQuoteBegin; 276 } 277 is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape; 278 // Remember if any characters required hex escaping. 279 if (is_previous_hex) { 280 print_format = kHexEscape; 281 } 282 } 283 *os << "\""; 284 return print_format; 285 } 286 287 // Prints a (const) char/wchar_t array of 'len' elements, starting at address 288 // 'begin'. CharType must be either char or wchar_t. 289 template <typename CharType> 290 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ 291 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ 292 GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 293 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ 294 static void UniversalPrintCharArray( 295 const CharType* begin, size_t len, ostream* os) { 296 // The code 297 // const char kFoo[] = "foo"; 298 // generates an array of 4, not 3, elements, with the last one being '\0'. 299 // 300 // Therefore when printing a char array, we don't print the last element if 301 // it's '\0', such that the output matches the string literal as it's 302 // written in the source code. 303 if (len > 0 && begin[len - 1] == '\0') { 304 PrintCharsAsStringTo(begin, len - 1, os); 305 return; 306 } 307 308 // If, however, the last element in the array is not '\0', e.g. 309 // const char kFoo[] = { 'f', 'o', 'o' }; 310 // we must print the entire array. We also print a message to indicate 311 // that the array is not NUL-terminated. 312 PrintCharsAsStringTo(begin, len, os); 313 *os << " (no terminating NUL)"; 314 } 315 316 // Prints a (const) char array of 'len' elements, starting at address 'begin'. 317 void UniversalPrintArray(const char* begin, size_t len, ostream* os) { 318 UniversalPrintCharArray(begin, len, os); 319 } 320 321 // Prints a (const) wchar_t array of 'len' elements, starting at address 322 // 'begin'. 323 void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) { 324 UniversalPrintCharArray(begin, len, os); 325 } 326 327 // Prints the given C string to the ostream. 328 void PrintTo(const char* s, ostream* os) { 329 if (s == nullptr) { 330 *os << "NULL"; 331 } else { 332 *os << ImplicitCast_<const void*>(s) << " pointing to "; 333 PrintCharsAsStringTo(s, strlen(s), os); 334 } 335 } 336 337 // MSVC compiler can be configured to define whar_t as a typedef 338 // of unsigned short. Defining an overload for const wchar_t* in that case 339 // would cause pointers to unsigned shorts be printed as wide strings, 340 // possibly accessing more memory than intended and causing invalid 341 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when 342 // wchar_t is implemented as a native type. 343 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 344 // Prints the given wide C string to the ostream. 345 void PrintTo(const wchar_t* s, ostream* os) { 346 if (s == nullptr) { 347 *os << "NULL"; 348 } else { 349 *os << ImplicitCast_<const void*>(s) << " pointing to "; 350 PrintCharsAsStringTo(s, wcslen(s), os); 351 } 352 } 353 #endif // wchar_t is native 354 355 namespace { 356 357 bool ContainsUnprintableControlCodes(const char* str, size_t length) { 358 const unsigned char *s = reinterpret_cast<const unsigned char *>(str); 359 360 for (size_t i = 0; i < length; i++) { 361 unsigned char ch = *s++; 362 if (std::iscntrl(ch)) { 363 switch (ch) { 364 case '\t': 365 case '\n': 366 case '\r': 367 break; 368 default: 369 return true; 370 } 371 } 372 } 373 return false; 374 } 375 376 bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t<= 0xbf; } 377 378 bool IsValidUTF8(const char* str, size_t length) { 379 const unsigned char *s = reinterpret_cast<const unsigned char *>(str); 380 381 for (size_t i = 0; i < length;) { 382 unsigned char lead = s[i++]; 383 384 if (lead <= 0x7f) { 385 continue; // single-byte character (ASCII) 0..7F 386 } 387 if (lead < 0xc2) { 388 return false; // trail byte or non-shortest form 389 } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) { 390 ++i; // 2-byte character 391 } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length && 392 IsUTF8TrailByte(s[i]) && 393 IsUTF8TrailByte(s[i + 1]) && 394 // check for non-shortest form and surrogate 395 (lead != 0xe0 || s[i] >= 0xa0) && 396 (lead != 0xed || s[i] < 0xa0)) { 397 i += 2; // 3-byte character 398 } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length && 399 IsUTF8TrailByte(s[i]) && 400 IsUTF8TrailByte(s[i + 1]) && 401 IsUTF8TrailByte(s[i + 2]) && 402 // check for non-shortest form 403 (lead != 0xf0 || s[i] >= 0x90) && 404 (lead != 0xf4 || s[i] < 0x90)) { 405 i += 3; // 4-byte character 406 } else { 407 return false; 408 } 409 } 410 return true; 411 } 412 413 void ConditionalPrintAsText(const char* str, size_t length, ostream* os) { 414 if (!ContainsUnprintableControlCodes(str, length) && 415 IsValidUTF8(str, length)) { 416 *os << "\n As Text: \"" << str << "\""; 417 } 418 } 419 420 } // anonymous namespace 421 422 void PrintStringTo(const ::std::string& s, ostream* os) { 423 if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) { 424 if (GTEST_FLAG(print_utf8)) { 425 ConditionalPrintAsText(s.data(), s.size(), os); 426 } 427 } 428 } 429 430 #if GTEST_HAS_STD_WSTRING 431 void PrintWideStringTo(const ::std::wstring& s, ostream* os) { 432 PrintCharsAsStringTo(s.data(), s.size(), os); 433 } 434 #endif // GTEST_HAS_STD_WSTRING 435 436 } // namespace internal 437 438 } // namespace testing