blob: df13fad37c3162ddfd4f605054f6737d3d9ddfa2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceManager.h"
15#include "clang/Basic/FileManager.h"
Chris Lattner5e36a7a2007-07-24 05:57:19 +000016#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/System/Path.h"
19#include <algorithm>
20#include <iostream>
Gabor Greif15012182007-07-12 16:00:00 +000021#include <fcntl.h>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23using namespace SrcMgr;
24using llvm::MemoryBuffer;
25
26SourceManager::~SourceManager() {
27 for (std::map<const FileEntry *, FileInfo>::iterator I = FileInfos.begin(),
28 E = FileInfos.end(); I != E; ++I) {
29 delete I->second.Buffer;
30 delete[] I->second.SourceLineCache;
31 }
32
33 for (std::list<InfoRec>::iterator I = MemBufferInfos.begin(),
34 E = MemBufferInfos.end(); I != E; ++I) {
35 delete I->second.Buffer;
36 delete[] I->second.SourceLineCache;
37 }
38}
39
40
41// FIXME: REMOVE THESE
42#include <unistd.h>
43#include <sys/types.h>
44#include <sys/uio.h>
45#include <sys/fcntl.h>
46#include <cerrno>
47
48static const MemoryBuffer *ReadFileFast(const FileEntry *FileEnt) {
49#if 0
50 // FIXME: Reintroduce this and zap this function once the common llvm stuff
51 // is fast for the small case.
52 return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()),
53 FileEnt->getSize());
54#endif
55
56 // If the file is larger than some threshold, use 'read', otherwise use mmap.
57 if (FileEnt->getSize() >= 4096*4)
58 return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()),
59 0, FileEnt->getSize());
60
61 MemoryBuffer *SB = MemoryBuffer::getNewUninitMemBuffer(FileEnt->getSize(),
62 FileEnt->getName());
63 char *BufPtr = const_cast<char*>(SB->getBufferStart());
64
65 int FD = ::open(FileEnt->getName(), O_RDONLY);
66 if (FD == -1) {
67 delete SB;
68 return 0;
69 }
70
71 unsigned BytesLeft = FileEnt->getSize();
72 while (BytesLeft) {
73 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
74 if (NumRead != -1) {
75 BytesLeft -= NumRead;
76 BufPtr += NumRead;
77 } else if (errno == EINTR) {
78 // try again
79 } else {
80 // error reading.
81 close(FD);
82 delete SB;
83 return 0;
84 }
85 }
86 close(FD);
87
88 return SB;
89}
90
91
92/// getFileInfo - Create or return a cached FileInfo for the specified file.
93///
94const InfoRec *
95SourceManager::getInfoRec(const FileEntry *FileEnt) {
96 assert(FileEnt && "Didn't specify a file entry to use?");
97 // Do we already have information about this file?
98 std::map<const FileEntry *, FileInfo>::iterator I =
99 FileInfos.lower_bound(FileEnt);
100 if (I != FileInfos.end() && I->first == FileEnt)
101 return &*I;
102
103 // Nope, get information.
104 const MemoryBuffer *File = ReadFileFast(FileEnt);
105 if (File == 0)
106 return 0;
107
108 const InfoRec &Entry =
109 *FileInfos.insert(I, std::make_pair(FileEnt, FileInfo()));
110 FileInfo &Info = const_cast<FileInfo &>(Entry.second);
111
112 Info.Buffer = File;
113 Info.SourceLineCache = 0;
114 Info.NumLines = 0;
115 return &Entry;
116}
117
118
119/// createMemBufferInfoRec - Create a new info record for the specified memory
120/// buffer. This does no caching.
121const InfoRec *
122SourceManager::createMemBufferInfoRec(const MemoryBuffer *Buffer) {
123 // Add a new info record to the MemBufferInfos list and return it.
124 FileInfo FI;
125 FI.Buffer = Buffer;
126 FI.SourceLineCache = 0;
127 FI.NumLines = 0;
128 MemBufferInfos.push_back(InfoRec(0, FI));
129 return &MemBufferInfos.back();
130}
131
132
133/// createFileID - Create a new fileID for the specified InfoRec and include
134/// position. This works regardless of whether the InfoRec corresponds to a
135/// file or some other input source.
136unsigned SourceManager::createFileID(const InfoRec *File,
137 SourceLocation IncludePos) {
138 // If FileEnt is really large (e.g. it's a large .i file), we may not be able
139 // to fit an arbitrary position in the file in the FilePos field. To handle
140 // this, we create one FileID for each chunk of the file that fits in a
141 // FilePos field.
142 unsigned FileSize = File->second.Buffer->getBufferSize();
143 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000144 FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File));
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
146 "Ran out of file ID's!");
147 return FileIDs.size();
148 }
149
150 // Create one FileID for each chunk of the file.
151 unsigned Result = FileIDs.size()+1;
152
153 unsigned ChunkNo = 0;
154 while (1) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000155 FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File));
Reid Spencer5f016e22007-07-11 17:01:13 +0000156
157 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
158 FileSize -= (1 << SourceLocation::FilePosBits);
159 }
160
161 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
162 "Ran out of file ID's!");
163 return Result;
164}
165
166/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
167/// that a token from physloc PhysLoc should actually be referenced from
168/// InstantiationLoc.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000169SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 SourceLocation InstantLoc) {
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000171 // The specified source location may be a mapped location, due to a macro
172 // instantiation or #line directive. Strip off this information to find out
173 // where the characters are actually located.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000174 PhysLoc = getPhysicalLoc(PhysLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000175
176 // Resolve InstantLoc down to a real logical location.
177 InstantLoc = getLogicalLoc(InstantLoc);
178
Chris Lattner31bb8be2007-07-20 18:00:12 +0000179
180 // If the last macro id is close to the currently requested location, try to
181 // reuse it. This implements a single-entry cache.
182 if (!MacroIDs.empty()) {
183 MacroIDInfo &LastOne = MacroIDs.back();
Chris Lattnerd1623a82007-07-21 06:41:57 +0000184
Chris Lattner31bb8be2007-07-20 18:00:12 +0000185 if (LastOne.getInstantiationLoc() == InstantLoc &&
186 LastOne.getPhysicalLoc().getFileID() == PhysLoc.getFileID()) {
187
188 int PhysDelta = PhysLoc.getRawFilePos() -
189 LastOne.getPhysicalLoc().getRawFilePos();
Chris Lattnerd1623a82007-07-21 06:41:57 +0000190 if (SourceLocation::isValidMacroPhysOffs(PhysDelta))
191 return SourceLocation::getMacroLoc(MacroIDs.size()-1, PhysDelta, 0);
Chris Lattner31bb8be2007-07-20 18:00:12 +0000192 }
193 }
194
Chris Lattner45011cf2007-07-20 18:26:45 +0000195
Chris Lattner9dc1f532007-07-20 16:37:10 +0000196 MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
Chris Lattner9dc1f532007-07-20 16:37:10 +0000197 return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
200
201
202/// getCharacterData - Return a pointer to the start of the specified location
203/// in the appropriate MemoryBuffer.
204const char *SourceManager::getCharacterData(SourceLocation SL) const {
205 // Note that this is a hot function in the getSpelling() path, which is
206 // heavily used by -E mode.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000207 SL = getPhysicalLoc(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
Chris Lattner9dc1f532007-07-20 16:37:10 +0000209 return getFileInfo(SL.getFileID())->Buffer->getBufferStart() +
210 getFullFilePos(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213
Chris Lattner9dc1f532007-07-20 16:37:10 +0000214/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000215/// this is significantly cheaper to compute than the line number. This returns
216/// zero if the column number isn't known.
217unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 unsigned FileID = Loc.getFileID();
219 if (FileID == 0) return 0;
220
Chris Lattner9dc1f532007-07-20 16:37:10 +0000221 unsigned FilePos = getFullFilePos(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 const MemoryBuffer *Buffer = getBuffer(FileID);
223 const char *Buf = Buffer->getBufferStart();
224
225 unsigned LineStart = FilePos;
226 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
227 --LineStart;
228 return FilePos-LineStart+1;
229}
230
231/// getSourceName - This method returns the name of the file or buffer that
232/// the SourceLocation specifies. This can be modified with #line directives,
233/// etc.
Chris Lattnerd8e30832007-07-24 06:57:14 +0000234const char *SourceManager::getSourceName(SourceLocation Loc) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 unsigned FileID = Loc.getFileID();
236 if (FileID == 0) return "";
237 return getFileInfo(FileID)->Buffer->getBufferIdentifier();
238}
239
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000240static void ComputeLineNumbers(FileInfo *FI) DISABLE_INLINE;
241static void ComputeLineNumbers(FileInfo *FI) {
242 const MemoryBuffer *Buffer = FI->Buffer;
243
244 // Find the file offsets of all of the *physical* source lines. This does
245 // not look at trigraphs, escaped newlines, or anything else tricky.
246 std::vector<unsigned> LineOffsets;
247
248 // Line #1 starts at char 0.
249 LineOffsets.push_back(0);
250
251 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
252 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
253 unsigned Offs = 0;
254 while (1) {
255 // Skip over the contents of the line.
256 // TODO: Vectorize this? This is very performance sensitive for programs
257 // with lots of diagnostics and in -E mode.
258 const unsigned char *NextBuf = (const unsigned char *)Buf;
259 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
260 ++NextBuf;
261 Offs += NextBuf-Buf;
262 Buf = NextBuf;
263
264 if (Buf[0] == '\n' || Buf[0] == '\r') {
265 // If this is \n\r or \r\n, skip both characters.
266 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
267 ++Offs, ++Buf;
268 ++Offs, ++Buf;
269 LineOffsets.push_back(Offs);
270 } else {
271 // Otherwise, this is a null. If end of file, exit.
272 if (Buf == End) break;
273 // Otherwise, skip the null.
274 ++Offs, ++Buf;
275 }
276 }
277 LineOffsets.push_back(Offs);
278
279 // Copy the offsets into the FileInfo structure.
280 FI->NumLines = LineOffsets.size();
281 FI->SourceLineCache = new unsigned[LineOffsets.size()];
282 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
283}
Reid Spencer5f016e22007-07-11 17:01:13 +0000284
285/// getLineNumber - Given a SourceLocation, return the physical line number
286/// for the position indicated. This requires building and caching a table of
287/// line offsets for the MemoryBuffer, so this is not cheap: use only when
288/// about to emit a diagnostic.
289unsigned SourceManager::getLineNumber(SourceLocation Loc) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 unsigned FileID = Loc.getFileID();
291 if (FileID == 0) return 0;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000292 FileInfo *FileInfo;
293
294 if (LastLineNoFileIDQuery == FileID)
295 FileInfo = LastLineNoFileInfo;
296 else
297 FileInfo = getFileInfo(FileID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298
299 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000300 /// SourceLineCache for it on demand.
301 if (FileInfo->SourceLineCache == 0)
302 ComputeLineNumbers(FileInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303
304 // Okay, we know we have a line number table. Do a binary search to find the
305 // line number that this character position lands on.
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 unsigned *SourceLineCache = FileInfo->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000307 unsigned *SourceLineCacheStart = SourceLineCache;
308 unsigned *SourceLineCacheEnd = SourceLineCache + FileInfo->NumLines;
309
310 unsigned QueriedFilePos = getFullFilePos(Loc)+1;
311
312 // If the previous query was to the same file, we know both the file pos from
313 // that query and the line number returned. This allows us to narrow the
314 // search space from the entire file to something near the match.
315 if (LastLineNoFileIDQuery == FileID) {
316 if (QueriedFilePos >= LastLineNoFilePos) {
317 SourceLineCache = SourceLineCache+LastLineNoResult-1;
318
319 // The query is likely to be nearby the previous one. Here we check to
320 // see if it is within 5, 10 or 20 lines. It can be far away in cases
321 // where big comment blocks and vertical whitespace eat up lines but
322 // contribute no tokens.
323 if (SourceLineCache+5 < SourceLineCacheEnd) {
324 if (SourceLineCache[5] > QueriedFilePos)
325 SourceLineCacheEnd = SourceLineCache+5;
326 else if (SourceLineCache+10 < SourceLineCacheEnd) {
327 if (SourceLineCache[10] > QueriedFilePos)
328 SourceLineCacheEnd = SourceLineCache+10;
329 else if (SourceLineCache+20 < SourceLineCacheEnd) {
330 if (SourceLineCache[20] > QueriedFilePos)
331 SourceLineCacheEnd = SourceLineCache+20;
332 }
333 }
334 }
335 } else {
336 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
337 }
338 }
339
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000340 // If the spread is large, do a "radix" test as our initial guess, based on
341 // the assumption that lines average to approximately the same length.
342 // NOTE: This is currently disabled, as it does not appear to be profitable in
343 // initial measurements.
344 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
345 unsigned FileLen = FileInfo->SourceLineCache[FileInfo->NumLines-1];
346
347 // Take a stab at guessing where it is.
348 unsigned ApproxPos = FileInfo->NumLines*QueriedFilePos / FileLen;
349
350 // Check for -10 and +10 lines.
351 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
352 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
353
354 // If the computed lower bound is less than the query location, move it in.
355 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
356 SourceLineCacheStart[LowerBound] < QueriedFilePos)
357 SourceLineCache = SourceLineCacheStart+LowerBound;
358
359 // If the computed upper bound is greater than the query location, move it.
360 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
361 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
362 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
363 }
364
365 unsigned *Pos
366 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000367 unsigned LineNo = Pos-SourceLineCacheStart;
368
369 LastLineNoFileIDQuery = FileID;
370 LastLineNoFileInfo = FileInfo;
371 LastLineNoFilePos = QueriedFilePos;
372 LastLineNoResult = LineNo;
373 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// PrintStats - Print statistics to stderr.
377///
378void SourceManager::PrintStats() const {
379 std::cerr << "\n*** Source Manager Stats:\n";
380 std::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
381 << " mem buffers mapped, " << FileIDs.size()
382 << " file ID's allocated.\n";
Chris Lattner9dc1f532007-07-20 16:37:10 +0000383 std::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
384 << MacroIDs.size() << " macro expansion FileID's.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000385
386
387
388 unsigned NumLineNumsComputed = 0;
389 unsigned NumFileBytesMapped = 0;
390 for (std::map<const FileEntry *, FileInfo>::const_iterator I =
391 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
392 NumLineNumsComputed += I->second.SourceLineCache != 0;
393 NumFileBytesMapped += I->second.Buffer->getBufferSize();
394 }
395 std::cerr << NumFileBytesMapped << " bytes of files mapped, "
396 << NumLineNumsComputed << " files with line #'s computed.\n";
397}