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