Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/DebugStringTableSubsection.cpp
35271 views
//===- DebugStringTableSubsection.cpp - CodeView String Table -------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"9#include "llvm/ADT/StringRef.h"10#include "llvm/DebugInfo/CodeView/CodeView.h"11#include "llvm/Support/BinaryStreamReader.h"12#include "llvm/Support/BinaryStreamWriter.h"13#include "llvm/Support/Error.h"14#include <algorithm>15#include <cassert>16#include <cstdint>1718using namespace llvm;19using namespace llvm::codeview;2021DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()22: DebugSubsectionRef(DebugSubsectionKind::StringTable) {}2324Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) {25Stream = Contents;26return Error::success();27}2829Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) {30return Reader.readStreamRef(Stream);31}3233Expected<StringRef>34DebugStringTableSubsectionRef::getString(uint32_t Offset) const {35BinaryStreamReader Reader(Stream);36Reader.setOffset(Offset);37StringRef Result;38if (auto EC = Reader.readCString(Result))39return std::move(EC);40return Result;41}4243DebugStringTableSubsection::DebugStringTableSubsection()44: DebugSubsection(DebugSubsectionKind::StringTable) {}4546uint32_t DebugStringTableSubsection::insert(StringRef S) {47auto P = StringToId.insert({S, StringSize});4849// If a given string didn't exist in the string table, we want to increment50// the string table size and insert it into the reverse lookup.51if (P.second) {52IdToString.insert({P.first->getValue(), P.first->getKey()});53StringSize += S.size() + 1; // +1 for '\0'54}5556return P.first->second;57}5859uint32_t DebugStringTableSubsection::calculateSerializedSize() const {60return StringSize;61}6263Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const {64uint32_t Begin = Writer.getOffset();65uint32_t End = Begin + StringSize;6667// Write a null string at the beginning.68if (auto EC = Writer.writeCString(StringRef()))69return EC;7071for (auto &Pair : StringToId) {72StringRef S = Pair.getKey();73uint32_t Offset = Begin + Pair.getValue();74Writer.setOffset(Offset);75if (auto EC = Writer.writeCString(S))76return EC;77assert(Writer.getOffset() <= End);78}7980Writer.setOffset(End);81assert((End - Begin) == StringSize);82return Error::success();83}8485uint32_t DebugStringTableSubsection::size() const { return StringToId.size(); }8687std::vector<uint32_t> DebugStringTableSubsection::sortedIds() const {88std::vector<uint32_t> Result;89Result.reserve(IdToString.size());90for (const auto &Entry : IdToString)91Result.push_back(Entry.first);92llvm::sort(Result);93return Result;94}9596uint32_t DebugStringTableSubsection::getIdForString(StringRef S) const {97auto Iter = StringToId.find(S);98assert(Iter != StringToId.end());99return Iter->second;100}101102StringRef DebugStringTableSubsection::getStringForId(uint32_t Id) const {103auto Iter = IdToString.find(Id);104assert(Iter != IdToString.end());105return Iter->second;106}107108109