SerializedDiagnosticPrinter.cpp 25 KB

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