Add option to control whether llvm-pdbdump outputs in color

Adds -color-output option to llvm-pdbdump pretty commands that lets the user
specify whether the output should have color. The default depends on whether
the output is going to a TTY (per prior discussion in
https://reviews.llvm.org/D31246).

This will enable tests that pipe llvm-pdbdump output to FileCheck to work
across platforms without regard to the differences in ANSI codes.

Differential Revision: https://reviews.llvm.org/D31263

llvm-svn: 298610
This commit is contained in:
Adrian McCarthy 2017-03-23 15:28:15 +00:00
parent a8ba572dcf
commit 1aa207d451
3 changed files with 17 additions and 7 deletions

View File

@ -42,8 +42,8 @@ bool IsItemExcluded(llvm::StringRef Item,
using namespace llvm; using namespace llvm;
LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream) LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
: OS(Stream), IndentSpaces(Indent), CurrentIndent(0) { : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(), SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
opts::pretty::ExcludeTypes.end()); opts::pretty::ExcludeTypes.end());
SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(), SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
@ -84,7 +84,8 @@ bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
} }
WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) { WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) {
applyColor(C); if (P.hasColor())
applyColor(C);
} }
WithColor::~WithColor() { OS.resetColor(); } WithColor::~WithColor() { OS.resetColor(); }

View File

@ -24,12 +24,13 @@ class LinePrinter {
friend class WithColor; friend class WithColor;
public: public:
LinePrinter(int Indent, raw_ostream &Stream); LinePrinter(int Indent, bool UseColor, raw_ostream &Stream);
void Indent(); void Indent();
void Unindent(); void Unindent();
void NewLine(); void NewLine();
bool hasColor() const { return UseColor; }
raw_ostream &getStream() { return OS; } raw_ostream &getStream() { return OS; }
int getIndentLevel() const { return CurrentIndent; } int getIndentLevel() const { return CurrentIndent; }
@ -48,6 +49,7 @@ private:
raw_ostream &OS; raw_ostream &OS;
int IndentSpaces; int IndentSpaces;
int CurrentIndent; int CurrentIndent;
bool UseColor;
std::list<Regex> ExcludeCompilandFilters; std::list<Regex> ExcludeCompilandFilters;
std::list<Regex> ExcludeTypeFilters; std::list<Regex> ExcludeTypeFilters;

View File

@ -125,8 +125,11 @@ cl::opt<uint64_t> LoadAddress(
cl::desc("Assume the module is loaded at the specified address"), cl::desc("Assume the module is loaded at the specified address"),
cl::cat(OtherOptions), cl::sub(PrettySubcommand)); cl::cat(OtherOptions), cl::sub(PrettySubcommand));
cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"), cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
cl::cat(OtherOptions), cl::sub(PrettySubcommand)); cl::cat(OtherOptions), cl::sub(PrettySubcommand));
cl::opt<cl::boolOrDefault>
ColorOutput("color-output",
cl::desc("Override use of color (default = isatty)"),
cl::cat(OtherOptions), cl::sub(PrettySubcommand));
cl::list<std::string> ExcludeTypes( cl::list<std::string> ExcludeTypes(
"exclude-types", cl::desc("Exclude types by regular expression"), "exclude-types", cl::desc("Exclude types by regular expression"),
cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand)); cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
@ -500,7 +503,11 @@ static void dumpPretty(StringRef Path) {
if (opts::pretty::LoadAddress) if (opts::pretty::LoadAddress)
Session->setLoadAddress(opts::pretty::LoadAddress); Session->setLoadAddress(opts::pretty::LoadAddress);
LinePrinter Printer(2, outs()); auto &Stream = outs();
const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
? Stream.has_colors()
: opts::pretty::ColorOutput == cl::BOU_TRUE;
LinePrinter Printer(2, UseColor, Stream);
auto GlobalScope(Session->getGlobalScope()); auto GlobalScope(Session->getGlobalScope());
std::string FileName(GlobalScope->getSymbolsFileName()); std::string FileName(GlobalScope->getSymbolsFileName());