2018-03-27 05:45:04 +08:00
|
|
|
//===- SerializedDiagnosticReader.cpp - Reads diagnostics -----------------===//
|
2014-10-14 14:30:31 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-10-14 14:30:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/SerializedDiagnosticReader.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2018-03-27 05:45:04 +08:00
|
|
|
#include "clang/Basic/FileSystemOptions.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/Frontend/SerializedDiagnostics.h"
|
2018-03-27 05:45:04 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-07-04 06:40:07 +08:00
|
|
|
#include "llvm/Bitstream/BitCodes.h"
|
|
|
|
#include "llvm/Bitstream/BitstreamReader.h"
|
2018-03-27 05:45:04 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2014-10-14 14:30:31 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2018-03-27 05:45:04 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <system_error>
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2018-03-27 05:45:04 +08:00
|
|
|
using namespace serialized_diags;
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) {
|
|
|
|
// Open the diagnostics file.
|
|
|
|
FileSystemOptions FO;
|
|
|
|
FileManager FileMgr(FO);
|
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
auto Buffer = FileMgr.getBufferForFile(File);
|
2014-10-14 14:30:31 +08:00
|
|
|
if (!Buffer)
|
|
|
|
return SDError::CouldNotLoad;
|
|
|
|
|
2016-11-08 12:17:11 +08:00
|
|
|
llvm::BitstreamCursor Stream(**Buffer);
|
|
|
|
Optional<llvm::BitstreamBlockInfo> BlockInfo;
|
2017-07-07 18:25:12 +08:00
|
|
|
|
|
|
|
if (Stream.AtEndOfStream())
|
|
|
|
return SDError::InvalidSignature;
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
// Sniff for the signature.
|
2019-06-27 03:50:12 +08:00
|
|
|
for (unsigned char C : {'D', 'I', 'A', 'G'}) {
|
|
|
|
if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
|
|
|
|
if (Res.get() == C)
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(Res.takeError());
|
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::InvalidSignature;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
// Read the top level blocks.
|
|
|
|
while (!Stream.AtEndOfStream()) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<unsigned> Res = Stream.ReadCode()) {
|
|
|
|
if (Res.get() != llvm::bitc::ENTER_SUBBLOCK)
|
|
|
|
return SDError::InvalidDiagnostics;
|
|
|
|
} else {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(Res.takeError());
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::InvalidDiagnostics;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
std::error_code EC;
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeSubBlockID = Stream.ReadSubBlockID();
|
|
|
|
if (!MaybeSubBlockID) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(MaybeSubBlockID.takeError());
|
|
|
|
return SDError::InvalidDiagnostics;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (MaybeSubBlockID.get()) {
|
|
|
|
case llvm::bitc::BLOCKINFO_BLOCK_ID: {
|
|
|
|
Expected<Optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
|
|
|
|
Stream.ReadBlockInfoBlock();
|
|
|
|
if (!MaybeBlockInfo) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(MaybeBlockInfo.takeError());
|
|
|
|
return SDError::InvalidDiagnostics;
|
|
|
|
}
|
|
|
|
BlockInfo = std::move(MaybeBlockInfo.get());
|
|
|
|
}
|
2016-11-08 12:17:11 +08:00
|
|
|
if (!BlockInfo)
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::MalformedBlockInfoBlock;
|
2016-11-08 12:17:11 +08:00
|
|
|
Stream.setBlockInfo(&*BlockInfo);
|
2014-10-14 14:30:31 +08:00
|
|
|
continue;
|
|
|
|
case BLOCK_META:
|
|
|
|
if ((EC = readMetaBlock(Stream)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case BLOCK_DIAG:
|
|
|
|
if ((EC = readDiagnosticBlock(Stream)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
default:
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = Stream.SkipBlock()) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(std::move(Err));
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::MalformedTopLevelBlock;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 05:45:04 +08:00
|
|
|
return {};
|
2014-10-14 14:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class SerializedDiagnosticReader::Cursor {
|
|
|
|
Record = 1,
|
|
|
|
BlockEnd,
|
|
|
|
BlockBegin
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::ErrorOr<SerializedDiagnosticReader::Cursor>
|
|
|
|
SerializedDiagnosticReader::skipUntilRecordOrBlock(
|
|
|
|
llvm::BitstreamCursor &Stream, unsigned &BlockOrRecordID) {
|
|
|
|
BlockOrRecordID = 0;
|
|
|
|
|
|
|
|
while (!Stream.AtEndOfStream()) {
|
2019-06-27 03:50:12 +08:00
|
|
|
unsigned Code;
|
|
|
|
if (Expected<unsigned> Res = Stream.ReadCode())
|
|
|
|
Code = Res.get();
|
|
|
|
else
|
|
|
|
return llvm::errorToErrorCode(Res.takeError());
|
2014-10-14 14:30:31 +08:00
|
|
|
|
2019-07-06 04:22:40 +08:00
|
|
|
if (Code >= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
|
|
|
|
// We found a record.
|
|
|
|
BlockOrRecordID = Code;
|
|
|
|
return Cursor::Record;
|
|
|
|
}
|
|
|
|
switch (static_cast<llvm::bitc::FixedAbbrevIDs>(Code)) {
|
2014-10-14 14:30:31 +08:00
|
|
|
case llvm::bitc::ENTER_SUBBLOCK:
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<unsigned> Res = Stream.ReadSubBlockID())
|
|
|
|
BlockOrRecordID = Res.get();
|
|
|
|
else
|
|
|
|
return llvm::errorToErrorCode(Res.takeError());
|
2014-10-14 14:30:31 +08:00
|
|
|
return Cursor::BlockBegin;
|
|
|
|
|
|
|
|
case llvm::bitc::END_BLOCK:
|
|
|
|
if (Stream.ReadBlockEnd())
|
|
|
|
return SDError::InvalidDiagnostics;
|
|
|
|
return Cursor::BlockEnd;
|
|
|
|
|
|
|
|
case llvm::bitc::DEFINE_ABBREV:
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = Stream.ReadAbbrevRecord())
|
|
|
|
return llvm::errorToErrorCode(std::move(Err));
|
2014-10-14 14:30:31 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case llvm::bitc::UNABBREV_RECORD:
|
|
|
|
return SDError::UnsupportedConstruct;
|
|
|
|
|
2019-07-06 04:22:40 +08:00
|
|
|
case llvm::bitc::FIRST_APPLICATION_ABBREV:
|
|
|
|
llvm_unreachable("Unexpected abbrev id.");
|
2014-10-14 14:30:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDError::InvalidDiagnostics;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
SerializedDiagnosticReader::readMetaBlock(llvm::BitstreamCursor &Stream) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err =
|
|
|
|
Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META)) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(std::move(Err));
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::MalformedMetadataBlock;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
bool VersionChecked = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
unsigned BlockOrCode = 0;
|
|
|
|
llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode);
|
|
|
|
if (!Res)
|
|
|
|
Res.getError();
|
|
|
|
|
|
|
|
switch (Res.get()) {
|
|
|
|
case Cursor::Record:
|
|
|
|
break;
|
|
|
|
case Cursor::BlockBegin:
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = Stream.SkipBlock()) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(std::move(Err));
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::MalformedMetadataBlock;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2017-06-03 14:27:16 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2014-10-14 14:30:31 +08:00
|
|
|
case Cursor::BlockEnd:
|
|
|
|
if (!VersionChecked)
|
|
|
|
return SDError::MissingVersion;
|
2018-03-27 05:45:04 +08:00
|
|
|
return {};
|
2014-10-14 14:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<uint64_t, 1> Record;
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeRecordID = Stream.readRecord(BlockOrCode, Record);
|
|
|
|
if (!MaybeRecordID)
|
|
|
|
return errorToErrorCode(MaybeRecordID.takeError());
|
|
|
|
unsigned RecordID = MaybeRecordID.get();
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
if (RecordID == RECORD_VERSION) {
|
|
|
|
if (Record.size() < 1)
|
|
|
|
return SDError::MissingVersion;
|
|
|
|
if (Record[0] > VersionNumber)
|
|
|
|
return SDError::VersionMismatch;
|
|
|
|
VersionChecked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
SerializedDiagnosticReader::readDiagnosticBlock(llvm::BitstreamCursor &Stream) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err =
|
|
|
|
Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG)) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(std::move(Err));
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::MalformedDiagnosticBlock;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
if ((EC = visitStartOfDiagnostic()))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
SmallVector<uint64_t, 16> Record;
|
|
|
|
while (true) {
|
|
|
|
unsigned BlockOrCode = 0;
|
|
|
|
llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode);
|
|
|
|
if (!Res)
|
|
|
|
Res.getError();
|
|
|
|
|
|
|
|
switch (Res.get()) {
|
|
|
|
case Cursor::BlockBegin:
|
|
|
|
// The only blocks we care about are subdiagnostics.
|
|
|
|
if (BlockOrCode == serialized_diags::BLOCK_DIAG) {
|
|
|
|
if ((EC = readDiagnosticBlock(Stream)))
|
|
|
|
return EC;
|
2019-06-27 03:50:12 +08:00
|
|
|
} else if (llvm::Error Err = Stream.SkipBlock()) {
|
|
|
|
// FIXME this drops the error on the floor.
|
|
|
|
consumeError(std::move(Err));
|
2014-10-14 14:30:31 +08:00
|
|
|
return SDError::MalformedSubBlock;
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2014-10-14 14:30:31 +08:00
|
|
|
continue;
|
|
|
|
case Cursor::BlockEnd:
|
|
|
|
if ((EC = visitEndOfDiagnostic()))
|
|
|
|
return EC;
|
2018-03-27 05:45:04 +08:00
|
|
|
return {};
|
2014-10-14 14:30:31 +08:00
|
|
|
case Cursor::Record:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the record.
|
|
|
|
Record.clear();
|
|
|
|
StringRef Blob;
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeRecID =
|
|
|
|
Stream.readRecord(BlockOrCode, Record, &Blob);
|
|
|
|
if (!MaybeRecID)
|
|
|
|
return errorToErrorCode(MaybeRecID.takeError());
|
|
|
|
unsigned RecID = MaybeRecID.get();
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
if (RecID < serialized_diags::RECORD_FIRST ||
|
|
|
|
RecID > serialized_diags::RECORD_LAST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch ((RecordIDs)RecID) {
|
|
|
|
case RECORD_CATEGORY:
|
|
|
|
// A category has ID and name size.
|
|
|
|
if (Record.size() != 2)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitCategoryRecord(Record[0], Blob)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case RECORD_DIAG:
|
|
|
|
// A diagnostic has severity, location (4), category, flag, and message
|
|
|
|
// size.
|
|
|
|
if (Record.size() != 8)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitDiagnosticRecord(
|
|
|
|
Record[0], Location(Record[1], Record[2], Record[3], Record[4]),
|
|
|
|
Record[5], Record[6], Blob)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case RECORD_DIAG_FLAG:
|
|
|
|
// A diagnostic flag has ID and name size.
|
|
|
|
if (Record.size() != 2)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitDiagFlagRecord(Record[0], Blob)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case RECORD_FILENAME:
|
|
|
|
// A filename has ID, size, timestamp, and name size. The size and
|
|
|
|
// timestamp are legacy fields that are always zero these days.
|
|
|
|
if (Record.size() != 4)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitFilenameRecord(Record[0], Record[1], Record[2], Blob)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case RECORD_FIXIT:
|
|
|
|
// A fixit has two locations (4 each) and message size.
|
|
|
|
if (Record.size() != 9)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitFixitRecord(
|
|
|
|
Location(Record[0], Record[1], Record[2], Record[3]),
|
|
|
|
Location(Record[4], Record[5], Record[6], Record[7]), Blob)))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case RECORD_SOURCE_RANGE:
|
|
|
|
// A source range is two locations (4 each).
|
|
|
|
if (Record.size() != 8)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitSourceRangeRecord(
|
|
|
|
Location(Record[0], Record[1], Record[2], Record[3]),
|
|
|
|
Location(Record[4], Record[5], Record[6], Record[7]))))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
case RECORD_VERSION:
|
|
|
|
// A version is just a number.
|
|
|
|
if (Record.size() != 1)
|
|
|
|
return SDError::MalformedDiagnosticRecord;
|
|
|
|
if ((EC = visitVersionRecord(Record[0])))
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2018-03-27 05:45:04 +08:00
|
|
|
|
2014-10-14 14:30:31 +08:00
|
|
|
class SDErrorCategoryType final : public std::error_category {
|
2016-10-20 07:39:55 +08:00
|
|
|
const char *name() const noexcept override {
|
2014-10-14 14:30:31 +08:00
|
|
|
return "clang.serialized_diags";
|
|
|
|
}
|
2018-03-27 05:45:04 +08:00
|
|
|
|
2014-10-14 14:30:31 +08:00
|
|
|
std::string message(int IE) const override {
|
2018-03-27 05:45:04 +08:00
|
|
|
auto E = static_cast<SDError>(IE);
|
2014-10-14 14:30:31 +08:00
|
|
|
switch (E) {
|
|
|
|
case SDError::CouldNotLoad:
|
|
|
|
return "Failed to open diagnostics file";
|
|
|
|
case SDError::InvalidSignature:
|
|
|
|
return "Invalid diagnostics signature";
|
|
|
|
case SDError::InvalidDiagnostics:
|
|
|
|
return "Parse error reading diagnostics";
|
|
|
|
case SDError::MalformedTopLevelBlock:
|
|
|
|
return "Malformed block at top-level of diagnostics";
|
|
|
|
case SDError::MalformedSubBlock:
|
|
|
|
return "Malformed sub-block in a diagnostic";
|
|
|
|
case SDError::MalformedBlockInfoBlock:
|
|
|
|
return "Malformed BlockInfo block";
|
|
|
|
case SDError::MalformedMetadataBlock:
|
|
|
|
return "Malformed Metadata block";
|
|
|
|
case SDError::MalformedDiagnosticBlock:
|
|
|
|
return "Malformed Diagnostic block";
|
|
|
|
case SDError::MalformedDiagnosticRecord:
|
|
|
|
return "Malformed Diagnostic record";
|
|
|
|
case SDError::MissingVersion:
|
|
|
|
return "No version provided in diagnostics";
|
|
|
|
case SDError::VersionMismatch:
|
|
|
|
return "Unsupported diagnostics version";
|
|
|
|
case SDError::UnsupportedConstruct:
|
|
|
|
return "Bitcode constructs that are not supported in diagnostics appear";
|
|
|
|
case SDError::HandlerFailed:
|
|
|
|
return "Generic error occurred while handling a record";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown error type!");
|
|
|
|
}
|
|
|
|
};
|
2018-03-27 05:45:04 +08:00
|
|
|
|
|
|
|
} // namespace
|
2014-10-14 14:30:31 +08:00
|
|
|
|
|
|
|
static llvm::ManagedStatic<SDErrorCategoryType> ErrorCategory;
|
|
|
|
const std::error_category &clang::serialized_diags::SDErrorCategory() {
|
|
|
|
return *ErrorCategory;
|
|
|
|
}
|