blob: 156b809b51f2b6e1ef8839ac50620ff82ab03b95 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceManager.h"
Douglas Gregora07ebc52009-04-13 15:31:25 +000015#include "clang/Basic/SourceManagerInternals.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000016#include "clang/Basic/FileManager.h"
Chris Lattner8996fff2007-07-24 05:57:19 +000017#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3441b4f2009-08-23 22:45:33 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000020#include "llvm/System/Path.h"
21#include <algorithm>
Chris Lattner22eb9722006-06-18 05:43:12 +000022using namespace clang;
Chris Lattner5f4b1ff2006-06-20 05:02:40 +000023using namespace SrcMgr;
Chris Lattner23b7eb62007-06-15 23:05:46 +000024using llvm::MemoryBuffer;
Chris Lattner22eb9722006-06-18 05:43:12 +000025
Chris Lattner153a0f12009-02-04 00:40:31 +000026//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +000027// SourceManager Helper Classes
Chris Lattner153a0f12009-02-04 00:40:31 +000028//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +000029
Ted Kremenekc08bca62007-10-30 21:08:08 +000030ContentCache::~ContentCache() {
31 delete Buffer;
Chris Lattner22eb9722006-06-18 05:43:12 +000032}
33
Ted Kremenek12c2af42009-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 Gregor53ad6b92009-12-02 06:49:09 +000044/// file is not lazily brought in from disk to satisfy this query.
Ted Kremenek12c2af42009-01-06 01:55:26 +000045unsigned ContentCache::getSize() const {
Ted Kremenek4a265242010-03-10 18:22:38 +000046 return Buffer ? (unsigned) Buffer->getBufferSize()
47 : (unsigned) Entry->getSize();
Ted Kremenek12c2af42009-01-06 01:55:26 +000048}
49
Douglas Gregor53ad6b92009-12-02 06:49:09 +000050void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B) {
Douglas Gregor5f498832009-12-03 17:05:59 +000051 assert(B != Buffer);
Douglas Gregor53ad6b92009-12-02 06:49:09 +000052
53 delete Buffer;
54 Buffer = B;
55}
56
Chris Lattnered3b3602009-12-01 22:52:33 +000057const llvm::MemoryBuffer *ContentCache::getBuffer(std::string *ErrorStr) const {
Ted Kremenek763ea552009-01-06 22:43:04 +000058 // Lazily create the Buffer for ContentCaches that wrap files.
59 if (!Buffer && Entry) {
Chris Lattnered3b3602009-12-01 22:52:33 +000060 Buffer = MemoryBuffer::getFile(Entry->getName(), ErrorStr,Entry->getSize());
Daniel Dunbar7cea5f12009-12-06 05:43:36 +000061
62 // If we were unable to open the file, then we are in an inconsistent
63 // situation where the content cache referenced a file which no longer
64 // exists. Most likely, we were using a stat cache with an invalid entry but
65 // the file could also have been removed during processing. Since we can't
66 // really deal with this situation, just create an empty buffer.
67 //
68 // FIXME: This is definitely not ideal, but our immediate clients can't
69 // currently handle returning a null entry here. Ideally we should detect
70 // that we are in an inconsistent situation and error out as quickly as
71 // possible.
72 if (!Buffer) {
73 const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
74 Buffer = MemoryBuffer::getNewMemBuffer(Entry->getSize(), "<invalid>");
75 char *Ptr = const_cast<char*>(Buffer->getBufferStart());
76 for (unsigned i = 0, e = Entry->getSize(); i != e; ++i)
77 Ptr[i] = FillStr[i % FillStr.size()];
78 }
Ted Kremenek763ea552009-01-06 22:43:04 +000079 }
Ted Kremenek12c2af42009-01-06 01:55:26 +000080 return Buffer;
81}
82
Chris Lattnerb5fba6f2009-01-26 07:57:50 +000083unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
84 // Look up the filename in the string table, returning the pre-existing value
85 // if it exists.
Mike Stump11289f42009-09-09 15:08:12 +000086 llvm::StringMapEntry<unsigned> &Entry =
Chris Lattnerb5fba6f2009-01-26 07:57:50 +000087 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
88 if (Entry.getValue() != ~0U)
89 return Entry.getValue();
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerb5fba6f2009-01-26 07:57:50 +000091 // Otherwise, assign this the next available ID.
92 Entry.setValue(FilenamesByID.size());
93 FilenamesByID.push_back(&Entry);
94 return FilenamesByID.size()-1;
95}
96
Chris Lattner6e0e1f42009-02-03 22:13:05 +000097/// AddLineNote - Add a line note to the line table that indicates that there
98/// is a #line at the specified FID/Offset location which changes the presumed
99/// location to LineNo/FilenameID.
Chris Lattner153a0f12009-02-04 00:40:31 +0000100void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000101 unsigned LineNo, int FilenameID) {
Chris Lattner153a0f12009-02-04 00:40:31 +0000102 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner153a0f12009-02-04 00:40:31 +0000104 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
105 "Adding line entries out of order!");
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000107 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner1c967782009-02-04 06:25:26 +0000108 unsigned IncludeOffset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000110 if (!Entries.empty()) {
111 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
112 // that we are still in "foo.h".
113 if (FilenameID == -1)
114 FilenameID = Entries.back().FilenameID;
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner1c967782009-02-04 06:25:26 +0000116 // If we are after a line marker that switched us to system header mode, or
117 // that set #include information, preserve it.
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000118 Kind = Entries.back().FileKind;
Chris Lattner1c967782009-02-04 06:25:26 +0000119 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000120 }
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner1c967782009-02-04 06:25:26 +0000122 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
123 IncludeOffset));
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000124}
125
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000126/// AddLineNote This is the same as the previous version of AddLineNote, but is
127/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
128/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
129/// this is a file exit. FileKind specifies whether this is a system header or
130/// extern C system header.
131void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
132 unsigned LineNo, int FilenameID,
133 unsigned EntryExit,
134 SrcMgr::CharacteristicKind FileKind) {
135 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
Mike Stump11289f42009-09-09 15:08:12 +0000136
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000137 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump11289f42009-09-09 15:08:12 +0000138
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000139 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
140 "Adding line entries out of order!");
141
Chris Lattner1c967782009-02-04 06:25:26 +0000142 unsigned IncludeOffset = 0;
143 if (EntryExit == 0) { // No #include stack change.
144 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
145 } else if (EntryExit == 1) {
146 IncludeOffset = Offset-1;
147 } else if (EntryExit == 2) {
148 assert(!Entries.empty() && Entries.back().IncludeOffset &&
149 "PPDirectives should have caught case when popping empty include stack");
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattner1c967782009-02-04 06:25:26 +0000151 // Get the include loc of the last entries' include loc as our include loc.
152 IncludeOffset = 0;
153 if (const LineEntry *PrevEntry =
154 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
155 IncludeOffset = PrevEntry->IncludeOffset;
156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattner1c967782009-02-04 06:25:26 +0000158 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
159 IncludeOffset));
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000160}
161
162
Chris Lattnerd4293922009-02-04 01:55:42 +0000163/// FindNearestLineEntry - Find the line entry nearest to FID that is before
164/// it. If there is no line entry before Offset in FID, return null.
Mike Stump11289f42009-09-09 15:08:12 +0000165const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
Chris Lattnerd4293922009-02-04 01:55:42 +0000166 unsigned Offset) {
167 const std::vector<LineEntry> &Entries = LineEntries[FID];
168 assert(!Entries.empty() && "No #line entries for this FID after all!");
169
Chris Lattner334a2ad2009-02-04 04:46:59 +0000170 // It is very common for the query to be after the last #line, check this
171 // first.
172 if (Entries.back().FileOffset <= Offset)
173 return &Entries.back();
Chris Lattnerd4293922009-02-04 01:55:42 +0000174
Chris Lattner334a2ad2009-02-04 04:46:59 +0000175 // Do a binary search to find the maximal element that is still before Offset.
176 std::vector<LineEntry>::const_iterator I =
177 std::upper_bound(Entries.begin(), Entries.end(), Offset);
178 if (I == Entries.begin()) return 0;
179 return &*--I;
Chris Lattnerd4293922009-02-04 01:55:42 +0000180}
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000181
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000182/// \brief Add a new line entry that has already been encoded into
183/// the internal representation of the line table.
Mike Stump11289f42009-09-09 15:08:12 +0000184void LineTableInfo::AddEntry(unsigned FID,
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000185 const std::vector<LineEntry> &Entries) {
186 LineEntries[FID] = Entries;
187}
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000188
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000189/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump11289f42009-09-09 15:08:12 +0000190///
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000191unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
192 if (LineTable == 0)
193 LineTable = new LineTableInfo();
194 return LineTable->getLineTableFilenameID(Ptr, Len);
195}
196
197
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000198/// AddLineNote - Add a line note to the line table for the FileID and offset
199/// specified by Loc. If FilenameID is -1, it is considered to be
200/// unspecified.
201void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
202 int FilenameID) {
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000203 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000205 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
206
207 // Remember that this file has #line directives now if it doesn't already.
208 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000210 if (LineTable == 0)
211 LineTable = new LineTableInfo();
Chris Lattner153a0f12009-02-04 00:40:31 +0000212 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000213}
214
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000215/// AddLineNote - Add a GNU line marker to the line table.
216void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
217 int FilenameID, bool IsFileEntry,
218 bool IsFileExit, bool IsSystemHeader,
219 bool IsExternCHeader) {
220 // If there is no filename and no flags, this is treated just like a #line,
221 // which does not change the flags of the previous line marker.
222 if (FilenameID == -1) {
223 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
224 "Can't set flags without setting the filename!");
225 return AddLineNote(Loc, LineNo, FilenameID);
226 }
Mike Stump11289f42009-09-09 15:08:12 +0000227
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000228 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
229 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
Mike Stump11289f42009-09-09 15:08:12 +0000230
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000231 // Remember that this file has #line directives now if it doesn't already.
232 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000234 if (LineTable == 0)
235 LineTable = new LineTableInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000237 SrcMgr::CharacteristicKind FileKind;
238 if (IsExternCHeader)
239 FileKind = SrcMgr::C_ExternCSystem;
240 else if (IsSystemHeader)
241 FileKind = SrcMgr::C_System;
242 else
243 FileKind = SrcMgr::C_User;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000245 unsigned EntryExit = 0;
246 if (IsFileEntry)
247 EntryExit = 1;
248 else if (IsFileExit)
249 EntryExit = 2;
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000251 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
252 EntryExit, FileKind);
253}
254
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000255LineTableInfo &SourceManager::getLineTable() {
256 if (LineTable == 0)
257 LineTable = new LineTableInfo();
258 return *LineTable;
259}
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000260
Chris Lattner153a0f12009-02-04 00:40:31 +0000261//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +0000262// Private 'Create' methods.
Chris Lattner153a0f12009-02-04 00:40:31 +0000263//===----------------------------------------------------------------------===//
Ted Kremenek12c2af42009-01-06 01:55:26 +0000264
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000265SourceManager::~SourceManager() {
266 delete LineTable;
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattnerc8233df2009-02-03 07:30:45 +0000268 // Delete FileEntry objects corresponding to content caches. Since the actual
269 // content cache objects are bump pointer allocated, we just have to run the
270 // dtors, but we call the deallocate method for completeness.
271 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
272 MemBufferInfos[i]->~ContentCache();
273 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
274 }
275 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
276 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
277 I->second->~ContentCache();
278 ContentCacheAlloc.Deallocate(I->second);
279 }
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000280}
281
282void SourceManager::clearIDTables() {
283 MainFileID = FileID();
284 SLocEntryTable.clear();
285 LastLineNoFileIDQuery = FileID();
286 LastLineNoContentCache = 0;
287 LastFileIDLookup = FileID();
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000289 if (LineTable)
290 LineTable->clear();
Mike Stump11289f42009-09-09 15:08:12 +0000291
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000292 // Use up FileID #0 as an invalid instantiation.
293 NextOffset = 0;
Chris Lattner9dc9c202009-02-15 20:52:18 +0000294 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000295}
296
Chris Lattner4fa23622009-01-26 00:43:02 +0000297/// getOrCreateContentCache - Create or return a cached ContentCache for the
298/// specified file.
299const ContentCache *
300SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000301 assert(FileEnt && "Didn't specify a file entry to use?");
Mike Stump11289f42009-09-09 15:08:12 +0000302
Chris Lattner22eb9722006-06-18 05:43:12 +0000303 // Do we already have information about this file?
Chris Lattnerc8233df2009-02-03 07:30:45 +0000304 ContentCache *&Entry = FileInfos[FileEnt];
305 if (Entry) return Entry;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattner9be4f6d2009-02-03 07:41:46 +0000307 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
308 // so that FileInfo can use the low 3 bits of the pointer for its own
309 // nefarious purposes.
310 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
311 EntryAlign = std::max(8U, EntryAlign);
312 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattnerc8233df2009-02-03 07:30:45 +0000313 new (Entry) ContentCache(FileEnt);
314 return Entry;
Chris Lattner22eb9722006-06-18 05:43:12 +0000315}
316
317
Ted Kremenek08bed092007-10-31 17:53:38 +0000318/// createMemBufferContentCache - Create a new ContentCache for the specified
319/// memory buffer. This does no caching.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000320const ContentCache*
321SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner9be4f6d2009-02-03 07:41:46 +0000322 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
323 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
324 // the pointer for its own nefarious purposes.
325 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
326 EntryAlign = std::max(8U, EntryAlign);
327 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattnerc8233df2009-02-03 07:30:45 +0000328 new (Entry) ContentCache();
329 MemBufferInfos.push_back(Entry);
330 Entry->setBuffer(Buffer);
331 return Entry;
Chris Lattner22eb9722006-06-18 05:43:12 +0000332}
333
Douglas Gregor258ae542009-04-27 06:38:32 +0000334void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
335 unsigned NumSLocEntries,
336 unsigned NextOffset) {
337 ExternalSLocEntries = Source;
338 this->NextOffset = NextOffset;
339 SLocEntryLoaded.resize(NumSLocEntries + 1);
340 SLocEntryLoaded[0] = true;
341 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries);
342}
343
Douglas Gregor0bc12932009-04-27 21:28:04 +0000344void SourceManager::ClearPreallocatedSLocEntries() {
345 unsigned I = 0;
346 for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
347 if (!SLocEntryLoaded[I])
348 break;
349
350 // We've already loaded all preallocated source location entries.
351 if (I == SLocEntryLoaded.size())
352 return;
353
354 // Remove everything from location I onward.
355 SLocEntryTable.resize(I);
356 SLocEntryLoaded.clear();
357 ExternalSLocEntries = 0;
358}
359
Douglas Gregor258ae542009-04-27 06:38:32 +0000360
Chris Lattner4fa23622009-01-26 00:43:02 +0000361//===----------------------------------------------------------------------===//
362// Methods to create new FileID's and instantiations.
363//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000364
Nico Weber378c5532008-09-29 00:25:48 +0000365/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremeneke26f3c52007-10-30 22:57:35 +0000366/// include position. This works regardless of whether the ContentCache
367/// corresponds to a file or some other input source.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000368FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattner4fa23622009-01-26 00:43:02 +0000369 SourceLocation IncludePos,
Douglas Gregor258ae542009-04-27 06:38:32 +0000370 SrcMgr::CharacteristicKind FileCharacter,
371 unsigned PreallocatedID,
372 unsigned Offset) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000373 if (PreallocatedID) {
374 // If we're filling in a preallocated ID, just load in the file
375 // entry and return.
Mike Stump11289f42009-09-09 15:08:12 +0000376 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000377 "Preallocate ID out-of-range");
Mike Stump11289f42009-09-09 15:08:12 +0000378 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000379 "Source location entry already loaded");
380 assert(Offset && "Preallocate source location cannot have zero offset");
Mike Stump11289f42009-09-09 15:08:12 +0000381 SLocEntryTable[PreallocatedID]
Douglas Gregor258ae542009-04-27 06:38:32 +0000382 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
383 SLocEntryLoaded[PreallocatedID] = true;
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000384 FileID FID = FileID::get(PreallocatedID);
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000385 return LastFileIDLookup = FID;
Douglas Gregor258ae542009-04-27 06:38:32 +0000386 }
387
Mike Stump11289f42009-09-09 15:08:12 +0000388 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
Chris Lattner4fa23622009-01-26 00:43:02 +0000389 FileInfo::get(IncludePos, File,
390 FileCharacter)));
Ted Kremenek12c2af42009-01-06 01:55:26 +0000391 unsigned FileSize = File->getSize();
Chris Lattner4fa23622009-01-26 00:43:02 +0000392 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
393 NextOffset += FileSize+1;
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattner4fa23622009-01-26 00:43:02 +0000395 // Set LastFileIDLookup to the newly created file. The next getFileID call is
396 // almost guaranteed to be from that file.
Argyrios Kyrtzidis0152c6c2009-06-23 00:42:06 +0000397 FileID FID = FileID::get(SLocEntryTable.size()-1);
Argyrios Kyrtzidis0152c6c2009-06-23 00:42:06 +0000398 return LastFileIDLookup = FID;
Chris Lattner22eb9722006-06-18 05:43:12 +0000399}
400
Chris Lattner4fa23622009-01-26 00:43:02 +0000401/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattner53e384f2009-01-16 07:00:02 +0000402/// that a token from SpellingLoc should actually be referenced from
Chris Lattner7d6a4f62006-06-30 06:10:08 +0000403/// InstantiationLoc.
Chris Lattner4fa23622009-01-26 00:43:02 +0000404SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000405 SourceLocation ILocStart,
406 SourceLocation ILocEnd,
Douglas Gregor258ae542009-04-27 06:38:32 +0000407 unsigned TokLength,
408 unsigned PreallocatedID,
409 unsigned Offset) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000410 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
Douglas Gregor258ae542009-04-27 06:38:32 +0000411 if (PreallocatedID) {
412 // If we're filling in a preallocated ID, just load in the
413 // instantiation entry and return.
Mike Stump11289f42009-09-09 15:08:12 +0000414 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000415 "Preallocate ID out-of-range");
Mike Stump11289f42009-09-09 15:08:12 +0000416 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000417 "Source location entry already loaded");
418 assert(Offset && "Preallocate source location cannot have zero offset");
419 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
420 SLocEntryLoaded[PreallocatedID] = true;
421 return SourceLocation::getMacroLoc(Offset);
422 }
Chris Lattner9dc9c202009-02-15 20:52:18 +0000423 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattner4fa23622009-01-26 00:43:02 +0000424 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
425 NextOffset += TokLength+1;
426 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Chris Lattner7d6a4f62006-06-30 06:10:08 +0000427}
428
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000429const llvm::MemoryBuffer *
430SourceManager::getMemoryBufferForFile(const FileEntry *File) {
431 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
432 if (IR == 0)
433 return 0;
434
435 return IR->getBuffer();
436}
437
438bool SourceManager::overrideFileContents(const FileEntry *SourceFile,
439 const llvm::MemoryBuffer *Buffer) {
440 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
441 if (IR == 0)
442 return true;
443
444 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer);
445 return false;
446}
447
Chris Lattner7e343b22009-01-19 07:32:13 +0000448/// getBufferData - Return a pointer to the start and end of the source buffer
449/// data for the specified FileID.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000450std::pair<const char*, const char*>
451SourceManager::getBufferData(FileID FID) const {
452 const llvm::MemoryBuffer *Buf = getBuffer(FID);
453 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
454}
455
456
Chris Lattner153a0f12009-02-04 00:40:31 +0000457//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +0000458// SourceLocation manipulation methods.
Chris Lattner153a0f12009-02-04 00:40:31 +0000459//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +0000460
461/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
462/// method that is used for all SourceManager queries that start with a
463/// SourceLocation object. It is responsible for finding the entry in
464/// SLocEntryTable which contains the specified location.
465///
466FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
467 assert(SLocOffset && "Invalid FileID");
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner4fa23622009-01-26 00:43:02 +0000469 // After the first and second level caches, I see two common sorts of
470 // behavior: 1) a lot of searched FileID's are "near" the cached file location
471 // or are "near" the cached instantiation location. 2) others are just
472 // completely random and may be a very long way away.
473 //
474 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
475 // then we fall back to a less cache efficient, but more scalable, binary
476 // search to find the location.
Mike Stump11289f42009-09-09 15:08:12 +0000477
Chris Lattner4fa23622009-01-26 00:43:02 +0000478 // See if this is near the file point - worst case we start scanning from the
479 // most newly created FileID.
480 std::vector<SrcMgr::SLocEntry>::const_iterator I;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattner4fa23622009-01-26 00:43:02 +0000482 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
483 // Neither loc prunes our search.
484 I = SLocEntryTable.end();
485 } else {
486 // Perhaps it is near the file point.
487 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
488 }
489
490 // Find the FileID that contains this. "I" is an iterator that points to a
491 // FileID whose offset is known to be larger than SLocOffset.
492 unsigned NumProbes = 0;
493 while (1) {
494 --I;
Douglas Gregor258ae542009-04-27 06:38:32 +0000495 if (ExternalSLocEntries)
496 getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
Chris Lattner4fa23622009-01-26 00:43:02 +0000497 if (I->getOffset() <= SLocOffset) {
498#if 0
499 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
500 I-SLocEntryTable.begin(),
501 I->isInstantiation() ? "inst" : "file",
502 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
503#endif
504 FileID Res = FileID::get(I-SLocEntryTable.begin());
Douglas Gregor258ae542009-04-27 06:38:32 +0000505
Chris Lattner4fa23622009-01-26 00:43:02 +0000506 // If this isn't an instantiation, remember it. We have good locality
507 // across FileID lookups.
508 if (!I->isInstantiation())
509 LastFileIDLookup = Res;
510 NumLinearScans += NumProbes+1;
511 return Res;
512 }
513 if (++NumProbes == 8)
514 break;
515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattner4fa23622009-01-26 00:43:02 +0000517 // Convert "I" back into an index. We know that it is an entry whose index is
518 // larger than the offset we are looking for.
519 unsigned GreaterIndex = I-SLocEntryTable.begin();
520 // LessIndex - This is the lower bound of the range that we're searching.
521 // We know that the offset corresponding to the FileID is is less than
522 // SLocOffset.
523 unsigned LessIndex = 0;
524 NumProbes = 0;
525 while (1) {
526 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
Douglas Gregor258ae542009-04-27 06:38:32 +0000527 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattner4fa23622009-01-26 00:43:02 +0000529 ++NumProbes;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Chris Lattner4fa23622009-01-26 00:43:02 +0000531 // If the offset of the midpoint is too large, chop the high side of the
532 // range to the midpoint.
533 if (MidOffset > SLocOffset) {
534 GreaterIndex = MiddleIndex;
535 continue;
536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattner4fa23622009-01-26 00:43:02 +0000538 // If the middle index contains the value, succeed and return.
539 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
540#if 0
541 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
542 I-SLocEntryTable.begin(),
543 I->isInstantiation() ? "inst" : "file",
544 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
545#endif
546 FileID Res = FileID::get(MiddleIndex);
547
548 // If this isn't an instantiation, remember it. We have good locality
549 // across FileID lookups.
550 if (!I->isInstantiation())
551 LastFileIDLookup = Res;
552 NumBinaryProbes += NumProbes;
553 return Res;
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner4fa23622009-01-26 00:43:02 +0000556 // Otherwise, move the low-side up to the middle index.
557 LessIndex = MiddleIndex;
558 }
559}
560
Chris Lattner659ac5f2009-01-26 20:04:19 +0000561SourceLocation SourceManager::
562getInstantiationLocSlowCase(SourceLocation Loc) const {
563 do {
Chris Lattner5647d312010-02-12 19:31:35 +0000564 // Note: If Loc indicates an offset into a token that came from a macro
565 // expansion (e.g. the 5th character of the token) we do not want to add
566 // this offset when going to the instantiation location. The instatiation
567 // location is the macro invocation, which the offset has nothing to do
568 // with. This is unlike when we get the spelling loc, because the offset
569 // directly correspond to the token whose spelling we're inspecting.
570 Loc = getSLocEntry(getFileID(Loc)).getInstantiation()
Chris Lattner9dc9c202009-02-15 20:52:18 +0000571 .getInstantiationLocStart();
Chris Lattner659ac5f2009-01-26 20:04:19 +0000572 } while (!Loc.isFileID());
573
574 return Loc;
575}
576
577SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
578 do {
579 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
580 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
581 Loc = Loc.getFileLocWithOffset(LocInfo.second);
582 } while (!Loc.isFileID());
583 return Loc;
584}
585
586
Chris Lattner4fa23622009-01-26 00:43:02 +0000587std::pair<FileID, unsigned>
588SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
589 unsigned Offset) const {
590 // If this is an instantiation record, walk through all the instantiation
591 // points.
592 FileID FID;
593 SourceLocation Loc;
594 do {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000595 Loc = E->getInstantiation().getInstantiationLocStart();
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattner4fa23622009-01-26 00:43:02 +0000597 FID = getFileID(Loc);
598 E = &getSLocEntry(FID);
599 Offset += Loc.getOffset()-E->getOffset();
Chris Lattner31af4e02009-01-26 19:41:58 +0000600 } while (!Loc.isFileID());
Mike Stump11289f42009-09-09 15:08:12 +0000601
Chris Lattner4fa23622009-01-26 00:43:02 +0000602 return std::make_pair(FID, Offset);
603}
604
605std::pair<FileID, unsigned>
606SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
607 unsigned Offset) const {
Chris Lattner31af4e02009-01-26 19:41:58 +0000608 // If this is an instantiation record, walk through all the instantiation
609 // points.
610 FileID FID;
611 SourceLocation Loc;
612 do {
613 Loc = E->getInstantiation().getSpellingLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattner31af4e02009-01-26 19:41:58 +0000615 FID = getFileID(Loc);
616 E = &getSLocEntry(FID);
617 Offset += Loc.getOffset()-E->getOffset();
618 } while (!Loc.isFileID());
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner4fa23622009-01-26 00:43:02 +0000620 return std::make_pair(FID, Offset);
621}
622
Chris Lattner8ad52d52009-02-17 08:04:48 +0000623/// getImmediateSpellingLoc - Given a SourceLocation object, return the
624/// spelling location referenced by the ID. This is the first level down
625/// towards the place where the characters that make up the lexed token can be
626/// found. This should not generally be used by clients.
627SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
628 if (Loc.isFileID()) return Loc;
629 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
630 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
631 return Loc.getFileLocWithOffset(LocInfo.second);
632}
633
634
Chris Lattner9dc9c202009-02-15 20:52:18 +0000635/// getImmediateInstantiationRange - Loc is required to be an instantiation
636/// location. Return the start/end of the instantiation information.
637std::pair<SourceLocation,SourceLocation>
638SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
639 assert(Loc.isMacroID() && "Not an instantiation loc!");
640 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
641 return II.getInstantiationLocRange();
642}
643
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000644/// getInstantiationRange - Given a SourceLocation object, return the
645/// range of tokens covered by the instantiation in the ultimate file.
646std::pair<SourceLocation,SourceLocation>
647SourceManager::getInstantiationRange(SourceLocation Loc) const {
648 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000650 std::pair<SourceLocation,SourceLocation> Res =
651 getImmediateInstantiationRange(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000653 // Fully resolve the start and end locations to their ultimate instantiation
654 // points.
655 while (!Res.first.isFileID())
656 Res.first = getImmediateInstantiationRange(Res.first).first;
657 while (!Res.second.isFileID())
658 Res.second = getImmediateInstantiationRange(Res.second).second;
659 return Res;
660}
661
Chris Lattner9dc9c202009-02-15 20:52:18 +0000662
Chris Lattner4fa23622009-01-26 00:43:02 +0000663
664//===----------------------------------------------------------------------===//
665// Queries about the code at a SourceLocation.
666//===----------------------------------------------------------------------===//
Chris Lattner30709b032006-06-21 03:01:55 +0000667
Chris Lattnerd01e2912006-06-18 16:22:51 +0000668/// getCharacterData - Return a pointer to the start of the specified location
Chris Lattner739e7392007-04-29 07:12:06 +0000669/// in the appropriate MemoryBuffer.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000670const char *SourceManager::getCharacterData(SourceLocation SL) const {
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000671 // Note that this is a hot function in the getSpelling() path, which is
672 // heavily used by -E mode.
Chris Lattner4fa23622009-01-26 00:43:02 +0000673 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Ted Kremenek12c2af42009-01-06 01:55:26 +0000675 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000676 return getSLocEntry(LocInfo.first).getFile().getContentCache()
677 ->getBuffer()->getBufferStart() + LocInfo.second;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000678}
679
Chris Lattner685730f2006-06-26 01:36:22 +0000680
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000681/// getColumnNumber - Return the column # for the specified file position.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000682/// this is significantly cheaper to compute than the line number.
683unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
684 const char *Buf = getBuffer(FID)->getBufferStart();
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattner22eb9722006-06-18 05:43:12 +0000686 unsigned LineStart = FilePos;
687 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
688 --LineStart;
689 return FilePos-LineStart+1;
690}
691
Chris Lattnere4ad4172009-02-04 00:55:58 +0000692unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner88ea93e2009-02-04 01:06:56 +0000693 if (Loc.isInvalid()) return 0;
Chris Lattnere4ad4172009-02-04 00:55:58 +0000694 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
695 return getColumnNumber(LocInfo.first, LocInfo.second);
696}
697
698unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner88ea93e2009-02-04 01:06:56 +0000699 if (Loc.isInvalid()) return 0;
Chris Lattnere4ad4172009-02-04 00:55:58 +0000700 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
701 return getColumnNumber(LocInfo.first, LocInfo.second);
702}
703
704
705
Benjamin Kramer5e738282009-11-14 16:36:57 +0000706static DISABLE_INLINE void ComputeLineNumbers(ContentCache* FI,
707 llvm::BumpPtrAllocator &Alloc);
Mike Stump11289f42009-09-09 15:08:12 +0000708static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenek12c2af42009-01-06 01:55:26 +0000709 // Note that calling 'getBuffer()' may lazily page in the file.
710 const MemoryBuffer *Buffer = FI->getBuffer();
Mike Stump11289f42009-09-09 15:08:12 +0000711
Chris Lattner8996fff2007-07-24 05:57:19 +0000712 // Find the file offsets of all of the *physical* source lines. This does
713 // not look at trigraphs, escaped newlines, or anything else tricky.
714 std::vector<unsigned> LineOffsets;
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner8996fff2007-07-24 05:57:19 +0000716 // Line #1 starts at char 0.
717 LineOffsets.push_back(0);
Mike Stump11289f42009-09-09 15:08:12 +0000718
Chris Lattner8996fff2007-07-24 05:57:19 +0000719 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
720 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
721 unsigned Offs = 0;
722 while (1) {
723 // Skip over the contents of the line.
724 // TODO: Vectorize this? This is very performance sensitive for programs
725 // with lots of diagnostics and in -E mode.
726 const unsigned char *NextBuf = (const unsigned char *)Buf;
727 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
728 ++NextBuf;
729 Offs += NextBuf-Buf;
730 Buf = NextBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000731
Chris Lattner8996fff2007-07-24 05:57:19 +0000732 if (Buf[0] == '\n' || Buf[0] == '\r') {
733 // If this is \n\r or \r\n, skip both characters.
734 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
735 ++Offs, ++Buf;
736 ++Offs, ++Buf;
737 LineOffsets.push_back(Offs);
738 } else {
739 // Otherwise, this is a null. If end of file, exit.
740 if (Buf == End) break;
741 // Otherwise, skip the null.
742 ++Offs, ++Buf;
743 }
744 }
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner8996fff2007-07-24 05:57:19 +0000746 // Copy the offsets into the FileInfo structure.
747 FI->NumLines = LineOffsets.size();
Chris Lattnerc8233df2009-02-03 07:30:45 +0000748 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner8996fff2007-07-24 05:57:19 +0000749 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
750}
Chris Lattner9a13bde2006-06-21 04:57:09 +0000751
Chris Lattner53e384f2009-01-16 07:00:02 +0000752/// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner22eb9722006-06-18 05:43:12 +0000753/// for the position indicated. This requires building and caching a table of
Chris Lattner739e7392007-04-29 07:12:06 +0000754/// line offsets for the MemoryBuffer, so this is not cheap: use only when
Chris Lattner22eb9722006-06-18 05:43:12 +0000755/// about to emit a diagnostic.
Chris Lattner88ea93e2009-02-04 01:06:56 +0000756unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000757 ContentCache *Content;
Chris Lattner88ea93e2009-02-04 01:06:56 +0000758 if (LastLineNoFileIDQuery == FID)
Ted Kremenekc08bca62007-10-30 21:08:08 +0000759 Content = LastLineNoContentCache;
Chris Lattner8996fff2007-07-24 05:57:19 +0000760 else
Chris Lattner88ea93e2009-02-04 01:06:56 +0000761 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattner4fa23622009-01-26 00:43:02 +0000762 .getFile().getContentCache());
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner22eb9722006-06-18 05:43:12 +0000764 // If this is the first use of line information for this buffer, compute the
Chris Lattner8996fff2007-07-24 05:57:19 +0000765 /// SourceLineCache for it on demand.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000766 if (Content->SourceLineCache == 0)
Chris Lattnerc8233df2009-02-03 07:30:45 +0000767 ComputeLineNumbers(Content, ContentCacheAlloc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000768
769 // Okay, we know we have a line number table. Do a binary search to find the
770 // line number that this character position lands on.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000771 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner8996fff2007-07-24 05:57:19 +0000772 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenekc08bca62007-10-30 21:08:08 +0000773 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner88ea93e2009-02-04 01:06:56 +0000775 unsigned QueriedFilePos = FilePos+1;
Chris Lattner8996fff2007-07-24 05:57:19 +0000776
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000777 // FIXME: I would like to be convinced that this code is worth being as
Mike Stump11289f42009-09-09 15:08:12 +0000778 // complicated as it is, binary search isn't that slow.
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000779 //
780 // If it is worth being optimized, then in my opinion it could be more
781 // performant, simpler, and more obviously correct by just "galloping" outward
782 // from the queried file position. In fact, this could be incorporated into a
783 // generic algorithm such as lower_bound_with_hint.
784 //
785 // If someone gives me a test case where this matters, and I will do it! - DWD
786
Chris Lattner8996fff2007-07-24 05:57:19 +0000787 // If the previous query was to the same file, we know both the file pos from
788 // that query and the line number returned. This allows us to narrow the
789 // search space from the entire file to something near the match.
Chris Lattner88ea93e2009-02-04 01:06:56 +0000790 if (LastLineNoFileIDQuery == FID) {
Chris Lattner8996fff2007-07-24 05:57:19 +0000791 if (QueriedFilePos >= LastLineNoFilePos) {
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000792 // FIXME: Potential overflow?
Chris Lattner8996fff2007-07-24 05:57:19 +0000793 SourceLineCache = SourceLineCache+LastLineNoResult-1;
Mike Stump11289f42009-09-09 15:08:12 +0000794
Chris Lattner8996fff2007-07-24 05:57:19 +0000795 // The query is likely to be nearby the previous one. Here we check to
796 // see if it is within 5, 10 or 20 lines. It can be far away in cases
797 // where big comment blocks and vertical whitespace eat up lines but
798 // contribute no tokens.
799 if (SourceLineCache+5 < SourceLineCacheEnd) {
800 if (SourceLineCache[5] > QueriedFilePos)
801 SourceLineCacheEnd = SourceLineCache+5;
802 else if (SourceLineCache+10 < SourceLineCacheEnd) {
803 if (SourceLineCache[10] > QueriedFilePos)
804 SourceLineCacheEnd = SourceLineCache+10;
805 else if (SourceLineCache+20 < SourceLineCacheEnd) {
806 if (SourceLineCache[20] > QueriedFilePos)
807 SourceLineCacheEnd = SourceLineCache+20;
808 }
809 }
810 }
811 } else {
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000812 if (LastLineNoResult < Content->NumLines)
813 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
Chris Lattner8996fff2007-07-24 05:57:19 +0000814 }
815 }
Mike Stump11289f42009-09-09 15:08:12 +0000816
Chris Lattner830a77f2007-07-24 06:43:46 +0000817 // If the spread is large, do a "radix" test as our initial guess, based on
818 // the assumption that lines average to approximately the same length.
819 // NOTE: This is currently disabled, as it does not appear to be profitable in
820 // initial measurements.
821 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenekc08bca62007-10-30 21:08:08 +0000822 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner830a77f2007-07-24 06:43:46 +0000824 // Take a stab at guessing where it is.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000825 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattner830a77f2007-07-24 06:43:46 +0000827 // Check for -10 and +10 lines.
828 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
829 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
830
831 // If the computed lower bound is less than the query location, move it in.
832 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
833 SourceLineCacheStart[LowerBound] < QueriedFilePos)
834 SourceLineCache = SourceLineCacheStart+LowerBound;
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattner830a77f2007-07-24 06:43:46 +0000836 // If the computed upper bound is greater than the query location, move it.
837 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
838 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
839 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
840 }
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner830a77f2007-07-24 06:43:46 +0000842 unsigned *Pos
843 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner8996fff2007-07-24 05:57:19 +0000844 unsigned LineNo = Pos-SourceLineCacheStart;
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner88ea93e2009-02-04 01:06:56 +0000846 LastLineNoFileIDQuery = FID;
Ted Kremenekc08bca62007-10-30 21:08:08 +0000847 LastLineNoContentCache = Content;
Chris Lattner8996fff2007-07-24 05:57:19 +0000848 LastLineNoFilePos = QueriedFilePos;
849 LastLineNoResult = LineNo;
850 return LineNo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000851}
852
Chris Lattner88ea93e2009-02-04 01:06:56 +0000853unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
854 if (Loc.isInvalid()) return 0;
855 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
856 return getLineNumber(LocInfo.first, LocInfo.second);
857}
858unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
859 if (Loc.isInvalid()) return 0;
860 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
861 return getLineNumber(LocInfo.first, LocInfo.second);
862}
863
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000864/// getFileCharacteristic - return the file characteristic of the specified
Mike Stump11289f42009-09-09 15:08:12 +0000865/// source location, indicating whether this is a normal file, a system
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000866/// header, or an "implicit extern C" system header.
867///
868/// This state can be modified with flags on GNU linemarker directives like:
869/// # 4 "foo.h" 3
870/// which changes all source locations in the current file after that to be
871/// considered to be from a system header.
Mike Stump11289f42009-09-09 15:08:12 +0000872SrcMgr::CharacteristicKind
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000873SourceManager::getFileCharacteristic(SourceLocation Loc) const {
874 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
875 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
876 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
877
878 // If there are no #line directives in this file, just return the whole-file
879 // state.
880 if (!FI.hasLineDirectives())
881 return FI.getFileCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000883 assert(LineTable && "Can't have linetable entries without a LineTable!");
884 // See if there is a #line directive before the location.
885 const LineEntry *Entry =
886 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
Mike Stump11289f42009-09-09 15:08:12 +0000887
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000888 // If this is before the first line marker, use the file characteristic.
889 if (!Entry)
890 return FI.getFileCharacteristic();
891
892 return Entry->FileKind;
893}
894
Chris Lattnera6f037c2009-02-17 08:39:06 +0000895/// Return the filename or buffer identifier of the buffer the location is in.
896/// Note that this name does not respect #line directives. Use getPresumedLoc
897/// for normal clients.
898const char *SourceManager::getBufferName(SourceLocation Loc) const {
899 if (Loc.isInvalid()) return "<invalid loc>";
Mike Stump11289f42009-09-09 15:08:12 +0000900
Chris Lattnera6f037c2009-02-17 08:39:06 +0000901 return getBuffer(getFileID(Loc))->getBufferIdentifier();
902}
903
Chris Lattner88ea93e2009-02-04 01:06:56 +0000904
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000905/// getPresumedLoc - This method returns the "presumed" location of a
906/// SourceLocation specifies. A "presumed location" can be modified by #line
907/// or GNU line marker directives. This provides a view on the data that a
908/// user should see in diagnostics, for example.
909///
910/// Note that a presumed location is always given as the instantiation point
911/// of an instantiation location, not at the spelling location.
912PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
913 if (Loc.isInvalid()) return PresumedLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000914
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000915 // Presumed locations are always for instantiation points.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000916 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000917
Chris Lattner88ea93e2009-02-04 01:06:56 +0000918 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000919 const SrcMgr::ContentCache *C = FI.getContentCache();
Mike Stump11289f42009-09-09 15:08:12 +0000920
Chris Lattnerd4293922009-02-04 01:55:42 +0000921 // To get the source name, first consult the FileEntry (if one exists)
922 // before the MemBuffer as this will avoid unnecessarily paging in the
923 // MemBuffer.
Mike Stump11289f42009-09-09 15:08:12 +0000924 const char *Filename =
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000925 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattnerd4293922009-02-04 01:55:42 +0000926 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
927 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
928 SourceLocation IncludeLoc = FI.getIncludeLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000929
Chris Lattnerd4293922009-02-04 01:55:42 +0000930 // If we have #line directives in this file, update and overwrite the physical
931 // location info if appropriate.
932 if (FI.hasLineDirectives()) {
933 assert(LineTable && "Can't have linetable entries without a LineTable!");
934 // See if there is a #line directive before this. If so, get it.
935 if (const LineEntry *Entry =
936 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerc1219ff2009-02-04 02:00:59 +0000937 // If the LineEntry indicates a filename, use it.
Chris Lattnerd4293922009-02-04 01:55:42 +0000938 if (Entry->FilenameID != -1)
939 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerc1219ff2009-02-04 02:00:59 +0000940
941 // Use the line number specified by the LineEntry. This line number may
942 // be multiple lines down from the line entry. Add the difference in
943 // physical line numbers from the query point and the line marker to the
944 // total.
945 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
946 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
Mike Stump11289f42009-09-09 15:08:12 +0000947
Chris Lattner20c50ba2009-02-04 02:15:40 +0000948 // Note that column numbers are not molested by line markers.
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner1c967782009-02-04 06:25:26 +0000950 // Handle virtual #include manipulation.
951 if (Entry->IncludeOffset) {
952 IncludeLoc = getLocForStartOfFile(LocInfo.first);
953 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
954 }
Chris Lattnerd4293922009-02-04 01:55:42 +0000955 }
956 }
957
958 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattner4fa23622009-01-26 00:43:02 +0000959}
960
961//===----------------------------------------------------------------------===//
962// Other miscellaneous methods.
963//===----------------------------------------------------------------------===//
964
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000965/// \brief Get the source location for the given file:line:col triplet.
966///
967/// If the source file is included multiple times, the source location will
968/// be based upon the first inclusion.
969SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
970 unsigned Line, unsigned Col) const {
971 assert(SourceFile && "Null source file!");
972 assert(Line && Col && "Line and column should start from 1!");
973
974 fileinfo_iterator FI = FileInfos.find(SourceFile);
975 if (FI == FileInfos.end())
976 return SourceLocation();
977 ContentCache *Content = FI->second;
Mike Stump11289f42009-09-09 15:08:12 +0000978
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000979 // If this is the first use of line information for this buffer, compute the
980 /// SourceLineCache for it on demand.
981 if (Content->SourceLineCache == 0)
982 ComputeLineNumbers(Content, ContentCacheAlloc);
983
Douglas Gregor2a1b6912009-12-02 05:34:39 +0000984 // Find the first file ID that corresponds to the given file.
985 FileID FirstFID;
986
987 // First, check the main file ID, since it is common to look for a
988 // location in the main file.
989 if (!MainFileID.isInvalid()) {
990 const SLocEntry &MainSLoc = getSLocEntry(MainFileID);
991 if (MainSLoc.isFile() && MainSLoc.getFile().getContentCache() == Content)
992 FirstFID = MainFileID;
993 }
994
995 if (FirstFID.isInvalid()) {
996 // The location we're looking for isn't in the main file; look
997 // through all of the source locations.
998 for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
999 const SLocEntry &SLoc = getSLocEntry(I);
1000 if (SLoc.isFile() && SLoc.getFile().getContentCache() == Content) {
1001 FirstFID = FileID::get(I);
1002 break;
1003 }
1004 }
1005 }
1006
1007 if (FirstFID.isInvalid())
1008 return SourceLocation();
1009
Douglas Gregorb8b9f282010-02-27 02:42:25 +00001010 if (Line > Content->NumLines) {
1011 unsigned Size = Content->getBuffer()->getBufferSize();
1012 if (Size > 0)
1013 --Size;
1014 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size);
1015 }
1016
1017 unsigned FilePos = Content->SourceLineCache[Line - 1];
1018 const char *Buf = Content->getBuffer()->getBufferStart() + FilePos;
1019 unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf;
1020 unsigned i = 0;
1021
1022 // Check that the given column is valid.
1023 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1024 ++i;
1025 if (i < Col-1)
1026 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i);
1027
Douglas Gregor2a1b6912009-12-02 05:34:39 +00001028 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1);
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +00001029}
1030
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001031/// \brief Determines the order of 2 source locations in the translation unit.
1032///
1033/// \returns true if LHS source location comes before RHS, false otherwise.
1034bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
1035 SourceLocation RHS) const {
1036 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
1037 if (LHS == RHS)
1038 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001039
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001040 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
1041 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00001042
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001043 // If the source locations are in the same file, just compare offsets.
1044 if (LOffs.first == ROffs.first)
1045 return LOffs.second < ROffs.second;
1046
1047 // If we are comparing a source location with multiple locations in the same
1048 // file, we get a big win by caching the result.
Mike Stump11289f42009-09-09 15:08:12 +00001049
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001050 if (LastLFIDForBeforeTUCheck == LOffs.first &&
1051 LastRFIDForBeforeTUCheck == ROffs.first)
1052 return LastResForBeforeTUCheck;
Mike Stump11289f42009-09-09 15:08:12 +00001053
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001054 LastLFIDForBeforeTUCheck = LOffs.first;
1055 LastRFIDForBeforeTUCheck = ROffs.first;
Mike Stump11289f42009-09-09 15:08:12 +00001056
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001057 // "Traverse" the include/instantiation stacks of both locations and try to
1058 // find a common "ancestor".
1059 //
1060 // First we traverse the stack of the right location and check each level
1061 // against the level of the left location, while collecting all levels in a
1062 // "stack map".
1063
1064 std::map<FileID, unsigned> ROffsMap;
1065 ROffsMap[ROffs.first] = ROffs.second;
1066
1067 while (1) {
1068 SourceLocation UpperLoc;
1069 const SrcMgr::SLocEntry &Entry = getSLocEntry(ROffs.first);
1070 if (Entry.isInstantiation())
1071 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1072 else
1073 UpperLoc = Entry.getFile().getIncludeLoc();
Mike Stump11289f42009-09-09 15:08:12 +00001074
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001075 if (UpperLoc.isInvalid())
1076 break; // We reached the top.
Mike Stump11289f42009-09-09 15:08:12 +00001077
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001078 ROffs = getDecomposedLoc(UpperLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001079
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001080 if (LOffs.first == ROffs.first)
1081 return LastResForBeforeTUCheck = LOffs.second < ROffs.second;
Mike Stump11289f42009-09-09 15:08:12 +00001082
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001083 ROffsMap[ROffs.first] = ROffs.second;
1084 }
1085
1086 // We didn't find a common ancestor. Now traverse the stack of the left
1087 // location, checking against the stack map of the right location.
1088
1089 while (1) {
1090 SourceLocation UpperLoc;
1091 const SrcMgr::SLocEntry &Entry = getSLocEntry(LOffs.first);
1092 if (Entry.isInstantiation())
1093 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1094 else
1095 UpperLoc = Entry.getFile().getIncludeLoc();
Mike Stump11289f42009-09-09 15:08:12 +00001096
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001097 if (UpperLoc.isInvalid())
1098 break; // We reached the top.
Mike Stump11289f42009-09-09 15:08:12 +00001099
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001100 LOffs = getDecomposedLoc(UpperLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001101
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001102 std::map<FileID, unsigned>::iterator I = ROffsMap.find(LOffs.first);
1103 if (I != ROffsMap.end())
1104 return LastResForBeforeTUCheck = LOffs.second < I->second;
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Daniel Dunbar465f4c42009-12-01 23:07:57 +00001107 // There is no common ancestor, most probably because one location is in the
1108 // predefines buffer.
1109 //
1110 // FIXME: We should rearrange the external interface so this simply never
1111 // happens; it can't conceptually happen. Also see PR5662.
Mike Stump11289f42009-09-09 15:08:12 +00001112
Daniel Dunbar465f4c42009-12-01 23:07:57 +00001113 // If exactly one location is a memory buffer, assume it preceeds the other.
1114 bool LIsMB = !getSLocEntry(LOffs.first).getFile().getContentCache()->Entry;
1115 bool RIsMB = !getSLocEntry(ROffs.first).getFile().getContentCache()->Entry;
1116 if (LIsMB != RIsMB)
1117 return LastResForBeforeTUCheck = LIsMB;
Mike Stump11289f42009-09-09 15:08:12 +00001118
Daniel Dunbar465f4c42009-12-01 23:07:57 +00001119 // Otherwise, just assume FileIDs were created in order.
1120 return LastResForBeforeTUCheck = (LOffs.first < ROffs.first);
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001121}
Chris Lattner4fa23622009-01-26 00:43:02 +00001122
Chris Lattner22eb9722006-06-18 05:43:12 +00001123/// PrintStats - Print statistics to stderr.
1124///
1125void SourceManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +00001126 llvm::errs() << "\n*** Source Manager Stats:\n";
1127 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
1128 << " mem buffers mapped.\n";
1129 llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
1130 << NextOffset << "B of Sloc address space used.\n";
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner22eb9722006-06-18 05:43:12 +00001132 unsigned NumLineNumsComputed = 0;
1133 unsigned NumFileBytesMapped = 0;
Chris Lattnerc8233df2009-02-03 07:30:45 +00001134 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
1135 NumLineNumsComputed += I->second->SourceLineCache != 0;
1136 NumFileBytesMapped += I->second->getSizeBytesMapped();
Chris Lattner22eb9722006-06-18 05:43:12 +00001137 }
Mike Stump11289f42009-09-09 15:08:12 +00001138
Benjamin Kramer89b422c2009-08-23 12:08:50 +00001139 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
1140 << NumLineNumsComputed << " files with line #'s computed.\n";
1141 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
1142 << NumBinaryProbes << " binary.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +00001143}
Douglas Gregor258ae542009-04-27 06:38:32 +00001144
1145ExternalSLocEntrySource::~ExternalSLocEntrySource() { }