1 //===- DebugStringTableSubsection.cpp - CodeView String Table -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/DebugInfo/CodeView/CodeView.h"
13 #include "llvm/Support/BinaryStreamReader.h"
14 #include "llvm/Support/BinaryStreamWriter.h"
15 #include "llvm/Support/Error.h"
21 using namespace llvm::codeview
;
23 DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()
24 : DebugSubsectionRef(DebugSubsectionKind::StringTable
) {}
26 Error
DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents
) {
28 return Error::success();
31 Error
DebugStringTableSubsectionRef::initialize(BinaryStreamReader
&Reader
) {
32 return Reader
.readStreamRef(Stream
);
36 DebugStringTableSubsectionRef::getString(uint32_t Offset
) const {
37 BinaryStreamReader
Reader(Stream
);
38 Reader
.setOffset(Offset
);
40 if (auto EC
= Reader
.readCString(Result
))
45 DebugStringTableSubsection::DebugStringTableSubsection()
46 : DebugSubsection(DebugSubsectionKind::StringTable
) {}
48 uint32_t DebugStringTableSubsection::insert(StringRef S
) {
49 auto P
= StringToId
.insert({S
, StringSize
});
51 // If a given string didn't exist in the string table, we want to increment
52 // the string table size and insert it into the reverse lookup.
54 IdToString
.insert({P
.first
->getValue(), P
.first
->getKey()});
55 StringSize
+= S
.size() + 1; // +1 for '\0'
58 return P
.first
->second
;
61 uint32_t DebugStringTableSubsection::calculateSerializedSize() const {
65 Error
DebugStringTableSubsection::commit(BinaryStreamWriter
&Writer
) const {
66 uint32_t Begin
= Writer
.getOffset();
67 uint32_t End
= Begin
+ StringSize
;
69 // Write a null string at the beginning.
70 if (auto EC
= Writer
.writeCString(StringRef()))
73 for (auto &Pair
: StringToId
) {
74 StringRef S
= Pair
.getKey();
75 uint32_t Offset
= Begin
+ Pair
.getValue();
76 Writer
.setOffset(Offset
);
77 if (auto EC
= Writer
.writeCString(S
))
79 assert(Writer
.getOffset() <= End
);
82 Writer
.setOffset(End
);
83 assert((End
- Begin
) == StringSize
);
84 return Error::success();
87 uint32_t DebugStringTableSubsection::size() const { return StringToId
.size(); }
89 std::vector
<uint32_t> DebugStringTableSubsection::sortedIds() const {
90 std::vector
<uint32_t> Result
;
91 Result
.reserve(IdToString
.size());
92 for (const auto &Entry
: IdToString
)
93 Result
.push_back(Entry
.first
);
98 uint32_t DebugStringTableSubsection::getIdForString(StringRef S
) const {
99 auto Iter
= StringToId
.find(S
);
100 assert(Iter
!= StringToId
.end());
104 StringRef
DebugStringTableSubsection::getStringForId(uint32_t Id
) const {
105 auto Iter
= IdToString
.find(Id
);
106 assert(Iter
!= IdToString
.end());