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(SourceLocation Loc,
68 DiagnosticsEngine::Level Level,
70 ArrayRef<CharSourceRange> Ranges,
71 const SourceManager *SM,
72 DiagOrStoredDiag D) override;
74 void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
75 DiagnosticsEngine::Level Level,
76 ArrayRef<CharSourceRange> Ranges,
77 const SourceManager &SM) override {}
79 void emitNote(SourceLocation Loc, StringRef Message,
80 const SourceManager *SM) override;
82 void emitCodeContext(SourceLocation Loc,
83 DiagnosticsEngine::Level Level,
84 SmallVectorImpl<CharSourceRange>& Ranges,
85 ArrayRef<FixItHint> Hints,
86 const SourceManager &SM) override;
88 void beginDiagnostic(DiagOrStoredDiag D,
89 DiagnosticsEngine::Level Level) override;
90 void endDiagnostic(DiagOrStoredDiag D,
91 DiagnosticsEngine::Level Level) override;
94 typedef llvm::DenseMap<unsigned, unsigned> AbbrevLookup;
96 class SDiagsMerger : SerializedDiagnosticReader {
98 AbbrevLookup FileLookup;
99 AbbrevLookup CategoryLookup;
100 AbbrevLookup DiagFlagLookup;
103 SDiagsMerger(SDiagsWriter &Writer)
104 : SerializedDiagnosticReader(), Writer(Writer) {}
106 std::error_code mergeRecordsFromFile(const char *File) {
107 return readDiagnostics(File);
111 std::error_code visitStartOfDiagnostic() override;
112 std::error_code visitEndOfDiagnostic() override;
113 std::error_code visitCategoryRecord(unsigned ID, StringRef Name) override;
114 std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) override;
115 std::error_code visitDiagnosticRecord(
116 unsigned Severity, const serialized_diags::Location &Location,
117 unsigned Category, unsigned Flag, StringRef Message) override;
118 std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
120 StringRef Name) override;
121 std::error_code visitFixitRecord(const serialized_diags::Location &Start,
122 const serialized_diags::Location &End,
123 StringRef CodeToInsert) override;
125 visitSourceRangeRecord(const serialized_diags::Location &Start,
126 const serialized_diags::Location &End) override;
129 std::error_code adjustSourceLocFilename(RecordData &Record,
130 unsigned int offset);
132 void adjustAbbrevID(RecordData &Record, AbbrevLookup &Lookup,
135 void writeRecordWithAbbrev(unsigned ID, RecordData &Record);
137 void writeRecordWithBlob(unsigned ID, RecordData &Record, StringRef Blob);
140 class SDiagsWriter : public DiagnosticConsumer {
141 friend class SDiagsRenderer;
142 friend class SDiagsMerger;
146 explicit SDiagsWriter(std::shared_ptr<SharedState> State)
147 : LangOpts(nullptr), OriginalInstance(false), MergeChildRecords(false),
148 State(std::move(State)) {}
151 SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords)
152 : LangOpts(nullptr), OriginalInstance(true),
153 MergeChildRecords(MergeChildRecords),
154 State(std::make_shared<SharedState>(File, Diags)) {
155 if (MergeChildRecords)
156 RemoveOldDiagnostics();
160 ~SDiagsWriter() override {}
162 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
163 const Diagnostic &Info) override;
165 void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
169 void finish() override;
172 /// \brief Build a DiagnosticsEngine to emit diagnostics about the diagnostics
173 DiagnosticsEngine *getMetaDiags();
175 /// \brief Remove old copies of the serialized diagnostics. This is necessary
176 /// so that we can detect when subprocesses write diagnostics that we should
177 /// merge into our own.
178 void RemoveOldDiagnostics();
180 /// \brief Emit the preamble for the serialized diagnostics.
183 /// \brief Emit the BLOCKINFO block.
184 void EmitBlockInfoBlock();
186 /// \brief Emit the META data block.
187 void EmitMetaBlock();
189 /// \brief Start a DIAG block.
190 void EnterDiagBlock();
192 /// \brief End a DIAG block.
193 void ExitDiagBlock();
195 /// \brief Emit a DIAG record.
196 void EmitDiagnosticMessage(SourceLocation Loc,
198 DiagnosticsEngine::Level Level,
200 const SourceManager *SM,
203 /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic.
204 void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
205 ArrayRef<FixItHint> Hints,
206 const SourceManager &SM);
208 /// \brief Emit a record for a CharSourceRange.
209 void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
211 /// \brief Emit the string information for the category.
212 unsigned getEmitCategory(unsigned category = 0);
214 /// \brief Emit the string information for diagnostic flags.
215 unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
216 unsigned DiagID = 0);
218 unsigned getEmitDiagnosticFlag(StringRef DiagName);
220 /// \brief Emit (lazily) the file string and retrieved the file identifier.
221 unsigned getEmitFile(const char *Filename);
223 /// \brief Add SourceLocation information the specified record.
224 void AddLocToRecord(SourceLocation Loc, const SourceManager *SM,
225 PresumedLoc PLoc, RecordDataImpl &Record,
226 unsigned TokSize = 0);
228 /// \brief Add SourceLocation information the specified record.
229 void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
230 const SourceManager *SM,
231 unsigned TokSize = 0) {
232 AddLocToRecord(Loc, SM, SM ? SM->getPresumedLoc(Loc) : PresumedLoc(),
236 /// \brief Add CharSourceRange information the specified record.
237 void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
238 const SourceManager &SM);
240 /// \brief Language options, which can differ from one clone of this client
242 const LangOptions *LangOpts;
244 /// \brief Whether this is the original instance (rather than one of its
245 /// clones), responsible for writing the file at the end.
246 bool OriginalInstance;
248 /// \brief Whether this instance should aggregate diagnostics that are
249 /// generated from child processes.
250 bool MergeChildRecords;
252 /// \brief State that is shared among the various clones of this diagnostic
255 SharedState(StringRef File, DiagnosticOptions *Diags)
256 : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()),
257 EmittedAnyDiagBlocks(false) {}
259 /// \brief Diagnostic options.
260 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
262 /// \brief The byte buffer for the serialized content.
263 SmallString<1024> Buffer;
265 /// \brief The BitStreamWriter for the serialized diagnostics.
266 llvm::BitstreamWriter Stream;
268 /// \brief The name of the diagnostics file.
269 std::string OutputFile;
271 /// \brief The set of constructed record abbreviations.
272 AbbreviationMap Abbrevs;
274 /// \brief A utility buffer for constructing record content.
277 /// \brief A text buffer for rendering diagnostic text.
278 SmallString<256> diagBuf;
280 /// \brief The collection of diagnostic categories used.
281 llvm::DenseSet<unsigned> Categories;
283 /// \brief The collection of files used.
284 llvm::DenseMap<const char *, unsigned> Files;
286 typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
289 /// \brief Map for uniquing strings.
290 DiagFlagsTy DiagFlags;
292 /// \brief Whether we have already started emission of any DIAG blocks. Once
293 /// this becomes \c true, we never close a DIAG block until we know that we're
294 /// starting another one or we're done.
295 bool EmittedAnyDiagBlocks;
297 /// \brief Engine for emitting diagnostics about the diagnostics.
298 std::unique_ptr<DiagnosticsEngine> MetaDiagnostics;
301 /// \brief State shared among the various clones of this diagnostic consumer.
302 std::shared_ptr<SharedState> State;
304 } // end anonymous namespace
307 namespace serialized_diags {
308 std::unique_ptr<DiagnosticConsumer>
309 create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords) {
310 return llvm::make_unique<SDiagsWriter>(OutputFile, Diags, MergeChildRecords);
313 } // end namespace serialized_diags
314 } // end namespace clang
316 //===----------------------------------------------------------------------===//
317 // Serialization methods.
318 //===----------------------------------------------------------------------===//
320 /// \brief Emits a block ID in the BLOCKINFO block.
321 static void EmitBlockID(unsigned ID, const char *Name,
322 llvm::BitstreamWriter &Stream,
323 RecordDataImpl &Record) {
325 Record.push_back(ID);
326 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
328 // Emit the block name if present.
329 if (!Name || Name[0] == 0)
335 Record.push_back(*Name++);
337 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
340 /// \brief Emits a record ID in the BLOCKINFO block.
341 static void EmitRecordID(unsigned ID, const char *Name,
342 llvm::BitstreamWriter &Stream,
343 RecordDataImpl &Record){
345 Record.push_back(ID);
348 Record.push_back(*Name++);
350 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
353 void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
354 const SourceManager *SM,
356 RecordDataImpl &Record,
358 if (PLoc.isInvalid()) {
359 // Emit a "sentinel" location.
360 Record.push_back((unsigned)0); // File.
361 Record.push_back((unsigned)0); // Line.
362 Record.push_back((unsigned)0); // Column.
363 Record.push_back((unsigned)0); // Offset.
367 Record.push_back(getEmitFile(PLoc.getFilename()));
368 Record.push_back(PLoc.getLine());
369 Record.push_back(PLoc.getColumn()+TokSize);
370 Record.push_back(SM->getFileOffset(Loc));
373 void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
374 RecordDataImpl &Record,
375 const SourceManager &SM) {
376 AddLocToRecord(Range.getBegin(), Record, &SM);
377 unsigned TokSize = 0;
378 if (Range.isTokenRange())
379 TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
382 AddLocToRecord(Range.getEnd(), Record, &SM, TokSize);
385 unsigned SDiagsWriter::getEmitFile(const char *FileName){
389 unsigned &entry = State->Files[FileName];
393 // Lazily generate the record for the file.
394 entry = State->Files.size();
395 StringRef Name(FileName);
396 RecordData::value_type Record[] = {RECORD_FILENAME, entry, 0 /* For legacy */,
397 0 /* For legacy */, Name.size()};
398 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
404 void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
405 const SourceManager &SM) {
406 State->Record.clear();
407 State->Record.push_back(RECORD_SOURCE_RANGE);
408 AddCharSourceRangeToRecord(R, State->Record, SM);
409 State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
413 /// \brief Emits the preamble of the diagnostics file.
414 void SDiagsWriter::EmitPreamble() {
415 // Emit the file header.
416 State->Stream.Emit((unsigned)'D', 8);
417 State->Stream.Emit((unsigned)'I', 8);
418 State->Stream.Emit((unsigned)'A', 8);
419 State->Stream.Emit((unsigned)'G', 8);
421 EmitBlockInfoBlock();
425 static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
426 using namespace llvm;
427 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
428 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
429 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
430 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
433 static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
434 AddSourceLocationAbbrev(Abbrev);
435 AddSourceLocationAbbrev(Abbrev);
438 void SDiagsWriter::EmitBlockInfoBlock() {
439 State->Stream.EnterBlockInfoBlock();
441 using namespace llvm;
442 llvm::BitstreamWriter &Stream = State->Stream;
443 RecordData &Record = State->Record;
444 AbbreviationMap &Abbrevs = State->Abbrevs;
446 // ==---------------------------------------------------------------------==//
447 // The subsequent records and Abbrevs are for the "Meta" block.
448 // ==---------------------------------------------------------------------==//
450 EmitBlockID(BLOCK_META, "Meta", Stream, Record);
451 EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
452 auto Abbrev = std::make_shared<BitCodeAbbrev>();
453 Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
455 Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
457 // ==---------------------------------------------------------------------==//
458 // The subsequent records and Abbrevs are for the "Diagnostic" block.
459 // ==---------------------------------------------------------------------==//
461 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
462 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
463 EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
464 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
465 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
466 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
467 EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
469 // Emit abbreviation for RECORD_DIAG.
470 Abbrev = std::make_shared<BitCodeAbbrev>();
471 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
473 AddSourceLocationAbbrev(*Abbrev);
474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
475 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Text size.
477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
478 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
480 // Emit abbrevation for RECORD_CATEGORY.
481 Abbrev = std::make_shared<BitCodeAbbrev>();
482 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
486 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
488 // Emit abbrevation for RECORD_SOURCE_RANGE.
489 Abbrev = std::make_shared<BitCodeAbbrev>();
490 Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
491 AddRangeLocationAbbrev(*Abbrev);
492 Abbrevs.set(RECORD_SOURCE_RANGE,
493 Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
495 // Emit the abbreviation for RECORD_DIAG_FLAG.
496 Abbrev = std::make_shared<BitCodeAbbrev>();
497 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
501 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
504 // Emit the abbreviation for RECORD_FILENAME.
505 Abbrev = std::make_shared<BitCodeAbbrev>();
506 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
507 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modification time.
510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
512 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
515 // Emit the abbreviation for RECORD_FIXIT.
516 Abbrev = std::make_shared<BitCodeAbbrev>();
517 Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
518 AddRangeLocationAbbrev(*Abbrev);
519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
521 Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
527 void SDiagsWriter::EmitMetaBlock() {
528 llvm::BitstreamWriter &Stream = State->Stream;
529 AbbreviationMap &Abbrevs = State->Abbrevs;
531 Stream.EnterSubblock(BLOCK_META, 3);
532 RecordData::value_type Record[] = {RECORD_VERSION, VersionNumber};
533 Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
537 unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
538 if (!State->Categories.insert(category).second)
541 // We use a local version of 'Record' so that we can be generating
542 // another record when we lazily generate one for the category entry.
543 StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
544 RecordData::value_type Record[] = {RECORD_CATEGORY, category, catName.size()};
545 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
551 unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
553 if (DiagLevel == DiagnosticsEngine::Note)
554 return 0; // No flag for notes.
556 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
557 return getEmitDiagnosticFlag(FlagName);
560 unsigned SDiagsWriter::getEmitDiagnosticFlag(StringRef FlagName) {
561 if (FlagName.empty())
564 // Here we assume that FlagName points to static data whose pointer
565 // value is fixed. This allows us to unique by diagnostic groups.
566 const void *data = FlagName.data();
567 std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
568 if (entry.first == 0) {
569 entry.first = State->DiagFlags.size();
570 entry.second = FlagName;
572 // Lazily emit the string in a separate record.
573 RecordData::value_type Record[] = {RECORD_DIAG_FLAG, entry.first,
575 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
582 void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
583 const Diagnostic &Info) {
584 // Enter the block for a non-note diagnostic immediately, rather than waiting
585 // for beginDiagnostic, in case associated notes are emitted before we get
587 if (DiagLevel != DiagnosticsEngine::Note) {
588 if (State->EmittedAnyDiagBlocks)
592 State->EmittedAnyDiagBlocks = true;
595 // Compute the diagnostic text.
596 State->diagBuf.clear();
597 Info.FormatDiagnostic(State->diagBuf);
599 if (Info.getLocation().isInvalid()) {
600 // Special-case diagnostics with no location. We may not have entered a
601 // source file in this case, so we can't use the normal DiagnosticsRenderer
604 // Make sure we bracket all notes as "sub-diagnostics". This matches
605 // the behavior in SDiagsRenderer::emitDiagnostic().
606 if (DiagLevel == DiagnosticsEngine::Note)
609 EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel,
610 State->diagBuf, nullptr, &Info);
612 if (DiagLevel == DiagnosticsEngine::Note)
618 assert(Info.hasSourceManager() && LangOpts &&
619 "Unexpected diagnostic with valid location outside of a source file");
620 SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
621 Renderer.emitDiagnostic(Info.getLocation(), DiagLevel,
624 Info.getFixItHints(),
625 &Info.getSourceManager(),
629 static serialized_diags::Level getStableLevel(DiagnosticsEngine::Level Level) {
631 #define CASE(X) case DiagnosticsEngine::X: return serialized_diags::X;
641 llvm_unreachable("invalid diagnostic level");
644 void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc,
646 DiagnosticsEngine::Level Level,
648 const SourceManager *SM,
649 DiagOrStoredDiag D) {
650 llvm::BitstreamWriter &Stream = State->Stream;
651 RecordData &Record = State->Record;
652 AbbreviationMap &Abbrevs = State->Abbrevs;
654 // Emit the RECORD_DIAG record.
656 Record.push_back(RECORD_DIAG);
657 Record.push_back(getStableLevel(Level));
658 AddLocToRecord(Loc, SM, PLoc, Record);
660 if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
661 // Emit the category string lazily and get the category ID.
662 unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
663 Record.push_back(getEmitCategory(DiagID));
664 // Emit the diagnostic flag string lazily and get the mapped ID.
665 Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
667 Record.push_back(getEmitCategory());
668 Record.push_back(getEmitDiagnosticFlag(Level));
671 Record.push_back(Message.size());
672 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
676 SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc,
678 DiagnosticsEngine::Level Level,
680 ArrayRef<clang::CharSourceRange> Ranges,
681 const SourceManager *SM,
682 DiagOrStoredDiag D) {
683 Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D);
686 void SDiagsWriter::EnterDiagBlock() {
687 State->Stream.EnterSubblock(BLOCK_DIAG, 4);
690 void SDiagsWriter::ExitDiagBlock() {
691 State->Stream.ExitBlock();
694 void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
695 DiagnosticsEngine::Level Level) {
696 if (Level == DiagnosticsEngine::Note)
697 Writer.EnterDiagBlock();
700 void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
701 DiagnosticsEngine::Level Level) {
702 // Only end note diagnostics here, because we can't be sure when we've seen
703 // the last note associated with a non-note diagnostic.
704 if (Level == DiagnosticsEngine::Note)
705 Writer.ExitDiagBlock();
708 void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
709 ArrayRef<FixItHint> Hints,
710 const SourceManager &SM) {
711 llvm::BitstreamWriter &Stream = State->Stream;
712 RecordData &Record = State->Record;
713 AbbreviationMap &Abbrevs = State->Abbrevs;
715 // Emit Source Ranges.
716 for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
719 EmitCharSourceRange(*I, SM);
722 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
724 const FixItHint &Fix = *I;
728 Record.push_back(RECORD_FIXIT);
729 AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
730 Record.push_back(Fix.CodeToInsert.size());
731 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
736 void SDiagsRenderer::emitCodeContext(SourceLocation Loc,
737 DiagnosticsEngine::Level Level,
738 SmallVectorImpl<CharSourceRange> &Ranges,
739 ArrayRef<FixItHint> Hints,
740 const SourceManager &SM) {
741 Writer.EmitCodeContext(Ranges, Hints, SM);
744 void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message,
745 const SourceManager *SM) {
746 Writer.EnterDiagBlock();
747 PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc();
748 Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note,
749 Message, SM, DiagOrStoredDiag());
750 Writer.ExitDiagBlock();
753 DiagnosticsEngine *SDiagsWriter::getMetaDiags() {
754 // FIXME: It's slightly absurd to create a new diagnostics engine here, but
755 // the other options that are available today are worse:
757 // 1. Teach DiagnosticsConsumers to emit diagnostics to the engine they are a
758 // part of. The DiagnosticsEngine would need to know not to send
759 // diagnostics back to the consumer that failed. This would require us to
760 // rework ChainedDiagnosticsConsumer and teach the engine about multiple
761 // consumers, which is difficult today because most APIs interface with
762 // consumers rather than the engine itself.
764 // 2. Pass a DiagnosticsEngine to SDiagsWriter on creation - this would need
765 // to be distinct from the engine the writer was being added to and would
766 // normally not be used.
767 if (!State->MetaDiagnostics) {
768 IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs());
770 new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get());
771 State->MetaDiagnostics = llvm::make_unique<DiagnosticsEngine>(
772 IDs, State->DiagOpts.get(), Client);
774 return State->MetaDiagnostics.get();
777 void SDiagsWriter::RemoveOldDiagnostics() {
778 if (!llvm::sys::fs::remove(State->OutputFile))
781 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
782 // Disable merging child records, as whatever is in this file may be
784 MergeChildRecords = false;
787 void SDiagsWriter::finish() {
788 // The original instance is responsible for writing the file.
789 if (!OriginalInstance)
792 // Finish off any diagnostic we were in the process of emitting.
793 if (State->EmittedAnyDiagBlocks)
796 if (MergeChildRecords) {
797 if (!State->EmittedAnyDiagBlocks)
798 // We have no diagnostics of our own, so we can just leave the child
799 // process' output alone
802 if (llvm::sys::fs::exists(State->OutputFile))
803 if (SDiagsMerger(*this).mergeRecordsFromFile(State->OutputFile.c_str()))
804 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
808 auto OS = llvm::make_unique<llvm::raw_fd_ostream>(State->OutputFile.c_str(),
809 EC, llvm::sys::fs::F_None);
811 getMetaDiags()->Report(diag::warn_fe_serialized_diag_failure)
812 << State->OutputFile << EC.message();
816 // Write the generated bitstream to "Out".
817 OS->write((char *)&State->Buffer.front(), State->Buffer.size());
821 std::error_code SDiagsMerger::visitStartOfDiagnostic() {
822 Writer.EnterDiagBlock();
823 return std::error_code();
826 std::error_code SDiagsMerger::visitEndOfDiagnostic() {
827 Writer.ExitDiagBlock();
828 return std::error_code();
832 SDiagsMerger::visitSourceRangeRecord(const serialized_diags::Location &Start,
833 const serialized_diags::Location &End) {
834 RecordData::value_type Record[] = {
835 RECORD_SOURCE_RANGE, FileLookup[Start.FileID], Start.Line, Start.Col,
836 Start.Offset, FileLookup[End.FileID], End.Line, End.Col, End.Offset};
837 Writer.State->Stream.EmitRecordWithAbbrev(
838 Writer.State->Abbrevs.get(RECORD_SOURCE_RANGE), Record);
839 return std::error_code();
842 std::error_code SDiagsMerger::visitDiagnosticRecord(
843 unsigned Severity, const serialized_diags::Location &Location,
844 unsigned Category, unsigned Flag, StringRef Message) {
845 RecordData::value_type Record[] = {
846 RECORD_DIAG, Severity, FileLookup[Location.FileID], Location.Line,
847 Location.Col, Location.Offset, CategoryLookup[Category],
848 Flag ? DiagFlagLookup[Flag] : 0, Message.size()};
850 Writer.State->Stream.EmitRecordWithBlob(
851 Writer.State->Abbrevs.get(RECORD_DIAG), Record, Message);
852 return std::error_code();
856 SDiagsMerger::visitFixitRecord(const serialized_diags::Location &Start,
857 const serialized_diags::Location &End,
859 RecordData::value_type Record[] = {RECORD_FIXIT, FileLookup[Start.FileID],
860 Start.Line, Start.Col, Start.Offset,
861 FileLookup[End.FileID], End.Line, End.Col,
862 End.Offset, Text.size()};
864 Writer.State->Stream.EmitRecordWithBlob(
865 Writer.State->Abbrevs.get(RECORD_FIXIT), Record, Text);
866 return std::error_code();
869 std::error_code SDiagsMerger::visitFilenameRecord(unsigned ID, unsigned Size,
872 FileLookup[ID] = Writer.getEmitFile(Name.str().c_str());
873 return std::error_code();
876 std::error_code SDiagsMerger::visitCategoryRecord(unsigned ID, StringRef Name) {
877 CategoryLookup[ID] = Writer.getEmitCategory(ID);
878 return std::error_code();
881 std::error_code SDiagsMerger::visitDiagFlagRecord(unsigned ID, StringRef Name) {
882 DiagFlagLookup[ID] = Writer.getEmitDiagnosticFlag(Name);
883 return std::error_code();