From 88bb163f816d00d2805b6a0d97ea0f4200b84553 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Tue, 3 May 2016 00:28:04 +0000 Subject: Move llvm-readobj/StreamWriter to Support. We wish to re-use this from llvm-pdbdump, and it provides a nice way to print structured data in scoped format that could prove useful for many other dumping tools as well. Moving to support and changing name to ScopedPrinter to better reflect its purpose. llvm-svn: 268342 --- llvm/lib/Support/CMakeLists.txt | 1 + llvm/lib/Support/ScopedPrinter.cpp | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 llvm/lib/Support/ScopedPrinter.cpp (limited to 'llvm/lib/Support') diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 0de1972b3c5..3d718e6a11c 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -76,6 +76,7 @@ add_llvm_library(LLVMSupport RandomNumberGenerator.cpp Regex.cpp ScaledNumber.cpp + ScopedPrinter.cpp SHA1.cpp SmallPtrSet.cpp SmallVector.cpp diff --git a/llvm/lib/Support/ScopedPrinter.cpp b/llvm/lib/Support/ScopedPrinter.cpp new file mode 100644 index 00000000000..0225f01156a --- /dev/null +++ b/llvm/lib/Support/ScopedPrinter.cpp @@ -0,0 +1,72 @@ +#include "llvm/Support/ScopedPrinter.h" + +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Format.h" +#include + +using namespace llvm::support; + +namespace llvm { + +raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) { + OS << "0x" << to_hexString(Value.Value); + return OS; +} + +const std::string to_hexString(uint64_t Value, bool UpperCase) { + std::string number; + llvm::raw_string_ostream stream(number); + stream << format_hex_no_prefix(Value, 1, UpperCase); + return stream.str(); +} + +void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str, + ArrayRef Data, bool Block) { + if (Data.size() > 16) + Block = true; + + if (Block) { + startLine() << Label; + if (Str.size() > 0) + 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"; + } + + startLine() << ")\n"; + } else { + startLine() << Label << ":"; + if (Str.size() > 0) + 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"; + } +} + +} // namespace llvm -- cgit v1.2.3