LogDiagnosticPrinter.cpp 4.9 KB

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