blob: 7dd42f4df8f76d272c090ceb6686ca50a2ba97a4 [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
Chris Lattner099e1982009-06-21 03:36:54 +000016#include "llvm/Support/SourceMgr.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000017#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/raw_ostream.h"
19using namespace llvm;
20
Chris Lattner1d96ccc2009-08-11 17:49:14 +000021namespace {
22 struct LineNoCacheTy {
23 int LastQueryBufferID;
24 const char *LastQuery;
25 unsigned LineNoOfQuery;
26 };
27}
28
29static LineNoCacheTy *getCache(void *Ptr) {
30 return (LineNoCacheTy*)Ptr;
31}
32
33
Chris Lattner8070ea32009-06-21 03:41:50 +000034SourceMgr::~SourceMgr() {
Chris Lattner1d96ccc2009-08-11 17:49:14 +000035 // Delete the line # cache if allocated.
36 if (LineNoCacheTy *Cache = getCache(LineNoCache))
37 delete Cache;
38
Chris Lattneraa739d22009-03-13 07:05:43 +000039 while (!Buffers.empty()) {
40 delete Buffers.back().Buffer;
41 Buffers.pop_back();
42 }
43}
44
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000045/// AddIncludeFile - Search for a file with the specified name in the current
46/// directory or in one of the IncludeDirs. If no file is found, this returns
47/// ~0, otherwise it returns the buffer ID of the stacked file.
48unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
49 SMLoc IncludeLoc) {
50
51 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
52
53 // If the file didn't exist directly, see if it's in an include path.
54 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
55 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
56 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
57 }
58
59 if (NewBuf == 0) return ~0U;
60
61 return AddNewSourceBuffer(NewBuf, IncludeLoc);
62}
63
64
Chris Lattneraa739d22009-03-13 07:05:43 +000065/// FindBufferContainingLoc - Return the ID of the buffer containing the
66/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000067int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000068 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000069 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000070 // Use <= here so that a pointer to the null at the end of the buffer
71 // is included as part of the buffer.
72 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000073 return i;
74 return -1;
75}
76
77/// FindLineNumber - Find the line number for the specified location in the
78/// specified file. This is not a fast method.
Chris Lattner8070ea32009-06-21 03:41:50 +000079unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000080 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
81 assert(BufferID != -1 && "Invalid Location!");
82
83 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
84
85 // Count the number of \n's between the start of the file and the specified
86 // location.
87 unsigned LineNo = 1;
88
89 const char *Ptr = Buff->getBufferStart();
90
Chris Lattner1d96ccc2009-08-11 17:49:14 +000091 // 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;
99 }
100
101 // Scan for the location being queried, keeping track of the number of lines
102 // we see.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000103 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +0000104 if (*Ptr == '\n') ++LineNo;
Chris Lattner1d96ccc2009-08-11 17:49:14 +0000105
106
107 // Allocate the line number cache if it doesn't exist.
108 if (LineNoCache == 0)
109 LineNoCache = new LineNoCacheTy();
110
111 // Update the line # cache.
112 LineNoCacheTy &Cache = *getCache(LineNoCache);
113 Cache.LastQueryBufferID = BufferID;
114 Cache.LastQuery = Ptr;
115 Cache.LineNoOfQuery = LineNo;
Chris Lattneraa739d22009-03-13 07:05:43 +0000116 return LineNo;
117}
118
Chris Lattner2f510ae2009-07-02 22:24:20 +0000119void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000120 if (IncludeLoc == SMLoc()) return; // Top of stack.
Chris Lattneraa739d22009-03-13 07:05:43 +0000121
122 int CurBuf = FindBufferContainingLoc(IncludeLoc);
123 assert(CurBuf != -1 && "Invalid or unspecified location!");
124
Chris Lattner2f510ae2009-07-02 22:24:20 +0000125 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +0000126
Chris Lattner2f510ae2009-07-02 22:24:20 +0000127 OS << "Included from "
128 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
129 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattneraa739d22009-03-13 07:05:43 +0000130}
131
132
Chris Lattnereeb4a842009-07-02 23:08:13 +0000133/// GetMessage - Return an SMDiagnostic at the specified location with the
134/// specified string.
135///
136/// @param Type - If non-null, the kind of message (e.g., "error") which is
137/// prefixed to the message.
138SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const std::string &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000139 const char *Type, bool ShowLine) const {
Chris Lattneraa739d22009-03-13 07:05:43 +0000140
141 // First thing to do: find the current buffer containing the specified
142 // location.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000143 int CurBuf = FindBufferContainingLoc(Loc);
Chris Lattneraa739d22009-03-13 07:05:43 +0000144 assert(CurBuf != -1 && "Invalid or unspecified location!");
145
Chris Lattneraa739d22009-03-13 07:05:43 +0000146 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
Daniel Dunbar41539822009-11-22 22:08:00 +0000147
Chris Lattneraa739d22009-03-13 07:05:43 +0000148 // Scan backward to find the start of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000149 const char *LineStart = Loc.getPointer();
Daniel Dunbar41539822009-11-22 22:08:00 +0000150 while (LineStart != CurMB->getBufferStart() &&
Chris Lattneraa739d22009-03-13 07:05:43 +0000151 LineStart[-1] != '\n' && LineStart[-1] != '\r')
152 --LineStart;
Daniel Dunbar41539822009-11-22 22:08:00 +0000153
154 std::string LineStr;
155 if (ShowLine) {
156 // Get the end of the line.
157 const char *LineEnd = Loc.getPointer();
158 while (LineEnd != CurMB->getBufferEnd() &&
159 LineEnd[0] != '\n' && LineEnd[0] != '\r')
160 ++LineEnd;
161 LineStr = std::string(LineStart, LineEnd);
162 }
Chris Lattner2f510ae2009-07-02 22:24:20 +0000163
164 std::string PrintedMsg;
165 if (Type) {
166 PrintedMsg = Type;
167 PrintedMsg += ": ";
168 }
169 PrintedMsg += Msg;
Daniel Dunbar41539822009-11-22 22:08:00 +0000170
Chris Lattnereeb4a842009-07-02 23:08:13 +0000171 return SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
172 Loc.getPointer()-LineStart, PrintedMsg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000173 LineStr, ShowLine);
Chris Lattnereeb4a842009-07-02 23:08:13 +0000174}
175
176void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg,
Daniel Dunbar41539822009-11-22 22:08:00 +0000177 const char *Type, bool ShowLine) const {
Chris Lattnereeb4a842009-07-02 23:08:13 +0000178 raw_ostream &OS = errs();
179
180 int CurBuf = FindBufferContainingLoc(Loc);
181 assert(CurBuf != -1 && "Invalid or unspecified location!");
182 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
183
Daniel Dunbar41539822009-11-22 22:08:00 +0000184 GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +0000185}
Chris Lattner2f510ae2009-07-02 22:24:20 +0000186
187//===----------------------------------------------------------------------===//
188// SMDiagnostic Implementation
189//===----------------------------------------------------------------------===//
190
191void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) {
192 if (ProgName && ProgName[0])
193 S << ProgName << ": ";
194
195 if (Filename == "-")
196 S << "<stdin>";
197 else
198 S << Filename;
199
200 if (LineNo != -1) {
201 S << ':' << LineNo;
202 if (ColumnNo != -1)
203 S << ':' << (ColumnNo+1);
204 }
205
206 S << ": " << Message << '\n';
Daniel Dunbar41539822009-11-22 22:08:00 +0000207
208 if (LineNo != -1 && ColumnNo != -1 && ShowLine) {
Chris Lattner2f510ae2009-07-02 22:24:20 +0000209 S << LineContents << '\n';
210
211 // Print out spaces/tabs before the caret.
212 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
213 S << (LineContents[i] == '\t' ? '\t' : ' ');
214 S << "^\n";
215 }
216}
217
218