Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/BinaryStreamWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/Support/BinaryStreamWriter.h"
10
11
#include "llvm/Support/BinaryStreamError.h"
12
#include "llvm/Support/BinaryStreamReader.h"
13
#include "llvm/Support/BinaryStreamRef.h"
14
#include "llvm/Support/LEB128.h"
15
16
using namespace llvm;
17
18
BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef Ref)
19
2.92k
    : Stream(Ref) {}
20
21
BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStream &Stream)
22
3.72k
    : Stream(Stream) {}
23
24
BinaryStreamWriter::BinaryStreamWriter(MutableArrayRef<uint8_t> Data,
25
                                       llvm::support::endianness Endian)
26
4.80k
    : Stream(Data, Endian) {}
27
28
138k
Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
29
138k
  if (auto EC = Stream.writeBytes(Offset, Buffer))
30
12
    return EC;
31
138k
  Offset += Buffer.size();
32
138k
  return Error::success();
33
138k
}
34
35
42
Error BinaryStreamWriter::writeULEB128(uint64_t Value) {
36
42
  uint8_t EncodedBytes[10] = {0};
37
42
  unsigned Size = encodeULEB128(Value, &EncodedBytes[0]);
38
42
  return writeBytes({EncodedBytes, Size});
39
42
}
40
41
60
Error BinaryStreamWriter::writeSLEB128(int64_t Value) {
42
60
  uint8_t EncodedBytes[10] = {0};
43
60
  unsigned Size = encodeSLEB128(Value, &EncodedBytes[0]);
44
60
  return writeBytes({EncodedBytes, Size});
45
60
}
46
47
18.9k
Error BinaryStreamWriter::writeCString(StringRef Str) {
48
18.9k
  if (auto EC = writeFixedString(Str))
49
0
    return EC;
50
18.9k
  if (auto EC = writeObject('\0'))
51
0
    return EC;
52
18.9k
53
18.9k
  return Error::success();
54
18.9k
}
55
56
19.0k
Error BinaryStreamWriter::writeFixedString(StringRef Str) {
57
19.0k
58
19.0k
  return writeBytes(arrayRefFromStringRef(Str));
59
19.0k
}
60
61
537
Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) {
62
537
  return writeStreamRef(Ref, Ref.getLength());
63
537
}
64
65
537
Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) {
66
537
  BinaryStreamReader SrcReader(Ref.slice(0, Length));
67
537
  // This is a bit tricky.  If we just call readBytes, we are requiring that it
68
537
  // return us the entire stream as a contiguous buffer.  There is no guarantee
69
537
  // this can be satisfied by returning a reference straight from the buffer, as
70
537
  // an implementation may not store all data in a single contiguous buffer.  So
71
537
  // we iterate over each contiguous chunk, writing each one in succession.
72
1.99k
  while (SrcReader.bytesRemaining() > 0) {
73
1.46k
    ArrayRef<uint8_t> Chunk;
74
1.46k
    if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
75
0
      return EC;
76
1.46k
    if (auto EC = writeBytes(Chunk))
77
0
      return EC;
78
1.46k
  }
79
537
  return Error::success();
80
537
}
81
82
std::pair<BinaryStreamWriter, BinaryStreamWriter>
83
1.06k
BinaryStreamWriter::split(uint32_t Off) const {
84
1.06k
  assert(getLength() >= Off);
85
1.06k
86
1.06k
  WritableBinaryStreamRef First = Stream.drop_front(Offset);
87
1.06k
88
1.06k
  WritableBinaryStreamRef Second = First.drop_front(Off);
89
1.06k
  First = First.keep_front(Off);
90
1.06k
  BinaryStreamWriter W1{First};
91
1.06k
  BinaryStreamWriter W2{Second};
92
1.06k
  return std::make_pair(W1, W2);
93
1.06k
}
94
95
2.73k
Error BinaryStreamWriter::padToAlignment(uint32_t Align) {
96
2.73k
  uint32_t NewOffset = alignTo(Offset, Align);
97
2.73k
  if (NewOffset > getLength())
98
0
    return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
99
5.25k
  
while (2.73k
Offset < NewOffset)
100
2.52k
    if (auto EC = writeInteger('\0'))
101
0
      return EC;
102
2.73k
  return Error::success();
103
2.73k
}