LLVM  7.0.0svn
NamedStreamMap.cpp
Go to the documentation of this file.
1 //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
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 
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstdint>
25 #include <tuple>
26 
27 using namespace llvm;
28 using namespace llvm::pdb;
29 
30 namespace {
31 struct NamedStreamMapTraits {
32  static uint16_t hash(StringRef S, const NamedStreamMap &NS) {
33  // In the reference implementation, this uses
34  // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
35  // Here, the type HASH is a typedef of unsigned short.
36  // ** It is not a bug that we truncate the result of hashStringV1, in fact
37  // it is a bug if we do not! **
38  return static_cast<uint16_t>(hashStringV1(S));
39  }
40  static StringRef realKey(uint32_t Offset, const NamedStreamMap &NS) {
41  return NS.getString(Offset);
42  }
43  static uint32_t lowerKey(StringRef S, NamedStreamMap &NS) {
44  return NS.appendStringData(S);
45  }
46 };
47 } // namespace
48 
50 
52  uint32_t StringBufferSize;
53  if (auto EC = Stream.readInteger(StringBufferSize))
54  return joinErrors(std::move(EC),
55  make_error<RawError>(raw_error_code::corrupt_file,
56  "Expected string buffer size"));
57 
58  StringRef Buffer;
59  if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
60  return EC;
61  NamesBuffer.assign(Buffer.begin(), Buffer.end());
62 
63  return OffsetIndexMap.load(Stream);
64 }
65 
67  // The first field is the number of bytes of string data.
68  if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
69  return EC;
70 
71  // Then the actual string data.
72  StringRef Data(NamesBuffer.data(), NamesBuffer.size());
73  if (auto EC = Writer.writeFixedString(Data))
74  return EC;
75 
76  // And finally the Offset Index map.
77  if (auto EC = OffsetIndexMap.commit(Writer))
78  return EC;
79 
80  return Error::success();
81 }
82 
84  return sizeof(uint32_t) // String data size
85  + NamesBuffer.size() // String data
86  + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
87 }
88 
89 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
90 
92  assert(NamesBuffer.size() > Offset);
93  return StringRef(NamesBuffer.data() + Offset);
94 }
95 
97  return hashStringV1(getString(Offset));
98 }
99 
100 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
101  auto Iter = OffsetIndexMap.find_as<NamedStreamMapTraits>(Stream, *this);
102  if (Iter == OffsetIndexMap.end())
103  return false;
104  StreamNo = (*Iter).second;
105  return true;
106 }
107 
109  StringMap<uint32_t> Result;
110  for (const auto &Entry : OffsetIndexMap) {
111  StringRef Stream(NamesBuffer.data() + Entry.first);
112  Result.try_emplace(Stream, Entry.second);
113  }
114  return Result;
115 }
116 
118  uint32_t Offset = NamesBuffer.size();
119  NamesBuffer.insert(NamesBuffer.end(), S.begin(), S.end());
120  NamesBuffer.push_back('\0');
121  return Offset;
122 }
123 
124 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
125  OffsetIndexMap.set_as<NamedStreamMapTraits>(Stream, StreamNo, *this);
126 }
uint32_t calculateSerializedLength() const
uint32_t appendStringData(StringRef S)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream&#39;s offset.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
StringRef getString(uint32_t Offset) const
void set(StringRef Stream, uint32_t StreamNo)
Error writeFixedString(StringRef Str)
Write the string Str to the underlying stream without a null terminator.
Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&... Args)
Emplace a new element for the specified key into the map if the key isn&#39;t already in the map...
Definition: StringMap.h:397
Error load(BinaryStreamReader &Stream)
StringMap< uint32_t > entries() const
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:21
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
uint32_t hashString(uint32_t Offset) const
Error commit(BinaryStreamWriter &Writer) const
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
iterator begin() const
Definition: StringRef.h:106
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:408
bool get(StringRef Stream, uint32_t &StreamNo) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
iterator end() const
Definition: StringRef.h:108