blob: 0dc1331d26010c62e05390ad9a86ebedd666a5b2 [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"
20using namespace llvm;
21
Chris Lattner1d96ccc2009-08-11 17:49:14 +000022namespace {
23 struct LineNoCacheTy {
24 int LastQueryBufferID;
25 const char *LastQuery;
26 unsigned LineNoOfQuery;
27 };
28}
29
30static LineNoCacheTy *getCache(void *Ptr) {
31 return (LineNoCacheTy*)Ptr;
32}
33
34
Chris Lattner8070ea32009-06-21 03:41:50 +000035SourceMgr::~SourceMgr() {
Chris Lattner1d96ccc2009-08-11 17:49:14 +000036 // Delete the line # cache if allocated.
37 if (LineNoCacheTy *Cache = getCache(LineNoCache))
38 delete Cache;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000039
Chris Lattneraa739d22009-03-13 07:05:43 +000040 while (!Buffers.empty()) {
41 delete Buffers.back().Buffer;
42 Buffers.pop_back();
43 }
44}
45
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000046/// AddIncludeFile - Search for a file with the specified name in the current
47/// directory or in one of the IncludeDirs. If no file is found, this returns
48/// ~0, otherwise it returns the buffer ID of the stacked file.
49unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
50 SMLoc IncludeLoc) {
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000051
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000052 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
53
54 // If the file didn't exist directly, see if it's in an include path.
55 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
56 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
57 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
58 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000059
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000060 if (NewBuf == 0) return ~0U;
61
62 return AddNewSourceBuffer(NewBuf, IncludeLoc);
63}
64
65
Chris Lattneraa739d22009-03-13 07:05:43 +000066/// FindBufferContainingLoc - Return the ID of the buffer containing the
67/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000068int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000069 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000070 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000071 // Use <= here so that a pointer to the null at the end of the buffer
72 // is included as part of the buffer.
73 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000074 return i;
75 return -1;
76}
77
78/// FindLineNumber - Find the line number for the specified location in the
79/// specified file. This is not a fast method.
Chris Lattner8070ea32009-06-21 03:41:50 +000080unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000081 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
82 assert(BufferID != -1 && "Invalid Location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000083
Chris Lattneraa739d22009-03-13 07:05:43 +000084 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000085
Chris Lattneraa739d22009-03-13 07:05:43 +000086 // Count the number of \n's between the start of the file and the specified
87 // location.
88 unsigned LineNo = 1;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000089
Chris Lattneraa739d22009-03-13 07:05:43 +000090 const char *Ptr = Buff->getBufferStart();
91
Chris Lattner1d96ccc2009-08-11 17:49:14 +000092 // If we have a line number cache, and if the query is to a later point in the
93 // same file, start searching from the last query location. This optimizes
94 // for the case when multiple diagnostics come out of one file in order.
95 if (LineNoCacheTy *Cache = getCache(LineNoCache))
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +000096 if (Cache->LastQueryBufferID == BufferID &&
Chris Lattner1d96ccc2009-08-11 17:49:14 +000097 Cache->LastQuery <= Loc.getPointer()) {
98 Ptr = Cache->LastQuery;
99 LineNo = Cache->LineNoOfQuery;
100 }
101
102 // Scan for the location being queried, keeping track of the number of lines
103 // we see.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000104 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +0000105 if (*Ptr == '\n') ++LineNo;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000106
107
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000108 // Allocate the line number cache if it doesn't exist.
109 if (LineNoCache == 0)
110 LineNoCache = new LineNoCacheTy();
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000111
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000112 // Update the line # cache.
113 LineNoCacheTy &Cache = *getCache(LineNoCache);
114 Cache.LastQueryBufferID = BufferID;
115 Cache.LastQuery = Ptr;
116 Cache.LineNoOfQuery = LineNo;
Chris Lattneraa739d22009-03-13 07:05:43 +0000117 return LineNo;
118}
119
Chris Lattner2f510ae2009-07-02 22:24:20 +0000120void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000121 if (IncludeLoc == SMLoc()) return; // Top of stack.
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000122
Chris Lattneraa739d22009-03-13 07:05:43 +0000123 int CurBuf = FindBufferContainingLoc(IncludeLoc);
124 assert(CurBuf != -1 && "Invalid or unspecified location!");
125
Chris Lattner2f510ae2009-07-02 22:24:20 +0000126 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000127
Chris Lattner2f510ae2009-07-02 22:24:20 +0000128 OS << "Included from "
129 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
130 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattneraa739d22009-03-13 07:05:43 +0000131}
132
133
Chris Lattnereeb4a842009-07-02 23:08:13 +0000134/// GetMessage - Return an SMDiagnostic at the specified location with the
135/// specified string.
136///
137/// @param Type - If non-null, the kind of message (e.g., "error") which is
138/// prefixed to the message.
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000139SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const Twine &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000140 const char *Type, bool ShowLine) const {
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000141
Chris Lattneraa739d22009-03-13 07:05:43 +0000142 // First thing to do: find the current buffer containing the specified
143 // location.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000144 int CurBuf = FindBufferContainingLoc(Loc);
Chris Lattneraa739d22009-03-13 07:05:43 +0000145 assert(CurBuf != -1 && "Invalid or unspecified location!");
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000146
Chris Lattneraa739d22009-03-13 07:05:43 +0000147 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
Daniel Dunbar41539822009-11-22 22:08:00 +0000148
Chris Lattneraa739d22009-03-13 07:05:43 +0000149 // Scan backward to find the start of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000150 const char *LineStart = Loc.getPointer();
Daniel Dunbar41539822009-11-22 22:08:00 +0000151 while (LineStart != CurMB->getBufferStart() &&
Chris Lattneraa739d22009-03-13 07:05:43 +0000152 LineStart[-1] != '\n' && LineStart[-1] != '\r')
153 --LineStart;
Daniel Dunbar41539822009-11-22 22:08:00 +0000154
155 std::string LineStr;
156 if (ShowLine) {
157 // Get the end of the line.
158 const char *LineEnd = Loc.getPointer();
159 while (LineEnd != CurMB->getBufferEnd() &&
160 LineEnd[0] != '\n' && LineEnd[0] != '\r')
161 ++LineEnd;
162 LineStr = std::string(LineStart, LineEnd);
163 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000164
Chris Lattner2f510ae2009-07-02 22:24:20 +0000165 std::string PrintedMsg;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000166 raw_string_ostream OS(PrintedMsg);
167 if (Type)
168 OS << Type << ": ";
169 OS << Msg;
Daniel Dunbar41539822009-11-22 22:08:00 +0000170
Chris Lattnerb0194912010-04-06 18:06:18 +0000171 return SMDiagnostic(*this, Loc,
Chris Lattner8f0f4802010-04-06 00:26:48 +0000172 CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000173 Loc.getPointer()-LineStart, OS.str(),
Daniel Dunbar41539822009-11-22 22:08:00 +0000174 LineStr, ShowLine);
Chris Lattnereeb4a842009-07-02 23:08:13 +0000175}
176
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000177void SourceMgr::PrintMessage(SMLoc Loc, const Twine &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000178 const char *Type, bool ShowLine) const {
Chris Lattner8f0f4802010-04-06 00:26:48 +0000179 // Report the message with the diagnostic handler if present.
180 if (DiagHandler) {
Chris Lattner4afa1282010-11-17 08:13:01 +0000181 DiagHandler(GetMessage(Loc, Msg, Type, ShowLine), DiagContext);
Chris Lattner8f0f4802010-04-06 00:26:48 +0000182 return;
183 }
184
Chris Lattnereeb4a842009-07-02 23:08:13 +0000185 raw_ostream &OS = errs();
186
187 int CurBuf = FindBufferContainingLoc(Loc);
188 assert(CurBuf != -1 && "Invalid or unspecified location!");
189 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
190
Daniel Dunbar41539822009-11-22 22:08:00 +0000191 GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +0000192}
Chris Lattner2f510ae2009-07-02 22:24:20 +0000193
194//===----------------------------------------------------------------------===//
195// SMDiagnostic Implementation
196//===----------------------------------------------------------------------===//
197
Mikhail Glushenkov42210662010-01-27 10:13:28 +0000198void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) const {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000199 if (ProgName && ProgName[0])
200 S << ProgName << ": ";
201
Dan Gohman5e5442c2010-01-21 10:13:27 +0000202 if (!Filename.empty()) {
203 if (Filename == "-")
204 S << "<stdin>";
205 else
206 S << Filename;
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000207
Dan Gohman5e5442c2010-01-21 10:13:27 +0000208 if (LineNo != -1) {
209 S << ':' << LineNo;
210 if (ColumnNo != -1)
211 S << ':' << (ColumnNo+1);
212 }
213 S << ": ";
Chris Lattner2f510ae2009-07-02 22:24:20 +0000214 }
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000215
Dan Gohman5e5442c2010-01-21 10:13:27 +0000216 S << Message << '\n';
Daniel Dunbar41539822009-11-22 22:08:00 +0000217
218 if (LineNo != -1 && ColumnNo != -1 && ShowLine) {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000219 S << LineContents << '\n';
Mikhail Glushenkove690ffb2010-01-27 10:13:11 +0000220
Chris Lattner2f510ae2009-07-02 22:24:20 +0000221 // Print out spaces/tabs before the caret.
222 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
223 S << (LineContents[i] == '\t' ? '\t' : ' ');
224 S << "^\n";
225 }
226}
227
228