File: | include/llvm/Bitcode/BitstreamReader.h |
Warning: | line 208, column 39 The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::SimpleBitstreamCursor::word_t' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- SerializedDiagnosticReader.cpp - Reads diagnostics -----------------===// | |||
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 "clang/Frontend/SerializedDiagnosticReader.h" | |||
10 | #include "clang/Basic/FileManager.h" | |||
11 | #include "clang/Basic/FileSystemOptions.h" | |||
12 | #include "clang/Frontend/SerializedDiagnostics.h" | |||
13 | #include "llvm/ADT/Optional.h" | |||
14 | #include "llvm/ADT/SmallVector.h" | |||
15 | #include "llvm/ADT/StringRef.h" | |||
16 | #include "llvm/Bitcode/BitCodes.h" | |||
17 | #include "llvm/Bitcode/BitstreamReader.h" | |||
18 | #include "llvm/Support/Compiler.h" | |||
19 | #include "llvm/Support/ErrorHandling.h" | |||
20 | #include "llvm/Support/ErrorOr.h" | |||
21 | #include "llvm/Support/ManagedStatic.h" | |||
22 | #include <cstdint> | |||
23 | #include <system_error> | |||
24 | ||||
25 | using namespace clang; | |||
26 | using namespace serialized_diags; | |||
27 | ||||
28 | std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) { | |||
29 | // Open the diagnostics file. | |||
30 | FileSystemOptions FO; | |||
31 | FileManager FileMgr(FO); | |||
32 | ||||
33 | auto Buffer = FileMgr.getBufferForFile(File); | |||
34 | if (!Buffer) | |||
35 | return SDError::CouldNotLoad; | |||
36 | ||||
37 | llvm::BitstreamCursor Stream(**Buffer); | |||
38 | Optional<llvm::BitstreamBlockInfo> BlockInfo; | |||
39 | ||||
40 | if (Stream.AtEndOfStream()) | |||
41 | return SDError::InvalidSignature; | |||
42 | ||||
43 | // Sniff for the signature. | |||
44 | if (Stream.Read(8) != 'D' || | |||
45 | Stream.Read(8) != 'I' || | |||
46 | Stream.Read(8) != 'A' || | |||
47 | Stream.Read(8) != 'G') | |||
48 | return SDError::InvalidSignature; | |||
49 | ||||
50 | // Read the top level blocks. | |||
51 | while (!Stream.AtEndOfStream()) { | |||
52 | if (Stream.ReadCode() != llvm::bitc::ENTER_SUBBLOCK) | |||
53 | return SDError::InvalidDiagnostics; | |||
54 | ||||
55 | std::error_code EC; | |||
56 | switch (Stream.ReadSubBlockID()) { | |||
57 | case llvm::bitc::BLOCKINFO_BLOCK_ID: | |||
58 | BlockInfo = Stream.ReadBlockInfoBlock(); | |||
59 | if (!BlockInfo) | |||
60 | return SDError::MalformedBlockInfoBlock; | |||
61 | Stream.setBlockInfo(&*BlockInfo); | |||
62 | continue; | |||
63 | case BLOCK_META: | |||
64 | if ((EC = readMetaBlock(Stream))) | |||
65 | return EC; | |||
66 | continue; | |||
67 | case BLOCK_DIAG: | |||
68 | if ((EC = readDiagnosticBlock(Stream))) | |||
69 | return EC; | |||
70 | continue; | |||
71 | default: | |||
72 | if (!Stream.SkipBlock()) | |||
73 | return SDError::MalformedTopLevelBlock; | |||
74 | continue; | |||
75 | } | |||
76 | } | |||
77 | return {}; | |||
78 | } | |||
79 | ||||
80 | enum class SerializedDiagnosticReader::Cursor { | |||
81 | Record = 1, | |||
82 | BlockEnd, | |||
83 | BlockBegin | |||
84 | }; | |||
85 | ||||
86 | llvm::ErrorOr<SerializedDiagnosticReader::Cursor> | |||
87 | SerializedDiagnosticReader::skipUntilRecordOrBlock( | |||
88 | llvm::BitstreamCursor &Stream, unsigned &BlockOrRecordID) { | |||
89 | BlockOrRecordID = 0; | |||
90 | ||||
91 | while (!Stream.AtEndOfStream()) { | |||
92 | unsigned Code = Stream.ReadCode(); | |||
93 | ||||
94 | switch ((llvm::bitc::FixedAbbrevIDs)Code) { | |||
95 | case llvm::bitc::ENTER_SUBBLOCK: | |||
96 | BlockOrRecordID = Stream.ReadSubBlockID(); | |||
97 | return Cursor::BlockBegin; | |||
98 | ||||
99 | case llvm::bitc::END_BLOCK: | |||
100 | if (Stream.ReadBlockEnd()) | |||
101 | return SDError::InvalidDiagnostics; | |||
102 | return Cursor::BlockEnd; | |||
103 | ||||
104 | case llvm::bitc::DEFINE_ABBREV: | |||
105 | Stream.ReadAbbrevRecord(); | |||
106 | continue; | |||
107 | ||||
108 | case llvm::bitc::UNABBREV_RECORD: | |||
109 | return SDError::UnsupportedConstruct; | |||
110 | ||||
111 | default: | |||
112 | // We found a record. | |||
113 | BlockOrRecordID = Code; | |||
114 | return Cursor::Record; | |||
115 | } | |||
116 | } | |||
117 | ||||
118 | return SDError::InvalidDiagnostics; | |||
119 | } | |||
120 | ||||
121 | std::error_code | |||
122 | SerializedDiagnosticReader::readMetaBlock(llvm::BitstreamCursor &Stream) { | |||
123 | if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META)) | |||
124 | return SDError::MalformedMetadataBlock; | |||
125 | ||||
126 | bool VersionChecked = false; | |||
127 | ||||
128 | while (true) { | |||
129 | unsigned BlockOrCode = 0; | |||
130 | llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode); | |||
131 | if (!Res) | |||
132 | Res.getError(); | |||
133 | ||||
134 | switch (Res.get()) { | |||
135 | case Cursor::Record: | |||
136 | break; | |||
137 | case Cursor::BlockBegin: | |||
138 | if (Stream.SkipBlock()) | |||
139 | return SDError::MalformedMetadataBlock; | |||
140 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | |||
141 | case Cursor::BlockEnd: | |||
142 | if (!VersionChecked) | |||
143 | return SDError::MissingVersion; | |||
144 | return {}; | |||
145 | } | |||
146 | ||||
147 | SmallVector<uint64_t, 1> Record; | |||
148 | unsigned RecordID = Stream.readRecord(BlockOrCode, Record); | |||
149 | ||||
150 | if (RecordID == RECORD_VERSION) { | |||
151 | if (Record.size() < 1) | |||
152 | return SDError::MissingVersion; | |||
153 | if (Record[0] > VersionNumber) | |||
154 | return SDError::VersionMismatch; | |||
155 | VersionChecked = true; | |||
156 | } | |||
157 | } | |||
158 | } | |||
159 | ||||
160 | std::error_code | |||
161 | SerializedDiagnosticReader::readDiagnosticBlock(llvm::BitstreamCursor &Stream) { | |||
162 | if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG)) | |||
| ||||
163 | return SDError::MalformedDiagnosticBlock; | |||
164 | ||||
165 | std::error_code EC; | |||
166 | if ((EC = visitStartOfDiagnostic())) | |||
167 | return EC; | |||
168 | ||||
169 | SmallVector<uint64_t, 16> Record; | |||
170 | while (true) { | |||
171 | unsigned BlockOrCode = 0; | |||
172 | llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode); | |||
173 | if (!Res) | |||
174 | Res.getError(); | |||
175 | ||||
176 | switch (Res.get()) { | |||
177 | case Cursor::BlockBegin: | |||
178 | // The only blocks we care about are subdiagnostics. | |||
179 | if (BlockOrCode == serialized_diags::BLOCK_DIAG) { | |||
180 | if ((EC = readDiagnosticBlock(Stream))) | |||
181 | return EC; | |||
182 | } else if (!Stream.SkipBlock()) | |||
183 | return SDError::MalformedSubBlock; | |||
184 | continue; | |||
185 | case Cursor::BlockEnd: | |||
186 | if ((EC = visitEndOfDiagnostic())) | |||
187 | return EC; | |||
188 | return {}; | |||
189 | case Cursor::Record: | |||
190 | break; | |||
191 | } | |||
192 | ||||
193 | // Read the record. | |||
194 | Record.clear(); | |||
195 | StringRef Blob; | |||
196 | unsigned RecID = Stream.readRecord(BlockOrCode, Record, &Blob); | |||
197 | ||||
198 | if (RecID < serialized_diags::RECORD_FIRST || | |||
199 | RecID > serialized_diags::RECORD_LAST) | |||
200 | continue; | |||
201 | ||||
202 | switch ((RecordIDs)RecID) { | |||
203 | case RECORD_CATEGORY: | |||
204 | // A category has ID and name size. | |||
205 | if (Record.size() != 2) | |||
206 | return SDError::MalformedDiagnosticRecord; | |||
207 | if ((EC = visitCategoryRecord(Record[0], Blob))) | |||
208 | return EC; | |||
209 | continue; | |||
210 | case RECORD_DIAG: | |||
211 | // A diagnostic has severity, location (4), category, flag, and message | |||
212 | // size. | |||
213 | if (Record.size() != 8) | |||
214 | return SDError::MalformedDiagnosticRecord; | |||
215 | if ((EC = visitDiagnosticRecord( | |||
216 | Record[0], Location(Record[1], Record[2], Record[3], Record[4]), | |||
217 | Record[5], Record[6], Blob))) | |||
218 | return EC; | |||
219 | continue; | |||
220 | case RECORD_DIAG_FLAG: | |||
221 | // A diagnostic flag has ID and name size. | |||
222 | if (Record.size() != 2) | |||
223 | return SDError::MalformedDiagnosticRecord; | |||
224 | if ((EC = visitDiagFlagRecord(Record[0], Blob))) | |||
225 | return EC; | |||
226 | continue; | |||
227 | case RECORD_FILENAME: | |||
228 | // A filename has ID, size, timestamp, and name size. The size and | |||
229 | // timestamp are legacy fields that are always zero these days. | |||
230 | if (Record.size() != 4) | |||
231 | return SDError::MalformedDiagnosticRecord; | |||
232 | if ((EC = visitFilenameRecord(Record[0], Record[1], Record[2], Blob))) | |||
233 | return EC; | |||
234 | continue; | |||
235 | case RECORD_FIXIT: | |||
236 | // A fixit has two locations (4 each) and message size. | |||
237 | if (Record.size() != 9) | |||
238 | return SDError::MalformedDiagnosticRecord; | |||
239 | if ((EC = visitFixitRecord( | |||
240 | Location(Record[0], Record[1], Record[2], Record[3]), | |||
241 | Location(Record[4], Record[5], Record[6], Record[7]), Blob))) | |||
242 | return EC; | |||
243 | continue; | |||
244 | case RECORD_SOURCE_RANGE: | |||
245 | // A source range is two locations (4 each). | |||
246 | if (Record.size() != 8) | |||
247 | return SDError::MalformedDiagnosticRecord; | |||
248 | if ((EC = visitSourceRangeRecord( | |||
249 | Location(Record[0], Record[1], Record[2], Record[3]), | |||
250 | Location(Record[4], Record[5], Record[6], Record[7])))) | |||
251 | return EC; | |||
252 | continue; | |||
253 | case RECORD_VERSION: | |||
254 | // A version is just a number. | |||
255 | if (Record.size() != 1) | |||
256 | return SDError::MalformedDiagnosticRecord; | |||
257 | if ((EC = visitVersionRecord(Record[0]))) | |||
258 | return EC; | |||
259 | continue; | |||
260 | } | |||
261 | } | |||
262 | } | |||
263 | ||||
264 | namespace { | |||
265 | ||||
266 | class SDErrorCategoryType final : public std::error_category { | |||
267 | const char *name() const noexcept override { | |||
268 | return "clang.serialized_diags"; | |||
269 | } | |||
270 | ||||
271 | std::string message(int IE) const override { | |||
272 | auto E = static_cast<SDError>(IE); | |||
273 | switch (E) { | |||
274 | case SDError::CouldNotLoad: | |||
275 | return "Failed to open diagnostics file"; | |||
276 | case SDError::InvalidSignature: | |||
277 | return "Invalid diagnostics signature"; | |||
278 | case SDError::InvalidDiagnostics: | |||
279 | return "Parse error reading diagnostics"; | |||
280 | case SDError::MalformedTopLevelBlock: | |||
281 | return "Malformed block at top-level of diagnostics"; | |||
282 | case SDError::MalformedSubBlock: | |||
283 | return "Malformed sub-block in a diagnostic"; | |||
284 | case SDError::MalformedBlockInfoBlock: | |||
285 | return "Malformed BlockInfo block"; | |||
286 | case SDError::MalformedMetadataBlock: | |||
287 | return "Malformed Metadata block"; | |||
288 | case SDError::MalformedDiagnosticBlock: | |||
289 | return "Malformed Diagnostic block"; | |||
290 | case SDError::MalformedDiagnosticRecord: | |||
291 | return "Malformed Diagnostic record"; | |||
292 | case SDError::MissingVersion: | |||
293 | return "No version provided in diagnostics"; | |||
294 | case SDError::VersionMismatch: | |||
295 | return "Unsupported diagnostics version"; | |||
296 | case SDError::UnsupportedConstruct: | |||
297 | return "Bitcode constructs that are not supported in diagnostics appear"; | |||
298 | case SDError::HandlerFailed: | |||
299 | return "Generic error occurred while handling a record"; | |||
300 | } | |||
301 | llvm_unreachable("Unknown error type!")::llvm::llvm_unreachable_internal("Unknown error type!", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Frontend/SerializedDiagnosticReader.cpp" , 301); | |||
302 | } | |||
303 | }; | |||
304 | ||||
305 | } // namespace | |||
306 | ||||
307 | static llvm::ManagedStatic<SDErrorCategoryType> ErrorCategory; | |||
308 | const std::error_category &clang::serialized_diags::SDErrorCategory() { | |||
309 | return *ErrorCategory; | |||
310 | } |
1 | //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===// | |||
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 | // This header defines the BitstreamReader class. This class can be used to | |||
10 | // read an arbitrary bitstream, regardless of its contents. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #ifndef LLVM_BITCODE_BITSTREAMREADER_H | |||
15 | #define LLVM_BITCODE_BITSTREAMREADER_H | |||
16 | ||||
17 | #include "llvm/ADT/ArrayRef.h" | |||
18 | #include "llvm/ADT/SmallVector.h" | |||
19 | #include "llvm/Bitcode/BitCodes.h" | |||
20 | #include "llvm/Support/Endian.h" | |||
21 | #include "llvm/Support/ErrorHandling.h" | |||
22 | #include "llvm/Support/MathExtras.h" | |||
23 | #include "llvm/Support/MemoryBuffer.h" | |||
24 | #include <algorithm> | |||
25 | #include <cassert> | |||
26 | #include <climits> | |||
27 | #include <cstddef> | |||
28 | #include <cstdint> | |||
29 | #include <memory> | |||
30 | #include <string> | |||
31 | #include <utility> | |||
32 | #include <vector> | |||
33 | ||||
34 | namespace llvm { | |||
35 | ||||
36 | /// This class maintains the abbreviations read from a block info block. | |||
37 | class BitstreamBlockInfo { | |||
38 | public: | |||
39 | /// This contains information emitted to BLOCKINFO_BLOCK blocks. These | |||
40 | /// describe abbreviations that all blocks of the specified ID inherit. | |||
41 | struct BlockInfo { | |||
42 | unsigned BlockID; | |||
43 | std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs; | |||
44 | std::string Name; | |||
45 | std::vector<std::pair<unsigned, std::string>> RecordNames; | |||
46 | }; | |||
47 | ||||
48 | private: | |||
49 | std::vector<BlockInfo> BlockInfoRecords; | |||
50 | ||||
51 | public: | |||
52 | /// If there is block info for the specified ID, return it, otherwise return | |||
53 | /// null. | |||
54 | const BlockInfo *getBlockInfo(unsigned BlockID) const { | |||
55 | // Common case, the most recent entry matches BlockID. | |||
56 | if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) | |||
57 | return &BlockInfoRecords.back(); | |||
58 | ||||
59 | for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); | |||
60 | i != e; ++i) | |||
61 | if (BlockInfoRecords[i].BlockID == BlockID) | |||
62 | return &BlockInfoRecords[i]; | |||
63 | return nullptr; | |||
64 | } | |||
65 | ||||
66 | BlockInfo &getOrCreateBlockInfo(unsigned BlockID) { | |||
67 | if (const BlockInfo *BI = getBlockInfo(BlockID)) | |||
68 | return *const_cast<BlockInfo*>(BI); | |||
69 | ||||
70 | // Otherwise, add a new record. | |||
71 | BlockInfoRecords.emplace_back(); | |||
72 | BlockInfoRecords.back().BlockID = BlockID; | |||
73 | return BlockInfoRecords.back(); | |||
74 | } | |||
75 | }; | |||
76 | ||||
77 | /// This represents a position within a bitstream. There may be multiple | |||
78 | /// independent cursors reading within one bitstream, each maintaining their | |||
79 | /// own local state. | |||
80 | class SimpleBitstreamCursor { | |||
81 | ArrayRef<uint8_t> BitcodeBytes; | |||
82 | size_t NextChar = 0; | |||
83 | ||||
84 | public: | |||
85 | /// This is the current data we have pulled from the stream but have not | |||
86 | /// returned to the client. This is specifically and intentionally defined to | |||
87 | /// follow the word size of the host machine for efficiency. We use word_t in | |||
88 | /// places that are aware of this to make it perfectly explicit what is going | |||
89 | /// on. | |||
90 | using word_t = size_t; | |||
91 | ||||
92 | private: | |||
93 | word_t CurWord = 0; | |||
94 | ||||
95 | /// This is the number of bits in CurWord that are valid. This is always from | |||
96 | /// [0...bits_of(size_t)-1] inclusive. | |||
97 | unsigned BitsInCurWord = 0; | |||
98 | ||||
99 | public: | |||
100 | static const size_t MaxChunkSize = sizeof(word_t) * 8; | |||
101 | ||||
102 | SimpleBitstreamCursor() = default; | |||
103 | explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) | |||
104 | : BitcodeBytes(BitcodeBytes) {} | |||
105 | explicit SimpleBitstreamCursor(StringRef BitcodeBytes) | |||
106 | : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {} | |||
107 | explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes) | |||
108 | : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {} | |||
109 | ||||
110 | bool canSkipToPos(size_t pos) const { | |||
111 | // pos can be skipped to if it is a valid address or one byte past the end. | |||
112 | return pos <= BitcodeBytes.size(); | |||
113 | } | |||
114 | ||||
115 | bool AtEndOfStream() { | |||
116 | return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar; | |||
117 | } | |||
118 | ||||
119 | /// Return the bit # of the bit we are reading. | |||
120 | uint64_t GetCurrentBitNo() const { | |||
121 | return NextChar*CHAR_BIT8 - BitsInCurWord; | |||
122 | } | |||
123 | ||||
124 | // Return the byte # of the current bit. | |||
125 | uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; } | |||
126 | ||||
127 | ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; } | |||
128 | ||||
129 | /// Reset the stream to the specified bit number. | |||
130 | void JumpToBit(uint64_t BitNo) { | |||
131 | size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1); | |||
132 | unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1)); | |||
133 | assert(canSkipToPos(ByteNo) && "Invalid location")((canSkipToPos(ByteNo) && "Invalid location") ? static_cast <void> (0) : __assert_fail ("canSkipToPos(ByteNo) && \"Invalid location\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Bitcode/BitstreamReader.h" , 133, __PRETTY_FUNCTION__)); | |||
134 | ||||
135 | // Move the cursor to the right word. | |||
136 | NextChar = ByteNo; | |||
137 | BitsInCurWord = 0; | |||
138 | ||||
139 | // Skip over any bits that are already consumed. | |||
140 | if (WordBitNo) | |||
141 | Read(WordBitNo); | |||
142 | } | |||
143 | ||||
144 | /// Get a pointer into the bitstream at the specified byte offset. | |||
145 | const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) { | |||
146 | return BitcodeBytes.data() + ByteNo; | |||
147 | } | |||
148 | ||||
149 | /// Get a pointer into the bitstream at the specified bit offset. | |||
150 | /// | |||
151 | /// The bit offset must be on a byte boundary. | |||
152 | const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) { | |||
153 | assert(!(BitNo % 8) && "Expected bit on byte boundary")((!(BitNo % 8) && "Expected bit on byte boundary") ? static_cast <void> (0) : __assert_fail ("!(BitNo % 8) && \"Expected bit on byte boundary\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Bitcode/BitstreamReader.h" , 153, __PRETTY_FUNCTION__)); | |||
154 | return getPointerToByte(BitNo / 8, NumBytes); | |||
155 | } | |||
156 | ||||
157 | void fillCurWord() { | |||
158 | if (NextChar >= BitcodeBytes.size()) | |||
159 | report_fatal_error("Unexpected end of file"); | |||
160 | ||||
161 | // Read the next word from the stream. | |||
162 | const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar; | |||
163 | unsigned BytesRead; | |||
164 | if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) { | |||
165 | BytesRead = sizeof(word_t); | |||
166 | CurWord = | |||
167 | support::endian::read<word_t, support::little, support::unaligned>( | |||
168 | NextCharPtr); | |||
169 | } else { | |||
170 | // Short read. | |||
171 | BytesRead = BitcodeBytes.size() - NextChar; | |||
172 | CurWord = 0; | |||
173 | for (unsigned B = 0; B != BytesRead; ++B) | |||
174 | CurWord |= uint64_t(NextCharPtr[B]) << (B * 8); | |||
175 | } | |||
176 | NextChar += BytesRead; | |||
177 | BitsInCurWord = BytesRead * 8; | |||
178 | } | |||
179 | ||||
180 | word_t Read(unsigned NumBits) { | |||
181 | static const unsigned BitsInWord = MaxChunkSize; | |||
182 | ||||
183 | assert(NumBits && NumBits <= BitsInWord &&((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!" ) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Bitcode/BitstreamReader.h" , 184, __PRETTY_FUNCTION__)) | |||
184 | "Cannot return zero or more than BitsInWord bits!")((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!" ) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Bitcode/BitstreamReader.h" , 184, __PRETTY_FUNCTION__)); | |||
185 | ||||
186 | static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f; | |||
187 | ||||
188 | // If the field is fully contained by CurWord, return it quickly. | |||
189 | if (BitsInCurWord >= NumBits) { | |||
190 | word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits)); | |||
191 | ||||
192 | // Use a mask to avoid undefined behavior. | |||
193 | CurWord >>= (NumBits & Mask); | |||
194 | ||||
195 | BitsInCurWord -= NumBits; | |||
196 | return R; | |||
197 | } | |||
198 | ||||
199 | word_t R = BitsInCurWord ? CurWord : 0; | |||
200 | unsigned BitsLeft = NumBits - BitsInCurWord; | |||
201 | ||||
202 | fillCurWord(); | |||
203 | ||||
204 | // If we run out of data, abort. | |||
205 | if (BitsLeft > BitsInCurWord) | |||
206 | report_fatal_error("Unexpected end of file"); | |||
207 | ||||
208 | word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft)); | |||
| ||||
209 | ||||
210 | // Use a mask to avoid undefined behavior. | |||
211 | CurWord >>= (BitsLeft & Mask); | |||
212 | ||||
213 | BitsInCurWord -= BitsLeft; | |||
214 | ||||
215 | R |= R2 << (NumBits - BitsLeft); | |||
216 | ||||
217 | return R; | |||
218 | } | |||
219 | ||||
220 | uint32_t ReadVBR(unsigned NumBits) { | |||
221 | uint32_t Piece = Read(NumBits); | |||
222 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
223 | return Piece; | |||
224 | ||||
225 | uint32_t Result = 0; | |||
226 | unsigned NextBit = 0; | |||
227 | while (true) { | |||
228 | Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit; | |||
229 | ||||
230 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
231 | return Result; | |||
232 | ||||
233 | NextBit += NumBits-1; | |||
234 | Piece = Read(NumBits); | |||
235 | } | |||
236 | } | |||
237 | ||||
238 | // Read a VBR that may have a value up to 64-bits in size. The chunk size of | |||
239 | // the VBR must still be <= 32 bits though. | |||
240 | uint64_t ReadVBR64(unsigned NumBits) { | |||
241 | uint32_t Piece = Read(NumBits); | |||
242 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
243 | return uint64_t(Piece); | |||
244 | ||||
245 | uint64_t Result = 0; | |||
246 | unsigned NextBit = 0; | |||
247 | while (true) { | |||
248 | Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit; | |||
249 | ||||
250 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
251 | return Result; | |||
252 | ||||
253 | NextBit += NumBits-1; | |||
254 | Piece = Read(NumBits); | |||
255 | } | |||
256 | } | |||
257 | ||||
258 | void SkipToFourByteBoundary() { | |||
259 | // If word_t is 64-bits and if we've read less than 32 bits, just dump | |||
260 | // the bits we have up to the next 32-bit boundary. | |||
261 | if (sizeof(word_t) > 4 && | |||
262 | BitsInCurWord >= 32) { | |||
263 | CurWord >>= BitsInCurWord-32; | |||
264 | BitsInCurWord = 32; | |||
265 | return; | |||
266 | } | |||
267 | ||||
268 | BitsInCurWord = 0; | |||
269 | } | |||
270 | ||||
271 | /// Skip to the end of the file. | |||
272 | void skipToEnd() { NextChar = BitcodeBytes.size(); } | |||
273 | }; | |||
274 | ||||
275 | /// When advancing through a bitstream cursor, each advance can discover a few | |||
276 | /// different kinds of entries: | |||
277 | struct BitstreamEntry { | |||
278 | enum { | |||
279 | Error, // Malformed bitcode was found. | |||
280 | EndBlock, // We've reached the end of the current block, (or the end of the | |||
281 | // file, which is treated like a series of EndBlock records. | |||
282 | SubBlock, // This is the start of a new subblock of a specific ID. | |||
283 | Record // This is a record with a specific AbbrevID. | |||
284 | } Kind; | |||
285 | ||||
286 | unsigned ID; | |||
287 | ||||
288 | static BitstreamEntry getError() { | |||
289 | BitstreamEntry E; E.Kind = Error; return E; | |||
290 | } | |||
291 | ||||
292 | static BitstreamEntry getEndBlock() { | |||
293 | BitstreamEntry E; E.Kind = EndBlock; return E; | |||
294 | } | |||
295 | ||||
296 | static BitstreamEntry getSubBlock(unsigned ID) { | |||
297 | BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E; | |||
298 | } | |||
299 | ||||
300 | static BitstreamEntry getRecord(unsigned AbbrevID) { | |||
301 | BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E; | |||
302 | } | |||
303 | }; | |||
304 | ||||
305 | /// This represents a position within a bitcode file, implemented on top of a | |||
306 | /// SimpleBitstreamCursor. | |||
307 | /// | |||
308 | /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not | |||
309 | /// be passed by value. | |||
310 | class BitstreamCursor : SimpleBitstreamCursor { | |||
311 | // This is the declared size of code values used for the current block, in | |||
312 | // bits. | |||
313 | unsigned CurCodeSize = 2; | |||
314 | ||||
315 | /// Abbrevs installed at in this block. | |||
316 | std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs; | |||
317 | ||||
318 | struct Block { | |||
319 | unsigned PrevCodeSize; | |||
320 | std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs; | |||
321 | ||||
322 | explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} | |||
323 | }; | |||
324 | ||||
325 | /// This tracks the codesize of parent blocks. | |||
326 | SmallVector<Block, 8> BlockScope; | |||
327 | ||||
328 | BitstreamBlockInfo *BlockInfo = nullptr; | |||
329 | ||||
330 | public: | |||
331 | static const size_t MaxChunkSize = sizeof(word_t) * 8; | |||
332 | ||||
333 | BitstreamCursor() = default; | |||
334 | explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) | |||
335 | : SimpleBitstreamCursor(BitcodeBytes) {} | |||
336 | explicit BitstreamCursor(StringRef BitcodeBytes) | |||
337 | : SimpleBitstreamCursor(BitcodeBytes) {} | |||
338 | explicit BitstreamCursor(MemoryBufferRef BitcodeBytes) | |||
339 | : SimpleBitstreamCursor(BitcodeBytes) {} | |||
340 | ||||
341 | using SimpleBitstreamCursor::canSkipToPos; | |||
342 | using SimpleBitstreamCursor::AtEndOfStream; | |||
343 | using SimpleBitstreamCursor::getBitcodeBytes; | |||
344 | using SimpleBitstreamCursor::GetCurrentBitNo; | |||
345 | using SimpleBitstreamCursor::getCurrentByteNo; | |||
346 | using SimpleBitstreamCursor::getPointerToByte; | |||
347 | using SimpleBitstreamCursor::JumpToBit; | |||
348 | using SimpleBitstreamCursor::fillCurWord; | |||
349 | using SimpleBitstreamCursor::Read; | |||
350 | using SimpleBitstreamCursor::ReadVBR; | |||
351 | using SimpleBitstreamCursor::ReadVBR64; | |||
352 | ||||
353 | /// Return the number of bits used to encode an abbrev #. | |||
354 | unsigned getAbbrevIDWidth() const { return CurCodeSize; } | |||
355 | ||||
356 | /// Flags that modify the behavior of advance(). | |||
357 | enum { | |||
358 | /// If this flag is used, the advance() method does not automatically pop | |||
359 | /// the block scope when the end of a block is reached. | |||
360 | AF_DontPopBlockAtEnd = 1, | |||
361 | ||||
362 | /// If this flag is used, abbrev entries are returned just like normal | |||
363 | /// records. | |||
364 | AF_DontAutoprocessAbbrevs = 2 | |||
365 | }; | |||
366 | ||||
367 | /// Advance the current bitstream, returning the next entry in the stream. | |||
368 | BitstreamEntry advance(unsigned Flags = 0) { | |||
369 | while (true) { | |||
370 | if (AtEndOfStream()) | |||
371 | return BitstreamEntry::getError(); | |||
372 | ||||
373 | unsigned Code = ReadCode(); | |||
374 | if (Code == bitc::END_BLOCK) { | |||
375 | // Pop the end of the block unless Flags tells us not to. | |||
376 | if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd()) | |||
377 | return BitstreamEntry::getError(); | |||
378 | return BitstreamEntry::getEndBlock(); | |||
379 | } | |||
380 | ||||
381 | if (Code == bitc::ENTER_SUBBLOCK) | |||
382 | return BitstreamEntry::getSubBlock(ReadSubBlockID()); | |||
383 | ||||
384 | if (Code == bitc::DEFINE_ABBREV && | |||
385 | !(Flags & AF_DontAutoprocessAbbrevs)) { | |||
386 | // We read and accumulate abbrev's, the client can't do anything with | |||
387 | // them anyway. | |||
388 | ReadAbbrevRecord(); | |||
389 | continue; | |||
390 | } | |||
391 | ||||
392 | return BitstreamEntry::getRecord(Code); | |||
393 | } | |||
394 | } | |||
395 | ||||
396 | /// This is a convenience function for clients that don't expect any | |||
397 | /// subblocks. This just skips over them automatically. | |||
398 | BitstreamEntry advanceSkippingSubblocks(unsigned Flags = 0) { | |||
399 | while (true) { | |||
400 | // If we found a normal entry, return it. | |||
401 | BitstreamEntry Entry = advance(Flags); | |||
402 | if (Entry.Kind != BitstreamEntry::SubBlock) | |||
403 | return Entry; | |||
404 | ||||
405 | // If we found a sub-block, just skip over it and check the next entry. | |||
406 | if (SkipBlock()) | |||
407 | return BitstreamEntry::getError(); | |||
408 | } | |||
409 | } | |||
410 | ||||
411 | unsigned ReadCode() { | |||
412 | return Read(CurCodeSize); | |||
413 | } | |||
414 | ||||
415 | // Block header: | |||
416 | // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | |||
417 | ||||
418 | /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block. | |||
419 | unsigned ReadSubBlockID() { | |||
420 | return ReadVBR(bitc::BlockIDWidth); | |||
421 | } | |||
422 | ||||
423 | /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body | |||
424 | /// of this block. If the block record is malformed, return true. | |||
425 | bool SkipBlock() { | |||
426 | // Read and ignore the codelen value. Since we are skipping this block, we | |||
427 | // don't care what code widths are used inside of it. | |||
428 | ReadVBR(bitc::CodeLenWidth); | |||
429 | SkipToFourByteBoundary(); | |||
430 | size_t NumFourBytes = Read(bitc::BlockSizeWidth); | |||
431 | ||||
432 | // Check that the block wasn't partially defined, and that the offset isn't | |||
433 | // bogus. | |||
434 | size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8; | |||
435 | if (AtEndOfStream() || !canSkipToPos(SkipTo/8)) | |||
436 | return true; | |||
437 | ||||
438 | JumpToBit(SkipTo); | |||
439 | return false; | |||
440 | } | |||
441 | ||||
442 | /// Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true | |||
443 | /// if the block has an error. | |||
444 | bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr); | |||
445 | ||||
446 | bool ReadBlockEnd() { | |||
447 | if (BlockScope.empty()) return true; | |||
448 | ||||
449 | // Block tail: | |||
450 | // [END_BLOCK, <align4bytes>] | |||
451 | SkipToFourByteBoundary(); | |||
452 | ||||
453 | popBlockScope(); | |||
454 | return false; | |||
455 | } | |||
456 | ||||
457 | private: | |||
458 | void popBlockScope() { | |||
459 | CurCodeSize = BlockScope.back().PrevCodeSize; | |||
460 | ||||
461 | CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs); | |||
462 | BlockScope.pop_back(); | |||
463 | } | |||
464 | ||||
465 | //===--------------------------------------------------------------------===// | |||
466 | // Record Processing | |||
467 | //===--------------------------------------------------------------------===// | |||
468 | ||||
469 | public: | |||
470 | /// Return the abbreviation for the specified AbbrevId. | |||
471 | const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) { | |||
472 | unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV; | |||
473 | if (AbbrevNo >= CurAbbrevs.size()) | |||
474 | report_fatal_error("Invalid abbrev number"); | |||
475 | return CurAbbrevs[AbbrevNo].get(); | |||
476 | } | |||
477 | ||||
478 | /// Read the current record and discard it, returning the code for the record. | |||
479 | unsigned skipRecord(unsigned AbbrevID); | |||
480 | ||||
481 | unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals, | |||
482 | StringRef *Blob = nullptr); | |||
483 | ||||
484 | //===--------------------------------------------------------------------===// | |||
485 | // Abbrev Processing | |||
486 | //===--------------------------------------------------------------------===// | |||
487 | void ReadAbbrevRecord(); | |||
488 | ||||
489 | /// Read and return a block info block from the bitstream. If an error was | |||
490 | /// encountered, return None. | |||
491 | /// | |||
492 | /// \param ReadBlockInfoNames Whether to read block/record name information in | |||
493 | /// the BlockInfo block. Only llvm-bcanalyzer uses this. | |||
494 | Optional<BitstreamBlockInfo> | |||
495 | ReadBlockInfoBlock(bool ReadBlockInfoNames = false); | |||
496 | ||||
497 | /// Set the block info to be used by this BitstreamCursor to interpret | |||
498 | /// abbreviated records. | |||
499 | void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; } | |||
500 | }; | |||
501 | ||||
502 | } // end llvm namespace | |||
503 | ||||
504 | #endif // LLVM_BITCODE_BITSTREAMREADER_H |