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/FileManager.h"
12 #include "clang/Basic/SourceManager.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/raw_ostream.h"
15 using namespace clang;
17 LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os,
18 const DiagnosticOptions &diags,
19 bool _OwnsOutputStream)
20 : OS(os), LangOpts(0), DiagOpts(&diags),
21 OwnsOutputStream(_OwnsOutputStream) {
24 LogDiagnosticPrinter::~LogDiagnosticPrinter() {
29 static StringRef getLevelName(DiagnosticsEngine::Level Level) {
33 case DiagnosticsEngine::Ignored: return "ignored";
34 case DiagnosticsEngine::Note: return "note";
35 case DiagnosticsEngine::Warning: return "warning";
36 case DiagnosticsEngine::Error: return "error";
37 case DiagnosticsEngine::Fatal: return "fatal error";
41 // Escape XML characters inside the raw string.
42 static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) {
43 for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) {
46 default: OS << c; break;
47 case '&': OS << "&"; break;
48 case '<': OS << "<"; break;
49 case '>': OS << ">"; break;
50 case '\'': OS << "'"; break;
51 case '\"': OS << """; break;
56 void LogDiagnosticPrinter::EndSourceFile() {
57 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
58 // entry if no diagnostics were present.
60 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
61 // will miss any diagnostics which are emitted after and outside the
62 // translation unit processing.
66 // Write to a temporary string to ensure atomic write of diagnostic object.
67 llvm::SmallString<512> Msg;
68 llvm::raw_svector_ostream OS(Msg);
71 if (!MainFilename.empty()) {
72 OS << " <key>main-file</key>\n"
74 emitString(OS, MainFilename);
77 if (!DwarfDebugFlags.empty()) {
78 OS << " <key>dwarf-debug-flags</key>\n"
80 emitString(OS, DwarfDebugFlags);
83 OS << " <key>diagnostics</key>\n";
85 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
86 DiagEntry &DE = Entries[i];
89 OS << " <key>level</key>\n"
91 emitString(OS, getLevelName(DE.DiagnosticLevel));
93 if (!DE.Filename.empty()) {
94 OS << " <key>filename</key>\n"
96 emitString(OS, DE.Filename);
100 OS << " <key>line</key>\n"
101 << " <integer>" << DE.Line << "</integer>\n";
103 if (DE.Column != 0) {
104 OS << " <key>column</key>\n"
105 << " <integer>" << DE.Column << "</integer>\n";
107 if (!DE.Message.empty()) {
108 OS << " <key>message</key>\n"
110 emitString(OS, DE.Message);
118 this->OS << OS.str();
121 void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
122 const Diagnostic &Info) {
123 // Default implementation (Warnings/errors count).
124 DiagnosticConsumer::HandleDiagnostic(Level, Info);
126 // Initialize the main file name, if we haven't already fetched it.
127 if (MainFilename.empty() && Info.hasSourceManager()) {
128 const SourceManager &SM = Info.getSourceManager();
129 FileID FID = SM.getMainFileID();
130 if (!FID.isInvalid()) {
131 const FileEntry *FE = SM.getFileEntryForID(FID);
132 if (FE && FE->getName())
133 MainFilename = FE->getName();
137 // Create the diag entry.
139 DE.DiagnosticID = Info.getID();
140 DE.DiagnosticLevel = Level;
142 // Format the message.
143 llvm::SmallString<100> MessageStr;
144 Info.FormatDiagnostic(MessageStr);
145 DE.Message = MessageStr.str();
147 // Set the location information.
149 DE.Line = DE.Column = 0;
150 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
151 const SourceManager &SM = Info.getSourceManager();
152 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
154 if (PLoc.isInvalid()) {
155 // At least print the file name if available:
156 FileID FID = SM.getFileID(Info.getLocation());
157 if (!FID.isInvalid()) {
158 const FileEntry *FE = SM.getFileEntryForID(FID);
159 if (FE && FE->getName())
160 DE.Filename = FE->getName();
163 DE.Filename = PLoc.getFilename();
164 DE.Line = PLoc.getLine();
165 DE.Column = PLoc.getColumn();
169 // Record the diagnostic entry.
170 Entries.push_back(DE);
174 LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
175 return new LogDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);