Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 1 | //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This diagnostic client prints out their diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 51adf58 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 15 | #include "clang/Basic/DiagnosticOptions.h" |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | a302885 | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/TextDiagnostic.h" |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 327984f | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 25 | TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os, |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 26 | DiagnosticOptions *diags, |
Daniel Dunbar | d0c3bb0 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 27 | bool _OwnsOutputStream) |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 28 | : OS(os), DiagOpts(diags), |
Daniel Dunbar | d0c3bb0 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 29 | OwnsOutputStream(_OwnsOutputStream) { |
| 30 | } |
| 31 | |
| 32 | TextDiagnosticPrinter::~TextDiagnosticPrinter() { |
| 33 | if (OwnsOutputStream) |
| 34 | delete &OS; |
Daniel Dunbar | c2e6a47 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chandler Carruth | 3eb8b54 | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 37 | void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO, |
| 38 | const Preprocessor *PP) { |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 39 | // Build the TextDiagnostic utility. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 40 | TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts)); |
Chandler Carruth | 3eb8b54 | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void TextDiagnosticPrinter::EndSourceFile() { |
David Blaikie | 3875a82 | 2014-07-19 01:06:45 +0000 | [diff] [blame] | 44 | TextDiag.reset(); |
Chandler Carruth | 3eb8b54 | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chandler Carruth | cf259a4 | 2011-09-26 01:30:09 +0000 | [diff] [blame] | 47 | /// \brief Print any diagnostic option information to a raw_ostream. |
| 48 | /// |
| 49 | /// This implements all of the logic for adding diagnostic options to a message |
| 50 | /// (via OS). Each relevant option is comma separated and all are enclosed in |
| 51 | /// the standard bracketing: " [...]". |
| 52 | static void printDiagnosticOptions(raw_ostream &OS, |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 53 | DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 54 | const Diagnostic &Info, |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 55 | const DiagnosticOptions &DiagOpts) { |
Chandler Carruth | 0059a9b | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 56 | bool Started = false; |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 57 | if (DiagOpts.ShowOptionNames) { |
Chandler Carruth | 0059a9b | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 58 | // Handle special cases for non-warnings early. |
| 59 | if (Info.getID() == diag::fatal_too_many_errors) { |
| 60 | OS << " [-ferror-limit=]"; |
| 61 | return; |
| 62 | } |
| 63 | |
Daniel Dunbar | aa11138 | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 64 | // The code below is somewhat fragile because we are essentially trying to |
| 65 | // report to the user what happened by inferring what the diagnostic engine |
| 66 | // did. Eventually it might make more sense to have the diagnostic engine |
| 67 | // include some "why" information in the diagnostic. |
| 68 | |
| 69 | // If this is a warning which has been mapped to an error by the user (as |
| 70 | // inferred by checking whether the default mapping is to an error) then |
| 71 | // flag it as such. Note that diagnostics could also have been mapped by a |
| 72 | // pragma, but we don't currently have a way to distinguish this. |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 73 | if (Level == DiagnosticsEngine::Error && |
Daniel Dunbar | aa11138 | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 74 | DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) && |
| 75 | !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) { |
| 76 | OS << " [-Werror"; |
| 77 | Started = true; |
Chandler Carruth | 0059a9b | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 80 | StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID()); |
| 81 | if (!Opt.empty()) { |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 82 | OS << (Started ? "," : " [") |
Alp Toker | 2724ec3 | 2014-06-22 10:08:06 +0000 | [diff] [blame] | 83 | << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt; |
Alp Toker | 4ea0d23 | 2014-07-09 01:37:24 +0000 | [diff] [blame] | 84 | StringRef OptValue = Info.getDiags()->getFlagValue(); |
Diego Novillo | 9f23997 | 2014-04-21 23:16:03 +0000 | [diff] [blame] | 85 | if (!OptValue.empty()) |
| 86 | OS << "=" << OptValue; |
Chandler Carruth | 0059a9b | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 87 | Started = true; |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 90 | |
Chandler Carruth | 0059a9b | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 91 | // If the user wants to see category information, include it too. |
| 92 | if (DiagOpts.ShowCategories) { |
| 93 | unsigned DiagCategory = |
| 94 | DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 95 | if (DiagCategory) { |
Chandler Carruth | 0059a9b | 2011-09-26 01:21:58 +0000 | [diff] [blame] | 96 | OS << (Started ? "," : " ["); |
Benjamin Kramer | e2125d8 | 2011-09-26 02:14:13 +0000 | [diff] [blame] | 97 | Started = true; |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 98 | if (DiagOpts.ShowCategories == 1) |
Benjamin Kramer | e2125d8 | 2011-09-26 02:14:13 +0000 | [diff] [blame] | 99 | OS << DiagCategory; |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 100 | else { |
| 101 | assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value"); |
| 102 | OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory); |
| 103 | } |
| 104 | } |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 105 | } |
Benjamin Kramer | e2125d8 | 2011-09-26 02:14:13 +0000 | [diff] [blame] | 106 | if (Started) |
| 107 | OS << ']'; |
Chandler Carruth | 6074084 | 2011-09-26 00:44:09 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chandler Carruth | 09c65ce | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 110 | void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 111 | const Diagnostic &Info) { |
Chandler Carruth | 09c65ce | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 112 | // Default implementation (Warnings/errors count). |
| 113 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
| 114 | |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 115 | // Render the diagnostic message into a temporary buffer eagerly. We'll use |
| 116 | // this later as we print out the diagnostic to the terminal. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 117 | SmallString<100> OutStr; |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 118 | Info.FormatDiagnostic(OutStr); |
| 119 | |
| 120 | llvm::raw_svector_ostream DiagMessageStream(OutStr); |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 121 | printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts); |
| 122 | |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 123 | // Keeps track of the starting position of the location |
Chandler Carruth | 09c65ce | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 124 | // information (e.g., "foo.c:10:4:") that precedes the error |
| 125 | // message. We use this information to determine how long the |
| 126 | // file+line+column number prefix is. |
| 127 | uint64_t StartOfLocationInfo = OS.tell(); |
| 128 | |
| 129 | if (!Prefix.empty()) |
| 130 | OS << Prefix << ": "; |
| 131 | |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 132 | // Use a dedicated, simpler path for diagnostics without a valid location. |
Chandler Carruth | 577372e | 2011-09-26 11:38:46 +0000 | [diff] [blame] | 133 | // This is important as if the location is missing, we may be emitting |
| 134 | // diagnostics in a context that lacks language options, a source manager, or |
| 135 | // other infrastructure necessary when emitting more rich diagnostics. |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 136 | if (!Info.getLocation().isValid()) { |
Hans Wennborg | f4aee18 | 2013-09-24 00:08:55 +0000 | [diff] [blame] | 137 | TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors, |
| 138 | DiagOpts->CLFallbackMode); |
Chandler Carruth | 76576db | 2011-10-15 22:57:29 +0000 | [diff] [blame] | 139 | TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(), |
| 140 | OS.tell() - StartOfLocationInfo, |
| 141 | DiagOpts->MessageLength, |
| 142 | DiagOpts->ShowColors); |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 143 | OS.flush(); |
| 144 | return; |
Chandler Carruth | 09c65ce | 2011-09-26 00:26:47 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chandler Carruth | 577372e | 2011-09-26 11:38:46 +0000 | [diff] [blame] | 147 | // Assert that the rest of our infrastructure is setup properly. |
Chandler Carruth | 577372e | 2011-09-26 11:38:46 +0000 | [diff] [blame] | 148 | assert(DiagOpts && "Unexpected diagnostic without options set"); |
| 149 | assert(Info.hasSourceManager() && |
| 150 | "Unexpected diagnostic with no source manager"); |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 151 | assert(TextDiag && "Unexpected diagnostic outside source file processing"); |
Chandler Carruth | 2be992d | 2011-09-26 11:25:30 +0000 | [diff] [blame] | 152 | |
Chandler Carruth | 3eb8b54 | 2011-10-16 02:57:39 +0000 | [diff] [blame] | 153 | TextDiag->emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(), |
| 154 | Info.getRanges(), |
Alexander Kornienko | d3b4e08 | 2014-05-22 19:56:11 +0000 | [diff] [blame] | 155 | Info.getFixItHints(), |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 156 | &Info.getSourceManager()); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 327984f | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 158 | OS.flush(); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 159 | } |