LCOV - code coverage report
Current view: top level - lib/DebugInfo/PDB/Native - PDBStringTable.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 47 65 72.3 %
Date: 2017-09-14 15:23:50 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- PDBStringTable.cpp - PDB String Table ---------------------*- C++-*-===//
       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/PDBStringTable.h"
      11             : 
      12             : #include "llvm/ADT/ArrayRef.h"
      13             : #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
      14             : #include "llvm/DebugInfo/PDB/Native/Hash.h"
      15             : #include "llvm/DebugInfo/PDB/Native/RawError.h"
      16             : #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
      17             : #include "llvm/Support/BinaryStreamReader.h"
      18             : #include "llvm/Support/Endian.h"
      19             : 
      20             : using namespace llvm;
      21             : using namespace llvm::support;
      22             : using namespace llvm::pdb;
      23             : 
      24           4 : uint32_t PDBStringTable::getByteSize() const { return Header->ByteSize; }
      25           3 : uint32_t PDBStringTable::getNameCount() const { return NameCount; }
      26           6 : uint32_t PDBStringTable::getHashVersion() const { return Header->HashVersion; }
      27           4 : uint32_t PDBStringTable::getSignature() const { return Header->Signature; }
      28             : 
      29         103 : Error PDBStringTable::readHeader(BinaryStreamReader &Reader) {
      30         309 :   if (auto EC = Reader.readObject(Header))
      31           0 :     return EC;
      32             : 
      33         206 :   if (Header->Signature != PDBStringTableSignature)
      34             :     return make_error<RawError>(raw_error_code::corrupt_file,
      35           0 :                                 "Invalid hash table signature");
      36         206 :   if (Header->HashVersion != 1 && Header->HashVersion != 2)
      37             :     return make_error<RawError>(raw_error_code::corrupt_file,
      38           0 :                                 "Unsupported hash version");
      39             : 
      40             :   assert(Reader.bytesRemaining() == 0);
      41         309 :   return Error::success();
      42             : }
      43             : 
      44         103 : Error PDBStringTable::readStrings(BinaryStreamReader &Reader) {
      45         206 :   BinaryStreamRef Stream;
      46         309 :   if (auto EC = Reader.readStreamRef(Stream))
      47           0 :     return EC;
      48             : 
      49         515 :   if (auto EC = Strings.initialize(Stream)) {
      50           0 :     return joinErrors(std::move(EC),
      51           0 :                       make_error<RawError>(raw_error_code::corrupt_file,
      52           0 :                                            "Invalid hash table byte length"));
      53             :   }
      54             : 
      55             :   assert(Reader.bytesRemaining() == 0);
      56         309 :   return Error::success();
      57             : }
      58             : 
      59             : const codeview::DebugStringTableSubsectionRef &
      60          36 : PDBStringTable::getStringTable() const {
      61          36 :   return Strings;
      62             : }
      63             : 
      64         103 : Error PDBStringTable::readHashTable(BinaryStreamReader &Reader) {
      65             :   const support::ulittle32_t *HashCount;
      66         309 :   if (auto EC = Reader.readObject(HashCount))
      67           0 :     return EC;
      68             : 
      69         412 :   if (auto EC = Reader.readArray(IDs, *HashCount)) {
      70           0 :     return joinErrors(std::move(EC),
      71           0 :                       make_error<RawError>(raw_error_code::corrupt_file,
      72           0 :                                            "Could not read bucket array"));
      73             :   }
      74             : 
      75         309 :   return Error::success();
      76             : }
      77             : 
      78         103 : Error PDBStringTable::readEpilogue(BinaryStreamReader &Reader) {
      79         309 :   if (auto EC = Reader.readInteger(NameCount))
      80           0 :     return EC;
      81             : 
      82             :   assert(Reader.bytesRemaining() == 0);
      83         309 :   return Error::success();
      84             : }
      85             : 
      86         103 : Error PDBStringTable::reload(BinaryStreamReader &Reader) {
      87             : 
      88         206 :   BinaryStreamReader SectionReader;
      89             : 
      90         206 :   std::tie(SectionReader, Reader) = Reader.split(sizeof(PDBStringTableHeader));
      91         309 :   if (auto EC = readHeader(SectionReader))
      92           0 :     return EC;
      93             : 
      94         309 :   std::tie(SectionReader, Reader) = Reader.split(Header->ByteSize);
      95         309 :   if (auto EC = readStrings(SectionReader))
      96           0 :     return EC;
      97             : 
      98             :   // We don't know how long the hash table is until we parse it, so let the
      99             :   // function responsible for doing that figure it out.
     100         309 :   if (auto EC = readHashTable(Reader))
     101           0 :     return EC;
     102             : 
     103         206 :   std::tie(SectionReader, Reader) = Reader.split(sizeof(uint32_t));
     104         309 :   if (auto EC = readEpilogue(SectionReader))
     105           0 :     return EC;
     106             : 
     107             :   assert(Reader.bytesRemaining() == 0);
     108         309 :   return Error::success();
     109             : }
     110             : 
     111         288 : Expected<StringRef> PDBStringTable::getStringForID(uint32_t ID) const {
     112         288 :   return Strings.getString(ID);
     113             : }
     114             : 
     115           7 : Expected<uint32_t> PDBStringTable::getIDForString(StringRef Str) const {
     116             :   uint32_t Hash =
     117          14 :       (Header->HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str);
     118          14 :   size_t Count = IDs.size();
     119           7 :   uint32_t Start = Hash % Count;
     120          10 :   for (size_t I = 0; I < Count; ++I) {
     121             :     // The hash is just a starting point for the search, but if it
     122             :     // doesn't work we should find the string no matter what, because
     123             :     // we iterate the entire array.
     124          10 :     uint32_t Index = (Start + I) % Count;
     125             : 
     126          20 :     uint32_t ID = IDs[Index];
     127          13 :     auto ExpectedStr = getStringForID(ID);
     128          10 :     if (!ExpectedStr)
     129           0 :       return ExpectedStr.takeError();
     130             : 
     131          10 :     if (*ExpectedStr == Str)
     132             :       return ID;
     133             :   }
     134           0 :   return make_error<RawError>(raw_error_code::no_entry);
     135             : }
     136             : 
     137          23 : FixedStreamArray<support::ulittle32_t> PDBStringTable::name_ids() const {
     138          46 :   return IDs;
     139             : }

Generated by: LCOV version 1.13