]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp
Restore packaging subdir to enable running unmodified configure script.
[FreeBSD/FreeBSD.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 "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/Frontend/FrontendDiagnostic.h"
18 #include "clang/Frontend/SerializedDiagnosticReader.h"
19 #include "clang/Frontend/SerializedDiagnostics.h"
20 #include "clang/Frontend/TextDiagnosticPrinter.h"
21 #include "clang/Lex/Lexer.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <vector>
27
28 using namespace clang;
29 using namespace clang::serialized_diags;
30
31 namespace {
32   
33 class AbbreviationMap {
34   llvm::DenseMap<unsigned, unsigned> Abbrevs;
35 public:
36   AbbreviationMap() {}
37   
38   void set(unsigned recordID, unsigned abbrevID) {
39     assert(Abbrevs.find(recordID) == Abbrevs.end() 
40            && "Abbreviation already set.");
41     Abbrevs[recordID] = abbrevID;
42   }
43   
44   unsigned get(unsigned recordID) {
45     assert(Abbrevs.find(recordID) != Abbrevs.end() &&
46            "Abbreviation not set.");
47     return Abbrevs[recordID];
48   }
49 };
50  
51 typedef SmallVector<uint64_t, 64> RecordData;
52 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
53
54 class SDiagsWriter;
55   
56 class SDiagsRenderer : public DiagnosticNoteRenderer {
57   SDiagsWriter &Writer;
58 public:
59   SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts,
60                  DiagnosticOptions *DiagOpts)
61     : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {}
62
63   virtual ~SDiagsRenderer() {}
64   
65 protected:
66   void emitDiagnosticMessage(SourceLocation Loc,
67                              PresumedLoc PLoc,
68                              DiagnosticsEngine::Level Level,
69                              StringRef Message,
70                              ArrayRef<CharSourceRange> Ranges,
71                              const SourceManager *SM,
72                              DiagOrStoredDiag D) override;
73
74   void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
75                          DiagnosticsEngine::Level Level,
76                          ArrayRef<CharSourceRange> Ranges,
77                          const SourceManager &SM) override {}
78
79   void emitNote(SourceLocation Loc, StringRef Message,
80                 const SourceManager *SM) override;
81
82   void emitCodeContext(SourceLocation Loc,
83                        DiagnosticsEngine::Level Level,
84                        SmallVectorImpl<CharSourceRange>& Ranges,
85                        ArrayRef<FixItHint> Hints,
86                        const SourceManager &SM) override;
87
88   void beginDiagnostic(DiagOrStoredDiag D,
89                        DiagnosticsEngine::Level Level) override;
90   void endDiagnostic(DiagOrStoredDiag D,
91                      DiagnosticsEngine::Level Level) override;
92 };
93
94 typedef llvm::DenseMap<unsigned, unsigned> AbbrevLookup;
95
96 class SDiagsMerger : SerializedDiagnosticReader {
97   SDiagsWriter &Writer;
98   AbbrevLookup FileLookup;
99   AbbrevLookup CategoryLookup;
100   AbbrevLookup DiagFlagLookup;
101
102 public:
103   SDiagsMerger(SDiagsWriter &Writer)
104       : SerializedDiagnosticReader(), Writer(Writer) {}
105
106   std::error_code mergeRecordsFromFile(const char *File) {
107     return readDiagnostics(File);
108   }
109
110 protected:
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,
119                                       unsigned Timestamp,
120                                       StringRef Name) override;
121   std::error_code visitFixitRecord(const serialized_diags::Location &Start,
122                                    const serialized_diags::Location &End,
123                                    StringRef CodeToInsert) override;
124   std::error_code
125   visitSourceRangeRecord(const serialized_diags::Location &Start,
126                          const serialized_diags::Location &End) override;
127
128 private:
129   std::error_code adjustSourceLocFilename(RecordData &Record,
130                                           unsigned int offset);
131
132   void adjustAbbrevID(RecordData &Record, AbbrevLookup &Lookup,
133                       unsigned NewAbbrev);
134
135   void writeRecordWithAbbrev(unsigned ID, RecordData &Record);
136
137   void writeRecordWithBlob(unsigned ID, RecordData &Record, StringRef Blob);
138 };
139
140 class SDiagsWriter : public DiagnosticConsumer {
141   friend class SDiagsRenderer;
142   friend class SDiagsMerger;
143
144   struct SharedState;
145
146   explicit SDiagsWriter(IntrusiveRefCntPtr<SharedState> State)
147       : LangOpts(nullptr), OriginalInstance(false), MergeChildRecords(false),
148         State(State) {}
149
150 public:
151   SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords)
152       : LangOpts(nullptr), OriginalInstance(true),
153         MergeChildRecords(MergeChildRecords),
154         State(new SharedState(File, Diags)) {
155     if (MergeChildRecords)
156       RemoveOldDiagnostics();
157     EmitPreamble();
158   }
159
160   ~SDiagsWriter() {}
161
162   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
163                         const Diagnostic &Info) override;
164
165   void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
166     LangOpts = &LO;
167   }
168
169   void finish() override;
170
171 private:
172   /// \brief Build a DiagnosticsEngine to emit diagnostics about the diagnostics
173   DiagnosticsEngine *getMetaDiags();
174
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();
179
180   /// \brief Emit the preamble for the serialized diagnostics.
181   void EmitPreamble();
182   
183   /// \brief Emit the BLOCKINFO block.
184   void EmitBlockInfoBlock();
185
186   /// \brief Emit the META data block.
187   void EmitMetaBlock();
188
189   /// \brief Start a DIAG block.
190   void EnterDiagBlock();
191
192   /// \brief End a DIAG block.
193   void ExitDiagBlock();
194
195   /// \brief Emit a DIAG record.
196   void EmitDiagnosticMessage(SourceLocation Loc,
197                              PresumedLoc PLoc,
198                              DiagnosticsEngine::Level Level,
199                              StringRef Message,
200                              const SourceManager *SM,
201                              DiagOrStoredDiag D);
202
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);
207
208   /// \brief Emit a record for a CharSourceRange.
209   void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
210   
211   /// \brief Emit the string information for the category.
212   unsigned getEmitCategory(unsigned category = 0);
213   
214   /// \brief Emit the string information for diagnostic flags.
215   unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
216                                  unsigned DiagID = 0);
217
218   unsigned getEmitDiagnosticFlag(StringRef DiagName);
219
220   /// \brief Emit (lazily) the file string and retrieved the file identifier.
221   unsigned getEmitFile(const char *Filename);
222
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);
227
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(),
233                    Record, TokSize);
234   }
235
236   /// \brief Add CharSourceRange information the specified record.
237   void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
238                                   const SourceManager &SM);
239
240   /// \brief Language options, which can differ from one clone of this client
241   /// to another.
242   const LangOptions *LangOpts;
243
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;
247
248   /// \brief Whether this instance should aggregate diagnostics that are
249   /// generated from child processes.
250   bool MergeChildRecords;
251
252   /// \brief State that is shared among the various clones of this diagnostic
253   /// consumer.
254   struct SharedState : RefCountedBase<SharedState> {
255     SharedState(StringRef File, DiagnosticOptions *Diags)
256         : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()),
257           EmittedAnyDiagBlocks(false) {}
258
259     /// \brief Diagnostic options.
260     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
261
262     /// \brief The byte buffer for the serialized content.
263     SmallString<1024> Buffer;
264
265     /// \brief The BitStreamWriter for the serialized diagnostics.
266     llvm::BitstreamWriter Stream;
267
268     /// \brief The name of the diagnostics file.
269     std::string OutputFile;
270
271     /// \brief The set of constructed record abbreviations.
272     AbbreviationMap Abbrevs;
273
274     /// \brief A utility buffer for constructing record content.
275     RecordData Record;
276
277     /// \brief A text buffer for rendering diagnostic text.
278     SmallString<256> diagBuf;
279
280     /// \brief The collection of diagnostic categories used.
281     llvm::DenseSet<unsigned> Categories;
282
283     /// \brief The collection of files used.
284     llvm::DenseMap<const char *, unsigned> Files;
285
286     typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
287     DiagFlagsTy;
288
289     /// \brief Map for uniquing strings.
290     DiagFlagsTy DiagFlags;
291
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;
296
297     /// \brief Engine for emitting diagnostics about the diagnostics.
298     std::unique_ptr<DiagnosticsEngine> MetaDiagnostics;
299   };
300
301   /// \brief State shared among the various clones of this diagnostic consumer.
302   IntrusiveRefCntPtr<SharedState> State;
303 };
304 } // end anonymous namespace
305
306 namespace clang {
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);
311 }
312
313 } // end namespace serialized_diags
314 } // end namespace clang
315
316 //===----------------------------------------------------------------------===//
317 // Serialization methods.
318 //===----------------------------------------------------------------------===//
319
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) {
324   Record.clear();
325   Record.push_back(ID);
326   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
327   
328   // Emit the block name if present.
329   if (!Name || Name[0] == 0)
330     return;
331
332   Record.clear();
333
334   while (*Name)
335     Record.push_back(*Name++);
336
337   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
338 }
339
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){
344   Record.clear();
345   Record.push_back(ID);
346
347   while (*Name)
348     Record.push_back(*Name++);
349
350   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
351 }
352
353 void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
354                                   const SourceManager *SM,
355                                   PresumedLoc PLoc,
356                                   RecordDataImpl &Record,
357                                   unsigned TokSize) {
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.
364     return;
365   }
366
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));
371 }
372
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(),
380                                         SM, *LangOpts);
381   
382   AddLocToRecord(Range.getEnd(), Record, &SM, TokSize);
383 }
384
385 unsigned SDiagsWriter::getEmitFile(const char *FileName){
386   if (!FileName)
387     return 0;
388   
389   unsigned &entry = State->Files[FileName];
390   if (entry)
391     return entry;
392   
393   // Lazily generate the record for the file.
394   entry = State->Files.size();
395   RecordData Record;
396   Record.push_back(RECORD_FILENAME);
397   Record.push_back(entry);
398   Record.push_back(0); // For legacy.
399   Record.push_back(0); // For legacy.
400   StringRef Name(FileName);
401   Record.push_back(Name.size());
402   State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
403                                    Name);
404
405   return entry;
406 }
407
408 void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
409                                        const SourceManager &SM) {
410   State->Record.clear();
411   State->Record.push_back(RECORD_SOURCE_RANGE);
412   AddCharSourceRangeToRecord(R, State->Record, SM);
413   State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
414                                      State->Record);
415 }
416
417 /// \brief Emits the preamble of the diagnostics file.
418 void SDiagsWriter::EmitPreamble() {
419   // Emit the file header.
420   State->Stream.Emit((unsigned)'D', 8);
421   State->Stream.Emit((unsigned)'I', 8);
422   State->Stream.Emit((unsigned)'A', 8);
423   State->Stream.Emit((unsigned)'G', 8);
424
425   EmitBlockInfoBlock();
426   EmitMetaBlock();
427 }
428
429 static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
430   using namespace llvm;
431   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
432   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
433   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
434   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
435 }
436
437 static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
438   AddSourceLocationAbbrev(Abbrev);
439   AddSourceLocationAbbrev(Abbrev);  
440 }
441
442 void SDiagsWriter::EmitBlockInfoBlock() {
443   State->Stream.EnterBlockInfoBlock(3);
444
445   using namespace llvm;
446   llvm::BitstreamWriter &Stream = State->Stream;
447   RecordData &Record = State->Record;
448   AbbreviationMap &Abbrevs = State->Abbrevs;
449
450   // ==---------------------------------------------------------------------==//
451   // The subsequent records and Abbrevs are for the "Meta" block.
452   // ==---------------------------------------------------------------------==//
453
454   EmitBlockID(BLOCK_META, "Meta", Stream, Record);
455   EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
456   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
457   Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
458   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
459   Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
460
461   // ==---------------------------------------------------------------------==//
462   // The subsequent records and Abbrevs are for the "Diagnostic" block.
463   // ==---------------------------------------------------------------------==//
464
465   EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
466   EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
467   EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
468   EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
469   EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
470   EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
471   EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
472
473   // Emit abbreviation for RECORD_DIAG.
474   Abbrev = new BitCodeAbbrev();
475   Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
476   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));  // Diag level.
477   AddSourceLocationAbbrev(Abbrev);
478   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.  
479   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
480   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
481   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
482   Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
483   
484   // Emit abbrevation for RECORD_CATEGORY.
485   Abbrev = new BitCodeAbbrev();
486   Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
487   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
488   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));  // Text size.
489   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));      // Category text.
490   Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
491
492   // Emit abbrevation for RECORD_SOURCE_RANGE.
493   Abbrev = new BitCodeAbbrev();
494   Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
495   AddRangeLocationAbbrev(Abbrev);
496   Abbrevs.set(RECORD_SOURCE_RANGE,
497               Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
498   
499   // Emit the abbreviation for RECORD_DIAG_FLAG.
500   Abbrev = new BitCodeAbbrev();
501   Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
502   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
503   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
504   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
505   Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
506                                                            Abbrev));
507   
508   // Emit the abbreviation for RECORD_FILENAME.
509   Abbrev = new BitCodeAbbrev();
510   Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
511   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
512   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
513   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time.  
514   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
515   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
516   Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
517                                                           Abbrev));
518   
519   // Emit the abbreviation for RECORD_FIXIT.
520   Abbrev = new BitCodeAbbrev();
521   Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
522   AddRangeLocationAbbrev(Abbrev);
523   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
524   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));      // FixIt text.
525   Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
526                                                        Abbrev));
527
528   Stream.ExitBlock();
529 }
530
531 void SDiagsWriter::EmitMetaBlock() {
532   llvm::BitstreamWriter &Stream = State->Stream;
533   RecordData &Record = State->Record;
534   AbbreviationMap &Abbrevs = State->Abbrevs;
535
536   Stream.EnterSubblock(BLOCK_META, 3);
537   Record.clear();
538   Record.push_back(RECORD_VERSION);
539   Record.push_back(VersionNumber);
540   Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);  
541   Stream.ExitBlock();
542 }
543
544 unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
545   if (!State->Categories.insert(category).second)
546     return category;
547
548   // We use a local version of 'Record' so that we can be generating
549   // another record when we lazily generate one for the category entry.
550   RecordData Record;
551   Record.push_back(RECORD_CATEGORY);
552   Record.push_back(category);
553   StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
554   Record.push_back(catName.size());
555   State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
556                                    catName);
557   
558   return category;
559 }
560
561 unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
562                                              unsigned DiagID) {
563   if (DiagLevel == DiagnosticsEngine::Note)
564     return 0; // No flag for notes.
565   
566   StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
567   return getEmitDiagnosticFlag(FlagName);
568 }
569
570 unsigned SDiagsWriter::getEmitDiagnosticFlag(StringRef FlagName) {
571   if (FlagName.empty())
572     return 0;
573
574   // Here we assume that FlagName points to static data whose pointer
575   // value is fixed.  This allows us to unique by diagnostic groups.
576   const void *data = FlagName.data();
577   std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
578   if (entry.first == 0) {
579     entry.first = State->DiagFlags.size();
580     entry.second = FlagName;
581     
582     // Lazily emit the string in a separate record.
583     RecordData Record;
584     Record.push_back(RECORD_DIAG_FLAG);
585     Record.push_back(entry.first);
586     Record.push_back(FlagName.size());
587     State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
588                                      Record, FlagName);
589   }
590
591   return entry.first;
592 }
593
594 void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
595                                     const Diagnostic &Info) {
596   // Enter the block for a non-note diagnostic immediately, rather than waiting
597   // for beginDiagnostic, in case associated notes are emitted before we get
598   // there.
599   if (DiagLevel != DiagnosticsEngine::Note) {
600     if (State->EmittedAnyDiagBlocks)
601       ExitDiagBlock();
602
603     EnterDiagBlock();
604     State->EmittedAnyDiagBlocks = true;
605   }
606
607   // Compute the diagnostic text.
608   State->diagBuf.clear();
609   Info.FormatDiagnostic(State->diagBuf);
610
611   if (Info.getLocation().isInvalid()) {
612     // Special-case diagnostics with no location. We may not have entered a
613     // source file in this case, so we can't use the normal DiagnosticsRenderer
614     // machinery.
615
616     // Make sure we bracket all notes as "sub-diagnostics".  This matches
617     // the behavior in SDiagsRenderer::emitDiagnostic().
618     if (DiagLevel == DiagnosticsEngine::Note)
619       EnterDiagBlock();
620
621     EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel,
622                           State->diagBuf, nullptr, &Info);
623
624     if (DiagLevel == DiagnosticsEngine::Note)
625       ExitDiagBlock();
626
627     return;
628   }
629
630   assert(Info.hasSourceManager() && LangOpts &&
631          "Unexpected diagnostic with valid location outside of a source file");
632   SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
633   Renderer.emitDiagnostic(Info.getLocation(), DiagLevel,
634                           State->diagBuf.str(),
635                           Info.getRanges(),
636                           Info.getFixItHints(),
637                           &Info.getSourceManager(),
638                           &Info);
639 }
640
641 static serialized_diags::Level getStableLevel(DiagnosticsEngine::Level Level) {
642   switch (Level) {
643 #define CASE(X) case DiagnosticsEngine::X: return serialized_diags::X;
644   CASE(Ignored)
645   CASE(Note)
646   CASE(Remark)
647   CASE(Warning)
648   CASE(Error)
649   CASE(Fatal)
650 #undef CASE
651   }
652
653   llvm_unreachable("invalid diagnostic level");
654 }
655
656 void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc,
657                                          PresumedLoc PLoc,
658                                          DiagnosticsEngine::Level Level,
659                                          StringRef Message,
660                                          const SourceManager *SM,
661                                          DiagOrStoredDiag D) {
662   llvm::BitstreamWriter &Stream = State->Stream;
663   RecordData &Record = State->Record;
664   AbbreviationMap &Abbrevs = State->Abbrevs;
665   
666   // Emit the RECORD_DIAG record.
667   Record.clear();
668   Record.push_back(RECORD_DIAG);
669   Record.push_back(getStableLevel(Level));
670   AddLocToRecord(Loc, SM, PLoc, Record);
671
672   if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
673     // Emit the category string lazily and get the category ID.
674     unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
675     Record.push_back(getEmitCategory(DiagID));
676     // Emit the diagnostic flag string lazily and get the mapped ID.
677     Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
678   } else {
679     Record.push_back(getEmitCategory());
680     Record.push_back(getEmitDiagnosticFlag(Level));
681   }
682
683   Record.push_back(Message.size());
684   Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
685 }
686
687 void
688 SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc,
689                                       PresumedLoc PLoc,
690                                       DiagnosticsEngine::Level Level,
691                                       StringRef Message,
692                                       ArrayRef<clang::CharSourceRange> Ranges,
693                                       const SourceManager *SM,
694                                       DiagOrStoredDiag D) {
695   Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D);
696 }
697
698 void SDiagsWriter::EnterDiagBlock() {
699   State->Stream.EnterSubblock(BLOCK_DIAG, 4);
700 }
701
702 void SDiagsWriter::ExitDiagBlock() {
703   State->Stream.ExitBlock();
704 }
705
706 void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
707                                      DiagnosticsEngine::Level Level) {
708   if (Level == DiagnosticsEngine::Note)
709     Writer.EnterDiagBlock();
710 }
711
712 void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
713                                    DiagnosticsEngine::Level Level) {
714   // Only end note diagnostics here, because we can't be sure when we've seen
715   // the last note associated with a non-note diagnostic.
716   if (Level == DiagnosticsEngine::Note)
717     Writer.ExitDiagBlock();
718 }
719
720 void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
721                                    ArrayRef<FixItHint> Hints,
722                                    const SourceManager &SM) {
723   llvm::BitstreamWriter &Stream = State->Stream;
724   RecordData &Record = State->Record;
725   AbbreviationMap &Abbrevs = State->Abbrevs;
726
727   // Emit Source Ranges.
728   for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
729        I != E; ++I)
730     if (I->isValid())
731       EmitCharSourceRange(*I, SM);
732
733   // Emit FixIts.
734   for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
735        I != E; ++I) {
736     const FixItHint &Fix = *I;
737     if (Fix.isNull())
738       continue;
739     Record.clear();
740     Record.push_back(RECORD_FIXIT);
741     AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
742     Record.push_back(Fix.CodeToInsert.size());
743     Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
744                               Fix.CodeToInsert);
745   }
746 }
747
748 void SDiagsRenderer::emitCodeContext(SourceLocation Loc,
749                                      DiagnosticsEngine::Level Level,
750                                      SmallVectorImpl<CharSourceRange> &Ranges,
751                                      ArrayRef<FixItHint> Hints,
752                                      const SourceManager &SM) {
753   Writer.EmitCodeContext(Ranges, Hints, SM);
754 }
755
756 void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message,
757                               const SourceManager *SM) {
758   Writer.EnterDiagBlock();
759   PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc();
760   Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note,
761                                Message, SM, DiagOrStoredDiag());
762   Writer.ExitDiagBlock();
763 }
764
765 DiagnosticsEngine *SDiagsWriter::getMetaDiags() {
766   // FIXME: It's slightly absurd to create a new diagnostics engine here, but
767   // the other options that are available today are worse:
768   //
769   // 1. Teach DiagnosticsConsumers to emit diagnostics to the engine they are a
770   //    part of. The DiagnosticsEngine would need to know not to send
771   //    diagnostics back to the consumer that failed. This would require us to
772   //    rework ChainedDiagnosticsConsumer and teach the engine about multiple
773   //    consumers, which is difficult today because most APIs interface with
774   //    consumers rather than the engine itself.
775   //
776   // 2. Pass a DiagnosticsEngine to SDiagsWriter on creation - this would need
777   //    to be distinct from the engine the writer was being added to and would
778   //    normally not be used.
779   if (!State->MetaDiagnostics) {
780     IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs());
781     auto Client =
782         new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get());
783     State->MetaDiagnostics = llvm::make_unique<DiagnosticsEngine>(
784         IDs, State->DiagOpts.get(), Client);
785   }
786   return State->MetaDiagnostics.get();
787 }
788
789 void SDiagsWriter::RemoveOldDiagnostics() {
790   if (!llvm::sys::fs::remove(State->OutputFile))
791     return;
792
793   getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
794   // Disable merging child records, as whatever is in this file may be
795   // misleading.
796   MergeChildRecords = false;
797 }
798
799 void SDiagsWriter::finish() {
800   // The original instance is responsible for writing the file.
801   if (!OriginalInstance)
802     return;
803
804   // Finish off any diagnostic we were in the process of emitting.
805   if (State->EmittedAnyDiagBlocks)
806     ExitDiagBlock();
807
808   if (MergeChildRecords) {
809     if (!State->EmittedAnyDiagBlocks)
810       // We have no diagnostics of our own, so we can just leave the child
811       // process' output alone
812       return;
813
814     if (llvm::sys::fs::exists(State->OutputFile))
815       if (SDiagsMerger(*this).mergeRecordsFromFile(State->OutputFile.c_str()))
816         getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
817   }
818
819   std::error_code EC;
820   auto OS = llvm::make_unique<llvm::raw_fd_ostream>(State->OutputFile.c_str(),
821                                                     EC, llvm::sys::fs::F_None);
822   if (EC) {
823     getMetaDiags()->Report(diag::warn_fe_serialized_diag_failure)
824         << State->OutputFile << EC.message();
825     return;
826   }
827
828   // Write the generated bitstream to "Out".
829   OS->write((char *)&State->Buffer.front(), State->Buffer.size());
830   OS->flush();
831 }
832
833 std::error_code SDiagsMerger::visitStartOfDiagnostic() {
834   Writer.EnterDiagBlock();
835   return std::error_code();
836 }
837
838 std::error_code SDiagsMerger::visitEndOfDiagnostic() {
839   Writer.ExitDiagBlock();
840   return std::error_code();
841 }
842
843 std::error_code
844 SDiagsMerger::visitSourceRangeRecord(const serialized_diags::Location &Start,
845                                      const serialized_diags::Location &End) {
846   RecordData Record;
847   Record.push_back(RECORD_SOURCE_RANGE);
848   Record.push_back(FileLookup[Start.FileID]);
849   Record.push_back(Start.Line);
850   Record.push_back(Start.Col);
851   Record.push_back(Start.Offset);
852   Record.push_back(FileLookup[End.FileID]);
853   Record.push_back(End.Line);
854   Record.push_back(End.Col);
855   Record.push_back(End.Offset);
856
857   Writer.State->Stream.EmitRecordWithAbbrev(
858       Writer.State->Abbrevs.get(RECORD_SOURCE_RANGE), Record);
859   return std::error_code();
860 }
861
862 std::error_code SDiagsMerger::visitDiagnosticRecord(
863     unsigned Severity, const serialized_diags::Location &Location,
864     unsigned Category, unsigned Flag, StringRef Message) {
865   RecordData MergedRecord;
866   MergedRecord.push_back(RECORD_DIAG);
867   MergedRecord.push_back(Severity);
868   MergedRecord.push_back(FileLookup[Location.FileID]);
869   MergedRecord.push_back(Location.Line);
870   MergedRecord.push_back(Location.Col);
871   MergedRecord.push_back(Location.Offset);
872   MergedRecord.push_back(CategoryLookup[Category]);
873   MergedRecord.push_back(Flag ? DiagFlagLookup[Flag] : 0);
874   MergedRecord.push_back(Message.size());
875
876   Writer.State->Stream.EmitRecordWithBlob(
877       Writer.State->Abbrevs.get(RECORD_DIAG), MergedRecord, Message);
878   return std::error_code();
879 }
880
881 std::error_code
882 SDiagsMerger::visitFixitRecord(const serialized_diags::Location &Start,
883                                const serialized_diags::Location &End,
884                                StringRef Text) {
885   RecordData Record;
886   Record.push_back(RECORD_FIXIT);
887   Record.push_back(FileLookup[Start.FileID]);
888   Record.push_back(Start.Line);
889   Record.push_back(Start.Col);
890   Record.push_back(Start.Offset);
891   Record.push_back(FileLookup[End.FileID]);
892   Record.push_back(End.Line);
893   Record.push_back(End.Col);
894   Record.push_back(End.Offset);
895   Record.push_back(Text.size());
896
897   Writer.State->Stream.EmitRecordWithBlob(
898       Writer.State->Abbrevs.get(RECORD_FIXIT), Record, Text);
899   return std::error_code();
900 }
901
902 std::error_code SDiagsMerger::visitFilenameRecord(unsigned ID, unsigned Size,
903                                                   unsigned Timestamp,
904                                                   StringRef Name) {
905   FileLookup[ID] = Writer.getEmitFile(Name.str().c_str());
906   return std::error_code();
907 }
908
909 std::error_code SDiagsMerger::visitCategoryRecord(unsigned ID, StringRef Name) {
910   CategoryLookup[ID] = Writer.getEmitCategory(ID);
911   return std::error_code();
912 }
913
914 std::error_code SDiagsMerger::visitDiagFlagRecord(unsigned ID, StringRef Name) {
915   DiagFlagLookup[ID] = Writer.getEmitDiagnosticFlag(Name);
916   return std::error_code();
917 }