1 //===--- SerializedDiagnosticReader.cpp - Reads diagnostics ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/SerializedDiagnosticReader.h"
11 #include "clang/Basic/FileManager.h"
12 #include "clang/Frontend/SerializedDiagnostics.h"
13 #include "llvm/Support/ManagedStatic.h"
15 using namespace clang;
16 using namespace clang::serialized_diags;
18 std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) {
19 // Open the diagnostics file.
21 FileManager FileMgr(FO);
23 auto Buffer = FileMgr.getBufferForFile(File);
25 return SDError::CouldNotLoad;
27 llvm::BitstreamCursor Stream(**Buffer);
28 Optional<llvm::BitstreamBlockInfo> BlockInfo;
30 // Sniff for the signature.
31 if (Stream.Read(8) != 'D' ||
32 Stream.Read(8) != 'I' ||
33 Stream.Read(8) != 'A' ||
34 Stream.Read(8) != 'G')
35 return SDError::InvalidSignature;
37 // Read the top level blocks.
38 while (!Stream.AtEndOfStream()) {
39 if (Stream.ReadCode() != llvm::bitc::ENTER_SUBBLOCK)
40 return SDError::InvalidDiagnostics;
43 switch (Stream.ReadSubBlockID()) {
44 case llvm::bitc::BLOCKINFO_BLOCK_ID: {
45 BlockInfo = Stream.ReadBlockInfoBlock();
47 return SDError::MalformedBlockInfoBlock;
48 Stream.setBlockInfo(&*BlockInfo);
52 if ((EC = readMetaBlock(Stream)))
56 if ((EC = readDiagnosticBlock(Stream)))
60 if (!Stream.SkipBlock())
61 return SDError::MalformedTopLevelBlock;
65 return std::error_code();
68 enum class SerializedDiagnosticReader::Cursor {
74 llvm::ErrorOr<SerializedDiagnosticReader::Cursor>
75 SerializedDiagnosticReader::skipUntilRecordOrBlock(
76 llvm::BitstreamCursor &Stream, unsigned &BlockOrRecordID) {
79 while (!Stream.AtEndOfStream()) {
80 unsigned Code = Stream.ReadCode();
82 switch ((llvm::bitc::FixedAbbrevIDs)Code) {
83 case llvm::bitc::ENTER_SUBBLOCK:
84 BlockOrRecordID = Stream.ReadSubBlockID();
85 return Cursor::BlockBegin;
87 case llvm::bitc::END_BLOCK:
88 if (Stream.ReadBlockEnd())
89 return SDError::InvalidDiagnostics;
90 return Cursor::BlockEnd;
92 case llvm::bitc::DEFINE_ABBREV:
93 Stream.ReadAbbrevRecord();
96 case llvm::bitc::UNABBREV_RECORD:
97 return SDError::UnsupportedConstruct;
100 // We found a record.
101 BlockOrRecordID = Code;
102 return Cursor::Record;
106 return SDError::InvalidDiagnostics;
110 SerializedDiagnosticReader::readMetaBlock(llvm::BitstreamCursor &Stream) {
111 if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META))
112 return SDError::MalformedMetadataBlock;
114 bool VersionChecked = false;
117 unsigned BlockOrCode = 0;
118 llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode);
125 case Cursor::BlockBegin:
126 if (Stream.SkipBlock())
127 return SDError::MalformedMetadataBlock;
129 case Cursor::BlockEnd:
131 return SDError::MissingVersion;
132 return std::error_code();
135 SmallVector<uint64_t, 1> Record;
136 unsigned RecordID = Stream.readRecord(BlockOrCode, Record);
138 if (RecordID == RECORD_VERSION) {
139 if (Record.size() < 1)
140 return SDError::MissingVersion;
141 if (Record[0] > VersionNumber)
142 return SDError::VersionMismatch;
143 VersionChecked = true;
149 SerializedDiagnosticReader::readDiagnosticBlock(llvm::BitstreamCursor &Stream) {
150 if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG))
151 return SDError::MalformedDiagnosticBlock;
154 if ((EC = visitStartOfDiagnostic()))
157 SmallVector<uint64_t, 16> Record;
159 unsigned BlockOrCode = 0;
160 llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode);
165 case Cursor::BlockBegin:
166 // The only blocks we care about are subdiagnostics.
167 if (BlockOrCode == serialized_diags::BLOCK_DIAG) {
168 if ((EC = readDiagnosticBlock(Stream)))
170 } else if (!Stream.SkipBlock())
171 return SDError::MalformedSubBlock;
173 case Cursor::BlockEnd:
174 if ((EC = visitEndOfDiagnostic()))
176 return std::error_code();
184 unsigned RecID = Stream.readRecord(BlockOrCode, Record, &Blob);
186 if (RecID < serialized_diags::RECORD_FIRST ||
187 RecID > serialized_diags::RECORD_LAST)
190 switch ((RecordIDs)RecID) {
191 case RECORD_CATEGORY:
192 // A category has ID and name size.
193 if (Record.size() != 2)
194 return SDError::MalformedDiagnosticRecord;
195 if ((EC = visitCategoryRecord(Record[0], Blob)))
199 // A diagnostic has severity, location (4), category, flag, and message
201 if (Record.size() != 8)
202 return SDError::MalformedDiagnosticRecord;
203 if ((EC = visitDiagnosticRecord(
204 Record[0], Location(Record[1], Record[2], Record[3], Record[4]),
205 Record[5], Record[6], Blob)))
208 case RECORD_DIAG_FLAG:
209 // A diagnostic flag has ID and name size.
210 if (Record.size() != 2)
211 return SDError::MalformedDiagnosticRecord;
212 if ((EC = visitDiagFlagRecord(Record[0], Blob)))
215 case RECORD_FILENAME:
216 // A filename has ID, size, timestamp, and name size. The size and
217 // timestamp are legacy fields that are always zero these days.
218 if (Record.size() != 4)
219 return SDError::MalformedDiagnosticRecord;
220 if ((EC = visitFilenameRecord(Record[0], Record[1], Record[2], Blob)))
224 // A fixit has two locations (4 each) and message size.
225 if (Record.size() != 9)
226 return SDError::MalformedDiagnosticRecord;
227 if ((EC = visitFixitRecord(
228 Location(Record[0], Record[1], Record[2], Record[3]),
229 Location(Record[4], Record[5], Record[6], Record[7]), Blob)))
232 case RECORD_SOURCE_RANGE:
233 // A source range is two locations (4 each).
234 if (Record.size() != 8)
235 return SDError::MalformedDiagnosticRecord;
236 if ((EC = visitSourceRangeRecord(
237 Location(Record[0], Record[1], Record[2], Record[3]),
238 Location(Record[4], Record[5], Record[6], Record[7]))))
242 // A version is just a number.
243 if (Record.size() != 1)
244 return SDError::MalformedDiagnosticRecord;
245 if ((EC = visitVersionRecord(Record[0])))
253 class SDErrorCategoryType final : public std::error_category {
254 const char *name() const noexcept override {
255 return "clang.serialized_diags";
257 std::string message(int IE) const override {
258 SDError E = static_cast<SDError>(IE);
260 case SDError::CouldNotLoad:
261 return "Failed to open diagnostics file";
262 case SDError::InvalidSignature:
263 return "Invalid diagnostics signature";
264 case SDError::InvalidDiagnostics:
265 return "Parse error reading diagnostics";
266 case SDError::MalformedTopLevelBlock:
267 return "Malformed block at top-level of diagnostics";
268 case SDError::MalformedSubBlock:
269 return "Malformed sub-block in a diagnostic";
270 case SDError::MalformedBlockInfoBlock:
271 return "Malformed BlockInfo block";
272 case SDError::MalformedMetadataBlock:
273 return "Malformed Metadata block";
274 case SDError::MalformedDiagnosticBlock:
275 return "Malformed Diagnostic block";
276 case SDError::MalformedDiagnosticRecord:
277 return "Malformed Diagnostic record";
278 case SDError::MissingVersion:
279 return "No version provided in diagnostics";
280 case SDError::VersionMismatch:
281 return "Unsupported diagnostics version";
282 case SDError::UnsupportedConstruct:
283 return "Bitcode constructs that are not supported in diagnostics appear";
284 case SDError::HandlerFailed:
285 return "Generic error occurred while handling a record";
287 llvm_unreachable("Unknown error type!");
292 static llvm::ManagedStatic<SDErrorCategoryType> ErrorCategory;
293 const std::error_category &clang::serialized_diags::SDErrorCategory() {
294 return *ErrorCategory;