This is an archive of the discontinued LLVM Phabricator instance.

Improve raw_ostream so that you can "write" colors using operator<<
ClosedPublic

Authored by ruiu on Aug 1 2019, 2:17 AM.

Details

Summary
  1. raw_ostream supports ANSI colors so that you can write messages

to the termina with colors. Previously, in order to change and reset
color, you had to call changeColor and resetColor functions,
respectively.

So, if you print out "error: " in red, for example, you had to do
something like this:

OS.changeColor(raw_ostream::RED);
OS << "error: ";
OS.resetColor();

With this patch, you can write the same code as follows:

OS << raw_ostream::RED << "error: " << raw_ostream::RESET;
  1. Add a boolean flag to raw_ostream so that you can disable colored

output. If you disable colors, changeColor, operator<<(Color), resetColor
and other color-related functions have no effect.

Most LLVM tools automatically prints out messages using colors, and
you can disable it by passing a flag such as --disable-colors.
This new flag makes it easy to write code that works that way.

Event Timeline

ruiu created this revision.Aug 1 2019, 2:17 AM
Herald added a reviewer: andreadb. · View Herald Transcript
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
MaskRay added a subscriber: JDevlieghere.
grimar added a comment.Aug 1 2019, 3:58 AM

It looks good to me. A minor nit is inlined.

clang/tools/diagtool/TreeView.cpp
30–32

nit: Seems out should be uppercase here.
(I see it was like that before your changes, but seems you touching all the places where it was used, so seems you can fix it).

andreadb accepted this revision.Aug 1 2019, 9:15 AM

LGTM too.

This revision is now accepted and ready to land.Aug 1 2019, 9:15 AM
MaskRay accepted this revision.Aug 1 2019, 5:58 PM
seiya accepted this revision.Aug 1 2019, 5:59 PM

LGTM too.

ruiu marked an inline comment as done.Aug 1 2019, 8:08 PM
ruiu added inline comments.
clang/tools/diagtool/TreeView.cpp
30–32

Yes, this is inconsistent, but we probably shouldn't change this in this patch.

ruiu updated this revision to Diff 212959.Aug 1 2019, 8:08 PM
  • rebased
This revision was automatically updated to reflect the committed changes.