10b57cec5SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
20b57cec5SDimitry Andric
30b57cec5SDimitry Andric #include "llvm/Support/Format.h"
40b57cec5SDimitry Andric
50b57cec5SDimitry Andric using namespace llvm::support;
60b57cec5SDimitry Andric
70b57cec5SDimitry Andric namespace llvm {
80b57cec5SDimitry Andric
operator <<(raw_ostream & OS,const HexNumber & Value)90b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) {
1081ad6265SDimitry Andric OS << "0x" << utohexstr(Value.Value);
110b57cec5SDimitry Andric return OS;
120b57cec5SDimitry Andric }
130b57cec5SDimitry Andric
printBinaryImpl(StringRef Label,StringRef Str,ArrayRef<uint8_t> Data,bool Block,uint32_t StartOffset)140b57cec5SDimitry Andric void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str,
150b57cec5SDimitry Andric ArrayRef<uint8_t> Data, bool Block,
160b57cec5SDimitry Andric uint32_t StartOffset) {
170b57cec5SDimitry Andric if (Data.size() > 16)
180b57cec5SDimitry Andric Block = true;
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric if (Block) {
210b57cec5SDimitry Andric startLine() << Label;
220b57cec5SDimitry Andric if (!Str.empty())
230b57cec5SDimitry Andric OS << ": " << Str;
240b57cec5SDimitry Andric OS << " (\n";
250b57cec5SDimitry Andric if (!Data.empty())
260b57cec5SDimitry Andric OS << format_bytes_with_ascii(Data, StartOffset, 16, 4,
270b57cec5SDimitry Andric (IndentLevel + 1) * 2, true)
280b57cec5SDimitry Andric << "\n";
290b57cec5SDimitry Andric startLine() << ")\n";
300b57cec5SDimitry Andric } else {
310b57cec5SDimitry Andric startLine() << Label << ":";
320b57cec5SDimitry Andric if (!Str.empty())
330b57cec5SDimitry Andric OS << " " << Str;
34*bdd1243dSDimitry Andric OS << " (" << format_bytes(Data, std::nullopt, Data.size(), 1, 0, true)
35*bdd1243dSDimitry Andric << ")\n";
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
JSONScopedPrinter(raw_ostream & OS,bool PrettyPrint,std::unique_ptr<DelimitedScope> && OuterScope)390eae32dcSDimitry Andric JSONScopedPrinter::JSONScopedPrinter(
400eae32dcSDimitry Andric raw_ostream &OS, bool PrettyPrint,
410eae32dcSDimitry Andric std::unique_ptr<DelimitedScope> &&OuterScope)
420eae32dcSDimitry Andric : ScopedPrinter(OS, ScopedPrinter::ScopedPrinterKind::JSON),
430eae32dcSDimitry Andric JOS(OS, /*Indent=*/PrettyPrint ? 2 : 0),
440eae32dcSDimitry Andric OuterScope(std::move(OuterScope)) {
450eae32dcSDimitry Andric if (this->OuterScope)
460eae32dcSDimitry Andric this->OuterScope->setPrinter(*this);
470eae32dcSDimitry Andric }
480eae32dcSDimitry Andric
490b57cec5SDimitry Andric } // namespace llvm
50