blob: 15278c598e52caf5db45a394445d501525a2cae1 [file] [log] [blame]
Chris Lattner099e1982009-06-21 03:36:54 +00001//===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===//
Chris Lattneraa739d22009-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 Lattner099e1982009-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 Lattneraa739d22009-03-13 07:05:43 +000013//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramerd1e17032010-09-27 17:42:11 +000016#include "llvm/ADT/Twine.h"
Chris Lattner099e1982009-06-21 03:36:54 +000017#include "llvm/Support/SourceMgr.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000018#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3ff95632010-12-16 03:29:14 +000019#include "llvm/ADT/OwningPtr.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000021#include "llvm/Support/system_error.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000022using namespace llvm;
23
Chris Lattner1d96ccc2009-08-11 17:49:14 +000024namespace {
25 struct LineNoCacheTy {
26 int LastQueryBufferID;
27 const char *LastQuery;
28 unsigned LineNoOfQuery;
29 };
30}
31
32static LineNoCacheTy *getCache(void *Ptr) {
33 return (LineNoCacheTy*)Ptr;
34}
35
36
Chris Lattner8070ea32009-06-21 03:41:50 +000037SourceMgr::~SourceMgr() {
Chris Lattner1d96ccc2009-08-11 17:49:14 +000038 // Delete the line # cache if allocated.
39 if (LineNoCacheTy *Cache = getCache(LineNoCache))
40 delete Cache;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000041
Chris Lattneraa739d22009-03-13 07:05:43 +000042 while (!Buffers.empty()) {
43 delete Buffers.back().Buffer;
44 Buffers.pop_back();
45 }
46}
47
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000048/// AddIncludeFile - Search for a file with the specified name in the current
49/// directory or in one of the IncludeDirs. If no file is found, this returns
50/// ~0, otherwise it returns the buffer ID of the stacked file.
51unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000052 SMLoc IncludeLoc,
53 std::string &IncludedFile) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +000054 OwningPtr<MemoryBuffer> NewBuf;
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000055 IncludedFile = Filename;
56 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000057
58 // If the file didn't exist directly, see if it's in an include path.
59 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000060 IncludedFile = IncludeDirectories[i] + "/" + Filename;
61 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000062 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000063
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000064 if (NewBuf == 0) return ~0U;
65
Michael J. Spencer3ff95632010-12-16 03:29:14 +000066 return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000067}
68
69
Chris Lattneraa739d22009-03-13 07:05:43 +000070/// FindBufferContainingLoc - Return the ID of the buffer containing the
71/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000072int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000073 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000074 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000075 // Use <= here so that a pointer to the null at the end of the buffer
76 // is included as part of the buffer.
77 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000078 return i;
79 return -1;
80}
81
Chris Lattnerc18e6d92012-05-05 22:11:04 +000082/// FindLineNumber - Find the line number for the specified location in the
83/// specified file. This is not a fast method.
84unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000085 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
86 assert(BufferID != -1 && "Invalid Location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000087
Chris Lattneraa739d22009-03-13 07:05:43 +000088 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000089
Chris Lattneraa739d22009-03-13 07:05:43 +000090 // Count the number of \n's between the start of the file and the specified
91 // location.
92 unsigned LineNo = 1;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000093
Chris Lattnerc18e6d92012-05-05 22:11:04 +000094 const char *Ptr = Buff->getBufferStart();
Chris Lattneraa739d22009-03-13 07:05:43 +000095
Chris Lattner1d96ccc2009-08-11 17:49:14 +000096 // If we have a line number cache, and if the query is to a later point in the
97 // same file, start searching from the last query location. This optimizes
98 // for the case when multiple diagnostics come out of one file in order.
99 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000100 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000101 Cache->LastQuery <= Loc.getPointer()) {
102 Ptr = Cache->LastQuery;
103 LineNo = Cache->LineNoOfQuery;
104 }
105
106 // Scan for the location being queried, keeping track of the number of lines
107 // we see.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000108 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +0000109 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000110
Chris Lattnerc18e6d92012-05-05 22:11:04 +0000111
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000112 // Allocate the line number cache if it doesn't exist.
113 if (LineNoCache == 0)
114 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000115
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000116 // Update the line # cache.
117 LineNoCacheTy &Cache = *getCache(LineNoCache);
118 Cache.LastQueryBufferID = BufferID;
119 Cache.LastQuery = Ptr;
120 Cache.LineNoOfQuery = LineNo;
Chris Lattnerc18e6d92012-05-05 22:11:04 +0000121 return LineNo;
Chris Lattneraa739d22009-03-13 07:05:43 +0000122}
123
Chris Lattner2f510ae2009-07-02 22:24:20 +0000124void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000125 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000126
Chris Lattneraa739d22009-03-13 07:05:43 +0000127 int CurBuf = FindBufferContainingLoc(IncludeLoc);
128 assert(CurBuf != -1 && "Invalid or unspecified location!");
129
Chris Lattner2f510ae2009-07-02 22:24:20 +0000130 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000131
Chris Lattner2f510ae2009-07-02 22:24:20 +0000132 OS << "Included from "
133 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
134 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattneraa739d22009-03-13 07:05:43 +0000135}
136
137
Chris Lattnereeb4a842009-07-02 23:08:13 +0000138/// GetMessage - Return an SMDiagnostic at the specified location with the
139/// specified string.
140///
141/// @param Type - If non-null, the kind of message (e.g., "error") which is
142/// prefixed to the message.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000143SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
Chris Lattner462b43c2011-10-16 05:47:55 +0000144 const Twine &Msg,
145 ArrayRef<SMRange> Ranges) const {
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000146
Chris Lattneraa739d22009-03-13 07:05:43 +0000147 // First thing to do: find the current buffer containing the specified
148 // location.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000149 int CurBuf = FindBufferContainingLoc(Loc);
Chris Lattneraa739d22009-03-13 07:05:43 +0000150 assert(CurBuf != -1 && "Invalid or unspecified location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000151
Chris Lattneraa739d22009-03-13 07:05:43 +0000152 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
Daniel Dunbar41539822009-11-22 22:08:00 +0000153
Chris Lattneraa739d22009-03-13 07:05:43 +0000154 // Scan backward to find the start of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000155 const char *LineStart = Loc.getPointer();
Chris Lattnerc18e6d92012-05-05 22:11:04 +0000156 while (LineStart != CurMB->getBufferStart() &&
157 LineStart[-1] != '\n' && LineStart[-1] != '\r')
Chris Lattneraa739d22009-03-13 07:05:43 +0000158 --LineStart;
Daniel Dunbar41539822009-11-22 22:08:00 +0000159
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000160 // Get the end of the line.
161 const char *LineEnd = Loc.getPointer();
Chris Lattnerc18e6d92012-05-05 22:11:04 +0000162 while (LineEnd != CurMB->getBufferEnd() &&
163 LineEnd[0] != '\n' && LineEnd[0] != '\r')
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000164 ++LineEnd;
165 std::string LineStr(LineStart, LineEnd);
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000166
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000167 // Convert any ranges to column ranges that only intersect the line of the
168 // location.
169 SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
170 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
171 SMRange R = Ranges[i];
172 if (!R.isValid()) continue;
173
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)
176 continue;
177
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);
183
184 // Translate from SMLoc ranges to column ranges.
185 ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart,
186 R.End.getPointer()-LineStart));
187 }
188
Chris Lattnerb0194912010-04-06 18:06:18 +0000189 return SMDiagnostic(*this, Loc,
Chris Lattnerc18e6d92012-05-05 22:11:04 +0000190 CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
191 Loc.getPointer()-LineStart, Kind, Msg.str(),
Chris Lattner462b43c2011-10-16 05:47:55 +0000192 LineStr, ColRanges);
Chris Lattnereeb4a842009-07-02 23:08:13 +0000193}
194
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000195void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000196 const Twine &Msg, ArrayRef<SMRange> Ranges,
197 bool ShowColors) const {
Chris Lattner462b43c2011-10-16 05:47:55 +0000198 SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000199
Chris Lattner8f0f4802010-04-06 00:26:48 +0000200 // Report the message with the diagnostic handler if present.
201 if (DiagHandler) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000202 DiagHandler(Diagnostic, DiagContext);
Chris Lattner8f0f4802010-04-06 00:26:48 +0000203 return;
204 }
Michael J. Spencer333ad3f2010-12-09 17:37:32 +0000205
Chris Lattnereeb4a842009-07-02 23:08:13 +0000206 raw_ostream &OS = errs();
207
208 int CurBuf = FindBufferContainingLoc(Loc);
209 assert(CurBuf != -1 && "Invalid or unspecified location!");
210 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
211
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000212 Diagnostic.print(0, OS, ShowColors);
Chris Lattneraa739d22009-03-13 07:05:43 +0000213}
Chris Lattner2f510ae2009-07-02 22:24:20 +0000214
215//===----------------------------------------------------------------------===//
216// SMDiagnostic Implementation
217//===----------------------------------------------------------------------===//
218
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000219SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN,
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000220 int Line, int Col, SourceMgr::DiagKind Kind,
221 const std::string &Msg,
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000222 const std::string &LineStr,
Chris Lattner462b43c2011-10-16 05:47:55 +0000223 ArrayRef<std::pair<unsigned,unsigned> > Ranges)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000224 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
Chris Lattner462b43c2011-10-16 05:47:55 +0000225 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000226}
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000227
228
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000229void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
230 bool ShowColors) const {
231 // Display colors only if OS goes to a tty.
232 ShowColors &= S.is_displayed();
233
234 if (ShowColors)
235 S.changeColor(raw_ostream::SAVEDCOLOR, true);
236
Chris Lattner2f510ae2009-07-02 22:24:20 +0000237 if (ProgName && ProgName[0])
238 S << ProgName << ": ";
239
Dan Gohman5e5442c2010-01-21 10:13:27 +0000240 if (!Filename.empty()) {
241 if (Filename == "-")
242 S << "<stdin>";
243 else
244 S << Filename;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000245
Dan Gohman5e5442c2010-01-21 10:13:27 +0000246 if (LineNo != -1) {
247 S << ':' << LineNo;
248 if (ColumnNo != -1)
249 S << ':' << (ColumnNo+1);
250 }
251 S << ": ";
Chris Lattner2f510ae2009-07-02 22:24:20 +0000252 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000253
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000254 switch (Kind) {
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000255 case SourceMgr::DK_Error:
256 if (ShowColors)
257 S.changeColor(raw_ostream::RED, true);
258 S << "error: ";
259 break;
260 case SourceMgr::DK_Warning:
261 if (ShowColors)
262 S.changeColor(raw_ostream::MAGENTA, true);
263 S << "warning: ";
264 break;
265 case SourceMgr::DK_Note:
266 if (ShowColors)
267 S.changeColor(raw_ostream::BLACK, true);
268 S << "note: ";
269 break;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000270 }
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000271
272 if (ShowColors) {
273 S.resetColor();
274 S.changeColor(raw_ostream::SAVEDCOLOR, true);
275 }
276
Dan Gohman5e5442c2010-01-21 10:13:27 +0000277 S << Message << '\n';
Daniel Dunbar41539822009-11-22 22:08:00 +0000278
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000279 if (ShowColors)
280 S.resetColor();
281
Chris Lattner462b43c2011-10-16 05:47:55 +0000282 if (LineNo == -1 || ColumnNo == -1)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000283 return;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000284
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000285 // Build the line with the caret and ranges.
286 std::string CaretLine(LineContents.size()+1, ' ');
287
288 // Expand any ranges.
289 for (unsigned r = 0, e = Ranges.size(); r != e; ++r) {
290 std::pair<unsigned, unsigned> R = Ranges[r];
291 for (unsigned i = R.first,
292 e = std::min(R.second, (unsigned)LineContents.size())+1; i != e; ++i)
293 CaretLine[i] = '~';
Chris Lattner2f510ae2009-07-02 22:24:20 +0000294 }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000295
296 // Finally, plop on the caret.
297 if (unsigned(ColumnNo) <= LineContents.size())
298 CaretLine[ColumnNo] = '^';
299 else
300 CaretLine[LineContents.size()] = '^';
301
302 // ... and remove trailing whitespace so the output doesn't wrap for it. We
303 // know that the line isn't completely empty because it has the caret in it at
304 // least.
305 CaretLine.erase(CaretLine.find_last_not_of(' ')+1);
306
307 // Print out the source line one character at a time, so we can expand tabs.
308 for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) {
309 if (LineContents[i] != '\t') {
310 S << LineContents[i];
311 ++OutCol;
312 continue;
313 }
314
315 // If we have a tab, emit at least one space, then round up to 8 columns.
316 do {
317 S << ' ';
318 ++OutCol;
319 } while (OutCol & 7);
320 }
321 S << '\n';
322
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000323 if (ShowColors)
324 S.changeColor(raw_ostream::GREEN, true);
325
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000326 // Print out the caret line, matching tabs in the source line.
327 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
328 if (i >= LineContents.size() || LineContents[i] != '\t') {
329 S << CaretLine[i];
330 ++OutCol;
331 continue;
332 }
333
334 // Okay, we have a tab. Insert the appropriate number of characters.
335 do {
336 S << CaretLine[i];
337 ++OutCol;
338 } while (OutCol & 7);
339 }
Benjamin Kramer89f33fd2012-04-18 19:04:15 +0000340
341 if (ShowColors)
342 S.resetColor();
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000343
344 S << '\n';
Chris Lattner2f510ae2009-07-02 22:24:20 +0000345}
346
347