blob: 1bf10d27694560e38ec528c9e7a2fc87d313341a [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"
13#include "clang/Basic/FileManager.h"
14#include "clang/Basic/SourceManager.h"
15#include "clang/Basic/Version.h"
Ted Kremenek4548e042011-12-17 05:26:11 +000016#include "clang/Frontend/DiagnosticRenderer.h"
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000018#include "clang/Frontend/SerializedDiagnosticReader.h"
19#include "clang/Frontend/SerializedDiagnostics.h"
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000020#include "clang/Frontend/TextDiagnosticPrinter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "llvm/ADT/DenseSet.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000023#include "llvm/ADT/STLExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/raw_ostream.h"
27#include <vector>
Ted Kremenek4610ea22011-10-29 00:12:39 +000028
29using namespace clang;
Ted Kremenek337cd2a2011-11-05 00:09:57 +000030using namespace clang::serialized_diags;
Ted Kremenek4610ea22011-10-29 00:12:39 +000031
32namespace {
Ted Kremenek4610ea22011-10-29 00:12:39 +000033
34class AbbreviationMap {
35 llvm::DenseMap<unsigned, unsigned> Abbrevs;
36public:
37 AbbreviationMap() {}
38
39 void set(unsigned recordID, unsigned abbrevID) {
40 assert(Abbrevs.find(recordID) == Abbrevs.end()
41 && "Abbreviation already set.");
42 Abbrevs[recordID] = abbrevID;
43 }
44
45 unsigned get(unsigned recordID) {
46 assert(Abbrevs.find(recordID) != Abbrevs.end() &&
47 "Abbreviation not set.");
48 return Abbrevs[recordID];
49 }
50};
51
Dmitri Gribenkof8579502013-01-12 19:30:44 +000052typedef SmallVector<uint64_t, 64> RecordData;
53typedef SmallVectorImpl<uint64_t> RecordDataImpl;
Mehdi Amini57a41912015-09-10 01:46:39 +000054typedef ArrayRef<uint64_t> RecordDataRef;
Ted Kremenek4548e042011-12-17 05:26:11 +000055
56class SDiagsWriter;
57
Ted Kremenek0964cca2012-02-14 02:46:00 +000058class SDiagsRenderer : public DiagnosticNoteRenderer {
Ted Kremenek4548e042011-12-17 05:26:11 +000059 SDiagsWriter &Writer;
Ted Kremenek4548e042011-12-17 05:26:11 +000060public:
Richard Smitha9f521f2012-08-21 03:11:53 +000061 SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts,
Douglas Gregor811db4e2012-10-23 22:26:28 +000062 DiagnosticOptions *DiagOpts)
Richard Smitha9f521f2012-08-21 03:11:53 +000063 : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {}
Ted Kremenek4548e042011-12-17 05:26:11 +000064
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000065 ~SDiagsRenderer() override {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000066
Ted Kremenek4548e042011-12-17 05:26:11 +000067protected:
Craig Topperafa7cb32014-03-13 06:07:04 +000068 void emitDiagnosticMessage(SourceLocation Loc,
69 PresumedLoc PLoc,
70 DiagnosticsEngine::Level Level,
71 StringRef Message,
72 ArrayRef<CharSourceRange> Ranges,
73 const SourceManager *SM,
74 DiagOrStoredDiag D) override;
Richard Smitha9f521f2012-08-21 03:11:53 +000075
Craig Topperafa7cb32014-03-13 06:07:04 +000076 void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
77 DiagnosticsEngine::Level Level,
78 ArrayRef<CharSourceRange> Ranges,
79 const SourceManager &SM) override {}
Richard Smitha9f521f2012-08-21 03:11:53 +000080
Craig Topperafa7cb32014-03-13 06:07:04 +000081 void emitNote(SourceLocation Loc, StringRef Message,
82 const SourceManager *SM) override;
Richard Smitha9f521f2012-08-21 03:11:53 +000083
Craig Topperafa7cb32014-03-13 06:07:04 +000084 void emitCodeContext(SourceLocation Loc,
85 DiagnosticsEngine::Level Level,
86 SmallVectorImpl<CharSourceRange>& Ranges,
87 ArrayRef<FixItHint> Hints,
88 const SourceManager &SM) override;
89
90 void beginDiagnostic(DiagOrStoredDiag D,
91 DiagnosticsEngine::Level Level) override;
92 void endDiagnostic(DiagOrStoredDiag D,
93 DiagnosticsEngine::Level Level) override;
Ted Kremenek4548e042011-12-17 05:26:11 +000094};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000095
96typedef llvm::DenseMap<unsigned, unsigned> AbbrevLookup;
97
98class SDiagsMerger : SerializedDiagnosticReader {
99 SDiagsWriter &Writer;
100 AbbrevLookup FileLookup;
101 AbbrevLookup CategoryLookup;
102 AbbrevLookup DiagFlagLookup;
103
104public:
105 SDiagsMerger(SDiagsWriter &Writer)
106 : SerializedDiagnosticReader(), Writer(Writer) {}
107
108 std::error_code mergeRecordsFromFile(const char *File) {
109 return readDiagnostics(File);
110 }
111
112protected:
113 std::error_code visitStartOfDiagnostic() override;
114 std::error_code visitEndOfDiagnostic() override;
115 std::error_code visitCategoryRecord(unsigned ID, StringRef Name) override;
116 std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) override;
117 std::error_code visitDiagnosticRecord(
118 unsigned Severity, const serialized_diags::Location &Location,
119 unsigned Category, unsigned Flag, StringRef Message) override;
120 std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
121 unsigned Timestamp,
122 StringRef Name) override;
123 std::error_code visitFixitRecord(const serialized_diags::Location &Start,
124 const serialized_diags::Location &End,
125 StringRef CodeToInsert) override;
126 std::error_code
127 visitSourceRangeRecord(const serialized_diags::Location &Start,
128 const serialized_diags::Location &End) override;
129
130private:
131 std::error_code adjustSourceLocFilename(RecordData &Record,
132 unsigned int offset);
133
134 void adjustAbbrevID(RecordData &Record, AbbrevLookup &Lookup,
135 unsigned NewAbbrev);
136
137 void writeRecordWithAbbrev(unsigned ID, RecordData &Record);
138
139 void writeRecordWithBlob(unsigned ID, RecordData &Record, StringRef Blob);
140};
141
Ted Kremenek4610ea22011-10-29 00:12:39 +0000142class SDiagsWriter : public DiagnosticConsumer {
Ted Kremenek4548e042011-12-17 05:26:11 +0000143 friend class SDiagsRenderer;
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000144 friend class SDiagsMerger;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000145
146 struct SharedState;
147
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000148 explicit SDiagsWriter(IntrusiveRefCntPtr<SharedState> State)
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000149 : LangOpts(nullptr), OriginalInstance(false), MergeChildRecords(false),
150 State(State) {}
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000151
152public:
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000153 SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords)
David Blaikieeb62b822014-08-29 20:17:13 +0000154 : LangOpts(nullptr), OriginalInstance(true),
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000155 MergeChildRecords(MergeChildRecords),
156 State(new SharedState(File, Diags)) {
157 if (MergeChildRecords)
158 RemoveOldDiagnostics();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000159 EmitPreamble();
Devang Patel9957e8b2011-11-15 01:30:40 +0000160 }
Richard Smitha9f521f2012-08-21 03:11:53 +0000161
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000162 ~SDiagsWriter() override {}
Craig Topperafa7cb32014-03-13 06:07:04 +0000163
Ted Kremenek4610ea22011-10-29 00:12:39 +0000164 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Craig Topperafa7cb32014-03-13 06:07:04 +0000165 const Diagnostic &Info) override;
166
167 void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
Ted Kremenekd010ba42011-11-10 08:43:12 +0000168 LangOpts = &LO;
169 }
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000170
Craig Topperafa7cb32014-03-13 06:07:04 +0000171 void finish() override;
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000172
Ted Kremenek4610ea22011-10-29 00:12:39 +0000173private:
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000174 /// \brief Build a DiagnosticsEngine to emit diagnostics about the diagnostics
175 DiagnosticsEngine *getMetaDiags();
176
177 /// \brief Remove old copies of the serialized diagnostics. This is necessary
178 /// so that we can detect when subprocesses write diagnostics that we should
179 /// merge into our own.
180 void RemoveOldDiagnostics();
181
Ted Kremenek4610ea22011-10-29 00:12:39 +0000182 /// \brief Emit the preamble for the serialized diagnostics.
183 void EmitPreamble();
184
185 /// \brief Emit the BLOCKINFO block.
186 void EmitBlockInfoBlock();
Ted Kremenekf264a202011-11-05 00:10:11 +0000187
Ted Kremenekcc88d262011-11-08 20:27:29 +0000188 /// \brief Emit the META data block.
189 void EmitMetaBlock();
Richard Smitha9f521f2012-08-21 03:11:53 +0000190
191 /// \brief Start a DIAG block.
192 void EnterDiagBlock();
193
194 /// \brief End a DIAG block.
195 void ExitDiagBlock();
196
197 /// \brief Emit a DIAG record.
198 void EmitDiagnosticMessage(SourceLocation Loc,
199 PresumedLoc PLoc,
200 DiagnosticsEngine::Level Level,
201 StringRef Message,
202 const SourceManager *SM,
203 DiagOrStoredDiag D);
204
205 /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic.
206 void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
207 ArrayRef<FixItHint> Hints,
208 const SourceManager &SM);
209
Ted Kremenek59f10252011-11-05 00:10:01 +0000210 /// \brief Emit a record for a CharSourceRange.
Ted Kremenek4548e042011-12-17 05:26:11 +0000211 void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
Ted Kremenek59f10252011-11-05 00:10:01 +0000212
Ted Kremenek4548e042011-12-17 05:26:11 +0000213 /// \brief Emit the string information for the category.
214 unsigned getEmitCategory(unsigned category = 0);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000215
216 /// \brief Emit the string information for diagnostic flags.
217 unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
Ted Kremenek4548e042011-12-17 05:26:11 +0000218 unsigned DiagID = 0);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000219
220 unsigned getEmitDiagnosticFlag(StringRef DiagName);
221
Ted Kremenekf264a202011-11-05 00:10:11 +0000222 /// \brief Emit (lazily) the file string and retrieved the file identifier.
Ted Kremenek4548e042011-12-17 05:26:11 +0000223 unsigned getEmitFile(const char *Filename);
224
225 /// \brief Add SourceLocation information the specified record.
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000226 void AddLocToRecord(SourceLocation Loc, const SourceManager *SM,
Ted Kremenek4548e042011-12-17 05:26:11 +0000227 PresumedLoc PLoc, RecordDataImpl &Record,
228 unsigned TokSize = 0);
229
Ted Kremenekf264a202011-11-05 00:10:11 +0000230 /// \brief Add SourceLocation information the specified record.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000231 void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000232 const SourceManager *SM,
Ted Kremenek4548e042011-12-17 05:26:11 +0000233 unsigned TokSize = 0) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000234 AddLocToRecord(Loc, SM, SM ? SM->getPresumedLoc(Loc) : PresumedLoc(),
235 Record, TokSize);
Ted Kremenek4548e042011-12-17 05:26:11 +0000236 }
Ted Kremenekf264a202011-11-05 00:10:11 +0000237
Ted Kremenekd89a8272011-11-05 03:34:23 +0000238 /// \brief Add CharSourceRange information the specified record.
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000239 void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000240 const SourceManager &SM);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000241
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000242 /// \brief Language options, which can differ from one clone of this client
243 /// to another.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000244 const LangOptions *LangOpts;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000245
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000246 /// \brief Whether this is the original instance (rather than one of its
247 /// clones), responsible for writing the file at the end.
248 bool OriginalInstance;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000249
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000250 /// \brief Whether this instance should aggregate diagnostics that are
251 /// generated from child processes.
252 bool MergeChildRecords;
253
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000254 /// \brief State that is shared among the various clones of this diagnostic
255 /// consumer.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000256 struct SharedState : RefCountedBase<SharedState> {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000257 SharedState(StringRef File, DiagnosticOptions *Diags)
258 : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()),
David Blaikieeb62b822014-08-29 20:17:13 +0000259 EmittedAnyDiagBlocks(false) {}
Ted Kremenek4610ea22011-10-29 00:12:39 +0000260
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000261 /// \brief Diagnostic options.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000262 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000263
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000264 /// \brief The byte buffer for the serialized content.
265 SmallString<1024> Buffer;
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000266
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000267 /// \brief The BitStreamWriter for the serialized diagnostics.
268 llvm::BitstreamWriter Stream;
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000269
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000270 /// \brief The name of the diagnostics file.
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000271 std::string OutputFile;
Richard Smitha9f521f2012-08-21 03:11:53 +0000272
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000273 /// \brief The set of constructed record abbreviations.
274 AbbreviationMap Abbrevs;
275
276 /// \brief A utility buffer for constructing record content.
277 RecordData Record;
278
279 /// \brief A text buffer for rendering diagnostic text.
280 SmallString<256> diagBuf;
281
282 /// \brief The collection of diagnostic categories used.
283 llvm::DenseSet<unsigned> Categories;
284
285 /// \brief The collection of files used.
286 llvm::DenseMap<const char *, unsigned> Files;
287
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000288 typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000289 DiagFlagsTy;
290
291 /// \brief Map for uniquing strings.
292 DiagFlagsTy DiagFlags;
293
294 /// \brief Whether we have already started emission of any DIAG blocks. Once
295 /// this becomes \c true, we never close a DIAG block until we know that we're
296 /// starting another one or we're done.
297 bool EmittedAnyDiagBlocks;
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000298
299 /// \brief Engine for emitting diagnostics about the diagnostics.
300 std::unique_ptr<DiagnosticsEngine> MetaDiagnostics;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000301 };
302
303 /// \brief State shared among the various clones of this diagnostic consumer.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000304 IntrusiveRefCntPtr<SharedState> State;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000305};
306} // end anonymous namespace
307
308namespace clang {
309namespace serialized_diags {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000310std::unique_ptr<DiagnosticConsumer>
311create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords) {
312 return llvm::make_unique<SDiagsWriter>(OutputFile, Diags, MergeChildRecords);
Ted Kremenek4610ea22011-10-29 00:12:39 +0000313}
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000314
Ted Kremenek4610ea22011-10-29 00:12:39 +0000315} // end namespace serialized_diags
316} // end namespace clang
317
318//===----------------------------------------------------------------------===//
319// Serialization methods.
320//===----------------------------------------------------------------------===//
321
322/// \brief Emits a block ID in the BLOCKINFO block.
323static void EmitBlockID(unsigned ID, const char *Name,
324 llvm::BitstreamWriter &Stream,
325 RecordDataImpl &Record) {
326 Record.clear();
327 Record.push_back(ID);
328 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
329
330 // Emit the block name if present.
Craig Topper49a27902014-05-22 04:46:25 +0000331 if (!Name || Name[0] == 0)
Ted Kremenek4610ea22011-10-29 00:12:39 +0000332 return;
333
334 Record.clear();
335
336 while (*Name)
337 Record.push_back(*Name++);
338
339 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
340}
341
342/// \brief Emits a record ID in the BLOCKINFO block.
343static void EmitRecordID(unsigned ID, const char *Name,
344 llvm::BitstreamWriter &Stream,
345 RecordDataImpl &Record){
346 Record.clear();
347 Record.push_back(ID);
348
349 while (*Name)
350 Record.push_back(*Name++);
351
352 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
353}
354
Ted Kremenekf264a202011-11-05 00:10:11 +0000355void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000356 const SourceManager *SM,
Ted Kremenek4548e042011-12-17 05:26:11 +0000357 PresumedLoc PLoc,
Ted Kremenekd010ba42011-11-10 08:43:12 +0000358 RecordDataImpl &Record,
359 unsigned TokSize) {
Ted Kremenek4548e042011-12-17 05:26:11 +0000360 if (PLoc.isInvalid()) {
Ted Kremenek59f10252011-11-05 00:10:01 +0000361 // Emit a "sentinel" location.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000362 Record.push_back((unsigned)0); // File.
363 Record.push_back((unsigned)0); // Line.
364 Record.push_back((unsigned)0); // Column.
365 Record.push_back((unsigned)0); // Offset.
Ted Kremenek59f10252011-11-05 00:10:01 +0000366 return;
367 }
368
Ted Kremenek4548e042011-12-17 05:26:11 +0000369 Record.push_back(getEmitFile(PLoc.getFilename()));
370 Record.push_back(PLoc.getLine());
371 Record.push_back(PLoc.getColumn()+TokSize);
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000372 Record.push_back(SM->getFileOffset(Loc));
Ted Kremenek59f10252011-11-05 00:10:01 +0000373}
374
Ted Kremenekd89a8272011-11-05 03:34:23 +0000375void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000376 RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000377 const SourceManager &SM) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000378 AddLocToRecord(Range.getBegin(), Record, &SM);
Ted Kremenekd010ba42011-11-10 08:43:12 +0000379 unsigned TokSize = 0;
380 if (Range.isTokenRange())
381 TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000382 SM, *LangOpts);
Ted Kremenekd010ba42011-11-10 08:43:12 +0000383
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000384 AddLocToRecord(Range.getEnd(), Record, &SM, TokSize);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000385}
386
Ted Kremenek4548e042011-12-17 05:26:11 +0000387unsigned SDiagsWriter::getEmitFile(const char *FileName){
388 if (!FileName)
Ted Kremenekf264a202011-11-05 00:10:11 +0000389 return 0;
390
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000391 unsigned &entry = State->Files[FileName];
Ted Kremenekf264a202011-11-05 00:10:11 +0000392 if (entry)
393 return entry;
394
395 // Lazily generate the record for the file.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000396 entry = State->Files.size();
Ted Kremenek4548e042011-12-17 05:26:11 +0000397 StringRef Name(FileName);
Mehdi Amini57a41912015-09-10 01:46:39 +0000398 RecordData::value_type Record[] = {RECORD_FILENAME, entry, 0 /* For legacy */,
399 0 /* For legacy */, Name.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000400 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
401 Name);
Ted Kremenekf264a202011-11-05 00:10:11 +0000402
403 return entry;
404}
405
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000406void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
Ted Kremenek4548e042011-12-17 05:26:11 +0000407 const SourceManager &SM) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000408 State->Record.clear();
409 State->Record.push_back(RECORD_SOURCE_RANGE);
410 AddCharSourceRangeToRecord(R, State->Record, SM);
411 State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000412 State->Record);
Ted Kremenek59f10252011-11-05 00:10:01 +0000413}
414
Ted Kremenek4610ea22011-10-29 00:12:39 +0000415/// \brief Emits the preamble of the diagnostics file.
416void SDiagsWriter::EmitPreamble() {
Ted Kremenek4610ea22011-10-29 00:12:39 +0000417 // Emit the file header.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000418 State->Stream.Emit((unsigned)'D', 8);
419 State->Stream.Emit((unsigned)'I', 8);
420 State->Stream.Emit((unsigned)'A', 8);
421 State->Stream.Emit((unsigned)'G', 8);
Ted Kremenek868504a2011-11-05 00:09:53 +0000422
Ted Kremenek4610ea22011-10-29 00:12:39 +0000423 EmitBlockInfoBlock();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000424 EmitMetaBlock();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000425}
426
Ted Kremenek59f10252011-11-05 00:10:01 +0000427static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
428 using namespace llvm;
Ted Kremenekf264a202011-11-05 00:10:11 +0000429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
Ted Kremenek59f10252011-11-05 00:10:01 +0000430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
433}
Ted Kremenekd89a8272011-11-05 03:34:23 +0000434
435static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) {
436 AddSourceLocationAbbrev(Abbrev);
437 AddSourceLocationAbbrev(Abbrev);
438}
439
Ted Kremenek4610ea22011-10-29 00:12:39 +0000440void SDiagsWriter::EmitBlockInfoBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000441 State->Stream.EnterBlockInfoBlock(3);
Ted Kremenekcc88d262011-11-08 20:27:29 +0000442
443 using namespace llvm;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000444 llvm::BitstreamWriter &Stream = State->Stream;
445 RecordData &Record = State->Record;
446 AbbreviationMap &Abbrevs = State->Abbrevs;
Ted Kremenekcc88d262011-11-08 20:27:29 +0000447
448 // ==---------------------------------------------------------------------==//
449 // The subsequent records and Abbrevs are for the "Meta" block.
450 // ==---------------------------------------------------------------------==//
451
452 EmitBlockID(BLOCK_META, "Meta", Stream, Record);
453 EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
455 Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
457 Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
458
Ted Kremenek4610ea22011-10-29 00:12:39 +0000459 // ==---------------------------------------------------------------------==//
460 // The subsequent records and Abbrevs are for the "Diagnostic" block.
461 // ==---------------------------------------------------------------------==//
462
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000463 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
464 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
Ted Kremenek59f10252011-11-05 00:10:01 +0000465 EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
Ted Kremenek319215062011-11-05 00:10:04 +0000466 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000467 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
Ted Kremenekf264a202011-11-05 00:10:11 +0000468 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000469 EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000470
Ted Kremenek4610ea22011-10-29 00:12:39 +0000471 // Emit abbreviation for RECORD_DIAG.
Ted Kremenekcc88d262011-11-08 20:27:29 +0000472 Abbrev = new BitCodeAbbrev();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000473 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
Ted Kremenek59f10252011-11-05 00:10:01 +0000475 AddSourceLocationAbbrev(Abbrev);
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
Argyrios Kyrtzidise26aea52015-08-06 18:46:36 +0000478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Text size.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
480 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
Ted Kremenek59f10252011-11-05 00:10:01 +0000481
Ted Kremenek319215062011-11-05 00:10:04 +0000482 // Emit abbrevation for RECORD_CATEGORY.
483 Abbrev = new BitCodeAbbrev();
484 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
487 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
488 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
489
Ted Kremenek59f10252011-11-05 00:10:01 +0000490 // Emit abbrevation for RECORD_SOURCE_RANGE.
491 Abbrev = new BitCodeAbbrev();
492 Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
Ted Kremenekd89a8272011-11-05 03:34:23 +0000493 AddRangeLocationAbbrev(Abbrev);
Ted Kremenek59f10252011-11-05 00:10:01 +0000494 Abbrevs.set(RECORD_SOURCE_RANGE,
495 Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000496
497 // Emit the abbreviation for RECORD_DIAG_FLAG.
498 Abbrev = new BitCodeAbbrev();
499 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
503 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
504 Abbrev));
Ted Kremenekf264a202011-11-05 00:10:11 +0000505
506 // Emit the abbreviation for RECORD_FILENAME.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000507 Abbrev = new BitCodeAbbrev();
Ted Kremenekfce371a2011-11-05 00:09:43 +0000508 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
Ted Kremenekf264a202011-11-05 00:10:11 +0000509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
Ted Kremenekf264a202011-11-05 00:10:11 +0000514 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
Ted Kremenek4610ea22011-10-29 00:12:39 +0000515 Abbrev));
Ted Kremenekd89a8272011-11-05 03:34:23 +0000516
517 // Emit the abbreviation for RECORD_FIXIT.
518 Abbrev = new BitCodeAbbrev();
519 Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
520 AddRangeLocationAbbrev(Abbrev);
521 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
523 Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
524 Abbrev));
Ted Kremenek4610ea22011-10-29 00:12:39 +0000525
526 Stream.ExitBlock();
527}
528
Ted Kremenekcc88d262011-11-08 20:27:29 +0000529void SDiagsWriter::EmitMetaBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000530 llvm::BitstreamWriter &Stream = State->Stream;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000531 AbbreviationMap &Abbrevs = State->Abbrevs;
532
Ted Kremenekcc88d262011-11-08 20:27:29 +0000533 Stream.EnterSubblock(BLOCK_META, 3);
Mehdi Amini57a41912015-09-10 01:46:39 +0000534 RecordData::value_type Record[] = {RECORD_VERSION, VersionNumber};
535 Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
Ted Kremenekcc88d262011-11-08 20:27:29 +0000536 Stream.ExitBlock();
537}
538
Ted Kremenek4548e042011-12-17 05:26:11 +0000539unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000540 if (!State->Categories.insert(category).second)
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000541 return category;
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000542
Ted Kremenek319215062011-11-05 00:10:04 +0000543 // We use a local version of 'Record' so that we can be generating
544 // another record when we lazily generate one for the category entry.
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000545 StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
Mehdi Amini57a41912015-09-10 01:46:39 +0000546 RecordData::value_type Record[] = {RECORD_CATEGORY, category, catName.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000547 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
548 catName);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000549
550 return category;
551}
552
553unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
Ted Kremenek4548e042011-12-17 05:26:11 +0000554 unsigned DiagID) {
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000555 if (DiagLevel == DiagnosticsEngine::Note)
556 return 0; // No flag for notes.
557
Ted Kremenek4548e042011-12-17 05:26:11 +0000558 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000559 return getEmitDiagnosticFlag(FlagName);
560}
561
562unsigned SDiagsWriter::getEmitDiagnosticFlag(StringRef FlagName) {
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000563 if (FlagName.empty())
564 return 0;
565
566 // Here we assume that FlagName points to static data whose pointer
567 // value is fixed. This allows us to unique by diagnostic groups.
568 const void *data = FlagName.data();
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000569 std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000570 if (entry.first == 0) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000571 entry.first = State->DiagFlags.size();
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000572 entry.second = FlagName;
573
574 // Lazily emit the string in a separate record.
Mehdi Amini57a41912015-09-10 01:46:39 +0000575 RecordData::value_type Record[] = {RECORD_DIAG_FLAG, entry.first,
576 FlagName.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000577 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
578 Record, FlagName);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000579 }
580
581 return entry.first;
Ted Kremenek319215062011-11-05 00:10:04 +0000582}
583
Ted Kremenek4610ea22011-10-29 00:12:39 +0000584void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
585 const Diagnostic &Info) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000586 // Enter the block for a non-note diagnostic immediately, rather than waiting
587 // for beginDiagnostic, in case associated notes are emitted before we get
588 // there.
Ted Kremenekf67bbca2011-11-05 00:09:47 +0000589 if (DiagLevel != DiagnosticsEngine::Note) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000590 if (State->EmittedAnyDiagBlocks)
Richard Smitha9f521f2012-08-21 03:11:53 +0000591 ExitDiagBlock();
592
593 EnterDiagBlock();
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000594 State->EmittedAnyDiagBlocks = true;
Ted Kremenekf67bbca2011-11-05 00:09:47 +0000595 }
Ted Kremenekd89a8272011-11-05 03:34:23 +0000596
Ted Kremenek4548e042011-12-17 05:26:11 +0000597 // Compute the diagnostic text.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000598 State->diagBuf.clear();
599 Info.FormatDiagnostic(State->diagBuf);
Ted Kremenek4548e042011-12-17 05:26:11 +0000600
Richard Smitha9f521f2012-08-21 03:11:53 +0000601 if (Info.getLocation().isInvalid()) {
602 // Special-case diagnostics with no location. We may not have entered a
603 // source file in this case, so we can't use the normal DiagnosticsRenderer
604 // machinery.
Ted Kremenekc6ebda12013-02-21 21:40:44 +0000605
606 // Make sure we bracket all notes as "sub-diagnostics". This matches
607 // the behavior in SDiagsRenderer::emitDiagnostic().
608 if (DiagLevel == DiagnosticsEngine::Note)
609 EnterDiagBlock();
610
Richard Smitha9f521f2012-08-21 03:11:53 +0000611 EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel,
Craig Topper49a27902014-05-22 04:46:25 +0000612 State->diagBuf, nullptr, &Info);
Ted Kremenekc6ebda12013-02-21 21:40:44 +0000613
614 if (DiagLevel == DiagnosticsEngine::Note)
615 ExitDiagBlock();
616
Richard Smitha9f521f2012-08-21 03:11:53 +0000617 return;
618 }
619
620 assert(Info.hasSourceManager() && LangOpts &&
621 "Unexpected diagnostic with valid location outside of a source file");
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000622 SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
Ted Kremenek4548e042011-12-17 05:26:11 +0000623 Renderer.emitDiagnostic(Info.getLocation(), DiagLevel,
Yaron Keren92e1b622015-03-18 10:17:07 +0000624 State->diagBuf,
Ted Kremenek4548e042011-12-17 05:26:11 +0000625 Info.getRanges(),
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000626 Info.getFixItHints(),
Richard Smitha9f521f2012-08-21 03:11:53 +0000627 &Info.getSourceManager(),
Ted Kremenek4548e042011-12-17 05:26:11 +0000628 &Info);
629}
630
Jordan Rose7ef1c382014-03-03 18:29:52 +0000631static serialized_diags::Level getStableLevel(DiagnosticsEngine::Level Level) {
632 switch (Level) {
633#define CASE(X) case DiagnosticsEngine::X: return serialized_diags::X;
634 CASE(Ignored)
635 CASE(Note)
636 CASE(Remark)
637 CASE(Warning)
638 CASE(Error)
639 CASE(Fatal)
640#undef CASE
641 }
642
643 llvm_unreachable("invalid diagnostic level");
644}
645
Richard Smitha9f521f2012-08-21 03:11:53 +0000646void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc,
647 PresumedLoc PLoc,
648 DiagnosticsEngine::Level Level,
649 StringRef Message,
650 const SourceManager *SM,
651 DiagOrStoredDiag D) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000652 llvm::BitstreamWriter &Stream = State->Stream;
653 RecordData &Record = State->Record;
654 AbbreviationMap &Abbrevs = State->Abbrevs;
655
Richard Smitha9f521f2012-08-21 03:11:53 +0000656 // Emit the RECORD_DIAG record.
657 Record.clear();
658 Record.push_back(RECORD_DIAG);
Jordan Rose7ef1c382014-03-03 18:29:52 +0000659 Record.push_back(getStableLevel(Level));
Richard Smitha9f521f2012-08-21 03:11:53 +0000660 AddLocToRecord(Loc, SM, PLoc, Record);
661
662 if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
663 // Emit the category string lazily and get the category ID.
664 unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
665 Record.push_back(getEmitCategory(DiagID));
666 // Emit the diagnostic flag string lazily and get the mapped ID.
667 Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
668 } else {
669 Record.push_back(getEmitCategory());
670 Record.push_back(getEmitDiagnosticFlag(Level));
671 }
672
673 Record.push_back(Message.size());
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000674 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
Richard Smitha9f521f2012-08-21 03:11:53 +0000675}
676
Ted Kremenek4548e042011-12-17 05:26:11 +0000677void
678SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc,
679 PresumedLoc PLoc,
680 DiagnosticsEngine::Level Level,
681 StringRef Message,
682 ArrayRef<clang::CharSourceRange> Ranges,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000683 const SourceManager *SM,
Ted Kremenek0964cca2012-02-14 02:46:00 +0000684 DiagOrStoredDiag D) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000685 Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D);
686}
Ted Kremenek4548e042011-12-17 05:26:11 +0000687
Richard Smitha9f521f2012-08-21 03:11:53 +0000688void SDiagsWriter::EnterDiagBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000689 State->Stream.EnterSubblock(BLOCK_DIAG, 4);
Richard Smitha9f521f2012-08-21 03:11:53 +0000690}
Ted Kremenek4548e042011-12-17 05:26:11 +0000691
Richard Smitha9f521f2012-08-21 03:11:53 +0000692void SDiagsWriter::ExitDiagBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000693 State->Stream.ExitBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000694}
695
Ted Kremenek0964cca2012-02-14 02:46:00 +0000696void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
Ted Kremenek4548e042011-12-17 05:26:11 +0000697 DiagnosticsEngine::Level Level) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000698 if (Level == DiagnosticsEngine::Note)
699 Writer.EnterDiagBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000700}
701
Ted Kremenek0964cca2012-02-14 02:46:00 +0000702void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
Ted Kremenek4548e042011-12-17 05:26:11 +0000703 DiagnosticsEngine::Level Level) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000704 // Only end note diagnostics here, because we can't be sure when we've seen
705 // the last note associated with a non-note diagnostic.
706 if (Level == DiagnosticsEngine::Note)
707 Writer.ExitDiagBlock();
708}
709
710void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
711 ArrayRef<FixItHint> Hints,
712 const SourceManager &SM) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000713 llvm::BitstreamWriter &Stream = State->Stream;
714 RecordData &Record = State->Record;
715 AbbreviationMap &Abbrevs = State->Abbrevs;
716
Richard Smitha9f521f2012-08-21 03:11:53 +0000717 // Emit Source Ranges.
718 for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
719 I != E; ++I)
720 if (I->isValid())
721 EmitCharSourceRange(*I, SM);
722
723 // Emit FixIts.
724 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
725 I != E; ++I) {
726 const FixItHint &Fix = *I;
727 if (Fix.isNull())
728 continue;
729 Record.clear();
730 Record.push_back(RECORD_FIXIT);
731 AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
732 Record.push_back(Fix.CodeToInsert.size());
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000733 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
Richard Smitha9f521f2012-08-21 03:11:53 +0000734 Fix.CodeToInsert);
735 }
Ted Kremenek4548e042011-12-17 05:26:11 +0000736}
737
738void SDiagsRenderer::emitCodeContext(SourceLocation Loc,
739 DiagnosticsEngine::Level Level,
740 SmallVectorImpl<CharSourceRange> &Ranges,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000741 ArrayRef<FixItHint> Hints,
Richard Smitha9f521f2012-08-21 03:11:53 +0000742 const SourceManager &SM) {
743 Writer.EmitCodeContext(Ranges, Hints, SM);
Ted Kremenek4610ea22011-10-29 00:12:39 +0000744}
745
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000746void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message,
747 const SourceManager *SM) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000748 Writer.EnterDiagBlock();
749 PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc();
750 Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note,
751 Message, SM, DiagOrStoredDiag());
752 Writer.ExitDiagBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000753}
754
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000755DiagnosticsEngine *SDiagsWriter::getMetaDiags() {
756 // FIXME: It's slightly absurd to create a new diagnostics engine here, but
757 // the other options that are available today are worse:
758 //
759 // 1. Teach DiagnosticsConsumers to emit diagnostics to the engine they are a
760 // part of. The DiagnosticsEngine would need to know not to send
761 // diagnostics back to the consumer that failed. This would require us to
762 // rework ChainedDiagnosticsConsumer and teach the engine about multiple
763 // consumers, which is difficult today because most APIs interface with
764 // consumers rather than the engine itself.
765 //
766 // 2. Pass a DiagnosticsEngine to SDiagsWriter on creation - this would need
767 // to be distinct from the engine the writer was being added to and would
768 // normally not be used.
769 if (!State->MetaDiagnostics) {
770 IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs());
771 auto Client =
772 new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get());
773 State->MetaDiagnostics = llvm::make_unique<DiagnosticsEngine>(
774 IDs, State->DiagOpts.get(), Client);
775 }
776 return State->MetaDiagnostics.get();
777}
778
779void SDiagsWriter::RemoveOldDiagnostics() {
780 if (!llvm::sys::fs::remove(State->OutputFile))
781 return;
782
783 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
784 // Disable merging child records, as whatever is in this file may be
785 // misleading.
786 MergeChildRecords = false;
787}
788
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000789void SDiagsWriter::finish() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000790 // The original instance is responsible for writing the file.
791 if (!OriginalInstance)
792 return;
793
Richard Smitha9f521f2012-08-21 03:11:53 +0000794 // Finish off any diagnostic we were in the process of emitting.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000795 if (State->EmittedAnyDiagBlocks)
Richard Smitha9f521f2012-08-21 03:11:53 +0000796 ExitDiagBlock();
Ted Kremenekf264a202011-11-05 00:10:11 +0000797
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000798 if (MergeChildRecords) {
799 if (!State->EmittedAnyDiagBlocks)
800 // We have no diagnostics of our own, so we can just leave the child
801 // process' output alone
802 return;
Richard Smitha9f521f2012-08-21 03:11:53 +0000803
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000804 if (llvm::sys::fs::exists(State->OutputFile))
805 if (SDiagsMerger(*this).mergeRecordsFromFile(State->OutputFile.c_str()))
806 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
807 }
808
809 std::error_code EC;
810 auto OS = llvm::make_unique<llvm::raw_fd_ostream>(State->OutputFile.c_str(),
811 EC, llvm::sys::fs::F_None);
812 if (EC) {
813 getMetaDiags()->Report(diag::warn_fe_serialized_diag_failure)
814 << State->OutputFile << EC.message();
815 return;
816 }
817
818 // Write the generated bitstream to "Out".
819 OS->write((char *)&State->Buffer.front(), State->Buffer.size());
820 OS->flush();
821}
822
823std::error_code SDiagsMerger::visitStartOfDiagnostic() {
824 Writer.EnterDiagBlock();
825 return std::error_code();
826}
827
828std::error_code SDiagsMerger::visitEndOfDiagnostic() {
829 Writer.ExitDiagBlock();
830 return std::error_code();
831}
832
833std::error_code
834SDiagsMerger::visitSourceRangeRecord(const serialized_diags::Location &Start,
835 const serialized_diags::Location &End) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000836 RecordData::value_type Record[] = {
837 RECORD_SOURCE_RANGE, FileLookup[Start.FileID], Start.Line, Start.Col,
838 Start.Offset, FileLookup[End.FileID], End.Line, End.Col, End.Offset};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000839 Writer.State->Stream.EmitRecordWithAbbrev(
840 Writer.State->Abbrevs.get(RECORD_SOURCE_RANGE), Record);
841 return std::error_code();
842}
843
844std::error_code SDiagsMerger::visitDiagnosticRecord(
845 unsigned Severity, const serialized_diags::Location &Location,
846 unsigned Category, unsigned Flag, StringRef Message) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000847 RecordData::value_type Record[] = {
848 RECORD_DIAG, Severity, FileLookup[Location.FileID], Location.Line,
849 Location.Col, Location.Offset, CategoryLookup[Category],
850 Flag ? DiagFlagLookup[Flag] : 0, Message.size()};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000851
852 Writer.State->Stream.EmitRecordWithBlob(
Mehdi Amini57a41912015-09-10 01:46:39 +0000853 Writer.State->Abbrevs.get(RECORD_DIAG), Record, Message);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000854 return std::error_code();
855}
856
857std::error_code
858SDiagsMerger::visitFixitRecord(const serialized_diags::Location &Start,
859 const serialized_diags::Location &End,
860 StringRef Text) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000861 RecordData::value_type Record[] = {RECORD_FIXIT, FileLookup[Start.FileID],
862 Start.Line, Start.Col, Start.Offset,
863 FileLookup[End.FileID], End.Line, End.Col,
864 End.Offset, Text.size()};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000865
866 Writer.State->Stream.EmitRecordWithBlob(
867 Writer.State->Abbrevs.get(RECORD_FIXIT), Record, Text);
868 return std::error_code();
869}
870
871std::error_code SDiagsMerger::visitFilenameRecord(unsigned ID, unsigned Size,
872 unsigned Timestamp,
873 StringRef Name) {
874 FileLookup[ID] = Writer.getEmitFile(Name.str().c_str());
875 return std::error_code();
876}
877
878std::error_code SDiagsMerger::visitCategoryRecord(unsigned ID, StringRef Name) {
879 CategoryLookup[ID] = Writer.getEmitCategory(ID);
880 return std::error_code();
881}
882
883std::error_code SDiagsMerger::visitDiagFlagRecord(unsigned ID, StringRef Name) {
884 DiagFlagLookup[ID] = Writer.getEmitDiagnosticFlag(Name);
885 return std::error_code();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000886}