LCOV - code coverage report
Current view: top level - lib/Support - BinaryStreamWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 40 46 87.0 %
Date: 2017-09-14 15:23:50 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===//
       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/BinaryStreamWriter.h"
      11             : 
      12             : #include "llvm/Support/BinaryStreamError.h"
      13             : #include "llvm/Support/BinaryStreamReader.h"
      14             : #include "llvm/Support/BinaryStreamRef.h"
      15             : 
      16             : using namespace llvm;
      17             : 
      18        1286 : BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef Ref)
      19        2572 :     : Stream(Ref) {}
      20             : 
      21        6432 : BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStream &Stream)
      22        6432 :     : Stream(Stream) {}
      23             : 
      24          55 : BinaryStreamWriter::BinaryStreamWriter(MutableArrayRef<uint8_t> Data,
      25          55 :                                        llvm::support::endianness Endian)
      26          55 :     : Stream(Data, Endian) {}
      27             : 
      28       57705 : Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
      29      173103 :   if (auto EC = Stream.writeBytes(Offset, Buffer))
      30          24 :     return EC;
      31       57693 :   Offset += Buffer.size();
      32      173079 :   return Error::success();
      33             : }
      34             : 
      35        8512 : Error BinaryStreamWriter::writeCString(StringRef Str) {
      36       25536 :   if (auto EC = writeFixedString(Str))
      37           0 :     return EC;
      38       34048 :   if (auto EC = writeObject('\0'))
      39           0 :     return EC;
      40             : 
      41       25536 :   return Error::success();
      42             : }
      43             : 
      44        8537 : Error BinaryStreamWriter::writeFixedString(StringRef Str) {
      45       25611 :   return writeBytes(ArrayRef<uint8_t>(Str.bytes_begin(), Str.bytes_end()));
      46             : }
      47             : 
      48         318 : Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) {
      49         954 :   return writeStreamRef(Ref, Ref.getLength());
      50             : }
      51             : 
      52         318 : Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) {
      53         954 :   BinaryStreamReader SrcReader(Ref.slice(0, Length));
      54             :   // This is a bit tricky.  If we just call readBytes, we are requiring that it
      55             :   // return us the entire stream as a contiguous buffer.  There is no guarantee
      56             :   // this can be satisfied by returning a reference straight from the buffer, as
      57             :   // an implementation may not store all data in a single contiguous buffer.  So
      58             :   // we iterate over each contiguous chunk, writing each one in succession.
      59        1091 :   while (SrcReader.bytesRemaining() > 0) {
      60         773 :     ArrayRef<uint8_t> Chunk;
      61        2319 :     if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
      62           0 :       return EC;
      63        2319 :     if (auto EC = writeBytes(Chunk))
      64           0 :       return EC;
      65             :   }
      66         954 :   return Error::success();
      67             : }
      68             : 
      69             : std::pair<BinaryStreamWriter, BinaryStreamWriter>
      70         472 : BinaryStreamWriter::split(uint32_t Off) const {
      71             :   assert(getLength() >= Off);
      72             : 
      73         944 :   WritableBinaryStreamRef First = Stream.drop_front(Offset);
      74             : 
      75         944 :   WritableBinaryStreamRef Second = First.drop_front(Off);
      76        1416 :   First = First.keep_front(Off);
      77        1416 :   BinaryStreamWriter W1{First};
      78        1416 :   BinaryStreamWriter W2{Second};
      79         944 :   return std::make_pair(W1, W2);
      80             : }
      81             : 
      82        1157 : Error BinaryStreamWriter::padToAlignment(uint32_t Align) {
      83        2314 :   uint32_t NewOffset = alignTo(Offset, Align);
      84        1157 :   if (NewOffset > getLength())
      85           0 :     return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
      86        2049 :   while (Offset < NewOffset)
      87        2676 :     if (auto EC = writeInteger('\0'))
      88           0 :       return EC;
      89        3471 :   return Error::success();
      90             : }

Generated by: LCOV version 1.13