1//===- FormatUtil.h ------------------------------------------- *- C++ --*-===//
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#ifndef LLDB_TOOLS_LLDB_TEST_FORMATUTIL_H
10#define LLDB_TOOLS_LLDB_TEST_FORMATUTIL_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/Support/FormatVariadic.h"
16#include "llvm/Support/raw_ostream.h"
17
18#include <list>
19
20namespace lldb_private {
21
22class LinePrinter {
23  llvm::raw_ostream &OS;
24  int IndentSpaces;
25  int CurrentIndent;
26
27public:
28  class Line {
29    LinePrinter *P;
30
31  public:
32    Line(LinePrinter &P) : P(&P) { P.OS.indent(P.CurrentIndent); }
33    ~Line();
34
35    Line(Line &&RHS) : P(RHS.P) { RHS.P = nullptr; }
36    void operator=(Line &&) = delete;
37
38    operator llvm::raw_ostream &() { return P->OS; }
39  };
40
41  LinePrinter(int Indent, llvm::raw_ostream &Stream);
42
43  void Indent(uint32_t Amount = 0);
44  void Unindent(uint32_t Amount = 0);
45  void NewLine();
46
47  void printLine(const llvm::Twine &T) { line() << T; }
48  template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
49    printLine(llvm::formatv(Fmt, std::forward<Ts>(Items)...));
50  }
51
52  void formatBinary(llvm::StringRef Label, llvm::ArrayRef<uint8_t> Data,
53                    uint32_t StartOffset);
54  void formatBinary(llvm::StringRef Label, llvm::ArrayRef<uint8_t> Data,
55                    uint64_t BaseAddr, uint32_t StartOffset);
56
57  Line line() { return Line(*this); }
58  int getIndentLevel() const { return CurrentIndent; }
59};
60
61struct AutoIndent {
62  explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
63      : L(&L), Amount(Amount) {
64    L.Indent(Amount);
65  }
66  ~AutoIndent() {
67    if (L)
68      L->Unindent(Amount);
69  }
70
71  LinePrinter *L = nullptr;
72  uint32_t Amount = 0;
73};
74
75} // namespace lldb_private
76
77#endif
78