[NFC] Update memcpy tests
[llvm-complete.git] / lib / DebugInfo / PDB / Native / NamedStreamMap.cpp
blob1c044e0c2653837a4e6aede26f93b02f9d063dfe
1 //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
10 #include "llvm/ADT/StringMap.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/iterator_range.h"
13 #include "llvm/DebugInfo/PDB/Native/Hash.h"
14 #include "llvm/DebugInfo/PDB/Native/HashTable.h"
15 #include "llvm/DebugInfo/PDB/Native/RawError.h"
16 #include "llvm/Support/BinaryStreamReader.h"
17 #include "llvm/Support/BinaryStreamRef.h"
18 #include "llvm/Support/BinaryStreamWriter.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Error.h"
21 #include <algorithm>
22 #include <cassert>
23 #include <cstdint>
24 #include <tuple>
26 using namespace llvm;
27 using namespace llvm::pdb;
29 NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {}
31 uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const {
32 // In the reference implementation, this uses
33 // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
34 // Here, the type HASH is a typedef of unsigned short.
35 // ** It is not a bug that we truncate the result of hashStringV1, in fact
36 // it is a bug if we do not! **
37 // See NMTNI::hash() in the reference implementation.
38 return static_cast<uint16_t>(hashStringV1(S));
41 StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const {
42 return NS->getString(Offset);
45 uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) {
46 return NS->appendStringData(S);
49 NamedStreamMap::NamedStreamMap()
50 : HashTraits(*this), OffsetIndexMap(1, HashTraits) {}
52 Error NamedStreamMap::load(BinaryStreamReader &Stream) {
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"));
59 StringRef Buffer;
60 if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
61 return EC;
62 NamesBuffer.assign(Buffer.begin(), Buffer.end());
64 return OffsetIndexMap.load(Stream);
67 Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const {
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;
72 // Then the actual string data.
73 StringRef Data(NamesBuffer.data(), NamesBuffer.size());
74 if (auto EC = Writer.writeFixedString(Data))
75 return EC;
77 // And finally the Offset Index map.
78 if (auto EC = OffsetIndexMap.commit(Writer))
79 return EC;
81 return Error::success();
84 uint32_t NamedStreamMap::calculateSerializedLength() const {
85 return sizeof(uint32_t) // String data size
86 + NamesBuffer.size() // String data
87 + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
90 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
92 StringRef NamedStreamMap::getString(uint32_t Offset) const {
93 assert(NamesBuffer.size() > Offset);
94 return StringRef(NamesBuffer.data() + Offset);
97 uint32_t NamedStreamMap::hashString(uint32_t Offset) const {
98 return hashStringV1(getString(Offset));
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;
109 StringMap<uint32_t> NamedStreamMap::entries() const {
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);
115 return Result;
118 uint32_t NamedStreamMap::appendStringData(StringRef S) {
119 uint32_t Offset = NamesBuffer.size();
120 NamesBuffer.insert(NamesBuffer.end(), S.begin(), S.end());
121 NamesBuffer.push_back('\0');
122 return Offset;
125 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
126 OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo));