1f4a2713aSLionel Sambuc //===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "clang/Frontend/LogDiagnosticPrinter.h"
11f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
12f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
13*0a6a1f1dSLionel Sambuc #include "clang/Basic/PlistSupport.h"
14f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
16f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
18f4a2713aSLionel Sambuc using namespace clang;
19*0a6a1f1dSLionel Sambuc using namespace markup;
20f4a2713aSLionel Sambuc
LogDiagnosticPrinter(raw_ostream & os,DiagnosticOptions * diags,std::unique_ptr<raw_ostream> StreamOwner)21*0a6a1f1dSLionel Sambuc LogDiagnosticPrinter::LogDiagnosticPrinter(
22*0a6a1f1dSLionel Sambuc raw_ostream &os, DiagnosticOptions *diags,
23*0a6a1f1dSLionel Sambuc std::unique_ptr<raw_ostream> StreamOwner)
24*0a6a1f1dSLionel Sambuc : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
25*0a6a1f1dSLionel Sambuc DiagOpts(diags) {}
26f4a2713aSLionel Sambuc
getLevelName(DiagnosticsEngine::Level Level)27f4a2713aSLionel Sambuc static StringRef getLevelName(DiagnosticsEngine::Level Level) {
28f4a2713aSLionel Sambuc switch (Level) {
29f4a2713aSLionel Sambuc case DiagnosticsEngine::Ignored: return "ignored";
30*0a6a1f1dSLionel Sambuc case DiagnosticsEngine::Remark: return "remark";
31f4a2713aSLionel Sambuc case DiagnosticsEngine::Note: return "note";
32f4a2713aSLionel Sambuc case DiagnosticsEngine::Warning: return "warning";
33f4a2713aSLionel Sambuc case DiagnosticsEngine::Error: return "error";
34f4a2713aSLionel Sambuc case DiagnosticsEngine::Fatal: return "fatal error";
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc llvm_unreachable("Invalid DiagnosticsEngine level!");
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc
39*0a6a1f1dSLionel Sambuc void
EmitDiagEntry(llvm::raw_ostream & OS,const LogDiagnosticPrinter::DiagEntry & DE)40*0a6a1f1dSLionel Sambuc LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
41*0a6a1f1dSLionel Sambuc const LogDiagnosticPrinter::DiagEntry &DE) {
42*0a6a1f1dSLionel Sambuc OS << " <dict>\n";
43*0a6a1f1dSLionel Sambuc OS << " <key>level</key>\n"
44*0a6a1f1dSLionel Sambuc << " ";
45*0a6a1f1dSLionel Sambuc EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
46*0a6a1f1dSLionel Sambuc if (!DE.Filename.empty()) {
47*0a6a1f1dSLionel Sambuc OS << " <key>filename</key>\n"
48*0a6a1f1dSLionel Sambuc << " ";
49*0a6a1f1dSLionel Sambuc EmitString(OS, DE.Filename) << '\n';
50f4a2713aSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc if (DE.Line != 0) {
52*0a6a1f1dSLionel Sambuc OS << " <key>line</key>\n"
53*0a6a1f1dSLionel Sambuc << " ";
54*0a6a1f1dSLionel Sambuc EmitInteger(OS, DE.Line) << '\n';
55f4a2713aSLionel Sambuc }
56*0a6a1f1dSLionel Sambuc if (DE.Column != 0) {
57*0a6a1f1dSLionel Sambuc OS << " <key>column</key>\n"
58*0a6a1f1dSLionel Sambuc << " ";
59*0a6a1f1dSLionel Sambuc EmitInteger(OS, DE.Column) << '\n';
60*0a6a1f1dSLionel Sambuc }
61*0a6a1f1dSLionel Sambuc if (!DE.Message.empty()) {
62*0a6a1f1dSLionel Sambuc OS << " <key>message</key>\n"
63*0a6a1f1dSLionel Sambuc << " ";
64*0a6a1f1dSLionel Sambuc EmitString(OS, DE.Message) << '\n';
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc OS << " <key>ID</key>\n"
67*0a6a1f1dSLionel Sambuc << " ";
68*0a6a1f1dSLionel Sambuc EmitInteger(OS, DE.DiagnosticID) << '\n';
69*0a6a1f1dSLionel Sambuc if (!DE.WarningOption.empty()) {
70*0a6a1f1dSLionel Sambuc OS << " <key>WarningOption</key>\n"
71*0a6a1f1dSLionel Sambuc << " ";
72*0a6a1f1dSLionel Sambuc EmitString(OS, DE.WarningOption) << '\n';
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc OS << " </dict>\n";
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc
EndSourceFile()77f4a2713aSLionel Sambuc void LogDiagnosticPrinter::EndSourceFile() {
78f4a2713aSLionel Sambuc // We emit all the diagnostics in EndSourceFile. However, we don't emit any
79f4a2713aSLionel Sambuc // entry if no diagnostics were present.
80f4a2713aSLionel Sambuc //
81f4a2713aSLionel Sambuc // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
82f4a2713aSLionel Sambuc // will miss any diagnostics which are emitted after and outside the
83f4a2713aSLionel Sambuc // translation unit processing.
84f4a2713aSLionel Sambuc if (Entries.empty())
85f4a2713aSLionel Sambuc return;
86f4a2713aSLionel Sambuc
87f4a2713aSLionel Sambuc // Write to a temporary string to ensure atomic write of diagnostic object.
88f4a2713aSLionel Sambuc SmallString<512> Msg;
89f4a2713aSLionel Sambuc llvm::raw_svector_ostream OS(Msg);
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc OS << "<dict>\n";
92f4a2713aSLionel Sambuc if (!MainFilename.empty()) {
93f4a2713aSLionel Sambuc OS << " <key>main-file</key>\n"
94*0a6a1f1dSLionel Sambuc << " ";
95*0a6a1f1dSLionel Sambuc EmitString(OS, MainFilename) << '\n';
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc if (!DwarfDebugFlags.empty()) {
98f4a2713aSLionel Sambuc OS << " <key>dwarf-debug-flags</key>\n"
99*0a6a1f1dSLionel Sambuc << " ";
100*0a6a1f1dSLionel Sambuc EmitString(OS, DwarfDebugFlags) << '\n';
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc OS << " <key>diagnostics</key>\n";
103f4a2713aSLionel Sambuc OS << " <array>\n";
104*0a6a1f1dSLionel Sambuc for (auto &DE : Entries)
105*0a6a1f1dSLionel Sambuc EmitDiagEntry(OS, DE);
106f4a2713aSLionel Sambuc OS << " </array>\n";
107f4a2713aSLionel Sambuc OS << "</dict>\n";
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc this->OS << OS.str();
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc
HandleDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)112f4a2713aSLionel Sambuc void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
113f4a2713aSLionel Sambuc const Diagnostic &Info) {
114f4a2713aSLionel Sambuc // Default implementation (Warnings/errors count).
115f4a2713aSLionel Sambuc DiagnosticConsumer::HandleDiagnostic(Level, Info);
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc // Initialize the main file name, if we haven't already fetched it.
118f4a2713aSLionel Sambuc if (MainFilename.empty() && Info.hasSourceManager()) {
119f4a2713aSLionel Sambuc const SourceManager &SM = Info.getSourceManager();
120f4a2713aSLionel Sambuc FileID FID = SM.getMainFileID();
121f4a2713aSLionel Sambuc if (!FID.isInvalid()) {
122f4a2713aSLionel Sambuc const FileEntry *FE = SM.getFileEntryForID(FID);
123*0a6a1f1dSLionel Sambuc if (FE && FE->isValid())
124f4a2713aSLionel Sambuc MainFilename = FE->getName();
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc // Create the diag entry.
129f4a2713aSLionel Sambuc DiagEntry DE;
130f4a2713aSLionel Sambuc DE.DiagnosticID = Info.getID();
131f4a2713aSLionel Sambuc DE.DiagnosticLevel = Level;
132f4a2713aSLionel Sambuc
133*0a6a1f1dSLionel Sambuc DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
134*0a6a1f1dSLionel Sambuc
135f4a2713aSLionel Sambuc // Format the message.
136f4a2713aSLionel Sambuc SmallString<100> MessageStr;
137f4a2713aSLionel Sambuc Info.FormatDiagnostic(MessageStr);
138f4a2713aSLionel Sambuc DE.Message = MessageStr.str();
139f4a2713aSLionel Sambuc
140f4a2713aSLionel Sambuc // Set the location information.
141f4a2713aSLionel Sambuc DE.Filename = "";
142f4a2713aSLionel Sambuc DE.Line = DE.Column = 0;
143f4a2713aSLionel Sambuc if (Info.getLocation().isValid() && Info.hasSourceManager()) {
144f4a2713aSLionel Sambuc const SourceManager &SM = Info.getSourceManager();
145f4a2713aSLionel Sambuc PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc if (PLoc.isInvalid()) {
148f4a2713aSLionel Sambuc // At least print the file name if available:
149f4a2713aSLionel Sambuc FileID FID = SM.getFileID(Info.getLocation());
150f4a2713aSLionel Sambuc if (!FID.isInvalid()) {
151f4a2713aSLionel Sambuc const FileEntry *FE = SM.getFileEntryForID(FID);
152*0a6a1f1dSLionel Sambuc if (FE && FE->isValid())
153f4a2713aSLionel Sambuc DE.Filename = FE->getName();
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc } else {
156f4a2713aSLionel Sambuc DE.Filename = PLoc.getFilename();
157f4a2713aSLionel Sambuc DE.Line = PLoc.getLine();
158f4a2713aSLionel Sambuc DE.Column = PLoc.getColumn();
159f4a2713aSLionel Sambuc }
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc
162f4a2713aSLionel Sambuc // Record the diagnostic entry.
163f4a2713aSLionel Sambuc Entries.push_back(DE);
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc
166