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