blob: 9ed216352590c0084cb4d957b1f90efd661dd874 [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"
Douglas Gregord4f77aa2009-04-13 15:31:25 +000015#include "clang/Basic/SourceManagerInternals.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/FileManager.h"
Chris Lattner5e36a7a2007-07-24 05:57:19 +000017#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/System/Path.h"
Ted Kremenek665dd4a2007-12-05 22:21:13 +000020#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include <algorithm>
Douglas Gregor370187c2009-04-22 21:45:53 +000022#include <iostream>
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 Lattner5b9a5042009-01-26 07:57:50 +000060unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
61 // Look up the filename in the string table, returning the pre-existing value
62 // if it exists.
63 llvm::StringMapEntry<unsigned> &Entry =
64 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
65 if (Entry.getValue() != ~0U)
66 return Entry.getValue();
67
68 // Otherwise, assign this the next available ID.
69 Entry.setValue(FilenamesByID.size());
70 FilenamesByID.push_back(&Entry);
71 return FilenamesByID.size()-1;
72}
73
Chris Lattnerac50e342009-02-03 22:13:05 +000074/// AddLineNote - Add a line note to the line table that indicates that there
75/// is a #line at the specified FID/Offset location which changes the presumed
76/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +000077void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +000078 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +000079 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +000080
Chris Lattner23b5dc62009-02-04 00:40:31 +000081 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
82 "Adding line entries out of order!");
Chris Lattner3cd949c2009-02-04 01:55:42 +000083
Chris Lattner9d79eba2009-02-04 05:21:58 +000084 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner137b6a62009-02-04 06:25:26 +000085 unsigned IncludeOffset = 0;
Chris Lattner3cd949c2009-02-04 01:55:42 +000086
Chris Lattner9d79eba2009-02-04 05:21:58 +000087 if (!Entries.empty()) {
88 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
89 // that we are still in "foo.h".
90 if (FilenameID == -1)
91 FilenameID = Entries.back().FilenameID;
92
Chris Lattner137b6a62009-02-04 06:25:26 +000093 // If we are after a line marker that switched us to system header mode, or
94 // that set #include information, preserve it.
Chris Lattner9d79eba2009-02-04 05:21:58 +000095 Kind = Entries.back().FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +000096 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +000097 }
98
Chris Lattner137b6a62009-02-04 06:25:26 +000099 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
100 IncludeOffset));
Chris Lattnerac50e342009-02-03 22:13:05 +0000101}
102
Chris Lattner9d79eba2009-02-04 05:21:58 +0000103/// AddLineNote This is the same as the previous version of AddLineNote, but is
104/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
105/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
106/// this is a file exit. FileKind specifies whether this is a system header or
107/// extern C system header.
108void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
109 unsigned LineNo, int FilenameID,
110 unsigned EntryExit,
111 SrcMgr::CharacteristicKind FileKind) {
112 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
113
114 std::vector<LineEntry> &Entries = LineEntries[FID];
115
116 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
117 "Adding line entries out of order!");
118
Chris Lattner137b6a62009-02-04 06:25:26 +0000119 unsigned IncludeOffset = 0;
120 if (EntryExit == 0) { // No #include stack change.
121 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
122 } else if (EntryExit == 1) {
123 IncludeOffset = Offset-1;
124 } else if (EntryExit == 2) {
125 assert(!Entries.empty() && Entries.back().IncludeOffset &&
126 "PPDirectives should have caught case when popping empty include stack");
127
128 // Get the include loc of the last entries' include loc as our include loc.
129 IncludeOffset = 0;
130 if (const LineEntry *PrevEntry =
131 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
132 IncludeOffset = PrevEntry->IncludeOffset;
133 }
Chris Lattner9d79eba2009-02-04 05:21:58 +0000134
Chris Lattner137b6a62009-02-04 06:25:26 +0000135 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
136 IncludeOffset));
Chris Lattner9d79eba2009-02-04 05:21:58 +0000137}
138
139
Chris Lattner3cd949c2009-02-04 01:55:42 +0000140/// FindNearestLineEntry - Find the line entry nearest to FID that is before
141/// it. If there is no line entry before Offset in FID, return null.
142const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
143 unsigned Offset) {
144 const std::vector<LineEntry> &Entries = LineEntries[FID];
145 assert(!Entries.empty() && "No #line entries for this FID after all!");
146
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000147 // It is very common for the query to be after the last #line, check this
148 // first.
149 if (Entries.back().FileOffset <= Offset)
150 return &Entries.back();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000151
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000152 // Do a binary search to find the maximal element that is still before Offset.
153 std::vector<LineEntry>::const_iterator I =
154 std::upper_bound(Entries.begin(), Entries.end(), Offset);
155 if (I == Entries.begin()) return 0;
156 return &*--I;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000157}
Chris Lattnerac50e342009-02-03 22:13:05 +0000158
Douglas Gregorbd945002009-04-13 16:31:14 +0000159/// \brief Add a new line entry that has already been encoded into
160/// the internal representation of the line table.
161void LineTableInfo::AddEntry(unsigned FID,
162 const std::vector<LineEntry> &Entries) {
163 LineEntries[FID] = Entries;
164}
Chris Lattnerac50e342009-02-03 22:13:05 +0000165
Chris Lattner5b9a5042009-01-26 07:57:50 +0000166/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
167///
168unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
169 if (LineTable == 0)
170 LineTable = new LineTableInfo();
171 return LineTable->getLineTableFilenameID(Ptr, Len);
172}
173
174
Chris Lattner4c4ea172009-02-03 21:52:55 +0000175/// AddLineNote - Add a line note to the line table for the FileID and offset
176/// specified by Loc. If FilenameID is -1, it is considered to be
177/// unspecified.
178void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
179 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000180 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000181
Chris Lattnerac50e342009-02-03 22:13:05 +0000182 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
183
184 // Remember that this file has #line directives now if it doesn't already.
185 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
186
187 if (LineTable == 0)
188 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000189 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000190}
191
Chris Lattner9d79eba2009-02-04 05:21:58 +0000192/// AddLineNote - Add a GNU line marker to the line table.
193void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
194 int FilenameID, bool IsFileEntry,
195 bool IsFileExit, bool IsSystemHeader,
196 bool IsExternCHeader) {
197 // If there is no filename and no flags, this is treated just like a #line,
198 // which does not change the flags of the previous line marker.
199 if (FilenameID == -1) {
200 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
201 "Can't set flags without setting the filename!");
202 return AddLineNote(Loc, LineNo, FilenameID);
203 }
204
205 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
206 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
207
208 // Remember that this file has #line directives now if it doesn't already.
209 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
210
211 if (LineTable == 0)
212 LineTable = new LineTableInfo();
213
214 SrcMgr::CharacteristicKind FileKind;
215 if (IsExternCHeader)
216 FileKind = SrcMgr::C_ExternCSystem;
217 else if (IsSystemHeader)
218 FileKind = SrcMgr::C_System;
219 else
220 FileKind = SrcMgr::C_User;
221
222 unsigned EntryExit = 0;
223 if (IsFileEntry)
224 EntryExit = 1;
225 else if (IsFileExit)
226 EntryExit = 2;
227
228 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
229 EntryExit, FileKind);
230}
231
Douglas Gregorbd945002009-04-13 16:31:14 +0000232LineTableInfo &SourceManager::getLineTable() {
233 if (LineTable == 0)
234 LineTable = new LineTableInfo();
235 return *LineTable;
236}
Chris Lattner4c4ea172009-02-03 21:52:55 +0000237
Chris Lattner23b5dc62009-02-04 00:40:31 +0000238//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000239// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000240//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000241
Chris Lattner5b9a5042009-01-26 07:57:50 +0000242SourceManager::~SourceManager() {
243 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000244
245 // Delete FileEntry objects corresponding to content caches. Since the actual
246 // content cache objects are bump pointer allocated, we just have to run the
247 // dtors, but we call the deallocate method for completeness.
248 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
249 MemBufferInfos[i]->~ContentCache();
250 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
251 }
252 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
253 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
254 I->second->~ContentCache();
255 ContentCacheAlloc.Deallocate(I->second);
256 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000257}
258
259void SourceManager::clearIDTables() {
260 MainFileID = FileID();
261 SLocEntryTable.clear();
262 LastLineNoFileIDQuery = FileID();
263 LastLineNoContentCache = 0;
264 LastFileIDLookup = FileID();
265
266 if (LineTable)
267 LineTable->clear();
268
269 // Use up FileID #0 as an invalid instantiation.
270 NextOffset = 0;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000271 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000272}
273
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000274/// getOrCreateContentCache - Create or return a cached ContentCache for the
275/// specified file.
276const ContentCache *
277SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000281 ContentCache *&Entry = FileInfos[FileEnt];
282 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
Chris Lattner00282d62009-02-03 07:41:46 +0000284 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
285 // so that FileInfo can use the low 3 bits of the pointer for its own
286 // nefarious purposes.
287 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
288 EntryAlign = std::max(8U, EntryAlign);
289 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000290 new (Entry) ContentCache(FileEnt);
291 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000292}
293
294
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000295/// createMemBufferContentCache - Create a new ContentCache for the specified
296/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000297const ContentCache*
298SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000299 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
300 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
301 // the pointer for its own nefarious purposes.
302 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
303 EntryAlign = std::max(8U, EntryAlign);
304 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000305 new (Entry) ContentCache();
306 MemBufferInfos.push_back(Entry);
307 Entry->setBuffer(Buffer);
308 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000309}
310
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000311//===----------------------------------------------------------------------===//
312// Methods to create new FileID's and instantiations.
313//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000314
Nico Weber48002c82008-09-29 00:25:48 +0000315/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000316/// include position. This works regardless of whether the ContentCache
317/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000318FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000319 SourceLocation IncludePos,
320 SrcMgr::CharacteristicKind FileCharacter) {
321 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
322 FileInfo::get(IncludePos, File,
323 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000324 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000325 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
326 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000327
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000328 // Set LastFileIDLookup to the newly created file. The next getFileID call is
329 // almost guaranteed to be from that file.
330 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000331}
332
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000333/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000334/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000335/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000336SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000337 SourceLocation ILocStart,
338 SourceLocation ILocEnd,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000339 unsigned TokLength) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000340 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
341 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000342 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
343 NextOffset += TokLength+1;
344 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000345}
346
Chris Lattner31530ba2009-01-19 07:32:13 +0000347/// getBufferData - Return a pointer to the start and end of the source buffer
348/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000349std::pair<const char*, const char*>
350SourceManager::getBufferData(FileID FID) const {
351 const llvm::MemoryBuffer *Buf = getBuffer(FID);
352 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
353}
354
355
Chris Lattner23b5dc62009-02-04 00:40:31 +0000356//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000357// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000358//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000359
360/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
361/// method that is used for all SourceManager queries that start with a
362/// SourceLocation object. It is responsible for finding the entry in
363/// SLocEntryTable which contains the specified location.
364///
365FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
366 assert(SLocOffset && "Invalid FileID");
367
368 // After the first and second level caches, I see two common sorts of
369 // behavior: 1) a lot of searched FileID's are "near" the cached file location
370 // or are "near" the cached instantiation location. 2) others are just
371 // completely random and may be a very long way away.
372 //
373 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
374 // then we fall back to a less cache efficient, but more scalable, binary
375 // search to find the location.
376
377 // See if this is near the file point - worst case we start scanning from the
378 // most newly created FileID.
379 std::vector<SrcMgr::SLocEntry>::const_iterator I;
380
381 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
382 // Neither loc prunes our search.
383 I = SLocEntryTable.end();
384 } else {
385 // Perhaps it is near the file point.
386 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
387 }
388
389 // Find the FileID that contains this. "I" is an iterator that points to a
390 // FileID whose offset is known to be larger than SLocOffset.
391 unsigned NumProbes = 0;
392 while (1) {
393 --I;
394 if (I->getOffset() <= SLocOffset) {
395#if 0
396 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
397 I-SLocEntryTable.begin(),
398 I->isInstantiation() ? "inst" : "file",
399 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
400#endif
401 FileID Res = FileID::get(I-SLocEntryTable.begin());
402
403 // If this isn't an instantiation, remember it. We have good locality
404 // across FileID lookups.
405 if (!I->isInstantiation())
406 LastFileIDLookup = Res;
407 NumLinearScans += NumProbes+1;
408 return Res;
409 }
410 if (++NumProbes == 8)
411 break;
412 }
413
414 // Convert "I" back into an index. We know that it is an entry whose index is
415 // larger than the offset we are looking for.
416 unsigned GreaterIndex = I-SLocEntryTable.begin();
417 // LessIndex - This is the lower bound of the range that we're searching.
418 // We know that the offset corresponding to the FileID is is less than
419 // SLocOffset.
420 unsigned LessIndex = 0;
421 NumProbes = 0;
422 while (1) {
423 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
424 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
425
426 ++NumProbes;
427
428 // If the offset of the midpoint is too large, chop the high side of the
429 // range to the midpoint.
430 if (MidOffset > SLocOffset) {
431 GreaterIndex = MiddleIndex;
432 continue;
433 }
434
435 // If the middle index contains the value, succeed and return.
436 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
437#if 0
438 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
439 I-SLocEntryTable.begin(),
440 I->isInstantiation() ? "inst" : "file",
441 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
442#endif
443 FileID Res = FileID::get(MiddleIndex);
444
445 // If this isn't an instantiation, remember it. We have good locality
446 // across FileID lookups.
447 if (!I->isInstantiation())
448 LastFileIDLookup = Res;
449 NumBinaryProbes += NumProbes;
450 return Res;
451 }
452
453 // Otherwise, move the low-side up to the middle index.
454 LessIndex = MiddleIndex;
455 }
456}
457
Chris Lattneraddb7972009-01-26 20:04:19 +0000458SourceLocation SourceManager::
459getInstantiationLocSlowCase(SourceLocation Loc) const {
460 do {
461 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000462 Loc = getSLocEntry(LocInfo.first).getInstantiation()
463 .getInstantiationLocStart();
Chris Lattneraddb7972009-01-26 20:04:19 +0000464 Loc = Loc.getFileLocWithOffset(LocInfo.second);
465 } while (!Loc.isFileID());
466
467 return Loc;
468}
469
470SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
471 do {
472 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
473 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
474 Loc = Loc.getFileLocWithOffset(LocInfo.second);
475 } while (!Loc.isFileID());
476 return Loc;
477}
478
479
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000480std::pair<FileID, unsigned>
481SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
482 unsigned Offset) const {
483 // If this is an instantiation record, walk through all the instantiation
484 // points.
485 FileID FID;
486 SourceLocation Loc;
487 do {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000488 Loc = E->getInstantiation().getInstantiationLocStart();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000489
490 FID = getFileID(Loc);
491 E = &getSLocEntry(FID);
492 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000493 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000494
495 return std::make_pair(FID, Offset);
496}
497
498std::pair<FileID, unsigned>
499SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
500 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000501 // If this is an instantiation record, walk through all the instantiation
502 // points.
503 FileID FID;
504 SourceLocation Loc;
505 do {
506 Loc = E->getInstantiation().getSpellingLoc();
507
508 FID = getFileID(Loc);
509 E = &getSLocEntry(FID);
510 Offset += Loc.getOffset()-E->getOffset();
511 } while (!Loc.isFileID());
512
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000513 return std::make_pair(FID, Offset);
514}
515
Chris Lattner387616e2009-02-17 08:04:48 +0000516/// getImmediateSpellingLoc - Given a SourceLocation object, return the
517/// spelling location referenced by the ID. This is the first level down
518/// towards the place where the characters that make up the lexed token can be
519/// found. This should not generally be used by clients.
520SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
521 if (Loc.isFileID()) return Loc;
522 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
523 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
524 return Loc.getFileLocWithOffset(LocInfo.second);
525}
526
527
Chris Lattnere7fb4842009-02-15 20:52:18 +0000528/// getImmediateInstantiationRange - Loc is required to be an instantiation
529/// location. Return the start/end of the instantiation information.
530std::pair<SourceLocation,SourceLocation>
531SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
532 assert(Loc.isMacroID() && "Not an instantiation loc!");
533 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
534 return II.getInstantiationLocRange();
535}
536
Chris Lattner66781332009-02-15 21:26:50 +0000537/// getInstantiationRange - Given a SourceLocation object, return the
538/// range of tokens covered by the instantiation in the ultimate file.
539std::pair<SourceLocation,SourceLocation>
540SourceManager::getInstantiationRange(SourceLocation Loc) const {
541 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
542
543 std::pair<SourceLocation,SourceLocation> Res =
544 getImmediateInstantiationRange(Loc);
545
546 // Fully resolve the start and end locations to their ultimate instantiation
547 // points.
548 while (!Res.first.isFileID())
549 Res.first = getImmediateInstantiationRange(Res.first).first;
550 while (!Res.second.isFileID())
551 Res.second = getImmediateInstantiationRange(Res.second).second;
552 return Res;
553}
554
Chris Lattnere7fb4842009-02-15 20:52:18 +0000555
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000556
557//===----------------------------------------------------------------------===//
558// Queries about the code at a SourceLocation.
559//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000560
561/// getCharacterData - Return a pointer to the start of the specified location
562/// in the appropriate MemoryBuffer.
563const char *SourceManager::getCharacterData(SourceLocation SL) const {
564 // Note that this is a hot function in the getSpelling() path, which is
565 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000566 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000567
Ted Kremenekc16c2082009-01-06 01:55:26 +0000568 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000569 return getSLocEntry(LocInfo.first).getFile().getContentCache()
570 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000571}
572
Reid Spencer5f016e22007-07-11 17:01:13 +0000573
Chris Lattner9dc1f532007-07-20 16:37:10 +0000574/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000575/// this is significantly cheaper to compute than the line number.
576unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
577 const char *Buf = getBuffer(FID)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 unsigned LineStart = FilePos;
580 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
581 --LineStart;
582 return FilePos-LineStart+1;
583}
584
Chris Lattner7da5aea2009-02-04 00:55:58 +0000585unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000586 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000587 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
588 return getColumnNumber(LocInfo.first, LocInfo.second);
589}
590
591unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000592 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000593 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
594 return getColumnNumber(LocInfo.first, LocInfo.second);
595}
596
597
598
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000599static void ComputeLineNumbers(ContentCache* FI,
600 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
601static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000602 // Note that calling 'getBuffer()' may lazily page in the file.
603 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000604
605 // Find the file offsets of all of the *physical* source lines. This does
606 // not look at trigraphs, escaped newlines, or anything else tricky.
607 std::vector<unsigned> LineOffsets;
608
609 // Line #1 starts at char 0.
610 LineOffsets.push_back(0);
611
612 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
613 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
614 unsigned Offs = 0;
615 while (1) {
616 // Skip over the contents of the line.
617 // TODO: Vectorize this? This is very performance sensitive for programs
618 // with lots of diagnostics and in -E mode.
619 const unsigned char *NextBuf = (const unsigned char *)Buf;
620 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
621 ++NextBuf;
622 Offs += NextBuf-Buf;
623 Buf = NextBuf;
624
625 if (Buf[0] == '\n' || Buf[0] == '\r') {
626 // If this is \n\r or \r\n, skip both characters.
627 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
628 ++Offs, ++Buf;
629 ++Offs, ++Buf;
630 LineOffsets.push_back(Offs);
631 } else {
632 // Otherwise, this is a null. If end of file, exit.
633 if (Buf == End) break;
634 // Otherwise, skip the null.
635 ++Offs, ++Buf;
636 }
637 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000638
639 // Copy the offsets into the FileInfo structure.
640 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000641 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000642 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
643}
Reid Spencer5f016e22007-07-11 17:01:13 +0000644
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000645/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000646/// for the position indicated. This requires building and caching a table of
647/// line offsets for the MemoryBuffer, so this is not cheap: use only when
648/// about to emit a diagnostic.
Chris Lattner30fc9332009-02-04 01:06:56 +0000649unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000650 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000651 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000652 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000653 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000654 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000655 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000656
657 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000658 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000659 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000660 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
662 // Okay, we know we have a line number table. Do a binary search to find the
663 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000664 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000665 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000666 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000667
Chris Lattner30fc9332009-02-04 01:06:56 +0000668 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000669
670 // If the previous query was to the same file, we know both the file pos from
671 // that query and the line number returned. This allows us to narrow the
672 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000673 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000674 if (QueriedFilePos >= LastLineNoFilePos) {
675 SourceLineCache = SourceLineCache+LastLineNoResult-1;
676
677 // The query is likely to be nearby the previous one. Here we check to
678 // see if it is within 5, 10 or 20 lines. It can be far away in cases
679 // where big comment blocks and vertical whitespace eat up lines but
680 // contribute no tokens.
681 if (SourceLineCache+5 < SourceLineCacheEnd) {
682 if (SourceLineCache[5] > QueriedFilePos)
683 SourceLineCacheEnd = SourceLineCache+5;
684 else if (SourceLineCache+10 < SourceLineCacheEnd) {
685 if (SourceLineCache[10] > QueriedFilePos)
686 SourceLineCacheEnd = SourceLineCache+10;
687 else if (SourceLineCache+20 < SourceLineCacheEnd) {
688 if (SourceLineCache[20] > QueriedFilePos)
689 SourceLineCacheEnd = SourceLineCache+20;
690 }
691 }
692 }
693 } else {
694 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
695 }
696 }
697
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000698 // If the spread is large, do a "radix" test as our initial guess, based on
699 // the assumption that lines average to approximately the same length.
700 // NOTE: This is currently disabled, as it does not appear to be profitable in
701 // initial measurements.
702 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000703 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000704
705 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000706 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000707
708 // Check for -10 and +10 lines.
709 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
710 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
711
712 // If the computed lower bound is less than the query location, move it in.
713 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
714 SourceLineCacheStart[LowerBound] < QueriedFilePos)
715 SourceLineCache = SourceLineCacheStart+LowerBound;
716
717 // If the computed upper bound is greater than the query location, move it.
718 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
719 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
720 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
721 }
722
723 unsigned *Pos
724 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000725 unsigned LineNo = Pos-SourceLineCacheStart;
726
Chris Lattner30fc9332009-02-04 01:06:56 +0000727 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000728 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000729 LastLineNoFilePos = QueriedFilePos;
730 LastLineNoResult = LineNo;
731 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000732}
733
Chris Lattner30fc9332009-02-04 01:06:56 +0000734unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
735 if (Loc.isInvalid()) return 0;
736 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
737 return getLineNumber(LocInfo.first, LocInfo.second);
738}
739unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
740 if (Loc.isInvalid()) return 0;
741 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
742 return getLineNumber(LocInfo.first, LocInfo.second);
743}
744
Chris Lattner6b306672009-02-04 05:33:01 +0000745/// getFileCharacteristic - return the file characteristic of the specified
746/// source location, indicating whether this is a normal file, a system
747/// header, or an "implicit extern C" system header.
748///
749/// This state can be modified with flags on GNU linemarker directives like:
750/// # 4 "foo.h" 3
751/// which changes all source locations in the current file after that to be
752/// considered to be from a system header.
753SrcMgr::CharacteristicKind
754SourceManager::getFileCharacteristic(SourceLocation Loc) const {
755 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
756 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
757 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
758
759 // If there are no #line directives in this file, just return the whole-file
760 // state.
761 if (!FI.hasLineDirectives())
762 return FI.getFileCharacteristic();
763
764 assert(LineTable && "Can't have linetable entries without a LineTable!");
765 // See if there is a #line directive before the location.
766 const LineEntry *Entry =
767 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
768
769 // If this is before the first line marker, use the file characteristic.
770 if (!Entry)
771 return FI.getFileCharacteristic();
772
773 return Entry->FileKind;
774}
775
Chris Lattnerbff5c512009-02-17 08:39:06 +0000776/// Return the filename or buffer identifier of the buffer the location is in.
777/// Note that this name does not respect #line directives. Use getPresumedLoc
778/// for normal clients.
779const char *SourceManager::getBufferName(SourceLocation Loc) const {
780 if (Loc.isInvalid()) return "<invalid loc>";
781
782 return getBuffer(getFileID(Loc))->getBufferIdentifier();
783}
784
Chris Lattner30fc9332009-02-04 01:06:56 +0000785
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000786/// getPresumedLoc - This method returns the "presumed" location of a
787/// SourceLocation specifies. A "presumed location" can be modified by #line
788/// or GNU line marker directives. This provides a view on the data that a
789/// user should see in diagnostics, for example.
790///
791/// Note that a presumed location is always given as the instantiation point
792/// of an instantiation location, not at the spelling location.
793PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
794 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000795
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000796 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000797 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000798
Chris Lattner30fc9332009-02-04 01:06:56 +0000799 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000800 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000801
802 // To get the source name, first consult the FileEntry (if one exists)
803 // before the MemBuffer as this will avoid unnecessarily paging in the
804 // MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000805 const char *Filename =
806 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000807 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
808 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
809 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000810
Chris Lattner3cd949c2009-02-04 01:55:42 +0000811 // If we have #line directives in this file, update and overwrite the physical
812 // location info if appropriate.
813 if (FI.hasLineDirectives()) {
814 assert(LineTable && "Can't have linetable entries without a LineTable!");
815 // See if there is a #line directive before this. If so, get it.
816 if (const LineEntry *Entry =
817 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +0000818 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000819 if (Entry->FilenameID != -1)
820 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +0000821
822 // Use the line number specified by the LineEntry. This line number may
823 // be multiple lines down from the line entry. Add the difference in
824 // physical line numbers from the query point and the line marker to the
825 // total.
826 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
827 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
828
Chris Lattner0e0e5da2009-02-04 02:15:40 +0000829 // Note that column numbers are not molested by line markers.
Chris Lattner137b6a62009-02-04 06:25:26 +0000830
831 // Handle virtual #include manipulation.
832 if (Entry->IncludeOffset) {
833 IncludeLoc = getLocForStartOfFile(LocInfo.first);
834 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
835 }
Chris Lattner3cd949c2009-02-04 01:55:42 +0000836 }
837 }
838
839 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000840}
841
842//===----------------------------------------------------------------------===//
843// Other miscellaneous methods.
844//===----------------------------------------------------------------------===//
845
846
Reid Spencer5f016e22007-07-11 17:01:13 +0000847/// PrintStats - Print statistics to stderr.
848///
849void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000850 llvm::cerr << "\n*** Source Manager Stats:\n";
851 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000852 << " mem buffers mapped.\n";
853 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
854 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 unsigned NumLineNumsComputed = 0;
857 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000858 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
859 NumLineNumsComputed += I->second->SourceLineCache != 0;
860 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000862
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000863 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
864 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000865 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
866 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000867}