Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 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 | StringTableRef::StringTableRef() {} |
| 20 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame^] | 21 | Error StringTableRef::initialize(BinaryStreamRef Contents) { |
| 22 | Stream = Contents; |
| 23 | return Error::success(); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame^] | 26 | Expected<StringRef> StringTableRef::getString(uint32_t Offset) const { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 27 | BinaryStreamReader Reader(Stream); |
| 28 | Reader.setOffset(Offset); |
| 29 | StringRef Result; |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame^] | 30 | if (auto EC = Reader.readCString(Result)) |
| 31 | return std::move(EC); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 32 | return Result; |
| 33 | } |
| 34 | |
| 35 | uint32_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 | |
| 45 | uint32_t StringTable::calculateSerializedSize() const { return StringSize; } |
| 46 | |
| 47 | Error 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 | |
| 65 | uint32_t StringTable::size() const { return Strings.size(); } |