Coverage Report

Created: 2017-10-03 07:32

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