summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-pdbutil/DiffPrinter.cpp
blob: b608d54d0e28735da335aebe4300b56e0bdec983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include "DiffPrinter.h"

#include "llvm/Support/FormatAdapters.h"

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;
  }
}

DiffPrinter::DiffPrinter(uint32_t Indent, StringRef Header,
                         uint32_t PropertyWidth, uint32_t FieldWidth,
                         raw_ostream &Stream)
    : Indent(Indent), PropertyWidth(PropertyWidth), FieldWidth(FieldWidth),
      OS(Stream) {
  printHeaderRow();
  printFullRow(Header);
}

DiffPrinter::~DiffPrinter() {}

void DiffPrinter::printFullRow(StringRef Text) {
  newLine();
  printField(Text, DiffResult::UNSPECIFIED, AlignStyle::Center,
             PropertyWidth + 1 + FieldWidth + 1 + FieldWidth);
  printSeparatorRow();
}

void DiffPrinter::printSeparatorRow() {
  newLine();
  OS << formatv("{0}", fmt_repeat('-', PropertyWidth));
  OS << '+';
  OS << formatv("{0}", fmt_repeat('-', FieldWidth));
  OS << '+';
  OS << formatv("{0}", fmt_repeat('-', FieldWidth));
  OS << '|';
}

void DiffPrinter::printHeaderRow() {
  newLine('-');
  OS << formatv("{0}", fmt_repeat('-', PropertyWidth + 2 * FieldWidth + 3));
}

void DiffPrinter::newLine(char InitialChar) {
  OS << "\n";
  OS.indent(Indent) << InitialChar;
}

void DiffPrinter::printExplicit(StringRef Property, DiffResult C,
                                StringRef Left, StringRef Right) {
  newLine();
  printField(Property, DiffResult::UNSPECIFIED, AlignStyle::Right,
             PropertyWidth);
  printField(Left, C, AlignStyle::Center, FieldWidth);
  printField(Right, C, AlignStyle::Center, FieldWidth);
  printSeparatorRow();
}

void DiffPrinter::printSame(StringRef Property, StringRef Value) {
  newLine();
  printField(Property, DiffResult::UNSPECIFIED, AlignStyle::Right,
             PropertyWidth);
  printField(Value, DiffResult::IDENTICAL, AlignStyle::Center,
             FieldWidth + 1 + FieldWidth);
  printSeparatorRow();
}

void DiffPrinter::printDifferent(StringRef Property, StringRef Left,
                                 StringRef Right) {
  newLine();
  printField(Property, DiffResult::UNSPECIFIED, AlignStyle::Right,
             PropertyWidth);
  printField(Left, DiffResult::DIFFERENT, AlignStyle::Center, FieldWidth);
  printField(Right, DiffResult::DIFFERENT, AlignStyle::Center, FieldWidth);
  printSeparatorRow();
}

void DiffPrinter::printField(StringRef Value, DiffResult C, AlignStyle Style,
                             uint32_t Width) {
  if (Style == AlignStyle::Right)
    --Width;

  std::string FormattedItem =
      formatv("{0}", fmt_align(Value, Style, Width)).str();
  if (C != DiffResult::UNSPECIFIED) {
    setColor(OS, C);
    OS << FormattedItem;
    OS.resetColor();
  } else
    OS << FormattedItem;
  if (Style == AlignStyle::Right)
    OS << ' ';
  OS << '|';
}
OpenPOWER on IntegriCloud