[llvm-pdbdump] Abstract some of the YAML/Raw printing code.

There is a lot of duplicate code for printing line info between
YAML and the raw output printer.  This introduces a base class
that can be shared between the two, and makes some minor
cleanups in the process.

llvm-svn: 301728
diff --git a/llvm/tools/llvm-pdbdump/C13DebugFragmentVisitor.cpp b/llvm/tools/llvm-pdbdump/C13DebugFragmentVisitor.cpp
new file mode 100644
index 0000000..b4e64bf
--- /dev/null
+++ b/llvm/tools/llvm-pdbdump/C13DebugFragmentVisitor.cpp
@@ -0,0 +1,77 @@
+//===- C13DebugFragmentVisitor.cpp -------------------------------*- C++-*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "C13DebugFragmentVisitor.h"
+
+#include "llvm/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.h"
+#include "llvm/DebugInfo/CodeView/ModuleDebugLineFragment.h"
+#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
+#include "llvm/DebugInfo/PDB/Native/RawError.h"
+#include "llvm/DebugInfo/PDB/Native/StringTable.h"
+
+using namespace llvm;
+using namespace llvm::codeview;
+using namespace llvm::pdb;
+
+C13DebugFragmentVisitor::C13DebugFragmentVisitor(PDBFile &F) : F(F) {}
+
+C13DebugFragmentVisitor::~C13DebugFragmentVisitor() {}
+
+Error C13DebugFragmentVisitor::visitUnknown(
+    codeview::ModuleDebugUnknownFragment &Fragment) {
+  return Error::success();
+}
+
+Error C13DebugFragmentVisitor::visitFileChecksums(
+    codeview::ModuleDebugFileChecksumFragment &Checksums) {
+  assert(!this->Checksums.hasValue());
+  this->Checksums = Checksums;
+  return Error::success();
+}
+
+Error C13DebugFragmentVisitor::visitLines(
+    codeview::ModuleDebugLineFragment &Lines) {
+  this->Lines.push_back(Lines);
+  return Error::success();
+}
+
+Error C13DebugFragmentVisitor::finished() {
+  if (!Checksums.hasValue()) {
+    assert(Lines.empty());
+    return Error::success();
+  }
+  if (auto EC = handleFileChecksums())
+    return EC;
+
+  if (auto EC = handleLines())
+    return EC;
+
+  return Error::success();
+}
+
+Expected<StringRef>
+C13DebugFragmentVisitor::getNameFromStringTable(uint32_t Offset) {
+  auto ST = F.getStringTable();
+  if (!ST)
+    return ST.takeError();
+
+  return ST->getStringForID(Offset);
+}
+
+Expected<StringRef>
+C13DebugFragmentVisitor::getNameFromChecksumsBuffer(uint32_t Offset) {
+  assert(Checksums.hasValue());
+
+  auto Array = Checksums->getArray();
+  auto ChecksumIter = Array.at(Offset);
+  if (ChecksumIter == Array.end())
+    return make_error<RawError>(raw_error_code::invalid_format);
+  const auto &Entry = *ChecksumIter;
+  return getNameFromStringTable(Entry.FileNameOffset);
+}