Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This diagnostic client prints out their diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Axel Naumann | 0433116 | 2011-01-27 10:55:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | eace874 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/DiagnosticOptions.h" |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/TextDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 548f6c8 | 2011-09-23 05:57:42 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 27 | TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os, |
Daniel Dunbar | aea3641 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 28 | const DiagnosticOptions &diags, |
| 29 | bool _OwnsOutputStream) |
Chandler Carruth | 21a869a | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 30 | : OS(os), LangOpts(0), DiagOpts(&diags), SM(0), |
Daniel Dunbar | aea3641 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 31 | OwnsOutputStream(_OwnsOutputStream) { |
| 32 | } |
| 33 | |
| 34 | TextDiagnosticPrinter::~TextDiagnosticPrinter() { |
| 35 | if (OwnsOutputStream) |
| 36 | delete &OS; |
Daniel Dunbar | eace874 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Chandler Carruth | 21a869a | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 39 | void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO, |
| 40 | const Preprocessor *PP) { |
| 41 | LangOpts = &LO; |
| 42 | } |
| 43 | |
| 44 | void TextDiagnosticPrinter::EndSourceFile() { |
| 45 | LangOpts = 0; |
| 46 | TextDiag.reset(0); |
| 47 | } |
| 48 | |
Chandler Carruth | a3ba6bb | 2011-09-26 01:30:09 +0000 | [diff] [blame] | 49 | /// \brief Print the diagnostic name to a raw_ostream. |
| 50 | /// |
| 51 | /// This prints the diagnostic name to a raw_ostream if it has one. It formats |
| 52 | /// the name according to the expected diagnostic message formatting: |
| 53 | /// " [diagnostic_name_here]" |
| 54 | static void printDiagnosticName(raw_ostream &OS, const Diagnostic &Info) { |
Chandler Carruth | 7614578 | 2011-09-26 00:37:30 +0000 | [diff] [blame] | 55 | if (!DiagnosticIDs::isBuiltinNote(Info.getID())) |
| 56 | OS << " [" << DiagnosticIDs::getName(Info.getID()) << "]"; |
| 57 | } |
| 58 | |
Chandler Carruth | a3ba6bb | 2011-09-26 01:30:09 +0000 | [diff] [blame] | 59 | /// \brief Print any diagnostic option information to a raw_ostream. |
| 60 | /// |
| 61 | /// This implements all of the logic for adding diagnostic options to a message |
| 62 | /// (via OS). Each relevant option is comma separated and all are enclosed in |
| 63 | /// the standard bracketing: " [...]". |
| 64 | static void printDiagnosticOptions(raw_ostream &OS, |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 65 | DiagnosticsEngine::Level Level, |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 66 | const Diagnostic &Info, |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 67 | const DiagnosticOptions &DiagOpts) { |
Chandler Carruth | 253d41d | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 68 | bool Started = false; |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 69 | if (DiagOpts.ShowOptionNames) { |
Chandler Carruth | 253d41d | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 70 | // Handle special cases for non-warnings early. |
| 71 | if (Info.getID() == diag::fatal_too_many_errors) { |
| 72 | OS << " [-ferror-limit=]"; |
| 73 | return; |
| 74 | } |
| 75 | |
Daniel Dunbar | 76101cf | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 76 | // The code below is somewhat fragile because we are essentially trying to |
| 77 | // report to the user what happened by inferring what the diagnostic engine |
| 78 | // did. Eventually it might make more sense to have the diagnostic engine |
| 79 | // include some "why" information in the diagnostic. |
| 80 | |
| 81 | // If this is a warning which has been mapped to an error by the user (as |
| 82 | // inferred by checking whether the default mapping is to an error) then |
| 83 | // flag it as such. Note that diagnostics could also have been mapped by a |
| 84 | // pragma, but we don't currently have a way to distinguish this. |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 85 | if (Level == DiagnosticsEngine::Error && |
Daniel Dunbar | 76101cf | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 86 | DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) && |
| 87 | !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) { |
| 88 | OS << " [-Werror"; |
| 89 | Started = true; |
Chandler Carruth | 253d41d | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // If the diagnostic is an extension diagnostic and not enabled by default |
| 93 | // then it must have been turned on with -pedantic. |
| 94 | bool EnabledByDefault; |
| 95 | if (DiagnosticIDs::isBuiltinExtensionDiag(Info.getID(), |
| 96 | EnabledByDefault) && |
| 97 | !EnabledByDefault) { |
| 98 | OS << (Started ? "," : " [") << "-pedantic"; |
| 99 | Started = true; |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID()); |
| 103 | if (!Opt.empty()) { |
Chandler Carruth | 253d41d | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 104 | OS << (Started ? "," : " [") << "-W" << Opt; |
| 105 | Started = true; |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 108 | |
Chandler Carruth | 253d41d | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 109 | // If the user wants to see category information, include it too. |
| 110 | if (DiagOpts.ShowCategories) { |
| 111 | unsigned DiagCategory = |
| 112 | DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 113 | if (DiagCategory) { |
Chandler Carruth | 253d41d | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 114 | OS << (Started ? "," : " ["); |
Benjamin Kramer | a65cb0c | 2011-09-26 02:14:13 +0000 | [diff] [blame] | 115 | Started = true; |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 116 | if (DiagOpts.ShowCategories == 1) |
Benjamin Kramer | a65cb0c | 2011-09-26 02:14:13 +0000 | [diff] [blame] | 117 | OS << DiagCategory; |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 118 | else { |
| 119 | assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value"); |
| 120 | OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory); |
| 121 | } |
| 122 | } |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 123 | } |
Benjamin Kramer | a65cb0c | 2011-09-26 02:14:13 +0000 | [diff] [blame] | 124 | if (Started) |
| 125 | OS << ']'; |
Chandler Carruth | 53a529e | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chandler Carruth | a629004 | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 128 | void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 129 | const Diagnostic &Info) { |
Chandler Carruth | a629004 | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 130 | // Default implementation (Warnings/errors count). |
| 131 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
| 132 | |
Chandler Carruth | 0ef8988 | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 133 | // Render the diagnostic message into a temporary buffer eagerly. We'll use |
| 134 | // this later as we print out the diagnostic to the terminal. |
| 135 | llvm::SmallString<100> OutStr; |
| 136 | Info.FormatDiagnostic(OutStr); |
| 137 | |
| 138 | llvm::raw_svector_ostream DiagMessageStream(OutStr); |
| 139 | if (DiagOpts->ShowNames) |
| 140 | printDiagnosticName(DiagMessageStream, Info); |
| 141 | printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts); |
| 142 | |
Chandler Carruth | a629004 | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 143 | // Keeps track of the the starting position of the location |
| 144 | // information (e.g., "foo.c:10:4:") that precedes the error |
| 145 | // message. We use this information to determine how long the |
| 146 | // file+line+column number prefix is. |
| 147 | uint64_t StartOfLocationInfo = OS.tell(); |
| 148 | |
| 149 | if (!Prefix.empty()) |
| 150 | OS << Prefix << ": "; |
| 151 | |
Chandler Carruth | 0ef8988 | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 152 | // Use a dedicated, simpler path for diagnostics without a valid location. |
Chandler Carruth | e6d1dff | 2011-09-26 11:38:46 +0000 | [diff] [blame] | 153 | // This is important as if the location is missing, we may be emitting |
| 154 | // diagnostics in a context that lacks language options, a source manager, or |
| 155 | // other infrastructure necessary when emitting more rich diagnostics. |
Chandler Carruth | 0ef8988 | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 156 | if (!Info.getLocation().isValid()) { |
Chandler Carruth | 7eb84dc | 2011-10-15 22:49:21 +0000 | [diff] [blame] | 157 | TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); |
Chandler Carruth | 1f3839e | 2011-10-15 22:57:29 +0000 | [diff] [blame] | 158 | TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(), |
| 159 | OS.tell() - StartOfLocationInfo, |
| 160 | DiagOpts->MessageLength, |
| 161 | DiagOpts->ShowColors); |
Chandler Carruth | 0ef8988 | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 162 | OS.flush(); |
| 163 | return; |
Chandler Carruth | a629004 | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chandler Carruth | e6d1dff | 2011-09-26 11:38:46 +0000 | [diff] [blame] | 166 | // Assert that the rest of our infrastructure is setup properly. |
| 167 | assert(LangOpts && "Unexpected diagnostic outside source file processing"); |
| 168 | assert(DiagOpts && "Unexpected diagnostic without options set"); |
| 169 | assert(Info.hasSourceManager() && |
| 170 | "Unexpected diagnostic with no source manager"); |
Chandler Carruth | e6d1dff | 2011-09-26 11:38:46 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | 21a869a | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 172 | // Rebuild the TextDiagnostic utility if missing or the source manager has |
| 173 | // changed. |
| 174 | if (!TextDiag || SM != &Info.getSourceManager()) { |
| 175 | SM = &Info.getSourceManager(); |
| 176 | TextDiag.reset(new TextDiagnostic(OS, *SM, *LangOpts, *DiagOpts)); |
| 177 | } |
Chandler Carruth | 0ef8988 | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 178 | |
Chandler Carruth | 21a869a | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 179 | TextDiag->emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(), |
| 180 | Info.getRanges(), |
| 181 | llvm::makeArrayRef(Info.getFixItHints(), |
| 182 | Info.getNumFixItHints())); |
Daniel Dunbar | cbff0dc | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 183 | |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 184 | OS.flush(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | } |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 186 | |
| 187 | DiagnosticConsumer * |
| 188 | TextDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const { |
| 189 | return new TextDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false); |
| 190 | } |