Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 1 | //===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Frontend/LogDiagnosticPrinter.h" |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 10 | #include "clang/Basic/DiagnosticOptions.h" |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 11 | #include "clang/Basic/FileManager.h" |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 12 | #include "clang/Basic/PlistSupport.h" |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 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; |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 18 | using namespace markup; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 19 | |
David Blaikie | 11f8a94 | 2014-09-15 17:30:56 +0000 | [diff] [blame] | 20 | LogDiagnosticPrinter::LogDiagnosticPrinter( |
| 21 | raw_ostream &os, DiagnosticOptions *diags, |
| 22 | std::unique_ptr<raw_ostream> StreamOwner) |
| 23 | : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr), |
| 24 | DiagOpts(diags) {} |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 25 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 26 | static StringRef getLevelName(DiagnosticsEngine::Level Level) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 27 | switch (Level) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 28 | case DiagnosticsEngine::Ignored: return "ignored"; |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 29 | case DiagnosticsEngine::Remark: return "remark"; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 30 | case DiagnosticsEngine::Note: return "note"; |
| 31 | case DiagnosticsEngine::Warning: return "warning"; |
| 32 | case DiagnosticsEngine::Error: return "error"; |
| 33 | case DiagnosticsEngine::Fatal: return "fatal error"; |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 34 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 35 | llvm_unreachable("Invalid DiagnosticsEngine level!"); |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 38 | void |
| 39 | LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS, |
| 40 | const LogDiagnosticPrinter::DiagEntry &DE) { |
| 41 | OS << " <dict>\n"; |
| 42 | OS << " <key>level</key>\n" |
| 43 | << " "; |
| 44 | EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n'; |
| 45 | if (!DE.Filename.empty()) { |
| 46 | OS << " <key>filename</key>\n" |
| 47 | << " "; |
| 48 | EmitString(OS, DE.Filename) << '\n'; |
Chad Rosier | 011824f | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 49 | } |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 50 | if (DE.Line != 0) { |
| 51 | OS << " <key>line</key>\n" |
| 52 | << " "; |
| 53 | EmitInteger(OS, DE.Line) << '\n'; |
| 54 | } |
| 55 | if (DE.Column != 0) { |
| 56 | OS << " <key>column</key>\n" |
| 57 | << " "; |
| 58 | EmitInteger(OS, DE.Column) << '\n'; |
| 59 | } |
| 60 | if (!DE.Message.empty()) { |
| 61 | OS << " <key>message</key>\n" |
| 62 | << " "; |
| 63 | EmitString(OS, DE.Message) << '\n'; |
| 64 | } |
Steven Wu | 2e2b0b3 | 2014-11-14 21:23:56 +0000 | [diff] [blame] | 65 | OS << " <key>ID</key>\n" |
| 66 | << " "; |
| 67 | EmitInteger(OS, DE.DiagnosticID) << '\n'; |
| 68 | if (!DE.WarningOption.empty()) { |
| 69 | OS << " <key>WarningOption</key>\n" |
| 70 | << " "; |
| 71 | EmitString(OS, DE.WarningOption) << '\n'; |
| 72 | } |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 73 | OS << " </dict>\n"; |
Chad Rosier | 011824f | 2011-09-28 23:05:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 76 | void LogDiagnosticPrinter::EndSourceFile() { |
| 77 | // We emit all the diagnostics in EndSourceFile. However, we don't emit any |
| 78 | // entry if no diagnostics were present. |
| 79 | // |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 80 | // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we |
| 81 | // will miss any diagnostics which are emitted after and outside the |
| 82 | // translation unit processing. |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 83 | if (Entries.empty()) |
| 84 | return; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 85 | |
| 86 | // Write to a temporary string to ensure atomic write of diagnostic object. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 87 | SmallString<512> Msg; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 88 | llvm::raw_svector_ostream OS(Msg); |
| 89 | |
Daniel Dunbar | 719393a | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 90 | OS << "<dict>\n"; |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 91 | if (!MainFilename.empty()) { |
| 92 | OS << " <key>main-file</key>\n" |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 93 | << " "; |
| 94 | EmitString(OS, MainFilename) << '\n'; |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 95 | } |
| 96 | if (!DwarfDebugFlags.empty()) { |
| 97 | OS << " <key>dwarf-debug-flags</key>\n" |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 98 | << " "; |
| 99 | EmitString(OS, DwarfDebugFlags) << '\n'; |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 100 | } |
Daniel Dunbar | 719393a | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 101 | OS << " <key>diagnostics</key>\n"; |
| 102 | OS << " <array>\n"; |
Alp Toker | 525fdfc | 2014-07-06 04:26:52 +0000 | [diff] [blame] | 103 | for (auto &DE : Entries) |
| 104 | EmitDiagEntry(OS, DE); |
Daniel Dunbar | 719393a | 2011-04-07 18:44:15 +0000 | [diff] [blame] | 105 | OS << " </array>\n"; |
| 106 | OS << "</dict>\n"; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 107 | |
| 108 | this->OS << OS.str(); |
| 109 | } |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 110 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 111 | void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 112 | const Diagnostic &Info) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 113 | // Default implementation (Warnings/errors count). |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 114 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 116 | // Initialize the main file name, if we haven't already fetched it. |
Daniel Dunbar | ce1035c | 2011-05-05 02:12:02 +0000 | [diff] [blame] | 117 | if (MainFilename.empty() && Info.hasSourceManager()) { |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 118 | const SourceManager &SM = Info.getSourceManager(); |
| 119 | FileID FID = SM.getMainFileID(); |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 120 | if (FID.isValid()) { |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 121 | const FileEntry *FE = SM.getFileEntryForID(FID); |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 122 | if (FE && FE->isValid()) |
Daniel Dunbar | c032503 | 2011-04-07 18:51:54 +0000 | [diff] [blame] | 123 | MainFilename = FE->getName(); |
| 124 | } |
| 125 | } |
| 126 | |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 127 | // Create the diag entry. |
| 128 | DiagEntry DE; |
| 129 | DE.DiagnosticID = Info.getID(); |
| 130 | DE.DiagnosticLevel = Level; |
| 131 | |
Steven Wu | 2e2b0b3 | 2014-11-14 21:23:56 +0000 | [diff] [blame] | 132 | DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID); |
| 133 | |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 134 | // Format the message. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 135 | SmallString<100> MessageStr; |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 136 | Info.FormatDiagnostic(MessageStr); |
| 137 | DE.Message = MessageStr.str(); |
| 138 | |
| 139 | // Set the location information. |
| 140 | DE.Filename = ""; |
| 141 | DE.Line = DE.Column = 0; |
Daniel Dunbar | ce1035c | 2011-05-05 02:12:02 +0000 | [diff] [blame] | 142 | if (Info.getLocation().isValid() && Info.hasSourceManager()) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 143 | const SourceManager &SM = Info.getSourceManager(); |
| 144 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 145 | |
| 146 | if (PLoc.isInvalid()) { |
| 147 | // At least print the file name if available: |
| 148 | FileID FID = SM.getFileID(Info.getLocation()); |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 149 | if (FID.isValid()) { |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 150 | const FileEntry *FE = SM.getFileEntryForID(FID); |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 151 | if (FE && FE->isValid()) |
Daniel Dunbar | 4f3a28b | 2011-04-07 18:37:34 +0000 | [diff] [blame] | 152 | DE.Filename = FE->getName(); |
| 153 | } |
| 154 | } else { |
| 155 | DE.Filename = PLoc.getFilename(); |
| 156 | DE.Line = PLoc.getLine(); |
| 157 | DE.Column = PLoc.getColumn(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Record the diagnostic entry. |
| 162 | Entries.push_back(DE); |
| 163 | } |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 164 | |