xref: /llvm-project/clang/lib/Basic/SourceManager.cpp (revision 43a85b1e4c8d72b6f49cb6c0f2eefbb0b852d3f1)
1 //===--- SourceManager.cpp - Track and cache source files -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
19 #include "llvm/Bitcode/Serialize.h"
20 #include "llvm/Bitcode/Deserialize.h"
21 #include "llvm/Support/Streams.h"
22 #include <algorithm>
23 using namespace clang;
24 using namespace SrcMgr;
25 using llvm::MemoryBuffer;
26 
27 //===--------------------------------------------------------------------===//
28 // SourceManager Helper Classes
29 //===--------------------------------------------------------------------===//
30 
31 ContentCache::~ContentCache() {
32   delete Buffer;
33   delete [] SourceLineCache;
34 }
35 
36 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
37 ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
38 ///  instantiated.
39 unsigned ContentCache::getSizeBytesMapped() const {
40   return Buffer ? Buffer->getBufferSize() : 0;
41 }
42 
43 /// getSize - Returns the size of the content encapsulated by this ContentCache.
44 ///  This can be the size of the source file or the size of an arbitrary
45 ///  scratch buffer.  If the ContentCache encapsulates a source file, that
46 ///  file is not lazily brought in from disk to satisfy this query.
47 unsigned ContentCache::getSize() const {
48   return Entry ? Entry->getSize() : Buffer->getBufferSize();
49 }
50 
51 const llvm::MemoryBuffer *ContentCache::getBuffer() const {
52   // Lazily create the Buffer for ContentCaches that wrap files.
53   if (!Buffer && Entry) {
54     // FIXME: Should we support a way to not have to do this check over
55     //   and over if we cannot open the file?
56     Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
57   }
58   return Buffer;
59 }
60 
61 //===--------------------------------------------------------------------===//
62 // Private 'Create' methods.
63 //===--------------------------------------------------------------------===//
64 
65 /// getOrCreateContentCache - Create or return a cached ContentCache for the
66 /// specified file.
67 const ContentCache *
68 SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
69   assert(FileEnt && "Didn't specify a file entry to use?");
70 
71   // Do we already have information about this file?
72   std::set<ContentCache>::iterator I =
73     FileInfos.lower_bound(ContentCache(FileEnt));
74 
75   if (I != FileInfos.end() && I->Entry == FileEnt)
76     return &*I;
77 
78   // Nope, create a new Cache entry.
79   ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
80   Entry.SourceLineCache = 0;
81   Entry.NumLines = 0;
82   return &Entry;
83 }
84 
85 
86 /// createMemBufferContentCache - Create a new ContentCache for the specified
87 ///  memory buffer.  This does no caching.
88 const ContentCache*
89 SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
90   // Add a new ContentCache to the MemBufferInfos list and return it.  We
91   // must default construct the object first that the instance actually
92   // stored within MemBufferInfos actually owns the Buffer, and not any
93   // temporary we would use in the call to "push_back".
94   MemBufferInfos.push_back(ContentCache());
95   ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
96   Entry.setBuffer(Buffer);
97   return &Entry;
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // Methods to create new FileID's and instantiations.
102 //===----------------------------------------------------------------------===//
103 
104 /// createFileID - Create a new fileID for the specified ContentCache and
105 /// include position.  This works regardless of whether the ContentCache
106 /// corresponds to a file or some other input source.
107 FileID SourceManager::createFileID(const ContentCache *File,
108                                    SourceLocation IncludePos,
109                                    SrcMgr::CharacteristicKind FileCharacter) {
110   SLocEntryTable.push_back(SLocEntry::get(NextOffset,
111                                           FileInfo::get(IncludePos, File,
112                                                         FileCharacter)));
113   unsigned FileSize = File->getSize();
114   assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
115   NextOffset += FileSize+1;
116 
117   // Set LastFileIDLookup to the newly created file.  The next getFileID call is
118   // almost guaranteed to be from that file.
119   return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
120 }
121 
122 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
123 /// that a token from SpellingLoc should actually be referenced from
124 /// InstantiationLoc.
125 SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
126                                                      SourceLocation InstantLoc,
127                                                      unsigned TokLength) {
128   // The specified source location may be a mapped location, due to a macro
129   // instantiation or #line directive.  Strip off this information to find out
130   // where the characters are actually located.
131   SpellingLoc = getSpellingLoc(SpellingLoc);
132 
133   // Resolve InstantLoc down to a real instantiation location.
134   InstantLoc = getInstantiationLoc(InstantLoc);
135 
136   SLocEntryTable.push_back(SLocEntry::get(NextOffset,
137                                           InstantiationInfo::get(InstantLoc,
138                                                                  SpellingLoc)));
139   assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
140   NextOffset += TokLength+1;
141   return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
142 }
143 
144 /// getBufferData - Return a pointer to the start and end of the source buffer
145 /// data for the specified FileID.
146 std::pair<const char*, const char*>
147 SourceManager::getBufferData(FileID FID) const {
148   const llvm::MemoryBuffer *Buf = getBuffer(FID);
149   return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
150 }
151 
152 
153 //===--------------------------------------------------------------------===//
154 // SourceLocation manipulation methods.
155 //===--------------------------------------------------------------------===//
156 
157 /// getFileIDSlow - Return the FileID for a SourceLocation.  This is a very hot
158 /// method that is used for all SourceManager queries that start with a
159 /// SourceLocation object.  It is responsible for finding the entry in
160 /// SLocEntryTable which contains the specified location.
161 ///
162 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
163   assert(SLocOffset && "Invalid FileID");
164 
165   // After the first and second level caches, I see two common sorts of
166   // behavior: 1) a lot of searched FileID's are "near" the cached file location
167   // or are "near" the cached instantiation location.  2) others are just
168   // completely random and may be a very long way away.
169   //
170   // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
171   // then we fall back to a less cache efficient, but more scalable, binary
172   // search to find the location.
173 
174   // See if this is near the file point - worst case we start scanning from the
175   // most newly created FileID.
176   std::vector<SrcMgr::SLocEntry>::const_iterator I;
177 
178   if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
179     // Neither loc prunes our search.
180     I = SLocEntryTable.end();
181   } else {
182     // Perhaps it is near the file point.
183     I = SLocEntryTable.begin()+LastFileIDLookup.ID;
184   }
185 
186   // Find the FileID that contains this.  "I" is an iterator that points to a
187   // FileID whose offset is known to be larger than SLocOffset.
188   unsigned NumProbes = 0;
189   while (1) {
190     --I;
191     if (I->getOffset() <= SLocOffset) {
192 #if 0
193       printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
194              I-SLocEntryTable.begin(),
195              I->isInstantiation() ? "inst" : "file",
196              LastFileIDLookup.ID,  int(SLocEntryTable.end()-I));
197 #endif
198       FileID Res = FileID::get(I-SLocEntryTable.begin());
199 
200       // If this isn't an instantiation, remember it.  We have good locality
201       // across FileID lookups.
202       if (!I->isInstantiation())
203         LastFileIDLookup = Res;
204       NumLinearScans += NumProbes+1;
205       return Res;
206     }
207     if (++NumProbes == 8)
208       break;
209   }
210 
211   // Convert "I" back into an index.  We know that it is an entry whose index is
212   // larger than the offset we are looking for.
213   unsigned GreaterIndex = I-SLocEntryTable.begin();
214   // LessIndex - This is the lower bound of the range that we're searching.
215   // We know that the offset corresponding to the FileID is is less than
216   // SLocOffset.
217   unsigned LessIndex = 0;
218   NumProbes = 0;
219   while (1) {
220     unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
221     unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
222 
223     ++NumProbes;
224 
225     // If the offset of the midpoint is too large, chop the high side of the
226     // range to the midpoint.
227     if (MidOffset > SLocOffset) {
228       GreaterIndex = MiddleIndex;
229       continue;
230     }
231 
232     // If the middle index contains the value, succeed and return.
233     if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
234 #if 0
235       printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
236              I-SLocEntryTable.begin(),
237              I->isInstantiation() ? "inst" : "file",
238              LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
239 #endif
240       FileID Res = FileID::get(MiddleIndex);
241 
242       // If this isn't an instantiation, remember it.  We have good locality
243       // across FileID lookups.
244       if (!I->isInstantiation())
245         LastFileIDLookup = Res;
246       NumBinaryProbes += NumProbes;
247       return Res;
248     }
249 
250     // Otherwise, move the low-side up to the middle index.
251     LessIndex = MiddleIndex;
252   }
253 }
254 
255 std::pair<FileID, unsigned>
256 SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
257                                                      unsigned Offset) const {
258   // If this is an instantiation record, walk through all the instantiation
259   // points.
260   FileID FID;
261   SourceLocation Loc;
262   do {
263     Loc = E->getInstantiation().getInstantiationLoc();
264 
265     FID = getFileID(Loc);
266     E = &getSLocEntry(FID);
267     Offset += Loc.getOffset()-E->getOffset();
268   } while (Loc.isFileID());
269 
270   return std::make_pair(FID, Offset);
271 }
272 
273 std::pair<FileID, unsigned>
274 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
275                                                 unsigned Offset) const {
276   // If this is an instantiation record, get and return the spelling.
277   SourceLocation Loc = E->getInstantiation().getSpellingLoc();
278   FileID FID = getFileID(Loc);
279   E = &getSLocEntry(FID);
280   Offset += Loc.getOffset()-E->getOffset();
281   assert(Loc.isFileID() && "Should only have one spelling link");
282   return std::make_pair(FID, Offset);
283 }
284 
285 
286 //===----------------------------------------------------------------------===//
287 // Queries about the code at a SourceLocation.
288 //===----------------------------------------------------------------------===//
289 
290 /// getCharacterData - Return a pointer to the start of the specified location
291 /// in the appropriate MemoryBuffer.
292 const char *SourceManager::getCharacterData(SourceLocation SL) const {
293   // Note that this is a hot function in the getSpelling() path, which is
294   // heavily used by -E mode.
295   std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
296 
297   // Note that calling 'getBuffer()' may lazily page in a source file.
298   return getSLocEntry(LocInfo.first).getFile().getContentCache()
299               ->getBuffer()->getBufferStart() + LocInfo.second;
300 }
301 
302 
303 /// getColumnNumber - Return the column # for the specified file position.
304 /// this is significantly cheaper to compute than the line number.  This returns
305 /// zero if the column number isn't known.
306 unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
307   if (Loc.isInvalid()) return 0;
308   assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
309 
310   std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
311   unsigned FilePos = LocInfo.second;
312 
313   const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
314 
315   unsigned LineStart = FilePos;
316   while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
317     --LineStart;
318   return FilePos-LineStart+1;
319 }
320 
321 static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
322 static void ComputeLineNumbers(ContentCache* FI) {
323   // Note that calling 'getBuffer()' may lazily page in the file.
324   const MemoryBuffer *Buffer = FI->getBuffer();
325 
326   // Find the file offsets of all of the *physical* source lines.  This does
327   // not look at trigraphs, escaped newlines, or anything else tricky.
328   std::vector<unsigned> LineOffsets;
329 
330   // Line #1 starts at char 0.
331   LineOffsets.push_back(0);
332 
333   const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
334   const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
335   unsigned Offs = 0;
336   while (1) {
337     // Skip over the contents of the line.
338     // TODO: Vectorize this?  This is very performance sensitive for programs
339     // with lots of diagnostics and in -E mode.
340     const unsigned char *NextBuf = (const unsigned char *)Buf;
341     while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
342       ++NextBuf;
343     Offs += NextBuf-Buf;
344     Buf = NextBuf;
345 
346     if (Buf[0] == '\n' || Buf[0] == '\r') {
347       // If this is \n\r or \r\n, skip both characters.
348       if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
349         ++Offs, ++Buf;
350       ++Offs, ++Buf;
351       LineOffsets.push_back(Offs);
352     } else {
353       // Otherwise, this is a null.  If end of file, exit.
354       if (Buf == End) break;
355       // Otherwise, skip the null.
356       ++Offs, ++Buf;
357     }
358   }
359 
360   // Copy the offsets into the FileInfo structure.
361   FI->NumLines = LineOffsets.size();
362   FI->SourceLineCache = new unsigned[LineOffsets.size()];
363   std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
364 }
365 
366 /// getLineNumber - Given a SourceLocation, return the spelling line number
367 /// for the position indicated.  This requires building and caching a table of
368 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
369 /// about to emit a diagnostic.
370 unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
371   if (Loc.isInvalid()) return 0;
372   assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
373 
374   std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
375 
376   ContentCache *Content;
377   if (LastLineNoFileIDQuery == LocInfo.first)
378     Content = LastLineNoContentCache;
379   else
380     Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
381                                         .getFile().getContentCache());
382 
383   // If this is the first use of line information for this buffer, compute the
384   /// SourceLineCache for it on demand.
385   if (Content->SourceLineCache == 0)
386     ComputeLineNumbers(Content);
387 
388   // Okay, we know we have a line number table.  Do a binary search to find the
389   // line number that this character position lands on.
390   unsigned *SourceLineCache = Content->SourceLineCache;
391   unsigned *SourceLineCacheStart = SourceLineCache;
392   unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
393 
394   unsigned QueriedFilePos = LocInfo.second+1;
395 
396   // If the previous query was to the same file, we know both the file pos from
397   // that query and the line number returned.  This allows us to narrow the
398   // search space from the entire file to something near the match.
399   if (LastLineNoFileIDQuery == LocInfo.first) {
400     if (QueriedFilePos >= LastLineNoFilePos) {
401       SourceLineCache = SourceLineCache+LastLineNoResult-1;
402 
403       // The query is likely to be nearby the previous one.  Here we check to
404       // see if it is within 5, 10 or 20 lines.  It can be far away in cases
405       // where big comment blocks and vertical whitespace eat up lines but
406       // contribute no tokens.
407       if (SourceLineCache+5 < SourceLineCacheEnd) {
408         if (SourceLineCache[5] > QueriedFilePos)
409           SourceLineCacheEnd = SourceLineCache+5;
410         else if (SourceLineCache+10 < SourceLineCacheEnd) {
411           if (SourceLineCache[10] > QueriedFilePos)
412             SourceLineCacheEnd = SourceLineCache+10;
413           else if (SourceLineCache+20 < SourceLineCacheEnd) {
414             if (SourceLineCache[20] > QueriedFilePos)
415               SourceLineCacheEnd = SourceLineCache+20;
416           }
417         }
418       }
419     } else {
420       SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
421     }
422   }
423 
424   // If the spread is large, do a "radix" test as our initial guess, based on
425   // the assumption that lines average to approximately the same length.
426   // NOTE: This is currently disabled, as it does not appear to be profitable in
427   // initial measurements.
428   if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
429     unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
430 
431     // Take a stab at guessing where it is.
432     unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
433 
434     // Check for -10 and +10 lines.
435     unsigned LowerBound = std::max(int(ApproxPos-10), 0);
436     unsigned UpperBound = std::min(ApproxPos+10, FileLen);
437 
438     // If the computed lower bound is less than the query location, move it in.
439     if (SourceLineCache < SourceLineCacheStart+LowerBound &&
440         SourceLineCacheStart[LowerBound] < QueriedFilePos)
441       SourceLineCache = SourceLineCacheStart+LowerBound;
442 
443     // If the computed upper bound is greater than the query location, move it.
444     if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
445         SourceLineCacheStart[UpperBound] >= QueriedFilePos)
446       SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
447   }
448 
449   unsigned *Pos
450     = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
451   unsigned LineNo = Pos-SourceLineCacheStart;
452 
453   LastLineNoFileIDQuery = LocInfo.first;
454   LastLineNoContentCache = Content;
455   LastLineNoFilePos = QueriedFilePos;
456   LastLineNoResult = LineNo;
457   return LineNo;
458 }
459 
460 /// getSourceName - This method returns the name of the file or buffer that
461 /// the SourceLocation specifies.  This can be modified with #line directives,
462 /// etc.
463 const char *SourceManager::getSourceName(SourceLocation Loc) const {
464   if (Loc.isInvalid()) return "";
465 
466   const SrcMgr::ContentCache *C =
467   getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache();
468 
469   // To get the source name, first consult the FileEntry (if one exists) before
470   // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
471   return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
472 }
473 
474 //===----------------------------------------------------------------------===//
475 // Other miscellaneous methods.
476 //===----------------------------------------------------------------------===//
477 
478 
479 /// PrintStats - Print statistics to stderr.
480 ///
481 void SourceManager::PrintStats() const {
482   llvm::cerr << "\n*** Source Manager Stats:\n";
483   llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
484              << " mem buffers mapped, " << SLocEntryTable.size()
485              << " SLocEntry's allocated.\n";
486 
487   unsigned NumLineNumsComputed = 0;
488   unsigned NumFileBytesMapped = 0;
489   for (std::set<ContentCache>::const_iterator I =
490        FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
491     NumLineNumsComputed += I->SourceLineCache != 0;
492     NumFileBytesMapped  += I->getSizeBytesMapped();
493   }
494 
495   llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
496              << NumLineNumsComputed << " files with line #'s computed.\n";
497   llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
498              << NumBinaryProbes << " binary.\n";
499 }
500 
501 //===----------------------------------------------------------------------===//
502 // Serialization.
503 //===----------------------------------------------------------------------===//
504 
505 void ContentCache::Emit(llvm::Serializer& S) const {
506   S.FlushRecord();
507   S.EmitPtr(this);
508 
509   if (Entry) {
510     llvm::sys::Path Fname(Buffer->getBufferIdentifier());
511 
512     if (Fname.isAbsolute())
513       S.EmitCStr(Fname.c_str());
514     else {
515       // Create an absolute path.
516       // FIXME: This will potentially contain ".." and "." in the path.
517       llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
518       path.appendComponent(Fname.c_str());
519       S.EmitCStr(path.c_str());
520     }
521   }
522   else {
523     const char* p = Buffer->getBufferStart();
524     const char* e = Buffer->getBufferEnd();
525 
526     S.EmitInt(e-p);
527 
528     for ( ; p != e; ++p)
529       S.EmitInt(*p);
530   }
531 
532   S.FlushRecord();
533 }
534 
535 void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
536                                        SourceManager& SMgr,
537                                        FileManager* FMgr,
538                                        std::vector<char>& Buf) {
539   if (FMgr) {
540     llvm::SerializedPtrID PtrID = D.ReadPtrID();
541     D.ReadCStr(Buf,false);
542 
543     // Create/fetch the FileEntry.
544     const char* start = &Buf[0];
545     const FileEntry* E = FMgr->getFile(start,start+Buf.size());
546 
547     // FIXME: Ideally we want a lazy materialization of the ContentCache
548     //  anyway, because we don't want to read in source files unless this
549     //  is absolutely needed.
550     if (!E)
551       D.RegisterPtr(PtrID,NULL);
552     else
553       // Get the ContextCache object and register it with the deserializer.
554       D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
555     return;
556   }
557 
558   // Register the ContextCache object with the deserializer.
559   SMgr.MemBufferInfos.push_back(ContentCache());
560   ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
561   D.RegisterPtr(&Entry);
562 
563   // Create the buffer.
564   unsigned Size = D.ReadInt();
565   Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
566 
567   // Read the contents of the buffer.
568   char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
569   for (unsigned i = 0; i < Size ; ++i)
570     p[i] = D.ReadInt();
571 }
572 
573 void SourceManager::Emit(llvm::Serializer& S) const {
574   S.EnterBlock();
575   S.EmitPtr(this);
576   S.EmitInt(MainFileID.getOpaqueValue());
577 
578   // Emit: FileInfos.  Just emit the file name.
579   S.EnterBlock();
580 
581   std::for_each(FileInfos.begin(),FileInfos.end(),
582                 S.MakeEmitter<ContentCache>());
583 
584   S.ExitBlock();
585 
586   // Emit: MemBufferInfos
587   S.EnterBlock();
588 
589   std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
590                 S.MakeEmitter<ContentCache>());
591 
592   S.ExitBlock();
593 
594   // FIXME: Emit SLocEntryTable.
595 
596   S.ExitBlock();
597 }
598 
599 SourceManager*
600 SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
601   SourceManager *M = new SourceManager();
602   D.RegisterPtr(M);
603 
604   // Read: the FileID of the main source file of the translation unit.
605   M->MainFileID = FileID::get(D.ReadInt());
606 
607   std::vector<char> Buf;
608 
609   { // Read: FileInfos.
610     llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
611     while (!D.FinishedBlock(BLoc))
612     ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
613   }
614 
615   { // Read: MemBufferInfos.
616     llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
617     while (!D.FinishedBlock(BLoc))
618     ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
619   }
620 
621   // FIXME: Read SLocEntryTable.
622 
623   return M;
624 }
625