Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/BinaryStreamRef.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- BinaryStreamRef.cpp - ----------------------------------------------===//
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/BinaryStreamRef.h"
10
#include "llvm/Support/BinaryByteStream.h"
11
12
using namespace llvm;
13
using namespace llvm::support;
14
15
namespace {
16
17
class ArrayRefImpl : public BinaryStream {
18
public:
19
3.44k
  ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
20
21
2.37k
  llvm::support::endianness getEndian() const override {
22
2.37k
    return BBS.getEndian();
23
2.37k
  }
24
  Error readBytes(uint32_t Offset, uint32_t Size,
25
32.0k
                  ArrayRef<uint8_t> &Buffer) override {
26
32.0k
    return BBS.readBytes(Offset, Size, Buffer);
27
32.0k
  }
28
  Error readLongestContiguousChunk(uint32_t Offset,
29
981
                                   ArrayRef<uint8_t> &Buffer) override {
30
981
    return BBS.readLongestContiguousChunk(Offset, Buffer);
31
981
  }
32
0
  uint32_t getLength() override { return BBS.getLength(); }
33
34
private:
35
  BinaryByteStream BBS;
36
};
37
38
class MutableArrayRefImpl : public WritableBinaryStream {
39
public:
40
  MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
41
4.80k
      : BBS(Data, Endian) {}
42
43
  // Inherited via WritableBinaryStream
44
19.5k
  llvm::support::endianness getEndian() const override {
45
19.5k
    return BBS.getEndian();
46
19.5k
  }
47
  Error readBytes(uint32_t Offset, uint32_t Size,
48
0
                  ArrayRef<uint8_t> &Buffer) override {
49
0
    return BBS.readBytes(Offset, Size, Buffer);
50
0
  }
51
  Error readLongestContiguousChunk(uint32_t Offset,
52
0
                                   ArrayRef<uint8_t> &Buffer) override {
53
0
    return BBS.readLongestContiguousChunk(Offset, Buffer);
54
0
  }
55
0
  uint32_t getLength() override { return BBS.getLength(); }
56
57
31.7k
  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
58
31.7k
    return BBS.writeBytes(Offset, Data);
59
31.7k
  }
60
0
  Error commit() override { return BBS.commit(); }
61
62
private:
63
  MutableBinaryByteStream BBS;
64
};
65
}
66
67
BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream)
68
35.7k
    : BinaryStreamRefBase(Stream) {}
69
BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream, uint32_t Offset,
70
                                 Optional<uint32_t> Length)
71
2.10k
    : BinaryStreamRefBase(Stream, Offset, Length) {}
72
BinaryStreamRef::BinaryStreamRef(ArrayRef<uint8_t> Data, endianness Endian)
73
    : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
74
3.44k
                          Data.size()) {}
75
BinaryStreamRef::BinaryStreamRef(StringRef Data, endianness Endian)
76
    : BinaryStreamRef(makeArrayRef(Data.bytes_begin(), Data.bytes_end()),
77
2.06k
                      Endian) {}
Unexecuted instantiation: llvm::BinaryStreamRef::BinaryStreamRef(llvm::StringRef, llvm::support::endianness)
llvm::BinaryStreamRef::BinaryStreamRef(llvm::StringRef, llvm::support::endianness)
Line
Count
Source
77
2.06k
                      Endian) {}
78
79
Error BinaryStreamRef::readBytes(uint32_t Offset, uint32_t Size,
80
670k
                                 ArrayRef<uint8_t> &Buffer) const {
81
670k
  if (auto EC = checkOffsetForRead(Offset, Size))
82
133
    return EC;
83
670k
  return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
84
670k
}
85
86
Error BinaryStreamRef::readLongestContiguousChunk(
87
63.0k
    uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
88
63.0k
  if (auto EC = checkOffsetForRead(Offset, 1))
89
347
    return EC;
90
62.7k
91
62.7k
  if (auto EC =
92
0
          BorrowedImpl->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
93
0
    return EC;
94
62.7k
  // This StreamRef might refer to a smaller window over a larger stream.  In
95
62.7k
  // that case we will have read out more bytes than we should return, because
96
62.7k
  // we should not read past the end of the current view.
97
62.7k
  uint32_t MaxLength = getLength() - Offset;
98
62.7k
  if (Buffer.size() > MaxLength)
99
4.61k
    Buffer = Buffer.slice(0, MaxLength);
100
62.7k
  return Error::success();
101
62.7k
}
102
103
WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream)
104
5.71k
    : BinaryStreamRefBase(Stream) {}
105
106
WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream,
107
                                                 uint32_t Offset,
108
                                                 Optional<uint32_t> Length)
109
0
    : BinaryStreamRefBase(Stream, Offset, Length) {}
110
111
WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
112
                                                 endianness Endian)
113
    : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
114
4.80k
                          0, Data.size()) {}
115
116
117
Error WritableBinaryStreamRef::writeBytes(uint32_t Offset,
118
158k
                                          ArrayRef<uint8_t> Data) const {
119
158k
  if (auto EC = checkOffsetForWrite(Offset, Data.size()))
120
12
    return EC;
121
158k
122
158k
  return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
123
158k
}
124
125
2.10k
WritableBinaryStreamRef::operator BinaryStreamRef() const {
126
2.10k
  return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
127
2.10k
}
128
129
/// For buffered streams, commits changes to the backing store.
130
0
Error WritableBinaryStreamRef::commit() { return BorrowedImpl->commit(); }