189cb50c9SDimitry Andric //===- DebugStringTableSubsection.cpp - CodeView String Table ---*- C++ -*-===//
289cb50c9SDimitry Andric //
389cb50c9SDimitry Andric //                     The LLVM Compiler Infrastructure
489cb50c9SDimitry Andric //
589cb50c9SDimitry Andric // This file is distributed under the University of Illinois Open Source
689cb50c9SDimitry Andric // License. See LICENSE.TXT for details.
789cb50c9SDimitry Andric //
889cb50c9SDimitry Andric //===----------------------------------------------------------------------===//
989cb50c9SDimitry Andric 
1089cb50c9SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
1189cb50c9SDimitry Andric 
1289cb50c9SDimitry Andric #include "llvm/Support/BinaryStream.h"
1389cb50c9SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
1489cb50c9SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
1589cb50c9SDimitry Andric 
1689cb50c9SDimitry Andric using namespace llvm;
1789cb50c9SDimitry Andric using namespace llvm::codeview;
1889cb50c9SDimitry Andric 
1989cb50c9SDimitry Andric DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()
2089cb50c9SDimitry Andric     : DebugSubsectionRef(DebugSubsectionKind::StringTable) {}
2189cb50c9SDimitry Andric 
2289cb50c9SDimitry Andric Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) {
2389cb50c9SDimitry Andric   Stream = Contents;
2489cb50c9SDimitry Andric   return Error::success();
2589cb50c9SDimitry Andric }
2689cb50c9SDimitry Andric 
2789cb50c9SDimitry Andric Expected<StringRef>
2889cb50c9SDimitry Andric DebugStringTableSubsectionRef::getString(uint32_t Offset) const {
2989cb50c9SDimitry Andric   BinaryStreamReader Reader(Stream);
3089cb50c9SDimitry Andric   Reader.setOffset(Offset);
3189cb50c9SDimitry Andric   StringRef Result;
3289cb50c9SDimitry Andric   if (auto EC = Reader.readCString(Result))
3389cb50c9SDimitry Andric     return std::move(EC);
3489cb50c9SDimitry Andric   return Result;
3589cb50c9SDimitry Andric }
3689cb50c9SDimitry Andric 
3789cb50c9SDimitry Andric DebugStringTableSubsection::DebugStringTableSubsection()
3889cb50c9SDimitry Andric     : DebugSubsection(DebugSubsectionKind::StringTable) {}
3989cb50c9SDimitry Andric 
4089cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::insert(StringRef S) {
4189cb50c9SDimitry Andric   auto P = Strings.insert({S, StringSize});
4289cb50c9SDimitry Andric 
4389cb50c9SDimitry Andric   // If a given string didn't exist in the string table, we want to increment
4489cb50c9SDimitry Andric   // the string table size.
4589cb50c9SDimitry Andric   if (P.second)
4689cb50c9SDimitry Andric     StringSize += S.size() + 1; // +1 for '\0'
4789cb50c9SDimitry Andric   return P.first->second;
4889cb50c9SDimitry Andric }
4989cb50c9SDimitry Andric 
5089cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::calculateSerializedSize() const {
5189cb50c9SDimitry Andric   return StringSize;
5289cb50c9SDimitry Andric }
5389cb50c9SDimitry Andric 
5489cb50c9SDimitry Andric Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const {
5589cb50c9SDimitry Andric   assert(Writer.bytesRemaining() == StringSize);
5689cb50c9SDimitry Andric   uint32_t MaxOffset = 1;
5789cb50c9SDimitry Andric 
5889cb50c9SDimitry Andric   for (auto &Pair : Strings) {
5989cb50c9SDimitry Andric     StringRef S = Pair.getKey();
6089cb50c9SDimitry Andric     uint32_t Offset = Pair.getValue();
6189cb50c9SDimitry Andric     Writer.setOffset(Offset);
6289cb50c9SDimitry Andric     if (auto EC = Writer.writeCString(S))
6389cb50c9SDimitry Andric       return EC;
6489cb50c9SDimitry Andric     MaxOffset = std::max<uint32_t>(MaxOffset, Offset + S.size() + 1);
6589cb50c9SDimitry Andric   }
6689cb50c9SDimitry Andric 
6789cb50c9SDimitry Andric   Writer.setOffset(MaxOffset);
6889cb50c9SDimitry Andric   assert(Writer.bytesRemaining() == 0);
6989cb50c9SDimitry Andric   return Error::success();
7089cb50c9SDimitry Andric }
7189cb50c9SDimitry Andric 
7289cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::size() const { return Strings.size(); }
7389cb50c9SDimitry Andric 
7489cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::getStringId(StringRef S) const {
756d97bb29SDimitry Andric   auto Iter = Strings.find(S);
766d97bb29SDimitry Andric   assert(Iter != Strings.end());
776d97bb29SDimitry Andric   return Iter->second;
7889cb50c9SDimitry Andric }
79