1 //===--- 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"
11 #include "clang/Basic/DiagnosticOptions.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/PlistSupport.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 using namespace clang;
19 using namespace markup;
20
LogDiagnosticPrinter(raw_ostream & os,DiagnosticOptions * diags,std::unique_ptr<raw_ostream> StreamOwner)21 LogDiagnosticPrinter::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) {}
26
getLevelName(DiagnosticsEngine::Level Level)27 static StringRef getLevelName(DiagnosticsEngine::Level Level) {
28 switch (Level) {
29 case DiagnosticsEngine::Ignored: return "ignored";
30 case DiagnosticsEngine::Remark: return "remark";
31 case DiagnosticsEngine::Note: return "note";
32 case DiagnosticsEngine::Warning: return "warning";
33 case DiagnosticsEngine::Error: return "error";
34 case DiagnosticsEngine::Fatal: return "fatal error";
35 }
36 llvm_unreachable("Invalid DiagnosticsEngine level!");
37 }
38
39 void
EmitDiagEntry(llvm::raw_ostream & OS,const LogDiagnosticPrinter::DiagEntry & DE)40 LogDiagnosticPrinter::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';
50 }
51 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 << " <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 }
74 OS << " </dict>\n";
75 }
76
EndSourceFile()77 void LogDiagnosticPrinter::EndSourceFile() {
78 // We emit all the diagnostics in EndSourceFile. However, we don't emit any
79 // entry if no diagnostics were present.
80 //
81 // 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.
84 if (Entries.empty())
85 return;
86
87 // Write to a temporary string to ensure atomic write of diagnostic object.
88 SmallString<512> Msg;
89 llvm::raw_svector_ostream OS(Msg);
90
91 OS << "<dict>\n";
92 if (!MainFilename.empty()) {
93 OS << " <key>main-file</key>\n"
94 << " ";
95 EmitString(OS, MainFilename) << '\n';
96 }
97 if (!DwarfDebugFlags.empty()) {
98 OS << " <key>dwarf-debug-flags</key>\n"
99 << " ";
100 EmitString(OS, DwarfDebugFlags) << '\n';
101 }
102 OS << " <key>diagnostics</key>\n";
103 OS << " <array>\n";
104 for (auto &DE : Entries)
105 EmitDiagEntry(OS, DE);
106 OS << " </array>\n";
107 OS << "</dict>\n";
108
109 this->OS << OS.str();
110 }
111
HandleDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)112 void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
113 const Diagnostic &Info) {
114 // Default implementation (Warnings/errors count).
115 DiagnosticConsumer::HandleDiagnostic(Level, Info);
116
117 // Initialize the main file name, if we haven't already fetched it.
118 if (MainFilename.empty() && Info.hasSourceManager()) {
119 const SourceManager &SM = Info.getSourceManager();
120 FileID FID = SM.getMainFileID();
121 if (FID.isValid()) {
122 const FileEntry *FE = SM.getFileEntryForID(FID);
123 if (FE && FE->isValid())
124 MainFilename = FE->getName();
125 }
126 }
127
128 // Create the diag entry.
129 DiagEntry DE;
130 DE.DiagnosticID = Info.getID();
131 DE.DiagnosticLevel = Level;
132
133 DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
134
135 // Format the message.
136 SmallString<100> MessageStr;
137 Info.FormatDiagnostic(MessageStr);
138 DE.Message = MessageStr.str();
139
140 // Set the location information.
141 DE.Filename = "";
142 DE.Line = DE.Column = 0;
143 if (Info.getLocation().isValid() && Info.hasSourceManager()) {
144 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.isValid()) {
151 const FileEntry *FE = SM.getFileEntryForID(FID);
152 if (FE && FE->isValid())
153 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 }
165
166