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