]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Frontend / SerializedDiagnosticPrinter.cpp
1 //===--- SerializedDiagnosticPrinter.cpp - Serializer for diagnostics -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <vector>
11 #include "llvm/Support/raw_ostream.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/Version.h"
20 #include "clang/Lex/Lexer.h"
21 #include "clang/Frontend/SerializedDiagnosticPrinter.h"
22 #include "clang/Frontend/DiagnosticRenderer.h"
23
24 using namespace clang;
25 using namespace clang::serialized_diags;
26
27 namespace {
28   
29 class AbbreviationMap {
30   llvm::DenseMap<unsigned, unsigned> Abbrevs;
31 public:
32   AbbreviationMap() {}
33   
34   void set(unsigned recordID, unsigned abbrevID) {
35     assert(Abbrevs.find(recordID) == Abbrevs.end() 
36            && "Abbreviation already set.");
37     Abbrevs[recordID] = abbrevID;
38   }
39   
40   unsigned get(unsigned recordID) {
41     assert(Abbrevs.find(recordID) != Abbrevs.end() &&
42            "Abbreviation not set.");
43     return Abbrevs[recordID];
44   }
45 };
46  
47 typedef llvm::SmallVector<uint64_t, 64> RecordData;
48 typedef llvm::SmallVectorImpl<uint64_t> RecordDataImpl;
49
50 class SDiagsWriter;
51   
52 class SDiagsRenderer : public DiagnosticNoteRenderer {
53   SDiagsWriter &Writer;
54 public:
55   SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts,
56                  DiagnosticOptions *DiagOpts)
57     : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {}
58
59   virtual ~SDiagsRenderer() {}
60   
61 protected:
62   virtual void emitDiagnosticMessage(SourceLocation Loc,
63                                      PresumedLoc PLoc,
64                                      DiagnosticsEngine::Level Level,
65                                      StringRef Message,
66                                      ArrayRef<CharSourceRange> Ranges,
67                                      const SourceManager *SM,
68                                      DiagOrStoredDiag D);
69   
70   virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
71                                  DiagnosticsEngine::Level Level,
72                                  ArrayRef<CharSourceRange> Ranges,
73                                  const SourceManager &SM) {}
74
75   virtual void emitNote(SourceLocation Loc, StringRef Message,
76                         const SourceManager *SM);
77
78   virtual void emitCodeContext(SourceLocation Loc,
79                                DiagnosticsEngine::Level Level,
80                                SmallVectorImpl<CharSourceRange>& Ranges,
81                                ArrayRef<FixItHint> Hints,
82                                const SourceManager &SM);
83
84   virtual void beginDiagnostic(DiagOrStoredDiag D,
85                                DiagnosticsEngine::Level Level);
86   virtual void endDiagnostic(DiagOrStoredDiag D,
87                              DiagnosticsEngine::Level Level);
88 };
89   
90 class SDiagsWriter : public DiagnosticConsumer {
91   friend class SDiagsRenderer;
92 public:  
93   explicit SDiagsWriter(llvm::raw_ostream *os, DiagnosticOptions *diags)
94     : LangOpts(0), DiagOpts(diags), Stream(Buffer), OS(os),
95       EmittedAnyDiagBlocks(false) {
96     EmitPreamble();
97   }
98
99   ~SDiagsWriter() {}
100   
101   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
102                         const Diagnostic &Info);
103   
104   void BeginSourceFile(const LangOptions &LO,
105                        const Preprocessor *PP) {
106     LangOpts = &LO;
107   }
108
109   virtual void finish();
110
111   DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
112     // It makes no sense to clone this.
113     return 0;
114   }
115
116 private:
117   /// \brief Emit the preamble for the serialized diagnostics.
118   void EmitPreamble();
119   
120   /// \brief Emit the BLOCKINFO block.
121   void EmitBlockInfoBlock();
122
123   /// \brief Emit the META data block.
124   void EmitMetaBlock();
125
126   /// \brief Start a DIAG block.
127   void EnterDiagBlock();
128
129   /// \brief End a DIAG block.
130   void ExitDiagBlock();
131
132   /// \brief Emit a DIAG record.
133   void EmitDiagnosticMessage(SourceLocation Loc,
134                              PresumedLoc PLoc,
135                              DiagnosticsEngine::Level Level,
136                              StringRef Message,
137                              const SourceManager *SM,
138                              DiagOrStoredDiag D);
139
140   /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic.
141   void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
142                        ArrayRef<FixItHint> Hints,
143                        const SourceManager &SM);
144
145   /// \brief Emit a record for a CharSourceRange.
146   void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
147   
148   /// \brief Emit the string information for the category.
149   unsigned getEmitCategory(unsigned category = 0);
150   
151   /// \brief Emit the string information for diagnostic flags.
152   unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
153                                  unsigned DiagID = 0);
154   
155   /// \brief Emit (lazily) the file string and retrieved the file identifier.
156   unsigned getEmitFile(const char *Filename);
157
158   /// \brief Add SourceLocation information the specified record.  
159   void AddLocToRecord(SourceLocation Loc, const SourceManager *SM,
160                       PresumedLoc PLoc, RecordDataImpl &Record,
161                       unsigned TokSize = 0);
162
163   /// \brief Add SourceLocation information the specified record.
164   void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
165                       const SourceManager *SM,
166                       unsigned TokSize = 0) {
167     AddLocToRecord(Loc, SM, SM ? SM->getPresumedLoc(Loc) : PresumedLoc(),
168                    Record, TokSize);
169   }
170
171   /// \brief Add CharSourceRange information the specified record.
172   void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
173                                   const SourceManager &SM);
174
175   /// \brief The version of the diagnostics file.
176   enum { Version = 1 };
177
178   const LangOptions *LangOpts;
179   llvm::IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
180   
181   /// \brief The byte buffer for the serialized content.
182   SmallString<1024> Buffer;
183
184   /// \brief The BitStreamWriter for the serialized diagnostics.
185   llvm::BitstreamWriter Stream;
186
187   /// \brief The name of the diagnostics file.
188   OwningPtr<llvm::raw_ostream> OS;
189   
190   /// \brief The set of constructed record abbreviations.
191   AbbreviationMap Abbrevs;
192
193   /// \brief A utility buffer for constructing record content.
194   RecordData Record;
195
196   /// \brief A text buffer for rendering diagnostic text.
197   SmallString<256> diagBuf;
198   
199   /// \brief The collection of diagnostic categories used.
200   llvm::DenseSet<unsigned> Categories;
201   
202   /// \brief The collection of files used.
203   llvm::DenseMap<const char *, unsigned> Files;
204
205   typedef llvm::DenseMap<const void *, std::pair<unsigned, llvm::StringRef> > 
206           DiagFlagsTy;
207
208   /// \brief Map for uniquing strings.
209   DiagFlagsTy DiagFlags;
210
211   /// \brief Whether we have already started emission of any DIAG blocks. Once
212   /// this becomes \c true, we never close a DIAG block until we know that we're
213   /// starting another one or we're done.
214   bool EmittedAnyDiagBlocks;
215 };
216 } // end anonymous namespace
217
218 namespace clang {
219 namespace serialized_diags {
220 DiagnosticConsumer *create(llvm::raw_ostream *OS,
221                            DiagnosticOptions *diags) {
222   return new SDiagsWriter(OS, diags);
223 }
224 } // end namespace serialized_diags
225 } // end namespace clang
226
227 //===----------------------------------------------------------------------===//
228 // Serialization methods.
229 //===----------------------------------------------------------------------===//
230
231 /// \brief Emits a block ID in the BLOCKINFO block.
232 static void EmitBlockID(unsigned ID, const char *Name,
233                         llvm::BitstreamWriter &Stream,
234                         RecordDataImpl &Record) {
235   Record.clear();
236   Record.push_back(ID);
237   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
238   
239   // Emit the block name if present.
240   if (Name == 0 || Name[0] == 0)
241     return;
242
243   Record.clear();
244
245   while (*Name)
246     Record.push_back(*Name++);
247
248   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
249 }
250
251 /// \brief Emits a record ID in the BLOCKINFO block.
252 static void EmitRecordID(unsigned ID, const char *Name,
253                          llvm::BitstreamWriter &Stream,
254                          RecordDataImpl &Record){
255   Record.clear();
256   Record.push_back(ID);
257
258   while (*Name)
259     Record.push_back(*Name++);
260
261   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
262 }
263
264 void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
265                                   const SourceManager *SM,
266                                   PresumedLoc PLoc,
267                                   RecordDataImpl &Record,
268                                   unsigned TokSize) {
269   if (PLoc.isInvalid()) {
270     // Emit a "sentinel" location.
271     Record.push_back((unsigned)0); // File.
272     Record.push_back((unsigned)0); // Line.
273     Record.push_back((unsigned)0); // Column.
274     Record.push_back((unsigned)0); // Offset.
275     return;
276   }
277
278   Record.push_back(getEmitFile(PLoc.getFilename()));
279   Record.push_back(PLoc.getLine());
280   Record.push_back(PLoc.getColumn()+TokSize);
281   Record.push_back(SM->getFileOffset(Loc));
282 }
283
284 void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
285                                               RecordDataImpl &Record,
286                                               const SourceManager &SM) {
287   AddLocToRecord(Range.getBegin(), Record, &SM);
288   unsigned TokSize = 0;
289   if (Range.isTokenRange())
290     TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
291                                         SM, *LangOpts);
292   
293   AddLocToRecord(Range.getEnd(), Record, &SM, TokSize);
294 }
295
296 unsigned SDiagsWriter::getEmitFile(const char *FileName){
297   if (!FileName)
298     return 0;
299   
300   unsigned &entry = Files[FileName];
301   if (entry)
302     return entry;
303   
304   // Lazily generate the record for the file.
305   entry = Files.size();
306   RecordData Record;
307   Record.push_back(RECORD_FILENAME);
308   Record.push_back(entry);
309   Record.push_back(0); // For legacy.
310   Record.push_back(0); // For legacy.
311   StringRef Name(FileName);
312   Record.push_back(Name.size());
313   Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FILENAME), Record, Name);
314
315   return entry;
316 }
317
318 void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
319                                        const SourceManager &SM) {
320   Record.clear();
321   Record.push_back(RECORD_SOURCE_RANGE);
322   AddCharSourceRangeToRecord(R, Record, SM);
323   Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_SOURCE_RANGE), Record);
324 }
325
326 /// \brief Emits the preamble of the diagnostics file.
327 void SDiagsWriter::EmitPreamble() {
328   // Emit the file header.
329   Stream.Emit((unsigned)'D', 8);
330   Stream.Emit((unsigned)'I', 8);
331   Stream.Emit((unsigned)'A', 8);
332   Stream.Emit((unsigned)'G', 8);
333
334   EmitBlockInfoBlock();
335   EmitMetaBlock();
336 }
337
338 static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
339   using namespace llvm;
340   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
341   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
342   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
343   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
344 }
345
346 static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
347   AddSourceLocationAbbrev(Abbrev);
348   AddSourceLocationAbbrev(Abbrev);  
349 }
350
351 void SDiagsWriter::EmitBlockInfoBlock() {
352   Stream.EnterBlockInfoBlock(3);
353
354   using namespace llvm;
355
356   // ==---------------------------------------------------------------------==//
357   // The subsequent records and Abbrevs are for the "Meta" block.
358   // ==---------------------------------------------------------------------==//
359
360   EmitBlockID(BLOCK_META, "Meta", Stream, Record);
361   EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
362   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
363   Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
364   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
365   Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
366
367   // ==---------------------------------------------------------------------==//
368   // The subsequent records and Abbrevs are for the "Diagnostic" block.
369   // ==---------------------------------------------------------------------==//
370
371   EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
372   EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
373   EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
374   EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
375   EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
376   EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
377   EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
378
379   // Emit abbreviation for RECORD_DIAG.
380   Abbrev = new BitCodeAbbrev();
381   Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
382   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));  // Diag level.
383   AddSourceLocationAbbrev(Abbrev);
384   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.  
385   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
386   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
387   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
388   Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
389   
390   // Emit abbrevation for RECORD_CATEGORY.
391   Abbrev = new BitCodeAbbrev();
392   Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
393   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
394   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));  // Text size.
395   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));      // Category text.
396   Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
397
398   // Emit abbrevation for RECORD_SOURCE_RANGE.
399   Abbrev = new BitCodeAbbrev();
400   Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
401   AddRangeLocationAbbrev(Abbrev);
402   Abbrevs.set(RECORD_SOURCE_RANGE,
403               Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
404   
405   // Emit the abbreviation for RECORD_DIAG_FLAG.
406   Abbrev = new BitCodeAbbrev();
407   Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
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)); // Flag name text.
411   Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
412                                                            Abbrev));
413   
414   // Emit the abbreviation for RECORD_FILENAME.
415   Abbrev = new BitCodeAbbrev();
416   Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
417   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
418   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
419   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time.  
420   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
421   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
422   Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
423                                                           Abbrev));
424   
425   // Emit the abbreviation for RECORD_FIXIT.
426   Abbrev = new BitCodeAbbrev();
427   Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
428   AddRangeLocationAbbrev(Abbrev);
429   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
430   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));      // FixIt text.
431   Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
432                                                        Abbrev));
433
434   Stream.ExitBlock();
435 }
436
437 void SDiagsWriter::EmitMetaBlock() {
438   Stream.EnterSubblock(BLOCK_META, 3);
439   Record.clear();
440   Record.push_back(RECORD_VERSION);
441   Record.push_back(Version);
442   Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);  
443   Stream.ExitBlock();
444 }
445
446 unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
447   if (Categories.count(category))
448     return category;
449   
450   Categories.insert(category);
451   
452   // We use a local version of 'Record' so that we can be generating
453   // another record when we lazily generate one for the category entry.
454   RecordData Record;
455   Record.push_back(RECORD_CATEGORY);
456   Record.push_back(category);
457   StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
458   Record.push_back(catName.size());
459   Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_CATEGORY), Record, catName);
460   
461   return category;
462 }
463
464 unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
465                                              unsigned DiagID) {
466   if (DiagLevel == DiagnosticsEngine::Note)
467     return 0; // No flag for notes.
468   
469   StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
470   if (FlagName.empty())
471     return 0;
472
473   // Here we assume that FlagName points to static data whose pointer
474   // value is fixed.  This allows us to unique by diagnostic groups.
475   const void *data = FlagName.data();
476   std::pair<unsigned, StringRef> &entry = DiagFlags[data];
477   if (entry.first == 0) {
478     entry.first = DiagFlags.size();
479     entry.second = FlagName;
480     
481     // Lazily emit the string in a separate record.
482     RecordData Record;
483     Record.push_back(RECORD_DIAG_FLAG);
484     Record.push_back(entry.first);
485     Record.push_back(FlagName.size());
486     Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG_FLAG),
487                               Record, FlagName);    
488   }
489
490   return entry.first;
491 }
492
493 void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
494                                     const Diagnostic &Info) {
495   // Enter the block for a non-note diagnostic immediately, rather than waiting
496   // for beginDiagnostic, in case associated notes are emitted before we get
497   // there.
498   if (DiagLevel != DiagnosticsEngine::Note) {
499     if (EmittedAnyDiagBlocks)
500       ExitDiagBlock();
501
502     EnterDiagBlock();
503     EmittedAnyDiagBlocks = true;
504   }
505
506   // Compute the diagnostic text.
507   diagBuf.clear();
508   Info.FormatDiagnostic(diagBuf);
509
510   if (Info.getLocation().isInvalid()) {
511     // Special-case diagnostics with no location. We may not have entered a
512     // source file in this case, so we can't use the normal DiagnosticsRenderer
513     // machinery.
514     EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel,
515                           diagBuf, 0, &Info);
516     return;
517   }
518
519   assert(Info.hasSourceManager() && LangOpts &&
520          "Unexpected diagnostic with valid location outside of a source file");
521   SDiagsRenderer Renderer(*this, *LangOpts, &*DiagOpts);
522   Renderer.emitDiagnostic(Info.getLocation(), DiagLevel,
523                           diagBuf.str(),
524                           Info.getRanges(),
525                           llvm::makeArrayRef(Info.getFixItHints(),
526                                              Info.getNumFixItHints()),
527                           &Info.getSourceManager(),
528                           &Info);
529 }
530
531 void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc,
532                                          PresumedLoc PLoc,
533                                          DiagnosticsEngine::Level Level,
534                                          StringRef Message,
535                                          const SourceManager *SM,
536                                          DiagOrStoredDiag D) {
537   // Emit the RECORD_DIAG record.
538   Record.clear();
539   Record.push_back(RECORD_DIAG);
540   Record.push_back(Level);
541   AddLocToRecord(Loc, SM, PLoc, Record);
542
543   if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
544     // Emit the category string lazily and get the category ID.
545     unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
546     Record.push_back(getEmitCategory(DiagID));
547     // Emit the diagnostic flag string lazily and get the mapped ID.
548     Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
549   } else {
550     Record.push_back(getEmitCategory());
551     Record.push_back(getEmitDiagnosticFlag(Level));
552   }
553
554   Record.push_back(Message.size());
555   Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
556 }
557
558 void
559 SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc,
560                                       PresumedLoc PLoc,
561                                       DiagnosticsEngine::Level Level,
562                                       StringRef Message,
563                                       ArrayRef<clang::CharSourceRange> Ranges,
564                                       const SourceManager *SM,
565                                       DiagOrStoredDiag D) {
566   Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D);
567 }
568
569 void SDiagsWriter::EnterDiagBlock() {
570   Stream.EnterSubblock(BLOCK_DIAG, 4);
571 }
572
573 void SDiagsWriter::ExitDiagBlock() {
574   Stream.ExitBlock();
575 }
576
577 void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
578                                      DiagnosticsEngine::Level Level) {
579   if (Level == DiagnosticsEngine::Note)
580     Writer.EnterDiagBlock();
581 }
582
583 void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
584                                    DiagnosticsEngine::Level Level) {
585   // Only end note diagnostics here, because we can't be sure when we've seen
586   // the last note associated with a non-note diagnostic.
587   if (Level == DiagnosticsEngine::Note)
588     Writer.ExitDiagBlock();
589 }
590
591 void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
592                                    ArrayRef<FixItHint> Hints,
593                                    const SourceManager &SM) {
594   // Emit Source Ranges.
595   for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
596        I != E; ++I)
597     if (I->isValid())
598       EmitCharSourceRange(*I, SM);
599
600   // Emit FixIts.
601   for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
602        I != E; ++I) {
603     const FixItHint &Fix = *I;
604     if (Fix.isNull())
605       continue;
606     Record.clear();
607     Record.push_back(RECORD_FIXIT);
608     AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
609     Record.push_back(Fix.CodeToInsert.size());
610     Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
611                               Fix.CodeToInsert);
612   }
613 }
614
615 void SDiagsRenderer::emitCodeContext(SourceLocation Loc,
616                                      DiagnosticsEngine::Level Level,
617                                      SmallVectorImpl<CharSourceRange> &Ranges,
618                                      ArrayRef<FixItHint> Hints,
619                                      const SourceManager &SM) {
620   Writer.EmitCodeContext(Ranges, Hints, SM);
621 }
622
623 void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message,
624                               const SourceManager *SM) {
625   Writer.EnterDiagBlock();
626   PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc();
627   Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note,
628                                Message, SM, DiagOrStoredDiag());
629   Writer.ExitDiagBlock();
630 }
631
632 void SDiagsWriter::finish() {
633   // Finish off any diagnostic we were in the process of emitting.
634   if (EmittedAnyDiagBlocks)
635     ExitDiagBlock();
636
637   // Write the generated bitstream to "Out".
638   OS->write((char *)&Buffer.front(), Buffer.size());
639   OS->flush();
640
641   OS.reset(0);
642 }