blob: 45641e2e4b0837ce9f7f78b673d934aa95611fad [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 Turnerba3836b2017-07-10 17:32:47 +00009static void setColor(llvm::raw_ostream &OS, DiffResult Result) {
10 switch (Result) {
11 case DiffResult::IDENTICAL:
12 OS.changeColor(raw_ostream::Colors::GREEN, false);
13 break;
14 case DiffResult::EQUIVALENT:
15 OS.changeColor(raw_ostream::Colors::YELLOW, true);
16 break;
17 default:
18 OS.changeColor(raw_ostream::Colors::RED, false);
19 break;
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000020 }
21}
22
23DiffPrinter::DiffPrinter(uint32_t Indent, StringRef Header,
24 uint32_t PropertyWidth, uint32_t FieldWidth,
Zachary Turnerc1e93e52017-07-07 18:45:56 +000025 bool Result, bool Fields, raw_ostream &Stream)
26 : PrintResult(Result), PrintValues(Fields), Indent(Indent),
27 PropertyWidth(PropertyWidth), FieldWidth(FieldWidth), OS(Stream) {
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000028 printHeaderRow();
29 printFullRow(Header);
30}
31
32DiffPrinter::~DiffPrinter() {}
33
Zachary Turnerc1e93e52017-07-07 18:45:56 +000034uint32_t DiffPrinter::tableWidth() const {
35 // `|`
36 uint32_t W = 1;
37
38 // `<width>|`
39 W += PropertyWidth + 1;
40
41 if (PrintResult) {
42 // ` I |`
43 W += 4;
44 }
45
46 if (PrintValues) {
47 // `<width>|<width>|`
48 W += 2 * (FieldWidth + 1);
49 }
50 return W;
51}
52
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000053void DiffPrinter::printFullRow(StringRef Text) {
54 newLine();
Zachary Turnerc1e93e52017-07-07 18:45:56 +000055 printValue(Text, DiffResult::UNSPECIFIED, AlignStyle::Center,
56 tableWidth() - 2, true);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000057 printSeparatorRow();
58}
59
60void DiffPrinter::printSeparatorRow() {
61 newLine();
62 OS << formatv("{0}", fmt_repeat('-', PropertyWidth));
Zachary Turnerc1e93e52017-07-07 18:45:56 +000063 if (PrintResult) {
64 OS << '+';
65 OS << formatv("{0}", fmt_repeat('-', 3));
66 }
67 if (PrintValues) {
68 OS << '+';
69 OS << formatv("{0}", fmt_repeat('-', FieldWidth));
70 OS << '+';
71 OS << formatv("{0}", fmt_repeat('-', FieldWidth));
72 }
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000073 OS << '|';
74}
75
76void DiffPrinter::printHeaderRow() {
77 newLine('-');
Zachary Turnerc1e93e52017-07-07 18:45:56 +000078 OS << formatv("{0}", fmt_repeat('-', tableWidth() - 1));
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000079}
80
81void DiffPrinter::newLine(char InitialChar) {
82 OS << "\n";
83 OS.indent(Indent) << InitialChar;
84}
85
86void DiffPrinter::printExplicit(StringRef Property, DiffResult C,
87 StringRef Left, StringRef Right) {
88 newLine();
Zachary Turnerc1e93e52017-07-07 18:45:56 +000089 printValue(Property, DiffResult::UNSPECIFIED, AlignStyle::Right,
90 PropertyWidth, true);
91 printResult(C);
92 printValue(Left, C, AlignStyle::Center, FieldWidth, false);
93 printValue(Right, C, AlignStyle::Center, FieldWidth, false);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000094 printSeparatorRow();
95}
96
Zachary Turnerc1e93e52017-07-07 18:45:56 +000097void DiffPrinter::printResult(DiffResult Result) {
98 if (!PrintResult)
99 return;
100 switch (Result) {
101 case DiffResult::DIFFERENT:
102 printValue("D", Result, AlignStyle::Center, 3, true);
103 break;
104 case DiffResult::EQUIVALENT:
105 printValue("E", Result, AlignStyle::Center, 3, true);
106 break;
107 case DiffResult::IDENTICAL:
108 printValue("I", Result, AlignStyle::Center, 3, true);
109 break;
110 case DiffResult::UNSPECIFIED:
111 printValue(" ", Result, AlignStyle::Center, 3, true);
112 break;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000113 }
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000114}
115
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000116void DiffPrinter::printValue(StringRef Value, DiffResult C, AlignStyle Style,
117 uint32_t Width, bool Force) {
118 if (!Force && !PrintValues)
119 return;
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000120
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000121 if (Style == AlignStyle::Right)
122 --Width;
123
124 std::string FormattedItem =
125 formatv("{0}", fmt_align(Value, Style, Width)).str();
126 if (C != DiffResult::UNSPECIFIED) {
Zachary Turnerba3836b2017-07-10 17:32:47 +0000127 setColor(OS, C);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000128 OS << FormattedItem;
Zachary Turnerba3836b2017-07-10 17:32:47 +0000129 OS.resetColor();
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +0000130 } else
131 OS << FormattedItem;
132 if (Style == AlignStyle::Right)
133 OS << ' ';
134 OS << '|';
135}