blob: 7b972a1a2770a9b6424d70950ec83041939aef2b [file] [log] [blame]
Zachary Turner591312c2017-05-30 17:13:33 +00001//===- DebugStringTableSubsection.cpp - CodeView String Table ---*- C++ -*-===//
Zachary Turnerc504ae32017-05-03 15:58:37 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner591312c2017-05-30 17:13:33 +000010#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
Zachary Turnerc504ae32017-05-03 15:58:37 +000011
12#include "llvm/Support/BinaryStream.h"
13#include "llvm/Support/BinaryStreamReader.h"
14#include "llvm/Support/BinaryStreamWriter.h"
15
16using namespace llvm;
17using namespace llvm::codeview;
18
Zachary Turner591312c2017-05-30 17:13:33 +000019DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()
20 : DebugSubsectionRef(DebugSubsectionKind::StringTable) {}
Zachary Turnerc504ae32017-05-03 15:58:37 +000021
Zachary Turner591312c2017-05-30 17:13:33 +000022Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000023 Stream = Contents;
24 return Error::success();
Zachary Turnerc504ae32017-05-03 15:58:37 +000025}
Zachary Turner1bf77622017-06-08 23:49:01 +000026Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) {
27 return Reader.readStreamRef(Stream, Reader.bytesRemaining());
28}
Zachary Turnerc504ae32017-05-03 15:58:37 +000029
Zachary Turner591312c2017-05-30 17:13:33 +000030Expected<StringRef>
31DebugStringTableSubsectionRef::getString(uint32_t Offset) const {
Zachary Turnerc504ae32017-05-03 15:58:37 +000032 BinaryStreamReader Reader(Stream);
33 Reader.setOffset(Offset);
34 StringRef Result;
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000035 if (auto EC = Reader.readCString(Result))
36 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +000037 return Result;
38}
39
Zachary Turner591312c2017-05-30 17:13:33 +000040DebugStringTableSubsection::DebugStringTableSubsection()
41 : DebugSubsection(DebugSubsectionKind::StringTable) {}
42
43uint32_t DebugStringTableSubsection::insert(StringRef S) {
Zachary Turnerc504ae32017-05-03 15:58:37 +000044 auto P = Strings.insert({S, StringSize});
45
46 // If a given string didn't exist in the string table, we want to increment
47 // the string table size.
48 if (P.second)
49 StringSize += S.size() + 1; // +1 for '\0'
50 return P.first->second;
51}
52
Zachary Turner591312c2017-05-30 17:13:33 +000053uint32_t DebugStringTableSubsection::calculateSerializedSize() const {
54 return StringSize;
55}
Zachary Turnerc504ae32017-05-03 15:58:37 +000056
Zachary Turner591312c2017-05-30 17:13:33 +000057Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const {
Zachary Turnerc504ae32017-05-03 15:58:37 +000058 assert(Writer.bytesRemaining() == StringSize);
59 uint32_t MaxOffset = 1;
60
61 for (auto &Pair : Strings) {
62 StringRef S = Pair.getKey();
63 uint32_t Offset = Pair.getValue();
64 Writer.setOffset(Offset);
65 if (auto EC = Writer.writeCString(S))
66 return EC;
67 MaxOffset = std::max<uint32_t>(MaxOffset, Offset + S.size() + 1);
68 }
69
70 Writer.setOffset(MaxOffset);
71 assert(Writer.bytesRemaining() == 0);
72 return Error::success();
73}
74
Zachary Turner591312c2017-05-30 17:13:33 +000075uint32_t DebugStringTableSubsection::size() const { return Strings.size(); }
Zachary Turnercf468d82017-05-03 17:11:40 +000076
Zachary Turner591312c2017-05-30 17:13:33 +000077uint32_t DebugStringTableSubsection::getStringId(StringRef S) const {
Zachary Turner92dcdda2017-06-02 19:49:14 +000078 auto Iter = Strings.find(S);
79 assert(Iter != Strings.end());
80 return Iter->second;
Zachary Turnercf468d82017-05-03 17:11:40 +000081}