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