LCOV - code coverage report
Current view: top level - lib/DebugInfo/PDB/Native - NamedStreamMap.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 44 48 91.7 %
Date: 2018-06-17 00:07:59 Functions: 14 15 93.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
       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/PDB/Native/NamedStreamMap.h"
      11             : #include "llvm/ADT/StringMap.h"
      12             : #include "llvm/ADT/StringRef.h"
      13             : #include "llvm/ADT/iterator_range.h"
      14             : #include "llvm/DebugInfo/PDB/Native/Hash.h"
      15             : #include "llvm/DebugInfo/PDB/Native/HashTable.h"
      16             : #include "llvm/DebugInfo/PDB/Native/RawError.h"
      17             : #include "llvm/Support/BinaryStreamReader.h"
      18             : #include "llvm/Support/BinaryStreamRef.h"
      19             : #include "llvm/Support/BinaryStreamWriter.h"
      20             : #include "llvm/Support/Endian.h"
      21             : #include "llvm/Support/Error.h"
      22             : #include <algorithm>
      23             : #include <cassert>
      24             : #include <cstdint>
      25             : #include <tuple>
      26             : 
      27             : using namespace llvm;
      28             : using namespace llvm::pdb;
      29             : 
      30        3061 : NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {}
      31             : 
      32       93282 : uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const {
      33             :   // In the reference implementation, this uses
      34             :   // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
      35             :   // Here, the type HASH is a typedef of unsigned short.
      36             :   // ** It is not a bug that we truncate the result of hashStringV1, in fact
      37             :   //    it is a bug if we do not! **
      38       93282 :   return static_cast<uint16_t>(hashStringV1(S));
      39             : }
      40             : 
      41       90281 : StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const {
      42       90281 :   return NS->getString(Offset);
      43             : }
      44             : 
      45       20459 : uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) {
      46       20459 :   return NS->appendStringData(S);
      47             : }
      48             : 
      49        3061 : NamedStreamMap::NamedStreamMap()
      50        6122 :     : HashTraits(*this), OffsetIndexMap(1, HashTraits) {}
      51             : 
      52          74 : Error NamedStreamMap::load(BinaryStreamReader &Stream) {
      53             :   uint32_t StringBufferSize;
      54         148 :   if (auto EC = Stream.readInteger(StringBufferSize))
      55             :     return joinErrors(std::move(EC),
      56           0 :                       make_error<RawError>(raw_error_code::corrupt_file,
      57           0 :                                            "Expected string buffer size"));
      58             : 
      59          74 :   StringRef Buffer;
      60         148 :   if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
      61             :     return EC;
      62          74 :   NamesBuffer.assign(Buffer.begin(), Buffer.end());
      63             : 
      64          74 :   return OffsetIndexMap.load(Stream);
      65             : }
      66             : 
      67          90 : Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const {
      68             :   // The first field is the number of bytes of string data.
      69         270 :   if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
      70             :     return EC;
      71             : 
      72             :   // Then the actual string data.
      73          90 :   StringRef Data(NamesBuffer.data(), NamesBuffer.size());
      74         180 :   if (auto EC = Writer.writeFixedString(Data))
      75             :     return EC;
      76             : 
      77             :   // And finally the Offset Index map.
      78         180 :   if (auto EC = OffsetIndexMap.commit(Writer))
      79             :     return EC;
      80             : 
      81             :   return Error::success();
      82             : }
      83             : 
      84         180 : uint32_t NamedStreamMap::calculateSerializedLength() const {
      85             :   return sizeof(uint32_t)                              // String data size
      86         180 :          + NamesBuffer.size()                          // String data
      87         180 :          + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
      88             : }
      89             : 
      90        5794 : uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
      91             : 
      92       90281 : StringRef NamedStreamMap::getString(uint32_t Offset) const {
      93             :   assert(NamesBuffer.size() > Offset);
      94      180562 :   return StringRef(NamesBuffer.data() + Offset);
      95             : }
      96             : 
      97           0 : uint32_t NamedStreamMap::hashString(uint32_t Offset) const {
      98           0 :   return hashStringV1(getString(Offset));
      99             : }
     100             : 
     101       20407 : bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
     102       20407 :   auto Iter = OffsetIndexMap.find_as(Stream);
     103       20407 :   if (Iter == OffsetIndexMap.end())
     104             :     return false;
     105       20407 :   StreamNo = (*Iter).second;
     106       20407 :   return true;
     107             : }
     108             : 
     109           9 : StringMap<uint32_t> NamedStreamMap::entries() const {
     110             :   StringMap<uint32_t> Result;
     111          45 :   for (const auto &Entry : OffsetIndexMap) {
     112          27 :     StringRef Stream(NamesBuffer.data() + Entry.first);
     113          27 :     Result.try_emplace(Stream, Entry.second);
     114             :   }
     115           9 :   return Result;
     116             : }
     117             : 
     118       20459 : uint32_t NamedStreamMap::appendStringData(StringRef S) {
     119       40918 :   uint32_t Offset = NamesBuffer.size();
     120       20459 :   NamesBuffer.insert(NamesBuffer.end(), S.begin(), S.end());
     121       40918 :   NamesBuffer.push_back('\0');
     122       20459 :   return Offset;
     123             : }
     124             : 
     125       20459 : void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
     126       40918 :   OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo));
     127       20459 : }

Generated by: LCOV version 1.13