blob: a8f6208a558c96577cf37b8cf9aaa0e198c4cc6d [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Support/SourceMgr.h"
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000017#include "llvm/ADT/ArrayRef.h"
David Majnemer562e8292016-08-12 00:18:03 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/ADT/SmallVector.h"
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000020#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/Twine.h"
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000022#include "llvm/Support/ErrorOr.h"
Jordan Roseefd8f802013-01-10 18:50:15 +000023#include "llvm/Support/Locale.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/MemoryBuffer.h"
Yaron Keren15217202014-05-16 13:16:30 +000025#include "llvm/Support/Path.h"
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000026#include "llvm/Support/SMLoc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/Support/raw_ostream.h"
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000028#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <memory>
32#include <string>
33#include <utility>
34
Chris Lattner8db9bc72009-03-13 07:05:43 +000035using namespace llvm;
36
Jordan Roseefd8f802013-01-10 18:50:15 +000037static const size_t TabStop = 8;
38
Chris Lattner5d47e932009-08-11 17:49:14 +000039namespace {
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000040
Chris Lattner5d47e932009-08-11 17:49:14 +000041 struct LineNoCacheTy {
Chris Lattner5d47e932009-08-11 17:49:14 +000042 const char *LastQuery;
Benjamin Kramer0464ae82016-08-16 19:20:10 +000043 unsigned LastQueryBufferID;
Chris Lattner5d47e932009-08-11 17:49:14 +000044 unsigned LineNoOfQuery;
45 };
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000046
47} // end anonymous namespace
Chris Lattner5d47e932009-08-11 17:49:14 +000048
49static LineNoCacheTy *getCache(void *Ptr) {
50 return (LineNoCacheTy*)Ptr;
51}
52
Chris Lattnerfd255752009-06-21 03:41:50 +000053SourceMgr::~SourceMgr() {
Gabor Horvath43b72d52017-05-01 16:18:42 +000054 delete getCache(LineNoCache);
Chris Lattner8db9bc72009-03-13 07:05:43 +000055}
56
Dmitri Gribenkoa5b27a72014-07-09 08:30:15 +000057unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
58 SMLoc IncludeLoc,
59 std::string &IncludedFile) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000060 IncludedFile = Filename;
Rafael Espindolaadf21f22014-07-06 17:43:13 +000061 ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
Matt Arsenault6e863d12014-11-06 01:13:27 +000062 MemoryBuffer::getFile(IncludedFile);
Chris Lattner976af622009-06-21 05:06:04 +000063
64 // If the file didn't exist directly, see if it's in an include path.
Rafael Espindolaadf21f22014-07-06 17:43:13 +000065 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
66 ++i) {
67 IncludedFile =
68 IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
Matt Arsenault6e863d12014-11-06 01:13:27 +000069 NewBufOrErr = MemoryBuffer::getFile(IncludedFile);
Chris Lattner976af622009-06-21 05:06:04 +000070 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000071
Rafael Espindolaadf21f22014-07-06 17:43:13 +000072 if (!NewBufOrErr)
Alp Tokera55b95b2014-07-06 10:33:31 +000073 return 0;
Chris Lattner976af622009-06-21 05:06:04 +000074
David Blaikie1961f142014-08-21 20:44:56 +000075 return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
Chris Lattner976af622009-06-21 05:06:04 +000076}
77
Alp Tokera55b95b2014-07-06 10:33:31 +000078unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +000079 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner87710ca2009-03-13 16:01:53 +000080 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattner0f6dc782009-03-18 20:36:45 +000081 // Use <= here so that a pointer to the null at the end of the buffer
82 // is included as part of the buffer.
83 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Alp Tokera55b95b2014-07-06 10:33:31 +000084 return i + 1;
85 return 0;
Chris Lattner8db9bc72009-03-13 07:05:43 +000086}
87
Chris Lattner9322ba82012-05-05 22:17:32 +000088std::pair<unsigned, unsigned>
Alp Tokera55b95b2014-07-06 10:33:31 +000089SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const {
90 if (!BufferID)
91 BufferID = FindBufferContainingLoc(Loc);
92 assert(BufferID && "Invalid Location!");
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000093
Alp Toker7899d502014-06-25 00:41:15 +000094 const MemoryBuffer *Buff = getMemoryBuffer(BufferID);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000095
Chris Lattner8db9bc72009-03-13 07:05:43 +000096 // Count the number of \n's between the start of the file and the specified
97 // location.
98 unsigned LineNo = 1;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +000099
Chris Lattner9322ba82012-05-05 22:17:32 +0000100 const char *BufStart = Buff->getBufferStart();
101 const char *Ptr = BufStart;
Chris Lattner8db9bc72009-03-13 07:05:43 +0000102
Chris Lattner5d47e932009-08-11 17:49:14 +0000103 // If we have a line number cache, and if the query is to a later point in the
104 // same file, start searching from the last query location. This optimizes
105 // for the case when multiple diagnostics come out of one file in order.
106 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000107 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner5d47e932009-08-11 17:49:14 +0000108 Cache->LastQuery <= Loc.getPointer()) {
109 Ptr = Cache->LastQuery;
110 LineNo = Cache->LineNoOfQuery;
111 }
112
113 // Scan for the location being queried, keeping track of the number of lines
114 // we see.
Chris Lattner526c8cb2009-06-21 03:39:35 +0000115 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattner8db9bc72009-03-13 07:05:43 +0000116 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000117
Chris Lattner5d47e932009-08-11 17:49:14 +0000118 // Allocate the line number cache if it doesn't exist.
Craig Topper8d399f82014-04-09 04:20:00 +0000119 if (!LineNoCache)
Chris Lattner5d47e932009-08-11 17:49:14 +0000120 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000121
Chris Lattner5d47e932009-08-11 17:49:14 +0000122 // Update the line # cache.
123 LineNoCacheTy &Cache = *getCache(LineNoCache);
124 Cache.LastQueryBufferID = BufferID;
125 Cache.LastQuery = Ptr;
126 Cache.LineNoOfQuery = LineNo;
Chris Lattner9322ba82012-05-05 22:17:32 +0000127
128 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
Matt Beaumont-Gaya1b3b002012-05-07 18:12:42 +0000129 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
Chris Lattner9322ba82012-05-05 22:17:32 +0000130 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000131}
132
Chris Lattnercc64cc92009-07-02 22:24:20 +0000133void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner526c8cb2009-06-21 03:39:35 +0000134 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000135
Alp Tokera55b95b2014-07-06 10:33:31 +0000136 unsigned CurBuf = FindBufferContainingLoc(IncludeLoc);
137 assert(CurBuf && "Invalid or unspecified location!");
Chris Lattner8db9bc72009-03-13 07:05:43 +0000138
Chris Lattnercc64cc92009-07-02 22:24:20 +0000139 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000140
Chris Lattnercc64cc92009-07-02 22:24:20 +0000141 OS << "Included from "
142 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
143 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattner8db9bc72009-03-13 07:05:43 +0000144}
145
Chris Lattner03b80a42011-10-16 05:43:57 +0000146SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
Chris Lattner72845262011-10-16 05:47:55 +0000147 const Twine &Msg,
Jordan Roseefd8f802013-01-10 18:50:15 +0000148 ArrayRef<SMRange> Ranges,
149 ArrayRef<SMFixIt> FixIts) const {
Chris Lattner8db9bc72009-03-13 07:05:43 +0000150 // First thing to do: find the current buffer containing the specified
Chris Lattner854f3662012-05-06 16:20:49 +0000151 // location to pull out the source line.
Chris Lattnera3a06812011-10-16 04:47:35 +0000152 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
Chris Lattner854f3662012-05-06 16:20:49 +0000153 std::pair<unsigned, unsigned> LineAndCol;
Mehdi Amini99d1b292016-10-01 16:38:28 +0000154 StringRef BufferID = "<unknown>";
Chris Lattner854f3662012-05-06 16:20:49 +0000155 std::string LineStr;
Chris Lattnera3a06812011-10-16 04:47:35 +0000156
Chris Lattner854f3662012-05-06 16:20:49 +0000157 if (Loc.isValid()) {
Alp Tokera55b95b2014-07-06 10:33:31 +0000158 unsigned CurBuf = FindBufferContainingLoc(Loc);
159 assert(CurBuf && "Invalid or unspecified location!");
Chris Lattner854f3662012-05-06 16:20:49 +0000160
Alp Toker7899d502014-06-25 00:41:15 +0000161 const MemoryBuffer *CurMB = getMemoryBuffer(CurBuf);
Chris Lattner854f3662012-05-06 16:20:49 +0000162 BufferID = CurMB->getBufferIdentifier();
163
164 // Scan backward to find the start of the line.
165 const char *LineStart = Loc.getPointer();
166 const char *BufStart = CurMB->getBufferStart();
167 while (LineStart != BufStart && LineStart[-1] != '\n' &&
168 LineStart[-1] != '\r')
169 --LineStart;
170
171 // Get the end of the line.
172 const char *LineEnd = Loc.getPointer();
173 const char *BufEnd = CurMB->getBufferEnd();
174 while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
175 ++LineEnd;
176 LineStr = std::string(LineStart, LineEnd);
177
178 // Convert any ranges to column ranges that only intersect the line of the
179 // location.
180 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
181 SMRange R = Ranges[i];
182 if (!R.isValid()) continue;
183
184 // If the line doesn't contain any part of the range, then ignore it.
185 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
186 continue;
187
188 // Ignore pieces of the range that go onto other lines.
189 if (R.Start.getPointer() < LineStart)
190 R.Start = SMLoc::getFromPointer(LineStart);
191 if (R.End.getPointer() > LineEnd)
192 R.End = SMLoc::getFromPointer(LineEnd);
193
194 // Translate from SMLoc ranges to column ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000195 // FIXME: Handle multibyte characters.
Chris Lattner854f3662012-05-06 16:20:49 +0000196 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
197 R.End.getPointer()-LineStart));
198 }
199
200 LineAndCol = getLineAndColumn(Loc, CurBuf);
201 }
202
203 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
Chris Lattner9322ba82012-05-05 22:17:32 +0000204 LineAndCol.second-1, Kind, Msg.str(),
Jordan Roseefd8f802013-01-10 18:50:15 +0000205 LineStr, ColRanges, FixIts);
Chris Lattner200e0752009-07-02 23:08:13 +0000206}
207
Jordan Rose57ffdb02014-06-17 02:15:40 +0000208void SourceMgr::PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
209 bool ShowColors) const {
Chris Lattner3c799812010-04-06 00:26:48 +0000210 // Report the message with the diagnostic handler if present.
211 if (DiagHandler) {
Chris Lattner03b80a42011-10-16 05:43:57 +0000212 DiagHandler(Diagnostic, DiagContext);
Chris Lattner3c799812010-04-06 00:26:48 +0000213 return;
214 }
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000215
Jordan Rose57ffdb02014-06-17 02:15:40 +0000216 if (Diagnostic.getLoc().isValid()) {
Alp Tokera55b95b2014-07-06 10:33:31 +0000217 unsigned CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
218 assert(CurBuf && "Invalid or unspecified location!");
Chris Lattner854f3662012-05-06 16:20:49 +0000219 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
220 }
Chris Lattner200e0752009-07-02 23:08:13 +0000221
Craig Topperc10719f2014-04-07 04:17:22 +0000222 Diagnostic.print(nullptr, OS, ShowColors);
Chris Lattner8db9bc72009-03-13 07:05:43 +0000223}
Chris Lattnercc64cc92009-07-02 22:24:20 +0000224
Jordan Rose57ffdb02014-06-17 02:15:40 +0000225void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
226 SourceMgr::DiagKind Kind,
227 const Twine &Msg, ArrayRef<SMRange> Ranges,
228 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
229 PrintMessage(OS, GetMessage(Loc, Kind, Msg, Ranges, FixIts), ShowColors);
230}
231
Dmitri Gribenko8f944622013-09-27 21:09:25 +0000232void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
233 const Twine &Msg, ArrayRef<SMRange> Ranges,
234 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
Eugene Zelenko454d0ce2017-02-15 22:17:02 +0000235 PrintMessage(errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
Dmitri Gribenko8f944622013-09-27 21:09:25 +0000236}
237
Chris Lattnercc64cc92009-07-02 22:24:20 +0000238//===----------------------------------------------------------------------===//
239// SMDiagnostic Implementation
240//===----------------------------------------------------------------------===//
241
Jordan Roseefd8f802013-01-10 18:50:15 +0000242SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
Chris Lattner03b80a42011-10-16 05:43:57 +0000243 int Line, int Col, SourceMgr::DiagKind Kind,
Jordan Roseefd8f802013-01-10 18:50:15 +0000244 StringRef Msg, StringRef LineStr,
Eugene Zelenko454d0ce2017-02-15 22:17:02 +0000245 ArrayRef<std::pair<unsigned,unsigned>> Ranges,
Jordan Roseefd8f802013-01-10 18:50:15 +0000246 ArrayRef<SMFixIt> Hints)
Chris Lattner03b80a42011-10-16 05:43:57 +0000247 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
Jordan Roseefd8f802013-01-10 18:50:15 +0000248 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
249 FixIts(Hints.begin(), Hints.end()) {
250 std::sort(FixIts.begin(), FixIts.end());
Chris Lattner03b80a42011-10-16 05:43:57 +0000251}
Chris Lattnera3a06812011-10-16 04:47:35 +0000252
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000253static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
254 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
Jordan Roseefd8f802013-01-10 18:50:15 +0000255 if (FixIts.empty())
256 return;
257
258 const char *LineStart = SourceLine.begin();
259 const char *LineEnd = SourceLine.end();
260
261 size_t PrevHintEndCol = 0;
262
263 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
264 I != E; ++I) {
265 // If the fixit contains a newline or tab, ignore it.
266 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
267 continue;
268
269 SMRange R = I->getRange();
270
271 // If the line doesn't contain any part of the range, then ignore it.
272 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
273 continue;
274
275 // Translate from SMLoc to column.
276 // Ignore pieces of the range that go onto other lines.
277 // FIXME: Handle multibyte characters in the source line.
278 unsigned FirstCol;
279 if (R.Start.getPointer() < LineStart)
280 FirstCol = 0;
281 else
282 FirstCol = R.Start.getPointer() - LineStart;
283
284 // If we inserted a long previous hint, push this one forwards, and add
285 // an extra space to show that this is not part of the previous
286 // completion. This is sort of the best we can do when two hints appear
287 // to overlap.
288 //
289 // Note that if this hint is located immediately after the previous
290 // hint, no space will be added, since the location is more important.
291 unsigned HintCol = FirstCol;
292 if (HintCol < PrevHintEndCol)
293 HintCol = PrevHintEndCol + 1;
294
295 // FIXME: This assertion is intended to catch unintended use of multibyte
296 // characters in fixits. If we decide to do this, we'll have to track
297 // separate byte widths for the source and fixit lines.
Eugene Zelenko454d0ce2017-02-15 22:17:02 +0000298 assert((size_t)sys::locale::columnWidth(I->getText()) ==
Jordan Roseefd8f802013-01-10 18:50:15 +0000299 I->getText().size());
300
301 // This relies on one byte per column in our fixit hints.
302 unsigned LastColumnModified = HintCol + I->getText().size();
303 if (LastColumnModified > FixItLine.size())
304 FixItLine.resize(LastColumnModified, ' ');
305
306 std::copy(I->getText().begin(), I->getText().end(),
307 FixItLine.begin() + HintCol);
308
309 PrevHintEndCol = LastColumnModified;
310
311 // For replacements, mark the removal range with '~'.
312 // FIXME: Handle multibyte characters in the source line.
313 unsigned LastCol;
314 if (R.End.getPointer() >= LineEnd)
315 LastCol = LineEnd - LineStart;
316 else
317 LastCol = R.End.getPointer() - LineStart;
318
319 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
320 }
321}
322
323static void printSourceLine(raw_ostream &S, StringRef LineContents) {
324 // Print out the source line one character at a time, so we can expand tabs.
325 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
326 if (LineContents[i] != '\t') {
327 S << LineContents[i];
328 ++OutCol;
329 continue;
330 }
331
332 // If we have a tab, emit at least one space, then round up to 8 columns.
333 do {
334 S << ' ';
335 ++OutCol;
336 } while ((OutCol % TabStop) != 0);
337 }
338 S << '\n';
339}
Chris Lattnera3a06812011-10-16 04:47:35 +0000340
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000341static bool isNonASCII(char c) {
342 return c & 0x80;
343}
344
Alex Lorenz735c47e2015-06-15 20:30:22 +0000345void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors,
346 bool ShowKindLabel) const {
Daniel Dunbarc8b8c492012-07-20 18:29:44 +0000347 // Display colors only if OS supports colors.
348 ShowColors &= S.has_colors();
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000349
350 if (ShowColors)
351 S.changeColor(raw_ostream::SAVEDCOLOR, true);
352
Chris Lattnercc64cc92009-07-02 22:24:20 +0000353 if (ProgName && ProgName[0])
354 S << ProgName << ": ";
355
Dan Gohman7c675902010-01-21 10:13:27 +0000356 if (!Filename.empty()) {
357 if (Filename == "-")
358 S << "<stdin>";
359 else
360 S << Filename;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000361
Dan Gohman7c675902010-01-21 10:13:27 +0000362 if (LineNo != -1) {
363 S << ':' << LineNo;
364 if (ColumnNo != -1)
365 S << ':' << (ColumnNo+1);
366 }
367 S << ": ";
Chris Lattnercc64cc92009-07-02 22:24:20 +0000368 }
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000369
Alex Lorenz735c47e2015-06-15 20:30:22 +0000370 if (ShowKindLabel) {
371 switch (Kind) {
372 case SourceMgr::DK_Error:
373 if (ShowColors)
374 S.changeColor(raw_ostream::RED, true);
375 S << "error: ";
376 break;
377 case SourceMgr::DK_Warning:
378 if (ShowColors)
379 S.changeColor(raw_ostream::MAGENTA, true);
380 S << "warning: ";
381 break;
382 case SourceMgr::DK_Note:
383 if (ShowColors)
384 S.changeColor(raw_ostream::BLACK, true);
385 S << "note: ";
386 break;
Adam Nemet01104ae2017-10-12 23:56:02 +0000387 case SourceMgr::DK_Remark:
388 if (ShowColors)
389 S.changeColor(raw_ostream::BLUE, true);
390 S << "remark: ";
391 break;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000392 }
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000393
Alex Lorenz735c47e2015-06-15 20:30:22 +0000394 if (ShowColors) {
395 S.resetColor();
396 S.changeColor(raw_ostream::SAVEDCOLOR, true);
397 }
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000398 }
399
Dan Gohman7c675902010-01-21 10:13:27 +0000400 S << Message << '\n';
Daniel Dunbar5a308f52009-11-22 22:08:00 +0000401
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000402 if (ShowColors)
403 S.resetColor();
404
Chris Lattner72845262011-10-16 05:47:55 +0000405 if (LineNo == -1 || ColumnNo == -1)
Chris Lattnera3a06812011-10-16 04:47:35 +0000406 return;
Mikhail Glushenkov84afae32010-01-27 10:13:11 +0000407
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000408 // FIXME: If there are multibyte or multi-column characters in the source, all
409 // our ranges will be wrong. To do this properly, we'll need a byte-to-column
410 // map like Clang's TextDiagnostic. For now, we'll just handle tabs by
411 // expanding them later, and bail out rather than show incorrect ranges and
412 // misaligned fixits for any other odd characters.
David Majnemer562e8292016-08-12 00:18:03 +0000413 if (find_if(LineContents, isNonASCII) != LineContents.end()) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000414 printSourceLine(S, LineContents);
415 return;
416 }
Jordan Roseceb1dbb2013-01-11 02:37:55 +0000417 size_t NumColumns = LineContents.size();
Jordan Roseefd8f802013-01-10 18:50:15 +0000418
Chris Lattnera3a06812011-10-16 04:47:35 +0000419 // Build the line with the caret and ranges.
Jordan Roseefd8f802013-01-10 18:50:15 +0000420 std::string CaretLine(NumColumns+1, ' ');
Chris Lattnera3a06812011-10-16 04:47:35 +0000421
422 // Expand any ranges.
423 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
424 std::pair<unsigned, unsigned> R = Ranges[r];
Jordan Roseefd8f802013-01-10 18:50:15 +0000425 std::fill(&CaretLine[R.first],
426 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
427 '~');
Chris Lattnercc64cc92009-07-02 22:24:20 +0000428 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000429
430 // Add any fix-its.
431 // FIXME: Find the beginning of the line properly for multibyte characters.
432 std::string FixItInsertionLine;
433 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
434 makeArrayRef(Loc.getPointer() - ColumnNo,
435 LineContents.size()));
436
Chris Lattnera3a06812011-10-16 04:47:35 +0000437 // Finally, plop on the caret.
Jordan Roseefd8f802013-01-10 18:50:15 +0000438 if (unsigned(ColumnNo) <= NumColumns)
Chris Lattnera3a06812011-10-16 04:47:35 +0000439 CaretLine[ColumnNo] = '^';
440 else
Jordan Roseefd8f802013-01-10 18:50:15 +0000441 CaretLine[NumColumns] = '^';
Chris Lattnera3a06812011-10-16 04:47:35 +0000442
443 // ... and remove trailing whitespace so the output doesn't wrap for it. We
444 // know that the line isn't completely empty because it has the caret in it at
445 // least.
446 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
447
Jordan Roseefd8f802013-01-10 18:50:15 +0000448 printSourceLine(S, LineContents);
Chris Lattnera3a06812011-10-16 04:47:35 +0000449
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000450 if (ShowColors)
451 S.changeColor(raw_ostream::GREEN, true);
452
Chris Lattnera3a06812011-10-16 04:47:35 +0000453 // Print out the caret line, matching tabs in the source line.
454 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
455 if (i >= LineContents.size() || LineContents[i] != '\t') {
456 S << CaretLine[i];
457 ++OutCol;
458 continue;
459 }
460
461 // Okay, we have a tab. Insert the appropriate number of characters.
462 do {
463 S << CaretLine[i];
464 ++OutCol;
Jordan Roseefd8f802013-01-10 18:50:15 +0000465 } while ((OutCol % TabStop) != 0);
Chris Lattnera3a06812011-10-16 04:47:35 +0000466 }
Jordan Roseefd8f802013-01-10 18:50:15 +0000467 S << '\n';
Benjamin Kramerbb73d192012-04-18 19:04:15 +0000468
469 if (ShowColors)
470 S.resetColor();
Jordan Roseefd8f802013-01-10 18:50:15 +0000471
472 // Print out the replacement line, matching tabs in the source line.
473 if (FixItInsertionLine.empty())
474 return;
Chris Lattnera3a06812011-10-16 04:47:35 +0000475
Dmitri Gribenko78fe2ba2013-09-27 21:24:36 +0000476 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
Jordan Roseefd8f802013-01-10 18:50:15 +0000477 if (i >= LineContents.size() || LineContents[i] != '\t') {
478 S << FixItInsertionLine[i];
479 ++OutCol;
480 continue;
481 }
482
483 // Okay, we have a tab. Insert the appropriate number of characters.
484 do {
485 S << FixItInsertionLine[i];
486 // FIXME: This is trying not to break up replacements, but then to re-sync
487 // with the tabs between replacements. This will fail, though, if two
488 // fix-it replacements are exactly adjacent, or if a fix-it contains a
489 // space. Really we should be precomputing column widths, which we'll
490 // need anyway for multibyte chars.
491 if (FixItInsertionLine[i] != ' ')
492 ++i;
493 ++OutCol;
494 } while (((OutCol % TabStop) != 0) && i != e);
495 }
Chris Lattnera3a06812011-10-16 04:47:35 +0000496 S << '\n';
Chris Lattnercc64cc92009-07-02 22:24:20 +0000497}