Index: include/llvm/Support/Format.h =================================================================== --- include/llvm/Support/Format.h +++ include/llvm/Support/Format.h @@ -23,6 +23,7 @@ #ifndef LLVM_SUPPORT_FORMAT_H #define LLVM_SUPPORT_FORMAT_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/DataTypes.h" @@ -202,6 +203,35 @@ return FormattedNumber(0, N, Width, false, false, false); } +class FormattedHexBytes { + llvm::ArrayRef Bytes; + uint64_t Offset; // If not -1ULL start each line with an appropriate offset. + uint32_t NumPerLine; // Number of bytes to show per line. + bool Upper; // Show hex bytes as upper case. + bool ASCII; // Show the ASCII bytes for the hex bytes to the right. + friend class raw_ostream; + +public: + FormattedHexBytes(const void *P, size_t N, uint64_t Offset = -1ULL, + uint32_t NPL = 32, bool U = false, bool A = false) + : Bytes(), Offset(Offset), NumPerLine(NPL), Upper(U), ASCII(A) { + if (P && N > 0) + Bytes = llvm::ArrayRef(static_cast(P), N); + } +}; + +inline FormattedHexBytes format_hex_bytes(const void *P, size_t N, + uint64_t Offset = -1ULL, + uint32_t NumPerLine = 16) { + return FormattedHexBytes(P, N, Offset, NumPerLine); +} + +inline FormattedHexBytes format_hex_bytes_with_ascii(const void *P, size_t N, + uint64_t Offset = -1ULL, + uint32_t NumPerLine = 16) { + return FormattedHexBytes(P, N, Offset, NumPerLine, false, true); +} + } // end namespace llvm #endif Index: include/llvm/Support/raw_ostream.h =================================================================== --- include/llvm/Support/raw_ostream.h +++ include/llvm/Support/raw_ostream.h @@ -23,6 +23,7 @@ class format_object_base; class FormattedString; class FormattedNumber; +class FormattedHexBytes; template class SmallVectorImpl; namespace sys { @@ -222,6 +223,9 @@ // Formatted output, see the formatHex() function in Support/Format.h. raw_ostream &operator<<(const FormattedNumber &); + // Formatted output, see the formatHex() function in Support/Format.h. + raw_ostream &operator<<(const FormattedHexBytes &); + /// indent - Insert 'NumSpaces' spaces. raw_ostream &indent(unsigned NumSpaces); Index: lib/Support/raw_ostream.cpp =================================================================== --- lib/Support/raw_ostream.cpp +++ lib/Support/raw_ostream.cpp @@ -352,6 +352,57 @@ return *this; } +raw_ostream &raw_ostream::operator<<(const FormattedHexBytes &FB) { + size_t LineIndex = 0; + uint64_t Offset = FB.Offset; + const size_t Size = FB.Bytes.size(); + HexPrintStyle OffsetStyle = + FB.Upper ? HexPrintStyle::PrefixUpper : HexPrintStyle::PrefixLower; + HexPrintStyle ByteStyle = + FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower; + while (LineIndex < Size) { + if (Offset != -1ULL) { + llvm::write_hex(*this, Offset + LineIndex, OffsetStyle, + sizeof(Offset) * 2 + 2); + *this << ": "; + } + // Print the hex bytes for this line + uint32_t i = 0; + for (i = 0; i < FB.NumPerLine; ++i) { + size_t Index = LineIndex + i; + if (Index < Size) { + if (i) + *this << " "; + llvm::write_hex(*this, FB.Bytes[Index], ByteStyle, 2); + } else { + break; + } + } + uint32_t BytesDisplayed = i; + if (FB.ASCII) { + // Print 3 spaces for any bytes that we didn't print on this line so that + // the ASCII bytes always align up correctly and 2 bytes space between + // the hex bytes and the ASCII bytes. + indent(3 * (FB.NumPerLine - i) + 2); + // Print the ASCII char values for each byte on this line + for (uint32_t i = 0; i < FB.NumPerLine; ++i) { + size_t Index = LineIndex + i; + if (Index < Size) { + char ch = (char)FB.Bytes[Index]; + if (isprint(ch)) + *this << ch; + else + *this << '.'; + } + } + } + LineIndex += BytesDisplayed; + if (LineIndex < Size) + *this << '\n'; + } + return *this; +} + /// indent - Insert 'NumSpaces' spaces. raw_ostream &raw_ostream::indent(unsigned NumSpaces) { static const char Spaces[] = " " Index: unittests/Support/NativeFormatTests.cpp =================================================================== --- unittests/Support/NativeFormatTests.cpp +++ unittests/Support/NativeFormatTests.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/SmallString.h" +#include "llvm/Support/Format.h" #include "llvm/Support/NativeFormatting.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" @@ -44,6 +45,25 @@ return S; } +std::string format_hex_bytes(const void *P, size_t N, uint64_t Offset = -1ULL, + uint32_t NumPerLine = 16) { + std::string S; + llvm::raw_string_ostream Str(S); + Str << llvm::format_hex_bytes(P, N, Offset, NumPerLine); + Str.flush(); + return S; +} + +std::string format_hex_bytes_with_ascii(const void *P, size_t N, + uint64_t Offset = -1ULL, + uint32_t NumPerLine = 16) { + std::string S; + llvm::raw_string_ostream Str(S); + Str << llvm::format_hex_bytes_with_ascii(P, N, Offset, NumPerLine); + Str.flush(); + return S; +} + // Test basic number formatting with various styles and default width and // precision. TEST(NativeFormatTest, BasicIntegerTests) { @@ -150,6 +170,73 @@ format_number(0xABCDE, HexPrintStyle::PrefixLower, 21)); } +TEST(NativeFormatTest, HexByteTests) { + char b[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', + 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', + 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + // Test raw bytes + const uint64_t InvalidOffset = -1ULL; + // Test invalid input. + EXPECT_EQ("", format_hex_bytes(nullptr, 0)); + EXPECT_EQ("", format_hex_bytes(b, 0)); + EXPECT_EQ("", format_hex_bytes(nullptr, 16)); + //---------------------------------------------------------------------- + // Test simple hex byte output + //---------------------------------------------------------------------- + EXPECT_EQ("61", format_hex_bytes(b, 1)); + EXPECT_EQ("61 62", format_hex_bytes(b, 2)); + // Test that 16 bytes get written to a line correctly. + EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70", + format_hex_bytes(b, 16)); + // Test raw bytes with default 16 bytes per line wrapping. + EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70\n71", + format_hex_bytes(b, 17)); + // Test raw bytes with 1 bytes per line wrapping. + EXPECT_EQ("61\n62\n63\n64\n65\n66", format_hex_bytes(b, 6, InvalidOffset, 1)); + // Test raw bytes with 7 bytes per line wrapping. + EXPECT_EQ("61 62 63 64 65 66 67\n68 69 6a 6b 6c 6d 6e\n6f 70 71", + format_hex_bytes(b, 17, InvalidOffset, 7)); + // Test raw bytes with 8 bytes per line wrapping. + EXPECT_EQ("61 62 63 64 65 66 67 68\n69 6a 6b 6c 6d 6e 6f 70\n71", + format_hex_bytes(b, 17, InvalidOffset, 8)); + //---------------------------------------------------------------------- + // Test hex bytes with offset + //---------------------------------------------------------------------- + EXPECT_EQ("0x0000000000000000: 61", format_hex_bytes(b, 1, 0x0)); + EXPECT_EQ("0x0000000000001000: 61", format_hex_bytes(b, 1, 0x1000)); + EXPECT_EQ("0x0000000000001000: 61\n0x0000000000001001: 62", + format_hex_bytes(b, 2, 0x1000, 1)); + //---------------------------------------------------------------------- + // Test hex bytes with ASCII. + //---------------------------------------------------------------------- + EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop", + format_hex_bytes_with_ascii(b, 16)); + EXPECT_EQ("61 62 63 64 65 66 67 68 abcdefgh\n" + "69 6a 6b 6c 6d 6e 6f 70 ijklmnop", + format_hex_bytes_with_ascii(b, 16, InvalidOffset, 8)); + EXPECT_EQ("61 62 63 64 65 66 67 68 abcdefgh\n" + "69 6a 6b 6c ijkl", + format_hex_bytes_with_ascii(b, 12, InvalidOffset, 8)); + char unprintable[] = {'a', '\x1e', 'b', '\x1f'}; + // Make sure the ASCII is still lined up correctly when fewer bytes than 16 + // bytes per line are available. The ASCII should still be aligned as if 16 + // bytes of hex might be displayed. + EXPECT_EQ("61 1e 62 1f a.b.", + format_hex_bytes_with_ascii(unprintable, 4)); + //---------------------------------------------------------------------- + // Test hex bytes with ASCII with offsets. + //---------------------------------------------------------------------- + EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 " + "69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop", + format_hex_bytes_with_ascii(b, 16, 0)); + EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 abcdefgh\n" + "0x0000000000000008: 69 6a 6b 6c 6d 6e 6f 70 ijklmnop", + format_hex_bytes_with_ascii(b, 16, 0, 8)); + EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 abcdefg\n" + "0x0000000000000007: 68 69 6a 6b 6c hijkl", + format_hex_bytes_with_ascii(b, 12, 0, 7)); +} + TEST(NativeFormatTest, IntegerTests) { EXPECT_EQ("-10", format_number(-10, IntegerStyle::Integer)); EXPECT_EQ("-100", format_number(-100, IntegerStyle::Integer));