diff options
author | Zachary Turner <zturner@google.com> | 2017-07-07 18:45:37 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2017-07-07 18:45:37 +0000 |
commit | f3b4b2d89d06ededb515703f86ca4ab8744c154d (patch) | |
tree | 967c205c7f899795989ad367f0e31c9a085f4476 /llvm/tools/llvm-pdbutil/DiffPrinter.h | |
parent | 61395176f9afae288f3ad807a503804fdc7a9cfb (diff) | |
download | bcm5719-llvm-f3b4b2d89d06ededb515703f86ca4ab8744c154d.tar.gz bcm5719-llvm-f3b4b2d89d06ededb515703f86ca4ab8744c154d.zip |
[llvm-pdbutil] Improve diff mode.
We're getting to the point that some MS tools (e.g. DIA) can recognize
our PDBs but others (e.g. link.exe) cannot. I think the way forward is
to improve our tooling to help us find differences more easily. For
example, if we can compile the same program with clang-cl and cl and
have a tool tell us all the places where the PDBs differ, this could
tell us what we're doing wrong. It's tricky though, because there are a
lot of "benign" differences in a PDB. For example, if the string table
in one PDB consists of "foo" followed by "bar" and in the other PDB it
consists of "bar" followed by "foo", this is not necessarily a critical
difference, as long as the uses of these strings also refer to the
correct location. On the other hand, if the second PDB doesn't even
contain the string "foo" at all, this is a critical difference.
diff mode has been in llvm-pdbutil for quite a while, but because of the
above challenge along with some others, it's been hard to make it
useful. I think this patch addresses that. It looks for all the same
things, but it now prints the output in tabular format (carefully
formatted and aligned into tables and fields), and it highlights
critical differences in red, non-critical differences in yellow, and
identical fields in green. This makes it easy to spot the places we
differ, and the general concept of outputting arbitrary fields in
tabular format can be extended to provide analysis into many of the
different types of information that show up in a PDB.
Differential Revision: https://reviews.llvm.org/D35039
llvm-svn: 307421
Diffstat (limited to 'llvm/tools/llvm-pdbutil/DiffPrinter.h')
-rw-r--r-- | llvm/tools/llvm-pdbutil/DiffPrinter.h | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/llvm/tools/llvm-pdbutil/DiffPrinter.h b/llvm/tools/llvm-pdbutil/DiffPrinter.h new file mode 100644 index 00000000000..8a4527c6274 --- /dev/null +++ b/llvm/tools/llvm-pdbutil/DiffPrinter.h @@ -0,0 +1,158 @@ +//===- DiffPrinter.h ------------------------------------------ *- C++ --*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVMPDBDUMP_DIFFPRINTER_H +#define LLVM_TOOLS_LLVMPDBDUMP_DIFFPRINTER_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/raw_ostream.h" + +#include <list> +#include <unordered_set> + +namespace llvm { +namespace pdb { + +class PDBFile; + +enum class DiffResult { UNSPECIFIED, IDENTICAL, EQUIVALENT, DIFFERENT }; + +struct IdenticalDiffProvider { + template <typename T, typename U> + DiffResult compare(const T &Left, const U &Right) { + return (Left == Right) ? DiffResult::IDENTICAL : DiffResult::DIFFERENT; + } + + template <typename T> std::string format(const T &Item) { + return formatv("{0}", Item).str(); + } +}; + +struct EquivalentDiffProvider { + template <typename T, typename U> + DiffResult compare(const T &Left, const U &Right) { + return (Left == Right) ? DiffResult::IDENTICAL : DiffResult::EQUIVALENT; + } + + template <typename T> std::string format(const T &Item) { + return formatv("{0}", Item).str(); + } +}; + +class DiffPrinter { +public: + DiffPrinter(uint32_t Indent, StringRef Header, uint32_t PropertyWidth, + uint32_t FieldWidth, raw_ostream &Stream); + ~DiffPrinter(); + + template <typename T, typename U> struct Identical {}; + + template <typename Provider = IdenticalDiffProvider, typename T, typename U> + void print(StringRef Property, const T &Left, const U &Right, + Provider P = Provider()) { + std::string L = P.format(Left); + std::string R = P.format(Right); + + DiffResult Result = P.compare(Left, Right); + printExplicit(Property, Result, L, R); + } + + void printExplicit(StringRef Property, DiffResult C, StringRef Left, + StringRef Right); + + template <typename T, typename U> + void printExplicit(StringRef Property, DiffResult C, const T &Left, + const U &Right) { + std::string L = formatv("{0}", Left).str(); + std::string R = formatv("{0}", Right).str(); + printExplicit(Property, C, StringRef(L), StringRef(R)); + } + + template <typename T, typename U> + void diffUnorderedArray(StringRef Property, ArrayRef<T> Left, + ArrayRef<U> Right) { + std::unordered_set<T> LS(Left.begin(), Left.end()); + std::unordered_set<U> RS(Right.begin(), Right.end()); + std::string Count1 = formatv("{0} element(s)", Left.size()); + std::string Count2 = formatv("{0} element(s)", Right.size()); + print(std::string(Property) + "s (set)", Count1, Count2); + for (const auto &L : LS) { + auto Iter = RS.find(L); + std::string Text = formatv("{0}", L).str(); + if (Iter == RS.end()) { + print(Property, Text, "(not present)"); + continue; + } + print(Property, Text, Text); + RS.erase(Iter); + } + for (const auto &R : RS) { + auto Iter = LS.find(R); + std::string Text = formatv("{0}", R).str(); + if (Iter == LS.end()) { + print(Property, "(not present)", Text); + continue; + } + print(Property, Text, Text); + } + } + + template <typename ValueProvider = IdenticalDiffProvider, typename T, + typename U> + void diffUnorderedMap(StringRef Property, const StringMap<T> &Left, + const StringMap<U> &Right, + ValueProvider P = ValueProvider()) { + StringMap<U> RightCopy(Right); + + std::string Count1 = formatv("{0} element(s)", Left.size()); + std::string Count2 = formatv("{0} element(s)", Right.size()); + print(std::string(Property) + "s (map)", Count1, Count2); + + for (const auto &L : Left) { + auto Iter = RightCopy.find(L.getKey()); + if (Iter == RightCopy.end()) { + printExplicit(L.getKey(), DiffResult::DIFFERENT, L.getValue(), + "(not present)"); + continue; + } + + print(L.getKey(), L.getValue(), Iter->getValue(), P); + RightCopy.erase(Iter); + } + + for (const auto &R : RightCopy) { + printExplicit(R.getKey(), DiffResult::DIFFERENT, "(not present)", + R.getValue()); + } + } + + void printFullRow(StringRef Text); + +private: + void printSame(StringRef Property, StringRef Value); + void printDifferent(StringRef Property, StringRef Left, StringRef Right); + + void printHeaderRow(); + void printSeparatorRow(); + void newLine(char InitialChar = '|'); + void printField(StringRef Value, DiffResult C, AlignStyle Style, + uint32_t Width); + + uint32_t Indent; + uint32_t PropertyWidth; + uint32_t FieldWidth; + raw_ostream &OS; +}; +} // namespace pdb +} // namespace llvm + +#endif |