blob: c27675f38bc696831642e9f1fe028952faea3fb0 [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 Gregorea9b03e2009-09-22 21:11:38 +000044/// file is not lazily brought in from disk to satisfy this query unless it
45/// needs to be truncated due to a truncateAt() call.
Ted Kremenek12c2af42009-01-06 01:55:26 +000046unsigned ContentCache::getSize() const {
Douglas Gregorea9b03e2009-09-22 21:11:38 +000047 return Buffer ? Buffer->getBufferSize() : Entry->getSize();
Ted Kremenek12c2af42009-01-06 01:55:26 +000048}
49
Chris Lattnered3b3602009-12-01 22:52:33 +000050const llvm::MemoryBuffer *ContentCache::getBuffer(std::string *ErrorStr) const {
Ted Kremenek763ea552009-01-06 22:43:04 +000051 // Lazily create the Buffer for ContentCaches that wrap files.
52 if (!Buffer && Entry) {
53 // FIXME: Should we support a way to not have to do this check over
54 // and over if we cannot open the file?
Chris Lattnered3b3602009-12-01 22:52:33 +000055 Buffer = MemoryBuffer::getFile(Entry->getName(), ErrorStr,Entry->getSize());
Douglas Gregorea9b03e2009-09-22 21:11:38 +000056 if (isTruncated())
57 const_cast<ContentCache *>(this)->truncateAt(TruncateAtLine,
58 TruncateAtColumn);
Ted Kremenek763ea552009-01-06 22:43:04 +000059 }
Ted Kremenek12c2af42009-01-06 01:55:26 +000060 return Buffer;
61}
62
Douglas Gregorea9b03e2009-09-22 21:11:38 +000063void ContentCache::truncateAt(unsigned Line, unsigned Column) {
64 TruncateAtLine = Line;
65 TruncateAtColumn = Column;
66
67 if (!isTruncated() || !Buffer)
68 return;
69
70 // Find the byte position of the truncation point.
71 const char *Position = Buffer->getBufferStart();
72 for (unsigned Line = 1; Line < TruncateAtLine; ++Line) {
73 for (; *Position; ++Position) {
74 if (*Position != '\r' && *Position != '\n')
75 continue;
76
77 // Eat \r\n or \n\r as a single line.
78 if ((Position[1] == '\r' || Position[1] == '\n') &&
79 Position[0] != Position[1])
80 ++Position;
81 ++Position;
82 break;
83 }
84 }
85
86 for (unsigned Column = 1; Column < TruncateAtColumn; ++Column, ++Position) {
87 if (!*Position)
88 break;
89
90 if (*Position == '\t')
91 Column += 7;
92 }
93
94 // Truncate the buffer.
95 if (Position != Buffer->getBufferEnd()) {
96 MemoryBuffer *TruncatedBuffer
97 = MemoryBuffer::getMemBufferCopy(Buffer->getBufferStart(), Position,
98 Buffer->getBufferIdentifier());
99 delete Buffer;
100 Buffer = TruncatedBuffer;
101 }
102}
103
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000104unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
105 // Look up the filename in the string table, returning the pre-existing value
106 // if it exists.
Mike Stump11289f42009-09-09 15:08:12 +0000107 llvm::StringMapEntry<unsigned> &Entry =
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000108 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
109 if (Entry.getValue() != ~0U)
110 return Entry.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000112 // Otherwise, assign this the next available ID.
113 Entry.setValue(FilenamesByID.size());
114 FilenamesByID.push_back(&Entry);
115 return FilenamesByID.size()-1;
116}
117
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000118/// AddLineNote - Add a line note to the line table that indicates that there
119/// is a #line at the specified FID/Offset location which changes the presumed
120/// location to LineNo/FilenameID.
Chris Lattner153a0f12009-02-04 00:40:31 +0000121void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000122 unsigned LineNo, int FilenameID) {
Chris Lattner153a0f12009-02-04 00:40:31 +0000123 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattner153a0f12009-02-04 00:40:31 +0000125 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
126 "Adding line entries out of order!");
Mike Stump11289f42009-09-09 15:08:12 +0000127
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000128 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner1c967782009-02-04 06:25:26 +0000129 unsigned IncludeOffset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000131 if (!Entries.empty()) {
132 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
133 // that we are still in "foo.h".
134 if (FilenameID == -1)
135 FilenameID = Entries.back().FilenameID;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Chris Lattner1c967782009-02-04 06:25:26 +0000137 // If we are after a line marker that switched us to system header mode, or
138 // that set #include information, preserve it.
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000139 Kind = Entries.back().FileKind;
Chris Lattner1c967782009-02-04 06:25:26 +0000140 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattner1c967782009-02-04 06:25:26 +0000143 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
144 IncludeOffset));
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000145}
146
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000147/// AddLineNote This is the same as the previous version of AddLineNote, but is
148/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
149/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
150/// this is a file exit. FileKind specifies whether this is a system header or
151/// extern C system header.
152void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
153 unsigned LineNo, int FilenameID,
154 unsigned EntryExit,
155 SrcMgr::CharacteristicKind FileKind) {
156 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000158 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000160 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
161 "Adding line entries out of order!");
162
Chris Lattner1c967782009-02-04 06:25:26 +0000163 unsigned IncludeOffset = 0;
164 if (EntryExit == 0) { // No #include stack change.
165 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
166 } else if (EntryExit == 1) {
167 IncludeOffset = Offset-1;
168 } else if (EntryExit == 2) {
169 assert(!Entries.empty() && Entries.back().IncludeOffset &&
170 "PPDirectives should have caught case when popping empty include stack");
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattner1c967782009-02-04 06:25:26 +0000172 // Get the include loc of the last entries' include loc as our include loc.
173 IncludeOffset = 0;
174 if (const LineEntry *PrevEntry =
175 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
176 IncludeOffset = PrevEntry->IncludeOffset;
177 }
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner1c967782009-02-04 06:25:26 +0000179 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
180 IncludeOffset));
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000181}
182
183
Chris Lattnerd4293922009-02-04 01:55:42 +0000184/// FindNearestLineEntry - Find the line entry nearest to FID that is before
185/// it. If there is no line entry before Offset in FID, return null.
Mike Stump11289f42009-09-09 15:08:12 +0000186const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
Chris Lattnerd4293922009-02-04 01:55:42 +0000187 unsigned Offset) {
188 const std::vector<LineEntry> &Entries = LineEntries[FID];
189 assert(!Entries.empty() && "No #line entries for this FID after all!");
190
Chris Lattner334a2ad2009-02-04 04:46:59 +0000191 // It is very common for the query to be after the last #line, check this
192 // first.
193 if (Entries.back().FileOffset <= Offset)
194 return &Entries.back();
Chris Lattnerd4293922009-02-04 01:55:42 +0000195
Chris Lattner334a2ad2009-02-04 04:46:59 +0000196 // Do a binary search to find the maximal element that is still before Offset.
197 std::vector<LineEntry>::const_iterator I =
198 std::upper_bound(Entries.begin(), Entries.end(), Offset);
199 if (I == Entries.begin()) return 0;
200 return &*--I;
Chris Lattnerd4293922009-02-04 01:55:42 +0000201}
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000202
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000203/// \brief Add a new line entry that has already been encoded into
204/// the internal representation of the line table.
Mike Stump11289f42009-09-09 15:08:12 +0000205void LineTableInfo::AddEntry(unsigned FID,
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000206 const std::vector<LineEntry> &Entries) {
207 LineEntries[FID] = Entries;
208}
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000209
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000210/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump11289f42009-09-09 15:08:12 +0000211///
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000212unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
213 if (LineTable == 0)
214 LineTable = new LineTableInfo();
215 return LineTable->getLineTableFilenameID(Ptr, Len);
216}
217
218
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000219/// AddLineNote - Add a line note to the line table for the FileID and offset
220/// specified by Loc. If FilenameID is -1, it is considered to be
221/// unspecified.
222void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
223 int FilenameID) {
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000224 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000226 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
227
228 // Remember that this file has #line directives now if it doesn't already.
229 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump11289f42009-09-09 15:08:12 +0000230
Chris Lattner6e0e1f42009-02-03 22:13:05 +0000231 if (LineTable == 0)
232 LineTable = new LineTableInfo();
Chris Lattner153a0f12009-02-04 00:40:31 +0000233 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000234}
235
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000236/// AddLineNote - Add a GNU line marker to the line table.
237void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
238 int FilenameID, bool IsFileEntry,
239 bool IsFileExit, bool IsSystemHeader,
240 bool IsExternCHeader) {
241 // If there is no filename and no flags, this is treated just like a #line,
242 // which does not change the flags of the previous line marker.
243 if (FilenameID == -1) {
244 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
245 "Can't set flags without setting the filename!");
246 return AddLineNote(Loc, LineNo, FilenameID);
247 }
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000249 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
250 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000252 // Remember that this file has #line directives now if it doesn't already.
253 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump11289f42009-09-09 15:08:12 +0000254
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000255 if (LineTable == 0)
256 LineTable = new LineTableInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000257
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000258 SrcMgr::CharacteristicKind FileKind;
259 if (IsExternCHeader)
260 FileKind = SrcMgr::C_ExternCSystem;
261 else if (IsSystemHeader)
262 FileKind = SrcMgr::C_System;
263 else
264 FileKind = SrcMgr::C_User;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000266 unsigned EntryExit = 0;
267 if (IsFileEntry)
268 EntryExit = 1;
269 else if (IsFileExit)
270 EntryExit = 2;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000272 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
273 EntryExit, FileKind);
274}
275
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000276LineTableInfo &SourceManager::getLineTable() {
277 if (LineTable == 0)
278 LineTable = new LineTableInfo();
279 return *LineTable;
280}
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000281
Chris Lattner153a0f12009-02-04 00:40:31 +0000282//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +0000283// Private 'Create' methods.
Chris Lattner153a0f12009-02-04 00:40:31 +0000284//===----------------------------------------------------------------------===//
Ted Kremenek12c2af42009-01-06 01:55:26 +0000285
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000286SourceManager::~SourceManager() {
287 delete LineTable;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerc8233df2009-02-03 07:30:45 +0000289 // Delete FileEntry objects corresponding to content caches. Since the actual
290 // content cache objects are bump pointer allocated, we just have to run the
291 // dtors, but we call the deallocate method for completeness.
292 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
293 MemBufferInfos[i]->~ContentCache();
294 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
295 }
296 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
297 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
298 I->second->~ContentCache();
299 ContentCacheAlloc.Deallocate(I->second);
300 }
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000301}
302
303void SourceManager::clearIDTables() {
304 MainFileID = FileID();
305 SLocEntryTable.clear();
306 LastLineNoFileIDQuery = FileID();
307 LastLineNoContentCache = 0;
308 LastFileIDLookup = FileID();
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000310 if (LineTable)
311 LineTable->clear();
Mike Stump11289f42009-09-09 15:08:12 +0000312
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000313 // Use up FileID #0 as an invalid instantiation.
314 NextOffset = 0;
Chris Lattner9dc9c202009-02-15 20:52:18 +0000315 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000316}
317
Chris Lattner4fa23622009-01-26 00:43:02 +0000318/// getOrCreateContentCache - Create or return a cached ContentCache for the
319/// specified file.
320const ContentCache *
321SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000322 assert(FileEnt && "Didn't specify a file entry to use?");
Mike Stump11289f42009-09-09 15:08:12 +0000323
Chris Lattner22eb9722006-06-18 05:43:12 +0000324 // Do we already have information about this file?
Chris Lattnerc8233df2009-02-03 07:30:45 +0000325 ContentCache *&Entry = FileInfos[FileEnt];
326 if (Entry) return Entry;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattner9be4f6d2009-02-03 07:41:46 +0000328 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
329 // so that FileInfo can use the low 3 bits of the pointer for its own
330 // nefarious purposes.
331 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
332 EntryAlign = std::max(8U, EntryAlign);
333 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattnerc8233df2009-02-03 07:30:45 +0000334 new (Entry) ContentCache(FileEnt);
Douglas Gregorea9b03e2009-09-22 21:11:38 +0000335
336 if (FileEnt == TruncateFile) {
337 // If we had queued up a file truncation request, perform the truncation
338 // now.
339 Entry->truncateAt(TruncateAtLine, TruncateAtColumn);
340 TruncateFile = 0;
341 TruncateAtLine = 0;
342 TruncateAtColumn = 0;
343 }
344
Chris Lattnerc8233df2009-02-03 07:30:45 +0000345 return Entry;
Chris Lattner22eb9722006-06-18 05:43:12 +0000346}
347
348
Ted Kremenek08bed092007-10-31 17:53:38 +0000349/// createMemBufferContentCache - Create a new ContentCache for the specified
350/// memory buffer. This does no caching.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000351const ContentCache*
352SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner9be4f6d2009-02-03 07:41:46 +0000353 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
354 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
355 // the pointer for its own nefarious purposes.
356 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
357 EntryAlign = std::max(8U, EntryAlign);
358 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattnerc8233df2009-02-03 07:30:45 +0000359 new (Entry) ContentCache();
360 MemBufferInfos.push_back(Entry);
361 Entry->setBuffer(Buffer);
362 return Entry;
Chris Lattner22eb9722006-06-18 05:43:12 +0000363}
364
Douglas Gregor258ae542009-04-27 06:38:32 +0000365void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
366 unsigned NumSLocEntries,
367 unsigned NextOffset) {
368 ExternalSLocEntries = Source;
369 this->NextOffset = NextOffset;
370 SLocEntryLoaded.resize(NumSLocEntries + 1);
371 SLocEntryLoaded[0] = true;
372 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries);
373}
374
Douglas Gregor0bc12932009-04-27 21:28:04 +0000375void SourceManager::ClearPreallocatedSLocEntries() {
376 unsigned I = 0;
377 for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
378 if (!SLocEntryLoaded[I])
379 break;
380
381 // We've already loaded all preallocated source location entries.
382 if (I == SLocEntryLoaded.size())
383 return;
384
385 // Remove everything from location I onward.
386 SLocEntryTable.resize(I);
387 SLocEntryLoaded.clear();
388 ExternalSLocEntries = 0;
389}
390
Douglas Gregor258ae542009-04-27 06:38:32 +0000391
Chris Lattner4fa23622009-01-26 00:43:02 +0000392//===----------------------------------------------------------------------===//
393// Methods to create new FileID's and instantiations.
394//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000395
Nico Weber378c5532008-09-29 00:25:48 +0000396/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremeneke26f3c52007-10-30 22:57:35 +0000397/// include position. This works regardless of whether the ContentCache
398/// corresponds to a file or some other input source.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000399FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattner4fa23622009-01-26 00:43:02 +0000400 SourceLocation IncludePos,
Douglas Gregor258ae542009-04-27 06:38:32 +0000401 SrcMgr::CharacteristicKind FileCharacter,
402 unsigned PreallocatedID,
403 unsigned Offset) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000404 if (PreallocatedID) {
405 // If we're filling in a preallocated ID, just load in the file
406 // entry and return.
Mike Stump11289f42009-09-09 15:08:12 +0000407 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000408 "Preallocate ID out-of-range");
Mike Stump11289f42009-09-09 15:08:12 +0000409 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000410 "Source location entry already loaded");
411 assert(Offset && "Preallocate source location cannot have zero offset");
Mike Stump11289f42009-09-09 15:08:12 +0000412 SLocEntryTable[PreallocatedID]
Douglas Gregor258ae542009-04-27 06:38:32 +0000413 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
414 SLocEntryLoaded[PreallocatedID] = true;
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000415 FileID FID = FileID::get(PreallocatedID);
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000416 return LastFileIDLookup = FID;
Douglas Gregor258ae542009-04-27 06:38:32 +0000417 }
418
Mike Stump11289f42009-09-09 15:08:12 +0000419 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
Chris Lattner4fa23622009-01-26 00:43:02 +0000420 FileInfo::get(IncludePos, File,
421 FileCharacter)));
Ted Kremenek12c2af42009-01-06 01:55:26 +0000422 unsigned FileSize = File->getSize();
Chris Lattner4fa23622009-01-26 00:43:02 +0000423 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
424 NextOffset += FileSize+1;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattner4fa23622009-01-26 00:43:02 +0000426 // Set LastFileIDLookup to the newly created file. The next getFileID call is
427 // almost guaranteed to be from that file.
Argyrios Kyrtzidis0152c6c2009-06-23 00:42:06 +0000428 FileID FID = FileID::get(SLocEntryTable.size()-1);
Argyrios Kyrtzidis0152c6c2009-06-23 00:42:06 +0000429 return LastFileIDLookup = FID;
Chris Lattner22eb9722006-06-18 05:43:12 +0000430}
431
Chris Lattner4fa23622009-01-26 00:43:02 +0000432/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattner53e384f2009-01-16 07:00:02 +0000433/// that a token from SpellingLoc should actually be referenced from
Chris Lattner7d6a4f62006-06-30 06:10:08 +0000434/// InstantiationLoc.
Chris Lattner4fa23622009-01-26 00:43:02 +0000435SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000436 SourceLocation ILocStart,
437 SourceLocation ILocEnd,
Douglas Gregor258ae542009-04-27 06:38:32 +0000438 unsigned TokLength,
439 unsigned PreallocatedID,
440 unsigned Offset) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000441 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
Douglas Gregor258ae542009-04-27 06:38:32 +0000442 if (PreallocatedID) {
443 // If we're filling in a preallocated ID, just load in the
444 // instantiation entry and return.
Mike Stump11289f42009-09-09 15:08:12 +0000445 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000446 "Preallocate ID out-of-range");
Mike Stump11289f42009-09-09 15:08:12 +0000447 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor258ae542009-04-27 06:38:32 +0000448 "Source location entry already loaded");
449 assert(Offset && "Preallocate source location cannot have zero offset");
450 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
451 SLocEntryLoaded[PreallocatedID] = true;
452 return SourceLocation::getMacroLoc(Offset);
453 }
Chris Lattner9dc9c202009-02-15 20:52:18 +0000454 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattner4fa23622009-01-26 00:43:02 +0000455 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
456 NextOffset += TokLength+1;
457 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Chris Lattner7d6a4f62006-06-30 06:10:08 +0000458}
459
Chris Lattner7e343b22009-01-19 07:32:13 +0000460/// getBufferData - Return a pointer to the start and end of the source buffer
461/// data for the specified FileID.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000462std::pair<const char*, const char*>
463SourceManager::getBufferData(FileID FID) const {
464 const llvm::MemoryBuffer *Buf = getBuffer(FID);
465 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
466}
467
468
Chris Lattner153a0f12009-02-04 00:40:31 +0000469//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +0000470// SourceLocation manipulation methods.
Chris Lattner153a0f12009-02-04 00:40:31 +0000471//===----------------------------------------------------------------------===//
Chris Lattner4fa23622009-01-26 00:43:02 +0000472
473/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
474/// method that is used for all SourceManager queries that start with a
475/// SourceLocation object. It is responsible for finding the entry in
476/// SLocEntryTable which contains the specified location.
477///
478FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
479 assert(SLocOffset && "Invalid FileID");
Mike Stump11289f42009-09-09 15:08:12 +0000480
Chris Lattner4fa23622009-01-26 00:43:02 +0000481 // After the first and second level caches, I see two common sorts of
482 // behavior: 1) a lot of searched FileID's are "near" the cached file location
483 // or are "near" the cached instantiation location. 2) others are just
484 // completely random and may be a very long way away.
485 //
486 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
487 // then we fall back to a less cache efficient, but more scalable, binary
488 // search to find the location.
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner4fa23622009-01-26 00:43:02 +0000490 // See if this is near the file point - worst case we start scanning from the
491 // most newly created FileID.
492 std::vector<SrcMgr::SLocEntry>::const_iterator I;
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner4fa23622009-01-26 00:43:02 +0000494 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
495 // Neither loc prunes our search.
496 I = SLocEntryTable.end();
497 } else {
498 // Perhaps it is near the file point.
499 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
500 }
501
502 // Find the FileID that contains this. "I" is an iterator that points to a
503 // FileID whose offset is known to be larger than SLocOffset.
504 unsigned NumProbes = 0;
505 while (1) {
506 --I;
Douglas Gregor258ae542009-04-27 06:38:32 +0000507 if (ExternalSLocEntries)
508 getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
Chris Lattner4fa23622009-01-26 00:43:02 +0000509 if (I->getOffset() <= SLocOffset) {
510#if 0
511 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
512 I-SLocEntryTable.begin(),
513 I->isInstantiation() ? "inst" : "file",
514 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
515#endif
516 FileID Res = FileID::get(I-SLocEntryTable.begin());
Douglas Gregor258ae542009-04-27 06:38:32 +0000517
Chris Lattner4fa23622009-01-26 00:43:02 +0000518 // If this isn't an instantiation, remember it. We have good locality
519 // across FileID lookups.
520 if (!I->isInstantiation())
521 LastFileIDLookup = Res;
522 NumLinearScans += NumProbes+1;
523 return Res;
524 }
525 if (++NumProbes == 8)
526 break;
527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattner4fa23622009-01-26 00:43:02 +0000529 // Convert "I" back into an index. We know that it is an entry whose index is
530 // larger than the offset we are looking for.
531 unsigned GreaterIndex = I-SLocEntryTable.begin();
532 // LessIndex - This is the lower bound of the range that we're searching.
533 // We know that the offset corresponding to the FileID is is less than
534 // SLocOffset.
535 unsigned LessIndex = 0;
536 NumProbes = 0;
537 while (1) {
538 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
Douglas Gregor258ae542009-04-27 06:38:32 +0000539 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner4fa23622009-01-26 00:43:02 +0000541 ++NumProbes;
Mike Stump11289f42009-09-09 15:08:12 +0000542
Chris Lattner4fa23622009-01-26 00:43:02 +0000543 // If the offset of the midpoint is too large, chop the high side of the
544 // range to the midpoint.
545 if (MidOffset > SLocOffset) {
546 GreaterIndex = MiddleIndex;
547 continue;
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattner4fa23622009-01-26 00:43:02 +0000550 // If the middle index contains the value, succeed and return.
551 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
552#if 0
553 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
554 I-SLocEntryTable.begin(),
555 I->isInstantiation() ? "inst" : "file",
556 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
557#endif
558 FileID Res = FileID::get(MiddleIndex);
559
560 // If this isn't an instantiation, remember it. We have good locality
561 // across FileID lookups.
562 if (!I->isInstantiation())
563 LastFileIDLookup = Res;
564 NumBinaryProbes += NumProbes;
565 return Res;
566 }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner4fa23622009-01-26 00:43:02 +0000568 // Otherwise, move the low-side up to the middle index.
569 LessIndex = MiddleIndex;
570 }
571}
572
Chris Lattner659ac5f2009-01-26 20:04:19 +0000573SourceLocation SourceManager::
574getInstantiationLocSlowCase(SourceLocation Loc) const {
575 do {
576 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattner9dc9c202009-02-15 20:52:18 +0000577 Loc = getSLocEntry(LocInfo.first).getInstantiation()
578 .getInstantiationLocStart();
Chris Lattner659ac5f2009-01-26 20:04:19 +0000579 Loc = Loc.getFileLocWithOffset(LocInfo.second);
580 } while (!Loc.isFileID());
581
582 return Loc;
583}
584
585SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
586 do {
587 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
588 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
589 Loc = Loc.getFileLocWithOffset(LocInfo.second);
590 } while (!Loc.isFileID());
591 return Loc;
592}
593
594
Chris Lattner4fa23622009-01-26 00:43:02 +0000595std::pair<FileID, unsigned>
596SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
597 unsigned Offset) const {
598 // If this is an instantiation record, walk through all the instantiation
599 // points.
600 FileID FID;
601 SourceLocation Loc;
602 do {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000603 Loc = E->getInstantiation().getInstantiationLocStart();
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner4fa23622009-01-26 00:43:02 +0000605 FID = getFileID(Loc);
606 E = &getSLocEntry(FID);
607 Offset += Loc.getOffset()-E->getOffset();
Chris Lattner31af4e02009-01-26 19:41:58 +0000608 } while (!Loc.isFileID());
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattner4fa23622009-01-26 00:43:02 +0000610 return std::make_pair(FID, Offset);
611}
612
613std::pair<FileID, unsigned>
614SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
615 unsigned Offset) const {
Chris Lattner31af4e02009-01-26 19:41:58 +0000616 // If this is an instantiation record, walk through all the instantiation
617 // points.
618 FileID FID;
619 SourceLocation Loc;
620 do {
621 Loc = E->getInstantiation().getSpellingLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattner31af4e02009-01-26 19:41:58 +0000623 FID = getFileID(Loc);
624 E = &getSLocEntry(FID);
625 Offset += Loc.getOffset()-E->getOffset();
626 } while (!Loc.isFileID());
Mike Stump11289f42009-09-09 15:08:12 +0000627
Chris Lattner4fa23622009-01-26 00:43:02 +0000628 return std::make_pair(FID, Offset);
629}
630
Chris Lattner8ad52d52009-02-17 08:04:48 +0000631/// getImmediateSpellingLoc - Given a SourceLocation object, return the
632/// spelling location referenced by the ID. This is the first level down
633/// towards the place where the characters that make up the lexed token can be
634/// found. This should not generally be used by clients.
635SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
636 if (Loc.isFileID()) return Loc;
637 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
638 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
639 return Loc.getFileLocWithOffset(LocInfo.second);
640}
641
642
Chris Lattner9dc9c202009-02-15 20:52:18 +0000643/// getImmediateInstantiationRange - Loc is required to be an instantiation
644/// location. Return the start/end of the instantiation information.
645std::pair<SourceLocation,SourceLocation>
646SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
647 assert(Loc.isMacroID() && "Not an instantiation loc!");
648 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
649 return II.getInstantiationLocRange();
650}
651
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000652/// getInstantiationRange - Given a SourceLocation object, return the
653/// range of tokens covered by the instantiation in the ultimate file.
654std::pair<SourceLocation,SourceLocation>
655SourceManager::getInstantiationRange(SourceLocation Loc) const {
656 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000658 std::pair<SourceLocation,SourceLocation> Res =
659 getImmediateInstantiationRange(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000661 // Fully resolve the start and end locations to their ultimate instantiation
662 // points.
663 while (!Res.first.isFileID())
664 Res.first = getImmediateInstantiationRange(Res.first).first;
665 while (!Res.second.isFileID())
666 Res.second = getImmediateInstantiationRange(Res.second).second;
667 return Res;
668}
669
Chris Lattner9dc9c202009-02-15 20:52:18 +0000670
Chris Lattner4fa23622009-01-26 00:43:02 +0000671
672//===----------------------------------------------------------------------===//
673// Queries about the code at a SourceLocation.
674//===----------------------------------------------------------------------===//
Chris Lattner30709b032006-06-21 03:01:55 +0000675
Chris Lattnerd01e2912006-06-18 16:22:51 +0000676/// getCharacterData - Return a pointer to the start of the specified location
Chris Lattner739e7392007-04-29 07:12:06 +0000677/// in the appropriate MemoryBuffer.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000678const char *SourceManager::getCharacterData(SourceLocation SL) const {
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000679 // Note that this is a hot function in the getSpelling() path, which is
680 // heavily used by -E mode.
Chris Lattner4fa23622009-01-26 00:43:02 +0000681 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Mike Stump11289f42009-09-09 15:08:12 +0000682
Ted Kremenek12c2af42009-01-06 01:55:26 +0000683 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000684 return getSLocEntry(LocInfo.first).getFile().getContentCache()
685 ->getBuffer()->getBufferStart() + LocInfo.second;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000686}
687
Chris Lattner685730f2006-06-26 01:36:22 +0000688
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000689/// getColumnNumber - Return the column # for the specified file position.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000690/// this is significantly cheaper to compute than the line number.
691unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
692 const char *Buf = getBuffer(FID)->getBufferStart();
Mike Stump11289f42009-09-09 15:08:12 +0000693
Chris Lattner22eb9722006-06-18 05:43:12 +0000694 unsigned LineStart = FilePos;
695 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
696 --LineStart;
697 return FilePos-LineStart+1;
698}
699
Chris Lattnere4ad4172009-02-04 00:55:58 +0000700unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner88ea93e2009-02-04 01:06:56 +0000701 if (Loc.isInvalid()) return 0;
Chris Lattnere4ad4172009-02-04 00:55:58 +0000702 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
703 return getColumnNumber(LocInfo.first, LocInfo.second);
704}
705
706unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner88ea93e2009-02-04 01:06:56 +0000707 if (Loc.isInvalid()) return 0;
Chris Lattnere4ad4172009-02-04 00:55:58 +0000708 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
709 return getColumnNumber(LocInfo.first, LocInfo.second);
710}
711
712
713
Benjamin Kramer5e738282009-11-14 16:36:57 +0000714static DISABLE_INLINE void ComputeLineNumbers(ContentCache* FI,
715 llvm::BumpPtrAllocator &Alloc);
Mike Stump11289f42009-09-09 15:08:12 +0000716static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenek12c2af42009-01-06 01:55:26 +0000717 // Note that calling 'getBuffer()' may lazily page in the file.
718 const MemoryBuffer *Buffer = FI->getBuffer();
Mike Stump11289f42009-09-09 15:08:12 +0000719
Chris Lattner8996fff2007-07-24 05:57:19 +0000720 // Find the file offsets of all of the *physical* source lines. This does
721 // not look at trigraphs, escaped newlines, or anything else tricky.
722 std::vector<unsigned> LineOffsets;
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner8996fff2007-07-24 05:57:19 +0000724 // Line #1 starts at char 0.
725 LineOffsets.push_back(0);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner8996fff2007-07-24 05:57:19 +0000727 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
728 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
729 unsigned Offs = 0;
730 while (1) {
731 // Skip over the contents of the line.
732 // TODO: Vectorize this? This is very performance sensitive for programs
733 // with lots of diagnostics and in -E mode.
734 const unsigned char *NextBuf = (const unsigned char *)Buf;
735 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
736 ++NextBuf;
737 Offs += NextBuf-Buf;
738 Buf = NextBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000739
Chris Lattner8996fff2007-07-24 05:57:19 +0000740 if (Buf[0] == '\n' || Buf[0] == '\r') {
741 // If this is \n\r or \r\n, skip both characters.
742 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
743 ++Offs, ++Buf;
744 ++Offs, ++Buf;
745 LineOffsets.push_back(Offs);
746 } else {
747 // Otherwise, this is a null. If end of file, exit.
748 if (Buf == End) break;
749 // Otherwise, skip the null.
750 ++Offs, ++Buf;
751 }
752 }
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner8996fff2007-07-24 05:57:19 +0000754 // Copy the offsets into the FileInfo structure.
755 FI->NumLines = LineOffsets.size();
Chris Lattnerc8233df2009-02-03 07:30:45 +0000756 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner8996fff2007-07-24 05:57:19 +0000757 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
758}
Chris Lattner9a13bde2006-06-21 04:57:09 +0000759
Chris Lattner53e384f2009-01-16 07:00:02 +0000760/// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner22eb9722006-06-18 05:43:12 +0000761/// for the position indicated. This requires building and caching a table of
Chris Lattner739e7392007-04-29 07:12:06 +0000762/// line offsets for the MemoryBuffer, so this is not cheap: use only when
Chris Lattner22eb9722006-06-18 05:43:12 +0000763/// about to emit a diagnostic.
Chris Lattner88ea93e2009-02-04 01:06:56 +0000764unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000765 ContentCache *Content;
Chris Lattner88ea93e2009-02-04 01:06:56 +0000766 if (LastLineNoFileIDQuery == FID)
Ted Kremenekc08bca62007-10-30 21:08:08 +0000767 Content = LastLineNoContentCache;
Chris Lattner8996fff2007-07-24 05:57:19 +0000768 else
Chris Lattner88ea93e2009-02-04 01:06:56 +0000769 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattner4fa23622009-01-26 00:43:02 +0000770 .getFile().getContentCache());
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner22eb9722006-06-18 05:43:12 +0000772 // If this is the first use of line information for this buffer, compute the
Chris Lattner8996fff2007-07-24 05:57:19 +0000773 /// SourceLineCache for it on demand.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000774 if (Content->SourceLineCache == 0)
Chris Lattnerc8233df2009-02-03 07:30:45 +0000775 ComputeLineNumbers(Content, ContentCacheAlloc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000776
777 // Okay, we know we have a line number table. Do a binary search to find the
778 // line number that this character position lands on.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000779 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner8996fff2007-07-24 05:57:19 +0000780 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenekc08bca62007-10-30 21:08:08 +0000781 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner88ea93e2009-02-04 01:06:56 +0000783 unsigned QueriedFilePos = FilePos+1;
Chris Lattner8996fff2007-07-24 05:57:19 +0000784
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000785 // FIXME: I would like to be convinced that this code is worth being as
Mike Stump11289f42009-09-09 15:08:12 +0000786 // complicated as it is, binary search isn't that slow.
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000787 //
788 // If it is worth being optimized, then in my opinion it could be more
789 // performant, simpler, and more obviously correct by just "galloping" outward
790 // from the queried file position. In fact, this could be incorporated into a
791 // generic algorithm such as lower_bound_with_hint.
792 //
793 // If someone gives me a test case where this matters, and I will do it! - DWD
794
Chris Lattner8996fff2007-07-24 05:57:19 +0000795 // If the previous query was to the same file, we know both the file pos from
796 // that query and the line number returned. This allows us to narrow the
797 // search space from the entire file to something near the match.
Chris Lattner88ea93e2009-02-04 01:06:56 +0000798 if (LastLineNoFileIDQuery == FID) {
Chris Lattner8996fff2007-07-24 05:57:19 +0000799 if (QueriedFilePos >= LastLineNoFilePos) {
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000800 // FIXME: Potential overflow?
Chris Lattner8996fff2007-07-24 05:57:19 +0000801 SourceLineCache = SourceLineCache+LastLineNoResult-1;
Mike Stump11289f42009-09-09 15:08:12 +0000802
Chris Lattner8996fff2007-07-24 05:57:19 +0000803 // The query is likely to be nearby the previous one. Here we check to
804 // see if it is within 5, 10 or 20 lines. It can be far away in cases
805 // where big comment blocks and vertical whitespace eat up lines but
806 // contribute no tokens.
807 if (SourceLineCache+5 < SourceLineCacheEnd) {
808 if (SourceLineCache[5] > QueriedFilePos)
809 SourceLineCacheEnd = SourceLineCache+5;
810 else if (SourceLineCache+10 < SourceLineCacheEnd) {
811 if (SourceLineCache[10] > QueriedFilePos)
812 SourceLineCacheEnd = SourceLineCache+10;
813 else if (SourceLineCache+20 < SourceLineCacheEnd) {
814 if (SourceLineCache[20] > QueriedFilePos)
815 SourceLineCacheEnd = SourceLineCache+20;
816 }
817 }
818 }
819 } else {
Daniel Dunbar70f924df82009-05-18 17:30:52 +0000820 if (LastLineNoResult < Content->NumLines)
821 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
Chris Lattner8996fff2007-07-24 05:57:19 +0000822 }
823 }
Mike Stump11289f42009-09-09 15:08:12 +0000824
Chris Lattner830a77f2007-07-24 06:43:46 +0000825 // If the spread is large, do a "radix" test as our initial guess, based on
826 // the assumption that lines average to approximately the same length.
827 // NOTE: This is currently disabled, as it does not appear to be profitable in
828 // initial measurements.
829 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenekc08bca62007-10-30 21:08:08 +0000830 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner830a77f2007-07-24 06:43:46 +0000832 // Take a stab at guessing where it is.
Ted Kremenekc08bca62007-10-30 21:08:08 +0000833 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattner830a77f2007-07-24 06:43:46 +0000835 // Check for -10 and +10 lines.
836 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
837 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
838
839 // If the computed lower bound is less than the query location, move it in.
840 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
841 SourceLineCacheStart[LowerBound] < QueriedFilePos)
842 SourceLineCache = SourceLineCacheStart+LowerBound;
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattner830a77f2007-07-24 06:43:46 +0000844 // If the computed upper bound is greater than the query location, move it.
845 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
846 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
847 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattner830a77f2007-07-24 06:43:46 +0000850 unsigned *Pos
851 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner8996fff2007-07-24 05:57:19 +0000852 unsigned LineNo = Pos-SourceLineCacheStart;
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner88ea93e2009-02-04 01:06:56 +0000854 LastLineNoFileIDQuery = FID;
Ted Kremenekc08bca62007-10-30 21:08:08 +0000855 LastLineNoContentCache = Content;
Chris Lattner8996fff2007-07-24 05:57:19 +0000856 LastLineNoFilePos = QueriedFilePos;
857 LastLineNoResult = LineNo;
858 return LineNo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000859}
860
Chris Lattner88ea93e2009-02-04 01:06:56 +0000861unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
862 if (Loc.isInvalid()) return 0;
863 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
864 return getLineNumber(LocInfo.first, LocInfo.second);
865}
866unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
867 if (Loc.isInvalid()) return 0;
868 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
869 return getLineNumber(LocInfo.first, LocInfo.second);
870}
871
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000872/// getFileCharacteristic - return the file characteristic of the specified
Mike Stump11289f42009-09-09 15:08:12 +0000873/// source location, indicating whether this is a normal file, a system
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000874/// header, or an "implicit extern C" system header.
875///
876/// This state can be modified with flags on GNU linemarker directives like:
877/// # 4 "foo.h" 3
878/// which changes all source locations in the current file after that to be
879/// considered to be from a system header.
Mike Stump11289f42009-09-09 15:08:12 +0000880SrcMgr::CharacteristicKind
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000881SourceManager::getFileCharacteristic(SourceLocation Loc) const {
882 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
883 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
884 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
885
886 // If there are no #line directives in this file, just return the whole-file
887 // state.
888 if (!FI.hasLineDirectives())
889 return FI.getFileCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000890
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000891 assert(LineTable && "Can't have linetable entries without a LineTable!");
892 // See if there is a #line directive before the location.
893 const LineEntry *Entry =
894 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
Mike Stump11289f42009-09-09 15:08:12 +0000895
Chris Lattner95d9c5e2009-02-04 05:33:01 +0000896 // If this is before the first line marker, use the file characteristic.
897 if (!Entry)
898 return FI.getFileCharacteristic();
899
900 return Entry->FileKind;
901}
902
Chris Lattnera6f037c2009-02-17 08:39:06 +0000903/// Return the filename or buffer identifier of the buffer the location is in.
904/// Note that this name does not respect #line directives. Use getPresumedLoc
905/// for normal clients.
906const char *SourceManager::getBufferName(SourceLocation Loc) const {
907 if (Loc.isInvalid()) return "<invalid loc>";
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattnera6f037c2009-02-17 08:39:06 +0000909 return getBuffer(getFileID(Loc))->getBufferIdentifier();
910}
911
Chris Lattner88ea93e2009-02-04 01:06:56 +0000912
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000913/// getPresumedLoc - This method returns the "presumed" location of a
914/// SourceLocation specifies. A "presumed location" can be modified by #line
915/// or GNU line marker directives. This provides a view on the data that a
916/// user should see in diagnostics, for example.
917///
918/// Note that a presumed location is always given as the instantiation point
919/// of an instantiation location, not at the spelling location.
920PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
921 if (Loc.isInvalid()) return PresumedLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000922
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000923 // Presumed locations are always for instantiation points.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000924 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattner88ea93e2009-02-04 01:06:56 +0000926 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000927 const SrcMgr::ContentCache *C = FI.getContentCache();
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattnerd4293922009-02-04 01:55:42 +0000929 // To get the source name, first consult the FileEntry (if one exists)
930 // before the MemBuffer as this will avoid unnecessarily paging in the
931 // MemBuffer.
Mike Stump11289f42009-09-09 15:08:12 +0000932 const char *Filename =
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000933 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattnerd4293922009-02-04 01:55:42 +0000934 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
935 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
936 SourceLocation IncludeLoc = FI.getIncludeLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000937
Chris Lattnerd4293922009-02-04 01:55:42 +0000938 // If we have #line directives in this file, update and overwrite the physical
939 // location info if appropriate.
940 if (FI.hasLineDirectives()) {
941 assert(LineTable && "Can't have linetable entries without a LineTable!");
942 // See if there is a #line directive before this. If so, get it.
943 if (const LineEntry *Entry =
944 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerc1219ff2009-02-04 02:00:59 +0000945 // If the LineEntry indicates a filename, use it.
Chris Lattnerd4293922009-02-04 01:55:42 +0000946 if (Entry->FilenameID != -1)
947 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerc1219ff2009-02-04 02:00:59 +0000948
949 // Use the line number specified by the LineEntry. This line number may
950 // be multiple lines down from the line entry. Add the difference in
951 // physical line numbers from the query point and the line marker to the
952 // total.
953 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
954 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
Mike Stump11289f42009-09-09 15:08:12 +0000955
Chris Lattner20c50ba2009-02-04 02:15:40 +0000956 // Note that column numbers are not molested by line markers.
Mike Stump11289f42009-09-09 15:08:12 +0000957
Chris Lattner1c967782009-02-04 06:25:26 +0000958 // Handle virtual #include manipulation.
959 if (Entry->IncludeOffset) {
960 IncludeLoc = getLocForStartOfFile(LocInfo.first);
961 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
962 }
Chris Lattnerd4293922009-02-04 01:55:42 +0000963 }
964 }
965
966 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattner4fa23622009-01-26 00:43:02 +0000967}
968
969//===----------------------------------------------------------------------===//
970// Other miscellaneous methods.
971//===----------------------------------------------------------------------===//
972
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000973/// \brief Get the source location for the given file:line:col triplet.
974///
975/// If the source file is included multiple times, the source location will
976/// be based upon the first inclusion.
977SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
978 unsigned Line, unsigned Col) const {
979 assert(SourceFile && "Null source file!");
980 assert(Line && Col && "Line and column should start from 1!");
981
982 fileinfo_iterator FI = FileInfos.find(SourceFile);
983 if (FI == FileInfos.end())
984 return SourceLocation();
985 ContentCache *Content = FI->second;
Mike Stump11289f42009-09-09 15:08:12 +0000986
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000987 // If this is the first use of line information for this buffer, compute the
988 /// SourceLineCache for it on demand.
989 if (Content->SourceLineCache == 0)
990 ComputeLineNumbers(Content, ContentCacheAlloc);
991
992 if (Line > Content->NumLines)
993 return SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000994
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000995 unsigned FilePos = Content->SourceLineCache[Line - 1];
Argyrios Kyrtzidis0e5ecbd2009-06-25 18:22:16 +0000996 const char *Buf = Content->getBuffer()->getBufferStart() + FilePos;
Argyrios Kyrtzidis69c2e062009-06-20 08:40:15 +0000997 unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf;
998 unsigned i = 0;
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +0000999
1000 // Check that the given column is valid.
Argyrios Kyrtzidis69c2e062009-06-20 08:40:15 +00001001 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1002 ++i;
1003 if (i < Col-1)
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +00001004 return SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001005
Douglas Gregor2a1b6912009-12-02 05:34:39 +00001006 // Find the first file ID that corresponds to the given file.
1007 FileID FirstFID;
1008
1009 // First, check the main file ID, since it is common to look for a
1010 // location in the main file.
1011 if (!MainFileID.isInvalid()) {
1012 const SLocEntry &MainSLoc = getSLocEntry(MainFileID);
1013 if (MainSLoc.isFile() && MainSLoc.getFile().getContentCache() == Content)
1014 FirstFID = MainFileID;
1015 }
1016
1017 if (FirstFID.isInvalid()) {
1018 // The location we're looking for isn't in the main file; look
1019 // through all of the source locations.
1020 for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
1021 const SLocEntry &SLoc = getSLocEntry(I);
1022 if (SLoc.isFile() && SLoc.getFile().getContentCache() == Content) {
1023 FirstFID = FileID::get(I);
1024 break;
1025 }
1026 }
1027 }
1028
1029 if (FirstFID.isInvalid())
1030 return SourceLocation();
1031
1032 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1);
Argyrios Kyrtzidis88f663c02009-06-20 08:09:57 +00001033}
1034
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001035/// \brief Determines the order of 2 source locations in the translation unit.
1036///
1037/// \returns true if LHS source location comes before RHS, false otherwise.
1038bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
1039 SourceLocation RHS) const {
1040 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
1041 if (LHS == RHS)
1042 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001043
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001044 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
1045 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00001046
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001047 // If the source locations are in the same file, just compare offsets.
1048 if (LOffs.first == ROffs.first)
1049 return LOffs.second < ROffs.second;
1050
1051 // If we are comparing a source location with multiple locations in the same
1052 // file, we get a big win by caching the result.
Mike Stump11289f42009-09-09 15:08:12 +00001053
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001054 if (LastLFIDForBeforeTUCheck == LOffs.first &&
1055 LastRFIDForBeforeTUCheck == ROffs.first)
1056 return LastResForBeforeTUCheck;
Mike Stump11289f42009-09-09 15:08:12 +00001057
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001058 LastLFIDForBeforeTUCheck = LOffs.first;
1059 LastRFIDForBeforeTUCheck = ROffs.first;
Mike Stump11289f42009-09-09 15:08:12 +00001060
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001061 // "Traverse" the include/instantiation stacks of both locations and try to
1062 // find a common "ancestor".
1063 //
1064 // First we traverse the stack of the right location and check each level
1065 // against the level of the left location, while collecting all levels in a
1066 // "stack map".
1067
1068 std::map<FileID, unsigned> ROffsMap;
1069 ROffsMap[ROffs.first] = ROffs.second;
1070
1071 while (1) {
1072 SourceLocation UpperLoc;
1073 const SrcMgr::SLocEntry &Entry = getSLocEntry(ROffs.first);
1074 if (Entry.isInstantiation())
1075 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1076 else
1077 UpperLoc = Entry.getFile().getIncludeLoc();
Mike Stump11289f42009-09-09 15:08:12 +00001078
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001079 if (UpperLoc.isInvalid())
1080 break; // We reached the top.
Mike Stump11289f42009-09-09 15:08:12 +00001081
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001082 ROffs = getDecomposedLoc(UpperLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001083
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001084 if (LOffs.first == ROffs.first)
1085 return LastResForBeforeTUCheck = LOffs.second < ROffs.second;
Mike Stump11289f42009-09-09 15:08:12 +00001086
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001087 ROffsMap[ROffs.first] = ROffs.second;
1088 }
1089
1090 // We didn't find a common ancestor. Now traverse the stack of the left
1091 // location, checking against the stack map of the right location.
1092
1093 while (1) {
1094 SourceLocation UpperLoc;
1095 const SrcMgr::SLocEntry &Entry = getSLocEntry(LOffs.first);
1096 if (Entry.isInstantiation())
1097 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1098 else
1099 UpperLoc = Entry.getFile().getIncludeLoc();
Mike Stump11289f42009-09-09 15:08:12 +00001100
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001101 if (UpperLoc.isInvalid())
1102 break; // We reached the top.
Mike Stump11289f42009-09-09 15:08:12 +00001103
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001104 LOffs = getDecomposedLoc(UpperLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001106 std::map<FileID, unsigned>::iterator I = ROffsMap.find(LOffs.first);
1107 if (I != ROffsMap.end())
1108 return LastResForBeforeTUCheck = LOffs.second < I->second;
1109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Daniel Dunbar465f4c42009-12-01 23:07:57 +00001111 // There is no common ancestor, most probably because one location is in the
1112 // predefines buffer.
1113 //
1114 // FIXME: We should rearrange the external interface so this simply never
1115 // happens; it can't conceptually happen. Also see PR5662.
Mike Stump11289f42009-09-09 15:08:12 +00001116
Daniel Dunbar465f4c42009-12-01 23:07:57 +00001117 // If exactly one location is a memory buffer, assume it preceeds the other.
1118 bool LIsMB = !getSLocEntry(LOffs.first).getFile().getContentCache()->Entry;
1119 bool RIsMB = !getSLocEntry(ROffs.first).getFile().getContentCache()->Entry;
1120 if (LIsMB != RIsMB)
1121 return LastResForBeforeTUCheck = LIsMB;
Mike Stump11289f42009-09-09 15:08:12 +00001122
Daniel Dunbar465f4c42009-12-01 23:07:57 +00001123 // Otherwise, just assume FileIDs were created in order.
1124 return LastResForBeforeTUCheck = (LOffs.first < ROffs.first);
Argyrios Kyrtzidis33661d92009-06-23 22:01:48 +00001125}
Chris Lattner4fa23622009-01-26 00:43:02 +00001126
Douglas Gregorea9b03e2009-09-22 21:11:38 +00001127void SourceManager::truncateFileAt(const FileEntry *Entry, unsigned Line,
1128 unsigned Column) {
1129 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator FI
1130 = FileInfos.find(Entry);
1131 if (FI != FileInfos.end()) {
1132 FI->second->truncateAt(Line, Column);
1133 return;
1134 }
1135
1136 // We cannot perform the truncation until we actually see the file, so
1137 // save the truncation information.
1138 assert(TruncateFile == 0 && "Can't queue up multiple file truncations!");
1139 TruncateFile = Entry;
1140 TruncateAtLine = Line;
1141 TruncateAtColumn = Column;
1142}
1143
1144/// \brief Determine whether this file was truncated.
1145bool SourceManager::isTruncatedFile(FileID FID) const {
1146 return getSLocEntry(FID).getFile().getContentCache()->isTruncated();
1147}
1148
Chris Lattner22eb9722006-06-18 05:43:12 +00001149/// PrintStats - Print statistics to stderr.
1150///
1151void SourceManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +00001152 llvm::errs() << "\n*** Source Manager Stats:\n";
1153 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
1154 << " mem buffers mapped.\n";
1155 llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
1156 << NextOffset << "B of Sloc address space used.\n";
Mike Stump11289f42009-09-09 15:08:12 +00001157
Chris Lattner22eb9722006-06-18 05:43:12 +00001158 unsigned NumLineNumsComputed = 0;
1159 unsigned NumFileBytesMapped = 0;
Chris Lattnerc8233df2009-02-03 07:30:45 +00001160 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
1161 NumLineNumsComputed += I->second->SourceLineCache != 0;
1162 NumFileBytesMapped += I->second->getSizeBytesMapped();
Chris Lattner22eb9722006-06-18 05:43:12 +00001163 }
Mike Stump11289f42009-09-09 15:08:12 +00001164
Benjamin Kramer89b422c2009-08-23 12:08:50 +00001165 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
1166 << NumLineNumsComputed << " files with line #'s computed.\n";
1167 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
1168 << NumBinaryProbes << " binary.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +00001169}
Douglas Gregor258ae542009-04-27 06:38:32 +00001170
1171ExternalSLocEntrySource::~ExternalSLocEntrySource() { }