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/FileManager.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Basic/Version.h"
16 #include "clang/Frontend/DiagnosticRenderer.h"
17 #include "clang/Lex/Lexer.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
25 using namespace clang::serialized_diags;
29 class AbbreviationMap {
30 llvm::DenseMap<unsigned, unsigned> Abbrevs;
34 void set(unsigned recordID, unsigned abbrevID) {
35 assert(Abbrevs.find(recordID) == Abbrevs.end()
36 && "Abbreviation already set.");
37 Abbrevs[recordID] = abbrevID;
40 unsigned get(unsigned recordID) {
41 assert(Abbrevs.find(recordID) != Abbrevs.end() &&
42 "Abbreviation not set.");
43 return Abbrevs[recordID];
47 typedef SmallVector<uint64_t, 64> RecordData;
48 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
52 class SDiagsRenderer : public DiagnosticNoteRenderer {
55 SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts,
56 DiagnosticOptions *DiagOpts)
57 : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {}
59 virtual ~SDiagsRenderer() {}
62 virtual void emitDiagnosticMessage(SourceLocation Loc,
64 DiagnosticsEngine::Level Level,
66 ArrayRef<CharSourceRange> Ranges,
67 const SourceManager *SM,
70 virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
71 DiagnosticsEngine::Level Level,
72 ArrayRef<CharSourceRange> Ranges,
73 const SourceManager &SM) {}
75 virtual void emitNote(SourceLocation Loc, StringRef Message,
76 const SourceManager *SM);
78 virtual void emitCodeContext(SourceLocation Loc,
79 DiagnosticsEngine::Level Level,
80 SmallVectorImpl<CharSourceRange>& Ranges,
81 ArrayRef<FixItHint> Hints,
82 const SourceManager &SM);
84 virtual void beginDiagnostic(DiagOrStoredDiag D,
85 DiagnosticsEngine::Level Level);
86 virtual void endDiagnostic(DiagOrStoredDiag D,
87 DiagnosticsEngine::Level Level);
90 class SDiagsWriter : public DiagnosticConsumer {
91 friend class SDiagsRenderer;
95 explicit SDiagsWriter(IntrusiveRefCntPtr<SharedState> State)
96 : LangOpts(0), OriginalInstance(false), State(State) { }
99 SDiagsWriter(raw_ostream *os, DiagnosticOptions *diags)
100 : LangOpts(0), OriginalInstance(true), State(new SharedState(os, diags))
107 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
108 const Diagnostic &Info);
110 void BeginSourceFile(const LangOptions &LO,
111 const Preprocessor *PP) {
115 virtual void finish();
118 /// \brief Emit the preamble for the serialized diagnostics.
121 /// \brief Emit the BLOCKINFO block.
122 void EmitBlockInfoBlock();
124 /// \brief Emit the META data block.
125 void EmitMetaBlock();
127 /// \brief Start a DIAG block.
128 void EnterDiagBlock();
130 /// \brief End a DIAG block.
131 void ExitDiagBlock();
133 /// \brief Emit a DIAG record.
134 void EmitDiagnosticMessage(SourceLocation Loc,
136 DiagnosticsEngine::Level Level,
138 const SourceManager *SM,
141 /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic.
142 void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
143 ArrayRef<FixItHint> Hints,
144 const SourceManager &SM);
146 /// \brief Emit a record for a CharSourceRange.
147 void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
149 /// \brief Emit the string information for the category.
150 unsigned getEmitCategory(unsigned category = 0);
152 /// \brief Emit the string information for diagnostic flags.
153 unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
154 unsigned DiagID = 0);
156 /// \brief Emit (lazily) the file string and retrieved the file identifier.
157 unsigned getEmitFile(const char *Filename);
159 /// \brief Add SourceLocation information the specified record.
160 void AddLocToRecord(SourceLocation Loc, const SourceManager *SM,
161 PresumedLoc PLoc, RecordDataImpl &Record,
162 unsigned TokSize = 0);
164 /// \brief Add SourceLocation information the specified record.
165 void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
166 const SourceManager *SM,
167 unsigned TokSize = 0) {
168 AddLocToRecord(Loc, SM, SM ? SM->getPresumedLoc(Loc) : PresumedLoc(),
172 /// \brief Add CharSourceRange information the specified record.
173 void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
174 const SourceManager &SM);
176 /// \brief The version of the diagnostics file.
177 enum { Version = 1 };
179 /// \brief Language options, which can differ from one clone of this client
181 const LangOptions *LangOpts;
183 /// \brief Whether this is the original instance (rather than one of its
184 /// clones), responsible for writing the file at the end.
185 bool OriginalInstance;
187 /// \brief State that is shared among the various clones of this diagnostic
189 struct SharedState : RefCountedBase<SharedState> {
190 SharedState(raw_ostream *os, DiagnosticOptions *diags)
191 : DiagOpts(diags), Stream(Buffer), OS(os), EmittedAnyDiagBlocks(false) { }
193 /// \brief Diagnostic options.
194 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
196 /// \brief The byte buffer for the serialized content.
197 SmallString<1024> Buffer;
199 /// \brief The BitStreamWriter for the serialized diagnostics.
200 llvm::BitstreamWriter Stream;
202 /// \brief The name of the diagnostics file.
203 OwningPtr<raw_ostream> OS;
205 /// \brief The set of constructed record abbreviations.
206 AbbreviationMap Abbrevs;
208 /// \brief A utility buffer for constructing record content.
211 /// \brief A text buffer for rendering diagnostic text.
212 SmallString<256> diagBuf;
214 /// \brief The collection of diagnostic categories used.
215 llvm::DenseSet<unsigned> Categories;
217 /// \brief The collection of files used.
218 llvm::DenseMap<const char *, unsigned> Files;
220 typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
223 /// \brief Map for uniquing strings.
224 DiagFlagsTy DiagFlags;
226 /// \brief Whether we have already started emission of any DIAG blocks. Once
227 /// this becomes \c true, we never close a DIAG block until we know that we're
228 /// starting another one or we're done.
229 bool EmittedAnyDiagBlocks;
232 /// \brief State shared among the various clones of this diagnostic consumer.
233 IntrusiveRefCntPtr<SharedState> State;
235 } // end anonymous namespace
238 namespace serialized_diags {
239 DiagnosticConsumer *create(raw_ostream *OS, DiagnosticOptions *diags) {
240 return new SDiagsWriter(OS, diags);
242 } // end namespace serialized_diags
243 } // end namespace clang
245 //===----------------------------------------------------------------------===//
246 // Serialization methods.
247 //===----------------------------------------------------------------------===//
249 /// \brief Emits a block ID in the BLOCKINFO block.
250 static void EmitBlockID(unsigned ID, const char *Name,
251 llvm::BitstreamWriter &Stream,
252 RecordDataImpl &Record) {
254 Record.push_back(ID);
255 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
257 // Emit the block name if present.
258 if (Name == 0 || Name[0] == 0)
264 Record.push_back(*Name++);
266 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
269 /// \brief Emits a record ID in the BLOCKINFO block.
270 static void EmitRecordID(unsigned ID, const char *Name,
271 llvm::BitstreamWriter &Stream,
272 RecordDataImpl &Record){
274 Record.push_back(ID);
277 Record.push_back(*Name++);
279 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
282 void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
283 const SourceManager *SM,
285 RecordDataImpl &Record,
287 if (PLoc.isInvalid()) {
288 // Emit a "sentinel" location.
289 Record.push_back((unsigned)0); // File.
290 Record.push_back((unsigned)0); // Line.
291 Record.push_back((unsigned)0); // Column.
292 Record.push_back((unsigned)0); // Offset.
296 Record.push_back(getEmitFile(PLoc.getFilename()));
297 Record.push_back(PLoc.getLine());
298 Record.push_back(PLoc.getColumn()+TokSize);
299 Record.push_back(SM->getFileOffset(Loc));
302 void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
303 RecordDataImpl &Record,
304 const SourceManager &SM) {
305 AddLocToRecord(Range.getBegin(), Record, &SM);
306 unsigned TokSize = 0;
307 if (Range.isTokenRange())
308 TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
311 AddLocToRecord(Range.getEnd(), Record, &SM, TokSize);
314 unsigned SDiagsWriter::getEmitFile(const char *FileName){
318 unsigned &entry = State->Files[FileName];
322 // Lazily generate the record for the file.
323 entry = State->Files.size();
325 Record.push_back(RECORD_FILENAME);
326 Record.push_back(entry);
327 Record.push_back(0); // For legacy.
328 Record.push_back(0); // For legacy.
329 StringRef Name(FileName);
330 Record.push_back(Name.size());
331 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
337 void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
338 const SourceManager &SM) {
339 State->Record.clear();
340 State->Record.push_back(RECORD_SOURCE_RANGE);
341 AddCharSourceRangeToRecord(R, State->Record, SM);
342 State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
346 /// \brief Emits the preamble of the diagnostics file.
347 void SDiagsWriter::EmitPreamble() {
348 // Emit the file header.
349 State->Stream.Emit((unsigned)'D', 8);
350 State->Stream.Emit((unsigned)'I', 8);
351 State->Stream.Emit((unsigned)'A', 8);
352 State->Stream.Emit((unsigned)'G', 8);
354 EmitBlockInfoBlock();
358 static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
359 using namespace llvm;
360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
366 static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
367 AddSourceLocationAbbrev(Abbrev);
368 AddSourceLocationAbbrev(Abbrev);
371 void SDiagsWriter::EmitBlockInfoBlock() {
372 State->Stream.EnterBlockInfoBlock(3);
374 using namespace llvm;
375 llvm::BitstreamWriter &Stream = State->Stream;
376 RecordData &Record = State->Record;
377 AbbreviationMap &Abbrevs = State->Abbrevs;
379 // ==---------------------------------------------------------------------==//
380 // The subsequent records and Abbrevs are for the "Meta" block.
381 // ==---------------------------------------------------------------------==//
383 EmitBlockID(BLOCK_META, "Meta", Stream, Record);
384 EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
385 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
386 Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
388 Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
390 // ==---------------------------------------------------------------------==//
391 // The subsequent records and Abbrevs are for the "Diagnostic" block.
392 // ==---------------------------------------------------------------------==//
394 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
395 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
396 EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
397 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
398 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
399 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
400 EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
402 // Emit abbreviation for RECORD_DIAG.
403 Abbrev = new BitCodeAbbrev();
404 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
406 AddSourceLocationAbbrev(Abbrev);
407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
411 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
413 // Emit abbrevation for RECORD_CATEGORY.
414 Abbrev = new BitCodeAbbrev();
415 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
419 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
421 // Emit abbrevation for RECORD_SOURCE_RANGE.
422 Abbrev = new BitCodeAbbrev();
423 Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
424 AddRangeLocationAbbrev(Abbrev);
425 Abbrevs.set(RECORD_SOURCE_RANGE,
426 Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
428 // Emit the abbreviation for RECORD_DIAG_FLAG.
429 Abbrev = new BitCodeAbbrev();
430 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
434 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
437 // Emit the abbreviation for RECORD_FILENAME.
438 Abbrev = new BitCodeAbbrev();
439 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
440 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time.
443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
445 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
448 // Emit the abbreviation for RECORD_FIXIT.
449 Abbrev = new BitCodeAbbrev();
450 Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
451 AddRangeLocationAbbrev(Abbrev);
452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
454 Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
460 void SDiagsWriter::EmitMetaBlock() {
461 llvm::BitstreamWriter &Stream = State->Stream;
462 RecordData &Record = State->Record;
463 AbbreviationMap &Abbrevs = State->Abbrevs;
465 Stream.EnterSubblock(BLOCK_META, 3);
467 Record.push_back(RECORD_VERSION);
468 Record.push_back(Version);
469 Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
473 unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
474 if (State->Categories.count(category))
477 State->Categories.insert(category);
479 // We use a local version of 'Record' so that we can be generating
480 // another record when we lazily generate one for the category entry.
482 Record.push_back(RECORD_CATEGORY);
483 Record.push_back(category);
484 StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
485 Record.push_back(catName.size());
486 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
492 unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
494 if (DiagLevel == DiagnosticsEngine::Note)
495 return 0; // No flag for notes.
497 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
498 if (FlagName.empty())
501 // Here we assume that FlagName points to static data whose pointer
502 // value is fixed. This allows us to unique by diagnostic groups.
503 const void *data = FlagName.data();
504 std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
505 if (entry.first == 0) {
506 entry.first = State->DiagFlags.size();
507 entry.second = FlagName;
509 // Lazily emit the string in a separate record.
511 Record.push_back(RECORD_DIAG_FLAG);
512 Record.push_back(entry.first);
513 Record.push_back(FlagName.size());
514 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
521 void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
522 const Diagnostic &Info) {
523 // Enter the block for a non-note diagnostic immediately, rather than waiting
524 // for beginDiagnostic, in case associated notes are emitted before we get
526 if (DiagLevel != DiagnosticsEngine::Note) {
527 if (State->EmittedAnyDiagBlocks)
531 State->EmittedAnyDiagBlocks = true;
534 // Compute the diagnostic text.
535 State->diagBuf.clear();
536 Info.FormatDiagnostic(State->diagBuf);
538 if (Info.getLocation().isInvalid()) {
539 // Special-case diagnostics with no location. We may not have entered a
540 // source file in this case, so we can't use the normal DiagnosticsRenderer
543 // Make sure we bracket all notes as "sub-diagnostics". This matches
544 // the behavior in SDiagsRenderer::emitDiagnostic().
545 if (DiagLevel == DiagnosticsEngine::Note)
548 EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel,
549 State->diagBuf, 0, &Info);
551 if (DiagLevel == DiagnosticsEngine::Note)
557 assert(Info.hasSourceManager() && LangOpts &&
558 "Unexpected diagnostic with valid location outside of a source file");
559 SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
560 Renderer.emitDiagnostic(Info.getLocation(), DiagLevel,
561 State->diagBuf.str(),
563 llvm::makeArrayRef(Info.getFixItHints(),
564 Info.getNumFixItHints()),
565 &Info.getSourceManager(),
569 void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc,
571 DiagnosticsEngine::Level Level,
573 const SourceManager *SM,
574 DiagOrStoredDiag D) {
575 llvm::BitstreamWriter &Stream = State->Stream;
576 RecordData &Record = State->Record;
577 AbbreviationMap &Abbrevs = State->Abbrevs;
579 // Emit the RECORD_DIAG record.
581 Record.push_back(RECORD_DIAG);
582 Record.push_back(Level);
583 AddLocToRecord(Loc, SM, PLoc, Record);
585 if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
586 // Emit the category string lazily and get the category ID.
587 unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
588 Record.push_back(getEmitCategory(DiagID));
589 // Emit the diagnostic flag string lazily and get the mapped ID.
590 Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
592 Record.push_back(getEmitCategory());
593 Record.push_back(getEmitDiagnosticFlag(Level));
596 Record.push_back(Message.size());
597 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
601 SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc,
603 DiagnosticsEngine::Level Level,
605 ArrayRef<clang::CharSourceRange> Ranges,
606 const SourceManager *SM,
607 DiagOrStoredDiag D) {
608 Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D);
611 void SDiagsWriter::EnterDiagBlock() {
612 State->Stream.EnterSubblock(BLOCK_DIAG, 4);
615 void SDiagsWriter::ExitDiagBlock() {
616 State->Stream.ExitBlock();
619 void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
620 DiagnosticsEngine::Level Level) {
621 if (Level == DiagnosticsEngine::Note)
622 Writer.EnterDiagBlock();
625 void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
626 DiagnosticsEngine::Level Level) {
627 // Only end note diagnostics here, because we can't be sure when we've seen
628 // the last note associated with a non-note diagnostic.
629 if (Level == DiagnosticsEngine::Note)
630 Writer.ExitDiagBlock();
633 void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
634 ArrayRef<FixItHint> Hints,
635 const SourceManager &SM) {
636 llvm::BitstreamWriter &Stream = State->Stream;
637 RecordData &Record = State->Record;
638 AbbreviationMap &Abbrevs = State->Abbrevs;
640 // Emit Source Ranges.
641 for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
644 EmitCharSourceRange(*I, SM);
647 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
649 const FixItHint &Fix = *I;
653 Record.push_back(RECORD_FIXIT);
654 AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
655 Record.push_back(Fix.CodeToInsert.size());
656 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
661 void SDiagsRenderer::emitCodeContext(SourceLocation Loc,
662 DiagnosticsEngine::Level Level,
663 SmallVectorImpl<CharSourceRange> &Ranges,
664 ArrayRef<FixItHint> Hints,
665 const SourceManager &SM) {
666 Writer.EmitCodeContext(Ranges, Hints, SM);
669 void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message,
670 const SourceManager *SM) {
671 Writer.EnterDiagBlock();
672 PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc();
673 Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note,
674 Message, SM, DiagOrStoredDiag());
675 Writer.ExitDiagBlock();
678 void SDiagsWriter::finish() {
679 // The original instance is responsible for writing the file.
680 if (!OriginalInstance)
683 // Finish off any diagnostic we were in the process of emitting.
684 if (State->EmittedAnyDiagBlocks)
687 // Write the generated bitstream to "Out".
688 State->OS->write((char *)&State->Buffer.front(), State->Buffer.size());