[PATCH] D65854: [diagtool] Use `operator<<(Colors)` to print out colored output.
Rui Ueyama via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 7 02:37:04 PDT 2019
ruiu created this revision.
ruiu added a reviewer: JDevlieghere.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
r368131 introduced this new API to print out messages in colors.
If the colored output is disabled, `operator<<(Colors)` becomes nop.
No functionality change intended.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D65854
Files:
clang/tools/diagtool/TreeView.cpp
Index: clang/tools/diagtool/TreeView.cpp
===================================================================
--- clang/tools/diagtool/TreeView.cpp
+++ clang/tools/diagtool/TreeView.cpp
@@ -27,24 +27,13 @@
}
class TreePrinter {
+ using Colors = llvm::raw_ostream::Colors;
+
public:
llvm::raw_ostream &out;
- const bool ShowColors;
bool Internal;
- TreePrinter(llvm::raw_ostream &out)
- : out(out), ShowColors(hasColors(out)), Internal(false) {}
-
- void setColor(llvm::raw_ostream::Colors Color) {
- if (ShowColors)
- out << llvm::sys::Process::OutputColor(static_cast<char>(Color), false,
- false);
- }
-
- void resetColor() {
- if (ShowColors)
- out << llvm::sys::Process::ResetColor();
- }
+ TreePrinter(llvm::raw_ostream &out) : out(out), Internal(false) {}
static bool isIgnored(unsigned DiagID) {
// FIXME: This feels like a hack.
@@ -71,12 +60,11 @@
out.indent(Indent * 2);
if (enabledByDefault(Group))
- setColor(llvm::raw_ostream::GREEN);
+ out << Colors::GREEN;
else
- setColor(llvm::raw_ostream::YELLOW);
+ out << Colors::YELLOW;
- out << "-W" << Group.getName() << "\n";
- resetColor();
+ out << "-W" << Group.getName() << "\n" << Colors::RESET;
++Indent;
for (const GroupRecord &GR : Group.subgroups()) {
@@ -85,12 +73,10 @@
if (Internal) {
for (const DiagnosticRecord &DR : Group.diagnostics()) {
- if (ShowColors && !isIgnored(DR.DiagID))
- setColor(llvm::raw_ostream::GREEN);
+ if (!isIgnored(DR.DiagID))
+ out << Colors::GREEN;
out.indent(Indent * 2);
- out << DR.getName();
- resetColor();
- out << "\n";
+ out << DR.getName() << Colors::RESET << "\n";
}
}
}
@@ -136,13 +122,8 @@
}
void showKey() {
- if (ShowColors) {
- out << '\n';
- setColor(llvm::raw_ostream::GREEN);
- out << "GREEN";
- resetColor();
- out << " = enabled by default\n\n";
- }
+ out << '\n' << Colors::GREEN << "GREEN" << Colors::RESET
+ << " = enabled by default\n\n";
}
};
@@ -182,6 +163,9 @@
return -1;
}
+ if (!hasColors(out))
+ out.enable_colors(false);
+
TreePrinter TP(out);
TP.Internal = Internal;
TP.showKey();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65854.213825.patch
Type: text/x-patch
Size: 2340 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190807/2db5a1a9/attachment.bin>
More information about the cfe-commits
mailing list