Index: llvm/trunk/include/llvm/Support/Format.h =================================================================== --- llvm/trunk/include/llvm/Support/Format.h +++ llvm/trunk/include/llvm/Support/Format.h @@ -203,10 +203,12 @@ return FormattedNumber(0, N, Width, false, false, false); } -class FormattedHexBytes { - llvm::ArrayRef Bytes; - // Display offsets for each line if FirstByteOffset has a value. - llvm::Optional FirstByteOffset; +class FormattedBytes { + ArrayRef Bytes; + + // If not None, display offsets for each line relative to starting value. + Optional FirstByteOffset; + uint32_t IndentLevel; // Number of characters to indent each line. uint32_t NumPerLine; // Number of bytes to show per line. uint8_t ByteGroupSize; // How many hex bytes are grouped without spaces bool Upper; // Show offset and hex bytes as upper case. @@ -214,26 +216,31 @@ friend class raw_ostream; public: - FormattedHexBytes(llvm::ArrayRef B, llvm::Optional O, - uint32_t NPL, uint8_t BGS, bool U, bool A) - : Bytes(B), FirstByteOffset(O), NumPerLine(NPL), ByteGroupSize(BGS), - Upper(U), ASCII(A) {} -}; - -inline FormattedHexBytes format_hex_bytes( - llvm::ArrayRef Bytes, - llvm::Optional FirstByteOffset = llvm::Optional(), - uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) { - return FormattedHexBytes(Bytes, FirstByteOffset, NumPerLine, ByteGroupSize, - false /*Upper*/, false /*ASCII*/); -} - -inline FormattedHexBytes format_hex_bytes_with_ascii( - llvm::ArrayRef Bytes, - llvm::Optional FirstByteOffset = llvm::Optional(), - uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) { - return FormattedHexBytes(Bytes, FirstByteOffset, NumPerLine, ByteGroupSize, - false /*Upper*/, true /*ASCII*/); + FormattedBytes(ArrayRef B, uint32_t IL, Optional O, + uint32_t NPL, uint8_t BGS, bool U, bool A) + : Bytes(B), IndentLevel(IL), FirstByteOffset(O), NumPerLine(NPL), + ByteGroupSize(BGS), Upper(U), ASCII(A) { + + if (ByteGroupSize > NumPerLine) + ByteGroupSize = NumPerLine; + } +}; + +inline FormattedBytes +format_bytes(ArrayRef Bytes, Optional FirstByteOffset = None, + uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4, + uint32_t IndentLevel = 0, bool Upper = false) { + return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine, + ByteGroupSize, Upper, false); +} + +inline FormattedBytes +format_bytes_with_ascii(ArrayRef Bytes, + Optional FirstByteOffset = None, + uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4, + uint32_t IndentLevel = 0, bool Upper = false) { + return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine, + ByteGroupSize, Upper, true); } } // end namespace llvm Index: llvm/trunk/include/llvm/Support/raw_ostream.h =================================================================== --- llvm/trunk/include/llvm/Support/raw_ostream.h +++ llvm/trunk/include/llvm/Support/raw_ostream.h @@ -23,7 +23,7 @@ class format_object_base; class FormattedString; class FormattedNumber; -class FormattedHexBytes; +class FormattedBytes; template class SmallVectorImpl; namespace sys { @@ -223,8 +223,8 @@ // 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 &); + // Formatted output, see the format_bytes() function in Support/Format.h. + raw_ostream &operator<<(const FormattedBytes &); /// indent - Insert 'NumSpaces' spaces. raw_ostream &indent(unsigned NumSpaces); Index: llvm/trunk/lib/Support/ScopedPrinter.cpp =================================================================== --- llvm/trunk/lib/Support/ScopedPrinter.cpp +++ llvm/trunk/lib/Support/ScopedPrinter.cpp @@ -27,45 +27,18 @@ if (Block) { startLine() << Label; - if (Str.size() > 0) + if (!Str.empty()) OS << ": " << Str; OS << " (\n"; - for (size_t addr = 0, end = Data.size(); addr < end; addr += 16) { - startLine() << format(" %04" PRIX64 ": ", uint64_t(addr)); - // Dump line of hex. - for (size_t i = 0; i < 16; ++i) { - if (i != 0 && i % 4 == 0) - OS << ' '; - if (addr + i < end) - OS << hexdigit((Data[addr + i] >> 4) & 0xF, false) - << hexdigit(Data[addr + i] & 0xF, false); - else - OS << " "; - } - // Print ascii. - OS << " |"; - for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { - if (std::isprint(Data[addr + i] & 0xFF)) - OS << Data[addr + i]; - else - OS << "."; - } - OS << "|\n"; - } - + if (!Data.empty()) + OS << format_bytes_with_ascii(Data, 0, 16, 4, (IndentLevel + 1) * 2, true) + << "\n"; startLine() << ")\n"; } else { startLine() << Label << ":"; - if (Str.size() > 0) + if (!Str.empty()) OS << " " << Str; - OS << " ("; - for (size_t i = 0; i < Data.size(); ++i) { - if (i > 0) - OS << " "; - - OS << format("%02X", static_cast(Data[i])); - } - OS << ")\n"; + OS << " (" << format_bytes(Data, None, Data.size(), 1, 0, true) << ")\n"; } } Index: llvm/trunk/lib/Support/raw_ostream.cpp =================================================================== --- llvm/trunk/lib/Support/raw_ostream.cpp +++ llvm/trunk/lib/Support/raw_ostream.cpp @@ -352,55 +352,72 @@ return *this; } -raw_ostream &raw_ostream::operator<<(const FormattedHexBytes &FB) { +raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) { + if (FB.Bytes.empty()) + return *this; + size_t LineIndex = 0; - 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) { + auto Bytes = FB.Bytes; + const size_t Size = Bytes.size(); + HexPrintStyle HPS = FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower; + uint64_t OffsetWidth = 0; + if (FB.FirstByteOffset.hasValue()) { + // Figure out how many nibbles are needed to print the largest offset + // represented by this data set, so that we can align the offset field + // to the right width. + size_t Lines = Size / FB.NumPerLine; + uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine; + unsigned Power = 0; + if (MaxOffset > 0) + Power = llvm::Log2_64_Ceil(MaxOffset); + OffsetWidth = std::max(4ULL, llvm::alignTo(Power, 4) / 4); + } + + // The width of a block of data including all spaces for group separators. + unsigned NumByteGroups = + alignTo(FB.NumPerLine, FB.ByteGroupSize) / FB.ByteGroupSize; + unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1; + + while (!Bytes.empty()) { + indent(FB.IndentLevel); + if (FB.FirstByteOffset.hasValue()) { uint64_t Offset = FB.FirstByteOffset.getValue(); - llvm::write_hex(*this, Offset + LineIndex, OffsetStyle, - sizeof(Offset) * 2 + 2); + llvm::write_hex(*this, Offset + LineIndex, HPS, OffsetWidth); *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) - break; - if (I && (I % FB.ByteGroupSize) == 0) + + auto Line = Bytes.take_front(FB.NumPerLine); + + size_t CharsPrinted = 0; + // Print the hex bytes for this line in groups + for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) { + if (I && (I % FB.ByteGroupSize) == 0) { + ++CharsPrinted; *this << " "; - llvm::write_hex(*this, FB.Bytes[Index], ByteStyle, 2); + } + llvm::write_hex(*this, Line[I], HPS, 2); } - uint32_t BytesDisplayed = I; + if (FB.ASCII) { // Print any spaces needed for any bytes that we didn't print on this // line so that the ASCII bytes are correctly aligned. - for (; I < FB.NumPerLine; ++I) { - if (I && (I % FB.ByteGroupSize) == 0) - indent(3); - else - indent(2); - } - *this << " |"; + assert(BlockCharWidth >= CharsPrinted); + indent(BlockCharWidth - CharsPrinted + 2); + *this << "|"; + // Print the ASCII char values for each byte on this line - for (I = 0; I < FB.NumPerLine; ++I) { - size_t Index = LineIndex + I; - if (Index >= Size) - break; - char ch = (char)FB.Bytes[Index]; - if (isprint(ch)) - *this << ch; + for (uint8_t Byte : Line) { + if (isprint(Byte)) + *this << static_cast(Byte); else *this << '.'; } *this << '|'; } - LineIndex += BytesDisplayed; + + Bytes = Bytes.drop_front(Line.size()); + LineIndex += Line.size(); if (LineIndex < Size) *this << '\n'; } Index: llvm/trunk/unittests/Support/raw_ostream_test.cpp =================================================================== --- llvm/trunk/unittests/Support/raw_ostream_test.cpp +++ llvm/trunk/unittests/Support/raw_ostream_test.cpp @@ -181,157 +181,153 @@ printToString(format_decimal(INT64_MIN, 21), 21)); } -std::string -format_hex_bytes(const void *P, size_t N, - llvm::Optional Offset = llvm::Optional(), - uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) { +static std::string formatted_bytes_str(ArrayRef Bytes, + llvm::Optional Offset = None, + uint32_t NumPerLine = 16, + uint8_t ByteGroupSize = 4) { std::string S; - if (P && N) { - llvm::raw_string_ostream Str(S); - Str << llvm::format_hex_bytes( - llvm::ArrayRef(static_cast(P), N), Offset, - NumPerLine, ByteGroupSize); - Str.flush(); - } + raw_string_ostream Str(S); + Str << format_bytes(Bytes, Offset, NumPerLine, ByteGroupSize); + Str.flush(); return S; } -std::string format_hex_bytes_with_ascii( - const void *P, size_t N, - llvm::Optional Offset = llvm::Optional(), - uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) { +static std::string format_bytes_with_ascii_str(ArrayRef Bytes, + Optional Offset = None, + uint32_t NumPerLine = 16, + uint8_t ByteGroupSize = 4) { std::string S; - if (P && N) { - llvm::raw_string_ostream Str(S); - Str << llvm::format_hex_bytes_with_ascii( - llvm::ArrayRef(static_cast(P), N), Offset, - NumPerLine, ByteGroupSize); - Str.flush(); - } + raw_string_ostream Str(S); + Str << format_bytes_with_ascii(Bytes, Offset, NumPerLine, ByteGroupSize); + Str.flush(); return S; } TEST(raw_ostreamTest, FormattedHexBytes) { - 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 llvm::Optional InvalidOffset; + std::vector Buf = {'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'}; + ArrayRef B(Buf); + // Test invalid input. - EXPECT_EQ("", format_hex_bytes(nullptr, 0)); - EXPECT_EQ("", format_hex_bytes(b, 0)); - EXPECT_EQ("", format_hex_bytes(nullptr, 16)); + EXPECT_EQ("", formatted_bytes_str(ArrayRef())); + EXPECT_EQ("", format_bytes_with_ascii_str(ArrayRef())); //---------------------------------------------------------------------- // Test hex byte output with the default 4 byte groups //---------------------------------------------------------------------- - EXPECT_EQ("61", format_hex_bytes(b, 1)); - EXPECT_EQ("61626364 65", format_hex_bytes(b, 5)); + EXPECT_EQ("61", formatted_bytes_str(B.take_front())); + EXPECT_EQ("61626364 65", formatted_bytes_str(B.take_front(5))); // Test that 16 bytes get written to a line correctly. - EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70", format_hex_bytes(b, 16)); + EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70", + formatted_bytes_str(B.take_front(16))); // Test raw bytes with default 16 bytes per line wrapping. - EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71", format_hex_bytes(b, 17)); + EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71", + formatted_bytes_str(B.take_front(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)); + EXPECT_EQ("61\n62\n63\n64\n65\n66", + formatted_bytes_str(B.take_front(6), None, 1)); // Test raw bytes with 7 bytes per line wrapping. EXPECT_EQ("61626364 656667\n68696a6b 6c6d6e\n6f7071", - format_hex_bytes(b, 17, InvalidOffset, 7)); + formatted_bytes_str(B.take_front(17), None, 7)); // Test raw bytes with 8 bytes per line wrapping. EXPECT_EQ("61626364 65666768\n696a6b6c 6d6e6f70\n71", - format_hex_bytes(b, 17, InvalidOffset, 8)); + formatted_bytes_str(B.take_front(17), None, 8)); //---------------------------------------------------------------------- // Test hex byte output with the 1 byte groups //---------------------------------------------------------------------- - EXPECT_EQ("61 62 63 64 65", format_hex_bytes(b, 5, InvalidOffset, 16, 1)); + EXPECT_EQ("61 62 63 64 65", + formatted_bytes_str(B.take_front(5), None, 16, 1)); // 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, InvalidOffset, 16, 1)); + formatted_bytes_str(B.take_front(16), None, 16, 1)); // 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, InvalidOffset, 16, 1)); + formatted_bytes_str(B.take_front(17), None, 16, 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, 1)); + formatted_bytes_str(B.take_front(17), None, 7, 1)); // 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, 1)); + formatted_bytes_str(B.take_front(17), None, 8, 1)); //---------------------------------------------------------------------- // Test hex byte output with the 2 byte groups //---------------------------------------------------------------------- - EXPECT_EQ("6162 6364 65", format_hex_bytes(b, 5, InvalidOffset, 16, 2)); + EXPECT_EQ("6162 6364 65", formatted_bytes_str(B.take_front(5), None, 16, 2)); // Test that 16 bytes get written to a line correctly. EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70", - format_hex_bytes(b, 16, InvalidOffset, 16, 2)); + formatted_bytes_str(B.take_front(16), None, 16, 2)); // Test raw bytes with default 16 bytes per line wrapping. EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70\n71", - format_hex_bytes(b, 17, InvalidOffset, 16, 2)); + formatted_bytes_str(B.take_front(17), None, 16, 2)); // Test raw bytes with 7 bytes per line wrapping. EXPECT_EQ("6162 6364 6566 67\n6869 6a6b 6c6d 6e\n6f70 71", - format_hex_bytes(b, 17, InvalidOffset, 7, 2)); + formatted_bytes_str(B.take_front(17), None, 7, 2)); // Test raw bytes with 8 bytes per line wrapping. EXPECT_EQ("6162 6364 6566 6768\n696a 6b6c 6d6e 6f70\n71", - format_hex_bytes(b, 17, InvalidOffset, 8, 2)); + formatted_bytes_str(B.take_front(17), None, 8, 2)); //---------------------------------------------------------------------- // Test hex bytes with offset with the default 4 byte groups. //---------------------------------------------------------------------- - 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)); + EXPECT_EQ("0000: 61", formatted_bytes_str(B.take_front(), 0x0)); + EXPECT_EQ("1000: 61", formatted_bytes_str(B.take_front(), 0x1000)); + EXPECT_EQ("1000: 61\n1001: 62", + formatted_bytes_str(B.take_front(2), 0x1000, 1)); //---------------------------------------------------------------------- // Test hex bytes with ASCII with the default 4 byte groups. //---------------------------------------------------------------------- EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70 |abcdefghijklmnop|", - format_hex_bytes_with_ascii(b, 16)); + format_bytes_with_ascii_str(B.take_front(16))); EXPECT_EQ("61626364 65666768 |abcdefgh|\n" "696a6b6c 6d6e6f70 |ijklmnop|", - format_hex_bytes_with_ascii(b, 16, InvalidOffset, 8)); + format_bytes_with_ascii_str(B.take_front(16), None, 8)); EXPECT_EQ("61626364 65666768 |abcdefgh|\n696a6b6c |ijkl|", - format_hex_bytes_with_ascii(b, 12, InvalidOffset, 8)); - char unprintable[] = {'a', '\x1e', 'b', '\x1f'}; + format_bytes_with_ascii_str(B.take_front(12), None, 8)); + std::vector 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("611e621f |a.b.|", - format_hex_bytes_with_ascii(unprintable, 4)); + format_bytes_with_ascii_str(Unprintable)); //---------------------------------------------------------------------- // Test hex bytes with ASCII with offsets with the default 4 byte groups. //---------------------------------------------------------------------- - EXPECT_EQ("0x0000000000000000: 61626364 65666768 " + EXPECT_EQ("0000: 61626364 65666768 " "696a6b6c 6d6e6f70 |abcdefghijklmnop|", - format_hex_bytes_with_ascii(b, 16, 0)); - EXPECT_EQ("0x0000000000000000: 61626364 65666768 |abcdefgh|\n" - "0x0000000000000008: 696a6b6c 6d6e6f70 |ijklmnop|", - format_hex_bytes_with_ascii(b, 16, 0, 8)); - EXPECT_EQ("0x0000000000000000: 61626364 656667 |abcdefg|\n" - "0x0000000000000007: 68696a6b 6c |hijkl|", - format_hex_bytes_with_ascii(b, 12, 0, 7)); + format_bytes_with_ascii_str(B.take_front(16), 0)); + EXPECT_EQ("0000: 61626364 65666768 |abcdefgh|\n" + "0008: 696a6b6c 6d6e6f70 |ijklmnop|", + format_bytes_with_ascii_str(B.take_front(16), 0, 8)); + EXPECT_EQ("0000: 61626364 656667 |abcdefg|\n" + "0007: 68696a6b 6c |hijkl|", + format_bytes_with_ascii_str(B.take_front(12), 0, 7)); //---------------------------------------------------------------------- // Test hex bytes with ASCII with offsets with the default 2 byte groups. //---------------------------------------------------------------------- - EXPECT_EQ("0x0000000000000000: 6162 6364 6566 6768 " + EXPECT_EQ("0000: 6162 6364 6566 6768 " "696a 6b6c 6d6e 6f70 |abcdefghijklmnop|", - format_hex_bytes_with_ascii(b, 16, 0, 16, 2)); - EXPECT_EQ("0x0000000000000000: 6162 6364 6566 6768 |abcdefgh|\n" - "0x0000000000000008: 696a 6b6c 6d6e 6f70 |ijklmnop|", - format_hex_bytes_with_ascii(b, 16, 0, 8, 2)); - EXPECT_EQ("0x0000000000000000: 6162 6364 6566 67 |abcdefg|\n" - "0x0000000000000007: 6869 6a6b 6c |hijkl|", - format_hex_bytes_with_ascii(b, 12, 0, 7, 2)); + format_bytes_with_ascii_str(B.take_front(16), 0, 16, 2)); + EXPECT_EQ("0000: 6162 6364 6566 6768 |abcdefgh|\n" + "0008: 696a 6b6c 6d6e 6f70 |ijklmnop|", + format_bytes_with_ascii_str(B.take_front(16), 0, 8, 2)); + EXPECT_EQ("0000: 6162 6364 6566 67 |abcdefg|\n" + "0007: 6869 6a6b 6c |hijkl|", + format_bytes_with_ascii_str(B.take_front(12), 0, 7, 2)); //---------------------------------------------------------------------- // Test hex bytes with ASCII with offsets with the default 1 byte groups. //---------------------------------------------------------------------- - EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 " + EXPECT_EQ("0000: 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, 16, 1)); - 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, 1)); - 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, 1)); + format_bytes_with_ascii_str(B.take_front(16), 0, 16, 1)); + EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 |abcdefgh|\n" + "0008: 69 6a 6b 6c 6d 6e 6f 70 |ijklmnop|", + format_bytes_with_ascii_str(B.take_front(16), 0, 8, 1)); + EXPECT_EQ("0000: 61 62 63 64 65 66 67 |abcdefg|\n" + "0007: 68 69 6a 6b 6c |hijkl|", + format_bytes_with_ascii_str(B.take_front(12), 0, 7, 1)); } }