blob: c6a18e0d80d25c3f7262f032b0464ff41e14a15e [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
David Blaikie11f8a942014-09-15 17:30:56 +000021LogDiagnosticPrinter::LogDiagnosticPrinter(
22 raw_ostream &os, DiagnosticOptions *diags,
23 std::unique_ptr<raw_ostream> StreamOwner)
24 : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
25 DiagOpts(diags) {}
Daniel Dunbar2083c322011-04-07 18:31:10 +000026
David Blaikie9c902b52011-09-25 23:23:43 +000027static StringRef getLevelName(DiagnosticsEngine::Level Level) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000028 switch (Level) {
David Blaikie9c902b52011-09-25 23:23:43 +000029 case DiagnosticsEngine::Ignored: return "ignored";
Tobias Grosser74160242014-02-28 09:11:08 +000030 case DiagnosticsEngine::Remark: return "remark";
David Blaikie9c902b52011-09-25 23:23:43 +000031 case DiagnosticsEngine::Note: return "note";
32 case DiagnosticsEngine::Warning: return "warning";
33 case DiagnosticsEngine::Error: return "error";
34 case DiagnosticsEngine::Fatal: return "fatal error";
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000035 }
David Blaikief47fa302012-01-17 02:30:50 +000036 llvm_unreachable("Invalid DiagnosticsEngine level!");
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000037}
38
Alp Toker525fdfc2014-07-06 04:26:52 +000039void
40LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
41 const LogDiagnosticPrinter::DiagEntry &DE) {
42 OS << " <dict>\n";
43 OS << " <key>level</key>\n"
44 << " ";
45 EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
46 if (!DE.Filename.empty()) {
47 OS << " <key>filename</key>\n"
48 << " ";
49 EmitString(OS, DE.Filename) << '\n';
Chad Rosier011824f2011-09-28 23:05:07 +000050 }
Alp Toker525fdfc2014-07-06 04:26:52 +000051 if (DE.Line != 0) {
52 OS << " <key>line</key>\n"
53 << " ";
54 EmitInteger(OS, DE.Line) << '\n';
55 }
56 if (DE.Column != 0) {
57 OS << " <key>column</key>\n"
58 << " ";
59 EmitInteger(OS, DE.Column) << '\n';
60 }
61 if (!DE.Message.empty()) {
62 OS << " <key>message</key>\n"
63 << " ";
64 EmitString(OS, DE.Message) << '\n';
65 }
Steven Wu2e2b0b32014-11-14 21:23:56 +000066 OS << " <key>ID</key>\n"
67 << " ";
68 EmitInteger(OS, DE.DiagnosticID) << '\n';
69 if (!DE.WarningOption.empty()) {
70 OS << " <key>WarningOption</key>\n"
71 << " ";
72 EmitString(OS, DE.WarningOption) << '\n';
73 }
Alp Toker525fdfc2014-07-06 04:26:52 +000074 OS << " </dict>\n";
Chad Rosier011824f2011-09-28 23:05:07 +000075}
76
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000077void LogDiagnosticPrinter::EndSourceFile() {
78 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
79 // entry if no diagnostics were present.
80 //
David Blaikiee2eefae2011-09-25 23:39:51 +000081 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
82 // will miss any diagnostics which are emitted after and outside the
83 // translation unit processing.
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000084 if (Entries.empty())
85 return;
Daniel Dunbar2083c322011-04-07 18:31:10 +000086
87 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000088 SmallString<512> Msg;
Daniel Dunbar2083c322011-04-07 18:31:10 +000089 llvm::raw_svector_ostream OS(Msg);
90
Daniel Dunbar719393a2011-04-07 18:44:15 +000091 OS << "<dict>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000092 if (!MainFilename.empty()) {
93 OS << " <key>main-file</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000094 << " ";
95 EmitString(OS, MainFilename) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +000096 }
97 if (!DwarfDebugFlags.empty()) {
98 OS << " <key>dwarf-debug-flags</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000099 << " ";
100 EmitString(OS, DwarfDebugFlags) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +0000101 }
Daniel Dunbar719393a2011-04-07 18:44:15 +0000102 OS << " <key>diagnostics</key>\n";
103 OS << " <array>\n";
Alp Toker525fdfc2014-07-06 04:26:52 +0000104 for (auto &DE : Entries)
105 EmitDiagEntry(OS, DE);
Daniel Dunbar719393a2011-04-07 18:44:15 +0000106 OS << " </array>\n";
107 OS << "</dict>\n";
Daniel Dunbar2083c322011-04-07 18:31:10 +0000108
109 this->OS << OS.str();
110}
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000111
David Blaikie9c902b52011-09-25 23:23:43 +0000112void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000113 const Diagnostic &Info) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000114 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000115 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000116
Daniel Dunbarc0325032011-04-07 18:51:54 +0000117 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000118 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbarc0325032011-04-07 18:51:54 +0000119 const SourceManager &SM = Info.getSourceManager();
120 FileID FID = SM.getMainFileID();
121 if (!FID.isInvalid()) {
122 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000123 if (FE && FE->isValid())
Daniel Dunbarc0325032011-04-07 18:51:54 +0000124 MainFilename = FE->getName();
125 }
126 }
127
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000128 // Create the diag entry.
129 DiagEntry DE;
130 DE.DiagnosticID = Info.getID();
131 DE.DiagnosticLevel = Level;
132
Steven Wu2e2b0b32014-11-14 21:23:56 +0000133 DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
134
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000135 // Format the message.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000136 SmallString<100> MessageStr;
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000137 Info.FormatDiagnostic(MessageStr);
138 DE.Message = MessageStr.str();
139
140 // Set the location information.
141 DE.Filename = "";
142 DE.Line = DE.Column = 0;
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000143 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000144 const SourceManager &SM = Info.getSourceManager();
145 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
146
147 if (PLoc.isInvalid()) {
148 // At least print the file name if available:
149 FileID FID = SM.getFileID(Info.getLocation());
150 if (!FID.isInvalid()) {
151 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000152 if (FE && FE->isValid())
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000153 DE.Filename = FE->getName();
154 }
155 } else {
156 DE.Filename = PLoc.getFilename();
157 DE.Line = PLoc.getLine();
158 DE.Column = PLoc.getColumn();
159 }
160 }
161
162 // Record the diagnostic entry.
163 Entries.push_back(DE);
164}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000165