blob: ef099163c221002612dacd27cb4e0ea801fe30c2 [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,
52 SMLoc IncludeLoc) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +000053 OwningPtr<MemoryBuffer> NewBuf;
54 MemoryBuffer::getFile(Filename.c_str(), NewBuf);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000055
56 // If the file didn't exist directly, see if it's in an include path.
57 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
58 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
Michael J. Spencer3ff95632010-12-16 03:29:14 +000059 MemoryBuffer::getFile(IncFile.c_str(), NewBuf);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000060 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000061
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000062 if (NewBuf == 0) return ~0U;
63
Michael J. Spencer3ff95632010-12-16 03:29:14 +000064 return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000065}
66
67
Chris Lattneraa739d22009-03-13 07:05:43 +000068/// FindBufferContainingLoc - Return the ID of the buffer containing the
69/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000070int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000071 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000072 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000073 // Use <= here so that a pointer to the null at the end of the buffer
74 // is included as part of the buffer.
75 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000076 return i;
77 return -1;
78}
79
80/// FindLineNumber - Find the line number for the specified location in the
81/// specified file. This is not a fast method.
Chris Lattner8070ea32009-06-21 03:41:50 +000082unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000083 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
84 assert(BufferID != -1 && "Invalid Location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000085
Chris Lattneraa739d22009-03-13 07:05:43 +000086 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000087
Chris Lattneraa739d22009-03-13 07:05:43 +000088 // Count the number of \n's between the start of the file and the specified
89 // location.
90 unsigned LineNo = 1;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000091
Chris Lattneraa739d22009-03-13 07:05:43 +000092 const char *Ptr = Buff->getBufferStart();
93
Chris Lattner1d96ccc2009-08-11 17:49:14 +000094 // If we have a line number cache, and if the query is to a later point in the
95 // same file, start searching from the last query location. This optimizes
96 // for the case when multiple diagnostics come out of one file in order.
97 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000098 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner1d96ccc2009-08-11 17:49:14 +000099 Cache->LastQuery <= Loc.getPointer()) {
100 Ptr = Cache->LastQuery;
101 LineNo = Cache->LineNoOfQuery;
102 }
103
104 // Scan for the location being queried, keeping track of the number of lines
105 // we see.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000106 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +0000107 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000108
109
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000110 // Allocate the line number cache if it doesn't exist.
111 if (LineNoCache == 0)
112 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000113
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000114 // Update the line # cache.
115 LineNoCacheTy &Cache = *getCache(LineNoCache);
116 Cache.LastQueryBufferID = BufferID;
117 Cache.LastQuery = Ptr;
118 Cache.LineNoOfQuery = LineNo;
Chris Lattneraa739d22009-03-13 07:05:43 +0000119 return LineNo;
120}
121
Chris Lattner2f510ae2009-07-02 22:24:20 +0000122void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000123 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000124
Chris Lattneraa739d22009-03-13 07:05:43 +0000125 int CurBuf = FindBufferContainingLoc(IncludeLoc);
126 assert(CurBuf != -1 && "Invalid or unspecified location!");
127
Chris Lattner2f510ae2009-07-02 22:24:20 +0000128 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000129
Chris Lattner2f510ae2009-07-02 22:24:20 +0000130 OS << "Included from "
131 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
132 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattneraa739d22009-03-13 07:05:43 +0000133}
134
135
Chris Lattnereeb4a842009-07-02 23:08:13 +0000136/// GetMessage - Return an SMDiagnostic at the specified location with the
137/// specified string.
138///
139/// @param Type - If non-null, the kind of message (e.g., "error") which is
140/// prefixed to the message.
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000141SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const Twine &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000142 const char *Type, bool ShowLine) const {
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000143
Chris Lattneraa739d22009-03-13 07:05:43 +0000144 // First thing to do: find the current buffer containing the specified
145 // location.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000146 int CurBuf = FindBufferContainingLoc(Loc);
Chris Lattneraa739d22009-03-13 07:05:43 +0000147 assert(CurBuf != -1 && "Invalid or unspecified location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000148
Chris Lattneraa739d22009-03-13 07:05:43 +0000149 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
Daniel Dunbar41539822009-11-22 22:08:00 +0000150
Chris Lattneraa739d22009-03-13 07:05:43 +0000151 // Scan backward to find the start of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000152 const char *LineStart = Loc.getPointer();
Daniel Dunbar41539822009-11-22 22:08:00 +0000153 while (LineStart != CurMB->getBufferStart() &&
Chris Lattneraa739d22009-03-13 07:05:43 +0000154 LineStart[-1] != '\n' && LineStart[-1] != '\r')
155 --LineStart;
Daniel Dunbar41539822009-11-22 22:08:00 +0000156
157 std::string LineStr;
158 if (ShowLine) {
159 // Get the end of the line.
160 const char *LineEnd = Loc.getPointer();
161 while (LineEnd != CurMB->getBufferEnd() &&
162 LineEnd[0] != '\n' && LineEnd[0] != '\r')
163 ++LineEnd;
164 LineStr = std::string(LineStart, LineEnd);
165 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000166
Chris Lattner2f510ae2009-07-02 22:24:20 +0000167 std::string PrintedMsg;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000168 raw_string_ostream OS(PrintedMsg);
169 if (Type)
170 OS << Type << ": ";
171 OS << Msg;
Daniel Dunbar41539822009-11-22 22:08:00 +0000172
Chris Lattnerb0194912010-04-06 18:06:18 +0000173 return SMDiagnostic(*this, Loc,
Chris Lattner8f0f4802010-04-06 00:26:48 +0000174 CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000175 Loc.getPointer()-LineStart, OS.str(),
Daniel Dunbar41539822009-11-22 22:08:00 +0000176 LineStr, ShowLine);
Chris Lattnereeb4a842009-07-02 23:08:13 +0000177}
178
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000179void SourceMgr::PrintMessage(SMLoc Loc, const Twine &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000180 const char *Type, bool ShowLine) const {
Chris Lattner8f0f4802010-04-06 00:26:48 +0000181 // Report the message with the diagnostic handler if present.
182 if (DiagHandler) {
Chris Lattner4afa1282010-11-17 08:13:01 +0000183 DiagHandler(GetMessage(Loc, Msg, Type, ShowLine), DiagContext);
Chris Lattner8f0f4802010-04-06 00:26:48 +0000184 return;
185 }
Michael J. Spencer333ad3f2010-12-09 17:37:32 +0000186
Chris Lattnereeb4a842009-07-02 23:08:13 +0000187 raw_ostream &OS = errs();
188
189 int CurBuf = FindBufferContainingLoc(Loc);
190 assert(CurBuf != -1 && "Invalid or unspecified location!");
191 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
192
Daniel Dunbar41539822009-11-22 22:08:00 +0000193 GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +0000194}
Chris Lattner2f510ae2009-07-02 22:24:20 +0000195
196//===----------------------------------------------------------------------===//
197// SMDiagnostic Implementation
198//===----------------------------------------------------------------------===//
199
Mikhail Glushenkov42210662010-01-27 10:13:28 +0000200void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) const {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000201 if (ProgName && ProgName[0])
202 S << ProgName << ": ";
203
Dan Gohman5e5442c2010-01-21 10:13:27 +0000204 if (!Filename.empty()) {
205 if (Filename == "-")
206 S << "<stdin>";
207 else
208 S << Filename;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000209
Dan Gohman5e5442c2010-01-21 10:13:27 +0000210 if (LineNo != -1) {
211 S << ':' << LineNo;
212 if (ColumnNo != -1)
213 S << ':' << (ColumnNo+1);
214 }
215 S << ": ";
Chris Lattner2f510ae2009-07-02 22:24:20 +0000216 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000217
Dan Gohman5e5442c2010-01-21 10:13:27 +0000218 S << Message << '\n';
Daniel Dunbar41539822009-11-22 22:08:00 +0000219
220 if (LineNo != -1 && ColumnNo != -1 && ShowLine) {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000221 S << LineContents << '\n';
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000222
Chris Lattner2f510ae2009-07-02 22:24:20 +0000223 // Print out spaces/tabs before the caret.
224 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
225 S << (LineContents[i] == '\t' ? '\t' : ' ');
226 S << "^\n";
227 }
228}
229
230