blob: f496854ffaf3ebf6cb647132aa660a013c8da6f2 [file] [log] [blame]
Zachary Turnerc504ae32017-05-03 15:58:37 +00001//===- StringTable.cpp - CodeView String Table Reader/Writer ----*- C++ -*-===//
2//
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
10#include "llvm/DebugInfo/CodeView/StringTable.h"
11
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
19StringTableRef::StringTableRef() {}
20
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000021Error StringTableRef::initialize(BinaryStreamRef Contents) {
22 Stream = Contents;
23 return Error::success();
Zachary Turnerc504ae32017-05-03 15:58:37 +000024}
25
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000026Expected<StringRef> StringTableRef::getString(uint32_t Offset) const {
Zachary Turnerc504ae32017-05-03 15:58:37 +000027 BinaryStreamReader Reader(Stream);
28 Reader.setOffset(Offset);
29 StringRef Result;
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000030 if (auto EC = Reader.readCString(Result))
31 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +000032 return Result;
33}
34
35uint32_t StringTable::insert(StringRef S) {
36 auto P = Strings.insert({S, StringSize});
37
38 // If a given string didn't exist in the string table, we want to increment
39 // the string table size.
40 if (P.second)
41 StringSize += S.size() + 1; // +1 for '\0'
42 return P.first->second;
43}
44
45uint32_t StringTable::calculateSerializedSize() const { return StringSize; }
46
47Error StringTable::commit(BinaryStreamWriter &Writer) const {
48 assert(Writer.bytesRemaining() == StringSize);
49 uint32_t MaxOffset = 1;
50
51 for (auto &Pair : Strings) {
52 StringRef S = Pair.getKey();
53 uint32_t Offset = Pair.getValue();
54 Writer.setOffset(Offset);
55 if (auto EC = Writer.writeCString(S))
56 return EC;
57 MaxOffset = std::max<uint32_t>(MaxOffset, Offset + S.size() + 1);
58 }
59
60 Writer.setOffset(MaxOffset);
61 assert(Writer.bytesRemaining() == 0);
62 return Error::success();
63}
64
65uint32_t StringTable::size() const { return Strings.size(); }