blob: b5a5acd8ad9adfbcdff1029159f72ed29f4f224d [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:
Craig Topperafa7cb32014-03-13 06:07:04 +000066 void emitDiagnosticMessage(SourceLocation Loc,
67 PresumedLoc PLoc,
68 DiagnosticsEngine::Level Level,
69 StringRef Message,
70 ArrayRef<CharSourceRange> Ranges,
71 const SourceManager *SM,
72 DiagOrStoredDiag D) override;
Richard Smitha9f521f2012-08-21 03:11:53 +000073
Craig Topperafa7cb32014-03-13 06:07:04 +000074 void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
75 DiagnosticsEngine::Level Level,
76 ArrayRef<CharSourceRange> Ranges,
77 const SourceManager &SM) override {}
Richard Smitha9f521f2012-08-21 03:11:53 +000078
Craig Topperafa7cb32014-03-13 06:07:04 +000079 void emitNote(SourceLocation Loc, StringRef Message,
80 const SourceManager *SM) override;
Richard Smitha9f521f2012-08-21 03:11:53 +000081
Craig Topperafa7cb32014-03-13 06:07:04 +000082 void emitCodeContext(SourceLocation Loc,
83 DiagnosticsEngine::Level Level,
84 SmallVectorImpl<CharSourceRange>& Ranges,
85 ArrayRef<FixItHint> Hints,
86 const SourceManager &SM) override;
87
88 void beginDiagnostic(DiagOrStoredDiag D,
89 DiagnosticsEngine::Level Level) override;
90 void endDiagnostic(DiagOrStoredDiag D,
91 DiagnosticsEngine::Level Level) override;
Ted Kremenek4548e042011-12-17 05:26:11 +000092};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +000093
94typedef llvm::DenseMap<unsigned, unsigned> AbbrevLookup;
95
96class SDiagsMerger : SerializedDiagnosticReader {
97 SDiagsWriter &Writer;
98 AbbrevLookup FileLookup;
99 AbbrevLookup CategoryLookup;
100 AbbrevLookup DiagFlagLookup;
101
102public:
103 SDiagsMerger(SDiagsWriter &Writer)
104 : SerializedDiagnosticReader(), Writer(Writer) {}
105
106 std::error_code mergeRecordsFromFile(const char *File) {
107 return readDiagnostics(File);
108 }
109
110protected:
111 std::error_code visitStartOfDiagnostic() override;
112 std::error_code visitEndOfDiagnostic() override;
113 std::error_code visitCategoryRecord(unsigned ID, StringRef Name) override;
114 std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) override;
115 std::error_code visitDiagnosticRecord(
116 unsigned Severity, const serialized_diags::Location &Location,
117 unsigned Category, unsigned Flag, StringRef Message) override;
118 std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
119 unsigned Timestamp,
120 StringRef Name) override;
121 std::error_code visitFixitRecord(const serialized_diags::Location &Start,
122 const serialized_diags::Location &End,
123 StringRef CodeToInsert) override;
124 std::error_code
125 visitSourceRangeRecord(const serialized_diags::Location &Start,
126 const serialized_diags::Location &End) override;
127
128private:
129 std::error_code adjustSourceLocFilename(RecordData &Record,
130 unsigned int offset);
131
132 void adjustAbbrevID(RecordData &Record, AbbrevLookup &Lookup,
133 unsigned NewAbbrev);
134
135 void writeRecordWithAbbrev(unsigned ID, RecordData &Record);
136
137 void writeRecordWithBlob(unsigned ID, RecordData &Record, StringRef Blob);
138};
139
Ted Kremenek4610ea22011-10-29 00:12:39 +0000140class SDiagsWriter : public DiagnosticConsumer {
Ted Kremenek4548e042011-12-17 05:26:11 +0000141 friend class SDiagsRenderer;
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000142 friend class SDiagsMerger;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000143
144 struct SharedState;
145
David Blaikie22105e12017-01-05 19:48:10 +0000146 explicit SDiagsWriter(std::shared_ptr<SharedState> State)
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000147 : LangOpts(nullptr), OriginalInstance(false), MergeChildRecords(false),
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000148 State(std::move(State)) {}
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000149
150public:
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000151 SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords)
David Blaikieeb62b822014-08-29 20:17:13 +0000152 : LangOpts(nullptr), OriginalInstance(true),
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000153 MergeChildRecords(MergeChildRecords),
David Blaikie22105e12017-01-05 19:48:10 +0000154 State(std::make_shared<SharedState>(File, Diags)) {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000155 if (MergeChildRecords)
156 RemoveOldDiagnostics();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000157 EmitPreamble();
Devang Patel9957e8b2011-11-15 01:30:40 +0000158 }
Richard Smitha9f521f2012-08-21 03:11:53 +0000159
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000160 ~SDiagsWriter() override {}
Craig Topperafa7cb32014-03-13 06:07:04 +0000161
Ted Kremenek4610ea22011-10-29 00:12:39 +0000162 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Craig Topperafa7cb32014-03-13 06:07:04 +0000163 const Diagnostic &Info) override;
164
165 void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
Ted Kremenekd010ba42011-11-10 08:43:12 +0000166 LangOpts = &LO;
167 }
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000168
Craig Topperafa7cb32014-03-13 06:07:04 +0000169 void finish() override;
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000170
Ted Kremenek4610ea22011-10-29 00:12:39 +0000171private:
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000172 /// \brief Build a DiagnosticsEngine to emit diagnostics about the diagnostics
173 DiagnosticsEngine *getMetaDiags();
174
175 /// \brief Remove old copies of the serialized diagnostics. This is necessary
176 /// so that we can detect when subprocesses write diagnostics that we should
177 /// merge into our own.
178 void RemoveOldDiagnostics();
179
Ted Kremenek4610ea22011-10-29 00:12:39 +0000180 /// \brief Emit the preamble for the serialized diagnostics.
181 void EmitPreamble();
182
183 /// \brief Emit the BLOCKINFO block.
184 void EmitBlockInfoBlock();
Ted Kremenekf264a202011-11-05 00:10:11 +0000185
Ted Kremenekcc88d262011-11-08 20:27:29 +0000186 /// \brief Emit the META data block.
187 void EmitMetaBlock();
Richard Smitha9f521f2012-08-21 03:11:53 +0000188
189 /// \brief Start a DIAG block.
190 void EnterDiagBlock();
191
192 /// \brief End a DIAG block.
193 void ExitDiagBlock();
194
195 /// \brief Emit a DIAG record.
196 void EmitDiagnosticMessage(SourceLocation Loc,
197 PresumedLoc PLoc,
198 DiagnosticsEngine::Level Level,
199 StringRef Message,
200 const SourceManager *SM,
201 DiagOrStoredDiag D);
202
203 /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic.
204 void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
205 ArrayRef<FixItHint> Hints,
206 const SourceManager &SM);
207
Ted Kremenek59f10252011-11-05 00:10:01 +0000208 /// \brief Emit a record for a CharSourceRange.
Ted Kremenek4548e042011-12-17 05:26:11 +0000209 void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM);
Ted Kremenek59f10252011-11-05 00:10:01 +0000210
Ted Kremenek4548e042011-12-17 05:26:11 +0000211 /// \brief Emit the string information for the category.
212 unsigned getEmitCategory(unsigned category = 0);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000213
214 /// \brief Emit the string information for diagnostic flags.
215 unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
Ted Kremenek4548e042011-12-17 05:26:11 +0000216 unsigned DiagID = 0);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000217
218 unsigned getEmitDiagnosticFlag(StringRef DiagName);
219
Ted Kremenekf264a202011-11-05 00:10:11 +0000220 /// \brief Emit (lazily) the file string and retrieved the file identifier.
Ted Kremenek4548e042011-12-17 05:26:11 +0000221 unsigned getEmitFile(const char *Filename);
222
223 /// \brief Add SourceLocation information the specified record.
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000224 void AddLocToRecord(SourceLocation Loc, const SourceManager *SM,
Ted Kremenek4548e042011-12-17 05:26:11 +0000225 PresumedLoc PLoc, RecordDataImpl &Record,
226 unsigned TokSize = 0);
227
Ted Kremenekf264a202011-11-05 00:10:11 +0000228 /// \brief Add SourceLocation information the specified record.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000229 void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000230 const SourceManager *SM,
Ted Kremenek4548e042011-12-17 05:26:11 +0000231 unsigned TokSize = 0) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000232 AddLocToRecord(Loc, SM, SM ? SM->getPresumedLoc(Loc) : PresumedLoc(),
233 Record, TokSize);
Ted Kremenek4548e042011-12-17 05:26:11 +0000234 }
Ted Kremenekf264a202011-11-05 00:10:11 +0000235
Ted Kremenekd89a8272011-11-05 03:34:23 +0000236 /// \brief Add CharSourceRange information the specified record.
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000237 void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000238 const SourceManager &SM);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000239
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000240 /// \brief Language options, which can differ from one clone of this client
241 /// to another.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000242 const LangOptions *LangOpts;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000243
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000244 /// \brief Whether this is the original instance (rather than one of its
245 /// clones), responsible for writing the file at the end.
246 bool OriginalInstance;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000247
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000248 /// \brief Whether this instance should aggregate diagnostics that are
249 /// generated from child processes.
250 bool MergeChildRecords;
251
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000252 /// \brief State that is shared among the various clones of this diagnostic
253 /// consumer.
David Blaikie22105e12017-01-05 19:48:10 +0000254 struct SharedState {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000255 SharedState(StringRef File, DiagnosticOptions *Diags)
256 : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()),
David Blaikieeb62b822014-08-29 20:17:13 +0000257 EmittedAnyDiagBlocks(false) {}
Ted Kremenek4610ea22011-10-29 00:12:39 +0000258
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000259 /// \brief Diagnostic options.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000260 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000261
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000262 /// \brief The byte buffer for the serialized content.
263 SmallString<1024> Buffer;
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000264
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000265 /// \brief The BitStreamWriter for the serialized diagnostics.
266 llvm::BitstreamWriter Stream;
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000267
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000268 /// \brief The name of the diagnostics file.
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000269 std::string OutputFile;
Richard Smitha9f521f2012-08-21 03:11:53 +0000270
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000271 /// \brief The set of constructed record abbreviations.
272 AbbreviationMap Abbrevs;
273
274 /// \brief A utility buffer for constructing record content.
275 RecordData Record;
276
277 /// \brief A text buffer for rendering diagnostic text.
278 SmallString<256> diagBuf;
279
280 /// \brief The collection of diagnostic categories used.
281 llvm::DenseSet<unsigned> Categories;
282
283 /// \brief The collection of files used.
284 llvm::DenseMap<const char *, unsigned> Files;
285
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000286 typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> >
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000287 DiagFlagsTy;
288
289 /// \brief Map for uniquing strings.
290 DiagFlagsTy DiagFlags;
291
292 /// \brief Whether we have already started emission of any DIAG blocks. Once
293 /// this becomes \c true, we never close a DIAG block until we know that we're
294 /// starting another one or we're done.
295 bool EmittedAnyDiagBlocks;
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000296
297 /// \brief Engine for emitting diagnostics about the diagnostics.
298 std::unique_ptr<DiagnosticsEngine> MetaDiagnostics;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000299 };
300
301 /// \brief State shared among the various clones of this diagnostic consumer.
David Blaikie22105e12017-01-05 19:48:10 +0000302 std::shared_ptr<SharedState> State;
Ted Kremenek4610ea22011-10-29 00:12:39 +0000303};
304} // end anonymous namespace
305
306namespace clang {
307namespace serialized_diags {
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000308std::unique_ptr<DiagnosticConsumer>
309create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords) {
310 return llvm::make_unique<SDiagsWriter>(OutputFile, Diags, MergeChildRecords);
Ted Kremenek4610ea22011-10-29 00:12:39 +0000311}
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000312
Ted Kremenek4610ea22011-10-29 00:12:39 +0000313} // end namespace serialized_diags
314} // end namespace clang
315
316//===----------------------------------------------------------------------===//
317// Serialization methods.
318//===----------------------------------------------------------------------===//
319
320/// \brief Emits a block ID in the BLOCKINFO block.
321static void EmitBlockID(unsigned ID, const char *Name,
322 llvm::BitstreamWriter &Stream,
323 RecordDataImpl &Record) {
324 Record.clear();
325 Record.push_back(ID);
326 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
327
328 // Emit the block name if present.
Craig Topper49a27902014-05-22 04:46:25 +0000329 if (!Name || Name[0] == 0)
Ted Kremenek4610ea22011-10-29 00:12:39 +0000330 return;
331
332 Record.clear();
333
334 while (*Name)
335 Record.push_back(*Name++);
336
337 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
338}
339
340/// \brief Emits a record ID in the BLOCKINFO block.
341static void EmitRecordID(unsigned ID, const char *Name,
342 llvm::BitstreamWriter &Stream,
343 RecordDataImpl &Record){
344 Record.clear();
345 Record.push_back(ID);
346
347 while (*Name)
348 Record.push_back(*Name++);
349
350 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
351}
352
Ted Kremenekf264a202011-11-05 00:10:11 +0000353void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000354 const SourceManager *SM,
Ted Kremenek4548e042011-12-17 05:26:11 +0000355 PresumedLoc PLoc,
Ted Kremenekd010ba42011-11-10 08:43:12 +0000356 RecordDataImpl &Record,
357 unsigned TokSize) {
Ted Kremenek4548e042011-12-17 05:26:11 +0000358 if (PLoc.isInvalid()) {
Ted Kremenek59f10252011-11-05 00:10:01 +0000359 // Emit a "sentinel" location.
Ted Kremenekd010ba42011-11-10 08:43:12 +0000360 Record.push_back((unsigned)0); // File.
361 Record.push_back((unsigned)0); // Line.
362 Record.push_back((unsigned)0); // Column.
363 Record.push_back((unsigned)0); // Offset.
Ted Kremenek59f10252011-11-05 00:10:01 +0000364 return;
365 }
366
Ted Kremenek4548e042011-12-17 05:26:11 +0000367 Record.push_back(getEmitFile(PLoc.getFilename()));
368 Record.push_back(PLoc.getLine());
369 Record.push_back(PLoc.getColumn()+TokSize);
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000370 Record.push_back(SM->getFileOffset(Loc));
Ted Kremenek59f10252011-11-05 00:10:01 +0000371}
372
Ted Kremenekd89a8272011-11-05 03:34:23 +0000373void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000374 RecordDataImpl &Record,
Ted Kremenek4548e042011-12-17 05:26:11 +0000375 const SourceManager &SM) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000376 AddLocToRecord(Range.getBegin(), Record, &SM);
Ted Kremenekd010ba42011-11-10 08:43:12 +0000377 unsigned TokSize = 0;
378 if (Range.isTokenRange())
379 TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000380 SM, *LangOpts);
Ted Kremenekd010ba42011-11-10 08:43:12 +0000381
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000382 AddLocToRecord(Range.getEnd(), Record, &SM, TokSize);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000383}
384
Ted Kremenek4548e042011-12-17 05:26:11 +0000385unsigned SDiagsWriter::getEmitFile(const char *FileName){
386 if (!FileName)
Ted Kremenekf264a202011-11-05 00:10:11 +0000387 return 0;
388
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000389 unsigned &entry = State->Files[FileName];
Ted Kremenekf264a202011-11-05 00:10:11 +0000390 if (entry)
391 return entry;
392
393 // Lazily generate the record for the file.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000394 entry = State->Files.size();
Ted Kremenek4548e042011-12-17 05:26:11 +0000395 StringRef Name(FileName);
Mehdi Amini57a41912015-09-10 01:46:39 +0000396 RecordData::value_type Record[] = {RECORD_FILENAME, entry, 0 /* For legacy */,
397 0 /* For legacy */, Name.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000398 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record,
399 Name);
Ted Kremenekf264a202011-11-05 00:10:11 +0000400
401 return entry;
402}
403
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000404void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
Ted Kremenek4548e042011-12-17 05:26:11 +0000405 const SourceManager &SM) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000406 State->Record.clear();
407 State->Record.push_back(RECORD_SOURCE_RANGE);
408 AddCharSourceRangeToRecord(R, State->Record, SM);
409 State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE),
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000410 State->Record);
Ted Kremenek59f10252011-11-05 00:10:01 +0000411}
412
Ted Kremenek4610ea22011-10-29 00:12:39 +0000413/// \brief Emits the preamble of the diagnostics file.
414void SDiagsWriter::EmitPreamble() {
Ted Kremenek4610ea22011-10-29 00:12:39 +0000415 // Emit the file header.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000416 State->Stream.Emit((unsigned)'D', 8);
417 State->Stream.Emit((unsigned)'I', 8);
418 State->Stream.Emit((unsigned)'A', 8);
419 State->Stream.Emit((unsigned)'G', 8);
Ted Kremenek868504a2011-11-05 00:09:53 +0000420
Ted Kremenek4610ea22011-10-29 00:12:39 +0000421 EmitBlockInfoBlock();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000422 EmitMetaBlock();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000423}
424
David Blaikieb44f0bf2017-01-04 22:36:43 +0000425static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
Ted Kremenek59f10252011-11-05 00:10:01 +0000426 using namespace llvm;
David Blaikieb44f0bf2017-01-04 22:36:43 +0000427 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
428 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line.
429 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column.
430 Abbrev.Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset;
Ted Kremenek59f10252011-11-05 00:10:01 +0000431}
Ted Kremenekd89a8272011-11-05 03:34:23 +0000432
David Blaikieb44f0bf2017-01-04 22:36:43 +0000433static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev &Abbrev) {
Ted Kremenekd89a8272011-11-05 03:34:23 +0000434 AddSourceLocationAbbrev(Abbrev);
435 AddSourceLocationAbbrev(Abbrev);
436}
437
Ted Kremenek4610ea22011-10-29 00:12:39 +0000438void SDiagsWriter::EmitBlockInfoBlock() {
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000439 State->Stream.EnterBlockInfoBlock();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000440
441 using namespace llvm;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000442 llvm::BitstreamWriter &Stream = State->Stream;
443 RecordData &Record = State->Record;
444 AbbreviationMap &Abbrevs = State->Abbrevs;
Ted Kremenekcc88d262011-11-08 20:27:29 +0000445
446 // ==---------------------------------------------------------------------==//
447 // The subsequent records and Abbrevs are for the "Meta" block.
448 // ==---------------------------------------------------------------------==//
449
450 EmitBlockID(BLOCK_META, "Meta", Stream, Record);
451 EmitRecordID(RECORD_VERSION, "Version", Stream, Record);
David Blaikieb44f0bf2017-01-04 22:36:43 +0000452 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenekcc88d262011-11-08 20:27:29 +0000453 Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION));
454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
455 Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev));
456
Ted Kremenek4610ea22011-10-29 00:12:39 +0000457 // ==---------------------------------------------------------------------==//
458 // The subsequent records and Abbrevs are for the "Diagnostic" block.
459 // ==---------------------------------------------------------------------==//
460
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000461 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record);
462 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record);
Ted Kremenek59f10252011-11-05 00:10:01 +0000463 EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record);
Ted Kremenek319215062011-11-05 00:10:04 +0000464 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000465 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
Ted Kremenekf264a202011-11-05 00:10:11 +0000466 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000467 EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000468
Ted Kremenek4610ea22011-10-29 00:12:39 +0000469 // Emit abbreviation for RECORD_DIAG.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000470 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000471 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG));
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000473 AddSourceLocationAbbrev(*Abbrev);
Ted Kremenek2724b1f2011-11-05 00:09:50 +0000474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category.
475 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
Argyrios Kyrtzidise26aea52015-08-06 18:46:36 +0000476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Text size.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text.
478 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
Ted Kremenek59f10252011-11-05 00:10:01 +0000479
Ted Kremenek319215062011-11-05 00:10:04 +0000480 // Emit abbrevation for RECORD_CATEGORY.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000481 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek319215062011-11-05 00:10:04 +0000482 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY));
483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID.
484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size.
485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text.
486 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
487
Ted Kremenek59f10252011-11-05 00:10:01 +0000488 // Emit abbrevation for RECORD_SOURCE_RANGE.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000489 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek59f10252011-11-05 00:10:01 +0000490 Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE));
David Blaikieb44f0bf2017-01-04 22:36:43 +0000491 AddRangeLocationAbbrev(*Abbrev);
Ted Kremenek59f10252011-11-05 00:10:01 +0000492 Abbrevs.set(RECORD_SOURCE_RANGE,
493 Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000494
495 // Emit the abbreviation for RECORD_DIAG_FLAG.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000496 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000497 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG));
498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID.
499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text.
501 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
502 Abbrev));
Ted Kremenekf264a202011-11-05 00:10:11 +0000503
504 // Emit the abbreviation for RECORD_FILENAME.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000505 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenekfce371a2011-11-05 00:09:43 +0000506 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME));
Ted Kremenekf264a202011-11-05 00:10:11 +0000507 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID.
508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size.
Hiroshi Inoued7b94d32017-05-30 05:06:46 +0000509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modification time.
Ted Kremenek4610ea22011-10-29 00:12:39 +0000510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text.
Ted Kremenekf264a202011-11-05 00:10:11 +0000512 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
Ted Kremenek4610ea22011-10-29 00:12:39 +0000513 Abbrev));
Ted Kremenekd89a8272011-11-05 03:34:23 +0000514
515 // Emit the abbreviation for RECORD_FIXIT.
David Blaikieb44f0bf2017-01-04 22:36:43 +0000516 Abbrev = std::make_shared<BitCodeAbbrev>();
Ted Kremenekd89a8272011-11-05 03:34:23 +0000517 Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT));
David Blaikieb44f0bf2017-01-04 22:36:43 +0000518 AddRangeLocationAbbrev(*Abbrev);
Ted Kremenekd89a8272011-11-05 03:34:23 +0000519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size.
520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text.
521 Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
522 Abbrev));
Ted Kremenek4610ea22011-10-29 00:12:39 +0000523
524 Stream.ExitBlock();
525}
526
Ted Kremenekcc88d262011-11-08 20:27:29 +0000527void SDiagsWriter::EmitMetaBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000528 llvm::BitstreamWriter &Stream = State->Stream;
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000529 AbbreviationMap &Abbrevs = State->Abbrevs;
530
Ted Kremenekcc88d262011-11-08 20:27:29 +0000531 Stream.EnterSubblock(BLOCK_META, 3);
Mehdi Amini57a41912015-09-10 01:46:39 +0000532 RecordData::value_type Record[] = {RECORD_VERSION, VersionNumber};
533 Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record);
Ted Kremenekcc88d262011-11-08 20:27:29 +0000534 Stream.ExitBlock();
535}
536
Ted Kremenek4548e042011-12-17 05:26:11 +0000537unsigned SDiagsWriter::getEmitCategory(unsigned int category) {
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000538 if (!State->Categories.insert(category).second)
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000539 return category;
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000540
Ted Kremenek319215062011-11-05 00:10:04 +0000541 // We use a local version of 'Record' so that we can be generating
542 // another record when we lazily generate one for the category entry.
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000543 StringRef catName = DiagnosticIDs::getCategoryNameFromID(category);
Mehdi Amini57a41912015-09-10 01:46:39 +0000544 RecordData::value_type Record[] = {RECORD_CATEGORY, category, catName.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000545 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record,
546 catName);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000547
548 return category;
549}
550
551unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel,
Ted Kremenek4548e042011-12-17 05:26:11 +0000552 unsigned DiagID) {
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000553 if (DiagLevel == DiagnosticsEngine::Note)
554 return 0; // No flag for notes.
555
Ted Kremenek4548e042011-12-17 05:26:11 +0000556 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000557 return getEmitDiagnosticFlag(FlagName);
558}
559
560unsigned SDiagsWriter::getEmitDiagnosticFlag(StringRef FlagName) {
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000561 if (FlagName.empty())
562 return 0;
563
564 // Here we assume that FlagName points to static data whose pointer
565 // value is fixed. This allows us to unique by diagnostic groups.
566 const void *data = FlagName.data();
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000567 std::pair<unsigned, StringRef> &entry = State->DiagFlags[data];
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000568 if (entry.first == 0) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000569 entry.first = State->DiagFlags.size();
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000570 entry.second = FlagName;
571
572 // Lazily emit the string in a separate record.
Mehdi Amini57a41912015-09-10 01:46:39 +0000573 RecordData::value_type Record[] = {RECORD_DIAG_FLAG, entry.first,
574 FlagName.size()};
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000575 State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG),
576 Record, FlagName);
Ted Kremenek0a49dae2011-11-05 00:10:07 +0000577 }
578
579 return entry.first;
Ted Kremenek319215062011-11-05 00:10:04 +0000580}
581
Ted Kremenek4610ea22011-10-29 00:12:39 +0000582void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
583 const Diagnostic &Info) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000584 // 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.
Ted Kremenekf67bbca2011-11-05 00:09:47 +0000587 if (DiagLevel != DiagnosticsEngine::Note) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000588 if (State->EmittedAnyDiagBlocks)
Richard Smitha9f521f2012-08-21 03:11:53 +0000589 ExitDiagBlock();
590
591 EnterDiagBlock();
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000592 State->EmittedAnyDiagBlocks = true;
Ted Kremenekf67bbca2011-11-05 00:09:47 +0000593 }
Ted Kremenekd89a8272011-11-05 03:34:23 +0000594
Ted Kremenek4548e042011-12-17 05:26:11 +0000595 // Compute the diagnostic text.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000596 State->diagBuf.clear();
597 Info.FormatDiagnostic(State->diagBuf);
Ted Kremenek4548e042011-12-17 05:26:11 +0000598
Richard Smitha9f521f2012-08-21 03:11:53 +0000599 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.
Ted Kremenekc6ebda12013-02-21 21:40:44 +0000603
604 // Make sure we bracket all notes as "sub-diagnostics". This matches
605 // the behavior in SDiagsRenderer::emitDiagnostic().
606 if (DiagLevel == DiagnosticsEngine::Note)
607 EnterDiagBlock();
608
Richard Smitha9f521f2012-08-21 03:11:53 +0000609 EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel,
Craig Topper49a27902014-05-22 04:46:25 +0000610 State->diagBuf, nullptr, &Info);
Ted Kremenekc6ebda12013-02-21 21:40:44 +0000611
612 if (DiagLevel == DiagnosticsEngine::Note)
613 ExitDiagBlock();
614
Richard Smitha9f521f2012-08-21 03:11:53 +0000615 return;
616 }
617
618 assert(Info.hasSourceManager() && LangOpts &&
619 "Unexpected diagnostic with valid location outside of a source file");
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000620 SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts);
Ted Kremenek4548e042011-12-17 05:26:11 +0000621 Renderer.emitDiagnostic(Info.getLocation(), DiagLevel,
Yaron Keren92e1b622015-03-18 10:17:07 +0000622 State->diagBuf,
Ted Kremenek4548e042011-12-17 05:26:11 +0000623 Info.getRanges(),
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000624 Info.getFixItHints(),
Richard Smitha9f521f2012-08-21 03:11:53 +0000625 &Info.getSourceManager(),
Ted Kremenek4548e042011-12-17 05:26:11 +0000626 &Info);
627}
628
Jordan Rose7ef1c382014-03-03 18:29:52 +0000629static serialized_diags::Level getStableLevel(DiagnosticsEngine::Level Level) {
630 switch (Level) {
631#define CASE(X) case DiagnosticsEngine::X: return serialized_diags::X;
632 CASE(Ignored)
633 CASE(Note)
634 CASE(Remark)
635 CASE(Warning)
636 CASE(Error)
637 CASE(Fatal)
638#undef CASE
639 }
640
641 llvm_unreachable("invalid diagnostic level");
642}
643
Richard Smitha9f521f2012-08-21 03:11:53 +0000644void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc,
645 PresumedLoc PLoc,
646 DiagnosticsEngine::Level Level,
647 StringRef Message,
648 const SourceManager *SM,
649 DiagOrStoredDiag D) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000650 llvm::BitstreamWriter &Stream = State->Stream;
651 RecordData &Record = State->Record;
652 AbbreviationMap &Abbrevs = State->Abbrevs;
653
Richard Smitha9f521f2012-08-21 03:11:53 +0000654 // Emit the RECORD_DIAG record.
655 Record.clear();
656 Record.push_back(RECORD_DIAG);
Jordan Rose7ef1c382014-03-03 18:29:52 +0000657 Record.push_back(getStableLevel(Level));
Richard Smitha9f521f2012-08-21 03:11:53 +0000658 AddLocToRecord(Loc, SM, PLoc, Record);
659
660 if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
661 // Emit the category string lazily and get the category ID.
662 unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
663 Record.push_back(getEmitCategory(DiagID));
664 // Emit the diagnostic flag string lazily and get the mapped ID.
665 Record.push_back(getEmitDiagnosticFlag(Level, Info->getID()));
666 } else {
667 Record.push_back(getEmitCategory());
668 Record.push_back(getEmitDiagnosticFlag(Level));
669 }
670
671 Record.push_back(Message.size());
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000672 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message);
Richard Smitha9f521f2012-08-21 03:11:53 +0000673}
674
Ted Kremenek4548e042011-12-17 05:26:11 +0000675void
676SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc,
677 PresumedLoc PLoc,
678 DiagnosticsEngine::Level Level,
679 StringRef Message,
680 ArrayRef<clang::CharSourceRange> Ranges,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000681 const SourceManager *SM,
Ted Kremenek0964cca2012-02-14 02:46:00 +0000682 DiagOrStoredDiag D) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000683 Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D);
684}
Ted Kremenek4548e042011-12-17 05:26:11 +0000685
Richard Smitha9f521f2012-08-21 03:11:53 +0000686void SDiagsWriter::EnterDiagBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000687 State->Stream.EnterSubblock(BLOCK_DIAG, 4);
Richard Smitha9f521f2012-08-21 03:11:53 +0000688}
Ted Kremenek4548e042011-12-17 05:26:11 +0000689
Richard Smitha9f521f2012-08-21 03:11:53 +0000690void SDiagsWriter::ExitDiagBlock() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000691 State->Stream.ExitBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000692}
693
Ted Kremenek0964cca2012-02-14 02:46:00 +0000694void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D,
Ted Kremenek4548e042011-12-17 05:26:11 +0000695 DiagnosticsEngine::Level Level) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000696 if (Level == DiagnosticsEngine::Note)
697 Writer.EnterDiagBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000698}
699
Ted Kremenek0964cca2012-02-14 02:46:00 +0000700void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D,
Ted Kremenek4548e042011-12-17 05:26:11 +0000701 DiagnosticsEngine::Level Level) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000702 // Only end note diagnostics here, because we can't be sure when we've seen
703 // the last note associated with a non-note diagnostic.
704 if (Level == DiagnosticsEngine::Note)
705 Writer.ExitDiagBlock();
706}
707
708void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges,
709 ArrayRef<FixItHint> Hints,
710 const SourceManager &SM) {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000711 llvm::BitstreamWriter &Stream = State->Stream;
712 RecordData &Record = State->Record;
713 AbbreviationMap &Abbrevs = State->Abbrevs;
714
Richard Smitha9f521f2012-08-21 03:11:53 +0000715 // Emit Source Ranges.
716 for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
717 I != E; ++I)
718 if (I->isValid())
719 EmitCharSourceRange(*I, SM);
720
721 // Emit FixIts.
722 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
723 I != E; ++I) {
724 const FixItHint &Fix = *I;
725 if (Fix.isNull())
726 continue;
727 Record.clear();
728 Record.push_back(RECORD_FIXIT);
729 AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM);
730 Record.push_back(Fix.CodeToInsert.size());
Mehdi Amini5ae4a852015-09-09 20:35:37 +0000731 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
Richard Smitha9f521f2012-08-21 03:11:53 +0000732 Fix.CodeToInsert);
733 }
Ted Kremenek4548e042011-12-17 05:26:11 +0000734}
735
736void SDiagsRenderer::emitCodeContext(SourceLocation Loc,
737 DiagnosticsEngine::Level Level,
738 SmallVectorImpl<CharSourceRange> &Ranges,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000739 ArrayRef<FixItHint> Hints,
Richard Smitha9f521f2012-08-21 03:11:53 +0000740 const SourceManager &SM) {
741 Writer.EmitCodeContext(Ranges, Hints, SM);
Ted Kremenek4610ea22011-10-29 00:12:39 +0000742}
743
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000744void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message,
745 const SourceManager *SM) {
Richard Smitha9f521f2012-08-21 03:11:53 +0000746 Writer.EnterDiagBlock();
747 PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc();
748 Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note,
749 Message, SM, DiagOrStoredDiag());
750 Writer.ExitDiagBlock();
Ted Kremenek4548e042011-12-17 05:26:11 +0000751}
752
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000753DiagnosticsEngine *SDiagsWriter::getMetaDiags() {
754 // FIXME: It's slightly absurd to create a new diagnostics engine here, but
755 // the other options that are available today are worse:
756 //
757 // 1. Teach DiagnosticsConsumers to emit diagnostics to the engine they are a
758 // part of. The DiagnosticsEngine would need to know not to send
759 // diagnostics back to the consumer that failed. This would require us to
760 // rework ChainedDiagnosticsConsumer and teach the engine about multiple
761 // consumers, which is difficult today because most APIs interface with
762 // consumers rather than the engine itself.
763 //
764 // 2. Pass a DiagnosticsEngine to SDiagsWriter on creation - this would need
765 // to be distinct from the engine the writer was being added to and would
766 // normally not be used.
767 if (!State->MetaDiagnostics) {
768 IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs());
769 auto Client =
770 new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get());
771 State->MetaDiagnostics = llvm::make_unique<DiagnosticsEngine>(
772 IDs, State->DiagOpts.get(), Client);
773 }
774 return State->MetaDiagnostics.get();
775}
776
777void SDiagsWriter::RemoveOldDiagnostics() {
778 if (!llvm::sys::fs::remove(State->OutputFile))
779 return;
780
781 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
782 // Disable merging child records, as whatever is in this file may be
783 // misleading.
784 MergeChildRecords = false;
785}
786
Argyrios Kyrtzidis7910d7b2011-12-07 05:52:12 +0000787void SDiagsWriter::finish() {
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000788 // The original instance is responsible for writing the file.
789 if (!OriginalInstance)
790 return;
791
Richard Smitha9f521f2012-08-21 03:11:53 +0000792 // Finish off any diagnostic we were in the process of emitting.
Douglas Gregorfa686fb2012-11-30 23:32:31 +0000793 if (State->EmittedAnyDiagBlocks)
Richard Smitha9f521f2012-08-21 03:11:53 +0000794 ExitDiagBlock();
Ted Kremenekf264a202011-11-05 00:10:11 +0000795
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000796 if (MergeChildRecords) {
797 if (!State->EmittedAnyDiagBlocks)
798 // We have no diagnostics of our own, so we can just leave the child
799 // process' output alone
800 return;
Richard Smitha9f521f2012-08-21 03:11:53 +0000801
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000802 if (llvm::sys::fs::exists(State->OutputFile))
803 if (SDiagsMerger(*this).mergeRecordsFromFile(State->OutputFile.c_str()))
804 getMetaDiags()->Report(diag::warn_fe_serialized_diag_merge_failure);
805 }
806
807 std::error_code EC;
808 auto OS = llvm::make_unique<llvm::raw_fd_ostream>(State->OutputFile.c_str(),
809 EC, llvm::sys::fs::F_None);
810 if (EC) {
811 getMetaDiags()->Report(diag::warn_fe_serialized_diag_failure)
812 << State->OutputFile << EC.message();
813 return;
814 }
815
816 // Write the generated bitstream to "Out".
817 OS->write((char *)&State->Buffer.front(), State->Buffer.size());
818 OS->flush();
819}
820
821std::error_code SDiagsMerger::visitStartOfDiagnostic() {
822 Writer.EnterDiagBlock();
823 return std::error_code();
824}
825
826std::error_code SDiagsMerger::visitEndOfDiagnostic() {
827 Writer.ExitDiagBlock();
828 return std::error_code();
829}
830
831std::error_code
832SDiagsMerger::visitSourceRangeRecord(const serialized_diags::Location &Start,
833 const serialized_diags::Location &End) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000834 RecordData::value_type Record[] = {
835 RECORD_SOURCE_RANGE, FileLookup[Start.FileID], Start.Line, Start.Col,
836 Start.Offset, FileLookup[End.FileID], End.Line, End.Col, End.Offset};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000837 Writer.State->Stream.EmitRecordWithAbbrev(
838 Writer.State->Abbrevs.get(RECORD_SOURCE_RANGE), Record);
839 return std::error_code();
840}
841
842std::error_code SDiagsMerger::visitDiagnosticRecord(
843 unsigned Severity, const serialized_diags::Location &Location,
844 unsigned Category, unsigned Flag, StringRef Message) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000845 RecordData::value_type Record[] = {
846 RECORD_DIAG, Severity, FileLookup[Location.FileID], Location.Line,
847 Location.Col, Location.Offset, CategoryLookup[Category],
848 Flag ? DiagFlagLookup[Flag] : 0, Message.size()};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000849
850 Writer.State->Stream.EmitRecordWithBlob(
Mehdi Amini57a41912015-09-10 01:46:39 +0000851 Writer.State->Abbrevs.get(RECORD_DIAG), Record, Message);
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000852 return std::error_code();
853}
854
855std::error_code
856SDiagsMerger::visitFixitRecord(const serialized_diags::Location &Start,
857 const serialized_diags::Location &End,
858 StringRef Text) {
Mehdi Amini57a41912015-09-10 01:46:39 +0000859 RecordData::value_type Record[] = {RECORD_FIXIT, FileLookup[Start.FileID],
860 Start.Line, Start.Col, Start.Offset,
861 FileLookup[End.FileID], End.Line, End.Col,
862 End.Offset, Text.size()};
Justin Bogner5a6a2fc2014-10-23 22:20:11 +0000863
864 Writer.State->Stream.EmitRecordWithBlob(
865 Writer.State->Abbrevs.get(RECORD_FIXIT), Record, Text);
866 return std::error_code();
867}
868
869std::error_code SDiagsMerger::visitFilenameRecord(unsigned ID, unsigned Size,
870 unsigned Timestamp,
871 StringRef Name) {
872 FileLookup[ID] = Writer.getEmitFile(Name.str().c_str());
873 return std::error_code();
874}
875
876std::error_code SDiagsMerger::visitCategoryRecord(unsigned ID, StringRef Name) {
877 CategoryLookup[ID] = Writer.getEmitCategory(ID);
878 return std::error_code();
879}
880
881std::error_code SDiagsMerger::visitDiagFlagRecord(unsigned ID, StringRef Name) {
882 DiagFlagLookup[ID] = Writer.getEmitDiagnosticFlag(Name);
883 return std::error_code();
Ted Kremenek4610ea22011-10-29 00:12:39 +0000884}