blob: 2401ce73772dd8fe9e251c5b66ff6cb21ab37a87 [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"
Jordan Roseefd8f802013-01-10 18:50:15 +000017#include "llvm/ADT/SmallString.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Twine.h"
Jordan Roseefd8f802013-01-10 18:50:15 +000019#include "llvm/Support/Locale.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/MemoryBuffer.h"
Yaron Keren15217202014-05-16 13:16:30 +000021#include "llvm/Support/Path.h"
Chris Lattner8db9bc72009-03-13 07:05:43 +000022#include "llvm/Support/raw_ostream.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Chris Lattner8db9bc72009-03-13 07:05:43 +000024using namespace llvm;
25
Jordan Roseefd8f802013-01-10 18:50:15 +000026static const size_t TabStop = 8;
27
Chris Lattner5d47e932009-08-11 17:49:14 +000028namespace {
29 struct LineNoCacheTy {
30 int LastQueryBufferID;
31 const char *LastQuery;
32 unsigned LineNoOfQuery;
33 };
34}
35
36static LineNoCacheTy *getCache(void *Ptr) {
37 return (LineNoCacheTy*)Ptr;
38}
39
40
Chris Lattnerfd255752009-06-21 03:41:50 +000041SourceMgr::~SourceMgr() {
Chris Lattner5d47e932009-08-11 17:49:14 +000042 // Delete the line # cache if allocated.
43 if (LineNoCacheTy *Cache = getCache(LineNoCache))
44 delete Cache;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000045
Chris Lattner8db9bc72009-03-13 07:05:43 +000046 while (!Buffers.empty()) {
47 delete Buffers.back().Buffer;
48 Buffers.pop_back();
49 }
50}
51
Matt Arsenaultfe8ff5c2013-07-20 00:20:10 +000052size_t SourceMgr::AddIncludeFile(const std::string &Filename,
53 SMLoc IncludeLoc,
54 std::string &IncludedFile) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000055 std::unique_ptr<MemoryBuffer> NewBuf;
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000056 IncludedFile = Filename;
57 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
Chris Lattner976af622009-06-21 05:06:04 +000058
59 // If the file didn't exist directly, see if it's in an include path.
60 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
Yaron Keren15217202014-05-16 13:16:30 +000061 IncludedFile = IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000062 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
Chris Lattner976af622009-06-21 05:06:04 +000063 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000064
David Blaikie041f1aa2013-05-15 07:36:59 +000065 if (!NewBuf) return ~0U;
Chris Lattner976af622009-06-21 05:06:04 +000066
Ahmed Charles96c9d952014-03-05 10:19:29 +000067 return AddNewSourceBuffer(NewBuf.release(), IncludeLoc);
Chris Lattner976af622009-06-21 05:06:04 +000068}
69
70
Chris Lattnerfd255752009-06-21 03:41:50 +000071int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +000072 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner87710ca2009-03-13 16:01:53 +000073 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattner0f6dc782009-03-18 20:36:45 +000074 // Use <= here so that a pointer to the null at the end of the buffer
75 // is included as part of the buffer.
76 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattner8db9bc72009-03-13 07:05:43 +000077 return i;
78 return -1;
79}
80
Chris Lattner9322ba82012-05-05 22:17:32 +000081std::pair<unsigned, unsigned>
82SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +000083 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
84 assert(BufferID != -1 && "Invalid Location!");
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000085
Alp Toker7899d502014-06-25 00:41:15 +000086 const MemoryBuffer *Buff = getMemoryBuffer(BufferID);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000087
Chris Lattner8db9bc72009-03-13 07:05:43 +000088 // Count the number of \n's between the start of the file and the specified
89 // location.
90 unsigned LineNo = 1;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000091
Chris Lattner9322ba82012-05-05 22:17:32 +000092 const char *BufStart = Buff->getBufferStart();
93 const char *Ptr = BufStart;
Chris Lattner8db9bc72009-03-13 07:05:43 +000094
Chris Lattner5d47e932009-08-11 17:49:14 +000095 // If we have a line number cache, and if the query is to a later point in the
96 // same file, start searching from the last query location. This optimizes
97 // for the case when multiple diagnostics come out of one file in order.
98 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000099 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner5d47e932009-08-11 17:49:14 +0000100 Cache->LastQuery <= Loc.getPointer()) {
101 Ptr = Cache->LastQuery;
102 LineNo = Cache->LineNoOfQuery;
103 }
104
105 // Scan for the location being queried, keeping track of the number of lines
106 // we see.
Chris Lattner526c8cb2009-06-21 03:39:35 +0000107 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattner8db9bc72009-03-13 07:05:43 +0000108 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000109
Chris Lattner5d47e932009-08-11 17:49:14 +0000110 // Allocate the line number cache if it doesn't exist.
Craig Topper8d399f82014-04-09 04:20:00 +0000111 if (!LineNoCache)
Chris Lattner5d47e932009-08-11 17:49:14 +0000112 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000113
Chris Lattner5d47e932009-08-11 17:49:14 +0000114 // Update the line # cache.
115 LineNoCacheTy &Cache = *getCache(LineNoCache);
116 Cache.LastQueryBufferID = BufferID;
117 Cache.LastQuery = Ptr;
118 Cache.LineNoOfQuery = LineNo;
Chris Lattner9322ba82012-05-05 22:17:32 +0000119
120 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
Matt Beaumont-Gaya1b3b002012-05-07 18:12:42 +0000121 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
Chris Lattner9322ba82012-05-05 22:17:32 +0000122 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000123}
124
Chris Lattnercc64cc92009-07-02 22:24:20 +0000125void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner526c8cb2009-06-21 03:39:35 +0000126 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000127
Chris Lattner8db9bc72009-03-13 07:05:43 +0000128 int CurBuf = FindBufferContainingLoc(IncludeLoc);
129 assert(CurBuf != -1 && "Invalid or unspecified location!");
130
Chris Lattnercc64cc92009-07-02 22:24:20 +0000131 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000132
Chris Lattnercc64cc92009-07-02 22:24:20 +0000133 OS << "Included from "
134 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
135 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattner8db9bc72009-03-13 07:05:43 +0000136}
137
138
Chris Lattner03b80a42011-10-16 05:43:57 +0000139SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
Chris Lattner72845262011-10-16 05:47:55 +0000140 const Twine &Msg,
Jordan Roseefd8f802013-01-10 18:50:15 +0000141 ArrayRef<SMRange> Ranges,
142 ArrayRef<SMFixIt> FixIts) const {
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000143
Chris Lattner8db9bc72009-03-13 07:05:43 +0000144 // First thing to do: find the current buffer containing the specified
Chris Lattner854f3662012-05-06 16:20:49 +0000145 // location to pull out the source line.
Chris Lattnera3a06812011-10-16 04:47:35 +0000146 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
Chris Lattner854f3662012-05-06 16:20:49 +0000147 std::pair<unsigned, unsigned> LineAndCol;
148 const char *BufferID = "<unknown>";
149 std::string LineStr;
Chris Lattnera3a06812011-10-16 04:47:35 +0000150
Chris Lattner854f3662012-05-06 16:20:49 +0000151 if (Loc.isValid()) {
152 int CurBuf = FindBufferContainingLoc(Loc);
153 assert(CurBuf != -1 && "Invalid or unspecified location!");
154
Alp Toker7899d502014-06-25 00:41:15 +0000155 const MemoryBuffer *CurMB = getMemoryBuffer(CurBuf);
Chris Lattner854f3662012-05-06 16:20:49 +0000156 BufferID = CurMB->getBufferIdentifier();
157
158 // Scan backward to find the start of the line.
159 const char *LineStart = Loc.getPointer();
160 const char *BufStart = CurMB->getBufferStart();
161 while (LineStart != BufStart && LineStart[-1] != '\n' &&
162 LineStart[-1] != '\r')
163 --LineStart;
164
165 // Get the end of the line.
166 const char *LineEnd = Loc.getPointer();
167 const char *BufEnd = CurMB->getBufferEnd();
168 while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
169 ++LineEnd;
170 LineStr = std::string(LineStart, LineEnd);
171
172 // Convert any ranges to column ranges that only intersect the line of the
173 // location.
174 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
175 SMRange R = Ranges[i];
176 if (!R.isValid()) continue;
177
178 // If the line doesn't contain any part of the range, then ignore it.
179 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
180 continue;
181
182 // Ignore pieces of the range that go onto other lines.
183 if (R.Start.getPointer() < LineStart)
184 R.Start = SMLoc::getFromPointer(LineStart);
185 if (R.End.getPointer() > LineEnd)
186 R.End = SMLoc::getFromPointer(LineEnd);
187
188 // Translate from SMLoc ranges to column ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000189 // FIXME: Handle multibyte characters.
Chris Lattner854f3662012-05-06 16:20:49 +0000190 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
191 R.End.getPointer()-LineStart));
192 }
193
194 LineAndCol = getLineAndColumn(Loc, CurBuf);
195 }
196
197 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
Chris Lattner9322ba82012-05-05 22:17:32 +0000198 LineAndCol.second-1, Kind, Msg.str(),
Jordan Roseefd8f802013-01-10 18:50:15 +0000199 LineStr, ColRanges, FixIts);
Chris Lattner200e0752009-07-02 23:08:13 +0000200}
201
Jordan Rose57ffdb02014-06-17 02:15:40 +0000202void SourceMgr::PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
203 bool ShowColors) const {
Chris Lattner3c799812010-04-06 00:26:48 +0000204 // Report the message with the diagnostic handler if present.
205 if (DiagHandler) {
Chris Lattner03b80a42011-10-16 05:43:57 +0000206 DiagHandler(Diagnostic, DiagContext);
Chris Lattner3c799812010-04-06 00:26:48 +0000207 return;
208 }
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000209
Jordan Rose57ffdb02014-06-17 02:15:40 +0000210 if (Diagnostic.getLoc().isValid()) {
211 int CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
Chris Lattner854f3662012-05-06 16:20:49 +0000212 assert(CurBuf != -1 && "Invalid or unspecified location!");
213 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
214 }
Chris Lattner200e0752009-07-02 23:08:13 +0000215
Craig Topperc10719f2014-04-07 04:17:22 +0000216 Diagnostic.print(nullptr, OS, ShowColors);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000217}
Chris Lattnercc64cc92009-07-02 22:24:20 +0000218
Jordan Rose57ffdb02014-06-17 02:15:40 +0000219void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
220 SourceMgr::DiagKind Kind,
221 const Twine &Msg, ArrayRef<SMRange> Ranges,
222 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
223 PrintMessage(OS, GetMessage(Loc, Kind, Msg, Ranges, FixIts), ShowColors);
224}
225
Dmitri Gribenko8f944622013-09-27 21:09:25 +0000226void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
227 const Twine &Msg, ArrayRef<SMRange> Ranges,
228 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
229 PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
230}
231
Chris Lattnercc64cc92009-07-02 22:24:20 +0000232//===----------------------------------------------------------------------===//
233// SMDiagnostic Implementation
234//===----------------------------------------------------------------------===//
235
Jordan Roseefd8f802013-01-10 18:50:15 +0000236SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
Chris Lattner03b80a42011-10-16 05:43:57 +0000237 int Line, int Col, SourceMgr::DiagKind Kind,
Jordan Roseefd8f802013-01-10 18:50:15 +0000238 StringRef Msg, StringRef LineStr,
239 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
240 ArrayRef<SMFixIt> Hints)
Chris Lattner03b80a42011-10-16 05:43:57 +0000241 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
Jordan Roseefd8f802013-01-10 18:50:15 +0000242 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
243 FixIts(Hints.begin(), Hints.end()) {
244 std::sort(FixIts.begin(), FixIts.end());
Chris Lattner03b80a42011-10-16 05:43:57 +0000245}
Chris Lattnera3a06812011-10-16 04:47:35 +0000246
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000247static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
248 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
Jordan Roseefd8f802013-01-10 18:50:15 +0000249 if (FixIts.empty())
250 return;
251
252 const char *LineStart = SourceLine.begin();
253 const char *LineEnd = SourceLine.end();
254
255 size_t PrevHintEndCol = 0;
256
257 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
258 I != E; ++I) {
259 // If the fixit contains a newline or tab, ignore it.
260 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
261 continue;
262
263 SMRange R = I->getRange();
264
265 // If the line doesn't contain any part of the range, then ignore it.
266 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
267 continue;
268
269 // Translate from SMLoc to column.
270 // Ignore pieces of the range that go onto other lines.
271 // FIXME: Handle multibyte characters in the source line.
272 unsigned FirstCol;
273 if (R.Start.getPointer() < LineStart)
274 FirstCol = 0;
275 else
276 FirstCol = R.Start.getPointer() - LineStart;
277
278 // If we inserted a long previous hint, push this one forwards, and add
279 // an extra space to show that this is not part of the previous
280 // completion. This is sort of the best we can do when two hints appear
281 // to overlap.
282 //
283 // Note that if this hint is located immediately after the previous
284 // hint, no space will be added, since the location is more important.
285 unsigned HintCol = FirstCol;
286 if (HintCol < PrevHintEndCol)
287 HintCol = PrevHintEndCol + 1;
288
289 // FIXME: This assertion is intended to catch unintended use of multibyte
290 // characters in fixits. If we decide to do this, we'll have to track
291 // separate byte widths for the source and fixit lines.
292 assert((size_t)llvm::sys::locale::columnWidth(I->getText()) ==
293 I->getText().size());
294
295 // This relies on one byte per column in our fixit hints.
296 unsigned LastColumnModified = HintCol + I->getText().size();
297 if (LastColumnModified > FixItLine.size())
298 FixItLine.resize(LastColumnModified, ' ');
299
300 std::copy(I->getText().begin(), I->getText().end(),
301 FixItLine.begin() + HintCol);
302
303 PrevHintEndCol = LastColumnModified;
304
305 // For replacements, mark the removal range with '~'.
306 // FIXME: Handle multibyte characters in the source line.
307 unsigned LastCol;
308 if (R.End.getPointer() >= LineEnd)
309 LastCol = LineEnd - LineStart;
310 else
311 LastCol = R.End.getPointer() - LineStart;
312
313 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
314 }
315}
316
317static void printSourceLine(raw_ostream &S, StringRef LineContents) {
318 // Print out the source line one character at a time, so we can expand tabs.
319 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
320 if (LineContents[i] != '\t') {
321 S << LineContents[i];
322 ++OutCol;
323 continue;
324 }
325
326 // If we have a tab, emit at least one space, then round up to 8 columns.
327 do {
328 S << ' ';
329 ++OutCol;
330 } while ((OutCol % TabStop) != 0);
331 }
332 S << '\n';
333}
Chris Lattnera3a06812011-10-16 04:47:35 +0000334
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000335static bool isNonASCII(char c) {
336 return c & 0x80;
337}
338
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000339void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
340 bool ShowColors) const {
Daniel Dunbarc8b8c492012-07-20 18:29:44 +0000341 // Display colors only if OS supports colors.
342 ShowColors &= S.has_colors();
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000343
344 if (ShowColors)
345 S.changeColor(raw_ostream::SAVEDCOLOR, true);
346
Chris Lattnercc64cc92009-07-02 22:24:20 +0000347 if (ProgName && ProgName[0])
348 S << ProgName << ": ";
349
Dan Gohman7c675902010-01-21 10:13:27 +0000350 if (!Filename.empty()) {
351 if (Filename == "-")
352 S << "<stdin>";
353 else
354 S << Filename;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000355
Dan Gohman7c675902010-01-21 10:13:27 +0000356 if (LineNo != -1) {
357 S << ':' << LineNo;
358 if (ColumnNo != -1)
359 S << ':' << (ColumnNo+1);
360 }
361 S << ": ";
Chris Lattnercc64cc92009-07-02 22:24:20 +0000362 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000363
Chris Lattner03b80a42011-10-16 05:43:57 +0000364 switch (Kind) {
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000365 case SourceMgr::DK_Error:
366 if (ShowColors)
367 S.changeColor(raw_ostream::RED, true);
368 S << "error: ";
369 break;
370 case SourceMgr::DK_Warning:
371 if (ShowColors)
372 S.changeColor(raw_ostream::MAGENTA, true);
373 S << "warning: ";
374 break;
375 case SourceMgr::DK_Note:
376 if (ShowColors)
377 S.changeColor(raw_ostream::BLACK, true);
378 S << "note: ";
379 break;
Chris Lattner03b80a42011-10-16 05:43:57 +0000380 }
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000381
382 if (ShowColors) {
383 S.resetColor();
384 S.changeColor(raw_ostream::SAVEDCOLOR, true);
385 }
386
Dan Gohman7c675902010-01-21 10:13:27 +0000387 S << Message << '\n';
Daniel Dunbar5a308f52009-11-22 22:08:00 +0000388
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000389 if (ShowColors)
390 S.resetColor();
391
Chris Lattner72845262011-10-16 05:47:55 +0000392 if (LineNo == -1 || ColumnNo == -1)
Chris Lattnera3a06812011-10-16 04:47:35 +0000393 return;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000394
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000395 // FIXME: If there are multibyte or multi-column characters in the source, all
396 // our ranges will be wrong. To do this properly, we'll need a byte-to-column
397 // map like Clang's TextDiagnostic. For now, we'll just handle tabs by
398 // expanding them later, and bail out rather than show incorrect ranges and
399 // misaligned fixits for any other odd characters.
400 if (std::find_if(LineContents.begin(), LineContents.end(), isNonASCII) !=
401 LineContents.end()) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000402 printSourceLine(S, LineContents);
403 return;
404 }
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000405 size_t NumColumns = LineContents.size();
Jordan Roseefd8f802013-01-10 18:50:15 +0000406
Chris Lattnera3a06812011-10-16 04:47:35 +0000407 // Build the line with the caret and ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000408 std::string CaretLine(NumColumns+1, ' ');
Chris Lattnera3a06812011-10-16 04:47:35 +0000409
410 // Expand any ranges.
411 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
412 std::pair<unsigned, unsigned> R = Ranges[r];
Jordan Roseefd8f802013-01-10 18:50:15 +0000413 std::fill(&CaretLine[R.first],
414 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
415 '~');
Chris Lattnercc64cc92009-07-02 22:24:20 +0000416 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000417
418 // Add any fix-its.
419 // FIXME: Find the beginning of the line properly for multibyte characters.
420 std::string FixItInsertionLine;
421 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
422 makeArrayRef(Loc.getPointer() - ColumnNo,
423 LineContents.size()));
424
Chris Lattnera3a06812011-10-16 04:47:35 +0000425 // Finally, plop on the caret.
Jordan Roseefd8f802013-01-10 18:50:15 +0000426 if (unsigned(ColumnNo) <= NumColumns)
Chris Lattnera3a06812011-10-16 04:47:35 +0000427 CaretLine[ColumnNo] = '^';
428 else
Jordan Roseefd8f802013-01-10 18:50:15 +0000429 CaretLine[NumColumns] = '^';
Chris Lattnera3a06812011-10-16 04:47:35 +0000430
431 // ... and remove trailing whitespace so the output doesn't wrap for it. We
432 // know that the line isn't completely empty because it has the caret in it at
433 // least.
434 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
435
Jordan Roseefd8f802013-01-10 18:50:15 +0000436 printSourceLine(S, LineContents);
Chris Lattnera3a06812011-10-16 04:47:35 +0000437
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000438 if (ShowColors)
439 S.changeColor(raw_ostream::GREEN, true);
440
Chris Lattnera3a06812011-10-16 04:47:35 +0000441 // Print out the caret line, matching tabs in the source line.
442 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
443 if (i >= LineContents.size() || LineContents[i] != '\t') {
444 S << CaretLine[i];
445 ++OutCol;
446 continue;
447 }
448
449 // Okay, we have a tab. Insert the appropriate number of characters.
450 do {
451 S << CaretLine[i];
452 ++OutCol;
Jordan Roseefd8f802013-01-10 18:50:15 +0000453 } while ((OutCol % TabStop) != 0);
Chris Lattnera3a06812011-10-16 04:47:35 +0000454 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000455 S << '\n';
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000456
457 if (ShowColors)
458 S.resetColor();
Jordan Roseefd8f802013-01-10 18:50:15 +0000459
460 // Print out the replacement line, matching tabs in the source line.
461 if (FixItInsertionLine.empty())
462 return;
Chris Lattnera3a06812011-10-16 04:47:35 +0000463
Dmitri Gribenko78fe2ba2013-09-27 21:24:36 +0000464 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000465 if (i >= LineContents.size() || LineContents[i] != '\t') {
466 S << FixItInsertionLine[i];
467 ++OutCol;
468 continue;
469 }
470
471 // Okay, we have a tab. Insert the appropriate number of characters.
472 do {
473 S << FixItInsertionLine[i];
474 // FIXME: This is trying not to break up replacements, but then to re-sync
475 // with the tabs between replacements. This will fail, though, if two
476 // fix-it replacements are exactly adjacent, or if a fix-it contains a
477 // space. Really we should be precomputing column widths, which we'll
478 // need anyway for multibyte chars.
479 if (FixItInsertionLine[i] != ' ')
480 ++i;
481 ++OutCol;
482 } while (((OutCol % TabStop) != 0) && i != e);
483 }
Chris Lattnera3a06812011-10-16 04:47:35 +0000484 S << '\n';
Chris Lattnercc64cc92009-07-02 22:24:20 +0000485}