Coverage Report

Created: 2022-01-18 06:27

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