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/Twine.h"
18 #include "llvm/Support/Locale.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/raw_ostream.h"
24 static const size_t TabStop = 8;
27 struct LineNoCacheTy {
28 unsigned LastQueryBufferID;
29 const char *LastQuery;
30 unsigned LineNoOfQuery;
34 static LineNoCacheTy *getCache(void *Ptr) {
35 return (LineNoCacheTy*)Ptr;
39 SourceMgr::~SourceMgr() {
40 // Delete the line # cache if allocated.
41 if (LineNoCacheTy *Cache = getCache(LineNoCache))
45 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
47 std::string &IncludedFile) {
48 IncludedFile = Filename;
49 ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
50 MemoryBuffer::getFile(IncludedFile);
52 // If the file didn't exist directly, see if it's in an include path.
53 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
56 IncludeDirectories[i] + sys::path::get_separator().data() + Filename;
57 NewBufOrErr = MemoryBuffer::getFile(IncludedFile);
63 return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
66 unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
67 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
68 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
69 // Use <= here so that a pointer to the null at the end of the buffer
70 // is included as part of the buffer.
71 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
76 std::pair<unsigned, unsigned>
77 SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const {
79 BufferID = FindBufferContainingLoc(Loc);
80 assert(BufferID && "Invalid Location!");
82 const MemoryBuffer *Buff = getMemoryBuffer(BufferID);
84 // Count the number of \n's between the start of the file and the specified
88 const char *BufStart = Buff->getBufferStart();
89 const char *Ptr = BufStart;
91 // If we have a line number cache, and if the query is to a later point in the
92 // same file, start searching from the last query location. This optimizes
93 // for the case when multiple diagnostics come out of one file in order.
94 if (LineNoCacheTy *Cache = getCache(LineNoCache))
95 if (Cache->LastQueryBufferID == BufferID &&
96 Cache->LastQuery <= Loc.getPointer()) {
97 Ptr = Cache->LastQuery;
98 LineNo = Cache->LineNoOfQuery;
101 // Scan for the location being queried, keeping track of the number of lines
103 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
104 if (*Ptr == '\n') ++LineNo;
106 // Allocate the line number cache if it doesn't exist.
108 LineNoCache = new LineNoCacheTy();
110 // Update the line # cache.
111 LineNoCacheTy &Cache = *getCache(LineNoCache);
112 Cache.LastQueryBufferID = BufferID;
113 Cache.LastQuery = Ptr;
114 Cache.LineNoOfQuery = LineNo;
116 size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r");
117 if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0;
118 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
121 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
122 if (IncludeLoc == SMLoc()) return; // Top of stack.
124 unsigned CurBuf = FindBufferContainingLoc(IncludeLoc);
125 assert(CurBuf && "Invalid or unspecified location!");
127 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
129 OS << "Included from "
130 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
131 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
135 SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
137 ArrayRef<SMRange> Ranges,
138 ArrayRef<SMFixIt> FixIts) const {
140 // First thing to do: find the current buffer containing the specified
141 // location to pull out the source line.
142 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
143 std::pair<unsigned, unsigned> LineAndCol;
144 const char *BufferID = "<unknown>";
148 unsigned CurBuf = FindBufferContainingLoc(Loc);
149 assert(CurBuf && "Invalid or unspecified location!");
151 const MemoryBuffer *CurMB = getMemoryBuffer(CurBuf);
152 BufferID = CurMB->getBufferIdentifier();
154 // Scan backward to find the start of the line.
155 const char *LineStart = Loc.getPointer();
156 const char *BufStart = CurMB->getBufferStart();
157 while (LineStart != BufStart && LineStart[-1] != '\n' &&
158 LineStart[-1] != '\r')
161 // Get the end of the line.
162 const char *LineEnd = Loc.getPointer();
163 const char *BufEnd = CurMB->getBufferEnd();
164 while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
166 LineStr = std::string(LineStart, LineEnd);
168 // Convert any ranges to column ranges that only intersect the line of the
170 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
171 SMRange R = Ranges[i];
172 if (!R.isValid()) continue;
174 // If the line doesn't contain any part of the range, then ignore it.
175 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
178 // Ignore pieces of the range that go onto other lines.
179 if (R.Start.getPointer() < LineStart)
180 R.Start = SMLoc::getFromPointer(LineStart);
181 if (R.End.getPointer() > LineEnd)
182 R.End = SMLoc::getFromPointer(LineEnd);
184 // Translate from SMLoc ranges to column ranges.
185 // FIXME: Handle multibyte characters.
186 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
187 R.End.getPointer()-LineStart));
190 LineAndCol = getLineAndColumn(Loc, CurBuf);
193 return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
194 LineAndCol.second-1, Kind, Msg.str(),
195 LineStr, ColRanges, FixIts);
198 void SourceMgr::PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
199 bool ShowColors) const {
200 // Report the message with the diagnostic handler if present.
202 DiagHandler(Diagnostic, DiagContext);
206 if (Diagnostic.getLoc().isValid()) {
207 unsigned CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
208 assert(CurBuf && "Invalid or unspecified location!");
209 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
212 Diagnostic.print(nullptr, OS, ShowColors);
215 void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
216 SourceMgr::DiagKind Kind,
217 const Twine &Msg, ArrayRef<SMRange> Ranges,
218 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
219 PrintMessage(OS, GetMessage(Loc, Kind, Msg, Ranges, FixIts), ShowColors);
222 void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
223 const Twine &Msg, ArrayRef<SMRange> Ranges,
224 ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
225 PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
228 //===----------------------------------------------------------------------===//
229 // SMDiagnostic Implementation
230 //===----------------------------------------------------------------------===//
232 SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
233 int Line, int Col, SourceMgr::DiagKind Kind,
234 StringRef Msg, StringRef LineStr,
235 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
236 ArrayRef<SMFixIt> Hints)
237 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
238 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
239 FixIts(Hints.begin(), Hints.end()) {
240 std::sort(FixIts.begin(), FixIts.end());
243 static void buildFixItLine(std::string &CaretLine, std::string &FixItLine,
244 ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){
248 const char *LineStart = SourceLine.begin();
249 const char *LineEnd = SourceLine.end();
251 size_t PrevHintEndCol = 0;
253 for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end();
255 // If the fixit contains a newline or tab, ignore it.
256 if (I->getText().find_first_of("\n\r\t") != StringRef::npos)
259 SMRange R = I->getRange();
261 // If the line doesn't contain any part of the range, then ignore it.
262 if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart)
265 // Translate from SMLoc to column.
266 // Ignore pieces of the range that go onto other lines.
267 // FIXME: Handle multibyte characters in the source line.
269 if (R.Start.getPointer() < LineStart)
272 FirstCol = R.Start.getPointer() - LineStart;
274 // If we inserted a long previous hint, push this one forwards, and add
275 // an extra space to show that this is not part of the previous
276 // completion. This is sort of the best we can do when two hints appear
279 // Note that if this hint is located immediately after the previous
280 // hint, no space will be added, since the location is more important.
281 unsigned HintCol = FirstCol;
282 if (HintCol < PrevHintEndCol)
283 HintCol = PrevHintEndCol + 1;
285 // FIXME: This assertion is intended to catch unintended use of multibyte
286 // characters in fixits. If we decide to do this, we'll have to track
287 // separate byte widths for the source and fixit lines.
288 assert((size_t)llvm::sys::locale::columnWidth(I->getText()) ==
289 I->getText().size());
291 // This relies on one byte per column in our fixit hints.
292 unsigned LastColumnModified = HintCol + I->getText().size();
293 if (LastColumnModified > FixItLine.size())
294 FixItLine.resize(LastColumnModified, ' ');
296 std::copy(I->getText().begin(), I->getText().end(),
297 FixItLine.begin() + HintCol);
299 PrevHintEndCol = LastColumnModified;
301 // For replacements, mark the removal range with '~'.
302 // FIXME: Handle multibyte characters in the source line.
304 if (R.End.getPointer() >= LineEnd)
305 LastCol = LineEnd - LineStart;
307 LastCol = R.End.getPointer() - LineStart;
309 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~');
313 static void printSourceLine(raw_ostream &S, StringRef LineContents) {
314 // Print out the source line one character at a time, so we can expand tabs.
315 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
316 if (LineContents[i] != '\t') {
317 S << LineContents[i];
322 // If we have a tab, emit at least one space, then round up to 8 columns.
326 } while ((OutCol % TabStop) != 0);
331 static bool isNonASCII(char c) {
335 void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
336 bool ShowColors) const {
337 // Display colors only if OS supports colors.
338 ShowColors &= S.has_colors();
341 S.changeColor(raw_ostream::SAVEDCOLOR, true);
343 if (ProgName && ProgName[0])
344 S << ProgName << ": ";
346 if (!Filename.empty()) {
355 S << ':' << (ColumnNo+1);
361 case SourceMgr::DK_Error:
363 S.changeColor(raw_ostream::RED, true);
366 case SourceMgr::DK_Warning:
368 S.changeColor(raw_ostream::MAGENTA, true);
371 case SourceMgr::DK_Note:
373 S.changeColor(raw_ostream::BLACK, true);
380 S.changeColor(raw_ostream::SAVEDCOLOR, true);
383 S << Message << '\n';
388 if (LineNo == -1 || ColumnNo == -1)
391 // FIXME: If there are multibyte or multi-column characters in the source, all
392 // our ranges will be wrong. To do this properly, we'll need a byte-to-column
393 // map like Clang's TextDiagnostic. For now, we'll just handle tabs by
394 // expanding them later, and bail out rather than show incorrect ranges and
395 // misaligned fixits for any other odd characters.
396 if (std::find_if(LineContents.begin(), LineContents.end(), isNonASCII) !=
397 LineContents.end()) {
398 printSourceLine(S, LineContents);
401 size_t NumColumns = LineContents.size();
403 // Build the line with the caret and ranges.
404 std::string CaretLine(NumColumns+1, ' ');
406 // Expand any ranges.
407 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
408 std::pair<unsigned, unsigned> R = Ranges[r];
409 std::fill(&CaretLine[R.first],
410 &CaretLine[std::min((size_t)R.second, CaretLine.size())],
415 // FIXME: Find the beginning of the line properly for multibyte characters.
416 std::string FixItInsertionLine;
417 buildFixItLine(CaretLine, FixItInsertionLine, FixIts,
418 makeArrayRef(Loc.getPointer() - ColumnNo,
419 LineContents.size()));
421 // Finally, plop on the caret.
422 if (unsigned(ColumnNo) <= NumColumns)
423 CaretLine[ColumnNo] = '^';
425 CaretLine[NumColumns] = '^';
427 // ... and remove trailing whitespace so the output doesn't wrap for it. We
428 // know that the line isn't completely empty because it has the caret in it at
430 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
432 printSourceLine(S, LineContents);
435 S.changeColor(raw_ostream::GREEN, true);
437 // Print out the caret line, matching tabs in the source line.
438 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
439 if (i >= LineContents.size() || LineContents[i] != '\t') {
445 // Okay, we have a tab. Insert the appropriate number of characters.
449 } while ((OutCol % TabStop) != 0);
456 // Print out the replacement line, matching tabs in the source line.
457 if (FixItInsertionLine.empty())
460 for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
461 if (i >= LineContents.size() || LineContents[i] != '\t') {
462 S << FixItInsertionLine[i];
467 // Okay, we have a tab. Insert the appropriate number of characters.
469 S << FixItInsertionLine[i];
470 // FIXME: This is trying not to break up replacements, but then to re-sync
471 // with the tabs between replacements. This will fail, though, if two
472 // fix-it replacements are exactly adjacent, or if a fix-it contains a
473 // space. Really we should be precomputing column widths, which we'll
474 // need anyway for multibyte chars.
475 if (FixItInsertionLine[i] != ' ')
478 } while (((OutCol % TabStop) != 0) && i != e);