blob: 14769c187393871bd8d359012077dc720d27c936 [file] [log] [blame]
Bill Wendling37b1dde2007-06-07 09:34:54 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling37b1dde2007-06-07 09:34:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar51adf582009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticPrinter.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000015#include "clang/Basic/SourceManager.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000016#include "clang/Lex/Lexer.h"
Chris Lattnerc45529b2009-05-05 22:03:18 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattner23be0672008-11-19 06:51:40 +000019#include "llvm/ADT/SmallString.h"
Douglas Gregor87f95b02009-02-26 21:00:50 +000020#include <algorithm>
Bill Wendling37b1dde2007-06-07 09:34:54 +000021using namespace clang;
22
Torok Edwinc91b6e02009-06-04 07:18:23 +000023static const enum llvm::raw_ostream::Colors noteColor =
24 llvm::raw_ostream::BLACK;
25static const enum llvm::raw_ostream::Colors fixitColor =
26 llvm::raw_ostream::GREEN;
27static const enum llvm::raw_ostream::Colors caretColor =
28 llvm::raw_ostream::GREEN;
29static const enum llvm::raw_ostream::Colors warningColor =
30 llvm::raw_ostream::MAGENTA;
31static const enum llvm::raw_ostream::Colors errorColor = llvm::raw_ostream::RED;
32static const enum llvm::raw_ostream::Colors fatalColor = llvm::raw_ostream::RED;
33// used for changing only the bold attribute
34static const enum llvm::raw_ostream::Colors savedColor =
35 llvm::raw_ostream::SAVEDCOLOR;
36
Douglas Gregor48185532009-05-01 21:53:04 +000037/// \brief Number of spaces to indent when word-wrapping.
38const unsigned WordWrapIndentation = 6;
39
Bill Wendling37b1dde2007-06-07 09:34:54 +000040void TextDiagnosticPrinter::
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000041PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
42 if (Loc.isInvalid()) return;
Chris Lattnerdc5c0552007-07-20 16:37:10 +000043
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000044 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattnerdc5c0552007-07-20 16:37:10 +000045
Bill Wendling37b1dde2007-06-07 09:34:54 +000046 // Print out the other include frames first.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000047 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Chris Lattner92b29b2f2009-04-21 03:57:54 +000048
49 if (ShowLocation)
50 OS << "In file included from " << PLoc.getFilename()
51 << ':' << PLoc.getLine() << ":\n";
52 else
53 OS << "In included file:\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000054}
55
Bill Wendling37b1dde2007-06-07 09:34:54 +000056/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
57/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000058void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000059 const SourceManager &SM,
Chris Lattner71dc14b2009-01-17 08:45:21 +000060 unsigned LineNo, FileID FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000061 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000062 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000063 assert(CaretLine.size() == SourceLine.size() &&
64 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000065 if (!R.isValid()) return;
66
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000067 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
68 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
Daniel Dunbar49f0e802009-09-07 23:07:56 +000069
Chris Lattnerb42700f2009-02-17 05:19:10 +000070 // If the End location and the start location are the same and are a macro
71 // location, then the range was something that came from a macro expansion
72 // or _Pragma. If this is an object-like macro, the best we can do is to
73 // highlight the range. If this is a function-like macro, we'd also like to
74 // highlight the arguments.
75 if (Begin == End && R.getEnd().isMacroID())
76 End = SM.getInstantiationRange(R.getEnd()).second;
Daniel Dunbar49f0e802009-09-07 23:07:56 +000077
Chris Lattner88ea93e2009-02-04 01:06:56 +000078 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000079 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000080 return; // No intersection.
Daniel Dunbar49f0e802009-09-07 23:07:56 +000081
Chris Lattner88ea93e2009-02-04 01:06:56 +000082 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000083 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000084 return; // No intersection.
Daniel Dunbar49f0e802009-09-07 23:07:56 +000085
Bill Wendling37b1dde2007-06-07 09:34:54 +000086 // Compute the column number of the start.
87 unsigned StartColNo = 0;
88 if (StartLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +000089 StartColNo = SM.getInstantiationColumnNumber(Begin);
Bill Wendling37b1dde2007-06-07 09:34:54 +000090 if (StartColNo) --StartColNo; // Zero base the col #.
91 }
92
93 // Pick the first non-whitespace column.
94 while (StartColNo < SourceLine.size() &&
95 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
96 ++StartColNo;
Daniel Dunbar49f0e802009-09-07 23:07:56 +000097
Bill Wendling37b1dde2007-06-07 09:34:54 +000098 // Compute the column number of the end.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000099 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000100 if (EndLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +0000101 EndColNo = SM.getInstantiationColumnNumber(End);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000102 if (EndColNo) {
103 --EndColNo; // Zero base the col #.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000104
Bill Wendling37b1dde2007-06-07 09:34:54 +0000105 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000106 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000107 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000108 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000109 }
110 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000111
Bill Wendling37b1dde2007-06-07 09:34:54 +0000112 // Pick the last non-whitespace column.
Nuno Lopesd86aa932008-08-05 19:40:20 +0000113 if (EndColNo <= SourceLine.size())
114 while (EndColNo-1 &&
115 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
116 --EndColNo;
117 else
118 EndColNo = SourceLine.size();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000119
Bill Wendling37b1dde2007-06-07 09:34:54 +0000120 // Fill the range with ~'s.
121 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd86aa932008-08-05 19:40:20 +0000122 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000123 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000124}
125
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000126/// \brief When the source code line we want to print is too long for
127/// the terminal, select the "interesting" region.
128static void SelectInterestingSourceRegion(std::string &SourceLine,
129 std::string &CaretLine,
130 std::string &FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000131 unsigned EndOfCaretToken,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000132 unsigned Columns) {
133 if (CaretLine.size() > SourceLine.size())
134 SourceLine.resize(CaretLine.size(), ' ');
135
136 // Find the slice that we need to display the full caret line
137 // correctly.
138 unsigned CaretStart = 0, CaretEnd = CaretLine.size();
139 for (; CaretStart != CaretEnd; ++CaretStart)
140 if (!isspace(CaretLine[CaretStart]))
141 break;
142
143 for (; CaretEnd != CaretStart; --CaretEnd)
144 if (!isspace(CaretLine[CaretEnd - 1]))
145 break;
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000146
147 // Make sure we don't chop the string shorter than the caret token
148 // itself.
149 if (CaretEnd < EndOfCaretToken)
150 CaretEnd = EndOfCaretToken;
151
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000152 // If we have a fix-it line, make sure the slice includes all of the
153 // fix-it information.
154 if (!FixItInsertionLine.empty()) {
155 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
156 for (; FixItStart != FixItEnd; ++FixItStart)
157 if (!isspace(FixItInsertionLine[FixItStart]))
158 break;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000159
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000160 for (; FixItEnd != FixItStart; --FixItEnd)
161 if (!isspace(FixItInsertionLine[FixItEnd - 1]))
162 break;
163
164 if (FixItStart < CaretStart)
165 CaretStart = FixItStart;
166 if (FixItEnd > CaretEnd)
167 CaretEnd = FixItEnd;
168 }
169
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000170 // CaretLine[CaretStart, CaretEnd) contains all of the interesting
171 // parts of the caret line. While this slice is smaller than the
172 // number of columns we have, try to grow the slice to encompass
173 // more context.
174
175 // If the end of the interesting region comes before we run out of
176 // space in the terminal, start at the beginning of the line.
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000177 if (Columns > 3 && CaretEnd < Columns - 3)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000178 CaretStart = 0;
179
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000180 unsigned TargetColumns = Columns;
181 if (TargetColumns > 8)
182 TargetColumns -= 8; // Give us extra room for the ellipses.
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000183 unsigned SourceLength = SourceLine.size();
Douglas Gregor006fd382009-05-04 06:45:38 +0000184 while ((CaretEnd - CaretStart) < TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000185 bool ExpandedRegion = false;
186 // Move the start of the interesting region left until we've
187 // pulled in something else interesting.
Douglas Gregor006fd382009-05-04 06:45:38 +0000188 if (CaretStart == 1)
189 CaretStart = 0;
190 else if (CaretStart > 1) {
191 unsigned NewStart = CaretStart - 1;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000192
Douglas Gregor006fd382009-05-04 06:45:38 +0000193 // Skip over any whitespace we see here; we're looking for
194 // another bit of interesting text.
195 while (NewStart && isspace(SourceLine[NewStart]))
196 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000197
Douglas Gregor006fd382009-05-04 06:45:38 +0000198 // Skip over this bit of "interesting" text.
199 while (NewStart && !isspace(SourceLine[NewStart]))
200 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000201
Douglas Gregor006fd382009-05-04 06:45:38 +0000202 // Move up to the non-whitespace character we just saw.
203 if (NewStart)
204 ++NewStart;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000205
206 // If we're still within our limit, update the starting
207 // position within the source/caret line.
Douglas Gregor006fd382009-05-04 06:45:38 +0000208 if (CaretEnd - NewStart <= TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000209 CaretStart = NewStart;
210 ExpandedRegion = true;
211 }
212 }
213
214 // Move the end of the interesting region right until we've
215 // pulled in something else interesting.
Daniel Dunbar6f949912009-05-03 23:04:40 +0000216 if (CaretEnd != SourceLength) {
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000217 assert(CaretEnd < SourceLength && "Unexpected caret position!");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000218 unsigned NewEnd = CaretEnd;
219
220 // Skip over any whitespace we see here; we're looking for
221 // another bit of interesting text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000222 while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000223 ++NewEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000224
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000225 // Skip over this bit of "interesting" text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000226 while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000227 ++NewEnd;
228
229 if (NewEnd - CaretStart <= TargetColumns) {
230 CaretEnd = NewEnd;
231 ExpandedRegion = true;
232 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000233 }
Daniel Dunbar6f949912009-05-03 23:04:40 +0000234
235 if (!ExpandedRegion)
236 break;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000237 }
238
239 // [CaretStart, CaretEnd) is the slice we want. Update the various
240 // output lines to show only this slice, with two-space padding
241 // before the lines so that it looks nicer.
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000242 if (CaretEnd < SourceLine.size())
243 SourceLine.replace(CaretEnd, std::string::npos, "...");
Douglas Gregorcb516622009-05-03 15:24:25 +0000244 if (CaretEnd < CaretLine.size())
245 CaretLine.erase(CaretEnd, std::string::npos);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000246 if (FixItInsertionLine.size() > CaretEnd)
247 FixItInsertionLine.erase(CaretEnd, std::string::npos);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000248
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000249 if (CaretStart > 2) {
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000250 SourceLine.replace(0, CaretStart, " ...");
251 CaretLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000252 if (FixItInsertionLine.size() >= CaretStart)
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000253 FixItInsertionLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000254 }
255}
256
Chris Lattner1973d842009-02-20 00:18:51 +0000257void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner464ceb42009-02-20 00:25:28 +0000258 SourceRange *Ranges,
Chris Lattner1973d842009-02-20 00:18:51 +0000259 unsigned NumRanges,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000260 SourceManager &SM,
261 const CodeModificationHint *Hints,
Douglas Gregor48185532009-05-01 21:53:04 +0000262 unsigned NumHints,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000263 unsigned Columns) {
Chris Lattner7b60a162009-02-17 08:44:50 +0000264 assert(!Loc.isInvalid() && "must have a valid source location here");
Chris Lattnerc45529b2009-05-05 22:03:18 +0000265
266 // If this is a macro ID, first emit information about where this was
267 // instantiated (recursively) then emit information about where. the token was
268 // spelled from.
Chris Lattner7b60a162009-02-17 08:44:50 +0000269 if (!Loc.isFileID()) {
Chris Lattner24eb28b2009-02-18 18:50:45 +0000270 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Chris Lattnerc45529b2009-05-05 22:03:18 +0000271 // FIXME: Map ranges?
Douglas Gregor080fe612009-05-06 04:43:47 +0000272 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns);
Chris Lattner464ceb42009-02-20 00:25:28 +0000273
Chris Lattnerc45529b2009-05-05 22:03:18 +0000274 Loc = SM.getImmediateSpellingLoc(Loc);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000275
Chris Lattner464ceb42009-02-20 00:25:28 +0000276 // Map the ranges.
277 for (unsigned i = 0; i != NumRanges; ++i) {
278 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
Chris Lattnerc45529b2009-05-05 22:03:18 +0000279 if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S);
280 if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E);
Chris Lattner464ceb42009-02-20 00:25:28 +0000281 Ranges[i] = SourceRange(S, E);
282 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000283
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000284 if (ShowLocation) {
Chris Lattnerc45529b2009-05-05 22:03:18 +0000285 std::pair<FileID, unsigned> IInfo = SM.getDecomposedInstantiationLoc(Loc);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000286
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000287 // Emit the file/line/column that this expansion came from.
Chris Lattnerc45529b2009-05-05 22:03:18 +0000288 OS << SM.getBuffer(IInfo.first)->getBufferIdentifier() << ':'
289 << SM.getLineNumber(IInfo.first, IInfo.second) << ':';
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000290 if (ShowColumn)
Chris Lattnerc45529b2009-05-05 22:03:18 +0000291 OS << SM.getColumnNumber(IInfo.first, IInfo.second) << ':';
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000292 OS << ' ';
293 }
294 OS << "note: instantiated from:\n";
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000295
Douglas Gregor080fe612009-05-06 04:43:47 +0000296 EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns);
Chris Lattnerc45529b2009-05-05 22:03:18 +0000297 return;
Chris Lattner7b60a162009-02-17 08:44:50 +0000298 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000299
Chris Lattnerd22603c2009-02-17 07:51:53 +0000300 // Decompose the location into a FID/Offset pair.
301 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
302 FileID FID = LocInfo.first;
303 unsigned FileOffset = LocInfo.second;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000304
Chris Lattnerd22603c2009-02-17 07:51:53 +0000305 // Get information about the buffer it points into.
306 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID);
307 const char *BufStart = BufferInfo.first;
Chris Lattnerd22603c2009-02-17 07:51:53 +0000308
309 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000310 unsigned CaretEndColNo
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000311 = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts);
312
Chris Lattner2cf68c12009-02-17 07:38:37 +0000313 // Rewind from the current position to the start of the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000314 const char *TokPtr = BufStart+FileOffset;
315 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000316
317
Chris Lattner2cf68c12009-02-17 07:38:37 +0000318 // Compute the line end. Scan forward from the error position to the end of
319 // the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000320 const char *LineEnd = TokPtr;
Chris Lattner139d87b2009-03-08 08:11:22 +0000321 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
Chris Lattner2cf68c12009-02-17 07:38:37 +0000322 ++LineEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000323
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000324 // FIXME: This shouldn't be necessary, but the CaretEndColNo can extend past
325 // the source line length as currently being computed. See
326 // test/Misc/message-length.c.
327 CaretEndColNo = std::min(CaretEndColNo, unsigned(LineEnd - LineStart));
328
Chris Lattner2cf68c12009-02-17 07:38:37 +0000329 // Copy the line of code into an std::string for ease of manipulation.
330 std::string SourceLine(LineStart, LineEnd);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000331
Chris Lattner2cf68c12009-02-17 07:38:37 +0000332 // Create a line for the caret that is filled with spaces that is the same
333 // length as the line of source code.
334 std::string CaretLine(LineEnd-LineStart, ' ');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000335
Chris Lattner2cf68c12009-02-17 07:38:37 +0000336 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner1973d842009-02-20 00:18:51 +0000337 if (NumRanges) {
Chris Lattnerd22603c2009-02-17 07:51:53 +0000338 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000339
Chris Lattner1973d842009-02-20 00:18:51 +0000340 for (unsigned i = 0, e = NumRanges; i != e; ++i)
341 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattnerd22603c2009-02-17 07:51:53 +0000342 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000343
Chris Lattner2cf68c12009-02-17 07:38:37 +0000344 // Next, insert the caret itself.
345 if (ColNo-1 < CaretLine.size())
346 CaretLine[ColNo-1] = '^';
347 else
348 CaretLine.push_back('^');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000349
Chris Lattner2cf68c12009-02-17 07:38:37 +0000350 // Scan the source line, looking for tabs. If we find any, manually expand
351 // them to 8 characters and update the CaretLine to match.
352 for (unsigned i = 0; i != SourceLine.size(); ++i) {
353 if (SourceLine[i] != '\t') continue;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000354
Chris Lattner2cf68c12009-02-17 07:38:37 +0000355 // Replace this tab with at least one space.
356 SourceLine[i] = ' ';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000357
Chris Lattner2cf68c12009-02-17 07:38:37 +0000358 // Compute the number of spaces we need to insert.
359 unsigned NumSpaces = ((i+8)&~7) - (i+1);
360 assert(NumSpaces < 8 && "Invalid computation of space amt");
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000361
Chris Lattner2cf68c12009-02-17 07:38:37 +0000362 // Insert spaces into the SourceLine.
363 SourceLine.insert(i+1, NumSpaces, ' ');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000364
Chris Lattner2cf68c12009-02-17 07:38:37 +0000365 // Insert spaces or ~'s into CaretLine.
366 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
367 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000368
Chris Lattneraf6094e2009-04-28 22:33:16 +0000369 // If we are in -fdiagnostics-print-source-range-info mode, we are trying to
370 // produce easily machine parsable output. Add a space before the source line
371 // and the caret to make it trivial to tell the main diagnostic line from what
372 // the user is intended to see.
373 if (PrintRangeInfo) {
374 SourceLine = ' ' + SourceLine;
375 CaretLine = ' ' + CaretLine;
376 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000377
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000378 std::string FixItInsertionLine;
Chris Lattner29d34ca2009-04-19 07:44:08 +0000379 if (NumHints && PrintFixItInfo) {
Chris Lattner29d34ca2009-04-19 07:44:08 +0000380 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints;
Douglas Gregor87f95b02009-02-26 21:00:50 +0000381 Hint != LastHint; ++Hint) {
382 if (Hint->InsertionLoc.isValid()) {
383 // We have an insertion hint. Determine whether the inserted
384 // code is on the same line as the caret.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000385 std::pair<FileID, unsigned> HintLocInfo
Chris Lattnere29509a2009-03-02 20:58:48 +0000386 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000387 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
388 SM.getLineNumber(FID, FileOffset)) {
389 // Insert the new code into the line just below the code
390 // that the user wrote.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000391 unsigned HintColNo
Douglas Gregor87f95b02009-02-26 21:00:50 +0000392 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000393 unsigned LastColumnModified
Douglas Gregor87f95b02009-02-26 21:00:50 +0000394 = HintColNo - 1 + Hint->CodeToInsert.size();
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000395 if (LastColumnModified > FixItInsertionLine.size())
396 FixItInsertionLine.resize(LastColumnModified, ' ');
Douglas Gregor87f95b02009-02-26 21:00:50 +0000397 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(),
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000398 FixItInsertionLine.begin() + HintColNo - 1);
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000399 } else {
400 FixItInsertionLine.clear();
401 break;
Douglas Gregor87f95b02009-02-26 21:00:50 +0000402 }
403 }
404 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000405 }
Douglas Gregor87f95b02009-02-26 21:00:50 +0000406
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000407 // If the source line is too long for our terminal, select only the
408 // "interesting" source region within that line.
409 if (Columns && SourceLine.size() > Columns)
410 SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000411 CaretEndColNo, Columns);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000412
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000413 // Finally, remove any blank spaces from the end of CaretLine.
414 while (CaretLine[CaretLine.size()-1] == ' ')
415 CaretLine.erase(CaretLine.end()-1);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000416
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000417 // Emit what we have computed.
418 OS << SourceLine << '\n';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000419
420 if (UseColors)
421 OS.changeColor(caretColor, true);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000422 OS << CaretLine << '\n';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000423 if (UseColors)
424 OS.resetColor();
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000425
426 if (!FixItInsertionLine.empty()) {
Torok Edwinc91b6e02009-06-04 07:18:23 +0000427 if (UseColors)
428 // Print fixit line in color
429 OS.changeColor(fixitColor, false);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000430 if (PrintRangeInfo)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000431 OS << ' ';
432 OS << FixItInsertionLine << '\n';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000433 if (UseColors)
434 OS.resetColor();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000435 }
Chris Lattner2cf68c12009-02-17 07:38:37 +0000436}
437
Douglas Gregor48185532009-05-01 21:53:04 +0000438/// \brief Skip over whitespace in the string, starting at the given
439/// index.
440///
441/// \returns The index of the first non-whitespace character that is
442/// greater than or equal to Idx or, if no such character exists,
443/// returns the end of the string.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000444static unsigned skipWhitespace(unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +0000445 const llvm::SmallVectorImpl<char> &Str,
Douglas Gregor48185532009-05-01 21:53:04 +0000446 unsigned Length) {
447 while (Idx < Length && isspace(Str[Idx]))
448 ++Idx;
449 return Idx;
450}
451
452/// \brief If the given character is the start of some kind of
453/// balanced punctuation (e.g., quotes or parentheses), return the
454/// character that will terminate the punctuation.
455///
456/// \returns The ending punctuation character, if any, or the NULL
457/// character if the input character does not start any punctuation.
458static inline char findMatchingPunctuation(char c) {
459 switch (c) {
460 case '\'': return '\'';
461 case '`': return '\'';
462 case '"': return '"';
463 case '(': return ')';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000464 case '[': return ']';
Douglas Gregor48185532009-05-01 21:53:04 +0000465 case '{': return '}';
466 default: break;
467 }
468
469 return 0;
470}
471
472/// \brief Find the end of the word starting at the given offset
473/// within a string.
474///
475/// \returns the index pointing one character past the end of the
476/// word.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000477unsigned findEndOfWord(unsigned Start,
Douglas Gregor48185532009-05-01 21:53:04 +0000478 const llvm::SmallVectorImpl<char> &Str,
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000479 unsigned Length, unsigned Column,
Douglas Gregor48185532009-05-01 21:53:04 +0000480 unsigned Columns) {
481 unsigned End = Start + 1;
482
483 // Determine if the start of the string is actually opening
484 // punctuation, e.g., a quote or parentheses.
485 char EndPunct = findMatchingPunctuation(Str[Start]);
486 if (!EndPunct) {
487 // This is a normal word. Just find the first space character.
488 while (End < Length && !isspace(Str[End]))
489 ++End;
490 return End;
491 }
492
493 // We have the start of a balanced punctuation sequence (quotes,
494 // parentheses, etc.). Determine the full sequence is.
495 llvm::SmallVector<char, 16> PunctuationEndStack;
496 PunctuationEndStack.push_back(EndPunct);
497 while (End < Length && !PunctuationEndStack.empty()) {
498 if (Str[End] == PunctuationEndStack.back())
499 PunctuationEndStack.pop_back();
500 else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
501 PunctuationEndStack.push_back(SubEndPunct);
502
503 ++End;
504 }
505
506 // Find the first space character after the punctuation ended.
507 while (End < Length && !isspace(Str[End]))
508 ++End;
509
510 unsigned PunctWordLength = End - Start;
511 if (// If the word fits on this line
512 Column + PunctWordLength <= Columns ||
513 // ... or the word is "short enough" to take up the next line
514 // without too much ugly white space
515 PunctWordLength < Columns/3)
516 return End; // Take the whole thing as a single "word".
517
518 // The whole quoted/parenthesized string is too long to print as a
519 // single "word". Instead, find the "word" that starts just after
520 // the punctuation and use that end-point instead. This will recurse
521 // until it finds something small enough to consider a word.
522 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
523}
524
525/// \brief Print the given string to a stream, word-wrapping it to
526/// some number of columns in the process.
527///
528/// \brief OS the stream to which the word-wrapping string will be
529/// emitted.
530///
531/// \brief Str the string to word-wrap and output.
532///
533/// \brief Columns the number of columns to word-wrap to.
534///
535/// \brief Column the column number at which the first character of \p
536/// Str will be printed. This will be non-zero when part of the first
537/// line has already been printed.
538///
539/// \brief Indentation the number of spaces to indent any lines beyond
540/// the first line.
541///
542/// \returns true if word-wrapping was required, or false if the
543/// string fit on the first line.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000544static bool PrintWordWrapped(llvm::raw_ostream &OS,
Mike Stump11289f42009-09-09 15:08:12 +0000545 const llvm::SmallVectorImpl<char> &Str,
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000546 unsigned Columns,
Douglas Gregor48185532009-05-01 21:53:04 +0000547 unsigned Column = 0,
548 unsigned Indentation = WordWrapIndentation) {
549 unsigned Length = Str.size();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000550
Douglas Gregor48185532009-05-01 21:53:04 +0000551 // If there is a newline in this message somewhere, find that
552 // newline and split the message into the part before the newline
553 // (which will be word-wrapped) and the part from the newline one
554 // (which will be emitted unchanged).
555 for (unsigned I = 0; I != Length; ++I)
556 if (Str[I] == '\n') {
557 Length = I;
558 break;
559 }
560
561 // The string used to indent each line.
562 llvm::SmallString<16> IndentStr;
563 IndentStr.assign(Indentation, ' ');
564 bool Wrapped = false;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000565 for (unsigned WordStart = 0, WordEnd; WordStart < Length;
Douglas Gregor48185532009-05-01 21:53:04 +0000566 WordStart = WordEnd) {
567 // Find the beginning of the next word.
568 WordStart = skipWhitespace(WordStart, Str, Length);
569 if (WordStart == Length)
570 break;
571
572 // Find the end of this word.
573 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000574
Douglas Gregor48185532009-05-01 21:53:04 +0000575 // Does this word fit on the current line?
576 unsigned WordLength = WordEnd - WordStart;
577 if (Column + WordLength < Columns) {
578 // This word fits on the current line; print it there.
579 if (WordStart) {
580 OS << ' ';
581 Column += 1;
582 }
583 OS.write(&Str[WordStart], WordLength);
584 Column += WordLength;
585 continue;
586 }
587
588 // This word does not fit on the current line, so wrap to the next
589 // line.
Douglas Gregor6f5a38b2009-05-03 03:52:38 +0000590 OS << '\n';
591 OS.write(&IndentStr[0], Indentation);
Douglas Gregor48185532009-05-01 21:53:04 +0000592 OS.write(&Str[WordStart], WordLength);
593 Column = Indentation + WordLength;
594 Wrapped = true;
595 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000596
Douglas Gregor48185532009-05-01 21:53:04 +0000597 if (Length == Str.size())
598 return Wrapped; // We're done.
599
600 // There is a newline in the message, followed by something that
601 // will not be word-wrapped. Print that.
602 OS.write(&Str[Length], Str.size() - Length);
603 return true;
604}
Chris Lattner2cf68c12009-02-17 07:38:37 +0000605
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000606void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
Chris Lattner8488c822008-11-18 07:04:44 +0000607 const DiagnosticInfo &Info) {
Douglas Gregor48185532009-05-01 21:53:04 +0000608 // Keeps track of the the starting position of the location
609 // information (e.g., "foo.c:10:4:") that precedes the error
610 // message. We use this information to determine how long the
611 // file+line+column number prefix is.
612 uint64_t StartOfLocationInfo = OS.tell();
613
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000614 // If the location is specified, print out a file/line/col and include trace
615 // if enabled.
616 if (Info.getLocation().isValid()) {
Ted Kremenek52418322009-01-28 20:47:47 +0000617 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000618 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
619 unsigned LineNo = PLoc.getLine();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000620
Bill Wendling37b1dde2007-06-07 09:34:54 +0000621 // First, if this diagnostic is not in the main file, print out the
622 // "included from" lines.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000623 if (LastWarningLoc != PLoc.getIncludeLoc()) {
624 LastWarningLoc = PLoc.getIncludeLoc();
625 PrintIncludeStack(LastWarningLoc, SM);
Douglas Gregor48185532009-05-01 21:53:04 +0000626 StartOfLocationInfo = OS.tell();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000627 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000628
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000629 // Compute the column number.
Chris Lattner19586772009-01-30 17:41:53 +0000630 if (ShowLocation) {
Torok Edwinc91b6e02009-06-04 07:18:23 +0000631 if (UseColors)
632 OS.changeColor(savedColor, true);
Chris Lattner19586772009-01-30 17:41:53 +0000633 OS << PLoc.getFilename() << ':' << LineNo << ':';
Chris Lattner11a2a432009-02-17 07:34:34 +0000634 if (ShowColumn)
635 if (unsigned ColNo = PLoc.getColumn())
636 OS << ColNo << ':';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000637
Chris Lattner44219f32009-03-13 01:08:23 +0000638 if (PrintRangeInfo && Info.getNumRanges()) {
639 FileID CaretFileID =
640 SM.getFileID(SM.getInstantiationLoc(Info.getLocation()));
641 bool PrintedRange = false;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000642
Chris Lattner44219f32009-03-13 01:08:23 +0000643 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) {
Chris Lattner3251e3c2009-04-19 22:24:10 +0000644 // Ignore invalid ranges.
645 if (!Info.getRange(i).isValid()) continue;
646
Chris Lattner44219f32009-03-13 01:08:23 +0000647 SourceLocation B = Info.getRange(i).getBegin();
648 SourceLocation E = Info.getRange(i).getEnd();
Chris Lattner6ed7d592009-06-15 05:18:27 +0000649 B = SM.getInstantiationLoc(B);
Chris Lattner44219f32009-03-13 01:08:23 +0000650 E = SM.getInstantiationLoc(E);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000651
Chris Lattner6ed7d592009-06-15 05:18:27 +0000652 // If the End location and the start location are the same and are a
653 // macro location, then the range was something that came from a macro
654 // expansion or _Pragma. If this is an object-like macro, the best we
655 // can do is to highlight the range. If this is a function-like
656 // macro, we'd also like to highlight the arguments.
657 if (B == E && Info.getRange(i).getEnd().isMacroID())
658 E = SM.getInstantiationRange(Info.getRange(i).getEnd()).second;
659
660 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
Chris Lattner44219f32009-03-13 01:08:23 +0000661 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000662
Chris Lattner44219f32009-03-13 01:08:23 +0000663 // If the start or end of the range is in another file, just discard
664 // it.
665 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
666 continue;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000667
Chris Lattner44219f32009-03-13 01:08:23 +0000668 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000669 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000670
Chris Lattner44219f32009-03-13 01:08:23 +0000671 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
672 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
673 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
674 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}';
675 PrintedRange = true;
676 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000677
Chris Lattner44219f32009-03-13 01:08:23 +0000678 if (PrintedRange)
679 OS << ':';
680 }
Chris Lattner19586772009-01-30 17:41:53 +0000681 OS << ' ';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000682 if (UseColors)
683 OS.resetColor();
684 }
685 }
686
687 if (UseColors) {
688 // Print diagnostic category in bold and color
689 switch (Level) {
690 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
691 case Diagnostic::Note: OS.changeColor(noteColor, true); break;
692 case Diagnostic::Warning: OS.changeColor(warningColor, true); break;
693 case Diagnostic::Error: OS.changeColor(errorColor, true); break;
694 case Diagnostic::Fatal: OS.changeColor(fatalColor, true); break;
Chris Lattner19586772009-01-30 17:41:53 +0000695 }
Bill Wendling37b1dde2007-06-07 09:34:54 +0000696 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000697
Bill Wendling37b1dde2007-06-07 09:34:54 +0000698 switch (Level) {
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000699 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman36c49272008-04-17 18:06:57 +0000700 case Diagnostic::Note: OS << "note: "; break;
701 case Diagnostic::Warning: OS << "warning: "; break;
702 case Diagnostic::Error: OS << "error: "; break;
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000703 case Diagnostic::Fatal: OS << "fatal error: "; break;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000704 }
Torok Edwinc91b6e02009-06-04 07:18:23 +0000705
706 if (UseColors)
707 OS.resetColor();
708
Chris Lattner23be0672008-11-19 06:51:40 +0000709 llvm::SmallString<100> OutStr;
710 Info.FormatDiagnostic(OutStr);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000711
Chris Lattner22cb8182009-04-16 05:44:38 +0000712 if (PrintDiagnosticOption)
Douglas Gregor48185532009-05-01 21:53:04 +0000713 if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) {
714 OutStr += " [-W";
715 OutStr += Opt;
716 OutStr += ']';
717 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000718
Torok Edwinc91b6e02009-06-04 07:18:23 +0000719 if (UseColors) {
720 // Print warnings, errors and fatal errors in bold, no color
721 switch (Level) {
722 case Diagnostic::Warning: OS.changeColor(savedColor, true); break;
723 case Diagnostic::Error: OS.changeColor(savedColor, true); break;
724 case Diagnostic::Fatal: OS.changeColor(savedColor, true); break;
725 default: break; //don't bold notes
726 }
727 }
728
Douglas Gregor48185532009-05-01 21:53:04 +0000729 if (MessageLength) {
730 // We will be word-wrapping the error message, so compute the
731 // column number where we currently are (after printing the
732 // location information).
733 unsigned Column = OS.tell() - StartOfLocationInfo;
Douglas Gregor080fe612009-05-06 04:43:47 +0000734 PrintWordWrapped(OS, OutStr, MessageLength, Column);
Douglas Gregor48185532009-05-01 21:53:04 +0000735 } else {
736 OS.write(OutStr.begin(), OutStr.size());
737 }
Chris Lattner23be0672008-11-19 06:51:40 +0000738 OS << '\n';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000739 if (UseColors)
740 OS.resetColor();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000741
Douglas Gregor79cf6032009-03-10 20:44:00 +0000742 // If caret diagnostics are enabled and we have location, we want to
743 // emit the caret. However, we only do this if the location moved
744 // from the last diagnostic, if the last diagnostic was a note that
745 // was part of a different warning or error diagnostic, or if the
746 // diagnostic has ranges. We don't want to emit the same caret
747 // multiple times if one loc has multiple diagnostics.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000748 if (CaretDiagnostics && Info.getLocation().isValid() &&
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000749 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
Douglas Gregor79cf6032009-03-10 20:44:00 +0000750 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) ||
Douglas Gregor87f95b02009-02-26 21:00:50 +0000751 Info.getNumCodeModificationHints())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000752 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000753 LastLoc = Info.getLocation();
Douglas Gregor79cf6032009-03-10 20:44:00 +0000754 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000755
Chris Lattner1973d842009-02-20 00:18:51 +0000756 // Get the ranges into a local array we can hack on.
Douglas Gregor87f95b02009-02-26 21:00:50 +0000757 SourceRange Ranges[20];
Chris Lattner1973d842009-02-20 00:18:51 +0000758 unsigned NumRanges = Info.getNumRanges();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000759 assert(NumRanges < 20 && "Out of space");
Chris Lattner1973d842009-02-20 00:18:51 +0000760 for (unsigned i = 0; i != NumRanges; ++i)
761 Ranges[i] = Info.getRange(i);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000762
Douglas Gregor87f95b02009-02-26 21:00:50 +0000763 unsigned NumHints = Info.getNumCodeModificationHints();
764 for (unsigned idx = 0; idx < NumHints; ++idx) {
765 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx);
766 if (Hint.RemoveRange.isValid()) {
767 assert(NumRanges < 20 && "Out of space");
768 Ranges[NumRanges++] = Hint.RemoveRange;
769 }
770 }
771
772 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
773 Info.getCodeModificationHints(),
Douglas Gregor48185532009-05-01 21:53:04 +0000774 Info.getNumCodeModificationHints(),
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000775 MessageLength);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000776 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000777
Chris Lattner327984f2008-11-19 06:56:25 +0000778 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000779}