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 
31 
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 
42  return NS->getString(Offset);
43 }
44 
46  return NS->appendStringData(S);
47 }
48 
50  : HashTraits(*this), OffsetIndexMap(1, HashTraits) {}
51 
53  uint32_t StringBufferSize;
54  if (auto EC = Stream.readInteger(StringBufferSize))
55  return joinErrors(std::move(EC),
56  make_error<RawError>(raw_error_code::corrupt_file,
57  "Expected string buffer size"));
58 
59  StringRef Buffer;
60  if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
61  return EC;
62  NamesBuffer.assign(Buffer.begin(), Buffer.end());
63 
64  return OffsetIndexMap.load(Stream);
65 }
66 
68  // The first field is the number of bytes of string data.
69  if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
70  return EC;
71 
72  // Then the actual string data.
73  StringRef Data(NamesBuffer.data(), NamesBuffer.size());
74  if (auto EC = Writer.writeFixedString(Data))
75  return EC;
76 
77  // And finally the Offset Index map.
78  if (auto EC = OffsetIndexMap.commit(Writer))
79  return EC;
80 
81  return Error::success();
82 }
83 
85  return sizeof(uint32_t) // String data size
86  + NamesBuffer.size() // String data
87  + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
88 }
89 
90 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
91 
93  assert(NamesBuffer.size() > Offset);
94  return StringRef(NamesBuffer.data() + Offset);
95 }
96 
98  return hashStringV1(getString(Offset));
99 }
100 
101 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
102  auto Iter = OffsetIndexMap.find_as(Stream);
103  if (Iter == OffsetIndexMap.end())
104  return false;
105  StreamNo = (*Iter).second;
106  return true;
107 }
108 
110  StringMap<uint32_t> Result;
111  for (const auto &Entry : OffsetIndexMap) {
112  StringRef Stream(NamesBuffer.data() + Entry.first);
113  Result.try_emplace(Stream, Entry.second);
114  }
115  return Result;
116 }
117 
119  uint32_t Offset = NamesBuffer.size();
120  NamesBuffer.insert(NamesBuffer.end(), S.begin(), S.end());
121  NamesBuffer.push_back('\0');
122  return Offset;
123 }
124 
125 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
126  OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo));
127 }
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)
StringRef storageKeyToLookupKey(uint32_t Offset) const
uint16_t hashLookupKey(StringRef S) const
uint32_t lookupKeyToStorageKey(StringRef S)
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:395
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
NamedStreamMapTraits(NamedStreamMap &NS)