ScopedPrinter.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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" << to_hexString(Value.Value);
  7. return OS;
  8. }
  9. std::string to_hexString(uint64_t Value, bool UpperCase) {
  10. std::string number;
  11. llvm::raw_string_ostream stream(number);
  12. stream << format_hex_no_prefix(Value, 1, UpperCase);
  13. return stream.str();
  14. }
  15. void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str,
  16. ArrayRef<uint8_t> Data, bool Block,
  17. uint32_t StartOffset) {
  18. if (Data.size() > 16)
  19. Block = true;
  20. if (Block) {
  21. startLine() << Label;
  22. if (!Str.empty())
  23. OS << ": " << Str;
  24. OS << " (\n";
  25. if (!Data.empty())
  26. OS << format_bytes_with_ascii(Data, StartOffset, 16, 4,
  27. (IndentLevel + 1) * 2, true)
  28. << "\n";
  29. startLine() << ")\n";
  30. } else {
  31. startLine() << Label << ":";
  32. if (!Str.empty())
  33. OS << " " << Str;
  34. OS << " (" << format_bytes(Data, None, Data.size(), 1, 0, true) << ")\n";
  35. }
  36. }
  37. JSONScopedPrinter::JSONScopedPrinter(
  38. raw_ostream &OS, bool PrettyPrint,
  39. std::unique_ptr<DelimitedScope> &&OuterScope)
  40. : ScopedPrinter(OS, ScopedPrinter::ScopedPrinterKind::JSON),
  41. JOS(OS, /*Indent=*/PrettyPrint ? 2 : 0),
  42. OuterScope(std::move(OuterScope)) {
  43. if (this->OuterScope)
  44. this->OuterScope->setPrinter(*this);
  45. }
  46. } // namespace llvm