blob: 9b509a56845226efed47d59de8e888df94c39e1f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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"
16#include "llvm/Support/Compiler.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/System/Path.h"
Ted Kremenekdd364ea2007-10-30 21:08:08 +000019#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
Ted Kremenekda29d8c2007-12-05 22:21:13 +000021#include "llvm/Support/Streams.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Chris Lattner27c0ced2009-01-26 00:43:02 +000027//===--------------------------------------------------------------------===//
28// SourceManager Helper Classes
29//===--------------------------------------------------------------------===//
30
Ted Kremenekdd364ea2007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
33 delete [] SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +000034}
35
Ted Kremenekaa7dac12009-01-06 01:55:26 +000036/// getSizeBytesMapped - Returns the number of bytes actually mapped for
37/// this ContentCache. This can be 0 if the MemBuffer was not actually
38/// instantiated.
39unsigned ContentCache::getSizeBytesMapped() const {
40 return Buffer ? Buffer->getBufferSize() : 0;
41}
42
43/// getSize - Returns the size of the content encapsulated by this ContentCache.
44/// This can be the size of the source file or the size of an arbitrary
45/// scratch buffer. If the ContentCache encapsulates a source file, that
46/// file is not lazily brought in from disk to satisfy this query.
47unsigned ContentCache::getSize() const {
48 return Entry ? Entry->getSize() : Buffer->getBufferSize();
49}
50
Chris Lattner68b28ff2009-01-26 07:37:49 +000051const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000052 // Lazily create the Buffer for ContentCaches that wrap files.
53 if (!Buffer && Entry) {
54 // FIXME: Should we support a way to not have to do this check over
55 // and over if we cannot open the file?
Chris Lattnerac49bb42009-01-17 03:54:16 +000056 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000057 }
Ted Kremenekaa7dac12009-01-06 01:55:26 +000058 return Buffer;
59}
60
Chris Lattner27c0ced2009-01-26 00:43:02 +000061//===--------------------------------------------------------------------===//
Chris Lattnerd2051b42009-01-26 07:57:50 +000062// Line Table Implementation
63//===--------------------------------------------------------------------===//
64
65namespace clang {
66/// LineTableInfo - This class is used to hold and unique data used to
67/// represent #line information.
68class LineTableInfo {
69 /// FilenameIDs - This map is used to assign unique IDs to filenames in
70 /// #line directives. This allows us to unique the filenames that
71 /// frequently reoccur and reference them with indices. FilenameIDs holds
72 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
73 /// to string.
74 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
75 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
76public:
77 LineTableInfo() {
78 }
79
80 void clear() {
81 FilenameIDs.clear();
82 FilenamesByID.clear();
83 }
84
85 ~LineTableInfo() {}
86
87 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
88
89};
90} // namespace clang
91
92
93
94
95unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
96 // Look up the filename in the string table, returning the pre-existing value
97 // if it exists.
98 llvm::StringMapEntry<unsigned> &Entry =
99 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
100 if (Entry.getValue() != ~0U)
101 return Entry.getValue();
102
103 // Otherwise, assign this the next available ID.
104 Entry.setValue(FilenamesByID.size());
105 FilenamesByID.push_back(&Entry);
106 return FilenamesByID.size()-1;
107}
108
109/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
110///
111unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
112 if (LineTable == 0)
113 LineTable = new LineTableInfo();
114 return LineTable->getLineTableFilenameID(Ptr, Len);
115}
116
117
118//===--------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000119// Private 'Create' methods.
120//===--------------------------------------------------------------------===//
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000121
Chris Lattnerd2051b42009-01-26 07:57:50 +0000122SourceManager::~SourceManager() {
123 delete LineTable;
124}
125
126void SourceManager::clearIDTables() {
127 MainFileID = FileID();
128 SLocEntryTable.clear();
129 LastLineNoFileIDQuery = FileID();
130 LastLineNoContentCache = 0;
131 LastFileIDLookup = FileID();
132
133 if (LineTable)
134 LineTable->clear();
135
136 // Use up FileID #0 as an invalid instantiation.
137 NextOffset = 0;
138 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
139}
140
Chris Lattner27c0ced2009-01-26 00:43:02 +0000141/// getOrCreateContentCache - Create or return a cached ContentCache for the
142/// specified file.
143const ContentCache *
144SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattner27c0ced2009-01-26 00:43:02 +0000146
Chris Lattner4b009652007-07-25 00:24:17 +0000147 // Do we already have information about this file?
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000148 std::set<ContentCache>::iterator I =
149 FileInfos.lower_bound(ContentCache(FileEnt));
150
151 if (I != FileInfos.end() && I->Entry == FileEnt)
Chris Lattner4b009652007-07-25 00:24:17 +0000152 return &*I;
153
Chris Lattner68b28ff2009-01-26 07:37:49 +0000154 // Nope, create a new Cache entry.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000155 ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000156 Entry.SourceLineCache = 0;
157 Entry.NumLines = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000158 return &Entry;
159}
160
161
Ted Kremenek27f9c9b2007-10-31 17:53:38 +0000162/// createMemBufferContentCache - Create a new ContentCache for the specified
163/// memory buffer. This does no caching.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000164const ContentCache*
165SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Ted Kremenek7670cca2007-10-30 22:57:35 +0000166 // Add a new ContentCache to the MemBufferInfos list and return it. We
167 // must default construct the object first that the instance actually
168 // stored within MemBufferInfos actually owns the Buffer, and not any
169 // temporary we would use in the call to "push_back".
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000170 MemBufferInfos.push_back(ContentCache());
171 ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000172 Entry.setBuffer(Buffer);
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000173 return &Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
Chris Lattner27c0ced2009-01-26 00:43:02 +0000176//===----------------------------------------------------------------------===//
177// Methods to create new FileID's and instantiations.
178//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000179
Nico Weber630347d2008-09-29 00:25:48 +0000180/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek7670cca2007-10-30 22:57:35 +0000181/// include position. This works regardless of whether the ContentCache
182/// corresponds to a file or some other input source.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000183FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000184 SourceLocation IncludePos,
185 SrcMgr::CharacteristicKind FileCharacter) {
186 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
187 FileInfo::get(IncludePos, File,
188 FileCharacter)));
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000189 unsigned FileSize = File->getSize();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000190 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
191 NextOffset += FileSize+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000192
Chris Lattner27c0ced2009-01-26 00:43:02 +0000193 // Set LastFileIDLookup to the newly created file. The next getFileID call is
194 // almost guaranteed to be from that file.
195 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000196}
197
Chris Lattner27c0ced2009-01-26 00:43:02 +0000198/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnercdf600e2009-01-16 07:00:02 +0000199/// that a token from SpellingLoc should actually be referenced from
Chris Lattner4b009652007-07-25 00:24:17 +0000200/// InstantiationLoc.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000201SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
202 SourceLocation InstantLoc,
203 unsigned TokLength) {
Chris Lattner27c0ced2009-01-26 00:43:02 +0000204 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
205 InstantiationInfo::get(InstantLoc,
206 SpellingLoc)));
207 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
208 NextOffset += TokLength+1;
209 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Chris Lattner4b009652007-07-25 00:24:17 +0000210}
211
Chris Lattner71e443a2009-01-19 07:32:13 +0000212/// getBufferData - Return a pointer to the start and end of the source buffer
213/// data for the specified FileID.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000214std::pair<const char*, const char*>
215SourceManager::getBufferData(FileID FID) const {
216 const llvm::MemoryBuffer *Buf = getBuffer(FID);
217 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
218}
219
220
Chris Lattner27c0ced2009-01-26 00:43:02 +0000221//===--------------------------------------------------------------------===//
222// SourceLocation manipulation methods.
223//===--------------------------------------------------------------------===//
224
225/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
226/// method that is used for all SourceManager queries that start with a
227/// SourceLocation object. It is responsible for finding the entry in
228/// SLocEntryTable which contains the specified location.
229///
230FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
231 assert(SLocOffset && "Invalid FileID");
232
233 // After the first and second level caches, I see two common sorts of
234 // behavior: 1) a lot of searched FileID's are "near" the cached file location
235 // or are "near" the cached instantiation location. 2) others are just
236 // completely random and may be a very long way away.
237 //
238 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
239 // then we fall back to a less cache efficient, but more scalable, binary
240 // search to find the location.
241
242 // See if this is near the file point - worst case we start scanning from the
243 // most newly created FileID.
244 std::vector<SrcMgr::SLocEntry>::const_iterator I;
245
246 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
247 // Neither loc prunes our search.
248 I = SLocEntryTable.end();
249 } else {
250 // Perhaps it is near the file point.
251 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
252 }
253
254 // Find the FileID that contains this. "I" is an iterator that points to a
255 // FileID whose offset is known to be larger than SLocOffset.
256 unsigned NumProbes = 0;
257 while (1) {
258 --I;
259 if (I->getOffset() <= SLocOffset) {
260#if 0
261 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
262 I-SLocEntryTable.begin(),
263 I->isInstantiation() ? "inst" : "file",
264 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
265#endif
266 FileID Res = FileID::get(I-SLocEntryTable.begin());
267
268 // If this isn't an instantiation, remember it. We have good locality
269 // across FileID lookups.
270 if (!I->isInstantiation())
271 LastFileIDLookup = Res;
272 NumLinearScans += NumProbes+1;
273 return Res;
274 }
275 if (++NumProbes == 8)
276 break;
277 }
278
279 // Convert "I" back into an index. We know that it is an entry whose index is
280 // larger than the offset we are looking for.
281 unsigned GreaterIndex = I-SLocEntryTable.begin();
282 // LessIndex - This is the lower bound of the range that we're searching.
283 // We know that the offset corresponding to the FileID is is less than
284 // SLocOffset.
285 unsigned LessIndex = 0;
286 NumProbes = 0;
287 while (1) {
288 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
289 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
290
291 ++NumProbes;
292
293 // If the offset of the midpoint is too large, chop the high side of the
294 // range to the midpoint.
295 if (MidOffset > SLocOffset) {
296 GreaterIndex = MiddleIndex;
297 continue;
298 }
299
300 // If the middle index contains the value, succeed and return.
301 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
302#if 0
303 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
304 I-SLocEntryTable.begin(),
305 I->isInstantiation() ? "inst" : "file",
306 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
307#endif
308 FileID Res = FileID::get(MiddleIndex);
309
310 // If this isn't an instantiation, remember it. We have good locality
311 // across FileID lookups.
312 if (!I->isInstantiation())
313 LastFileIDLookup = Res;
314 NumBinaryProbes += NumProbes;
315 return Res;
316 }
317
318 // Otherwise, move the low-side up to the middle index.
319 LessIndex = MiddleIndex;
320 }
321}
322
Chris Lattner8d92c1a2009-01-26 20:04:19 +0000323SourceLocation SourceManager::
324getInstantiationLocSlowCase(SourceLocation Loc) const {
325 do {
326 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
327 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
328 Loc = Loc.getFileLocWithOffset(LocInfo.second);
329 } while (!Loc.isFileID());
330
331 return Loc;
332}
333
334SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
335 do {
336 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
337 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
338 Loc = Loc.getFileLocWithOffset(LocInfo.second);
339 } while (!Loc.isFileID());
340 return Loc;
341}
342
343
Chris Lattner27c0ced2009-01-26 00:43:02 +0000344std::pair<FileID, unsigned>
345SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
346 unsigned Offset) const {
347 // If this is an instantiation record, walk through all the instantiation
348 // points.
349 FileID FID;
350 SourceLocation Loc;
351 do {
352 Loc = E->getInstantiation().getInstantiationLoc();
353
354 FID = getFileID(Loc);
355 E = &getSLocEntry(FID);
356 Offset += Loc.getOffset()-E->getOffset();
Chris Lattner18ad5582009-01-26 19:41:58 +0000357 } while (!Loc.isFileID());
Chris Lattner27c0ced2009-01-26 00:43:02 +0000358
359 return std::make_pair(FID, Offset);
360}
361
362std::pair<FileID, unsigned>
363SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
364 unsigned Offset) const {
Chris Lattner18ad5582009-01-26 19:41:58 +0000365 // If this is an instantiation record, walk through all the instantiation
366 // points.
367 FileID FID;
368 SourceLocation Loc;
369 do {
370 Loc = E->getInstantiation().getSpellingLoc();
371
372 FID = getFileID(Loc);
373 E = &getSLocEntry(FID);
374 Offset += Loc.getOffset()-E->getOffset();
375 } while (!Loc.isFileID());
376
Chris Lattner27c0ced2009-01-26 00:43:02 +0000377 return std::make_pair(FID, Offset);
378}
379
380
381//===----------------------------------------------------------------------===//
382// Queries about the code at a SourceLocation.
383//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000384
385/// getCharacterData - Return a pointer to the start of the specified location
386/// in the appropriate MemoryBuffer.
387const char *SourceManager::getCharacterData(SourceLocation SL) const {
388 // Note that this is a hot function in the getSpelling() path, which is
389 // heavily used by -E mode.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000390 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000391
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000392 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000393 return getSLocEntry(LocInfo.first).getFile().getContentCache()
394 ->getBuffer()->getBufferStart() + LocInfo.second;
Chris Lattner4b009652007-07-25 00:24:17 +0000395}
396
397
398/// getColumnNumber - Return the column # for the specified file position.
399/// this is significantly cheaper to compute than the line number. This returns
400/// zero if the column number isn't known.
401unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Chris Lattner27c0ced2009-01-26 00:43:02 +0000402 if (Loc.isInvalid()) return 0;
403 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Chris Lattner4b009652007-07-25 00:24:17 +0000404
Chris Lattner27c0ced2009-01-26 00:43:02 +0000405 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000406 unsigned FilePos = LocInfo.second;
407
408 const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
Chris Lattner4b009652007-07-25 00:24:17 +0000409
410 unsigned LineStart = FilePos;
411 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
412 --LineStart;
413 return FilePos-LineStart+1;
414}
415
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000416static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000417static void ComputeLineNumbers(ContentCache* FI) {
418 // Note that calling 'getBuffer()' may lazily page in the file.
419 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000420
421 // Find the file offsets of all of the *physical* source lines. This does
422 // not look at trigraphs, escaped newlines, or anything else tricky.
423 std::vector<unsigned> LineOffsets;
424
425 // Line #1 starts at char 0.
426 LineOffsets.push_back(0);
427
428 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
429 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
430 unsigned Offs = 0;
431 while (1) {
432 // Skip over the contents of the line.
433 // TODO: Vectorize this? This is very performance sensitive for programs
434 // with lots of diagnostics and in -E mode.
435 const unsigned char *NextBuf = (const unsigned char *)Buf;
436 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
437 ++NextBuf;
438 Offs += NextBuf-Buf;
439 Buf = NextBuf;
440
441 if (Buf[0] == '\n' || Buf[0] == '\r') {
442 // If this is \n\r or \r\n, skip both characters.
443 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
444 ++Offs, ++Buf;
445 ++Offs, ++Buf;
446 LineOffsets.push_back(Offs);
447 } else {
448 // Otherwise, this is a null. If end of file, exit.
449 if (Buf == End) break;
450 // Otherwise, skip the null.
451 ++Offs, ++Buf;
452 }
453 }
Chris Lattner4b009652007-07-25 00:24:17 +0000454
455 // Copy the offsets into the FileInfo structure.
456 FI->NumLines = LineOffsets.size();
457 FI->SourceLineCache = new unsigned[LineOffsets.size()];
458 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
459}
460
Chris Lattnercdf600e2009-01-16 07:00:02 +0000461/// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner4b009652007-07-25 00:24:17 +0000462/// for the position indicated. This requires building and caching a table of
463/// line offsets for the MemoryBuffer, so this is not cheap: use only when
464/// about to emit a diagnostic.
Chris Lattnere9bf3e32008-11-18 06:51:15 +0000465unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattner27c0ced2009-01-26 00:43:02 +0000466 if (Loc.isInvalid()) return 0;
467 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000468
Chris Lattner27c0ced2009-01-26 00:43:02 +0000469 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
470
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000471 ContentCache *Content;
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000472 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000473 Content = LastLineNoContentCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000474 else
Chris Lattner27c0ced2009-01-26 00:43:02 +0000475 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
476 .getFile().getContentCache());
Chris Lattner4b009652007-07-25 00:24:17 +0000477
478 // If this is the first use of line information for this buffer, compute the
479 /// SourceLineCache for it on demand.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000480 if (Content->SourceLineCache == 0)
481 ComputeLineNumbers(Content);
Chris Lattner4b009652007-07-25 00:24:17 +0000482
483 // Okay, we know we have a line number table. Do a binary search to find the
484 // line number that this character position lands on.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000485 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000486 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000487 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner4b009652007-07-25 00:24:17 +0000488
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000489 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000490
491 // If the previous query was to the same file, we know both the file pos from
492 // that query and the line number returned. This allows us to narrow the
493 // search space from the entire file to something near the match.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000494 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner4b009652007-07-25 00:24:17 +0000495 if (QueriedFilePos >= LastLineNoFilePos) {
496 SourceLineCache = SourceLineCache+LastLineNoResult-1;
497
498 // The query is likely to be nearby the previous one. Here we check to
499 // see if it is within 5, 10 or 20 lines. It can be far away in cases
500 // where big comment blocks and vertical whitespace eat up lines but
501 // contribute no tokens.
502 if (SourceLineCache+5 < SourceLineCacheEnd) {
503 if (SourceLineCache[5] > QueriedFilePos)
504 SourceLineCacheEnd = SourceLineCache+5;
505 else if (SourceLineCache+10 < SourceLineCacheEnd) {
506 if (SourceLineCache[10] > QueriedFilePos)
507 SourceLineCacheEnd = SourceLineCache+10;
508 else if (SourceLineCache+20 < SourceLineCacheEnd) {
509 if (SourceLineCache[20] > QueriedFilePos)
510 SourceLineCacheEnd = SourceLineCache+20;
511 }
512 }
513 }
514 } else {
515 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
516 }
517 }
518
519 // If the spread is large, do a "radix" test as our initial guess, based on
520 // the assumption that lines average to approximately the same length.
521 // NOTE: This is currently disabled, as it does not appear to be profitable in
522 // initial measurements.
523 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000524 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner4b009652007-07-25 00:24:17 +0000525
526 // Take a stab at guessing where it is.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000527 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner4b009652007-07-25 00:24:17 +0000528
529 // Check for -10 and +10 lines.
530 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
531 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
532
533 // If the computed lower bound is less than the query location, move it in.
534 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
535 SourceLineCacheStart[LowerBound] < QueriedFilePos)
536 SourceLineCache = SourceLineCacheStart+LowerBound;
537
538 // If the computed upper bound is greater than the query location, move it.
539 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
540 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
541 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
542 }
543
544 unsigned *Pos
545 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
546 unsigned LineNo = Pos-SourceLineCacheStart;
547
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000548 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000549 LastLineNoContentCache = Content;
Chris Lattner4b009652007-07-25 00:24:17 +0000550 LastLineNoFilePos = QueriedFilePos;
551 LastLineNoResult = LineNo;
552 return LineNo;
553}
554
Chris Lattner27c0ced2009-01-26 00:43:02 +0000555/// getSourceName - This method returns the name of the file or buffer that
556/// the SourceLocation specifies. This can be modified with #line directives,
557/// etc.
558const char *SourceManager::getSourceName(SourceLocation Loc) const {
559 if (Loc.isInvalid()) return "";
560
561 const SrcMgr::ContentCache *C =
562 getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache();
563
564 // To get the source name, first consult the FileEntry (if one exists) before
565 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
566 return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
567}
568
569//===----------------------------------------------------------------------===//
570// Other miscellaneous methods.
571//===----------------------------------------------------------------------===//
572
573
Chris Lattner4b009652007-07-25 00:24:17 +0000574/// PrintStats - Print statistics to stderr.
575///
576void SourceManager::PrintStats() const {
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000577 llvm::cerr << "\n*** Source Manager Stats:\n";
578 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner66284fd2009-01-27 05:22:43 +0000579 << " mem buffers mapped.\n";
580 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
581 << NextOffset << "B of Sloc address space used.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000582
Chris Lattner4b009652007-07-25 00:24:17 +0000583 unsigned NumLineNumsComputed = 0;
584 unsigned NumFileBytesMapped = 0;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000585 for (std::set<ContentCache>::const_iterator I =
Chris Lattner4b009652007-07-25 00:24:17 +0000586 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000587 NumLineNumsComputed += I->SourceLineCache != 0;
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000588 NumFileBytesMapped += I->getSizeBytesMapped();
Chris Lattner4b009652007-07-25 00:24:17 +0000589 }
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000590
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000591 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
592 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattner27c0ced2009-01-26 00:43:02 +0000593 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
594 << NumBinaryProbes << " binary.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000595}
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000596
597//===----------------------------------------------------------------------===//
598// Serialization.
599//===----------------------------------------------------------------------===//
Ted Kremenek9c856e92007-12-05 00:14:18 +0000600
601void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000602 S.FlushRecord();
603 S.EmitPtr(this);
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000604
Ted Kremenek1b6dd6f2007-12-18 22:12:19 +0000605 if (Entry) {
606 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
607
608 if (Fname.isAbsolute())
609 S.EmitCStr(Fname.c_str());
610 else {
611 // Create an absolute path.
612 // FIXME: This will potentially contain ".." and "." in the path.
613 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
614 path.appendComponent(Fname.c_str());
615 S.EmitCStr(path.c_str());
616 }
617 }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000618 else {
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000619 const char* p = Buffer->getBufferStart();
620 const char* e = Buffer->getBufferEnd();
621
Ted Kremenek9c856e92007-12-05 00:14:18 +0000622 S.EmitInt(e-p);
623
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000624 for ( ; p != e; ++p)
Ted Kremenek9c856e92007-12-05 00:14:18 +0000625 S.EmitInt(*p);
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000626 }
627
Ted Kremenek9c856e92007-12-05 00:14:18 +0000628 S.FlushRecord();
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000629}
Ted Kremenek9c856e92007-12-05 00:14:18 +0000630
631void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
632 SourceManager& SMgr,
633 FileManager* FMgr,
634 std::vector<char>& Buf) {
635 if (FMgr) {
636 llvm::SerializedPtrID PtrID = D.ReadPtrID();
637 D.ReadCStr(Buf,false);
638
639 // Create/fetch the FileEntry.
640 const char* start = &Buf[0];
641 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
642
Ted Kremenekb92cd872007-12-13 18:12:10 +0000643 // FIXME: Ideally we want a lazy materialization of the ContentCache
644 // anyway, because we don't want to read in source files unless this
645 // is absolutely needed.
646 if (!E)
647 D.RegisterPtr(PtrID,NULL);
Nico Weber630347d2008-09-29 00:25:48 +0000648 else
Ted Kremenekb92cd872007-12-13 18:12:10 +0000649 // Get the ContextCache object and register it with the deserializer.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000650 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
651 return;
Ted Kremenek9c856e92007-12-05 00:14:18 +0000652 }
Chris Lattner27c0ced2009-01-26 00:43:02 +0000653
654 // Register the ContextCache object with the deserializer.
655 SMgr.MemBufferInfos.push_back(ContentCache());
656 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
657 D.RegisterPtr(&Entry);
658
659 // Create the buffer.
660 unsigned Size = D.ReadInt();
661 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
662
663 // Read the contents of the buffer.
664 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
665 for (unsigned i = 0; i < Size ; ++i)
666 p[i] = D.ReadInt();
Ted Kremenek9c856e92007-12-05 00:14:18 +0000667}
668
669void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000670 S.EnterBlock();
671 S.EmitPtr(this);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000672 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000673
Ted Kremenek9c856e92007-12-05 00:14:18 +0000674 // Emit: FileInfos. Just emit the file name.
675 S.EnterBlock();
676
677 std::for_each(FileInfos.begin(),FileInfos.end(),
678 S.MakeEmitter<ContentCache>());
679
680 S.ExitBlock();
681
682 // Emit: MemBufferInfos
683 S.EnterBlock();
684
685 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
686 S.MakeEmitter<ContentCache>());
687
688 S.ExitBlock();
689
Chris Lattner27c0ced2009-01-26 00:43:02 +0000690 // FIXME: Emit SLocEntryTable.
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000691
692 S.ExitBlock();
Ted Kremenek9c856e92007-12-05 00:14:18 +0000693}
694
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000695SourceManager*
696SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
697 SourceManager *M = new SourceManager();
698 D.RegisterPtr(M);
699
Ted Kremenek2578dd02007-12-19 22:29:55 +0000700 // Read: the FileID of the main source file of the translation unit.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000701 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek2578dd02007-12-19 22:29:55 +0000702
Ted Kremenek9c856e92007-12-05 00:14:18 +0000703 std::vector<char> Buf;
704
705 { // Read: FileInfos.
706 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
707 while (!D.FinishedBlock(BLoc))
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000708 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000709 }
710
711 { // Read: MemBufferInfos.
712 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
713 while (!D.FinishedBlock(BLoc))
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000714 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000715 }
716
Chris Lattner27c0ced2009-01-26 00:43:02 +0000717 // FIXME: Read SLocEntryTable.
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000718
719 return M;
Ted Kremenek12206af2007-12-10 18:01:25 +0000720}