LogDiagnosticPrinter.cpp 5.1 KB

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