1 /*===-- CXLoadedDiagnostic.cpp - Handling of persisent diags -*- C++ -*-===*\
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 |* Implements handling of persisent diagnostics. *|
12 \*===----------------------------------------------------------------------===*/
14 #include "CXLoadedDiagnostic.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Frontend/SerializedDiagnosticPrinter.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/ADT/Optional.h"
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Bitcode/BitstreamReader.h"
25 #include "llvm/Support/MemoryBuffer.h"
28 using namespace clang;
29 using namespace clang::cxstring;
31 //===----------------------------------------------------------------------===//
32 // Extend CXDiagnosticSetImpl which contains strings for diagnostics.
33 //===----------------------------------------------------------------------===//
35 typedef llvm::DenseMap<unsigned, llvm::StringRef> Strings;
38 class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {
40 CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {}
41 virtual ~CXLoadedDiagnosticSetImpl() {}
43 llvm::StringRef makeString(const char *blob, unsigned blobLen);
45 llvm::BumpPtrAllocator Alloc;
51 FileManager FakeFiles;
52 llvm::DenseMap<unsigned, const FileEntry *> Files;
56 llvm::StringRef CXLoadedDiagnosticSetImpl::makeString(const char *blob,
58 char *mem = Alloc.Allocate<char>(bloblen + 1);
59 memcpy(mem, blob, bloblen);
60 // Add a null terminator for those clients accessing the buffer
63 return llvm::StringRef(mem, bloblen);
66 //===----------------------------------------------------------------------===//
68 //===----------------------------------------------------------------------===//
70 CXLoadedDiagnostic::~CXLoadedDiagnostic() {}
72 //===----------------------------------------------------------------------===//
73 // Public CXLoadedDiagnostic methods.
74 //===----------------------------------------------------------------------===//
76 CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const {
77 // FIXME: possibly refactor with logic in CXStoredDiagnostic.
79 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
80 case DiagnosticsEngine::Note: return CXDiagnostic_Note;
81 case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
82 case DiagnosticsEngine::Error: return CXDiagnostic_Error;
83 case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal;
86 llvm_unreachable("Invalid diagnostic level");
89 static CXSourceLocation makeLocation(const CXLoadedDiagnostic::Location *DLoc) {
90 // The lowest bit of ptr_data[0] is always set to 1 to indicate this
91 // is a persistent diagnostic.
92 uintptr_t V = (uintptr_t) DLoc;
94 CXSourceLocation Loc = { { (void*) V, 0 }, 0 };
98 CXSourceLocation CXLoadedDiagnostic::getLocation() const {
99 // The lowest bit of ptr_data[0] is always set to 1 to indicate this
100 // is a persistent diagnostic.
101 return makeLocation(&DiagLoc);
104 CXString CXLoadedDiagnostic::getSpelling() const {
105 return cxstring::createCXString(Spelling, false);
108 CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const {
109 if (DiagOption.empty())
110 return createCXString("");
112 // FIXME: possibly refactor with logic in CXStoredDiagnostic.
114 *Disable = createCXString((Twine("-Wno-") + DiagOption).str());
115 return createCXString((Twine("-W") + DiagOption).str());
118 unsigned CXLoadedDiagnostic::getCategory() const {
122 CXString CXLoadedDiagnostic::getCategoryText() const {
123 return cxstring::createCXString(CategoryText);
126 unsigned CXLoadedDiagnostic::getNumRanges() const {
127 return Ranges.size();
130 CXSourceRange CXLoadedDiagnostic::getRange(unsigned Range) const {
131 assert(Range < Ranges.size());
132 return Ranges[Range];
135 unsigned CXLoadedDiagnostic::getNumFixIts() const {
136 return FixIts.size();
139 CXString CXLoadedDiagnostic::getFixIt(unsigned FixIt,
140 CXSourceRange *ReplacementRange) const {
141 assert(FixIt < FixIts.size());
142 if (ReplacementRange)
143 *ReplacementRange = FixIts[FixIt].first;
144 return FixIts[FixIt].second;
147 void CXLoadedDiagnostic::decodeLocation(CXSourceLocation location,
150 unsigned int *column,
151 unsigned int *offset) {
154 // CXSourceLocation consists of the following fields:
156 // void *ptr_data[2];
157 // unsigned int_data;
159 // The lowest bit of ptr_data[0] is always set to 1 to indicate this
160 // is a persistent diagnostic.
162 // For now, do the unoptimized approach and store the data in a side
163 // data structure. We can optimize this case later.
165 uintptr_t V = (uintptr_t) location.ptr_data[0];
166 assert((V & 0x1) == 1);
169 const Location &Loc = *((Location*)V);
176 *column = Loc.column;
178 *offset = Loc.offset;
181 //===----------------------------------------------------------------------===//
182 // Deserialize diagnostics.
183 //===----------------------------------------------------------------------===//
185 enum { MaxSupportedVersion = 1 };
186 typedef SmallVector<uint64_t, 64> RecordData;
187 enum LoadResult { Failure = 1, Success = 0 };
188 enum StreamResult { Read_EndOfStream,
196 enum CXLoadDiag_Error *error;
197 CXString *errorString;
199 void reportBad(enum CXLoadDiag_Error code, llvm::StringRef err) {
203 *errorString = createCXString(err);
206 void reportInvalidFile(llvm::StringRef err) {
207 return reportBad(CXLoadDiag_InvalidFile, err);
210 LoadResult readMetaBlock(llvm::BitstreamCursor &Stream);
212 LoadResult readDiagnosticBlock(llvm::BitstreamCursor &Stream,
213 CXDiagnosticSetImpl &Diags,
214 CXLoadedDiagnosticSetImpl &TopDiags);
216 StreamResult readToNextRecordOrBlock(llvm::BitstreamCursor &Stream,
217 llvm::StringRef errorContext,
218 unsigned &BlockOrRecordID,
219 const bool atTopLevel = false);
222 LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,
223 Strings &strings, llvm::StringRef errorContext,
225 const char *BlobStart,
227 bool allowEmptyString = false);
229 LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,
230 llvm::StringRef &RetStr,
231 llvm::StringRef errorContext,
233 const char *BlobStart,
235 bool allowEmptyString = false);
237 LoadResult readRange(CXLoadedDiagnosticSetImpl &TopDiags,
238 RecordData &Record, unsigned RecStartIdx,
241 LoadResult readLocation(CXLoadedDiagnosticSetImpl &TopDiags,
242 RecordData &Record, unsigned &offset,
243 CXLoadedDiagnostic::Location &Loc);
246 DiagLoader(enum CXLoadDiag_Error *e, CXString *es)
247 : error(e), errorString(es) {
249 *error = CXLoadDiag_None;
251 *errorString = createCXString("");
254 CXDiagnosticSet load(const char *file);
258 CXDiagnosticSet DiagLoader::load(const char *file) {
259 // Open the diagnostics file.
261 FileSystemOptions FO;
262 FileManager FileMgr(FO);
264 OwningPtr<llvm::MemoryBuffer> Buffer;
265 Buffer.reset(FileMgr.getBufferForFile(file));
268 reportBad(CXLoadDiag_CannotLoad, ErrStr);
272 llvm::BitstreamReader StreamFile;
273 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
274 (const unsigned char *)Buffer->getBufferEnd());
276 llvm::BitstreamCursor Stream;
277 Stream.init(StreamFile);
279 // Sniff for the signature.
280 if (Stream.Read(8) != 'D' ||
281 Stream.Read(8) != 'I' ||
282 Stream.Read(8) != 'A' ||
283 Stream.Read(8) != 'G') {
284 reportBad(CXLoadDiag_InvalidFile,
285 "Bad header in diagnostics file");
289 OwningPtr<CXLoadedDiagnosticSetImpl>
290 Diags(new CXLoadedDiagnosticSetImpl());
293 unsigned BlockID = 0;
294 StreamResult Res = readToNextRecordOrBlock(Stream, "Top-level",
297 case Read_EndOfStream:
298 return (CXDiagnosticSet) Diags.take();
302 llvm_unreachable("Top-level does not have records");
305 case Read_BlockBegin:
310 case serialized_diags::BLOCK_META:
311 if (readMetaBlock(Stream))
314 case serialized_diags::BLOCK_DIAG:
315 if (readDiagnosticBlock(Stream, *Diags.get(), *Diags.get()))
319 if (!Stream.SkipBlock()) {
320 reportInvalidFile("Malformed block at top-level of diagnostics file");
328 StreamResult DiagLoader::readToNextRecordOrBlock(llvm::BitstreamCursor &Stream,
329 llvm::StringRef errorContext,
330 unsigned &blockOrRecordID,
331 const bool atTopLevel) {
335 while (!Stream.AtEndOfStream()) {
336 unsigned Code = Stream.ReadCode();
338 // Handle the top-level specially.
340 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
341 unsigned BlockID = Stream.ReadSubBlockID();
342 if (BlockID == llvm::bitc::BLOCKINFO_BLOCK_ID) {
343 if (Stream.ReadBlockInfoBlock()) {
344 reportInvalidFile("Malformed BlockInfoBlock in diagnostics file");
349 blockOrRecordID = BlockID;
350 return Read_BlockBegin;
352 reportInvalidFile("Only blocks can appear at the top of a "
357 switch ((llvm::bitc::FixedAbbrevIDs)Code) {
358 case llvm::bitc::ENTER_SUBBLOCK:
359 blockOrRecordID = Stream.ReadSubBlockID();
360 return Read_BlockBegin;
362 case llvm::bitc::END_BLOCK:
363 if (Stream.ReadBlockEnd()) {
364 reportInvalidFile("Cannot read end of block");
367 return Read_BlockEnd;
369 case llvm::bitc::DEFINE_ABBREV:
370 Stream.ReadAbbrevRecord();
373 case llvm::bitc::UNABBREV_RECORD:
374 reportInvalidFile("Diagnostics file should have no unabbreviated "
379 // We found a record.
380 blockOrRecordID = Code;
386 return Read_EndOfStream;
388 reportInvalidFile(Twine("Premature end of diagnostics file within ").str() +
393 LoadResult DiagLoader::readMetaBlock(llvm::BitstreamCursor &Stream) {
394 if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META)) {
395 reportInvalidFile("Malformed metadata block");
399 bool versionChecked = false;
402 unsigned blockOrCode = 0;
403 StreamResult Res = readToNextRecordOrBlock(Stream, "Metadata Block",
407 case Read_EndOfStream:
408 llvm_unreachable("EndOfStream handled by readToNextRecordOrBlock");
413 case Read_BlockBegin:
414 if (Stream.SkipBlock()) {
415 reportInvalidFile("Malformed metadata block");
419 if (!versionChecked) {
420 reportInvalidFile("Diagnostics file does not contain version"
430 unsigned recordID = Stream.ReadRecord(blockOrCode, Record, &Blob, &BlobLen);
432 if (recordID == serialized_diags::RECORD_VERSION) {
433 if (Record.size() < 1) {
434 reportInvalidFile("malformed VERSION identifier in diagnostics file");
437 if (Record[0] > MaxSupportedVersion) {
438 reportInvalidFile("diagnosics file is a newer version than the one "
442 versionChecked = true;
447 LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,
448 llvm::StringRef &RetStr,
449 llvm::StringRef errorContext,
451 const char *BlobStart,
453 bool allowEmptyString) {
455 // Basic buffer overflow check.
456 if (BlobLen > 65536) {
457 reportInvalidFile(std::string("Out-of-bounds string in ") +
458 std::string(errorContext));
462 if (allowEmptyString && Record.size() >= 1 && BlobLen == 0) {
467 if (Record.size() < 1 || BlobLen == 0) {
468 reportInvalidFile(std::string("Corrupted ") + std::string(errorContext)
469 + std::string(" entry"));
473 RetStr = TopDiags.makeString(BlobStart, BlobLen);
477 LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,
479 llvm::StringRef errorContext,
481 const char *BlobStart,
483 bool allowEmptyString) {
484 llvm::StringRef RetStr;
485 if (readString(TopDiags, RetStr, errorContext, Record, BlobStart, BlobLen,
488 strings[Record[0]] = RetStr;
492 LoadResult DiagLoader::readLocation(CXLoadedDiagnosticSetImpl &TopDiags,
493 RecordData &Record, unsigned &offset,
494 CXLoadedDiagnostic::Location &Loc) {
495 if (Record.size() < offset + 3) {
496 reportInvalidFile("Corrupted source location");
500 unsigned fileID = Record[offset++];
510 const FileEntry *FE = TopDiags.Files[fileID];
512 reportInvalidFile("Corrupted file entry in source location");
515 Loc.file = (void*) FE;
516 Loc.line = Record[offset++];
517 Loc.column = Record[offset++];
518 Loc.offset = Record[offset++];
522 LoadResult DiagLoader::readRange(CXLoadedDiagnosticSetImpl &TopDiags,
524 unsigned int RecStartIdx,
526 CXLoadedDiagnostic::Location *Start, *End;
527 Start = TopDiags.Alloc.Allocate<CXLoadedDiagnostic::Location>();
528 End = TopDiags.Alloc.Allocate<CXLoadedDiagnostic::Location>();
530 if (readLocation(TopDiags, Record, RecStartIdx, *Start))
532 if (readLocation(TopDiags, Record, RecStartIdx, *End))
535 CXSourceLocation startLoc = makeLocation(Start);
536 CXSourceLocation endLoc = makeLocation(End);
537 SR = clang_getRange(startLoc, endLoc);
541 LoadResult DiagLoader::readDiagnosticBlock(llvm::BitstreamCursor &Stream,
542 CXDiagnosticSetImpl &Diags,
543 CXLoadedDiagnosticSetImpl &TopDiags){
545 if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG)) {
546 reportInvalidFile("malformed diagnostic block");
550 OwningPtr<CXLoadedDiagnostic> D(new CXLoadedDiagnostic());
554 unsigned blockOrCode = 0;
555 StreamResult Res = readToNextRecordOrBlock(Stream, "Diagnostic Block",
558 case Read_EndOfStream:
559 llvm_unreachable("EndOfStream handled in readToNextRecordOrBlock");
562 case Read_BlockBegin: {
563 // The only blocks we care about are subdiagnostics.
564 if (blockOrCode != serialized_diags::BLOCK_DIAG) {
565 if (!Stream.SkipBlock()) {
566 reportInvalidFile("Invalid subblock in Diagnostics block");
569 } else if (readDiagnosticBlock(Stream, D->getChildDiagnostics(),
577 Diags.appendDiagnostic(D.take());
585 const char *BlobStart = 0;
586 unsigned BlobLen = 0;
587 unsigned recID = Stream.ReadRecord(blockOrCode, Record,
590 if (recID < serialized_diags::RECORD_FIRST ||
591 recID > serialized_diags::RECORD_LAST)
594 switch ((serialized_diags::RecordIDs)recID) {
595 case serialized_diags::RECORD_VERSION:
597 case serialized_diags::RECORD_CATEGORY:
598 if (readString(TopDiags, TopDiags.Categories, "category", Record,
600 /* allowEmptyString */ true))
604 case serialized_diags::RECORD_DIAG_FLAG:
605 if (readString(TopDiags, TopDiags.WarningFlags, "warning flag", Record,
610 case serialized_diags::RECORD_FILENAME: {
611 if (readString(TopDiags, TopDiags.FileNames, "filename", Record,
615 if (Record.size() < 3) {
616 reportInvalidFile("Invalid file entry");
620 const FileEntry *FE =
621 TopDiags.FakeFiles.getVirtualFile(TopDiags.FileNames[Record[0]],
622 /* size */ Record[1],
623 /* time */ Record[2]);
625 TopDiags.Files[Record[0]] = FE;
629 case serialized_diags::RECORD_SOURCE_RANGE: {
631 if (readRange(TopDiags, Record, 0, SR))
633 D->Ranges.push_back(SR);
637 case serialized_diags::RECORD_FIXIT: {
639 if (readRange(TopDiags, Record, 0, SR))
641 llvm::StringRef RetStr;
642 if (readString(TopDiags, RetStr, "FIXIT", Record, BlobStart, BlobLen,
643 /* allowEmptyString */ true))
645 D->FixIts.push_back(std::make_pair(SR, createCXString(RetStr, false)));
649 case serialized_diags::RECORD_DIAG: {
650 D->severity = Record[0];
652 if (readLocation(TopDiags, Record, offset, D->DiagLoc))
654 D->category = Record[offset++];
655 unsigned diagFlag = Record[offset++];
656 D->DiagOption = diagFlag ? TopDiags.WarningFlags[diagFlag] : "";
657 D->CategoryText = D->category ? TopDiags.Categories[D->category] : "";
658 D->Spelling = TopDiags.makeString(BlobStart, BlobLen);
666 CXDiagnosticSet clang_loadDiagnostics(const char *file,
667 enum CXLoadDiag_Error *error,
668 CXString *errorString) {
669 DiagLoader L(error, errorString);