LLVM 20.0.0git
DebugStringTableSubsection.cpp
Go to the documentation of this file.
1//===- DebugStringTableSubsection.cpp - CodeView String Table -------------===//
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/StringRef.h"
14#include "llvm/Support/Error.h"
15#include <cassert>
16#include <cstdint>
17
18using namespace llvm;
19using namespace llvm::codeview;
20
23
25 Stream = Contents;
26 return Error::success();
27}
28
30 return Reader.readStreamRef(Stream);
31}
32
35 BinaryStreamReader Reader(Stream);
36 Reader.setOffset(Offset);
37 StringRef Result;
38 if (auto EC = Reader.readCString(Result))
39 return std::move(EC);
40 return Result;
41}
42
45
47 auto P = StringToId.insert({S, StringSize});
48
49 // If a given string didn't exist in the string table, we want to increment
50 // the string table size and insert it into the reverse lookup.
51 if (P.second) {
52 IdToString.insert({P.first->getValue(), P.first->getKey()});
53 StringSize += S.size() + 1; // +1 for '\0'
54 }
55
56 return P.first->second;
57}
58
60 return StringSize;
61}
62
64 uint32_t Begin = Writer.getOffset();
65 uint32_t End = Begin + StringSize;
66
67 // Write a null string at the beginning.
68 if (auto EC = Writer.writeCString(StringRef()))
69 return EC;
70
71 for (auto &Pair : StringToId) {
72 StringRef S = Pair.getKey();
73 uint32_t Offset = Begin + Pair.getValue();
74 Writer.setOffset(Offset);
75 if (auto EC = Writer.writeCString(S))
76 return EC;
77 assert(Writer.getOffset() <= End);
78 }
79
80 Writer.setOffset(End);
81 assert((End - Begin) == StringSize);
82 return Error::success();
83}
84
85uint32_t DebugStringTableSubsection::size() const { return StringToId.size(); }
86
87std::vector<uint32_t> DebugStringTableSubsection::sortedIds() const {
88 std::vector<uint32_t> Result;
89 Result.reserve(IdToString.size());
90 for (const auto &Entry : IdToString)
91 Result.push_back(Entry.first);
92 llvm::sort(Result);
93 return Result;
94}
95
97 auto Iter = StringToId.find(S);
98 assert(Iter != StringToId.end());
99 return Iter->second;
100}
101
103 auto Iter = IdToString.find(Id);
104 assert(Iter != IdToString.end());
105 return Iter->second;
106}
bool End
Definition: ELF_riscv.cpp:480
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Provides read only access to a subclass of BinaryStream.
Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
void setOffset(uint64_t Off)
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Provides write only access to a subclass of WritableBinaryStream.
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
void setOffset(uint64_t Off)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
unsigned size() const
Definition: StringMap.h:104
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
A table of densely packed, null-terminated strings indexed by offset.
Definition: StringTable.h:31
Expected< StringRef > getString(uint32_t Offset) const
Error commit(BinaryStreamWriter &Writer) const override
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1664