LCOV - code coverage report
Current view: top level - lib/DebugInfo/CodeView - DebugCrossImpSubsection.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 34 46 73.9 %
Date: 2017-09-14 15:23:50 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DebugCrossImpSubsection.cpp ----------------------------------------===//
       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/DebugCrossImpSubsection.h"
      11             : #include "llvm/ADT/ArrayRef.h"
      12             : #include "llvm/DebugInfo/CodeView/CodeViewError.h"
      13             : #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
      14             : #include "llvm/Support/BinaryStreamReader.h"
      15             : #include "llvm/Support/BinaryStreamWriter.h"
      16             : #include "llvm/Support/Endian.h"
      17             : #include "llvm/Support/Error.h"
      18             : #include <algorithm>
      19             : #include <cstdint>
      20             : #include <utility>
      21             : #include <vector>
      22             : 
      23             : using namespace llvm;
      24             : using namespace llvm::codeview;
      25             : 
      26           2 : Error VarStreamArrayExtractor<CrossModuleImportItem>::
      27             : operator()(BinaryStreamRef Stream, uint32_t &Len,
      28             :            codeview::CrossModuleImportItem &Item) {
      29           6 :   BinaryStreamReader Reader(Stream);
      30           2 :   if (Reader.bytesRemaining() < sizeof(CrossModuleImport))
      31             :     return make_error<CodeViewError>(
      32             :         cv_error_code::insufficient_buffer,
      33           0 :         "Not enough bytes for a Cross Module Import Header!");
      34           6 :   if (auto EC = Reader.readObject(Item.Header))
      35           0 :     return EC;
      36           4 :   if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))
      37             :     return make_error<CodeViewError>(
      38             :         cv_error_code::insufficient_buffer,
      39           0 :         "Not enough to read specified number of Cross Module References!");
      40           8 :   if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))
      41           0 :     return EC;
      42           6 :   return Error::success();
      43             : }
      44             : 
      45           1 : Error DebugCrossModuleImportsSubsectionRef::initialize(
      46             :     BinaryStreamReader Reader) {
      47           1 :   return Reader.readArray(References, Reader.bytesRemaining());
      48             : }
      49             : 
      50           0 : Error DebugCrossModuleImportsSubsectionRef::initialize(BinaryStreamRef Stream) {
      51           0 :   BinaryStreamReader Reader(Stream);
      52           0 :   return initialize(Reader);
      53             : }
      54             : 
      55           2 : void DebugCrossModuleImportsSubsection::addImport(StringRef Module,
      56             :                                                   uint32_t ImportId) {
      57           2 :   Strings.insert(Module);
      58          10 :   std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};
      59           8 :   auto Result = Mappings.insert(std::make_pair(Module, Targets));
      60           2 :   if (!Result.second)
      61           2 :     Result.first->getValue().push_back(Targets[0]);
      62           2 : }
      63             : 
      64           3 : uint32_t DebugCrossModuleImportsSubsection::calculateSerializedSize() const {
      65           3 :   uint32_t Size = 0;
      66          18 :   for (const auto &Item : Mappings) {
      67           3 :     Size += sizeof(CrossModuleImport);
      68           6 :     Size += sizeof(support::ulittle32_t) * Item.second.size();
      69             :   }
      70           3 :   return Size;
      71             : }
      72             : 
      73           1 : Error DebugCrossModuleImportsSubsection::commit(
      74             :     BinaryStreamWriter &Writer) const {
      75             :   using T = decltype(&*Mappings.begin());
      76           2 :   std::vector<T> Ids;
      77           1 :   Ids.reserve(Mappings.size());
      78             : 
      79           6 :   for (const auto &M : Mappings)
      80           2 :     Ids.push_back(&M);
      81             : 
      82           4 :   std::sort(Ids.begin(), Ids.end(), [this](const T &L1, const T &L2) {
      83           0 :     return Strings.getStringId(L1->getKey()) <
      84           0 :            Strings.getStringId(L2->getKey());
      85           0 :   });
      86             : 
      87           5 :   for (const auto &Item : Ids) {
      88             :     CrossModuleImport Imp;
      89           3 :     Imp.ModuleNameOffset = Strings.getStringId(Item->getKey());
      90           3 :     Imp.Count = Item->getValue().size();
      91           3 :     if (auto EC = Writer.writeObject(Imp))
      92           0 :       return EC;
      93           4 :     if (auto EC = Writer.writeArray(makeArrayRef(Item->getValue())))
      94           0 :       return EC;
      95             :   }
      96           3 :   return Error::success();
      97             : }

Generated by: LCOV version 1.13