blob: 53b13fc6f978758d791d9a75a433dbfb65a3764b [file] [log] [blame]
Daniel Dunbar2083c322011-04-07 18:31:10 +00001//===--- 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
10#include "clang/Frontend/LogDiagnosticPrinter.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000011#include "clang/Basic/DiagnosticOptions.h"
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Basic/SourceManager.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000014#include "llvm/ADT/SmallString.h"
David Blaikief47fa302012-01-17 02:30:50 +000015#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000017using namespace clang;
18
Chris Lattner0e62c1c2011-07-23 10:55:15 +000019LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os,
Douglas Gregor811db4e2012-10-23 22:26:28 +000020 DiagnosticOptions *diags,
Daniel Dunbar2083c322011-04-07 18:31:10 +000021 bool _OwnsOutputStream)
Douglas Gregor811db4e2012-10-23 22:26:28 +000022 : OS(os), LangOpts(0), DiagOpts(diags),
Daniel Dunbar2083c322011-04-07 18:31:10 +000023 OwnsOutputStream(_OwnsOutputStream) {
24}
25
26LogDiagnosticPrinter::~LogDiagnosticPrinter() {
27 if (OwnsOutputStream)
28 delete &OS;
29}
30
David Blaikie9c902b52011-09-25 23:23:43 +000031static StringRef getLevelName(DiagnosticsEngine::Level Level) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000032 switch (Level) {
David Blaikie9c902b52011-09-25 23:23:43 +000033 case DiagnosticsEngine::Ignored: return "ignored";
Tobias Grosser74160242014-02-28 09:11:08 +000034 case DiagnosticsEngine::Remark: return "remark";
David Blaikie9c902b52011-09-25 23:23:43 +000035 case DiagnosticsEngine::Note: return "note";
36 case DiagnosticsEngine::Warning: return "warning";
37 case DiagnosticsEngine::Error: return "error";
38 case DiagnosticsEngine::Fatal: return "fatal error";
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000039 }
David Blaikief47fa302012-01-17 02:30:50 +000040 llvm_unreachable("Invalid DiagnosticsEngine level!");
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000041}
42
Chad Rosier011824f2011-09-28 23:05:07 +000043// Escape XML characters inside the raw string.
44static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) {
45 for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) {
46 char c = *I;
47 switch (c) {
48 default: OS << c; break;
49 case '&': OS << "&amp;"; break;
50 case '<': OS << "&lt;"; break;
51 case '>': OS << "&gt;"; break;
52 case '\'': OS << "&apos;"; break;
53 case '\"': OS << "&quot;"; break;
54 }
55 }
56}
57
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000058void LogDiagnosticPrinter::EndSourceFile() {
59 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
60 // entry if no diagnostics were present.
61 //
David Blaikiee2eefae2011-09-25 23:39:51 +000062 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
63 // will miss any diagnostics which are emitted after and outside the
64 // translation unit processing.
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000065 if (Entries.empty())
66 return;
Daniel Dunbar2083c322011-04-07 18:31:10 +000067
68 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000069 SmallString<512> Msg;
Daniel Dunbar2083c322011-04-07 18:31:10 +000070 llvm::raw_svector_ostream OS(Msg);
71
Daniel Dunbar719393a2011-04-07 18:44:15 +000072 OS << "<dict>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000073 if (!MainFilename.empty()) {
74 OS << " <key>main-file</key>\n"
Chad Rosier011824f2011-09-28 23:05:07 +000075 << " <string>";
76 emitString(OS, MainFilename);
77 OS << "</string>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000078 }
79 if (!DwarfDebugFlags.empty()) {
80 OS << " <key>dwarf-debug-flags</key>\n"
Chad Rosier011824f2011-09-28 23:05:07 +000081 << " <string>";
82 emitString(OS, DwarfDebugFlags);
83 OS << "</string>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000084 }
Daniel Dunbar719393a2011-04-07 18:44:15 +000085 OS << " <key>diagnostics</key>\n";
86 OS << " <array>\n";
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000087 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
88 DiagEntry &DE = Entries[i];
89
Daniel Dunbar719393a2011-04-07 18:44:15 +000090 OS << " <dict>\n";
91 OS << " <key>level</key>\n"
Chad Rosier011824f2011-09-28 23:05:07 +000092 << " <string>";
93 emitString(OS, getLevelName(DE.DiagnosticLevel));
94 OS << "</string>\n";
Daniel Dunbar719393a2011-04-07 18:44:15 +000095 if (!DE.Filename.empty()) {
96 OS << " <key>filename</key>\n"
Chad Rosier011824f2011-09-28 23:05:07 +000097 << " <string>";
98 emitString(OS, DE.Filename);
99 OS << "</string>\n";
Daniel Dunbar719393a2011-04-07 18:44:15 +0000100 }
101 if (DE.Line != 0) {
102 OS << " <key>line</key>\n"
103 << " <integer>" << DE.Line << "</integer>\n";
104 }
105 if (DE.Column != 0) {
106 OS << " <key>column</key>\n"
107 << " <integer>" << DE.Column << "</integer>\n";
108 }
109 if (!DE.Message.empty()) {
110 OS << " <key>message</key>\n"
Chad Rosier011824f2011-09-28 23:05:07 +0000111 << " <string>";
112 emitString(OS, DE.Message);
113 OS << "</string>\n";
Daniel Dunbar719393a2011-04-07 18:44:15 +0000114 }
115 OS << " </dict>\n";
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000116 }
Daniel Dunbar719393a2011-04-07 18:44:15 +0000117 OS << " </array>\n";
118 OS << "</dict>\n";
Daniel Dunbar2083c322011-04-07 18:31:10 +0000119
120 this->OS << OS.str();
121}
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000122
David Blaikie9c902b52011-09-25 23:23:43 +0000123void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000124 const Diagnostic &Info) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000125 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000126 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000127
Daniel Dunbarc0325032011-04-07 18:51:54 +0000128 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000129 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbarc0325032011-04-07 18:51:54 +0000130 const SourceManager &SM = Info.getSourceManager();
131 FileID FID = SM.getMainFileID();
132 if (!FID.isInvalid()) {
133 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000134 if (FE && FE->isValid())
Daniel Dunbarc0325032011-04-07 18:51:54 +0000135 MainFilename = FE->getName();
136 }
137 }
138
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000139 // Create the diag entry.
140 DiagEntry DE;
141 DE.DiagnosticID = Info.getID();
142 DE.DiagnosticLevel = Level;
143
144 // Format the message.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000145 SmallString<100> MessageStr;
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000146 Info.FormatDiagnostic(MessageStr);
147 DE.Message = MessageStr.str();
148
149 // Set the location information.
150 DE.Filename = "";
151 DE.Line = DE.Column = 0;
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000152 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000153 const SourceManager &SM = Info.getSourceManager();
154 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
155
156 if (PLoc.isInvalid()) {
157 // At least print the file name if available:
158 FileID FID = SM.getFileID(Info.getLocation());
159 if (!FID.isInvalid()) {
160 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000161 if (FE && FE->isValid())
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000162 DE.Filename = FE->getName();
163 }
164 } else {
165 DE.Filename = PLoc.getFilename();
166 DE.Line = PLoc.getLine();
167 DE.Column = PLoc.getColumn();
168 }
169 }
170
171 // Record the diagnostic entry.
172 Entries.push_back(DE);
173}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000174