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