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