diff options
author | Rui Ueyama <ruiu@google.com> | 2019-08-08 07:04:01 +0000 |
---|---|---|
committer | Rui Ueyama <ruiu@google.com> | 2019-08-08 07:04:01 +0000 |
commit | 6fd13f0849573aeec26f28ab0927eea0b5a7ccb9 (patch) | |
tree | bf21e99ed755987add84940c081a13c9e5c7784f /clang/tools/diagtool/TreeView.cpp | |
parent | 724c6053acd69bbaab52365c016624f489e7b2d5 (diff) | |
download | bcm5719-llvm-6fd13f0849573aeec26f28ab0927eea0b5a7ccb9.tar.gz bcm5719-llvm-6fd13f0849573aeec26f28ab0927eea0b5a7ccb9.zip |
[diagtool] Use `operator<<(Colors)` to print out colored output.
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.
Differential Revision: https://reviews.llvm.org/D65854
llvm-svn: 368259
Diffstat (limited to 'clang/tools/diagtool/TreeView.cpp')
-rw-r--r-- | clang/tools/diagtool/TreeView.cpp | 49 |
1 files changed, 13 insertions, 36 deletions
diff --git a/clang/tools/diagtool/TreeView.cpp b/clang/tools/diagtool/TreeView.cpp index 5160afc64ab..96951287cc4 100644 --- a/clang/tools/diagtool/TreeView.cpp +++ b/clang/tools/diagtool/TreeView.cpp @@ -20,31 +20,14 @@ DEF_DIAGTOOL("tree", "Show warning flags in a tree view", TreeView) using namespace clang; using namespace diagtool; -static bool hasColors(const llvm::raw_ostream &out) { - if (&out != &llvm::errs() && &out != &llvm::outs()) - return false; - return llvm::errs().is_displayed() && llvm::outs().is_displayed(); -} - 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 +54,11 @@ public: 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 +67,10 @@ public: 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 +116,8 @@ public: } 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 +157,8 @@ int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { return -1; } + out.enable_colors(out.has_colors()); + TreePrinter TP(out); TP.Internal = Internal; TP.showKey(); |