]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbutil/DiffPrinter.h
MFC r343601:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbutil / DiffPrinter.h
1 //===- DiffPrinter.h ------------------------------------------ *- C++ --*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_TOOLS_LLVMPDBDUMP_DIFFPRINTER_H
11 #define LLVM_TOOLS_LLVMPDBDUMP_DIFFPRINTER_H
12
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"
19
20 #include <list>
21 #include <unordered_set>
22
23 namespace std {
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));
29   }
30 };
31 } // namespace std
32
33 namespace llvm {
34 namespace pdb {
35
36 class PDBFile;
37
38 enum class DiffResult { UNSPECIFIED, IDENTICAL, EQUIVALENT, DIFFERENT };
39
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;
44   }
45
46   template <typename T> std::string format(const T &Item, bool Right) {
47     return formatv("{0}", Item).str();
48   }
49 };
50
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;
55   }
56
57   template <typename T> std::string format(const T &Item, bool Right) {
58     return formatv("{0}", Item).str();
59   }
60 };
61
62 class DiffPrinter {
63 public:
64   DiffPrinter(uint32_t Indent, StringRef Header, uint32_t PropertyWidth,
65               uint32_t FieldWidth, bool Result, bool Values,
66               raw_ostream &Stream);
67   ~DiffPrinter();
68
69   template <typename T, typename U> struct Identical {};
70
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);
76
77     DiffResult Result = P.compare(Left, Right);
78     printExplicit(Property, Result, L, R);
79   }
80
81   void printExplicit(StringRef Property, DiffResult C, StringRef Left,
82                      StringRef Right);
83
84   template <typename T, typename U>
85   void printExplicit(StringRef Property, DiffResult C, const T &Left,
86                      const U &Right) {
87     std::string L = formatv("{0}", Left).str();
88     std::string R = formatv("{0}", Right).str();
89     printExplicit(Property, C, StringRef(L), StringRef(R));
90   }
91
92   template <typename T, typename U>
93   void diffUnorderedArray(StringRef Property, ArrayRef<T> Left,
94                           ArrayRef<U> Right) {
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)");
105         continue;
106       }
107       print(Property, Text, Text);
108       RS.erase(Iter);
109     }
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);
115         continue;
116       }
117       print(Property, Text, Text);
118     }
119   }
120
121   template <typename ValueProvider = IdenticalDiffProvider, typename T,
122             typename U>
123   void diffUnorderedMap(StringRef Property, const StringMap<T> &Left,
124                         const StringMap<U> &Right,
125                         ValueProvider P = ValueProvider()) {
126     StringMap<U> RightCopy(Right);
127
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);
131
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(),
136                       "(not present)");
137         continue;
138       }
139
140       print(L.getKey(), L.getValue(), Iter->getValue(), P);
141       RightCopy.erase(Iter);
142     }
143
144     for (const auto &R : RightCopy) {
145       printExplicit(R.getKey(), DiffResult::DIFFERENT, "(not present)",
146                     R.getValue());
147     }
148   }
149
150   void printFullRow(StringRef Text);
151
152 private:
153   uint32_t tableWidth() const;
154
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);
161
162   bool PrintResult;
163   bool PrintValues;
164   uint32_t Indent;
165   uint32_t PropertyWidth;
166   uint32_t FieldWidth;
167   raw_ostream &OS;
168 };
169 } // namespace pdb
170 } // namespace llvm
171
172 #endif