blob: 4bac17553999d49b6c7520a2459a21c254267acd [file] [log] [blame]
Daniel Dunbar2083c322011-04-07 18:31:10 +00001//===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar2083c322011-04-07 18:31:10 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Frontend/LogDiagnosticPrinter.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000010#include "clang/Basic/DiagnosticOptions.h"
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000011#include "clang/Basic/FileManager.h"
Alp Toker525fdfc2014-07-06 04:26:52 +000012#include "clang/Basic/PlistSupport.h"
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000013#include "clang/Basic/SourceManager.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000014#include "llvm/ADT/SmallString.h"
David Blaikief47fa302012-01-17 02:30:50 +000015#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000017using namespace clang;
Alp Toker525fdfc2014-07-06 04:26:52 +000018using namespace markup;
Daniel Dunbar2083c322011-04-07 18:31:10 +000019
David Blaikie11f8a942014-09-15 17:30:56 +000020LogDiagnosticPrinter::LogDiagnosticPrinter(
21 raw_ostream &os, DiagnosticOptions *diags,
22 std::unique_ptr<raw_ostream> StreamOwner)
23 : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
24 DiagOpts(diags) {}
Daniel Dunbar2083c322011-04-07 18:31:10 +000025
David Blaikie9c902b52011-09-25 23:23:43 +000026static StringRef getLevelName(DiagnosticsEngine::Level Level) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000027 switch (Level) {
David Blaikie9c902b52011-09-25 23:23:43 +000028 case DiagnosticsEngine::Ignored: return "ignored";
Tobias Grosser74160242014-02-28 09:11:08 +000029 case DiagnosticsEngine::Remark: return "remark";
David Blaikie9c902b52011-09-25 23:23:43 +000030 case DiagnosticsEngine::Note: return "note";
31 case DiagnosticsEngine::Warning: return "warning";
32 case DiagnosticsEngine::Error: return "error";
33 case DiagnosticsEngine::Fatal: return "fatal error";
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000034 }
David Blaikief47fa302012-01-17 02:30:50 +000035 llvm_unreachable("Invalid DiagnosticsEngine level!");
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000036}
37
Alp Toker525fdfc2014-07-06 04:26:52 +000038void
39LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
40 const LogDiagnosticPrinter::DiagEntry &DE) {
41 OS << " <dict>\n";
42 OS << " <key>level</key>\n"
43 << " ";
44 EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
45 if (!DE.Filename.empty()) {
46 OS << " <key>filename</key>\n"
47 << " ";
48 EmitString(OS, DE.Filename) << '\n';
Chad Rosier011824f2011-09-28 23:05:07 +000049 }
Alp Toker525fdfc2014-07-06 04:26:52 +000050 if (DE.Line != 0) {
51 OS << " <key>line</key>\n"
52 << " ";
53 EmitInteger(OS, DE.Line) << '\n';
54 }
55 if (DE.Column != 0) {
56 OS << " <key>column</key>\n"
57 << " ";
58 EmitInteger(OS, DE.Column) << '\n';
59 }
60 if (!DE.Message.empty()) {
61 OS << " <key>message</key>\n"
62 << " ";
63 EmitString(OS, DE.Message) << '\n';
64 }
Steven Wu2e2b0b32014-11-14 21:23:56 +000065 OS << " <key>ID</key>\n"
66 << " ";
67 EmitInteger(OS, DE.DiagnosticID) << '\n';
68 if (!DE.WarningOption.empty()) {
69 OS << " <key>WarningOption</key>\n"
70 << " ";
71 EmitString(OS, DE.WarningOption) << '\n';
72 }
Alp Toker525fdfc2014-07-06 04:26:52 +000073 OS << " </dict>\n";
Chad Rosier011824f2011-09-28 23:05:07 +000074}
75
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000076void LogDiagnosticPrinter::EndSourceFile() {
77 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
78 // entry if no diagnostics were present.
79 //
David Blaikiee2eefae2011-09-25 23:39:51 +000080 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
81 // will miss any diagnostics which are emitted after and outside the
82 // translation unit processing.
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +000083 if (Entries.empty())
84 return;
Daniel Dunbar2083c322011-04-07 18:31:10 +000085
86 // Write to a temporary string to ensure atomic write of diagnostic object.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000087 SmallString<512> Msg;
Daniel Dunbar2083c322011-04-07 18:31:10 +000088 llvm::raw_svector_ostream OS(Msg);
89
Daniel Dunbar719393a2011-04-07 18:44:15 +000090 OS << "<dict>\n";
Daniel Dunbarc0325032011-04-07 18:51:54 +000091 if (!MainFilename.empty()) {
92 OS << " <key>main-file</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000093 << " ";
94 EmitString(OS, MainFilename) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +000095 }
96 if (!DwarfDebugFlags.empty()) {
97 OS << " <key>dwarf-debug-flags</key>\n"
Alp Toker525fdfc2014-07-06 04:26:52 +000098 << " ";
99 EmitString(OS, DwarfDebugFlags) << '\n';
Daniel Dunbarc0325032011-04-07 18:51:54 +0000100 }
Daniel Dunbar719393a2011-04-07 18:44:15 +0000101 OS << " <key>diagnostics</key>\n";
102 OS << " <array>\n";
Alp Toker525fdfc2014-07-06 04:26:52 +0000103 for (auto &DE : Entries)
104 EmitDiagEntry(OS, DE);
Daniel Dunbar719393a2011-04-07 18:44:15 +0000105 OS << " </array>\n";
106 OS << "</dict>\n";
Daniel Dunbar2083c322011-04-07 18:31:10 +0000107
108 this->OS << OS.str();
109}
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000110
David Blaikie9c902b52011-09-25 23:23:43 +0000111void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000112 const Diagnostic &Info) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000113 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000114 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000115
Daniel Dunbarc0325032011-04-07 18:51:54 +0000116 // Initialize the main file name, if we haven't already fetched it.
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000117 if (MainFilename.empty() && Info.hasSourceManager()) {
Daniel Dunbarc0325032011-04-07 18:51:54 +0000118 const SourceManager &SM = Info.getSourceManager();
119 FileID FID = SM.getMainFileID();
Yaron Keren8b563662015-10-03 10:46:20 +0000120 if (FID.isValid()) {
Daniel Dunbarc0325032011-04-07 18:51:54 +0000121 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000122 if (FE && FE->isValid())
Daniel Dunbarc0325032011-04-07 18:51:54 +0000123 MainFilename = FE->getName();
124 }
125 }
126
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000127 // Create the diag entry.
128 DiagEntry DE;
129 DE.DiagnosticID = Info.getID();
130 DE.DiagnosticLevel = Level;
131
Steven Wu2e2b0b32014-11-14 21:23:56 +0000132 DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
133
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000134 // Format the message.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000135 SmallString<100> MessageStr;
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000136 Info.FormatDiagnostic(MessageStr);
137 DE.Message = MessageStr.str();
138
139 // Set the location information.
140 DE.Filename = "";
141 DE.Line = DE.Column = 0;
Daniel Dunbarce1035c2011-05-05 02:12:02 +0000142 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000143 const SourceManager &SM = Info.getSourceManager();
144 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
145
146 if (PLoc.isInvalid()) {
147 // At least print the file name if available:
148 FileID FID = SM.getFileID(Info.getLocation());
Yaron Keren8b563662015-10-03 10:46:20 +0000149 if (FID.isValid()) {
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000150 const FileEntry *FE = SM.getFileEntryForID(FID);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000151 if (FE && FE->isValid())
Daniel Dunbar4f3a28b2011-04-07 18:37:34 +0000152 DE.Filename = FE->getName();
153 }
154 } else {
155 DE.Filename = PLoc.getFilename();
156 DE.Line = PLoc.getLine();
157 DE.Column = PLoc.getColumn();
158 }
159 }
160
161 // Record the diagnostic entry.
162 Entries.push_back(DE);
163}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000164