Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1 | //===- DebugStringTableSubsection.cpp - CodeView String Table ---*- C++ -*-===// |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 19 | DebugStringTableSubsectionRef::DebugStringTableSubsectionRef() |
| 20 | : DebugSubsectionRef(DebugSubsectionKind::StringTable) {} |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 21 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 22 | Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) { |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 23 | Stream = Contents; |
| 24 | return Error::success(); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 25 | } |
Zachary Turner | 1bf7762 | 2017-06-08 23:49:01 +0000 | [diff] [blame^] | 26 | Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) { |
| 27 | return Reader.readStreamRef(Stream, Reader.bytesRemaining()); |
| 28 | } |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 29 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 30 | Expected<StringRef> |
| 31 | DebugStringTableSubsectionRef::getString(uint32_t Offset) const { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 32 | BinaryStreamReader Reader(Stream); |
| 33 | Reader.setOffset(Offset); |
| 34 | StringRef Result; |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 35 | if (auto EC = Reader.readCString(Result)) |
| 36 | return std::move(EC); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 37 | return Result; |
| 38 | } |
| 39 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 40 | DebugStringTableSubsection::DebugStringTableSubsection() |
| 41 | : DebugSubsection(DebugSubsectionKind::StringTable) {} |
| 42 | |
| 43 | uint32_t DebugStringTableSubsection::insert(StringRef S) { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 44 | 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 Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 53 | uint32_t DebugStringTableSubsection::calculateSerializedSize() const { |
| 54 | return StringSize; |
| 55 | } |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 56 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 57 | Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 58 | 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 Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 75 | uint32_t DebugStringTableSubsection::size() const { return Strings.size(); } |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 76 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 77 | uint32_t DebugStringTableSubsection::getStringId(StringRef S) const { |
Zachary Turner | 92dcdda | 2017-06-02 19:49:14 +0000 | [diff] [blame] | 78 | auto Iter = Strings.find(S); |
| 79 | assert(Iter != Strings.end()); |
| 80 | return Iter->second; |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 81 | } |