1 //===- DiffPrinter.h ------------------------------------------ *- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TOOLS_LLVMPDBDUMP_DIFFPRINTER_H
11 #define LLVM_TOOLS_LLVMPDBDUMP_DIFFPRINTER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
17 #include "llvm/Support/FormatVariadic.h"
18 #include "llvm/Support/raw_ostream.h"
21 #include <unordered_set>
24 template <> struct hash<llvm::pdb::PdbRaw_FeatureSig> {
25 typedef llvm::pdb::PdbRaw_FeatureSig argument_type;
26 typedef std::size_t result_type;
27 result_type operator()(argument_type Item) const {
28 return std::hash<uint32_t>{}(uint32_t(Item));
38 enum class DiffResult { UNSPECIFIED, IDENTICAL, EQUIVALENT, DIFFERENT };
40 struct IdenticalDiffProvider {
41 template <typename T, typename U>
42 DiffResult compare(const T &Left, const U &Right) {
43 return (Left == Right) ? DiffResult::IDENTICAL : DiffResult::DIFFERENT;
46 template <typename T> std::string format(const T &Item, bool Right) {
47 return formatv("{0}", Item).str();
51 struct EquivalentDiffProvider {
52 template <typename T, typename U>
53 DiffResult compare(const T &Left, const U &Right) {
54 return (Left == Right) ? DiffResult::IDENTICAL : DiffResult::EQUIVALENT;
57 template <typename T> std::string format(const T &Item, bool Right) {
58 return formatv("{0}", Item).str();
64 DiffPrinter(uint32_t Indent, StringRef Header, uint32_t PropertyWidth,
65 uint32_t FieldWidth, bool Result, bool Values,
69 template <typename T, typename U> struct Identical {};
71 template <typename Provider = IdenticalDiffProvider, typename T, typename U>
72 void print(StringRef Property, const T &Left, const U &Right,
73 Provider P = Provider()) {
74 std::string L = P.format(Left, false);
75 std::string R = P.format(Right, true);
77 DiffResult Result = P.compare(Left, Right);
78 printExplicit(Property, Result, L, R);
81 void printExplicit(StringRef Property, DiffResult C, StringRef Left,
84 template <typename T, typename U>
85 void printExplicit(StringRef Property, DiffResult C, const T &Left,
87 std::string L = formatv("{0}", Left).str();
88 std::string R = formatv("{0}", Right).str();
89 printExplicit(Property, C, StringRef(L), StringRef(R));
92 template <typename T, typename U>
93 void diffUnorderedArray(StringRef Property, ArrayRef<T> Left,
95 std::unordered_set<T> LS(Left.begin(), Left.end());
96 std::unordered_set<U> RS(Right.begin(), Right.end());
97 std::string Count1 = formatv("{0} element(s)", Left.size());
98 std::string Count2 = formatv("{0} element(s)", Right.size());
99 print(std::string(Property) + "s (set)", Count1, Count2);
100 for (const auto &L : LS) {
101 auto Iter = RS.find(L);
102 std::string Text = formatv("{0}", L).str();
103 if (Iter == RS.end()) {
104 print(Property, Text, "(not present)");
107 print(Property, Text, Text);
110 for (const auto &R : RS) {
111 auto Iter = LS.find(R);
112 std::string Text = formatv("{0}", R).str();
113 if (Iter == LS.end()) {
114 print(Property, "(not present)", Text);
117 print(Property, Text, Text);
121 template <typename ValueProvider = IdenticalDiffProvider, typename T,
123 void diffUnorderedMap(StringRef Property, const StringMap<T> &Left,
124 const StringMap<U> &Right,
125 ValueProvider P = ValueProvider()) {
126 StringMap<U> RightCopy(Right);
128 std::string Count1 = formatv("{0} element(s)", Left.size());
129 std::string Count2 = formatv("{0} element(s)", Right.size());
130 print(std::string(Property) + "s (map)", Count1, Count2);
132 for (const auto &L : Left) {
133 auto Iter = RightCopy.find(L.getKey());
134 if (Iter == RightCopy.end()) {
135 printExplicit(L.getKey(), DiffResult::DIFFERENT, L.getValue(),
140 print(L.getKey(), L.getValue(), Iter->getValue(), P);
141 RightCopy.erase(Iter);
144 for (const auto &R : RightCopy) {
145 printExplicit(R.getKey(), DiffResult::DIFFERENT, "(not present)",
150 void printFullRow(StringRef Text);
153 uint32_t tableWidth() const;
155 void printHeaderRow();
156 void printSeparatorRow();
157 void newLine(char InitialChar = '|');
158 void printValue(StringRef Value, DiffResult C, AlignStyle Style,
159 uint32_t Width, bool Force);
160 void printResult(DiffResult Result);
165 uint32_t PropertyWidth;