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