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 size_t SourceMgr::AddIncludeFile(const std::string &Filename,
54 std::string &IncludedFile) {
55 std::unique_ptr<MemoryBuffer> NewBuf;
56 IncludedFile = Filename;
57 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
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) {
61 IncludedFile = IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
62 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
65 if (!NewBuf) return ~0U;
67 return AddNewSourceBuffer(NewBuf.release(), IncludeLoc);
71 int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
72 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
73 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
74 // 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())
81 std::pair<unsigned, unsigned>
82 SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const {
83 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
84 assert(BufferID != -1 && "Invalid Location!");
86 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
88 // Count the number of \n's between the start of the file and the specified
92 const char *BufStart = Buff->getBufferStart();
93 const char *Ptr = BufStart;
95 // 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))
99 if (Cache->LastQueryBufferID == BufferID &&
100 Cache->LastQuery <= Loc.getPointer()) {
101 Ptr = Cache->LastQuery;
102 LineNo = Cache->LineNoOfQuery;
105 // Scan for the location being queried, keeping track of the number of lines
107 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
108 if (*Ptr == '\n') ++LineNo;
110 // Allocate the line number cache if it doesn't exist.
112 LineNoCache = new LineNoCacheTy();
114 // Update the line # cache.
115 LineNoCacheTy &Cache = *getCache(LineNoCache);
116 Cache.LastQueryBufferID = BufferID;
117 Cache.LastQuery = Ptr;
118 Cache.LineNoOfQuery = LineNo;
120 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
121 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
122 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
125 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
126 if (IncludeLoc == SMLoc()) return; // Top of stack.
128 int CurBuf = FindBufferContainingLoc(IncludeLoc);
129 assert(CurBuf != -1 && "Invalid or unspecified location!");
131 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
133 OS << "Included from "
134 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
135 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
139 SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
141 ArrayRef<SMRange> Ranges,
142 ArrayRef<SMFixIt> FixIts) const {
144 // First thing to do: find the current buffer containing the specified
145 // location to pull out the source line.
146 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
147 std::pair<unsigned, unsigned> LineAndCol;
148 const char *BufferID = "<unknown>";
152 int CurBuf = FindBufferContainingLoc(Loc);
153 assert(CurBuf != -1 && "Invalid or unspecified location!");
155 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
156 BufferID = CurMB->getBufferIdentifier();
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')
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')
170 LineStr = std::string(LineStart, LineEnd);
172 // Convert any ranges to column ranges that only intersect the line of the
174 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
175 SMRange R = Ranges[i];
176 if (!R.isValid()) continue;
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)
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);
188 // Translate from SMLoc ranges to column ranges.
189 // FIXME: Handle multibyte characters.
190 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
191 R.End.getPointer()-LineStart));
194 LineAndCol = getLineAndColumn(Loc, CurBuf);
197 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
198 LineAndCol.second-1, Kind, Msg.str(),
199 LineStr, ColRanges, FixIts);
202 void SourceMgr::PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
203 bool ShowColors) const {
204 // Report the message with the diagnostic handler if present.
206 DiagHandler(Diagnostic, DiagContext);
210 if (Diagnostic.getLoc().isValid()) {
211 int CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
212 assert(CurBuf != -1 && "Invalid or unspecified location!");
213 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
216 Diagnostic.print(nullptr, OS, ShowColors);
219 void 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);
226 void 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);
232 //===----------------------------------------------------------------------===//
233 // SMDiagnostic Implementation
234 //===----------------------------------------------------------------------===//
236 SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
237 int Line, int Col, SourceMgr::DiagKind Kind,
238 StringRef Msg, StringRef LineStr,
239 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
240 ArrayRef<SMFixIt> Hints)
241 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
242 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
243 FixIts(Hints.begin(), Hints.end()) {
244 std::sort(FixIts.begin(), FixIts.end());
247 static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
248 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
252 const char *LineStart = SourceLine.begin();
253 const char *LineEnd = SourceLine.end();
255 size_t PrevHintEndCol = 0;
257 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
259 // If the fixit contains a newline or tab, ignore it.
260 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
263 SMRange R = I->getRange();
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)
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.
273 if (R.Start.getPointer() < LineStart)
276 FirstCol = R.Start.getPointer() - LineStart;
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
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;
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());
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, ' ');
300 std::copy(I->getText().begin(), I->getText().end(),
301 FixItLine.begin() + HintCol);
303 PrevHintEndCol = LastColumnModified;
305 // For replacements, mark the removal range with '~'.
306 // FIXME: Handle multibyte characters in the source line.
308 if (R.End.getPointer() >= LineEnd)
309 LastCol = LineEnd - LineStart;
311 LastCol = R.End.getPointer() - LineStart;
313 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
317 static 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];
326 // If we have a tab, emit at least one space, then round up to 8 columns.
330 } while ((OutCol % TabStop) != 0);
335 static bool isNonASCII(char c) {
339 void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
340 bool ShowColors) const {
341 // Display colors only if OS supports colors.
342 ShowColors &= S.has_colors();
345 S.changeColor(raw_ostream::SAVEDCOLOR, true);
347 if (ProgName && ProgName[0])
348 S << ProgName << ": ";
350 if (!Filename.empty()) {
359 S << ':' << (ColumnNo+1);
365 case SourceMgr::DK_Error:
367 S.changeColor(raw_ostream::RED, true);
370 case SourceMgr::DK_Warning:
372 S.changeColor(raw_ostream::MAGENTA, true);
375 case SourceMgr::DK_Note:
377 S.changeColor(raw_ostream::BLACK, true);
384 S.changeColor(raw_ostream::SAVEDCOLOR, true);
387 S << Message << '\n';
392 if (LineNo == -1 || ColumnNo == -1)
395 // 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()) {
402 printSourceLine(S, LineContents);
405 size_t NumColumns = LineContents.size();
407 // Build the line with the caret and ranges.
408 std::string CaretLine(NumColumns+1, ' ');
410 // Expand any ranges.
411 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
412 std::pair<unsigned, unsigned> R = Ranges[r];
413 std::fill(&CaretLine[R.first],
414 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
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()));
425 // Finally, plop on the caret.
426 if (unsigned(ColumnNo) <= NumColumns)
427 CaretLine[ColumnNo] = '^';
429 CaretLine[NumColumns] = '^';
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
434 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
436 printSourceLine(S, LineContents);
439 S.changeColor(raw_ostream::GREEN, true);
441 // 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') {
449 // Okay, we have a tab. Insert the appropriate number of characters.
453 } while ((OutCol % TabStop) != 0);
460 // Print out the replacement line, matching tabs in the source line.
461 if (FixItInsertionLine.empty())
464 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
465 if (i >= LineContents.size() || LineContents[i] != '\t') {
466 S << FixItInsertionLine[i];
471 // Okay, we have a tab. Insert the appropriate number of characters.
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] != ' ')
482 } while (((OutCol % TabStop) != 0) && i != e);