1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/ScopedPrinter.h"
10#include "llvm/Support/Format.h"
11
12using namespace llvm;
13
14raw_ostream &llvm::operator<<(raw_ostream &OS, const HexNumber &Value) {
15 OS << "0x" << utohexstr(X: Value.Value);
16 return OS;
17}
18
19void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str,
20 ArrayRef<uint8_t> Data, bool Block,
21 uint32_t StartOffset) {
22 if (Data.size() > 16)
23 Block = true;
24
25 if (Block) {
26 startLine() << Label;
27 if (!Str.empty())
28 OS << ": " << Str;
29 OS << " (\n";
30 if (!Data.empty())
31 OS << format_bytes_with_ascii(Bytes: Data, FirstByteOffset: StartOffset, NumPerLine: 16, ByteGroupSize: 4,
32 IndentLevel: (IndentLevel + 1) * 2, Upper: true)
33 << "\n";
34 startLine() << ")\n";
35 } else {
36 startLine() << Label << ":";
37 if (!Str.empty())
38 OS << " " << Str;
39 OS << " (" << format_bytes(Bytes: Data, FirstByteOffset: std::nullopt, NumPerLine: Data.size(), ByteGroupSize: 1, IndentLevel: 0, Upper: true)
40 << ")\n";
41 }
42}
43
44JSONScopedPrinter::JSONScopedPrinter(
45 raw_ostream &OS, bool PrettyPrint,
46 std::unique_ptr<DelimitedScope> &&OuterScope)
47 : ScopedPrinter(OS, ScopedPrinter::ScopedPrinterKind::JSON),
48 JOS(OS, /*Indent=*/PrettyPrint ? 2 : 0),
49 OuterScope(std::move(OuterScope)) {
50 if (this->OuterScope)
51 this->OuterScope->setPrinter(*this);
52}
53