blob: 6381687d6ef65991300c2425dcb0c4de5b68c6b0 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceManager.h"
15#include "clang/Basic/FileManager.h"
16#include "llvm/Support/Compiler.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/System/Path.h"
Ted Kremenekdd364ea2007-10-30 21:08:08 +000019#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
Ted Kremenekda29d8c2007-12-05 22:21:13 +000021#include "llvm/Support/Streams.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Chris Lattnerfa45efd2009-02-04 00:40:31 +000027//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +000028// SourceManager Helper Classes
Chris Lattnerfa45efd2009-02-04 00:40:31 +000029//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +000030
Ted Kremenekdd364ea2007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
Chris Lattner4b009652007-07-25 00:24:17 +000033}
34
Ted Kremenekaa7dac12009-01-06 01:55:26 +000035/// getSizeBytesMapped - Returns the number of bytes actually mapped for
36/// this ContentCache. This can be 0 if the MemBuffer was not actually
37/// instantiated.
38unsigned ContentCache::getSizeBytesMapped() const {
39 return Buffer ? Buffer->getBufferSize() : 0;
40}
41
42/// getSize - Returns the size of the content encapsulated by this ContentCache.
43/// This can be the size of the source file or the size of an arbitrary
44/// scratch buffer. If the ContentCache encapsulates a source file, that
45/// file is not lazily brought in from disk to satisfy this query.
46unsigned ContentCache::getSize() const {
47 return Entry ? Entry->getSize() : Buffer->getBufferSize();
48}
49
Chris Lattner68b28ff2009-01-26 07:37:49 +000050const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000051 // Lazily create the Buffer for ContentCaches that wrap files.
52 if (!Buffer && Entry) {
53 // FIXME: Should we support a way to not have to do this check over
54 // and over if we cannot open the file?
Chris Lattnerac49bb42009-01-17 03:54:16 +000055 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000056 }
Ted Kremenekaa7dac12009-01-06 01:55:26 +000057 return Buffer;
58}
59
Chris Lattnerfa45efd2009-02-04 00:40:31 +000060//===----------------------------------------------------------------------===//
Chris Lattnerd2051b42009-01-26 07:57:50 +000061// Line Table Implementation
Chris Lattnerfa45efd2009-02-04 00:40:31 +000062//===----------------------------------------------------------------------===//
Chris Lattnerd2051b42009-01-26 07:57:50 +000063
64namespace clang {
Chris Lattnerfa45efd2009-02-04 00:40:31 +000065struct LineEntry {
66 /// FileOffset - The offset in this file that the line entry occurs at.
67 unsigned FileOffset;
68 /// LineNo - The presumed line number of this line entry: #line 4.
69 unsigned LineNo;
70 /// FilenameID - The ID of the filename identified by this line entry:
71 /// #line 4 "foo.c". This is -1 if not specified.
72 int FilenameID;
73
74 static LineEntry get(unsigned Offs, unsigned Line, int Filename) {
75 LineEntry E;
76 E.FileOffset = Offs;
77 E.LineNo = Line;
78 E.FilenameID = Filename;
79 return E;
80 }
81};
Chris Lattner1a2fbe42009-02-04 04:46:59 +000082
83inline bool operator<(const LineEntry &E, unsigned Offset) {
84 return E.FileOffset < Offset;
85}
86
87inline bool operator<(unsigned Offset, const LineEntry &E) {
88 return Offset < E.FileOffset;
89}
Chris Lattnerfa45efd2009-02-04 00:40:31 +000090
Chris Lattnerd2051b42009-01-26 07:57:50 +000091/// LineTableInfo - This class is used to hold and unique data used to
92/// represent #line information.
93class LineTableInfo {
94 /// FilenameIDs - This map is used to assign unique IDs to filenames in
95 /// #line directives. This allows us to unique the filenames that
96 /// frequently reoccur and reference them with indices. FilenameIDs holds
97 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
98 /// to string.
99 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
100 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000101
102 /// LineEntries - This is a map from FileIDs to a list of line entries (sorted
103 /// by the offset they occur in the file.
104 std::map<unsigned, std::vector<LineEntry> > LineEntries;
Chris Lattnerd2051b42009-01-26 07:57:50 +0000105public:
106 LineTableInfo() {
107 }
108
109 void clear() {
110 FilenameIDs.clear();
111 FilenamesByID.clear();
112 }
113
114 ~LineTableInfo() {}
115
116 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000117 const char *getFilename(unsigned ID) const {
118 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
119 return FilenamesByID[ID]->getKeyData();
120 }
121
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000122 void AddLineNote(unsigned FID, unsigned Offset,
Chris Lattner6256b2b2009-02-03 22:13:05 +0000123 unsigned LineNo, int FilenameID);
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000124
125 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
126 /// it. If there is no line entry before Offset in FID, return null.
127 const LineEntry *FindNearestLineEntry(unsigned FID, unsigned Offset);
Chris Lattnerd2051b42009-01-26 07:57:50 +0000128};
129} // namespace clang
130
Chris Lattnerd2051b42009-01-26 07:57:50 +0000131unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
132 // Look up the filename in the string table, returning the pre-existing value
133 // if it exists.
134 llvm::StringMapEntry<unsigned> &Entry =
135 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
136 if (Entry.getValue() != ~0U)
137 return Entry.getValue();
138
139 // Otherwise, assign this the next available ID.
140 Entry.setValue(FilenamesByID.size());
141 FilenamesByID.push_back(&Entry);
142 return FilenamesByID.size()-1;
143}
144
Chris Lattner6256b2b2009-02-03 22:13:05 +0000145/// AddLineNote - Add a line note to the line table that indicates that there
146/// is a #line at the specified FID/Offset location which changes the presumed
147/// location to LineNo/FilenameID.
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000148void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattner6256b2b2009-02-03 22:13:05 +0000149 unsigned LineNo, int FilenameID) {
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000150 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattner6256b2b2009-02-03 22:13:05 +0000151
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000152 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
153 "Adding line entries out of order!");
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000154
155 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember that
156 // we are still in "foo.h".
157 if (FilenameID == -1 && !Entries.empty())
158 FilenameID = Entries.back().FilenameID;
159
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000160 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID));
Chris Lattner6256b2b2009-02-03 22:13:05 +0000161}
162
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000163/// FindNearestLineEntry - Find the line entry nearest to FID that is before
164/// it. If there is no line entry before Offset in FID, return null.
165const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
166 unsigned Offset) {
167 const std::vector<LineEntry> &Entries = LineEntries[FID];
168 assert(!Entries.empty() && "No #line entries for this FID after all!");
169
Chris Lattner1a2fbe42009-02-04 04:46:59 +0000170 // It is very common for the query to be after the last #line, check this
171 // first.
172 if (Entries.back().FileOffset <= Offset)
173 return &Entries.back();
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000174
Chris Lattner1a2fbe42009-02-04 04:46:59 +0000175 // Do a binary search to find the maximal element that is still before Offset.
176 std::vector<LineEntry>::const_iterator I =
177 std::upper_bound(Entries.begin(), Entries.end(), Offset);
178 if (I == Entries.begin()) return 0;
179 return &*--I;
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000180}
Chris Lattner6256b2b2009-02-03 22:13:05 +0000181
182
Chris Lattnerd2051b42009-01-26 07:57:50 +0000183/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
184///
185unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
186 if (LineTable == 0)
187 LineTable = new LineTableInfo();
188 return LineTable->getLineTableFilenameID(Ptr, Len);
189}
190
191
Chris Lattnerb06739c2009-02-03 21:52:55 +0000192/// AddLineNote - Add a line note to the line table for the FileID and offset
193/// specified by Loc. If FilenameID is -1, it is considered to be
194/// unspecified.
195void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
196 int FilenameID) {
Chris Lattner6256b2b2009-02-03 22:13:05 +0000197 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb06739c2009-02-03 21:52:55 +0000198
Chris Lattner6256b2b2009-02-03 22:13:05 +0000199 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
200
201 // Remember that this file has #line directives now if it doesn't already.
202 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
203
204 if (LineTable == 0)
205 LineTable = new LineTableInfo();
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000206 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattnerb06739c2009-02-03 21:52:55 +0000207}
208
209
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000210//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000211// Private 'Create' methods.
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000212//===----------------------------------------------------------------------===//
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000213
Chris Lattnerd2051b42009-01-26 07:57:50 +0000214SourceManager::~SourceManager() {
215 delete LineTable;
Chris Lattner8dedb842009-02-03 07:30:45 +0000216
217 // Delete FileEntry objects corresponding to content caches. Since the actual
218 // content cache objects are bump pointer allocated, we just have to run the
219 // dtors, but we call the deallocate method for completeness.
220 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
221 MemBufferInfos[i]->~ContentCache();
222 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
223 }
224 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
225 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
226 I->second->~ContentCache();
227 ContentCacheAlloc.Deallocate(I->second);
228 }
Chris Lattnerd2051b42009-01-26 07:57:50 +0000229}
230
231void SourceManager::clearIDTables() {
232 MainFileID = FileID();
233 SLocEntryTable.clear();
234 LastLineNoFileIDQuery = FileID();
235 LastLineNoContentCache = 0;
236 LastFileIDLookup = FileID();
237
238 if (LineTable)
239 LineTable->clear();
240
241 // Use up FileID #0 as an invalid instantiation.
242 NextOffset = 0;
243 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
244}
245
Chris Lattner27c0ced2009-01-26 00:43:02 +0000246/// getOrCreateContentCache - Create or return a cached ContentCache for the
247/// specified file.
248const ContentCache *
249SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Chris Lattner4b009652007-07-25 00:24:17 +0000250 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattner27c0ced2009-01-26 00:43:02 +0000251
Chris Lattner4b009652007-07-25 00:24:17 +0000252 // Do we already have information about this file?
Chris Lattner8dedb842009-02-03 07:30:45 +0000253 ContentCache *&Entry = FileInfos[FileEnt];
254 if (Entry) return Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000255
Chris Lattner917d7c62009-02-03 07:41:46 +0000256 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
257 // so that FileInfo can use the low 3 bits of the pointer for its own
258 // nefarious purposes.
259 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
260 EntryAlign = std::max(8U, EntryAlign);
261 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner8dedb842009-02-03 07:30:45 +0000262 new (Entry) ContentCache(FileEnt);
263 return Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000264}
265
266
Ted Kremenek27f9c9b2007-10-31 17:53:38 +0000267/// createMemBufferContentCache - Create a new ContentCache for the specified
268/// memory buffer. This does no caching.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000269const ContentCache*
270SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner917d7c62009-02-03 07:41:46 +0000271 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
272 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
273 // the pointer for its own nefarious purposes.
274 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
275 EntryAlign = std::max(8U, EntryAlign);
276 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner8dedb842009-02-03 07:30:45 +0000277 new (Entry) ContentCache();
278 MemBufferInfos.push_back(Entry);
279 Entry->setBuffer(Buffer);
280 return Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000281}
282
Chris Lattner27c0ced2009-01-26 00:43:02 +0000283//===----------------------------------------------------------------------===//
284// Methods to create new FileID's and instantiations.
285//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000286
Nico Weber630347d2008-09-29 00:25:48 +0000287/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek7670cca2007-10-30 22:57:35 +0000288/// include position. This works regardless of whether the ContentCache
289/// corresponds to a file or some other input source.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000290FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000291 SourceLocation IncludePos,
292 SrcMgr::CharacteristicKind FileCharacter) {
293 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
294 FileInfo::get(IncludePos, File,
295 FileCharacter)));
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000296 unsigned FileSize = File->getSize();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000297 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
298 NextOffset += FileSize+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000299
Chris Lattner27c0ced2009-01-26 00:43:02 +0000300 // Set LastFileIDLookup to the newly created file. The next getFileID call is
301 // almost guaranteed to be from that file.
302 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000303}
304
Chris Lattner27c0ced2009-01-26 00:43:02 +0000305/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnercdf600e2009-01-16 07:00:02 +0000306/// that a token from SpellingLoc should actually be referenced from
Chris Lattner4b009652007-07-25 00:24:17 +0000307/// InstantiationLoc.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000308SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
309 SourceLocation InstantLoc,
310 unsigned TokLength) {
Chris Lattner27c0ced2009-01-26 00:43:02 +0000311 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
312 InstantiationInfo::get(InstantLoc,
313 SpellingLoc)));
314 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
315 NextOffset += TokLength+1;
316 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Chris Lattner4b009652007-07-25 00:24:17 +0000317}
318
Chris Lattner71e443a2009-01-19 07:32:13 +0000319/// getBufferData - Return a pointer to the start and end of the source buffer
320/// data for the specified FileID.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000321std::pair<const char*, const char*>
322SourceManager::getBufferData(FileID FID) const {
323 const llvm::MemoryBuffer *Buf = getBuffer(FID);
324 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
325}
326
327
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000328//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000329// SourceLocation manipulation methods.
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000330//===----------------------------------------------------------------------===//
Chris Lattner27c0ced2009-01-26 00:43:02 +0000331
332/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
333/// method that is used for all SourceManager queries that start with a
334/// SourceLocation object. It is responsible for finding the entry in
335/// SLocEntryTable which contains the specified location.
336///
337FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
338 assert(SLocOffset && "Invalid FileID");
339
340 // After the first and second level caches, I see two common sorts of
341 // behavior: 1) a lot of searched FileID's are "near" the cached file location
342 // or are "near" the cached instantiation location. 2) others are just
343 // completely random and may be a very long way away.
344 //
345 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
346 // then we fall back to a less cache efficient, but more scalable, binary
347 // search to find the location.
348
349 // See if this is near the file point - worst case we start scanning from the
350 // most newly created FileID.
351 std::vector<SrcMgr::SLocEntry>::const_iterator I;
352
353 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
354 // Neither loc prunes our search.
355 I = SLocEntryTable.end();
356 } else {
357 // Perhaps it is near the file point.
358 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
359 }
360
361 // Find the FileID that contains this. "I" is an iterator that points to a
362 // FileID whose offset is known to be larger than SLocOffset.
363 unsigned NumProbes = 0;
364 while (1) {
365 --I;
366 if (I->getOffset() <= SLocOffset) {
367#if 0
368 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
369 I-SLocEntryTable.begin(),
370 I->isInstantiation() ? "inst" : "file",
371 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
372#endif
373 FileID Res = FileID::get(I-SLocEntryTable.begin());
374
375 // If this isn't an instantiation, remember it. We have good locality
376 // across FileID lookups.
377 if (!I->isInstantiation())
378 LastFileIDLookup = Res;
379 NumLinearScans += NumProbes+1;
380 return Res;
381 }
382 if (++NumProbes == 8)
383 break;
384 }
385
386 // Convert "I" back into an index. We know that it is an entry whose index is
387 // larger than the offset we are looking for.
388 unsigned GreaterIndex = I-SLocEntryTable.begin();
389 // LessIndex - This is the lower bound of the range that we're searching.
390 // We know that the offset corresponding to the FileID is is less than
391 // SLocOffset.
392 unsigned LessIndex = 0;
393 NumProbes = 0;
394 while (1) {
395 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
396 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
397
398 ++NumProbes;
399
400 // If the offset of the midpoint is too large, chop the high side of the
401 // range to the midpoint.
402 if (MidOffset > SLocOffset) {
403 GreaterIndex = MiddleIndex;
404 continue;
405 }
406
407 // If the middle index contains the value, succeed and return.
408 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
409#if 0
410 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
411 I-SLocEntryTable.begin(),
412 I->isInstantiation() ? "inst" : "file",
413 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
414#endif
415 FileID Res = FileID::get(MiddleIndex);
416
417 // If this isn't an instantiation, remember it. We have good locality
418 // across FileID lookups.
419 if (!I->isInstantiation())
420 LastFileIDLookup = Res;
421 NumBinaryProbes += NumProbes;
422 return Res;
423 }
424
425 // Otherwise, move the low-side up to the middle index.
426 LessIndex = MiddleIndex;
427 }
428}
429
Chris Lattner8d92c1a2009-01-26 20:04:19 +0000430SourceLocation SourceManager::
431getInstantiationLocSlowCase(SourceLocation Loc) const {
432 do {
433 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
434 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
435 Loc = Loc.getFileLocWithOffset(LocInfo.second);
436 } while (!Loc.isFileID());
437
438 return Loc;
439}
440
441SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
442 do {
443 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
444 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
445 Loc = Loc.getFileLocWithOffset(LocInfo.second);
446 } while (!Loc.isFileID());
447 return Loc;
448}
449
450
Chris Lattner27c0ced2009-01-26 00:43:02 +0000451std::pair<FileID, unsigned>
452SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
453 unsigned Offset) const {
454 // If this is an instantiation record, walk through all the instantiation
455 // points.
456 FileID FID;
457 SourceLocation Loc;
458 do {
459 Loc = E->getInstantiation().getInstantiationLoc();
460
461 FID = getFileID(Loc);
462 E = &getSLocEntry(FID);
463 Offset += Loc.getOffset()-E->getOffset();
Chris Lattner18ad5582009-01-26 19:41:58 +0000464 } while (!Loc.isFileID());
Chris Lattner27c0ced2009-01-26 00:43:02 +0000465
466 return std::make_pair(FID, Offset);
467}
468
469std::pair<FileID, unsigned>
470SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
471 unsigned Offset) const {
Chris Lattner18ad5582009-01-26 19:41:58 +0000472 // If this is an instantiation record, walk through all the instantiation
473 // points.
474 FileID FID;
475 SourceLocation Loc;
476 do {
477 Loc = E->getInstantiation().getSpellingLoc();
478
479 FID = getFileID(Loc);
480 E = &getSLocEntry(FID);
481 Offset += Loc.getOffset()-E->getOffset();
482 } while (!Loc.isFileID());
483
Chris Lattner27c0ced2009-01-26 00:43:02 +0000484 return std::make_pair(FID, Offset);
485}
486
487
488//===----------------------------------------------------------------------===//
489// Queries about the code at a SourceLocation.
490//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000491
492/// getCharacterData - Return a pointer to the start of the specified location
493/// in the appropriate MemoryBuffer.
494const char *SourceManager::getCharacterData(SourceLocation SL) const {
495 // Note that this is a hot function in the getSpelling() path, which is
496 // heavily used by -E mode.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000497 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000498
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000499 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000500 return getSLocEntry(LocInfo.first).getFile().getContentCache()
501 ->getBuffer()->getBufferStart() + LocInfo.second;
Chris Lattner4b009652007-07-25 00:24:17 +0000502}
503
504
505/// getColumnNumber - Return the column # for the specified file position.
Chris Lattnere79fc852009-02-04 00:55:58 +0000506/// this is significantly cheaper to compute than the line number.
507unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
508 const char *Buf = getBuffer(FID)->getBufferStart();
Chris Lattner4b009652007-07-25 00:24:17 +0000509
Chris Lattner4b009652007-07-25 00:24:17 +0000510 unsigned LineStart = FilePos;
511 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
512 --LineStart;
513 return FilePos-LineStart+1;
514}
515
Chris Lattnere79fc852009-02-04 00:55:58 +0000516unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner2d89c562009-02-04 01:06:56 +0000517 if (Loc.isInvalid()) return 0;
Chris Lattnere79fc852009-02-04 00:55:58 +0000518 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
519 return getColumnNumber(LocInfo.first, LocInfo.second);
520}
521
522unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner2d89c562009-02-04 01:06:56 +0000523 if (Loc.isInvalid()) return 0;
Chris Lattnere79fc852009-02-04 00:55:58 +0000524 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
525 return getColumnNumber(LocInfo.first, LocInfo.second);
526}
527
528
529
Chris Lattner8dedb842009-02-03 07:30:45 +0000530static void ComputeLineNumbers(ContentCache* FI,
531 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
532static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000533 // Note that calling 'getBuffer()' may lazily page in the file.
534 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000535
536 // Find the file offsets of all of the *physical* source lines. This does
537 // not look at trigraphs, escaped newlines, or anything else tricky.
538 std::vector<unsigned> LineOffsets;
539
540 // Line #1 starts at char 0.
541 LineOffsets.push_back(0);
542
543 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
544 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
545 unsigned Offs = 0;
546 while (1) {
547 // Skip over the contents of the line.
548 // TODO: Vectorize this? This is very performance sensitive for programs
549 // with lots of diagnostics and in -E mode.
550 const unsigned char *NextBuf = (const unsigned char *)Buf;
551 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
552 ++NextBuf;
553 Offs += NextBuf-Buf;
554 Buf = NextBuf;
555
556 if (Buf[0] == '\n' || Buf[0] == '\r') {
557 // If this is \n\r or \r\n, skip both characters.
558 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
559 ++Offs, ++Buf;
560 ++Offs, ++Buf;
561 LineOffsets.push_back(Offs);
562 } else {
563 // Otherwise, this is a null. If end of file, exit.
564 if (Buf == End) break;
565 // Otherwise, skip the null.
566 ++Offs, ++Buf;
567 }
568 }
Chris Lattner4b009652007-07-25 00:24:17 +0000569
570 // Copy the offsets into the FileInfo structure.
571 FI->NumLines = LineOffsets.size();
Chris Lattner8dedb842009-02-03 07:30:45 +0000572 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000573 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
574}
575
Chris Lattnercdf600e2009-01-16 07:00:02 +0000576/// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner4b009652007-07-25 00:24:17 +0000577/// for the position indicated. This requires building and caching a table of
578/// line offsets for the MemoryBuffer, so this is not cheap: use only when
579/// about to emit a diagnostic.
Chris Lattner2d89c562009-02-04 01:06:56 +0000580unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000581 ContentCache *Content;
Chris Lattner2d89c562009-02-04 01:06:56 +0000582 if (LastLineNoFileIDQuery == FID)
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000583 Content = LastLineNoContentCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000584 else
Chris Lattner2d89c562009-02-04 01:06:56 +0000585 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattner27c0ced2009-01-26 00:43:02 +0000586 .getFile().getContentCache());
Chris Lattner4b009652007-07-25 00:24:17 +0000587
588 // If this is the first use of line information for this buffer, compute the
589 /// SourceLineCache for it on demand.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000590 if (Content->SourceLineCache == 0)
Chris Lattner8dedb842009-02-03 07:30:45 +0000591 ComputeLineNumbers(Content, ContentCacheAlloc);
Chris Lattner4b009652007-07-25 00:24:17 +0000592
593 // Okay, we know we have a line number table. Do a binary search to find the
594 // line number that this character position lands on.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000595 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000596 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000597 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner4b009652007-07-25 00:24:17 +0000598
Chris Lattner2d89c562009-02-04 01:06:56 +0000599 unsigned QueriedFilePos = FilePos+1;
Chris Lattner4b009652007-07-25 00:24:17 +0000600
601 // If the previous query was to the same file, we know both the file pos from
602 // that query and the line number returned. This allows us to narrow the
603 // search space from the entire file to something near the match.
Chris Lattner2d89c562009-02-04 01:06:56 +0000604 if (LastLineNoFileIDQuery == FID) {
Chris Lattner4b009652007-07-25 00:24:17 +0000605 if (QueriedFilePos >= LastLineNoFilePos) {
606 SourceLineCache = SourceLineCache+LastLineNoResult-1;
607
608 // The query is likely to be nearby the previous one. Here we check to
609 // see if it is within 5, 10 or 20 lines. It can be far away in cases
610 // where big comment blocks and vertical whitespace eat up lines but
611 // contribute no tokens.
612 if (SourceLineCache+5 < SourceLineCacheEnd) {
613 if (SourceLineCache[5] > QueriedFilePos)
614 SourceLineCacheEnd = SourceLineCache+5;
615 else if (SourceLineCache+10 < SourceLineCacheEnd) {
616 if (SourceLineCache[10] > QueriedFilePos)
617 SourceLineCacheEnd = SourceLineCache+10;
618 else if (SourceLineCache+20 < SourceLineCacheEnd) {
619 if (SourceLineCache[20] > QueriedFilePos)
620 SourceLineCacheEnd = SourceLineCache+20;
621 }
622 }
623 }
624 } else {
625 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
626 }
627 }
628
629 // If the spread is large, do a "radix" test as our initial guess, based on
630 // the assumption that lines average to approximately the same length.
631 // NOTE: This is currently disabled, as it does not appear to be profitable in
632 // initial measurements.
633 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000634 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner4b009652007-07-25 00:24:17 +0000635
636 // Take a stab at guessing where it is.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000637 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner4b009652007-07-25 00:24:17 +0000638
639 // Check for -10 and +10 lines.
640 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
641 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
642
643 // If the computed lower bound is less than the query location, move it in.
644 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
645 SourceLineCacheStart[LowerBound] < QueriedFilePos)
646 SourceLineCache = SourceLineCacheStart+LowerBound;
647
648 // If the computed upper bound is greater than the query location, move it.
649 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
650 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
651 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
652 }
653
654 unsigned *Pos
655 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
656 unsigned LineNo = Pos-SourceLineCacheStart;
657
Chris Lattner2d89c562009-02-04 01:06:56 +0000658 LastLineNoFileIDQuery = FID;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000659 LastLineNoContentCache = Content;
Chris Lattner4b009652007-07-25 00:24:17 +0000660 LastLineNoFilePos = QueriedFilePos;
661 LastLineNoResult = LineNo;
662 return LineNo;
663}
664
Chris Lattner2d89c562009-02-04 01:06:56 +0000665unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
666 if (Loc.isInvalid()) return 0;
667 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
668 return getLineNumber(LocInfo.first, LocInfo.second);
669}
670unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
671 if (Loc.isInvalid()) return 0;
672 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
673 return getLineNumber(LocInfo.first, LocInfo.second);
674}
675
676
Chris Lattner836774b2009-01-27 07:57:44 +0000677/// getPresumedLoc - This method returns the "presumed" location of a
678/// SourceLocation specifies. A "presumed location" can be modified by #line
679/// or GNU line marker directives. This provides a view on the data that a
680/// user should see in diagnostics, for example.
681///
682/// Note that a presumed location is always given as the instantiation point
683/// of an instantiation location, not at the spelling location.
684PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
685 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattner27c0ced2009-01-26 00:43:02 +0000686
Chris Lattner836774b2009-01-27 07:57:44 +0000687 // Presumed locations are always for instantiation points.
Chris Lattnere79fc852009-02-04 00:55:58 +0000688 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner836774b2009-01-27 07:57:44 +0000689
Chris Lattner2d89c562009-02-04 01:06:56 +0000690 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattner836774b2009-01-27 07:57:44 +0000691 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000692
693 // To get the source name, first consult the FileEntry (if one exists)
694 // before the MemBuffer as this will avoid unnecessarily paging in the
695 // MemBuffer.
Chris Lattner836774b2009-01-27 07:57:44 +0000696 const char *Filename =
697 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000698 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
699 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
700 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattner836774b2009-01-27 07:57:44 +0000701
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000702 // If we have #line directives in this file, update and overwrite the physical
703 // location info if appropriate.
704 if (FI.hasLineDirectives()) {
705 assert(LineTable && "Can't have linetable entries without a LineTable!");
706 // See if there is a #line directive before this. If so, get it.
707 if (const LineEntry *Entry =
708 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattner74ae2d92009-02-04 02:00:59 +0000709 // If the LineEntry indicates a filename, use it.
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000710 if (Entry->FilenameID != -1)
711 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattner74ae2d92009-02-04 02:00:59 +0000712
713 // Use the line number specified by the LineEntry. This line number may
714 // be multiple lines down from the line entry. Add the difference in
715 // physical line numbers from the query point and the line marker to the
716 // total.
717 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
718 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
719
Chris Lattneraabd3222009-02-04 02:15:40 +0000720 // Note that column numbers are not molested by line markers.
Chris Lattner6f9fa5a2009-02-04 01:55:42 +0000721 }
722 }
723
724 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattner27c0ced2009-01-26 00:43:02 +0000725}
726
727//===----------------------------------------------------------------------===//
728// Other miscellaneous methods.
729//===----------------------------------------------------------------------===//
730
731
Chris Lattner4b009652007-07-25 00:24:17 +0000732/// PrintStats - Print statistics to stderr.
733///
734void SourceManager::PrintStats() const {
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000735 llvm::cerr << "\n*** Source Manager Stats:\n";
736 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner66284fd2009-01-27 05:22:43 +0000737 << " mem buffers mapped.\n";
738 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
739 << NextOffset << "B of Sloc address space used.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000740
Chris Lattner4b009652007-07-25 00:24:17 +0000741 unsigned NumLineNumsComputed = 0;
742 unsigned NumFileBytesMapped = 0;
Chris Lattner8dedb842009-02-03 07:30:45 +0000743 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
744 NumLineNumsComputed += I->second->SourceLineCache != 0;
745 NumFileBytesMapped += I->second->getSizeBytesMapped();
Chris Lattner4b009652007-07-25 00:24:17 +0000746 }
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000747
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000748 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
749 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattner27c0ced2009-01-26 00:43:02 +0000750 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
751 << NumBinaryProbes << " binary.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000752}
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000753
754//===----------------------------------------------------------------------===//
755// Serialization.
756//===----------------------------------------------------------------------===//
Ted Kremenek9c856e92007-12-05 00:14:18 +0000757
758void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000759 S.FlushRecord();
760 S.EmitPtr(this);
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000761
Ted Kremenek1b6dd6f2007-12-18 22:12:19 +0000762 if (Entry) {
763 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
764
765 if (Fname.isAbsolute())
766 S.EmitCStr(Fname.c_str());
767 else {
768 // Create an absolute path.
769 // FIXME: This will potentially contain ".." and "." in the path.
770 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
771 path.appendComponent(Fname.c_str());
772 S.EmitCStr(path.c_str());
773 }
774 }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000775 else {
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000776 const char* p = Buffer->getBufferStart();
777 const char* e = Buffer->getBufferEnd();
778
Ted Kremenek9c856e92007-12-05 00:14:18 +0000779 S.EmitInt(e-p);
780
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000781 for ( ; p != e; ++p)
Ted Kremenek9c856e92007-12-05 00:14:18 +0000782 S.EmitInt(*p);
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000783 }
784
Ted Kremenek9c856e92007-12-05 00:14:18 +0000785 S.FlushRecord();
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000786}
Ted Kremenek9c856e92007-12-05 00:14:18 +0000787
788void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
789 SourceManager& SMgr,
790 FileManager* FMgr,
791 std::vector<char>& Buf) {
792 if (FMgr) {
793 llvm::SerializedPtrID PtrID = D.ReadPtrID();
794 D.ReadCStr(Buf,false);
795
796 // Create/fetch the FileEntry.
797 const char* start = &Buf[0];
798 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
799
Ted Kremenekb92cd872007-12-13 18:12:10 +0000800 // FIXME: Ideally we want a lazy materialization of the ContentCache
801 // anyway, because we don't want to read in source files unless this
802 // is absolutely needed.
803 if (!E)
804 D.RegisterPtr(PtrID,NULL);
Nico Weber630347d2008-09-29 00:25:48 +0000805 else
Ted Kremenekb92cd872007-12-13 18:12:10 +0000806 // Get the ContextCache object and register it with the deserializer.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000807 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
808 return;
Ted Kremenek9c856e92007-12-05 00:14:18 +0000809 }
Chris Lattner27c0ced2009-01-26 00:43:02 +0000810
811 // Register the ContextCache object with the deserializer.
Chris Lattner8dedb842009-02-03 07:30:45 +0000812 /* FIXME:
813 ContentCache *Entry
Chris Lattner27c0ced2009-01-26 00:43:02 +0000814 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner8dedb842009-02-03 07:30:45 +0000815 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattner27c0ced2009-01-26 00:43:02 +0000816 D.RegisterPtr(&Entry);
817
818 // Create the buffer.
819 unsigned Size = D.ReadInt();
820 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
821
822 // Read the contents of the buffer.
823 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
824 for (unsigned i = 0; i < Size ; ++i)
825 p[i] = D.ReadInt();
Chris Lattner8dedb842009-02-03 07:30:45 +0000826 */
Ted Kremenek9c856e92007-12-05 00:14:18 +0000827}
828
829void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000830 S.EnterBlock();
831 S.EmitPtr(this);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000832 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000833
Ted Kremenek9c856e92007-12-05 00:14:18 +0000834 // Emit: FileInfos. Just emit the file name.
835 S.EnterBlock();
836
Chris Lattner8dedb842009-02-03 07:30:45 +0000837 // FIXME: Emit FileInfos.
838 //std::for_each(FileInfos.begin(), FileInfos.end(),
839 // S.MakeEmitter<ContentCache>());
Ted Kremenek9c856e92007-12-05 00:14:18 +0000840
841 S.ExitBlock();
842
843 // Emit: MemBufferInfos
844 S.EnterBlock();
845
Chris Lattner8dedb842009-02-03 07:30:45 +0000846 /* FIXME: EMIT.
Ted Kremenek9c856e92007-12-05 00:14:18 +0000847 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
848 S.MakeEmitter<ContentCache>());
Chris Lattner8dedb842009-02-03 07:30:45 +0000849 */
Ted Kremenek9c856e92007-12-05 00:14:18 +0000850
851 S.ExitBlock();
852
Chris Lattner27c0ced2009-01-26 00:43:02 +0000853 // FIXME: Emit SLocEntryTable.
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000854
855 S.ExitBlock();
Ted Kremenek9c856e92007-12-05 00:14:18 +0000856}
857
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000858SourceManager*
Chris Lattnerfa45efd2009-02-04 00:40:31 +0000859SourceManager::CreateAndRegister(llvm::Deserializer &D, FileManager &FMgr) {
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000860 SourceManager *M = new SourceManager();
861 D.RegisterPtr(M);
862
Ted Kremenek2578dd02007-12-19 22:29:55 +0000863 // Read: the FileID of the main source file of the translation unit.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000864 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek2578dd02007-12-19 22:29:55 +0000865
Ted Kremenek9c856e92007-12-05 00:14:18 +0000866 std::vector<char> Buf;
867
Chris Lattner8dedb842009-02-03 07:30:45 +0000868 /*{ // FIXME Read: FileInfos.
Ted Kremenek9c856e92007-12-05 00:14:18 +0000869 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
870 while (!D.FinishedBlock(BLoc))
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000871 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner8dedb842009-02-03 07:30:45 +0000872 }*/
Ted Kremenek9c856e92007-12-05 00:14:18 +0000873
874 { // Read: MemBufferInfos.
875 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
876 while (!D.FinishedBlock(BLoc))
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000877 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000878 }
879
Chris Lattner27c0ced2009-01-26 00:43:02 +0000880 // FIXME: Read SLocEntryTable.
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000881
882 return M;
Ted Kremenek12206af2007-12-10 18:01:25 +0000883}