blob: 42e6040b7464d84ca57556bc10dfd42f93dcedb8 [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 Lattnerabca2bb2007-07-15 06:35:27 +0000204 // The specified source location may be a mapped location, due to a macro
205 // instantiation or #line directive. Strip off this information to find out
206 // where the characters are actually located.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000207 SpellingLoc = getSpellingLoc(SpellingLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000209 // Resolve InstantLoc down to a real instantiation location.
210 InstantLoc = getInstantiationLoc(InstantLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000211
212 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
213 InstantiationInfo::get(InstantLoc,
214 SpellingLoc)));
215 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
216 NextOffset += TokLength+1;
217 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000218}
219
Chris Lattner31530ba2009-01-19 07:32:13 +0000220/// getBufferData - Return a pointer to the start and end of the source buffer
221/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000222std::pair<const char*, const char*>
223SourceManager::getBufferData(FileID FID) const {
224 const llvm::MemoryBuffer *Buf = getBuffer(FID);
225 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
226}
227
228
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000229//===--------------------------------------------------------------------===//
230// SourceLocation manipulation methods.
231//===--------------------------------------------------------------------===//
232
233/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
234/// method that is used for all SourceManager queries that start with a
235/// SourceLocation object. It is responsible for finding the entry in
236/// SLocEntryTable which contains the specified location.
237///
238FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
239 assert(SLocOffset && "Invalid FileID");
240
241 // After the first and second level caches, I see two common sorts of
242 // behavior: 1) a lot of searched FileID's are "near" the cached file location
243 // or are "near" the cached instantiation location. 2) others are just
244 // completely random and may be a very long way away.
245 //
246 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
247 // then we fall back to a less cache efficient, but more scalable, binary
248 // search to find the location.
249
250 // See if this is near the file point - worst case we start scanning from the
251 // most newly created FileID.
252 std::vector<SrcMgr::SLocEntry>::const_iterator I;
253
254 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
255 // Neither loc prunes our search.
256 I = SLocEntryTable.end();
257 } else {
258 // Perhaps it is near the file point.
259 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
260 }
261
262 // Find the FileID that contains this. "I" is an iterator that points to a
263 // FileID whose offset is known to be larger than SLocOffset.
264 unsigned NumProbes = 0;
265 while (1) {
266 --I;
267 if (I->getOffset() <= SLocOffset) {
268#if 0
269 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
270 I-SLocEntryTable.begin(),
271 I->isInstantiation() ? "inst" : "file",
272 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
273#endif
274 FileID Res = FileID::get(I-SLocEntryTable.begin());
275
276 // If this isn't an instantiation, remember it. We have good locality
277 // across FileID lookups.
278 if (!I->isInstantiation())
279 LastFileIDLookup = Res;
280 NumLinearScans += NumProbes+1;
281 return Res;
282 }
283 if (++NumProbes == 8)
284 break;
285 }
286
287 // Convert "I" back into an index. We know that it is an entry whose index is
288 // larger than the offset we are looking for.
289 unsigned GreaterIndex = I-SLocEntryTable.begin();
290 // LessIndex - This is the lower bound of the range that we're searching.
291 // We know that the offset corresponding to the FileID is is less than
292 // SLocOffset.
293 unsigned LessIndex = 0;
294 NumProbes = 0;
295 while (1) {
296 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
297 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
298
299 ++NumProbes;
300
301 // If the offset of the midpoint is too large, chop the high side of the
302 // range to the midpoint.
303 if (MidOffset > SLocOffset) {
304 GreaterIndex = MiddleIndex;
305 continue;
306 }
307
308 // If the middle index contains the value, succeed and return.
309 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
310#if 0
311 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
312 I-SLocEntryTable.begin(),
313 I->isInstantiation() ? "inst" : "file",
314 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
315#endif
316 FileID Res = FileID::get(MiddleIndex);
317
318 // If this isn't an instantiation, remember it. We have good locality
319 // across FileID lookups.
320 if (!I->isInstantiation())
321 LastFileIDLookup = Res;
322 NumBinaryProbes += NumProbes;
323 return Res;
324 }
325
326 // Otherwise, move the low-side up to the middle index.
327 LessIndex = MiddleIndex;
328 }
329}
330
331std::pair<FileID, unsigned>
332SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
333 unsigned Offset) const {
334 // If this is an instantiation record, walk through all the instantiation
335 // points.
336 FileID FID;
337 SourceLocation Loc;
338 do {
339 Loc = E->getInstantiation().getInstantiationLoc();
340
341 FID = getFileID(Loc);
342 E = &getSLocEntry(FID);
343 Offset += Loc.getOffset()-E->getOffset();
344 } while (Loc.isFileID());
345
346 return std::make_pair(FID, Offset);
347}
348
349std::pair<FileID, unsigned>
350SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
351 unsigned Offset) const {
352 // If this is an instantiation record, get and return the spelling.
353 SourceLocation Loc = E->getInstantiation().getSpellingLoc();
354 FileID FID = getFileID(Loc);
355 E = &getSLocEntry(FID);
356 Offset += Loc.getOffset()-E->getOffset();
357 assert(Loc.isFileID() && "Should only have one spelling link");
358 return std::make_pair(FID, Offset);
359}
360
361
362//===----------------------------------------------------------------------===//
363// Queries about the code at a SourceLocation.
364//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000365
366/// getCharacterData - Return a pointer to the start of the specified location
367/// in the appropriate MemoryBuffer.
368const char *SourceManager::getCharacterData(SourceLocation SL) const {
369 // Note that this is a hot function in the getSpelling() path, which is
370 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000371 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000372
Ted Kremenekc16c2082009-01-06 01:55:26 +0000373 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000374 return getSLocEntry(LocInfo.first).getFile().getContentCache()
375 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
Reid Spencer5f016e22007-07-11 17:01:13 +0000378
Chris Lattner9dc1f532007-07-20 16:37:10 +0000379/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000380/// this is significantly cheaper to compute than the line number. This returns
381/// zero if the column number isn't known.
382unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000383 if (Loc.isInvalid()) return 0;
384 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Reid Spencer5f016e22007-07-11 17:01:13 +0000385
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000386 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000387 unsigned FilePos = LocInfo.second;
388
389 const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
391 unsigned LineStart = FilePos;
392 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
393 --LineStart;
394 return FilePos-LineStart+1;
395}
396
Ted Kremenek78d85f52007-10-30 21:08:08 +0000397static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
Ted Kremenekc16c2082009-01-06 01:55:26 +0000398static void ComputeLineNumbers(ContentCache* FI) {
399 // Note that calling 'getBuffer()' may lazily page in the file.
400 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000401
402 // Find the file offsets of all of the *physical* source lines. This does
403 // not look at trigraphs, escaped newlines, or anything else tricky.
404 std::vector<unsigned> LineOffsets;
405
406 // Line #1 starts at char 0.
407 LineOffsets.push_back(0);
408
409 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
410 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
411 unsigned Offs = 0;
412 while (1) {
413 // Skip over the contents of the line.
414 // TODO: Vectorize this? This is very performance sensitive for programs
415 // with lots of diagnostics and in -E mode.
416 const unsigned char *NextBuf = (const unsigned char *)Buf;
417 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
418 ++NextBuf;
419 Offs += NextBuf-Buf;
420 Buf = NextBuf;
421
422 if (Buf[0] == '\n' || Buf[0] == '\r') {
423 // If this is \n\r or \r\n, skip both characters.
424 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
425 ++Offs, ++Buf;
426 ++Offs, ++Buf;
427 LineOffsets.push_back(Offs);
428 } else {
429 // Otherwise, this is a null. If end of file, exit.
430 if (Buf == End) break;
431 // Otherwise, skip the null.
432 ++Offs, ++Buf;
433 }
434 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000435
436 // Copy the offsets into the FileInfo structure.
437 FI->NumLines = LineOffsets.size();
438 FI->SourceLineCache = new unsigned[LineOffsets.size()];
439 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
440}
Reid Spencer5f016e22007-07-11 17:01:13 +0000441
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000442/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000443/// for the position indicated. This requires building and caching a table of
444/// line offsets for the MemoryBuffer, so this is not cheap: use only when
445/// about to emit a diagnostic.
Chris Lattnerf812a452008-11-18 06:51:15 +0000446unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000447 if (Loc.isInvalid()) return 0;
448 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Ted Kremenek78d85f52007-10-30 21:08:08 +0000449
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000450 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
451
Chris Lattner2b2453a2009-01-17 06:22:33 +0000452 ContentCache *Content;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000453 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000454 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000455 else
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000456 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
457 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000458
459 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000460 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000461 if (Content->SourceLineCache == 0)
462 ComputeLineNumbers(Content);
Reid Spencer5f016e22007-07-11 17:01:13 +0000463
464 // Okay, we know we have a line number table. Do a binary search to find the
465 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000466 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000467 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000468 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000469
Chris Lattner2b2453a2009-01-17 06:22:33 +0000470 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000471
472 // If the previous query was to the same file, we know both the file pos from
473 // that query and the line number returned. This allows us to narrow the
474 // search space from the entire file to something near the match.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000475 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000476 if (QueriedFilePos >= LastLineNoFilePos) {
477 SourceLineCache = SourceLineCache+LastLineNoResult-1;
478
479 // The query is likely to be nearby the previous one. Here we check to
480 // see if it is within 5, 10 or 20 lines. It can be far away in cases
481 // where big comment blocks and vertical whitespace eat up lines but
482 // contribute no tokens.
483 if (SourceLineCache+5 < SourceLineCacheEnd) {
484 if (SourceLineCache[5] > QueriedFilePos)
485 SourceLineCacheEnd = SourceLineCache+5;
486 else if (SourceLineCache+10 < SourceLineCacheEnd) {
487 if (SourceLineCache[10] > QueriedFilePos)
488 SourceLineCacheEnd = SourceLineCache+10;
489 else if (SourceLineCache+20 < SourceLineCacheEnd) {
490 if (SourceLineCache[20] > QueriedFilePos)
491 SourceLineCacheEnd = SourceLineCache+20;
492 }
493 }
494 }
495 } else {
496 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
497 }
498 }
499
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000500 // If the spread is large, do a "radix" test as our initial guess, based on
501 // the assumption that lines average to approximately the same length.
502 // NOTE: This is currently disabled, as it does not appear to be profitable in
503 // initial measurements.
504 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000505 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000506
507 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000508 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000509
510 // Check for -10 and +10 lines.
511 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
512 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
513
514 // If the computed lower bound is less than the query location, move it in.
515 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
516 SourceLineCacheStart[LowerBound] < QueriedFilePos)
517 SourceLineCache = SourceLineCacheStart+LowerBound;
518
519 // If the computed upper bound is greater than the query location, move it.
520 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
521 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
522 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
523 }
524
525 unsigned *Pos
526 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000527 unsigned LineNo = Pos-SourceLineCacheStart;
528
Chris Lattner2b2453a2009-01-17 06:22:33 +0000529 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000530 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000531 LastLineNoFilePos = QueriedFilePos;
532 LastLineNoResult = LineNo;
533 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000534}
535
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000536/// getSourceName - This method returns the name of the file or buffer that
537/// the SourceLocation specifies. This can be modified with #line directives,
538/// etc.
539const char *SourceManager::getSourceName(SourceLocation Loc) const {
540 if (Loc.isInvalid()) return "";
541
542 const SrcMgr::ContentCache *C =
543 getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache();
544
545 // To get the source name, first consult the FileEntry (if one exists) before
546 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
547 return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
548}
549
550//===----------------------------------------------------------------------===//
551// Other miscellaneous methods.
552//===----------------------------------------------------------------------===//
553
554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555/// PrintStats - Print statistics to stderr.
556///
557void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000558 llvm::cerr << "\n*** Source Manager Stats:\n";
559 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000560 << " mem buffers mapped, " << SLocEntryTable.size()
561 << " SLocEntry's allocated.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000562
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 unsigned NumLineNumsComputed = 0;
564 unsigned NumFileBytesMapped = 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000565 for (std::set<ContentCache>::const_iterator I =
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000567 NumLineNumsComputed += I->SourceLineCache != 0;
Ted Kremenekc16c2082009-01-06 01:55:26 +0000568 NumFileBytesMapped += I->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000570
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000571 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
572 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000573 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
574 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000575}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000576
577//===----------------------------------------------------------------------===//
578// Serialization.
579//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000580
581void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000582 S.FlushRecord();
583 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000584
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000585 if (Entry) {
586 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
587
588 if (Fname.isAbsolute())
589 S.EmitCStr(Fname.c_str());
590 else {
591 // Create an absolute path.
592 // FIXME: This will potentially contain ".." and "." in the path.
593 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
594 path.appendComponent(Fname.c_str());
595 S.EmitCStr(path.c_str());
596 }
597 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000598 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000599 const char* p = Buffer->getBufferStart();
600 const char* e = Buffer->getBufferEnd();
601
Ted Kremenek099b4742007-12-05 00:14:18 +0000602 S.EmitInt(e-p);
603
Ted Kremeneke21272f2007-12-04 19:39:02 +0000604 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000605 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000606 }
607
Ted Kremenek099b4742007-12-05 00:14:18 +0000608 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000609}
Ted Kremenek099b4742007-12-05 00:14:18 +0000610
611void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
612 SourceManager& SMgr,
613 FileManager* FMgr,
614 std::vector<char>& Buf) {
615 if (FMgr) {
616 llvm::SerializedPtrID PtrID = D.ReadPtrID();
617 D.ReadCStr(Buf,false);
618
619 // Create/fetch the FileEntry.
620 const char* start = &Buf[0];
621 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
622
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000623 // FIXME: Ideally we want a lazy materialization of the ContentCache
624 // anyway, because we don't want to read in source files unless this
625 // is absolutely needed.
626 if (!E)
627 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000628 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000629 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000630 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
631 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000632 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000633
634 // Register the ContextCache object with the deserializer.
635 SMgr.MemBufferInfos.push_back(ContentCache());
636 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
637 D.RegisterPtr(&Entry);
638
639 // Create the buffer.
640 unsigned Size = D.ReadInt();
641 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
642
643 // Read the contents of the buffer.
644 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
645 for (unsigned i = 0; i < Size ; ++i)
646 p[i] = D.ReadInt();
Ted Kremenek099b4742007-12-05 00:14:18 +0000647}
648
649void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000650 S.EnterBlock();
651 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000652 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000653
Ted Kremenek099b4742007-12-05 00:14:18 +0000654 // Emit: FileInfos. Just emit the file name.
655 S.EnterBlock();
656
657 std::for_each(FileInfos.begin(),FileInfos.end(),
658 S.MakeEmitter<ContentCache>());
659
660 S.ExitBlock();
661
662 // Emit: MemBufferInfos
663 S.EnterBlock();
664
665 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
666 S.MakeEmitter<ContentCache>());
667
668 S.ExitBlock();
669
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000670 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000671
672 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000673}
674
Ted Kremenek1f941002007-12-05 00:19:51 +0000675SourceManager*
676SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
677 SourceManager *M = new SourceManager();
678 D.RegisterPtr(M);
679
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000680 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000681 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000682
Ted Kremenek099b4742007-12-05 00:14:18 +0000683 std::vector<char> Buf;
684
685 { // Read: FileInfos.
686 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
687 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000688 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000689 }
690
691 { // Read: MemBufferInfos.
692 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
693 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000694 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000695 }
696
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000697 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000698
699 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000700}