SerializedDiagnosticPrinter.cpp revision fdd0ced001babd4e65fb909cc2f847df53faf764
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> 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 21using namespace clang; 22using namespace clang::serialized_diags; 23 24namespace { 25 26/// \brief A utility class for entering and exiting bitstream blocks. 27class BlockEnterExit { 28 llvm::BitstreamWriter &Stream; 29public: 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 40class AbbreviationMap { 41 llvm::DenseMap<unsigned, unsigned> Abbrevs; 42public: 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 58typedef llvm::SmallVector<uint64_t, 64> RecordData; 59typedef llvm::SmallVectorImpl<uint64_t> RecordDataImpl; 60 61class SDiagsWriter : public DiagnosticConsumer { 62public: 63 SDiagsWriter(DiagnosticsEngine &diags, llvm::raw_ostream *os) 64 : Stream(Buffer), OS(os), Diags(diags), inNonNoteDiagnostic(false) 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 81private: 82 /// \brief Emit the preamble for the serialized diagnostics. 83 void EmitPreamble(); 84 85 /// \brief Emit the BLOCKINFO block. 86 void EmitBlockInfoBlock(); 87 88 /// \brief Emit the raw characters of the provided string. 89 void EmitRawStringContents(StringRef str); 90 91 /// \brief Emit the block containing categories and file names. 92 void EmitCategoriesAndFileNames(); 93 94 /// \brief The version of the diagnostics file. 95 enum { Version = 1 }; 96 97 /// \brief The byte buffer for the serialized content. 98 std::vector<unsigned char> Buffer; 99 100 /// \brief The BitStreamWriter for the serialized diagnostics. 101 llvm::BitstreamWriter Stream; 102 103 /// \brief The name of the diagnostics file. 104 llvm::OwningPtr<llvm::raw_ostream> OS; 105 106 /// \brief The DiagnosticsEngine tied to all diagnostic locations. 107 DiagnosticsEngine &Diags; 108 109 /// \brief The set of constructed record abbreviations. 110 AbbreviationMap Abbrevs; 111 112 /// \brief A utility buffer for constructing record content. 113 RecordData Record; 114 115 /// \brief A text buffer for rendering diagnostic text. 116 llvm::SmallString<256> diagBuf; 117 118 /// \brief The collection of diagnostic categories used. 119 llvm::DenseSet<unsigned> Categories; 120 121 /// \brief The collection of files used. 122 llvm::DenseSet<FileID> Files; 123 124 typedef llvm::DenseMap<const void *, std::pair<unsigned, llvm::StringRef> > 125 DiagFlagsTy; 126 127 /// \brief Map for uniquing strings. 128 DiagFlagsTy DiagFlags; 129 130 /// \brief Flag indicating whether or not we are in the process of 131 /// emitting a non-note diagnostic. 132 bool inNonNoteDiagnostic; 133}; 134} // end anonymous namespace 135 136namespace clang { 137namespace serialized_diags { 138DiagnosticConsumer *create(llvm::raw_ostream *OS, DiagnosticsEngine &Diags) { 139 return new SDiagsWriter(Diags, OS); 140} 141} // end namespace serialized_diags 142} // end namespace clang 143 144//===----------------------------------------------------------------------===// 145// Serialization methods. 146//===----------------------------------------------------------------------===// 147 148/// \brief Emits a block ID in the BLOCKINFO block. 149static void EmitBlockID(unsigned ID, const char *Name, 150 llvm::BitstreamWriter &Stream, 151 RecordDataImpl &Record) { 152 Record.clear(); 153 Record.push_back(ID); 154 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 155 156 // Emit the block name if present. 157 if (Name == 0 || Name[0] == 0) 158 return; 159 160 Record.clear(); 161 162 while (*Name) 163 Record.push_back(*Name++); 164 165 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 166} 167 168/// \brief Emits a record ID in the BLOCKINFO block. 169static void EmitRecordID(unsigned ID, const char *Name, 170 llvm::BitstreamWriter &Stream, 171 RecordDataImpl &Record){ 172 Record.clear(); 173 Record.push_back(ID); 174 175 while (*Name) 176 Record.push_back(*Name++); 177 178 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 179} 180 181/// \brief Emits the preamble of the diagnostics file. 182void SDiagsWriter::EmitPreamble() { 183 // EmitRawStringContents("CLANG_DIAGS"); 184 // Stream.Emit(Version, 32); 185 186 // Emit the file header. 187 Stream.Emit((unsigned)'D', 8); 188 Stream.Emit((unsigned) Version, 32 - 8); 189 190 EmitBlockInfoBlock(); 191} 192 193void SDiagsWriter::EmitBlockInfoBlock() { 194 Stream.EnterBlockInfoBlock(3); 195 196 // ==---------------------------------------------------------------------==// 197 // The subsequent records and Abbrevs are for the "Diagnostic" block. 198 // ==---------------------------------------------------------------------==// 199 200 EmitBlockID(BLOCK_DIAG, "Diag", Stream, Record); 201 EmitRecordID(RECORD_DIAG, "DiagInfo", Stream, Record); 202 203 // Emit Abbrevs. 204 using namespace llvm; 205 206 // Emit abbreviation for RECORD_DIAG. 207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 208 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG)); 209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Diag level. 210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Category. 211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID. 212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. 213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Diagnostc text. 214 Abbrevs.set(RECORD_DIAG, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev)); 215 216 // ==---------------------------------------------------------------------==// 217 // The subsequent records and Abbrevs are for the "Strings" block. 218 // ==---------------------------------------------------------------------==// 219 220 EmitBlockID(BLOCK_STRINGS, "Strings", Stream, Record); 221 EmitRecordID(RECORD_CATEGORY, "CatName", Stream, Record); 222 EmitRecordID(RECORD_FILENAME, "FileName", Stream, Record); 223 EmitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record); 224 225 Abbrev = new BitCodeAbbrev(); 226 Abbrev->Add(BitCodeAbbrevOp(RECORD_CATEGORY)); 227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Text size. 228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Category text. 229 Abbrevs.set(RECORD_CATEGORY, Stream.EmitBlockInfoAbbrev(BLOCK_STRINGS, 230 Abbrev)); 231 232 Abbrev = new BitCodeAbbrev(); 233 Abbrev->Add(BitCodeAbbrevOp(RECORD_FILENAME)); 234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 64)); // Size. 235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 64)); // Modifcation time. 236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. 237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name text. 238 Abbrevs.set(RECORD_FILENAME, Stream.EmitBlockInfoAbbrev(BLOCK_STRINGS, 239 Abbrev)); 240 241 // Emit the abbreviation for RECORD_DIAG_FLAG. 242 Abbrev = new BitCodeAbbrev(); 243 Abbrev->Add(BitCodeAbbrevOp(RECORD_DIAG_FLAG)); 244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // Mapped Diag ID. 245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Text size. 246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Flag name text. 247 Abbrevs.set(RECORD_DIAG_FLAG, Stream.EmitBlockInfoAbbrev(BLOCK_STRINGS, 248 Abbrev)); 249 250 Stream.ExitBlock(); 251} 252 253void SDiagsWriter::EmitRawStringContents(llvm::StringRef str) { 254 for (StringRef::const_iterator I = str.begin(), E = str.end(); I!=E; ++I) 255 Stream.Emit(*I, 8); 256} 257 258void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 259 const Diagnostic &Info) { 260 261 if (DiagLevel != DiagnosticsEngine::Note) { 262 if (inNonNoteDiagnostic) { 263 // We have encountered a non-note diagnostic. Finish up the previous 264 // diagnostic block before starting a new one. 265 Stream.ExitBlock(); 266 } 267 inNonNoteDiagnostic = true; 268 } 269 270 Stream.EnterSubblock(BLOCK_DIAG, 3); 271 272 // Emit the RECORD_DIAG record. 273 Record.clear(); 274 Record.push_back(RECORD_DIAG); 275 Record.push_back(DiagLevel); 276 unsigned category = DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); 277 Record.push_back(category); 278 Categories.insert(category); 279 if (DiagLevel == DiagnosticsEngine::Note) 280 Record.push_back(0); // No flag for notes. 281 else { 282 StringRef FlagName = DiagnosticIDs::getWarningOptionForDiag(Info.getID()); 283 if (FlagName.empty()) 284 Record.push_back(0); 285 else { 286 // Here we assume that FlagName points to static data whose pointer 287 // value is fixed. 288 const void *data = FlagName.data(); 289 std::pair<unsigned, StringRef> &entry = DiagFlags[data]; 290 if (entry.first == 0) { 291 entry.first = DiagFlags.size(); 292 entry.second = FlagName; 293 } 294 Record.push_back(entry.first); 295 } 296 } 297 298 diagBuf.clear(); 299 Info.FormatDiagnostic(diagBuf); // Compute the diagnostic text. 300 Record.push_back(diagBuf.str().size()); 301 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG), Record, diagBuf.str()); 302 303 // FIXME: emit location 304 // FIXME: emit ranges 305 // FIXME: emit fixits 306 307 if (DiagLevel == DiagnosticsEngine::Note) { 308 // Notes currently cannot have child diagnostics. Complete the 309 // diagnostic now. 310 Stream.ExitBlock(); 311 } 312} 313 314template <typename T> 315static void populateAndSort(std::vector<T> &scribble, 316 llvm::DenseSet<T> &set) { 317 scribble.clear(); 318 319 for (typename llvm::DenseSet<T>::iterator it = set.begin(), ei = set.end(); 320 it != ei; ++it) 321 scribble.push_back(*it); 322 323 // Sort 'scribble' so we always have a deterministic ordering in the 324 // serialized file. 325 std::sort(scribble.begin(), scribble.end()); 326} 327 328void SDiagsWriter::EmitCategoriesAndFileNames() { 329 330 if (Categories.empty() && Files.empty()) 331 return; 332 333 BlockEnterExit BlockEnter(Stream, BLOCK_STRINGS); 334 335 // Emit the category names. 336 { 337 std::vector<unsigned> scribble; 338 populateAndSort(scribble, Categories); 339 for (std::vector<unsigned>::iterator it = scribble.begin(), 340 ei = scribble.end(); it != ei ; ++it) { 341 Record.clear(); 342 Record.push_back(RECORD_CATEGORY); 343 StringRef catName = DiagnosticIDs::getCategoryNameFromID(*it); 344 Record.push_back(catName.size()); 345 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_CATEGORY), Record, catName); 346 } 347 } 348 349 // Emit the file names. 350 { 351 std::vector<FileID> scribble; 352 populateAndSort(scribble, Files); 353 for (std::vector<FileID>::iterator it = scribble.begin(), 354 ei = scribble.end(); it != ei; ++it) { 355 SourceManager &SM = Diags.getSourceManager(); 356 const FileEntry *FE = SM.getFileEntryForID(*it); 357 StringRef Name = FE->getName(); 358 359 Record.clear(); 360 Record.push_back(FE->getSize()); 361 Record.push_back(FE->getModificationTime()); 362 Record.push_back(Name.size()); 363 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FILENAME), Record, Name); 364 } 365 } 366 367 // Emit the flag strings. 368 { 369 std::vector<StringRef> scribble; 370 scribble.resize(DiagFlags.size()); 371 372 for (DiagFlagsTy::iterator it = DiagFlags.begin(), ei = DiagFlags.end(); 373 it != ei; ++it) { 374 scribble[it->second.first - 1] = it->second.second; 375 } 376 for (unsigned i = 0, n = scribble.size(); i != n; ++i) { 377 Record.clear(); 378 Record.push_back(RECORD_DIAG_FLAG); 379 Record.push_back(i+1); 380 StringRef FlagName = scribble[i]; 381 Record.push_back(FlagName.size()); 382 Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_DIAG_FLAG), 383 Record, FlagName); 384 } 385 } 386} 387 388void SDiagsWriter::EndSourceFile() { 389 if (inNonNoteDiagnostic) { 390 // Finish off any diagnostics we were in the process of emitting. 391 Stream.ExitBlock(); 392 inNonNoteDiagnostic = false; 393 } 394 395 EmitCategoriesAndFileNames(); 396 397 // Write the generated bitstream to "Out". 398 OS->write((char *)&Buffer.front(), Buffer.size()); 399 OS->flush(); 400 401 OS.reset(0); 402} 403 404