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" |
| 19 | #include "clang/Frontend/SerializedDiagnosticPrinter.h" |
| 20 | |
| 21 | using namespace clang; |
Ted Kremenek | fdd0ced | 2011-11-05 00:09:57 +0000 | [diff] [blame] | 22 | using namespace clang::serialized_diags; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | /// \brief A utility class for entering and exiting bitstream blocks. |
| 27 | class BlockEnterExit { |
| 28 | llvm::BitstreamWriter &Stream; |
| 29 | public: |
| 30 | BlockEnterExit(llvm::BitstreamWriter &stream, unsigned blockID, |
| 31 | unsigned codelen = 3) |
| 32 | : Stream(stream) { |
| 33 | Stream.EnterSubblock(blockID, codelen); |
| 34 | } |
| 35 | ~BlockEnterExit() { |
| 36 | Stream.ExitBlock(); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | class AbbreviationMap { |
| 41 | llvm::DenseMap<unsigned, unsigned> Abbrevs; |
| 42 | public: |
| 43 | AbbreviationMap() {} |
| 44 | |
| 45 | void set(unsigned recordID, unsigned abbrevID) { |
| 46 | assert(Abbrevs.find(recordID) == Abbrevs.end() |
| 47 | && "Abbreviation already set."); |
| 48 | Abbrevs[recordID] = abbrevID; |
| 49 | } |
| 50 | |
| 51 | unsigned get(unsigned recordID) { |
| 52 | assert(Abbrevs.find(recordID) != Abbrevs.end() && |
| 53 | "Abbreviation not set."); |
| 54 | return Abbrevs[recordID]; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | typedef llvm::SmallVector<uint64_t, 64> RecordData; |
| 59 | typedef llvm::SmallVectorImpl<uint64_t> RecordDataImpl; |
| 60 | |
| 61 | class SDiagsWriter : public DiagnosticConsumer { |
| 62 | public: |
| 63 | SDiagsWriter(DiagnosticsEngine &diags, llvm::raw_ostream *os) |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 64 | : Stream(Buffer), OS(os), Diags(diags), inNonNoteDiagnostic(false) |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 65 | { |
| 66 | EmitPreamble(); |
| 67 | }; |
| 68 | |
| 69 | ~SDiagsWriter() {} |
| 70 | |
| 71 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 72 | const Diagnostic &Info); |
| 73 | |
| 74 | void EndSourceFile(); |
| 75 | |
| 76 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 77 | // It makes no sense to clone this. |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | /// \brief Emit the preamble for the serialized diagnostics. |
| 83 | void EmitPreamble(); |
| 84 | |
| 85 | /// \brief Emit the BLOCKINFO block. |
| 86 | void EmitBlockInfoBlock(); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 88 | /// \brief Emit a record for a CharSourceRange. |
| 89 | void EmitCharSourceRange(CharSourceRange R); |
| 90 | |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 91 | /// \brief Emit the string information for the category for a diagnostic. |
| 92 | unsigned getEmitCategory(unsigned DiagID); |
| 93 | |
| 94 | /// \brief Emit the string information for diagnostic flags. |
| 95 | unsigned getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel, |
| 96 | const Diagnostic &Info); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 98 | /// \brief Emit (lazily) the file string and retrieved the file identifier. |
| 99 | unsigned getEmitFile(SourceLocation Loc); |
| 100 | |
| 101 | /// \brief Add SourceLocation information the specified record. |
| 102 | void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record); |
| 103 | |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 104 | /// \brief Add CharSourceRange information the specified record. |
| 105 | void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record); |
| 106 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 107 | /// \brief The version of the diagnostics file. |
| 108 | enum { Version = 1 }; |
| 109 | |
| 110 | /// \brief The byte buffer for the serialized content. |
| 111 | std::vector<unsigned char> Buffer; |
| 112 | |
| 113 | /// \brief The BitStreamWriter for the serialized diagnostics. |
| 114 | llvm::BitstreamWriter Stream; |
| 115 | |
| 116 | /// \brief The name of the diagnostics file. |
| 117 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 118 | |
| 119 | /// \brief The DiagnosticsEngine tied to all diagnostic locations. |
| 120 | DiagnosticsEngine &Diags; |
| 121 | |
| 122 | /// \brief The set of constructed record abbreviations. |
| 123 | AbbreviationMap Abbrevs; |
| 124 | |
| 125 | /// \brief A utility buffer for constructing record content. |
| 126 | RecordData Record; |
| 127 | |
| 128 | /// \brief A text buffer for rendering diagnostic text. |
| 129 | llvm::SmallString<256> diagBuf; |
| 130 | |
| 131 | /// \brief The collection of diagnostic categories used. |
| 132 | llvm::DenseSet<unsigned> Categories; |
| 133 | |
| 134 | /// \brief The collection of files used. |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 135 | llvm::DenseMap<const FileEntry *, unsigned> Files; |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 136 | |
| 137 | typedef llvm::DenseMap<const void *, std::pair<unsigned, llvm::StringRef> > |
| 138 | DiagFlagsTy; |
| 139 | |
| 140 | /// \brief Map for uniquing strings. |
| 141 | DiagFlagsTy DiagFlags; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 143 | /// \brief Flag indicating whether or not we are in the process of |
| 144 | /// emitting a non-note diagnostic. |
| 145 | bool inNonNoteDiagnostic; |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 146 | }; |
| 147 | } // end anonymous namespace |
| 148 | |
| 149 | namespace clang { |
| 150 | namespace serialized_diags { |
| 151 | DiagnosticConsumer *create(llvm::raw_ostream *OS, DiagnosticsEngine &Diags) { |
| 152 | return new SDiagsWriter(Diags, OS); |
| 153 | } |
| 154 | } // end namespace serialized_diags |
| 155 | } // end namespace clang |
| 156 | |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | // Serialization methods. |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | |
| 161 | /// \brief Emits a block ID in the BLOCKINFO block. |
| 162 | static void EmitBlockID(unsigned ID, const char *Name, |
| 163 | llvm::BitstreamWriter &Stream, |
| 164 | RecordDataImpl &Record) { |
| 165 | Record.clear(); |
| 166 | Record.push_back(ID); |
| 167 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 168 | |
| 169 | // Emit the block name if present. |
| 170 | if (Name == 0 || Name[0] == 0) |
| 171 | return; |
| 172 | |
| 173 | Record.clear(); |
| 174 | |
| 175 | while (*Name) |
| 176 | Record.push_back(*Name++); |
| 177 | |
| 178 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 179 | } |
| 180 | |
| 181 | /// \brief Emits a record ID in the BLOCKINFO block. |
| 182 | static void EmitRecordID(unsigned ID, const char *Name, |
| 183 | llvm::BitstreamWriter &Stream, |
| 184 | RecordDataImpl &Record){ |
| 185 | Record.clear(); |
| 186 | Record.push_back(ID); |
| 187 | |
| 188 | while (*Name) |
| 189 | Record.push_back(*Name++); |
| 190 | |
| 191 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
| 192 | } |
| 193 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 194 | void SDiagsWriter::AddLocToRecord(SourceLocation Loc, |
| 195 | RecordDataImpl &Record) { |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 196 | if (Loc.isInvalid()) { |
| 197 | // Emit a "sentinel" location. |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 198 | Record.push_back((unsigned) 0); // File. |
| 199 | Record.push_back(~(unsigned)0); // Line. |
| 200 | Record.push_back(~(unsigned)0); // Column. |
| 201 | Record.push_back(~(unsigned)0); // Offset. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 205 | SourceManager &SM = Diags.getSourceManager(); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 206 | Loc = SM.getSpellingLoc(Loc); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 207 | Record.push_back(getEmitFile(Loc)); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 208 | Record.push_back(SM.getSpellingLineNumber(Loc)); |
| 209 | Record.push_back(SM.getSpellingColumnNumber(Loc)); |
| 210 | |
| 211 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 212 | FileID FID = LocInfo.first; |
| 213 | unsigned FileOffset = LocInfo.second; |
| 214 | Record.push_back(FileOffset); |
| 215 | } |
| 216 | |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 217 | void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range, |
| 218 | RecordDataImpl &Record) { |
| 219 | AddLocToRecord(Range.getBegin(), Record); |
| 220 | AddLocToRecord(Range.getEnd(), Record); |
| 221 | } |
| 222 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 223 | unsigned SDiagsWriter::getEmitFile(SourceLocation Loc) { |
| 224 | SourceManager &SM = Diags.getSourceManager(); |
| 225 | assert(Loc.isValid()); |
| 226 | const std::pair<FileID, unsigned> &LocInfo = SM.getDecomposedLoc(Loc); |
| 227 | const FileEntry *FE = SM.getFileEntryForID(LocInfo.first); |
| 228 | if (!FE) |
| 229 | return 0; |
| 230 | |
| 231 | unsigned &entry = Files[FE]; |
| 232 | if (entry) |
| 233 | return entry; |
| 234 | |
| 235 | // Lazily generate the record for the file. |
| 236 | entry = Files.size(); |
| 237 | RecordData Record; |
| 238 | Record.push_back(RECORD_FILENAME); |
| 239 | Record.push_back(entry); |
| 240 | Record.push_back(FE->getSize()); |
| 241 | Record.push_back(FE->getModificationTime()); |
| 242 | StringRef Name = FE->getName(); |
| 243 | Record.push_back(Name.size()); |
| 244 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FILENAME), Record, Name); |
| 245 | |
| 246 | return entry; |
| 247 | } |
| 248 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 249 | void SDiagsWriter::EmitCharSourceRange(CharSourceRange R) { |
| 250 | Record.clear(); |
| 251 | Record.push_back(RECORD_SOURCE_RANGE); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 252 | AddCharSourceRangeToRecord(R, Record); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 253 | Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_SOURCE_RANGE), Record); |
| 254 | } |
| 255 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 256 | /// \brief Emits the preamble of the diagnostics file. |
| 257 | void SDiagsWriter::EmitPreamble() { |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 258 | // Emit the file header. |
| 259 | Stream.Emit((unsigned)'D', 8); |
Ted Kremenek | 069f9c2 | 2011-11-05 00:09:53 +0000 | [diff] [blame] | 260 | Stream.Emit((unsigned) Version, 32 - 8); |
| 261 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 262 | EmitBlockInfoBlock(); |
| 263 | } |
| 264 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 265 | static void AddSourceLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) { |
| 266 | using namespace llvm; |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 267 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 268 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line. |
| 269 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column. |
| 270 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Offset; |
| 271 | } |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 272 | |
| 273 | static void AddRangeLocationAbbrev(llvm::BitCodeAbbrev *Abbrev) { |
| 274 | AddSourceLocationAbbrev(Abbrev); |
| 275 | AddSourceLocationAbbrev(Abbrev); |
| 276 | } |
| 277 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 278 | void SDiagsWriter::EmitBlockInfoBlock() { |
| 279 | Stream.EnterBlockInfoBlock(3); |
| 280 | |
| 281 | // ==---------------------------------------------------------------------==// |
| 282 | // The subsequent records and Abbrevs are for the "Diagnostic" block. |
| 283 | // ==---------------------------------------------------------------------==// |
| 284 | |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 285 | EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record); |
| 286 | EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 287 | EmitRecordID(RECORD_SOURCE_RANGE, "SrcRange", Stream, Record); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 288 | EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 289 | EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 290 | EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 291 | EmitRecordID(RECORD_FIXIT, "FixIt", Stream, Record); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 293 | // Emit Abbrevs. |
| 294 | using namespace llvm; |
| 295 | |
| 296 | // Emit abbreviation for RECORD_DIAG. |
| 297 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 298 | Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG)); |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 299 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 300 | AddSourceLocationAbbrev(Abbrev); |
Ted Kremenek | 45d9275 | 2011-11-05 00:09:50 +0000 | [diff] [blame] | 301 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category. |
| 302 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 303 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 304 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text. |
| 305 | Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 307 | // Emit abbrevation for RECORD_CATEGORY. |
| 308 | Abbrev = new BitCodeAbbrev(); |
| 309 | Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY)); |
| 310 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Category ID. |
| 311 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size. |
| 312 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text. |
| 313 | Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); |
| 314 | |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 315 | // Emit abbrevation for RECORD_SOURCE_RANGE. |
| 316 | Abbrev = new BitCodeAbbrev(); |
| 317 | Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_RANGE)); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 318 | AddRangeLocationAbbrev(Abbrev); |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 319 | Abbrevs.set(RECORD_SOURCE_RANGE, |
| 320 | Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 321 | |
| 322 | // Emit the abbreviation for RECORD_DIAG_FLAG. |
| 323 | Abbrev = new BitCodeAbbrev(); |
| 324 | Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG)); |
| 325 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID. |
| 326 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 327 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text. |
| 328 | Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, |
| 329 | Abbrev)); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 330 | |
| 331 | // Emit the abbreviation for RECORD_FILENAME. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 332 | Abbrev = new BitCodeAbbrev(); |
Ted Kremenek | 28eac52 | 2011-11-05 00:09:43 +0000 | [diff] [blame] | 333 | Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME)); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 334 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped file ID. |
| 335 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Size. |
| 336 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Modifcation time. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 337 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 338 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text. |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 339 | Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 340 | Abbrev)); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 341 | |
| 342 | // Emit the abbreviation for RECORD_FIXIT. |
| 343 | Abbrev = new BitCodeAbbrev(); |
| 344 | Abbrev->Add(BitCodeAbbrevOp(RECORD_FIXIT)); |
| 345 | AddRangeLocationAbbrev(Abbrev); |
| 346 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. |
| 347 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // FixIt text. |
| 348 | Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, |
| 349 | Abbrev)); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 350 | |
| 351 | Stream.ExitBlock(); |
| 352 | } |
| 353 | |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 354 | unsigned SDiagsWriter::getEmitCategory(unsigned int DiagID) { |
| 355 | unsigned category = DiagnosticIDs::getCategoryNumberForDiag(DiagID); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 357 | if (Categories.count(category)) |
| 358 | return category; |
| 359 | |
| 360 | Categories.insert(category); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 361 | |
| 362 | // We use a local version of 'Record' so that we can be generating |
| 363 | // another record when we lazily generate one for the category entry. |
| 364 | RecordData Record; |
| 365 | Record.push_back(RECORD_CATEGORY); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 366 | Record.push_back(category); |
| 367 | StringRef catName = DiagnosticIDs::getCategoryNameFromID(category); |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 368 | Record.push_back(catName.size()); |
| 369 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_CATEGORY), Record, catName); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 370 | |
| 371 | return category; |
| 372 | } |
| 373 | |
| 374 | unsigned SDiagsWriter::getEmitDiagnosticFlag(DiagnosticsEngine::Level DiagLevel, |
| 375 | const Diagnostic &Info) { |
| 376 | if (DiagLevel == DiagnosticsEngine::Note) |
| 377 | return 0; // No flag for notes. |
| 378 | |
| 379 | StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(Info.getID()); |
| 380 | if (FlagName.empty()) |
| 381 | return 0; |
| 382 | |
| 383 | // Here we assume that FlagName points to static data whose pointer |
| 384 | // value is fixed. This allows us to unique by diagnostic groups. |
| 385 | const void *data = FlagName.data(); |
| 386 | std::pair<unsigned, StringRef> &entry = DiagFlags[data]; |
| 387 | if (entry.first == 0) { |
| 388 | entry.first = DiagFlags.size(); |
| 389 | entry.second = FlagName; |
| 390 | |
| 391 | // Lazily emit the string in a separate record. |
| 392 | RecordData Record; |
| 393 | Record.push_back(RECORD_DIAG_FLAG); |
| 394 | Record.push_back(entry.first); |
| 395 | Record.push_back(FlagName.size()); |
| 396 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG_FLAG), |
| 397 | Record, FlagName); |
| 398 | } |
| 399 | |
| 400 | return entry.first; |
Ted Kremenek | 0dbadc4 | 2011-11-05 00:10:04 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 403 | void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 404 | const Diagnostic &Info) { |
| 405 | |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 406 | if (DiagLevel != DiagnosticsEngine::Note) { |
| 407 | if (inNonNoteDiagnostic) { |
| 408 | // We have encountered a non-note diagnostic. Finish up the previous |
| 409 | // diagnostic block before starting a new one. |
| 410 | Stream.ExitBlock(); |
| 411 | } |
| 412 | inNonNoteDiagnostic = true; |
| 413 | } |
| 414 | |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 415 | Stream.EnterSubblock(BLOCK_DIAG, 4); |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 416 | |
| 417 | // Emit the RECORD_DIAG record. |
| 418 | Record.clear(); |
| 419 | Record.push_back(RECORD_DIAG); |
| 420 | Record.push_back(DiagLevel); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 421 | AddLocToRecord(Info.getLocation(), Record); |
Ted Kremenek | 3baf63d | 2011-11-05 00:10:07 +0000 | [diff] [blame] | 422 | // Emit the category string lazily and get the category ID. |
| 423 | Record.push_back(getEmitCategory(Info.getID())); |
| 424 | // Emit the diagnostic flag string lazily and get the mapped ID. |
| 425 | Record.push_back(getEmitDiagnosticFlag(DiagLevel, Info)); |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 426 | // Emit the diagnostic text. |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 427 | diagBuf.clear(); |
| 428 | Info.FormatDiagnostic(diagBuf); // Compute the diagnostic text. |
| 429 | Record.push_back(diagBuf.str().size()); |
| 430 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, diagBuf.str()); |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 431 | |
| 432 | // Emit Source Ranges. |
Ted Kremenek | 2a20b4f | 2011-11-05 00:10:01 +0000 | [diff] [blame] | 433 | ArrayRef<CharSourceRange> Ranges = Info.getRanges(); |
| 434 | for (ArrayRef<CharSourceRange>::iterator it=Ranges.begin(), ei=Ranges.end(); |
| 435 | it != ei; ++it) { |
| 436 | EmitCharSourceRange(*it); |
| 437 | } |
| 438 | |
Ted Kremenek | 96dcade | 2011-11-05 03:34:23 +0000 | [diff] [blame^] | 439 | // Emit FixIts. |
| 440 | for (unsigned i = 0, n = Info.getNumFixItHints(); i != n; ++i) { |
| 441 | const FixItHint &fix = Info.getFixItHint(i); |
| 442 | if (fix.isNull()) |
| 443 | continue; |
| 444 | Record.clear(); |
| 445 | Record.push_back(RECORD_FIXIT); |
| 446 | AddCharSourceRangeToRecord(fix.RemoveRange, Record); |
| 447 | Record.push_back(fix.CodeToInsert.size()); |
| 448 | Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record, |
| 449 | fix.CodeToInsert); |
| 450 | } |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 451 | |
| 452 | if (DiagLevel == DiagnosticsEngine::Note) { |
| 453 | // Notes currently cannot have child diagnostics. Complete the |
| 454 | // diagnostic now. |
| 455 | Stream.ExitBlock(); |
| 456 | } |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 459 | void SDiagsWriter::EndSourceFile() { |
Ted Kremenek | 59b6161 | 2011-11-05 00:09:47 +0000 | [diff] [blame] | 460 | if (inNonNoteDiagnostic) { |
| 461 | // Finish off any diagnostics we were in the process of emitting. |
| 462 | Stream.ExitBlock(); |
| 463 | inNonNoteDiagnostic = false; |
| 464 | } |
Ted Kremenek | 0d34e6e | 2011-11-05 00:10:11 +0000 | [diff] [blame] | 465 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 466 | // Write the generated bitstream to "Out". |
| 467 | OS->write((char *)&Buffer.front(), Buffer.size()); |
| 468 | OS->flush(); |
| 469 | |
| 470 | OS.reset(0); |
| 471 | } |
| 472 | |