Files
revng-revng/lib/PTML/IndentedOstream.cpp
Pietro Fezzardi 7fa7b3cbe2 PTMLIndentedOstream: honor GenerateTagLessPTML
Before this commit PTMLIndentOstream was still emitting tag around
indentation, even when GenerateTagLessPTML was true.
This commit fixes the problem.
2024-06-27 22:10:01 +02:00

55 lines
1.2 KiB
C++

//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/Support/ErrorHandling.h"
#include "revng/PTML/Constants.h"
#include "revng/PTML/IndentedOstream.h"
#include "revng/PTML/Tag.h"
namespace ptml {
uint64_t PTMLIndentedOstream::current_pos() const {
return OS.tell();
}
void PTMLIndentedOstream::write_impl(const char *Ptr, size_t Size) {
llvm::StringRef Str(Ptr, Size);
llvm::SmallVector<llvm::StringRef> Lines;
Str.split(Lines, '\n');
bool EndsInNewLine = Str.endswith("\n");
if (EndsInNewLine)
Lines.pop_back();
if (TrailingNewline)
writeIndent();
for (auto &Line : llvm::make_range(Lines.begin(), std::prev(Lines.end()))) {
OS << Line << '\n';
writeIndent();
}
OS << Lines.pop_back_val();
if (EndsInNewLine)
OS << '\n';
TrailingNewline = EndsInNewLine;
}
void PTMLIndentedOstream::writeIndent() {
if (IndentDepth > 0) {
Tag IndentTag = B.getTag(tags::Span,
std::string(IndentSize * IndentDepth, ' '));
if (not B.isGenerateTagLessPTML())
IndentTag.addAttribute(attributes::Token, ptml::tokens::Indentation);
OS << IndentTag.serialize();
}
TrailingNewline = false;
}
} // namespace ptml