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/Support/SourceMgr.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/Locale.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <system_error>
26 static const size_t TabStop = 8;
29 struct LineNoCacheTy {
30 int LastQueryBufferID;
31 const char *LastQuery;
32 unsigned LineNoOfQuery;
36 static LineNoCacheTy *getCache(void *Ptr) {
37 return (LineNoCacheTy*)Ptr;
41 SourceMgr::~SourceMgr() {
42 // Delete the line # cache if allocated.
43 if (LineNoCacheTy *Cache = getCache(LineNoCache))
46 while (!Buffers.empty()) {
47 delete Buffers.back().Buffer;
52 /// AddIncludeFile - Search for a file with the specified name in the current
53 /// directory or in one of the IncludeDirs. If no file is found, this returns
54 /// ~0, otherwise it returns the buffer ID of the stacked file.
55 size_t SourceMgr::AddIncludeFile(const std::string &Filename,
57 std::string &IncludedFile) {
58 std::unique_ptr<MemoryBuffer> NewBuf;
59 IncludedFile = Filename;
60 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
62 // If the file didn't exist directly, see if it's in an include path.
63 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
64 IncludedFile = IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
65 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
68 if (!NewBuf) return ~0U;
70 return AddNewSourceBuffer(NewBuf.release(), IncludeLoc);
74 /// FindBufferContainingLoc - Return the ID of the buffer containing the
75 /// specified location, returning -1 if not found.
76 int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
77 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
78 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
79 // Use <= here so that a pointer to the null at the end of the buffer
80 // is included as part of the buffer.
81 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
86 /// getLineAndColumn - Find the line and column number for the specified
87 /// location in the specified file. This is not a fast method.
88 std::pair<unsigned, unsigned>
89 SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const {
90 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
91 assert(BufferID != -1 && "Invalid Location!");
93 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
95 // Count the number of \n's between the start of the file and the specified
99 const char *BufStart = Buff->getBufferStart();
100 const char *Ptr = BufStart;
102 // If we have a line number cache, and if the query is to a later point in the
103 // same file, start searching from the last query location. This optimizes
104 // for the case when multiple diagnostics come out of one file in order.
105 if (LineNoCacheTy *Cache = getCache(LineNoCache))
106 if (Cache->LastQueryBufferID == BufferID &&
107 Cache->LastQuery <= Loc.getPointer()) {
108 Ptr = Cache->LastQuery;
109 LineNo = Cache->LineNoOfQuery;
112 // Scan for the location being queried, keeping track of the number of lines
114 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
115 if (*Ptr == '\n') ++LineNo;
117 // Allocate the line number cache if it doesn't exist.
119 LineNoCache = new LineNoCacheTy();
121 // Update the line # cache.
122 LineNoCacheTy &Cache = *getCache(LineNoCache);
123 Cache.LastQueryBufferID = BufferID;
124 Cache.LastQuery = Ptr;
125 Cache.LineNoOfQuery = LineNo;
127 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
128 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
129 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
132 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
133 if (IncludeLoc == SMLoc()) return; // Top of stack.
135 int CurBuf = FindBufferContainingLoc(IncludeLoc);
136 assert(CurBuf != -1 && "Invalid or unspecified location!");
138 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
140 OS << "Included from "
141 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
142 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
146 /// GetMessage - Return an SMDiagnostic at the specified location with the
147 /// specified string.
149 /// @param Type - If non-null, the kind of message (e.g., "error") which is
150 /// prefixed to the message.
151 SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
153 ArrayRef<SMRange> Ranges,
154 ArrayRef<SMFixIt> FixIts) const {
156 // First thing to do: find the current buffer containing the specified
157 // location to pull out the source line.
158 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
159 std::pair<unsigned, unsigned> LineAndCol;
160 const char *BufferID = "<unknown>";
164 int CurBuf = FindBufferContainingLoc(Loc);
165 assert(CurBuf != -1 && "Invalid or unspecified location!");
167 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
168 BufferID = CurMB->getBufferIdentifier();
170 // Scan backward to find the start of the line.
171 const char *LineStart = Loc.getPointer();
172 const char *BufStart = CurMB->getBufferStart();
173 while (LineStart != BufStart && LineStart[-1] != '\n' &&
174 LineStart[-1] != '\r')
177 // Get the end of the line.
178 const char *LineEnd = Loc.getPointer();
179 const char *BufEnd = CurMB->getBufferEnd();
180 while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
182 LineStr = std::string(LineStart, LineEnd);
184 // Convert any ranges to column ranges that only intersect the line of the
186 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
187 SMRange R = Ranges[i];
188 if (!R.isValid()) continue;
190 // If the line doesn't contain any part of the range, then ignore it.
191 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
194 // Ignore pieces of the range that go onto other lines.
195 if (R.Start.getPointer() < LineStart)
196 R.Start = SMLoc::getFromPointer(LineStart);
197 if (R.End.getPointer() > LineEnd)
198 R.End = SMLoc::getFromPointer(LineEnd);
200 // Translate from SMLoc ranges to column ranges.
201 // FIXME: Handle multibyte characters.
202 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
203 R.End.getPointer()-LineStart));
206 LineAndCol = getLineAndColumn(Loc, CurBuf);
209 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
210 LineAndCol.second-1, Kind, Msg.str(),
211 LineStr, ColRanges, FixIts);
214 void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
215 SourceMgr::DiagKind Kind,
216 const Twine &Msg, ArrayRef<SMRange> Ranges,
217 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
218 SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts);
220 // Report the message with the diagnostic handler if present.
222 DiagHandler(Diagnostic, DiagContext);
226 if (Loc != SMLoc()) {
227 int CurBuf = FindBufferContainingLoc(Loc);
228 assert(CurBuf != -1 && "Invalid or unspecified location!");
229 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
232 Diagnostic.print(nullptr, OS, ShowColors);
235 void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
236 const Twine &Msg, ArrayRef<SMRange> Ranges,
237 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
238 PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
241 //===----------------------------------------------------------------------===//
242 // SMDiagnostic Implementation
243 //===----------------------------------------------------------------------===//
245 SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
246 int Line, int Col, SourceMgr::DiagKind Kind,
247 StringRef Msg, StringRef LineStr,
248 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
249 ArrayRef<SMFixIt> Hints)
250 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
251 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
252 FixIts(Hints.begin(), Hints.end()) {
253 std::sort(FixIts.begin(), FixIts.end());
256 static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
257 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
261 const char *LineStart = SourceLine.begin();
262 const char *LineEnd = SourceLine.end();
264 size_t PrevHintEndCol = 0;
266 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
268 // If the fixit contains a newline or tab, ignore it.
269 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
272 SMRange R = I->getRange();
274 // If the line doesn't contain any part of the range, then ignore it.
275 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
278 // Translate from SMLoc to column.
279 // Ignore pieces of the range that go onto other lines.
280 // FIXME: Handle multibyte characters in the source line.
282 if (R.Start.getPointer() < LineStart)
285 FirstCol = R.Start.getPointer() - LineStart;
287 // If we inserted a long previous hint, push this one forwards, and add
288 // an extra space to show that this is not part of the previous
289 // completion. This is sort of the best we can do when two hints appear
292 // Note that if this hint is located immediately after the previous
293 // hint, no space will be added, since the location is more important.
294 unsigned HintCol = FirstCol;
295 if (HintCol < PrevHintEndCol)
296 HintCol = PrevHintEndCol + 1;
298 // FIXME: This assertion is intended to catch unintended use of multibyte
299 // characters in fixits. If we decide to do this, we'll have to track
300 // separate byte widths for the source and fixit lines.
301 assert((size_t)llvm::sys::locale::columnWidth(I->getText()) ==
302 I->getText().size());
304 // This relies on one byte per column in our fixit hints.
305 unsigned LastColumnModified = HintCol + I->getText().size();
306 if (LastColumnModified > FixItLine.size())
307 FixItLine.resize(LastColumnModified, ' ');
309 std::copy(I->getText().begin(), I->getText().end(),
310 FixItLine.begin() + HintCol);
312 PrevHintEndCol = LastColumnModified;
314 // For replacements, mark the removal range with '~'.
315 // FIXME: Handle multibyte characters in the source line.
317 if (R.End.getPointer() >= LineEnd)
318 LastCol = LineEnd - LineStart;
320 LastCol = R.End.getPointer() - LineStart;
322 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
326 static void printSourceLine(raw_ostream &S, StringRef LineContents) {
327 // Print out the source line one character at a time, so we can expand tabs.
328 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
329 if (LineContents[i] != '\t') {
330 S << LineContents[i];
335 // If we have a tab, emit at least one space, then round up to 8 columns.
339 } while ((OutCol % TabStop) != 0);
344 static bool isNonASCII(char c) {
348 void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
349 bool ShowColors) const {
350 // Display colors only if OS supports colors.
351 ShowColors &= S.has_colors();
354 S.changeColor(raw_ostream::SAVEDCOLOR, true);
356 if (ProgName && ProgName[0])
357 S << ProgName << ": ";
359 if (!Filename.empty()) {
368 S << ':' << (ColumnNo+1);
374 case SourceMgr::DK_Error:
376 S.changeColor(raw_ostream::RED, true);
379 case SourceMgr::DK_Warning:
381 S.changeColor(raw_ostream::MAGENTA, true);
384 case SourceMgr::DK_Note:
386 S.changeColor(raw_ostream::BLACK, true);
393 S.changeColor(raw_ostream::SAVEDCOLOR, true);
396 S << Message << '\n';
401 if (LineNo == -1 || ColumnNo == -1)
404 // FIXME: If there are multibyte or multi-column characters in the source, all
405 // our ranges will be wrong. To do this properly, we'll need a byte-to-column
406 // map like Clang's TextDiagnostic. For now, we'll just handle tabs by
407 // expanding them later, and bail out rather than show incorrect ranges and
408 // misaligned fixits for any other odd characters.
409 if (std::find_if(LineContents.begin(), LineContents.end(), isNonASCII) !=
410 LineContents.end()) {
411 printSourceLine(S, LineContents);
414 size_t NumColumns = LineContents.size();
416 // Build the line with the caret and ranges.
417 std::string CaretLine(NumColumns+1, ' ');
419 // Expand any ranges.
420 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
421 std::pair<unsigned, unsigned> R = Ranges[r];
422 std::fill(&CaretLine[R.first],
423 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
428 // FIXME: Find the beginning of the line properly for multibyte characters.
429 std::string FixItInsertionLine;
430 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
431 makeArrayRef(Loc.getPointer() - ColumnNo,
432 LineContents.size()));
434 // Finally, plop on the caret.
435 if (unsigned(ColumnNo) <= NumColumns)
436 CaretLine[ColumnNo] = '^';
438 CaretLine[NumColumns] = '^';
440 // ... and remove trailing whitespace so the output doesn't wrap for it. We
441 // know that the line isn't completely empty because it has the caret in it at
443 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
445 printSourceLine(S, LineContents);
448 S.changeColor(raw_ostream::GREEN, true);
450 // Print out the caret line, matching tabs in the source line.
451 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
452 if (i >= LineContents.size() || LineContents[i] != '\t') {
458 // Okay, we have a tab. Insert the appropriate number of characters.
462 } while ((OutCol % TabStop) != 0);
469 // Print out the replacement line, matching tabs in the source line.
470 if (FixItInsertionLine.empty())
473 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
474 if (i >= LineContents.size() || LineContents[i] != '\t') {
475 S << FixItInsertionLine[i];
480 // Okay, we have a tab. Insert the appropriate number of characters.
482 S << FixItInsertionLine[i];
483 // FIXME: This is trying not to break up replacements, but then to re-sync
484 // with the tabs between replacements. This will fail, though, if two
485 // fix-it replacements are exactly adjacent, or if a fix-it contains a
486 // space. Really we should be precomputing column widths, which we'll
487 // need anyway for multibyte chars.
488 if (FixItInsertionLine[i] != ' ')
491 } while (((OutCol % TabStop) != 0) && i != e);