blob: 946a6caa1514e2f1456da4b3f0ab021c81ef1e99 [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;
Daniel Dunbaraa7d55a2010-02-25 03:23:40 +000034// Used for changing only the bold attribute.
Torok Edwinc91b6e02009-06-04 07:18:23 +000035static 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,
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000042 const DiagnosticOptions &diags,
43 bool _OwnsOutputStream)
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000044 : OS(os), LangOpts(0), DiagOpts(&diags),
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000045 LastCaretDiagnosticWasNote(0),
46 OwnsOutputStream(_OwnsOutputStream) {
47}
48
49TextDiagnosticPrinter::~TextDiagnosticPrinter() {
50 if (OwnsOutputStream)
51 delete &OS;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000052}
53
Bill Wendling37b1dde2007-06-07 09:34:54 +000054void TextDiagnosticPrinter::
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000055PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
56 if (Loc.isInvalid()) return;
Chris Lattnerdc5c0552007-07-20 16:37:10 +000057
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000058 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattnerdc5c0552007-07-20 16:37:10 +000059
Bill Wendling37b1dde2007-06-07 09:34:54 +000060 // Print out the other include frames first.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000061 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Chris Lattner92b29b2f2009-04-21 03:57:54 +000062
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000063 if (DiagOpts->ShowLocation)
Chris Lattner92b29b2f2009-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";
Bill Wendling37b1dde2007-06-07 09:34:54 +000068}
69
Bill Wendling37b1dde2007-06-07 09:34:54 +000070/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
71/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000072void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000073 const SourceManager &SM,
Chris Lattner71dc14b2009-01-17 08:45:21 +000074 unsigned LineNo, FileID FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000075 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000076 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000077 assert(CaretLine.size() == SourceLine.size() &&
78 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000079 if (!R.isValid()) return;
80
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000081 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
82 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
Daniel Dunbar49f0e802009-09-07 23:07:56 +000083
Chris Lattnerb42700f2009-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 Dunbar49f0e802009-09-07 23:07:56 +000091
Chris Lattner88ea93e2009-02-04 01:06:56 +000092 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000093 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000094 return; // No intersection.
Daniel Dunbar49f0e802009-09-07 23:07:56 +000095
Chris Lattner88ea93e2009-02-04 01:06:56 +000096 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000097 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000098 return; // No intersection.
Daniel Dunbar49f0e802009-09-07 23:07:56 +000099
Bill Wendling37b1dde2007-06-07 09:34:54 +0000100 // Compute the column number of the start.
101 unsigned StartColNo = 0;
102 if (StartLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +0000103 StartColNo = SM.getInstantiationColumnNumber(Begin);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000104 if (StartColNo) --StartColNo; // Zero base the col #.
105 }
106
Bill Wendling37b1dde2007-06-07 09:34:54 +0000107 // Compute the column number of the end.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000108 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000109 if (EndLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +0000110 EndColNo = SM.getInstantiationColumnNumber(End);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000111 if (EndColNo) {
112 --EndColNo; // Zero base the col #.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000113
Bill Wendling37b1dde2007-06-07 09:34:54 +0000114 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000115 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000116 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000117 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000118 }
119 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000120
Chris Lattnerfc38aa72010-02-12 18:52:52 +0000121 assert(StartColNo <= EndColNo && "Invalid range!");
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000122
Chris Lattnerfc38aa72010-02-12 18:52:52 +0000123 // Pick the first non-whitespace column.
124 while (StartColNo < SourceLine.size() &&
125 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
126 ++StartColNo;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000127
Bill Wendling37b1dde2007-06-07 09:34:54 +0000128 // Pick the last non-whitespace column.
Chris Lattnerfc38aa72010-02-12 18:52:52 +0000129 if (EndColNo > SourceLine.size())
Nuno Lopesd86aa932008-08-05 19:40:20 +0000130 EndColNo = SourceLine.size();
Chris Lattnerfc38aa72010-02-12 18:52:52 +0000131 while (EndColNo-1 &&
132 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
133 --EndColNo;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000134
Chris Lattnerfc38aa72010-02-12 18:52:52 +0000135 // If the start/end passed each other, then we are trying to highlight a range
136 // that just exists in whitespace, which must be some sort of other bug.
137 assert(StartColNo <= EndColNo && "Trying to highlight whitespace??");
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000138
Bill Wendling37b1dde2007-06-07 09:34:54 +0000139 // Fill the range with ~'s.
Nuno Lopesd86aa932008-08-05 19:40:20 +0000140 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000141 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000142}
143
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000144/// \brief When the source code line we want to print is too long for
145/// the terminal, select the "interesting" region.
146static void SelectInterestingSourceRegion(std::string &SourceLine,
147 std::string &CaretLine,
148 std::string &FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000149 unsigned EndOfCaretToken,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000150 unsigned Columns) {
151 if (CaretLine.size() > SourceLine.size())
152 SourceLine.resize(CaretLine.size(), ' ');
153
154 // Find the slice that we need to display the full caret line
155 // correctly.
156 unsigned CaretStart = 0, CaretEnd = CaretLine.size();
157 for (; CaretStart != CaretEnd; ++CaretStart)
158 if (!isspace(CaretLine[CaretStart]))
159 break;
160
161 for (; CaretEnd != CaretStart; --CaretEnd)
162 if (!isspace(CaretLine[CaretEnd - 1]))
163 break;
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000164
165 // Make sure we don't chop the string shorter than the caret token
166 // itself.
167 if (CaretEnd < EndOfCaretToken)
168 CaretEnd = EndOfCaretToken;
169
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000170 // If we have a fix-it line, make sure the slice includes all of the
171 // fix-it information.
172 if (!FixItInsertionLine.empty()) {
173 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
174 for (; FixItStart != FixItEnd; ++FixItStart)
175 if (!isspace(FixItInsertionLine[FixItStart]))
176 break;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000177
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000178 for (; FixItEnd != FixItStart; --FixItEnd)
179 if (!isspace(FixItInsertionLine[FixItEnd - 1]))
180 break;
181
182 if (FixItStart < CaretStart)
183 CaretStart = FixItStart;
184 if (FixItEnd > CaretEnd)
185 CaretEnd = FixItEnd;
186 }
187
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000188 // CaretLine[CaretStart, CaretEnd) contains all of the interesting
189 // parts of the caret line. While this slice is smaller than the
190 // number of columns we have, try to grow the slice to encompass
191 // more context.
192
193 // If the end of the interesting region comes before we run out of
194 // space in the terminal, start at the beginning of the line.
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000195 if (Columns > 3 && CaretEnd < Columns - 3)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000196 CaretStart = 0;
197
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000198 unsigned TargetColumns = Columns;
199 if (TargetColumns > 8)
200 TargetColumns -= 8; // Give us extra room for the ellipses.
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000201 unsigned SourceLength = SourceLine.size();
Douglas Gregor006fd382009-05-04 06:45:38 +0000202 while ((CaretEnd - CaretStart) < TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000203 bool ExpandedRegion = false;
204 // Move the start of the interesting region left until we've
205 // pulled in something else interesting.
Douglas Gregor006fd382009-05-04 06:45:38 +0000206 if (CaretStart == 1)
207 CaretStart = 0;
208 else if (CaretStart > 1) {
209 unsigned NewStart = CaretStart - 1;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000210
Douglas Gregor006fd382009-05-04 06:45:38 +0000211 // Skip over any whitespace we see here; we're looking for
212 // another bit of interesting text.
213 while (NewStart && isspace(SourceLine[NewStart]))
214 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000215
Douglas Gregor006fd382009-05-04 06:45:38 +0000216 // Skip over this bit of "interesting" text.
217 while (NewStart && !isspace(SourceLine[NewStart]))
218 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000219
Douglas Gregor006fd382009-05-04 06:45:38 +0000220 // Move up to the non-whitespace character we just saw.
221 if (NewStart)
222 ++NewStart;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000223
224 // If we're still within our limit, update the starting
225 // position within the source/caret line.
Douglas Gregor006fd382009-05-04 06:45:38 +0000226 if (CaretEnd - NewStart <= TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000227 CaretStart = NewStart;
228 ExpandedRegion = true;
229 }
230 }
231
232 // Move the end of the interesting region right until we've
233 // pulled in something else interesting.
Daniel Dunbar6f949912009-05-03 23:04:40 +0000234 if (CaretEnd != SourceLength) {
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000235 assert(CaretEnd < SourceLength && "Unexpected caret position!");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000236 unsigned NewEnd = CaretEnd;
237
238 // Skip over any whitespace we see here; we're looking for
239 // another bit of interesting text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000240 while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000241 ++NewEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000242
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000243 // Skip over this bit of "interesting" text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000244 while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000245 ++NewEnd;
246
247 if (NewEnd - CaretStart <= TargetColumns) {
248 CaretEnd = NewEnd;
249 ExpandedRegion = true;
250 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000251 }
Daniel Dunbar6f949912009-05-03 23:04:40 +0000252
253 if (!ExpandedRegion)
254 break;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000255 }
256
257 // [CaretStart, CaretEnd) is the slice we want. Update the various
258 // output lines to show only this slice, with two-space padding
259 // before the lines so that it looks nicer.
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000260 if (CaretEnd < SourceLine.size())
261 SourceLine.replace(CaretEnd, std::string::npos, "...");
Douglas Gregorcb516622009-05-03 15:24:25 +0000262 if (CaretEnd < CaretLine.size())
263 CaretLine.erase(CaretEnd, std::string::npos);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000264 if (FixItInsertionLine.size() > CaretEnd)
265 FixItInsertionLine.erase(CaretEnd, std::string::npos);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000266
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000267 if (CaretStart > 2) {
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000268 SourceLine.replace(0, CaretStart, " ...");
269 CaretLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000270 if (FixItInsertionLine.size() >= CaretStart)
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000271 FixItInsertionLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000272 }
273}
274
Chris Lattner1973d842009-02-20 00:18:51 +0000275void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner464ceb42009-02-20 00:25:28 +0000276 SourceRange *Ranges,
Chris Lattner1973d842009-02-20 00:18:51 +0000277 unsigned NumRanges,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000278 SourceManager &SM,
279 const CodeModificationHint *Hints,
Douglas Gregor48185532009-05-01 21:53:04 +0000280 unsigned NumHints,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000281 unsigned Columns) {
Daniel Dunbar02318952009-11-05 02:42:12 +0000282 assert(LangOpts && "Unexpected diagnostic outside source file processing");
Chris Lattner7b60a162009-02-17 08:44:50 +0000283 assert(!Loc.isInvalid() && "must have a valid source location here");
Chris Lattnerc45529b2009-05-05 22:03:18 +0000284
285 // If this is a macro ID, first emit information about where this was
Chris Lattner2474a7e2009-12-04 07:06:35 +0000286 // instantiated (recursively) then emit information about where the token was
Chris Lattnerc45529b2009-05-05 22:03:18 +0000287 // spelled from.
Chris Lattner7b60a162009-02-17 08:44:50 +0000288 if (!Loc.isFileID()) {
Chris Lattner24eb28b2009-02-18 18:50:45 +0000289 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Chris Lattnerc45529b2009-05-05 22:03:18 +0000290 // FIXME: Map ranges?
Douglas Gregor080fe612009-05-06 04:43:47 +0000291 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns);
Chris Lattner464ceb42009-02-20 00:25:28 +0000292
Chris Lattner2474a7e2009-12-04 07:06:35 +0000293 // Map the location.
Chris Lattnerc45529b2009-05-05 22:03:18 +0000294 Loc = SM.getImmediateSpellingLoc(Loc);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000295
Chris Lattner464ceb42009-02-20 00:25:28 +0000296 // Map the ranges.
297 for (unsigned i = 0; i != NumRanges; ++i) {
298 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
Chris Lattnerc45529b2009-05-05 22:03:18 +0000299 if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S);
300 if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E);
Chris Lattner464ceb42009-02-20 00:25:28 +0000301 Ranges[i] = SourceRange(S, E);
302 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000303
Chris Lattner2474a7e2009-12-04 07:06:35 +0000304 // Get the pretty name, according to #line directives etc.
305 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000306
Chris Lattner2474a7e2009-12-04 07:06:35 +0000307 // If this diagnostic is not in the main file, print out the "included from"
308 // lines.
309 if (LastWarningLoc != PLoc.getIncludeLoc()) {
310 LastWarningLoc = PLoc.getIncludeLoc();
311 PrintIncludeStack(LastWarningLoc, SM);
312 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000313
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000314 if (DiagOpts->ShowLocation) {
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000315 // Emit the file/line/column that this expansion came from.
Chris Lattner2474a7e2009-12-04 07:06:35 +0000316 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000317 if (DiagOpts->ShowColumn)
Chris Lattner2474a7e2009-12-04 07:06:35 +0000318 OS << PLoc.getColumn() << ':';
Chris Lattner92b29b2f2009-04-21 03:57:54 +0000319 OS << ' ';
320 }
321 OS << "note: instantiated from:\n";
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000322
Douglas Gregor080fe612009-05-06 04:43:47 +0000323 EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns);
Chris Lattnerc45529b2009-05-05 22:03:18 +0000324 return;
Chris Lattner7b60a162009-02-17 08:44:50 +0000325 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000326
Chris Lattnerd22603c2009-02-17 07:51:53 +0000327 // Decompose the location into a FID/Offset pair.
328 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
329 FileID FID = LocInfo.first;
330 unsigned FileOffset = LocInfo.second;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000331
Chris Lattnerd22603c2009-02-17 07:51:53 +0000332 // Get information about the buffer it points into.
Douglas Gregore0fbb832010-03-16 00:06:06 +0000333 bool Invalid = false;
Douglas Gregor802b7762010-03-15 22:54:52 +0000334 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID,
Douglas Gregore0fbb832010-03-16 00:06:06 +0000335 &Invalid);
336 if (Invalid)
Douglas Gregor802b7762010-03-15 22:54:52 +0000337 return;
338
Douglas Gregore0fbb832010-03-16 00:06:06 +0000339 const char *BufStart = BufferInfo.first;
340
Chris Lattnerd22603c2009-02-17 07:51:53 +0000341 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000342 unsigned CaretEndColNo
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000343 = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts);
344
Chris Lattner2cf68c12009-02-17 07:38:37 +0000345 // Rewind from the current position to the start of the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000346 const char *TokPtr = BufStart+FileOffset;
347 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000348
349
Chris Lattner2cf68c12009-02-17 07:38:37 +0000350 // Compute the line end. Scan forward from the error position to the end of
351 // the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000352 const char *LineEnd = TokPtr;
Chris Lattner139d87b2009-03-08 08:11:22 +0000353 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
Chris Lattner2cf68c12009-02-17 07:38:37 +0000354 ++LineEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000355
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000356 // FIXME: This shouldn't be necessary, but the CaretEndColNo can extend past
357 // the source line length as currently being computed. See
358 // test/Misc/message-length.c.
359 CaretEndColNo = std::min(CaretEndColNo, unsigned(LineEnd - LineStart));
360
Chris Lattner2cf68c12009-02-17 07:38:37 +0000361 // Copy the line of code into an std::string for ease of manipulation.
362 std::string SourceLine(LineStart, LineEnd);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000363
Chris Lattner2cf68c12009-02-17 07:38:37 +0000364 // Create a line for the caret that is filled with spaces that is the same
365 // length as the line of source code.
366 std::string CaretLine(LineEnd-LineStart, ' ');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000367
Chris Lattner2cf68c12009-02-17 07:38:37 +0000368 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner1973d842009-02-20 00:18:51 +0000369 if (NumRanges) {
Chris Lattnerd22603c2009-02-17 07:51:53 +0000370 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000371
Chris Lattner1973d842009-02-20 00:18:51 +0000372 for (unsigned i = 0, e = NumRanges; i != e; ++i)
373 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattnerd22603c2009-02-17 07:51:53 +0000374 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000375
Chris Lattner2cf68c12009-02-17 07:38:37 +0000376 // Next, insert the caret itself.
377 if (ColNo-1 < CaretLine.size())
378 CaretLine[ColNo-1] = '^';
379 else
380 CaretLine.push_back('^');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000381
Chris Lattner2cf68c12009-02-17 07:38:37 +0000382 // Scan the source line, looking for tabs. If we find any, manually expand
Chris Lattner3d756f12010-01-13 03:06:50 +0000383 // them to spaces and update the CaretLine to match.
Chris Lattner2cf68c12009-02-17 07:38:37 +0000384 for (unsigned i = 0; i != SourceLine.size(); ++i) {
385 if (SourceLine[i] != '\t') continue;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000386
Chris Lattner2cf68c12009-02-17 07:38:37 +0000387 // Replace this tab with at least one space.
388 SourceLine[i] = ' ';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000389
Chris Lattner2cf68c12009-02-17 07:38:37 +0000390 // Compute the number of spaces we need to insert.
Chris Lattner3d756f12010-01-13 03:06:50 +0000391 unsigned TabStop = DiagOpts->TabStop;
392 assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
393 "Invalid -ftabstop value");
Chris Lattnere23003d2010-01-09 21:54:33 +0000394 unsigned NumSpaces = ((i+TabStop)/TabStop * TabStop) - (i+1);
395 assert(NumSpaces < TabStop && "Invalid computation of space amt");
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000396
Chris Lattner2cf68c12009-02-17 07:38:37 +0000397 // Insert spaces into the SourceLine.
398 SourceLine.insert(i+1, NumSpaces, ' ');
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000399
Chris Lattner2cf68c12009-02-17 07:38:37 +0000400 // Insert spaces or ~'s into CaretLine.
401 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
402 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000403
Chris Lattneraf6094e2009-04-28 22:33:16 +0000404 // If we are in -fdiagnostics-print-source-range-info mode, we are trying to
405 // produce easily machine parsable output. Add a space before the source line
406 // and the caret to make it trivial to tell the main diagnostic line from what
407 // the user is intended to see.
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000408 if (DiagOpts->ShowSourceRanges) {
Chris Lattneraf6094e2009-04-28 22:33:16 +0000409 SourceLine = ' ' + SourceLine;
410 CaretLine = ' ' + CaretLine;
411 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000412
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000413 std::string FixItInsertionLine;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000414 if (NumHints && DiagOpts->ShowFixits) {
Chris Lattner29d34ca2009-04-19 07:44:08 +0000415 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints;
Douglas Gregor87f95b02009-02-26 21:00:50 +0000416 Hint != LastHint; ++Hint) {
417 if (Hint->InsertionLoc.isValid()) {
418 // We have an insertion hint. Determine whether the inserted
419 // code is on the same line as the caret.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000420 std::pair<FileID, unsigned> HintLocInfo
Chris Lattnere29509a2009-03-02 20:58:48 +0000421 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000422 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
423 SM.getLineNumber(FID, FileOffset)) {
424 // Insert the new code into the line just below the code
425 // that the user wrote.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000426 unsigned HintColNo
Douglas Gregor87f95b02009-02-26 21:00:50 +0000427 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000428 unsigned LastColumnModified
Douglas Gregor87f95b02009-02-26 21:00:50 +0000429 = HintColNo - 1 + Hint->CodeToInsert.size();
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000430 if (LastColumnModified > FixItInsertionLine.size())
431 FixItInsertionLine.resize(LastColumnModified, ' ');
Douglas Gregor87f95b02009-02-26 21:00:50 +0000432 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(),
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000433 FixItInsertionLine.begin() + HintColNo - 1);
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000434 } else {
435 FixItInsertionLine.clear();
436 break;
Douglas Gregor87f95b02009-02-26 21:00:50 +0000437 }
438 }
439 }
Douglas Gregor9a48db12010-01-18 19:28:01 +0000440 // Now that we have the entire fixit line, expand the tabs in it.
441 // Since we don't want to insert spaces in the middle of a word,
442 // find each word and the column it should line up with and insert
443 // spaces until they match.
444 if (!FixItInsertionLine.empty()) {
445 unsigned FixItPos = 0;
446 unsigned LinePos = 0;
447 unsigned TabExpandedCol = 0;
448 unsigned LineLength = LineEnd - LineStart;
449
450 while (FixItPos < FixItInsertionLine.size() && LinePos < LineLength) {
451 // Find the next word in the FixIt line.
452 while (FixItPos < FixItInsertionLine.size() &&
453 FixItInsertionLine[FixItPos] == ' ')
454 ++FixItPos;
455 unsigned CharDistance = FixItPos - TabExpandedCol;
456
457 // Walk forward in the source line, keeping track of
458 // the tab-expanded column.
459 for (unsigned I = 0; I < CharDistance; ++I, ++LinePos)
460 if (LinePos >= LineLength || LineStart[LinePos] != '\t')
461 ++TabExpandedCol;
462 else
463 TabExpandedCol =
464 (TabExpandedCol/DiagOpts->TabStop + 1) * DiagOpts->TabStop;
465
466 // Adjust the fixit line to match this column.
467 FixItInsertionLine.insert(FixItPos, TabExpandedCol-FixItPos, ' ');
468 FixItPos = TabExpandedCol;
469
470 // Walk to the end of the word.
471 while (FixItPos < FixItInsertionLine.size() &&
472 FixItInsertionLine[FixItPos] != ' ')
473 ++FixItPos;
474 }
475 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000476 }
Douglas Gregor87f95b02009-02-26 21:00:50 +0000477
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000478 // If the source line is too long for our terminal, select only the
479 // "interesting" source region within that line.
480 if (Columns && SourceLine.size() > Columns)
481 SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000482 CaretEndColNo, Columns);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000483
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000484 // Finally, remove any blank spaces from the end of CaretLine.
485 while (CaretLine[CaretLine.size()-1] == ' ')
486 CaretLine.erase(CaretLine.end()-1);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000487
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000488 // Emit what we have computed.
489 OS << SourceLine << '\n';
Torok Edwinc91b6e02009-06-04 07:18:23 +0000490
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000491 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000492 OS.changeColor(caretColor, true);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000493 OS << CaretLine << '\n';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000494 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000495 OS.resetColor();
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000496
497 if (!FixItInsertionLine.empty()) {
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000498 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000499 // Print fixit line in color
500 OS.changeColor(fixitColor, false);
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000501 if (DiagOpts->ShowSourceRanges)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000502 OS << ' ';
503 OS << FixItInsertionLine << '\n';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000504 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000505 OS.resetColor();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000506 }
Chris Lattner2cf68c12009-02-17 07:38:37 +0000507}
508
Douglas Gregor48185532009-05-01 21:53:04 +0000509/// \brief Skip over whitespace in the string, starting at the given
510/// index.
511///
512/// \returns The index of the first non-whitespace character that is
513/// greater than or equal to Idx or, if no such character exists,
514/// returns the end of the string.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000515static unsigned skipWhitespace(unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +0000516 const llvm::SmallVectorImpl<char> &Str,
Douglas Gregor48185532009-05-01 21:53:04 +0000517 unsigned Length) {
518 while (Idx < Length && isspace(Str[Idx]))
519 ++Idx;
520 return Idx;
521}
522
523/// \brief If the given character is the start of some kind of
524/// balanced punctuation (e.g., quotes or parentheses), return the
525/// character that will terminate the punctuation.
526///
527/// \returns The ending punctuation character, if any, or the NULL
528/// character if the input character does not start any punctuation.
529static inline char findMatchingPunctuation(char c) {
530 switch (c) {
531 case '\'': return '\'';
532 case '`': return '\'';
533 case '"': return '"';
534 case '(': return ')';
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000535 case '[': return ']';
Douglas Gregor48185532009-05-01 21:53:04 +0000536 case '{': return '}';
537 default: break;
538 }
539
540 return 0;
541}
542
543/// \brief Find the end of the word starting at the given offset
544/// within a string.
545///
546/// \returns the index pointing one character past the end of the
547/// word.
Daniel Dunbara6cb9f22009-12-06 09:56:18 +0000548static unsigned findEndOfWord(unsigned Start,
549 const llvm::SmallVectorImpl<char> &Str,
550 unsigned Length, unsigned Column,
551 unsigned Columns) {
552 assert(Start < Str.size() && "Invalid start position!");
Douglas Gregor48185532009-05-01 21:53:04 +0000553 unsigned End = Start + 1;
554
Daniel Dunbara6cb9f22009-12-06 09:56:18 +0000555 // If we are already at the end of the string, take that as the word.
556 if (End == Str.size())
557 return End;
558
Douglas Gregor48185532009-05-01 21:53:04 +0000559 // Determine if the start of the string is actually opening
560 // punctuation, e.g., a quote or parentheses.
561 char EndPunct = findMatchingPunctuation(Str[Start]);
562 if (!EndPunct) {
563 // This is a normal word. Just find the first space character.
564 while (End < Length && !isspace(Str[End]))
565 ++End;
566 return End;
567 }
568
569 // We have the start of a balanced punctuation sequence (quotes,
570 // parentheses, etc.). Determine the full sequence is.
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000571 llvm::SmallString<16> PunctuationEndStack;
Douglas Gregor48185532009-05-01 21:53:04 +0000572 PunctuationEndStack.push_back(EndPunct);
573 while (End < Length && !PunctuationEndStack.empty()) {
574 if (Str[End] == PunctuationEndStack.back())
575 PunctuationEndStack.pop_back();
576 else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
577 PunctuationEndStack.push_back(SubEndPunct);
578
579 ++End;
580 }
581
582 // Find the first space character after the punctuation ended.
583 while (End < Length && !isspace(Str[End]))
584 ++End;
585
586 unsigned PunctWordLength = End - Start;
587 if (// If the word fits on this line
588 Column + PunctWordLength <= Columns ||
589 // ... or the word is "short enough" to take up the next line
590 // without too much ugly white space
591 PunctWordLength < Columns/3)
592 return End; // Take the whole thing as a single "word".
593
594 // The whole quoted/parenthesized string is too long to print as a
595 // single "word". Instead, find the "word" that starts just after
596 // the punctuation and use that end-point instead. This will recurse
597 // until it finds something small enough to consider a word.
598 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
599}
600
601/// \brief Print the given string to a stream, word-wrapping it to
602/// some number of columns in the process.
603///
604/// \brief OS the stream to which the word-wrapping string will be
605/// emitted.
606///
607/// \brief Str the string to word-wrap and output.
608///
609/// \brief Columns the number of columns to word-wrap to.
610///
611/// \brief Column the column number at which the first character of \p
612/// Str will be printed. This will be non-zero when part of the first
613/// line has already been printed.
614///
615/// \brief Indentation the number of spaces to indent any lines beyond
616/// the first line.
617///
618/// \returns true if word-wrapping was required, or false if the
619/// string fit on the first line.
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000620static bool PrintWordWrapped(llvm::raw_ostream &OS,
Mike Stump11289f42009-09-09 15:08:12 +0000621 const llvm::SmallVectorImpl<char> &Str,
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000622 unsigned Columns,
Douglas Gregor48185532009-05-01 21:53:04 +0000623 unsigned Column = 0,
624 unsigned Indentation = WordWrapIndentation) {
625 unsigned Length = Str.size();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000626
Douglas Gregor48185532009-05-01 21:53:04 +0000627 // If there is a newline in this message somewhere, find that
628 // newline and split the message into the part before the newline
629 // (which will be word-wrapped) and the part from the newline one
630 // (which will be emitted unchanged).
631 for (unsigned I = 0; I != Length; ++I)
632 if (Str[I] == '\n') {
633 Length = I;
634 break;
635 }
636
637 // The string used to indent each line.
638 llvm::SmallString<16> IndentStr;
639 IndentStr.assign(Indentation, ' ');
640 bool Wrapped = false;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000641 for (unsigned WordStart = 0, WordEnd; WordStart < Length;
Douglas Gregor48185532009-05-01 21:53:04 +0000642 WordStart = WordEnd) {
643 // Find the beginning of the next word.
644 WordStart = skipWhitespace(WordStart, Str, Length);
645 if (WordStart == Length)
646 break;
647
648 // Find the end of this word.
649 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000650
Douglas Gregor48185532009-05-01 21:53:04 +0000651 // Does this word fit on the current line?
652 unsigned WordLength = WordEnd - WordStart;
653 if (Column + WordLength < Columns) {
654 // This word fits on the current line; print it there.
655 if (WordStart) {
656 OS << ' ';
657 Column += 1;
658 }
659 OS.write(&Str[WordStart], WordLength);
660 Column += WordLength;
661 continue;
662 }
663
664 // This word does not fit on the current line, so wrap to the next
665 // line.
Douglas Gregor6f5a38b2009-05-03 03:52:38 +0000666 OS << '\n';
667 OS.write(&IndentStr[0], Indentation);
Douglas Gregor48185532009-05-01 21:53:04 +0000668 OS.write(&Str[WordStart], WordLength);
669 Column = Indentation + WordLength;
670 Wrapped = true;
671 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000672
Douglas Gregor48185532009-05-01 21:53:04 +0000673 if (Length == Str.size())
674 return Wrapped; // We're done.
675
676 // There is a newline in the message, followed by something that
677 // will not be word-wrapped. Print that.
678 OS.write(&Str[Length], Str.size() - Length);
679 return true;
680}
Chris Lattner2cf68c12009-02-17 07:38:37 +0000681
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000682void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
Chris Lattner8488c822008-11-18 07:04:44 +0000683 const DiagnosticInfo &Info) {
Douglas Gregor48185532009-05-01 21:53:04 +0000684 // Keeps track of the the starting position of the location
685 // information (e.g., "foo.c:10:4:") that precedes the error
686 // message. We use this information to determine how long the
687 // file+line+column number prefix is.
688 uint64_t StartOfLocationInfo = OS.tell();
689
Daniel Dunbaraa7d55a2010-02-25 03:23:40 +0000690 if (!Prefix.empty())
691 OS << Prefix << ": ";
692
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000693 // If the location is specified, print out a file/line/col and include trace
694 // if enabled.
695 if (Info.getLocation().isValid()) {
Ted Kremenek52418322009-01-28 20:47:47 +0000696 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000697 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
698 unsigned LineNo = PLoc.getLine();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000699
Bill Wendling37b1dde2007-06-07 09:34:54 +0000700 // First, if this diagnostic is not in the main file, print out the
701 // "included from" lines.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000702 if (LastWarningLoc != PLoc.getIncludeLoc()) {
703 LastWarningLoc = PLoc.getIncludeLoc();
704 PrintIncludeStack(LastWarningLoc, SM);
Douglas Gregor48185532009-05-01 21:53:04 +0000705 StartOfLocationInfo = OS.tell();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000706 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000707
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000708 // Compute the column number.
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000709 if (DiagOpts->ShowLocation) {
710 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000711 OS.changeColor(savedColor, true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000712
Steve Naroff4fb3d9f2009-12-05 02:14:08 +0000713 // Emit a Visual Studio compatible line number syntax.
Steve Naroffc275f702009-12-05 12:23:07 +0000714 if (LangOpts && LangOpts->Microsoft) {
Steve Naroff4fb3d9f2009-12-05 02:14:08 +0000715 OS << PLoc.getFilename() << '(' << LineNo << ')';
716 OS << " : ";
717 } else {
718 OS << PLoc.getFilename() << ':' << LineNo << ':';
719 if (DiagOpts->ShowColumn)
720 if (unsigned ColNo = PLoc.getColumn())
721 OS << ColNo << ':';
722 }
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000723 if (DiagOpts->ShowSourceRanges && Info.getNumRanges()) {
Chris Lattner44219f32009-03-13 01:08:23 +0000724 FileID CaretFileID =
725 SM.getFileID(SM.getInstantiationLoc(Info.getLocation()));
726 bool PrintedRange = false;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000727
Chris Lattner44219f32009-03-13 01:08:23 +0000728 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) {
Chris Lattner3251e3c2009-04-19 22:24:10 +0000729 // Ignore invalid ranges.
730 if (!Info.getRange(i).isValid()) continue;
731
Chris Lattner44219f32009-03-13 01:08:23 +0000732 SourceLocation B = Info.getRange(i).getBegin();
733 SourceLocation E = Info.getRange(i).getEnd();
Chris Lattner6ed7d592009-06-15 05:18:27 +0000734 B = SM.getInstantiationLoc(B);
Chris Lattner44219f32009-03-13 01:08:23 +0000735 E = SM.getInstantiationLoc(E);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000736
Chris Lattner6ed7d592009-06-15 05:18:27 +0000737 // If the End location and the start location are the same and are a
738 // macro location, then the range was something that came from a macro
739 // expansion or _Pragma. If this is an object-like macro, the best we
740 // can do is to highlight the range. If this is a function-like
741 // macro, we'd also like to highlight the arguments.
742 if (B == E && Info.getRange(i).getEnd().isMacroID())
743 E = SM.getInstantiationRange(Info.getRange(i).getEnd()).second;
744
745 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
Chris Lattner44219f32009-03-13 01:08:23 +0000746 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000747
Chris Lattner44219f32009-03-13 01:08:23 +0000748 // If the start or end of the range is in another file, just discard
749 // it.
750 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
751 continue;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000752
Chris Lattner44219f32009-03-13 01:08:23 +0000753 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000754 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000755
Chris Lattner44219f32009-03-13 01:08:23 +0000756 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
757 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
758 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
759 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}';
760 PrintedRange = true;
761 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000762
Chris Lattner44219f32009-03-13 01:08:23 +0000763 if (PrintedRange)
764 OS << ':';
765 }
Chris Lattner19586772009-01-30 17:41:53 +0000766 OS << ' ';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000767 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000768 OS.resetColor();
769 }
770 }
771
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000772 if (DiagOpts->ShowColors) {
Torok Edwinc91b6e02009-06-04 07:18:23 +0000773 // Print diagnostic category in bold and color
774 switch (Level) {
775 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
776 case Diagnostic::Note: OS.changeColor(noteColor, true); break;
777 case Diagnostic::Warning: OS.changeColor(warningColor, true); break;
778 case Diagnostic::Error: OS.changeColor(errorColor, true); break;
779 case Diagnostic::Fatal: OS.changeColor(fatalColor, true); break;
Chris Lattner19586772009-01-30 17:41:53 +0000780 }
Bill Wendling37b1dde2007-06-07 09:34:54 +0000781 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000782
Bill Wendling37b1dde2007-06-07 09:34:54 +0000783 switch (Level) {
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000784 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman36c49272008-04-17 18:06:57 +0000785 case Diagnostic::Note: OS << "note: "; break;
786 case Diagnostic::Warning: OS << "warning: "; break;
787 case Diagnostic::Error: OS << "error: "; break;
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000788 case Diagnostic::Fatal: OS << "fatal error: "; break;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000789 }
Torok Edwinc91b6e02009-06-04 07:18:23 +0000790
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000791 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000792 OS.resetColor();
793
Chris Lattner23be0672008-11-19 06:51:40 +0000794 llvm::SmallString<100> OutStr;
795 Info.FormatDiagnostic(OutStr);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000796
Chris Lattnerfff5ed42010-02-16 18:29:31 +0000797 if (DiagOpts->ShowOptionNames) {
Douglas Gregor48185532009-05-01 21:53:04 +0000798 if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) {
799 OutStr += " [-W";
800 OutStr += Opt;
801 OutStr += ']';
Chris Lattnerfff5ed42010-02-16 18:29:31 +0000802 } else if (Diagnostic::isBuiltinExtensionDiag(Info.getID())) {
803 OutStr += " [-pedantic]";
Douglas Gregor48185532009-05-01 21:53:04 +0000804 }
Chris Lattnerfff5ed42010-02-16 18:29:31 +0000805 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000806
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000807 if (DiagOpts->ShowColors) {
Torok Edwinc91b6e02009-06-04 07:18:23 +0000808 // Print warnings, errors and fatal errors in bold, no color
809 switch (Level) {
810 case Diagnostic::Warning: OS.changeColor(savedColor, true); break;
811 case Diagnostic::Error: OS.changeColor(savedColor, true); break;
812 case Diagnostic::Fatal: OS.changeColor(savedColor, true); break;
813 default: break; //don't bold notes
814 }
815 }
816
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000817 if (DiagOpts->MessageLength) {
Douglas Gregor48185532009-05-01 21:53:04 +0000818 // We will be word-wrapping the error message, so compute the
819 // column number where we currently are (after printing the
820 // location information).
821 unsigned Column = OS.tell() - StartOfLocationInfo;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000822 PrintWordWrapped(OS, OutStr, DiagOpts->MessageLength, Column);
Douglas Gregor48185532009-05-01 21:53:04 +0000823 } else {
824 OS.write(OutStr.begin(), OutStr.size());
825 }
Chris Lattner23be0672008-11-19 06:51:40 +0000826 OS << '\n';
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000827 if (DiagOpts->ShowColors)
Torok Edwinc91b6e02009-06-04 07:18:23 +0000828 OS.resetColor();
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000829
Douglas Gregor79cf6032009-03-10 20:44:00 +0000830 // If caret diagnostics are enabled and we have location, we want to
831 // emit the caret. However, we only do this if the location moved
832 // from the last diagnostic, if the last diagnostic was a note that
833 // was part of a different warning or error diagnostic, or if the
834 // diagnostic has ranges. We don't want to emit the same caret
835 // multiple times if one loc has multiple diagnostics.
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000836 if (DiagOpts->ShowCarets && Info.getLocation().isValid() &&
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000837 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
Douglas Gregor79cf6032009-03-10 20:44:00 +0000838 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) ||
Douglas Gregor87f95b02009-02-26 21:00:50 +0000839 Info.getNumCodeModificationHints())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000840 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000841 LastLoc = Info.getLocation();
Douglas Gregor79cf6032009-03-10 20:44:00 +0000842 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000843
Chris Lattner1973d842009-02-20 00:18:51 +0000844 // Get the ranges into a local array we can hack on.
Douglas Gregor87f95b02009-02-26 21:00:50 +0000845 SourceRange Ranges[20];
Chris Lattner1973d842009-02-20 00:18:51 +0000846 unsigned NumRanges = Info.getNumRanges();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000847 assert(NumRanges < 20 && "Out of space");
Chris Lattner1973d842009-02-20 00:18:51 +0000848 for (unsigned i = 0; i != NumRanges; ++i)
849 Ranges[i] = Info.getRange(i);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000850
Douglas Gregor87f95b02009-02-26 21:00:50 +0000851 unsigned NumHints = Info.getNumCodeModificationHints();
852 for (unsigned idx = 0; idx < NumHints; ++idx) {
853 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx);
854 if (Hint.RemoveRange.isValid()) {
855 assert(NumRanges < 20 && "Out of space");
856 Ranges[NumRanges++] = Hint.RemoveRange;
857 }
858 }
859
860 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
861 Info.getCodeModificationHints(),
Douglas Gregor48185532009-05-01 21:53:04 +0000862 Info.getNumCodeModificationHints(),
Daniel Dunbarc2e6a472009-11-04 06:24:30 +0000863 DiagOpts->MessageLength);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000864 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000865
Chris Lattner327984f2008-11-19 06:56:25 +0000866 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000867}