LogDiagnosticPrinter.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
  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/LogDiagnosticPrinter.h"
  10. #include "clang/Basic/DiagnosticOptions.h"
  11. #include "clang/Basic/FileManager.h"
  12. #include "clang/Basic/PlistSupport.h"
  13. #include "clang/Basic/SourceManager.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace clang;
  18. using namespace markup;
  19. LogDiagnosticPrinter::LogDiagnosticPrinter(
  20. raw_ostream &os, DiagnosticOptions *diags,
  21. std::unique_ptr<raw_ostream> StreamOwner)
  22. : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
  23. DiagOpts(diags) {}
  24. static StringRef getLevelName(DiagnosticsEngine::Level Level) {
  25. switch (Level) {
  26. case DiagnosticsEngine::Ignored: return "ignored";
  27. case DiagnosticsEngine::Remark: return "remark";
  28. case DiagnosticsEngine::Note: return "note";
  29. case DiagnosticsEngine::Warning: return "warning";
  30. case DiagnosticsEngine::Error: return "error";
  31. case DiagnosticsEngine::Fatal: return "fatal error";
  32. }
  33. llvm_unreachable("Invalid DiagnosticsEngine level!");
  34. }
  35. void
  36. LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
  37. const LogDiagnosticPrinter::DiagEntry &DE) {
  38. OS << " <dict>\n";
  39. OS << " <key>level</key>\n"
  40. << " ";
  41. EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
  42. if (!DE.Filename.empty()) {
  43. OS << " <key>filename</key>\n"
  44. << " ";
  45. EmitString(OS, DE.Filename) << '\n';
  46. }
  47. if (DE.Line != 0) {
  48. OS << " <key>line</key>\n"
  49. << " ";
  50. EmitInteger(OS, DE.Line) << '\n';
  51. }
  52. if (DE.Column != 0) {
  53. OS << " <key>column</key>\n"
  54. << " ";
  55. EmitInteger(OS, DE.Column) << '\n';
  56. }
  57. if (!DE.Message.empty()) {
  58. OS << " <key>message</key>\n"
  59. << " ";
  60. EmitString(OS, DE.Message) << '\n';
  61. }
  62. OS << " <key>ID</key>\n"
  63. << " ";
  64. EmitInteger(OS, DE.DiagnosticID) << '\n';
  65. if (!DE.WarningOption.empty()) {
  66. OS << " <key>WarningOption</key>\n"
  67. << " ";
  68. EmitString(OS, DE.WarningOption) << '\n';
  69. }
  70. OS << " </dict>\n";
  71. }
  72. void LogDiagnosticPrinter::EndSourceFile() {
  73. // We emit all the diagnostics in EndSourceFile. However, we don't emit any
  74. // entry if no diagnostics were present.
  75. //
  76. // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
  77. // will miss any diagnostics which are emitted after and outside the
  78. // translation unit processing.
  79. if (Entries.empty())
  80. return;
  81. // Write to a temporary string to ensure atomic write of diagnostic object.
  82. SmallString<512> Msg;
  83. llvm::raw_svector_ostream OS(Msg);
  84. OS << "<dict>\n";
  85. if (!MainFilename.empty()) {
  86. OS << " <key>main-file</key>\n"
  87. << " ";
  88. EmitString(OS, MainFilename) << '\n';
  89. }
  90. if (!DwarfDebugFlags.empty()) {
  91. OS << " <key>dwarf-debug-flags</key>\n"
  92. << " ";
  93. EmitString(OS, DwarfDebugFlags) << '\n';
  94. }
  95. OS << " <key>diagnostics</key>\n";
  96. OS << " <array>\n";
  97. for (auto &DE : Entries)
  98. EmitDiagEntry(OS, DE);
  99. OS << " </array>\n";
  100. OS << "</dict>\n";
  101. this->OS << OS.str();
  102. }
  103. void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
  104. const Diagnostic &Info) {
  105. // Default implementation (Warnings/errors count).
  106. DiagnosticConsumer::HandleDiagnostic(Level, Info);
  107. // Initialize the main file name, if we haven't already fetched it.
  108. if (MainFilename.empty() && Info.hasSourceManager()) {
  109. const SourceManager &SM = Info.getSourceManager();
  110. FileID FID = SM.getMainFileID();
  111. if (!FID.isInvalid()) {
  112. const FileEntry *FE = SM.getFileEntryForID(FID);
  113. if (FE && FE->isValid())
  114. MainFilename = FE->getName();
  115. }
  116. }
  117. // Create the diag entry.
  118. DiagEntry DE;
  119. DE.DiagnosticID = Info.getID();
  120. DE.DiagnosticLevel = Level;
  121. DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
  122. // Format the message.
  123. SmallString<100> MessageStr;
  124. Info.FormatDiagnostic(MessageStr);
  125. DE.Message = MessageStr.str();
  126. // Set the location information.
  127. DE.Filename = "";
  128. DE.Line = DE.Column = 0;
  129. if (Info.getLocation().isValid() && Info.hasSourceManager()) {
  130. const SourceManager &SM = Info.getSourceManager();
  131. PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
  132. if (PLoc.isInvalid()) {
  133. // At least print the file name if available:
  134. FileID FID = SM.getFileID(Info.getLocation());
  135. if (!FID.isInvalid()) {
  136. const FileEntry *FE = SM.getFileEntryForID(FID);
  137. if (FE && FE->isValid())
  138. DE.Filename = FE->getName();
  139. }
  140. } else {
  141. DE.Filename = PLoc.getFilename();
  142. DE.Line = PLoc.getLine();
  143. DE.Column = PLoc.getColumn();
  144. }
  145. }
  146. // Record the diagnostic entry.
  147. Entries.push_back(DE);
  148. }