blob: ed5eb46c2847f49913fdc57642981ddee203d1bd [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceManager.h"
Douglas Gregorcdad0ba2009-04-13 15:31:25 +000015#include "clang/Basic/SourceManagerInternals.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/FileManager.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/System/Path.h"
Ted Kremenekda29d8c2007-12-05 22:21:13 +000020#include "llvm/Support/Streams.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include <algorithm>
Douglas Gregor608ff622009-04-22 21:45:53 +000022#include <iostream>
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Chris Lattnerfa45efd2009-02-04 00:40:31 +000027//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +000028// SourceManager Helper Classes
Chris Lattnerfa45efd2009-02-04 00:40:31 +000029//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +000030
Ted Kremenekdd364ea2007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
Chris Lattner4b009652007-07-25 00:24:17 +000033}
34
Ted Kremenekaa7dac12009-01-06 01:55:26 +000035/// getSizeBytesMapped - Returns the number of bytes actually mapped for
36/// this ContentCache. This can be 0 if the MemBuffer was not actually
37/// instantiated.
38unsigned ContentCache::getSizeBytesMapped() const {
39 return Buffer ? Buffer->getBufferSize() : 0;
40}
41
42/// getSize - Returns the size of the content encapsulated by this ContentCache.
43/// This can be the size of the source file or the size of an arbitrary
44/// scratch buffer. If the ContentCache encapsulates a source file, that
45/// file is not lazily brought in from disk to satisfy this query.
46unsigned ContentCache::getSize() const {
47 return Entry ? Entry->getSize() : Buffer->getBufferSize();
48}
49
Chris Lattner68b28ff2009-01-26 07:37:49 +000050const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek2bb9e6c2009-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 Lattnerac49bb42009-01-17 03:54:16 +000055 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000056 }
Ted Kremenekaa7dac12009-01-06 01:55:26 +000057 return Buffer;
58}
59
Chris Lattnerd2051b42009-01-26 07:57:50 +000060unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
61 // Look up the filename in the string table, returning the pre-existing value
62 // if it exists.
63 llvm::StringMapEntry<unsigned> &Entry =
64 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
65 if (Entry.getValue() != ~0U)
66 return Entry.getValue();
67
68 // Otherwise, assign this the next available ID.
69 Entry.setValue(FilenamesByID.size());
70 FilenamesByID.push_back(&Entry);
71 return FilenamesByID.size()-1;
72}
73
Chris Lattner6256b2b2009-02-03 22:13:05 +000074/// AddLineNote - Add a line note to the line table that indicates that there
75/// is a #line at the specified FID/Offset location which changes the presumed
76/// location to LineNo/FilenameID.
Chris Lattnerfa45efd2009-02-04 00:40:31 +000077void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattner6256b2b2009-02-03 22:13:05 +000078 unsigned LineNo, int FilenameID) {
Chris Lattnerfa45efd2009-02-04 00:40:31 +000079 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattner6256b2b2009-02-03 22:13:05 +000080
Chris Lattnerfa45efd2009-02-04 00:40:31 +000081 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
82 "Adding line entries out of order!");
Chris Lattner6f9fa5a2009-02-04 01:55:42 +000083
Chris Lattner7bf78512009-02-04 05:21:58 +000084 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner778ae212009-02-04 06:25:26 +000085 unsigned IncludeOffset = 0;
Chris Lattner6f9fa5a2009-02-04 01:55:42 +000086
Chris Lattner7bf78512009-02-04 05:21:58 +000087 if (!Entries.empty()) {
88 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
89 // that we are still in "foo.h".
90 if (FilenameID == -1)
91 FilenameID = Entries.back().FilenameID;
92
Chris Lattner778ae212009-02-04 06:25:26 +000093 // If we are after a line marker that switched us to system header mode, or
94 // that set #include information, preserve it.
Chris Lattner7bf78512009-02-04 05:21:58 +000095 Kind = Entries.back().FileKind;
Chris Lattner778ae212009-02-04 06:25:26 +000096 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner7bf78512009-02-04 05:21:58 +000097 }
98
Chris Lattner778ae212009-02-04 06:25:26 +000099 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
100 IncludeOffset));
Chris Lattner6256b2b2009-02-03 22:13:05 +0000101}
102
Chris Lattner7bf78512009-02-04 05:21:58 +0000103/// AddLineNote This is the same as the previous version of AddLineNote, but is
104/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
105/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
106/// this is a file exit. FileKind specifies whether this is a system header or
107/// extern C system header.
108void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
109 unsigned LineNo, int FilenameID,
110 unsigned EntryExit,
111 SrcMgr::CharacteristicKind FileKind) {
112 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
113
114 std::vector<LineEntry> &Entries = LineEntries[FID];
115
116 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
117 "Adding line entries out of order!");
118
Chris Lattner778ae212009-02-04 06:25:26 +0000119 unsigned IncludeOffset = 0;
120 if (EntryExit == 0) { // No #include stack change.
121 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
122 } else if (EntryExit == 1) {
123 IncludeOffset = Offset-1;
124 } else if (EntryExit == 2) {
125 assert(!Entries.empty() && Entries.back().IncludeOffset &&
126 "PPDirectives should have caught case when popping empty include stack");
127
128 // Get the include loc of the last entries' include loc as our include loc.
129 IncludeOffset = 0;
130 if (const LineEntry *PrevEntry =
131 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
132 IncludeOffset = PrevEntry->IncludeOffset;
133 }
Chris Lattner7bf78512009-02-04 05:21:58 +0000134
Chris Lattner778ae212009-02-04 06:25:26 +0000135 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
136 IncludeOffset));
Chris Lattner7bf78512009-02-04 05:21:58 +0000137}
138
139
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000140/// FindNearestLineEntry - Find the line entry nearest to FID that is before
141/// it. If there is no line entry before Offset in FID, return null.
142const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
143 unsigned Offset) {
144 const std::vector<LineEntry> &Entries = LineEntries[FID];
145 assert(!Entries.empty() && "No #line entries for this FID after all!");
146
Chris Lattner1a2fbe42009-02-04 04:46:59 +0000147 // It is very common for the query to be after the last #line, check this
148 // first.
149 if (Entries.back().FileOffset <= Offset)
150 return &Entries.back();
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000151
Chris Lattner1a2fbe42009-02-04 04:46:59 +0000152 // Do a binary search to find the maximal element that is still before Offset.
153 std::vector<LineEntry>::const_iterator I =
154 std::upper_bound(Entries.begin(), Entries.end(), Offset);
155 if (I == Entries.begin()) return 0;
156 return &*--I;
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000157}
Chris Lattner6256b2b2009-02-03 22:13:05 +0000158
Douglas Gregor635f97f2009-04-13 16:31:14 +0000159/// \brief Add a new line entry that has already been encoded into
160/// the internal representation of the line table.
161void LineTableInfo::AddEntry(unsigned FID,
162 const std::vector<LineEntry> &Entries) {
163 LineEntries[FID] = Entries;
164}
Chris Lattner6256b2b2009-02-03 22:13:05 +0000165
Chris Lattnerd2051b42009-01-26 07:57:50 +0000166/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
167///
168unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
169 if (LineTable == 0)
170 LineTable = new LineTableInfo();
171 return LineTable->getLineTableFilenameID(Ptr, Len);
172}
173
174
Chris Lattnerb06739c2009-02-03 21:52:55 +0000175/// AddLineNote - Add a line note to the line table for the FileID and offset
176/// specified by Loc. If FilenameID is -1, it is considered to be
177/// unspecified.
178void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
179 int FilenameID) {
Chris Lattner6256b2b2009-02-03 22:13:05 +0000180 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb06739c2009-02-03 21:52:55 +0000181
Chris Lattner6256b2b2009-02-03 22:13:05 +0000182 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
183
184 // Remember that this file has #line directives now if it doesn't already.
185 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
186
187 if (LineTable == 0)
188 LineTable = new LineTableInfo();
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000189 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattnerb06739c2009-02-03 21:52:55 +0000190}
191
Chris Lattner7bf78512009-02-04 05:21:58 +0000192/// AddLineNote - Add a GNU line marker to the line table.
193void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
194 int FilenameID, bool IsFileEntry,
195 bool IsFileExit, bool IsSystemHeader,
196 bool IsExternCHeader) {
197 // If there is no filename and no flags, this is treated just like a #line,
198 // which does not change the flags of the previous line marker.
199 if (FilenameID == -1) {
200 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
201 "Can't set flags without setting the filename!");
202 return AddLineNote(Loc, LineNo, FilenameID);
203 }
204
205 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
206 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
207
208 // Remember that this file has #line directives now if it doesn't already.
209 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
210
211 if (LineTable == 0)
212 LineTable = new LineTableInfo();
213
214 SrcMgr::CharacteristicKind FileKind;
215 if (IsExternCHeader)
216 FileKind = SrcMgr::C_ExternCSystem;
217 else if (IsSystemHeader)
218 FileKind = SrcMgr::C_System;
219 else
220 FileKind = SrcMgr::C_User;
221
222 unsigned EntryExit = 0;
223 if (IsFileEntry)
224 EntryExit = 1;
225 else if (IsFileExit)
226 EntryExit = 2;
227
228 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
229 EntryExit, FileKind);
230}
231
Douglas Gregor635f97f2009-04-13 16:31:14 +0000232LineTableInfo &SourceManager::getLineTable() {
233 if (LineTable == 0)
234 LineTable = new LineTableInfo();
235 return *LineTable;
236}
Chris Lattnerb06739c2009-02-03 21:52:55 +0000237
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000238//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000239// Private 'Create' methods.
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000240//===----------------------------------------------------------------------===//
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000241
Chris Lattnerd2051b42009-01-26 07:57:50 +0000242SourceManager::~SourceManager() {
243 delete LineTable;
Chris Lattner8dedb842009-02-03 07:30:45 +0000244
245 // Delete FileEntry objects corresponding to content caches. Since the actual
246 // content cache objects are bump pointer allocated, we just have to run the
247 // dtors, but we call the deallocate method for completeness.
248 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
249 MemBufferInfos[i]->~ContentCache();
250 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
251 }
252 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
253 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
254 I->second->~ContentCache();
255 ContentCacheAlloc.Deallocate(I->second);
256 }
Chris Lattnerd2051b42009-01-26 07:57:50 +0000257}
258
259void SourceManager::clearIDTables() {
260 MainFileID = FileID();
261 SLocEntryTable.clear();
262 LastLineNoFileIDQuery = FileID();
263 LastLineNoContentCache = 0;
264 LastFileIDLookup = FileID();
265
266 if (LineTable)
267 LineTable->clear();
268
269 // Use up FileID #0 as an invalid instantiation.
270 NextOffset = 0;
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000271 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattnerd2051b42009-01-26 07:57:50 +0000272}
273
Chris Lattner27c0ced2009-01-26 00:43:02 +0000274/// getOrCreateContentCache - Create or return a cached ContentCache for the
275/// specified file.
276const ContentCache *
277SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Chris Lattner4b009652007-07-25 00:24:17 +0000278 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattner27c0ced2009-01-26 00:43:02 +0000279
Chris Lattner4b009652007-07-25 00:24:17 +0000280 // Do we already have information about this file?
Chris Lattner8dedb842009-02-03 07:30:45 +0000281 ContentCache *&Entry = FileInfos[FileEnt];
282 if (Entry) return Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000283
Chris Lattner917d7c62009-02-03 07:41:46 +0000284 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
285 // so that FileInfo can use the low 3 bits of the pointer for its own
286 // nefarious purposes.
287 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
288 EntryAlign = std::max(8U, EntryAlign);
289 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner8dedb842009-02-03 07:30:45 +0000290 new (Entry) ContentCache(FileEnt);
291 return Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000292}
293
294
Ted Kremenek27f9c9b2007-10-31 17:53:38 +0000295/// createMemBufferContentCache - Create a new ContentCache for the specified
296/// memory buffer. This does no caching.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000297const ContentCache*
298SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner917d7c62009-02-03 07:41:46 +0000299 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
300 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
301 // the pointer for its own nefarious purposes.
302 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
303 EntryAlign = std::max(8U, EntryAlign);
304 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner8dedb842009-02-03 07:30:45 +0000305 new (Entry) ContentCache();
306 MemBufferInfos.push_back(Entry);
307 Entry->setBuffer(Buffer);
308 return Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000309}
310
Douglas Gregor32e231c2009-04-27 06:38:32 +0000311void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
312 unsigned NumSLocEntries,
313 unsigned NextOffset) {
314 ExternalSLocEntries = Source;
315 this->NextOffset = NextOffset;
316 SLocEntryLoaded.resize(NumSLocEntries + 1);
317 SLocEntryLoaded[0] = true;
318 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries);
319}
320
Douglas Gregor57885192009-04-27 21:28:04 +0000321void SourceManager::ClearPreallocatedSLocEntries() {
322 unsigned I = 0;
323 for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
324 if (!SLocEntryLoaded[I])
325 break;
326
327 // We've already loaded all preallocated source location entries.
328 if (I == SLocEntryLoaded.size())
329 return;
330
331 // Remove everything from location I onward.
332 SLocEntryTable.resize(I);
333 SLocEntryLoaded.clear();
334 ExternalSLocEntries = 0;
335}
336
Douglas Gregor32e231c2009-04-27 06:38:32 +0000337
Chris Lattner27c0ced2009-01-26 00:43:02 +0000338//===----------------------------------------------------------------------===//
339// Methods to create new FileID's and instantiations.
340//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000341
Nico Weber630347d2008-09-29 00:25:48 +0000342/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek7670cca2007-10-30 22:57:35 +0000343/// include position. This works regardless of whether the ContentCache
344/// corresponds to a file or some other input source.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000345FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000346 SourceLocation IncludePos,
Douglas Gregor32e231c2009-04-27 06:38:32 +0000347 SrcMgr::CharacteristicKind FileCharacter,
348 unsigned PreallocatedID,
349 unsigned Offset) {
Douglas Gregor32e231c2009-04-27 06:38:32 +0000350 if (PreallocatedID) {
351 // If we're filling in a preallocated ID, just load in the file
352 // entry and return.
353 assert(PreallocatedID < SLocEntryLoaded.size() &&
354 "Preallocate ID out-of-range");
355 assert(!SLocEntryLoaded[PreallocatedID] &&
356 "Source location entry already loaded");
357 assert(Offset && "Preallocate source location cannot have zero offset");
358 SLocEntryTable[PreallocatedID]
359 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
360 SLocEntryLoaded[PreallocatedID] = true;
361 return LastFileIDLookup = FileID::get(PreallocatedID);
362 }
363
Chris Lattner27c0ced2009-01-26 00:43:02 +0000364 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
365 FileInfo::get(IncludePos, File,
366 FileCharacter)));
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000367 unsigned FileSize = File->getSize();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000368 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
369 NextOffset += FileSize+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000370
Chris Lattner27c0ced2009-01-26 00:43:02 +0000371 // Set LastFileIDLookup to the newly created file. The next getFileID call is
372 // almost guaranteed to be from that file.
373 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000374}
375
Chris Lattner27c0ced2009-01-26 00:43:02 +0000376/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnercdf600e2009-01-16 07:00:02 +0000377/// that a token from SpellingLoc should actually be referenced from
Chris Lattner4b009652007-07-25 00:24:17 +0000378/// InstantiationLoc.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000379SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000380 SourceLocation ILocStart,
381 SourceLocation ILocEnd,
Douglas Gregor32e231c2009-04-27 06:38:32 +0000382 unsigned TokLength,
383 unsigned PreallocatedID,
384 unsigned Offset) {
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000385 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000386 if (PreallocatedID) {
387 // If we're filling in a preallocated ID, just load in the
388 // instantiation entry and return.
389 assert(PreallocatedID < SLocEntryLoaded.size() &&
390 "Preallocate ID out-of-range");
391 assert(!SLocEntryLoaded[PreallocatedID] &&
392 "Source location entry already loaded");
393 assert(Offset && "Preallocate source location cannot have zero offset");
394 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
395 SLocEntryLoaded[PreallocatedID] = true;
396 return SourceLocation::getMacroLoc(Offset);
397 }
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000398 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattner27c0ced2009-01-26 00:43:02 +0000399 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
400 NextOffset += TokLength+1;
401 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Chris Lattner4b009652007-07-25 00:24:17 +0000402}
403
Chris Lattner71e443a2009-01-19 07:32:13 +0000404/// getBufferData - Return a pointer to the start and end of the source buffer
405/// data for the specified FileID.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000406std::pair<const char*, const char*>
407SourceManager::getBufferData(FileID FID) const {
408 const llvm::MemoryBuffer *Buf = getBuffer(FID);
409 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
410}
411
412
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000413//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000414// SourceLocation manipulation methods.
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000415//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000416
417/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
418/// method that is used for all SourceManager queries that start with a
419/// SourceLocation object. It is responsible for finding the entry in
420/// SLocEntryTable which contains the specified location.
421///
422FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
423 assert(SLocOffset && "Invalid FileID");
424
425 // After the first and second level caches, I see two common sorts of
426 // behavior: 1) a lot of searched FileID's are "near" the cached file location
427 // or are "near" the cached instantiation location. 2) others are just
428 // completely random and may be a very long way away.
429 //
430 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
431 // then we fall back to a less cache efficient, but more scalable, binary
432 // search to find the location.
433
434 // See if this is near the file point - worst case we start scanning from the
435 // most newly created FileID.
436 std::vector<SrcMgr::SLocEntry>::const_iterator I;
437
438 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
439 // Neither loc prunes our search.
440 I = SLocEntryTable.end();
441 } else {
442 // Perhaps it is near the file point.
443 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
444 }
445
446 // Find the FileID that contains this. "I" is an iterator that points to a
447 // FileID whose offset is known to be larger than SLocOffset.
448 unsigned NumProbes = 0;
449 while (1) {
450 --I;
Douglas Gregor32e231c2009-04-27 06:38:32 +0000451 if (ExternalSLocEntries)
452 getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
Chris Lattner27c0ced2009-01-26 00:43:02 +0000453 if (I->getOffset() <= SLocOffset) {
454#if 0
455 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
456 I-SLocEntryTable.begin(),
457 I->isInstantiation() ? "inst" : "file",
458 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
459#endif
460 FileID Res = FileID::get(I-SLocEntryTable.begin());
Douglas Gregor32e231c2009-04-27 06:38:32 +0000461
Chris Lattner27c0ced2009-01-26 00:43:02 +0000462 // If this isn't an instantiation, remember it. We have good locality
463 // across FileID lookups.
464 if (!I->isInstantiation())
465 LastFileIDLookup = Res;
466 NumLinearScans += NumProbes+1;
467 return Res;
468 }
469 if (++NumProbes == 8)
470 break;
471 }
472
473 // Convert "I" back into an index. We know that it is an entry whose index is
474 // larger than the offset we are looking for.
475 unsigned GreaterIndex = I-SLocEntryTable.begin();
476 // LessIndex - This is the lower bound of the range that we're searching.
477 // We know that the offset corresponding to the FileID is is less than
478 // SLocOffset.
479 unsigned LessIndex = 0;
480 NumProbes = 0;
481 while (1) {
482 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
Douglas Gregor32e231c2009-04-27 06:38:32 +0000483 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000484
485 ++NumProbes;
486
487 // If the offset of the midpoint is too large, chop the high side of the
488 // range to the midpoint.
489 if (MidOffset > SLocOffset) {
490 GreaterIndex = MiddleIndex;
491 continue;
492 }
493
494 // If the middle index contains the value, succeed and return.
495 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
496#if 0
497 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
498 I-SLocEntryTable.begin(),
499 I->isInstantiation() ? "inst" : "file",
500 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
501#endif
502 FileID Res = FileID::get(MiddleIndex);
503
504 // If this isn't an instantiation, remember it. We have good locality
505 // across FileID lookups.
506 if (!I->isInstantiation())
507 LastFileIDLookup = Res;
508 NumBinaryProbes += NumProbes;
509 return Res;
510 }
511
512 // Otherwise, move the low-side up to the middle index.
513 LessIndex = MiddleIndex;
514 }
515}
516
Chris Lattner8d92c1a2009-01-26 20:04:19 +0000517SourceLocation SourceManager::
518getInstantiationLocSlowCase(SourceLocation Loc) const {
519 do {
520 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000521 Loc = getSLocEntry(LocInfo.first).getInstantiation()
522 .getInstantiationLocStart();
Chris Lattner8d92c1a2009-01-26 20:04:19 +0000523 Loc = Loc.getFileLocWithOffset(LocInfo.second);
524 } while (!Loc.isFileID());
525
526 return Loc;
527}
528
529SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
530 do {
531 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
532 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
533 Loc = Loc.getFileLocWithOffset(LocInfo.second);
534 } while (!Loc.isFileID());
535 return Loc;
536}
537
538
Chris Lattner27c0ced2009-01-26 00:43:02 +0000539std::pair<FileID, unsigned>
540SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
541 unsigned Offset) const {
542 // If this is an instantiation record, walk through all the instantiation
543 // points.
544 FileID FID;
545 SourceLocation Loc;
546 do {
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000547 Loc = E->getInstantiation().getInstantiationLocStart();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000548
549 FID = getFileID(Loc);
550 E = &getSLocEntry(FID);
551 Offset += Loc.getOffset()-E->getOffset();
Chris Lattner18ad5582009-01-26 19:41:58 +0000552 } while (!Loc.isFileID());
Chris Lattner27c0ced2009-01-26 00:43:02 +0000553
554 return std::make_pair(FID, Offset);
555}
556
557std::pair<FileID, unsigned>
558SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
559 unsigned Offset) const {
Chris Lattner18ad5582009-01-26 19:41:58 +0000560 // If this is an instantiation record, walk through all the instantiation
561 // points.
562 FileID FID;
563 SourceLocation Loc;
564 do {
565 Loc = E->getInstantiation().getSpellingLoc();
566
567 FID = getFileID(Loc);
568 E = &getSLocEntry(FID);
569 Offset += Loc.getOffset()-E->getOffset();
570 } while (!Loc.isFileID());
571
Chris Lattner27c0ced2009-01-26 00:43:02 +0000572 return std::make_pair(FID, Offset);
573}
574
Chris Lattnerbf9a0e32009-02-17 08:04:48 +0000575/// getImmediateSpellingLoc - Given a SourceLocation object, return the
576/// spelling location referenced by the ID. This is the first level down
577/// towards the place where the characters that make up the lexed token can be
578/// found. This should not generally be used by clients.
579SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
580 if (Loc.isFileID()) return Loc;
581 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
582 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
583 return Loc.getFileLocWithOffset(LocInfo.second);
584}
585
586
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000587/// getImmediateInstantiationRange - Loc is required to be an instantiation
588/// location. Return the start/end of the instantiation information.
589std::pair<SourceLocation,SourceLocation>
590SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
591 assert(Loc.isMacroID() && "Not an instantiation loc!");
592 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
593 return II.getInstantiationLocRange();
594}
595
Chris Lattner46558bf2009-02-15 21:26:50 +0000596/// getInstantiationRange - Given a SourceLocation object, return the
597/// range of tokens covered by the instantiation in the ultimate file.
598std::pair<SourceLocation,SourceLocation>
599SourceManager::getInstantiationRange(SourceLocation Loc) const {
600 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
601
602 std::pair<SourceLocation,SourceLocation> Res =
603 getImmediateInstantiationRange(Loc);
604
605 // Fully resolve the start and end locations to their ultimate instantiation
606 // points.
607 while (!Res.first.isFileID())
608 Res.first = getImmediateInstantiationRange(Res.first).first;
609 while (!Res.second.isFileID())
610 Res.second = getImmediateInstantiationRange(Res.second).second;
611 return Res;
612}
613
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000614
Chris Lattner27c0ced2009-01-26 00:43:02 +0000615
616//===----------------------------------------------------------------------===//
617// Queries about the code at a SourceLocation.
618//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000619
620/// getCharacterData - Return a pointer to the start of the specified location
621/// in the appropriate MemoryBuffer.
622const char *SourceManager::getCharacterData(SourceLocation SL) const {
623 // Note that this is a hot function in the getSpelling() path, which is
624 // heavily used by -E mode.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000625 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000626
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000627 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000628 return getSLocEntry(LocInfo.first).getFile().getContentCache()
629 ->getBuffer()->getBufferStart() + LocInfo.second;
Chris Lattner4b009652007-07-25 00:24:17 +0000630}
631
632
633/// getColumnNumber - Return the column # for the specified file position.
Chris Lattnere79fc852009-02-04 00:55:58 +0000634/// this is significantly cheaper to compute than the line number.
635unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
636 const char *Buf = getBuffer(FID)->getBufferStart();
Chris Lattner4b009652007-07-25 00:24:17 +0000637
Chris Lattner4b009652007-07-25 00:24:17 +0000638 unsigned LineStart = FilePos;
639 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
640 --LineStart;
641 return FilePos-LineStart+1;
642}
643
Chris Lattnere79fc852009-02-04 00:55:58 +0000644unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner2d89c562009-02-04 01:06:56 +0000645 if (Loc.isInvalid()) return 0;
Chris Lattnere79fc852009-02-04 00:55:58 +0000646 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
647 return getColumnNumber(LocInfo.first, LocInfo.second);
648}
649
650unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner2d89c562009-02-04 01:06:56 +0000651 if (Loc.isInvalid()) return 0;
Chris Lattnere79fc852009-02-04 00:55:58 +0000652 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
653 return getColumnNumber(LocInfo.first, LocInfo.second);
654}
655
656
657
Chris Lattner8dedb842009-02-03 07:30:45 +0000658static void ComputeLineNumbers(ContentCache* FI,
659 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
660static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000661 // Note that calling 'getBuffer()' may lazily page in the file.
662 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000663
664 // Find the file offsets of all of the *physical* source lines. This does
665 // not look at trigraphs, escaped newlines, or anything else tricky.
666 std::vector<unsigned> LineOffsets;
667
668 // Line #1 starts at char 0.
669 LineOffsets.push_back(0);
670
671 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
672 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
673 unsigned Offs = 0;
674 while (1) {
675 // Skip over the contents of the line.
676 // TODO: Vectorize this? This is very performance sensitive for programs
677 // with lots of diagnostics and in -E mode.
678 const unsigned char *NextBuf = (const unsigned char *)Buf;
679 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
680 ++NextBuf;
681 Offs += NextBuf-Buf;
682 Buf = NextBuf;
683
684 if (Buf[0] == '\n' || Buf[0] == '\r') {
685 // If this is \n\r or \r\n, skip both characters.
686 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
687 ++Offs, ++Buf;
688 ++Offs, ++Buf;
689 LineOffsets.push_back(Offs);
690 } else {
691 // Otherwise, this is a null. If end of file, exit.
692 if (Buf == End) break;
693 // Otherwise, skip the null.
694 ++Offs, ++Buf;
695 }
696 }
Chris Lattner4b009652007-07-25 00:24:17 +0000697
698 // Copy the offsets into the FileInfo structure.
699 FI->NumLines = LineOffsets.size();
Chris Lattner8dedb842009-02-03 07:30:45 +0000700 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000701 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
702}
703
Chris Lattnercdf600e2009-01-16 07:00:02 +0000704/// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner4b009652007-07-25 00:24:17 +0000705/// for the position indicated. This requires building and caching a table of
706/// line offsets for the MemoryBuffer, so this is not cheap: use only when
707/// about to emit a diagnostic.
Chris Lattner2d89c562009-02-04 01:06:56 +0000708unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000709 ContentCache *Content;
Chris Lattner2d89c562009-02-04 01:06:56 +0000710 if (LastLineNoFileIDQuery == FID)
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000711 Content = LastLineNoContentCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000712 else
Chris Lattner2d89c562009-02-04 01:06:56 +0000713 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattner27c0ced2009-01-26 00:43:02 +0000714 .getFile().getContentCache());
Chris Lattner4b009652007-07-25 00:24:17 +0000715
716 // If this is the first use of line information for this buffer, compute the
717 /// SourceLineCache for it on demand.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000718 if (Content->SourceLineCache == 0)
Chris Lattner8dedb842009-02-03 07:30:45 +0000719 ComputeLineNumbers(Content, ContentCacheAlloc);
Chris Lattner4b009652007-07-25 00:24:17 +0000720
721 // Okay, we know we have a line number table. Do a binary search to find the
722 // line number that this character position lands on.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000723 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000724 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000725 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner4b009652007-07-25 00:24:17 +0000726
Chris Lattner2d89c562009-02-04 01:06:56 +0000727 unsigned QueriedFilePos = FilePos+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000728
Daniel Dunbare0690492009-05-18 17:30:52 +0000729 // FIXME: I would like to be convinced that this code is worth being as
730 // complicated as it is, binary search isn't that slow.
731 //
732 // If it is worth being optimized, then in my opinion it could be more
733 // performant, simpler, and more obviously correct by just "galloping" outward
734 // from the queried file position. In fact, this could be incorporated into a
735 // generic algorithm such as lower_bound_with_hint.
736 //
737 // If someone gives me a test case where this matters, and I will do it! - DWD
738
Chris Lattner4b009652007-07-25 00:24:17 +0000739 // If the previous query was to the same file, we know both the file pos from
740 // that query and the line number returned. This allows us to narrow the
741 // search space from the entire file to something near the match.
Chris Lattner2d89c562009-02-04 01:06:56 +0000742 if (LastLineNoFileIDQuery == FID) {
Chris Lattner4b009652007-07-25 00:24:17 +0000743 if (QueriedFilePos >= LastLineNoFilePos) {
Daniel Dunbare0690492009-05-18 17:30:52 +0000744 // FIXME: Potential overflow?
Chris Lattner4b009652007-07-25 00:24:17 +0000745 SourceLineCache = SourceLineCache+LastLineNoResult-1;
746
747 // The query is likely to be nearby the previous one. Here we check to
748 // see if it is within 5, 10 or 20 lines. It can be far away in cases
749 // where big comment blocks and vertical whitespace eat up lines but
750 // contribute no tokens.
751 if (SourceLineCache+5 < SourceLineCacheEnd) {
752 if (SourceLineCache[5] > QueriedFilePos)
753 SourceLineCacheEnd = SourceLineCache+5;
754 else if (SourceLineCache+10 < SourceLineCacheEnd) {
755 if (SourceLineCache[10] > QueriedFilePos)
756 SourceLineCacheEnd = SourceLineCache+10;
757 else if (SourceLineCache+20 < SourceLineCacheEnd) {
758 if (SourceLineCache[20] > QueriedFilePos)
759 SourceLineCacheEnd = SourceLineCache+20;
760 }
761 }
762 }
763 } else {
Daniel Dunbare0690492009-05-18 17:30:52 +0000764 if (LastLineNoResult < Content->NumLines)
765 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000766 }
767 }
768
769 // If the spread is large, do a "radix" test as our initial guess, based on
770 // the assumption that lines average to approximately the same length.
771 // NOTE: This is currently disabled, as it does not appear to be profitable in
772 // initial measurements.
773 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000774 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner4b009652007-07-25 00:24:17 +0000775
776 // Take a stab at guessing where it is.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000777 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner4b009652007-07-25 00:24:17 +0000778
779 // Check for -10 and +10 lines.
780 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
781 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
782
783 // If the computed lower bound is less than the query location, move it in.
784 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
785 SourceLineCacheStart[LowerBound] < QueriedFilePos)
786 SourceLineCache = SourceLineCacheStart+LowerBound;
787
788 // If the computed upper bound is greater than the query location, move it.
789 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
790 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
791 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
792 }
793
794 unsigned *Pos
795 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
796 unsigned LineNo = Pos-SourceLineCacheStart;
797
Chris Lattner2d89c562009-02-04 01:06:56 +0000798 LastLineNoFileIDQuery = FID;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000799 LastLineNoContentCache = Content;
Chris Lattner4b009652007-07-25 00:24:17 +0000800 LastLineNoFilePos = QueriedFilePos;
801 LastLineNoResult = LineNo;
802 return LineNo;
803}
804
Chris Lattner2d89c562009-02-04 01:06:56 +0000805unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
806 if (Loc.isInvalid()) return 0;
807 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
808 return getLineNumber(LocInfo.first, LocInfo.second);
809}
810unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
811 if (Loc.isInvalid()) return 0;
812 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
813 return getLineNumber(LocInfo.first, LocInfo.second);
814}
815
Chris Lattnerd0ff6022009-02-04 05:33:01 +0000816/// getFileCharacteristic - return the file characteristic of the specified
817/// source location, indicating whether this is a normal file, a system
818/// header, or an "implicit extern C" system header.
819///
820/// This state can be modified with flags on GNU linemarker directives like:
821/// # 4 "foo.h" 3
822/// which changes all source locations in the current file after that to be
823/// considered to be from a system header.
824SrcMgr::CharacteristicKind
825SourceManager::getFileCharacteristic(SourceLocation Loc) const {
826 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
827 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
828 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
829
830 // If there are no #line directives in this file, just return the whole-file
831 // state.
832 if (!FI.hasLineDirectives())
833 return FI.getFileCharacteristic();
834
835 assert(LineTable && "Can't have linetable entries without a LineTable!");
836 // See if there is a #line directive before the location.
837 const LineEntry *Entry =
838 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
839
840 // If this is before the first line marker, use the file characteristic.
841 if (!Entry)
842 return FI.getFileCharacteristic();
843
844 return Entry->FileKind;
845}
846
Chris Lattnerf1790ca2009-02-17 08:39:06 +0000847/// Return the filename or buffer identifier of the buffer the location is in.
848/// Note that this name does not respect #line directives. Use getPresumedLoc
849/// for normal clients.
850const char *SourceManager::getBufferName(SourceLocation Loc) const {
851 if (Loc.isInvalid()) return "<invalid loc>";
852
853 return getBuffer(getFileID(Loc))->getBufferIdentifier();
854}
855
Chris Lattner2d89c562009-02-04 01:06:56 +0000856
Chris Lattner836774b2009-01-27 07:57:44 +0000857/// getPresumedLoc - This method returns the "presumed" location of a
858/// SourceLocation specifies. A "presumed location" can be modified by #line
859/// or GNU line marker directives. This provides a view on the data that a
860/// user should see in diagnostics, for example.
861///
862/// Note that a presumed location is always given as the instantiation point
863/// of an instantiation location, not at the spelling location.
864PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
865 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000866
Chris Lattner836774b2009-01-27 07:57:44 +0000867 // Presumed locations are always for instantiation points.
Chris Lattnere79fc852009-02-04 00:55:58 +0000868 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner836774b2009-01-27 07:57:44 +0000869
Chris Lattner2d89c562009-02-04 01:06:56 +0000870 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattner836774b2009-01-27 07:57:44 +0000871 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000872
873 // To get the source name, first consult the FileEntry (if one exists)
874 // before the MemBuffer as this will avoid unnecessarily paging in the
875 // MemBuffer.
Chris Lattner836774b2009-01-27 07:57:44 +0000876 const char *Filename =
877 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000878 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
879 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
880 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattner836774b2009-01-27 07:57:44 +0000881
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000882 // If we have #line directives in this file, update and overwrite the physical
883 // location info if appropriate.
884 if (FI.hasLineDirectives()) {
885 assert(LineTable && "Can't have linetable entries without a LineTable!");
886 // See if there is a #line directive before this. If so, get it.
887 if (const LineEntry *Entry =
888 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattner74ae2d92009-02-04 02:00:59 +0000889 // If the LineEntry indicates a filename, use it.
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000890 if (Entry->FilenameID != -1)
891 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattner74ae2d92009-02-04 02:00:59 +0000892
893 // Use the line number specified by the LineEntry. This line number may
894 // be multiple lines down from the line entry. Add the difference in
895 // physical line numbers from the query point and the line marker to the
896 // total.
897 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
898 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
899
Chris Lattneraabd3222009-02-04 02:15:40 +0000900 // Note that column numbers are not molested by line markers.
Chris Lattner778ae212009-02-04 06:25:26 +0000901
902 // Handle virtual #include manipulation.
903 if (Entry->IncludeOffset) {
904 IncludeLoc = getLocForStartOfFile(LocInfo.first);
905 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
906 }
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000907 }
908 }
909
910 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattner27c0ced2009-01-26 00:43:02 +0000911}
912
913//===----------------------------------------------------------------------===//
914// Other miscellaneous methods.
915//===----------------------------------------------------------------------===//
916
917
Chris Lattner4b009652007-07-25 00:24:17 +0000918/// PrintStats - Print statistics to stderr.
919///
920void SourceManager::PrintStats() const {
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000921 llvm::cerr << "\n*** Source Manager Stats:\n";
922 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner66284fd2009-01-27 05:22:43 +0000923 << " mem buffers mapped.\n";
924 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
925 << NextOffset << "B of Sloc address space used.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000926
Chris Lattner4b009652007-07-25 00:24:17 +0000927 unsigned NumLineNumsComputed = 0;
928 unsigned NumFileBytesMapped = 0;
Chris Lattner8dedb842009-02-03 07:30:45 +0000929 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
930 NumLineNumsComputed += I->second->SourceLineCache != 0;
931 NumFileBytesMapped += I->second->getSizeBytesMapped();
Chris Lattner4b009652007-07-25 00:24:17 +0000932 }
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000933
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000934 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
935 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattner27c0ced2009-01-26 00:43:02 +0000936 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
937 << NumBinaryProbes << " binary.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000938}
Douglas Gregor32e231c2009-04-27 06:38:32 +0000939
940ExternalSLocEntrySource::~ExternalSLocEntrySource() { }