blob: b6c4ffab4db01bf575c9cfdd53a0f5a3a15368cf [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//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Ted Kremenek78d85f52007-10-30 21:08:08 +000019#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
Ted Kremenek665dd4a2007-12-05 22:21:13 +000021#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Ted Kremenek5b034ad2009-01-06 22:43:04 +000027// This (temporary) directive toggles between lazy and eager creation of
28// MemBuffers. This directive is not permanent, and is here to test a few
29// potential optimizations in PTH. Once it is clear whether eager or lazy
30// creation of MemBuffers is better this directive will get removed.
31#define LAZY
32
Ted Kremenek78d85f52007-10-30 21:08:08 +000033ContentCache::~ContentCache() {
34 delete Buffer;
35 delete [] SourceLineCache;
Reid Spencer5f016e22007-07-11 17:01:13 +000036}
37
Ted Kremenekc16c2082009-01-06 01:55:26 +000038/// getSizeBytesMapped - Returns the number of bytes actually mapped for
39/// this ContentCache. This can be 0 if the MemBuffer was not actually
40/// instantiated.
41unsigned ContentCache::getSizeBytesMapped() const {
42 return Buffer ? Buffer->getBufferSize() : 0;
43}
44
45/// getSize - Returns the size of the content encapsulated by this ContentCache.
46/// This can be the size of the source file or the size of an arbitrary
47/// scratch buffer. If the ContentCache encapsulates a source file, that
48/// file is not lazily brought in from disk to satisfy this query.
49unsigned ContentCache::getSize() const {
50 return Entry ? Entry->getSize() : Buffer->getBufferSize();
51}
52
Ted Kremenek5b034ad2009-01-06 22:43:04 +000053const llvm::MemoryBuffer* ContentCache::getBuffer() const {
54#ifdef LAZY
55 // Lazily create the Buffer for ContentCaches that wrap files.
56 if (!Buffer && Entry) {
57 // FIXME: Should we support a way to not have to do this check over
58 // and over if we cannot open the file?
Chris Lattner05816592009-01-17 03:54:16 +000059 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek5b034ad2009-01-06 22:43:04 +000060 }
61#endif
Ted Kremenekc16c2082009-01-06 01:55:26 +000062 return Buffer;
63}
64
65
Reid Spencer5f016e22007-07-11 17:01:13 +000066/// getFileInfo - Create or return a cached FileInfo for the specified file.
67///
Ted Kremenek78d85f52007-10-30 21:08:08 +000068const ContentCache* SourceManager::getContentCache(const FileEntry *FileEnt) {
69
Reid Spencer5f016e22007-07-11 17:01:13 +000070 assert(FileEnt && "Didn't specify a file entry to use?");
71 // Do we already have information about this file?
Ted Kremenek78d85f52007-10-30 21:08:08 +000072 std::set<ContentCache>::iterator I =
73 FileInfos.lower_bound(ContentCache(FileEnt));
74
75 if (I != FileInfos.end() && I->Entry == FileEnt)
Reid Spencer5f016e22007-07-11 17:01:13 +000076 return &*I;
77
78 // Nope, get information.
Ted Kremenek5b034ad2009-01-06 22:43:04 +000079#ifndef LAZY
Chris Lattner3c1f7b62008-04-01 06:06:37 +000080 const MemoryBuffer *File =
Chris Lattner35de5122008-04-01 18:04:30 +000081 MemoryBuffer::getFile(FileEnt->getName(), 0, FileEnt->getSize());
Reid Spencer5f016e22007-07-11 17:01:13 +000082 if (File == 0)
83 return 0;
Ted Kremenek5b034ad2009-01-06 22:43:04 +000084#endif
85
Ted Kremenek78d85f52007-10-30 21:08:08 +000086 ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
Ted Kremenek5b034ad2009-01-06 22:43:04 +000087#ifndef LAZY
Ted Kremenekc16c2082009-01-06 01:55:26 +000088 Entry.setBuffer(File);
Ted Kremenek5b034ad2009-01-06 22:43:04 +000089#endif
Ted Kremenek78d85f52007-10-30 21:08:08 +000090 Entry.SourceLineCache = 0;
91 Entry.NumLines = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000092 return &Entry;
93}
94
95
Ted Kremenekd1c0eee2007-10-31 17:53:38 +000096/// createMemBufferContentCache - Create a new ContentCache for the specified
97/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +000098const ContentCache*
99SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Ted Kremenek0d892d82007-10-30 22:57:35 +0000100 // Add a new ContentCache to the MemBufferInfos list and return it. We
101 // must default construct the object first that the instance actually
102 // stored within MemBufferInfos actually owns the Buffer, and not any
103 // temporary we would use in the call to "push_back".
Ted Kremenek78d85f52007-10-30 21:08:08 +0000104 MemBufferInfos.push_back(ContentCache());
105 ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
Ted Kremenekc16c2082009-01-06 01:55:26 +0000106 Entry.setBuffer(Buffer);
Ted Kremenek78d85f52007-10-30 21:08:08 +0000107 return &Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
110
Nico Weber48002c82008-09-29 00:25:48 +0000111/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000112/// include position. This works regardless of whether the ContentCache
113/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000114FileID SourceManager::createFileID(const ContentCache *File,
Nico Weber7bfaaae2008-08-10 19:59:06 +0000115 SourceLocation IncludePos,
Chris Lattner9d728512008-10-27 01:19:25 +0000116 SrcMgr::CharacteristicKind FileCharacter) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // If FileEnt is really large (e.g. it's a large .i file), we may not be able
118 // to fit an arbitrary position in the file in the FilePos field. To handle
119 // this, we create one FileID for each chunk of the file that fits in a
120 // FilePos field.
Ted Kremenekc16c2082009-01-06 01:55:26 +0000121 unsigned FileSize = File->getSize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
Chris Lattner0b9e7362008-09-26 21:18:42 +0000123 FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File, FileCharacter));
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
125 "Ran out of file ID's!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000126 return FileID::Create(FileIDs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
128
129 // Create one FileID for each chunk of the file.
130 unsigned Result = FileIDs.size()+1;
131
132 unsigned ChunkNo = 0;
133 while (1) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000134 FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File,
Chris Lattner0b9e7362008-09-26 21:18:42 +0000135 FileCharacter));
Reid Spencer5f016e22007-07-11 17:01:13 +0000136
137 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
138 FileSize -= (1 << SourceLocation::FilePosBits);
139 }
140
141 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
142 "Ran out of file ID's!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000143 return FileID::Create(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
146/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000147/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000148/// InstantiationLoc.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000149SourceLocation SourceManager::getInstantiationLoc(SourceLocation SpellingLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 SourceLocation InstantLoc) {
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000151 // The specified source location may be a mapped location, due to a macro
152 // instantiation or #line directive. Strip off this information to find out
153 // where the characters are actually located.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000154 SpellingLoc = getSpellingLoc(SpellingLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000156 // Resolve InstantLoc down to a real instantiation location.
157 InstantLoc = getInstantiationLoc(InstantLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158
Chris Lattner31bb8be2007-07-20 18:00:12 +0000159
160 // If the last macro id is close to the currently requested location, try to
Chris Lattner991ae512007-08-02 03:55:37 +0000161 // reuse it. This implements a small cache.
162 for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){
163 MacroIDInfo &LastOne = MacroIDs[i];
Chris Lattnerd1623a82007-07-21 06:41:57 +0000164
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000165 // The instanitation point and source SpellingLoc have to exactly match to
166 // reuse (for now). We could allow "nearby" instantiations in the future.
Chris Lattner88054de2009-01-16 07:15:35 +0000167 if (LastOne.getInstantiationLoc() != InstantLoc ||
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000168 LastOne.getSpellingLoc().getFileID() != SpellingLoc.getFileID())
Chris Lattner991ae512007-08-02 03:55:37 +0000169 continue;
170
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000171 // Check to see if the spellloc of the token came from near enough to reuse.
172 int SpellDelta = SpellingLoc.getRawFilePos() -
173 LastOne.getSpellingLoc().getRawFilePos();
174 if (SourceLocation::isValidMacroSpellingOffs(SpellDelta))
175 return SourceLocation::getMacroLoc(i, SpellDelta);
Chris Lattner31bb8be2007-07-20 18:00:12 +0000176 }
177
Chris Lattner45011cf2007-07-20 18:26:45 +0000178
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000179 MacroIDs.push_back(MacroIDInfo::get(InstantLoc, SpellingLoc));
Chris Lattnerf8484542008-02-03 08:24:13 +0000180 return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Chris Lattner8a12c272007-10-11 18:38:32 +0000183/// getBufferData - Return a pointer to the start and end of the character
Chris Lattner2b2453a2009-01-17 06:22:33 +0000184/// data for the specified location.
Chris Lattner8a12c272007-10-11 18:38:32 +0000185std::pair<const char*, const char*>
Chris Lattner2b2453a2009-01-17 06:22:33 +0000186SourceManager::getBufferData(SourceLocation Loc) const {
187 const llvm::MemoryBuffer *Buf = getBuffer(Loc);
Chris Lattner8a12c272007-10-11 18:38:32 +0000188 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
189}
Reid Spencer5f016e22007-07-11 17:01:13 +0000190
Chris Lattner2b2453a2009-01-17 06:22:33 +0000191std::pair<const char*, const char*>
192SourceManager::getBufferData(FileID FID) const {
193 const llvm::MemoryBuffer *Buf = getBuffer(FID);
194 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
195}
196
197
Reid Spencer5f016e22007-07-11 17:01:13 +0000198
199/// getCharacterData - Return a pointer to the start of the specified location
200/// in the appropriate MemoryBuffer.
201const char *SourceManager::getCharacterData(SourceLocation SL) const {
202 // Note that this is a hot function in the getSpelling() path, which is
203 // heavily used by -E mode.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000204 SL = getSpellingLoc(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205
Chris Lattner2b2453a2009-01-17 06:22:33 +0000206 std::pair<FileID, unsigned> LocInfo = getDecomposedFileLoc(SL);
207
Ted Kremenekc16c2082009-01-06 01:55:26 +0000208 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000209 return getContentCache(LocInfo.first)->getBuffer()->getBufferStart() +
210 LocInfo.second;
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 {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000218 if (Loc.getFileID() == 0) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
Chris Lattner2b2453a2009-01-17 06:22:33 +0000220 std::pair<FileID, unsigned> LocInfo = getDecomposedFileLoc(Loc);
221 unsigned FilePos = LocInfo.second;
222
223 const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
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 Lattner8b6ca882007-08-30 05:59:30 +0000234const char *SourceManager::getSourceName(SourceLocation Loc) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000235 if (Loc.getFileID() == 0) return "";
Ted Kremenekc16c2082009-01-06 01:55:26 +0000236
237 // To get the source name, first consult the FileEntry (if one exists) before
238 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000239 const SrcMgr::ContentCache *C = getContentCacheForLoc(Loc);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000240 return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Reid Spencer5f016e22007-07-11 17:01:13 +0000241}
242
Ted Kremenek78d85f52007-10-30 21:08:08 +0000243static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
Ted Kremenekc16c2082009-01-06 01:55:26 +0000244static void ComputeLineNumbers(ContentCache* FI) {
245 // Note that calling 'getBuffer()' may lazily page in the file.
246 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000247
248 // Find the file offsets of all of the *physical* source lines. This does
249 // not look at trigraphs, escaped newlines, or anything else tricky.
250 std::vector<unsigned> LineOffsets;
251
252 // Line #1 starts at char 0.
253 LineOffsets.push_back(0);
254
255 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
256 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
257 unsigned Offs = 0;
258 while (1) {
259 // Skip over the contents of the line.
260 // TODO: Vectorize this? This is very performance sensitive for programs
261 // with lots of diagnostics and in -E mode.
262 const unsigned char *NextBuf = (const unsigned char *)Buf;
263 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
264 ++NextBuf;
265 Offs += NextBuf-Buf;
266 Buf = NextBuf;
267
268 if (Buf[0] == '\n' || Buf[0] == '\r') {
269 // If this is \n\r or \r\n, skip both characters.
270 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
271 ++Offs, ++Buf;
272 ++Offs, ++Buf;
273 LineOffsets.push_back(Offs);
274 } else {
275 // Otherwise, this is a null. If end of file, exit.
276 if (Buf == End) break;
277 // Otherwise, skip the null.
278 ++Offs, ++Buf;
279 }
280 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000281
282 // Copy the offsets into the FileInfo structure.
283 FI->NumLines = LineOffsets.size();
284 FI->SourceLineCache = new unsigned[LineOffsets.size()];
285 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
286}
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000288/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000289/// for the position indicated. This requires building and caching a table of
290/// line offsets for the MemoryBuffer, so this is not cheap: use only when
291/// about to emit a diagnostic.
Chris Lattnerf812a452008-11-18 06:51:15 +0000292unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000293 if (Loc.getFileID() == 0) return 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000294
Chris Lattner2b2453a2009-01-17 06:22:33 +0000295 ContentCache *Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000296
Chris Lattner2b2453a2009-01-17 06:22:33 +0000297 std::pair<FileID, unsigned> LocInfo = getDecomposedFileLoc(Loc);
298
299 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000300 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000301 else
Chris Lattner2b2453a2009-01-17 06:22:33 +0000302 Content = const_cast<ContentCache*>(getContentCache(LocInfo.first));
Reid Spencer5f016e22007-07-11 17:01:13 +0000303
304 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000305 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000306 if (Content->SourceLineCache == 0)
307 ComputeLineNumbers(Content);
Reid Spencer5f016e22007-07-11 17:01:13 +0000308
309 // Okay, we know we have a line number table. Do a binary search to find the
310 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000311 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000312 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000313 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000314
Chris Lattner2b2453a2009-01-17 06:22:33 +0000315 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000316
317 // If the previous query was to the same file, we know both the file pos from
318 // that query and the line number returned. This allows us to narrow the
319 // search space from the entire file to something near the match.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000320 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000321 if (QueriedFilePos >= LastLineNoFilePos) {
322 SourceLineCache = SourceLineCache+LastLineNoResult-1;
323
324 // The query is likely to be nearby the previous one. Here we check to
325 // see if it is within 5, 10 or 20 lines. It can be far away in cases
326 // where big comment blocks and vertical whitespace eat up lines but
327 // contribute no tokens.
328 if (SourceLineCache+5 < SourceLineCacheEnd) {
329 if (SourceLineCache[5] > QueriedFilePos)
330 SourceLineCacheEnd = SourceLineCache+5;
331 else if (SourceLineCache+10 < SourceLineCacheEnd) {
332 if (SourceLineCache[10] > QueriedFilePos)
333 SourceLineCacheEnd = SourceLineCache+10;
334 else if (SourceLineCache+20 < SourceLineCacheEnd) {
335 if (SourceLineCache[20] > QueriedFilePos)
336 SourceLineCacheEnd = SourceLineCache+20;
337 }
338 }
339 }
340 } else {
341 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
342 }
343 }
344
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000345 // If the spread is large, do a "radix" test as our initial guess, based on
346 // the assumption that lines average to approximately the same length.
347 // NOTE: This is currently disabled, as it does not appear to be profitable in
348 // initial measurements.
349 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000350 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000351
352 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000353 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000354
355 // Check for -10 and +10 lines.
356 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
357 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
358
359 // If the computed lower bound is less than the query location, move it in.
360 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
361 SourceLineCacheStart[LowerBound] < QueriedFilePos)
362 SourceLineCache = SourceLineCacheStart+LowerBound;
363
364 // If the computed upper bound is greater than the query location, move it.
365 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
366 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
367 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
368 }
369
370 unsigned *Pos
371 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000372 unsigned LineNo = Pos-SourceLineCacheStart;
373
Chris Lattner2b2453a2009-01-17 06:22:33 +0000374 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000375 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000376 LastLineNoFilePos = QueriedFilePos;
377 LastLineNoResult = LineNo;
378 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000379}
380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381/// PrintStats - Print statistics to stderr.
382///
383void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000384 llvm::cerr << "\n*** Source Manager Stats:\n";
385 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
386 << " mem buffers mapped, " << FileIDs.size()
387 << " file ID's allocated.\n";
388 llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
389 << MacroIDs.size() << " macro expansion FileID's.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 unsigned NumLineNumsComputed = 0;
392 unsigned NumFileBytesMapped = 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000393 for (std::set<ContentCache>::const_iterator I =
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000395 NumLineNumsComputed += I->SourceLineCache != 0;
Ted Kremenekc16c2082009-01-06 01:55:26 +0000396 NumFileBytesMapped += I->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000398
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000399 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
400 << NumLineNumsComputed << " files with line #'s computed.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000401}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000402
403//===----------------------------------------------------------------------===//
404// Serialization.
405//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000406
407void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000408 S.FlushRecord();
409 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000410
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000411 if (Entry) {
412 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
413
414 if (Fname.isAbsolute())
415 S.EmitCStr(Fname.c_str());
416 else {
417 // Create an absolute path.
418 // FIXME: This will potentially contain ".." and "." in the path.
419 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
420 path.appendComponent(Fname.c_str());
421 S.EmitCStr(path.c_str());
422 }
423 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000424 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000425 const char* p = Buffer->getBufferStart();
426 const char* e = Buffer->getBufferEnd();
427
Ted Kremenek099b4742007-12-05 00:14:18 +0000428 S.EmitInt(e-p);
429
Ted Kremeneke21272f2007-12-04 19:39:02 +0000430 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000431 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000432 }
433
Ted Kremenek099b4742007-12-05 00:14:18 +0000434 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000435}
Ted Kremenek099b4742007-12-05 00:14:18 +0000436
437void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
438 SourceManager& SMgr,
439 FileManager* FMgr,
440 std::vector<char>& Buf) {
441 if (FMgr) {
442 llvm::SerializedPtrID PtrID = D.ReadPtrID();
443 D.ReadCStr(Buf,false);
444
445 // Create/fetch the FileEntry.
446 const char* start = &Buf[0];
447 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
448
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000449 // FIXME: Ideally we want a lazy materialization of the ContentCache
450 // anyway, because we don't want to read in source files unless this
451 // is absolutely needed.
452 if (!E)
453 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000454 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000455 // Get the ContextCache object and register it with the deserializer.
456 D.RegisterPtr(PtrID,SMgr.getContentCache(E));
Ted Kremenek099b4742007-12-05 00:14:18 +0000457 }
458 else {
459 // Register the ContextCache object with the deserializer.
460 SMgr.MemBufferInfos.push_back(ContentCache());
Nico Weber48002c82008-09-29 00:25:48 +0000461 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Ted Kremenek099b4742007-12-05 00:14:18 +0000462 D.RegisterPtr(&Entry);
463
464 // Create the buffer.
465 unsigned Size = D.ReadInt();
466 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
467
468 // Read the contents of the buffer.
469 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
470 for (unsigned i = 0; i < Size ; ++i)
471 p[i] = D.ReadInt();
472 }
473}
474
475void FileIDInfo::Emit(llvm::Serializer& S) const {
476 S.Emit(IncludeLoc);
477 S.EmitInt(ChunkNo);
478 S.EmitPtr(Content);
479}
480
481FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) {
482 FileIDInfo I;
483 I.IncludeLoc = SourceLocation::ReadVal(D);
484 I.ChunkNo = D.ReadInt();
485 D.ReadPtr(I.Content,false);
486 return I;
487}
488
489void MacroIDInfo::Emit(llvm::Serializer& S) const {
Chris Lattner88054de2009-01-16 07:15:35 +0000490 S.Emit(InstantiationLoc);
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000491 S.Emit(SpellingLoc);
Ted Kremenek099b4742007-12-05 00:14:18 +0000492}
493
494MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) {
495 MacroIDInfo I;
Chris Lattner88054de2009-01-16 07:15:35 +0000496 I.InstantiationLoc = SourceLocation::ReadVal(D);
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000497 I.SpellingLoc = SourceLocation::ReadVal(D);
Ted Kremenek099b4742007-12-05 00:14:18 +0000498 return I;
499}
500
501void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000502 S.EnterBlock();
503 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000504 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000505
Ted Kremenek099b4742007-12-05 00:14:18 +0000506 // Emit: FileInfos. Just emit the file name.
507 S.EnterBlock();
508
509 std::for_each(FileInfos.begin(),FileInfos.end(),
510 S.MakeEmitter<ContentCache>());
511
512 S.ExitBlock();
513
514 // Emit: MemBufferInfos
515 S.EnterBlock();
516
517 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
518 S.MakeEmitter<ContentCache>());
519
520 S.ExitBlock();
521
Nico Weber48002c82008-09-29 00:25:48 +0000522 // Emit: FileIDs
Ted Kremenek099b4742007-12-05 00:14:18 +0000523 S.EmitInt(FileIDs.size());
524 std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>());
525
526 // Emit: MacroIDs
527 S.EmitInt(MacroIDs.size());
528 std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>());
Ted Kremenek1f941002007-12-05 00:19:51 +0000529
530 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000531}
532
Ted Kremenek1f941002007-12-05 00:19:51 +0000533SourceManager*
534SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
535 SourceManager *M = new SourceManager();
536 D.RegisterPtr(M);
537
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000538 // Read: the FileID of the main source file of the translation unit.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000539 M->MainFileID = FileID::Create(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000540
Ted Kremenek099b4742007-12-05 00:14:18 +0000541 std::vector<char> Buf;
542
543 { // Read: FileInfos.
544 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
545 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000546 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000547 }
548
549 { // Read: MemBufferInfos.
550 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
551 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000552 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000553 }
554
555 // Read: FileIDs.
556 unsigned Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000557 M->FileIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000558 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000559 M->FileIDs.push_back(FileIDInfo::ReadVal(D));
Ted Kremenek099b4742007-12-05 00:14:18 +0000560
561 // Read: MacroIDs.
562 Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000563 M->MacroIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000564 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000565 M->MacroIDs.push_back(MacroIDInfo::ReadVal(D));
566
567 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000568}