LLVM 18.0.0git
NamedStreamMap.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/StringMap.h"
12#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/Error.h"
20#include <algorithm>
21#include <cassert>
22#include <cstdint>
23
24using namespace llvm;
25using namespace llvm::pdb;
26
28
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
40 return NS->getString(Offset);
41}
42
44 return NS->appendStringData(S);
45}
46
47NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
48
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
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
82 return sizeof(uint32_t) // String data size
83 + NamesBuffer.size() // String data
84 + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
85}
86
87uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
88
90 assert(NamesBuffer.size() > Offset);
91 return StringRef(NamesBuffer.data() + Offset);
92}
93
96}
97
98bool 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
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
116 uint32_t Offset = NamesBuffer.size();
117 llvm::append_range(NamesBuffer, S);
118 NamesBuffer.push_back('\0');
119 return Offset;
120}
121
122void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
123 OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
124}
This file defines the StringMap class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SparseBitVector class.
Provides read only access to a subclass of BinaryStream.
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
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.
Error writeFixedString(StringRef Str)
Write the string Str to the underlying stream without a null terminator.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
iterator begin() const
Definition: StringRef.h:111
iterator end() const
Definition: StringRef.h:113
void set(StringRef Stream, uint32_t StreamNo)
Error load(BinaryStreamReader &Stream)
StringMap< uint32_t > entries() const
Error commit(BinaryStreamWriter &Writer) const
uint32_t hashString(uint32_t Offset) const
uint32_t appendStringData(StringRef S)
bool get(StringRef Stream, uint32_t &StreamNo) const
StringRef getString(uint32_t Offset) const
uint32_t calculateSerializedLength() const
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:20
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:2037
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:431
NamedStreamMapTraits(NamedStreamMap &NS)
uint16_t hashLookupKey(StringRef S) const
uint32_t lookupKeyToStorageKey(StringRef S)
StringRef storageKeyToLookupKey(uint32_t Offset) const