xref: /freebsd/contrib/llvm-project/llvm/lib/Support/ScopedPrinter.cpp (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 #include "llvm/Support/ScopedPrinter.h"
2 
3 #include "llvm/Support/Format.h"
4 
5 using namespace llvm::support;
6 
7 namespace llvm {
8 
9 raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) {
10   OS << "0x" << utohexstr(Value.Value);
11   return OS;
12 }
13 
14 void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str,
15                                     ArrayRef<uint8_t> Data, bool Block,
16                                     uint32_t StartOffset) {
17   if (Data.size() > 16)
18     Block = true;
19 
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, std::nullopt, Data.size(), 1, 0, true)
35        << ")\n";
36   }
37 }
38 
39 JSONScopedPrinter::JSONScopedPrinter(
40     raw_ostream &OS, bool PrettyPrint,
41     std::unique_ptr<DelimitedScope> &&OuterScope)
42     : ScopedPrinter(OS, ScopedPrinter::ScopedPrinterKind::JSON),
43       JOS(OS, /*Indent=*/PrettyPrint ? 2 : 0),
44       OuterScope(std::move(OuterScope)) {
45   if (this->OuterScope)
46     this->OuterScope->setPrinter(*this);
47 }
48 
49 } // namespace llvm
50