Skip to content

Commit

Permalink
[Support] Fix color handling in formatted_raw_ostream (llvm#86700)
Browse files Browse the repository at this point in the history
The color methods in formatted_raw_ostream were forwarding directly to
the underlying stream without considering existing buffered output. This
would cause incorrect colored output for buffered uses of
formatted_raw_ostream.

Fix this issue by applying the color to the formatted_raw_ostream itself
and temporarily disabling scanning of any color related output so as not
to affect the position tracking.

This fix means that workarounds that forced formatted_raw_ostream
buffering to be disabled can be removed. In the case of llvm-objdump,
this can improve disassembly performance when redirecting to a file by
more than an order of magnitude on both Windows and Linux. This
improvement restores the disassembly performance when redirecting to a
file to a level similar to before color support was added.

(cherry picked from commit c9db031)
  • Loading branch information
nga888 authored and tstellar committed Mar 28, 2024
1 parent 0cd4bab commit a385a91
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
51 changes: 44 additions & 7 deletions llvm/include/llvm/Support/FormattedStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class formatted_raw_ostream : public raw_ostream {
/// have the rest of it.
SmallString<4> PartialUTF8Char;

/// DisableScan - Temporarily disable scanning of output. Used to ignore color
/// codes.
bool DisableScan;

void write_impl(const char *Ptr, size_t Size) override;

/// current_pos - Return the current position within the stream,
Expand Down Expand Up @@ -89,9 +93,33 @@ class formatted_raw_ostream : public raw_ostream {
SetUnbuffered();
TheStream->SetUnbuffered();

enable_colors(TheStream->colors_enabled());

Scanned = nullptr;
}

void PreDisableScan() {
assert(!DisableScan);
ComputePosition(getBufferStart(), GetNumBytesInBuffer());
assert(PartialUTF8Char.empty());
DisableScan = true;
}

void PostDisableScan() {
assert(DisableScan);
DisableScan = false;
Scanned = getBufferStart() + GetNumBytesInBuffer();
}

struct DisableScanScope {
formatted_raw_ostream *S;

DisableScanScope(formatted_raw_ostream *FRO) : S(FRO) {
S->PreDisableScan();
}
~DisableScanScope() { S->PostDisableScan(); }
};

public:
/// formatted_raw_ostream - Open the specified file for
/// writing. If an error occurs, information about the error is
Expand All @@ -104,12 +132,12 @@ class formatted_raw_ostream : public raw_ostream {
/// underneath it.
///
formatted_raw_ostream(raw_ostream &Stream)
: TheStream(nullptr), Position(0, 0) {
: TheStream(nullptr), Position(0, 0), DisableScan(false) {
setStream(Stream);
}
explicit formatted_raw_ostream() : TheStream(nullptr), Position(0, 0) {
Scanned = nullptr;
}
explicit formatted_raw_ostream()
: TheStream(nullptr), Position(0, 0), Scanned(nullptr),
DisableScan(false) {}

~formatted_raw_ostream() override {
flush();
Expand All @@ -136,17 +164,26 @@ class formatted_raw_ostream : public raw_ostream {
}

raw_ostream &resetColor() override {
TheStream->resetColor();
if (colors_enabled()) {
DisableScanScope S(this);
raw_ostream::resetColor();
}
return *this;
}

raw_ostream &reverseColor() override {
TheStream->reverseColor();
if (colors_enabled()) {
DisableScanScope S(this);
raw_ostream::reverseColor();
}
return *this;
}

raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override {
TheStream->changeColor(Color, Bold, BG);
if (colors_enabled()) {
DisableScanScope S(this);
raw_ostream::changeColor(Color, Bold, BG);
}
return *this;
}

Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Support/FormattedStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ void formatted_raw_ostream::UpdatePosition(const char *Ptr, size_t Size) {
/// ComputePosition - Examine the current output and update line and column
/// counts.
void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
if (DisableScan)
return;

// If our previous scan pointer is inside the buffer, assume we already
// scanned those bytes. This depends on raw_ostream to not change our buffer
// in unexpected ways.
Expand Down
5 changes: 0 additions & 5 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,6 @@ int main(int argc, char **argv) {
std::unique_ptr<MCAsmBackend> MAB(
TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
auto FOut = std::make_unique<formatted_raw_ostream>(*OS);
// FIXME: Workaround for bug in formatted_raw_ostream. Color escape codes
// are (incorrectly) written directly to the unbuffered raw_ostream wrapped
// by the formatted_raw_ostream.
if (Action == AC_CDisassemble)
FOut->SetUnbuffered();
Str.reset(
TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true,
/*useDwarfDirectory*/ true, IP,
Expand Down
7 changes: 0 additions & 7 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2032,13 +2032,6 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,

formatted_raw_ostream FOS(outs());

// FIXME: Workaround for bug in formatted_raw_ostream. Color escape codes
// are (incorrectly) written directly to the unbuffered raw_ostream
// wrapped by the formatted_raw_ostream.
if (DisassemblyColor == ColorOutput::Enable ||
DisassemblyColor == ColorOutput::Auto)
FOS.SetUnbuffered();

std::unordered_map<uint64_t, std::string> AllLabels;
std::unordered_map<uint64_t, std::vector<BBAddrMapLabel>> BBAddrMapLabels;
if (SymbolizeOperands) {
Expand Down

0 comments on commit a385a91

Please sign in to comment.