blob: 3fee9574998670b5f2b315005a6d1c69a1e76a9b [file] [log] [blame]
Daniel Dunbar9df23492011-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"
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000011#include "clang/Basic/FileManager.h"
12#include "clang/Basic/SourceManager.h"
Daniel Dunbar9df23492011-04-07 18:31:10 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/Support/raw_ostream.h"
David Blaikie561d3ab2012-01-17 02:30:50 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar9df23492011-04-07 18:31:10 +000016using namespace clang;
17
Chris Lattner5f9e2722011-07-23 10:55:15 +000018LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os,
Daniel Dunbar9df23492011-04-07 18:31:10 +000019 const DiagnosticOptions &diags,
20 bool _OwnsOutputStream)
21 : OS(os), LangOpts(0), DiagOpts(&diags),
22 OwnsOutputStream(_OwnsOutputStream) {
23}
24
25LogDiagnosticPrinter::~LogDiagnosticPrinter() {
26 if (OwnsOutputStream)
27 delete &OS;
28}
29
David Blaikied6471f72011-09-25 23:23:43 +000030static StringRef getLevelName(DiagnosticsEngine::Level Level) {
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000031 switch (Level) {
David Blaikied6471f72011-09-25 23:23:43 +000032 case DiagnosticsEngine::Ignored: return "ignored";
33 case DiagnosticsEngine::Note: return "note";
34 case DiagnosticsEngine::Warning: return "warning";
35 case DiagnosticsEngine::Error: return "error";
36 case DiagnosticsEngine::Fatal: return "fatal error";
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000037 }
David Blaikie561d3ab2012-01-17 02:30:50 +000038 llvm_unreachable("Invalid DiagnosticsEngine level!");
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000039}
40
Chad Rosiercc78c6c2011-09-28 23:05:07 +000041// Escape XML characters inside the raw string.
42static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) {
43 for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) {
44 char c = *I;
45 switch (c) {
46 default: OS << c; break;
47 case '&': OS << "&amp;"; break;
48 case '<': OS << "&lt;"; break;
49 case '>': OS << "&gt;"; break;
50 case '\'': OS << "&apos;"; break;
51 case '\"': OS << "&quot;"; break;
52 }
53 }
54}
55
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000056void LogDiagnosticPrinter::EndSourceFile() {
57 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
58 // entry if no diagnostics were present.
59 //
David Blaikie78ad0b92011-09-25 23:39:51 +000060 // 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.
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000063 if (Entries.empty())
64 return;
Daniel Dunbar9df23492011-04-07 18:31:10 +000065
66 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000067 SmallString<512> Msg;
Daniel Dunbar9df23492011-04-07 18:31:10 +000068 llvm::raw_svector_ostream OS(Msg);
69
Daniel Dunbar5dccf572011-04-07 18:44:15 +000070 OS << "<dict>\n";
Daniel Dunbar28f14932011-04-07 18:51:54 +000071 if (!MainFilename.empty()) {
72 OS << " <key>main-file</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000073 << " <string>";
74 emitString(OS, MainFilename);
75 OS << "</string>\n";
Daniel Dunbar28f14932011-04-07 18:51:54 +000076 }
77 if (!DwarfDebugFlags.empty()) {
78 OS << " <key>dwarf-debug-flags</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000079 << " <string>";
80 emitString(OS, DwarfDebugFlags);
81 OS << "</string>\n";
Daniel Dunbar28f14932011-04-07 18:51:54 +000082 }
Daniel Dunbar5dccf572011-04-07 18:44:15 +000083 OS << " <key>diagnostics</key>\n";
84 OS << " <array>\n";
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000085 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
86 DiagEntry &DE = Entries[i];
87
Daniel Dunbar5dccf572011-04-07 18:44:15 +000088 OS << " <dict>\n";
89 OS << " <key>level</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000090 << " <string>";
91 emitString(OS, getLevelName(DE.DiagnosticLevel));
92 OS << "</string>\n";
Daniel Dunbar5dccf572011-04-07 18:44:15 +000093 if (!DE.Filename.empty()) {
94 OS << " <key>filename</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000095 << " <string>";
96 emitString(OS, DE.Filename);
97 OS << "</string>\n";
Daniel Dunbar5dccf572011-04-07 18:44:15 +000098 }
99 if (DE.Line != 0) {
100 OS << " <key>line</key>\n"
101 << " <integer>" << DE.Line << "</integer>\n";
102 }
103 if (DE.Column != 0) {
104 OS << " <key>column</key>\n"
105 << " <integer>" << DE.Column << "</integer>\n";
106 }
107 if (!DE.Message.empty()) {
108 OS << " <key>message</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +0000109 << " <string>";
110 emitString(OS, DE.Message);
111 OS << "</string>\n";
Daniel Dunbar5dccf572011-04-07 18:44:15 +0000112 }
113 OS << " </dict>\n";
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000114 }
Daniel Dunbar5dccf572011-04-07 18:44:15 +0000115 OS << " </array>\n";
116 OS << "</dict>\n";
Daniel Dunbar9df23492011-04-07 18:31:10 +0000117
118 this->OS << OS.str();
119}
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000120
David Blaikied6471f72011-09-25 23:23:43 +0000121void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000122 const Diagnostic &Info) {
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000123 // Default implementation (Warnings/errors count).
David Blaikie78ad0b92011-09-25 23:39:51 +0000124 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000125
Daniel Dunbar28f14932011-04-07 18:51:54 +0000126 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbar7665ad82011-05-05 02:12:02 +0000127 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbar28f14932011-04-07 18:51:54 +0000128 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();
134 }
135 }
136
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000137 // Create the diag entry.
138 DiagEntry DE;
139 DE.DiagnosticID = Info.getID();
140 DE.DiagnosticLevel = Level;
141
142 // Format the message.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000143 SmallString<100> MessageStr;
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000144 Info.FormatDiagnostic(MessageStr);
145 DE.Message = MessageStr.str();
146
147 // Set the location information.
148 DE.Filename = "";
149 DE.Line = DE.Column = 0;
Daniel Dunbar7665ad82011-05-05 02:12:02 +0000150 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000151 const SourceManager &SM = Info.getSourceManager();
152 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
153
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();
161 }
162 } else {
163 DE.Filename = PLoc.getFilename();
164 DE.Line = PLoc.getLine();
165 DE.Column = PLoc.getColumn();
166 }
167 }
168
169 // Record the diagnostic entry.
170 Entries.push_back(DE);
171}
Douglas Gregoraee526e2011-09-29 00:38:00 +0000172
173DiagnosticConsumer *
174LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
175 return new LogDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
176}
177