blob: c2dcd1be313b719d8ea7102ca6909a941288afcd [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 }
66 OS << " </dict>\n";
Chad Rosier011824f2011-09-28 23:05:07 +000067}
68
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000069void LogDiagnosticPrinter::EndSourceFile() {
70 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
71 // entry if no diagnostics were present.
72 //
David Blaikiee2eefae2011-09-25 23:39:51 +000073 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
74 // will miss any diagnostics which are emitted after and outside the
75 // translation unit processing.
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000076 if (Entries.empty())
77 return;
Daniel Dunbar2083c322011-04-07 18:31:10 +000078
79 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000080 SmallString<512> Msg;
Daniel Dunbar2083c322011-04-07 18:31:10 +000081 llvm::raw_svector_ostream OS(Msg);
82
Daniel Dunbar719393a2011-04-07 18:44:15 +000083 OS << "<dict>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000084 if (!MainFilename.empty()) {
85 OS << " <key>main-file</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000086 << " ";
87 EmitString(OS, MainFilename) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +000088 }
89 if (!DwarfDebugFlags.empty()) {
90 OS << " <key>dwarf-debug-flags</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000091 << " ";
92 EmitString(OS, DwarfDebugFlags) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +000093 }
Daniel Dunbar719393a2011-04-07 18:44:15 +000094 OS << " <key>diagnostics</key>\n";
95 OS << " <array>\n";
Alp Toker525fdfc2014-07-06 04:26:52 +000096 for (auto &DE : Entries)
97 EmitDiagEntry(OS, DE);
Daniel Dunbar719393a2011-04-07 18:44:15 +000098 OS << " </array>\n";
99 OS << "</dict>\n";
Daniel Dunbar2083c322011-04-07 18:31:10 +0000100
101 this->OS << OS.str();
102}
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000103
David Blaikie9c902b52011-09-25 23:23:43 +0000104void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000105 const Diagnostic &Info) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000106 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000107 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000108
Daniel Dunbarc0325032011-04-07 18:51:54 +0000109 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000110 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbarc0325032011-04-07 18:51:54 +0000111 const SourceManager &SM = Info.getSourceManager();
112 FileID FID = SM.getMainFileID();
113 if (!FID.isInvalid()) {
114 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000115 if (FE && FE->isValid())
Daniel Dunbarc0325032011-04-07 18:51:54 +0000116 MainFilename = FE->getName();
117 }
118 }
119
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000120 // Create the diag entry.
121 DiagEntry DE;
122 DE.DiagnosticID = Info.getID();
123 DE.DiagnosticLevel = Level;
124
125 // Format the message.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000126 SmallString<100> MessageStr;
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000127 Info.FormatDiagnostic(MessageStr);
128 DE.Message = MessageStr.str();
129
130 // Set the location information.
131 DE.Filename = "";
132 DE.Line = DE.Column = 0;
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000133 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000134 const SourceManager &SM = Info.getSourceManager();
135 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
136
137 if (PLoc.isInvalid()) {
138 // At least print the file name if available:
139 FileID FID = SM.getFileID(Info.getLocation());
140 if (!FID.isInvalid()) {
141 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000142 if (FE && FE->isValid())
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000143 DE.Filename = FE->getName();
144 }
145 } else {
146 DE.Filename = PLoc.getFilename();
147 DE.Line = PLoc.getLine();
148 DE.Column = PLoc.getColumn();
149 }
150 }
151
152 // Record the diagnostic entry.
153 Entries.push_back(DE);
154}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000155