1 //===- DebugStringTableSubsection.cpp - CodeView String Table -------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/Support/BinaryStreamReader.h"
13 #include "llvm/Support/BinaryStreamWriter.h"
14 #include "llvm/Support/Error.h"
20 using namespace llvm::codeview
;
22 DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()
23 : DebugSubsectionRef(DebugSubsectionKind::StringTable
) {}
25 Error
DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents
) {
27 return Error::success();
30 Error
DebugStringTableSubsectionRef::initialize(BinaryStreamReader
&Reader
) {
31 return Reader
.readStreamRef(Stream
);
35 DebugStringTableSubsectionRef::getString(uint32_t Offset
) const {
36 BinaryStreamReader
Reader(Stream
);
37 Reader
.setOffset(Offset
);
39 if (auto EC
= Reader
.readCString(Result
))
44 DebugStringTableSubsection::DebugStringTableSubsection()
45 : DebugSubsection(DebugSubsectionKind::StringTable
) {}
47 uint32_t DebugStringTableSubsection::insert(StringRef S
) {
48 auto P
= StringToId
.insert({S
, StringSize
});
50 // If a given string didn't exist in the string table, we want to increment
51 // the string table size and insert it into the reverse lookup.
53 IdToString
.insert({P
.first
->getValue(), P
.first
->getKey()});
54 StringSize
+= S
.size() + 1; // +1 for '\0'
57 return P
.first
->second
;
60 uint32_t DebugStringTableSubsection::calculateSerializedSize() const {
64 Error
DebugStringTableSubsection::commit(BinaryStreamWriter
&Writer
) const {
65 uint32_t Begin
= Writer
.getOffset();
66 uint32_t End
= Begin
+ StringSize
;
68 // Write a null string at the beginning.
69 if (auto EC
= Writer
.writeCString(StringRef()))
72 for (auto &Pair
: StringToId
) {
73 StringRef S
= Pair
.getKey();
74 uint32_t Offset
= Begin
+ Pair
.getValue();
75 Writer
.setOffset(Offset
);
76 if (auto EC
= Writer
.writeCString(S
))
78 assert(Writer
.getOffset() <= End
);
81 Writer
.setOffset(End
);
82 assert((End
- Begin
) == StringSize
);
83 return Error::success();
86 uint32_t DebugStringTableSubsection::size() const { return StringToId
.size(); }
88 std::vector
<uint32_t> DebugStringTableSubsection::sortedIds() const {
89 std::vector
<uint32_t> Result
;
90 Result
.reserve(IdToString
.size());
91 for (const auto &Entry
: IdToString
)
92 Result
.push_back(Entry
.first
);
97 uint32_t DebugStringTableSubsection::getIdForString(StringRef S
) const {
98 auto Iter
= StringToId
.find(S
);
99 assert(Iter
!= StringToId
.end());
103 StringRef
DebugStringTableSubsection::getStringForId(uint32_t Id
) const {
104 auto Iter
= IdToString
.find(Id
);
105 assert(Iter
!= IdToString
.end());