2013-01-20 02:47:35 +08:00
|
|
|
//===-- CXLoadedDiagnostic.cpp - Handling of persisent diags ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implements handling of persisent diagnostics.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
#include "CXLoadedDiagnostic.h"
|
|
|
|
#include "CXString.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2011-11-10 16:43:12 +08:00
|
|
|
#include "clang/Frontend/SerializedDiagnosticPrinter.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2011-11-10 16:43:12 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Bitcode/BitstreamReader.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-11-10 16:43:12 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Extend CXDiagnosticSetImpl which contains strings for diagnostics.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-02-19 03:50:38 +08:00
|
|
|
typedef llvm::DenseMap<unsigned, const char *> Strings;
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {
|
|
|
|
public:
|
|
|
|
CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {}
|
|
|
|
virtual ~CXLoadedDiagnosticSetImpl() {}
|
|
|
|
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
|
|
|
Strings Categories;
|
|
|
|
Strings WarningFlags;
|
|
|
|
Strings FileNames;
|
|
|
|
|
|
|
|
FileSystemOptions FO;
|
|
|
|
FileManager FakeFiles;
|
|
|
|
llvm::DenseMap<unsigned, const FileEntry *> Files;
|
2013-02-19 03:50:38 +08:00
|
|
|
|
|
|
|
/// \brief Copy the string into our own allocator.
|
|
|
|
const char *copyString(StringRef Blob) {
|
2013-01-20 10:38:54 +08:00
|
|
|
char *mem = Alloc.Allocate<char>(Blob.size() + 1);
|
|
|
|
memcpy(mem, Blob.data(), Blob.size());
|
|
|
|
mem[Blob.size()] = '\0';
|
2013-02-19 03:50:38 +08:00
|
|
|
return mem;
|
2013-01-20 10:38:54 +08:00
|
|
|
}
|
2011-11-10 16:43:12 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Cleanup.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
CXLoadedDiagnostic::~CXLoadedDiagnostic() {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public CXLoadedDiagnostic methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const {
|
2014-03-04 02:29:52 +08:00
|
|
|
// FIXME: Fail more softly if the diagnostic level is unknown?
|
2014-03-05 01:45:43 +08:00
|
|
|
auto severityAsLevel = static_cast<serialized_diags::Level>(severity);
|
|
|
|
assert(severity == static_cast<unsigned>(severityAsLevel) &&
|
2014-03-04 02:29:52 +08:00
|
|
|
"unknown serialized diagnostic level");
|
|
|
|
|
2014-03-05 01:45:43 +08:00
|
|
|
switch (severityAsLevel) {
|
2014-03-04 02:29:52 +08:00
|
|
|
#define CASE(X) case serialized_diags::X: return CXDiagnostic_##X;
|
|
|
|
CASE(Ignored)
|
|
|
|
CASE(Note)
|
|
|
|
CASE(Warning)
|
|
|
|
CASE(Error)
|
|
|
|
CASE(Fatal)
|
|
|
|
CASE(Remark)
|
|
|
|
#undef CASE
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid diagnostic level");
|
|
|
|
}
|
|
|
|
|
|
|
|
static CXSourceLocation makeLocation(const CXLoadedDiagnostic::Location *DLoc) {
|
|
|
|
// The lowest bit of ptr_data[0] is always set to 1 to indicate this
|
|
|
|
// is a persistent diagnostic.
|
|
|
|
uintptr_t V = (uintptr_t) DLoc;
|
|
|
|
V |= 0x1;
|
|
|
|
CXSourceLocation Loc = { { (void*) V, 0 }, 0 };
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation CXLoadedDiagnostic::getLocation() const {
|
|
|
|
// The lowest bit of ptr_data[0] is always set to 1 to indicate this
|
|
|
|
// is a persistent diagnostic.
|
|
|
|
return makeLocation(&DiagLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString CXLoadedDiagnostic::getSpelling() const {
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createRef(Spelling);
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const {
|
|
|
|
if (DiagOption.empty())
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
// FIXME: possibly refactor with logic in CXStoredDiagnostic.
|
|
|
|
if (Disable)
|
2013-02-02 10:19:29 +08:00
|
|
|
*Disable = cxstring::createDup((Twine("-Wno-") + DiagOption).str());
|
|
|
|
return cxstring::createDup((Twine("-W") + DiagOption).str());
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CXLoadedDiagnostic::getCategory() const {
|
|
|
|
return category;
|
|
|
|
}
|
|
|
|
|
2012-04-12 08:03:31 +08:00
|
|
|
CXString CXLoadedDiagnostic::getCategoryText() const {
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(CategoryText);
|
2012-04-12 08:03:31 +08:00
|
|
|
}
|
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
unsigned CXLoadedDiagnostic::getNumRanges() const {
|
|
|
|
return Ranges.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange CXLoadedDiagnostic::getRange(unsigned Range) const {
|
|
|
|
assert(Range < Ranges.size());
|
|
|
|
return Ranges[Range];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CXLoadedDiagnostic::getNumFixIts() const {
|
|
|
|
return FixIts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString CXLoadedDiagnostic::getFixIt(unsigned FixIt,
|
|
|
|
CXSourceRange *ReplacementRange) const {
|
|
|
|
assert(FixIt < FixIts.size());
|
|
|
|
if (ReplacementRange)
|
|
|
|
*ReplacementRange = FixIts[FixIt].first;
|
2013-02-19 03:50:38 +08:00
|
|
|
return cxstring::createRef(FixIts[FixIt].second);
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXLoadedDiagnostic::decodeLocation(CXSourceLocation location,
|
|
|
|
CXFile *file,
|
|
|
|
unsigned int *line,
|
|
|
|
unsigned int *column,
|
|
|
|
unsigned int *offset) {
|
|
|
|
|
|
|
|
|
|
|
|
// CXSourceLocation consists of the following fields:
|
|
|
|
//
|
|
|
|
// void *ptr_data[2];
|
|
|
|
// unsigned int_data;
|
|
|
|
//
|
|
|
|
// The lowest bit of ptr_data[0] is always set to 1 to indicate this
|
|
|
|
// is a persistent diagnostic.
|
|
|
|
//
|
|
|
|
// For now, do the unoptimized approach and store the data in a side
|
|
|
|
// data structure. We can optimize this case later.
|
|
|
|
|
|
|
|
uintptr_t V = (uintptr_t) location.ptr_data[0];
|
|
|
|
assert((V & 0x1) == 1);
|
|
|
|
V &= ~(uintptr_t)1;
|
|
|
|
|
|
|
|
const Location &Loc = *((Location*)V);
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
*file = Loc.file;
|
|
|
|
if (line)
|
|
|
|
*line = Loc.line;
|
|
|
|
if (column)
|
|
|
|
*column = Loc.column;
|
|
|
|
if (offset)
|
|
|
|
*offset = Loc.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Deserialize diagnostics.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 02:29:52 +08:00
|
|
|
enum { MaxSupportedVersion = 2 };
|
2011-11-10 16:43:12 +08:00
|
|
|
typedef SmallVector<uint64_t, 64> RecordData;
|
|
|
|
enum LoadResult { Failure = 1, Success = 0 };
|
|
|
|
enum StreamResult { Read_EndOfStream,
|
|
|
|
Read_BlockBegin,
|
|
|
|
Read_Failure,
|
|
|
|
Read_Record,
|
|
|
|
Read_BlockEnd };
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DiagLoader {
|
|
|
|
enum CXLoadDiag_Error *error;
|
|
|
|
CXString *errorString;
|
|
|
|
|
|
|
|
void reportBad(enum CXLoadDiag_Error code, llvm::StringRef err) {
|
|
|
|
if (error)
|
|
|
|
*error = code;
|
|
|
|
if (errorString)
|
2013-02-02 10:19:29 +08:00
|
|
|
*errorString = cxstring::createDup(err);
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void reportInvalidFile(llvm::StringRef err) {
|
|
|
|
return reportBad(CXLoadDiag_InvalidFile, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult readMetaBlock(llvm::BitstreamCursor &Stream);
|
|
|
|
|
|
|
|
LoadResult readDiagnosticBlock(llvm::BitstreamCursor &Stream,
|
|
|
|
CXDiagnosticSetImpl &Diags,
|
|
|
|
CXLoadedDiagnosticSetImpl &TopDiags);
|
|
|
|
|
|
|
|
StreamResult readToNextRecordOrBlock(llvm::BitstreamCursor &Stream,
|
|
|
|
llvm::StringRef errorContext,
|
|
|
|
unsigned &BlockOrRecordID,
|
2013-01-20 05:35:35 +08:00
|
|
|
bool atTopLevel = false);
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,
|
|
|
|
Strings &strings, llvm::StringRef errorContext,
|
|
|
|
RecordData &Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
StringRef Blob,
|
2011-11-29 08:30:52 +08:00
|
|
|
bool allowEmptyString = false);
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,
|
2013-02-19 03:50:38 +08:00
|
|
|
const char *&RetStr,
|
2011-11-10 16:43:12 +08:00
|
|
|
llvm::StringRef errorContext,
|
|
|
|
RecordData &Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
StringRef Blob,
|
2011-11-29 08:30:52 +08:00
|
|
|
bool allowEmptyString = false);
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
LoadResult readRange(CXLoadedDiagnosticSetImpl &TopDiags,
|
|
|
|
RecordData &Record, unsigned RecStartIdx,
|
|
|
|
CXSourceRange &SR);
|
|
|
|
|
|
|
|
LoadResult readLocation(CXLoadedDiagnosticSetImpl &TopDiags,
|
|
|
|
RecordData &Record, unsigned &offset,
|
|
|
|
CXLoadedDiagnostic::Location &Loc);
|
|
|
|
|
|
|
|
public:
|
|
|
|
DiagLoader(enum CXLoadDiag_Error *e, CXString *es)
|
|
|
|
: error(e), errorString(es) {
|
|
|
|
if (error)
|
|
|
|
*error = CXLoadDiag_None;
|
|
|
|
if (errorString)
|
2013-02-01 22:21:22 +08:00
|
|
|
*errorString = cxstring::createEmpty();
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
2011-11-11 23:19:48 +08:00
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
CXDiagnosticSet load(const char *file);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
CXDiagnosticSet DiagLoader::load(const char *file) {
|
|
|
|
// Open the diagnostics file.
|
|
|
|
std::string ErrStr;
|
|
|
|
FileSystemOptions FO;
|
|
|
|
FileManager FileMgr(FO);
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer;
|
2011-11-10 16:43:12 +08:00
|
|
|
Buffer.reset(FileMgr.getBufferForFile(file));
|
|
|
|
|
|
|
|
if (!Buffer) {
|
|
|
|
reportBad(CXLoadDiag_CannotLoad, ErrStr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::BitstreamReader StreamFile;
|
|
|
|
StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd());
|
|
|
|
|
|
|
|
llvm::BitstreamCursor Stream;
|
|
|
|
Stream.init(StreamFile);
|
|
|
|
|
|
|
|
// Sniff for the signature.
|
|
|
|
if (Stream.Read(8) != 'D' ||
|
|
|
|
Stream.Read(8) != 'I' ||
|
|
|
|
Stream.Read(8) != 'A' ||
|
|
|
|
Stream.Read(8) != 'G') {
|
|
|
|
reportBad(CXLoadDiag_InvalidFile,
|
|
|
|
"Bad header in diagnostics file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CXLoadedDiagnosticSetImpl> Diags(
|
|
|
|
new CXLoadedDiagnosticSetImpl());
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
unsigned BlockID = 0;
|
|
|
|
StreamResult Res = readToNextRecordOrBlock(Stream, "Top-level",
|
|
|
|
BlockID, true);
|
|
|
|
switch (Res) {
|
|
|
|
case Read_EndOfStream:
|
2014-03-08 03:33:25 +08:00
|
|
|
return (CXDiagnosticSet)Diags.release();
|
2011-11-10 16:43:12 +08:00
|
|
|
case Read_Failure:
|
|
|
|
return 0;
|
|
|
|
case Read_Record:
|
|
|
|
llvm_unreachable("Top-level does not have records");
|
|
|
|
case Read_BlockEnd:
|
|
|
|
continue;
|
|
|
|
case Read_BlockBegin:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (BlockID) {
|
|
|
|
case serialized_diags::BLOCK_META:
|
|
|
|
if (readMetaBlock(Stream))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case serialized_diags::BLOCK_DIAG:
|
|
|
|
if (readDiagnosticBlock(Stream, *Diags.get(), *Diags.get()))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!Stream.SkipBlock()) {
|
|
|
|
reportInvalidFile("Malformed block at top-level of diagnostics file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamResult DiagLoader::readToNextRecordOrBlock(llvm::BitstreamCursor &Stream,
|
|
|
|
llvm::StringRef errorContext,
|
|
|
|
unsigned &blockOrRecordID,
|
2013-01-20 05:35:35 +08:00
|
|
|
bool atTopLevel) {
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
blockOrRecordID = 0;
|
|
|
|
|
|
|
|
while (!Stream.AtEndOfStream()) {
|
|
|
|
unsigned Code = Stream.ReadCode();
|
|
|
|
|
|
|
|
// Handle the top-level specially.
|
|
|
|
if (atTopLevel) {
|
|
|
|
if (Code == llvm::bitc::ENTER_SUBBLOCK) {
|
|
|
|
unsigned BlockID = Stream.ReadSubBlockID();
|
|
|
|
if (BlockID == llvm::bitc::BLOCKINFO_BLOCK_ID) {
|
|
|
|
if (Stream.ReadBlockInfoBlock()) {
|
|
|
|
reportInvalidFile("Malformed BlockInfoBlock in diagnostics file");
|
|
|
|
return Read_Failure;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
blockOrRecordID = BlockID;
|
|
|
|
return Read_BlockBegin;
|
|
|
|
}
|
|
|
|
reportInvalidFile("Only blocks can appear at the top of a "
|
|
|
|
"diagnostic file");
|
|
|
|
return Read_Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((llvm::bitc::FixedAbbrevIDs)Code) {
|
|
|
|
case llvm::bitc::ENTER_SUBBLOCK:
|
|
|
|
blockOrRecordID = Stream.ReadSubBlockID();
|
|
|
|
return Read_BlockBegin;
|
|
|
|
|
|
|
|
case llvm::bitc::END_BLOCK:
|
|
|
|
if (Stream.ReadBlockEnd()) {
|
|
|
|
reportInvalidFile("Cannot read end of block");
|
|
|
|
return Read_Failure;
|
|
|
|
}
|
|
|
|
return Read_BlockEnd;
|
|
|
|
|
|
|
|
case llvm::bitc::DEFINE_ABBREV:
|
|
|
|
Stream.ReadAbbrevRecord();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case llvm::bitc::UNABBREV_RECORD:
|
|
|
|
reportInvalidFile("Diagnostics file should have no unabbreviated "
|
|
|
|
"records");
|
|
|
|
return Read_Failure;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// We found a record.
|
|
|
|
blockOrRecordID = Code;
|
|
|
|
return Read_Record;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atTopLevel)
|
|
|
|
return Read_EndOfStream;
|
|
|
|
|
|
|
|
reportInvalidFile(Twine("Premature end of diagnostics file within ").str() +
|
|
|
|
errorContext.str());
|
|
|
|
return Read_Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult DiagLoader::readMetaBlock(llvm::BitstreamCursor &Stream) {
|
|
|
|
if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META)) {
|
|
|
|
reportInvalidFile("Malformed metadata block");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool versionChecked = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
unsigned blockOrCode = 0;
|
|
|
|
StreamResult Res = readToNextRecordOrBlock(Stream, "Metadata Block",
|
|
|
|
blockOrCode);
|
|
|
|
|
|
|
|
switch(Res) {
|
|
|
|
case Read_EndOfStream:
|
|
|
|
llvm_unreachable("EndOfStream handled by readToNextRecordOrBlock");
|
|
|
|
case Read_Failure:
|
|
|
|
return Failure;
|
|
|
|
case Read_Record:
|
|
|
|
break;
|
|
|
|
case Read_BlockBegin:
|
|
|
|
if (Stream.SkipBlock()) {
|
|
|
|
reportInvalidFile("Malformed metadata block");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
case Read_BlockEnd:
|
|
|
|
if (!versionChecked) {
|
|
|
|
reportInvalidFile("Diagnostics file does not contain version"
|
|
|
|
" information");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
RecordData Record;
|
2013-01-20 10:38:54 +08:00
|
|
|
unsigned recordID = Stream.readRecord(blockOrCode, Record);
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
if (recordID == serialized_diags::RECORD_VERSION) {
|
|
|
|
if (Record.size() < 1) {
|
|
|
|
reportInvalidFile("malformed VERSION identifier in diagnostics file");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
if (Record[0] > MaxSupportedVersion) {
|
2013-03-02 05:41:22 +08:00
|
|
|
reportInvalidFile("diagnostics file is a newer version than the one "
|
2011-11-10 16:43:12 +08:00
|
|
|
"supported");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
versionChecked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,
|
2013-02-19 03:50:38 +08:00
|
|
|
const char *&RetStr,
|
2011-11-10 16:43:12 +08:00
|
|
|
llvm::StringRef errorContext,
|
|
|
|
RecordData &Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
StringRef Blob,
|
2011-11-29 08:30:52 +08:00
|
|
|
bool allowEmptyString) {
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
// Basic buffer overflow check.
|
2013-01-20 10:38:54 +08:00
|
|
|
if (Blob.size() > 65536) {
|
2011-11-10 16:43:12 +08:00
|
|
|
reportInvalidFile(std::string("Out-of-bounds string in ") +
|
|
|
|
std::string(errorContext));
|
|
|
|
return Failure;
|
|
|
|
}
|
2011-11-29 08:30:52 +08:00
|
|
|
|
2013-01-20 10:38:54 +08:00
|
|
|
if (allowEmptyString && Record.size() >= 1 && Blob.size() == 0) {
|
2011-11-29 08:30:52 +08:00
|
|
|
RetStr = "";
|
|
|
|
return Success;
|
|
|
|
}
|
2011-11-10 16:43:12 +08:00
|
|
|
|
2013-01-20 10:38:54 +08:00
|
|
|
if (Record.size() < 1 || Blob.size() == 0) {
|
2011-11-10 16:43:12 +08:00
|
|
|
reportInvalidFile(std::string("Corrupted ") + std::string(errorContext)
|
|
|
|
+ std::string(" entry"));
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
2013-02-19 03:50:38 +08:00
|
|
|
RetStr = TopDiags.copyString(Blob);
|
2011-11-10 16:43:12 +08:00
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,
|
|
|
|
Strings &strings,
|
|
|
|
llvm::StringRef errorContext,
|
|
|
|
RecordData &Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
StringRef Blob,
|
2011-11-29 08:30:52 +08:00
|
|
|
bool allowEmptyString) {
|
2013-02-19 03:50:38 +08:00
|
|
|
const char *RetStr;
|
2013-01-20 10:38:54 +08:00
|
|
|
if (readString(TopDiags, RetStr, errorContext, Record, Blob,
|
2011-11-29 08:30:52 +08:00
|
|
|
allowEmptyString))
|
2011-11-10 16:43:12 +08:00
|
|
|
return Failure;
|
|
|
|
strings[Record[0]] = RetStr;
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult DiagLoader::readLocation(CXLoadedDiagnosticSetImpl &TopDiags,
|
|
|
|
RecordData &Record, unsigned &offset,
|
|
|
|
CXLoadedDiagnostic::Location &Loc) {
|
|
|
|
if (Record.size() < offset + 3) {
|
|
|
|
reportInvalidFile("Corrupted source location");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned fileID = Record[offset++];
|
|
|
|
if (fileID == 0) {
|
|
|
|
// Sentinel value.
|
|
|
|
Loc.file = 0;
|
|
|
|
Loc.line = 0;
|
|
|
|
Loc.column = 0;
|
|
|
|
Loc.offset = 0;
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileEntry *FE = TopDiags.Files[fileID];
|
|
|
|
if (!FE) {
|
|
|
|
reportInvalidFile("Corrupted file entry in source location");
|
|
|
|
return Failure;
|
|
|
|
}
|
2013-01-16 06:09:48 +08:00
|
|
|
Loc.file = const_cast<FileEntry *>(FE);
|
2011-11-10 16:43:12 +08:00
|
|
|
Loc.line = Record[offset++];
|
|
|
|
Loc.column = Record[offset++];
|
|
|
|
Loc.offset = Record[offset++];
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult DiagLoader::readRange(CXLoadedDiagnosticSetImpl &TopDiags,
|
|
|
|
RecordData &Record,
|
|
|
|
unsigned int RecStartIdx,
|
|
|
|
CXSourceRange &SR) {
|
|
|
|
CXLoadedDiagnostic::Location *Start, *End;
|
|
|
|
Start = TopDiags.Alloc.Allocate<CXLoadedDiagnostic::Location>();
|
|
|
|
End = TopDiags.Alloc.Allocate<CXLoadedDiagnostic::Location>();
|
|
|
|
|
|
|
|
if (readLocation(TopDiags, Record, RecStartIdx, *Start))
|
|
|
|
return Failure;
|
|
|
|
if (readLocation(TopDiags, Record, RecStartIdx, *End))
|
|
|
|
return Failure;
|
|
|
|
|
|
|
|
CXSourceLocation startLoc = makeLocation(Start);
|
|
|
|
CXSourceLocation endLoc = makeLocation(End);
|
|
|
|
SR = clang_getRange(startLoc, endLoc);
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadResult DiagLoader::readDiagnosticBlock(llvm::BitstreamCursor &Stream,
|
|
|
|
CXDiagnosticSetImpl &Diags,
|
|
|
|
CXLoadedDiagnosticSetImpl &TopDiags){
|
|
|
|
|
|
|
|
if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG)) {
|
|
|
|
reportInvalidFile("malformed diagnostic block");
|
|
|
|
return Failure;
|
|
|
|
}
|
2014-03-08 04:03:18 +08:00
|
|
|
|
|
|
|
std::unique_ptr<CXLoadedDiagnostic> D(new CXLoadedDiagnostic());
|
2011-11-10 16:43:12 +08:00
|
|
|
RecordData Record;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
unsigned blockOrCode = 0;
|
|
|
|
StreamResult Res = readToNextRecordOrBlock(Stream, "Diagnostic Block",
|
|
|
|
blockOrCode);
|
|
|
|
switch (Res) {
|
|
|
|
case Read_EndOfStream:
|
|
|
|
llvm_unreachable("EndOfStream handled in readToNextRecordOrBlock");
|
|
|
|
case Read_Failure:
|
|
|
|
return Failure;
|
|
|
|
case Read_BlockBegin: {
|
|
|
|
// The only blocks we care about are subdiagnostics.
|
|
|
|
if (blockOrCode != serialized_diags::BLOCK_DIAG) {
|
|
|
|
if (!Stream.SkipBlock()) {
|
|
|
|
reportInvalidFile("Invalid subblock in Diagnostics block");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
} else if (readDiagnosticBlock(Stream, D->getChildDiagnostics(),
|
|
|
|
TopDiags)) {
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case Read_BlockEnd:
|
2014-03-08 03:33:25 +08:00
|
|
|
Diags.appendDiagnostic(D.release());
|
2011-11-10 16:43:12 +08:00
|
|
|
return Success;
|
|
|
|
case Read_Record:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the record.
|
|
|
|
Record.clear();
|
2013-01-20 10:38:54 +08:00
|
|
|
StringRef Blob;
|
|
|
|
unsigned recID = Stream.readRecord(blockOrCode, Record, &Blob);
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
if (recID < serialized_diags::RECORD_FIRST ||
|
|
|
|
recID > serialized_diags::RECORD_LAST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch ((serialized_diags::RecordIDs)recID) {
|
|
|
|
case serialized_diags::RECORD_VERSION:
|
|
|
|
continue;
|
|
|
|
case serialized_diags::RECORD_CATEGORY:
|
|
|
|
if (readString(TopDiags, TopDiags.Categories, "category", Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
Blob, /* allowEmptyString */ true))
|
2011-11-10 16:43:12 +08:00
|
|
|
return Failure;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case serialized_diags::RECORD_DIAG_FLAG:
|
|
|
|
if (readString(TopDiags, TopDiags.WarningFlags, "warning flag", Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
Blob))
|
2011-11-10 16:43:12 +08:00
|
|
|
return Failure;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case serialized_diags::RECORD_FILENAME: {
|
|
|
|
if (readString(TopDiags, TopDiags.FileNames, "filename", Record,
|
2013-01-20 10:38:54 +08:00
|
|
|
Blob))
|
2011-11-10 16:43:12 +08:00
|
|
|
return Failure;
|
|
|
|
|
|
|
|
if (Record.size() < 3) {
|
|
|
|
reportInvalidFile("Invalid file entry");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileEntry *FE =
|
|
|
|
TopDiags.FakeFiles.getVirtualFile(TopDiags.FileNames[Record[0]],
|
|
|
|
/* size */ Record[1],
|
|
|
|
/* time */ Record[2]);
|
|
|
|
|
|
|
|
TopDiags.Files[Record[0]] = FE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case serialized_diags::RECORD_SOURCE_RANGE: {
|
|
|
|
CXSourceRange SR;
|
|
|
|
if (readRange(TopDiags, Record, 0, SR))
|
|
|
|
return Failure;
|
|
|
|
D->Ranges.push_back(SR);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case serialized_diags::RECORD_FIXIT: {
|
|
|
|
CXSourceRange SR;
|
|
|
|
if (readRange(TopDiags, Record, 0, SR))
|
|
|
|
return Failure;
|
2013-02-19 03:50:38 +08:00
|
|
|
const char *RetStr;
|
2013-01-20 10:38:54 +08:00
|
|
|
if (readString(TopDiags, RetStr, "FIXIT", Record, Blob,
|
2011-11-29 08:30:52 +08:00
|
|
|
/* allowEmptyString */ true))
|
2011-11-10 16:43:12 +08:00
|
|
|
return Failure;
|
2013-02-19 03:50:38 +08:00
|
|
|
D->FixIts.push_back(std::make_pair(SR, RetStr));
|
2011-11-11 23:19:48 +08:00
|
|
|
continue;
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case serialized_diags::RECORD_DIAG: {
|
|
|
|
D->severity = Record[0];
|
|
|
|
unsigned offset = 1;
|
|
|
|
if (readLocation(TopDiags, Record, offset, D->DiagLoc))
|
|
|
|
return Failure;
|
|
|
|
D->category = Record[offset++];
|
|
|
|
unsigned diagFlag = Record[offset++];
|
|
|
|
D->DiagOption = diagFlag ? TopDiags.WarningFlags[diagFlag] : "";
|
2012-04-12 08:03:31 +08:00
|
|
|
D->CategoryText = D->category ? TopDiags.Categories[D->category] : "";
|
2013-02-19 03:50:38 +08:00
|
|
|
D->Spelling = TopDiags.copyString(Blob);
|
2011-11-10 16:43:12 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXDiagnosticSet clang_loadDiagnostics(const char *file,
|
|
|
|
enum CXLoadDiag_Error *error,
|
|
|
|
CXString *errorString) {
|
|
|
|
DiagLoader L(error, errorString);
|
|
|
|
return L.load(file);
|
|
|
|
}
|
|
|
|
} // end extern 'C'.
|