Line data Source code
1 : //===- 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 :
16 : using namespace llvm;
17 : using namespace llvm::codeview;
18 :
19 616 : StringTableRef::StringTableRef() {}
20 :
21 97 : Error StringTableRef::initialize(BinaryStreamRef Contents) {
22 194 : Stream = Contents;
23 291 : return Error::success();
24 : }
25 :
26 323 : Expected<StringRef> StringTableRef::getString(uint32_t Offset) const {
27 969 : BinaryStreamReader Reader(Stream);
28 646 : Reader.setOffset(Offset);
29 323 : StringRef Result;
30 969 : if (auto EC = Reader.readCString(Result))
31 0 : return std::move(EC);
32 : return Result;
33 : }
34 :
35 9 : uint32_t StringTable::insert(StringRef S) {
36 27 : 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 9 : if (P.second)
41 8 : StringSize += S.size() + 1; // +1 for '\0'
42 9 : return P.first->second;
43 : }
44 :
45 87 : uint32_t StringTable::calculateSerializedSize() const { return StringSize; }
46 :
47 29 : Error StringTable::commit(BinaryStreamWriter &Writer) const {
48 : assert(Writer.bytesRemaining() == StringSize);
49 29 : uint32_t MaxOffset = 1;
50 :
51 132 : for (auto &Pair : Strings) {
52 8 : StringRef S = Pair.getKey();
53 8 : uint32_t Offset = Pair.getValue();
54 16 : Writer.setOffset(Offset);
55 24 : if (auto EC = Writer.writeCString(S))
56 0 : return EC;
57 16 : MaxOffset = std::max<uint32_t>(MaxOffset, Offset + S.size() + 1);
58 : }
59 :
60 58 : Writer.setOffset(MaxOffset);
61 : assert(Writer.bytesRemaining() == 0);
62 87 : return Error::success();
63 : }
64 :
65 116 : uint32_t StringTable::size() const { return Strings.size(); }
66 :
67 2 : uint32_t StringTable::getStringId(StringRef S) const {
68 2 : auto P = Strings.find(S);
69 : assert(P != Strings.end());
70 2 : return P->second;
71 : }
|