LCOV - code coverage report
Current view: top level - lib/Support - BinaryStreamRef.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 50 59 84.7 %
Date: 2017-09-14 15:23:50 Functions: 16 26 61.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- BinaryStreamRef.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/Support/BinaryStreamRef.h"
      11             : #include "llvm/Support/BinaryByteStream.h"
      12             : 
      13             : using namespace llvm;
      14             : using namespace llvm::support;
      15             : 
      16             : namespace {
      17             : 
      18        2804 : class ArrayRefImpl : public BinaryStream {
      19             : public:
      20        2806 :   ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
      21             : 
      22         987 :   llvm::support::endianness getEndian() const override {
      23        1974 :     return BBS.getEndian();
      24             :   }
      25       10963 :   Error readBytes(uint32_t Offset, uint32_t Size,
      26             :                   ArrayRef<uint8_t> &Buffer) override {
      27       10963 :     return BBS.readBytes(Offset, Size, Buffer);
      28             :   }
      29         347 :   Error readLongestContiguousChunk(uint32_t Offset,
      30             :                                    ArrayRef<uint8_t> &Buffer) override {
      31         347 :     return BBS.readLongestContiguousChunk(Offset, Buffer);
      32             :   }
      33           0 :   uint32_t getLength() override { return BBS.getLength(); }
      34             : 
      35             : private:
      36             :   BinaryByteStream BBS;
      37             : };
      38             : 
      39         165 : class MutableArrayRefImpl : public WritableBinaryStream {
      40             : public:
      41             :   MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
      42         165 :       : BBS(Data, Endian) {}
      43             : 
      44             :   // Inherited via WritableBinaryStream
      45         250 :   llvm::support::endianness getEndian() const override {
      46         500 :     return BBS.getEndian();
      47             :   }
      48           0 :   Error readBytes(uint32_t Offset, uint32_t Size,
      49             :                   ArrayRef<uint8_t> &Buffer) override {
      50           0 :     return BBS.readBytes(Offset, Size, Buffer);
      51             :   }
      52           0 :   Error readLongestContiguousChunk(uint32_t Offset,
      53             :                                    ArrayRef<uint8_t> &Buffer) override {
      54           0 :     return BBS.readLongestContiguousChunk(Offset, Buffer);
      55             :   }
      56           0 :   uint32_t getLength() override { return BBS.getLength(); }
      57             : 
      58        1098 :   Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
      59        1098 :     return BBS.writeBytes(Offset, Data);
      60             :   }
      61           0 :   Error commit() override { return BBS.commit(); }
      62             : 
      63             : private:
      64             :   MutableBinaryByteStream BBS;
      65             : };
      66             : }
      67             : 
      68       10777 : BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream)
      69       10777 :     : BinaryStreamRef(Stream, 0, Stream.getLength()) {}
      70       11747 : BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream, uint32_t Offset,
      71             :                                  uint32_t Length)
      72       23494 :     : BinaryStreamRefBase(Stream, Offset, Length) {}
      73        1403 : BinaryStreamRef::BinaryStreamRef(ArrayRef<uint8_t> Data, endianness Endian)
      74        2806 :     : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
      75        7015 :                           Data.size()) {}
      76         908 : BinaryStreamRef::BinaryStreamRef(StringRef Data, endianness Endian)
      77             :     : BinaryStreamRef(makeArrayRef(Data.bytes_begin(), Data.bytes_end()),
      78        2724 :                       Endian) {}
      79             : 
      80      135953 : Error BinaryStreamRef::readBytes(uint32_t Offset, uint32_t Size,
      81             :                                  ArrayRef<uint8_t> &Buffer) const {
      82      407828 :   if (auto EC = checkOffset(Offset, Size))
      83          62 :     return EC;
      84      135922 :   return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
      85             : }
      86             : 
      87       17269 : Error BinaryStreamRef::readLongestContiguousChunk(
      88             :     uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
      89       51647 :   if (auto EC = checkOffset(Offset, 1))
      90         320 :     return EC;
      91             : 
      92       17109 :   if (auto EC =
      93       51327 :           BorrowedImpl->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
      94           0 :     return EC;
      95             :   // This StreamRef might refer to a smaller window over a larger stream.  In
      96             :   // that case we will have read out more bytes than we should return, because
      97             :   // we should not read past the end of the current view.
      98       17109 :   uint32_t MaxLength = Length - Offset;
      99       17109 :   if (Buffer.size() > MaxLength)
     100        7826 :     Buffer = Buffer.slice(0, MaxLength);
     101       51327 :   return Error::success();
     102             : }
     103             : 
     104        7301 : WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream)
     105        7301 :     : WritableBinaryStreamRef(Stream, 0, Stream.getLength()) {}
     106             : 
     107        7301 : WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream,
     108             :                                                  uint32_t Offset,
     109             :                                                  uint32_t Length)
     110       14602 :     : BinaryStreamRefBase(Stream, Offset, Length) {}
     111             : 
     112          55 : WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
     113          55 :                                                  endianness Endian)
     114         110 :     : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
     115         275 :                           0, Data.size()) {}
     116             : 
     117             : 
     118       64633 : Error WritableBinaryStreamRef::writeBytes(uint32_t Offset,
     119             :                                           ArrayRef<uint8_t> Data) const {
     120      193887 :   if (auto EC = checkOffset(Offset, Data.size()))
     121          24 :     return EC;
     122             : 
     123       64621 :   return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
     124             : }
     125             : 
     126         970 : WritableBinaryStreamRef::operator BinaryStreamRef() const {
     127         970 :   return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
     128             : }
     129             : 
     130             : /// \brief For buffered streams, commits changes to the backing store.
     131           0 : Error WritableBinaryStreamRef::commit() { return BorrowedImpl->commit(); }

Generated by: LCOV version 1.13