blob: 19539e0a5eb2c0db418df7a40cddd27e42a5d38a [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"
Alp Toker525fdfc2014-07-06 04:26:52 +000013#include "clang/Basic/PlistSupport.h"
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000014#include "clang/Basic/SourceManager.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000015#include "llvm/ADT/SmallString.h"
David Blaikief47fa302012-01-17 02:30:50 +000016#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "llvm/Support/raw_ostream.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000018using namespace clang;
Alp Toker525fdfc2014-07-06 04:26:52 +000019using namespace markup;
Daniel Dunbar2083c322011-04-07 18:31:10 +000020
Chris Lattner0e62c1c2011-07-23 10:55:15 +000021LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os,
Douglas Gregor811db4e2012-10-23 22:26:28 +000022 DiagnosticOptions *diags,
Daniel Dunbar2083c322011-04-07 18:31:10 +000023 bool _OwnsOutputStream)
Craig Topper49a27902014-05-22 04:46:25 +000024 : OS(os), LangOpts(nullptr), DiagOpts(diags),
Daniel Dunbar2083c322011-04-07 18:31:10 +000025 OwnsOutputStream(_OwnsOutputStream) {
26}
27
28LogDiagnosticPrinter::~LogDiagnosticPrinter() {
29 if (OwnsOutputStream)
30 delete &OS;
31}
32
David Blaikie9c902b52011-09-25 23:23:43 +000033static StringRef getLevelName(DiagnosticsEngine::Level Level) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000034 switch (Level) {
David Blaikie9c902b52011-09-25 23:23:43 +000035 case DiagnosticsEngine::Ignored: return "ignored";
Tobias Grosser74160242014-02-28 09:11:08 +000036 case DiagnosticsEngine::Remark: return "remark";
David Blaikie9c902b52011-09-25 23:23:43 +000037 case DiagnosticsEngine::Note: return "note";
38 case DiagnosticsEngine::Warning: return "warning";
39 case DiagnosticsEngine::Error: return "error";
40 case DiagnosticsEngine::Fatal: return "fatal error";
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000041 }
David Blaikief47fa302012-01-17 02:30:50 +000042 llvm_unreachable("Invalid DiagnosticsEngine level!");
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000043}
44
Alp Toker525fdfc2014-07-06 04:26:52 +000045void
46LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
47 const LogDiagnosticPrinter::DiagEntry &DE) {
48 OS << " <dict>\n";
49 OS << " <key>level</key>\n"
50 << " ";
51 EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
52 if (!DE.Filename.empty()) {
53 OS << " <key>filename</key>\n"
54 << " ";
55 EmitString(OS, DE.Filename) << '\n';
Chad Rosier011824f2011-09-28 23:05:07 +000056 }
Alp Toker525fdfc2014-07-06 04:26:52 +000057 if (DE.Line != 0) {
58 OS << " <key>line</key>\n"
59 << " ";
60 EmitInteger(OS, DE.Line) << '\n';
61 }
62 if (DE.Column != 0) {
63 OS << " <key>column</key>\n"
64 << " ";
65 EmitInteger(OS, DE.Column) << '\n';
66 }
67 if (!DE.Message.empty()) {
68 OS << " <key>message</key>\n"
69 << " ";
70 EmitString(OS, DE.Message) << '\n';
71 }
72 OS << " </dict>\n";
Chad Rosier011824f2011-09-28 23:05:07 +000073}
74
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000075void LogDiagnosticPrinter::EndSourceFile() {
76 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
77 // entry if no diagnostics were present.
78 //
David Blaikiee2eefae2011-09-25 23:39:51 +000079 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
80 // will miss any diagnostics which are emitted after and outside the
81 // translation unit processing.
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000082 if (Entries.empty())
83 return;
Daniel Dunbar2083c322011-04-07 18:31:10 +000084
85 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000086 SmallString<512> Msg;
Daniel Dunbar2083c322011-04-07 18:31:10 +000087 llvm::raw_svector_ostream OS(Msg);
88
Daniel Dunbar719393a2011-04-07 18:44:15 +000089 OS << "<dict>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000090 if (!MainFilename.empty()) {
91 OS << " <key>main-file</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000092 << " ";
93 EmitString(OS, MainFilename) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +000094 }
95 if (!DwarfDebugFlags.empty()) {
96 OS << " <key>dwarf-debug-flags</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000097 << " ";
98 EmitString(OS, DwarfDebugFlags) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +000099 }
Daniel Dunbar719393a2011-04-07 18:44:15 +0000100 OS << " <key>diagnostics</key>\n";
101 OS << " <array>\n";
Alp Toker525fdfc2014-07-06 04:26:52 +0000102 for (auto &DE : Entries)
103 EmitDiagEntry(OS, DE);
Daniel Dunbar719393a2011-04-07 18:44:15 +0000104 OS << " </array>\n";
105 OS << "</dict>\n";
Daniel Dunbar2083c322011-04-07 18:31:10 +0000106
107 this->OS << OS.str();
108}
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000109
David Blaikie9c902b52011-09-25 23:23:43 +0000110void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000111 const Diagnostic &Info) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000112 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000113 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000114
Daniel Dunbarc0325032011-04-07 18:51:54 +0000115 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000116 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbarc0325032011-04-07 18:51:54 +0000117 const SourceManager &SM = Info.getSourceManager();
118 FileID FID = SM.getMainFileID();
119 if (!FID.isInvalid()) {
120 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000121 if (FE && FE->isValid())
Daniel Dunbarc0325032011-04-07 18:51:54 +0000122 MainFilename = FE->getName();
123 }
124 }
125
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000126 // Create the diag entry.
127 DiagEntry DE;
128 DE.DiagnosticID = Info.getID();
129 DE.DiagnosticLevel = Level;
130
131 // Format the message.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000132 SmallString<100> MessageStr;
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000133 Info.FormatDiagnostic(MessageStr);
134 DE.Message = MessageStr.str();
135
136 // Set the location information.
137 DE.Filename = "";
138 DE.Line = DE.Column = 0;
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000139 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000140 const SourceManager &SM = Info.getSourceManager();
141 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
142
143 if (PLoc.isInvalid()) {
144 // At least print the file name if available:
145 FileID FID = SM.getFileID(Info.getLocation());
146 if (!FID.isInvalid()) {
147 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000148 if (FE && FE->isValid())
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000149 DE.Filename = FE->getName();
150 }
151 } else {
152 DE.Filename = PLoc.getFilename();
153 DE.Line = PLoc.getLine();
154 DE.Column = PLoc.getColumn();
155 }
156 }
157
158 // Record the diagnostic entry.
159 Entries.push_back(DE);
160}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000161