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" |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 13 | #include "clang/Basic/PlistSupport.h" |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 14 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 18 | using namespace clang; |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 19 | using namespace markup; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 20 | |
David Blaikie | 11f8a94 | 2014-09-15 17:30:56 +0000 | [diff] [blame^] | 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) {} |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 26 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 27 | static StringRef getLevelName(DiagnosticsEngine::Level Level) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 28 | switch (Level) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 29 | case DiagnosticsEngine::Ignored: return "ignored"; |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 30 | case DiagnosticsEngine::Remark: return "remark"; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 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"; |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 35 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 36 | llvm_unreachable("Invalid DiagnosticsEngine level!"); |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 39 | void |
| 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'; |
Chad Rosier | 011824f | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 50 | } |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 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 << " </dict>\n"; |
Chad Rosier | 011824f | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 69 | void LogDiagnosticPrinter::EndSourceFile() { |
| 70 | // We emit all the diagnostics in EndSourceFile. However, we don't emit any |
| 71 | // entry if no diagnostics were present. |
| 72 | // |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 73 | // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we |
| 74 | // will miss any diagnostics which are emitted after and outside the |
| 75 | // translation unit processing. |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 76 | if (Entries.empty()) |
| 77 | return; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 78 | |
| 79 | // Write to a temporary string to ensure atomic write of diagnostic object. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 80 | SmallString<512> Msg; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 81 | llvm::raw_svector_ostream OS(Msg); |
| 82 | |
Daniel Dunbar | 719393a | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 83 | OS << "<dict>\n"; |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 84 | if (!MainFilename.empty()) { |
| 85 | OS << " <key>main-file</key>\n" |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 86 | << " "; |
| 87 | EmitString(OS, MainFilename) << '\n'; |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 88 | } |
| 89 | if (!DwarfDebugFlags.empty()) { |
| 90 | OS << " <key>dwarf-debug-flags</key>\n" |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 91 | << " "; |
| 92 | EmitString(OS, DwarfDebugFlags) << '\n'; |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 93 | } |
Daniel Dunbar | 719393a | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 94 | OS << " <key>diagnostics</key>\n"; |
| 95 | OS << " <array>\n"; |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 96 | for (auto &DE : Entries) |
| 97 | EmitDiagEntry(OS, DE); |
Daniel Dunbar | 719393a | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 98 | OS << " </array>\n"; |
| 99 | OS << "</dict>\n"; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 100 | |
| 101 | this->OS << OS.str(); |
| 102 | } |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 103 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 104 | void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 105 | const Diagnostic &Info) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 106 | // Default implementation (Warnings/errors count). |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 107 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 108 | |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 109 | // Initialize the main file name, if we haven't already fetched it. |
Daniel Dunbar | ce1035c | 2011-05-05 02:12:02 +0000 | [diff] [blame] | 110 | if (MainFilename.empty() && Info.hasSourceManager()) { |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 111 | const SourceManager &SM = Info.getSourceManager(); |
| 112 | FileID FID = SM.getMainFileID(); |
| 113 | if (!FID.isInvalid()) { |
| 114 | const FileEntry *FE = SM.getFileEntryForID(FID); |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 115 | if (FE && FE->isValid()) |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 116 | MainFilename = FE->getName(); |
| 117 | } |
| 118 | } |
| 119 | |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 120 | // Create the diag entry. |
| 121 | DiagEntry DE; |
| 122 | DE.DiagnosticID = Info.getID(); |
| 123 | DE.DiagnosticLevel = Level; |
| 124 | |
| 125 | // Format the message. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 126 | SmallString<100> MessageStr; |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 127 | Info.FormatDiagnostic(MessageStr); |
| 128 | DE.Message = MessageStr.str(); |
| 129 | |
| 130 | // Set the location information. |
| 131 | DE.Filename = ""; |
| 132 | DE.Line = DE.Column = 0; |
Daniel Dunbar | ce1035c | 2011-05-05 02:12:02 +0000 | [diff] [blame] | 133 | if (Info.getLocation().isValid() && Info.hasSourceManager()) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 134 | const SourceManager &SM = Info.getSourceManager(); |
| 135 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 136 | |
| 137 | if (PLoc.isInvalid()) { |
| 138 | // At least print the file name if available: |
| 139 | FileID FID = SM.getFileID(Info.getLocation()); |
| 140 | if (!FID.isInvalid()) { |
| 141 | const FileEntry *FE = SM.getFileEntryForID(FID); |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 142 | if (FE && FE->isValid()) |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 143 | DE.Filename = FE->getName(); |
| 144 | } |
| 145 | } else { |
| 146 | DE.Filename = PLoc.getFilename(); |
| 147 | DE.Line = PLoc.getLine(); |
| 148 | DE.Column = PLoc.getColumn(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Record the diagnostic entry. |
| 153 | Entries.push_back(DE); |
| 154 | } |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 155 | |