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