1 //===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/LogDiagnosticPrinter.h"
11 #include "clang/Basic/DiagnosticOptions.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/PlistSupport.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 using namespace clang;
19 using namespace markup;
21 LogDiagnosticPrinter::LogDiagnosticPrinter(
22 raw_ostream &os, DiagnosticOptions *diags,
23 std::unique_ptr<raw_ostream> StreamOwner)
24 : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
27 static StringRef getLevelName(DiagnosticsEngine::Level Level) {
29 case DiagnosticsEngine::Ignored: return "ignored";
30 case DiagnosticsEngine::Remark: return "remark";
31 case DiagnosticsEngine::Note: return "note";
32 case DiagnosticsEngine::Warning: return "warning";
33 case DiagnosticsEngine::Error: return "error";
34 case DiagnosticsEngine::Fatal: return "fatal error";
36 llvm_unreachable("Invalid DiagnosticsEngine level!");
40 LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
41 const LogDiagnosticPrinter::DiagEntry &DE) {
43 OS << " <key>level</key>\n"
45 EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
46 if (!DE.Filename.empty()) {
47 OS << " <key>filename</key>\n"
49 EmitString(OS, DE.Filename) << '\n';
52 OS << " <key>line</key>\n"
54 EmitInteger(OS, DE.Line) << '\n';
57 OS << " <key>column</key>\n"
59 EmitInteger(OS, DE.Column) << '\n';
61 if (!DE.Message.empty()) {
62 OS << " <key>message</key>\n"
64 EmitString(OS, DE.Message) << '\n';
66 OS << " <key>ID</key>\n"
68 EmitInteger(OS, DE.DiagnosticID) << '\n';
69 if (!DE.WarningOption.empty()) {
70 OS << " <key>WarningOption</key>\n"
72 EmitString(OS, DE.WarningOption) << '\n';
77 void LogDiagnosticPrinter::EndSourceFile() {
78 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
79 // entry if no diagnostics were present.
81 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
82 // will miss any diagnostics which are emitted after and outside the
83 // translation unit processing.
87 // Write to a temporary string to ensure atomic write of diagnostic object.
89 llvm::raw_svector_ostream OS(Msg);
92 if (!MainFilename.empty()) {
93 OS << " <key>main-file</key>\n"
95 EmitString(OS, MainFilename) << '\n';
97 if (!DwarfDebugFlags.empty()) {
98 OS << " <key>dwarf-debug-flags</key>\n"
100 EmitString(OS, DwarfDebugFlags) << '\n';
102 OS << " <key>diagnostics</key>\n";
104 for (auto &DE : Entries)
105 EmitDiagEntry(OS, DE);
109 this->OS << OS.str();
112 void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
113 const Diagnostic &Info) {
114 // Default implementation (Warnings/errors count).
115 DiagnosticConsumer::HandleDiagnostic(Level, Info);
117 // Initialize the main file name, if we haven't already fetched it.
118 if (MainFilename.empty() && Info.hasSourceManager()) {
119 const SourceManager &SM = Info.getSourceManager();
120 FileID FID = SM.getMainFileID();
122 const FileEntry *FE = SM.getFileEntryForID(FID);
123 if (FE && FE->isValid())
124 MainFilename = FE->getName();
128 // Create the diag entry.
130 DE.DiagnosticID = Info.getID();
131 DE.DiagnosticLevel = Level;
133 DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
135 // Format the message.
136 SmallString<100> MessageStr;
137 Info.FormatDiagnostic(MessageStr);
138 DE.Message = MessageStr.str();
140 // Set the location information.
142 DE.Line = DE.Column = 0;
143 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
144 const SourceManager &SM = Info.getSourceManager();
145 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
147 if (PLoc.isInvalid()) {
148 // At least print the file name if available:
149 FileID FID = SM.getFileID(Info.getLocation());
151 const FileEntry *FE = SM.getFileEntryForID(FID);
152 if (FE && FE->isValid())
153 DE.Filename = FE->getName();
156 DE.Filename = PLoc.getFilename();
157 DE.Line = PLoc.getLine();
158 DE.Column = PLoc.getColumn();
162 // Record the diagnostic entry.
163 Entries.push_back(DE);