blob: cf2826484f80cad4b5e02d08e0c274042480500d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticPrinter.h"
Axel Naumann04331162011-01-27 10:55:51 +000015#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/SourceManager.h"
Daniel Dunbareace8742009-11-04 06:24:30 +000017#include "clang/Frontend/DiagnosticOptions.h"
Chandler Carruthdb463bb2011-10-15 23:43:53 +000018#include "clang/Frontend/TextDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Lex/Lexer.h"
Chris Lattner037fb7f2009-05-05 22:03:18 +000020#include "llvm/Support/MemoryBuffer.h"
Chris Lattnera03a5b52008-11-19 06:56:25 +000021#include "llvm/Support/raw_ostream.h"
David Blaikie548f6c82011-09-23 05:57:42 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000023#include "llvm/ADT/SmallString.h"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000024#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Chris Lattner5f9e2722011-07-23 10:55:15 +000027TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
Daniel Dunbaraea36412009-11-11 09:38:24 +000028 const DiagnosticOptions &diags,
29 bool _OwnsOutputStream)
Chandler Carruth03efd2e2011-10-15 22:39:16 +000030 : OS(os), LangOpts(0), DiagOpts(&diags), LastLevel(),
Daniel Dunbaraea36412009-11-11 09:38:24 +000031 OwnsOutputStream(_OwnsOutputStream) {
32}
33
34TextDiagnosticPrinter::~TextDiagnosticPrinter() {
35 if (OwnsOutputStream)
36 delete &OS;
Daniel Dunbareace8742009-11-04 06:24:30 +000037}
38
Chandler Carrutha3ba6bb2011-09-26 01:30:09 +000039/// \brief Print the diagnostic name to a raw_ostream.
40///
41/// This prints the diagnostic name to a raw_ostream if it has one. It formats
42/// the name according to the expected diagnostic message formatting:
43/// " [diagnostic_name_here]"
44static void printDiagnosticName(raw_ostream &OS, const Diagnostic &Info) {
Chandler Carruth76145782011-09-26 00:37:30 +000045 if (!DiagnosticIDs::isBuiltinNote(Info.getID()))
46 OS << " [" << DiagnosticIDs::getName(Info.getID()) << "]";
47}
48
Chandler Carrutha3ba6bb2011-09-26 01:30:09 +000049/// \brief Print any diagnostic option information to a raw_ostream.
50///
51/// This implements all of the logic for adding diagnostic options to a message
52/// (via OS). Each relevant option is comma separated and all are enclosed in
53/// the standard bracketing: " [...]".
54static void printDiagnosticOptions(raw_ostream &OS,
Chandler Carruth53a529e2011-09-26 00:44:09 +000055 DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +000056 const Diagnostic &Info,
Chandler Carruth53a529e2011-09-26 00:44:09 +000057 const DiagnosticOptions &DiagOpts) {
Chandler Carruth253d41d2011-09-26 01:21:58 +000058 bool Started = false;
Chandler Carruth53a529e2011-09-26 00:44:09 +000059 if (DiagOpts.ShowOptionNames) {
Chandler Carruth253d41d2011-09-26 01:21:58 +000060 // Handle special cases for non-warnings early.
61 if (Info.getID() == diag::fatal_too_many_errors) {
62 OS << " [-ferror-limit=]";
63 return;
64 }
65
Daniel Dunbar76101cf2011-09-29 01:01:08 +000066 // The code below is somewhat fragile because we are essentially trying to
67 // report to the user what happened by inferring what the diagnostic engine
68 // did. Eventually it might make more sense to have the diagnostic engine
69 // include some "why" information in the diagnostic.
70
71 // If this is a warning which has been mapped to an error by the user (as
72 // inferred by checking whether the default mapping is to an error) then
73 // flag it as such. Note that diagnostics could also have been mapped by a
74 // pragma, but we don't currently have a way to distinguish this.
Chandler Carruth53a529e2011-09-26 00:44:09 +000075 if (Level == DiagnosticsEngine::Error &&
Daniel Dunbar76101cf2011-09-29 01:01:08 +000076 DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) &&
77 !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) {
78 OS << " [-Werror";
79 Started = true;
Chandler Carruth253d41d2011-09-26 01:21:58 +000080 }
81
82 // If the diagnostic is an extension diagnostic and not enabled by default
83 // then it must have been turned on with -pedantic.
84 bool EnabledByDefault;
85 if (DiagnosticIDs::isBuiltinExtensionDiag(Info.getID(),
86 EnabledByDefault) &&
87 !EnabledByDefault) {
88 OS << (Started ? "," : " [") << "-pedantic";
89 Started = true;
Chandler Carruth53a529e2011-09-26 00:44:09 +000090 }
91
92 StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
93 if (!Opt.empty()) {
Chandler Carruth253d41d2011-09-26 01:21:58 +000094 OS << (Started ? "," : " [") << "-W" << Opt;
95 Started = true;
Chandler Carruth53a529e2011-09-26 00:44:09 +000096 }
97 }
Chandler Carruth53a529e2011-09-26 00:44:09 +000098
Chandler Carruth253d41d2011-09-26 01:21:58 +000099 // If the user wants to see category information, include it too.
100 if (DiagOpts.ShowCategories) {
101 unsigned DiagCategory =
102 DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
Chandler Carruth53a529e2011-09-26 00:44:09 +0000103 if (DiagCategory) {
Chandler Carruth253d41d2011-09-26 01:21:58 +0000104 OS << (Started ? "," : " [");
Benjamin Kramera65cb0c2011-09-26 02:14:13 +0000105 Started = true;
Chandler Carruth53a529e2011-09-26 00:44:09 +0000106 if (DiagOpts.ShowCategories == 1)
Benjamin Kramera65cb0c2011-09-26 02:14:13 +0000107 OS << DiagCategory;
Chandler Carruth53a529e2011-09-26 00:44:09 +0000108 else {
109 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
110 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
111 }
112 }
Chandler Carruth53a529e2011-09-26 00:44:09 +0000113 }
Benjamin Kramera65cb0c2011-09-26 02:14:13 +0000114 if (Started)
115 OS << ']';
Chandler Carruth53a529e2011-09-26 00:44:09 +0000116}
117
Chandler Carrutha6290042011-09-26 00:26:47 +0000118void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000119 const Diagnostic &Info) {
Chandler Carrutha6290042011-09-26 00:26:47 +0000120 // Default implementation (Warnings/errors count).
121 DiagnosticConsumer::HandleDiagnostic(Level, Info);
122
Chandler Carruth0ef89882011-09-26 11:25:30 +0000123 // Render the diagnostic message into a temporary buffer eagerly. We'll use
124 // this later as we print out the diagnostic to the terminal.
125 llvm::SmallString<100> OutStr;
126 Info.FormatDiagnostic(OutStr);
127
128 llvm::raw_svector_ostream DiagMessageStream(OutStr);
129 if (DiagOpts->ShowNames)
130 printDiagnosticName(DiagMessageStream, Info);
131 printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
132
Chandler Carrutha6290042011-09-26 00:26:47 +0000133 // Keeps track of the the starting position of the location
134 // information (e.g., "foo.c:10:4:") that precedes the error
135 // message. We use this information to determine how long the
136 // file+line+column number prefix is.
137 uint64_t StartOfLocationInfo = OS.tell();
138
139 if (!Prefix.empty())
140 OS << Prefix << ": ";
141
Chandler Carruth0ef89882011-09-26 11:25:30 +0000142 // Use a dedicated, simpler path for diagnostics without a valid location.
Chandler Carruthe6d1dff2011-09-26 11:38:46 +0000143 // This is important as if the location is missing, we may be emitting
144 // diagnostics in a context that lacks language options, a source manager, or
145 // other infrastructure necessary when emitting more rich diagnostics.
Chandler Carruth0ef89882011-09-26 11:25:30 +0000146 if (!Info.getLocation().isValid()) {
Chandler Carruth7eb84dc2011-10-15 22:49:21 +0000147 TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
Chandler Carruth1f3839e2011-10-15 22:57:29 +0000148 TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
149 OS.tell() - StartOfLocationInfo,
150 DiagOpts->MessageLength,
151 DiagOpts->ShowColors);
Chandler Carruth0ef89882011-09-26 11:25:30 +0000152 OS.flush();
153 return;
Chandler Carrutha6290042011-09-26 00:26:47 +0000154 }
155
Chandler Carruthe6d1dff2011-09-26 11:38:46 +0000156 // Assert that the rest of our infrastructure is setup properly.
157 assert(LangOpts && "Unexpected diagnostic outside source file processing");
158 assert(DiagOpts && "Unexpected diagnostic without options set");
159 assert(Info.hasSourceManager() &&
160 "Unexpected diagnostic with no source manager");
Chandler Carruth0ef89882011-09-26 11:25:30 +0000161 const SourceManager &SM = Info.getSourceManager();
Chandler Carruth67e2d512011-10-15 12:07:49 +0000162 TextDiagnostic TextDiag(OS, SM, *LangOpts, *DiagOpts,
Chandler Carruth03efd2e2011-10-15 22:39:16 +0000163 LastLoc, LastIncludeLoc, LastLevel);
Chandler Carruthe6d1dff2011-09-26 11:38:46 +0000164
Chandler Carruth7531f572011-10-15 23:54:09 +0000165 TextDiag.emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(),
166 Info.getRanges(),
167 llvm::makeArrayRef(Info.getFixItHints(),
168 Info.getNumFixItHints()));
Chandler Carruth0ef89882011-09-26 11:25:30 +0000169
Chandler Carruthf54a6142011-10-15 11:44:27 +0000170 // Cache the LastLoc from the TextDiagnostic printing.
Chandler Carruth03efd2e2011-10-15 22:39:16 +0000171 // FIXME: Rather than this, we should persist a TextDiagnostic object across
172 // diagnostics until the SourceManager changes. That will allow the
173 // TextDiagnostic object to form a 'session' of output where we can
174 // reasonably collapse redundant information.
Chandler Carruthf54a6142011-10-15 11:44:27 +0000175 LastLoc = FullSourceLoc(TextDiag.getLastLoc(), SM);
Chandler Carruthd6140402011-10-15 12:12:44 +0000176 LastIncludeLoc = FullSourceLoc(TextDiag.getLastIncludeLoc(), SM);
Chandler Carruth03efd2e2011-10-15 22:39:16 +0000177 LastLevel = TextDiag.getLastLevel();
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000178
Chris Lattnera03a5b52008-11-19 06:56:25 +0000179 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
Douglas Gregoraee526e2011-09-29 00:38:00 +0000181
182DiagnosticConsumer *
183TextDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
184 return new TextDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
185}