blob: b1d8800369e550de720243af4981d7090db4c8c4 [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"
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000016#include "clang/Frontend/DiagnosticOptions.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000017#include "clang/Lex/Lexer.h"
Chris Lattnerc45529b2009-05-05 22:03:18 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner23be0672008-11-19 06:51:40 +000020#include "llvm/ADT/SmallString.h"
Douglas Gregor87f95b02009-02-26 21:00:50 +000021#include <algorithm>
Bill Wendling37b1dde2007-06-07 09:34:54 +000022using namespace clang;
23
Torok Edwinc91b6e02009-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 Gregor48185532009-05-01 21:53:04 +000038/// \brief Number of spaces to indent when word-wrapping.
39const unsigned WordWrapIndentation = 6;
40
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000041TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream &os,
42 const DiagnosticOptions &diags)
43 : OS(os), LangOpts(0), DiagOpts(&diags),
44 LastCaretDiagnosticWasNote(false) {
45}
46
Bill Wendling37b1dde2007-06-07 09:34:54 +000047void TextDiagnosticPrinter::
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000048PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
49 if (Loc.isInvalid()) return;
Chris Lattnerdc5c0552007-07-20 16:37:10 +000050
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000051 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattnerdc5c0552007-07-20 16:37:10 +000052
Bill Wendling37b1dde2007-06-07 09:34:54 +000053 // Print out the other include frames first.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000054 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Chris Lattner92b29b2f2009-04-21 03:57:54 +000055
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000056 if (DiagOpts->ShowLocation)
Chris Lattner92b29b2f2009-04-21 03:57:54 +000057 OS << "In file included from " << PLoc.getFilename()
58 << ':' << PLoc.getLine() << ":\n";
59 else
60 OS << "In included file:\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000061}
62
Bill Wendling37b1dde2007-06-07 09:34:54 +000063/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
64/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000065void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000066 const SourceManager &SM,
Chris Lattner71dc14b2009-01-17 08:45:21 +000067 unsigned LineNo, FileID FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000068 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000069 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000070 assert(CaretLine.size() == SourceLine.size() &&
71 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000072 if (!R.isValid()) return;
73
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000074 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
75 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
Daniel Dunbar49f0e802009-09-07 23:07:56 +000076
Chris Lattnerb42700f2009-02-17 05:19:10 +000077 // If the End location and the start location are the same and are a macro
78 // location, then the range was something that came from a macro expansion
79 // or _Pragma. If this is an object-like macro, the best we can do is to
80 // highlight the range. If this is a function-like macro, we'd also like to
81 // highlight the arguments.
82 if (Begin == End && R.getEnd().isMacroID())
83 End = SM.getInstantiationRange(R.getEnd()).second;
Daniel Dunbar49f0e802009-09-07 23:07:56 +000084
Chris Lattner88ea93e2009-02-04 01:06:56 +000085 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000086 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000087 return; // No intersection.
Daniel Dunbar49f0e802009-09-07 23:07:56 +000088
Chris Lattner88ea93e2009-02-04 01:06:56 +000089 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000090 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000091 return; // No intersection.
Daniel Dunbar49f0e802009-09-07 23:07:56 +000092
Bill Wendling37b1dde2007-06-07 09:34:54 +000093 // Compute the column number of the start.
94 unsigned StartColNo = 0;
95 if (StartLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +000096 StartColNo = SM.getInstantiationColumnNumber(Begin);
Bill Wendling37b1dde2007-06-07 09:34:54 +000097 if (StartColNo) --StartColNo; // Zero base the col #.
98 }
99
100 // Pick the first non-whitespace column.
101 while (StartColNo < SourceLine.size() &&
102 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
103 ++StartColNo;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000104
Bill Wendling37b1dde2007-06-07 09:34:54 +0000105 // Compute the column number of the end.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000106 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000107 if (EndLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +0000108 EndColNo = SM.getInstantiationColumnNumber(End);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000109 if (EndColNo) {
110 --EndColNo; // Zero base the col #.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000111
Bill Wendling37b1dde2007-06-07 09:34:54 +0000112 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000113 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000114 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000115 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000116 }
117 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000118
Bill Wendling37b1dde2007-06-07 09:34:54 +0000119 // Pick the last non-whitespace column.
Nuno Lopesd86aa932008-08-05 19:40:20 +0000120 if (EndColNo <= SourceLine.size())
121 while (EndColNo-1 &&
122 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
123 --EndColNo;
124 else
125 EndColNo = SourceLine.size();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000126
Bill Wendling37b1dde2007-06-07 09:34:54 +0000127 // Fill the range with ~'s.
128 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd86aa932008-08-05 19:40:20 +0000129 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000130 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000131}
132
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000133/// \brief When the source code line we want to print is too long for
134/// the terminal, select the "interesting" region.
135static void SelectInterestingSourceRegion(std::string &SourceLine,
136 std::string &CaretLine,
137 std::string &FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000138 unsigned EndOfCaretToken,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000139 unsigned Columns) {
140 if (CaretLine.size() > SourceLine.size())
141 SourceLine.resize(CaretLine.size(), ' ');
142
143 // Find the slice that we need to display the full caret line
144 // correctly.
145 unsigned CaretStart = 0, CaretEnd = CaretLine.size();
146 for (; CaretStart != CaretEnd; ++CaretStart)
147 if (!isspace(CaretLine[CaretStart]))
148 break;
149
150 for (; CaretEnd != CaretStart; --CaretEnd)
151 if (!isspace(CaretLine[CaretEnd - 1]))
152 break;
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000153
154 // Make sure we don't chop the string shorter than the caret token
155 // itself.
156 if (CaretEnd < EndOfCaretToken)
157 CaretEnd = EndOfCaretToken;
158
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000159 // If we have a fix-it line, make sure the slice includes all of the
160 // fix-it information.
161 if (!FixItInsertionLine.empty()) {
162 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
163 for (; FixItStart != FixItEnd; ++FixItStart)
164 if (!isspace(FixItInsertionLine[FixItStart]))
165 break;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000166
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000167 for (; FixItEnd != FixItStart; --FixItEnd)
168 if (!isspace(FixItInsertionLine[FixItEnd - 1]))
169 break;
170
171 if (FixItStart < CaretStart)
172 CaretStart = FixItStart;
173 if (FixItEnd > CaretEnd)
174 CaretEnd = FixItEnd;
175 }
176
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000177 // CaretLine[CaretStart, CaretEnd) contains all of the interesting
178 // parts of the caret line. While this slice is smaller than the
179 // number of columns we have, try to grow the slice to encompass
180 // more context.
181
182 // If the end of the interesting region comes before we run out of
183 // space in the terminal, start at the beginning of the line.
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000184 if (Columns > 3 && CaretEnd < Columns - 3)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000185 CaretStart = 0;
186
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000187 unsigned TargetColumns = Columns;
188 if (TargetColumns > 8)
189 TargetColumns -= 8; // Give us extra room for the ellipses.
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000190 unsigned SourceLength = SourceLine.size();
Douglas Gregor006fd382009-05-04 06:45:38 +0000191 while ((CaretEnd - CaretStart) < TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000192 bool ExpandedRegion = false;
193 // Move the start of the interesting region left until we've
194 // pulled in something else interesting.
Douglas Gregor006fd382009-05-04 06:45:38 +0000195 if (CaretStart == 1)
196 CaretStart = 0;
197 else if (CaretStart > 1) {
198 unsigned NewStart = CaretStart - 1;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000199
Douglas Gregor006fd382009-05-04 06:45:38 +0000200 // Skip over any whitespace we see here; we're looking for
201 // another bit of interesting text.
202 while (NewStart && isspace(SourceLine[NewStart]))
203 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000204
Douglas Gregor006fd382009-05-04 06:45:38 +0000205 // Skip over this bit of "interesting" text.
206 while (NewStart && !isspace(SourceLine[NewStart]))
207 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000208
Douglas Gregor006fd382009-05-04 06:45:38 +0000209 // Move up to the non-whitespace character we just saw.
210 if (NewStart)
211 ++NewStart;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000212
213 // If we're still within our limit, update the starting
214 // position within the source/caret line.
Douglas Gregor006fd382009-05-04 06:45:38 +0000215 if (CaretEnd - NewStart <= TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000216 CaretStart = NewStart;
217 ExpandedRegion = true;
218 }
219 }
220
221 // Move the end of the interesting region right until we've
222 // pulled in something else interesting.
Daniel Dunbar6f949912009-05-03 23:04:40 +0000223 if (CaretEnd != SourceLength) {
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000224 assert(CaretEnd < SourceLength && "Unexpected caret position!");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000225 unsigned NewEnd = CaretEnd;
226
227 // Skip over any whitespace we see here; we're looking for
228 // another bit of interesting text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000229 while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000230 ++NewEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000231
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000232 // Skip over this bit of "interesting" text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000233 while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000234 ++NewEnd;
235
236 if (NewEnd - CaretStart <= TargetColumns) {
237 CaretEnd = NewEnd;
238 ExpandedRegion = true;
239 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000240 }
Daniel Dunbar6f949912009-05-03 23:04:40 +0000241
242 if (!ExpandedRegion)
243 break;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000244 }
245
246 // [CaretStart, CaretEnd) is the slice we want. Update the various
247 // output lines to show only this slice, with two-space padding
248 // before the lines so that it looks nicer.
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000249 if (CaretEnd < SourceLine.size())
250 SourceLine.replace(CaretEnd, std::string::npos, "...");
Douglas Gregorcb516622009-05-03 15:24:25 +0000251 if (CaretEnd < CaretLine.size())
252 CaretLine.erase(CaretEnd, std::string::npos);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000253 if (FixItInsertionLine.size() > CaretEnd)
254 FixItInsertionLine.erase(CaretEnd, std::string::npos);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000255
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000256 if (CaretStart > 2) {
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000257 SourceLine.replace(0, CaretStart, " ...");
258 CaretLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000259 if (FixItInsertionLine.size() >= CaretStart)
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000260 FixItInsertionLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000261 }
262}
263
Chris Lattner1973d842009-02-20 00:18:51 +0000264void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner464ceb42009-02-20 00:25:28 +0000265 SourceRange *Ranges,
Chris Lattner1973d842009-02-20 00:18:51 +0000266 unsigned NumRanges,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000267 SourceManager &SM,
268 const CodeModificationHint *Hints,
Douglas Gregor48185532009-05-01 21:53:04 +0000269 unsigned NumHints,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000270 unsigned Columns) {
Chris Lattner7b60a162009-02-17 08:44:50 +0000271 assert(!Loc.isInvalid() && "must have a valid source location here");
Chris Lattnerc45529b2009-05-05 22:03:18 +0000272
273 // If this is a macro ID, first emit information about where this was
274 // instantiated (recursively) then emit information about where. the token was
275 // spelled from.
Chris Lattner7b60a162009-02-17 08:44:50 +0000276 if (!Loc.isFileID()) {
Chris Lattner24eb28b2009-02-18 18:50:45 +0000277 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Chris Lattnerc45529b2009-05-05 22:03:18 +0000278 // FIXME: Map ranges?
Douglas Gregor080fe612009-05-06 04:43:47 +0000279 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns);
Chris Lattner464ceb42009-02-20 00:25:28 +0000280
Chris Lattnerc45529b2009-05-05 22:03:18 +0000281 Loc = SM.getImmediateSpellingLoc(Loc);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000282
Chris Lattner464ceb42009-02-20 00:25:28 +0000283 // Map the ranges.
284 for (unsigned i = 0; i != NumRanges; ++i) {
285 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
Chris Lattnerc45529b2009-05-05 22:03:18 +0000286 if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S);
287 if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E);
Chris Lattner464ceb42009-02-20 00:25:28 +0000288 Ranges[i] = SourceRange(S, E);
289 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000290
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000291 if (DiagOpts->ShowLocation) {
Chris Lattnerc45529b2009-05-05 22:03:18 +0000292 std::pair<FileID, unsigned> IInfo = SM.getDecomposedInstantiationLoc(Loc);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000293
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000294 // Emit the file/line/column that this expansion came from.
Chris Lattnerc45529b2009-05-05 22:03:18 +0000295 OS << SM.getBuffer(IInfo.first)->getBufferIdentifier() << ':'
296 << SM.getLineNumber(IInfo.first, IInfo.second) << ':';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000297 if (DiagOpts->ShowColumn)
Chris Lattnerc45529b2009-05-05 22:03:18 +0000298 OS << SM.getColumnNumber(IInfo.first, IInfo.second) << ':';
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000299 OS << ' ';
300 }
301 OS << "note: instantiated from:\n";
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000302
Douglas Gregor080fe612009-05-06 04:43:47 +0000303 EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns);
Chris Lattnerc45529b2009-05-05 22:03:18 +0000304 return;
Chris Lattner7b60a162009-02-17 08:44:50 +0000305 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000306
Chris Lattnerd22603c2009-02-17 07:51:53 +0000307 // Decompose the location into a FID/Offset pair.
308 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
309 FileID FID = LocInfo.first;
310 unsigned FileOffset = LocInfo.second;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000311
Chris Lattnerd22603c2009-02-17 07:51:53 +0000312 // Get information about the buffer it points into.
313 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID);
314 const char *BufStart = BufferInfo.first;
Chris Lattnerd22603c2009-02-17 07:51:53 +0000315
316 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000317 unsigned CaretEndColNo
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000318 = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts);
319
Chris Lattner2cf68c12009-02-17 07:38:37 +0000320 // Rewind from the current position to the start of the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000321 const char *TokPtr = BufStart+FileOffset;
322 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000323
324
Chris Lattner2cf68c12009-02-17 07:38:37 +0000325 // Compute the line end. Scan forward from the error position to the end of
326 // the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000327 const char *LineEnd = TokPtr;
Chris Lattner139d87b2009-03-08 08:11:22 +0000328 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
Chris Lattner2cf68c12009-02-17 07:38:37 +0000329 ++LineEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000330
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000331 // FIXME: This shouldn't be necessary, but the CaretEndColNo can extend past
332 // the source line length as currently being computed. See
333 // test/Misc/message-length.c.
334 CaretEndColNo = std::min(CaretEndColNo, unsigned(LineEnd - LineStart));
335
Chris Lattner2cf68c12009-02-17 07:38:37 +0000336 // Copy the line of code into an std::string for ease of manipulation.
337 std::string SourceLine(LineStart, LineEnd);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000338
Chris Lattner2cf68c12009-02-17 07:38:37 +0000339 // Create a line for the caret that is filled with spaces that is the same
340 // length as the line of source code.
341 std::string CaretLine(LineEnd-LineStart, ' ');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000342
Chris Lattner2cf68c12009-02-17 07:38:37 +0000343 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner1973d842009-02-20 00:18:51 +0000344 if (NumRanges) {
Chris Lattnerd22603c2009-02-17 07:51:53 +0000345 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000346
Chris Lattner1973d842009-02-20 00:18:51 +0000347 for (unsigned i = 0, e = NumRanges; i != e; ++i)
348 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattnerd22603c2009-02-17 07:51:53 +0000349 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000350
Chris Lattner2cf68c12009-02-17 07:38:37 +0000351 // Next, insert the caret itself.
352 if (ColNo-1 < CaretLine.size())
353 CaretLine[ColNo-1] = '^';
354 else
355 CaretLine.push_back('^');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000356
Chris Lattner2cf68c12009-02-17 07:38:37 +0000357 // Scan the source line, looking for tabs. If we find any, manually expand
358 // them to 8 characters and update the CaretLine to match.
359 for (unsigned i = 0; i != SourceLine.size(); ++i) {
360 if (SourceLine[i] != '\t') continue;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000361
Chris Lattner2cf68c12009-02-17 07:38:37 +0000362 // Replace this tab with at least one space.
363 SourceLine[i] = ' ';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000364
Chris Lattner2cf68c12009-02-17 07:38:37 +0000365 // Compute the number of spaces we need to insert.
366 unsigned NumSpaces = ((i+8)&~7) - (i+1);
367 assert(NumSpaces < 8 && "Invalid computation of space amt");
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000368
Chris Lattner2cf68c12009-02-17 07:38:37 +0000369 // Insert spaces into the SourceLine.
370 SourceLine.insert(i+1, NumSpaces, ' ');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000371
Chris Lattner2cf68c12009-02-17 07:38:37 +0000372 // Insert spaces or ~'s into CaretLine.
373 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
374 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000375
Chris Lattneraf6094e2009-04-28 22:33:16 +0000376 // If we are in -fdiagnostics-print-source-range-info mode, we are trying to
377 // produce easily machine parsable output. Add a space before the source line
378 // and the caret to make it trivial to tell the main diagnostic line from what
379 // the user is intended to see.
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000380 if (DiagOpts->ShowSourceRanges) {
Chris Lattneraf6094e2009-04-28 22:33:16 +0000381 SourceLine = ' ' + SourceLine;
382 CaretLine = ' ' + CaretLine;
383 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000384
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000385 std::string FixItInsertionLine;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000386 if (NumHints && DiagOpts->ShowFixits) {
Chris Lattner29d34ca2009-04-19 07:44:08 +0000387 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints;
Douglas Gregor87f95b02009-02-26 21:00:50 +0000388 Hint != LastHint; ++Hint) {
389 if (Hint->InsertionLoc.isValid()) {
390 // We have an insertion hint. Determine whether the inserted
391 // code is on the same line as the caret.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000392 std::pair<FileID, unsigned> HintLocInfo
Chris Lattnere29509a2009-03-02 20:58:48 +0000393 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000394 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
395 SM.getLineNumber(FID, FileOffset)) {
396 // Insert the new code into the line just below the code
397 // that the user wrote.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000398 unsigned HintColNo
Douglas Gregor87f95b02009-02-26 21:00:50 +0000399 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000400 unsigned LastColumnModified
Douglas Gregor87f95b02009-02-26 21:00:50 +0000401 = HintColNo - 1 + Hint->CodeToInsert.size();
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000402 if (LastColumnModified > FixItInsertionLine.size())
403 FixItInsertionLine.resize(LastColumnModified, ' ');
Douglas Gregor87f95b02009-02-26 21:00:50 +0000404 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(),
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000405 FixItInsertionLine.begin() + HintColNo - 1);
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000406 } else {
407 FixItInsertionLine.clear();
408 break;
Douglas Gregor87f95b02009-02-26 21:00:50 +0000409 }
410 }
411 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000412 }
Douglas Gregor87f95b02009-02-26 21:00:50 +0000413
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000414 // If the source line is too long for our terminal, select only the
415 // "interesting" source region within that line.
416 if (Columns && SourceLine.size() > Columns)
417 SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000418 CaretEndColNo, Columns);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000419
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000420 // Finally, remove any blank spaces from the end of CaretLine.
421 while (CaretLine[CaretLine.size()-1] == ' ')
422 CaretLine.erase(CaretLine.end()-1);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000423
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000424 // Emit what we have computed.
425 OS << SourceLine << '\n';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000426
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000427 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000428 OS.changeColor(caretColor, true);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000429 OS << CaretLine << '\n';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000430 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000431 OS.resetColor();
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000432
433 if (!FixItInsertionLine.empty()) {
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000434 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000435 // Print fixit line in color
436 OS.changeColor(fixitColor, false);
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000437 if (DiagOpts->ShowSourceRanges)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000438 OS << ' ';
439 OS << FixItInsertionLine << '\n';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000440 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000441 OS.resetColor();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000442 }
Chris Lattner2cf68c12009-02-17 07:38:37 +0000443}
444
Douglas Gregor48185532009-05-01 21:53:04 +0000445/// \brief Skip over whitespace in the string, starting at the given
446/// index.
447///
448/// \returns The index of the first non-whitespace character that is
449/// greater than or equal to Idx or, if no such character exists,
450/// returns the end of the string.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000451static unsigned skipWhitespace(unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +0000452 const llvm::SmallVectorImpl<char> &Str,
Douglas Gregor48185532009-05-01 21:53:04 +0000453 unsigned Length) {
454 while (Idx < Length && isspace(Str[Idx]))
455 ++Idx;
456 return Idx;
457}
458
459/// \brief If the given character is the start of some kind of
460/// balanced punctuation (e.g., quotes or parentheses), return the
461/// character that will terminate the punctuation.
462///
463/// \returns The ending punctuation character, if any, or the NULL
464/// character if the input character does not start any punctuation.
465static inline char findMatchingPunctuation(char c) {
466 switch (c) {
467 case '\'': return '\'';
468 case '`': return '\'';
469 case '"': return '"';
470 case '(': return ')';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000471 case '[': return ']';
Douglas Gregor48185532009-05-01 21:53:04 +0000472 case '{': return '}';
473 default: break;
474 }
475
476 return 0;
477}
478
479/// \brief Find the end of the word starting at the given offset
480/// within a string.
481///
482/// \returns the index pointing one character past the end of the
483/// word.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000484unsigned findEndOfWord(unsigned Start,
Douglas Gregor48185532009-05-01 21:53:04 +0000485 const llvm::SmallVectorImpl<char> &Str,
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000486 unsigned Length, unsigned Column,
Douglas Gregor48185532009-05-01 21:53:04 +0000487 unsigned Columns) {
488 unsigned End = Start + 1;
489
490 // Determine if the start of the string is actually opening
491 // punctuation, e.g., a quote or parentheses.
492 char EndPunct = findMatchingPunctuation(Str[Start]);
493 if (!EndPunct) {
494 // This is a normal word. Just find the first space character.
495 while (End < Length && !isspace(Str[End]))
496 ++End;
497 return End;
498 }
499
500 // We have the start of a balanced punctuation sequence (quotes,
501 // parentheses, etc.). Determine the full sequence is.
502 llvm::SmallVector<char, 16> PunctuationEndStack;
503 PunctuationEndStack.push_back(EndPunct);
504 while (End < Length && !PunctuationEndStack.empty()) {
505 if (Str[End] == PunctuationEndStack.back())
506 PunctuationEndStack.pop_back();
507 else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
508 PunctuationEndStack.push_back(SubEndPunct);
509
510 ++End;
511 }
512
513 // Find the first space character after the punctuation ended.
514 while (End < Length && !isspace(Str[End]))
515 ++End;
516
517 unsigned PunctWordLength = End - Start;
518 if (// If the word fits on this line
519 Column + PunctWordLength <= Columns ||
520 // ... or the word is "short enough" to take up the next line
521 // without too much ugly white space
522 PunctWordLength < Columns/3)
523 return End; // Take the whole thing as a single "word".
524
525 // The whole quoted/parenthesized string is too long to print as a
526 // single "word". Instead, find the "word" that starts just after
527 // the punctuation and use that end-point instead. This will recurse
528 // until it finds something small enough to consider a word.
529 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
530}
531
532/// \brief Print the given string to a stream, word-wrapping it to
533/// some number of columns in the process.
534///
535/// \brief OS the stream to which the word-wrapping string will be
536/// emitted.
537///
538/// \brief Str the string to word-wrap and output.
539///
540/// \brief Columns the number of columns to word-wrap to.
541///
542/// \brief Column the column number at which the first character of \p
543/// Str will be printed. This will be non-zero when part of the first
544/// line has already been printed.
545///
546/// \brief Indentation the number of spaces to indent any lines beyond
547/// the first line.
548///
549/// \returns true if word-wrapping was required, or false if the
550/// string fit on the first line.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000551static bool PrintWordWrapped(llvm::raw_ostream &OS,
Mike Stump11289f42009-09-09 15:08:12 +0000552 const llvm::SmallVectorImpl<char> &Str,
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000553 unsigned Columns,
Douglas Gregor48185532009-05-01 21:53:04 +0000554 unsigned Column = 0,
555 unsigned Indentation = WordWrapIndentation) {
556 unsigned Length = Str.size();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000557
Douglas Gregor48185532009-05-01 21:53:04 +0000558 // If there is a newline in this message somewhere, find that
559 // newline and split the message into the part before the newline
560 // (which will be word-wrapped) and the part from the newline one
561 // (which will be emitted unchanged).
562 for (unsigned I = 0; I != Length; ++I)
563 if (Str[I] == '\n') {
564 Length = I;
565 break;
566 }
567
568 // The string used to indent each line.
569 llvm::SmallString<16> IndentStr;
570 IndentStr.assign(Indentation, ' ');
571 bool Wrapped = false;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000572 for (unsigned WordStart = 0, WordEnd; WordStart < Length;
Douglas Gregor48185532009-05-01 21:53:04 +0000573 WordStart = WordEnd) {
574 // Find the beginning of the next word.
575 WordStart = skipWhitespace(WordStart, Str, Length);
576 if (WordStart == Length)
577 break;
578
579 // Find the end of this word.
580 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000581
Douglas Gregor48185532009-05-01 21:53:04 +0000582 // Does this word fit on the current line?
583 unsigned WordLength = WordEnd - WordStart;
584 if (Column + WordLength < Columns) {
585 // This word fits on the current line; print it there.
586 if (WordStart) {
587 OS << ' ';
588 Column += 1;
589 }
590 OS.write(&Str[WordStart], WordLength);
591 Column += WordLength;
592 continue;
593 }
594
595 // This word does not fit on the current line, so wrap to the next
596 // line.
Douglas Gregor6f5a38b2009-05-03 03:52:38 +0000597 OS << '\n';
598 OS.write(&IndentStr[0], Indentation);
Douglas Gregor48185532009-05-01 21:53:04 +0000599 OS.write(&Str[WordStart], WordLength);
600 Column = Indentation + WordLength;
601 Wrapped = true;
602 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000603
Douglas Gregor48185532009-05-01 21:53:04 +0000604 if (Length == Str.size())
605 return Wrapped; // We're done.
606
607 // There is a newline in the message, followed by something that
608 // will not be word-wrapped. Print that.
609 OS.write(&Str[Length], Str.size() - Length);
610 return true;
611}
Chris Lattner2cf68c12009-02-17 07:38:37 +0000612
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000613void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
Chris Lattner8488c822008-11-18 07:04:44 +0000614 const DiagnosticInfo &Info) {
Douglas Gregor48185532009-05-01 21:53:04 +0000615 // Keeps track of the the starting position of the location
616 // information (e.g., "foo.c:10:4:") that precedes the error
617 // message. We use this information to determine how long the
618 // file+line+column number prefix is.
619 uint64_t StartOfLocationInfo = OS.tell();
620
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000621 // If the location is specified, print out a file/line/col and include trace
622 // if enabled.
623 if (Info.getLocation().isValid()) {
Ted Kremenek52418322009-01-28 20:47:47 +0000624 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000625 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
626 unsigned LineNo = PLoc.getLine();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000627
Bill Wendling37b1dde2007-06-07 09:34:54 +0000628 // First, if this diagnostic is not in the main file, print out the
629 // "included from" lines.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000630 if (LastWarningLoc != PLoc.getIncludeLoc()) {
631 LastWarningLoc = PLoc.getIncludeLoc();
632 PrintIncludeStack(LastWarningLoc, SM);
Douglas Gregor48185532009-05-01 21:53:04 +0000633 StartOfLocationInfo = OS.tell();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000634 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000635
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000636 // Compute the column number.
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000637 if (DiagOpts->ShowLocation) {
638 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000639 OS.changeColor(savedColor, true);
Chris Lattner19586772009-01-30 17:41:53 +0000640 OS << PLoc.getFilename() << ':' << LineNo << ':';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000641 if (DiagOpts->ShowColumn)
Chris Lattner11a2a432009-02-17 07:34:34 +0000642 if (unsigned ColNo = PLoc.getColumn())
643 OS << ColNo << ':';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000644
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000645 if (DiagOpts->ShowSourceRanges && Info.getNumRanges()) {
Chris Lattner44219f32009-03-13 01:08:23 +0000646 FileID CaretFileID =
647 SM.getFileID(SM.getInstantiationLoc(Info.getLocation()));
648 bool PrintedRange = false;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000649
Chris Lattner44219f32009-03-13 01:08:23 +0000650 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) {
Chris Lattner3251e3c2009-04-19 22:24:10 +0000651 // Ignore invalid ranges.
652 if (!Info.getRange(i).isValid()) continue;
653
Chris Lattner44219f32009-03-13 01:08:23 +0000654 SourceLocation B = Info.getRange(i).getBegin();
655 SourceLocation E = Info.getRange(i).getEnd();
Chris Lattner6ed7d592009-06-15 05:18:27 +0000656 B = SM.getInstantiationLoc(B);
Chris Lattner44219f32009-03-13 01:08:23 +0000657 E = SM.getInstantiationLoc(E);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000658
Chris Lattner6ed7d592009-06-15 05:18:27 +0000659 // If the End location and the start location are the same and are a
660 // macro location, then the range was something that came from a macro
661 // expansion or _Pragma. If this is an object-like macro, the best we
662 // can do is to highlight the range. If this is a function-like
663 // macro, we'd also like to highlight the arguments.
664 if (B == E && Info.getRange(i).getEnd().isMacroID())
665 E = SM.getInstantiationRange(Info.getRange(i).getEnd()).second;
666
667 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
Chris Lattner44219f32009-03-13 01:08:23 +0000668 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000669
Chris Lattner44219f32009-03-13 01:08:23 +0000670 // If the start or end of the range is in another file, just discard
671 // it.
672 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
673 continue;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000674
Chris Lattner44219f32009-03-13 01:08:23 +0000675 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000676 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000677
Chris Lattner44219f32009-03-13 01:08:23 +0000678 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
679 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
680 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
681 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}';
682 PrintedRange = true;
683 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000684
Chris Lattner44219f32009-03-13 01:08:23 +0000685 if (PrintedRange)
686 OS << ':';
687 }
Chris Lattner19586772009-01-30 17:41:53 +0000688 OS << ' ';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000689 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000690 OS.resetColor();
691 }
692 }
693
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000694 if (DiagOpts->ShowColors) {
Torok Edwinc91b6e02009-06-04 07:18:23 +0000695 // Print diagnostic category in bold and color
696 switch (Level) {
697 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
698 case Diagnostic::Note: OS.changeColor(noteColor, true); break;
699 case Diagnostic::Warning: OS.changeColor(warningColor, true); break;
700 case Diagnostic::Error: OS.changeColor(errorColor, true); break;
701 case Diagnostic::Fatal: OS.changeColor(fatalColor, true); break;
Chris Lattner19586772009-01-30 17:41:53 +0000702 }
Bill Wendling37b1dde2007-06-07 09:34:54 +0000703 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000704
Bill Wendling37b1dde2007-06-07 09:34:54 +0000705 switch (Level) {
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000706 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman36c49272008-04-17 18:06:57 +0000707 case Diagnostic::Note: OS << "note: "; break;
708 case Diagnostic::Warning: OS << "warning: "; break;
709 case Diagnostic::Error: OS << "error: "; break;
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000710 case Diagnostic::Fatal: OS << "fatal error: "; break;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000711 }
Torok Edwinc91b6e02009-06-04 07:18:23 +0000712
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000713 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000714 OS.resetColor();
715
Chris Lattner23be0672008-11-19 06:51:40 +0000716 llvm::SmallString<100> OutStr;
717 Info.FormatDiagnostic(OutStr);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000718
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000719 if (DiagOpts->ShowOptionNames)
Douglas Gregor48185532009-05-01 21:53:04 +0000720 if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) {
721 OutStr += " [-W";
722 OutStr += Opt;
723 OutStr += ']';
724 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000725
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000726 if (DiagOpts->ShowColors) {
Torok Edwinc91b6e02009-06-04 07:18:23 +0000727 // Print warnings, errors and fatal errors in bold, no color
728 switch (Level) {
729 case Diagnostic::Warning: OS.changeColor(savedColor, true); break;
730 case Diagnostic::Error: OS.changeColor(savedColor, true); break;
731 case Diagnostic::Fatal: OS.changeColor(savedColor, true); break;
732 default: break; //don't bold notes
733 }
734 }
735
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000736 if (DiagOpts->MessageLength) {
Douglas Gregor48185532009-05-01 21:53:04 +0000737 // We will be word-wrapping the error message, so compute the
738 // column number where we currently are (after printing the
739 // location information).
740 unsigned Column = OS.tell() - StartOfLocationInfo;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000741 PrintWordWrapped(OS, OutStr, DiagOpts->MessageLength, Column);
Douglas Gregor48185532009-05-01 21:53:04 +0000742 } else {
743 OS.write(OutStr.begin(), OutStr.size());
744 }
Chris Lattner23be0672008-11-19 06:51:40 +0000745 OS << '\n';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000746 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000747 OS.resetColor();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000748
Douglas Gregor79cf6032009-03-10 20:44:00 +0000749 // If caret diagnostics are enabled and we have location, we want to
750 // emit the caret. However, we only do this if the location moved
751 // from the last diagnostic, if the last diagnostic was a note that
752 // was part of a different warning or error diagnostic, or if the
753 // diagnostic has ranges. We don't want to emit the same caret
754 // multiple times if one loc has multiple diagnostics.
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000755 if (DiagOpts->ShowCarets && Info.getLocation().isValid() &&
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000756 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
Douglas Gregor79cf6032009-03-10 20:44:00 +0000757 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) ||
Douglas Gregor87f95b02009-02-26 21:00:50 +0000758 Info.getNumCodeModificationHints())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000759 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000760 LastLoc = Info.getLocation();
Douglas Gregor79cf6032009-03-10 20:44:00 +0000761 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000762
Chris Lattner1973d842009-02-20 00:18:51 +0000763 // Get the ranges into a local array we can hack on.
Douglas Gregor87f95b02009-02-26 21:00:50 +0000764 SourceRange Ranges[20];
Chris Lattner1973d842009-02-20 00:18:51 +0000765 unsigned NumRanges = Info.getNumRanges();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000766 assert(NumRanges < 20 && "Out of space");
Chris Lattner1973d842009-02-20 00:18:51 +0000767 for (unsigned i = 0; i != NumRanges; ++i)
768 Ranges[i] = Info.getRange(i);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000769
Douglas Gregor87f95b02009-02-26 21:00:50 +0000770 unsigned NumHints = Info.getNumCodeModificationHints();
771 for (unsigned idx = 0; idx < NumHints; ++idx) {
772 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx);
773 if (Hint.RemoveRange.isValid()) {
774 assert(NumRanges < 20 && "Out of space");
775 Ranges[NumRanges++] = Hint.RemoveRange;
776 }
777 }
778
779 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
780 Info.getCodeModificationHints(),
Douglas Gregor48185532009-05-01 21:53:04 +0000781 Info.getNumCodeModificationHints(),
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000782 DiagOpts->MessageLength);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000783 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000784
Chris Lattner327984f2008-11-19 06:56:25 +0000785 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000786}