1 //===--- SerializedDiagnosticPrinter.cpp - Serializer for 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/SerializedDiagnosticPrinter.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Frontend/DiagnosticRenderer.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/SerializedDiagnosticReader.h"
17 #include "clang/Frontend/SerializedDiagnostics.h"
18 #include "clang/Frontend/TextDiagnosticPrinter.h"
19 #include "clang/Lex/Lexer.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
28 using namespace clang::serialized_diags;
32 class AbbreviationMap {
33 llvm::DenseMap<unsigned, unsigned> Abbrevs;
37 void set(unsigned recordID, unsigned abbrevID) {
38 assert(Abbrevs.find(recordID) == Abbrevs.end()
39 && "Abbreviation already set.");
40 Abbrevs[recordID] = abbrevID;
43 unsigned get(unsigned recordID) {
44 assert(Abbrevs.find(recordID) != Abbrevs.end() &&
45 "Abbreviation not set.");
46 return Abbrevs[recordID];
50 typedef SmallVector<uint64_t, 64> RecordData;
51 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
52 typedef ArrayRef<uint64_t> RecordDataRef;
56 class SDiagsRenderer : public DiagnosticNoteRenderer {
59 SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts,
60 DiagnosticOptions *DiagOpts)
61 : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {}
63 ~SDiagsRenderer() override {}
66 void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
67 DiagnosticsEngine::Level Level, StringRef Message,
68 ArrayRef<CharSourceRange> Ranges,
69 DiagOrStoredDiag D) override;
71 void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
72 DiagnosticsEngine::Level Level,
73 ArrayRef<CharSourceRange> Ranges) override {}
75 void emitNote(FullSourceLoc Loc, StringRef Message) override;
77 void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
78 SmallVectorImpl<CharSourceRange> &Ranges,
79 ArrayRef<FixItHint> Hints) override;
81 void beginDiagnostic(DiagOrStoredDiag D,
82 DiagnosticsEngine::Level Level) override;
83 void endDiagnostic(DiagOrStoredDiag D,
84 DiagnosticsEngine::Level Level) override;
87 typedef llvm::DenseMap<unsigned, unsigned> AbbrevLookup;
89 class SDiagsMerger : SerializedDiagnosticReader {
91 AbbrevLookup FileLookup;
92 AbbrevLookup CategoryLookup;
93 AbbrevLookup DiagFlagLookup;
96 SDiagsMerger(SDiagsWriter &Writer)
97 : SerializedDiagnosticReader(), Writer(Writer) {}
99 std::error_code mergeRecordsFromFile(const char *File) {
100 return readDiagnostics(File);
104 std::error_code visitStartOfDiagnostic() override;
105 std::error_code visitEndOfDiagnostic() override;
106 std::error_code visitCategoryRecord(unsigned ID, StringRef Name) override;
107 std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) override;
108 std::error_code visitDiagnosticRecord(
109 unsigned Severity, const serialized_diags::Location &Location,
110 unsigned Category, unsigned Flag, StringRef Message) override;
111 std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
113 StringRef Name) override;
114 std::error_code visitFixitRecord(const serialized_diags::Location &Start,
115 const serialized_diags::Location &End,
116 StringRef CodeToInsert) override;
118 visitSourceRangeRecord(const serialized_diags::Location &Start,
119 const serialized_diags::Location &End) override;
122 std::error_code adjustSourceLocFilename(RecordData &Record,
123 unsigned int offset);
125 void adjustAbbrevID(RecordData &Record, AbbrevLookup &Lookup,
128 void writeRecordWithAbbrev(unsigned ID, RecordData &Record);
130 void writeRecordWithBlob(unsigned ID, RecordData &Record, StringRef Blob);
133 class SDiagsWriter : public DiagnosticConsumer {
134 friend class SDiagsRenderer;
135 friend class SDiagsMerger;
139 explicit SDiagsWriter(std::shared_ptr<SharedState> State)
140 : LangOpts(nullptr), OriginalInstance(false), MergeChildRecords(false),
141 State(std::move(State)) {}
144 SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords)
145 : LangOpts(nullptr), OriginalInstance(true),
146 MergeChildRecords(MergeChildRecords),
147 State(std::make_shared<SharedState>(File, Diags)) {
148 if (MergeChildRecords)
149 RemoveOldDiagnostics();
153 ~SDiagsWriter() override {}
155 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
156 const Diagnostic &Info) override;
158 void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
162 void finish() override;
165 /// \brief Build a DiagnosticsEngine to emit diagnostics about the diagnostics
166 DiagnosticsEngine *getMetaDiags();
168 /// \brief Remove old copies of the serialized diagnostics. This is necessary
169 /// so that we can detect when subprocesses write diagnostics that we should
170 /// merge into our own.
171 void RemoveOldDiagnostics();
173 /// \brief Emit the preamble for the serialized diagnostics.
176 /// \brief Emit the BLOCKINFO block.
177 void EmitBlockInfoBlock();
179 /// \brief Emit the META data block.
180 void EmitMetaBlock();
182 /// \brief Start a DIAG block.
183 void EnterDiagBlock();
185 /// \brief End a DIAG block.
186 void ExitDiagBlock();
188 /// \brief Emit a DIAG record.
189 void EmitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
190 DiagnosticsEngine::Level Level, StringRef Message,
193 /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic.
194 void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
195 ArrayRef<FixItHint> Hints,
196 const SourceManager &SM);
198 /// \brief Emit a record for a CharSourceRange.
199 void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
201 /// \brief Emit the string information for the category.
202 unsigned getEmitCategory(unsigned category = 0);
204 /// \brief Emit the string information for diagnostic flags.
205 unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
206 unsigned DiagID = 0);
208 unsigned getEmitDiagnosticFlag(StringRef DiagName);
210 /// \brief Emit (lazily) the file string and retrieved the file identifier.
211 unsigned getEmitFile(const char *Filename);
213 /// \brief Add SourceLocation information the specified record.
214 void AddLocToRecord(FullSourceLoc Loc, PresumedLoc PLoc,
215 RecordDataImpl &Record, unsigned TokSize = 0);
217 /// \brief Add SourceLocation information the specified record.
218 void AddLocToRecord(FullSourceLoc Loc, RecordDataImpl &Record,
219 unsigned TokSize = 0) {
220 AddLocToRecord(Loc, Loc.hasManager() ? Loc.getPresumedLoc() : PresumedLoc(),
224 /// \brief Add CharSourceRange information the specified record.
225 void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
226 const SourceManager &SM);
228 /// \brief Language options, which can differ from one clone of this client
230 const LangOptions *LangOpts;
232 /// \brief Whether this is the original instance (rather than one of its
233 /// clones), responsible for writing the file at the end.
234 bool OriginalInstance;
236 /// \brief Whether this instance should aggregate diagnostics that are
237 /// generated from child processes.
238 bool MergeChildRecords;
240 /// \brief State that is shared among the various clones of this diagnostic
243 SharedState(StringRef File, DiagnosticOptions *Diags)
244 : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()),
245 EmittedAnyDiagBlocks(false) {}
247 /// \brief Diagnostic options.
248 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
250 /// \brief The byte buffer for the serialized content.
251 SmallString<1024> Buffer;
253 /// \brief The BitStreamWriter for the serialized diagnostics.
254 llvm::BitstreamWriter Stream;
256 /// \brief The name of the diagnostics file.
257 std::string OutputFile;
259 /// \brief The set of constructed record abbreviations.
260 AbbreviationMap Abbrevs;
262 /// \brief A utility buffer for constructing record content.
265 /// \brief A text buffer for rendering diagnostic text.
266 SmallString<256> diagBuf;
268 /// \brief The collection of diagnostic categories used.
269 llvm::DenseSet<unsigned> Categories;
271 /// \brief The collection of files used.
272 llvm::DenseMap<const char *, unsigned> Files;
274 typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
277 /// \brief Map for uniquing strings.
278 DiagFlagsTy DiagFlags;
280 /// \brief Whether we have already started emission of any DIAG blocks. Once
281 /// this becomes \c true, we never close a DIAG block until we know that we're
282 /// starting another one or we're done.
283 bool EmittedAnyDiagBlocks;
285 /// \brief Engine for emitting diagnostics about the diagnostics.
286 std::unique_ptr<DiagnosticsEngine> MetaDiagnostics;
289 /// \brief State shared among the various clones of this diagnostic consumer.
290 std::shared_ptr<SharedState> State;
292 } // end anonymous namespace
295 namespace serialized_diags {
296 std::unique_ptr<DiagnosticConsumer>
297 create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords) {
298 return llvm::make_unique<SDiagsWriter>(OutputFile, Diags, MergeChildRecords);
301 } // end namespace serialized_diags
302 } // end namespace clang
304 //===----------------------------------------------------------------------===//
305 // Serialization methods.
306 //===----------------------------------------------------------------------===//
308 /// \brief Emits a block ID in the BLOCKINFO block.
309 static void EmitBlockID(unsigned ID, const char *Name,
310 llvm::BitstreamWriter &Stream,
311 RecordDataImpl &Record) {
313 Record.push_back(ID);
314 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
316 // Emit the block name if present.
317 if (!Name || Name[0] == 0)
323 Record.push_back(*Name++);
325 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
328 /// \brief Emits a record ID in the BLOCKINFO block.
329 static void EmitRecordID(unsigned ID, const char *Name,
330 llvm::BitstreamWriter &Stream,
331 RecordDataImpl &Record){
333 Record.push_back(ID);
336 Record.push_back(*Name++);
338 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
341 void SDiagsWriter::AddLocToRecord(FullSourceLoc Loc, PresumedLoc PLoc,
342 RecordDataImpl &Record, unsigned TokSize) {
343 if (PLoc.isInvalid()) {
344 // Emit a "sentinel" location.
345 Record.push_back((unsigned)0); // File.
346 Record.push_back((unsigned)0); // Line.
347 Record.push_back((unsigned)0); // Column.
348 Record.push_back((unsigned)0); // Offset.
352 Record.push_back(getEmitFile(PLoc.getFilename()));
353 Record.push_back(PLoc.getLine());
354 Record.push_back(PLoc.getColumn()+TokSize);
355 Record.push_back(Loc.getFileOffset());
358 void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
359 RecordDataImpl &Record,
360 const SourceManager &SM) {
361 AddLocToRecord(FullSourceLoc(Range.getBegin(), SM), Record);
362 unsigned TokSize = 0;
363 if (Range.isTokenRange())
364 TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
367 AddLocToRecord(FullSourceLoc(Range.getEnd(), SM), Record, TokSize);
370 unsigned SDiagsWriter::getEmitFile(const char *FileName){
374 unsigned &entry = State->Files[FileName];
378 // Lazily generate the record for the file.
379 entry = State->Files.size();
380 StringRef Name(FileName);
381 RecordData::value_type Record[] = {RECORD_FILENAME, entry, 0 /* For legacy */,
382 0 /* For legacy */, Name.size()};
383 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
389 void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
390 const SourceManager &SM) {
391 State->Record.clear();
392 State->Record.push_back(RECORD_SOURCE_RANGE);
393 AddCharSourceRangeToRecord(R, State->Record, SM);
394 State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
398 /// \brief Emits the preamble of the diagnostics file.
399 void SDiagsWriter::EmitPreamble() {
400 // Emit the file header.
401 State->Stream.Emit((unsigned)'D', 8);
402 State->Stream.Emit((unsigned)'I', 8);
403 State->Stream.Emit((unsigned)'A', 8);
404 State->Stream.Emit((unsigned)'G', 8);
406 EmitBlockInfoBlock();
410 static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
411 using namespace llvm;
412 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
413 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
414 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
415 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
418 static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
419 AddSourceLocationAbbrev(Abbrev);
420 AddSourceLocationAbbrev(Abbrev);
423 void SDiagsWriter::EmitBlockInfoBlock() {
424 State->Stream.EnterBlockInfoBlock();
426 using namespace llvm;
427 llvm::BitstreamWriter &Stream = State->Stream;
428 RecordData &Record = State->Record;
429 AbbreviationMap &Abbrevs = State->Abbrevs;
431 // ==---------------------------------------------------------------------==//
432 // The subsequent records and Abbrevs are for the "Meta" block.
433 // ==---------------------------------------------------------------------==//
435 EmitBlockID(BLOCK_META, "Meta", Stream, Record);
436 EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
437 auto Abbrev = std::make_shared<BitCodeAbbrev>();
438 Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
440 Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
442 // ==---------------------------------------------------------------------==//
443 // The subsequent records and Abbrevs are for the "Diagnostic" block.
444 // ==---------------------------------------------------------------------==//
446 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
447 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
448 EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
449 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
450 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
451 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
452 EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
454 // Emit abbreviation for RECORD_DIAG.
455 Abbrev = std::make_shared<BitCodeAbbrev>();
456 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
458 AddSourceLocationAbbrev(*Abbrev);
459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Text size.
462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
463 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
465 // Emit abbrevation for RECORD_CATEGORY.
466 Abbrev = std::make_shared<BitCodeAbbrev>();
467 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
468 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
471 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
473 // Emit abbrevation for RECORD_SOURCE_RANGE.
474 Abbrev = std::make_shared<BitCodeAbbrev>();
475 Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
476 AddRangeLocationAbbrev(*Abbrev);
477 Abbrevs.set(RECORD_SOURCE_RANGE,
478 Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
480 // Emit the abbreviation for RECORD_DIAG_FLAG.
481 Abbrev = std::make_shared<BitCodeAbbrev>();
482 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
486 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
489 // Emit the abbreviation for RECORD_FILENAME.
490 Abbrev = std::make_shared<BitCodeAbbrev>();
491 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modification time.
495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
497 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
500 // Emit the abbreviation for RECORD_FIXIT.
501 Abbrev = std::make_shared<BitCodeAbbrev>();
502 Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
503 AddRangeLocationAbbrev(*Abbrev);
504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
506 Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
512 void SDiagsWriter::EmitMetaBlock() {
513 llvm::BitstreamWriter &Stream = State->Stream;
514 AbbreviationMap &Abbrevs = State->Abbrevs;
516 Stream.EnterSubblock(BLOCK_META, 3);
517 RecordData::value_type Record[] = {RECORD_VERSION, VersionNumber};
518 Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
522 unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
523 if (!State->Categories.insert(category).second)
526 // We use a local version of 'Record' so that we can be generating
527 // another record when we lazily generate one for the category entry.
528 StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
529 RecordData::value_type Record[] = {RECORD_CATEGORY, category, catName.size()};
530 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
536 unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
538 if (DiagLevel == DiagnosticsEngine::Note)
539 return 0; // No flag for notes.
541 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
542 return getEmitDiagnosticFlag(FlagName);
545 unsigned SDiagsWriter::getEmitDiagnosticFlag(StringRef FlagName) {
546 if (FlagName.empty())
549 // Here we assume that FlagName points to static data whose pointer
550 // value is fixed. This allows us to unique by diagnostic groups.
551 const void *data = FlagName.data();
552 std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
553 if (entry.first == 0) {
554 entry.first = State->DiagFlags.size();
555 entry.second = FlagName;
557 // Lazily emit the string in a separate record.
558 RecordData::value_type Record[] = {RECORD_DIAG_FLAG, entry.first,
560 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
567 void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
568 const Diagnostic &Info) {
569 // Enter the block for a non-note diagnostic immediately, rather than waiting
570 // for beginDiagnostic, in case associated notes are emitted before we get
572 if (DiagLevel != DiagnosticsEngine::Note) {
573 if (State->EmittedAnyDiagBlocks)
577 State->EmittedAnyDiagBlocks = true;
580 // Compute the diagnostic text.
581 State->diagBuf.clear();
582 Info.FormatDiagnostic(State->diagBuf);
584 if (Info.getLocation().isInvalid()) {
585 // Special-case diagnostics with no location. We may not have entered a
586 // source file in this case, so we can't use the normal DiagnosticsRenderer
589 // Make sure we bracket all notes as "sub-diagnostics". This matches
590 // the behavior in SDiagsRenderer::emitDiagnostic().
591 if (DiagLevel == DiagnosticsEngine::Note)
594 EmitDiagnosticMessage(FullSourceLoc(), PresumedLoc(), DiagLevel,
595 State->diagBuf, &Info);
597 if (DiagLevel == DiagnosticsEngine::Note)
603 assert(Info.hasSourceManager() && LangOpts &&
604 "Unexpected diagnostic with valid location outside of a source file");
605 SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
606 Renderer.emitDiagnostic(
607 FullSourceLoc(Info.getLocation(), Info.getSourceManager()), DiagLevel,
608 State->diagBuf, Info.getRanges(), Info.getFixItHints(), &Info);
611 static serialized_diags::Level getStableLevel(DiagnosticsEngine::Level Level) {
613 #define CASE(X) case DiagnosticsEngine::X: return serialized_diags::X;
623 llvm_unreachable("invalid diagnostic level");
626 void SDiagsWriter::EmitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
627 DiagnosticsEngine::Level Level,
629 DiagOrStoredDiag D) {
630 llvm::BitstreamWriter &Stream = State->Stream;
631 RecordData &Record = State->Record;
632 AbbreviationMap &Abbrevs = State->Abbrevs;
634 // Emit the RECORD_DIAG record.
636 Record.push_back(RECORD_DIAG);
637 Record.push_back(getStableLevel(Level));
638 AddLocToRecord(Loc, PLoc, Record);
640 if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
641 // Emit the category string lazily and get the category ID.
642 unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
643 Record.push_back(getEmitCategory(DiagID));
644 // Emit the diagnostic flag string lazily and get the mapped ID.
645 Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
647 Record.push_back(getEmitCategory());
648 Record.push_back(getEmitDiagnosticFlag(Level));
651 Record.push_back(Message.size());
652 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
655 void SDiagsRenderer::emitDiagnosticMessage(
656 FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
657 StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
658 DiagOrStoredDiag D) {
659 Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, D);
662 void SDiagsWriter::EnterDiagBlock() {
663 State->Stream.EnterSubblock(BLOCK_DIAG, 4);
666 void SDiagsWriter::ExitDiagBlock() {
667 State->Stream.ExitBlock();
670 void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
671 DiagnosticsEngine::Level Level) {
672 if (Level == DiagnosticsEngine::Note)
673 Writer.EnterDiagBlock();
676 void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
677 DiagnosticsEngine::Level Level) {
678 // Only end note diagnostics here, because we can't be sure when we've seen
679 // the last note associated with a non-note diagnostic.
680 if (Level == DiagnosticsEngine::Note)
681 Writer.ExitDiagBlock();
684 void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
685 ArrayRef<FixItHint> Hints,
686 const SourceManager &SM) {
687 llvm::BitstreamWriter &Stream = State->Stream;
688 RecordData &Record = State->Record;
689 AbbreviationMap &Abbrevs = State->Abbrevs;
691 // Emit Source Ranges.
692 for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
695 EmitCharSourceRange(*I, SM);
698 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
700 const FixItHint &Fix = *I;
704 Record.push_back(RECORD_FIXIT);
705 AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
706 Record.push_back(Fix.CodeToInsert.size());
707 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
712 void SDiagsRenderer::emitCodeContext(FullSourceLoc Loc,
713 DiagnosticsEngine::Level Level,
714 SmallVectorImpl<CharSourceRange> &Ranges,
715 ArrayRef<FixItHint> Hints) {
716 Writer.EmitCodeContext(Ranges, Hints, Loc.getManager());
719 void SDiagsRenderer::emitNote(FullSourceLoc Loc, StringRef Message) {
720 Writer.EnterDiagBlock();
721 PresumedLoc PLoc = Loc.hasManager() ? Loc.getPresumedLoc() : PresumedLoc();
722 Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note, Message,
724 Writer.ExitDiagBlock();
727 DiagnosticsEngine *SDiagsWriter::getMetaDiags() {
728 // FIXME: It's slightly absurd to create a new diagnostics engine here, but
729 // the other options that are available today are worse:
731 // 1. Teach DiagnosticsConsumers to emit diagnostics to the engine they are a
732 // part of. The DiagnosticsEngine would need to know not to send
733 // diagnostics back to the consumer that failed. This would require us to
734 // rework ChainedDiagnosticsConsumer and teach the engine about multiple
735 // consumers, which is difficult today because most APIs interface with
736 // consumers rather than the engine itself.
738 // 2. Pass a DiagnosticsEngine to SDiagsWriter on creation - this would need
739 // to be distinct from the engine the writer was being added to and would
740 // normally not be used.
741 if (!State->MetaDiagnostics) {
742 IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs());
744 new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get());
745 State->MetaDiagnostics = llvm::make_unique<DiagnosticsEngine>(
746 IDs, State->DiagOpts.get(), Client);
748 return State->MetaDiagnostics.get();
751 void SDiagsWriter::RemoveOldDiagnostics() {
752 if (!llvm::sys::fs::remove(State->OutputFile))
755 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
756 // Disable merging child records, as whatever is in this file may be
758 MergeChildRecords = false;
761 void SDiagsWriter::finish() {
762 // The original instance is responsible for writing the file.
763 if (!OriginalInstance)
766 // Finish off any diagnostic we were in the process of emitting.
767 if (State->EmittedAnyDiagBlocks)
770 if (MergeChildRecords) {
771 if (!State->EmittedAnyDiagBlocks)
772 // We have no diagnostics of our own, so we can just leave the child
773 // process' output alone
776 if (llvm::sys::fs::exists(State->OutputFile))
777 if (SDiagsMerger(*this).mergeRecordsFromFile(State->OutputFile.c_str()))
778 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
782 auto OS = llvm::make_unique<llvm::raw_fd_ostream>(State->OutputFile.c_str(),
783 EC, llvm::sys::fs::F_None);
785 getMetaDiags()->Report(diag::warn_fe_serialized_diag_failure)
786 << State->OutputFile << EC.message();
790 // Write the generated bitstream to "Out".
791 OS->write((char *)&State->Buffer.front(), State->Buffer.size());
795 std::error_code SDiagsMerger::visitStartOfDiagnostic() {
796 Writer.EnterDiagBlock();
797 return std::error_code();
800 std::error_code SDiagsMerger::visitEndOfDiagnostic() {
801 Writer.ExitDiagBlock();
802 return std::error_code();
806 SDiagsMerger::visitSourceRangeRecord(const serialized_diags::Location &Start,
807 const serialized_diags::Location &End) {
808 RecordData::value_type Record[] = {
809 RECORD_SOURCE_RANGE, FileLookup[Start.FileID], Start.Line, Start.Col,
810 Start.Offset, FileLookup[End.FileID], End.Line, End.Col, End.Offset};
811 Writer.State->Stream.EmitRecordWithAbbrev(
812 Writer.State->Abbrevs.get(RECORD_SOURCE_RANGE), Record);
813 return std::error_code();
816 std::error_code SDiagsMerger::visitDiagnosticRecord(
817 unsigned Severity, const serialized_diags::Location &Location,
818 unsigned Category, unsigned Flag, StringRef Message) {
819 RecordData::value_type Record[] = {
820 RECORD_DIAG, Severity, FileLookup[Location.FileID], Location.Line,
821 Location.Col, Location.Offset, CategoryLookup[Category],
822 Flag ? DiagFlagLookup[Flag] : 0, Message.size()};
824 Writer.State->Stream.EmitRecordWithBlob(
825 Writer.State->Abbrevs.get(RECORD_DIAG), Record, Message);
826 return std::error_code();
830 SDiagsMerger::visitFixitRecord(const serialized_diags::Location &Start,
831 const serialized_diags::Location &End,
833 RecordData::value_type Record[] = {RECORD_FIXIT, FileLookup[Start.FileID],
834 Start.Line, Start.Col, Start.Offset,
835 FileLookup[End.FileID], End.Line, End.Col,
836 End.Offset, Text.size()};
838 Writer.State->Stream.EmitRecordWithBlob(
839 Writer.State->Abbrevs.get(RECORD_FIXIT), Record, Text);
840 return std::error_code();
843 std::error_code SDiagsMerger::visitFilenameRecord(unsigned ID, unsigned Size,
846 FileLookup[ID] = Writer.getEmitFile(Name.str().c_str());
847 return std::error_code();
850 std::error_code SDiagsMerger::visitCategoryRecord(unsigned ID, StringRef Name) {
851 CategoryLookup[ID] = Writer.getEmitCategory(ID);
852 return std::error_code();
855 std::error_code SDiagsMerger::visitDiagFlagRecord(unsigned ID, StringRef Name) {
856 DiagFlagLookup[ID] = Writer.getEmitDiagnosticFlag(Name);
857 return std::error_code();