[llvm] r268071 - Refactor the PDB Stream reading interface.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 29 10:22:59 PDT 2016


Author: zturner
Date: Fri Apr 29 12:22:58 2016
New Revision: 268071

URL: http://llvm.org/viewvc/llvm-project?rev=268071&view=rev
Log:
Refactor the PDB Stream reading interface.

The motivation for this change is that PDB has the notion of
streams and substreams.  Substreams often consist of variable
length structures that are convenient to be able to treat as
guaranteed, contiguous byte arrays, whereas the streams they
are contained in are not necessarily so, as a single stream
could be spread across many discontiguous blocks.

So, when processing data from a substream, we want to be able
to assume that we have a contiguous byte array so that we can
cast pointers to variable length arrays and such.

This leads to the question of how to be able to read the same
data structure from either a stream or a substream using the
same interface, which is where this patch comes in.

We separate out the stream's read state from the underlying
representation, and introduce a `StreamReader` class.  Then
we change the name of `PDBStream` to `MappedBlockStream`, and
introduce a second kind of stream called a `ByteStream` which is
simply a sequence of contiguous bytes.  Finally, we update all
of the std::vectors in `PDBDbiStream` to use `ByteStream` instead
as a proof of concept.

Added:
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/ByteStream.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamInterface.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamReader.h
    llvm/trunk/lib/DebugInfo/PDB/Raw/ByteStream.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp
      - copied, changed from r268069, llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/StreamReader.cpp
Removed:
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBStream.h
    llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp
Modified:
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBDbiStream.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBInfoStream.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBNameMap.h
    llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt
    llvm/trunk/lib/DebugInfo/PDB/Raw/PDBDbiStream.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/PDBInfoStream.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/PDBNameMap.cpp
    llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp

Added: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/ByteStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/ByteStream.h?rev=268071&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/ByteStream.h (added)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/ByteStream.h Fri Apr 29 12:22:58 2016
@@ -0,0 +1,47 @@
+//===- ByteStream.h - Reads stream data from a byte sequence ----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_DEBUGINFO_PDB_RAW_BYTESTREAM_H
+#define LLVM_DEBUGINFO_PDB_RAW_BYTESTREAM_H
+
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/DebugInfo/PDB/Raw/StreamInterface.h"
+
+#include <stdint.h>
+
+#include <system_error>
+#include <vector>
+
+namespace llvm {
+class StreamReader;
+class ByteStream : public StreamInterface {
+public:
+  ByteStream();
+  explicit ByteStream(MutableArrayRef<uint8_t> Bytes);
+  explicit ByteStream(uint32_t Length);
+  ~ByteStream() override;
+
+  void reset();
+  void initialize(MutableArrayRef<uint8_t> Bytes);
+  void initialize(uint32_t Length);
+  std::error_code initialize(StreamReader &Reader, uint32_t Length);
+
+  std::error_code readBytes(uint32_t Offset,
+                            MutableArrayRef<uint8_t> Buffer) const override;
+  uint32_t getLength() const override;
+
+  ArrayRef<uint8_t> data() const { return Data; }
+
+private:
+  MutableArrayRef<uint8_t> Data;
+  bool Owned;
+};
+}
+
+#endif

Added: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h?rev=268071&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h (added)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h Fri Apr 29 12:22:58 2016
@@ -0,0 +1,38 @@
+//===- MappedBlockStream.h - Reads stream data from a PDBFile ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_DEBUGINFO_PDB_RAW_MAPPEDBLOCKSTREAM_H
+#define LLVM_DEBUGINFO_PDB_RAW_MAPPEDBLOCKSTREAM_H
+
+#include "llvm/DebugInfo/PDB/Raw/StreamInterface.h"
+
+#include <stdint.h>
+
+#include <system_error>
+#include <vector>
+
+namespace llvm {
+class PDBFile;
+
+class MappedBlockStream : public StreamInterface {
+public:
+  MappedBlockStream(uint32_t StreamIdx, const PDBFile &File);
+
+  std::error_code readBytes(uint32_t Offset,
+                            MutableArrayRef<uint8_t> Buffer) const override;
+  uint32_t getLength() const override { return StreamLength; }
+
+private:
+  uint32_t StreamLength;
+  std::vector<uint32_t> BlockList;
+  const PDBFile &Pdb;
+};
+}
+
+#endif

Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBDbiStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBDbiStream.h?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBDbiStream.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBDbiStream.h Fri Apr 29 12:22:58 2016
@@ -11,9 +11,10 @@
 #define LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAM_H
 
 #include "llvm/DebugInfo/PDB/PDBTypes.h"
+#include "llvm/DebugInfo/PDB/Raw/ByteStream.h"
+#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
 #include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBRawConstants.h"
-#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
 #include "llvm/Support/Endian.h"
 
 namespace llvm {
@@ -46,20 +47,21 @@ public:
   ArrayRef<ModuleInfoEx> modules() const;
 
 private:
-  std::error_code readSubstream(std::vector<uint8_t> &Bytes, uint32_t Size);
   std::error_code initializeFileInfo();
 
   PDBFile &Pdb;
-  PDBStream Stream;
+  MappedBlockStream Stream;
 
   std::vector<ModuleInfoEx> ModuleInfos;
 
-  std::vector<uint8_t> ModInfoSubstream;
-  std::vector<uint8_t> SecContrSubstream;
-  std::vector<uint8_t> SecMapSubstream;
-  std::vector<uint8_t> FileInfoSubstream;
-  std::vector<uint8_t> TypeServerMapSubstream;
-  std::vector<uint8_t> ECSubstream;
+  ByteStream ModInfoSubstream;
+  ByteStream SecContrSubstream;
+  ByteStream SecMapSubstream;
+  ByteStream FileInfoSubstream;
+  ByteStream TypeServerMapSubstream;
+  ByteStream ECSubstream;
+  ByteStream DbgHeader;
+
   std::unique_ptr<HeaderInfo> Header;
 };
 }

Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBInfoStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBInfoStream.h?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBInfoStream.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBInfoStream.h Fri Apr 29 12:22:58 2016
@@ -12,9 +12,9 @@
 
 #include "llvm/ADT/StringMap.h"
 #include "llvm/DebugInfo/PDB/PDBTypes.h"
+#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBNameMap.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBRawConstants.h"
-#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
 
 #include "llvm/Support/Endian.h"
 
@@ -37,7 +37,7 @@ public:
 
 private:
   PDBFile &Pdb;
-  PDBStream Stream1;
+  MappedBlockStream Stream;
 
   // PDB file format version.  We only support VC70.  See the enumeration
   // `PdbRaw_ImplVer` for the other possible values.

Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBNameMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBNameMap.h?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBNameMap.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBNameMap.h Fri Apr 29 12:22:58 2016
@@ -17,12 +17,12 @@
 #include <utility>
 
 namespace llvm {
-class PDBStream;
+class StreamReader;
 class PDBNameMap {
 public:
   PDBNameMap();
 
-  std::error_code load(PDBStream &Stream);
+  std::error_code load(StreamReader &Stream);
 
   bool tryGetValue(StringRef Name, uint32_t &Value) const;
 

Removed: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBStream.h?rev=268070&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBStream.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBStream.h (removed)
@@ -1,46 +0,0 @@
-//===- PDBStream.h - Low level interface to a PDB stream --------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_DEBUGINFO_PDB_RAW_PDBSTREAM_H
-#define LLVM_DEBUGINFO_PDB_RAW_PDBSTREAM_H
-
-#include "llvm/ADT/ArrayRef.h"
-#include "llvm/ADT/StringRef.h"
-
-namespace llvm {
-
-class MemoryBufferRef;
-class PDBFile;
-
-class PDBStream {
-public:
-  PDBStream(uint32_t StreamIdx, const PDBFile &File);
-
-  std::error_code readInteger(uint32_t &Dest);
-  std::error_code readZeroString(std::string &Dest);
-  std::error_code readBytes(void *Dest, uint32_t Length);
-
-  void setOffset(uint32_t Off);
-  uint32_t getOffset() const;
-  uint32_t getLength() const;
-
-  template <typename T> std::error_code readObject(T *Dest) {
-    return readBytes(reinterpret_cast<void *>(Dest), sizeof(T));
-  }
-
-private:
-  uint32_t Offset;
-
-  uint32_t StreamLength;
-  std::vector<uint32_t> BlockList;
-  const PDBFile &Pdb;
-};
-}
-
-#endif

Added: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamInterface.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamInterface.h?rev=268071&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamInterface.h (added)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamInterface.h Fri Apr 29 12:22:58 2016
@@ -0,0 +1,29 @@
+//===- StreamInterface.h - Base interface for a PDB stream ------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_DEBUGINFO_PDB_RAW_STREAMINTERFACE_H
+#define LLVM_DEBUGINFO_PDB_RAW_STREAMINTERFACE_H
+
+#include "llvm/ADT/ArrayRef.h"
+
+#include <stdint.h>
+#include <system_error>
+
+namespace llvm {
+class StreamInterface {
+public:
+  virtual ~StreamInterface() {}
+
+  virtual std::error_code readBytes(uint32_t Offset,
+                                    MutableArrayRef<uint8_t> Buffer) const = 0;
+  virtual uint32_t getLength() const = 0;
+};
+}
+
+#endif

Added: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamReader.h?rev=268071&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamReader.h (added)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/StreamReader.h Fri Apr 29 12:22:58 2016
@@ -0,0 +1,46 @@
+//===- StreamReader.h - Reads bytes and objects from a stream ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_DEBUGINFO_PDB_RAW_STREAMREADER_H
+#define LLVM_DEBUGINFO_PDB_RAW_STREAMREADER_H
+
+#include "llvm/DebugInfo/PDB/Raw/StreamInterface.h"
+#include "llvm/Support/Endian.h"
+
+#include <string>
+#include <system_error>
+
+namespace llvm {
+
+class StreamReader {
+public:
+  StreamReader(const StreamInterface &S);
+
+  std::error_code readBytes(MutableArrayRef<uint8_t> Buffer);
+  std::error_code readInteger(uint32_t &Dest);
+  std::error_code readZeroString(std::string &Dest);
+
+  template <typename T> std::error_code readObject(T *Dest) {
+    MutableArrayRef<uint8_t> Buffer(reinterpret_cast<uint8_t *>(Dest),
+                                    sizeof(T));
+    return readBytes(Buffer);
+  }
+
+  void setOffset(uint32_t Off) { Offset = Off; }
+  uint32_t getOffset() const { return Offset; }
+  uint32_t getLength() const { return Stream.getLength(); }
+  uint32_t bytesRemaining() const { return getLength() - getOffset(); }
+
+private:
+  const StreamInterface &Stream;
+  uint32_t Offset;
+};
+}
+
+#endif

Modified: llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt (original)
+++ llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt Fri Apr 29 12:22:58 2016
@@ -27,13 +27,15 @@ if(HAVE_DIA_SDK)
 endif()
 
 add_pdb_impl_folder(Raw
+  Raw/ByteStream.cpp
+  Raw/MappedBlockStream.cpp
   Raw/ModInfo.cpp
   Raw/PDBFile.cpp
   Raw/PDBDbiStream.cpp
   Raw/PDBInfoStream.cpp
   Raw/PDBNameMap.cpp
-  Raw/PDBStream.cpp
-  Raw/RawSession.cpp)
+  Raw/RawSession.cpp
+  Raw/StreamReader.cpp)
 
 list(APPEND LIBPDB_ADDITIONAL_HEADER_DIRS "${LLVM_MAIN_INCLUDE_DIR}/llvm/DebugInfo/PDB")
 

Added: llvm/trunk/lib/DebugInfo/PDB/Raw/ByteStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/ByteStream.cpp?rev=268071&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/ByteStream.cpp (added)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/ByteStream.cpp Fri Apr 29 12:22:58 2016
@@ -0,0 +1,60 @@
+//===- ByteStream.cpp - Reads stream data from a byte sequence ------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/PDB/Raw/ByteStream.h"
+#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
+
+using namespace llvm;
+
+ByteStream::ByteStream() : Owned(false) {}
+
+ByteStream::ByteStream(MutableArrayRef<uint8_t> Bytes) : Owned(false) {
+  initialize(Bytes);
+}
+
+ByteStream::ByteStream(uint32_t Length) : Owned(false) { initialize(Length); }
+
+ByteStream::~ByteStream() { reset(); }
+
+void ByteStream::reset() {
+  if (Owned)
+    delete[] Data.data();
+  Owned = false;
+  Data = MutableArrayRef<uint8_t>();
+}
+
+void ByteStream::initialize(MutableArrayRef<uint8_t> Bytes) {
+  reset();
+  Data = Bytes;
+  Owned = false;
+}
+
+void ByteStream::initialize(uint32_t Length) {
+  reset();
+  Data = MutableArrayRef<uint8_t>(new uint8_t[Length], Length);
+  Owned = true;
+}
+
+std::error_code ByteStream::initialize(StreamReader &Reader, uint32_t Length) {
+  initialize(Length);
+  std::error_code EC = Reader.readBytes(Data);
+  if (EC)
+    reset();
+  return EC;
+}
+
+std::error_code ByteStream::readBytes(uint32_t Offset,
+                                      MutableArrayRef<uint8_t> Buffer) const {
+  if (Data.size() < Buffer.size() + Offset)
+    return std::make_error_code(std::errc::bad_address);
+  ::memcpy(Buffer.data(), Data.data() + Offset, Buffer.size());
+  return std::error_code();
+}
+
+uint32_t ByteStream::getLength() const { return Data.size(); }

Copied: llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp (from r268069, llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp)
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp?p2=llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp&p1=llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp&r1=268069&r2=268071&rev=268071&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp Fri Apr 29 12:22:58 2016
@@ -1,4 +1,4 @@
-//===- PDBStream.cpp - Low level interface to a PDB stream ------*- C++ -*-===//
+//===- MappedBlockStream.cpp - Reads stream data from a PDBFile -----------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -7,48 +7,31 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
+#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
 
 using namespace llvm;
 
-PDBStream::PDBStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
+MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
   StreamLength = Pdb.getStreamByteSize(StreamIdx);
   BlockList = Pdb.getStreamBlockList(StreamIdx);
-  Offset = 0;
 }
 
-std::error_code PDBStream::readInteger(uint32_t &Dest) {
-  support::ulittle32_t P;
-  if (std::error_code EC = readObject(&P))
-    return EC;
-  Dest = P;
-  return std::error_code();
-}
-
-std::error_code PDBStream::readZeroString(std::string &Dest) {
-  char C;
-  do {
-    readObject(&C);
-    if (C != '\0')
-      Dest.push_back(C);
-  } while (C != '\0');
-  return std::error_code();
-}
-
-std::error_code PDBStream::readBytes(void *Dest, uint32_t Length) {
+std::error_code
+MappedBlockStream::readBytes(uint32_t Offset,
+                             MutableArrayRef<uint8_t> Buffer) const {
   uint32_t BlockNum = Offset / Pdb.getBlockSize();
   uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
 
   // Make sure we aren't trying to read beyond the end of the stream.
-  if (Length > StreamLength)
+  if (Buffer.size() > StreamLength)
     return std::make_error_code(std::errc::bad_address);
-  if (Offset > StreamLength - Length)
+  if (Offset > StreamLength - Buffer.size())
     return std::make_error_code(std::errc::bad_address);
 
-  uint32_t BytesLeft = Length;
+  uint32_t BytesLeft = Buffer.size();
   uint32_t BytesWritten = 0;
-  char *WriteBuffer = static_cast<char *>(Dest);
+  uint8_t *WriteBuffer = Buffer.data();
   while (BytesLeft > 0) {
     uint32_t StreamBlockAddr = BlockList[BlockNum];
 
@@ -65,14 +48,5 @@ std::error_code PDBStream::readBytes(voi
     OffsetInBlock = 0;
   }
 
-  // Modify the offset to point to the data after the object.
-  Offset += Length;
-
   return std::error_code();
 }
-
-void PDBStream::setOffset(uint32_t O) { Offset = O; }
-
-uint32_t PDBStream::getOffset() const { return Offset; }
-
-uint32_t PDBStream::getLength() const { return StreamLength; }

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/PDBDbiStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/PDBDbiStream.cpp?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/PDBDbiStream.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/PDBDbiStream.cpp Fri Apr 29 12:22:58 2016
@@ -12,6 +12,7 @@
 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBInfoStream.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBRawConstants.h"
+#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
 
 using namespace llvm;
 using namespace llvm::support;
@@ -58,12 +59,12 @@ struct PDBDbiStream::HeaderInfo {
   little32_t SecContrSubstreamSize; // Size of sec. contribution stream
   little32_t SectionMapSize;        // Size of sec. map substream
   little32_t FileInfoSize;          // Size of file info substream
-  little32_t TypeServerSize;      // Size of type server map
-  ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
-  little32_t OptionalDbgHdrSize;  // Size of DbgHeader info
-  little32_t ECSubstreamSize;     // Size of EC stream (what is EC?)
-  ulittle16_t Flags;              // See DbiFlags enum.
-  ulittle16_t MachineType;        // See PDB_MachineType enum.
+  little32_t TypeServerSize;        // Size of type server map
+  ulittle32_t MFCTypeServerIndex;   // Index of MFC Type Server
+  little32_t OptionalDbgHdrSize;    // Size of DbgHeader info
+  little32_t ECSubstreamSize;       // Size of EC stream (what is EC?)
+  ulittle16_t Flags;                // See DbiFlags enum.
+  ulittle16_t MachineType;          // See PDB_MachineType enum.
 
   ulittle32_t Reserved; // Pad to 64 bytes
 };
@@ -75,12 +76,13 @@ PDBDbiStream::PDBDbiStream(PDBFile &File
 PDBDbiStream::~PDBDbiStream() {}
 
 std::error_code PDBDbiStream::reload() {
-  Stream.setOffset(0);
+  StreamReader Reader(Stream);
+
   Header.reset(new HeaderInfo());
 
   if (Stream.getLength() < sizeof(HeaderInfo))
     return std::make_error_code(std::errc::illegal_byte_sequence);
-  Stream.readObject(Header.get());
+  Reader.readObject(Header.get());
 
   if (Header->VersionSignature != -1)
     return std::make_error_code(std::errc::illegal_byte_sequence);
@@ -115,30 +117,35 @@ std::error_code PDBDbiStream::reload() {
     return std::make_error_code(std::errc::illegal_byte_sequence);
 
   std::error_code EC;
-  if ((EC = readSubstream(ModInfoSubstream, Header->ModiSubstreamSize)))
-    return EC;
+  ModInfoSubstream.initialize(Reader, Header->ModiSubstreamSize);
 
   // Since each ModInfo in the stream is a variable length, we have to iterate
   // them to know how many there actually are.
-  auto Range = llvm::make_range(ModInfoIterator(&ModInfoSubstream.front()),
-                                ModInfoIterator(&ModInfoSubstream.back() + 1));
+  auto Range =
+      llvm::make_range(ModInfoIterator(&ModInfoSubstream.data().front()),
+                       ModInfoIterator(&ModInfoSubstream.data().back() + 1));
   for (auto Info : Range)
     ModuleInfos.push_back(ModuleInfoEx(Info));
 
-  if ((EC = readSubstream(SecContrSubstream, Header->SecContrSubstreamSize)))
+  if ((EC = SecContrSubstream.initialize(Reader, Header->SecContrSubstreamSize)))
     return EC;
-  if ((EC = readSubstream(SecMapSubstream, Header->SectionMapSize)))
+  if ((EC = SecMapSubstream.initialize(Reader, Header->SectionMapSize)))
     return EC;
-  if ((EC = readSubstream(FileInfoSubstream, Header->FileInfoSize)))
+  if ((EC = FileInfoSubstream.initialize(Reader, Header->FileInfoSize)))
     return EC;
-  if ((EC = readSubstream(TypeServerMapSubstream, Header->TypeServerSize)))
+  if ((EC = TypeServerMapSubstream.initialize(Reader, Header->TypeServerSize)))
     return EC;
-  if ((EC = readSubstream(ECSubstream, Header->ECSubstreamSize)))
+  if ((EC = ECSubstream.initialize(Reader, Header->ECSubstreamSize)))
+    return EC;
+  if ((EC = DbgHeader.initialize(Reader, Header->OptionalDbgHdrSize)))
     return EC;
 
   if ((EC = initializeFileInfo()))
     return EC;
 
+  if (Reader.bytesRemaining() > 0)
+    return std::make_error_code(std::errc::illegal_byte_sequence);
+
   return std::error_code();
 }
 
@@ -182,15 +189,6 @@ PDB_Machine PDBDbiStream::getMachineType
 
 ArrayRef<ModuleInfoEx> PDBDbiStream::modules() const { return ModuleInfos; }
 
-std::error_code PDBDbiStream::readSubstream(std::vector<uint8_t> &Bytes, uint32_t Size) {
-  Bytes.clear();
-  if (Size == 0)
-    return std::error_code();
-
-  Bytes.resize(Size);
-  return Stream.readBytes(&Bytes[0], Size);
-}
-
 std::error_code PDBDbiStream::initializeFileInfo() {
   struct FileInfoSubstreamHeader {
     ulittle16_t NumModules;     // Total # of modules, should match number of
@@ -213,7 +211,7 @@ std::error_code PDBDbiStream::initialize
   // with the caveat that `NumSourceFiles` cannot be trusted, so
   // it is computed by summing `ModFileCounts`.
   //
-  const uint8_t *Buf = &FileInfoSubstream[0];
+  const uint8_t *Buf = &FileInfoSubstream.data().front();
   auto FI = reinterpret_cast<const FileInfoSubstreamHeader *>(Buf);
   Buf += sizeof(FileInfoSubstreamHeader);
   // The number of modules in the stream should be the same as reported by

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/PDBInfoStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/PDBInfoStream.cpp?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/PDBInfoStream.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/PDBInfoStream.cpp Fri Apr 29 12:22:58 2016
@@ -10,28 +10,30 @@
 #include "llvm/DebugInfo/PDB/Raw/PDBInfoStream.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
 
 using namespace llvm;
 
-PDBInfoStream::PDBInfoStream(PDBFile &File) : Pdb(File), Stream1(1, File) {}
+PDBInfoStream::PDBInfoStream(PDBFile &File) : Pdb(File), Stream(1, File) {}
 
 std::error_code PDBInfoStream::reload() {
-  Stream1.setOffset(0);
+  StreamReader Reader(Stream);
+
   support::ulittle32_t Value;
 
-  Stream1.readObject(&Value);
+  Reader.readObject(&Value);
   Version = Value;
   if (Version < PdbRaw_ImplVer::PdbImplVC70)
     return std::make_error_code(std::errc::not_supported);
 
-  Stream1.readObject(&Value);
+  Reader.readObject(&Value);
   Signature = Value;
 
-  Stream1.readObject(&Value);
+  Reader.readObject(&Value);
   Age = Value;
 
-  Stream1.readObject(&Guid);
-  NamedStreams.load(Stream1);
+  Reader.readObject(&Guid);
+  NamedStreams.load(Reader);
 
   return std::error_code();
 }

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/PDBNameMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/PDBNameMap.cpp?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/PDBNameMap.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/PDBNameMap.cpp Fri Apr 29 12:22:58 2016
@@ -9,13 +9,14 @@
 
 #include "llvm/DebugInfo/PDB/Raw/PDBNameMap.h"
 #include "llvm/ADT/BitVector.h"
-#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
+#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
 
 using namespace llvm;
 
 PDBNameMap::PDBNameMap() {}
 
-std::error_code PDBNameMap::load(PDBStream &Stream) {
+std::error_code PDBNameMap::load(StreamReader &Stream) {
+
   // This is some sort of weird string-set/hash table encoded in the stream.
   // It starts with the number of bytes in the table.
   uint32_t NumberOfBytes;

Removed: llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp?rev=268070&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/PDBStream.cpp (removed)
@@ -1,78 +0,0 @@
-//===- PDBStream.cpp - Low level interface to a PDB stream ------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
-#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
-
-using namespace llvm;
-
-PDBStream::PDBStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
-  StreamLength = Pdb.getStreamByteSize(StreamIdx);
-  BlockList = Pdb.getStreamBlockList(StreamIdx);
-  Offset = 0;
-}
-
-std::error_code PDBStream::readInteger(uint32_t &Dest) {
-  support::ulittle32_t P;
-  if (std::error_code EC = readObject(&P))
-    return EC;
-  Dest = P;
-  return std::error_code();
-}
-
-std::error_code PDBStream::readZeroString(std::string &Dest) {
-  char C;
-  do {
-    readObject(&C);
-    if (C != '\0')
-      Dest.push_back(C);
-  } while (C != '\0');
-  return std::error_code();
-}
-
-std::error_code PDBStream::readBytes(void *Dest, uint32_t Length) {
-  uint32_t BlockNum = Offset / Pdb.getBlockSize();
-  uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
-
-  // Make sure we aren't trying to read beyond the end of the stream.
-  if (Length > StreamLength)
-    return std::make_error_code(std::errc::bad_address);
-  if (Offset > StreamLength - Length)
-    return std::make_error_code(std::errc::bad_address);
-
-  uint32_t BytesLeft = Length;
-  uint32_t BytesWritten = 0;
-  char *WriteBuffer = static_cast<char *>(Dest);
-  while (BytesLeft > 0) {
-    uint32_t StreamBlockAddr = BlockList[BlockNum];
-
-    StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());
-
-    const char *ChunkStart = Data.data() + OffsetInBlock;
-    uint32_t BytesInChunk =
-        std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
-    ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
-
-    BytesWritten += BytesInChunk;
-    BytesLeft -= BytesInChunk;
-    ++BlockNum;
-    OffsetInBlock = 0;
-  }
-
-  // Modify the offset to point to the data after the object.
-  Offset += Length;
-
-  return std::error_code();
-}
-
-void PDBStream::setOffset(uint32_t O) { Offset = O; }
-
-uint32_t PDBStream::getOffset() const { return Offset; }
-
-uint32_t PDBStream::getLength() const { return StreamLength; }

Added: llvm/trunk/lib/DebugInfo/PDB/Raw/StreamReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/StreamReader.cpp?rev=268071&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/StreamReader.cpp (added)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/StreamReader.cpp Fri Apr 29 12:22:58 2016
@@ -0,0 +1,40 @@
+//===- StreamReader.cpp - Reads bytes and objects from a stream -----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
+
+using namespace llvm;
+
+StreamReader::StreamReader(const StreamInterface &S) : Stream(S), Offset(0) {}
+
+std::error_code StreamReader::readBytes(MutableArrayRef<uint8_t> Buffer) {
+  if (auto EC = Stream.readBytes(Offset, Buffer))
+    return EC;
+  Offset += Buffer.size();
+  return std::error_code();
+}
+
+std::error_code StreamReader::readInteger(uint32_t &Dest) {
+  support::ulittle32_t P;
+  if (std::error_code EC = readObject(&P))
+    return EC;
+  Dest = P;
+  return std::error_code();
+}
+
+std::error_code StreamReader::readZeroString(std::string &Dest) {
+  Dest.clear();
+  char C;
+  do {
+    readObject(&C);
+    if (C != '\0')
+      Dest.push_back(C);
+  } while (C != '\0');
+  return std::error_code();
+}

Modified: llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp?rev=268071&r1=268070&r2=268071&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp (original)
+++ llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp Fri Apr 29 12:22:58 2016
@@ -35,12 +35,13 @@
 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
+#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
 #include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBDbiStream.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBInfoStream.h"
-#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
 #include "llvm/DebugInfo/PDB/Raw/RawSession.h"
+#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/FileSystem.h"
@@ -246,17 +247,19 @@ static void dumpStructure(RawSession &RS
   // Let's try to dump out the named stream "/names".
   uint32_t NameStreamIndex = InfoStream.getNamedStreamIndex("/names");
   if (NameStreamIndex != 0) {
-    PDBStream NameStream(NameStreamIndex, File);
+    MappedBlockStream NameStream(NameStreamIndex, File);
+    StreamReader Reader(NameStream);
+
     outs() << "NameStream: " << NameStreamIndex << '\n';
 
     // The name stream appears to start with a signature and version.
     uint32_t NameStreamSignature;
-    NameStream.readInteger(NameStreamSignature);
+    Reader.readInteger(NameStreamSignature);
     outs() << "NameStreamSignature: ";
     outs().write_hex(NameStreamSignature) << '\n';
 
     uint32_t NameStreamVersion;
-    NameStream.readInteger(NameStreamVersion);
+    Reader.readInteger(NameStreamVersion);
     outs() << "NameStreamVersion: " << NameStreamVersion << '\n';
 
     // We only support this particular version of the name stream.




More information about the llvm-commits mailing list