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