]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/DebugInfo/PDB/Native/NamedStreamMap.cpp
Merge llvm-project main llvmorg-17-init-19304-gd0b54bb50e51
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / DebugInfo / PDB / Native / NamedStreamMap.cpp
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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
10 #include "llvm/ADT/SparseBitVector.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/ADT/StringRef.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/BinaryStreamWriter.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cstdint>
23
24 using namespace llvm;
25 using namespace llvm::pdb;
26
27 NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {}
28
29 uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const {
30   // In the reference implementation, this uses
31   // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
32   // Here, the type HASH is a typedef of unsigned short.
33   // ** It is not a bug that we truncate the result of hashStringV1, in fact
34   //    it is a bug if we do not! **
35   // See NMTNI::hash() in the reference implementation.
36   return static_cast<uint16_t>(hashStringV1(S));
37 }
38
39 StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const {
40   return NS->getString(Offset);
41 }
42
43 uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) {
44   return NS->appendStringData(S);
45 }
46
47 NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
48
49 Error NamedStreamMap::load(BinaryStreamReader &Stream) {
50   uint32_t StringBufferSize;
51   if (auto EC = Stream.readInteger(StringBufferSize))
52     return joinErrors(std::move(EC),
53                       make_error<RawError>(raw_error_code::corrupt_file,
54                                            "Expected string buffer size"));
55
56   StringRef Buffer;
57   if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
58     return EC;
59   NamesBuffer.assign(Buffer.begin(), Buffer.end());
60
61   return OffsetIndexMap.load(Stream);
62 }
63
64 Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const {
65   // The first field is the number of bytes of string data.
66   if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
67     return EC;
68
69   // Then the actual string data.
70   StringRef Data(NamesBuffer.data(), NamesBuffer.size());
71   if (auto EC = Writer.writeFixedString(Data))
72     return EC;
73
74   // And finally the Offset Index map.
75   if (auto EC = OffsetIndexMap.commit(Writer))
76     return EC;
77
78   return Error::success();
79 }
80
81 uint32_t NamedStreamMap::calculateSerializedLength() const {
82   return sizeof(uint32_t)                              // String data size
83          + NamesBuffer.size()                          // String data
84          + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
85 }
86
87 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
88
89 StringRef NamedStreamMap::getString(uint32_t Offset) const {
90   assert(NamesBuffer.size() > Offset);
91   return StringRef(NamesBuffer.data() + Offset);
92 }
93
94 uint32_t NamedStreamMap::hashString(uint32_t Offset) const {
95   return hashStringV1(getString(Offset));
96 }
97
98 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
99   auto Iter = OffsetIndexMap.find_as(Stream, HashTraits);
100   if (Iter == OffsetIndexMap.end())
101     return false;
102   StreamNo = (*Iter).second;
103   return true;
104 }
105
106 StringMap<uint32_t> NamedStreamMap::entries() const {
107   StringMap<uint32_t> Result;
108   for (const auto &Entry : OffsetIndexMap) {
109     StringRef Stream(NamesBuffer.data() + Entry.first);
110     Result.try_emplace(Stream, Entry.second);
111   }
112   return Result;
113 }
114
115 uint32_t NamedStreamMap::appendStringData(StringRef S) {
116   uint32_t Offset = NamesBuffer.size();
117   llvm::append_range(NamesBuffer, S);
118   NamesBuffer.push_back('\0');
119   return Offset;
120 }
121
122 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
123   OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
124 }