From e0c6a2938f9af33dadfe2edd73b705af631101f2 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 2 Aug 2019 07:22:34 +0000 Subject: [PATCH] Revert r367649: Improve raw_ostream so that you can "write" colors using operator<< This reverts commit r367649 in an attempt to unbreak Windows bots. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@367658 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ASTDumperUtils.h | 2 +- lib/Analysis/CFG.cpp | 4 +-- lib/Frontend/CompilerInvocation.cpp | 4 --- lib/Frontend/TextDiagnostic.cpp | 25 +++++++++------ tools/diagtool/TreeView.cpp | 48 ++++++++++++++++++++--------- 5 files changed, 53 insertions(+), 30 deletions(-) diff --git a/include/clang/AST/ASTDumperUtils.h b/include/clang/AST/ASTDumperUtils.h index 3dd4f9c081..55a085449a 100644 --- a/include/clang/AST/ASTDumperUtils.h +++ b/include/clang/AST/ASTDumperUtils.h @@ -27,7 +27,7 @@ enum ASTDumpOutputFormat { // Do not use bold yellow for any text. It is hard to read on white screens. struct TerminalColor { - llvm::raw_ostream::Color Color; + llvm::raw_ostream::Colors Color; bool Bold; }; diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index 5db15077ce..0ed1e988a1 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -5509,7 +5509,7 @@ static void print_block(raw_ostream &OS, const CFG* cfg, if (print_edges) { // Print the predecessors of this block. if (!B.pred_empty()) { - const raw_ostream::Color Color = raw_ostream::BLUE; + const raw_ostream::Colors Color = raw_ostream::BLUE; if (ShowColors) OS.changeColor(Color); OS << " Preds " ; @@ -5546,7 +5546,7 @@ static void print_block(raw_ostream &OS, const CFG* cfg, // Print the successors of this block. if (!B.succ_empty()) { - const raw_ostream::Color Color = raw_ostream::MAGENTA; + const raw_ostream::Colors Color = raw_ostream::MAGENTA; if (ShowColors) OS.changeColor(Color); OS << " Succs "; diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index d9bbe25381..6bf4fbcbb2 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -1491,10 +1491,6 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, OPT_fno_diagnostics_show_option, DefaultShowOpt); llvm::sys::Process::UseANSIEscapeCodes(Args.hasArg(OPT_fansi_escape_codes)); - if (Opts.ShowColors) { - llvm::outs().enable_colors(); - llvm::errs().enable_colors(); - } // Default behavior is to not to show note include stacks. Opts.ShowNoteIncludeStack = false; diff --git a/lib/Frontend/TextDiagnostic.cpp b/lib/Frontend/TextDiagnostic.cpp index d066ce0671..d6e75a9180 100644 --- a/lib/Frontend/TextDiagnostic.cpp +++ b/lib/Frontend/TextDiagnostic.cpp @@ -23,16 +23,23 @@ using namespace clang; -static const raw_ostream::Color noteColor = raw_ostream::BLACK; -static const raw_ostream::Color remarkColor = raw_ostream::BLUE; -static const raw_ostream::Color fixitColor = raw_ostream::GREEN; -static const raw_ostream::Color caretColor = raw_ostream::GREEN; -static const raw_ostream::Color warningColor = raw_ostream::MAGENTA; -static const raw_ostream::Color templateColor = raw_ostream::CYAN; -static const raw_ostream::Color errorColor = raw_ostream::RED; -static const raw_ostream::Color fatalColor = raw_ostream::RED; +static const enum raw_ostream::Colors noteColor = + raw_ostream::BLACK; +static const enum raw_ostream::Colors remarkColor = + raw_ostream::BLUE; +static const enum raw_ostream::Colors fixitColor = + raw_ostream::GREEN; +static const enum raw_ostream::Colors caretColor = + raw_ostream::GREEN; +static const enum raw_ostream::Colors warningColor = + raw_ostream::MAGENTA; +static const enum raw_ostream::Colors templateColor = + raw_ostream::CYAN; +static const enum raw_ostream::Colors errorColor = raw_ostream::RED; +static const enum raw_ostream::Colors fatalColor = raw_ostream::RED; // Used for changing only the bold attribute. -static const raw_ostream::Color savedColor = raw_ostream::SAVEDCOLOR; +static const enum raw_ostream::Colors savedColor = + raw_ostream::SAVEDCOLOR; /// Add highlights to differences in template strings. static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str, diff --git a/tools/diagtool/TreeView.cpp b/tools/diagtool/TreeView.cpp index 9bb710775d..154c52a485 100644 --- a/tools/diagtool/TreeView.cpp +++ b/tools/diagtool/TreeView.cpp @@ -20,16 +20,29 @@ DEF_DIAGTOOL("tree", "Show warning flags in a tree view", TreeView) using namespace clang; using namespace diagtool; -class TreePrinter { - using Color = llvm::raw_ostream::Color; +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 { public: llvm::raw_ostream &out; + const bool ShowColors; bool Internal; - TreePrinter(llvm::raw_ostream &out) : out(out), Internal(false) { - if (&out != &llvm::errs() && &out != &llvm::outs()) - out.disable_colors(); + 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(Color, false, false); + } + + void resetColor() { + if (ShowColors) + out << llvm::sys::Process::ResetColor(); } static bool isIgnored(unsigned DiagID) { @@ -57,11 +70,12 @@ public: out.indent(Indent * 2); if (enabledByDefault(Group)) - out << Color::GREEN; + setColor(llvm::raw_ostream::GREEN); else - out << Color::YELLOW; + setColor(llvm::raw_ostream::YELLOW); - out << "-W" << Group.getName() << "\n" << Color::RESET; + out << "-W" << Group.getName() << "\n"; + resetColor(); ++Indent; for (const GroupRecord &GR : Group.subgroups()) { @@ -70,10 +84,12 @@ public: if (Internal) { for (const DiagnosticRecord &DR : Group.diagnostics()) { - if (!isIgnored(DR.DiagID)) - out << Color::GREEN; + if (ShowColors && !isIgnored(DR.DiagID)) + setColor(llvm::raw_ostream::GREEN); out.indent(Indent * 2); - out << DR.getName() << Color::RESET << "\n"; + out << DR.getName(); + resetColor(); + out << "\n"; } } } @@ -119,9 +135,13 @@ public: } void showKey() { - out << '\n' - << Color::GREEN << "GREEN" << Color::RESET - << " = enabled by default\n\n"; + if (ShowColors) { + out << '\n'; + setColor(llvm::raw_ostream::GREEN); + out << "GREEN"; + resetColor(); + out << " = enabled by default\n\n"; + } } }; -- 2.40.0