Files
clang-p2996/lldb/tools/lldb-test/FormatUtil.h
Pavel Labath d211d1a59c lldb-test: Improve newline handling
Summary:
Previously lldb-test's LinePrinter would output the indentation spaces
even on completely empty lines. This is not nice, as trailing spaces get
flagged as errors in some tools/editors, and it prevents FileCheck's
CHECK-EMPTY from working.

Equally annoying was the fact that the LinePrinter did not terminate
it's output with a newline (instead it would leave the unterminated hanging
indent from the last NewLine() command), which meant that the shell prompt
following the lldb-test command came out wrong.

This fixes both issues by changing how newlines are handled. NewLine(),
which was ending the previous line ('\n') *and* begging the next line by
printing the indent, is now "demoted" to just printing literal "\n".
Instead, lines are now delimited via a helper Line object, which makes
sure the line is indented and terminated in an RAII fashion. The typical
usage would be:
Printer.line() << "This text will be indented and terminated";
If one needs to do more work than it will fit into a single statement,
one can also assign the result of the line() function to a local
variable. The line will then be terminated when that object goes out of
scope.

Reviewers: zturner

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D55597

llvm-svn: 349269
2018-12-15 13:49:25 +00:00

79 lines
2.0 KiB
C++

//===- FormatUtil.h ------------------------------------------- *- C++ --*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLDBTEST_FORMATUTIL_H
#define LLVM_TOOLS_LLDBTEST_FORMATUTIL_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <list>
namespace lldb_private {
class LinePrinter {
llvm::raw_ostream &OS;
int IndentSpaces;
int CurrentIndent;
public:
class Line {
LinePrinter *P;
public:
Line(LinePrinter &P) : P(&P) { P.OS.indent(P.CurrentIndent); }
~Line();
Line(Line &&RHS) : P(RHS.P) { RHS.P = nullptr; }
void operator=(Line &&) = delete;
operator llvm::raw_ostream &() { return P->OS; }
};
LinePrinter(int Indent, llvm::raw_ostream &Stream);
void Indent(uint32_t Amount = 0);
void Unindent(uint32_t Amount = 0);
void NewLine();
void printLine(const llvm::Twine &T) { line() << T; }
template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
printLine(llvm::formatv(Fmt, std::forward<Ts>(Items)...));
}
void formatBinary(llvm::StringRef Label, llvm::ArrayRef<uint8_t> Data,
uint32_t StartOffset);
void formatBinary(llvm::StringRef Label, llvm::ArrayRef<uint8_t> Data,
uint64_t BaseAddr, uint32_t StartOffset);
Line line() { return Line(*this); }
int getIndentLevel() const { return CurrentIndent; }
};
struct AutoIndent {
explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
: L(&L), Amount(Amount) {
L.Indent(Amount);
}
~AutoIndent() {
if (L)
L->Unindent(Amount);
}
LinePrinter *L = nullptr;
uint32_t Amount = 0;
};
} // namespace lldb_private
#endif