LCOV - code coverage report
Current view: top level - lib/DebugInfo/PDB/Native - DbiModuleList.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 88 110 80.0 %
Date: 2017-09-14 15:23:50 Functions: 17 19 89.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DbiModuleList.cpp - PDB module information list --------------------===//
       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/DbiModuleList.h"
      11             : #include "llvm/ADT/StringRef.h"
      12             : #include "llvm/ADT/iterator_range.h"
      13             : #include "llvm/DebugInfo/PDB/Native/RawError.h"
      14             : #include "llvm/Support/BinaryStreamReader.h"
      15             : #include "llvm/Support/Error.h"
      16             : #include <algorithm>
      17             : #include <cassert>
      18             : #include <cstddef>
      19             : #include <cstdint>
      20             : 
      21             : using namespace llvm;
      22             : using namespace llvm::pdb;
      23             : 
      24          61 : DbiModuleSourceFilesIterator::DbiModuleSourceFilesIterator(
      25          61 :     const DbiModuleList &Modules, uint32_t Modi, uint16_t Filei)
      26         122 :     : Modules(&Modules), Modi(Modi), Filei(Filei) {
      27          61 :   setValue();
      28          61 : }
      29             : 
      30         378 : bool DbiModuleSourceFilesIterator::
      31             : operator==(const DbiModuleSourceFilesIterator &R) const {
      32             :   // incompatible iterators are never equal
      33         378 :   if (!isCompatible(R))
      34             :     return false;
      35             : 
      36             :   // If they're compatible, and they're both ends, then they're equal.
      37         378 :   if (isEnd() && R.isEnd())
      38             :     return true;
      39             : 
      40             :   // If one is an end and the other is not, they're not equal.
      41         322 :   if (isEnd() != R.isEnd())
      42             :     return false;
      43             : 
      44             :   // Now we know:
      45             :   // - They're compatible
      46             :   // - They're not *both* end iterators
      47             :   // - Their endness is the same.
      48             :   // Thus, they're compatible iterators pointing to a valid file on the same
      49             :   // module.  All we need to check are the file indices.
      50             :   assert(Modules == R.Modules);
      51             :   assert(Modi == R.Modi);
      52             :   assert(!isEnd());
      53             :   assert(!R.isEnd());
      54             : 
      55           0 :   return (Filei == R.Filei);
      56             : }
      57             : 
      58           0 : bool DbiModuleSourceFilesIterator::
      59             : operator<(const DbiModuleSourceFilesIterator &R) const {
      60             :   assert(isCompatible(R));
      61             : 
      62             :   // It's not sufficient to compare the file indices, because default
      63             :   // constructed iterators could be equal to iterators with valid indices.  To
      64             :   // account for this, early-out if they're equal.
      65           0 :   if (*this == R)
      66             :     return false;
      67             : 
      68           0 :   return Filei < R.Filei;
      69             : }
      70             : 
      71          16 : std::ptrdiff_t DbiModuleSourceFilesIterator::
      72             : operator-(const DbiModuleSourceFilesIterator &R) const {
      73             :   assert(isCompatible(R));
      74             :   assert(!(*this < R));
      75             : 
      76             :   // If they're both end iterators, the distance is 0.
      77          16 :   if (isEnd() && R.isEnd())
      78             :     return 0;
      79             : 
      80             :   assert(!R.isEnd());
      81             : 
      82             :   // At this point, R cannot be end, but *this can, which means that *this
      83             :   // might be a universal end iterator with none of its fields set.  So in that
      84             :   // case have to rely on R as the authority to figure out how many files there
      85             :   // are to compute the distance.
      86           6 :   uint32_t Thisi = Filei;
      87           6 :   if (isEnd()) {
      88           6 :     uint32_t RealModi = R.Modi;
      89           6 :     Thisi = R.Modules->getSourceFileCount(RealModi);
      90             :   }
      91             : 
      92             :   assert(Thisi >= R.Filei);
      93           6 :   return Thisi - R.Filei;
      94             : }
      95             : 
      96         322 : DbiModuleSourceFilesIterator &DbiModuleSourceFilesIterator::
      97             : operator+=(std::ptrdiff_t N) {
      98             :   assert(!isEnd());
      99             : 
     100         322 :   Filei += N;
     101             :   assert(Filei <= Modules->getSourceFileCount(Modi));
     102         322 :   setValue();
     103         322 :   return *this;
     104             : }
     105             : 
     106           0 : DbiModuleSourceFilesIterator &DbiModuleSourceFilesIterator::
     107             : operator-=(std::ptrdiff_t N) {
     108             :   // Note that we can subtract from an end iterator, but not a universal end
     109             :   // iterator.
     110             :   assert(!isUniversalEnd());
     111             : 
     112             :   assert(N <= Filei);
     113             : 
     114           0 :   Filei -= N;
     115           0 :   return *this;
     116             : }
     117             : 
     118         383 : void DbiModuleSourceFilesIterator::setValue() {
     119         383 :   if (isEnd()) {
     120          61 :     ThisValue = "";
     121          61 :     return;
     122             :   }
     123             : 
     124         644 :   uint32_t Off = Modules->ModuleInitialFileIndex[Modi] + Filei;
     125         644 :   auto ExpectedValue = Modules->getFileName(Off);
     126         322 :   if (!ExpectedValue) {
     127           0 :     consumeError(ExpectedValue.takeError());
     128           0 :     Filei = Modules->getSourceFileCount(Modi);
     129             :   } else
     130         322 :     ThisValue = *ExpectedValue;
     131             : }
     132             : 
     133        1499 : bool DbiModuleSourceFilesIterator::isEnd() const {
     134        1499 :   if (isUniversalEnd())
     135             :     return true;
     136             : 
     137             :   assert(Modules);
     138             :   assert(Modi <= Modules->getModuleCount());
     139             :   assert(Filei <= Modules->getSourceFileCount(Modi));
     140             : 
     141        1099 :   if (Modi == Modules->getModuleCount())
     142             :     return true;
     143        1099 :   if (Filei == Modules->getSourceFileCount(Modi))
     144             :     return true;
     145         972 :   return false;
     146             : }
     147             : 
     148        2255 : bool DbiModuleSourceFilesIterator::isUniversalEnd() const { return !Modules; }
     149             : 
     150         378 : bool DbiModuleSourceFilesIterator::isCompatible(
     151             :     const DbiModuleSourceFilesIterator &R) const {
     152             :   // Universal iterators are compatible with any other iterator.
     153         378 :   if (isUniversalEnd() || R.isUniversalEnd())
     154             :     return true;
     155             : 
     156             :   // At this point, neither iterator is a universal end iterator, although one
     157             :   // or both might be non-universal end iterators.  Regardless, the module index
     158             :   // is valid, so they are compatible if and only if they refer to the same
     159             :   // module.
     160           0 :   return Modi == R.Modi;
     161             : }
     162             : 
     163          65 : Error DbiModuleList::initialize(BinaryStreamRef ModInfo,
     164             :                                 BinaryStreamRef FileInfo) {
     165         260 :   if (auto EC = initializeModInfo(ModInfo))
     166           0 :     return EC;
     167         260 :   if (auto EC = initializeFileInfo(FileInfo))
     168           0 :     return EC;
     169             : 
     170         195 :   return Error::success();
     171             : }
     172             : 
     173          65 : Error DbiModuleList::initializeModInfo(BinaryStreamRef ModInfo) {
     174         130 :   ModInfoSubstream = ModInfo;
     175             : 
     176          65 :   if (ModInfo.getLength() == 0)
     177           0 :     return Error::success();
     178             : 
     179         130 :   BinaryStreamReader Reader(ModInfo);
     180             : 
     181         195 :   if (auto EC = Reader.readArray(Descriptors, ModInfo.getLength()))
     182           0 :     return EC;
     183             : 
     184         195 :   return Error::success();
     185             : }
     186             : 
     187          65 : Error DbiModuleList::initializeFileInfo(BinaryStreamRef FileInfo) {
     188         130 :   FileInfoSubstream = FileInfo;
     189             : 
     190          65 :   if (FileInfo.getLength() == 0)
     191           0 :     return Error::success();
     192             : 
     193         130 :   BinaryStreamReader FISR(FileInfo);
     194         195 :   if (auto EC = FISR.readObject(FileInfoHeader))
     195           0 :     return EC;
     196             : 
     197             :   // First is an array of `NumModules` module indices.  This does not seem to be
     198             :   // used for anything meaningful, so we ignore it.
     199          65 :   FixedStreamArray<support::ulittle16_t> ModuleIndices;
     200         260 :   if (auto EC = FISR.readArray(ModuleIndices, FileInfoHeader->NumModules))
     201           0 :     return EC;
     202         260 :   if (auto EC = FISR.readArray(ModFileCountArray, FileInfoHeader->NumModules))
     203           0 :     return EC;
     204             : 
     205             :   // Compute the real number of source files.  We can't trust the value in
     206             :   // `FileInfoHeader->NumSourceFiles` because it is a unit16, and the sum of all
     207             :   // source file counts might be larger than a unit16.  So we compute the real
     208             :   // count by summing up the individual counts.
     209          65 :   uint32_t NumSourceFiles = 0;
     210         694 :   for (auto Count : ModFileCountArray)
     211         369 :     NumSourceFiles += Count;
     212             : 
     213             :   // In the reference implementation, this array is where the pointer documented
     214             :   // at the definition of ModuleInfoHeader::FileNameOffs points to.  Note that
     215             :   // although the field in ModuleInfoHeader is ignored this array is not, as it
     216             :   // is the authority on where each filename begins in the names buffer.
     217         195 :   if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
     218           0 :     return EC;
     219             : 
     220         195 :   if (auto EC = FISR.readStreamRef(NamesBuffer))
     221           0 :     return EC;
     222             : 
     223         195 :   auto DescriptorIter = Descriptors.begin();
     224          65 :   uint32_t NextFileIndex = 0;
     225         130 :   ModuleInitialFileIndex.resize(FileInfoHeader->NumModules);
     226         130 :   ModuleDescriptorOffsets.resize(FileInfoHeader->NumModules);
     227         868 :   for (size_t I = 0; I < FileInfoHeader->NumModules; ++I) {
     228             :     assert(DescriptorIter != Descriptors.end());
     229         738 :     ModuleInitialFileIndex[I] = NextFileIndex;
     230         738 :     ModuleDescriptorOffsets[I] = DescriptorIter.offset();
     231             : 
     232         738 :     NextFileIndex += ModFileCountArray[I];
     233         369 :     ++DescriptorIter;
     234             :   }
     235             : 
     236             :   assert(DescriptorIter == Descriptors.end());
     237             :   assert(NextFileIndex == NumSourceFiles);
     238             : 
     239         195 :   return Error::success();
     240             : }
     241             : 
     242        1823 : uint32_t DbiModuleList::getModuleCount() const {
     243        3646 :   return FileInfoHeader->NumModules;
     244             : }
     245             : 
     246         324 : uint32_t DbiModuleList::getSourceFileCount() const {
     247         648 :   return FileNameOffsets.size();
     248             : }
     249             : 
     250        1105 : uint16_t DbiModuleList::getSourceFileCount(uint32_t Modi) const {
     251        2210 :   return ModFileCountArray[Modi];
     252             : }
     253             : 
     254         499 : DbiModuleDescriptor DbiModuleList::getModuleDescriptor(uint32_t Modi) const {
     255             :   assert(Modi < getModuleCount());
     256         998 :   uint32_t Offset = ModuleDescriptorOffsets[Modi];
     257        1497 :   auto Iter = Descriptors.at(Offset);
     258             :   assert(Iter != Descriptors.end());
     259         998 :   return *Iter;
     260             : }
     261             : 
     262             : iterator_range<DbiModuleSourceFilesIterator>
     263          61 : DbiModuleList::source_files(uint32_t Modi) const {
     264             :   return make_range<DbiModuleSourceFilesIterator>(
     265             :       DbiModuleSourceFilesIterator(*this, Modi, 0),
     266         183 :       DbiModuleSourceFilesIterator());
     267             : }
     268             : 
     269         322 : Expected<StringRef> DbiModuleList::getFileName(uint32_t Index) const {
     270        1288 :   BinaryStreamReader Names(NamesBuffer);
     271         322 :   if (Index >= getSourceFileCount())
     272           0 :     return make_error<RawError>(raw_error_code::index_out_of_bounds);
     273             : 
     274         644 :   uint32_t FileOffset = FileNameOffsets[Index];
     275         644 :   Names.setOffset(FileOffset);
     276         322 :   StringRef Name;
     277         966 :   if (auto EC = Names.readCString(Name))
     278           0 :     return std::move(EC);
     279             :   return Name;
     280             : }

Generated by: LCOV version 1.13