From a9d944fd6fd19ac377b5ebea9272676642b7ceaa Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Mon, 10 Jul 2017 19:16:49 +0000 Subject: Resubmit "Add pdb-diff test." This was originally reverted because of two issues. 1) Printing ANSI color escape codes even when outputting to a file 2) Module name comparisons were failing when comparing a PDB generated on one machine to a PDB generated on another machine. I attempted to fix #2 by adding command line options which let you specify prefixes to strip from the beginning of embedded paths, which effectively lets us specify a path to "base" each PDB from and only compare the parts under the base. But this is tricky because PDB paths always use Windows path syntax, even when they are created on non-Windows hosts. A problem still existed when constructing the prefix to strip, where we were accidentally using a host-specific path separator instead of a Windows path separator. This resubmission fixes the issue on Linux (and I have verified that the test now passes on Linux). llvm-svn: 307571 --- llvm/tools/llvm-pdbutil/DiffPrinter.cpp | 38 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'llvm/tools/llvm-pdbutil/DiffPrinter.cpp') diff --git a/llvm/tools/llvm-pdbutil/DiffPrinter.cpp b/llvm/tools/llvm-pdbutil/DiffPrinter.cpp index 45641e2e4b0..dd61cc18259 100644 --- a/llvm/tools/llvm-pdbutil/DiffPrinter.cpp +++ b/llvm/tools/llvm-pdbutil/DiffPrinter.cpp @@ -6,18 +6,31 @@ using namespace llvm; using namespace llvm::pdb; -static void setColor(llvm::raw_ostream &OS, DiffResult Result) { - switch (Result) { - case DiffResult::IDENTICAL: - OS.changeColor(raw_ostream::Colors::GREEN, false); - break; - case DiffResult::EQUIVALENT: - OS.changeColor(raw_ostream::Colors::YELLOW, true); - break; - default: - OS.changeColor(raw_ostream::Colors::RED, false); - break; +namespace { +struct Colorize { + Colorize(raw_ostream &OS, DiffResult Result) : OS(OS) { + if (!OS.has_colors()) + return; + switch (Result) { + case DiffResult::IDENTICAL: + OS.changeColor(raw_ostream::Colors::GREEN, false); + break; + case DiffResult::EQUIVALENT: + OS.changeColor(raw_ostream::Colors::YELLOW, true); + break; + default: + OS.changeColor(raw_ostream::Colors::RED, false); + break; + } + } + + ~Colorize() { + if (OS.has_colors()) + OS.resetColor(); } + + raw_ostream &OS; +}; } DiffPrinter::DiffPrinter(uint32_t Indent, StringRef Header, @@ -124,9 +137,8 @@ void DiffPrinter::printValue(StringRef Value, DiffResult C, AlignStyle Style, std::string FormattedItem = formatv("{0}", fmt_align(Value, Style, Width)).str(); if (C != DiffResult::UNSPECIFIED) { - setColor(OS, C); + Colorize Color(OS, C); OS << FormattedItem; - OS.resetColor(); } else OS << FormattedItem; if (Style == AlignStyle::Right) -- cgit v1.2.3