blob: 9d91debc43f926c06fabb0875cb8f87ed55a3757 [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
Chris Lattner5b9a5042009-01-26 07:57:50 +0000115unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
116 // Look up the filename in the string table, returning the pre-existing value
117 // if it exists.
118 llvm::StringMapEntry<unsigned> &Entry =
119 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
120 if (Entry.getValue() != ~0U)
121 return Entry.getValue();
122
123 // Otherwise, assign this the next available ID.
124 Entry.setValue(FilenamesByID.size());
125 FilenamesByID.push_back(&Entry);
126 return FilenamesByID.size()-1;
127}
128
Chris Lattnerac50e342009-02-03 22:13:05 +0000129/// AddLineNote - Add a line note to the line table that indicates that there
130/// is a #line at the specified FID/Offset location which changes the presumed
131/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000132void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000133 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000134 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +0000135
Chris Lattner23b5dc62009-02-04 00:40:31 +0000136 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
137 "Adding line entries out of order!");
138 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID));
Chris Lattnerac50e342009-02-03 22:13:05 +0000139}
140
141
142
Chris Lattner5b9a5042009-01-26 07:57:50 +0000143/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
144///
145unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
146 if (LineTable == 0)
147 LineTable = new LineTableInfo();
148 return LineTable->getLineTableFilenameID(Ptr, Len);
149}
150
151
Chris Lattner4c4ea172009-02-03 21:52:55 +0000152/// AddLineNote - Add a line note to the line table for the FileID and offset
153/// specified by Loc. If FilenameID is -1, it is considered to be
154/// unspecified.
155void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
156 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000157 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000158
Chris Lattnerac50e342009-02-03 22:13:05 +0000159 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
160
161 // Remember that this file has #line directives now if it doesn't already.
162 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
163
164 if (LineTable == 0)
165 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000166 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000167}
168
169
Chris Lattner23b5dc62009-02-04 00:40:31 +0000170//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000171// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000172//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000173
Chris Lattner5b9a5042009-01-26 07:57:50 +0000174SourceManager::~SourceManager() {
175 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000176
177 // Delete FileEntry objects corresponding to content caches. Since the actual
178 // content cache objects are bump pointer allocated, we just have to run the
179 // dtors, but we call the deallocate method for completeness.
180 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
181 MemBufferInfos[i]->~ContentCache();
182 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
183 }
184 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
185 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
186 I->second->~ContentCache();
187 ContentCacheAlloc.Deallocate(I->second);
188 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000189}
190
191void SourceManager::clearIDTables() {
192 MainFileID = FileID();
193 SLocEntryTable.clear();
194 LastLineNoFileIDQuery = FileID();
195 LastLineNoContentCache = 0;
196 LastFileIDLookup = FileID();
197
198 if (LineTable)
199 LineTable->clear();
200
201 // Use up FileID #0 as an invalid instantiation.
202 NextOffset = 0;
203 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
204}
205
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000206/// getOrCreateContentCache - Create or return a cached ContentCache for the
207/// specified file.
208const ContentCache *
209SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000213 ContentCache *&Entry = FileInfos[FileEnt];
214 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
Chris Lattner00282d62009-02-03 07:41:46 +0000216 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
217 // so that FileInfo can use the low 3 bits of the pointer for its own
218 // nefarious purposes.
219 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
220 EntryAlign = std::max(8U, EntryAlign);
221 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000222 new (Entry) ContentCache(FileEnt);
223 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000224}
225
226
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000227/// createMemBufferContentCache - Create a new ContentCache for the specified
228/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000229const ContentCache*
230SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000231 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
232 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
233 // the pointer for its own nefarious purposes.
234 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
235 EntryAlign = std::max(8U, EntryAlign);
236 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000237 new (Entry) ContentCache();
238 MemBufferInfos.push_back(Entry);
239 Entry->setBuffer(Buffer);
240 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000241}
242
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000243//===----------------------------------------------------------------------===//
244// Methods to create new FileID's and instantiations.
245//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
Nico Weber48002c82008-09-29 00:25:48 +0000247/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000248/// include position. This works regardless of whether the ContentCache
249/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000250FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000251 SourceLocation IncludePos,
252 SrcMgr::CharacteristicKind FileCharacter) {
253 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
254 FileInfo::get(IncludePos, File,
255 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000256 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000257 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
258 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000259
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000260 // Set LastFileIDLookup to the newly created file. The next getFileID call is
261 // almost guaranteed to be from that file.
262 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000263}
264
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000265/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000266/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000267/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000268SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
269 SourceLocation InstantLoc,
270 unsigned TokLength) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000271 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
272 InstantiationInfo::get(InstantLoc,
273 SpellingLoc)));
274 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
275 NextOffset += TokLength+1;
276 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000277}
278
Chris Lattner31530ba2009-01-19 07:32:13 +0000279/// getBufferData - Return a pointer to the start and end of the source buffer
280/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000281std::pair<const char*, const char*>
282SourceManager::getBufferData(FileID FID) const {
283 const llvm::MemoryBuffer *Buf = getBuffer(FID);
284 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
285}
286
287
Chris Lattner23b5dc62009-02-04 00:40:31 +0000288//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000289// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000290//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000291
292/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
293/// method that is used for all SourceManager queries that start with a
294/// SourceLocation object. It is responsible for finding the entry in
295/// SLocEntryTable which contains the specified location.
296///
297FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
298 assert(SLocOffset && "Invalid FileID");
299
300 // After the first and second level caches, I see two common sorts of
301 // behavior: 1) a lot of searched FileID's are "near" the cached file location
302 // or are "near" the cached instantiation location. 2) others are just
303 // completely random and may be a very long way away.
304 //
305 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
306 // then we fall back to a less cache efficient, but more scalable, binary
307 // search to find the location.
308
309 // See if this is near the file point - worst case we start scanning from the
310 // most newly created FileID.
311 std::vector<SrcMgr::SLocEntry>::const_iterator I;
312
313 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
314 // Neither loc prunes our search.
315 I = SLocEntryTable.end();
316 } else {
317 // Perhaps it is near the file point.
318 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
319 }
320
321 // Find the FileID that contains this. "I" is an iterator that points to a
322 // FileID whose offset is known to be larger than SLocOffset.
323 unsigned NumProbes = 0;
324 while (1) {
325 --I;
326 if (I->getOffset() <= SLocOffset) {
327#if 0
328 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
329 I-SLocEntryTable.begin(),
330 I->isInstantiation() ? "inst" : "file",
331 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
332#endif
333 FileID Res = FileID::get(I-SLocEntryTable.begin());
334
335 // If this isn't an instantiation, remember it. We have good locality
336 // across FileID lookups.
337 if (!I->isInstantiation())
338 LastFileIDLookup = Res;
339 NumLinearScans += NumProbes+1;
340 return Res;
341 }
342 if (++NumProbes == 8)
343 break;
344 }
345
346 // Convert "I" back into an index. We know that it is an entry whose index is
347 // larger than the offset we are looking for.
348 unsigned GreaterIndex = I-SLocEntryTable.begin();
349 // LessIndex - This is the lower bound of the range that we're searching.
350 // We know that the offset corresponding to the FileID is is less than
351 // SLocOffset.
352 unsigned LessIndex = 0;
353 NumProbes = 0;
354 while (1) {
355 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
356 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
357
358 ++NumProbes;
359
360 // If the offset of the midpoint is too large, chop the high side of the
361 // range to the midpoint.
362 if (MidOffset > SLocOffset) {
363 GreaterIndex = MiddleIndex;
364 continue;
365 }
366
367 // If the middle index contains the value, succeed and return.
368 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
369#if 0
370 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
371 I-SLocEntryTable.begin(),
372 I->isInstantiation() ? "inst" : "file",
373 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
374#endif
375 FileID Res = FileID::get(MiddleIndex);
376
377 // If this isn't an instantiation, remember it. We have good locality
378 // across FileID lookups.
379 if (!I->isInstantiation())
380 LastFileIDLookup = Res;
381 NumBinaryProbes += NumProbes;
382 return Res;
383 }
384
385 // Otherwise, move the low-side up to the middle index.
386 LessIndex = MiddleIndex;
387 }
388}
389
Chris Lattneraddb7972009-01-26 20:04:19 +0000390SourceLocation SourceManager::
391getInstantiationLocSlowCase(SourceLocation Loc) const {
392 do {
393 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
394 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
395 Loc = Loc.getFileLocWithOffset(LocInfo.second);
396 } while (!Loc.isFileID());
397
398 return Loc;
399}
400
401SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
402 do {
403 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
404 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
405 Loc = Loc.getFileLocWithOffset(LocInfo.second);
406 } while (!Loc.isFileID());
407 return Loc;
408}
409
410
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000411std::pair<FileID, unsigned>
412SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
413 unsigned Offset) const {
414 // If this is an instantiation record, walk through all the instantiation
415 // points.
416 FileID FID;
417 SourceLocation Loc;
418 do {
419 Loc = E->getInstantiation().getInstantiationLoc();
420
421 FID = getFileID(Loc);
422 E = &getSLocEntry(FID);
423 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000424 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000425
426 return std::make_pair(FID, Offset);
427}
428
429std::pair<FileID, unsigned>
430SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
431 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000432 // If this is an instantiation record, walk through all the instantiation
433 // points.
434 FileID FID;
435 SourceLocation Loc;
436 do {
437 Loc = E->getInstantiation().getSpellingLoc();
438
439 FID = getFileID(Loc);
440 E = &getSLocEntry(FID);
441 Offset += Loc.getOffset()-E->getOffset();
442 } while (!Loc.isFileID());
443
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000444 return std::make_pair(FID, Offset);
445}
446
447
448//===----------------------------------------------------------------------===//
449// Queries about the code at a SourceLocation.
450//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000451
452/// getCharacterData - Return a pointer to the start of the specified location
453/// in the appropriate MemoryBuffer.
454const char *SourceManager::getCharacterData(SourceLocation SL) const {
455 // Note that this is a hot function in the getSpelling() path, which is
456 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000457 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000458
Ted Kremenekc16c2082009-01-06 01:55:26 +0000459 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000460 return getSLocEntry(LocInfo.first).getFile().getContentCache()
461 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000462}
463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464
Chris Lattner9dc1f532007-07-20 16:37:10 +0000465/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000466/// this is significantly cheaper to compute than the line number.
467unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
468 const char *Buf = getBuffer(FID)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 unsigned LineStart = FilePos;
471 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
472 --LineStart;
473 return FilePos-LineStart+1;
474}
475
Chris Lattner7da5aea2009-02-04 00:55:58 +0000476unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
477 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
478 return getColumnNumber(LocInfo.first, LocInfo.second);
479}
480
481unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
482 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
483 return getColumnNumber(LocInfo.first, LocInfo.second);
484}
485
486
487
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000488static void ComputeLineNumbers(ContentCache* FI,
489 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
490static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000491 // Note that calling 'getBuffer()' may lazily page in the file.
492 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000493
494 // Find the file offsets of all of the *physical* source lines. This does
495 // not look at trigraphs, escaped newlines, or anything else tricky.
496 std::vector<unsigned> LineOffsets;
497
498 // Line #1 starts at char 0.
499 LineOffsets.push_back(0);
500
501 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
502 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
503 unsigned Offs = 0;
504 while (1) {
505 // Skip over the contents of the line.
506 // TODO: Vectorize this? This is very performance sensitive for programs
507 // with lots of diagnostics and in -E mode.
508 const unsigned char *NextBuf = (const unsigned char *)Buf;
509 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
510 ++NextBuf;
511 Offs += NextBuf-Buf;
512 Buf = NextBuf;
513
514 if (Buf[0] == '\n' || Buf[0] == '\r') {
515 // If this is \n\r or \r\n, skip both characters.
516 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
517 ++Offs, ++Buf;
518 ++Offs, ++Buf;
519 LineOffsets.push_back(Offs);
520 } else {
521 // Otherwise, this is a null. If end of file, exit.
522 if (Buf == End) break;
523 // Otherwise, skip the null.
524 ++Offs, ++Buf;
525 }
526 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000527
528 // Copy the offsets into the FileInfo structure.
529 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000530 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000531 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
532}
Reid Spencer5f016e22007-07-11 17:01:13 +0000533
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000534/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000535/// for the position indicated. This requires building and caching a table of
536/// line offsets for the MemoryBuffer, so this is not cheap: use only when
537/// about to emit a diagnostic.
Chris Lattnerf812a452008-11-18 06:51:15 +0000538unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000539 if (Loc.isInvalid()) return 0;
540 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Ted Kremenek78d85f52007-10-30 21:08:08 +0000541
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000542 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
543
Chris Lattner2b2453a2009-01-17 06:22:33 +0000544 ContentCache *Content;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000545 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000546 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000547 else
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000548 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
549 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000550
551 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000552 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000553 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000554 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000555
556 // Okay, we know we have a line number table. Do a binary search to find the
557 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000558 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000559 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000560 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000561
Chris Lattner2b2453a2009-01-17 06:22:33 +0000562 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000563
564 // If the previous query was to the same file, we know both the file pos from
565 // that query and the line number returned. This allows us to narrow the
566 // search space from the entire file to something near the match.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000567 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000568 if (QueriedFilePos >= LastLineNoFilePos) {
569 SourceLineCache = SourceLineCache+LastLineNoResult-1;
570
571 // The query is likely to be nearby the previous one. Here we check to
572 // see if it is within 5, 10 or 20 lines. It can be far away in cases
573 // where big comment blocks and vertical whitespace eat up lines but
574 // contribute no tokens.
575 if (SourceLineCache+5 < SourceLineCacheEnd) {
576 if (SourceLineCache[5] > QueriedFilePos)
577 SourceLineCacheEnd = SourceLineCache+5;
578 else if (SourceLineCache+10 < SourceLineCacheEnd) {
579 if (SourceLineCache[10] > QueriedFilePos)
580 SourceLineCacheEnd = SourceLineCache+10;
581 else if (SourceLineCache+20 < SourceLineCacheEnd) {
582 if (SourceLineCache[20] > QueriedFilePos)
583 SourceLineCacheEnd = SourceLineCache+20;
584 }
585 }
586 }
587 } else {
588 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
589 }
590 }
591
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000592 // If the spread is large, do a "radix" test as our initial guess, based on
593 // the assumption that lines average to approximately the same length.
594 // NOTE: This is currently disabled, as it does not appear to be profitable in
595 // initial measurements.
596 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000597 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000598
599 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000600 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000601
602 // Check for -10 and +10 lines.
603 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
604 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
605
606 // If the computed lower bound is less than the query location, move it in.
607 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
608 SourceLineCacheStart[LowerBound] < QueriedFilePos)
609 SourceLineCache = SourceLineCacheStart+LowerBound;
610
611 // If the computed upper bound is greater than the query location, move it.
612 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
613 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
614 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
615 }
616
617 unsigned *Pos
618 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000619 unsigned LineNo = Pos-SourceLineCacheStart;
620
Chris Lattner2b2453a2009-01-17 06:22:33 +0000621 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000622 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000623 LastLineNoFilePos = QueriedFilePos;
624 LastLineNoResult = LineNo;
625 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000626}
627
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000628/// getPresumedLoc - This method returns the "presumed" location of a
629/// SourceLocation specifies. A "presumed location" can be modified by #line
630/// or GNU line marker directives. This provides a view on the data that a
631/// user should see in diagnostics, for example.
632///
633/// Note that a presumed location is always given as the instantiation point
634/// of an instantiation location, not at the spelling location.
635PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
636 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000637
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000638 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000639 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000640 Loc = getInstantiationLoc(Loc);
Chris Lattner7da5aea2009-02-04 00:55:58 +0000641
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000642 // FIXME: Could just decompose Loc once!
643
644 const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile();
645 const SrcMgr::ContentCache *C = FI.getContentCache();
646
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000647 // To get the source name, first consult the FileEntry (if one exists) before
648 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000649 const char *Filename =
650 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
651
Chris Lattner7da5aea2009-02-04 00:55:58 +0000652 return PresumedLoc(Filename, getLineNumber(Loc),
653 getColumnNumber(LocInfo.first, LocInfo.second),
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000654 FI.getIncludeLoc());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000655}
656
657//===----------------------------------------------------------------------===//
658// Other miscellaneous methods.
659//===----------------------------------------------------------------------===//
660
661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662/// PrintStats - Print statistics to stderr.
663///
664void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000665 llvm::cerr << "\n*** Source Manager Stats:\n";
666 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000667 << " mem buffers mapped.\n";
668 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
669 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 unsigned NumLineNumsComputed = 0;
672 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000673 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
674 NumLineNumsComputed += I->second->SourceLineCache != 0;
675 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000677
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000678 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
679 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000680 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
681 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000682}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000683
684//===----------------------------------------------------------------------===//
685// Serialization.
686//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000687
688void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000689 S.FlushRecord();
690 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000691
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000692 if (Entry) {
693 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
694
695 if (Fname.isAbsolute())
696 S.EmitCStr(Fname.c_str());
697 else {
698 // Create an absolute path.
699 // FIXME: This will potentially contain ".." and "." in the path.
700 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
701 path.appendComponent(Fname.c_str());
702 S.EmitCStr(path.c_str());
703 }
704 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000705 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000706 const char* p = Buffer->getBufferStart();
707 const char* e = Buffer->getBufferEnd();
708
Ted Kremenek099b4742007-12-05 00:14:18 +0000709 S.EmitInt(e-p);
710
Ted Kremeneke21272f2007-12-04 19:39:02 +0000711 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000712 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000713 }
714
Ted Kremenek099b4742007-12-05 00:14:18 +0000715 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000716}
Ted Kremenek099b4742007-12-05 00:14:18 +0000717
718void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
719 SourceManager& SMgr,
720 FileManager* FMgr,
721 std::vector<char>& Buf) {
722 if (FMgr) {
723 llvm::SerializedPtrID PtrID = D.ReadPtrID();
724 D.ReadCStr(Buf,false);
725
726 // Create/fetch the FileEntry.
727 const char* start = &Buf[0];
728 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
729
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000730 // FIXME: Ideally we want a lazy materialization of the ContentCache
731 // anyway, because we don't want to read in source files unless this
732 // is absolutely needed.
733 if (!E)
734 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000735 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000736 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000737 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
738 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000739 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000740
741 // Register the ContextCache object with the deserializer.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000742 /* FIXME:
743 ContentCache *Entry
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000744 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000745 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000746 D.RegisterPtr(&Entry);
747
748 // Create the buffer.
749 unsigned Size = D.ReadInt();
750 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
751
752 // Read the contents of the buffer.
753 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
754 for (unsigned i = 0; i < Size ; ++i)
755 p[i] = D.ReadInt();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000756 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000757}
758
759void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000760 S.EnterBlock();
761 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000762 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000763
Ted Kremenek099b4742007-12-05 00:14:18 +0000764 // Emit: FileInfos. Just emit the file name.
765 S.EnterBlock();
766
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000767 // FIXME: Emit FileInfos.
768 //std::for_each(FileInfos.begin(), FileInfos.end(),
769 // S.MakeEmitter<ContentCache>());
Ted Kremenek099b4742007-12-05 00:14:18 +0000770
771 S.ExitBlock();
772
773 // Emit: MemBufferInfos
774 S.EnterBlock();
775
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000776 /* FIXME: EMIT.
Ted Kremenek099b4742007-12-05 00:14:18 +0000777 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
778 S.MakeEmitter<ContentCache>());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000779 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000780
781 S.ExitBlock();
782
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000783 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000784
785 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000786}
787
Ted Kremenek1f941002007-12-05 00:19:51 +0000788SourceManager*
Chris Lattner23b5dc62009-02-04 00:40:31 +0000789SourceManager::CreateAndRegister(llvm::Deserializer &D, FileManager &FMgr) {
Ted Kremenek1f941002007-12-05 00:19:51 +0000790 SourceManager *M = new SourceManager();
791 D.RegisterPtr(M);
792
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000793 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000794 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000795
Ted Kremenek099b4742007-12-05 00:14:18 +0000796 std::vector<char> Buf;
797
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000798 /*{ // FIXME Read: FileInfos.
Ted Kremenek099b4742007-12-05 00:14:18 +0000799 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
800 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000801 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000802 }*/
Ted Kremenek099b4742007-12-05 00:14:18 +0000803
804 { // Read: MemBufferInfos.
805 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
806 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000807 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000808 }
809
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000810 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000811
812 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000813}