xref: /freebsd/contrib/llvm-project/llvm/lib/Support/ScopedPrinter.cpp (revision ccb59683b98360afaf5b5bb641a68fea22c68d0b)
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, None, Data.size(), 1, 0, true) << ")\n";
35   }
36 }
37 
38 JSONScopedPrinter::JSONScopedPrinter(
39     raw_ostream &OS, bool PrettyPrint,
40     std::unique_ptr<DelimitedScope> &&OuterScope)
41     : ScopedPrinter(OS, ScopedPrinter::ScopedPrinterKind::JSON),
42       JOS(OS, /*Indent=*/PrettyPrint ? 2 : 0),
43       OuterScope(std::move(OuterScope)) {
44   if (this->OuterScope)
45     this->OuterScope->setPrinter(*this);
46 }
47 
48 } // namespace llvm
49