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