Coverage Report

Created: 2019-07-24 05:18

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