blob: f53169a8cfc944f86ea70a1eae74ccd125059d6d [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"
19#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000020#include "llvm/Support/system_error.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000021using namespace llvm;
22
Chris Lattner1d96ccc2009-08-11 17:49:14 +000023namespace {
24 struct LineNoCacheTy {
25 int LastQueryBufferID;
26 const char *LastQuery;
27 unsigned LineNoOfQuery;
28 };
29}
30
31static LineNoCacheTy *getCache(void *Ptr) {
32 return (LineNoCacheTy*)Ptr;
33}
34
35
Chris Lattner8070ea32009-06-21 03:41:50 +000036SourceMgr::~SourceMgr() {
Chris Lattner1d96ccc2009-08-11 17:49:14 +000037 // Delete the line # cache if allocated.
38 if (LineNoCacheTy *Cache = getCache(LineNoCache))
39 delete Cache;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000040
Chris Lattneraa739d22009-03-13 07:05:43 +000041 while (!Buffers.empty()) {
42 delete Buffers.back().Buffer;
43 Buffers.pop_back();
44 }
45}
46
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000047/// AddIncludeFile - Search for a file with the specified name in the current
48/// directory or in one of the IncludeDirs. If no file is found, this returns
49/// ~0, otherwise it returns the buffer ID of the stacked file.
50unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
51 SMLoc IncludeLoc) {
Michael J. Spencer333fb042010-12-09 17:36:48 +000052 error_code ec;
53 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str(), ec);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000054
55 // If the file didn't exist directly, see if it's in an include path.
56 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
57 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
Michael J. Spencer333fb042010-12-09 17:36:48 +000058 NewBuf = MemoryBuffer::getFile(IncFile.c_str(), ec);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000059 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000060
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000061 if (NewBuf == 0) return ~0U;
62
63 return AddNewSourceBuffer(NewBuf, IncludeLoc);
64}
65
66
Chris Lattneraa739d22009-03-13 07:05:43 +000067/// FindBufferContainingLoc - Return the ID of the buffer containing the
68/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000069int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000070 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000071 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000072 // Use <= here so that a pointer to the null at the end of the buffer
73 // is included as part of the buffer.
74 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000075 return i;
76 return -1;
77}
78
79/// FindLineNumber - Find the line number for the specified location in the
80/// specified file. This is not a fast method.
Chris Lattner8070ea32009-06-21 03:41:50 +000081unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000082 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
83 assert(BufferID != -1 && "Invalid Location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000084
Chris Lattneraa739d22009-03-13 07:05:43 +000085 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000086
Chris Lattneraa739d22009-03-13 07:05:43 +000087 // Count the number of \n's between the start of the file and the specified
88 // location.
89 unsigned LineNo = 1;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000090
Chris Lattneraa739d22009-03-13 07:05:43 +000091 const char *Ptr = Buff->getBufferStart();
92
Chris Lattner1d96ccc2009-08-11 17:49:14 +000093 // If we have a line number cache, and if the query is to a later point in the
94 // same file, start searching from the last query location. This optimizes
95 // for the case when multiple diagnostics come out of one file in order.
96 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000097 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner1d96ccc2009-08-11 17:49:14 +000098 Cache->LastQuery <= Loc.getPointer()) {
99 Ptr = Cache->LastQuery;
100 LineNo = Cache->LineNoOfQuery;
101 }
102
103 // Scan for the location being queried, keeping track of the number of lines
104 // we see.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000105 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +0000106 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000107
108
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000109 // Allocate the line number cache if it doesn't exist.
110 if (LineNoCache == 0)
111 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000112
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000113 // Update the line # cache.
114 LineNoCacheTy &Cache = *getCache(LineNoCache);
115 Cache.LastQueryBufferID = BufferID;
116 Cache.LastQuery = Ptr;
117 Cache.LineNoOfQuery = LineNo;
Chris Lattneraa739d22009-03-13 07:05:43 +0000118 return LineNo;
119}
120
Chris Lattner2f510ae2009-07-02 22:24:20 +0000121void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000122 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000123
Chris Lattneraa739d22009-03-13 07:05:43 +0000124 int CurBuf = FindBufferContainingLoc(IncludeLoc);
125 assert(CurBuf != -1 && "Invalid or unspecified location!");
126
Chris Lattner2f510ae2009-07-02 22:24:20 +0000127 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000128
Chris Lattner2f510ae2009-07-02 22:24:20 +0000129 OS << "Included from "
130 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
131 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattneraa739d22009-03-13 07:05:43 +0000132}
133
134
Chris Lattnereeb4a842009-07-02 23:08:13 +0000135/// GetMessage - Return an SMDiagnostic at the specified location with the
136/// specified string.
137///
138/// @param Type - If non-null, the kind of message (e.g., "error") which is
139/// prefixed to the message.
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000140SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const Twine &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000141 const char *Type, bool ShowLine) const {
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000142
Chris Lattneraa739d22009-03-13 07:05:43 +0000143 // First thing to do: find the current buffer containing the specified
144 // location.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000145 int CurBuf = FindBufferContainingLoc(Loc);
Chris Lattneraa739d22009-03-13 07:05:43 +0000146 assert(CurBuf != -1 && "Invalid or unspecified location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000147
Chris Lattneraa739d22009-03-13 07:05:43 +0000148 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
Daniel Dunbar41539822009-11-22 22:08:00 +0000149
Chris Lattneraa739d22009-03-13 07:05:43 +0000150 // Scan backward to find the start of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000151 const char *LineStart = Loc.getPointer();
Daniel Dunbar41539822009-11-22 22:08:00 +0000152 while (LineStart != CurMB->getBufferStart() &&
Chris Lattneraa739d22009-03-13 07:05:43 +0000153 LineStart[-1] != '\n' && LineStart[-1] != '\r')
154 --LineStart;
Daniel Dunbar41539822009-11-22 22:08:00 +0000155
156 std::string LineStr;
157 if (ShowLine) {
158 // Get the end of the line.
159 const char *LineEnd = Loc.getPointer();
160 while (LineEnd != CurMB->getBufferEnd() &&
161 LineEnd[0] != '\n' && LineEnd[0] != '\r')
162 ++LineEnd;
163 LineStr = std::string(LineStart, LineEnd);
164 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000165
Chris Lattner2f510ae2009-07-02 22:24:20 +0000166 std::string PrintedMsg;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000167 raw_string_ostream OS(PrintedMsg);
168 if (Type)
169 OS << Type << ": ";
170 OS << Msg;
Daniel Dunbar41539822009-11-22 22:08:00 +0000171
Chris Lattnerb0194912010-04-06 18:06:18 +0000172 return SMDiagnostic(*this, Loc,
Chris Lattner8f0f4802010-04-06 00:26:48 +0000173 CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000174 Loc.getPointer()-LineStart, OS.str(),
Daniel Dunbar41539822009-11-22 22:08:00 +0000175 LineStr, ShowLine);
Chris Lattnereeb4a842009-07-02 23:08:13 +0000176}
177
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000178void SourceMgr::PrintMessage(SMLoc Loc, const Twine &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000179 const char *Type, bool ShowLine) const {
Chris Lattner8f0f4802010-04-06 00:26:48 +0000180 // Report the message with the diagnostic handler if present.
181 if (DiagHandler) {
Chris Lattner4afa1282010-11-17 08:13:01 +0000182 DiagHandler(GetMessage(Loc, Msg, Type, ShowLine), DiagContext);
Chris Lattner8f0f4802010-04-06 00:26:48 +0000183 return;
184 }
185
Chris Lattnereeb4a842009-07-02 23:08:13 +0000186 raw_ostream &OS = errs();
187
188 int CurBuf = FindBufferContainingLoc(Loc);
189 assert(CurBuf != -1 && "Invalid or unspecified location!");
190 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
191
Daniel Dunbar41539822009-11-22 22:08:00 +0000192 GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +0000193}
Chris Lattner2f510ae2009-07-02 22:24:20 +0000194
195//===----------------------------------------------------------------------===//
196// SMDiagnostic Implementation
197//===----------------------------------------------------------------------===//
198
Mikhail Glushenkov42210662010-01-27 10:13:28 +0000199void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) const {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000200 if (ProgName && ProgName[0])
201 S << ProgName << ": ";
202
Dan Gohman5e5442c2010-01-21 10:13:27 +0000203 if (!Filename.empty()) {
204 if (Filename == "-")
205 S << "<stdin>";
206 else
207 S << Filename;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000208
Dan Gohman5e5442c2010-01-21 10:13:27 +0000209 if (LineNo != -1) {
210 S << ':' << LineNo;
211 if (ColumnNo != -1)
212 S << ':' << (ColumnNo+1);
213 }
214 S << ": ";
Chris Lattner2f510ae2009-07-02 22:24:20 +0000215 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000216
Dan Gohman5e5442c2010-01-21 10:13:27 +0000217 S << Message << '\n';
Daniel Dunbar41539822009-11-22 22:08:00 +0000218
219 if (LineNo != -1 && ColumnNo != -1 && ShowLine) {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000220 S << LineContents << '\n';
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000221
Chris Lattner2f510ae2009-07-02 22:24:20 +0000222 // Print out spaces/tabs before the caret.
223 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
224 S << (LineContents[i] == '\t' ? '\t' : ' ');
225 S << "^\n";
226 }
227}
228
229