blob: 0c22de7bddb1a8bab3bc578cc841ea61ad150156 [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"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000019#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/System/Path.h"
21#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23using namespace SrcMgr;
24using llvm::MemoryBuffer;
25
Chris Lattner23b5dc62009-02-04 00:40:31 +000026//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000027// SourceManager Helper Classes
Chris Lattner23b5dc62009-02-04 00:40:31 +000028//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000029
Ted Kremenek78d85f52007-10-30 21:08:08 +000030ContentCache::~ContentCache() {
31 delete Buffer;
Reid Spencer5f016e22007-07-11 17:01:13 +000032}
33
Ted Kremenekc16c2082009-01-06 01:55:26 +000034/// getSizeBytesMapped - Returns the number of bytes actually mapped for
35/// this ContentCache. This can be 0 if the MemBuffer was not actually
36/// instantiated.
37unsigned ContentCache::getSizeBytesMapped() const {
38 return Buffer ? Buffer->getBufferSize() : 0;
39}
40
41/// getSize - Returns the size of the content encapsulated by this ContentCache.
42/// This can be the size of the source file or the size of an arbitrary
43/// scratch buffer. If the ContentCache encapsulates a source file, that
Douglas Gregor29684422009-12-02 06:49:09 +000044/// file is not lazily brought in from disk to satisfy this query.
Ted Kremenekc16c2082009-01-06 01:55:26 +000045unsigned ContentCache::getSize() const {
Douglas Gregorb657f112009-09-22 21:11:38 +000046 return Buffer ? Buffer->getBufferSize() : Entry->getSize();
Ted Kremenekc16c2082009-01-06 01:55:26 +000047}
48
Douglas Gregor29684422009-12-02 06:49:09 +000049void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B) {
Douglas Gregor109ae732009-12-03 17:05:59 +000050 assert(B != Buffer);
Douglas Gregor29684422009-12-02 06:49:09 +000051
52 delete Buffer;
53 Buffer = B;
54}
55
Chris Lattner39d98412009-12-01 22:52:33 +000056const llvm::MemoryBuffer *ContentCache::getBuffer(std::string *ErrorStr) const {
Ted Kremenek5b034ad2009-01-06 22:43:04 +000057 // Lazily create the Buffer for ContentCaches that wrap files.
58 if (!Buffer && Entry) {
Chris Lattner39d98412009-12-01 22:52:33 +000059 Buffer = MemoryBuffer::getFile(Entry->getName(), ErrorStr,Entry->getSize());
Daniel Dunbar21a8bed2009-12-06 05:43:36 +000060
61 // If we were unable to open the file, then we are in an inconsistent
62 // situation where the content cache referenced a file which no longer
63 // exists. Most likely, we were using a stat cache with an invalid entry but
64 // the file could also have been removed during processing. Since we can't
65 // really deal with this situation, just create an empty buffer.
66 //
67 // FIXME: This is definitely not ideal, but our immediate clients can't
68 // currently handle returning a null entry here. Ideally we should detect
69 // that we are in an inconsistent situation and error out as quickly as
70 // possible.
71 if (!Buffer) {
72 const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
73 Buffer = MemoryBuffer::getNewMemBuffer(Entry->getSize(), "<invalid>");
74 char *Ptr = const_cast<char*>(Buffer->getBufferStart());
75 for (unsigned i = 0, e = Entry->getSize(); i != e; ++i)
76 Ptr[i] = FillStr[i % FillStr.size()];
77 }
Ted Kremenek5b034ad2009-01-06 22:43:04 +000078 }
Ted Kremenekc16c2082009-01-06 01:55:26 +000079 return Buffer;
80}
81
Chris Lattner5b9a5042009-01-26 07:57:50 +000082unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
83 // Look up the filename in the string table, returning the pre-existing value
84 // if it exists.
Mike Stump1eb44332009-09-09 15:08:12 +000085 llvm::StringMapEntry<unsigned> &Entry =
Chris Lattner5b9a5042009-01-26 07:57:50 +000086 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
87 if (Entry.getValue() != ~0U)
88 return Entry.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner5b9a5042009-01-26 07:57:50 +000090 // Otherwise, assign this the next available ID.
91 Entry.setValue(FilenamesByID.size());
92 FilenamesByID.push_back(&Entry);
93 return FilenamesByID.size()-1;
94}
95
Chris Lattnerac50e342009-02-03 22:13:05 +000096/// AddLineNote - Add a line note to the line table that indicates that there
97/// is a #line at the specified FID/Offset location which changes the presumed
98/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +000099void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000100 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000101 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner23b5dc62009-02-04 00:40:31 +0000103 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
104 "Adding line entries out of order!");
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattner9d79eba2009-02-04 05:21:58 +0000106 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner137b6a62009-02-04 06:25:26 +0000107 unsigned IncludeOffset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner9d79eba2009-02-04 05:21:58 +0000109 if (!Entries.empty()) {
110 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
111 // that we are still in "foo.h".
112 if (FilenameID == -1)
113 FilenameID = Entries.back().FilenameID;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner137b6a62009-02-04 06:25:26 +0000115 // If we are after a line marker that switched us to system header mode, or
116 // that set #include information, preserve it.
Chris Lattner9d79eba2009-02-04 05:21:58 +0000117 Kind = Entries.back().FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +0000118 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chris Lattner137b6a62009-02-04 06:25:26 +0000121 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
122 IncludeOffset));
Chris Lattnerac50e342009-02-03 22:13:05 +0000123}
124
Chris Lattner9d79eba2009-02-04 05:21:58 +0000125/// AddLineNote This is the same as the previous version of AddLineNote, but is
126/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
127/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
128/// this is a file exit. FileKind specifies whether this is a system header or
129/// extern C system header.
130void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
131 unsigned LineNo, int FilenameID,
132 unsigned EntryExit,
133 SrcMgr::CharacteristicKind FileKind) {
134 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattner9d79eba2009-02-04 05:21:58 +0000136 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattner9d79eba2009-02-04 05:21:58 +0000138 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
139 "Adding line entries out of order!");
140
Chris Lattner137b6a62009-02-04 06:25:26 +0000141 unsigned IncludeOffset = 0;
142 if (EntryExit == 0) { // No #include stack change.
143 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
144 } else if (EntryExit == 1) {
145 IncludeOffset = Offset-1;
146 } else if (EntryExit == 2) {
147 assert(!Entries.empty() && Entries.back().IncludeOffset &&
148 "PPDirectives should have caught case when popping empty include stack");
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Chris Lattner137b6a62009-02-04 06:25:26 +0000150 // Get the include loc of the last entries' include loc as our include loc.
151 IncludeOffset = 0;
152 if (const LineEntry *PrevEntry =
153 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
154 IncludeOffset = PrevEntry->IncludeOffset;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattner137b6a62009-02-04 06:25:26 +0000157 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
158 IncludeOffset));
Chris Lattner9d79eba2009-02-04 05:21:58 +0000159}
160
161
Chris Lattner3cd949c2009-02-04 01:55:42 +0000162/// FindNearestLineEntry - Find the line entry nearest to FID that is before
163/// it. If there is no line entry before Offset in FID, return null.
Mike Stump1eb44332009-09-09 15:08:12 +0000164const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
Chris Lattner3cd949c2009-02-04 01:55:42 +0000165 unsigned Offset) {
166 const std::vector<LineEntry> &Entries = LineEntries[FID];
167 assert(!Entries.empty() && "No #line entries for this FID after all!");
168
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000169 // It is very common for the query to be after the last #line, check this
170 // first.
171 if (Entries.back().FileOffset <= Offset)
172 return &Entries.back();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000173
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000174 // Do a binary search to find the maximal element that is still before Offset.
175 std::vector<LineEntry>::const_iterator I =
176 std::upper_bound(Entries.begin(), Entries.end(), Offset);
177 if (I == Entries.begin()) return 0;
178 return &*--I;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000179}
Chris Lattnerac50e342009-02-03 22:13:05 +0000180
Douglas Gregorbd945002009-04-13 16:31:14 +0000181/// \brief Add a new line entry that has already been encoded into
182/// the internal representation of the line table.
Mike Stump1eb44332009-09-09 15:08:12 +0000183void LineTableInfo::AddEntry(unsigned FID,
Douglas Gregorbd945002009-04-13 16:31:14 +0000184 const std::vector<LineEntry> &Entries) {
185 LineEntries[FID] = Entries;
186}
Chris Lattnerac50e342009-02-03 22:13:05 +0000187
Chris Lattner5b9a5042009-01-26 07:57:50 +0000188/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +0000189///
Chris Lattner5b9a5042009-01-26 07:57:50 +0000190unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
191 if (LineTable == 0)
192 LineTable = new LineTableInfo();
193 return LineTable->getLineTableFilenameID(Ptr, Len);
194}
195
196
Chris Lattner4c4ea172009-02-03 21:52:55 +0000197/// AddLineNote - Add a line note to the line table for the FileID and offset
198/// specified by Loc. If FilenameID is -1, it is considered to be
199/// unspecified.
200void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
201 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000202 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattnerac50e342009-02-03 22:13:05 +0000204 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
205
206 // Remember that this file has #line directives now if it doesn't already.
207 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattnerac50e342009-02-03 22:13:05 +0000209 if (LineTable == 0)
210 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000211 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000212}
213
Chris Lattner9d79eba2009-02-04 05:21:58 +0000214/// AddLineNote - Add a GNU line marker to the line table.
215void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
216 int FilenameID, bool IsFileEntry,
217 bool IsFileExit, bool IsSystemHeader,
218 bool IsExternCHeader) {
219 // If there is no filename and no flags, this is treated just like a #line,
220 // which does not change the flags of the previous line marker.
221 if (FilenameID == -1) {
222 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
223 "Can't set flags without setting the filename!");
224 return AddLineNote(Loc, LineNo, FilenameID);
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner9d79eba2009-02-04 05:21:58 +0000227 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
228 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Chris Lattner9d79eba2009-02-04 05:21:58 +0000230 // Remember that this file has #line directives now if it doesn't already.
231 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattner9d79eba2009-02-04 05:21:58 +0000233 if (LineTable == 0)
234 LineTable = new LineTableInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattner9d79eba2009-02-04 05:21:58 +0000236 SrcMgr::CharacteristicKind FileKind;
237 if (IsExternCHeader)
238 FileKind = SrcMgr::C_ExternCSystem;
239 else if (IsSystemHeader)
240 FileKind = SrcMgr::C_System;
241 else
242 FileKind = SrcMgr::C_User;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattner9d79eba2009-02-04 05:21:58 +0000244 unsigned EntryExit = 0;
245 if (IsFileEntry)
246 EntryExit = 1;
247 else if (IsFileExit)
248 EntryExit = 2;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner9d79eba2009-02-04 05:21:58 +0000250 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
251 EntryExit, FileKind);
252}
253
Douglas Gregorbd945002009-04-13 16:31:14 +0000254LineTableInfo &SourceManager::getLineTable() {
255 if (LineTable == 0)
256 LineTable = new LineTableInfo();
257 return *LineTable;
258}
Chris Lattner4c4ea172009-02-03 21:52:55 +0000259
Chris Lattner23b5dc62009-02-04 00:40:31 +0000260//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000261// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000262//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000263
Chris Lattner5b9a5042009-01-26 07:57:50 +0000264SourceManager::~SourceManager() {
265 delete LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000267 // Delete FileEntry objects corresponding to content caches. Since the actual
268 // content cache objects are bump pointer allocated, we just have to run the
269 // dtors, but we call the deallocate method for completeness.
270 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
271 MemBufferInfos[i]->~ContentCache();
272 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
273 }
274 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
275 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
276 I->second->~ContentCache();
277 ContentCacheAlloc.Deallocate(I->second);
278 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000279}
280
281void SourceManager::clearIDTables() {
282 MainFileID = FileID();
283 SLocEntryTable.clear();
284 LastLineNoFileIDQuery = FileID();
285 LastLineNoContentCache = 0;
286 LastFileIDLookup = FileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner5b9a5042009-01-26 07:57:50 +0000288 if (LineTable)
289 LineTable->clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner5b9a5042009-01-26 07:57:50 +0000291 // Use up FileID #0 as an invalid instantiation.
292 NextOffset = 0;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000293 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000294}
295
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000296/// getOrCreateContentCache - Create or return a cached ContentCache for the
297/// specified file.
298const ContentCache *
299SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 assert(FileEnt && "Didn't specify a file entry to use?");
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000303 ContentCache *&Entry = FileInfos[FileEnt];
304 if (Entry) return Entry;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner00282d62009-02-03 07:41:46 +0000306 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
307 // so that FileInfo can use the low 3 bits of the pointer for its own
308 // nefarious purposes.
309 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
310 EntryAlign = std::max(8U, EntryAlign);
311 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000312 new (Entry) ContentCache(FileEnt);
313 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314}
315
316
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000317/// createMemBufferContentCache - Create a new ContentCache for the specified
318/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000319const ContentCache*
320SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000321 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
322 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
323 // the pointer for its own nefarious purposes.
324 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
325 EntryAlign = std::max(8U, EntryAlign);
326 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000327 new (Entry) ContentCache();
328 MemBufferInfos.push_back(Entry);
329 Entry->setBuffer(Buffer);
330 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000331}
332
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000333void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
334 unsigned NumSLocEntries,
335 unsigned NextOffset) {
336 ExternalSLocEntries = Source;
337 this->NextOffset = NextOffset;
338 SLocEntryLoaded.resize(NumSLocEntries + 1);
339 SLocEntryLoaded[0] = true;
340 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries);
341}
342
Douglas Gregor2bf1eb02009-04-27 21:28:04 +0000343void SourceManager::ClearPreallocatedSLocEntries() {
344 unsigned I = 0;
345 for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
346 if (!SLocEntryLoaded[I])
347 break;
348
349 // We've already loaded all preallocated source location entries.
350 if (I == SLocEntryLoaded.size())
351 return;
352
353 // Remove everything from location I onward.
354 SLocEntryTable.resize(I);
355 SLocEntryLoaded.clear();
356 ExternalSLocEntries = 0;
357}
358
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000359
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000360//===----------------------------------------------------------------------===//
361// Methods to create new FileID's and instantiations.
362//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000363
Nico Weber48002c82008-09-29 00:25:48 +0000364/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000365/// include position. This works regardless of whether the ContentCache
366/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000367FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000368 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000369 SrcMgr::CharacteristicKind FileCharacter,
370 unsigned PreallocatedID,
371 unsigned Offset) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000372 if (PreallocatedID) {
373 // If we're filling in a preallocated ID, just load in the file
374 // entry and return.
Mike Stump1eb44332009-09-09 15:08:12 +0000375 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000376 "Preallocate ID out-of-range");
Mike Stump1eb44332009-09-09 15:08:12 +0000377 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000378 "Source location entry already loaded");
379 assert(Offset && "Preallocate source location cannot have zero offset");
Mike Stump1eb44332009-09-09 15:08:12 +0000380 SLocEntryTable[PreallocatedID]
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000381 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
382 SLocEntryLoaded[PreallocatedID] = true;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000383 FileID FID = FileID::get(PreallocatedID);
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000384 return LastFileIDLookup = FID;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000385 }
386
Mike Stump1eb44332009-09-09 15:08:12 +0000387 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000388 FileInfo::get(IncludePos, File,
389 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000390 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000391 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
392 NextOffset += FileSize+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000394 // Set LastFileIDLookup to the newly created file. The next getFileID call is
395 // almost guaranteed to be from that file.
Argyrios Kyrtzidisea703f12009-06-23 00:42:06 +0000396 FileID FID = FileID::get(SLocEntryTable.size()-1);
Argyrios Kyrtzidisea703f12009-06-23 00:42:06 +0000397 return LastFileIDLookup = FID;
Reid Spencer5f016e22007-07-11 17:01:13 +0000398}
399
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000400/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000401/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000402/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000403SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000404 SourceLocation ILocStart,
405 SourceLocation ILocEnd,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000406 unsigned TokLength,
407 unsigned PreallocatedID,
408 unsigned Offset) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000409 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000410 if (PreallocatedID) {
411 // If we're filling in a preallocated ID, just load in the
412 // instantiation entry and return.
Mike Stump1eb44332009-09-09 15:08:12 +0000413 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000414 "Preallocate ID out-of-range");
Mike Stump1eb44332009-09-09 15:08:12 +0000415 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000416 "Source location entry already loaded");
417 assert(Offset && "Preallocate source location cannot have zero offset");
418 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
419 SLocEntryLoaded[PreallocatedID] = true;
420 return SourceLocation::getMacroLoc(Offset);
421 }
Chris Lattnere7fb4842009-02-15 20:52:18 +0000422 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000423 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
424 NextOffset += TokLength+1;
425 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000426}
427
Douglas Gregor29684422009-12-02 06:49:09 +0000428const llvm::MemoryBuffer *
429SourceManager::getMemoryBufferForFile(const FileEntry *File) {
430 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
431 if (IR == 0)
432 return 0;
433
434 return IR->getBuffer();
435}
436
437bool SourceManager::overrideFileContents(const FileEntry *SourceFile,
438 const llvm::MemoryBuffer *Buffer) {
439 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
440 if (IR == 0)
441 return true;
442
443 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer);
444 return false;
445}
446
Chris Lattner31530ba2009-01-19 07:32:13 +0000447/// getBufferData - Return a pointer to the start and end of the source buffer
448/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000449std::pair<const char*, const char*>
450SourceManager::getBufferData(FileID FID) const {
451 const llvm::MemoryBuffer *Buf = getBuffer(FID);
452 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
453}
454
455
Chris Lattner23b5dc62009-02-04 00:40:31 +0000456//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000457// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000458//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000459
460/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
461/// method that is used for all SourceManager queries that start with a
462/// SourceLocation object. It is responsible for finding the entry in
463/// SLocEntryTable which contains the specified location.
464///
465FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
466 assert(SLocOffset && "Invalid FileID");
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000468 // After the first and second level caches, I see two common sorts of
469 // behavior: 1) a lot of searched FileID's are "near" the cached file location
470 // or are "near" the cached instantiation location. 2) others are just
471 // completely random and may be a very long way away.
472 //
473 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
474 // then we fall back to a less cache efficient, but more scalable, binary
475 // search to find the location.
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000477 // See if this is near the file point - worst case we start scanning from the
478 // most newly created FileID.
479 std::vector<SrcMgr::SLocEntry>::const_iterator I;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000481 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
482 // Neither loc prunes our search.
483 I = SLocEntryTable.end();
484 } else {
485 // Perhaps it is near the file point.
486 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
487 }
488
489 // Find the FileID that contains this. "I" is an iterator that points to a
490 // FileID whose offset is known to be larger than SLocOffset.
491 unsigned NumProbes = 0;
492 while (1) {
493 --I;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000494 if (ExternalSLocEntries)
495 getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000496 if (I->getOffset() <= SLocOffset) {
497#if 0
498 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
499 I-SLocEntryTable.begin(),
500 I->isInstantiation() ? "inst" : "file",
501 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
502#endif
503 FileID Res = FileID::get(I-SLocEntryTable.begin());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000504
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000505 // If this isn't an instantiation, remember it. We have good locality
506 // across FileID lookups.
507 if (!I->isInstantiation())
508 LastFileIDLookup = Res;
509 NumLinearScans += NumProbes+1;
510 return Res;
511 }
512 if (++NumProbes == 8)
513 break;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000516 // Convert "I" back into an index. We know that it is an entry whose index is
517 // larger than the offset we are looking for.
518 unsigned GreaterIndex = I-SLocEntryTable.begin();
519 // LessIndex - This is the lower bound of the range that we're searching.
520 // We know that the offset corresponding to the FileID is is less than
521 // SLocOffset.
522 unsigned LessIndex = 0;
523 NumProbes = 0;
524 while (1) {
525 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000526 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000528 ++NumProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000530 // If the offset of the midpoint is too large, chop the high side of the
531 // range to the midpoint.
532 if (MidOffset > SLocOffset) {
533 GreaterIndex = MiddleIndex;
534 continue;
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000537 // If the middle index contains the value, succeed and return.
538 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
539#if 0
540 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
541 I-SLocEntryTable.begin(),
542 I->isInstantiation() ? "inst" : "file",
543 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
544#endif
545 FileID Res = FileID::get(MiddleIndex);
546
547 // If this isn't an instantiation, remember it. We have good locality
548 // across FileID lookups.
549 if (!I->isInstantiation())
550 LastFileIDLookup = Res;
551 NumBinaryProbes += NumProbes;
552 return Res;
553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000555 // Otherwise, move the low-side up to the middle index.
556 LessIndex = MiddleIndex;
557 }
558}
559
Chris Lattneraddb7972009-01-26 20:04:19 +0000560SourceLocation SourceManager::
561getInstantiationLocSlowCase(SourceLocation Loc) const {
562 do {
Chris Lattnera5c6c582010-02-12 19:31:35 +0000563 // Note: If Loc indicates an offset into a token that came from a macro
564 // expansion (e.g. the 5th character of the token) we do not want to add
565 // this offset when going to the instantiation location. The instatiation
566 // location is the macro invocation, which the offset has nothing to do
567 // with. This is unlike when we get the spelling loc, because the offset
568 // directly correspond to the token whose spelling we're inspecting.
569 Loc = getSLocEntry(getFileID(Loc)).getInstantiation()
Chris Lattnere7fb4842009-02-15 20:52:18 +0000570 .getInstantiationLocStart();
Chris Lattneraddb7972009-01-26 20:04:19 +0000571 } while (!Loc.isFileID());
572
573 return Loc;
574}
575
576SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
577 do {
578 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
579 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
580 Loc = Loc.getFileLocWithOffset(LocInfo.second);
581 } while (!Loc.isFileID());
582 return Loc;
583}
584
585
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000586std::pair<FileID, unsigned>
587SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
588 unsigned Offset) const {
589 // If this is an instantiation record, walk through all the instantiation
590 // points.
591 FileID FID;
592 SourceLocation Loc;
593 do {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000594 Loc = E->getInstantiation().getInstantiationLocStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000596 FID = getFileID(Loc);
597 E = &getSLocEntry(FID);
598 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000599 } while (!Loc.isFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000601 return std::make_pair(FID, Offset);
602}
603
604std::pair<FileID, unsigned>
605SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
606 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000607 // If this is an instantiation record, walk through all the instantiation
608 // points.
609 FileID FID;
610 SourceLocation Loc;
611 do {
612 Loc = E->getInstantiation().getSpellingLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000614 FID = getFileID(Loc);
615 E = &getSLocEntry(FID);
616 Offset += Loc.getOffset()-E->getOffset();
617 } while (!Loc.isFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000619 return std::make_pair(FID, Offset);
620}
621
Chris Lattner387616e2009-02-17 08:04:48 +0000622/// getImmediateSpellingLoc - Given a SourceLocation object, return the
623/// spelling location referenced by the ID. This is the first level down
624/// towards the place where the characters that make up the lexed token can be
625/// found. This should not generally be used by clients.
626SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
627 if (Loc.isFileID()) return Loc;
628 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
629 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
630 return Loc.getFileLocWithOffset(LocInfo.second);
631}
632
633
Chris Lattnere7fb4842009-02-15 20:52:18 +0000634/// getImmediateInstantiationRange - Loc is required to be an instantiation
635/// location. Return the start/end of the instantiation information.
636std::pair<SourceLocation,SourceLocation>
637SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
638 assert(Loc.isMacroID() && "Not an instantiation loc!");
639 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
640 return II.getInstantiationLocRange();
641}
642
Chris Lattner66781332009-02-15 21:26:50 +0000643/// getInstantiationRange - Given a SourceLocation object, return the
644/// range of tokens covered by the instantiation in the ultimate file.
645std::pair<SourceLocation,SourceLocation>
646SourceManager::getInstantiationRange(SourceLocation Loc) const {
647 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Chris Lattner66781332009-02-15 21:26:50 +0000649 std::pair<SourceLocation,SourceLocation> Res =
650 getImmediateInstantiationRange(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner66781332009-02-15 21:26:50 +0000652 // Fully resolve the start and end locations to their ultimate instantiation
653 // points.
654 while (!Res.first.isFileID())
655 Res.first = getImmediateInstantiationRange(Res.first).first;
656 while (!Res.second.isFileID())
657 Res.second = getImmediateInstantiationRange(Res.second).second;
658 return Res;
659}
660
Chris Lattnere7fb4842009-02-15 20:52:18 +0000661
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000662
663//===----------------------------------------------------------------------===//
664// Queries about the code at a SourceLocation.
665//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000666
667/// getCharacterData - Return a pointer to the start of the specified location
668/// in the appropriate MemoryBuffer.
669const char *SourceManager::getCharacterData(SourceLocation SL) const {
670 // Note that this is a hot function in the getSpelling() path, which is
671 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000672 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenekc16c2082009-01-06 01:55:26 +0000674 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000675 return getSLocEntry(LocInfo.first).getFile().getContentCache()
676 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000677}
678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679
Chris Lattner9dc1f532007-07-20 16:37:10 +0000680/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000681/// this is significantly cheaper to compute than the line number.
682unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
683 const char *Buf = getBuffer(FID)->getBufferStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 unsigned LineStart = FilePos;
686 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
687 --LineStart;
688 return FilePos-LineStart+1;
689}
690
Chris Lattner7da5aea2009-02-04 00:55:58 +0000691unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000692 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000693 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
694 return getColumnNumber(LocInfo.first, LocInfo.second);
695}
696
697unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000698 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000699 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
700 return getColumnNumber(LocInfo.first, LocInfo.second);
701}
702
703
704
Benjamin Kramerc997eb42009-11-14 16:36:57 +0000705static DISABLE_INLINE void ComputeLineNumbers(ContentCache* FI,
706 llvm::BumpPtrAllocator &Alloc);
Mike Stump1eb44332009-09-09 15:08:12 +0000707static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000708 // Note that calling 'getBuffer()' may lazily page in the file.
709 const MemoryBuffer *Buffer = FI->getBuffer();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000711 // Find the file offsets of all of the *physical* source lines. This does
712 // not look at trigraphs, escaped newlines, or anything else tricky.
713 std::vector<unsigned> LineOffsets;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000715 // Line #1 starts at char 0.
716 LineOffsets.push_back(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000718 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
719 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
720 unsigned Offs = 0;
721 while (1) {
722 // Skip over the contents of the line.
723 // TODO: Vectorize this? This is very performance sensitive for programs
724 // with lots of diagnostics and in -E mode.
725 const unsigned char *NextBuf = (const unsigned char *)Buf;
726 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
727 ++NextBuf;
728 Offs += NextBuf-Buf;
729 Buf = NextBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000731 if (Buf[0] == '\n' || Buf[0] == '\r') {
732 // If this is \n\r or \r\n, skip both characters.
733 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
734 ++Offs, ++Buf;
735 ++Offs, ++Buf;
736 LineOffsets.push_back(Offs);
737 } else {
738 // Otherwise, this is a null. If end of file, exit.
739 if (Buf == End) break;
740 // Otherwise, skip the null.
741 ++Offs, ++Buf;
742 }
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000745 // Copy the offsets into the FileInfo structure.
746 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000747 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000748 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
749}
Reid Spencer5f016e22007-07-11 17:01:13 +0000750
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000751/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000752/// for the position indicated. This requires building and caching a table of
753/// line offsets for the MemoryBuffer, so this is not cheap: use only when
754/// about to emit a diagnostic.
Chris Lattner30fc9332009-02-04 01:06:56 +0000755unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000756 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000757 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000758 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000759 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000760 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000761 .getFile().getContentCache());
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000764 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000765 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000766 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000767
768 // Okay, we know we have a line number table. Do a binary search to find the
769 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000770 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000771 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000772 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattner30fc9332009-02-04 01:06:56 +0000774 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000775
Daniel Dunbar4106d692009-05-18 17:30:52 +0000776 // FIXME: I would like to be convinced that this code is worth being as
Mike Stump1eb44332009-09-09 15:08:12 +0000777 // complicated as it is, binary search isn't that slow.
Daniel Dunbar4106d692009-05-18 17:30:52 +0000778 //
779 // If it is worth being optimized, then in my opinion it could be more
780 // performant, simpler, and more obviously correct by just "galloping" outward
781 // from the queried file position. In fact, this could be incorporated into a
782 // generic algorithm such as lower_bound_with_hint.
783 //
784 // If someone gives me a test case where this matters, and I will do it! - DWD
785
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000786 // If the previous query was to the same file, we know both the file pos from
787 // that query and the line number returned. This allows us to narrow the
788 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000789 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000790 if (QueriedFilePos >= LastLineNoFilePos) {
Daniel Dunbar4106d692009-05-18 17:30:52 +0000791 // FIXME: Potential overflow?
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000792 SourceLineCache = SourceLineCache+LastLineNoResult-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000794 // The query is likely to be nearby the previous one. Here we check to
795 // see if it is within 5, 10 or 20 lines. It can be far away in cases
796 // where big comment blocks and vertical whitespace eat up lines but
797 // contribute no tokens.
798 if (SourceLineCache+5 < SourceLineCacheEnd) {
799 if (SourceLineCache[5] > QueriedFilePos)
800 SourceLineCacheEnd = SourceLineCache+5;
801 else if (SourceLineCache+10 < SourceLineCacheEnd) {
802 if (SourceLineCache[10] > QueriedFilePos)
803 SourceLineCacheEnd = SourceLineCache+10;
804 else if (SourceLineCache+20 < SourceLineCacheEnd) {
805 if (SourceLineCache[20] > QueriedFilePos)
806 SourceLineCacheEnd = SourceLineCache+20;
807 }
808 }
809 }
810 } else {
Daniel Dunbar4106d692009-05-18 17:30:52 +0000811 if (LastLineNoResult < Content->NumLines)
812 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000813 }
814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000816 // If the spread is large, do a "radix" test as our initial guess, based on
817 // the assumption that lines average to approximately the same length.
818 // NOTE: This is currently disabled, as it does not appear to be profitable in
819 // initial measurements.
820 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000821 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000823 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000824 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000826 // Check for -10 and +10 lines.
827 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
828 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
829
830 // If the computed lower bound is less than the query location, move it in.
831 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
832 SourceLineCacheStart[LowerBound] < QueriedFilePos)
833 SourceLineCache = SourceLineCacheStart+LowerBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000835 // If the computed upper bound is greater than the query location, move it.
836 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
837 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
838 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000841 unsigned *Pos
842 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000843 unsigned LineNo = Pos-SourceLineCacheStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattner30fc9332009-02-04 01:06:56 +0000845 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000846 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000847 LastLineNoFilePos = QueriedFilePos;
848 LastLineNoResult = LineNo;
849 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000850}
851
Chris Lattner30fc9332009-02-04 01:06:56 +0000852unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
853 if (Loc.isInvalid()) return 0;
854 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
855 return getLineNumber(LocInfo.first, LocInfo.second);
856}
857unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
858 if (Loc.isInvalid()) return 0;
859 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
860 return getLineNumber(LocInfo.first, LocInfo.second);
861}
862
Chris Lattner6b306672009-02-04 05:33:01 +0000863/// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000864/// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +0000865/// header, or an "implicit extern C" system header.
866///
867/// This state can be modified with flags on GNU linemarker directives like:
868/// # 4 "foo.h" 3
869/// which changes all source locations in the current file after that to be
870/// considered to be from a system header.
Mike Stump1eb44332009-09-09 15:08:12 +0000871SrcMgr::CharacteristicKind
Chris Lattner6b306672009-02-04 05:33:01 +0000872SourceManager::getFileCharacteristic(SourceLocation Loc) const {
873 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
874 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
875 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
876
877 // If there are no #line directives in this file, just return the whole-file
878 // state.
879 if (!FI.hasLineDirectives())
880 return FI.getFileCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner6b306672009-02-04 05:33:01 +0000882 assert(LineTable && "Can't have linetable entries without a LineTable!");
883 // See if there is a #line directive before the location.
884 const LineEntry *Entry =
885 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chris Lattner6b306672009-02-04 05:33:01 +0000887 // If this is before the first line marker, use the file characteristic.
888 if (!Entry)
889 return FI.getFileCharacteristic();
890
891 return Entry->FileKind;
892}
893
Chris Lattnerbff5c512009-02-17 08:39:06 +0000894/// Return the filename or buffer identifier of the buffer the location is in.
895/// Note that this name does not respect #line directives. Use getPresumedLoc
896/// for normal clients.
897const char *SourceManager::getBufferName(SourceLocation Loc) const {
898 if (Loc.isInvalid()) return "<invalid loc>";
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Chris Lattnerbff5c512009-02-17 08:39:06 +0000900 return getBuffer(getFileID(Loc))->getBufferIdentifier();
901}
902
Chris Lattner30fc9332009-02-04 01:06:56 +0000903
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000904/// getPresumedLoc - This method returns the "presumed" location of a
905/// SourceLocation specifies. A "presumed location" can be modified by #line
906/// or GNU line marker directives. This provides a view on the data that a
907/// user should see in diagnostics, for example.
908///
909/// Note that a presumed location is always given as the instantiation point
910/// of an instantiation location, not at the spelling location.
911PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
912 if (Loc.isInvalid()) return PresumedLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000914 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000915 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner30fc9332009-02-04 01:06:56 +0000917 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000918 const SrcMgr::ContentCache *C = FI.getContentCache();
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner3cd949c2009-02-04 01:55:42 +0000920 // To get the source name, first consult the FileEntry (if one exists)
921 // before the MemBuffer as this will avoid unnecessarily paging in the
922 // MemBuffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000923 const char *Filename =
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000924 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000925 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
926 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
927 SourceLocation IncludeLoc = FI.getIncludeLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Chris Lattner3cd949c2009-02-04 01:55:42 +0000929 // If we have #line directives in this file, update and overwrite the physical
930 // location info if appropriate.
931 if (FI.hasLineDirectives()) {
932 assert(LineTable && "Can't have linetable entries without a LineTable!");
933 // See if there is a #line directive before this. If so, get it.
934 if (const LineEntry *Entry =
935 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +0000936 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000937 if (Entry->FilenameID != -1)
938 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +0000939
940 // Use the line number specified by the LineEntry. This line number may
941 // be multiple lines down from the line entry. Add the difference in
942 // physical line numbers from the query point and the line marker to the
943 // total.
944 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
945 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner0e0e5da2009-02-04 02:15:40 +0000947 // Note that column numbers are not molested by line markers.
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattner137b6a62009-02-04 06:25:26 +0000949 // Handle virtual #include manipulation.
950 if (Entry->IncludeOffset) {
951 IncludeLoc = getLocForStartOfFile(LocInfo.first);
952 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
953 }
Chris Lattner3cd949c2009-02-04 01:55:42 +0000954 }
955 }
956
957 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000958}
959
960//===----------------------------------------------------------------------===//
961// Other miscellaneous methods.
962//===----------------------------------------------------------------------===//
963
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000964/// \brief Get the source location for the given file:line:col triplet.
965///
966/// If the source file is included multiple times, the source location will
967/// be based upon the first inclusion.
968SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
969 unsigned Line, unsigned Col) const {
970 assert(SourceFile && "Null source file!");
971 assert(Line && Col && "Line and column should start from 1!");
972
973 fileinfo_iterator FI = FileInfos.find(SourceFile);
974 if (FI == FileInfos.end())
975 return SourceLocation();
976 ContentCache *Content = FI->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000978 // If this is the first use of line information for this buffer, compute the
979 /// SourceLineCache for it on demand.
980 if (Content->SourceLineCache == 0)
981 ComputeLineNumbers(Content, ContentCacheAlloc);
982
Douglas Gregor4a160e12009-12-02 05:34:39 +0000983 // Find the first file ID that corresponds to the given file.
984 FileID FirstFID;
985
986 // First, check the main file ID, since it is common to look for a
987 // location in the main file.
988 if (!MainFileID.isInvalid()) {
989 const SLocEntry &MainSLoc = getSLocEntry(MainFileID);
990 if (MainSLoc.isFile() && MainSLoc.getFile().getContentCache() == Content)
991 FirstFID = MainFileID;
992 }
993
994 if (FirstFID.isInvalid()) {
995 // The location we're looking for isn't in the main file; look
996 // through all of the source locations.
997 for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
998 const SLocEntry &SLoc = getSLocEntry(I);
999 if (SLoc.isFile() && SLoc.getFile().getContentCache() == Content) {
1000 FirstFID = FileID::get(I);
1001 break;
1002 }
1003 }
1004 }
1005
1006 if (FirstFID.isInvalid())
1007 return SourceLocation();
1008
Douglas Gregord1eabfb2010-02-27 02:42:25 +00001009 if (Line > Content->NumLines) {
1010 unsigned Size = Content->getBuffer()->getBufferSize();
1011 if (Size > 0)
1012 --Size;
1013 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size);
1014 }
1015
1016 unsigned FilePos = Content->SourceLineCache[Line - 1];
1017 const char *Buf = Content->getBuffer()->getBufferStart() + FilePos;
1018 unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf;
1019 unsigned i = 0;
1020
1021 // Check that the given column is valid.
1022 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1023 ++i;
1024 if (i < Col-1)
1025 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i);
1026
Douglas Gregor4a160e12009-12-02 05:34:39 +00001027 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1);
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001028}
1029
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001030/// \brief Determines the order of 2 source locations in the translation unit.
1031///
1032/// \returns true if LHS source location comes before RHS, false otherwise.
1033bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
1034 SourceLocation RHS) const {
1035 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
1036 if (LHS == RHS)
1037 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001039 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
1040 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001042 // If the source locations are in the same file, just compare offsets.
1043 if (LOffs.first == ROffs.first)
1044 return LOffs.second < ROffs.second;
1045
1046 // If we are comparing a source location with multiple locations in the same
1047 // file, we get a big win by caching the result.
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001049 if (LastLFIDForBeforeTUCheck == LOffs.first &&
1050 LastRFIDForBeforeTUCheck == ROffs.first)
1051 return LastResForBeforeTUCheck;
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001053 LastLFIDForBeforeTUCheck = LOffs.first;
1054 LastRFIDForBeforeTUCheck = ROffs.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001056 // "Traverse" the include/instantiation stacks of both locations and try to
1057 // find a common "ancestor".
1058 //
1059 // First we traverse the stack of the right location and check each level
1060 // against the level of the left location, while collecting all levels in a
1061 // "stack map".
1062
1063 std::map<FileID, unsigned> ROffsMap;
1064 ROffsMap[ROffs.first] = ROffs.second;
1065
1066 while (1) {
1067 SourceLocation UpperLoc;
1068 const SrcMgr::SLocEntry &Entry = getSLocEntry(ROffs.first);
1069 if (Entry.isInstantiation())
1070 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1071 else
1072 UpperLoc = Entry.getFile().getIncludeLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001074 if (UpperLoc.isInvalid())
1075 break; // We reached the top.
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001077 ROffs = getDecomposedLoc(UpperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001079 if (LOffs.first == ROffs.first)
1080 return LastResForBeforeTUCheck = LOffs.second < ROffs.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001082 ROffsMap[ROffs.first] = ROffs.second;
1083 }
1084
1085 // We didn't find a common ancestor. Now traverse the stack of the left
1086 // location, checking against the stack map of the right location.
1087
1088 while (1) {
1089 SourceLocation UpperLoc;
1090 const SrcMgr::SLocEntry &Entry = getSLocEntry(LOffs.first);
1091 if (Entry.isInstantiation())
1092 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1093 else
1094 UpperLoc = Entry.getFile().getIncludeLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001096 if (UpperLoc.isInvalid())
1097 break; // We reached the top.
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001099 LOffs = getDecomposedLoc(UpperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001101 std::map<FileID, unsigned>::iterator I = ROffsMap.find(LOffs.first);
1102 if (I != ROffsMap.end())
1103 return LastResForBeforeTUCheck = LOffs.second < I->second;
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Daniel Dunbarfbcc7be2009-12-01 23:07:57 +00001106 // There is no common ancestor, most probably because one location is in the
1107 // predefines buffer.
1108 //
1109 // FIXME: We should rearrange the external interface so this simply never
1110 // happens; it can't conceptually happen. Also see PR5662.
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Daniel Dunbarfbcc7be2009-12-01 23:07:57 +00001112 // If exactly one location is a memory buffer, assume it preceeds the other.
1113 bool LIsMB = !getSLocEntry(LOffs.first).getFile().getContentCache()->Entry;
1114 bool RIsMB = !getSLocEntry(ROffs.first).getFile().getContentCache()->Entry;
1115 if (LIsMB != RIsMB)
1116 return LastResForBeforeTUCheck = LIsMB;
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Daniel Dunbarfbcc7be2009-12-01 23:07:57 +00001118 // Otherwise, just assume FileIDs were created in order.
1119 return LastResForBeforeTUCheck = (LOffs.first < ROffs.first);
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001120}
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001121
Reid Spencer5f016e22007-07-11 17:01:13 +00001122/// PrintStats - Print statistics to stderr.
1123///
1124void SourceManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00001125 llvm::errs() << "\n*** Source Manager Stats:\n";
1126 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
1127 << " mem buffers mapped.\n";
1128 llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
1129 << NextOffset << "B of Sloc address space used.\n";
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 unsigned NumLineNumsComputed = 0;
1132 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001133 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
1134 NumLineNumsComputed += I->second->SourceLineCache != 0;
1135 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00001138 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
1139 << NumLineNumsComputed << " files with line #'s computed.\n";
1140 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
1141 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +00001142}
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001143
1144ExternalSLocEntrySource::~ExternalSLocEntrySource() { }