1 //===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include "llvm/Support/Locale.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/SourceMgr.h"
37 static const size_t TabStop = 8;
41 struct LineNoCacheTy {
42 const char *LastQuery;
43 unsigned LastQueryBufferID;
44 unsigned LineNoOfQuery;
47 } // end anonymous namespace
49 static LineNoCacheTy *getCache(void *Ptr) {
50 return (LineNoCacheTy*)Ptr;
53 SourceMgr::~SourceMgr() {
54 delete getCache(LineNoCache);
57 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
59 std::string &IncludedFile) {
60 IncludedFile = Filename;
61 ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
62 MemoryBuffer::getFile(IncludedFile);
64 // If the file didn't exist directly, see if it's in an include path.
65 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
68 IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
69 NewBufOrErr = MemoryBuffer::getFile(IncludedFile);
75 return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
78 unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
79 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
80 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
81 // 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())
88 std::pair<unsigned, unsigned>
89 SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const {
91 BufferID = FindBufferContainingLoc(Loc);
92 assert(BufferID && "Invalid Location!");
94 const MemoryBuffer *Buff = getMemoryBuffer(BufferID);
96 // Count the number of \n's between the start of the file and the specified
100 const char *BufStart = Buff->getBufferStart();
101 const char *Ptr = BufStart;
103 // 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))
107 if (Cache->LastQueryBufferID == BufferID &&
108 Cache->LastQuery <= Loc.getPointer()) {
109 Ptr = Cache->LastQuery;
110 LineNo = Cache->LineNoOfQuery;
113 // Scan for the location being queried, keeping track of the number of lines
115 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
116 if (*Ptr == '\n') ++LineNo;
118 // Allocate the line number cache if it doesn't exist.
120 LineNoCache = new LineNoCacheTy();
122 // Update the line # cache.
123 LineNoCacheTy &Cache = *getCache(LineNoCache);
124 Cache.LastQueryBufferID = BufferID;
125 Cache.LastQuery = Ptr;
126 Cache.LineNoOfQuery = LineNo;
128 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
129 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
130 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
133 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
134 if (IncludeLoc == SMLoc()) return; // Top of stack.
136 unsigned CurBuf = FindBufferContainingLoc(IncludeLoc);
137 assert(CurBuf && "Invalid or unspecified location!");
139 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
141 OS << "Included from "
142 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
143 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
146 SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
148 ArrayRef<SMRange> Ranges,
149 ArrayRef<SMFixIt> FixIts) const {
150 // First thing to do: find the current buffer containing the specified
151 // location to pull out the source line.
152 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
153 std::pair<unsigned, unsigned> LineAndCol;
154 StringRef BufferID = "<unknown>";
158 unsigned CurBuf = FindBufferContainingLoc(Loc);
159 assert(CurBuf && "Invalid or unspecified location!");
161 const MemoryBuffer *CurMB = getMemoryBuffer(CurBuf);
162 BufferID = CurMB->getBufferIdentifier();
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')
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')
176 LineStr = std::string(LineStart, LineEnd);
178 // Convert any ranges to column ranges that only intersect the line of the
180 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
181 SMRange R = Ranges[i];
182 if (!R.isValid()) continue;
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)
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);
194 // Translate from SMLoc ranges to column ranges.
195 // FIXME: Handle multibyte characters.
196 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
197 R.End.getPointer()-LineStart));
200 LineAndCol = getLineAndColumn(Loc, CurBuf);
203 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
204 LineAndCol.second-1, Kind, Msg.str(),
205 LineStr, ColRanges, FixIts);
208 void SourceMgr::PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
209 bool ShowColors) const {
210 // Report the message with the diagnostic handler if present.
212 DiagHandler(Diagnostic, DiagContext);
216 if (Diagnostic.getLoc().isValid()) {
217 unsigned CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
218 assert(CurBuf && "Invalid or unspecified location!");
219 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
222 Diagnostic.print(nullptr, OS, ShowColors);
225 void 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);
232 void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
233 const Twine &Msg, ArrayRef<SMRange> Ranges,
234 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
235 PrintMessage(errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
238 //===----------------------------------------------------------------------===//
239 // SMDiagnostic Implementation
240 //===----------------------------------------------------------------------===//
242 SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
243 int Line, int Col, SourceMgr::DiagKind Kind,
244 StringRef Msg, StringRef LineStr,
245 ArrayRef<std::pair<unsigned,unsigned>> Ranges,
246 ArrayRef<SMFixIt> Hints)
247 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
248 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
249 FixIts(Hints.begin(), Hints.end()) {
250 std::sort(FixIts.begin(), FixIts.end());
253 static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
254 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
258 const char *LineStart = SourceLine.begin();
259 const char *LineEnd = SourceLine.end();
261 size_t PrevHintEndCol = 0;
263 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
265 // If the fixit contains a newline or tab, ignore it.
266 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
269 SMRange R = I->getRange();
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)
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.
279 if (R.Start.getPointer() < LineStart)
282 FirstCol = R.Start.getPointer() - LineStart;
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
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;
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.
298 assert((size_t)sys::locale::columnWidth(I->getText()) ==
299 I->getText().size());
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, ' ');
306 std::copy(I->getText().begin(), I->getText().end(),
307 FixItLine.begin() + HintCol);
309 PrevHintEndCol = LastColumnModified;
311 // For replacements, mark the removal range with '~'.
312 // FIXME: Handle multibyte characters in the source line.
314 if (R.End.getPointer() >= LineEnd)
315 LastCol = LineEnd - LineStart;
317 LastCol = R.End.getPointer() - LineStart;
319 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
323 static 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];
332 // If we have a tab, emit at least one space, then round up to 8 columns.
336 } while ((OutCol % TabStop) != 0);
341 static bool isNonASCII(char c) {
345 void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors,
346 bool ShowKindLabel) const {
347 // Display colors only if OS supports colors.
348 ShowColors &= S.has_colors();
351 S.changeColor(raw_ostream::SAVEDCOLOR, true);
353 if (ProgName && ProgName[0])
354 S << ProgName << ": ";
356 if (!Filename.empty()) {
365 S << ':' << (ColumnNo+1);
372 case SourceMgr::DK_Error:
374 S.changeColor(raw_ostream::RED, true);
377 case SourceMgr::DK_Warning:
379 S.changeColor(raw_ostream::MAGENTA, true);
382 case SourceMgr::DK_Note:
384 S.changeColor(raw_ostream::BLACK, true);
391 S.changeColor(raw_ostream::SAVEDCOLOR, true);
395 S << Message << '\n';
400 if (LineNo == -1 || ColumnNo == -1)
403 // 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 (find_if(LineContents, isNonASCII) != LineContents.end()) {
409 printSourceLine(S, LineContents);
412 size_t NumColumns = LineContents.size();
414 // Build the line with the caret and ranges.
415 std::string CaretLine(NumColumns+1, ' ');
417 // Expand any ranges.
418 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
419 std::pair<unsigned, unsigned> R = Ranges[r];
420 std::fill(&CaretLine[R.first],
421 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
426 // FIXME: Find the beginning of the line properly for multibyte characters.
427 std::string FixItInsertionLine;
428 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
429 makeArrayRef(Loc.getPointer() - ColumnNo,
430 LineContents.size()));
432 // Finally, plop on the caret.
433 if (unsigned(ColumnNo) <= NumColumns)
434 CaretLine[ColumnNo] = '^';
436 CaretLine[NumColumns] = '^';
438 // ... and remove trailing whitespace so the output doesn't wrap for it. We
439 // know that the line isn't completely empty because it has the caret in it at
441 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
443 printSourceLine(S, LineContents);
446 S.changeColor(raw_ostream::GREEN, true);
448 // Print out the caret line, matching tabs in the source line.
449 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
450 if (i >= LineContents.size() || LineContents[i] != '\t') {
456 // Okay, we have a tab. Insert the appropriate number of characters.
460 } while ((OutCol % TabStop) != 0);
467 // Print out the replacement line, matching tabs in the source line.
468 if (FixItInsertionLine.empty())
471 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
472 if (i >= LineContents.size() || LineContents[i] != '\t') {
473 S << FixItInsertionLine[i];
478 // Okay, we have a tab. Insert the appropriate number of characters.
480 S << FixItInsertionLine[i];
481 // FIXME: This is trying not to break up replacements, but then to re-sync
482 // with the tabs between replacements. This will fail, though, if two
483 // fix-it replacements are exactly adjacent, or if a fix-it contains a
484 // space. Really we should be precomputing column widths, which we'll
485 // need anyway for multibyte chars.
486 if (FixItInsertionLine[i] != ' ')
489 } while (((OutCol % TabStop) != 0) && i != e);