blob: d5e3157b064e898e1ad4e8d66647b1e028c66472 [file] [log] [blame]
Chris Lattner1b30e1ac2009-06-21 03:36:54 +00001//===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===//
Chris Lattner8db9bc72009-03-13 07:05:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner1b30e1ac2009-06-21 03:36:54 +000010// This file implements the SourceMgr class. This class is used as a simple
11// substrate for diagnostics, #include handling, and other low level things for
12// simple parsers.
Chris Lattner8db9bc72009-03-13 07:05:43 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner1b30e1ac2009-06-21 03:36:54 +000016#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Twine.h"
Jordan Roseefd8f802013-01-10 18:50:15 +000018#include "llvm/Support/Locale.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/MemoryBuffer.h"
Yaron Keren15217202014-05-16 13:16:30 +000020#include "llvm/Support/Path.h"
Chris Lattner8db9bc72009-03-13 07:05:43 +000021#include "llvm/Support/raw_ostream.h"
22using namespace llvm;
23
Jordan Roseefd8f802013-01-10 18:50:15 +000024static const size_t TabStop = 8;
25
Chris Lattner5d47e932009-08-11 17:49:14 +000026namespace {
27 struct LineNoCacheTy {
Alp Tokera55b95b2014-07-06 10:33:31 +000028 unsigned LastQueryBufferID;
Chris Lattner5d47e932009-08-11 17:49:14 +000029 const char *LastQuery;
30 unsigned LineNoOfQuery;
31 };
32}
33
34static LineNoCacheTy *getCache(void *Ptr) {
35 return (LineNoCacheTy*)Ptr;
36}
37
38
Chris Lattnerfd255752009-06-21 03:41:50 +000039SourceMgr::~SourceMgr() {
Chris Lattner5d47e932009-08-11 17:49:14 +000040 // Delete the line # cache if allocated.
41 if (LineNoCacheTy *Cache = getCache(LineNoCache))
42 delete Cache;
Chris Lattner8db9bc72009-03-13 07:05:43 +000043}
44
Dmitri Gribenkoa5b27a72014-07-09 08:30:15 +000045unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
46 SMLoc IncludeLoc,
47 std::string &IncludedFile) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000048 IncludedFile = Filename;
Rafael Espindolaadf21f22014-07-06 17:43:13 +000049 ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
Matt Arsenault6e863d12014-11-06 01:13:27 +000050 MemoryBuffer::getFile(IncludedFile);
Chris Lattner976af622009-06-21 05:06:04 +000051
52 // If the file didn't exist directly, see if it's in an include path.
Rafael Espindolaadf21f22014-07-06 17:43:13 +000053 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
54 ++i) {
55 IncludedFile =
56 IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
Matt Arsenault6e863d12014-11-06 01:13:27 +000057 NewBufOrErr = MemoryBuffer::getFile(IncludedFile);
Chris Lattner976af622009-06-21 05:06:04 +000058 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000059
Rafael Espindolaadf21f22014-07-06 17:43:13 +000060 if (!NewBufOrErr)
Alp Tokera55b95b2014-07-06 10:33:31 +000061 return 0;
Chris Lattner976af622009-06-21 05:06:04 +000062
David Blaikie1961f142014-08-21 20:44:56 +000063 return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
Chris Lattner976af622009-06-21 05:06:04 +000064}
65
Alp Tokera55b95b2014-07-06 10:33:31 +000066unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +000067 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner87710ca2009-03-13 16:01:53 +000068 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattner0f6dc782009-03-18 20:36:45 +000069 // Use <= here so that a pointer to the null at the end of the buffer
70 // is included as part of the buffer.
71 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Alp Tokera55b95b2014-07-06 10:33:31 +000072 return i + 1;
73 return 0;
Chris Lattner8db9bc72009-03-13 07:05:43 +000074}
75
Chris Lattner9322ba82012-05-05 22:17:32 +000076std::pair<unsigned, unsigned>
Alp Tokera55b95b2014-07-06 10:33:31 +000077SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const {
78 if (!BufferID)
79 BufferID = FindBufferContainingLoc(Loc);
80 assert(BufferID && "Invalid Location!");
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000081
Alp Toker7899d502014-06-25 00:41:15 +000082 const MemoryBuffer *Buff = getMemoryBuffer(BufferID);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000083
Chris Lattner8db9bc72009-03-13 07:05:43 +000084 // Count the number of \n's between the start of the file and the specified
85 // location.
86 unsigned LineNo = 1;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000087
Chris Lattner9322ba82012-05-05 22:17:32 +000088 const char *BufStart = Buff->getBufferStart();
89 const char *Ptr = BufStart;
Chris Lattner8db9bc72009-03-13 07:05:43 +000090
Chris Lattner5d47e932009-08-11 17:49:14 +000091 // If we have a line number cache, and if the query is to a later point in the
92 // same file, start searching from the last query location. This optimizes
93 // for the case when multiple diagnostics come out of one file in order.
94 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000095 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner5d47e932009-08-11 17:49:14 +000096 Cache->LastQuery <= Loc.getPointer()) {
97 Ptr = Cache->LastQuery;
98 LineNo = Cache->LineNoOfQuery;
99 }
100
101 // Scan for the location being queried, keeping track of the number of lines
102 // we see.
Chris Lattner526c8cb2009-06-21 03:39:35 +0000103 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattner8db9bc72009-03-13 07:05:43 +0000104 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000105
Chris Lattner5d47e932009-08-11 17:49:14 +0000106 // Allocate the line number cache if it doesn't exist.
Craig Topper8d399f82014-04-09 04:20:00 +0000107 if (!LineNoCache)
Chris Lattner5d47e932009-08-11 17:49:14 +0000108 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000109
Chris Lattner5d47e932009-08-11 17:49:14 +0000110 // Update the line # cache.
111 LineNoCacheTy &Cache = *getCache(LineNoCache);
112 Cache.LastQueryBufferID = BufferID;
113 Cache.LastQuery = Ptr;
114 Cache.LineNoOfQuery = LineNo;
Chris Lattner9322ba82012-05-05 22:17:32 +0000115
116 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
Matt Beaumont-Gaya1b3b002012-05-07 18:12:42 +0000117 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
Chris Lattner9322ba82012-05-05 22:17:32 +0000118 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000119}
120
Chris Lattnercc64cc92009-07-02 22:24:20 +0000121void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner526c8cb2009-06-21 03:39:35 +0000122 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000123
Alp Tokera55b95b2014-07-06 10:33:31 +0000124 unsigned CurBuf = FindBufferContainingLoc(IncludeLoc);
125 assert(CurBuf && "Invalid or unspecified location!");
Chris Lattner8db9bc72009-03-13 07:05:43 +0000126
Chris Lattnercc64cc92009-07-02 22:24:20 +0000127 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000128
Chris Lattnercc64cc92009-07-02 22:24:20 +0000129 OS << "Included from "
130 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
131 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattner8db9bc72009-03-13 07:05:43 +0000132}
133
134
Chris Lattner03b80a42011-10-16 05:43:57 +0000135SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
Chris Lattner72845262011-10-16 05:47:55 +0000136 const Twine &Msg,
Jordan Roseefd8f802013-01-10 18:50:15 +0000137 ArrayRef<SMRange> Ranges,
138 ArrayRef<SMFixIt> FixIts) const {
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000139
Chris Lattner8db9bc72009-03-13 07:05:43 +0000140 // First thing to do: find the current buffer containing the specified
Chris Lattner854f3662012-05-06 16:20:49 +0000141 // location to pull out the source line.
Chris Lattnera3a06812011-10-16 04:47:35 +0000142 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
Chris Lattner854f3662012-05-06 16:20:49 +0000143 std::pair<unsigned, unsigned> LineAndCol;
144 const char *BufferID = "<unknown>";
145 std::string LineStr;
Chris Lattnera3a06812011-10-16 04:47:35 +0000146
Chris Lattner854f3662012-05-06 16:20:49 +0000147 if (Loc.isValid()) {
Alp Tokera55b95b2014-07-06 10:33:31 +0000148 unsigned CurBuf = FindBufferContainingLoc(Loc);
149 assert(CurBuf && "Invalid or unspecified location!");
Chris Lattner854f3662012-05-06 16:20:49 +0000150
Alp Toker7899d502014-06-25 00:41:15 +0000151 const MemoryBuffer *CurMB = getMemoryBuffer(CurBuf);
Chris Lattner854f3662012-05-06 16:20:49 +0000152 BufferID = CurMB->getBufferIdentifier();
153
154 // Scan backward to find the start of the line.
155 const char *LineStart = Loc.getPointer();
156 const char *BufStart = CurMB->getBufferStart();
157 while (LineStart != BufStart && LineStart[-1] != '\n' &&
158 LineStart[-1] != '\r')
159 --LineStart;
160
161 // Get the end of the line.
162 const char *LineEnd = Loc.getPointer();
163 const char *BufEnd = CurMB->getBufferEnd();
164 while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
165 ++LineEnd;
166 LineStr = std::string(LineStart, LineEnd);
167
168 // Convert any ranges to column ranges that only intersect the line of the
169 // location.
170 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
171 SMRange R = Ranges[i];
172 if (!R.isValid()) continue;
173
174 // If the line doesn't contain any part of the range, then ignore it.
175 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
176 continue;
177
178 // Ignore pieces of the range that go onto other lines.
179 if (R.Start.getPointer() < LineStart)
180 R.Start = SMLoc::getFromPointer(LineStart);
181 if (R.End.getPointer() > LineEnd)
182 R.End = SMLoc::getFromPointer(LineEnd);
183
184 // Translate from SMLoc ranges to column ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000185 // FIXME: Handle multibyte characters.
Chris Lattner854f3662012-05-06 16:20:49 +0000186 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
187 R.End.getPointer()-LineStart));
188 }
189
190 LineAndCol = getLineAndColumn(Loc, CurBuf);
191 }
192
193 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
Chris Lattner9322ba82012-05-05 22:17:32 +0000194 LineAndCol.second-1, Kind, Msg.str(),
Jordan Roseefd8f802013-01-10 18:50:15 +0000195 LineStr, ColRanges, FixIts);
Chris Lattner200e0752009-07-02 23:08:13 +0000196}
197
Jordan Rose57ffdb02014-06-17 02:15:40 +0000198void SourceMgr::PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
199 bool ShowColors) const {
Chris Lattner3c799812010-04-06 00:26:48 +0000200 // Report the message with the diagnostic handler if present.
201 if (DiagHandler) {
Chris Lattner03b80a42011-10-16 05:43:57 +0000202 DiagHandler(Diagnostic, DiagContext);
Chris Lattner3c799812010-04-06 00:26:48 +0000203 return;
204 }
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000205
Jordan Rose57ffdb02014-06-17 02:15:40 +0000206 if (Diagnostic.getLoc().isValid()) {
Alp Tokera55b95b2014-07-06 10:33:31 +0000207 unsigned CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
208 assert(CurBuf && "Invalid or unspecified location!");
Chris Lattner854f3662012-05-06 16:20:49 +0000209 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
210 }
Chris Lattner200e0752009-07-02 23:08:13 +0000211
Craig Topperc10719f2014-04-07 04:17:22 +0000212 Diagnostic.print(nullptr, OS, ShowColors);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000213}
Chris Lattnercc64cc92009-07-02 22:24:20 +0000214
Jordan Rose57ffdb02014-06-17 02:15:40 +0000215void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
216 SourceMgr::DiagKind Kind,
217 const Twine &Msg, ArrayRef<SMRange> Ranges,
218 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
219 PrintMessage(OS, GetMessage(Loc, Kind, Msg, Ranges, FixIts), ShowColors);
220}
221
Dmitri Gribenko8f944622013-09-27 21:09:25 +0000222void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
223 const Twine &Msg, ArrayRef<SMRange> Ranges,
224 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
225 PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
226}
227
Chris Lattnercc64cc92009-07-02 22:24:20 +0000228//===----------------------------------------------------------------------===//
229// SMDiagnostic Implementation
230//===----------------------------------------------------------------------===//
231
Jordan Roseefd8f802013-01-10 18:50:15 +0000232SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
Chris Lattner03b80a42011-10-16 05:43:57 +0000233 int Line, int Col, SourceMgr::DiagKind Kind,
Jordan Roseefd8f802013-01-10 18:50:15 +0000234 StringRef Msg, StringRef LineStr,
235 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
236 ArrayRef<SMFixIt> Hints)
Chris Lattner03b80a42011-10-16 05:43:57 +0000237 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
Jordan Roseefd8f802013-01-10 18:50:15 +0000238 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
239 FixIts(Hints.begin(), Hints.end()) {
240 std::sort(FixIts.begin(), FixIts.end());
Chris Lattner03b80a42011-10-16 05:43:57 +0000241}
Chris Lattnera3a06812011-10-16 04:47:35 +0000242
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000243static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
244 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
Jordan Roseefd8f802013-01-10 18:50:15 +0000245 if (FixIts.empty())
246 return;
247
248 const char *LineStart = SourceLine.begin();
249 const char *LineEnd = SourceLine.end();
250
251 size_t PrevHintEndCol = 0;
252
253 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
254 I != E; ++I) {
255 // If the fixit contains a newline or tab, ignore it.
256 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
257 continue;
258
259 SMRange R = I->getRange();
260
261 // If the line doesn't contain any part of the range, then ignore it.
262 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
263 continue;
264
265 // Translate from SMLoc to column.
266 // Ignore pieces of the range that go onto other lines.
267 // FIXME: Handle multibyte characters in the source line.
268 unsigned FirstCol;
269 if (R.Start.getPointer() < LineStart)
270 FirstCol = 0;
271 else
272 FirstCol = R.Start.getPointer() - LineStart;
273
274 // If we inserted a long previous hint, push this one forwards, and add
275 // an extra space to show that this is not part of the previous
276 // completion. This is sort of the best we can do when two hints appear
277 // to overlap.
278 //
279 // Note that if this hint is located immediately after the previous
280 // hint, no space will be added, since the location is more important.
281 unsigned HintCol = FirstCol;
282 if (HintCol < PrevHintEndCol)
283 HintCol = PrevHintEndCol + 1;
284
285 // FIXME: This assertion is intended to catch unintended use of multibyte
286 // characters in fixits. If we decide to do this, we'll have to track
287 // separate byte widths for the source and fixit lines.
288 assert((size_t)llvm::sys::locale::columnWidth(I->getText()) ==
289 I->getText().size());
290
291 // This relies on one byte per column in our fixit hints.
292 unsigned LastColumnModified = HintCol + I->getText().size();
293 if (LastColumnModified > FixItLine.size())
294 FixItLine.resize(LastColumnModified, ' ');
295
296 std::copy(I->getText().begin(), I->getText().end(),
297 FixItLine.begin() + HintCol);
298
299 PrevHintEndCol = LastColumnModified;
300
301 // For replacements, mark the removal range with '~'.
302 // FIXME: Handle multibyte characters in the source line.
303 unsigned LastCol;
304 if (R.End.getPointer() >= LineEnd)
305 LastCol = LineEnd - LineStart;
306 else
307 LastCol = R.End.getPointer() - LineStart;
308
309 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
310 }
311}
312
313static void printSourceLine(raw_ostream &S, StringRef LineContents) {
314 // Print out the source line one character at a time, so we can expand tabs.
315 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
316 if (LineContents[i] != '\t') {
317 S << LineContents[i];
318 ++OutCol;
319 continue;
320 }
321
322 // If we have a tab, emit at least one space, then round up to 8 columns.
323 do {
324 S << ' ';
325 ++OutCol;
326 } while ((OutCol % TabStop) != 0);
327 }
328 S << '\n';
329}
Chris Lattnera3a06812011-10-16 04:47:35 +0000330
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000331static bool isNonASCII(char c) {
332 return c & 0x80;
333}
334
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000335void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
336 bool ShowColors) const {
Daniel Dunbarc8b8c492012-07-20 18:29:44 +0000337 // Display colors only if OS supports colors.
338 ShowColors &= S.has_colors();
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000339
340 if (ShowColors)
341 S.changeColor(raw_ostream::SAVEDCOLOR, true);
342
Chris Lattnercc64cc92009-07-02 22:24:20 +0000343 if (ProgName && ProgName[0])
344 S << ProgName << ": ";
345
Dan Gohman7c675902010-01-21 10:13:27 +0000346 if (!Filename.empty()) {
347 if (Filename == "-")
348 S << "<stdin>";
349 else
350 S << Filename;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000351
Dan Gohman7c675902010-01-21 10:13:27 +0000352 if (LineNo != -1) {
353 S << ':' << LineNo;
354 if (ColumnNo != -1)
355 S << ':' << (ColumnNo+1);
356 }
357 S << ": ";
Chris Lattnercc64cc92009-07-02 22:24:20 +0000358 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000359
Chris Lattner03b80a42011-10-16 05:43:57 +0000360 switch (Kind) {
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000361 case SourceMgr::DK_Error:
362 if (ShowColors)
363 S.changeColor(raw_ostream::RED, true);
364 S << "error: ";
365 break;
366 case SourceMgr::DK_Warning:
367 if (ShowColors)
368 S.changeColor(raw_ostream::MAGENTA, true);
369 S << "warning: ";
370 break;
371 case SourceMgr::DK_Note:
372 if (ShowColors)
373 S.changeColor(raw_ostream::BLACK, true);
374 S << "note: ";
375 break;
Chris Lattner03b80a42011-10-16 05:43:57 +0000376 }
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000377
378 if (ShowColors) {
379 S.resetColor();
380 S.changeColor(raw_ostream::SAVEDCOLOR, true);
381 }
382
Dan Gohman7c675902010-01-21 10:13:27 +0000383 S << Message << '\n';
Daniel Dunbar5a308f52009-11-22 22:08:00 +0000384
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000385 if (ShowColors)
386 S.resetColor();
387
Chris Lattner72845262011-10-16 05:47:55 +0000388 if (LineNo == -1 || ColumnNo == -1)
Chris Lattnera3a06812011-10-16 04:47:35 +0000389 return;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000390
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000391 // FIXME: If there are multibyte or multi-column characters in the source, all
392 // our ranges will be wrong. To do this properly, we'll need a byte-to-column
393 // map like Clang's TextDiagnostic. For now, we'll just handle tabs by
394 // expanding them later, and bail out rather than show incorrect ranges and
395 // misaligned fixits for any other odd characters.
396 if (std::find_if(LineContents.begin(), LineContents.end(), isNonASCII) !=
397 LineContents.end()) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000398 printSourceLine(S, LineContents);
399 return;
400 }
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000401 size_t NumColumns = LineContents.size();
Jordan Roseefd8f802013-01-10 18:50:15 +0000402
Chris Lattnera3a06812011-10-16 04:47:35 +0000403 // Build the line with the caret and ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000404 std::string CaretLine(NumColumns+1, ' ');
Chris Lattnera3a06812011-10-16 04:47:35 +0000405
406 // Expand any ranges.
407 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
408 std::pair<unsigned, unsigned> R = Ranges[r];
Jordan Roseefd8f802013-01-10 18:50:15 +0000409 std::fill(&CaretLine[R.first],
410 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
411 '~');
Chris Lattnercc64cc92009-07-02 22:24:20 +0000412 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000413
414 // Add any fix-its.
415 // FIXME: Find the beginning of the line properly for multibyte characters.
416 std::string FixItInsertionLine;
417 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
418 makeArrayRef(Loc.getPointer() - ColumnNo,
419 LineContents.size()));
420
Chris Lattnera3a06812011-10-16 04:47:35 +0000421 // Finally, plop on the caret.
Jordan Roseefd8f802013-01-10 18:50:15 +0000422 if (unsigned(ColumnNo) <= NumColumns)
Chris Lattnera3a06812011-10-16 04:47:35 +0000423 CaretLine[ColumnNo] = '^';
424 else
Jordan Roseefd8f802013-01-10 18:50:15 +0000425 CaretLine[NumColumns] = '^';
Chris Lattnera3a06812011-10-16 04:47:35 +0000426
427 // ... and remove trailing whitespace so the output doesn't wrap for it. We
428 // know that the line isn't completely empty because it has the caret in it at
429 // least.
430 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
431
Jordan Roseefd8f802013-01-10 18:50:15 +0000432 printSourceLine(S, LineContents);
Chris Lattnera3a06812011-10-16 04:47:35 +0000433
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000434 if (ShowColors)
435 S.changeColor(raw_ostream::GREEN, true);
436
Chris Lattnera3a06812011-10-16 04:47:35 +0000437 // Print out the caret line, matching tabs in the source line.
438 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
439 if (i >= LineContents.size() || LineContents[i] != '\t') {
440 S << CaretLine[i];
441 ++OutCol;
442 continue;
443 }
444
445 // Okay, we have a tab. Insert the appropriate number of characters.
446 do {
447 S << CaretLine[i];
448 ++OutCol;
Jordan Roseefd8f802013-01-10 18:50:15 +0000449 } while ((OutCol % TabStop) != 0);
Chris Lattnera3a06812011-10-16 04:47:35 +0000450 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000451 S << '\n';
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000452
453 if (ShowColors)
454 S.resetColor();
Jordan Roseefd8f802013-01-10 18:50:15 +0000455
456 // Print out the replacement line, matching tabs in the source line.
457 if (FixItInsertionLine.empty())
458 return;
Chris Lattnera3a06812011-10-16 04:47:35 +0000459
Dmitri Gribenko78fe2ba2013-09-27 21:24:36 +0000460 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000461 if (i >= LineContents.size() || LineContents[i] != '\t') {
462 S << FixItInsertionLine[i];
463 ++OutCol;
464 continue;
465 }
466
467 // Okay, we have a tab. Insert the appropriate number of characters.
468 do {
469 S << FixItInsertionLine[i];
470 // FIXME: This is trying not to break up replacements, but then to re-sync
471 // with the tabs between replacements. This will fail, though, if two
472 // fix-it replacements are exactly adjacent, or if a fix-it contains a
473 // space. Really we should be precomputing column widths, which we'll
474 // need anyway for multibyte chars.
475 if (FixItInsertionLine[i] != ' ')
476 ++i;
477 ++OutCol;
478 } while (((OutCol % TabStop) != 0) && i != e);
479 }
Chris Lattnera3a06812011-10-16 04:47:35 +0000480 S << '\n';
Chris Lattnercc64cc92009-07-02 22:24:20 +0000481}