blob: 88da73f797f8088b1583ae94676665541dcad529 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceManager.h"
Douglas Gregord4f77aa2009-04-13 15:31:25 +000015#include "clang/Basic/SourceManagerInternals.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/FileManager.h"
Chris Lattner5e36a7a2007-07-24 05:57:19 +000017#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/System/Path.h"
20#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22using namespace SrcMgr;
23using llvm::MemoryBuffer;
24
Chris Lattner23b5dc62009-02-04 00:40:31 +000025//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000026// SourceManager Helper Classes
Chris Lattner23b5dc62009-02-04 00:40:31 +000027//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000028
Ted Kremenek78d85f52007-10-30 21:08:08 +000029ContentCache::~ContentCache() {
30 delete Buffer;
Reid Spencer5f016e22007-07-11 17:01:13 +000031}
32
Ted Kremenekc16c2082009-01-06 01:55:26 +000033/// getSizeBytesMapped - Returns the number of bytes actually mapped for
34/// this ContentCache. This can be 0 if the MemBuffer was not actually
35/// instantiated.
36unsigned ContentCache::getSizeBytesMapped() const {
37 return Buffer ? Buffer->getBufferSize() : 0;
38}
39
40/// getSize - Returns the size of the content encapsulated by this ContentCache.
41/// This can be the size of the source file or the size of an arbitrary
42/// scratch buffer. If the ContentCache encapsulates a source file, that
43/// file is not lazily brought in from disk to satisfy this query.
44unsigned ContentCache::getSize() const {
45 return Entry ? Entry->getSize() : Buffer->getBufferSize();
46}
47
Chris Lattner987cd3d2009-01-26 07:37:49 +000048const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek5b034ad2009-01-06 22:43:04 +000049 // Lazily create the Buffer for ContentCaches that wrap files.
50 if (!Buffer && Entry) {
51 // FIXME: Should we support a way to not have to do this check over
52 // and over if we cannot open the file?
Chris Lattner05816592009-01-17 03:54:16 +000053 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek5b034ad2009-01-06 22:43:04 +000054 }
Ted Kremenekc16c2082009-01-06 01:55:26 +000055 return Buffer;
56}
57
Chris Lattner5b9a5042009-01-26 07:57:50 +000058unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
59 // Look up the filename in the string table, returning the pre-existing value
60 // if it exists.
61 llvm::StringMapEntry<unsigned> &Entry =
62 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
63 if (Entry.getValue() != ~0U)
64 return Entry.getValue();
65
66 // Otherwise, assign this the next available ID.
67 Entry.setValue(FilenamesByID.size());
68 FilenamesByID.push_back(&Entry);
69 return FilenamesByID.size()-1;
70}
71
Chris Lattnerac50e342009-02-03 22:13:05 +000072/// AddLineNote - Add a line note to the line table that indicates that there
73/// is a #line at the specified FID/Offset location which changes the presumed
74/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +000075void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +000076 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +000077 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +000078
Chris Lattner23b5dc62009-02-04 00:40:31 +000079 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
80 "Adding line entries out of order!");
Chris Lattner3cd949c2009-02-04 01:55:42 +000081
Chris Lattner9d79eba2009-02-04 05:21:58 +000082 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner137b6a62009-02-04 06:25:26 +000083 unsigned IncludeOffset = 0;
Chris Lattner3cd949c2009-02-04 01:55:42 +000084
Chris Lattner9d79eba2009-02-04 05:21:58 +000085 if (!Entries.empty()) {
86 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
87 // that we are still in "foo.h".
88 if (FilenameID == -1)
89 FilenameID = Entries.back().FilenameID;
90
Chris Lattner137b6a62009-02-04 06:25:26 +000091 // If we are after a line marker that switched us to system header mode, or
92 // that set #include information, preserve it.
Chris Lattner9d79eba2009-02-04 05:21:58 +000093 Kind = Entries.back().FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +000094 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +000095 }
96
Chris Lattner137b6a62009-02-04 06:25:26 +000097 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
98 IncludeOffset));
Chris Lattnerac50e342009-02-03 22:13:05 +000099}
100
Chris Lattner9d79eba2009-02-04 05:21:58 +0000101/// AddLineNote This is the same as the previous version of AddLineNote, but is
102/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
103/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
104/// this is a file exit. FileKind specifies whether this is a system header or
105/// extern C system header.
106void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
107 unsigned LineNo, int FilenameID,
108 unsigned EntryExit,
109 SrcMgr::CharacteristicKind FileKind) {
110 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
111
112 std::vector<LineEntry> &Entries = LineEntries[FID];
113
114 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
115 "Adding line entries out of order!");
116
Chris Lattner137b6a62009-02-04 06:25:26 +0000117 unsigned IncludeOffset = 0;
118 if (EntryExit == 0) { // No #include stack change.
119 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
120 } else if (EntryExit == 1) {
121 IncludeOffset = Offset-1;
122 } else if (EntryExit == 2) {
123 assert(!Entries.empty() && Entries.back().IncludeOffset &&
124 "PPDirectives should have caught case when popping empty include stack");
125
126 // Get the include loc of the last entries' include loc as our include loc.
127 IncludeOffset = 0;
128 if (const LineEntry *PrevEntry =
129 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
130 IncludeOffset = PrevEntry->IncludeOffset;
131 }
Chris Lattner9d79eba2009-02-04 05:21:58 +0000132
Chris Lattner137b6a62009-02-04 06:25:26 +0000133 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
134 IncludeOffset));
Chris Lattner9d79eba2009-02-04 05:21:58 +0000135}
136
137
Chris Lattner3cd949c2009-02-04 01:55:42 +0000138/// FindNearestLineEntry - Find the line entry nearest to FID that is before
139/// it. If there is no line entry before Offset in FID, return null.
140const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
141 unsigned Offset) {
142 const std::vector<LineEntry> &Entries = LineEntries[FID];
143 assert(!Entries.empty() && "No #line entries for this FID after all!");
144
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000145 // It is very common for the query to be after the last #line, check this
146 // first.
147 if (Entries.back().FileOffset <= Offset)
148 return &Entries.back();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000149
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000150 // Do a binary search to find the maximal element that is still before Offset.
151 std::vector<LineEntry>::const_iterator I =
152 std::upper_bound(Entries.begin(), Entries.end(), Offset);
153 if (I == Entries.begin()) return 0;
154 return &*--I;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000155}
Chris Lattnerac50e342009-02-03 22:13:05 +0000156
Douglas Gregorbd945002009-04-13 16:31:14 +0000157/// \brief Add a new line entry that has already been encoded into
158/// the internal representation of the line table.
159void LineTableInfo::AddEntry(unsigned FID,
160 const std::vector<LineEntry> &Entries) {
161 LineEntries[FID] = Entries;
162}
Chris Lattnerac50e342009-02-03 22:13:05 +0000163
Chris Lattner5b9a5042009-01-26 07:57:50 +0000164/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
165///
166unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
167 if (LineTable == 0)
168 LineTable = new LineTableInfo();
169 return LineTable->getLineTableFilenameID(Ptr, Len);
170}
171
172
Chris Lattner4c4ea172009-02-03 21:52:55 +0000173/// AddLineNote - Add a line note to the line table for the FileID and offset
174/// specified by Loc. If FilenameID is -1, it is considered to be
175/// unspecified.
176void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
177 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000178 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000179
Chris Lattnerac50e342009-02-03 22:13:05 +0000180 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
181
182 // Remember that this file has #line directives now if it doesn't already.
183 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
184
185 if (LineTable == 0)
186 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000187 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000188}
189
Chris Lattner9d79eba2009-02-04 05:21:58 +0000190/// AddLineNote - Add a GNU line marker to the line table.
191void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
192 int FilenameID, bool IsFileEntry,
193 bool IsFileExit, bool IsSystemHeader,
194 bool IsExternCHeader) {
195 // If there is no filename and no flags, this is treated just like a #line,
196 // which does not change the flags of the previous line marker.
197 if (FilenameID == -1) {
198 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
199 "Can't set flags without setting the filename!");
200 return AddLineNote(Loc, LineNo, FilenameID);
201 }
202
203 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
204 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
205
206 // Remember that this file has #line directives now if it doesn't already.
207 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
208
209 if (LineTable == 0)
210 LineTable = new LineTableInfo();
211
212 SrcMgr::CharacteristicKind FileKind;
213 if (IsExternCHeader)
214 FileKind = SrcMgr::C_ExternCSystem;
215 else if (IsSystemHeader)
216 FileKind = SrcMgr::C_System;
217 else
218 FileKind = SrcMgr::C_User;
219
220 unsigned EntryExit = 0;
221 if (IsFileEntry)
222 EntryExit = 1;
223 else if (IsFileExit)
224 EntryExit = 2;
225
226 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
227 EntryExit, FileKind);
228}
229
Douglas Gregorbd945002009-04-13 16:31:14 +0000230LineTableInfo &SourceManager::getLineTable() {
231 if (LineTable == 0)
232 LineTable = new LineTableInfo();
233 return *LineTable;
234}
Chris Lattner4c4ea172009-02-03 21:52:55 +0000235
Chris Lattner23b5dc62009-02-04 00:40:31 +0000236//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000237// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000238//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000239
Chris Lattner5b9a5042009-01-26 07:57:50 +0000240SourceManager::~SourceManager() {
241 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000242
243 // Delete FileEntry objects corresponding to content caches. Since the actual
244 // content cache objects are bump pointer allocated, we just have to run the
245 // dtors, but we call the deallocate method for completeness.
246 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
247 MemBufferInfos[i]->~ContentCache();
248 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
249 }
250 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
251 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
252 I->second->~ContentCache();
253 ContentCacheAlloc.Deallocate(I->second);
254 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000255}
256
257void SourceManager::clearIDTables() {
258 MainFileID = FileID();
259 SLocEntryTable.clear();
260 LastLineNoFileIDQuery = FileID();
261 LastLineNoContentCache = 0;
262 LastFileIDLookup = FileID();
263
264 if (LineTable)
265 LineTable->clear();
266
267 // Use up FileID #0 as an invalid instantiation.
268 NextOffset = 0;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000269 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000270}
271
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000272/// getOrCreateContentCache - Create or return a cached ContentCache for the
273/// specified file.
274const ContentCache *
275SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000279 ContentCache *&Entry = FileInfos[FileEnt];
280 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
Chris Lattner00282d62009-02-03 07:41:46 +0000282 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
283 // so that FileInfo can use the low 3 bits of the pointer for its own
284 // nefarious purposes.
285 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
286 EntryAlign = std::max(8U, EntryAlign);
287 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000288 new (Entry) ContentCache(FileEnt);
289 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000290}
291
292
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000293/// createMemBufferContentCache - Create a new ContentCache for the specified
294/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000295const ContentCache*
296SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000297 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
298 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
299 // the pointer for its own nefarious purposes.
300 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
301 EntryAlign = std::max(8U, EntryAlign);
302 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000303 new (Entry) ContentCache();
304 MemBufferInfos.push_back(Entry);
305 Entry->setBuffer(Buffer);
306 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000307}
308
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000309void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
310 unsigned NumSLocEntries,
311 unsigned NextOffset) {
312 ExternalSLocEntries = Source;
313 this->NextOffset = NextOffset;
314 SLocEntryLoaded.resize(NumSLocEntries + 1);
315 SLocEntryLoaded[0] = true;
316 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries);
317}
318
Douglas Gregor2bf1eb02009-04-27 21:28:04 +0000319void SourceManager::ClearPreallocatedSLocEntries() {
320 unsigned I = 0;
321 for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
322 if (!SLocEntryLoaded[I])
323 break;
324
325 // We've already loaded all preallocated source location entries.
326 if (I == SLocEntryLoaded.size())
327 return;
328
329 // Remove everything from location I onward.
330 SLocEntryTable.resize(I);
331 SLocEntryLoaded.clear();
332 ExternalSLocEntries = 0;
333}
334
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000335
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000336//===----------------------------------------------------------------------===//
337// Methods to create new FileID's and instantiations.
338//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
Nico Weber48002c82008-09-29 00:25:48 +0000340/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000341/// include position. This works regardless of whether the ContentCache
342/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000343FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000344 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000345 SrcMgr::CharacteristicKind FileCharacter,
346 unsigned PreallocatedID,
347 unsigned Offset) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000348 if (PreallocatedID) {
349 // If we're filling in a preallocated ID, just load in the file
350 // entry and return.
351 assert(PreallocatedID < SLocEntryLoaded.size() &&
352 "Preallocate ID out-of-range");
353 assert(!SLocEntryLoaded[PreallocatedID] &&
354 "Source location entry already loaded");
355 assert(Offset && "Preallocate source location cannot have zero offset");
356 SLocEntryTable[PreallocatedID]
357 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
358 SLocEntryLoaded[PreallocatedID] = true;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000359 FileID FID = FileID::get(PreallocatedID);
360 if (File->FirstFID.isInvalid())
361 File->FirstFID = FID;
362 return LastFileIDLookup = FID;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000363 }
364
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000365 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
366 FileInfo::get(IncludePos, File,
367 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000368 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000369 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
370 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000372 // Set LastFileIDLookup to the newly created file. The next getFileID call is
373 // almost guaranteed to be from that file.
Argyrios Kyrtzidisea703f12009-06-23 00:42:06 +0000374 FileID FID = FileID::get(SLocEntryTable.size()-1);
375 if (File->FirstFID.isInvalid())
376 File->FirstFID = FID;
377 return LastFileIDLookup = FID;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378}
379
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000380/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000381/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000382/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000383SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000384 SourceLocation ILocStart,
385 SourceLocation ILocEnd,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000386 unsigned TokLength,
387 unsigned PreallocatedID,
388 unsigned Offset) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000389 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000390 if (PreallocatedID) {
391 // If we're filling in a preallocated ID, just load in the
392 // instantiation entry and return.
393 assert(PreallocatedID < SLocEntryLoaded.size() &&
394 "Preallocate ID out-of-range");
395 assert(!SLocEntryLoaded[PreallocatedID] &&
396 "Source location entry already loaded");
397 assert(Offset && "Preallocate source location cannot have zero offset");
398 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
399 SLocEntryLoaded[PreallocatedID] = true;
400 return SourceLocation::getMacroLoc(Offset);
401 }
Chris Lattnere7fb4842009-02-15 20:52:18 +0000402 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000403 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
404 NextOffset += TokLength+1;
405 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000406}
407
Chris Lattner31530ba2009-01-19 07:32:13 +0000408/// getBufferData - Return a pointer to the start and end of the source buffer
409/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000410std::pair<const char*, const char*>
411SourceManager::getBufferData(FileID FID) const {
412 const llvm::MemoryBuffer *Buf = getBuffer(FID);
413 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
414}
415
416
Chris Lattner23b5dc62009-02-04 00:40:31 +0000417//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000418// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000419//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000420
421/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
422/// method that is used for all SourceManager queries that start with a
423/// SourceLocation object. It is responsible for finding the entry in
424/// SLocEntryTable which contains the specified location.
425///
426FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
427 assert(SLocOffset && "Invalid FileID");
428
429 // After the first and second level caches, I see two common sorts of
430 // behavior: 1) a lot of searched FileID's are "near" the cached file location
431 // or are "near" the cached instantiation location. 2) others are just
432 // completely random and may be a very long way away.
433 //
434 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
435 // then we fall back to a less cache efficient, but more scalable, binary
436 // search to find the location.
437
438 // See if this is near the file point - worst case we start scanning from the
439 // most newly created FileID.
440 std::vector<SrcMgr::SLocEntry>::const_iterator I;
441
442 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
443 // Neither loc prunes our search.
444 I = SLocEntryTable.end();
445 } else {
446 // Perhaps it is near the file point.
447 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
448 }
449
450 // Find the FileID that contains this. "I" is an iterator that points to a
451 // FileID whose offset is known to be larger than SLocOffset.
452 unsigned NumProbes = 0;
453 while (1) {
454 --I;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000455 if (ExternalSLocEntries)
456 getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000457 if (I->getOffset() <= SLocOffset) {
458#if 0
459 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
460 I-SLocEntryTable.begin(),
461 I->isInstantiation() ? "inst" : "file",
462 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
463#endif
464 FileID Res = FileID::get(I-SLocEntryTable.begin());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000465
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000466 // If this isn't an instantiation, remember it. We have good locality
467 // across FileID lookups.
468 if (!I->isInstantiation())
469 LastFileIDLookup = Res;
470 NumLinearScans += NumProbes+1;
471 return Res;
472 }
473 if (++NumProbes == 8)
474 break;
475 }
476
477 // Convert "I" back into an index. We know that it is an entry whose index is
478 // larger than the offset we are looking for.
479 unsigned GreaterIndex = I-SLocEntryTable.begin();
480 // LessIndex - This is the lower bound of the range that we're searching.
481 // We know that the offset corresponding to the FileID is is less than
482 // SLocOffset.
483 unsigned LessIndex = 0;
484 NumProbes = 0;
485 while (1) {
486 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000487 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000488
489 ++NumProbes;
490
491 // If the offset of the midpoint is too large, chop the high side of the
492 // range to the midpoint.
493 if (MidOffset > SLocOffset) {
494 GreaterIndex = MiddleIndex;
495 continue;
496 }
497
498 // If the middle index contains the value, succeed and return.
499 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
500#if 0
501 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
502 I-SLocEntryTable.begin(),
503 I->isInstantiation() ? "inst" : "file",
504 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
505#endif
506 FileID Res = FileID::get(MiddleIndex);
507
508 // If this isn't an instantiation, remember it. We have good locality
509 // across FileID lookups.
510 if (!I->isInstantiation())
511 LastFileIDLookup = Res;
512 NumBinaryProbes += NumProbes;
513 return Res;
514 }
515
516 // Otherwise, move the low-side up to the middle index.
517 LessIndex = MiddleIndex;
518 }
519}
520
Chris Lattneraddb7972009-01-26 20:04:19 +0000521SourceLocation SourceManager::
522getInstantiationLocSlowCase(SourceLocation Loc) const {
523 do {
524 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000525 Loc = getSLocEntry(LocInfo.first).getInstantiation()
526 .getInstantiationLocStart();
Chris Lattneraddb7972009-01-26 20:04:19 +0000527 Loc = Loc.getFileLocWithOffset(LocInfo.second);
528 } while (!Loc.isFileID());
529
530 return Loc;
531}
532
533SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
534 do {
535 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
536 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
537 Loc = Loc.getFileLocWithOffset(LocInfo.second);
538 } while (!Loc.isFileID());
539 return Loc;
540}
541
542
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000543std::pair<FileID, unsigned>
544SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
545 unsigned Offset) const {
546 // If this is an instantiation record, walk through all the instantiation
547 // points.
548 FileID FID;
549 SourceLocation Loc;
550 do {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000551 Loc = E->getInstantiation().getInstantiationLocStart();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000552
553 FID = getFileID(Loc);
554 E = &getSLocEntry(FID);
555 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000556 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000557
558 return std::make_pair(FID, Offset);
559}
560
561std::pair<FileID, unsigned>
562SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
563 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000564 // If this is an instantiation record, walk through all the instantiation
565 // points.
566 FileID FID;
567 SourceLocation Loc;
568 do {
569 Loc = E->getInstantiation().getSpellingLoc();
570
571 FID = getFileID(Loc);
572 E = &getSLocEntry(FID);
573 Offset += Loc.getOffset()-E->getOffset();
574 } while (!Loc.isFileID());
575
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000576 return std::make_pair(FID, Offset);
577}
578
Chris Lattner387616e2009-02-17 08:04:48 +0000579/// getImmediateSpellingLoc - Given a SourceLocation object, return the
580/// spelling location referenced by the ID. This is the first level down
581/// towards the place where the characters that make up the lexed token can be
582/// found. This should not generally be used by clients.
583SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
584 if (Loc.isFileID()) return Loc;
585 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
586 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
587 return Loc.getFileLocWithOffset(LocInfo.second);
588}
589
590
Chris Lattnere7fb4842009-02-15 20:52:18 +0000591/// getImmediateInstantiationRange - Loc is required to be an instantiation
592/// location. Return the start/end of the instantiation information.
593std::pair<SourceLocation,SourceLocation>
594SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
595 assert(Loc.isMacroID() && "Not an instantiation loc!");
596 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
597 return II.getInstantiationLocRange();
598}
599
Chris Lattner66781332009-02-15 21:26:50 +0000600/// getInstantiationRange - Given a SourceLocation object, return the
601/// range of tokens covered by the instantiation in the ultimate file.
602std::pair<SourceLocation,SourceLocation>
603SourceManager::getInstantiationRange(SourceLocation Loc) const {
604 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
605
606 std::pair<SourceLocation,SourceLocation> Res =
607 getImmediateInstantiationRange(Loc);
608
609 // Fully resolve the start and end locations to their ultimate instantiation
610 // points.
611 while (!Res.first.isFileID())
612 Res.first = getImmediateInstantiationRange(Res.first).first;
613 while (!Res.second.isFileID())
614 Res.second = getImmediateInstantiationRange(Res.second).second;
615 return Res;
616}
617
Chris Lattnere7fb4842009-02-15 20:52:18 +0000618
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000619
620//===----------------------------------------------------------------------===//
621// Queries about the code at a SourceLocation.
622//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000623
624/// getCharacterData - Return a pointer to the start of the specified location
625/// in the appropriate MemoryBuffer.
626const char *SourceManager::getCharacterData(SourceLocation SL) const {
627 // Note that this is a hot function in the getSpelling() path, which is
628 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000629 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000630
Ted Kremenekc16c2082009-01-06 01:55:26 +0000631 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000632 return getSLocEntry(LocInfo.first).getFile().getContentCache()
633 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000634}
635
Reid Spencer5f016e22007-07-11 17:01:13 +0000636
Chris Lattner9dc1f532007-07-20 16:37:10 +0000637/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000638/// this is significantly cheaper to compute than the line number.
639unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
640 const char *Buf = getBuffer(FID)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000641
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 unsigned LineStart = FilePos;
643 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
644 --LineStart;
645 return FilePos-LineStart+1;
646}
647
Chris Lattner7da5aea2009-02-04 00:55:58 +0000648unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000649 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000650 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
651 return getColumnNumber(LocInfo.first, LocInfo.second);
652}
653
654unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000655 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000656 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
657 return getColumnNumber(LocInfo.first, LocInfo.second);
658}
659
660
661
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000662static void ComputeLineNumbers(ContentCache* FI,
663 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
664static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000665 // Note that calling 'getBuffer()' may lazily page in the file.
666 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000667
668 // Find the file offsets of all of the *physical* source lines. This does
669 // not look at trigraphs, escaped newlines, or anything else tricky.
670 std::vector<unsigned> LineOffsets;
671
672 // Line #1 starts at char 0.
673 LineOffsets.push_back(0);
674
675 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
676 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
677 unsigned Offs = 0;
678 while (1) {
679 // Skip over the contents of the line.
680 // TODO: Vectorize this? This is very performance sensitive for programs
681 // with lots of diagnostics and in -E mode.
682 const unsigned char *NextBuf = (const unsigned char *)Buf;
683 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
684 ++NextBuf;
685 Offs += NextBuf-Buf;
686 Buf = NextBuf;
687
688 if (Buf[0] == '\n' || Buf[0] == '\r') {
689 // If this is \n\r or \r\n, skip both characters.
690 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
691 ++Offs, ++Buf;
692 ++Offs, ++Buf;
693 LineOffsets.push_back(Offs);
694 } else {
695 // Otherwise, this is a null. If end of file, exit.
696 if (Buf == End) break;
697 // Otherwise, skip the null.
698 ++Offs, ++Buf;
699 }
700 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000701
702 // Copy the offsets into the FileInfo structure.
703 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000704 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000705 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
706}
Reid Spencer5f016e22007-07-11 17:01:13 +0000707
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000708/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000709/// for the position indicated. This requires building and caching a table of
710/// line offsets for the MemoryBuffer, so this is not cheap: use only when
711/// about to emit a diagnostic.
Chris Lattner30fc9332009-02-04 01:06:56 +0000712unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000713 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000714 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000715 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000716 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000717 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000718 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000719
720 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000721 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000722 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000723 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000724
725 // Okay, we know we have a line number table. Do a binary search to find the
726 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000727 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000728 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000729 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000730
Chris Lattner30fc9332009-02-04 01:06:56 +0000731 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000732
Daniel Dunbar4106d692009-05-18 17:30:52 +0000733 // FIXME: I would like to be convinced that this code is worth being as
734 // complicated as it is, binary search isn't that slow.
735 //
736 // If it is worth being optimized, then in my opinion it could be more
737 // performant, simpler, and more obviously correct by just "galloping" outward
738 // from the queried file position. In fact, this could be incorporated into a
739 // generic algorithm such as lower_bound_with_hint.
740 //
741 // If someone gives me a test case where this matters, and I will do it! - DWD
742
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000743 // If the previous query was to the same file, we know both the file pos from
744 // that query and the line number returned. This allows us to narrow the
745 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000746 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000747 if (QueriedFilePos >= LastLineNoFilePos) {
Daniel Dunbar4106d692009-05-18 17:30:52 +0000748 // FIXME: Potential overflow?
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000749 SourceLineCache = SourceLineCache+LastLineNoResult-1;
750
751 // The query is likely to be nearby the previous one. Here we check to
752 // see if it is within 5, 10 or 20 lines. It can be far away in cases
753 // where big comment blocks and vertical whitespace eat up lines but
754 // contribute no tokens.
755 if (SourceLineCache+5 < SourceLineCacheEnd) {
756 if (SourceLineCache[5] > QueriedFilePos)
757 SourceLineCacheEnd = SourceLineCache+5;
758 else if (SourceLineCache+10 < SourceLineCacheEnd) {
759 if (SourceLineCache[10] > QueriedFilePos)
760 SourceLineCacheEnd = SourceLineCache+10;
761 else if (SourceLineCache+20 < SourceLineCacheEnd) {
762 if (SourceLineCache[20] > QueriedFilePos)
763 SourceLineCacheEnd = SourceLineCache+20;
764 }
765 }
766 }
767 } else {
Daniel Dunbar4106d692009-05-18 17:30:52 +0000768 if (LastLineNoResult < Content->NumLines)
769 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000770 }
771 }
772
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000773 // If the spread is large, do a "radix" test as our initial guess, based on
774 // the assumption that lines average to approximately the same length.
775 // NOTE: This is currently disabled, as it does not appear to be profitable in
776 // initial measurements.
777 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000778 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000779
780 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000781 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000782
783 // Check for -10 and +10 lines.
784 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
785 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
786
787 // If the computed lower bound is less than the query location, move it in.
788 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
789 SourceLineCacheStart[LowerBound] < QueriedFilePos)
790 SourceLineCache = SourceLineCacheStart+LowerBound;
791
792 // If the computed upper bound is greater than the query location, move it.
793 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
794 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
795 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
796 }
797
798 unsigned *Pos
799 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000800 unsigned LineNo = Pos-SourceLineCacheStart;
801
Chris Lattner30fc9332009-02-04 01:06:56 +0000802 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000803 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000804 LastLineNoFilePos = QueriedFilePos;
805 LastLineNoResult = LineNo;
806 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}
808
Chris Lattner30fc9332009-02-04 01:06:56 +0000809unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
810 if (Loc.isInvalid()) return 0;
811 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
812 return getLineNumber(LocInfo.first, LocInfo.second);
813}
814unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
815 if (Loc.isInvalid()) return 0;
816 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
817 return getLineNumber(LocInfo.first, LocInfo.second);
818}
819
Chris Lattner6b306672009-02-04 05:33:01 +0000820/// getFileCharacteristic - return the file characteristic of the specified
821/// source location, indicating whether this is a normal file, a system
822/// header, or an "implicit extern C" system header.
823///
824/// This state can be modified with flags on GNU linemarker directives like:
825/// # 4 "foo.h" 3
826/// which changes all source locations in the current file after that to be
827/// considered to be from a system header.
828SrcMgr::CharacteristicKind
829SourceManager::getFileCharacteristic(SourceLocation Loc) const {
830 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
831 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
832 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
833
834 // If there are no #line directives in this file, just return the whole-file
835 // state.
836 if (!FI.hasLineDirectives())
837 return FI.getFileCharacteristic();
838
839 assert(LineTable && "Can't have linetable entries without a LineTable!");
840 // See if there is a #line directive before the location.
841 const LineEntry *Entry =
842 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
843
844 // If this is before the first line marker, use the file characteristic.
845 if (!Entry)
846 return FI.getFileCharacteristic();
847
848 return Entry->FileKind;
849}
850
Chris Lattnerbff5c512009-02-17 08:39:06 +0000851/// Return the filename or buffer identifier of the buffer the location is in.
852/// Note that this name does not respect #line directives. Use getPresumedLoc
853/// for normal clients.
854const char *SourceManager::getBufferName(SourceLocation Loc) const {
855 if (Loc.isInvalid()) return "<invalid loc>";
856
857 return getBuffer(getFileID(Loc))->getBufferIdentifier();
858}
859
Chris Lattner30fc9332009-02-04 01:06:56 +0000860
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000861/// getPresumedLoc - This method returns the "presumed" location of a
862/// SourceLocation specifies. A "presumed location" can be modified by #line
863/// or GNU line marker directives. This provides a view on the data that a
864/// user should see in diagnostics, for example.
865///
866/// Note that a presumed location is always given as the instantiation point
867/// of an instantiation location, not at the spelling location.
868PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
869 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000870
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000871 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000872 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000873
Chris Lattner30fc9332009-02-04 01:06:56 +0000874 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000875 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000876
877 // To get the source name, first consult the FileEntry (if one exists)
878 // before the MemBuffer as this will avoid unnecessarily paging in the
879 // MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000880 const char *Filename =
881 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000882 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
883 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
884 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000885
Chris Lattner3cd949c2009-02-04 01:55:42 +0000886 // If we have #line directives in this file, update and overwrite the physical
887 // location info if appropriate.
888 if (FI.hasLineDirectives()) {
889 assert(LineTable && "Can't have linetable entries without a LineTable!");
890 // See if there is a #line directive before this. If so, get it.
891 if (const LineEntry *Entry =
892 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +0000893 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000894 if (Entry->FilenameID != -1)
895 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +0000896
897 // Use the line number specified by the LineEntry. This line number may
898 // be multiple lines down from the line entry. Add the difference in
899 // physical line numbers from the query point and the line marker to the
900 // total.
901 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
902 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
903
Chris Lattner0e0e5da2009-02-04 02:15:40 +0000904 // Note that column numbers are not molested by line markers.
Chris Lattner137b6a62009-02-04 06:25:26 +0000905
906 // Handle virtual #include manipulation.
907 if (Entry->IncludeOffset) {
908 IncludeLoc = getLocForStartOfFile(LocInfo.first);
909 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
910 }
Chris Lattner3cd949c2009-02-04 01:55:42 +0000911 }
912 }
913
914 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000915}
916
917//===----------------------------------------------------------------------===//
918// Other miscellaneous methods.
919//===----------------------------------------------------------------------===//
920
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000921/// \brief Get the source location for the given file:line:col triplet.
922///
923/// If the source file is included multiple times, the source location will
924/// be based upon the first inclusion.
925SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
926 unsigned Line, unsigned Col) const {
927 assert(SourceFile && "Null source file!");
928 assert(Line && Col && "Line and column should start from 1!");
929
930 fileinfo_iterator FI = FileInfos.find(SourceFile);
931 if (FI == FileInfos.end())
932 return SourceLocation();
933 ContentCache *Content = FI->second;
934
935 // If this is the first use of line information for this buffer, compute the
936 /// SourceLineCache for it on demand.
937 if (Content->SourceLineCache == 0)
938 ComputeLineNumbers(Content, ContentCacheAlloc);
939
940 if (Line > Content->NumLines)
941 return SourceLocation();
942
943 unsigned FilePos = Content->SourceLineCache[Line - 1];
Argyrios Kyrtzidis081445c2009-06-25 18:22:16 +0000944 const char *Buf = Content->getBuffer()->getBufferStart() + FilePos;
Argyrios Kyrtzidis93edc3c2009-06-20 08:40:15 +0000945 unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf;
946 unsigned i = 0;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000947
948 // Check that the given column is valid.
Argyrios Kyrtzidis93edc3c2009-06-20 08:40:15 +0000949 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
950 ++i;
951 if (i < Col-1)
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000952 return SourceLocation();
953
954 return getLocForStartOfFile(Content->FirstFID).
955 getFileLocWithOffset(FilePos + Col - 1);
956}
957
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000958/// \brief Determines the order of 2 source locations in the translation unit.
959///
960/// \returns true if LHS source location comes before RHS, false otherwise.
961bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
962 SourceLocation RHS) const {
963 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
964 if (LHS == RHS)
965 return false;
966
967 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
968 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
969
970 // If the source locations are in the same file, just compare offsets.
971 if (LOffs.first == ROffs.first)
972 return LOffs.second < ROffs.second;
973
974 // If we are comparing a source location with multiple locations in the same
975 // file, we get a big win by caching the result.
976
977 if (LastLFIDForBeforeTUCheck == LOffs.first &&
978 LastRFIDForBeforeTUCheck == ROffs.first)
979 return LastResForBeforeTUCheck;
980
981 LastLFIDForBeforeTUCheck = LOffs.first;
982 LastRFIDForBeforeTUCheck = ROffs.first;
983
984 // "Traverse" the include/instantiation stacks of both locations and try to
985 // find a common "ancestor".
986 //
987 // First we traverse the stack of the right location and check each level
988 // against the level of the left location, while collecting all levels in a
989 // "stack map".
990
991 std::map<FileID, unsigned> ROffsMap;
992 ROffsMap[ROffs.first] = ROffs.second;
993
994 while (1) {
995 SourceLocation UpperLoc;
996 const SrcMgr::SLocEntry &Entry = getSLocEntry(ROffs.first);
997 if (Entry.isInstantiation())
998 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
999 else
1000 UpperLoc = Entry.getFile().getIncludeLoc();
1001
1002 if (UpperLoc.isInvalid())
1003 break; // We reached the top.
1004
1005 ROffs = getDecomposedLoc(UpperLoc);
1006
1007 if (LOffs.first == ROffs.first)
1008 return LastResForBeforeTUCheck = LOffs.second < ROffs.second;
1009
1010 ROffsMap[ROffs.first] = ROffs.second;
1011 }
1012
1013 // We didn't find a common ancestor. Now traverse the stack of the left
1014 // location, checking against the stack map of the right location.
1015
1016 while (1) {
1017 SourceLocation UpperLoc;
1018 const SrcMgr::SLocEntry &Entry = getSLocEntry(LOffs.first);
1019 if (Entry.isInstantiation())
1020 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1021 else
1022 UpperLoc = Entry.getFile().getIncludeLoc();
1023
1024 if (UpperLoc.isInvalid())
1025 break; // We reached the top.
1026
1027 LOffs = getDecomposedLoc(UpperLoc);
1028
1029 std::map<FileID, unsigned>::iterator I = ROffsMap.find(LOffs.first);
1030 if (I != ROffsMap.end())
1031 return LastResForBeforeTUCheck = LOffs.second < I->second;
1032 }
1033
1034 // No common ancestor.
1035 // Now we are getting into murky waters. Most probably this is because one
1036 // location is in the predefines buffer.
1037
1038 const FileEntry *LEntry =
1039 getSLocEntry(LOffs.first).getFile().getContentCache()->Entry;
1040 const FileEntry *REntry =
1041 getSLocEntry(ROffs.first).getFile().getContentCache()->Entry;
1042
1043 // If the locations are in two memory buffers we give up, we can't answer
1044 // which one should be considered first.
1045 // FIXME: Should there be a way to "include" memory buffers in the translation
1046 // unit ?
1047 assert((LEntry != 0 || REntry != 0) && "Locations in memory buffers.");
Daniel Dunbar5eee0902009-06-23 23:09:58 +00001048 (void) REntry;
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001049
1050 // Consider the memory buffer as coming before the file in the translation
1051 // unit.
1052 if (LEntry == 0)
1053 return LastResForBeforeTUCheck = true;
1054 else {
1055 assert(REntry == 0 && "Locations in not #included files ?");
1056 return LastResForBeforeTUCheck = false;
1057 }
1058}
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060/// PrintStats - Print statistics to stderr.
1061///
1062void SourceManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00001063 llvm::errs() << "\n*** Source Manager Stats:\n";
1064 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
1065 << " mem buffers mapped.\n";
1066 llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
1067 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +00001068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 unsigned NumLineNumsComputed = 0;
1070 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001071 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
1072 NumLineNumsComputed += I->second->SourceLineCache != 0;
1073 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 }
Ted Kremenek78d85f52007-10-30 21:08:08 +00001075
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00001076 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
1077 << NumLineNumsComputed << " files with line #'s computed.\n";
1078 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
1079 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +00001080}
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001081
1082ExternalSLocEntrySource::~ExternalSLocEntrySource() { }