blob: 4bfd96abe5b6a30775b882a6fe4bfc6981d6d3ad [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"
Chris Lattner8db9bc72009-03-13 07:05:43 +000021#include "llvm/Support/raw_ostream.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000022#include "llvm/Support/system_error.h"
Chris Lattner8db9bc72009-03-13 07:05:43 +000023using namespace llvm;
24
Jordan Roseefd8f802013-01-10 18:50:15 +000025static const size_t TabStop = 8;
26
Chris Lattner5d47e932009-08-11 17:49:14 +000027namespace {
28 struct LineNoCacheTy {
29 int LastQueryBufferID;
30 const char *LastQuery;
31 unsigned LineNoOfQuery;
32 };
33}
34
35static LineNoCacheTy *getCache(void *Ptr) {
36 return (LineNoCacheTy*)Ptr;
37}
38
39
Chris Lattnerfd255752009-06-21 03:41:50 +000040SourceMgr::~SourceMgr() {
Chris Lattner5d47e932009-08-11 17:49:14 +000041 // Delete the line # cache if allocated.
42 if (LineNoCacheTy *Cache = getCache(LineNoCache))
43 delete Cache;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000044
Chris Lattner8db9bc72009-03-13 07:05:43 +000045 while (!Buffers.empty()) {
46 delete Buffers.back().Buffer;
47 Buffers.pop_back();
48 }
49}
50
Chris Lattner976af622009-06-21 05:06:04 +000051/// AddIncludeFile - Search for a file with the specified name in the current
52/// directory or in one of the IncludeDirs. If no file is found, this returns
53/// ~0, otherwise it returns the buffer ID of the stacked file.
Matt Arsenaultfe8ff5c2013-07-20 00:20:10 +000054size_t SourceMgr::AddIncludeFile(const std::string &Filename,
55 SMLoc IncludeLoc,
56 std::string &IncludedFile) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000057 std::unique_ptr<MemoryBuffer> NewBuf;
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000058 IncludedFile = Filename;
59 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
Chris Lattner976af622009-06-21 05:06:04 +000060
61 // If the file didn't exist directly, see if it's in an include path.
62 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000063 IncludedFile = IncludeDirectories[i] + "/" + Filename;
64 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
Chris Lattner976af622009-06-21 05:06:04 +000065 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000066
David Blaikie041f1aa2013-05-15 07:36:59 +000067 if (!NewBuf) return ~0U;
Chris Lattner976af622009-06-21 05:06:04 +000068
Ahmed Charles96c9d952014-03-05 10:19:29 +000069 return AddNewSourceBuffer(NewBuf.release(), IncludeLoc);
Chris Lattner976af622009-06-21 05:06:04 +000070}
71
72
Chris Lattner8db9bc72009-03-13 07:05:43 +000073/// FindBufferContainingLoc - Return the ID of the buffer containing the
74/// specified location, returning -1 if not found.
Chris Lattnerfd255752009-06-21 03:41:50 +000075int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +000076 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner87710ca2009-03-13 16:01:53 +000077 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattner0f6dc782009-03-18 20:36:45 +000078 // Use <= here so that a pointer to the null at the end of the buffer
79 // is included as part of the buffer.
80 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattner8db9bc72009-03-13 07:05:43 +000081 return i;
82 return -1;
83}
84
Chris Lattner9322ba82012-05-05 22:17:32 +000085/// getLineAndColumn - Find the line and column number for the specified
86/// location in the specified file. This is not a fast method.
87std::pair<unsigned, unsigned>
88SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +000089 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
90 assert(BufferID != -1 && "Invalid Location!");
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000091
Chris Lattner8db9bc72009-03-13 07:05:43 +000092 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000093
Chris Lattner8db9bc72009-03-13 07:05:43 +000094 // Count the number of \n's between the start of the file and the specified
95 // location.
96 unsigned LineNo = 1;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000097
Chris Lattner9322ba82012-05-05 22:17:32 +000098 const char *BufStart = Buff->getBufferStart();
99 const char *Ptr = BufStart;
Chris Lattner8db9bc72009-03-13 07:05:43 +0000100
Chris Lattner5d47e932009-08-11 17:49:14 +0000101 // If we have a line number cache, and if the query is to a later point in the
102 // same file, start searching from the last query location. This optimizes
103 // for the case when multiple diagnostics come out of one file in order.
104 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000105 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner5d47e932009-08-11 17:49:14 +0000106 Cache->LastQuery <= Loc.getPointer()) {
107 Ptr = Cache->LastQuery;
108 LineNo = Cache->LineNoOfQuery;
109 }
110
111 // Scan for the location being queried, keeping track of the number of lines
112 // we see.
Chris Lattner526c8cb2009-06-21 03:39:35 +0000113 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattner8db9bc72009-03-13 07:05:43 +0000114 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000115
Chris Lattner5d47e932009-08-11 17:49:14 +0000116 // Allocate the line number cache if it doesn't exist.
117 if (LineNoCache == 0)
118 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000119
Chris Lattner5d47e932009-08-11 17:49:14 +0000120 // Update the line # cache.
121 LineNoCacheTy &Cache = *getCache(LineNoCache);
122 Cache.LastQueryBufferID = BufferID;
123 Cache.LastQuery = Ptr;
124 Cache.LineNoOfQuery = LineNo;
Chris Lattner9322ba82012-05-05 22:17:32 +0000125
126 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
Matt Beaumont-Gaya1b3b002012-05-07 18:12:42 +0000127 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
Chris Lattner9322ba82012-05-05 22:17:32 +0000128 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000129}
130
Chris Lattnercc64cc92009-07-02 22:24:20 +0000131void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner526c8cb2009-06-21 03:39:35 +0000132 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000133
Chris Lattner8db9bc72009-03-13 07:05:43 +0000134 int CurBuf = FindBufferContainingLoc(IncludeLoc);
135 assert(CurBuf != -1 && "Invalid or unspecified location!");
136
Chris Lattnercc64cc92009-07-02 22:24:20 +0000137 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000138
Chris Lattnercc64cc92009-07-02 22:24:20 +0000139 OS << "Included from "
140 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
141 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattner8db9bc72009-03-13 07:05:43 +0000142}
143
144
Chris Lattner200e0752009-07-02 23:08:13 +0000145/// GetMessage - Return an SMDiagnostic at the specified location with the
146/// specified string.
147///
148/// @param Type - If non-null, the kind of message (e.g., "error") which is
149/// prefixed to the message.
Chris Lattner03b80a42011-10-16 05:43:57 +0000150SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
Chris Lattner72845262011-10-16 05:47:55 +0000151 const Twine &Msg,
Jordan Roseefd8f802013-01-10 18:50:15 +0000152 ArrayRef<SMRange> Ranges,
153 ArrayRef<SMFixIt> FixIts) const {
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000154
Chris Lattner8db9bc72009-03-13 07:05:43 +0000155 // First thing to do: find the current buffer containing the specified
Chris Lattner854f3662012-05-06 16:20:49 +0000156 // location to pull out the source line.
Chris Lattnera3a06812011-10-16 04:47:35 +0000157 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
Chris Lattner854f3662012-05-06 16:20:49 +0000158 std::pair<unsigned, unsigned> LineAndCol;
159 const char *BufferID = "<unknown>";
160 std::string LineStr;
Chris Lattnera3a06812011-10-16 04:47:35 +0000161
Chris Lattner854f3662012-05-06 16:20:49 +0000162 if (Loc.isValid()) {
163 int CurBuf = FindBufferContainingLoc(Loc);
164 assert(CurBuf != -1 && "Invalid or unspecified location!");
165
166 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
167 BufferID = CurMB->getBufferIdentifier();
168
169 // Scan backward to find the start of the line.
170 const char *LineStart = Loc.getPointer();
171 const char *BufStart = CurMB->getBufferStart();
172 while (LineStart != BufStart && LineStart[-1] != '\n' &&
173 LineStart[-1] != '\r')
174 --LineStart;
175
176 // Get the end of the line.
177 const char *LineEnd = Loc.getPointer();
178 const char *BufEnd = CurMB->getBufferEnd();
179 while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
180 ++LineEnd;
181 LineStr = std::string(LineStart, LineEnd);
182
183 // Convert any ranges to column ranges that only intersect the line of the
184 // location.
185 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
186 SMRange R = Ranges[i];
187 if (!R.isValid()) continue;
188
189 // If the line doesn't contain any part of the range, then ignore it.
190 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
191 continue;
192
193 // Ignore pieces of the range that go onto other lines.
194 if (R.Start.getPointer() < LineStart)
195 R.Start = SMLoc::getFromPointer(LineStart);
196 if (R.End.getPointer() > LineEnd)
197 R.End = SMLoc::getFromPointer(LineEnd);
198
199 // Translate from SMLoc ranges to column ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000200 // FIXME: Handle multibyte characters.
Chris Lattner854f3662012-05-06 16:20:49 +0000201 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
202 R.End.getPointer()-LineStart));
203 }
204
205 LineAndCol = getLineAndColumn(Loc, CurBuf);
206 }
207
208 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
Chris Lattner9322ba82012-05-05 22:17:32 +0000209 LineAndCol.second-1, Kind, Msg.str(),
Jordan Roseefd8f802013-01-10 18:50:15 +0000210 LineStr, ColRanges, FixIts);
Chris Lattner200e0752009-07-02 23:08:13 +0000211}
212
Dmitri Gribenko8f944622013-09-27 21:09:25 +0000213void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
214 SourceMgr::DiagKind Kind,
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000215 const Twine &Msg, ArrayRef<SMRange> Ranges,
Jordan Roseefd8f802013-01-10 18:50:15 +0000216 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
217 SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts);
Chris Lattner03b80a42011-10-16 05:43:57 +0000218
Chris Lattner3c799812010-04-06 00:26:48 +0000219 // Report the message with the diagnostic handler if present.
220 if (DiagHandler) {
Chris Lattner03b80a42011-10-16 05:43:57 +0000221 DiagHandler(Diagnostic, DiagContext);
Chris Lattner3c799812010-04-06 00:26:48 +0000222 return;
223 }
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000224
Chris Lattner854f3662012-05-06 16:20:49 +0000225 if (Loc != SMLoc()) {
226 int CurBuf = FindBufferContainingLoc(Loc);
227 assert(CurBuf != -1 && "Invalid or unspecified location!");
228 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
229 }
Chris Lattner200e0752009-07-02 23:08:13 +0000230
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000231 Diagnostic.print(0, OS, ShowColors);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000232}
Chris Lattnercc64cc92009-07-02 22:24:20 +0000233
Dmitri Gribenko8f944622013-09-27 21:09:25 +0000234void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
235 const Twine &Msg, ArrayRef<SMRange> Ranges,
236 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
237 PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
238}
239
Chris Lattnercc64cc92009-07-02 22:24:20 +0000240//===----------------------------------------------------------------------===//
241// SMDiagnostic Implementation
242//===----------------------------------------------------------------------===//
243
Jordan Roseefd8f802013-01-10 18:50:15 +0000244SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
Chris Lattner03b80a42011-10-16 05:43:57 +0000245 int Line, int Col, SourceMgr::DiagKind Kind,
Jordan Roseefd8f802013-01-10 18:50:15 +0000246 StringRef Msg, StringRef LineStr,
247 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
248 ArrayRef<SMFixIt> Hints)
Chris Lattner03b80a42011-10-16 05:43:57 +0000249 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
Jordan Roseefd8f802013-01-10 18:50:15 +0000250 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
251 FixIts(Hints.begin(), Hints.end()) {
252 std::sort(FixIts.begin(), FixIts.end());
Chris Lattner03b80a42011-10-16 05:43:57 +0000253}
Chris Lattnera3a06812011-10-16 04:47:35 +0000254
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000255static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
256 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
Jordan Roseefd8f802013-01-10 18:50:15 +0000257 if (FixIts.empty())
258 return;
259
260 const char *LineStart = SourceLine.begin();
261 const char *LineEnd = SourceLine.end();
262
263 size_t PrevHintEndCol = 0;
264
265 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
266 I != E; ++I) {
267 // If the fixit contains a newline or tab, ignore it.
268 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
269 continue;
270
271 SMRange R = I->getRange();
272
273 // If the line doesn't contain any part of the range, then ignore it.
274 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
275 continue;
276
277 // Translate from SMLoc to column.
278 // Ignore pieces of the range that go onto other lines.
279 // FIXME: Handle multibyte characters in the source line.
280 unsigned FirstCol;
281 if (R.Start.getPointer() < LineStart)
282 FirstCol = 0;
283 else
284 FirstCol = R.Start.getPointer() - LineStart;
285
286 // If we inserted a long previous hint, push this one forwards, and add
287 // an extra space to show that this is not part of the previous
288 // completion. This is sort of the best we can do when two hints appear
289 // to overlap.
290 //
291 // Note that if this hint is located immediately after the previous
292 // hint, no space will be added, since the location is more important.
293 unsigned HintCol = FirstCol;
294 if (HintCol < PrevHintEndCol)
295 HintCol = PrevHintEndCol + 1;
296
297 // FIXME: This assertion is intended to catch unintended use of multibyte
298 // characters in fixits. If we decide to do this, we'll have to track
299 // separate byte widths for the source and fixit lines.
300 assert((size_t)llvm::sys::locale::columnWidth(I->getText()) ==
301 I->getText().size());
302
303 // This relies on one byte per column in our fixit hints.
304 unsigned LastColumnModified = HintCol + I->getText().size();
305 if (LastColumnModified > FixItLine.size())
306 FixItLine.resize(LastColumnModified, ' ');
307
308 std::copy(I->getText().begin(), I->getText().end(),
309 FixItLine.begin() + HintCol);
310
311 PrevHintEndCol = LastColumnModified;
312
313 // For replacements, mark the removal range with '~'.
314 // FIXME: Handle multibyte characters in the source line.
315 unsigned LastCol;
316 if (R.End.getPointer() >= LineEnd)
317 LastCol = LineEnd - LineStart;
318 else
319 LastCol = R.End.getPointer() - LineStart;
320
321 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
322 }
323}
324
325static void printSourceLine(raw_ostream &S, StringRef LineContents) {
326 // Print out the source line one character at a time, so we can expand tabs.
327 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
328 if (LineContents[i] != '\t') {
329 S << LineContents[i];
330 ++OutCol;
331 continue;
332 }
333
334 // If we have a tab, emit at least one space, then round up to 8 columns.
335 do {
336 S << ' ';
337 ++OutCol;
338 } while ((OutCol % TabStop) != 0);
339 }
340 S << '\n';
341}
Chris Lattnera3a06812011-10-16 04:47:35 +0000342
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000343static bool isNonASCII(char c) {
344 return c & 0x80;
345}
346
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000347void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
348 bool ShowColors) const {
Daniel Dunbarc8b8c492012-07-20 18:29:44 +0000349 // Display colors only if OS supports colors.
350 ShowColors &= S.has_colors();
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000351
352 if (ShowColors)
353 S.changeColor(raw_ostream::SAVEDCOLOR, true);
354
Chris Lattnercc64cc92009-07-02 22:24:20 +0000355 if (ProgName && ProgName[0])
356 S << ProgName << ": ";
357
Dan Gohman7c675902010-01-21 10:13:27 +0000358 if (!Filename.empty()) {
359 if (Filename == "-")
360 S << "<stdin>";
361 else
362 S << Filename;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000363
Dan Gohman7c675902010-01-21 10:13:27 +0000364 if (LineNo != -1) {
365 S << ':' << LineNo;
366 if (ColumnNo != -1)
367 S << ':' << (ColumnNo+1);
368 }
369 S << ": ";
Chris Lattnercc64cc92009-07-02 22:24:20 +0000370 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000371
Chris Lattner03b80a42011-10-16 05:43:57 +0000372 switch (Kind) {
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000373 case SourceMgr::DK_Error:
374 if (ShowColors)
375 S.changeColor(raw_ostream::RED, true);
376 S << "error: ";
377 break;
378 case SourceMgr::DK_Warning:
379 if (ShowColors)
380 S.changeColor(raw_ostream::MAGENTA, true);
381 S << "warning: ";
382 break;
383 case SourceMgr::DK_Note:
384 if (ShowColors)
385 S.changeColor(raw_ostream::BLACK, true);
386 S << "note: ";
387 break;
Chris Lattner03b80a42011-10-16 05:43:57 +0000388 }
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000389
390 if (ShowColors) {
391 S.resetColor();
392 S.changeColor(raw_ostream::SAVEDCOLOR, true);
393 }
394
Dan Gohman7c675902010-01-21 10:13:27 +0000395 S << Message << '\n';
Daniel Dunbar5a308f52009-11-22 22:08:00 +0000396
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000397 if (ShowColors)
398 S.resetColor();
399
Chris Lattner72845262011-10-16 05:47:55 +0000400 if (LineNo == -1 || ColumnNo == -1)
Chris Lattnera3a06812011-10-16 04:47:35 +0000401 return;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000402
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000403 // FIXME: If there are multibyte or multi-column characters in the source, all
404 // our ranges will be wrong. To do this properly, we'll need a byte-to-column
405 // map like Clang's TextDiagnostic. For now, we'll just handle tabs by
406 // expanding them later, and bail out rather than show incorrect ranges and
407 // misaligned fixits for any other odd characters.
408 if (std::find_if(LineContents.begin(), LineContents.end(), isNonASCII) !=
409 LineContents.end()) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000410 printSourceLine(S, LineContents);
411 return;
412 }
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000413 size_t NumColumns = LineContents.size();
Jordan Roseefd8f802013-01-10 18:50:15 +0000414
Chris Lattnera3a06812011-10-16 04:47:35 +0000415 // Build the line with the caret and ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000416 std::string CaretLine(NumColumns+1, ' ');
Chris Lattnera3a06812011-10-16 04:47:35 +0000417
418 // Expand any ranges.
419 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
420 std::pair<unsigned, unsigned> R = Ranges[r];
Jordan Roseefd8f802013-01-10 18:50:15 +0000421 std::fill(&CaretLine[R.first],
422 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
423 '~');
Chris Lattnercc64cc92009-07-02 22:24:20 +0000424 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000425
426 // Add any fix-its.
427 // FIXME: Find the beginning of the line properly for multibyte characters.
428 std::string FixItInsertionLine;
429 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
430 makeArrayRef(Loc.getPointer() - ColumnNo,
431 LineContents.size()));
432
Chris Lattnera3a06812011-10-16 04:47:35 +0000433 // Finally, plop on the caret.
Jordan Roseefd8f802013-01-10 18:50:15 +0000434 if (unsigned(ColumnNo) <= NumColumns)
Chris Lattnera3a06812011-10-16 04:47:35 +0000435 CaretLine[ColumnNo] = '^';
436 else
Jordan Roseefd8f802013-01-10 18:50:15 +0000437 CaretLine[NumColumns] = '^';
Chris Lattnera3a06812011-10-16 04:47:35 +0000438
439 // ... and remove trailing whitespace so the output doesn't wrap for it. We
440 // know that the line isn't completely empty because it has the caret in it at
441 // least.
442 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
443
Jordan Roseefd8f802013-01-10 18:50:15 +0000444 printSourceLine(S, LineContents);
Chris Lattnera3a06812011-10-16 04:47:35 +0000445
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000446 if (ShowColors)
447 S.changeColor(raw_ostream::GREEN, true);
448
Chris Lattnera3a06812011-10-16 04:47:35 +0000449 // Print out the caret line, matching tabs in the source line.
450 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
451 if (i >= LineContents.size() || LineContents[i] != '\t') {
452 S << CaretLine[i];
453 ++OutCol;
454 continue;
455 }
456
457 // Okay, we have a tab. Insert the appropriate number of characters.
458 do {
459 S << CaretLine[i];
460 ++OutCol;
Jordan Roseefd8f802013-01-10 18:50:15 +0000461 } while ((OutCol % TabStop) != 0);
Chris Lattnera3a06812011-10-16 04:47:35 +0000462 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000463 S << '\n';
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000464
465 if (ShowColors)
466 S.resetColor();
Jordan Roseefd8f802013-01-10 18:50:15 +0000467
468 // Print out the replacement line, matching tabs in the source line.
469 if (FixItInsertionLine.empty())
470 return;
Chris Lattnera3a06812011-10-16 04:47:35 +0000471
Dmitri Gribenko78fe2ba2013-09-27 21:24:36 +0000472 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000473 if (i >= LineContents.size() || LineContents[i] != '\t') {
474 S << FixItInsertionLine[i];
475 ++OutCol;
476 continue;
477 }
478
479 // Okay, we have a tab. Insert the appropriate number of characters.
480 do {
481 S << FixItInsertionLine[i];
482 // FIXME: This is trying not to break up replacements, but then to re-sync
483 // with the tabs between replacements. This will fail, though, if two
484 // fix-it replacements are exactly adjacent, or if a fix-it contains a
485 // space. Really we should be precomputing column widths, which we'll
486 // need anyway for multibyte chars.
487 if (FixItInsertionLine[i] != ' ')
488 ++i;
489 ++OutCol;
490 } while (((OutCol % TabStop) != 0) && i != e);
491 }
Chris Lattnera3a06812011-10-16 04:47:35 +0000492 S << '\n';
Chris Lattnercc64cc92009-07-02 22:24:20 +0000493}