blob: 7666fe10b3818d06b7926919510e0b3d8ee4fd4a [file] [log] [blame]
Ted Kremenek4610ea22011-10-29 00:12:39 +00001//===--- 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
Ted Kremenek4610ea22011-10-29 00:12:39 +000010#include "clang/Frontend/SerializedDiagnosticPrinter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "clang/Basic/Diagnostic.h"
12#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Basic/SourceManager.h"
Ted Kremenek4548e042011-12-17 05:26:11 +000014#include "clang/Frontend/DiagnosticRenderer.h"
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000015#include "clang/Frontend/FrontendDiagnostic.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000016#include "clang/Frontend/SerializedDiagnosticReader.h"
17#include "clang/Frontend/SerializedDiagnostics.h"
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000018#include "clang/Frontend/TextDiagnosticPrinter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Lex/Lexer.h"
20#include "llvm/ADT/DenseSet.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/raw_ostream.h"
Benjamin Kramercfeacf52016-05-27 14:27:13 +000025#include <utility>
Ted Kremenek4610ea22011-10-29 00:12:39 +000026
27using namespace clang;
Ted Kremenek337cd2a2011-11-05 00:09:57 +000028using namespace clang::serialized_diags;
Ted Kremenek4610ea22011-10-29 00:12:39 +000029
30namespace {
Ted Kremenek4610ea22011-10-29 00:12:39 +000031
32class AbbreviationMap {
33 llvm::DenseMap<unsigned, unsigned> Abbrevs;
34public:
35 AbbreviationMap() {}
36
37 void set(unsigned recordID, unsigned abbrevID) {
38 assert(Abbrevs.find(recordID) == Abbrevs.end()
39 && "Abbreviation already set.");
40 Abbrevs[recordID] = abbrevID;
41 }
42
43 unsigned get(unsigned recordID) {
44 assert(Abbrevs.find(recordID) != Abbrevs.end() &&
45 "Abbreviation not set.");
46 return Abbrevs[recordID];
47 }
48};
49
Dmitri Gribenkof8579502013-01-12 19:30:44 +000050typedef SmallVector<uint64_t, 64> RecordData;
51typedef SmallVectorImpl<uint64_t> RecordDataImpl;
Mehdi Amini57a41912015-09-10 01:46:39 +000052typedef ArrayRef<uint64_t> RecordDataRef;
Ted Kremenek4548e042011-12-17 05:26:11 +000053
54class SDiagsWriter;
55
Ted Kremenek0964cca2012-02-14 02:46:00 +000056class SDiagsRenderer : public DiagnosticNoteRenderer {
Ted Kremenek4548e042011-12-17 05:26:11 +000057 SDiagsWriter &Writer;
Ted Kremenek4548e042011-12-17 05:26:11 +000058public:
Richard Smitha9f521f2012-08-21 03:11:53 +000059 SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts,
Douglas Gregor811db4e2012-10-23 22:26:28 +000060 DiagnosticOptions *DiagOpts)
Richard Smitha9f521f2012-08-21 03:11:53 +000061 : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {}
Ted Kremenek4548e042011-12-17 05:26:11 +000062
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000063 ~SDiagsRenderer() override {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000064
Ted Kremenek4548e042011-12-17 05:26:11 +000065protected:
Christof Doumafb4a0452017-06-27 09:50:38 +000066 void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
67 DiagnosticsEngine::Level Level, StringRef Message,
Craig Topperafa7cb32014-03-13 06:07:04 +000068 ArrayRef<CharSourceRange> Ranges,
Craig Topperafa7cb32014-03-13 06:07:04 +000069 DiagOrStoredDiag D) override;
Richard Smitha9f521f2012-08-21 03:11:53 +000070
Christof Doumafb4a0452017-06-27 09:50:38 +000071 void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
Craig Topperafa7cb32014-03-13 06:07:04 +000072 DiagnosticsEngine::Level Level,
Christof Doumafb4a0452017-06-27 09:50:38 +000073 ArrayRef<CharSourceRange> Ranges) override {}
Richard Smitha9f521f2012-08-21 03:11:53 +000074
Christof Doumafb4a0452017-06-27 09:50:38 +000075 void emitNote(FullSourceLoc Loc, StringRef Message) override;
Richard Smitha9f521f2012-08-21 03:11:53 +000076
Christof Doumafb4a0452017-06-27 09:50:38 +000077 void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
78 SmallVectorImpl<CharSourceRange> &Ranges,
79 ArrayRef<FixItHint> Hints) override;
Craig Topperafa7cb32014-03-13 06:07:04 +000080
81 void beginDiagnostic(DiagOrStoredDiag D,
82 DiagnosticsEngine::Level Level) override;
83 void endDiagnostic(DiagOrStoredDiag D,
84 DiagnosticsEngine::Level Level) override;
Ted Kremenek4548e042011-12-17 05:26:11 +000085};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000086
87typedef llvm::DenseMap<unsigned, unsigned> AbbrevLookup;
88
89class SDiagsMerger : SerializedDiagnosticReader {
90 SDiagsWriter &Writer;
91 AbbrevLookup FileLookup;
92 AbbrevLookup CategoryLookup;
93 AbbrevLookup DiagFlagLookup;
94
95public:
96 SDiagsMerger(SDiagsWriter &Writer)
97 : SerializedDiagnosticReader(), Writer(Writer) {}
98
99 std::error_code mergeRecordsFromFile(const char *File) {
100 return readDiagnostics(File);
101 }
102
103protected:
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
121private:
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
Ted Kremenek4610ea22011-10-29 00:12:39 +0000133class SDiagsWriter : public DiagnosticConsumer {
Ted Kremenek4548e042011-12-17 05:26:11 +0000134 friend class SDiagsRenderer;
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000135 friend class SDiagsMerger;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000136
137 struct SharedState;
138
David Blaikie22105e12017-01-05 19:48:10 +0000139 explicit SDiagsWriter(std::shared_ptr<SharedState> State)
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000140 : LangOpts(nullptr), OriginalInstance(false), MergeChildRecords(false),
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000141 State(std::move(State)) {}
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000142
143public:
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000144 SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords)
David Blaikieeb62b822014-08-29 20:17:13 +0000145 : LangOpts(nullptr), OriginalInstance(true),
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000146 MergeChildRecords(MergeChildRecords),
David Blaikie22105e12017-01-05 19:48:10 +0000147 State(std::make_shared<SharedState>(File, Diags)) {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000148 if (MergeChildRecords)
149 RemoveOldDiagnostics();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000150 EmitPreamble();
Devang Patel9957e8b2011-11-15 01:30:40 +0000151 }
Richard Smitha9f521f2012-08-21 03:11:53 +0000152
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000153 ~SDiagsWriter() override {}
Craig Topperafa7cb32014-03-13 06:07:04 +0000154
Ted Kremenek4610ea22011-10-29 00:12:39 +0000155 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Craig Topperafa7cb32014-03-13 06:07:04 +0000156 const Diagnostic &Info) override;
157
158 void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
Ted Kremenekd010ba42011-11-10 08:43:12 +0000159 LangOpts = &LO;
160 }
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000161
Craig Topperafa7cb32014-03-13 06:07:04 +0000162 void finish() override;
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000163
Ted Kremenek4610ea22011-10-29 00:12:39 +0000164private:
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000165 /// \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
Ted Kremenek4610ea22011-10-29 00:12:39 +0000173 /// \brief Emit the preamble for the serialized diagnostics.
174 void EmitPreamble();
175
176 /// \brief Emit the BLOCKINFO block.
177 void EmitBlockInfoBlock();
Ted Kremenekf264a202011-11-05 00:10:11 +0000178
Ted Kremenekcc88d262011-11-08 20:27:29 +0000179 /// \brief Emit the META data block.
180 void EmitMetaBlock();
Richard Smitha9f521f2012-08-21 03:11:53 +0000181
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.
Christof Doumafb4a0452017-06-27 09:50:38 +0000189 void EmitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
190 DiagnosticsEngine::Level Level, StringRef Message,
Richard Smitha9f521f2012-08-21 03:11:53 +0000191 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
Ted Kremenek59f10252011-11-05 00:10:01 +0000198 /// \brief Emit a record for a CharSourceRange.
Ted Kremenek4548e042011-12-17 05:26:11 +0000199 void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
Ted Kremenek59f10252011-11-05 00:10:01 +0000200
Ted Kremenek4548e042011-12-17 05:26:11 +0000201 /// \brief Emit the string information for the category.
202 unsigned getEmitCategory(unsigned category = 0);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000203
204 /// \brief Emit the string information for diagnostic flags.
205 unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
Ted Kremenek4548e042011-12-17 05:26:11 +0000206 unsigned DiagID = 0);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000207
208 unsigned getEmitDiagnosticFlag(StringRef DiagName);
209
Ted Kremenekf264a202011-11-05 00:10:11 +0000210 /// \brief Emit (lazily) the file string and retrieved the file identifier.
Ted Kremenek4548e042011-12-17 05:26:11 +0000211 unsigned getEmitFile(const char *Filename);
212
Christof Doumafb4a0452017-06-27 09:50:38 +0000213 /// \brief Add SourceLocation information the specified record.
214 void AddLocToRecord(FullSourceLoc Loc, PresumedLoc PLoc,
215 RecordDataImpl &Record, unsigned TokSize = 0);
Ted Kremenek4548e042011-12-17 05:26:11 +0000216
Ted Kremenekf264a202011-11-05 00:10:11 +0000217 /// \brief Add SourceLocation information the specified record.
Christof Doumafb4a0452017-06-27 09:50:38 +0000218 void AddLocToRecord(FullSourceLoc Loc, RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000219 unsigned TokSize = 0) {
Christof Doumafb4a0452017-06-27 09:50:38 +0000220 AddLocToRecord(Loc, Loc.hasManager() ? Loc.getPresumedLoc() : PresumedLoc(),
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000221 Record, TokSize);
Ted Kremenek4548e042011-12-17 05:26:11 +0000222 }
Ted Kremenekf264a202011-11-05 00:10:11 +0000223
Ted Kremenekd89a8272011-11-05 03:34:23 +0000224 /// \brief Add CharSourceRange information the specified record.
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000225 void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000226 const SourceManager &SM);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000227
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000228 /// \brief Language options, which can differ from one clone of this client
229 /// to another.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000230 const LangOptions *LangOpts;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000231
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000232 /// \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;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000235
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000236 /// \brief Whether this instance should aggregate diagnostics that are
237 /// generated from child processes.
238 bool MergeChildRecords;
239
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000240 /// \brief State that is shared among the various clones of this diagnostic
241 /// consumer.
David Blaikie22105e12017-01-05 19:48:10 +0000242 struct SharedState {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000243 SharedState(StringRef File, DiagnosticOptions *Diags)
244 : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()),
David Blaikieeb62b822014-08-29 20:17:13 +0000245 EmittedAnyDiagBlocks(false) {}
Ted Kremenek4610ea22011-10-29 00:12:39 +0000246
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000247 /// \brief Diagnostic options.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000248 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000249
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000250 /// \brief The byte buffer for the serialized content.
251 SmallString<1024> Buffer;
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000252
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000253 /// \brief The BitStreamWriter for the serialized diagnostics.
254 llvm::BitstreamWriter Stream;
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000255
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000256 /// \brief The name of the diagnostics file.
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000257 std::string OutputFile;
Richard Smitha9f521f2012-08-21 03:11:53 +0000258
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000259 /// \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
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000274 typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000275 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;
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000284
285 /// \brief Engine for emitting diagnostics about the diagnostics.
286 std::unique_ptr<DiagnosticsEngine> MetaDiagnostics;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000287 };
288
289 /// \brief State shared among the various clones of this diagnostic consumer.
David Blaikie22105e12017-01-05 19:48:10 +0000290 std::shared_ptr<SharedState> State;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000291};
292} // end anonymous namespace
293
294namespace clang {
295namespace serialized_diags {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000296std::unique_ptr<DiagnosticConsumer>
297create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords) {
298 return llvm::make_unique<SDiagsWriter>(OutputFile, Diags, MergeChildRecords);
Ted Kremenek4610ea22011-10-29 00:12:39 +0000299}
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000300
Ted Kremenek4610ea22011-10-29 00:12:39 +0000301} // 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.
309static void EmitBlockID(unsigned ID, const char *Name,
310 llvm::BitstreamWriter &Stream,
311 RecordDataImpl &Record) {
312 Record.clear();
313 Record.push_back(ID);
314 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
315
316 // Emit the block name if present.
Craig Topper49a27902014-05-22 04:46:25 +0000317 if (!Name || Name[0] == 0)
Ted Kremenek4610ea22011-10-29 00:12:39 +0000318 return;
319
320 Record.clear();
321
322 while (*Name)
323 Record.push_back(*Name++);
324
325 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
326}
327
328/// \brief Emits a record ID in the BLOCKINFO block.
329static void EmitRecordID(unsigned ID, const char *Name,
330 llvm::BitstreamWriter &Stream,
331 RecordDataImpl &Record){
332 Record.clear();
333 Record.push_back(ID);
334
335 while (*Name)
336 Record.push_back(*Name++);
337
338 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
339}
340
Christof Doumafb4a0452017-06-27 09:50:38 +0000341void SDiagsWriter::AddLocToRecord(FullSourceLoc Loc, PresumedLoc PLoc,
342 RecordDataImpl &Record, unsigned TokSize) {
Ted Kremenek4548e042011-12-17 05:26:11 +0000343 if (PLoc.isInvalid()) {
Ted Kremenek59f10252011-11-05 00:10:01 +0000344 // Emit a "sentinel" location.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000345 Record.push_back((unsigned)0); // File.
346 Record.push_back((unsigned)0); // Line.
347 Record.push_back((unsigned)0); // Column.
348 Record.push_back((unsigned)0); // Offset.
Ted Kremenek59f10252011-11-05 00:10:01 +0000349 return;
350 }
351
Ted Kremenek4548e042011-12-17 05:26:11 +0000352 Record.push_back(getEmitFile(PLoc.getFilename()));
353 Record.push_back(PLoc.getLine());
354 Record.push_back(PLoc.getColumn()+TokSize);
Christof Doumafb4a0452017-06-27 09:50:38 +0000355 Record.push_back(Loc.getFileOffset());
Ted Kremenek59f10252011-11-05 00:10:01 +0000356}
357
Ted Kremenekd89a8272011-11-05 03:34:23 +0000358void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000359 RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000360 const SourceManager &SM) {
Christof Doumafb4a0452017-06-27 09:50:38 +0000361 AddLocToRecord(FullSourceLoc(Range.getBegin(), SM), Record);
Ted Kremenekd010ba42011-11-10 08:43:12 +0000362 unsigned TokSize = 0;
363 if (Range.isTokenRange())
364 TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000365 SM, *LangOpts);
Christof Doumafb4a0452017-06-27 09:50:38 +0000366
367 AddLocToRecord(FullSourceLoc(Range.getEnd(), SM), Record, TokSize);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000368}
369
Ted Kremenek4548e042011-12-17 05:26:11 +0000370unsigned SDiagsWriter::getEmitFile(const char *FileName){
371 if (!FileName)
Ted Kremenekf264a202011-11-05 00:10:11 +0000372 return 0;
373
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000374 unsigned &entry = State->Files[FileName];
Ted Kremenekf264a202011-11-05 00:10:11 +0000375 if (entry)
376 return entry;
377
378 // Lazily generate the record for the file.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000379 entry = State->Files.size();
Ted Kremenek4548e042011-12-17 05:26:11 +0000380 StringRef Name(FileName);
Mehdi Amini57a41912015-09-10 01:46:39 +0000381 RecordData::value_type Record[] = {RECORD_FILENAME, entry, 0 /* For legacy */,
382 0 /* For legacy */, Name.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000383 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
384 Name);
Ted Kremenekf264a202011-11-05 00:10:11 +0000385
386 return entry;
387}
388
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000389void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
Ted Kremenek4548e042011-12-17 05:26:11 +0000390 const SourceManager &SM) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000391 State->Record.clear();
392 State->Record.push_back(RECORD_SOURCE_RANGE);
393 AddCharSourceRangeToRecord(R, State->Record, SM);
394 State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000395 State->Record);
Ted Kremenek59f10252011-11-05 00:10:01 +0000396}
397
Ted Kremenek4610ea22011-10-29 00:12:39 +0000398/// \brief Emits the preamble of the diagnostics file.
399void SDiagsWriter::EmitPreamble() {
Ted Kremenek4610ea22011-10-29 00:12:39 +0000400 // Emit the file header.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000401 State->Stream.Emit((unsigned)'D', 8);
402 State->Stream.Emit((unsigned)'I', 8);
403 State->Stream.Emit((unsigned)'A', 8);
404 State->Stream.Emit((unsigned)'G', 8);
Ted Kremenek868504a2011-11-05 00:09:53 +0000405
Ted Kremenek4610ea22011-10-29 00:12:39 +0000406 EmitBlockInfoBlock();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000407 EmitMetaBlock();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000408}
409
David Blaikieb44f0bf2017-01-04 22:36:43 +0000410static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
Ted Kremenek59f10252011-11-05 00:10:01 +0000411 using namespace llvm;
David Blaikieb44f0bf2017-01-04 22:36:43 +0000412 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
413 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
414 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
415 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
Ted Kremenek59f10252011-11-05 00:10:01 +0000416}
Ted Kremenekd89a8272011-11-05 03:34:23 +0000417
David Blaikieb44f0bf2017-01-04 22:36:43 +0000418static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
Ted Kremenekd89a8272011-11-05 03:34:23 +0000419 AddSourceLocationAbbrev(Abbrev);
420 AddSourceLocationAbbrev(Abbrev);
421}
422
Ted Kremenek4610ea22011-10-29 00:12:39 +0000423void SDiagsWriter::EmitBlockInfoBlock() {
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000424 State->Stream.EnterBlockInfoBlock();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000425
426 using namespace llvm;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000427 llvm::BitstreamWriter &Stream = State->Stream;
428 RecordData &Record = State->Record;
429 AbbreviationMap &Abbrevs = State->Abbrevs;
Ted Kremenekcc88d262011-11-08 20:27:29 +0000430
431 // ==---------------------------------------------------------------------==//
432 // The subsequent records and Abbrevs are for the "Meta" block.
433 // ==---------------------------------------------------------------------==//
434
435 EmitBlockID(BLOCK_META, "Meta", Stream, Record);
436 EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
David Blaikieb44f0bf2017-01-04 22:36:43 +0000437 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000438 Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
440 Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
441
Ted Kremenek4610ea22011-10-29 00:12:39 +0000442 // ==---------------------------------------------------------------------==//
443 // The subsequent records and Abbrevs are for the "Diagnostic" block.
444 // ==---------------------------------------------------------------------==//
445
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000446 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
447 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
Ted Kremenek59f10252011-11-05 00:10:01 +0000448 EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
Ted Kremenek319215062011-11-05 00:10:04 +0000449 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000450 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
Ted Kremenekf264a202011-11-05 00:10:11 +0000451 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000452 EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000453
Ted Kremenek4610ea22011-10-29 00:12:39 +0000454 // Emit abbreviation for RECORD_DIAG.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000455 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000456 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000458 AddSourceLocationAbbrev(*Abbrev);
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
Argyrios Kyrtzidise26aea52015-08-06 18:46:36 +0000461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Text size.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
463 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
Ted Kremenek59f10252011-11-05 00:10:01 +0000464
Ted Kremenek319215062011-11-05 00:10:04 +0000465 // Emit abbrevation for RECORD_CATEGORY.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000466 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek319215062011-11-05 00:10:04 +0000467 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
468 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
471 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
472
Ted Kremenek59f10252011-11-05 00:10:01 +0000473 // Emit abbrevation for RECORD_SOURCE_RANGE.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000474 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek59f10252011-11-05 00:10:01 +0000475 Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
David Blaikieb44f0bf2017-01-04 22:36:43 +0000476 AddRangeLocationAbbrev(*Abbrev);
Ted Kremenek59f10252011-11-05 00:10:01 +0000477 Abbrevs.set(RECORD_SOURCE_RANGE,
478 Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000479
480 // Emit the abbreviation for RECORD_DIAG_FLAG.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000481 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000482 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
486 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
487 Abbrev));
Ted Kremenekf264a202011-11-05 00:10:11 +0000488
489 // Emit the abbreviation for RECORD_FILENAME.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000490 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenekfce371a2011-11-05 00:09:43 +0000491 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
Ted Kremenekf264a202011-11-05 00:10:11 +0000492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
Hiroshi Inoued7b94d32017-05-30 05:06:46 +0000494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modification time.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
Ted Kremenekf264a202011-11-05 00:10:11 +0000497 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
Ted Kremenek4610ea22011-10-29 00:12:39 +0000498 Abbrev));
Ted Kremenekd89a8272011-11-05 03:34:23 +0000499
500 // Emit the abbreviation for RECORD_FIXIT.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000501 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenekd89a8272011-11-05 03:34:23 +0000502 Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
David Blaikieb44f0bf2017-01-04 22:36:43 +0000503 AddRangeLocationAbbrev(*Abbrev);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
506 Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
507 Abbrev));
Ted Kremenek4610ea22011-10-29 00:12:39 +0000508
509 Stream.ExitBlock();
510}
511
Ted Kremenekcc88d262011-11-08 20:27:29 +0000512void SDiagsWriter::EmitMetaBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000513 llvm::BitstreamWriter &Stream = State->Stream;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000514 AbbreviationMap &Abbrevs = State->Abbrevs;
515
Ted Kremenekcc88d262011-11-08 20:27:29 +0000516 Stream.EnterSubblock(BLOCK_META, 3);
Mehdi Amini57a41912015-09-10 01:46:39 +0000517 RecordData::value_type Record[] = {RECORD_VERSION, VersionNumber};
518 Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
Ted Kremenekcc88d262011-11-08 20:27:29 +0000519 Stream.ExitBlock();
520}
521
Ted Kremenek4548e042011-12-17 05:26:11 +0000522unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000523 if (!State->Categories.insert(category).second)
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000524 return category;
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000525
Ted Kremenek319215062011-11-05 00:10:04 +0000526 // We use a local version of 'Record' so that we can be generating
527 // another record when we lazily generate one for the category entry.
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000528 StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
Mehdi Amini57a41912015-09-10 01:46:39 +0000529 RecordData::value_type Record[] = {RECORD_CATEGORY, category, catName.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000530 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
531 catName);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000532
533 return category;
534}
535
536unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
Ted Kremenek4548e042011-12-17 05:26:11 +0000537 unsigned DiagID) {
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000538 if (DiagLevel == DiagnosticsEngine::Note)
539 return 0; // No flag for notes.
540
Ted Kremenek4548e042011-12-17 05:26:11 +0000541 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000542 return getEmitDiagnosticFlag(FlagName);
543}
544
545unsigned SDiagsWriter::getEmitDiagnosticFlag(StringRef FlagName) {
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000546 if (FlagName.empty())
547 return 0;
548
549 // Here we assume that FlagName points to static data whose pointer
550 // value is fixed. This allows us to unique by diagnostic groups.
551 const void *data = FlagName.data();
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000552 std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000553 if (entry.first == 0) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000554 entry.first = State->DiagFlags.size();
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000555 entry.second = FlagName;
556
557 // Lazily emit the string in a separate record.
Mehdi Amini57a41912015-09-10 01:46:39 +0000558 RecordData::value_type Record[] = {RECORD_DIAG_FLAG, entry.first,
559 FlagName.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000560 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
561 Record, FlagName);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000562 }
563
564 return entry.first;
Ted Kremenek319215062011-11-05 00:10:04 +0000565}
566
Ted Kremenek4610ea22011-10-29 00:12:39 +0000567void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
568 const Diagnostic &Info) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000569 // Enter the block for a non-note diagnostic immediately, rather than waiting
570 // for beginDiagnostic, in case associated notes are emitted before we get
571 // there.
Ted Kremenekf67bbca2011-11-05 00:09:47 +0000572 if (DiagLevel != DiagnosticsEngine::Note) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000573 if (State->EmittedAnyDiagBlocks)
Richard Smitha9f521f2012-08-21 03:11:53 +0000574 ExitDiagBlock();
575
576 EnterDiagBlock();
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000577 State->EmittedAnyDiagBlocks = true;
Ted Kremenekf67bbca2011-11-05 00:09:47 +0000578 }
Ted Kremenekd89a8272011-11-05 03:34:23 +0000579
Ted Kremenek4548e042011-12-17 05:26:11 +0000580 // Compute the diagnostic text.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000581 State->diagBuf.clear();
582 Info.FormatDiagnostic(State->diagBuf);
Ted Kremenek4548e042011-12-17 05:26:11 +0000583
Richard Smitha9f521f2012-08-21 03:11:53 +0000584 if (Info.getLocation().isInvalid()) {
585 // Special-case diagnostics with no location. We may not have entered a
586 // source file in this case, so we can't use the normal DiagnosticsRenderer
587 // machinery.
Ted Kremenekc6ebda12013-02-21 21:40:44 +0000588
589 // Make sure we bracket all notes as "sub-diagnostics". This matches
590 // the behavior in SDiagsRenderer::emitDiagnostic().
591 if (DiagLevel == DiagnosticsEngine::Note)
592 EnterDiagBlock();
593
Christof Doumafb4a0452017-06-27 09:50:38 +0000594 EmitDiagnosticMessage(FullSourceLoc(), PresumedLoc(), DiagLevel,
595 State->diagBuf, &Info);
Ted Kremenekc6ebda12013-02-21 21:40:44 +0000596
597 if (DiagLevel == DiagnosticsEngine::Note)
598 ExitDiagBlock();
599
Richard Smitha9f521f2012-08-21 03:11:53 +0000600 return;
601 }
602
603 assert(Info.hasSourceManager() && LangOpts &&
604 "Unexpected diagnostic with valid location outside of a source file");
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000605 SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
Christof Doumafb4a0452017-06-27 09:50:38 +0000606 Renderer.emitDiagnostic(
607 FullSourceLoc(Info.getLocation(), Info.getSourceManager()), DiagLevel,
608 State->diagBuf, Info.getRanges(), Info.getFixItHints(), &Info);
Ted Kremenek4548e042011-12-17 05:26:11 +0000609}
610
Jordan Rose7ef1c382014-03-03 18:29:52 +0000611static serialized_diags::Level getStableLevel(DiagnosticsEngine::Level Level) {
612 switch (Level) {
613#define CASE(X) case DiagnosticsEngine::X: return serialized_diags::X;
614 CASE(Ignored)
615 CASE(Note)
616 CASE(Remark)
617 CASE(Warning)
618 CASE(Error)
619 CASE(Fatal)
620#undef CASE
621 }
622
623 llvm_unreachable("invalid diagnostic level");
624}
625
Christof Doumafb4a0452017-06-27 09:50:38 +0000626void SDiagsWriter::EmitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
Richard Smitha9f521f2012-08-21 03:11:53 +0000627 DiagnosticsEngine::Level Level,
628 StringRef Message,
Richard Smitha9f521f2012-08-21 03:11:53 +0000629 DiagOrStoredDiag D) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000630 llvm::BitstreamWriter &Stream = State->Stream;
631 RecordData &Record = State->Record;
632 AbbreviationMap &Abbrevs = State->Abbrevs;
633
Richard Smitha9f521f2012-08-21 03:11:53 +0000634 // Emit the RECORD_DIAG record.
635 Record.clear();
636 Record.push_back(RECORD_DIAG);
Jordan Rose7ef1c382014-03-03 18:29:52 +0000637 Record.push_back(getStableLevel(Level));
Christof Doumafb4a0452017-06-27 09:50:38 +0000638 AddLocToRecord(Loc, PLoc, Record);
Richard Smitha9f521f2012-08-21 03:11:53 +0000639
640 if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
641 // Emit the category string lazily and get the category ID.
642 unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
643 Record.push_back(getEmitCategory(DiagID));
644 // Emit the diagnostic flag string lazily and get the mapped ID.
645 Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
646 } else {
647 Record.push_back(getEmitCategory());
648 Record.push_back(getEmitDiagnosticFlag(Level));
649 }
650
651 Record.push_back(Message.size());
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000652 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
Richard Smitha9f521f2012-08-21 03:11:53 +0000653}
654
Christof Doumafb4a0452017-06-27 09:50:38 +0000655void SDiagsRenderer::emitDiagnosticMessage(
656 FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
657 StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
658 DiagOrStoredDiag D) {
659 Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, D);
Richard Smitha9f521f2012-08-21 03:11:53 +0000660}
Ted Kremenek4548e042011-12-17 05:26:11 +0000661
Richard Smitha9f521f2012-08-21 03:11:53 +0000662void SDiagsWriter::EnterDiagBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000663 State->Stream.EnterSubblock(BLOCK_DIAG, 4);
Richard Smitha9f521f2012-08-21 03:11:53 +0000664}
Ted Kremenek4548e042011-12-17 05:26:11 +0000665
Richard Smitha9f521f2012-08-21 03:11:53 +0000666void SDiagsWriter::ExitDiagBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000667 State->Stream.ExitBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000668}
669
Ted Kremenek0964cca2012-02-14 02:46:00 +0000670void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
Ted Kremenek4548e042011-12-17 05:26:11 +0000671 DiagnosticsEngine::Level Level) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000672 if (Level == DiagnosticsEngine::Note)
673 Writer.EnterDiagBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000674}
675
Ted Kremenek0964cca2012-02-14 02:46:00 +0000676void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
Ted Kremenek4548e042011-12-17 05:26:11 +0000677 DiagnosticsEngine::Level Level) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000678 // Only end note diagnostics here, because we can't be sure when we've seen
679 // the last note associated with a non-note diagnostic.
680 if (Level == DiagnosticsEngine::Note)
681 Writer.ExitDiagBlock();
682}
683
684void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
685 ArrayRef<FixItHint> Hints,
686 const SourceManager &SM) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000687 llvm::BitstreamWriter &Stream = State->Stream;
688 RecordData &Record = State->Record;
689 AbbreviationMap &Abbrevs = State->Abbrevs;
690
Richard Smitha9f521f2012-08-21 03:11:53 +0000691 // Emit Source Ranges.
692 for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
693 I != E; ++I)
694 if (I->isValid())
695 EmitCharSourceRange(*I, SM);
696
697 // Emit FixIts.
698 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
699 I != E; ++I) {
700 const FixItHint &Fix = *I;
701 if (Fix.isNull())
702 continue;
703 Record.clear();
704 Record.push_back(RECORD_FIXIT);
705 AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
706 Record.push_back(Fix.CodeToInsert.size());
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000707 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
Richard Smitha9f521f2012-08-21 03:11:53 +0000708 Fix.CodeToInsert);
709 }
Ted Kremenek4548e042011-12-17 05:26:11 +0000710}
711
Christof Doumafb4a0452017-06-27 09:50:38 +0000712void SDiagsRenderer::emitCodeContext(FullSourceLoc Loc,
Ted Kremenek4548e042011-12-17 05:26:11 +0000713 DiagnosticsEngine::Level Level,
714 SmallVectorImpl<CharSourceRange> &Ranges,
Christof Doumafb4a0452017-06-27 09:50:38 +0000715 ArrayRef<FixItHint> Hints) {
716 Writer.EmitCodeContext(Ranges, Hints, Loc.getManager());
Ted Kremenek4610ea22011-10-29 00:12:39 +0000717}
718
Christof Doumafb4a0452017-06-27 09:50:38 +0000719void SDiagsRenderer::emitNote(FullSourceLoc Loc, StringRef Message) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000720 Writer.EnterDiagBlock();
Christof Doumafb4a0452017-06-27 09:50:38 +0000721 PresumedLoc PLoc = Loc.hasManager() ? Loc.getPresumedLoc() : PresumedLoc();
722 Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note, Message,
723 DiagOrStoredDiag());
Richard Smitha9f521f2012-08-21 03:11:53 +0000724 Writer.ExitDiagBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000725}
726
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000727DiagnosticsEngine *SDiagsWriter::getMetaDiags() {
728 // FIXME: It's slightly absurd to create a new diagnostics engine here, but
729 // the other options that are available today are worse:
730 //
731 // 1. Teach DiagnosticsConsumers to emit diagnostics to the engine they are a
732 // part of. The DiagnosticsEngine would need to know not to send
733 // diagnostics back to the consumer that failed. This would require us to
734 // rework ChainedDiagnosticsConsumer and teach the engine about multiple
735 // consumers, which is difficult today because most APIs interface with
736 // consumers rather than the engine itself.
737 //
738 // 2. Pass a DiagnosticsEngine to SDiagsWriter on creation - this would need
739 // to be distinct from the engine the writer was being added to and would
740 // normally not be used.
741 if (!State->MetaDiagnostics) {
742 IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs());
743 auto Client =
744 new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get());
745 State->MetaDiagnostics = llvm::make_unique<DiagnosticsEngine>(
746 IDs, State->DiagOpts.get(), Client);
747 }
748 return State->MetaDiagnostics.get();
749}
750
751void SDiagsWriter::RemoveOldDiagnostics() {
752 if (!llvm::sys::fs::remove(State->OutputFile))
753 return;
754
755 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
756 // Disable merging child records, as whatever is in this file may be
757 // misleading.
758 MergeChildRecords = false;
759}
760
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000761void SDiagsWriter::finish() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000762 // The original instance is responsible for writing the file.
763 if (!OriginalInstance)
764 return;
765
Richard Smitha9f521f2012-08-21 03:11:53 +0000766 // Finish off any diagnostic we were in the process of emitting.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000767 if (State->EmittedAnyDiagBlocks)
Richard Smitha9f521f2012-08-21 03:11:53 +0000768 ExitDiagBlock();
Ted Kremenekf264a202011-11-05 00:10:11 +0000769
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000770 if (MergeChildRecords) {
771 if (!State->EmittedAnyDiagBlocks)
772 // We have no diagnostics of our own, so we can just leave the child
773 // process' output alone
774 return;
Richard Smitha9f521f2012-08-21 03:11:53 +0000775
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000776 if (llvm::sys::fs::exists(State->OutputFile))
777 if (SDiagsMerger(*this).mergeRecordsFromFile(State->OutputFile.c_str()))
778 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
779 }
780
781 std::error_code EC;
782 auto OS = llvm::make_unique<llvm::raw_fd_ostream>(State->OutputFile.c_str(),
783 EC, llvm::sys::fs::F_None);
784 if (EC) {
785 getMetaDiags()->Report(diag::warn_fe_serialized_diag_failure)
786 << State->OutputFile << EC.message();
787 return;
788 }
789
790 // Write the generated bitstream to "Out".
791 OS->write((char *)&State->Buffer.front(), State->Buffer.size());
792 OS->flush();
793}
794
795std::error_code SDiagsMerger::visitStartOfDiagnostic() {
796 Writer.EnterDiagBlock();
797 return std::error_code();
798}
799
800std::error_code SDiagsMerger::visitEndOfDiagnostic() {
801 Writer.ExitDiagBlock();
802 return std::error_code();
803}
804
805std::error_code
806SDiagsMerger::visitSourceRangeRecord(const serialized_diags::Location &Start,
807 const serialized_diags::Location &End) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000808 RecordData::value_type Record[] = {
809 RECORD_SOURCE_RANGE, FileLookup[Start.FileID], Start.Line, Start.Col,
810 Start.Offset, FileLookup[End.FileID], End.Line, End.Col, End.Offset};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000811 Writer.State->Stream.EmitRecordWithAbbrev(
812 Writer.State->Abbrevs.get(RECORD_SOURCE_RANGE), Record);
813 return std::error_code();
814}
815
816std::error_code SDiagsMerger::visitDiagnosticRecord(
817 unsigned Severity, const serialized_diags::Location &Location,
818 unsigned Category, unsigned Flag, StringRef Message) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000819 RecordData::value_type Record[] = {
820 RECORD_DIAG, Severity, FileLookup[Location.FileID], Location.Line,
821 Location.Col, Location.Offset, CategoryLookup[Category],
822 Flag ? DiagFlagLookup[Flag] : 0, Message.size()};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000823
824 Writer.State->Stream.EmitRecordWithBlob(
Mehdi Amini57a41912015-09-10 01:46:39 +0000825 Writer.State->Abbrevs.get(RECORD_DIAG), Record, Message);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000826 return std::error_code();
827}
828
829std::error_code
830SDiagsMerger::visitFixitRecord(const serialized_diags::Location &Start,
831 const serialized_diags::Location &End,
832 StringRef Text) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000833 RecordData::value_type Record[] = {RECORD_FIXIT, FileLookup[Start.FileID],
834 Start.Line, Start.Col, Start.Offset,
835 FileLookup[End.FileID], End.Line, End.Col,
836 End.Offset, Text.size()};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000837
838 Writer.State->Stream.EmitRecordWithBlob(
839 Writer.State->Abbrevs.get(RECORD_FIXIT), Record, Text);
840 return std::error_code();
841}
842
843std::error_code SDiagsMerger::visitFilenameRecord(unsigned ID, unsigned Size,
844 unsigned Timestamp,
845 StringRef Name) {
846 FileLookup[ID] = Writer.getEmitFile(Name.str().c_str());
847 return std::error_code();
848}
849
850std::error_code SDiagsMerger::visitCategoryRecord(unsigned ID, StringRef Name) {
851 CategoryLookup[ID] = Writer.getEmitCategory(ID);
852 return std::error_code();
853}
854
855std::error_code SDiagsMerger::visitDiagFlagRecord(unsigned ID, StringRef Name) {
856 DiagFlagLookup[ID] = Writer.getEmitDiagnosticFlag(Name);
857 return std::error_code();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000858}