blob: 54aed7c9f17150371299c62813d7b192513a4413 [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 Lattner23b5dc62009-02-04 00:40:31 +000027//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000028// SourceManager Helper Classes
Chris Lattner23b5dc62009-02-04 00:40:31 +000029//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000030
Ted Kremenek78d85f52007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
Ted Kremenekc16c2082009-01-06 01:55:26 +000035/// getSizeBytesMapped - Returns the number of bytes actually mapped for
36/// this ContentCache. This can be 0 if the MemBuffer was not actually
37/// instantiated.
38unsigned ContentCache::getSizeBytesMapped() const {
39 return Buffer ? Buffer->getBufferSize() : 0;
40}
41
42/// getSize - Returns the size of the content encapsulated by this ContentCache.
43/// This can be the size of the source file or the size of an arbitrary
44/// scratch buffer. If the ContentCache encapsulates a source file, that
45/// file is not lazily brought in from disk to satisfy this query.
46unsigned ContentCache::getSize() const {
47 return Entry ? Entry->getSize() : Buffer->getBufferSize();
48}
49
Chris Lattner987cd3d2009-01-26 07:37:49 +000050const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek5b034ad2009-01-06 22:43:04 +000051 // Lazily create the Buffer for ContentCaches that wrap files.
52 if (!Buffer && Entry) {
53 // FIXME: Should we support a way to not have to do this check over
54 // and over if we cannot open the file?
Chris Lattner05816592009-01-17 03:54:16 +000055 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek5b034ad2009-01-06 22:43:04 +000056 }
Ted Kremenekc16c2082009-01-06 01:55:26 +000057 return Buffer;
58}
59
Chris Lattner23b5dc62009-02-04 00:40:31 +000060//===----------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000061// Line Table Implementation
Chris Lattner23b5dc62009-02-04 00:40:31 +000062//===----------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000063
64namespace clang {
Chris Lattner23b5dc62009-02-04 00:40:31 +000065struct LineEntry {
66 /// FileOffset - The offset in this file that the line entry occurs at.
67 unsigned FileOffset;
68 /// LineNo - The presumed line number of this line entry: #line 4.
69 unsigned LineNo;
70 /// FilenameID - The ID of the filename identified by this line entry:
71 /// #line 4 "foo.c". This is -1 if not specified.
72 int FilenameID;
73
74 static LineEntry get(unsigned Offs, unsigned Line, int Filename) {
75 LineEntry E;
76 E.FileOffset = Offs;
77 E.LineNo = Line;
78 E.FilenameID = Filename;
79 return E;
80 }
81};
82
83
Chris Lattner5b9a5042009-01-26 07:57:50 +000084/// LineTableInfo - This class is used to hold and unique data used to
85/// represent #line information.
86class LineTableInfo {
87 /// FilenameIDs - This map is used to assign unique IDs to filenames in
88 /// #line directives. This allows us to unique the filenames that
89 /// frequently reoccur and reference them with indices. FilenameIDs holds
90 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
91 /// to string.
92 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
93 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
Chris Lattner23b5dc62009-02-04 00:40:31 +000094
95 /// LineEntries - This is a map from FileIDs to a list of line entries (sorted
96 /// by the offset they occur in the file.
97 std::map<unsigned, std::vector<LineEntry> > LineEntries;
Chris Lattner5b9a5042009-01-26 07:57:50 +000098public:
99 LineTableInfo() {
100 }
101
102 void clear() {
103 FilenameIDs.clear();
104 FilenamesByID.clear();
105 }
106
107 ~LineTableInfo() {}
108
109 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Chris Lattner23b5dc62009-02-04 00:40:31 +0000110 void AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000111 unsigned LineNo, int FilenameID);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000112};
113} // namespace clang
114
115
116
117
118unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
119 // Look up the filename in the string table, returning the pre-existing value
120 // if it exists.
121 llvm::StringMapEntry<unsigned> &Entry =
122 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
123 if (Entry.getValue() != ~0U)
124 return Entry.getValue();
125
126 // Otherwise, assign this the next available ID.
127 Entry.setValue(FilenamesByID.size());
128 FilenamesByID.push_back(&Entry);
129 return FilenamesByID.size()-1;
130}
131
Chris Lattnerac50e342009-02-03 22:13:05 +0000132/// AddLineNote - Add a line note to the line table that indicates that there
133/// is a #line at the specified FID/Offset location which changes the presumed
134/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000135void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000136 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000137 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +0000138
Chris Lattner23b5dc62009-02-04 00:40:31 +0000139 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
140 "Adding line entries out of order!");
141 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID));
Chris Lattnerac50e342009-02-03 22:13:05 +0000142}
143
144
145
Chris Lattner5b9a5042009-01-26 07:57:50 +0000146/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
147///
148unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
149 if (LineTable == 0)
150 LineTable = new LineTableInfo();
151 return LineTable->getLineTableFilenameID(Ptr, Len);
152}
153
154
Chris Lattner4c4ea172009-02-03 21:52:55 +0000155/// AddLineNote - Add a line note to the line table for the FileID and offset
156/// specified by Loc. If FilenameID is -1, it is considered to be
157/// unspecified.
158void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
159 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000160 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000161
Chris Lattnerac50e342009-02-03 22:13:05 +0000162 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
163
164 // Remember that this file has #line directives now if it doesn't already.
165 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
166
167 if (LineTable == 0)
168 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000169 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000170}
171
172
Chris Lattner23b5dc62009-02-04 00:40:31 +0000173//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000174// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000175//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000176
Chris Lattner5b9a5042009-01-26 07:57:50 +0000177SourceManager::~SourceManager() {
178 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000179
180 // Delete FileEntry objects corresponding to content caches. Since the actual
181 // content cache objects are bump pointer allocated, we just have to run the
182 // dtors, but we call the deallocate method for completeness.
183 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
184 MemBufferInfos[i]->~ContentCache();
185 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
186 }
187 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
188 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
189 I->second->~ContentCache();
190 ContentCacheAlloc.Deallocate(I->second);
191 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000192}
193
194void SourceManager::clearIDTables() {
195 MainFileID = FileID();
196 SLocEntryTable.clear();
197 LastLineNoFileIDQuery = FileID();
198 LastLineNoContentCache = 0;
199 LastFileIDLookup = FileID();
200
201 if (LineTable)
202 LineTable->clear();
203
204 // Use up FileID #0 as an invalid instantiation.
205 NextOffset = 0;
206 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
207}
208
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000209/// getOrCreateContentCache - Create or return a cached ContentCache for the
210/// specified file.
211const ContentCache *
212SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000216 ContentCache *&Entry = FileInfos[FileEnt];
217 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
Chris Lattner00282d62009-02-03 07:41:46 +0000219 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
220 // so that FileInfo can use the low 3 bits of the pointer for its own
221 // nefarious purposes.
222 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
223 EntryAlign = std::max(8U, EntryAlign);
224 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000225 new (Entry) ContentCache(FileEnt);
226 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000227}
228
229
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000230/// createMemBufferContentCache - Create a new ContentCache for the specified
231/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000232const ContentCache*
233SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000234 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
235 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
236 // the pointer for its own nefarious purposes.
237 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
238 EntryAlign = std::max(8U, EntryAlign);
239 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000240 new (Entry) ContentCache();
241 MemBufferInfos.push_back(Entry);
242 Entry->setBuffer(Buffer);
243 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000246//===----------------------------------------------------------------------===//
247// Methods to create new FileID's and instantiations.
248//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
Nico Weber48002c82008-09-29 00:25:48 +0000250/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000251/// include position. This works regardless of whether the ContentCache
252/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000253FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000254 SourceLocation IncludePos,
255 SrcMgr::CharacteristicKind FileCharacter) {
256 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
257 FileInfo::get(IncludePos, File,
258 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000259 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000260 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
261 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000263 // Set LastFileIDLookup to the newly created file. The next getFileID call is
264 // almost guaranteed to be from that file.
265 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266}
267
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000268/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000269/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000270/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000271SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
272 SourceLocation InstantLoc,
273 unsigned TokLength) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000274 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
275 InstantiationInfo::get(InstantLoc,
276 SpellingLoc)));
277 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
278 NextOffset += TokLength+1;
279 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Chris Lattner31530ba2009-01-19 07:32:13 +0000282/// getBufferData - Return a pointer to the start and end of the source buffer
283/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000284std::pair<const char*, const char*>
285SourceManager::getBufferData(FileID FID) const {
286 const llvm::MemoryBuffer *Buf = getBuffer(FID);
287 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
288}
289
290
Chris Lattner23b5dc62009-02-04 00:40:31 +0000291//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000292// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000293//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000294
295/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
296/// method that is used for all SourceManager queries that start with a
297/// SourceLocation object. It is responsible for finding the entry in
298/// SLocEntryTable which contains the specified location.
299///
300FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
301 assert(SLocOffset && "Invalid FileID");
302
303 // After the first and second level caches, I see two common sorts of
304 // behavior: 1) a lot of searched FileID's are "near" the cached file location
305 // or are "near" the cached instantiation location. 2) others are just
306 // completely random and may be a very long way away.
307 //
308 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
309 // then we fall back to a less cache efficient, but more scalable, binary
310 // search to find the location.
311
312 // See if this is near the file point - worst case we start scanning from the
313 // most newly created FileID.
314 std::vector<SrcMgr::SLocEntry>::const_iterator I;
315
316 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
317 // Neither loc prunes our search.
318 I = SLocEntryTable.end();
319 } else {
320 // Perhaps it is near the file point.
321 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
322 }
323
324 // Find the FileID that contains this. "I" is an iterator that points to a
325 // FileID whose offset is known to be larger than SLocOffset.
326 unsigned NumProbes = 0;
327 while (1) {
328 --I;
329 if (I->getOffset() <= SLocOffset) {
330#if 0
331 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
332 I-SLocEntryTable.begin(),
333 I->isInstantiation() ? "inst" : "file",
334 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
335#endif
336 FileID Res = FileID::get(I-SLocEntryTable.begin());
337
338 // If this isn't an instantiation, remember it. We have good locality
339 // across FileID lookups.
340 if (!I->isInstantiation())
341 LastFileIDLookup = Res;
342 NumLinearScans += NumProbes+1;
343 return Res;
344 }
345 if (++NumProbes == 8)
346 break;
347 }
348
349 // Convert "I" back into an index. We know that it is an entry whose index is
350 // larger than the offset we are looking for.
351 unsigned GreaterIndex = I-SLocEntryTable.begin();
352 // LessIndex - This is the lower bound of the range that we're searching.
353 // We know that the offset corresponding to the FileID is is less than
354 // SLocOffset.
355 unsigned LessIndex = 0;
356 NumProbes = 0;
357 while (1) {
358 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
359 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
360
361 ++NumProbes;
362
363 // If the offset of the midpoint is too large, chop the high side of the
364 // range to the midpoint.
365 if (MidOffset > SLocOffset) {
366 GreaterIndex = MiddleIndex;
367 continue;
368 }
369
370 // If the middle index contains the value, succeed and return.
371 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
372#if 0
373 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
374 I-SLocEntryTable.begin(),
375 I->isInstantiation() ? "inst" : "file",
376 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
377#endif
378 FileID Res = FileID::get(MiddleIndex);
379
380 // If this isn't an instantiation, remember it. We have good locality
381 // across FileID lookups.
382 if (!I->isInstantiation())
383 LastFileIDLookup = Res;
384 NumBinaryProbes += NumProbes;
385 return Res;
386 }
387
388 // Otherwise, move the low-side up to the middle index.
389 LessIndex = MiddleIndex;
390 }
391}
392
Chris Lattneraddb7972009-01-26 20:04:19 +0000393SourceLocation SourceManager::
394getInstantiationLocSlowCase(SourceLocation Loc) const {
395 do {
396 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
397 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
398 Loc = Loc.getFileLocWithOffset(LocInfo.second);
399 } while (!Loc.isFileID());
400
401 return Loc;
402}
403
404SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
405 do {
406 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
407 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
408 Loc = Loc.getFileLocWithOffset(LocInfo.second);
409 } while (!Loc.isFileID());
410 return Loc;
411}
412
413
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000414std::pair<FileID, unsigned>
415SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
416 unsigned Offset) const {
417 // If this is an instantiation record, walk through all the instantiation
418 // points.
419 FileID FID;
420 SourceLocation Loc;
421 do {
422 Loc = E->getInstantiation().getInstantiationLoc();
423
424 FID = getFileID(Loc);
425 E = &getSLocEntry(FID);
426 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000427 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000428
429 return std::make_pair(FID, Offset);
430}
431
432std::pair<FileID, unsigned>
433SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
434 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000435 // If this is an instantiation record, walk through all the instantiation
436 // points.
437 FileID FID;
438 SourceLocation Loc;
439 do {
440 Loc = E->getInstantiation().getSpellingLoc();
441
442 FID = getFileID(Loc);
443 E = &getSLocEntry(FID);
444 Offset += Loc.getOffset()-E->getOffset();
445 } while (!Loc.isFileID());
446
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000447 return std::make_pair(FID, Offset);
448}
449
450
451//===----------------------------------------------------------------------===//
452// Queries about the code at a SourceLocation.
453//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000454
455/// getCharacterData - Return a pointer to the start of the specified location
456/// in the appropriate MemoryBuffer.
457const char *SourceManager::getCharacterData(SourceLocation SL) const {
458 // Note that this is a hot function in the getSpelling() path, which is
459 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000460 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000461
Ted Kremenekc16c2082009-01-06 01:55:26 +0000462 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000463 return getSLocEntry(LocInfo.first).getFile().getContentCache()
464 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467
Chris Lattner9dc1f532007-07-20 16:37:10 +0000468/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000469/// this is significantly cheaper to compute than the line number. This returns
470/// zero if the column number isn't known.
471unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000472 if (Loc.isInvalid()) return 0;
473 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Reid Spencer5f016e22007-07-11 17:01:13 +0000474
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000475 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000476 unsigned FilePos = LocInfo.second;
477
478 const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000479
480 unsigned LineStart = FilePos;
481 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
482 --LineStart;
483 return FilePos-LineStart+1;
484}
485
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000486static void ComputeLineNumbers(ContentCache* FI,
487 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
488static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000489 // Note that calling 'getBuffer()' may lazily page in the file.
490 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000491
492 // Find the file offsets of all of the *physical* source lines. This does
493 // not look at trigraphs, escaped newlines, or anything else tricky.
494 std::vector<unsigned> LineOffsets;
495
496 // Line #1 starts at char 0.
497 LineOffsets.push_back(0);
498
499 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
500 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
501 unsigned Offs = 0;
502 while (1) {
503 // Skip over the contents of the line.
504 // TODO: Vectorize this? This is very performance sensitive for programs
505 // with lots of diagnostics and in -E mode.
506 const unsigned char *NextBuf = (const unsigned char *)Buf;
507 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
508 ++NextBuf;
509 Offs += NextBuf-Buf;
510 Buf = NextBuf;
511
512 if (Buf[0] == '\n' || Buf[0] == '\r') {
513 // If this is \n\r or \r\n, skip both characters.
514 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
515 ++Offs, ++Buf;
516 ++Offs, ++Buf;
517 LineOffsets.push_back(Offs);
518 } else {
519 // Otherwise, this is a null. If end of file, exit.
520 if (Buf == End) break;
521 // Otherwise, skip the null.
522 ++Offs, ++Buf;
523 }
524 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000525
526 // Copy the offsets into the FileInfo structure.
527 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000528 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000529 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
530}
Reid Spencer5f016e22007-07-11 17:01:13 +0000531
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000532/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000533/// for the position indicated. This requires building and caching a table of
534/// line offsets for the MemoryBuffer, so this is not cheap: use only when
535/// about to emit a diagnostic.
Chris Lattnerf812a452008-11-18 06:51:15 +0000536unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000537 if (Loc.isInvalid()) return 0;
538 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Ted Kremenek78d85f52007-10-30 21:08:08 +0000539
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000540 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
541
Chris Lattner2b2453a2009-01-17 06:22:33 +0000542 ContentCache *Content;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000543 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000544 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000545 else
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000546 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
547 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000548
549 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000550 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000551 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000552 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
554 // Okay, we know we have a line number table. Do a binary search to find the
555 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000556 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000557 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000558 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000559
Chris Lattner2b2453a2009-01-17 06:22:33 +0000560 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000561
562 // If the previous query was to the same file, we know both the file pos from
563 // that query and the line number returned. This allows us to narrow the
564 // search space from the entire file to something near the match.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000565 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000566 if (QueriedFilePos >= LastLineNoFilePos) {
567 SourceLineCache = SourceLineCache+LastLineNoResult-1;
568
569 // The query is likely to be nearby the previous one. Here we check to
570 // see if it is within 5, 10 or 20 lines. It can be far away in cases
571 // where big comment blocks and vertical whitespace eat up lines but
572 // contribute no tokens.
573 if (SourceLineCache+5 < SourceLineCacheEnd) {
574 if (SourceLineCache[5] > QueriedFilePos)
575 SourceLineCacheEnd = SourceLineCache+5;
576 else if (SourceLineCache+10 < SourceLineCacheEnd) {
577 if (SourceLineCache[10] > QueriedFilePos)
578 SourceLineCacheEnd = SourceLineCache+10;
579 else if (SourceLineCache+20 < SourceLineCacheEnd) {
580 if (SourceLineCache[20] > QueriedFilePos)
581 SourceLineCacheEnd = SourceLineCache+20;
582 }
583 }
584 }
585 } else {
586 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
587 }
588 }
589
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000590 // If the spread is large, do a "radix" test as our initial guess, based on
591 // the assumption that lines average to approximately the same length.
592 // NOTE: This is currently disabled, as it does not appear to be profitable in
593 // initial measurements.
594 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000595 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000596
597 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000598 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000599
600 // Check for -10 and +10 lines.
601 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
602 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
603
604 // If the computed lower bound is less than the query location, move it in.
605 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
606 SourceLineCacheStart[LowerBound] < QueriedFilePos)
607 SourceLineCache = SourceLineCacheStart+LowerBound;
608
609 // If the computed upper bound is greater than the query location, move it.
610 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
611 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
612 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
613 }
614
615 unsigned *Pos
616 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000617 unsigned LineNo = Pos-SourceLineCacheStart;
618
Chris Lattner2b2453a2009-01-17 06:22:33 +0000619 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000620 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000621 LastLineNoFilePos = QueriedFilePos;
622 LastLineNoResult = LineNo;
623 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624}
625
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000626/// getPresumedLoc - This method returns the "presumed" location of a
627/// SourceLocation specifies. A "presumed location" can be modified by #line
628/// or GNU line marker directives. This provides a view on the data that a
629/// user should see in diagnostics, for example.
630///
631/// Note that a presumed location is always given as the instantiation point
632/// of an instantiation location, not at the spelling location.
633PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
634 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000635
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000636 // Presumed locations are always for instantiation points.
637 Loc = getInstantiationLoc(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000638
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000639 // FIXME: Could just decompose Loc once!
640
641 const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile();
642 const SrcMgr::ContentCache *C = FI.getContentCache();
643
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000644 // To get the source name, first consult the FileEntry (if one exists) before
645 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000646 const char *Filename =
647 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
648
649 return PresumedLoc(Filename, getLineNumber(Loc), getColumnNumber(Loc),
650 FI.getIncludeLoc());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000651}
652
653//===----------------------------------------------------------------------===//
654// Other miscellaneous methods.
655//===----------------------------------------------------------------------===//
656
657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658/// PrintStats - Print statistics to stderr.
659///
660void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000661 llvm::cerr << "\n*** Source Manager Stats:\n";
662 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000663 << " mem buffers mapped.\n";
664 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
665 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 unsigned NumLineNumsComputed = 0;
668 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000669 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
670 NumLineNumsComputed += I->second->SourceLineCache != 0;
671 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000673
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000674 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
675 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000676 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
677 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000678}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000679
680//===----------------------------------------------------------------------===//
681// Serialization.
682//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000683
684void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000685 S.FlushRecord();
686 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000687
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000688 if (Entry) {
689 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
690
691 if (Fname.isAbsolute())
692 S.EmitCStr(Fname.c_str());
693 else {
694 // Create an absolute path.
695 // FIXME: This will potentially contain ".." and "." in the path.
696 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
697 path.appendComponent(Fname.c_str());
698 S.EmitCStr(path.c_str());
699 }
700 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000701 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000702 const char* p = Buffer->getBufferStart();
703 const char* e = Buffer->getBufferEnd();
704
Ted Kremenek099b4742007-12-05 00:14:18 +0000705 S.EmitInt(e-p);
706
Ted Kremeneke21272f2007-12-04 19:39:02 +0000707 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000708 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000709 }
710
Ted Kremenek099b4742007-12-05 00:14:18 +0000711 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000712}
Ted Kremenek099b4742007-12-05 00:14:18 +0000713
714void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
715 SourceManager& SMgr,
716 FileManager* FMgr,
717 std::vector<char>& Buf) {
718 if (FMgr) {
719 llvm::SerializedPtrID PtrID = D.ReadPtrID();
720 D.ReadCStr(Buf,false);
721
722 // Create/fetch the FileEntry.
723 const char* start = &Buf[0];
724 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
725
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000726 // FIXME: Ideally we want a lazy materialization of the ContentCache
727 // anyway, because we don't want to read in source files unless this
728 // is absolutely needed.
729 if (!E)
730 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000731 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000732 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000733 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
734 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000735 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000736
737 // Register the ContextCache object with the deserializer.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000738 /* FIXME:
739 ContentCache *Entry
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000740 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000741 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000742 D.RegisterPtr(&Entry);
743
744 // Create the buffer.
745 unsigned Size = D.ReadInt();
746 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
747
748 // Read the contents of the buffer.
749 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
750 for (unsigned i = 0; i < Size ; ++i)
751 p[i] = D.ReadInt();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000752 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000753}
754
755void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000756 S.EnterBlock();
757 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000758 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000759
Ted Kremenek099b4742007-12-05 00:14:18 +0000760 // Emit: FileInfos. Just emit the file name.
761 S.EnterBlock();
762
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000763 // FIXME: Emit FileInfos.
764 //std::for_each(FileInfos.begin(), FileInfos.end(),
765 // S.MakeEmitter<ContentCache>());
Ted Kremenek099b4742007-12-05 00:14:18 +0000766
767 S.ExitBlock();
768
769 // Emit: MemBufferInfos
770 S.EnterBlock();
771
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000772 /* FIXME: EMIT.
Ted Kremenek099b4742007-12-05 00:14:18 +0000773 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
774 S.MakeEmitter<ContentCache>());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000775 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000776
777 S.ExitBlock();
778
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000779 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000780
781 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000782}
783
Ted Kremenek1f941002007-12-05 00:19:51 +0000784SourceManager*
Chris Lattner23b5dc62009-02-04 00:40:31 +0000785SourceManager::CreateAndRegister(llvm::Deserializer &D, FileManager &FMgr) {
Ted Kremenek1f941002007-12-05 00:19:51 +0000786 SourceManager *M = new SourceManager();
787 D.RegisterPtr(M);
788
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000789 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000790 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000791
Ted Kremenek099b4742007-12-05 00:14:18 +0000792 std::vector<char> Buf;
793
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000794 /*{ // FIXME Read: FileInfos.
Ted Kremenek099b4742007-12-05 00:14:18 +0000795 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
796 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000797 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000798 }*/
Ted Kremenek099b4742007-12-05 00:14:18 +0000799
800 { // Read: MemBufferInfos.
801 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
802 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000803 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000804 }
805
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000806 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000807
808 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000809}