blob: 598d4c5440f5d15d846b3bf172f8174a22449f86 [file] [log] [blame]
Zachary Turner888a4282017-12-01 00:52:51 +00001//===- FormatUtil.h ------------------------------------------- *- C++ --*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_TOOLS_LLDBTEST_FORMATUTIL_H
11#define LLVM_TOOLS_LLDBTEST_FORMATUTIL_H
12
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/raw_ostream.h"
18
19#include <list>
20
21namespace lldb_private {
22
23class LinePrinter {
24 llvm::raw_ostream &OS;
25 int IndentSpaces;
26 int CurrentIndent;
27
28public:
Pavel Labathd211d1a2018-12-15 13:49:25 +000029 class Line {
30 LinePrinter *P;
31
32 public:
33 Line(LinePrinter &P) : P(&P) { P.OS.indent(P.CurrentIndent); }
34 ~Line();
35
36 Line(Line &&RHS) : P(RHS.P) { RHS.P = nullptr; }
37 void operator=(Line &&) = delete;
38
39 operator llvm::raw_ostream &() { return P->OS; }
40 };
41
Zachary Turner888a4282017-12-01 00:52:51 +000042 LinePrinter(int Indent, llvm::raw_ostream &Stream);
43
44 void Indent(uint32_t Amount = 0);
45 void Unindent(uint32_t Amount = 0);
46 void NewLine();
47
Pavel Labathd211d1a2018-12-15 13:49:25 +000048 void printLine(const llvm::Twine &T) { line() << T; }
Zachary Turner888a4282017-12-01 00:52:51 +000049 template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
50 printLine(llvm::formatv(Fmt, std::forward<Ts>(Items)...));
51 }
Zachary Turner888a4282017-12-01 00:52:51 +000052
53 void formatBinary(llvm::StringRef Label, llvm::ArrayRef<uint8_t> Data,
54 uint32_t StartOffset);
55 void formatBinary(llvm::StringRef Label, llvm::ArrayRef<uint8_t> Data,
56 uint64_t BaseAddr, uint32_t StartOffset);
57
Pavel Labathd211d1a2018-12-15 13:49:25 +000058 Line line() { return Line(*this); }
Zachary Turner888a4282017-12-01 00:52:51 +000059 int getIndentLevel() const { return CurrentIndent; }
60};
61
62struct AutoIndent {
63 explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
64 : L(&L), Amount(Amount) {
65 L.Indent(Amount);
66 }
67 ~AutoIndent() {
68 if (L)
69 L->Unindent(Amount);
70 }
71
72 LinePrinter *L = nullptr;
73 uint32_t Amount = 0;
74};
75
Zachary Turner888a4282017-12-01 00:52:51 +000076} // namespace lldb_private
77
78#endif