/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/include/llvm/Support/BinaryStreamRef.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- BinaryStreamRef.h - A copyable reference to a stream -----*- C++ -*-===// |
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 | | #ifndef LLVM_SUPPORT_BINARYSTREAMREF_H |
11 | | #define LLVM_SUPPORT_BINARYSTREAMREF_H |
12 | | |
13 | | #include "llvm/ADT/ArrayRef.h" |
14 | | #include "llvm/Support/BinaryStream.h" |
15 | | #include "llvm/Support/BinaryStreamError.h" |
16 | | #include "llvm/Support/Error.h" |
17 | | #include <algorithm> |
18 | | #include <cstdint> |
19 | | #include <memory> |
20 | | |
21 | | namespace llvm { |
22 | | |
23 | | /// Common stuff for mutable and immutable StreamRefs. |
24 | | template <class RefType, class StreamType> class BinaryStreamRefBase { |
25 | | protected: |
26 | 13.6k | BinaryStreamRefBase() = default; llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::BinaryStreamRefBase() Line | Count | Source | 26 | 175 | BinaryStreamRefBase() = default; |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::BinaryStreamRefBase() Line | Count | Source | 26 | 13.4k | BinaryStreamRefBase() = default; |
|
27 | | BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint32_t Offset, |
28 | | uint32_t Length) |
29 | | : SharedImpl(SharedImpl), BorrowedImpl(SharedImpl.get()), |
30 | 1.48k | ViewOffset(Offset), Length(Length) {} llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::BinaryStreamRefBase(std::__1::shared_ptr<llvm::WritableBinaryStream>, unsigned int, unsigned int) Line | Count | Source | 30 | 55 | ViewOffset(Offset), Length(Length) {} |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::BinaryStreamRefBase(std::__1::shared_ptr<llvm::BinaryStream>, unsigned int, unsigned int) Line | Count | Source | 30 | 1.43k | ViewOffset(Offset), Length(Length) {} |
|
31 | | BinaryStreamRefBase(StreamType &BorrowedImpl, uint32_t Offset, |
32 | | uint32_t Length) |
33 | 19.2k | : BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {} llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::BinaryStreamRefBase(llvm::WritableBinaryStream&, unsigned int, unsigned int) Line | Count | Source | 33 | 7.39k | : BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {} |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::BinaryStreamRefBase(llvm::BinaryStream&, unsigned int, unsigned int) Line | Count | Source | 33 | 11.8k | : BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {} |
|
34 | 100k | BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default; llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::BinaryStreamRefBase(llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream> const&) Line | Count | Source | 34 | 91.5k | BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default; |
llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::BinaryStreamRefBase(llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream> const&) Line | Count | Source | 34 | 8.65k | BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default; |
|
35 | | BinaryStreamRefBase &operator=(const BinaryStreamRefBase &Other) = default; |
36 | | |
37 | 15.9k | BinaryStreamRefBase &operator=(BinaryStreamRefBase &&Other) = default; llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::operator=(llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>&&) Line | Count | Source | 37 | 529 | BinaryStreamRefBase &operator=(BinaryStreamRefBase &&Other) = default; |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::operator=(llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>&&) Line | Count | Source | 37 | 15.3k | BinaryStreamRefBase &operator=(BinaryStreamRefBase &&Other) = default; |
|
38 | 29.7k | BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default; llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::BinaryStreamRefBase(llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>&&) Line | Count | Source | 38 | 1.53k | BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default; |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::BinaryStreamRefBase(llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>&&) Line | Count | Source | 38 | 28.2k | BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default; |
|
39 | | |
40 | | public: |
41 | 84.4k | llvm::support::endianness getEndian() const { |
42 | 84.4k | return BorrowedImpl->getEndian(); |
43 | 84.4k | } llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::getEndian() const Line | Count | Source | 41 | 33.5k | llvm::support::endianness getEndian() const { | 42 | 33.5k | return BorrowedImpl->getEndian(); | 43 | 33.5k | } |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::getEndian() const Line | Count | Source | 41 | 50.8k | llvm::support::endianness getEndian() const { | 42 | 50.8k | return BorrowedImpl->getEndian(); | 43 | 50.8k | } |
|
44 | | |
45 | 494k | uint32_t getLength() const { return Length; } llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::getLength() const Line | Count | Source | 45 | 362k | uint32_t getLength() const { return Length; } |
llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::getLength() const Line | Count | Source | 45 | 131k | uint32_t getLength() const { return Length; } |
|
46 | | |
47 | | /// Return a new BinaryStreamRef with the first \p N elements removed. |
48 | 19.3k | RefType drop_front(uint32_t N) const { |
49 | 19.3k | if (!BorrowedImpl) |
50 | 2 | return RefType(); |
51 | 19.3k | |
52 | 19.3k | N = std::min(N, Length); |
53 | 19.3k | RefType Result(static_cast<const RefType &>(*this)); |
54 | 19.3k | Result.ViewOffset += N; |
55 | 19.3k | Result.Length -= N; |
56 | 19.3k | return Result; |
57 | 19.3k | } llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::drop_front(unsigned int) const Line | Count | Source | 48 | 1.00k | RefType drop_front(uint32_t N) const { | 49 | 1.00k | if (!BorrowedImpl) | 50 | 0 | return RefType(); | 51 | 1.00k | | 52 | 1.00k | N = std::min(N, Length); | 53 | 1.00k | RefType Result(static_cast<const RefType &>(*this)); | 54 | 1.00k | Result.ViewOffset += N; | 55 | 1.00k | Result.Length -= N; | 56 | 1.00k | return Result; | 57 | 1.00k | } |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::drop_front(unsigned int) const Line | Count | Source | 48 | 18.3k | RefType drop_front(uint32_t N) const { | 49 | 18.3k | if (!BorrowedImpl) | 50 | 2 | return RefType(); | 51 | 18.3k | | 52 | 18.3k | N = std::min(N, Length); | 53 | 18.3k | RefType Result(static_cast<const RefType &>(*this)); | 54 | 18.3k | Result.ViewOffset += N; | 55 | 18.3k | Result.Length -= N; | 56 | 18.3k | return Result; | 57 | 18.3k | } |
|
58 | | |
59 | | /// Return a new BinaryStreamRef with the first \p N elements removed. |
60 | 6.90k | RefType drop_back(uint32_t N) const { |
61 | 6.90k | if (!BorrowedImpl) |
62 | 0 | return RefType(); |
63 | 6.90k | |
64 | 6.90k | N = std::min(N, Length); |
65 | 6.90k | RefType Result(static_cast<const RefType &>(*this)); |
66 | 6.90k | Result.Length -= N; |
67 | 6.90k | return Result; |
68 | 6.90k | } llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::drop_back(unsigned int) const Line | Count | Source | 60 | 529 | RefType drop_back(uint32_t N) const { | 61 | 529 | if (!BorrowedImpl) | 62 | 0 | return RefType(); | 63 | 529 | | 64 | 529 | N = std::min(N, Length); | 65 | 529 | RefType Result(static_cast<const RefType &>(*this)); | 66 | 529 | Result.Length -= N; | 67 | 529 | return Result; | 68 | 529 | } |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::drop_back(unsigned int) const Line | Count | Source | 60 | 6.37k | RefType drop_back(uint32_t N) const { | 61 | 6.37k | if (!BorrowedImpl) | 62 | 0 | return RefType(); | 63 | 6.37k | | 64 | 6.37k | N = std::min(N, Length); | 65 | 6.37k | RefType Result(static_cast<const RefType &>(*this)); | 66 | 6.37k | Result.Length -= N; | 67 | 6.37k | return Result; | 68 | 6.37k | } |
|
69 | | |
70 | | /// Return a new BinaryStreamRef with only the first \p N elements remaining. |
71 | 6.90k | RefType keep_front(uint32_t N) const { |
72 | 6.90k | assert(N <= getLength()); |
73 | 6.90k | return drop_back(getLength() - N); |
74 | 6.90k | } llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::keep_front(unsigned int) const Line | Count | Source | 71 | 529 | RefType keep_front(uint32_t N) const { | 72 | 529 | assert(N <= getLength()); | 73 | 529 | return drop_back(getLength() - N); | 74 | 529 | } |
llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::keep_front(unsigned int) const Line | Count | Source | 71 | 6.37k | RefType keep_front(uint32_t N) const { | 72 | 6.37k | assert(N <= getLength()); | 73 | 6.37k | return drop_back(getLength() - N); | 74 | 6.37k | } |
|
75 | | |
76 | | /// Return a new BinaryStreamRef with only the last \p N elements remaining. |
77 | | RefType keep_back(uint32_t N) const { |
78 | | assert(N <= getLength()); |
79 | | return drop_front(getLength() - N); |
80 | | } |
81 | | |
82 | | /// Return a new BinaryStreamRef with the first and last \p N elements |
83 | | /// removed. |
84 | | RefType drop_symmetric(uint32_t N) const { |
85 | | return drop_front(N).drop_back(N); |
86 | | } |
87 | | |
88 | | /// Return a new BinaryStreamRef with the first \p Offset elements removed, |
89 | | /// and retaining exactly \p Len elements. |
90 | 6.05k | RefType slice(uint32_t Offset, uint32_t Len) const { |
91 | 6.05k | return drop_front(Offset).keep_front(Len); |
92 | 6.05k | } |
93 | | |
94 | | bool valid() const { return BorrowedImpl != nullptr; } |
95 | | |
96 | 616 | bool operator==(const RefType &Other) const { |
97 | 616 | if (BorrowedImpl != Other.BorrowedImpl) |
98 | 0 | return false; |
99 | 616 | if (616 ViewOffset != Other.ViewOffset616 ) |
100 | 0 | return false; |
101 | 616 | if (616 Length != Other.Length616 ) |
102 | 0 | return false; |
103 | 616 | return true; |
104 | 616 | } |
105 | | |
106 | | protected: |
107 | 218k | Error checkOffset(uint32_t Offset, uint32_t DataSize) const { |
108 | 218k | if (Offset > getLength()) |
109 | 6 | return make_error<BinaryStreamError>(stream_error_code::invalid_offset); |
110 | 218k | if (218k getLength() < DataSize + Offset218k ) |
111 | 199 | return make_error<BinaryStreamError>(stream_error_code::stream_too_short); |
112 | 218k | return Error::success(); |
113 | 218k | } llvm::BinaryStreamRefBase<llvm::BinaryStreamRef, llvm::BinaryStream>::checkOffset(unsigned int, unsigned int) const Line | Count | Source | 107 | 153k | Error checkOffset(uint32_t Offset, uint32_t DataSize) const { | 108 | 153k | if (Offset > getLength()) | 109 | 6 | return make_error<BinaryStreamError>(stream_error_code::invalid_offset); | 110 | 153k | if (153k getLength() < DataSize + Offset153k ) | 111 | 187 | return make_error<BinaryStreamError>(stream_error_code::stream_too_short); | 112 | 153k | return Error::success(); | 113 | 153k | } |
llvm::BinaryStreamRefBase<llvm::WritableBinaryStreamRef, llvm::WritableBinaryStream>::checkOffset(unsigned int, unsigned int) const Line | Count | Source | 107 | 64.8k | Error checkOffset(uint32_t Offset, uint32_t DataSize) const { | 108 | 64.8k | if (Offset > getLength()) | 109 | 0 | return make_error<BinaryStreamError>(stream_error_code::invalid_offset); | 110 | 64.8k | if (64.8k getLength() < DataSize + Offset64.8k ) | 111 | 12 | return make_error<BinaryStreamError>(stream_error_code::stream_too_short); | 112 | 64.8k | return Error::success(); | 113 | 64.8k | } |
|
114 | | |
115 | | std::shared_ptr<StreamType> SharedImpl; |
116 | | StreamType *BorrowedImpl = nullptr; |
117 | | uint32_t ViewOffset = 0; |
118 | | uint32_t Length = 0; |
119 | | }; |
120 | | |
121 | | /// \brief BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It |
122 | | /// provides copy-semantics and read only access to a "window" of the underlying |
123 | | /// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to |
124 | | /// say, it does not inherit and override the methods of BinaryStream. In |
125 | | /// general, you should not pass around pointers or references to BinaryStreams |
126 | | /// and use inheritance to achieve polymorphism. Instead, you should pass |
127 | | /// around BinaryStreamRefs by value and achieve polymorphism that way. |
128 | | class BinaryStreamRef |
129 | | : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> { |
130 | | friend BinaryStreamRefBase<BinaryStreamRef, BinaryStream>; |
131 | | friend class WritableBinaryStreamRef; |
132 | | BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint32_t ViewOffset, |
133 | | uint32_t Length) |
134 | 0 | : BinaryStreamRefBase(Impl, ViewOffset, Length) {} |
135 | | |
136 | | public: |
137 | 13.4k | BinaryStreamRef() = default; |
138 | | BinaryStreamRef(BinaryStream &Stream); |
139 | | BinaryStreamRef(BinaryStream &Stream, uint32_t Offset, uint32_t Length); |
140 | | explicit BinaryStreamRef(ArrayRef<uint8_t> Data, |
141 | | llvm::support::endianness Endian); |
142 | | explicit BinaryStreamRef(StringRef Data, llvm::support::endianness Endian); |
143 | | |
144 | 91.5k | BinaryStreamRef(const BinaryStreamRef &Other) = default; |
145 | | BinaryStreamRef &operator=(const BinaryStreamRef &Other) = default; |
146 | 28.2k | BinaryStreamRef(BinaryStreamRef &&Other) = default; |
147 | 15.3k | BinaryStreamRef &operator=(BinaryStreamRef &&Other) = default; |
148 | | |
149 | | // Use BinaryStreamRef.slice() instead. |
150 | | BinaryStreamRef(BinaryStreamRef &S, uint32_t Offset, |
151 | | uint32_t Length) = delete; |
152 | | |
153 | | /// Given an Offset into this StreamRef and a Size, return a reference to a |
154 | | /// buffer owned by the stream. |
155 | | /// |
156 | | /// \returns a success error code if the entire range of data is within the |
157 | | /// bounds of this BinaryStreamRef's view and the implementation could read |
158 | | /// the data, and an appropriate error code otherwise. |
159 | | Error readBytes(uint32_t Offset, uint32_t Size, |
160 | | ArrayRef<uint8_t> &Buffer) const; |
161 | | |
162 | | /// Given an Offset into this BinaryStreamRef, return a reference to the |
163 | | /// largest buffer the stream could support without necessitating a copy. |
164 | | /// |
165 | | /// \returns a success error code if implementation could read the data, |
166 | | /// and an appropriate error code otherwise. |
167 | | Error readLongestContiguousChunk(uint32_t Offset, |
168 | | ArrayRef<uint8_t> &Buffer) const; |
169 | | }; |
170 | | |
171 | | struct BinarySubstreamRef { |
172 | | uint32_t Offset; // Offset in the parent stream |
173 | | BinaryStreamRef StreamData; // Stream Data |
174 | | |
175 | 0 | BinarySubstreamRef slice(uint32_t Off, uint32_t Size) const { |
176 | 0 | BinaryStreamRef SubSub = StreamData.slice(Off, Size); |
177 | 0 | return {Off + Offset, SubSub}; |
178 | 0 | } |
179 | 0 | BinarySubstreamRef drop_front(uint32_t N) const { |
180 | 0 | return slice(N, size() - N); |
181 | 0 | } |
182 | 0 | BinarySubstreamRef keep_front(uint32_t N) const { return slice(0, N); } |
183 | | |
184 | | std::pair<BinarySubstreamRef, BinarySubstreamRef> |
185 | 0 | split(uint32_t Offset) const { |
186 | 0 | return std::make_pair(keep_front(Offset), drop_front(Offset)); |
187 | 0 | } |
188 | | |
189 | 0 | uint32_t size() const { return StreamData.getLength(); } |
190 | 0 | bool empty() const { return size() == 0; } |
191 | | }; |
192 | | |
193 | | class WritableBinaryStreamRef |
194 | | : public BinaryStreamRefBase<WritableBinaryStreamRef, |
195 | | WritableBinaryStream> { |
196 | | friend BinaryStreamRefBase<WritableBinaryStreamRef, WritableBinaryStream>; |
197 | | WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl, |
198 | | uint32_t ViewOffset, uint32_t Length) |
199 | 0 | : BinaryStreamRefBase(Impl, ViewOffset, Length) {} |
200 | | |
201 | | public: |
202 | 175 | WritableBinaryStreamRef() = default; |
203 | | WritableBinaryStreamRef(WritableBinaryStream &Stream); |
204 | | WritableBinaryStreamRef(WritableBinaryStream &Stream, uint32_t Offset, |
205 | | uint32_t Length); |
206 | | explicit WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data, |
207 | | llvm::support::endianness Endian); |
208 | 8.65k | WritableBinaryStreamRef(const WritableBinaryStreamRef &Other) = default; |
209 | | WritableBinaryStreamRef & |
210 | | operator=(const WritableBinaryStreamRef &Other) = default; |
211 | | |
212 | 1.53k | WritableBinaryStreamRef(WritableBinaryStreamRef &&Other) = default; |
213 | 529 | WritableBinaryStreamRef &operator=(WritableBinaryStreamRef &&Other) = default; |
214 | | |
215 | | // Use WritableBinaryStreamRef.slice() instead. |
216 | | WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint32_t Offset, |
217 | | uint32_t Length) = delete; |
218 | | |
219 | | /// Given an Offset into this WritableBinaryStreamRef and some input data, |
220 | | /// writes the data to the underlying stream. |
221 | | /// |
222 | | /// \returns a success error code if the data could fit within the underlying |
223 | | /// stream at the specified location and the implementation could write the |
224 | | /// data, and an appropriate error code otherwise. |
225 | | Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const; |
226 | | |
227 | | /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef. |
228 | | operator BinaryStreamRef() const; |
229 | | |
230 | | /// \brief For buffered streams, commits changes to the backing store. |
231 | | Error commit(); |
232 | | }; |
233 | | |
234 | | } // end namespace llvm |
235 | | |
236 | | #endif // LLVM_SUPPORT_BINARYSTREAMREF_H |