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