14 #ifndef MLIR_SUPPORT_INDENTEDOSTREAM_H_
15 #define MLIR_SUPPORT_INDENTEDOSTREAM_H_
18 #include "llvm/Support/raw_ostream.h"
32 StringRef close =
"",
bool indent =
true)
33 :
os(
os), open(open), close(close), indent(indent) {
47 StringRef open, close;
65 StringRef extraPrefix =
"");
69 currentIndent += indentSize;
75 currentIndent =
std::max(0, currentIndent - indentSize);
82 atStartOfLine =
false;
88 void write_impl(
const char *ptr,
size_t size)
final;
92 uint64_t current_pos() const final {
return os.tell(); }
95 static constexpr
int indentSize = 2;
98 int currentIndent = 0;
104 StringRef currentExtraPrefix;
107 bool atStartOfLine =
true;
113 inline raw_indented_ostream &
115 StringRef extraPrefix) {
116 StringRef output = str;
118 while (!output.empty()) {
119 auto split = output.split(
'\n');
121 auto firstTrimmed = split.first.rtrim(
'\r');
122 size_t indent = firstTrimmed.find_first_not_of(
" \t");
123 if (indent != StringRef::npos) {
128 output = split.second;
131 StringRef remaining = output;
132 while (!remaining.empty()) {
133 auto split = remaining.split(
'\n');
134 auto firstTrimmed = split.first.rtrim(
'\r');
135 size_t indent = firstTrimmed.find_first_not_of(
" \t");
136 if (indent != StringRef::npos)
137 leadingWs =
std::min(leadingWs,
static_cast<int>(indent));
138 remaining = split.second;
141 std::swap(currentExtraPrefix, extraPrefix);
143 std::swap(currentExtraPrefix, extraPrefix);
148 inline void mlir::raw_indented_ostream::write_impl(
const char *ptr,
150 StringRef str(ptr, size);
152 auto print = [
this](StringRef str) {
154 os.indent(currentIndent) << currentExtraPrefix << str.substr(leadingWs);
156 os << str.substr(leadingWs);
159 while (!str.empty()) {
160 size_t idx = str.find(
'\n');
161 if (idx == StringRef::npos) {
162 if (!str.substr(leadingWs).empty()) {
164 atStartOfLine =
false;
169 auto split = std::make_pair(str.substr(0, idx), str.substr(idx + 1));
172 if (!split.first.ltrim().empty() || !currentExtraPrefix.empty())
175 atStartOfLine =
true;
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
raw_ostream subclass that simplifies indention a sequence of code.
raw_indented_ostream & unindent()
Decreases the indent and returning this raw_indented_ostream.
DelimitedScope scope(StringRef open="", StringRef close="", bool indent=true)
Returns DelimitedScope.
raw_indented_ostream & indent(int with)
Emits whitespace and sets the indentation for the stream.
raw_ostream & getOStream() const
Returns the underlying (unindented) raw_ostream.
raw_indented_ostream & indent()
Increases the indent and returning this raw_indented_ostream.
raw_indented_ostream(llvm::raw_ostream &os)
raw_indented_ostream & printReindented(StringRef str, StringRef extraPrefix="")
Prints a string re-indented to the current indent.
Include the generated interface declarations.
Simple RAII struct to use to indentation around entering/exiting region.
raw_indented_ostream & os
DelimitedScope(raw_indented_ostream &os, StringRef open="", StringRef close="", bool indent=true)