blob: dd61cc1825936b698d6264972c8681f37f6d8d16 [file] [log] [blame]
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +00001
2#include "DiffPrinter.h"
3
4#include "llvm/Support/FormatAdapters.h"
5
6using namespace llvm;
7using namespace llvm::pdb;
8
Zachary Turnera9d944f2017-07-10 19:16:49 +00009namespace {
10struct Colorize {
11 Colorize(raw_ostream &OS, DiffResult Result) : OS(OS) {
12 if (!OS.has_colors())
13 return;
14 switch (Result) {
15 case DiffResult::IDENTICAL:
16 OS.changeColor(raw_ostream::Colors::GREEN, false);
17 break;
18 case DiffResult::EQUIVALENT:
19 OS.changeColor(raw_ostream::Colors::YELLOW, true);
20 break;
21 default:
22 OS.changeColor(raw_ostream::Colors::RED, false);
23 break;
24 }
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000025 }
Zachary Turnera9d944f2017-07-10 19:16:49 +000026
27 ~Colorize() {
28 if (OS.has_colors())
29 OS.resetColor();
30 }
31
32 raw_ostream &OS;
33};
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000034}
35
36DiffPrinter::DiffPrinter(uint32_t Indent, StringRef Header,
37 uint32_t PropertyWidth, uint32_t FieldWidth,
Zachary Turnerc1e93e52017-07-07 18:45:56 +000038 bool Result, bool Fields, raw_ostream &Stream)
39 : PrintResult(Result), PrintValues(Fields), Indent(Indent),
40 PropertyWidth(PropertyWidth), FieldWidth(FieldWidth), OS(Stream) {
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000041 printHeaderRow();
42 printFullRow(Header);
43}
44
45DiffPrinter::~DiffPrinter() {}
46
Zachary Turnerc1e93e52017-07-07 18:45:56 +000047uint32_t DiffPrinter::tableWidth() const {
48 // `|`
49 uint32_t W = 1;
50
51 // `<width>|`
52 W += PropertyWidth + 1;
53
54 if (PrintResult) {
55 // ` I |`
56 W += 4;
57 }
58
59 if (PrintValues) {
60 // `<width>|<width>|`
61 W += 2 * (FieldWidth + 1);
62 }
63 return W;
64}
65
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000066void DiffPrinter::printFullRow(StringRef Text) {
67 newLine();
Zachary Turnerc1e93e52017-07-07 18:45:56 +000068 printValue(Text, DiffResult::UNSPECIFIED, AlignStyle::Center,
69 tableWidth() - 2, true);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000070 printSeparatorRow();
71}
72
73void DiffPrinter::printSeparatorRow() {
74 newLine();
75 OS << formatv("{0}", fmt_repeat('-', PropertyWidth));
Zachary Turnerc1e93e52017-07-07 18:45:56 +000076 if (PrintResult) {
77 OS << '+';
78 OS << formatv("{0}", fmt_repeat('-', 3));
79 }
80 if (PrintValues) {
81 OS << '+';
82 OS << formatv("{0}", fmt_repeat('-', FieldWidth));
83 OS << '+';
84 OS << formatv("{0}", fmt_repeat('-', FieldWidth));
85 }
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000086 OS << '|';
87}
88
89void DiffPrinter::printHeaderRow() {
90 newLine('-');
Zachary Turnerc1e93e52017-07-07 18:45:56 +000091 OS << formatv("{0}", fmt_repeat('-', tableWidth() - 1));
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000092}
93
94void DiffPrinter::newLine(char InitialChar) {
95 OS << "\n";
96 OS.indent(Indent) << InitialChar;
97}
98
99void DiffPrinter::printExplicit(StringRef Property, DiffResult C,
100 StringRef Left, StringRef Right) {
101 newLine();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000102 printValue(Property, DiffResult::UNSPECIFIED, AlignStyle::Right,
103 PropertyWidth, true);
104 printResult(C);
105 printValue(Left, C, AlignStyle::Center, FieldWidth, false);
106 printValue(Right, C, AlignStyle::Center, FieldWidth, false);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000107 printSeparatorRow();
108}
109
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000110void DiffPrinter::printResult(DiffResult Result) {
111 if (!PrintResult)
112 return;
113 switch (Result) {
114 case DiffResult::DIFFERENT:
115 printValue("D", Result, AlignStyle::Center, 3, true);
116 break;
117 case DiffResult::EQUIVALENT:
118 printValue("E", Result, AlignStyle::Center, 3, true);
119 break;
120 case DiffResult::IDENTICAL:
121 printValue("I", Result, AlignStyle::Center, 3, true);
122 break;
123 case DiffResult::UNSPECIFIED:
124 printValue(" ", Result, AlignStyle::Center, 3, true);
125 break;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000126 }
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000127}
128
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000129void DiffPrinter::printValue(StringRef Value, DiffResult C, AlignStyle Style,
130 uint32_t Width, bool Force) {
131 if (!Force && !PrintValues)
132 return;
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000133
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000134 if (Style == AlignStyle::Right)
135 --Width;
136
137 std::string FormattedItem =
138 formatv("{0}", fmt_align(Value, Style, Width)).str();
139 if (C != DiffResult::UNSPECIFIED) {
Zachary Turnera9d944f2017-07-10 19:16:49 +0000140 Colorize Color(OS, C);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000141 OS << FormattedItem;
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000142 } else
143 OS << FormattedItem;
144 if (Style == AlignStyle::Right)
145 OS << ' ';
146 OS << '|';
147}