LLVM 24.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
10#include "llvm/ADT/StringMap.h"
11#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Endian.h"
18#include "llvm/Support/Error.h"
19#include <cassert>
20#include <cstdint>
21
22using namespace llvm;
23using namespace llvm::pdb;
24
26
28 // In the reference implementation, this uses
29 // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
30 // Here, the type HASH is a typedef of unsigned short.
31 // ** It is not a bug that we truncate the result of hashStringV1, in fact
32 // it is a bug if we do not! **
33 // See NMTNI::hash() in the reference implementation.
34 return static_cast<uint16_t>(hashStringV1(S));
35}
36
40
42 return NS->appendStringData(S);
43}
44
45NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
46
48 uint32_t StringBufferSize;
49 if (auto EC = Stream.readInteger(StringBufferSize))
50 return joinErrors(std::move(EC),
52 "Expected string buffer size"));
53
54 StringRef Buffer;
55 if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
56 return EC;
57 NamesBuffer.assign(Buffer.begin(), Buffer.end());
58
59 if (auto EC = OffsetIndexMap.load(Stream))
60 return EC;
61
62 uint32_t NiMac;
63 return Stream.readInteger(NiMac);
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 // Followed by the Offset Index map.
77 if (auto EC = OffsetIndexMap.commit(Writer))
78 return EC;
79
80 // And finally the NMTNI::niMac value.
81 if (auto EC = Writer.writeInteger<uint32_t>(0))
82 return EC;
83
84 return Error::success();
85}
86
88 return sizeof(uint32_t) // String data size
89 + NamesBuffer.size() // String data
90 + OffsetIndexMap.calculateSerializedLength() // Offset Index Map
91 + sizeof(uint32_t); // NMTNI::niMac.
92}
93
94uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
95
97 assert(NamesBuffer.size() > Offset);
98 return StringRef(NamesBuffer.data() + Offset);
99}
100
104
105bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
106 auto Iter = OffsetIndexMap.find_as(Stream, HashTraits);
107 if (Iter == OffsetIndexMap.end())
108 return false;
109 StreamNo = (*Iter).second;
110 return true;
111}
112
114 StringMap<uint32_t> Result;
115 for (const auto &Entry : OffsetIndexMap) {
116 StringRef Stream(NamesBuffer.data() + Entry.first);
117 Result.try_emplace(Stream, Entry.second);
118 }
119 return Result;
120}
121
123 uint32_t Offset = NamesBuffer.size();
124 llvm::append_range(NamesBuffer, S);
125 NamesBuffer.push_back('\0');
126 return Offset;
127}
128
129void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
130 OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
131}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap 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.
LLVM_ABI 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.
LLVM_ABI 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:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
iterator begin() const
Definition StringRef.h:114
iterator end() const
Definition StringRef.h:116
LLVM_ABI void set(StringRef Stream, uint32_t StreamNo)
LLVM_ABI Error load(BinaryStreamReader &Stream)
LLVM_ABI StringMap< uint32_t > entries() const
LLVM_ABI Error commit(BinaryStreamWriter &Writer) const
LLVM_ABI uint32_t hashString(uint32_t Offset) const
LLVM_ABI uint32_t appendStringData(StringRef S)
LLVM_ABI bool get(StringRef Stream, uint32_t &StreamNo) const
LLVM_ABI StringRef getString(uint32_t Offset) const
LLVM_ABI uint32_t size() const
LLVM_ABI uint32_t calculateSerializedLength() const
LLVM_ABI uint32_t hashStringV1(StringRef Str)
Definition Hash.cpp:20
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition Endian.h:270
This is an optimization pass for GlobalISel generic memory operations.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI NamedStreamMapTraits(NamedStreamMap &NS)
LLVM_ABI uint16_t hashLookupKey(StringRef S) const
LLVM_ABI uint32_t lookupKeyToStorageKey(StringRef S)
LLVM_ABI StringRef storageKeyToLookupKey(uint32_t Offset) const