Ted Kremenek | 7800212 | 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 | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 10 | #include "clang/Frontend/SerializedDiagnosticPrinter.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 11 | #include "clang/Basic/Diagnostic.h" |
| 12 | #include "clang/Basic/DiagnosticOptions.h" |
| 13 | #include "clang/Basic/FileManager.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "clang/Basic/Version.h" |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/DiagnosticRenderer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Lexer.h" |
| 18 | #include "llvm/ADT/DenseSet.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include <vector> |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
Ted Kremenek | fdd0ced | 2011-11-05 00:09:57 +0000 | [diff] [blame] | 25 | using namespace clang::serialized_diags; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 28 | |
| 29 | class AbbreviationMap { |
| 30 | llvm::DenseMap<unsigned, unsigned> Abbrevs; |
| 31 | public: |
| 32 | AbbreviationMap() {} |
| 33 | |
| 34 | void set(unsigned recordID, unsigned abbrevID) { |
| 35 | assert(Abbrevs.find(recordID) == Abbrevs.end() |
| 36 | && "Abbreviation already set."); |
| 37 | Abbrevs[recordID] = abbrevID; |
| 38 | } |
| 39 | |
| 40 | unsigned get(unsigned recordID) { |
| 41 | assert(Abbrevs.find(recordID) != Abbrevs.end() && |
| 42 | "Abbreviation not set."); |
| 43 | return Abbrevs[recordID]; |
| 44 | } |
| 45 | }; |
| 46 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 47 | typedef SmallVector<uint64_t, 64> RecordData; |
| 48 | typedef SmallVectorImpl<uint64_t> RecordDataImpl; |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 49 | |
| 50 | class SDiagsWriter; |
| 51 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 52 | class SDiagsRenderer : public DiagnosticNoteRenderer { |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 53 | SDiagsWriter &Writer; |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 54 | public: |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 55 | SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 56 | DiagnosticOptions *DiagOpts) |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 57 | : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {} |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 58 | |
| 59 | virtual ~SDiagsRenderer() {} |
| 60 | |
| 61 | protected: |
| 62 | virtual void emitDiagnosticMessage(SourceLocation Loc, |
| 63 | PresumedLoc PLoc, |
| 64 | DiagnosticsEngine::Level Level, |
| 65 | StringRef Message, |
| 66 | ArrayRef<CharSourceRange> Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 67 | const SourceManager *SM, |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 68 | DiagOrStoredDiag D); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 69 | |
| 70 | virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, |
| 71 | DiagnosticsEngine::Level Level, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 72 | ArrayRef<CharSourceRange> Ranges, |
| 73 | const SourceManager &SM) {} |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 74 | |
| 75 | virtual void emitNote(SourceLocation Loc, StringRef Message, |
| 76 | const SourceManager *SM); |
| 77 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 78 | virtual void emitCodeContext(SourceLocation Loc, |
| 79 | DiagnosticsEngine::Level Level, |
| 80 | SmallVectorImpl<CharSourceRange>& Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 81 | ArrayRef<FixItHint> Hints, |
| 82 | const SourceManager &SM); |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 84 | virtual void beginDiagnostic(DiagOrStoredDiag D, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 85 | DiagnosticsEngine::Level Level); |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 86 | virtual void endDiagnostic(DiagOrStoredDiag D, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 87 | DiagnosticsEngine::Level Level); |
| 88 | }; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 89 | |
| 90 | class SDiagsWriter : public DiagnosticConsumer { |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 91 | friend class SDiagsRenderer; |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 92 | |
| 93 | struct SharedState; |
| 94 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 95 | explicit SDiagsWriter(IntrusiveRefCntPtr<SharedState> State) |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 96 | : LangOpts(0), OriginalInstance(false), State(State) { } |
| 97 | |
| 98 | public: |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 99 | SDiagsWriter(raw_ostream *os, DiagnosticOptions *diags) |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 100 | : LangOpts(0), OriginalInstance(true), State(new SharedState(os, diags)) |
| 101 | { |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 102 | EmitPreamble(); |
Devang Patel | 02ae32a | 2011-11-15 01:30:40 +0000 | [diff] [blame] | 103 | } |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 105 | ~SDiagsWriter() {} |
| 106 | |
| 107 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 108 | const Diagnostic &Info); |
| 109 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 110 | void BeginSourceFile(const LangOptions &LO, |
| 111 | const Preprocessor *PP) { |
| 112 | LangOpts = &LO; |
| 113 | } |
Argyrios Kyrtzidis | 29f2787 | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 114 | |
| 115 | virtual void finish(); |
| 116 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 117 | private: |
| 118 | /// \brief Emit the preamble for the serialized diagnostics. |
| 119 | void EmitPreamble(); |
| 120 | |
| 121 | /// \brief Emit the BLOCKINFO block. |
| 122 | void EmitBlockInfoBlock(); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 124 | /// \brief Emit the META data block. |
| 125 | void EmitMetaBlock(); |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 126 | |
| 127 | /// \brief Start a DIAG block. |
| 128 | void EnterDiagBlock(); |
| 129 | |
| 130 | /// \brief End a DIAG block. |
| 131 | void ExitDiagBlock(); |
| 132 | |
| 133 | /// \brief Emit a DIAG record. |
| 134 | void EmitDiagnosticMessage(SourceLocation Loc, |
| 135 | PresumedLoc PLoc, |
| 136 | DiagnosticsEngine::Level Level, |
| 137 | StringRef Message, |
| 138 | const SourceManager *SM, |
| 139 | DiagOrStoredDiag D); |
| 140 | |
| 141 | /// \brief Emit FIXIT and SOURCE_RANGE records for a diagnostic. |
| 142 | void EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges, |
| 143 | ArrayRef<FixItHint> Hints, |
| 144 | const SourceManager &SM); |
| 145 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 146 | /// \brief Emit a record for a CharSourceRange. |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 147 | void EmitCharSourceRange(CharSourceRange R, const SourceManager &SM); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 149 | /// \brief Emit the string information for the category. |
| 150 | unsigned getEmitCategory(unsigned category = 0); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 151 | |
| 152 | /// \brief Emit the string information for diagnostic flags. |
| 153 | unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 154 | unsigned DiagID = 0); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 156 | /// \brief Emit (lazily) the file string and retrieved the file identifier. |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 157 | unsigned getEmitFile(const char *Filename); |
| 158 | |
| 159 | /// \brief Add SourceLocation information the specified record. |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 160 | void AddLocToRecord(SourceLocation Loc, const SourceManager *SM, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 161 | PresumedLoc PLoc, RecordDataImpl &Record, |
| 162 | unsigned TokSize = 0); |
| 163 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 164 | /// \brief Add SourceLocation information the specified record. |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 165 | void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 166 | const SourceManager *SM, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 167 | unsigned TokSize = 0) { |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 168 | AddLocToRecord(Loc, SM, SM ? SM->getPresumedLoc(Loc) : PresumedLoc(), |
| 169 | Record, TokSize); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 172 | /// \brief Add CharSourceRange information the specified record. |
Argyrios Kyrtzidis | 29f2787 | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 173 | void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 174 | const SourceManager &SM); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 176 | /// \brief The version of the diagnostics file. |
| 177 | enum { Version = 1 }; |
| 178 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 179 | /// \brief Language options, which can differ from one clone of this client |
| 180 | /// to another. |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 181 | const LangOptions *LangOpts; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 183 | /// \brief Whether this is the original instance (rather than one of its |
| 184 | /// clones), responsible for writing the file at the end. |
| 185 | bool OriginalInstance; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 187 | /// \brief State that is shared among the various clones of this diagnostic |
| 188 | /// consumer. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 189 | struct SharedState : RefCountedBase<SharedState> { |
| 190 | SharedState(raw_ostream *os, DiagnosticOptions *diags) |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 191 | : DiagOpts(diags), Stream(Buffer), OS(os), EmittedAnyDiagBlocks(false) { } |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 193 | /// \brief Diagnostic options. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 194 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 196 | /// \brief The byte buffer for the serialized content. |
| 197 | SmallString<1024> Buffer; |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 199 | /// \brief The BitStreamWriter for the serialized diagnostics. |
| 200 | llvm::BitstreamWriter Stream; |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 201 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 202 | /// \brief The name of the diagnostics file. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 203 | OwningPtr<raw_ostream> OS; |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 204 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 205 | /// \brief The set of constructed record abbreviations. |
| 206 | AbbreviationMap Abbrevs; |
| 207 | |
| 208 | /// \brief A utility buffer for constructing record content. |
| 209 | RecordData Record; |
| 210 | |
| 211 | /// \brief A text buffer for rendering diagnostic text. |
| 212 | SmallString<256> diagBuf; |
| 213 | |
| 214 | /// \brief The collection of diagnostic categories used. |
| 215 | llvm::DenseSet<unsigned> Categories; |
| 216 | |
| 217 | /// \brief The collection of files used. |
| 218 | llvm::DenseMap<const char *, unsigned> Files; |
| 219 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 220 | typedef llvm::DenseMap<const void *, std::pair<unsigned, StringRef> > |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 221 | DiagFlagsTy; |
| 222 | |
| 223 | /// \brief Map for uniquing strings. |
| 224 | DiagFlagsTy DiagFlags; |
| 225 | |
| 226 | /// \brief Whether we have already started emission of any DIAG blocks. Once |
| 227 | /// this becomes \c true, we never close a DIAG block until we know that we're |
| 228 | /// starting another one or we're done. |
| 229 | bool EmittedAnyDiagBlocks; |
| 230 | }; |
| 231 | |
| 232 | /// \brief State shared among the various clones of this diagnostic consumer. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 233 | IntrusiveRefCntPtr<SharedState> State; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 234 | }; |
| 235 | } // end anonymous namespace |
| 236 | |
| 237 | namespace clang { |
| 238 | namespace serialized_diags { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 239 | DiagnosticConsumer *create(raw_ostream *OS, DiagnosticOptions *diags) { |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 240 | return new SDiagsWriter(OS, diags); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 241 | } |
| 242 | } // end namespace serialized_diags |
| 243 | } // end namespace clang |
| 244 | |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | // Serialization methods. |
| 247 | //===----------------------------------------------------------------------===// |
| 248 | |
| 249 | /// \brief Emits a block ID in the BLOCKINFO block. |
| 250 | static void EmitBlockID(unsigned ID, const char *Name, |
| 251 | llvm::BitstreamWriter &Stream, |
| 252 | RecordDataImpl &Record) { |
| 253 | Record.clear(); |
| 254 | Record.push_back(ID); |
| 255 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 256 | |
| 257 | // Emit the block name if present. |
| 258 | if (Name == 0 || Name[0] == 0) |
| 259 | return; |
| 260 | |
| 261 | Record.clear(); |
| 262 | |
| 263 | while (*Name) |
| 264 | Record.push_back(*Name++); |
| 265 | |
| 266 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 267 | } |
| 268 | |
| 269 | /// \brief Emits a record ID in the BLOCKINFO block. |
| 270 | static void EmitRecordID(unsigned ID, const char *Name, |
| 271 | llvm::BitstreamWriter &Stream, |
| 272 | RecordDataImpl &Record){ |
| 273 | Record.clear(); |
| 274 | Record.push_back(ID); |
| 275 | |
| 276 | while (*Name) |
| 277 | Record.push_back(*Name++); |
| 278 | |
| 279 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
| 280 | } |
| 281 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 282 | void SDiagsWriter::AddLocToRecord(SourceLocation Loc, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 283 | const SourceManager *SM, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 284 | PresumedLoc PLoc, |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 285 | RecordDataImpl &Record, |
| 286 | unsigned TokSize) { |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 287 | if (PLoc.isInvalid()) { |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 288 | // Emit a "sentinel" location. |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 289 | Record.push_back((unsigned)0); // File. |
| 290 | Record.push_back((unsigned)0); // Line. |
| 291 | Record.push_back((unsigned)0); // Column. |
| 292 | Record.push_back((unsigned)0); // Offset. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 296 | Record.push_back(getEmitFile(PLoc.getFilename())); |
| 297 | Record.push_back(PLoc.getLine()); |
| 298 | Record.push_back(PLoc.getColumn()+TokSize); |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 299 | Record.push_back(SM->getFileOffset(Loc)); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 302 | void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range, |
Argyrios Kyrtzidis | 29f2787 | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 303 | RecordDataImpl &Record, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 304 | const SourceManager &SM) { |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 305 | AddLocToRecord(Range.getBegin(), Record, &SM); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 306 | unsigned TokSize = 0; |
| 307 | if (Range.isTokenRange()) |
| 308 | TokSize = Lexer::MeasureTokenLength(Range.getEnd(), |
Argyrios Kyrtzidis | 29f2787 | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 309 | SM, *LangOpts); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 310 | |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 311 | AddLocToRecord(Range.getEnd(), Record, &SM, TokSize); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 314 | unsigned SDiagsWriter::getEmitFile(const char *FileName){ |
| 315 | if (!FileName) |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 316 | return 0; |
| 317 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 318 | unsigned &entry = State->Files[FileName]; |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 319 | if (entry) |
| 320 | return entry; |
| 321 | |
| 322 | // Lazily generate the record for the file. |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 323 | entry = State->Files.size(); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 324 | RecordData Record; |
| 325 | Record.push_back(RECORD_FILENAME); |
| 326 | Record.push_back(entry); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 327 | Record.push_back(0); // For legacy. |
| 328 | Record.push_back(0); // For legacy. |
| 329 | StringRef Name(FileName); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 330 | Record.push_back(Name.size()); |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 331 | State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME), Record, |
| 332 | Name); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 333 | |
| 334 | return entry; |
| 335 | } |
| 336 | |
Argyrios Kyrtzidis | 29f2787 | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 337 | void SDiagsWriter::EmitCharSourceRange(CharSourceRange R, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 338 | const SourceManager &SM) { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 339 | State->Record.clear(); |
| 340 | State->Record.push_back(RECORD_SOURCE_RANGE); |
| 341 | AddCharSourceRangeToRecord(R, State->Record, SM); |
| 342 | State->Stream.EmitRecordWithAbbrev(State->Abbrevs.get(RECORD_SOURCE_RANGE), |
| 343 | State->Record); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 346 | /// \brief Emits the preamble of the diagnostics file. |
| 347 | void SDiagsWriter::EmitPreamble() { |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 348 | // Emit the file header. |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 349 | State->Stream.Emit((unsigned)'D', 8); |
| 350 | State->Stream.Emit((unsigned)'I', 8); |
| 351 | State->Stream.Emit((unsigned)'A', 8); |
| 352 | State->Stream.Emit((unsigned)'G', 8); |
Ted Kremenek | 069f9c2 | 2011-11-05 00:09:53 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 354 | EmitBlockInfoBlock(); |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 355 | EmitMetaBlock(); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 358 | static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) { |
| 359 | using namespace llvm; |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 360 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 361 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line. |
| 362 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column. |
| 363 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset; |
| 364 | } |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 365 | |
| 366 | static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) { |
| 367 | AddSourceLocationAbbrev(Abbrev); |
| 368 | AddSourceLocationAbbrev(Abbrev); |
| 369 | } |
| 370 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 371 | void SDiagsWriter::EmitBlockInfoBlock() { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 372 | State->Stream.EnterBlockInfoBlock(3); |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 373 | |
| 374 | using namespace llvm; |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 375 | llvm::BitstreamWriter &Stream = State->Stream; |
| 376 | RecordData &Record = State->Record; |
| 377 | AbbreviationMap &Abbrevs = State->Abbrevs; |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 378 | |
| 379 | // ==---------------------------------------------------------------------==// |
| 380 | // The subsequent records and Abbrevs are for the "Meta" block. |
| 381 | // ==---------------------------------------------------------------------==// |
| 382 | |
| 383 | EmitBlockID(BLOCK_META, "Meta", Stream, Record); |
| 384 | EmitRecordID(RECORD_VERSION, "Version", Stream, Record); |
| 385 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 386 | Abbrev->Add(BitCodeAbbrevOp(RECORD_VERSION)); |
| 387 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 388 | Abbrevs.set(RECORD_VERSION, Stream.EmitBlockInfoAbbrev(BLOCK_META, Abbrev)); |
| 389 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 390 | // ==---------------------------------------------------------------------==// |
| 391 | // The subsequent records and Abbrevs are for the "Diagnostic" block. |
| 392 | // ==---------------------------------------------------------------------==// |
| 393 | |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 394 | EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record); |
| 395 | EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 396 | EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 397 | EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 398 | EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 399 | EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 400 | EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 402 | // Emit abbreviation for RECORD_DIAG. |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 403 | Abbrev = new BitCodeAbbrev(); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 404 | Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG)); |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 405 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 406 | AddSourceLocationAbbrev(Abbrev); |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 407 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category. |
| 408 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 409 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 410 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text. |
| 411 | Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 413 | // Emit abbrevation for RECORD_CATEGORY. |
| 414 | Abbrev = new BitCodeAbbrev(); |
| 415 | Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY)); |
| 416 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID. |
| 417 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size. |
| 418 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text. |
| 419 | Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); |
| 420 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 421 | // Emit abbrevation for RECORD_SOURCE_RANGE. |
| 422 | Abbrev = new BitCodeAbbrev(); |
| 423 | Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE)); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 424 | AddRangeLocationAbbrev(Abbrev); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 425 | Abbrevs.set(RECORD_SOURCE_RANGE, |
| 426 | Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 427 | |
| 428 | // Emit the abbreviation for RECORD_DIAG_FLAG. |
| 429 | Abbrev = new BitCodeAbbrev(); |
| 430 | Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG)); |
| 431 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID. |
| 432 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 433 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text. |
| 434 | Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, |
| 435 | Abbrev)); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 436 | |
| 437 | // Emit the abbreviation for RECORD_FILENAME. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 438 | Abbrev = new BitCodeAbbrev(); |
Ted Kremenek | 28eac52 | 2011-11-05 00:09:43 +0000 | [diff] [blame] | 439 | Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME)); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 440 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID. |
| 441 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size. |
| 442 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 443 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 444 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text. |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 445 | Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 446 | Abbrev)); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 447 | |
| 448 | // Emit the abbreviation for RECORD_FIXIT. |
| 449 | Abbrev = new BitCodeAbbrev(); |
| 450 | Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT)); |
| 451 | AddRangeLocationAbbrev(Abbrev); |
| 452 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 453 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text. |
| 454 | Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, |
| 455 | Abbrev)); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 456 | |
| 457 | Stream.ExitBlock(); |
| 458 | } |
| 459 | |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 460 | void SDiagsWriter::EmitMetaBlock() { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 461 | llvm::BitstreamWriter &Stream = State->Stream; |
| 462 | RecordData &Record = State->Record; |
| 463 | AbbreviationMap &Abbrevs = State->Abbrevs; |
| 464 | |
Ted Kremenek | 0b69aa8 | 2011-11-08 20:27:29 +0000 | [diff] [blame] | 465 | Stream.EnterSubblock(BLOCK_META, 3); |
| 466 | Record.clear(); |
| 467 | Record.push_back(RECORD_VERSION); |
| 468 | Record.push_back(Version); |
| 469 | Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_VERSION), Record); |
| 470 | Stream.ExitBlock(); |
| 471 | } |
| 472 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 473 | unsigned SDiagsWriter::getEmitCategory(unsigned int category) { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 474 | if (State->Categories.count(category)) |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 475 | return category; |
| 476 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 477 | State->Categories.insert(category); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 478 | |
| 479 | // We use a local version of 'Record' so that we can be generating |
| 480 | // another record when we lazily generate one for the category entry. |
| 481 | RecordData Record; |
| 482 | Record.push_back(RECORD_CATEGORY); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 483 | Record.push_back(category); |
| 484 | StringRef catName = DiagnosticIDs::getCategoryNameFromID(category); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 485 | Record.push_back(catName.size()); |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 486 | State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_CATEGORY), Record, |
| 487 | catName); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 488 | |
| 489 | return category; |
| 490 | } |
| 491 | |
| 492 | unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 493 | unsigned DiagID) { |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 494 | if (DiagLevel == DiagnosticsEngine::Note) |
| 495 | return 0; // No flag for notes. |
| 496 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 497 | StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(DiagID); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 498 | if (FlagName.empty()) |
| 499 | return 0; |
| 500 | |
| 501 | // Here we assume that FlagName points to static data whose pointer |
| 502 | // value is fixed. This allows us to unique by diagnostic groups. |
| 503 | const void *data = FlagName.data(); |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 504 | std::pair<unsigned, StringRef> &entry = State->DiagFlags[data]; |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 505 | if (entry.first == 0) { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 506 | entry.first = State->DiagFlags.size(); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 507 | entry.second = FlagName; |
| 508 | |
| 509 | // Lazily emit the string in a separate record. |
| 510 | RecordData Record; |
| 511 | Record.push_back(RECORD_DIAG_FLAG); |
| 512 | Record.push_back(entry.first); |
| 513 | Record.push_back(FlagName.size()); |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 514 | State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_DIAG_FLAG), |
| 515 | Record, FlagName); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | return entry.first; |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 521 | void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 522 | const Diagnostic &Info) { |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 523 | // Enter the block for a non-note diagnostic immediately, rather than waiting |
| 524 | // for beginDiagnostic, in case associated notes are emitted before we get |
| 525 | // there. |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 526 | if (DiagLevel != DiagnosticsEngine::Note) { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 527 | if (State->EmittedAnyDiagBlocks) |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 528 | ExitDiagBlock(); |
| 529 | |
| 530 | EnterDiagBlock(); |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 531 | State->EmittedAnyDiagBlocks = true; |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 532 | } |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame] | 533 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 534 | // Compute the diagnostic text. |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 535 | State->diagBuf.clear(); |
| 536 | Info.FormatDiagnostic(State->diagBuf); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 537 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 538 | if (Info.getLocation().isInvalid()) { |
| 539 | // Special-case diagnostics with no location. We may not have entered a |
| 540 | // source file in this case, so we can't use the normal DiagnosticsRenderer |
| 541 | // machinery. |
Ted Kremenek | 63631bd | 2013-02-21 21:40:44 +0000 | [diff] [blame] | 542 | |
| 543 | // Make sure we bracket all notes as "sub-diagnostics". This matches |
| 544 | // the behavior in SDiagsRenderer::emitDiagnostic(). |
| 545 | if (DiagLevel == DiagnosticsEngine::Note) |
| 546 | EnterDiagBlock(); |
| 547 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 548 | EmitDiagnosticMessage(SourceLocation(), PresumedLoc(), DiagLevel, |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 549 | State->diagBuf, 0, &Info); |
Ted Kremenek | 63631bd | 2013-02-21 21:40:44 +0000 | [diff] [blame] | 550 | |
| 551 | if (DiagLevel == DiagnosticsEngine::Note) |
| 552 | ExitDiagBlock(); |
| 553 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
| 556 | |
| 557 | assert(Info.hasSourceManager() && LangOpts && |
| 558 | "Unexpected diagnostic with valid location outside of a source file"); |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 559 | SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 560 | Renderer.emitDiagnostic(Info.getLocation(), DiagLevel, |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 561 | State->diagBuf.str(), |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 562 | Info.getRanges(), |
| 563 | llvm::makeArrayRef(Info.getFixItHints(), |
| 564 | Info.getNumFixItHints()), |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 565 | &Info.getSourceManager(), |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 566 | &Info); |
| 567 | } |
| 568 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 569 | void SDiagsWriter::EmitDiagnosticMessage(SourceLocation Loc, |
| 570 | PresumedLoc PLoc, |
| 571 | DiagnosticsEngine::Level Level, |
| 572 | StringRef Message, |
| 573 | const SourceManager *SM, |
| 574 | DiagOrStoredDiag D) { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 575 | llvm::BitstreamWriter &Stream = State->Stream; |
| 576 | RecordData &Record = State->Record; |
| 577 | AbbreviationMap &Abbrevs = State->Abbrevs; |
| 578 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 579 | // Emit the RECORD_DIAG record. |
| 580 | Record.clear(); |
| 581 | Record.push_back(RECORD_DIAG); |
| 582 | Record.push_back(Level); |
| 583 | AddLocToRecord(Loc, SM, PLoc, Record); |
| 584 | |
| 585 | if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) { |
| 586 | // Emit the category string lazily and get the category ID. |
| 587 | unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID()); |
| 588 | Record.push_back(getEmitCategory(DiagID)); |
| 589 | // Emit the diagnostic flag string lazily and get the mapped ID. |
| 590 | Record.push_back(getEmitDiagnosticFlag(Level, Info->getID())); |
| 591 | } else { |
| 592 | Record.push_back(getEmitCategory()); |
| 593 | Record.push_back(getEmitDiagnosticFlag(Level)); |
| 594 | } |
| 595 | |
| 596 | Record.push_back(Message.size()); |
| 597 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, Message); |
| 598 | } |
| 599 | |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 600 | void |
| 601 | SDiagsRenderer::emitDiagnosticMessage(SourceLocation Loc, |
| 602 | PresumedLoc PLoc, |
| 603 | DiagnosticsEngine::Level Level, |
| 604 | StringRef Message, |
| 605 | ArrayRef<clang::CharSourceRange> Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 606 | const SourceManager *SM, |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 607 | DiagOrStoredDiag D) { |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 608 | Writer.EmitDiagnosticMessage(Loc, PLoc, Level, Message, SM, D); |
| 609 | } |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 610 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 611 | void SDiagsWriter::EnterDiagBlock() { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 612 | State->Stream.EnterSubblock(BLOCK_DIAG, 4); |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 613 | } |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 614 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 615 | void SDiagsWriter::ExitDiagBlock() { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 616 | State->Stream.ExitBlock(); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 619 | void SDiagsRenderer::beginDiagnostic(DiagOrStoredDiag D, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 620 | DiagnosticsEngine::Level Level) { |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 621 | if (Level == DiagnosticsEngine::Note) |
| 622 | Writer.EnterDiagBlock(); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 625 | void SDiagsRenderer::endDiagnostic(DiagOrStoredDiag D, |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 626 | DiagnosticsEngine::Level Level) { |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 627 | // Only end note diagnostics here, because we can't be sure when we've seen |
| 628 | // the last note associated with a non-note diagnostic. |
| 629 | if (Level == DiagnosticsEngine::Note) |
| 630 | Writer.ExitDiagBlock(); |
| 631 | } |
| 632 | |
| 633 | void SDiagsWriter::EmitCodeContext(SmallVectorImpl<CharSourceRange> &Ranges, |
| 634 | ArrayRef<FixItHint> Hints, |
| 635 | const SourceManager &SM) { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 636 | llvm::BitstreamWriter &Stream = State->Stream; |
| 637 | RecordData &Record = State->Record; |
| 638 | AbbreviationMap &Abbrevs = State->Abbrevs; |
| 639 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 640 | // Emit Source Ranges. |
| 641 | for (ArrayRef<CharSourceRange>::iterator I = Ranges.begin(), E = Ranges.end(); |
| 642 | I != E; ++I) |
| 643 | if (I->isValid()) |
| 644 | EmitCharSourceRange(*I, SM); |
| 645 | |
| 646 | // Emit FixIts. |
| 647 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 648 | I != E; ++I) { |
| 649 | const FixItHint &Fix = *I; |
| 650 | if (Fix.isNull()) |
| 651 | continue; |
| 652 | Record.clear(); |
| 653 | Record.push_back(RECORD_FIXIT); |
| 654 | AddCharSourceRangeToRecord(Fix.RemoveRange, Record, SM); |
| 655 | Record.push_back(Fix.CodeToInsert.size()); |
| 656 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record, |
| 657 | Fix.CodeToInsert); |
| 658 | } |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | void SDiagsRenderer::emitCodeContext(SourceLocation Loc, |
| 662 | DiagnosticsEngine::Level Level, |
| 663 | SmallVectorImpl<CharSourceRange> &Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 664 | ArrayRef<FixItHint> Hints, |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 665 | const SourceManager &SM) { |
| 666 | Writer.EmitCodeContext(Ranges, Hints, SM); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 669 | void SDiagsRenderer::emitNote(SourceLocation Loc, StringRef Message, |
| 670 | const SourceManager *SM) { |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 671 | Writer.EnterDiagBlock(); |
| 672 | PresumedLoc PLoc = SM ? SM->getPresumedLoc(Loc) : PresumedLoc(); |
| 673 | Writer.EmitDiagnosticMessage(Loc, PLoc, DiagnosticsEngine::Note, |
| 674 | Message, SM, DiagOrStoredDiag()); |
| 675 | Writer.ExitDiagBlock(); |
Ted Kremenek | 2a76410 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Argyrios Kyrtzidis | 29f2787 | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 678 | void SDiagsWriter::finish() { |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 679 | // The original instance is responsible for writing the file. |
| 680 | if (!OriginalInstance) |
| 681 | return; |
| 682 | |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 683 | // Finish off any diagnostic we were in the process of emitting. |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 684 | if (State->EmittedAnyDiagBlocks) |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 685 | ExitDiagBlock(); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 686 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 687 | // Write the generated bitstream to "Out". |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 688 | State->OS->write((char *)&State->Buffer.front(), State->Buffer.size()); |
| 689 | State->OS->flush(); |
Richard Smith | db309ae | 2012-08-21 03:11:53 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 14afab3 | 2012-11-30 23:32:31 +0000 | [diff] [blame] | 691 | State->OS.reset(0); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 692 | } |