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