blob: 88292cd42bd98ebb8341aad4bd5fa5608b5a40b2 [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
Chris Lattnerde7aeef2009-01-26 00:43:02 +000027//===--------------------------------------------------------------------===//
28// SourceManager Helper Classes
29//===--------------------------------------------------------------------===//
30
Ted Kremenek78d85f52007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
33 delete [] SourceLineCache;
Reid Spencer5f016e22007-07-11 17:01:13 +000034}
35
Ted Kremenekc16c2082009-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 Lattner987cd3d2009-01-26 07:37:49 +000051const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek5b034ad2009-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 Lattner05816592009-01-17 03:54:16 +000056 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek5b034ad2009-01-06 22:43:04 +000057 }
Ted Kremenekc16c2082009-01-06 01:55:26 +000058 return Buffer;
59}
60
Chris Lattnerde7aeef2009-01-26 00:43:02 +000061//===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-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 Lattnerde7aeef2009-01-26 00:43:02 +0000119// Private 'Create' methods.
120//===--------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000121
Chris Lattner5b9a5042009-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 Lattnerde7aeef2009-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) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // Do we already have information about this file?
Ted Kremenek78d85f52007-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)
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 return &*I;
153
Chris Lattner987cd3d2009-01-26 07:37:49 +0000154 // Nope, create a new Cache entry.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000155 ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
Ted Kremenek78d85f52007-10-30 21:08:08 +0000156 Entry.SourceLineCache = 0;
157 Entry.NumLines = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 return &Entry;
159}
160
161
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000162/// createMemBufferContentCache - Create a new ContentCache for the specified
163/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000164const ContentCache*
165SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Ted Kremenek0d892d82007-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 Kremenek78d85f52007-10-30 21:08:08 +0000170 MemBufferInfos.push_back(ContentCache());
171 ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
Ted Kremenekc16c2082009-01-06 01:55:26 +0000172 Entry.setBuffer(Buffer);
Ted Kremenek78d85f52007-10-30 21:08:08 +0000173 return &Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000176//===----------------------------------------------------------------------===//
177// Methods to create new FileID's and instantiations.
178//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000179
Nico Weber48002c82008-09-29 00:25:48 +0000180/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-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 Lattner2b2453a2009-01-17 06:22:33 +0000183FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-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 Kremenekc16c2082009-01-06 01:55:26 +0000189 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000190 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
191 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000192
Chris Lattnerde7aeef2009-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196}
197
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000198/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000199/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000200/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000201SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
202 SourceLocation InstantLoc,
203 unsigned TokLength) {
Chris Lattnerde7aeef2009-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));
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Chris Lattner31530ba2009-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 Lattner2b2453a2009-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 Lattnerde7aeef2009-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 Lattneraddb7972009-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 Lattnerde7aeef2009-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 Lattnerbcd1a1b2009-01-26 19:41:58 +0000357 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-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 Lattnerbcd1a1b2009-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 Lattnerde7aeef2009-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//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerde7aeef2009-01-26 00:43:02 +0000390 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000391
Ted Kremenekc16c2082009-01-06 01:55:26 +0000392 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000393 return getSLocEntry(LocInfo.first).getFile().getContentCache()
394 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000395}
396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397
Chris Lattner9dc1f532007-07-20 16:37:10 +0000398/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000399/// 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 Lattnerde7aeef2009-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");
Reid Spencer5f016e22007-07-11 17:01:13 +0000404
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000405 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000406 unsigned FilePos = LocInfo.second;
407
408 const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek78d85f52007-10-30 21:08:08 +0000416static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
Ted Kremenekc16c2082009-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 Lattner5e36a7a2007-07-24 05:57:19 +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 Lattner5e36a7a2007-07-24 05:57:19 +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}
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000461/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerf812a452008-11-18 06:51:15 +0000465unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-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 Kremenek78d85f52007-10-30 21:08:08 +0000468
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000469 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
470
Chris Lattner2b2453a2009-01-17 06:22:33 +0000471 ContentCache *Content;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000472 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000473 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000474 else
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000475 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
476 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000477
478 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000479 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000480 if (Content->SourceLineCache == 0)
481 ComputeLineNumbers(Content);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek78d85f52007-10-30 21:08:08 +0000485 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000486 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000487 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000488
Chris Lattner2b2453a2009-01-17 06:22:33 +0000489 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +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 Lattner2b2453a2009-01-17 06:22:33 +0000494 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +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
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000519 // 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 Kremenek78d85f52007-10-30 21:08:08 +0000524 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000525
526 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000527 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +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);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000546 unsigned LineNo = Pos-SourceLineCacheStart;
547
Chris Lattner2b2453a2009-01-17 06:22:33 +0000548 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000549 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000550 LastLineNoFilePos = QueriedFilePos;
551 LastLineNoResult = LineNo;
552 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000553}
554
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000555/// getPresumedLoc - This method returns the "presumed" location of a
556/// SourceLocation specifies. A "presumed location" can be modified by #line
557/// or GNU line marker directives. This provides a view on the data that a
558/// user should see in diagnostics, for example.
559///
560/// Note that a presumed location is always given as the instantiation point
561/// of an instantiation location, not at the spelling location.
562PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
563 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000564
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000565 // Presumed locations are always for instantiation points.
566 Loc = getInstantiationLoc(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000567
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000568 // FIXME: Could just decompose Loc once!
569
570 const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile();
571 const SrcMgr::ContentCache *C = FI.getContentCache();
572
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000573 // To get the source name, first consult the FileEntry (if one exists) before
574 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000575 const char *Filename =
576 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
577
578 return PresumedLoc(Filename, getLineNumber(Loc), getColumnNumber(Loc),
579 FI.getIncludeLoc());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000580}
581
582//===----------------------------------------------------------------------===//
583// Other miscellaneous methods.
584//===----------------------------------------------------------------------===//
585
586
Reid Spencer5f016e22007-07-11 17:01:13 +0000587/// PrintStats - Print statistics to stderr.
588///
589void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000590 llvm::cerr << "\n*** Source Manager Stats:\n";
591 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000592 << " mem buffers mapped.\n";
593 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
594 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000595
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 unsigned NumLineNumsComputed = 0;
597 unsigned NumFileBytesMapped = 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000598 for (std::set<ContentCache>::const_iterator I =
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000600 NumLineNumsComputed += I->SourceLineCache != 0;
Ted Kremenekc16c2082009-01-06 01:55:26 +0000601 NumFileBytesMapped += I->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000603
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000604 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
605 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000606 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
607 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000608}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000609
610//===----------------------------------------------------------------------===//
611// Serialization.
612//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000613
614void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000615 S.FlushRecord();
616 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000617
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000618 if (Entry) {
619 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
620
621 if (Fname.isAbsolute())
622 S.EmitCStr(Fname.c_str());
623 else {
624 // Create an absolute path.
625 // FIXME: This will potentially contain ".." and "." in the path.
626 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
627 path.appendComponent(Fname.c_str());
628 S.EmitCStr(path.c_str());
629 }
630 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000631 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000632 const char* p = Buffer->getBufferStart();
633 const char* e = Buffer->getBufferEnd();
634
Ted Kremenek099b4742007-12-05 00:14:18 +0000635 S.EmitInt(e-p);
636
Ted Kremeneke21272f2007-12-04 19:39:02 +0000637 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000638 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000639 }
640
Ted Kremenek099b4742007-12-05 00:14:18 +0000641 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000642}
Ted Kremenek099b4742007-12-05 00:14:18 +0000643
644void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
645 SourceManager& SMgr,
646 FileManager* FMgr,
647 std::vector<char>& Buf) {
648 if (FMgr) {
649 llvm::SerializedPtrID PtrID = D.ReadPtrID();
650 D.ReadCStr(Buf,false);
651
652 // Create/fetch the FileEntry.
653 const char* start = &Buf[0];
654 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
655
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000656 // FIXME: Ideally we want a lazy materialization of the ContentCache
657 // anyway, because we don't want to read in source files unless this
658 // is absolutely needed.
659 if (!E)
660 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000661 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000662 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000663 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
664 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000665 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000666
667 // Register the ContextCache object with the deserializer.
668 SMgr.MemBufferInfos.push_back(ContentCache());
669 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
670 D.RegisterPtr(&Entry);
671
672 // Create the buffer.
673 unsigned Size = D.ReadInt();
674 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
675
676 // Read the contents of the buffer.
677 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
678 for (unsigned i = 0; i < Size ; ++i)
679 p[i] = D.ReadInt();
Ted Kremenek099b4742007-12-05 00:14:18 +0000680}
681
682void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000683 S.EnterBlock();
684 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000685 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000686
Ted Kremenek099b4742007-12-05 00:14:18 +0000687 // Emit: FileInfos. Just emit the file name.
688 S.EnterBlock();
689
690 std::for_each(FileInfos.begin(),FileInfos.end(),
691 S.MakeEmitter<ContentCache>());
692
693 S.ExitBlock();
694
695 // Emit: MemBufferInfos
696 S.EnterBlock();
697
698 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
699 S.MakeEmitter<ContentCache>());
700
701 S.ExitBlock();
702
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000703 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000704
705 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000706}
707
Ted Kremenek1f941002007-12-05 00:19:51 +0000708SourceManager*
709SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
710 SourceManager *M = new SourceManager();
711 D.RegisterPtr(M);
712
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000713 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000714 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000715
Ted Kremenek099b4742007-12-05 00:14:18 +0000716 std::vector<char> Buf;
717
718 { // Read: FileInfos.
719 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
720 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000721 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000722 }
723
724 { // Read: MemBufferInfos.
725 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
726 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000727 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000728 }
729
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000730 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000731
732 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000733}