2011-10-29 08:12:39 +08:00
|
|
|
//===--- SerializedDiagnosticPrinter.cpp - Serializer for diagnostics -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/Version.h"
|
2011-11-10 16:43:12 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2011-10-29 08:12:39 +08:00
|
|
|
#include "clang/Frontend/SerializedDiagnosticPrinter.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2011-11-05 08:09:57 +08:00
|
|
|
using namespace clang::serialized_diags;
|
2011-10-29 08:12:39 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AbbreviationMap {
|
|
|
|
llvm::DenseMap<unsigned, unsigned> Abbrevs;
|
|
|
|
public:
|
|
|
|
AbbreviationMap() {}
|
|
|
|
|
|
|
|
void set(unsigned recordID, unsigned abbrevID) {
|
|
|
|
assert(Abbrevs.find(recordID) == Abbrevs.end()
|
|
|
|
&& "Abbreviation already set.");
|
|
|
|
Abbrevs[recordID] = abbrevID;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get(unsigned recordID) {
|
|
|
|
assert(Abbrevs.find(recordID) != Abbrevs.end() &&
|
|
|
|
"Abbreviation not set.");
|
|
|
|
return Abbrevs[recordID];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::SmallVector<uint64_t, 64> RecordData;
|
|
|
|
typedef llvm::SmallVectorImpl<uint64_t> RecordDataImpl;
|
|
|
|
|
|
|
|
class SDiagsWriter : public DiagnosticConsumer {
|
|
|
|
public:
|
2011-12-07 13:52:12 +08:00
|
|
|
explicit SDiagsWriter(llvm::raw_ostream *os)
|
|
|
|
: LangOpts(0), Stream(Buffer), OS(os), inNonNoteDiagnostic(false)
|
2011-10-29 08:12:39 +08:00
|
|
|
{
|
|
|
|
EmitPreamble();
|
2011-11-15 09:30:40 +08:00
|
|
|
}
|
2011-10-29 08:12:39 +08:00
|
|
|
|
|
|
|
~SDiagsWriter() {}
|
|
|
|
|
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const Diagnostic &Info);
|
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
void BeginSourceFile(const LangOptions &LO,
|
|
|
|
const Preprocessor *PP) {
|
|
|
|
LangOpts = &LO;
|
|
|
|
}
|
2011-12-07 13:52:12 +08:00
|
|
|
|
|
|
|
virtual void finish();
|
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
|
|
|
|
// It makes no sense to clone this.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief Emit the preamble for the serialized diagnostics.
|
|
|
|
void EmitPreamble();
|
|
|
|
|
|
|
|
/// \brief Emit the BLOCKINFO block.
|
|
|
|
void EmitBlockInfoBlock();
|
2011-11-05 08:10:11 +08:00
|
|
|
|
2011-11-09 04:27:29 +08:00
|
|
|
/// \brief Emit the META data block.
|
|
|
|
void EmitMetaBlock();
|
|
|
|
|
2011-11-05 08:10:01 +08:00
|
|
|
/// \brief Emit a record for a CharSourceRange.
|
2011-12-07 13:52:12 +08:00
|
|
|
void EmitCharSourceRange(CharSourceRange R, SourceManager &SM);
|
2011-11-05 08:10:01 +08:00
|
|
|
|
2011-11-05 08:10:07 +08:00
|
|
|
/// \brief Emit the string information for the category for a diagnostic.
|
|
|
|
unsigned getEmitCategory(unsigned DiagID);
|
|
|
|
|
|
|
|
/// \brief Emit the string information for diagnostic flags.
|
|
|
|
unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const Diagnostic &Info);
|
2011-11-05 08:10:04 +08:00
|
|
|
|
2011-11-05 08:10:11 +08:00
|
|
|
/// \brief Emit (lazily) the file string and retrieved the file identifier.
|
2011-12-07 13:52:12 +08:00
|
|
|
unsigned getEmitFile(SourceLocation Loc, SourceManager &SM);
|
2011-11-05 08:10:11 +08:00
|
|
|
|
|
|
|
/// \brief Add SourceLocation information the specified record.
|
2011-11-10 16:43:12 +08:00
|
|
|
void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
|
2011-12-07 13:52:12 +08:00
|
|
|
SourceManager &SM,
|
2011-11-10 16:43:12 +08:00
|
|
|
unsigned TokSize = 0);
|
2011-11-05 08:10:11 +08:00
|
|
|
|
2011-11-05 11:34:23 +08:00
|
|
|
/// \brief Add CharSourceRange information the specified record.
|
2011-12-07 13:52:12 +08:00
|
|
|
void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
|
|
|
|
SourceManager &SM);
|
2011-11-05 11:34:23 +08:00
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
/// \brief The version of the diagnostics file.
|
|
|
|
enum { Version = 1 };
|
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
const LangOptions *LangOpts;
|
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
/// \brief The byte buffer for the serialized content.
|
|
|
|
std::vector<unsigned char> Buffer;
|
|
|
|
|
|
|
|
/// \brief The BitStreamWriter for the serialized diagnostics.
|
|
|
|
llvm::BitstreamWriter Stream;
|
|
|
|
|
|
|
|
/// \brief The name of the diagnostics file.
|
|
|
|
llvm::OwningPtr<llvm::raw_ostream> OS;
|
|
|
|
|
|
|
|
/// \brief The set of constructed record abbreviations.
|
|
|
|
AbbreviationMap Abbrevs;
|
|
|
|
|
|
|
|
/// \brief A utility buffer for constructing record content.
|
|
|
|
RecordData Record;
|
|
|
|
|
|
|
|
/// \brief A text buffer for rendering diagnostic text.
|
|
|
|
llvm::SmallString<256> diagBuf;
|
|
|
|
|
|
|
|
/// \brief The collection of diagnostic categories used.
|
|
|
|
llvm::DenseSet<unsigned> Categories;
|
|
|
|
|
|
|
|
/// \brief The collection of files used.
|
2011-11-05 08:10:11 +08:00
|
|
|
llvm::DenseMap<const FileEntry *, unsigned> Files;
|
2011-11-05 08:09:50 +08:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<const void *, std::pair<unsigned, llvm::StringRef> >
|
|
|
|
DiagFlagsTy;
|
|
|
|
|
|
|
|
/// \brief Map for uniquing strings.
|
|
|
|
DiagFlagsTy DiagFlags;
|
2011-10-29 08:12:39 +08:00
|
|
|
|
2011-11-05 08:09:47 +08:00
|
|
|
/// \brief Flag indicating whether or not we are in the process of
|
|
|
|
/// emitting a non-note diagnostic.
|
|
|
|
bool inNonNoteDiagnostic;
|
2011-10-29 08:12:39 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace serialized_diags {
|
2011-12-07 13:52:12 +08:00
|
|
|
DiagnosticConsumer *create(llvm::raw_ostream *OS) {
|
|
|
|
return new SDiagsWriter(OS);
|
2011-10-29 08:12:39 +08:00
|
|
|
}
|
|
|
|
} // end namespace serialized_diags
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Serialization methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// \brief Emits a block ID in the BLOCKINFO block.
|
|
|
|
static void EmitBlockID(unsigned ID, const char *Name,
|
|
|
|
llvm::BitstreamWriter &Stream,
|
|
|
|
RecordDataImpl &Record) {
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(ID);
|
|
|
|
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
|
|
|
|
|
|
|
|
// Emit the block name if present.
|
|
|
|
if (Name == 0 || Name[0] == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Record.clear();
|
|
|
|
|
|
|
|
while (*Name)
|
|
|
|
Record.push_back(*Name++);
|
|
|
|
|
|
|
|
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Emits a record ID in the BLOCKINFO block.
|
|
|
|
static void EmitRecordID(unsigned ID, const char *Name,
|
|
|
|
llvm::BitstreamWriter &Stream,
|
|
|
|
RecordDataImpl &Record){
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(ID);
|
|
|
|
|
|
|
|
while (*Name)
|
|
|
|
Record.push_back(*Name++);
|
|
|
|
|
|
|
|
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
|
|
|
|
}
|
|
|
|
|
2011-11-05 08:10:11 +08:00
|
|
|
void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
|
2011-11-10 16:43:12 +08:00
|
|
|
RecordDataImpl &Record,
|
2011-12-07 13:52:12 +08:00
|
|
|
SourceManager &SM,
|
2011-11-10 16:43:12 +08:00
|
|
|
unsigned TokSize) {
|
2011-11-05 08:10:01 +08:00
|
|
|
if (Loc.isInvalid()) {
|
|
|
|
// Emit a "sentinel" location.
|
2011-11-10 16:43:12 +08:00
|
|
|
Record.push_back((unsigned)0); // File.
|
|
|
|
Record.push_back((unsigned)0); // Line.
|
|
|
|
Record.push_back((unsigned)0); // Column.
|
|
|
|
Record.push_back((unsigned)0); // Offset.
|
2011-11-05 08:10:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loc = SM.getSpellingLoc(Loc);
|
2011-12-07 13:52:12 +08:00
|
|
|
Record.push_back(getEmitFile(Loc, SM));
|
2011-11-05 08:10:01 +08:00
|
|
|
Record.push_back(SM.getSpellingLineNumber(Loc));
|
2011-11-10 16:43:12 +08:00
|
|
|
Record.push_back(SM.getSpellingColumnNumber(Loc)+TokSize);
|
2011-11-10 19:29:20 +08:00
|
|
|
Record.push_back(SM.getFileOffset(Loc));
|
2011-11-05 08:10:01 +08:00
|
|
|
}
|
|
|
|
|
2011-11-05 11:34:23 +08:00
|
|
|
void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
|
2011-12-07 13:52:12 +08:00
|
|
|
RecordDataImpl &Record,
|
|
|
|
SourceManager &SM) {
|
|
|
|
AddLocToRecord(Range.getBegin(), Record, SM);
|
2011-11-10 16:43:12 +08:00
|
|
|
unsigned TokSize = 0;
|
|
|
|
if (Range.isTokenRange())
|
|
|
|
TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
|
2011-12-07 13:52:12 +08:00
|
|
|
SM, *LangOpts);
|
2011-11-10 16:43:12 +08:00
|
|
|
|
2011-12-07 13:52:12 +08:00
|
|
|
AddLocToRecord(Range.getEnd(), Record, SM, TokSize);
|
2011-11-05 11:34:23 +08:00
|
|
|
}
|
|
|
|
|
2011-12-07 13:52:12 +08:00
|
|
|
unsigned SDiagsWriter::getEmitFile(SourceLocation Loc, SourceManager &SM) {
|
2011-11-05 08:10:11 +08:00
|
|
|
assert(Loc.isValid());
|
|
|
|
const std::pair<FileID, unsigned> &LocInfo = SM.getDecomposedLoc(Loc);
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(LocInfo.first);
|
|
|
|
if (!FE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned &entry = Files[FE];
|
|
|
|
if (entry)
|
|
|
|
return entry;
|
|
|
|
|
|
|
|
// Lazily generate the record for the file.
|
|
|
|
entry = Files.size();
|
|
|
|
RecordData Record;
|
|
|
|
Record.push_back(RECORD_FILENAME);
|
|
|
|
Record.push_back(entry);
|
|
|
|
Record.push_back(FE->getSize());
|
|
|
|
Record.push_back(FE->getModificationTime());
|
|
|
|
StringRef Name = FE->getName();
|
|
|
|
Record.push_back(Name.size());
|
|
|
|
Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FILENAME), Record, Name);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:52:12 +08:00
|
|
|
void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
|
|
|
|
SourceManager &SM) {
|
2011-11-05 08:10:01 +08:00
|
|
|
Record.clear();
|
|
|
|
Record.push_back(RECORD_SOURCE_RANGE);
|
2011-12-07 13:52:12 +08:00
|
|
|
AddCharSourceRangeToRecord(R, Record, SM);
|
2011-11-05 08:10:01 +08:00
|
|
|
Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_SOURCE_RANGE), Record);
|
|
|
|
}
|
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
/// \brief Emits the preamble of the diagnostics file.
|
|
|
|
void SDiagsWriter::EmitPreamble() {
|
|
|
|
// Emit the file header.
|
|
|
|
Stream.Emit((unsigned)'D', 8);
|
2011-11-09 04:27:29 +08:00
|
|
|
Stream.Emit((unsigned)'I', 8);
|
|
|
|
Stream.Emit((unsigned)'A', 8);
|
|
|
|
Stream.Emit((unsigned)'G', 8);
|
2011-11-05 08:09:53 +08:00
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
EmitBlockInfoBlock();
|
2011-11-09 04:27:29 +08:00
|
|
|
EmitMetaBlock();
|
2011-10-29 08:12:39 +08:00
|
|
|
}
|
|
|
|
|
2011-11-05 08:10:01 +08:00
|
|
|
static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
|
|
|
|
using namespace llvm;
|
2011-11-05 08:10:11 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
|
2011-11-05 08:10:01 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
|
|
|
|
}
|
2011-11-05 11:34:23 +08:00
|
|
|
|
|
|
|
static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
|
|
|
|
AddSourceLocationAbbrev(Abbrev);
|
|
|
|
AddSourceLocationAbbrev(Abbrev);
|
|
|
|
}
|
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
void SDiagsWriter::EmitBlockInfoBlock() {
|
|
|
|
Stream.EnterBlockInfoBlock(3);
|
2011-11-09 04:27:29 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// ==---------------------------------------------------------------------==//
|
|
|
|
// The subsequent records and Abbrevs are for the "Meta" block.
|
|
|
|
// ==---------------------------------------------------------------------==//
|
|
|
|
|
|
|
|
EmitBlockID(BLOCK_META, "Meta", Stream, Record);
|
|
|
|
EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
|
|
|
|
BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
|
|
|
|
Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
|
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
// ==---------------------------------------------------------------------==//
|
|
|
|
// The subsequent records and Abbrevs are for the "Diagnostic" block.
|
|
|
|
// ==---------------------------------------------------------------------==//
|
|
|
|
|
2011-11-05 08:09:50 +08:00
|
|
|
EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
|
|
|
|
EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
|
2011-11-05 08:10:01 +08:00
|
|
|
EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
|
2011-11-05 08:10:04 +08:00
|
|
|
EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
|
2011-11-05 08:10:07 +08:00
|
|
|
EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
|
2011-11-05 08:10:11 +08:00
|
|
|
EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
|
2011-11-05 11:34:23 +08:00
|
|
|
EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
|
2011-11-05 08:10:07 +08:00
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
// Emit abbreviation for RECORD_DIAG.
|
2011-11-09 04:27:29 +08:00
|
|
|
Abbrev = new BitCodeAbbrev();
|
2011-10-29 08:12:39 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
|
2011-11-05 08:09:50 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
|
2011-11-05 08:10:01 +08:00
|
|
|
AddSourceLocationAbbrev(Abbrev);
|
2011-11-05 08:09:50 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
|
2011-10-29 08:12:39 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
|
|
|
|
Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
|
2011-11-05 08:10:01 +08:00
|
|
|
|
2011-11-05 08:10:04 +08:00
|
|
|
// Emit abbrevation for RECORD_CATEGORY.
|
|
|
|
Abbrev = new BitCodeAbbrev();
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
|
|
|
|
Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
|
|
|
|
|
2011-11-05 08:10:01 +08:00
|
|
|
// Emit abbrevation for RECORD_SOURCE_RANGE.
|
|
|
|
Abbrev = new BitCodeAbbrev();
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
|
2011-11-05 11:34:23 +08:00
|
|
|
AddRangeLocationAbbrev(Abbrev);
|
2011-11-05 08:10:01 +08:00
|
|
|
Abbrevs.set(RECORD_SOURCE_RANGE,
|
|
|
|
Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
|
2011-11-05 08:10:07 +08:00
|
|
|
|
|
|
|
// Emit the abbreviation for RECORD_DIAG_FLAG.
|
|
|
|
Abbrev = new BitCodeAbbrev();
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
|
|
|
|
Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
|
|
|
|
Abbrev));
|
2011-11-05 08:10:11 +08:00
|
|
|
|
|
|
|
// Emit the abbreviation for RECORD_FILENAME.
|
2011-10-29 08:12:39 +08:00
|
|
|
Abbrev = new BitCodeAbbrev();
|
2011-11-05 08:09:43 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
|
2011-11-05 08:10:11 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time.
|
2011-10-29 08:12:39 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
|
2011-11-05 08:10:11 +08:00
|
|
|
Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
|
2011-10-29 08:12:39 +08:00
|
|
|
Abbrev));
|
2011-11-05 11:34:23 +08:00
|
|
|
|
|
|
|
// Emit the abbreviation for RECORD_FIXIT.
|
|
|
|
Abbrev = new BitCodeAbbrev();
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
|
|
|
|
AddRangeLocationAbbrev(Abbrev);
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
|
|
|
|
Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
|
|
|
|
Abbrev));
|
2011-10-29 08:12:39 +08:00
|
|
|
|
|
|
|
Stream.ExitBlock();
|
|
|
|
}
|
|
|
|
|
2011-11-09 04:27:29 +08:00
|
|
|
void SDiagsWriter::EmitMetaBlock() {
|
|
|
|
Stream.EnterSubblock(BLOCK_META, 3);
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(RECORD_VERSION);
|
|
|
|
Record.push_back(Version);
|
|
|
|
Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
|
|
|
|
Stream.ExitBlock();
|
|
|
|
}
|
|
|
|
|
2011-11-05 08:10:07 +08:00
|
|
|
unsigned SDiagsWriter::getEmitCategory(unsigned int DiagID) {
|
|
|
|
unsigned category = DiagnosticIDs::getCategoryNumberForDiag(DiagID);
|
|
|
|
|
|
|
|
if (Categories.count(category))
|
|
|
|
return category;
|
2011-11-05 08:10:04 +08:00
|
|
|
|
2011-11-05 08:10:07 +08:00
|
|
|
Categories.insert(category);
|
2011-11-05 08:10:04 +08:00
|
|
|
|
|
|
|
// We use a local version of 'Record' so that we can be generating
|
|
|
|
// another record when we lazily generate one for the category entry.
|
|
|
|
RecordData Record;
|
|
|
|
Record.push_back(RECORD_CATEGORY);
|
2011-11-05 08:10:07 +08:00
|
|
|
Record.push_back(category);
|
|
|
|
StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
|
2011-11-05 08:10:04 +08:00
|
|
|
Record.push_back(catName.size());
|
|
|
|
Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_CATEGORY), Record, catName);
|
2011-11-05 08:10:07 +08:00
|
|
|
|
|
|
|
return category;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const Diagnostic &Info) {
|
|
|
|
if (DiagLevel == DiagnosticsEngine::Note)
|
|
|
|
return 0; // No flag for notes.
|
|
|
|
|
|
|
|
StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
|
|
|
|
if (FlagName.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Here we assume that FlagName points to static data whose pointer
|
|
|
|
// value is fixed. This allows us to unique by diagnostic groups.
|
|
|
|
const void *data = FlagName.data();
|
|
|
|
std::pair<unsigned, StringRef> &entry = DiagFlags[data];
|
|
|
|
if (entry.first == 0) {
|
|
|
|
entry.first = DiagFlags.size();
|
|
|
|
entry.second = FlagName;
|
|
|
|
|
|
|
|
// Lazily emit the string in a separate record.
|
|
|
|
RecordData Record;
|
|
|
|
Record.push_back(RECORD_DIAG_FLAG);
|
|
|
|
Record.push_back(entry.first);
|
|
|
|
Record.push_back(FlagName.size());
|
|
|
|
Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG_FLAG),
|
|
|
|
Record, FlagName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.first;
|
2011-11-05 08:10:04 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const Diagnostic &Info) {
|
2011-12-07 13:52:12 +08:00
|
|
|
SourceManager &SM = Info.getSourceManager();
|
2011-10-29 08:12:39 +08:00
|
|
|
|
2011-11-05 08:09:47 +08:00
|
|
|
if (DiagLevel != DiagnosticsEngine::Note) {
|
|
|
|
if (inNonNoteDiagnostic) {
|
|
|
|
// We have encountered a non-note diagnostic. Finish up the previous
|
|
|
|
// diagnostic block before starting a new one.
|
|
|
|
Stream.ExitBlock();
|
|
|
|
}
|
|
|
|
inNonNoteDiagnostic = true;
|
|
|
|
}
|
|
|
|
|
2011-11-05 08:10:11 +08:00
|
|
|
Stream.EnterSubblock(BLOCK_DIAG, 4);
|
2011-10-29 08:12:39 +08:00
|
|
|
|
|
|
|
// Emit the RECORD_DIAG record.
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(RECORD_DIAG);
|
|
|
|
Record.push_back(DiagLevel);
|
2011-12-07 13:52:12 +08:00
|
|
|
AddLocToRecord(Info.getLocation(), Record, SM);
|
2011-11-05 08:10:07 +08:00
|
|
|
// Emit the category string lazily and get the category ID.
|
|
|
|
Record.push_back(getEmitCategory(Info.getID()));
|
|
|
|
// Emit the diagnostic flag string lazily and get the mapped ID.
|
|
|
|
Record.push_back(getEmitDiagnosticFlag(DiagLevel, Info));
|
2011-11-05 08:10:11 +08:00
|
|
|
// Emit the diagnostic text.
|
2011-10-29 08:12:39 +08:00
|
|
|
diagBuf.clear();
|
|
|
|
Info.FormatDiagnostic(diagBuf); // Compute the diagnostic text.
|
|
|
|
Record.push_back(diagBuf.str().size());
|
|
|
|
Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, diagBuf.str());
|
2011-11-05 11:34:23 +08:00
|
|
|
|
|
|
|
// Emit Source Ranges.
|
2011-11-05 08:10:01 +08:00
|
|
|
ArrayRef<CharSourceRange> Ranges = Info.getRanges();
|
|
|
|
for (ArrayRef<CharSourceRange>::iterator it=Ranges.begin(), ei=Ranges.end();
|
|
|
|
it != ei; ++it) {
|
2011-12-07 13:52:12 +08:00
|
|
|
if (it->isValid())
|
|
|
|
EmitCharSourceRange(*it, SM);
|
2011-11-05 08:10:01 +08:00
|
|
|
}
|
|
|
|
|
2011-11-05 11:34:23 +08:00
|
|
|
// Emit FixIts.
|
|
|
|
for (unsigned i = 0, n = Info.getNumFixItHints(); i != n; ++i) {
|
|
|
|
const FixItHint &fix = Info.getFixItHint(i);
|
|
|
|
if (fix.isNull())
|
|
|
|
continue;
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(RECORD_FIXIT);
|
2011-12-07 13:52:12 +08:00
|
|
|
AddCharSourceRangeToRecord(fix.RemoveRange, Record, SM);
|
2011-11-05 11:34:23 +08:00
|
|
|
Record.push_back(fix.CodeToInsert.size());
|
|
|
|
Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
|
|
|
|
fix.CodeToInsert);
|
|
|
|
}
|
2011-11-05 08:09:47 +08:00
|
|
|
|
|
|
|
if (DiagLevel == DiagnosticsEngine::Note) {
|
|
|
|
// Notes currently cannot have child diagnostics. Complete the
|
|
|
|
// diagnostic now.
|
|
|
|
Stream.ExitBlock();
|
|
|
|
}
|
2011-10-29 08:12:39 +08:00
|
|
|
}
|
|
|
|
|
2011-12-07 13:52:12 +08:00
|
|
|
void SDiagsWriter::finish() {
|
2011-11-05 08:09:47 +08:00
|
|
|
if (inNonNoteDiagnostic) {
|
|
|
|
// Finish off any diagnostics we were in the process of emitting.
|
|
|
|
Stream.ExitBlock();
|
|
|
|
inNonNoteDiagnostic = false;
|
|
|
|
}
|
2011-11-05 08:10:11 +08:00
|
|
|
|
2011-10-29 08:12:39 +08:00
|
|
|
// Write the generated bitstream to "Out".
|
|
|
|
OS->write((char *)&Buffer.front(), Buffer.size());
|
|
|
|
OS->flush();
|
|
|
|
|
|
|
|
OS.reset(0);
|
|
|
|
}
|
|
|
|
|