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