1 //===- 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 21 namespace lldb_private { 22 23 class LinePrinter { 24 llvm::raw_ostream &OS; 25 int IndentSpaces; 26 int CurrentIndent; 27 28 public: 29 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 42 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 48 void printLine(const llvm::Twine &T) { line() << T; } 49 template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) { 50 printLine(llvm::formatv(Fmt, std::forward<Ts>(Items)...)); 51 } 52 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 58 Line line() { return Line(*this); } 59 int getIndentLevel() const { return CurrentIndent; } 60 }; 61 62 struct 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 76 } // namespace lldb_private 77 78 #endif 79