LCOV - code coverage report
Current view: top level - lib/DebugInfo/CodeView - DebugStringTableSubsection.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 32 40 80.0 %
Date: 2018-06-17 00:07:59 Functions: 10 12 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DebugStringTableSubsection.cpp - CodeView String Table -------------===//
       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/DebugStringTableSubsection.h"
      11             : #include "llvm/ADT/StringRef.h"
      12             : #include "llvm/DebugInfo/CodeView/CodeView.h"
      13             : #include "llvm/Support/BinaryStreamReader.h"
      14             : #include "llvm/Support/BinaryStreamWriter.h"
      15             : #include "llvm/Support/Error.h"
      16             : #include <algorithm>
      17             : #include <cassert>
      18             : #include <cstdint>
      19             : 
      20             : using namespace llvm;
      21             : using namespace llvm::codeview;
      22             : 
      23         710 : DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()
      24        1420 :     : DebugSubsectionRef(DebugSubsectionKind::StringTable) {}
      25             : 
      26         265 : Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) {
      27             :   Stream = Contents;
      28         265 :   return Error::success();
      29             : }
      30             : 
      31           5 : Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) {
      32           5 :   return Reader.readStreamRef(Stream);
      33             : }
      34             : 
      35             : Expected<StringRef>
      36        2133 : DebugStringTableSubsectionRef::getString(uint32_t Offset) const {
      37        4266 :   BinaryStreamReader Reader(Stream);
      38             :   Reader.setOffset(Offset);
      39        2133 :   StringRef Result;
      40        4266 :   if (auto EC = Reader.readCString(Result))
      41             :     return std::move(EC);
      42             :   return Result;
      43             : }
      44             : 
      45         334 : DebugStringTableSubsection::DebugStringTableSubsection()
      46         668 :     : DebugSubsection(DebugSubsectionKind::StringTable) {}
      47             : 
      48         354 : uint32_t DebugStringTableSubsection::insert(StringRef S) {
      49         354 :   auto P = StringToId.insert({S, StringSize});
      50             : 
      51             :   // If a given string didn't exist in the string table, we want to increment
      52             :   // the string table size and insert it into the reverse lookup.
      53         354 :   if (P.second) {
      54         552 :     IdToString.insert({P.first->getValue(), P.first->getKey()});
      55         276 :     StringSize += S.size() + 1; // +1 for '\0'
      56             :   }
      57             : 
      58         354 :   return P.first->second;
      59             : }
      60             : 
      61         692 : uint32_t DebugStringTableSubsection::calculateSerializedSize() const {
      62         692 :   return StringSize;
      63             : }
      64             : 
      65         213 : Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const {
      66         213 :   uint32_t Begin = Writer.getOffset();
      67         213 :   uint32_t End = Begin + StringSize;
      68             : 
      69             :   // Write a null string at the beginning.
      70         426 :   if (auto EC = Writer.writeCString(StringRef()))
      71             :     return EC;
      72             : 
      73         667 :   for (auto &Pair : StringToId) {
      74         241 :     StringRef S = Pair.getKey();
      75         241 :     uint32_t Offset = Begin + Pair.getValue();
      76             :     Writer.setOffset(Offset);
      77         482 :     if (auto EC = Writer.writeCString(S))
      78             :       return EC;
      79             :     assert(Writer.getOffset() <= End);
      80             :   }
      81             : 
      82             :   Writer.setOffset(End);
      83             :   assert((End - Begin) == StringSize);
      84             :   return Error::success();
      85             : }
      86             : 
      87         799 : uint32_t DebugStringTableSubsection::size() const { return StringToId.size(); }
      88             : 
      89           0 : std::vector<uint32_t> DebugStringTableSubsection::sortedIds() const {
      90             :   std::vector<uint32_t> Result;
      91           0 :   Result.reserve(IdToString.size());
      92           0 :   for (const auto &Entry : IdToString)
      93           0 :     Result.push_back(Entry.first);
      94             :   llvm::sort(Result.begin(), Result.end());
      95           0 :   return Result;
      96             : }
      97             : 
      98          43 : uint32_t DebugStringTableSubsection::getIdForString(StringRef S) const {
      99          43 :   auto Iter = StringToId.find(S);
     100             :   assert(Iter != StringToId.end());
     101          43 :   return Iter->second;
     102             : }
     103             : 
     104           0 : StringRef DebugStringTableSubsection::getStringForId(uint32_t Id) const {
     105           0 :   auto Iter = IdToString.find(Id);
     106             :   assert(Iter != IdToString.end());
     107           0 :   return Iter->second;
     108             : }

Generated by: LCOV version 1.13