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