blob: c32264627aee6d5b60f856e7b47119a9f8775f11 [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;
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};
82
83
Chris Lattner5b9a5042009-01-26 07:57:50 +000084/// LineTableInfo - This class is used to hold and unique data used to
85/// represent #line information.
86class LineTableInfo {
87 /// FilenameIDs - This map is used to assign unique IDs to filenames in
88 /// #line directives. This allows us to unique the filenames that
89 /// frequently reoccur and reference them with indices. FilenameIDs holds
90 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
91 /// to string.
92 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
93 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
Chris Lattner23b5dc62009-02-04 00:40:31 +000094
95 /// LineEntries - This is a map from FileIDs to a list of line entries (sorted
96 /// by the offset they occur in the file.
97 std::map<unsigned, std::vector<LineEntry> > LineEntries;
Chris Lattner5b9a5042009-01-26 07:57:50 +000098public:
99 LineTableInfo() {
100 }
101
102 void clear() {
103 FilenameIDs.clear();
104 FilenamesByID.clear();
105 }
106
107 ~LineTableInfo() {}
108
109 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Chris Lattner3cd949c2009-02-04 01:55:42 +0000110 const char *getFilename(unsigned ID) const {
111 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
112 return FilenamesByID[ID]->getKeyData();
113 }
114
Chris Lattner23b5dc62009-02-04 00:40:31 +0000115 void AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000116 unsigned LineNo, int FilenameID);
Chris Lattner3cd949c2009-02-04 01:55:42 +0000117
118 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
119 /// it. If there is no line entry before Offset in FID, return null.
120 const LineEntry *FindNearestLineEntry(unsigned FID, unsigned Offset);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000121};
122} // namespace clang
123
Chris Lattner5b9a5042009-01-26 07:57:50 +0000124unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
125 // Look up the filename in the string table, returning the pre-existing value
126 // if it exists.
127 llvm::StringMapEntry<unsigned> &Entry =
128 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
129 if (Entry.getValue() != ~0U)
130 return Entry.getValue();
131
132 // Otherwise, assign this the next available ID.
133 Entry.setValue(FilenamesByID.size());
134 FilenamesByID.push_back(&Entry);
135 return FilenamesByID.size()-1;
136}
137
Chris Lattnerac50e342009-02-03 22:13:05 +0000138/// AddLineNote - Add a line note to the line table that indicates that there
139/// is a #line at the specified FID/Offset location which changes the presumed
140/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000141void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000142 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000143 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +0000144
Chris Lattner23b5dc62009-02-04 00:40:31 +0000145 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
146 "Adding line entries out of order!");
Chris Lattner3cd949c2009-02-04 01:55:42 +0000147
148 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember that
149 // we are still in "foo.h".
150 if (FilenameID == -1 && !Entries.empty())
151 FilenameID = Entries.back().FilenameID;
152
Chris Lattner23b5dc62009-02-04 00:40:31 +0000153 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID));
Chris Lattnerac50e342009-02-03 22:13:05 +0000154}
155
Chris Lattner3cd949c2009-02-04 01:55:42 +0000156/// FindNearestLineEntry - Find the line entry nearest to FID that is before
157/// it. If there is no line entry before Offset in FID, return null.
158const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
159 unsigned Offset) {
160 const std::vector<LineEntry> &Entries = LineEntries[FID];
161 assert(!Entries.empty() && "No #line entries for this FID after all!");
162
163 if (Entries[0].FileOffset > Offset) return 0;
164
165 // FIXME: Dumb linear search.
166 // Find the maximal element that is still before Offset.
167 for (unsigned i = 1, e = Entries.size(); i != e; ++i)
168 if (Entries[i].FileOffset > Offset) return &Entries[i-1];
169 // Otherwise, all entries are before Offset.
170 return &Entries.back();
171}
Chris Lattnerac50e342009-02-03 22:13:05 +0000172
173
Chris Lattner5b9a5042009-01-26 07:57:50 +0000174/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
175///
176unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
177 if (LineTable == 0)
178 LineTable = new LineTableInfo();
179 return LineTable->getLineTableFilenameID(Ptr, Len);
180}
181
182
Chris Lattner4c4ea172009-02-03 21:52:55 +0000183/// AddLineNote - Add a line note to the line table for the FileID and offset
184/// specified by Loc. If FilenameID is -1, it is considered to be
185/// unspecified.
186void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
187 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000188 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000189
Chris Lattnerac50e342009-02-03 22:13:05 +0000190 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
191
192 // Remember that this file has #line directives now if it doesn't already.
193 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
194
195 if (LineTable == 0)
196 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000197 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000198}
199
200
Chris Lattner23b5dc62009-02-04 00:40:31 +0000201//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000202// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000203//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000204
Chris Lattner5b9a5042009-01-26 07:57:50 +0000205SourceManager::~SourceManager() {
206 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000207
208 // Delete FileEntry objects corresponding to content caches. Since the actual
209 // content cache objects are bump pointer allocated, we just have to run the
210 // dtors, but we call the deallocate method for completeness.
211 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
212 MemBufferInfos[i]->~ContentCache();
213 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
214 }
215 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
216 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
217 I->second->~ContentCache();
218 ContentCacheAlloc.Deallocate(I->second);
219 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000220}
221
222void SourceManager::clearIDTables() {
223 MainFileID = FileID();
224 SLocEntryTable.clear();
225 LastLineNoFileIDQuery = FileID();
226 LastLineNoContentCache = 0;
227 LastFileIDLookup = FileID();
228
229 if (LineTable)
230 LineTable->clear();
231
232 // Use up FileID #0 as an invalid instantiation.
233 NextOffset = 0;
234 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
235}
236
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000237/// getOrCreateContentCache - Create or return a cached ContentCache for the
238/// specified file.
239const ContentCache *
240SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000244 ContentCache *&Entry = FileInfos[FileEnt];
245 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
Chris Lattner00282d62009-02-03 07:41:46 +0000247 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
248 // so that FileInfo can use the low 3 bits of the pointer for its own
249 // nefarious purposes.
250 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
251 EntryAlign = std::max(8U, EntryAlign);
252 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000253 new (Entry) ContentCache(FileEnt);
254 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
257
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000258/// createMemBufferContentCache - Create a new ContentCache for the specified
259/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000260const ContentCache*
261SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000262 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
263 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
264 // the pointer for its own nefarious purposes.
265 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
266 EntryAlign = std::max(8U, EntryAlign);
267 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000268 new (Entry) ContentCache();
269 MemBufferInfos.push_back(Entry);
270 Entry->setBuffer(Buffer);
271 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000272}
273
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000274//===----------------------------------------------------------------------===//
275// Methods to create new FileID's and instantiations.
276//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000277
Nico Weber48002c82008-09-29 00:25:48 +0000278/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000279/// include position. This works regardless of whether the ContentCache
280/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000281FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000282 SourceLocation IncludePos,
283 SrcMgr::CharacteristicKind FileCharacter) {
284 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
285 FileInfo::get(IncludePos, File,
286 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000287 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000288 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
289 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000290
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000291 // Set LastFileIDLookup to the newly created file. The next getFileID call is
292 // almost guaranteed to be from that file.
293 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000296/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000297/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000298/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000299SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
300 SourceLocation InstantLoc,
301 unsigned TokLength) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000302 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
303 InstantiationInfo::get(InstantLoc,
304 SpellingLoc)));
305 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
306 NextOffset += TokLength+1;
307 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000308}
309
Chris Lattner31530ba2009-01-19 07:32:13 +0000310/// getBufferData - Return a pointer to the start and end of the source buffer
311/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000312std::pair<const char*, const char*>
313SourceManager::getBufferData(FileID FID) const {
314 const llvm::MemoryBuffer *Buf = getBuffer(FID);
315 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
316}
317
318
Chris Lattner23b5dc62009-02-04 00:40:31 +0000319//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000320// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000321//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000322
323/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
324/// method that is used for all SourceManager queries that start with a
325/// SourceLocation object. It is responsible for finding the entry in
326/// SLocEntryTable which contains the specified location.
327///
328FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
329 assert(SLocOffset && "Invalid FileID");
330
331 // After the first and second level caches, I see two common sorts of
332 // behavior: 1) a lot of searched FileID's are "near" the cached file location
333 // or are "near" the cached instantiation location. 2) others are just
334 // completely random and may be a very long way away.
335 //
336 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
337 // then we fall back to a less cache efficient, but more scalable, binary
338 // search to find the location.
339
340 // See if this is near the file point - worst case we start scanning from the
341 // most newly created FileID.
342 std::vector<SrcMgr::SLocEntry>::const_iterator I;
343
344 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
345 // Neither loc prunes our search.
346 I = SLocEntryTable.end();
347 } else {
348 // Perhaps it is near the file point.
349 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
350 }
351
352 // Find the FileID that contains this. "I" is an iterator that points to a
353 // FileID whose offset is known to be larger than SLocOffset.
354 unsigned NumProbes = 0;
355 while (1) {
356 --I;
357 if (I->getOffset() <= SLocOffset) {
358#if 0
359 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
360 I-SLocEntryTable.begin(),
361 I->isInstantiation() ? "inst" : "file",
362 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
363#endif
364 FileID Res = FileID::get(I-SLocEntryTable.begin());
365
366 // If this isn't an instantiation, remember it. We have good locality
367 // across FileID lookups.
368 if (!I->isInstantiation())
369 LastFileIDLookup = Res;
370 NumLinearScans += NumProbes+1;
371 return Res;
372 }
373 if (++NumProbes == 8)
374 break;
375 }
376
377 // Convert "I" back into an index. We know that it is an entry whose index is
378 // larger than the offset we are looking for.
379 unsigned GreaterIndex = I-SLocEntryTable.begin();
380 // LessIndex - This is the lower bound of the range that we're searching.
381 // We know that the offset corresponding to the FileID is is less than
382 // SLocOffset.
383 unsigned LessIndex = 0;
384 NumProbes = 0;
385 while (1) {
386 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
387 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
388
389 ++NumProbes;
390
391 // If the offset of the midpoint is too large, chop the high side of the
392 // range to the midpoint.
393 if (MidOffset > SLocOffset) {
394 GreaterIndex = MiddleIndex;
395 continue;
396 }
397
398 // If the middle index contains the value, succeed and return.
399 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
400#if 0
401 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
402 I-SLocEntryTable.begin(),
403 I->isInstantiation() ? "inst" : "file",
404 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
405#endif
406 FileID Res = FileID::get(MiddleIndex);
407
408 // If this isn't an instantiation, remember it. We have good locality
409 // across FileID lookups.
410 if (!I->isInstantiation())
411 LastFileIDLookup = Res;
412 NumBinaryProbes += NumProbes;
413 return Res;
414 }
415
416 // Otherwise, move the low-side up to the middle index.
417 LessIndex = MiddleIndex;
418 }
419}
420
Chris Lattneraddb7972009-01-26 20:04:19 +0000421SourceLocation SourceManager::
422getInstantiationLocSlowCase(SourceLocation Loc) const {
423 do {
424 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
425 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
426 Loc = Loc.getFileLocWithOffset(LocInfo.second);
427 } while (!Loc.isFileID());
428
429 return Loc;
430}
431
432SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
433 do {
434 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
435 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
436 Loc = Loc.getFileLocWithOffset(LocInfo.second);
437 } while (!Loc.isFileID());
438 return Loc;
439}
440
441
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000442std::pair<FileID, unsigned>
443SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
444 unsigned Offset) const {
445 // If this is an instantiation record, walk through all the instantiation
446 // points.
447 FileID FID;
448 SourceLocation Loc;
449 do {
450 Loc = E->getInstantiation().getInstantiationLoc();
451
452 FID = getFileID(Loc);
453 E = &getSLocEntry(FID);
454 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000455 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000456
457 return std::make_pair(FID, Offset);
458}
459
460std::pair<FileID, unsigned>
461SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
462 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000463 // If this is an instantiation record, walk through all the instantiation
464 // points.
465 FileID FID;
466 SourceLocation Loc;
467 do {
468 Loc = E->getInstantiation().getSpellingLoc();
469
470 FID = getFileID(Loc);
471 E = &getSLocEntry(FID);
472 Offset += Loc.getOffset()-E->getOffset();
473 } while (!Loc.isFileID());
474
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000475 return std::make_pair(FID, Offset);
476}
477
478
479//===----------------------------------------------------------------------===//
480// Queries about the code at a SourceLocation.
481//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000482
483/// getCharacterData - Return a pointer to the start of the specified location
484/// in the appropriate MemoryBuffer.
485const char *SourceManager::getCharacterData(SourceLocation SL) const {
486 // Note that this is a hot function in the getSpelling() path, which is
487 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000488 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000489
Ted Kremenekc16c2082009-01-06 01:55:26 +0000490 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000491 return getSLocEntry(LocInfo.first).getFile().getContentCache()
492 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000493}
494
Reid Spencer5f016e22007-07-11 17:01:13 +0000495
Chris Lattner9dc1f532007-07-20 16:37:10 +0000496/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000497/// this is significantly cheaper to compute than the line number.
498unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
499 const char *Buf = getBuffer(FID)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000500
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 unsigned LineStart = FilePos;
502 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
503 --LineStart;
504 return FilePos-LineStart+1;
505}
506
Chris Lattner7da5aea2009-02-04 00:55:58 +0000507unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000508 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000509 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
510 return getColumnNumber(LocInfo.first, LocInfo.second);
511}
512
513unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000514 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000515 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
516 return getColumnNumber(LocInfo.first, LocInfo.second);
517}
518
519
520
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000521static void ComputeLineNumbers(ContentCache* FI,
522 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
523static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000524 // Note that calling 'getBuffer()' may lazily page in the file.
525 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000526
527 // Find the file offsets of all of the *physical* source lines. This does
528 // not look at trigraphs, escaped newlines, or anything else tricky.
529 std::vector<unsigned> LineOffsets;
530
531 // Line #1 starts at char 0.
532 LineOffsets.push_back(0);
533
534 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
535 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
536 unsigned Offs = 0;
537 while (1) {
538 // Skip over the contents of the line.
539 // TODO: Vectorize this? This is very performance sensitive for programs
540 // with lots of diagnostics and in -E mode.
541 const unsigned char *NextBuf = (const unsigned char *)Buf;
542 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
543 ++NextBuf;
544 Offs += NextBuf-Buf;
545 Buf = NextBuf;
546
547 if (Buf[0] == '\n' || Buf[0] == '\r') {
548 // If this is \n\r or \r\n, skip both characters.
549 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
550 ++Offs, ++Buf;
551 ++Offs, ++Buf;
552 LineOffsets.push_back(Offs);
553 } else {
554 // Otherwise, this is a null. If end of file, exit.
555 if (Buf == End) break;
556 // Otherwise, skip the null.
557 ++Offs, ++Buf;
558 }
559 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000560
561 // Copy the offsets into the FileInfo structure.
562 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000563 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000564 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
565}
Reid Spencer5f016e22007-07-11 17:01:13 +0000566
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000567/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000568/// for the position indicated. This requires building and caching a table of
569/// line offsets for the MemoryBuffer, so this is not cheap: use only when
570/// about to emit a diagnostic.
Chris Lattner30fc9332009-02-04 01:06:56 +0000571unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000572 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000573 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000574 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000575 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000576 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000577 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
579 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000580 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000581 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000582 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583
584 // Okay, we know we have a line number table. Do a binary search to find the
585 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000586 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000587 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000588 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000589
Chris Lattner30fc9332009-02-04 01:06:56 +0000590 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000591
592 // If the previous query was to the same file, we know both the file pos from
593 // that query and the line number returned. This allows us to narrow the
594 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000595 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000596 if (QueriedFilePos >= LastLineNoFilePos) {
597 SourceLineCache = SourceLineCache+LastLineNoResult-1;
598
599 // The query is likely to be nearby the previous one. Here we check to
600 // see if it is within 5, 10 or 20 lines. It can be far away in cases
601 // where big comment blocks and vertical whitespace eat up lines but
602 // contribute no tokens.
603 if (SourceLineCache+5 < SourceLineCacheEnd) {
604 if (SourceLineCache[5] > QueriedFilePos)
605 SourceLineCacheEnd = SourceLineCache+5;
606 else if (SourceLineCache+10 < SourceLineCacheEnd) {
607 if (SourceLineCache[10] > QueriedFilePos)
608 SourceLineCacheEnd = SourceLineCache+10;
609 else if (SourceLineCache+20 < SourceLineCacheEnd) {
610 if (SourceLineCache[20] > QueriedFilePos)
611 SourceLineCacheEnd = SourceLineCache+20;
612 }
613 }
614 }
615 } else {
616 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
617 }
618 }
619
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000620 // If the spread is large, do a "radix" test as our initial guess, based on
621 // the assumption that lines average to approximately the same length.
622 // NOTE: This is currently disabled, as it does not appear to be profitable in
623 // initial measurements.
624 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000625 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000626
627 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000628 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000629
630 // Check for -10 and +10 lines.
631 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
632 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
633
634 // If the computed lower bound is less than the query location, move it in.
635 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
636 SourceLineCacheStart[LowerBound] < QueriedFilePos)
637 SourceLineCache = SourceLineCacheStart+LowerBound;
638
639 // If the computed upper bound is greater than the query location, move it.
640 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
641 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
642 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
643 }
644
645 unsigned *Pos
646 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000647 unsigned LineNo = Pos-SourceLineCacheStart;
648
Chris Lattner30fc9332009-02-04 01:06:56 +0000649 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000650 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000651 LastLineNoFilePos = QueriedFilePos;
652 LastLineNoResult = LineNo;
653 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654}
655
Chris Lattner30fc9332009-02-04 01:06:56 +0000656unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
657 if (Loc.isInvalid()) return 0;
658 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
659 return getLineNumber(LocInfo.first, LocInfo.second);
660}
661unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
662 if (Loc.isInvalid()) return 0;
663 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
664 return getLineNumber(LocInfo.first, LocInfo.second);
665}
666
667
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000668/// getPresumedLoc - This method returns the "presumed" location of a
669/// SourceLocation specifies. A "presumed location" can be modified by #line
670/// or GNU line marker directives. This provides a view on the data that a
671/// user should see in diagnostics, for example.
672///
673/// Note that a presumed location is always given as the instantiation point
674/// of an instantiation location, not at the spelling location.
675PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
676 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000677
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000678 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000679 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000680
Chris Lattner30fc9332009-02-04 01:06:56 +0000681 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000682 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000683
684 // To get the source name, first consult the FileEntry (if one exists)
685 // before the MemBuffer as this will avoid unnecessarily paging in the
686 // MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000687 const char *Filename =
688 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000689 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
690 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
691 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000692
Chris Lattner3cd949c2009-02-04 01:55:42 +0000693 // If we have #line directives in this file, update and overwrite the physical
694 // location info if appropriate.
695 if (FI.hasLineDirectives()) {
696 assert(LineTable && "Can't have linetable entries without a LineTable!");
697 // See if there is a #line directive before this. If so, get it.
698 if (const LineEntry *Entry =
699 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +0000700 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000701 if (Entry->FilenameID != -1)
702 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +0000703
704 // Use the line number specified by the LineEntry. This line number may
705 // be multiple lines down from the line entry. Add the difference in
706 // physical line numbers from the query point and the line marker to the
707 // total.
708 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
709 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
710
Chris Lattner0e0e5da2009-02-04 02:15:40 +0000711 // Note that column numbers are not molested by line markers.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000712 }
713 }
714
715 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000716}
717
718//===----------------------------------------------------------------------===//
719// Other miscellaneous methods.
720//===----------------------------------------------------------------------===//
721
722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723/// PrintStats - Print statistics to stderr.
724///
725void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000726 llvm::cerr << "\n*** Source Manager Stats:\n";
727 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000728 << " mem buffers mapped.\n";
729 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
730 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000731
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 unsigned NumLineNumsComputed = 0;
733 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000734 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
735 NumLineNumsComputed += I->second->SourceLineCache != 0;
736 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000738
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000739 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
740 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000741 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
742 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000743}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000744
745//===----------------------------------------------------------------------===//
746// Serialization.
747//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000748
749void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000750 S.FlushRecord();
751 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000752
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000753 if (Entry) {
754 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
755
756 if (Fname.isAbsolute())
757 S.EmitCStr(Fname.c_str());
758 else {
759 // Create an absolute path.
760 // FIXME: This will potentially contain ".." and "." in the path.
761 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
762 path.appendComponent(Fname.c_str());
763 S.EmitCStr(path.c_str());
764 }
765 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000766 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000767 const char* p = Buffer->getBufferStart();
768 const char* e = Buffer->getBufferEnd();
769
Ted Kremenek099b4742007-12-05 00:14:18 +0000770 S.EmitInt(e-p);
771
Ted Kremeneke21272f2007-12-04 19:39:02 +0000772 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000773 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000774 }
775
Ted Kremenek099b4742007-12-05 00:14:18 +0000776 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000777}
Ted Kremenek099b4742007-12-05 00:14:18 +0000778
779void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
780 SourceManager& SMgr,
781 FileManager* FMgr,
782 std::vector<char>& Buf) {
783 if (FMgr) {
784 llvm::SerializedPtrID PtrID = D.ReadPtrID();
785 D.ReadCStr(Buf,false);
786
787 // Create/fetch the FileEntry.
788 const char* start = &Buf[0];
789 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
790
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000791 // FIXME: Ideally we want a lazy materialization of the ContentCache
792 // anyway, because we don't want to read in source files unless this
793 // is absolutely needed.
794 if (!E)
795 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000796 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000797 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000798 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
799 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000800 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000801
802 // Register the ContextCache object with the deserializer.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000803 /* FIXME:
804 ContentCache *Entry
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000805 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000806 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000807 D.RegisterPtr(&Entry);
808
809 // Create the buffer.
810 unsigned Size = D.ReadInt();
811 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
812
813 // Read the contents of the buffer.
814 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
815 for (unsigned i = 0; i < Size ; ++i)
816 p[i] = D.ReadInt();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000817 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000818}
819
820void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000821 S.EnterBlock();
822 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000823 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000824
Ted Kremenek099b4742007-12-05 00:14:18 +0000825 // Emit: FileInfos. Just emit the file name.
826 S.EnterBlock();
827
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000828 // FIXME: Emit FileInfos.
829 //std::for_each(FileInfos.begin(), FileInfos.end(),
830 // S.MakeEmitter<ContentCache>());
Ted Kremenek099b4742007-12-05 00:14:18 +0000831
832 S.ExitBlock();
833
834 // Emit: MemBufferInfos
835 S.EnterBlock();
836
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000837 /* FIXME: EMIT.
Ted Kremenek099b4742007-12-05 00:14:18 +0000838 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
839 S.MakeEmitter<ContentCache>());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000840 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000841
842 S.ExitBlock();
843
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000844 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000845
846 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000847}
848
Ted Kremenek1f941002007-12-05 00:19:51 +0000849SourceManager*
Chris Lattner23b5dc62009-02-04 00:40:31 +0000850SourceManager::CreateAndRegister(llvm::Deserializer &D, FileManager &FMgr) {
Ted Kremenek1f941002007-12-05 00:19:51 +0000851 SourceManager *M = new SourceManager();
852 D.RegisterPtr(M);
853
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000854 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000855 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000856
Ted Kremenek099b4742007-12-05 00:14:18 +0000857 std::vector<char> Buf;
858
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000859 /*{ // FIXME Read: FileInfos.
Ted Kremenek099b4742007-12-05 00:14:18 +0000860 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
861 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000862 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000863 }*/
Ted Kremenek099b4742007-12-05 00:14:18 +0000864
865 { // Read: MemBufferInfos.
866 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
867 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000868 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000869 }
870
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000871 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000872
873 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000874}