@@ -334,6 +334,7 @@ void SourceManager::clearIDTables() {
334334 LastLineNoFileIDQuery = FileID ();
335335 LastLineNoContentCache = nullptr ;
336336 LastFileIDLookup = FileID ();
337+ LastLookupStartOffset = LastLookupEndOffset = 0 ;
337338
338339 IncludedLocMap.clear ();
339340 if (LineTable)
@@ -639,9 +640,11 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
639640 LocalSLocEntryTable.push_back (
640641 SLocEntry::get (NextLocalOffset,
641642 FileInfo::get (IncludePos, File, FileCharacter, Filename)));
643+ LastLookupStartOffset = NextLocalOffset;
642644 // We do a +1 here because we want a SourceLocation that means "the end of the
643645 // file", e.g. for the "no newline at the end of the file" diagnostic.
644646 NextLocalOffset += FileSize + 1 ;
647+ LastLookupEndOffset = NextLocalOffset;
645648 updateSlocUsageStats ();
646649
647650 // Set LastFileIDLookup to the newly created file. The next getFileID call is
@@ -832,13 +835,11 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
832835 unsigned LessIndex = 0 ;
833836 // upper bound of the search range.
834837 unsigned GreaterIndex = LocalSLocEntryTable.size ();
835- if (LastFileIDLookup.ID >= 0 ) {
836- // Use the LastFileIDLookup to prune the search space.
837- if (LocalSLocEntryTable[LastFileIDLookup.ID ].getOffset () < SLocOffset)
838- LessIndex = LastFileIDLookup.ID ;
839- else
840- GreaterIndex = LastFileIDLookup.ID ;
841- }
838+ // Use the LastFileIDLookup to prune the search space.
839+ if (LastLookupStartOffset < SLocOffset)
840+ LessIndex = LastFileIDLookup.ID ;
841+ else
842+ GreaterIndex = LastFileIDLookup.ID ;
842843
843844 // Find the FileID that contains this.
844845 unsigned NumProbes = 0 ;
@@ -849,7 +850,12 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
849850 FileID Res = FileID::get (int (GreaterIndex));
850851 // Remember it. We have good locality across FileID lookups.
851852 LastFileIDLookup = Res;
852- NumLinearScans += NumProbes+1 ;
853+ LastLookupStartOffset = LocalSLocEntryTable[GreaterIndex].getOffset ();
854+ LastLookupEndOffset =
855+ GreaterIndex + 1 >= LocalSLocEntryTable.size ()
856+ ? NextLocalOffset
857+ : LocalSLocEntryTable[GreaterIndex + 1 ].getOffset ();
858+ NumLinearScans += NumProbes + 1 ;
853859 return Res;
854860 }
855861 if (++NumProbes == 8 )
@@ -873,6 +879,8 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
873879 // At this point, LessIndex is the index of the *first element greater than*
874880 // SLocOffset. The element we are actually looking for is the one immediately
875881 // before it.
882+ LastLookupStartOffset = LocalSLocEntryTable[LessIndex - 1 ].getOffset ();
883+ LastLookupEndOffset = LocalSLocEntryTable[LessIndex].getOffset ();
876884 return LastFileIDLookup = FileID::get (LessIndex - 1 );
877885}
878886
0 commit comments