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