clang API Documentation

LogDiagnosticPrinter.cpp
Go to the documentation of this file.
00001 //===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "clang/Frontend/LogDiagnosticPrinter.h"
00011 #include "clang/Basic/DiagnosticOptions.h"
00012 #include "clang/Basic/FileManager.h"
00013 #include "clang/Basic/PlistSupport.h"
00014 #include "clang/Basic/SourceManager.h"
00015 #include "llvm/ADT/SmallString.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 using namespace clang;
00019 using namespace markup;
00020 
00021 LogDiagnosticPrinter::LogDiagnosticPrinter(
00022     raw_ostream &os, DiagnosticOptions *diags,
00023     std::unique_ptr<raw_ostream> StreamOwner)
00024     : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
00025       DiagOpts(diags) {}
00026 
00027 static StringRef getLevelName(DiagnosticsEngine::Level Level) {
00028   switch (Level) {
00029   case DiagnosticsEngine::Ignored: return "ignored";
00030   case DiagnosticsEngine::Remark:  return "remark";
00031   case DiagnosticsEngine::Note:    return "note";
00032   case DiagnosticsEngine::Warning: return "warning";
00033   case DiagnosticsEngine::Error:   return "error";
00034   case DiagnosticsEngine::Fatal:   return "fatal error";
00035   }
00036   llvm_unreachable("Invalid DiagnosticsEngine level!");
00037 }
00038 
00039 void
00040 LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
00041                                     const LogDiagnosticPrinter::DiagEntry &DE) {
00042   OS << "    <dict>\n";
00043   OS << "      <key>level</key>\n"
00044      << "      ";
00045   EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
00046   if (!DE.Filename.empty()) {
00047     OS << "      <key>filename</key>\n"
00048        << "      ";
00049     EmitString(OS, DE.Filename) << '\n';
00050   }
00051   if (DE.Line != 0) {
00052     OS << "      <key>line</key>\n"
00053        << "      ";
00054     EmitInteger(OS, DE.Line) << '\n';
00055   }
00056   if (DE.Column != 0) {
00057     OS << "      <key>column</key>\n"
00058        << "      ";
00059     EmitInteger(OS, DE.Column) << '\n';
00060   }
00061   if (!DE.Message.empty()) {
00062     OS << "      <key>message</key>\n"
00063        << "      ";
00064     EmitString(OS, DE.Message) << '\n';
00065   }
00066   OS << "      <key>ID</key>\n"
00067      << "      ";
00068   EmitInteger(OS, DE.DiagnosticID) << '\n';
00069   if (!DE.WarningOption.empty()) {
00070     OS << "      <key>WarningOption</key>\n"
00071        << "      ";
00072     EmitString(OS, DE.WarningOption) << '\n';
00073   }
00074   OS << "    </dict>\n";
00075 }
00076 
00077 void LogDiagnosticPrinter::EndSourceFile() {
00078   // We emit all the diagnostics in EndSourceFile. However, we don't emit any
00079   // entry if no diagnostics were present.
00080   //
00081   // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
00082   // will miss any diagnostics which are emitted after and outside the
00083   // translation unit processing.
00084   if (Entries.empty())
00085     return;
00086 
00087   // Write to a temporary string to ensure atomic write of diagnostic object.
00088   SmallString<512> Msg;
00089   llvm::raw_svector_ostream OS(Msg);
00090 
00091   OS << "<dict>\n";
00092   if (!MainFilename.empty()) {
00093     OS << "  <key>main-file</key>\n"
00094        << "  ";
00095     EmitString(OS, MainFilename) << '\n';
00096   }
00097   if (!DwarfDebugFlags.empty()) {
00098     OS << "  <key>dwarf-debug-flags</key>\n"
00099        << "  ";
00100     EmitString(OS, DwarfDebugFlags) << '\n';
00101   }
00102   OS << "  <key>diagnostics</key>\n";
00103   OS << "  <array>\n";
00104   for (auto &DE : Entries)
00105     EmitDiagEntry(OS, DE);
00106   OS << "  </array>\n";
00107   OS << "</dict>\n";
00108 
00109   this->OS << OS.str();
00110 }
00111 
00112 void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
00113                                             const Diagnostic &Info) {
00114   // Default implementation (Warnings/errors count).
00115   DiagnosticConsumer::HandleDiagnostic(Level, Info);
00116 
00117   // Initialize the main file name, if we haven't already fetched it.
00118   if (MainFilename.empty() && Info.hasSourceManager()) {
00119     const SourceManager &SM = Info.getSourceManager();
00120     FileID FID = SM.getMainFileID();
00121     if (!FID.isInvalid()) {
00122       const FileEntry *FE = SM.getFileEntryForID(FID);
00123       if (FE && FE->isValid())
00124         MainFilename = FE->getName();
00125     }
00126   }
00127 
00128   // Create the diag entry.
00129   DiagEntry DE;
00130   DE.DiagnosticID = Info.getID();
00131   DE.DiagnosticLevel = Level;
00132 
00133   DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
00134 
00135   // Format the message.
00136   SmallString<100> MessageStr;
00137   Info.FormatDiagnostic(MessageStr);
00138   DE.Message = MessageStr.str();
00139 
00140   // Set the location information.
00141   DE.Filename = "";
00142   DE.Line = DE.Column = 0;
00143   if (Info.getLocation().isValid() && Info.hasSourceManager()) {
00144     const SourceManager &SM = Info.getSourceManager();
00145     PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
00146 
00147     if (PLoc.isInvalid()) {
00148       // At least print the file name if available:
00149       FileID FID = SM.getFileID(Info.getLocation());
00150       if (!FID.isInvalid()) {
00151         const FileEntry *FE = SM.getFileEntryForID(FID);
00152         if (FE && FE->isValid())
00153           DE.Filename = FE->getName();
00154       }
00155     } else {
00156       DE.Filename = PLoc.getFilename();
00157       DE.Line = PLoc.getLine();
00158       DE.Column = PLoc.getColumn();
00159     }
00160   }
00161 
00162   // Record the diagnostic entry.
00163   Entries.push_back(DE);
00164 }
00165