blob: fcefd4e358224cbf617499192bb4a204762e0e37 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Basic/SourceManager.h"
Daniel Dunbareace8742009-11-04 06:24:30 +000016#include "clang/Frontend/DiagnosticOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Lex/Lexer.h"
Chris Lattner037fb7f2009-05-05 22:03:18 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattnera03a5b52008-11-19 06:56:25 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000020#include "llvm/ADT/SmallString.h"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000021#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Torok Edwin603fca72009-06-04 07:18:23 +000024static const enum llvm::raw_ostream::Colors noteColor =
25 llvm::raw_ostream::BLACK;
26static const enum llvm::raw_ostream::Colors fixitColor =
27 llvm::raw_ostream::GREEN;
28static const enum llvm::raw_ostream::Colors caretColor =
29 llvm::raw_ostream::GREEN;
30static const enum llvm::raw_ostream::Colors warningColor =
31 llvm::raw_ostream::MAGENTA;
32static const enum llvm::raw_ostream::Colors errorColor = llvm::raw_ostream::RED;
33static const enum llvm::raw_ostream::Colors fatalColor = llvm::raw_ostream::RED;
34// used for changing only the bold attribute
35static const enum llvm::raw_ostream::Colors savedColor =
36 llvm::raw_ostream::SAVEDCOLOR;
37
Douglas Gregorfffd93f2009-05-01 21:53:04 +000038/// \brief Number of spaces to indent when word-wrapping.
39const unsigned WordWrapIndentation = 6;
40
Daniel Dunbareace8742009-11-04 06:24:30 +000041TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream &os,
Daniel Dunbaraea36412009-11-11 09:38:24 +000042 const DiagnosticOptions &diags,
43 bool _OwnsOutputStream)
Daniel Dunbareace8742009-11-04 06:24:30 +000044 : OS(os), LangOpts(0), DiagOpts(&diags),
Daniel Dunbaraea36412009-11-11 09:38:24 +000045 LastCaretDiagnosticWasNote(0),
46 OwnsOutputStream(_OwnsOutputStream) {
47}
48
49TextDiagnosticPrinter::~TextDiagnosticPrinter() {
50 if (OwnsOutputStream)
51 delete &OS;
Daniel Dunbareace8742009-11-04 06:24:30 +000052}
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054void TextDiagnosticPrinter::
Chris Lattnerb9c3f962009-01-27 07:57:44 +000055PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
56 if (Loc.isInvalid()) return;
Chris Lattner9dc1f532007-07-20 16:37:10 +000057
Chris Lattnerb9c3f962009-01-27 07:57:44 +000058 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattner9dc1f532007-07-20 16:37:10 +000059
Reid Spencer5f016e22007-07-11 17:01:13 +000060 // Print out the other include frames first.
Chris Lattnerb9c3f962009-01-27 07:57:44 +000061 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Chris Lattner5ce24c82009-04-21 03:57:54 +000062
Daniel Dunbareace8742009-11-04 06:24:30 +000063 if (DiagOpts->ShowLocation)
Chris Lattner5ce24c82009-04-21 03:57:54 +000064 OS << "In file included from " << PLoc.getFilename()
65 << ':' << PLoc.getLine() << ":\n";
66 else
67 OS << "In included file:\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000068}
69
70/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
71/// any characters in LineNo that intersect the SourceRange.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000072void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerb9c3f962009-01-27 07:57:44 +000073 const SourceManager &SM,
Chris Lattner3b4d5e92009-01-17 08:45:21 +000074 unsigned LineNo, FileID FID,
Gordon Henriksenaad69532008-08-09 19:58:22 +000075 std::string &CaretLine,
Nuno Lopesdb825682008-08-05 19:40:20 +000076 const std::string &SourceLine) {
Gordon Henriksenaad69532008-08-09 19:58:22 +000077 assert(CaretLine.size() == SourceLine.size() &&
78 "Expect a correspondence between source and caret line!");
Reid Spencer5f016e22007-07-11 17:01:13 +000079 if (!R.isValid()) return;
80
Chris Lattnerb9c3f962009-01-27 07:57:44 +000081 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
82 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +000083
Chris Lattner34837a52009-02-17 05:19:10 +000084 // If the End location and the start location are the same and are a macro
85 // location, then the range was something that came from a macro expansion
86 // or _Pragma. If this is an object-like macro, the best we can do is to
87 // highlight the range. If this is a function-like macro, we'd also like to
88 // highlight the arguments.
89 if (Begin == End && R.getEnd().isMacroID())
90 End = SM.getInstantiationRange(R.getEnd()).second;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +000091
Chris Lattner30fc9332009-02-04 01:06:56 +000092 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattnerb9c3f962009-01-27 07:57:44 +000093 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000094 return; // No intersection.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +000095
Chris Lattner30fc9332009-02-04 01:06:56 +000096 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattnerb9c3f962009-01-27 07:57:44 +000097 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000098 return; // No intersection.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +000099
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 // Compute the column number of the start.
101 unsigned StartColNo = 0;
102 if (StartLineNo == LineNo) {
Chris Lattner7da5aea2009-02-04 00:55:58 +0000103 StartColNo = SM.getInstantiationColumnNumber(Begin);
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 if (StartColNo) --StartColNo; // Zero base the col #.
105 }
106
107 // Pick the first non-whitespace column.
108 while (StartColNo < SourceLine.size() &&
109 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
110 ++StartColNo;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // Compute the column number of the end.
Gordon Henriksenaad69532008-08-09 19:58:22 +0000113 unsigned EndColNo = CaretLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 if (EndLineNo == LineNo) {
Chris Lattner7da5aea2009-02-04 00:55:58 +0000115 EndColNo = SM.getInstantiationColumnNumber(End);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 if (EndColNo) {
117 --EndColNo; // Zero base the col #.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000120 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 } else {
Gordon Henriksenaad69532008-08-09 19:58:22 +0000122 EndColNo = CaretLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
124 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000125
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 // Pick the last non-whitespace column.
Nuno Lopesdb825682008-08-05 19:40:20 +0000127 if (EndColNo <= SourceLine.size())
128 while (EndColNo-1 &&
129 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
130 --EndColNo;
131 else
132 EndColNo = SourceLine.size();
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // Fill the range with ~'s.
135 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesdb825682008-08-05 19:40:20 +0000136 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenaad69532008-08-09 19:58:22 +0000137 CaretLine[i] = '~';
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
Douglas Gregor47f71772009-05-01 23:32:58 +0000140/// \brief When the source code line we want to print is too long for
141/// the terminal, select the "interesting" region.
142static void SelectInterestingSourceRegion(std::string &SourceLine,
143 std::string &CaretLine,
144 std::string &FixItInsertionLine,
Douglas Gregorcfe1f9d2009-05-04 06:27:32 +0000145 unsigned EndOfCaretToken,
Douglas Gregor47f71772009-05-01 23:32:58 +0000146 unsigned Columns) {
147 if (CaretLine.size() > SourceLine.size())
148 SourceLine.resize(CaretLine.size(), ' ');
149
150 // Find the slice that we need to display the full caret line
151 // correctly.
152 unsigned CaretStart = 0, CaretEnd = CaretLine.size();
153 for (; CaretStart != CaretEnd; ++CaretStart)
154 if (!isspace(CaretLine[CaretStart]))
155 break;
156
157 for (; CaretEnd != CaretStart; --CaretEnd)
158 if (!isspace(CaretLine[CaretEnd - 1]))
159 break;
Douglas Gregorcfe1f9d2009-05-04 06:27:32 +0000160
161 // Make sure we don't chop the string shorter than the caret token
162 // itself.
163 if (CaretEnd < EndOfCaretToken)
164 CaretEnd = EndOfCaretToken;
165
Douglas Gregor844da342009-05-03 04:33:32 +0000166 // If we have a fix-it line, make sure the slice includes all of the
167 // fix-it information.
168 if (!FixItInsertionLine.empty()) {
169 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
170 for (; FixItStart != FixItEnd; ++FixItStart)
171 if (!isspace(FixItInsertionLine[FixItStart]))
172 break;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000173
Douglas Gregor844da342009-05-03 04:33:32 +0000174 for (; FixItEnd != FixItStart; --FixItEnd)
175 if (!isspace(FixItInsertionLine[FixItEnd - 1]))
176 break;
177
178 if (FixItStart < CaretStart)
179 CaretStart = FixItStart;
180 if (FixItEnd > CaretEnd)
181 CaretEnd = FixItEnd;
182 }
183
Douglas Gregor47f71772009-05-01 23:32:58 +0000184 // CaretLine[CaretStart, CaretEnd) contains all of the interesting
185 // parts of the caret line. While this slice is smaller than the
186 // number of columns we have, try to grow the slice to encompass
187 // more context.
188
189 // If the end of the interesting region comes before we run out of
190 // space in the terminal, start at the beginning of the line.
Douglas Gregorc95bd4d2009-05-15 18:05:24 +0000191 if (Columns > 3 && CaretEnd < Columns - 3)
Douglas Gregor47f71772009-05-01 23:32:58 +0000192 CaretStart = 0;
193
Douglas Gregorc95bd4d2009-05-15 18:05:24 +0000194 unsigned TargetColumns = Columns;
195 if (TargetColumns > 8)
196 TargetColumns -= 8; // Give us extra room for the ellipses.
Douglas Gregor47f71772009-05-01 23:32:58 +0000197 unsigned SourceLength = SourceLine.size();
Douglas Gregor2fb3ea32009-05-04 06:45:38 +0000198 while ((CaretEnd - CaretStart) < TargetColumns) {
Douglas Gregor47f71772009-05-01 23:32:58 +0000199 bool ExpandedRegion = false;
200 // Move the start of the interesting region left until we've
201 // pulled in something else interesting.
Douglas Gregor2fb3ea32009-05-04 06:45:38 +0000202 if (CaretStart == 1)
203 CaretStart = 0;
204 else if (CaretStart > 1) {
205 unsigned NewStart = CaretStart - 1;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000206
Douglas Gregor2fb3ea32009-05-04 06:45:38 +0000207 // Skip over any whitespace we see here; we're looking for
208 // another bit of interesting text.
209 while (NewStart && isspace(SourceLine[NewStart]))
210 --NewStart;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000211
Douglas Gregor2fb3ea32009-05-04 06:45:38 +0000212 // Skip over this bit of "interesting" text.
213 while (NewStart && !isspace(SourceLine[NewStart]))
214 --NewStart;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000215
Douglas Gregor2fb3ea32009-05-04 06:45:38 +0000216 // Move up to the non-whitespace character we just saw.
217 if (NewStart)
218 ++NewStart;
Douglas Gregor47f71772009-05-01 23:32:58 +0000219
220 // If we're still within our limit, update the starting
221 // position within the source/caret line.
Douglas Gregor2fb3ea32009-05-04 06:45:38 +0000222 if (CaretEnd - NewStart <= TargetColumns) {
Douglas Gregor47f71772009-05-01 23:32:58 +0000223 CaretStart = NewStart;
224 ExpandedRegion = true;
225 }
226 }
227
228 // Move the end of the interesting region right until we've
229 // pulled in something else interesting.
Daniel Dunbar1ef29d22009-05-03 23:04:40 +0000230 if (CaretEnd != SourceLength) {
Daniel Dunbar06d10722009-10-19 09:11:21 +0000231 assert(CaretEnd < SourceLength && "Unexpected caret position!");
Douglas Gregor47f71772009-05-01 23:32:58 +0000232 unsigned NewEnd = CaretEnd;
233
234 // Skip over any whitespace we see here; we're looking for
235 // another bit of interesting text.
Douglas Gregor1f0eb562009-05-18 22:09:16 +0000236 while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1]))
Douglas Gregor47f71772009-05-01 23:32:58 +0000237 ++NewEnd;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000238
Douglas Gregor47f71772009-05-01 23:32:58 +0000239 // Skip over this bit of "interesting" text.
Douglas Gregor1f0eb562009-05-18 22:09:16 +0000240 while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1]))
Douglas Gregor47f71772009-05-01 23:32:58 +0000241 ++NewEnd;
242
243 if (NewEnd - CaretStart <= TargetColumns) {
244 CaretEnd = NewEnd;
245 ExpandedRegion = true;
246 }
Douglas Gregor47f71772009-05-01 23:32:58 +0000247 }
Daniel Dunbar1ef29d22009-05-03 23:04:40 +0000248
249 if (!ExpandedRegion)
250 break;
Douglas Gregor47f71772009-05-01 23:32:58 +0000251 }
252
253 // [CaretStart, CaretEnd) is the slice we want. Update the various
254 // output lines to show only this slice, with two-space padding
255 // before the lines so that it looks nicer.
Douglas Gregor7d101f62009-05-03 04:12:51 +0000256 if (CaretEnd < SourceLine.size())
257 SourceLine.replace(CaretEnd, std::string::npos, "...");
Douglas Gregor2167de42009-05-03 15:24:25 +0000258 if (CaretEnd < CaretLine.size())
259 CaretLine.erase(CaretEnd, std::string::npos);
Douglas Gregor47f71772009-05-01 23:32:58 +0000260 if (FixItInsertionLine.size() > CaretEnd)
261 FixItInsertionLine.erase(CaretEnd, std::string::npos);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000262
Douglas Gregor47f71772009-05-01 23:32:58 +0000263 if (CaretStart > 2) {
Douglas Gregor7d101f62009-05-03 04:12:51 +0000264 SourceLine.replace(0, CaretStart, " ...");
265 CaretLine.replace(0, CaretStart, " ");
Douglas Gregor47f71772009-05-01 23:32:58 +0000266 if (FixItInsertionLine.size() >= CaretStart)
Douglas Gregor7d101f62009-05-03 04:12:51 +0000267 FixItInsertionLine.replace(0, CaretStart, " ");
Douglas Gregor47f71772009-05-01 23:32:58 +0000268 }
269}
270
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000271void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner676f0242009-02-20 00:25:28 +0000272 SourceRange *Ranges,
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000273 unsigned NumRanges,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000274 SourceManager &SM,
275 const CodeModificationHint *Hints,
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000276 unsigned NumHints,
Douglas Gregor47f71772009-05-01 23:32:58 +0000277 unsigned Columns) {
Daniel Dunbarefcbe942009-11-05 02:42:12 +0000278 assert(LangOpts && "Unexpected diagnostic outside source file processing");
Chris Lattner55dcef02009-02-17 08:44:50 +0000279 assert(!Loc.isInvalid() && "must have a valid source location here");
Chris Lattner037fb7f2009-05-05 22:03:18 +0000280
281 // If this is a macro ID, first emit information about where this was
Chris Lattner2e77aa12009-12-04 07:06:35 +0000282 // instantiated (recursively) then emit information about where the token was
Chris Lattner037fb7f2009-05-05 22:03:18 +0000283 // spelled from.
Chris Lattner55dcef02009-02-17 08:44:50 +0000284 if (!Loc.isFileID()) {
Chris Lattner609b3ab2009-02-18 18:50:45 +0000285 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Chris Lattner037fb7f2009-05-05 22:03:18 +0000286 // FIXME: Map ranges?
Douglas Gregor2cc2b9c2009-05-06 04:43:47 +0000287 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns);
Chris Lattner676f0242009-02-20 00:25:28 +0000288
Chris Lattner2e77aa12009-12-04 07:06:35 +0000289 // Map the location.
Chris Lattner037fb7f2009-05-05 22:03:18 +0000290 Loc = SM.getImmediateSpellingLoc(Loc);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000291
Chris Lattner676f0242009-02-20 00:25:28 +0000292 // Map the ranges.
293 for (unsigned i = 0; i != NumRanges; ++i) {
294 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
Chris Lattner037fb7f2009-05-05 22:03:18 +0000295 if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S);
296 if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E);
Chris Lattner676f0242009-02-20 00:25:28 +0000297 Ranges[i] = SourceRange(S, E);
298 }
Chris Lattner2e77aa12009-12-04 07:06:35 +0000299
300 // Get the pretty name, according to #line directives etc.
301 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
302
303 // If this diagnostic is not in the main file, print out the "included from"
304 // lines.
305 if (LastWarningLoc != PLoc.getIncludeLoc()) {
306 LastWarningLoc = PLoc.getIncludeLoc();
307 PrintIncludeStack(LastWarningLoc, SM);
308 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000309
Daniel Dunbareace8742009-11-04 06:24:30 +0000310 if (DiagOpts->ShowLocation) {
Chris Lattner5ce24c82009-04-21 03:57:54 +0000311 // Emit the file/line/column that this expansion came from.
Chris Lattner2e77aa12009-12-04 07:06:35 +0000312 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':';
Daniel Dunbareace8742009-11-04 06:24:30 +0000313 if (DiagOpts->ShowColumn)
Chris Lattner2e77aa12009-12-04 07:06:35 +0000314 OS << PLoc.getColumn() << ':';
Chris Lattner5ce24c82009-04-21 03:57:54 +0000315 OS << ' ';
316 }
317 OS << "note: instantiated from:\n";
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000318
Douglas Gregor2cc2b9c2009-05-06 04:43:47 +0000319 EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns);
Chris Lattner037fb7f2009-05-05 22:03:18 +0000320 return;
Chris Lattner55dcef02009-02-17 08:44:50 +0000321 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000322
Chris Lattnerb88af812009-02-17 07:51:53 +0000323 // Decompose the location into a FID/Offset pair.
324 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
325 FileID FID = LocInfo.first;
326 unsigned FileOffset = LocInfo.second;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000327
Chris Lattnerb88af812009-02-17 07:51:53 +0000328 // Get information about the buffer it points into.
329 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID);
330 const char *BufStart = BufferInfo.first;
Chris Lattnerb88af812009-02-17 07:51:53 +0000331
332 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000333 unsigned CaretEndColNo
Douglas Gregorcfe1f9d2009-05-04 06:27:32 +0000334 = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts);
335
Chris Lattner94f55782009-02-17 07:38:37 +0000336 // Rewind from the current position to the start of the line.
Chris Lattnerb88af812009-02-17 07:51:53 +0000337 const char *TokPtr = BufStart+FileOffset;
338 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000339
340
Chris Lattner94f55782009-02-17 07:38:37 +0000341 // Compute the line end. Scan forward from the error position to the end of
342 // the line.
Chris Lattnerb88af812009-02-17 07:51:53 +0000343 const char *LineEnd = TokPtr;
Chris Lattnercd1148b2009-03-08 08:11:22 +0000344 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
Chris Lattner94f55782009-02-17 07:38:37 +0000345 ++LineEnd;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000346
Daniel Dunbar06d10722009-10-19 09:11:21 +0000347 // FIXME: This shouldn't be necessary, but the CaretEndColNo can extend past
348 // the source line length as currently being computed. See
349 // test/Misc/message-length.c.
350 CaretEndColNo = std::min(CaretEndColNo, unsigned(LineEnd - LineStart));
351
Chris Lattner94f55782009-02-17 07:38:37 +0000352 // Copy the line of code into an std::string for ease of manipulation.
353 std::string SourceLine(LineStart, LineEnd);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000354
Chris Lattner94f55782009-02-17 07:38:37 +0000355 // Create a line for the caret that is filled with spaces that is the same
356 // length as the line of source code.
357 std::string CaretLine(LineEnd-LineStart, ' ');
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000358
Chris Lattner94f55782009-02-17 07:38:37 +0000359 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000360 if (NumRanges) {
Chris Lattnerb88af812009-02-17 07:51:53 +0000361 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000362
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000363 for (unsigned i = 0, e = NumRanges; i != e; ++i)
364 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattnerb88af812009-02-17 07:51:53 +0000365 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000366
Chris Lattner94f55782009-02-17 07:38:37 +0000367 // Next, insert the caret itself.
368 if (ColNo-1 < CaretLine.size())
369 CaretLine[ColNo-1] = '^';
370 else
371 CaretLine.push_back('^');
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000372
Chris Lattner94f55782009-02-17 07:38:37 +0000373 // Scan the source line, looking for tabs. If we find any, manually expand
Chris Lattner52388f92010-01-13 03:06:50 +0000374 // them to spaces and update the CaretLine to match.
Chris Lattner94f55782009-02-17 07:38:37 +0000375 for (unsigned i = 0; i != SourceLine.size(); ++i) {
376 if (SourceLine[i] != '\t') continue;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000377
Chris Lattner94f55782009-02-17 07:38:37 +0000378 // Replace this tab with at least one space.
379 SourceLine[i] = ' ';
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000380
Chris Lattner94f55782009-02-17 07:38:37 +0000381 // Compute the number of spaces we need to insert.
Chris Lattner52388f92010-01-13 03:06:50 +0000382 unsigned TabStop = DiagOpts->TabStop;
383 assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
384 "Invalid -ftabstop value");
Chris Lattner124fca52010-01-09 21:54:33 +0000385 unsigned NumSpaces = ((i+TabStop)/TabStop * TabStop) - (i+1);
386 assert(NumSpaces < TabStop && "Invalid computation of space amt");
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000387
Chris Lattner94f55782009-02-17 07:38:37 +0000388 // Insert spaces into the SourceLine.
389 SourceLine.insert(i+1, NumSpaces, ' ');
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000390
Chris Lattner94f55782009-02-17 07:38:37 +0000391 // Insert spaces or ~'s into CaretLine.
392 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
393 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000394
Chris Lattner770dbf02009-04-28 22:33:16 +0000395 // If we are in -fdiagnostics-print-source-range-info mode, we are trying to
396 // produce easily machine parsable output. Add a space before the source line
397 // and the caret to make it trivial to tell the main diagnostic line from what
398 // the user is intended to see.
Daniel Dunbareace8742009-11-04 06:24:30 +0000399 if (DiagOpts->ShowSourceRanges) {
Chris Lattner770dbf02009-04-28 22:33:16 +0000400 SourceLine = ' ' + SourceLine;
401 CaretLine = ' ' + CaretLine;
402 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000403
Douglas Gregor47f71772009-05-01 23:32:58 +0000404 std::string FixItInsertionLine;
Daniel Dunbareace8742009-11-04 06:24:30 +0000405 if (NumHints && DiagOpts->ShowFixits) {
Chris Lattneraa5bf2e2009-04-19 07:44:08 +0000406 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints;
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000407 Hint != LastHint; ++Hint) {
408 if (Hint->InsertionLoc.isValid()) {
409 // We have an insertion hint. Determine whether the inserted
410 // code is on the same line as the caret.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000411 std::pair<FileID, unsigned> HintLocInfo
Chris Lattner7b5b5b42009-03-02 20:58:48 +0000412 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000413 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
414 SM.getLineNumber(FID, FileOffset)) {
415 // Insert the new code into the line just below the code
416 // that the user wrote.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000417 unsigned HintColNo
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000418 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000419 unsigned LastColumnModified
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000420 = HintColNo - 1 + Hint->CodeToInsert.size();
Douglas Gregor47f71772009-05-01 23:32:58 +0000421 if (LastColumnModified > FixItInsertionLine.size())
422 FixItInsertionLine.resize(LastColumnModified, ' ');
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000423 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(),
Douglas Gregor47f71772009-05-01 23:32:58 +0000424 FixItInsertionLine.begin() + HintColNo - 1);
Douglas Gregor844da342009-05-03 04:33:32 +0000425 } else {
426 FixItInsertionLine.clear();
427 break;
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000428 }
429 }
430 }
Douglas Gregor47f71772009-05-01 23:32:58 +0000431 }
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000432
Douglas Gregor47f71772009-05-01 23:32:58 +0000433 // If the source line is too long for our terminal, select only the
434 // "interesting" source region within that line.
435 if (Columns && SourceLine.size() > Columns)
436 SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
Douglas Gregorcfe1f9d2009-05-04 06:27:32 +0000437 CaretEndColNo, Columns);
Douglas Gregor47f71772009-05-01 23:32:58 +0000438
Douglas Gregor47f71772009-05-01 23:32:58 +0000439 // Finally, remove any blank spaces from the end of CaretLine.
440 while (CaretLine[CaretLine.size()-1] == ' ')
441 CaretLine.erase(CaretLine.end()-1);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000442
Douglas Gregor47f71772009-05-01 23:32:58 +0000443 // Emit what we have computed.
444 OS << SourceLine << '\n';
Torok Edwin603fca72009-06-04 07:18:23 +0000445
Daniel Dunbareace8742009-11-04 06:24:30 +0000446 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000447 OS.changeColor(caretColor, true);
Douglas Gregor47f71772009-05-01 23:32:58 +0000448 OS << CaretLine << '\n';
Daniel Dunbareace8742009-11-04 06:24:30 +0000449 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000450 OS.resetColor();
Douglas Gregor47f71772009-05-01 23:32:58 +0000451
452 if (!FixItInsertionLine.empty()) {
Daniel Dunbareace8742009-11-04 06:24:30 +0000453 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000454 // Print fixit line in color
455 OS.changeColor(fixitColor, false);
Daniel Dunbareace8742009-11-04 06:24:30 +0000456 if (DiagOpts->ShowSourceRanges)
Douglas Gregor47f71772009-05-01 23:32:58 +0000457 OS << ' ';
458 OS << FixItInsertionLine << '\n';
Daniel Dunbareace8742009-11-04 06:24:30 +0000459 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000460 OS.resetColor();
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000461 }
Chris Lattner94f55782009-02-17 07:38:37 +0000462}
463
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000464/// \brief Skip over whitespace in the string, starting at the given
465/// index.
466///
467/// \returns The index of the first non-whitespace character that is
468/// greater than or equal to Idx or, if no such character exists,
469/// returns the end of the string.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000470static unsigned skipWhitespace(unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +0000471 const llvm::SmallVectorImpl<char> &Str,
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000472 unsigned Length) {
473 while (Idx < Length && isspace(Str[Idx]))
474 ++Idx;
475 return Idx;
476}
477
478/// \brief If the given character is the start of some kind of
479/// balanced punctuation (e.g., quotes or parentheses), return the
480/// character that will terminate the punctuation.
481///
482/// \returns The ending punctuation character, if any, or the NULL
483/// character if the input character does not start any punctuation.
484static inline char findMatchingPunctuation(char c) {
485 switch (c) {
486 case '\'': return '\'';
487 case '`': return '\'';
488 case '"': return '"';
489 case '(': return ')';
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000490 case '[': return ']';
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000491 case '{': return '}';
492 default: break;
493 }
494
495 return 0;
496}
497
498/// \brief Find the end of the word starting at the given offset
499/// within a string.
500///
501/// \returns the index pointing one character past the end of the
502/// word.
Daniel Dunbareae18f82009-12-06 09:56:18 +0000503static unsigned findEndOfWord(unsigned Start,
504 const llvm::SmallVectorImpl<char> &Str,
505 unsigned Length, unsigned Column,
506 unsigned Columns) {
507 assert(Start < Str.size() && "Invalid start position!");
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000508 unsigned End = Start + 1;
509
Daniel Dunbareae18f82009-12-06 09:56:18 +0000510 // If we are already at the end of the string, take that as the word.
511 if (End == Str.size())
512 return End;
513
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000514 // Determine if the start of the string is actually opening
515 // punctuation, e.g., a quote or parentheses.
516 char EndPunct = findMatchingPunctuation(Str[Start]);
517 if (!EndPunct) {
518 // This is a normal word. Just find the first space character.
519 while (End < Length && !isspace(Str[End]))
520 ++End;
521 return End;
522 }
523
524 // We have the start of a balanced punctuation sequence (quotes,
525 // parentheses, etc.). Determine the full sequence is.
526 llvm::SmallVector<char, 16> PunctuationEndStack;
527 PunctuationEndStack.push_back(EndPunct);
528 while (End < Length && !PunctuationEndStack.empty()) {
529 if (Str[End] == PunctuationEndStack.back())
530 PunctuationEndStack.pop_back();
531 else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
532 PunctuationEndStack.push_back(SubEndPunct);
533
534 ++End;
535 }
536
537 // Find the first space character after the punctuation ended.
538 while (End < Length && !isspace(Str[End]))
539 ++End;
540
541 unsigned PunctWordLength = End - Start;
542 if (// If the word fits on this line
543 Column + PunctWordLength <= Columns ||
544 // ... or the word is "short enough" to take up the next line
545 // without too much ugly white space
546 PunctWordLength < Columns/3)
547 return End; // Take the whole thing as a single "word".
548
549 // The whole quoted/parenthesized string is too long to print as a
550 // single "word". Instead, find the "word" that starts just after
551 // the punctuation and use that end-point instead. This will recurse
552 // until it finds something small enough to consider a word.
553 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
554}
555
556/// \brief Print the given string to a stream, word-wrapping it to
557/// some number of columns in the process.
558///
559/// \brief OS the stream to which the word-wrapping string will be
560/// emitted.
561///
562/// \brief Str the string to word-wrap and output.
563///
564/// \brief Columns the number of columns to word-wrap to.
565///
566/// \brief Column the column number at which the first character of \p
567/// Str will be printed. This will be non-zero when part of the first
568/// line has already been printed.
569///
570/// \brief Indentation the number of spaces to indent any lines beyond
571/// the first line.
572///
573/// \returns true if word-wrapping was required, or false if the
574/// string fit on the first line.
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000575static bool PrintWordWrapped(llvm::raw_ostream &OS,
Mike Stump1eb44332009-09-09 15:08:12 +0000576 const llvm::SmallVectorImpl<char> &Str,
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000577 unsigned Columns,
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000578 unsigned Column = 0,
579 unsigned Indentation = WordWrapIndentation) {
580 unsigned Length = Str.size();
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000581
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000582 // If there is a newline in this message somewhere, find that
583 // newline and split the message into the part before the newline
584 // (which will be word-wrapped) and the part from the newline one
585 // (which will be emitted unchanged).
586 for (unsigned I = 0; I != Length; ++I)
587 if (Str[I] == '\n') {
588 Length = I;
589 break;
590 }
591
592 // The string used to indent each line.
593 llvm::SmallString<16> IndentStr;
594 IndentStr.assign(Indentation, ' ');
595 bool Wrapped = false;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000596 for (unsigned WordStart = 0, WordEnd; WordStart < Length;
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000597 WordStart = WordEnd) {
598 // Find the beginning of the next word.
599 WordStart = skipWhitespace(WordStart, Str, Length);
600 if (WordStart == Length)
601 break;
602
603 // Find the end of this word.
604 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000605
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000606 // Does this word fit on the current line?
607 unsigned WordLength = WordEnd - WordStart;
608 if (Column + WordLength < Columns) {
609 // This word fits on the current line; print it there.
610 if (WordStart) {
611 OS << ' ';
612 Column += 1;
613 }
614 OS.write(&Str[WordStart], WordLength);
615 Column += WordLength;
616 continue;
617 }
618
619 // This word does not fit on the current line, so wrap to the next
620 // line.
Douglas Gregor44cf08e2009-05-03 03:52:38 +0000621 OS << '\n';
622 OS.write(&IndentStr[0], Indentation);
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000623 OS.write(&Str[WordStart], WordLength);
624 Column = Indentation + WordLength;
625 Wrapped = true;
626 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000627
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000628 if (Length == Str.size())
629 return Wrapped; // We're done.
630
631 // There is a newline in the message, followed by something that
632 // will not be word-wrapped. Print that.
633 OS.write(&Str[Length], Str.size() - Length);
634 return true;
635}
Chris Lattner94f55782009-02-17 07:38:37 +0000636
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000637void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
Chris Lattner0a14eee2008-11-18 07:04:44 +0000638 const DiagnosticInfo &Info) {
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000639 // Keeps track of the the starting position of the location
640 // information (e.g., "foo.c:10:4:") that precedes the error
641 // message. We use this information to determine how long the
642 // file+line+column number prefix is.
643 uint64_t StartOfLocationInfo = OS.tell();
644
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000645 // If the location is specified, print out a file/line/col and include trace
646 // if enabled.
647 if (Info.getLocation().isValid()) {
Ted Kremenek05f39572009-01-28 20:47:47 +0000648 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000649 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
650 unsigned LineNo = PLoc.getLine();
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 // First, if this diagnostic is not in the main file, print out the
653 // "included from" lines.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000654 if (LastWarningLoc != PLoc.getIncludeLoc()) {
655 LastWarningLoc = PLoc.getIncludeLoc();
656 PrintIncludeStack(LastWarningLoc, SM);
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000657 StartOfLocationInfo = OS.tell();
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000659
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000660 // Compute the column number.
Daniel Dunbareace8742009-11-04 06:24:30 +0000661 if (DiagOpts->ShowLocation) {
662 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000663 OS.changeColor(savedColor, true);
Steve Naroffe0c4d892009-12-05 02:14:08 +0000664
665 // Emit a Visual Studio compatible line number syntax.
Steve Naroff0304c6c2009-12-05 12:23:07 +0000666 if (LangOpts && LangOpts->Microsoft) {
Steve Naroffe0c4d892009-12-05 02:14:08 +0000667 OS << PLoc.getFilename() << '(' << LineNo << ')';
668 OS << " : ";
669 } else {
670 OS << PLoc.getFilename() << ':' << LineNo << ':';
671 if (DiagOpts->ShowColumn)
672 if (unsigned ColNo = PLoc.getColumn())
673 OS << ColNo << ':';
674 }
Daniel Dunbareace8742009-11-04 06:24:30 +0000675 if (DiagOpts->ShowSourceRanges && Info.getNumRanges()) {
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000676 FileID CaretFileID =
677 SM.getFileID(SM.getInstantiationLoc(Info.getLocation()));
678 bool PrintedRange = false;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000679
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000680 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) {
Chris Lattner74548e62009-04-19 22:24:10 +0000681 // Ignore invalid ranges.
682 if (!Info.getRange(i).isValid()) continue;
683
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000684 SourceLocation B = Info.getRange(i).getBegin();
685 SourceLocation E = Info.getRange(i).getEnd();
Chris Lattner81ebe9b2009-06-15 05:18:27 +0000686 B = SM.getInstantiationLoc(B);
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000687 E = SM.getInstantiationLoc(E);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000688
Chris Lattner81ebe9b2009-06-15 05:18:27 +0000689 // If the End location and the start location are the same and are a
690 // macro location, then the range was something that came from a macro
691 // expansion or _Pragma. If this is an object-like macro, the best we
692 // can do is to highlight the range. If this is a function-like
693 // macro, we'd also like to highlight the arguments.
694 if (B == E && Info.getRange(i).getEnd().isMacroID())
695 E = SM.getInstantiationRange(Info.getRange(i).getEnd()).second;
696
697 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000698 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000699
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000700 // If the start or end of the range is in another file, just discard
701 // it.
702 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
703 continue;
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000704
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000705 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000706 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000707
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000708 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
709 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
710 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
711 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}';
712 PrintedRange = true;
713 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000714
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000715 if (PrintedRange)
716 OS << ':';
717 }
Chris Lattnerb8bf65e2009-01-30 17:41:53 +0000718 OS << ' ';
Daniel Dunbareace8742009-11-04 06:24:30 +0000719 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000720 OS.resetColor();
721 }
722 }
723
Daniel Dunbareace8742009-11-04 06:24:30 +0000724 if (DiagOpts->ShowColors) {
Torok Edwin603fca72009-06-04 07:18:23 +0000725 // Print diagnostic category in bold and color
726 switch (Level) {
727 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
728 case Diagnostic::Note: OS.changeColor(noteColor, true); break;
729 case Diagnostic::Warning: OS.changeColor(warningColor, true); break;
730 case Diagnostic::Error: OS.changeColor(errorColor, true); break;
731 case Diagnostic::Fatal: OS.changeColor(fatalColor, true); break;
Chris Lattnerb8bf65e2009-01-30 17:41:53 +0000732 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000734
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 switch (Level) {
Chris Lattner41327582009-02-06 03:57:44 +0000736 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman165b9542008-04-17 18:06:57 +0000737 case Diagnostic::Note: OS << "note: "; break;
738 case Diagnostic::Warning: OS << "warning: "; break;
739 case Diagnostic::Error: OS << "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +0000740 case Diagnostic::Fatal: OS << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 }
Torok Edwin603fca72009-06-04 07:18:23 +0000742
Daniel Dunbareace8742009-11-04 06:24:30 +0000743 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000744 OS.resetColor();
745
Chris Lattnerf4c83962008-11-19 06:51:40 +0000746 llvm::SmallString<100> OutStr;
747 Info.FormatDiagnostic(OutStr);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000748
Daniel Dunbareace8742009-11-04 06:24:30 +0000749 if (DiagOpts->ShowOptionNames)
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000750 if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) {
751 OutStr += " [-W";
752 OutStr += Opt;
753 OutStr += ']';
754 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000755
Daniel Dunbareace8742009-11-04 06:24:30 +0000756 if (DiagOpts->ShowColors) {
Torok Edwin603fca72009-06-04 07:18:23 +0000757 // Print warnings, errors and fatal errors in bold, no color
758 switch (Level) {
759 case Diagnostic::Warning: OS.changeColor(savedColor, true); break;
760 case Diagnostic::Error: OS.changeColor(savedColor, true); break;
761 case Diagnostic::Fatal: OS.changeColor(savedColor, true); break;
762 default: break; //don't bold notes
763 }
764 }
765
Daniel Dunbareace8742009-11-04 06:24:30 +0000766 if (DiagOpts->MessageLength) {
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000767 // We will be word-wrapping the error message, so compute the
768 // column number where we currently are (after printing the
769 // location information).
770 unsigned Column = OS.tell() - StartOfLocationInfo;
Daniel Dunbareace8742009-11-04 06:24:30 +0000771 PrintWordWrapped(OS, OutStr, DiagOpts->MessageLength, Column);
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000772 } else {
773 OS.write(OutStr.begin(), OutStr.size());
774 }
Chris Lattnerf4c83962008-11-19 06:51:40 +0000775 OS << '\n';
Daniel Dunbareace8742009-11-04 06:24:30 +0000776 if (DiagOpts->ShowColors)
Torok Edwin603fca72009-06-04 07:18:23 +0000777 OS.resetColor();
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000778
Douglas Gregordf667e72009-03-10 20:44:00 +0000779 // If caret diagnostics are enabled and we have location, we want to
780 // emit the caret. However, we only do this if the location moved
781 // from the last diagnostic, if the last diagnostic was a note that
782 // was part of a different warning or error diagnostic, or if the
783 // diagnostic has ranges. We don't want to emit the same caret
784 // multiple times if one loc has multiple diagnostics.
Daniel Dunbareace8742009-11-04 06:24:30 +0000785 if (DiagOpts->ShowCarets && Info.getLocation().isValid() &&
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000786 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
Douglas Gregordf667e72009-03-10 20:44:00 +0000787 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) ||
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000788 Info.getNumCodeModificationHints())) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000789 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000790 LastLoc = Info.getLocation();
Douglas Gregordf667e72009-03-10 20:44:00 +0000791 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000792
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000793 // Get the ranges into a local array we can hack on.
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000794 SourceRange Ranges[20];
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000795 unsigned NumRanges = Info.getNumRanges();
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000796 assert(NumRanges < 20 && "Out of space");
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000797 for (unsigned i = 0; i != NumRanges; ++i)
798 Ranges[i] = Info.getRange(i);
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000799
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000800 unsigned NumHints = Info.getNumCodeModificationHints();
801 for (unsigned idx = 0; idx < NumHints; ++idx) {
802 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx);
803 if (Hint.RemoveRange.isValid()) {
804 assert(NumRanges < 20 && "Out of space");
805 Ranges[NumRanges++] = Hint.RemoveRange;
806 }
807 }
808
809 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
810 Info.getCodeModificationHints(),
Douglas Gregorfffd93f2009-05-01 21:53:04 +0000811 Info.getNumCodeModificationHints(),
Daniel Dunbareace8742009-11-04 06:24:30 +0000812 DiagOpts->MessageLength);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 }
Daniel Dunbarcbff0dc2009-09-07 23:07:56 +0000814
Chris Lattnera03a5b52008-11-19 06:56:25 +0000815 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +0000816}