blob: 0a22481cb6342a9c2af885798e6c06d2febd3ba6 [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"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000011#include "clang/Basic/DiagnosticOptions.h"
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Basic/SourceManager.h"
Daniel Dunbar9df23492011-04-07 18:31:10 +000014#include "llvm/ADT/SmallString.h"
David Blaikie561d3ab2012-01-17 02:30:50 +000015#include "llvm/Support/ErrorHandling.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar9df23492011-04-07 18:31:10 +000017using namespace clang;
18
Chris Lattner5f9e2722011-07-23 10:55:15 +000019LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os,
Douglas Gregor02c23eb2012-10-23 22:26:28 +000020 DiagnosticOptions *diags,
Daniel Dunbar9df23492011-04-07 18:31:10 +000021 bool _OwnsOutputStream)
Douglas Gregor02c23eb2012-10-23 22:26:28 +000022 : OS(os), LangOpts(0), DiagOpts(diags),
Daniel Dunbar9df23492011-04-07 18:31:10 +000023 OwnsOutputStream(_OwnsOutputStream) {
24}
25
26LogDiagnosticPrinter::~LogDiagnosticPrinter() {
27 if (OwnsOutputStream)
28 delete &OS;
29}
30
David Blaikied6471f72011-09-25 23:23:43 +000031static StringRef getLevelName(DiagnosticsEngine::Level Level) {
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000032 switch (Level) {
David Blaikied6471f72011-09-25 23:23:43 +000033 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";
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000038 }
David Blaikie561d3ab2012-01-17 02:30:50 +000039 llvm_unreachable("Invalid DiagnosticsEngine level!");
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000040}
41
Chad Rosiercc78c6c2011-09-28 23:05:07 +000042// Escape XML characters inside the raw string.
43static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) {
44 for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) {
45 char c = *I;
46 switch (c) {
47 default: OS << c; break;
48 case '&': OS << "&amp;"; break;
49 case '<': OS << "&lt;"; break;
50 case '>': OS << "&gt;"; break;
51 case '\'': OS << "&apos;"; break;
52 case '\"': OS << "&quot;"; break;
53 }
54 }
55}
56
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000057void LogDiagnosticPrinter::EndSourceFile() {
58 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
59 // entry if no diagnostics were present.
60 //
David Blaikie78ad0b92011-09-25 23:39:51 +000061 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
62 // will miss any diagnostics which are emitted after and outside the
63 // translation unit processing.
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000064 if (Entries.empty())
65 return;
Daniel Dunbar9df23492011-04-07 18:31:10 +000066
67 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000068 SmallString<512> Msg;
Daniel Dunbar9df23492011-04-07 18:31:10 +000069 llvm::raw_svector_ostream OS(Msg);
70
Daniel Dunbar5dccf572011-04-07 18:44:15 +000071 OS << "<dict>\n";
Daniel Dunbar28f14932011-04-07 18:51:54 +000072 if (!MainFilename.empty()) {
73 OS << " <key>main-file</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000074 << " <string>";
75 emitString(OS, MainFilename);
76 OS << "</string>\n";
Daniel Dunbar28f14932011-04-07 18:51:54 +000077 }
78 if (!DwarfDebugFlags.empty()) {
79 OS << " <key>dwarf-debug-flags</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000080 << " <string>";
81 emitString(OS, DwarfDebugFlags);
82 OS << "</string>\n";
Daniel Dunbar28f14932011-04-07 18:51:54 +000083 }
Daniel Dunbar5dccf572011-04-07 18:44:15 +000084 OS << " <key>diagnostics</key>\n";
85 OS << " <array>\n";
Daniel Dunbar64bfbf52011-04-07 18:37:34 +000086 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
87 DiagEntry &DE = Entries[i];
88
Daniel Dunbar5dccf572011-04-07 18:44:15 +000089 OS << " <dict>\n";
90 OS << " <key>level</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000091 << " <string>";
92 emitString(OS, getLevelName(DE.DiagnosticLevel));
93 OS << "</string>\n";
Daniel Dunbar5dccf572011-04-07 18:44:15 +000094 if (!DE.Filename.empty()) {
95 OS << " <key>filename</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +000096 << " <string>";
97 emitString(OS, DE.Filename);
98 OS << "</string>\n";
Daniel Dunbar5dccf572011-04-07 18:44:15 +000099 }
100 if (DE.Line != 0) {
101 OS << " <key>line</key>\n"
102 << " <integer>" << DE.Line << "</integer>\n";
103 }
104 if (DE.Column != 0) {
105 OS << " <key>column</key>\n"
106 << " <integer>" << DE.Column << "</integer>\n";
107 }
108 if (!DE.Message.empty()) {
109 OS << " <key>message</key>\n"
Chad Rosiercc78c6c2011-09-28 23:05:07 +0000110 << " <string>";
111 emitString(OS, DE.Message);
112 OS << "</string>\n";
Daniel Dunbar5dccf572011-04-07 18:44:15 +0000113 }
114 OS << " </dict>\n";
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000115 }
Daniel Dunbar5dccf572011-04-07 18:44:15 +0000116 OS << " </array>\n";
117 OS << "</dict>\n";
Daniel Dunbar9df23492011-04-07 18:31:10 +0000118
119 this->OS << OS.str();
120}
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000121
David Blaikied6471f72011-09-25 23:23:43 +0000122void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000123 const Diagnostic &Info) {
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000124 // Default implementation (Warnings/errors count).
David Blaikie78ad0b92011-09-25 23:39:51 +0000125 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000126
Daniel Dunbar28f14932011-04-07 18:51:54 +0000127 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbar7665ad82011-05-05 02:12:02 +0000128 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbar28f14932011-04-07 18:51:54 +0000129 const SourceManager &SM = Info.getSourceManager();
130 FileID FID = SM.getMainFileID();
131 if (!FID.isInvalid()) {
132 const FileEntry *FE = SM.getFileEntryForID(FID);
133 if (FE && FE->getName())
134 MainFilename = FE->getName();
135 }
136 }
137
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000138 // Create the diag entry.
139 DiagEntry DE;
140 DE.DiagnosticID = Info.getID();
141 DE.DiagnosticLevel = Level;
142
143 // Format the message.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000144 SmallString<100> MessageStr;
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000145 Info.FormatDiagnostic(MessageStr);
146 DE.Message = MessageStr.str();
147
148 // Set the location information.
149 DE.Filename = "";
150 DE.Line = DE.Column = 0;
Daniel Dunbar7665ad82011-05-05 02:12:02 +0000151 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar64bfbf52011-04-07 18:37:34 +0000152 const SourceManager &SM = Info.getSourceManager();
153 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
154
155 if (PLoc.isInvalid()) {
156 // At least print the file name if available:
157 FileID FID = SM.getFileID(Info.getLocation());
158 if (!FID.isInvalid()) {
159 const FileEntry *FE = SM.getFileEntryForID(FID);
160 if (FE && FE->getName())
161 DE.Filename = FE->getName();
162 }
163 } else {
164 DE.Filename = PLoc.getFilename();
165 DE.Line = PLoc.getLine();
166 DE.Column = PLoc.getColumn();
167 }
168 }
169
170 // Record the diagnostic entry.
171 Entries.push_back(DE);
172}
Douglas Gregoraee526e2011-09-29 00:38:00 +0000173
174DiagnosticConsumer *
175LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000176 return new LogDiagnosticPrinter(OS, &*DiagOpts, /*OwnsOutputStream=*/false);
Douglas Gregoraee526e2011-09-29 00:38:00 +0000177}
178