ScopedPrinter.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "llvm/Support/ScopedPrinter.h"
  2. #include "llvm/Support/Format.h"
  3. using namespace llvm::support;
  4. namespace llvm {
  5. raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) {
  6. OS << "0x" << utohexstr(Value.Value);
  7. return OS;
  8. }
  9. void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str,
  10. ArrayRef<uint8_t> Data, bool Block,
  11. uint32_t StartOffset) {
  12. if (Data.size() > 16)
  13. Block = true;
  14. if (Block) {
  15. startLine() << Label;
  16. if (!Str.empty())
  17. OS << ": " << Str;
  18. OS << " (\n";
  19. if (!Data.empty())
  20. OS << format_bytes_with_ascii(Data, StartOffset, 16, 4,
  21. (IndentLevel + 1) * 2, true)
  22. << "\n";
  23. startLine() << ")\n";
  24. } else {
  25. startLine() << Label << ":";
  26. if (!Str.empty())
  27. OS << " " << Str;
  28. OS << " (" << format_bytes(Data, std::nullopt, Data.size(), 1, 0, true)
  29. << ")\n";
  30. }
  31. }
  32. JSONScopedPrinter::JSONScopedPrinter(
  33. raw_ostream &OS, bool PrettyPrint,
  34. std::unique_ptr<DelimitedScope> &&OuterScope)
  35. : ScopedPrinter(OS, ScopedPrinter::ScopedPrinterKind::JSON),
  36. JOS(OS, /*Indent=*/PrettyPrint ? 2 : 0),
  37. OuterScope(std::move(OuterScope)) {
  38. if (this->OuterScope)
  39. this->OuterScope->setPrinter(*this);
  40. }
  41. } // namespace llvm