Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 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" |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 11 | #include "clang/Basic/DiagnosticOptions.h" |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 19 | LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 20 | DiagnosticOptions *diags, |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 21 | bool _OwnsOutputStream) |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 22 | : OS(os), LangOpts(0), DiagOpts(diags), |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 23 | OwnsOutputStream(_OwnsOutputStream) { |
| 24 | } |
| 25 | |
| 26 | LogDiagnosticPrinter::~LogDiagnosticPrinter() { |
| 27 | if (OwnsOutputStream) |
| 28 | delete &OS; |
| 29 | } |
| 30 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 31 | static StringRef getLevelName(DiagnosticsEngine::Level Level) { |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 32 | switch (Level) { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 33 | case DiagnosticsEngine::Ignored: return "ignored"; |
| 34 | case DiagnosticsEngine::Note: return "note"; |
| 35 | case DiagnosticsEngine::Warning: return "warning"; |
| 36 | case DiagnosticsEngine::Error: return "error"; |
| 37 | case DiagnosticsEngine::Fatal: return "fatal error"; |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 38 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 39 | llvm_unreachable("Invalid DiagnosticsEngine level!"); |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chad Rosier | cc78c6c | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 42 | // Escape XML characters inside the raw string. |
| 43 | static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) { |
| 44 | for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) { |
| 45 | char c = *I; |
| 46 | switch (c) { |
| 47 | default: OS << c; break; |
| 48 | case '&': OS << "&"; break; |
| 49 | case '<': OS << "<"; break; |
| 50 | case '>': OS << ">"; break; |
| 51 | case '\'': OS << "'"; break; |
| 52 | case '\"': OS << """; break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 57 | void LogDiagnosticPrinter::EndSourceFile() { |
| 58 | // We emit all the diagnostics in EndSourceFile. However, we don't emit any |
| 59 | // entry if no diagnostics were present. |
| 60 | // |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 61 | // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we |
| 62 | // will miss any diagnostics which are emitted after and outside the |
| 63 | // translation unit processing. |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 64 | if (Entries.empty()) |
| 65 | return; |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 66 | |
| 67 | // Write to a temporary string to ensure atomic write of diagnostic object. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 68 | SmallString<512> Msg; |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 69 | llvm::raw_svector_ostream OS(Msg); |
| 70 | |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 71 | OS << "<dict>\n"; |
Daniel Dunbar | 28f1493 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 72 | if (!MainFilename.empty()) { |
| 73 | OS << " <key>main-file</key>\n" |
Chad Rosier | cc78c6c | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 74 | << " <string>"; |
| 75 | emitString(OS, MainFilename); |
| 76 | OS << "</string>\n"; |
Daniel Dunbar | 28f1493 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 77 | } |
| 78 | if (!DwarfDebugFlags.empty()) { |
| 79 | OS << " <key>dwarf-debug-flags</key>\n" |
Chad Rosier | cc78c6c | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 80 | << " <string>"; |
| 81 | emitString(OS, DwarfDebugFlags); |
| 82 | OS << "</string>\n"; |
Daniel Dunbar | 28f1493 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 83 | } |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 84 | OS << " <key>diagnostics</key>\n"; |
| 85 | OS << " <array>\n"; |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 86 | for (unsigned i = 0, e = Entries.size(); i != e; ++i) { |
| 87 | DiagEntry &DE = Entries[i]; |
| 88 | |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 89 | OS << " <dict>\n"; |
| 90 | OS << " <key>level</key>\n" |
Chad Rosier | cc78c6c | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 91 | << " <string>"; |
| 92 | emitString(OS, getLevelName(DE.DiagnosticLevel)); |
| 93 | OS << "</string>\n"; |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 94 | if (!DE.Filename.empty()) { |
| 95 | OS << " <key>filename</key>\n" |
Chad Rosier | cc78c6c | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 96 | << " <string>"; |
| 97 | emitString(OS, DE.Filename); |
| 98 | OS << "</string>\n"; |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 99 | } |
| 100 | if (DE.Line != 0) { |
| 101 | OS << " <key>line</key>\n" |
| 102 | << " <integer>" << DE.Line << "</integer>\n"; |
| 103 | } |
| 104 | if (DE.Column != 0) { |
| 105 | OS << " <key>column</key>\n" |
| 106 | << " <integer>" << DE.Column << "</integer>\n"; |
| 107 | } |
| 108 | if (!DE.Message.empty()) { |
| 109 | OS << " <key>message</key>\n" |
Chad Rosier | cc78c6c | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 110 | << " <string>"; |
| 111 | emitString(OS, DE.Message); |
| 112 | OS << "</string>\n"; |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 113 | } |
| 114 | OS << " </dict>\n"; |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 115 | } |
Daniel Dunbar | 5dccf57 | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 116 | OS << " </array>\n"; |
| 117 | OS << "</dict>\n"; |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 118 | |
| 119 | this->OS << OS.str(); |
| 120 | } |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 121 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 122 | void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 123 | const Diagnostic &Info) { |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 124 | // Default implementation (Warnings/errors count). |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 125 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | 28f1493 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 127 | // Initialize the main file name, if we haven't already fetched it. |
Daniel Dunbar | 7665ad8 | 2011-05-05 02:12:02 +0000 | [diff] [blame] | 128 | if (MainFilename.empty() && Info.hasSourceManager()) { |
Daniel Dunbar | 28f1493 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 129 | const SourceManager &SM = Info.getSourceManager(); |
| 130 | FileID FID = SM.getMainFileID(); |
| 131 | if (!FID.isInvalid()) { |
| 132 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 133 | if (FE && FE->getName()) |
| 134 | MainFilename = FE->getName(); |
| 135 | } |
| 136 | } |
| 137 | |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 138 | // Create the diag entry. |
| 139 | DiagEntry DE; |
| 140 | DE.DiagnosticID = Info.getID(); |
| 141 | DE.DiagnosticLevel = Level; |
| 142 | |
| 143 | // Format the message. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 144 | SmallString<100> MessageStr; |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 145 | Info.FormatDiagnostic(MessageStr); |
| 146 | DE.Message = MessageStr.str(); |
| 147 | |
| 148 | // Set the location information. |
| 149 | DE.Filename = ""; |
| 150 | DE.Line = DE.Column = 0; |
Daniel Dunbar | 7665ad8 | 2011-05-05 02:12:02 +0000 | [diff] [blame] | 151 | if (Info.getLocation().isValid() && Info.hasSourceManager()) { |
Daniel Dunbar | 64bfbf5 | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 152 | const SourceManager &SM = Info.getSourceManager(); |
| 153 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 154 | |
| 155 | if (PLoc.isInvalid()) { |
| 156 | // At least print the file name if available: |
| 157 | FileID FID = SM.getFileID(Info.getLocation()); |
| 158 | if (!FID.isInvalid()) { |
| 159 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 160 | if (FE && FE->getName()) |
| 161 | DE.Filename = FE->getName(); |
| 162 | } |
| 163 | } else { |
| 164 | DE.Filename = PLoc.getFilename(); |
| 165 | DE.Line = PLoc.getLine(); |
| 166 | DE.Column = PLoc.getColumn(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Record the diagnostic entry. |
| 171 | Entries.push_back(DE); |
| 172 | } |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 173 | |
| 174 | DiagnosticConsumer * |
| 175 | LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const { |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 176 | return new LogDiagnosticPrinter(OS, &*DiagOpts, /*OwnsOutputStream=*/false); |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 177 | } |
| 178 | |