1 //===- IndentedOstream.h - raw ostream wrapper to indent --------*- 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 // raw_ostream subclass that keeps track of indentation for textual output 10 // where indentation helps readability. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MLIR_SUPPORT_INDENTEDOSTREAM_H_ 15 #define MLIR_SUPPORT_INDENTEDOSTREAM_H_ 16 17 #include "mlir/Support/LLVM.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 namespace mlir { 21 22 /// raw_ostream subclass that simplifies indention a sequence of code. 23 class raw_indented_ostream : public raw_ostream { 24 public: raw_indented_ostream(llvm::raw_ostream & os)25 explicit raw_indented_ostream(llvm::raw_ostream &os) : os(os) { 26 SetUnbuffered(); 27 } 28 29 /// Simple RAII struct to use to indentation around entering/exiting region. 30 struct DelimitedScope { 31 explicit DelimitedScope(raw_indented_ostream &os, StringRef open = "", 32 StringRef close = "", bool indent = true) osDelimitedScope33 : os(os), open(open), close(close), indent(indent) { 34 os << open; 35 if (indent) 36 os.indent(); 37 } ~DelimitedScopeDelimitedScope38 ~DelimitedScope() { 39 if (indent) 40 os.unindent(); 41 os << close; 42 } 43 44 raw_indented_ostream &os; 45 46 private: 47 StringRef open, close; 48 bool indent; 49 }; 50 51 /// Returns the underlying (unindented) raw_ostream. getOStream()52 raw_ostream &getOStream() const { return os; } 53 54 /// Returns DelimitedScope. 55 DelimitedScope scope(StringRef open = "", StringRef close = "", 56 bool indent = true) { 57 return DelimitedScope(*this, open, close, indent); 58 } 59 60 /// Prints a string re-indented to the current indent. Re-indents by removing 61 /// the leading whitespace from the first non-empty line from every line of 62 /// the string, skipping over empty lines at the start. 63 raw_indented_ostream &printReindented(StringRef str); 64 65 /// Increases the indent and returning this raw_indented_ostream. indent()66 raw_indented_ostream &indent() { 67 currentIndent += indentSize; 68 return *this; 69 } 70 71 /// Decreases the indent and returning this raw_indented_ostream. unindent()72 raw_indented_ostream &unindent() { 73 currentIndent = std::max(0, currentIndent - indentSize); 74 return *this; 75 } 76 77 /// Emits whitespace and sets the indentation for the stream. indent(int with)78 raw_indented_ostream &indent(int with) { 79 os.indent(with); 80 atStartOfLine = false; 81 currentIndent = with; 82 return *this; 83 } 84 85 private: 86 void write_impl(const char *ptr, size_t size) override; 87 88 /// Return the current position within the stream, not counting the bytes 89 /// currently in the buffer. current_pos()90 uint64_t current_pos() const override { return os.tell(); } 91 92 /// Constant indent added/removed. 93 static constexpr int indentSize = 2; 94 95 // Tracker for current indentation. 96 int currentIndent = 0; 97 98 // The leading whitespace of the string being printed, if reindent is used. 99 int leadingWs = 0; 100 101 // Tracks whether at start of line and so indent is required or not. 102 bool atStartOfLine = true; 103 104 // The underlying raw_ostream. 105 raw_ostream &os; 106 }; 107 108 } // namespace mlir 109 #endif // MLIR_SUPPORT_INDENTEDOSTREAM_H_ 110