gtest-printers.cc 15 KB

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