blob: 28f7218dc23f549e5f226483cea3554e3c74333f [file] [log] [blame]
Bill Wendling37b1dde2007-06-07 09:34:54 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Wendling37b1dde2007-06-07 09:34:54 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This diagnostic client prints out their diagnostic messages.
10//
11//===----------------------------------------------------------------------===//
12
Daniel Dunbar51adf582009-03-02 06:16:29 +000013#include "clang/Frontend/TextDiagnosticPrinter.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000014#include "clang/Basic/DiagnosticOptions.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000015#include "clang/Basic/SourceManager.h"
Chandler Carrutha3028852011-10-15 23:43:53 +000016#include "clang/Frontend/TextDiagnostic.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000017#include "clang/Lex/Lexer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "llvm/ADT/SmallString.h"
19#include "llvm/Support/ErrorHandling.h"
Chris Lattner327984f2008-11-19 06:56:25 +000020#include "llvm/Support/raw_ostream.h"
Douglas Gregor87f95b02009-02-26 21:00:50 +000021#include <algorithm>
Bill Wendling37b1dde2007-06-07 09:34:54 +000022using namespace clang;
23
Chris Lattner0e62c1c2011-07-23 10:55:15 +000024TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
Douglas Gregor811db4e2012-10-23 22:26:28 +000025 DiagnosticOptions *diags,
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000026 bool _OwnsOutputStream)
Douglas Gregor811db4e2012-10-23 22:26:28 +000027 : OS(os), DiagOpts(diags),
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000028 OwnsOutputStream(_OwnsOutputStream) {
29}
30
31TextDiagnosticPrinter::~TextDiagnosticPrinter() {
32 if (OwnsOutputStream)
33 delete &OS;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000034}
35
Chandler Carruth3eb8b542011-10-16 02:57:39 +000036void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
37 const Preprocessor *PP) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +000038 // Build the TextDiagnostic utility.
Timm Baeder718aac92024-01-27 17:52:20 +010039 TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts, PP));
Chandler Carruth3eb8b542011-10-16 02:57:39 +000040}
41
42void TextDiagnosticPrinter::EndSourceFile() {
David Blaikie3875a822014-07-19 01:06:45 +000043 TextDiag.reset();
Chandler Carruth3eb8b542011-10-16 02:57:39 +000044}
45
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046/// Print any diagnostic option information to a raw_ostream.
Chandler Carruthcf259a42011-09-26 01:30:09 +000047///
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: " [...]".
51static void printDiagnosticOptions(raw_ostream &OS,
Chandler Carruth60740842011-09-26 00:44:09 +000052 DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +000053 const Diagnostic &Info,
Chandler Carruth60740842011-09-26 00:44:09 +000054 const DiagnosticOptions &DiagOpts) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000055 bool Started = false;
Chandler Carruth60740842011-09-26 00:44:09 +000056 if (DiagOpts.ShowOptionNames) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000057 // 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 Dunbaraa111382011-09-29 01:01:08 +000063 // 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 Carruth60740842011-09-26 00:44:09 +000072 if (Level == DiagnosticsEngine::Error &&
Nikolas Klauser0865ecc2025-01-28 08:41:31 +010073 Info.getDiags()->getDiagnosticIDs()->isWarningOrExtension(
74 Info.getID()) &&
75 !Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
76 Info.getID())) {
Daniel Dunbaraa111382011-09-29 01:01:08 +000077 OS << " [-Werror";
78 Started = true;
Chandler Carruth0059a9b2011-09-26 01:21:58 +000079 }
80
Nikolas Klauser0865ecc2025-01-28 08:41:31 +010081 StringRef Opt =
82 Info.getDiags()->getDiagnosticIDs()->getWarningOptionForDiag(
83 Info.getID());
Chandler Carruth60740842011-09-26 00:44:09 +000084 if (!Opt.empty()) {
Diego Novillo829b1702014-04-16 16:54:24 +000085 OS << (Started ? "," : " [")
Alp Toker2724ec32014-06-22 10:08:06 +000086 << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt;
Vakhurin Sergeieda72fa2024-09-18 18:46:25 +030087 StringRef OptValue = Info.getFlagValue();
Diego Novillo9f239972014-04-21 23:16:03 +000088 if (!OptValue.empty())
89 OS << "=" << OptValue;
Chandler Carruth0059a9b2011-09-26 01:21:58 +000090 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +000091 }
92 }
Chandler Carruth60740842011-09-26 00:44:09 +000093
Chandler Carruth0059a9b2011-09-26 01:21:58 +000094 // If the user wants to see category information, include it too.
95 if (DiagOpts.ShowCategories) {
96 unsigned DiagCategory =
97 DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
Chandler Carruth60740842011-09-26 00:44:09 +000098 if (DiagCategory) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000099 OS << (Started ? "," : " [");
Benjamin Kramere2125d82011-09-26 02:14:13 +0000100 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +0000101 if (DiagOpts.ShowCategories == 1)
Benjamin Kramere2125d82011-09-26 02:14:13 +0000102 OS << DiagCategory;
Chandler Carruth60740842011-09-26 00:44:09 +0000103 else {
104 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
105 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
106 }
107 }
Chandler Carruth60740842011-09-26 00:44:09 +0000108 }
Benjamin Kramere2125d82011-09-26 02:14:13 +0000109 if (Started)
110 OS << ']';
Chandler Carruth60740842011-09-26 00:44:09 +0000111}
112
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000113void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000114 const Diagnostic &Info) {
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000115 // Default implementation (Warnings/errors count).
116 DiagnosticConsumer::HandleDiagnostic(Level, Info);
117
Chandler Carruth2be992d2011-09-26 11:25:30 +0000118 // 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 Noblesmith2c1dd272012-02-05 02:13:05 +0000120 SmallString<100> OutStr;
Chandler Carruth2be992d2011-09-26 11:25:30 +0000121 Info.FormatDiagnostic(OutStr);
122
123 llvm::raw_svector_ostream DiagMessageStream(OutStr);
Chandler Carruth2be992d2011-09-26 11:25:30 +0000124 printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
125
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000126 // Keeps track of the starting position of the location
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000127 // 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 Carruth2be992d2011-09-26 11:25:30 +0000135 // Use a dedicated, simpler path for diagnostics without a valid location.
Chandler Carruth577372e2011-09-26 11:38:46 +0000136 // 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 Carruth2be992d2011-09-26 11:25:30 +0000139 if (!Info.getLocation().isValid()) {
Hans Wennborg66256802021-02-02 14:10:26 +0100140 TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
Andrzej Warzynski4eae6fc2020-09-16 17:54:29 +0100141 TextDiagnostic::printDiagnosticMessage(
142 OS, /*IsSupplemental=*/Level == DiagnosticsEngine::Note,
143 DiagMessageStream.str(), OS.tell() - StartOfLocationInfo,
144 DiagOpts->MessageLength, DiagOpts->ShowColors);
Chandler Carruth2be992d2011-09-26 11:25:30 +0000145 OS.flush();
146 return;
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000147 }
148
Chandler Carruth577372e2011-09-26 11:38:46 +0000149 // Assert that the rest of our infrastructure is setup properly.
Chandler Carruth577372e2011-09-26 11:38:46 +0000150 assert(DiagOpts && "Unexpected diagnostic without options set");
151 assert(Info.hasSourceManager() &&
152 "Unexpected diagnostic with no source manager");
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000153 assert(TextDiag && "Unexpected diagnostic outside source file processing");
Chandler Carruth2be992d2011-09-26 11:25:30 +0000154
Christof Doumafb4a0452017-06-27 09:50:38 +0000155 TextDiag->emitDiagnostic(
156 FullSourceLoc(Info.getLocation(), Info.getSourceManager()), Level,
157 DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints());
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000158
Chris Lattner327984f2008-11-19 06:56:25 +0000159 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000160}