blob: cc75b87c467fd8b0429773a149a01c4649817334 [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"
15#include "clang/Basic/FileManager.h"
Chris Lattner5e36a7a2007-07-24 05:57:19 +000016#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/System/Path.h"
Ted Kremenek78d85f52007-10-30 21:08:08 +000019#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
Ted Kremenek665dd4a2007-12-05 22:21:13 +000021#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Chris Lattner23b5dc62009-02-04 00:40:31 +000027//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000028// SourceManager Helper Classes
Chris Lattner23b5dc62009-02-04 00:40:31 +000029//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000030
Ted Kremenek78d85f52007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
Ted Kremenekc16c2082009-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 Lattner987cd3d2009-01-26 07:37:49 +000050const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek5b034ad2009-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 Lattner05816592009-01-17 03:54:16 +000055 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek5b034ad2009-01-06 22:43:04 +000056 }
Ted Kremenekc16c2082009-01-06 01:55:26 +000057 return Buffer;
58}
59
Chris Lattner23b5dc62009-02-04 00:40:31 +000060//===----------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000061// Line Table Implementation
Chris Lattner23b5dc62009-02-04 00:40:31 +000062//===----------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000063
64namespace clang {
Chris Lattner23b5dc62009-02-04 00:40:31 +000065struct LineEntry {
66 /// FileOffset - The offset in this file that the line entry occurs at.
67 unsigned FileOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +000068
Chris Lattner23b5dc62009-02-04 00:40:31 +000069 /// LineNo - The presumed line number of this line entry: #line 4.
70 unsigned LineNo;
Chris Lattner9d79eba2009-02-04 05:21:58 +000071
Chris Lattner23b5dc62009-02-04 00:40:31 +000072 /// FilenameID - The ID of the filename identified by this line entry:
73 /// #line 4 "foo.c". This is -1 if not specified.
74 int FilenameID;
75
Chris Lattner9d79eba2009-02-04 05:21:58 +000076 /// Flags - Set the 0 if no flags, 1 if a system header,
77 SrcMgr::CharacteristicKind FileKind;
78
Chris Lattner137b6a62009-02-04 06:25:26 +000079 /// IncludeOffset - This is the offset of the virtual include stack location,
80 /// which is manipulated by GNU linemarker directives. If this is 0 then
81 /// there is no virtual #includer.
82 unsigned IncludeOffset;
83
Chris Lattner9d79eba2009-02-04 05:21:58 +000084 static LineEntry get(unsigned Offs, unsigned Line, int Filename,
Chris Lattner137b6a62009-02-04 06:25:26 +000085 SrcMgr::CharacteristicKind FileKind,
86 unsigned IncludeOffset) {
Chris Lattner23b5dc62009-02-04 00:40:31 +000087 LineEntry E;
88 E.FileOffset = Offs;
89 E.LineNo = Line;
90 E.FilenameID = Filename;
Chris Lattner6b306672009-02-04 05:33:01 +000091 E.FileKind = FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +000092 E.IncludeOffset = IncludeOffset;
Chris Lattner23b5dc62009-02-04 00:40:31 +000093 return E;
94 }
95};
Chris Lattner6c1fbe02009-02-04 04:46:59 +000096
97inline bool operator<(const LineEntry &E, unsigned Offset) {
98 return E.FileOffset < Offset;
99}
100
101inline bool operator<(unsigned Offset, const LineEntry &E) {
102 return Offset < E.FileOffset;
103}
Chris Lattner23b5dc62009-02-04 00:40:31 +0000104
Chris Lattner5b9a5042009-01-26 07:57:50 +0000105/// LineTableInfo - This class is used to hold and unique data used to
106/// represent #line information.
107class LineTableInfo {
108 /// FilenameIDs - This map is used to assign unique IDs to filenames in
109 /// #line directives. This allows us to unique the filenames that
110 /// frequently reoccur and reference them with indices. FilenameIDs holds
111 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
112 /// to string.
113 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
114 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
Chris Lattner23b5dc62009-02-04 00:40:31 +0000115
116 /// LineEntries - This is a map from FileIDs to a list of line entries (sorted
117 /// by the offset they occur in the file.
118 std::map<unsigned, std::vector<LineEntry> > LineEntries;
Chris Lattner5b9a5042009-01-26 07:57:50 +0000119public:
120 LineTableInfo() {
121 }
122
123 void clear() {
124 FilenameIDs.clear();
125 FilenamesByID.clear();
126 }
127
128 ~LineTableInfo() {}
129
130 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Chris Lattner3cd949c2009-02-04 01:55:42 +0000131 const char *getFilename(unsigned ID) const {
132 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
133 return FilenamesByID[ID]->getKeyData();
134 }
135
Chris Lattner23b5dc62009-02-04 00:40:31 +0000136 void AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000137 unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +0000138 void AddLineNote(unsigned FID, unsigned Offset,
139 unsigned LineNo, int FilenameID,
140 unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
141
Chris Lattner3cd949c2009-02-04 01:55:42 +0000142
143 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
144 /// it. If there is no line entry before Offset in FID, return null.
145 const LineEntry *FindNearestLineEntry(unsigned FID, unsigned Offset);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000146};
147} // namespace clang
148
Chris Lattner5b9a5042009-01-26 07:57:50 +0000149unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
150 // Look up the filename in the string table, returning the pre-existing value
151 // if it exists.
152 llvm::StringMapEntry<unsigned> &Entry =
153 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
154 if (Entry.getValue() != ~0U)
155 return Entry.getValue();
156
157 // Otherwise, assign this the next available ID.
158 Entry.setValue(FilenamesByID.size());
159 FilenamesByID.push_back(&Entry);
160 return FilenamesByID.size()-1;
161}
162
Chris Lattnerac50e342009-02-03 22:13:05 +0000163/// AddLineNote - Add a line note to the line table that indicates that there
164/// is a #line at the specified FID/Offset location which changes the presumed
165/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000166void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000167 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000168 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +0000169
Chris Lattner23b5dc62009-02-04 00:40:31 +0000170 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
171 "Adding line entries out of order!");
Chris Lattner3cd949c2009-02-04 01:55:42 +0000172
Chris Lattner9d79eba2009-02-04 05:21:58 +0000173 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner137b6a62009-02-04 06:25:26 +0000174 unsigned IncludeOffset = 0;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000175
Chris Lattner9d79eba2009-02-04 05:21:58 +0000176 if (!Entries.empty()) {
177 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
178 // that we are still in "foo.h".
179 if (FilenameID == -1)
180 FilenameID = Entries.back().FilenameID;
181
Chris Lattner137b6a62009-02-04 06:25:26 +0000182 // If we are after a line marker that switched us to system header mode, or
183 // that set #include information, preserve it.
Chris Lattner9d79eba2009-02-04 05:21:58 +0000184 Kind = Entries.back().FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +0000185 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +0000186 }
187
Chris Lattner137b6a62009-02-04 06:25:26 +0000188 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
189 IncludeOffset));
Chris Lattnerac50e342009-02-03 22:13:05 +0000190}
191
Chris Lattner9d79eba2009-02-04 05:21:58 +0000192/// AddLineNote This is the same as the previous version of AddLineNote, but is
193/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
194/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
195/// this is a file exit. FileKind specifies whether this is a system header or
196/// extern C system header.
197void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
198 unsigned LineNo, int FilenameID,
199 unsigned EntryExit,
200 SrcMgr::CharacteristicKind FileKind) {
201 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
202
203 std::vector<LineEntry> &Entries = LineEntries[FID];
204
205 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
206 "Adding line entries out of order!");
207
Chris Lattner137b6a62009-02-04 06:25:26 +0000208 unsigned IncludeOffset = 0;
209 if (EntryExit == 0) { // No #include stack change.
210 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
211 } else if (EntryExit == 1) {
212 IncludeOffset = Offset-1;
213 } else if (EntryExit == 2) {
214 assert(!Entries.empty() && Entries.back().IncludeOffset &&
215 "PPDirectives should have caught case when popping empty include stack");
216
217 // Get the include loc of the last entries' include loc as our include loc.
218 IncludeOffset = 0;
219 if (const LineEntry *PrevEntry =
220 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
221 IncludeOffset = PrevEntry->IncludeOffset;
222 }
Chris Lattner9d79eba2009-02-04 05:21:58 +0000223
Chris Lattner137b6a62009-02-04 06:25:26 +0000224 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
225 IncludeOffset));
Chris Lattner9d79eba2009-02-04 05:21:58 +0000226}
227
228
Chris Lattner3cd949c2009-02-04 01:55:42 +0000229/// FindNearestLineEntry - Find the line entry nearest to FID that is before
230/// it. If there is no line entry before Offset in FID, return null.
231const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
232 unsigned Offset) {
233 const std::vector<LineEntry> &Entries = LineEntries[FID];
234 assert(!Entries.empty() && "No #line entries for this FID after all!");
235
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000236 // It is very common for the query to be after the last #line, check this
237 // first.
238 if (Entries.back().FileOffset <= Offset)
239 return &Entries.back();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000240
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000241 // Do a binary search to find the maximal element that is still before Offset.
242 std::vector<LineEntry>::const_iterator I =
243 std::upper_bound(Entries.begin(), Entries.end(), Offset);
244 if (I == Entries.begin()) return 0;
245 return &*--I;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000246}
Chris Lattnerac50e342009-02-03 22:13:05 +0000247
248
Chris Lattner5b9a5042009-01-26 07:57:50 +0000249/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
250///
251unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
252 if (LineTable == 0)
253 LineTable = new LineTableInfo();
254 return LineTable->getLineTableFilenameID(Ptr, Len);
255}
256
257
Chris Lattner4c4ea172009-02-03 21:52:55 +0000258/// AddLineNote - Add a line note to the line table for the FileID and offset
259/// specified by Loc. If FilenameID is -1, it is considered to be
260/// unspecified.
261void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
262 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000263 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000264
Chris Lattnerac50e342009-02-03 22:13:05 +0000265 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
266
267 // Remember that this file has #line directives now if it doesn't already.
268 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
269
270 if (LineTable == 0)
271 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000272 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000273}
274
Chris Lattner9d79eba2009-02-04 05:21:58 +0000275/// AddLineNote - Add a GNU line marker to the line table.
276void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
277 int FilenameID, bool IsFileEntry,
278 bool IsFileExit, bool IsSystemHeader,
279 bool IsExternCHeader) {
280 // If there is no filename and no flags, this is treated just like a #line,
281 // which does not change the flags of the previous line marker.
282 if (FilenameID == -1) {
283 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
284 "Can't set flags without setting the filename!");
285 return AddLineNote(Loc, LineNo, FilenameID);
286 }
287
288 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
289 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
290
291 // Remember that this file has #line directives now if it doesn't already.
292 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
293
294 if (LineTable == 0)
295 LineTable = new LineTableInfo();
296
297 SrcMgr::CharacteristicKind FileKind;
298 if (IsExternCHeader)
299 FileKind = SrcMgr::C_ExternCSystem;
300 else if (IsSystemHeader)
301 FileKind = SrcMgr::C_System;
302 else
303 FileKind = SrcMgr::C_User;
304
305 unsigned EntryExit = 0;
306 if (IsFileEntry)
307 EntryExit = 1;
308 else if (IsFileExit)
309 EntryExit = 2;
310
311 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
312 EntryExit, FileKind);
313}
314
Chris Lattner4c4ea172009-02-03 21:52:55 +0000315
Chris Lattner23b5dc62009-02-04 00:40:31 +0000316//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000317// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000318//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000319
Chris Lattner5b9a5042009-01-26 07:57:50 +0000320SourceManager::~SourceManager() {
321 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000322
323 // Delete FileEntry objects corresponding to content caches. Since the actual
324 // content cache objects are bump pointer allocated, we just have to run the
325 // dtors, but we call the deallocate method for completeness.
326 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
327 MemBufferInfos[i]->~ContentCache();
328 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
329 }
330 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
331 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
332 I->second->~ContentCache();
333 ContentCacheAlloc.Deallocate(I->second);
334 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000335}
336
337void SourceManager::clearIDTables() {
338 MainFileID = FileID();
339 SLocEntryTable.clear();
340 LastLineNoFileIDQuery = FileID();
341 LastLineNoContentCache = 0;
342 LastFileIDLookup = FileID();
343
344 if (LineTable)
345 LineTable->clear();
346
347 // Use up FileID #0 as an invalid instantiation.
348 NextOffset = 0;
349 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
350}
351
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000352/// getOrCreateContentCache - Create or return a cached ContentCache for the
353/// specified file.
354const ContentCache *
355SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000357
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000359 ContentCache *&Entry = FileInfos[FileEnt];
360 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361
Chris Lattner00282d62009-02-03 07:41:46 +0000362 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
363 // so that FileInfo can use the low 3 bits of the pointer for its own
364 // nefarious purposes.
365 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
366 EntryAlign = std::max(8U, EntryAlign);
367 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000368 new (Entry) ContentCache(FileEnt);
369 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000370}
371
372
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000373/// createMemBufferContentCache - Create a new ContentCache for the specified
374/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000375const ContentCache*
376SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000377 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
378 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
379 // the pointer for its own nefarious purposes.
380 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
381 EntryAlign = std::max(8U, EntryAlign);
382 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000383 new (Entry) ContentCache();
384 MemBufferInfos.push_back(Entry);
385 Entry->setBuffer(Buffer);
386 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000387}
388
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000389//===----------------------------------------------------------------------===//
390// Methods to create new FileID's and instantiations.
391//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000392
Nico Weber48002c82008-09-29 00:25:48 +0000393/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000394/// include position. This works regardless of whether the ContentCache
395/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000396FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000397 SourceLocation IncludePos,
398 SrcMgr::CharacteristicKind FileCharacter) {
399 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
400 FileInfo::get(IncludePos, File,
401 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000402 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000403 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
404 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000406 // Set LastFileIDLookup to the newly created file. The next getFileID call is
407 // almost guaranteed to be from that file.
408 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000409}
410
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000411/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000412/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000413/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000414SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
415 SourceLocation InstantLoc,
416 unsigned TokLength) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000417 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
418 InstantiationInfo::get(InstantLoc,
419 SpellingLoc)));
420 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
421 NextOffset += TokLength+1;
422 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000423}
424
Chris Lattner31530ba2009-01-19 07:32:13 +0000425/// getBufferData - Return a pointer to the start and end of the source buffer
426/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000427std::pair<const char*, const char*>
428SourceManager::getBufferData(FileID FID) const {
429 const llvm::MemoryBuffer *Buf = getBuffer(FID);
430 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
431}
432
433
Chris Lattner23b5dc62009-02-04 00:40:31 +0000434//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000435// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000436//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000437
438/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
439/// method that is used for all SourceManager queries that start with a
440/// SourceLocation object. It is responsible for finding the entry in
441/// SLocEntryTable which contains the specified location.
442///
443FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
444 assert(SLocOffset && "Invalid FileID");
445
446 // After the first and second level caches, I see two common sorts of
447 // behavior: 1) a lot of searched FileID's are "near" the cached file location
448 // or are "near" the cached instantiation location. 2) others are just
449 // completely random and may be a very long way away.
450 //
451 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
452 // then we fall back to a less cache efficient, but more scalable, binary
453 // search to find the location.
454
455 // See if this is near the file point - worst case we start scanning from the
456 // most newly created FileID.
457 std::vector<SrcMgr::SLocEntry>::const_iterator I;
458
459 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
460 // Neither loc prunes our search.
461 I = SLocEntryTable.end();
462 } else {
463 // Perhaps it is near the file point.
464 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
465 }
466
467 // Find the FileID that contains this. "I" is an iterator that points to a
468 // FileID whose offset is known to be larger than SLocOffset.
469 unsigned NumProbes = 0;
470 while (1) {
471 --I;
472 if (I->getOffset() <= SLocOffset) {
473#if 0
474 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
475 I-SLocEntryTable.begin(),
476 I->isInstantiation() ? "inst" : "file",
477 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
478#endif
479 FileID Res = FileID::get(I-SLocEntryTable.begin());
480
481 // If this isn't an instantiation, remember it. We have good locality
482 // across FileID lookups.
483 if (!I->isInstantiation())
484 LastFileIDLookup = Res;
485 NumLinearScans += NumProbes+1;
486 return Res;
487 }
488 if (++NumProbes == 8)
489 break;
490 }
491
492 // Convert "I" back into an index. We know that it is an entry whose index is
493 // larger than the offset we are looking for.
494 unsigned GreaterIndex = I-SLocEntryTable.begin();
495 // LessIndex - This is the lower bound of the range that we're searching.
496 // We know that the offset corresponding to the FileID is is less than
497 // SLocOffset.
498 unsigned LessIndex = 0;
499 NumProbes = 0;
500 while (1) {
501 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
502 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
503
504 ++NumProbes;
505
506 // If the offset of the midpoint is too large, chop the high side of the
507 // range to the midpoint.
508 if (MidOffset > SLocOffset) {
509 GreaterIndex = MiddleIndex;
510 continue;
511 }
512
513 // If the middle index contains the value, succeed and return.
514 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
515#if 0
516 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
517 I-SLocEntryTable.begin(),
518 I->isInstantiation() ? "inst" : "file",
519 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
520#endif
521 FileID Res = FileID::get(MiddleIndex);
522
523 // If this isn't an instantiation, remember it. We have good locality
524 // across FileID lookups.
525 if (!I->isInstantiation())
526 LastFileIDLookup = Res;
527 NumBinaryProbes += NumProbes;
528 return Res;
529 }
530
531 // Otherwise, move the low-side up to the middle index.
532 LessIndex = MiddleIndex;
533 }
534}
535
Chris Lattneraddb7972009-01-26 20:04:19 +0000536SourceLocation SourceManager::
537getInstantiationLocSlowCase(SourceLocation Loc) const {
538 do {
539 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
540 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
541 Loc = Loc.getFileLocWithOffset(LocInfo.second);
542 } while (!Loc.isFileID());
543
544 return Loc;
545}
546
547SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
548 do {
549 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
550 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
551 Loc = Loc.getFileLocWithOffset(LocInfo.second);
552 } while (!Loc.isFileID());
553 return Loc;
554}
555
556
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000557std::pair<FileID, unsigned>
558SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
559 unsigned Offset) const {
560 // 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().getInstantiationLoc();
566
567 FID = getFileID(Loc);
568 E = &getSLocEntry(FID);
569 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000570 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000571
572 return std::make_pair(FID, Offset);
573}
574
575std::pair<FileID, unsigned>
576SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
577 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000578 // If this is an instantiation record, walk through all the instantiation
579 // points.
580 FileID FID;
581 SourceLocation Loc;
582 do {
583 Loc = E->getInstantiation().getSpellingLoc();
584
585 FID = getFileID(Loc);
586 E = &getSLocEntry(FID);
587 Offset += Loc.getOffset()-E->getOffset();
588 } while (!Loc.isFileID());
589
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000590 return std::make_pair(FID, Offset);
591}
592
593
594//===----------------------------------------------------------------------===//
595// Queries about the code at a SourceLocation.
596//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000597
598/// getCharacterData - Return a pointer to the start of the specified location
599/// in the appropriate MemoryBuffer.
600const char *SourceManager::getCharacterData(SourceLocation SL) const {
601 // Note that this is a hot function in the getSpelling() path, which is
602 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000603 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000604
Ted Kremenekc16c2082009-01-06 01:55:26 +0000605 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000606 return getSLocEntry(LocInfo.first).getFile().getContentCache()
607 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000608}
609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610
Chris Lattner9dc1f532007-07-20 16:37:10 +0000611/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000612/// this is significantly cheaper to compute than the line number.
613unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
614 const char *Buf = getBuffer(FID)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 unsigned LineStart = FilePos;
617 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
618 --LineStart;
619 return FilePos-LineStart+1;
620}
621
Chris Lattner7da5aea2009-02-04 00:55:58 +0000622unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000623 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000624 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
625 return getColumnNumber(LocInfo.first, LocInfo.second);
626}
627
628unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000629 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000630 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
631 return getColumnNumber(LocInfo.first, LocInfo.second);
632}
633
634
635
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000636static void ComputeLineNumbers(ContentCache* FI,
637 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
638static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000639 // Note that calling 'getBuffer()' may lazily page in the file.
640 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000641
642 // Find the file offsets of all of the *physical* source lines. This does
643 // not look at trigraphs, escaped newlines, or anything else tricky.
644 std::vector<unsigned> LineOffsets;
645
646 // Line #1 starts at char 0.
647 LineOffsets.push_back(0);
648
649 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
650 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
651 unsigned Offs = 0;
652 while (1) {
653 // Skip over the contents of the line.
654 // TODO: Vectorize this? This is very performance sensitive for programs
655 // with lots of diagnostics and in -E mode.
656 const unsigned char *NextBuf = (const unsigned char *)Buf;
657 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
658 ++NextBuf;
659 Offs += NextBuf-Buf;
660 Buf = NextBuf;
661
662 if (Buf[0] == '\n' || Buf[0] == '\r') {
663 // If this is \n\r or \r\n, skip both characters.
664 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
665 ++Offs, ++Buf;
666 ++Offs, ++Buf;
667 LineOffsets.push_back(Offs);
668 } else {
669 // Otherwise, this is a null. If end of file, exit.
670 if (Buf == End) break;
671 // Otherwise, skip the null.
672 ++Offs, ++Buf;
673 }
674 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000675
676 // Copy the offsets into the FileInfo structure.
677 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000678 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000679 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
680}
Reid Spencer5f016e22007-07-11 17:01:13 +0000681
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000682/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000683/// for the position indicated. This requires building and caching a table of
684/// line offsets for the MemoryBuffer, so this is not cheap: use only when
685/// about to emit a diagnostic.
Chris Lattner30fc9332009-02-04 01:06:56 +0000686unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000687 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000688 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000689 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000690 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000691 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000692 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000693
694 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000695 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000696 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000697 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000698
699 // Okay, we know we have a line number table. Do a binary search to find the
700 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000701 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000702 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000703 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000704
Chris Lattner30fc9332009-02-04 01:06:56 +0000705 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000706
707 // If the previous query was to the same file, we know both the file pos from
708 // that query and the line number returned. This allows us to narrow the
709 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000710 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000711 if (QueriedFilePos >= LastLineNoFilePos) {
712 SourceLineCache = SourceLineCache+LastLineNoResult-1;
713
714 // The query is likely to be nearby the previous one. Here we check to
715 // see if it is within 5, 10 or 20 lines. It can be far away in cases
716 // where big comment blocks and vertical whitespace eat up lines but
717 // contribute no tokens.
718 if (SourceLineCache+5 < SourceLineCacheEnd) {
719 if (SourceLineCache[5] > QueriedFilePos)
720 SourceLineCacheEnd = SourceLineCache+5;
721 else if (SourceLineCache+10 < SourceLineCacheEnd) {
722 if (SourceLineCache[10] > QueriedFilePos)
723 SourceLineCacheEnd = SourceLineCache+10;
724 else if (SourceLineCache+20 < SourceLineCacheEnd) {
725 if (SourceLineCache[20] > QueriedFilePos)
726 SourceLineCacheEnd = SourceLineCache+20;
727 }
728 }
729 }
730 } else {
731 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
732 }
733 }
734
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000735 // If the spread is large, do a "radix" test as our initial guess, based on
736 // the assumption that lines average to approximately the same length.
737 // NOTE: This is currently disabled, as it does not appear to be profitable in
738 // initial measurements.
739 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000740 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000741
742 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000743 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000744
745 // Check for -10 and +10 lines.
746 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
747 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
748
749 // If the computed lower bound is less than the query location, move it in.
750 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
751 SourceLineCacheStart[LowerBound] < QueriedFilePos)
752 SourceLineCache = SourceLineCacheStart+LowerBound;
753
754 // If the computed upper bound is greater than the query location, move it.
755 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
756 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
757 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
758 }
759
760 unsigned *Pos
761 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000762 unsigned LineNo = Pos-SourceLineCacheStart;
763
Chris Lattner30fc9332009-02-04 01:06:56 +0000764 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000765 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000766 LastLineNoFilePos = QueriedFilePos;
767 LastLineNoResult = LineNo;
768 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000769}
770
Chris Lattner30fc9332009-02-04 01:06:56 +0000771unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
772 if (Loc.isInvalid()) return 0;
773 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
774 return getLineNumber(LocInfo.first, LocInfo.second);
775}
776unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
777 if (Loc.isInvalid()) return 0;
778 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
779 return getLineNumber(LocInfo.first, LocInfo.second);
780}
781
Chris Lattner6b306672009-02-04 05:33:01 +0000782/// getFileCharacteristic - return the file characteristic of the specified
783/// source location, indicating whether this is a normal file, a system
784/// header, or an "implicit extern C" system header.
785///
786/// This state can be modified with flags on GNU linemarker directives like:
787/// # 4 "foo.h" 3
788/// which changes all source locations in the current file after that to be
789/// considered to be from a system header.
790SrcMgr::CharacteristicKind
791SourceManager::getFileCharacteristic(SourceLocation Loc) const {
792 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
793 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
794 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
795
796 // If there are no #line directives in this file, just return the whole-file
797 // state.
798 if (!FI.hasLineDirectives())
799 return FI.getFileCharacteristic();
800
801 assert(LineTable && "Can't have linetable entries without a LineTable!");
802 // See if there is a #line directive before the location.
803 const LineEntry *Entry =
804 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
805
806 // If this is before the first line marker, use the file characteristic.
807 if (!Entry)
808 return FI.getFileCharacteristic();
809
810 return Entry->FileKind;
811}
812
Chris Lattner30fc9332009-02-04 01:06:56 +0000813
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000814/// getPresumedLoc - This method returns the "presumed" location of a
815/// SourceLocation specifies. A "presumed location" can be modified by #line
816/// or GNU line marker directives. This provides a view on the data that a
817/// user should see in diagnostics, for example.
818///
819/// Note that a presumed location is always given as the instantiation point
820/// of an instantiation location, not at the spelling location.
821PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
822 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000823
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000824 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000825 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000826
Chris Lattner30fc9332009-02-04 01:06:56 +0000827 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000828 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000829
830 // To get the source name, first consult the FileEntry (if one exists)
831 // before the MemBuffer as this will avoid unnecessarily paging in the
832 // MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000833 const char *Filename =
834 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000835 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
836 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
837 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000838
Chris Lattner3cd949c2009-02-04 01:55:42 +0000839 // If we have #line directives in this file, update and overwrite the physical
840 // location info if appropriate.
841 if (FI.hasLineDirectives()) {
842 assert(LineTable && "Can't have linetable entries without a LineTable!");
843 // See if there is a #line directive before this. If so, get it.
844 if (const LineEntry *Entry =
845 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +0000846 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000847 if (Entry->FilenameID != -1)
848 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +0000849
850 // Use the line number specified by the LineEntry. This line number may
851 // be multiple lines down from the line entry. Add the difference in
852 // physical line numbers from the query point and the line marker to the
853 // total.
854 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
855 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
856
Chris Lattner0e0e5da2009-02-04 02:15:40 +0000857 // Note that column numbers are not molested by line markers.
Chris Lattner137b6a62009-02-04 06:25:26 +0000858
859 // Handle virtual #include manipulation.
860 if (Entry->IncludeOffset) {
861 IncludeLoc = getLocForStartOfFile(LocInfo.first);
862 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
863 }
Chris Lattner3cd949c2009-02-04 01:55:42 +0000864 }
865 }
866
867 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000868}
869
870//===----------------------------------------------------------------------===//
871// Other miscellaneous methods.
872//===----------------------------------------------------------------------===//
873
874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875/// PrintStats - Print statistics to stderr.
876///
877void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000878 llvm::cerr << "\n*** Source Manager Stats:\n";
879 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000880 << " mem buffers mapped.\n";
881 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
882 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000883
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 unsigned NumLineNumsComputed = 0;
885 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000886 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
887 NumLineNumsComputed += I->second->SourceLineCache != 0;
888 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000890
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000891 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
892 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000893 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
894 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000895}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000896
897//===----------------------------------------------------------------------===//
898// Serialization.
899//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000900
901void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000902 S.FlushRecord();
903 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000904
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000905 if (Entry) {
906 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
907
908 if (Fname.isAbsolute())
909 S.EmitCStr(Fname.c_str());
910 else {
911 // Create an absolute path.
912 // FIXME: This will potentially contain ".." and "." in the path.
913 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
914 path.appendComponent(Fname.c_str());
915 S.EmitCStr(path.c_str());
916 }
917 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000918 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000919 const char* p = Buffer->getBufferStart();
920 const char* e = Buffer->getBufferEnd();
921
Ted Kremenek099b4742007-12-05 00:14:18 +0000922 S.EmitInt(e-p);
923
Ted Kremeneke21272f2007-12-04 19:39:02 +0000924 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000925 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000926 }
927
Ted Kremenek099b4742007-12-05 00:14:18 +0000928 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000929}
Ted Kremenek099b4742007-12-05 00:14:18 +0000930
931void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
932 SourceManager& SMgr,
933 FileManager* FMgr,
934 std::vector<char>& Buf) {
935 if (FMgr) {
936 llvm::SerializedPtrID PtrID = D.ReadPtrID();
937 D.ReadCStr(Buf,false);
938
939 // Create/fetch the FileEntry.
940 const char* start = &Buf[0];
941 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
942
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000943 // FIXME: Ideally we want a lazy materialization of the ContentCache
944 // anyway, because we don't want to read in source files unless this
945 // is absolutely needed.
946 if (!E)
947 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000948 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000949 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000950 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
951 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000952 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000953
954 // Register the ContextCache object with the deserializer.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000955 /* FIXME:
956 ContentCache *Entry
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000957 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000958 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000959 D.RegisterPtr(&Entry);
960
961 // Create the buffer.
962 unsigned Size = D.ReadInt();
963 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
964
965 // Read the contents of the buffer.
966 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
967 for (unsigned i = 0; i < Size ; ++i)
968 p[i] = D.ReadInt();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000969 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000970}
971
972void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000973 S.EnterBlock();
974 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000975 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000976
Ted Kremenek099b4742007-12-05 00:14:18 +0000977 // Emit: FileInfos. Just emit the file name.
978 S.EnterBlock();
979
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000980 // FIXME: Emit FileInfos.
981 //std::for_each(FileInfos.begin(), FileInfos.end(),
982 // S.MakeEmitter<ContentCache>());
Ted Kremenek099b4742007-12-05 00:14:18 +0000983
984 S.ExitBlock();
985
986 // Emit: MemBufferInfos
987 S.EnterBlock();
988
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000989 /* FIXME: EMIT.
Ted Kremenek099b4742007-12-05 00:14:18 +0000990 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
991 S.MakeEmitter<ContentCache>());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000992 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000993
994 S.ExitBlock();
995
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000996 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000997
998 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000999}
1000
Ted Kremenek1f941002007-12-05 00:19:51 +00001001SourceManager*
Chris Lattner23b5dc62009-02-04 00:40:31 +00001002SourceManager::CreateAndRegister(llvm::Deserializer &D, FileManager &FMgr) {
Ted Kremenek1f941002007-12-05 00:19:51 +00001003 SourceManager *M = new SourceManager();
1004 D.RegisterPtr(M);
1005
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001006 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001007 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001008
Ted Kremenek099b4742007-12-05 00:14:18 +00001009 std::vector<char> Buf;
1010
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001011 /*{ // FIXME Read: FileInfos.
Ted Kremenek099b4742007-12-05 00:14:18 +00001012 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
1013 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +00001014 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001015 }*/
Ted Kremenek099b4742007-12-05 00:14:18 +00001016
1017 { // Read: MemBufferInfos.
1018 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
1019 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +00001020 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +00001021 }
1022
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001023 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +00001024
1025 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +00001026}