@@ -811,6 +811,8 @@ FileID SourceManager::getFileIDSlow(SourceLocation::UIntTy SLocOffset) const {
811811// / loaded one.
812812FileID SourceManager::getFileIDLocal (SourceLocation::UIntTy SLocOffset) const {
813813 assert (SLocOffset < NextLocalOffset && " Bad function choice" );
814+ assert (SLocOffset >= LocalSLocEntryTable[0 ].getOffset () &&
815+ " Invalid SLocOffset" );
814816
815817 // After the first and second level caches, I see two common sorts of
816818 // behavior: 1) a lot of searched FileID's are "near" the cached file
@@ -854,35 +856,24 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
854856 break ;
855857 }
856858
857- NumProbes = 0 ;
858- while (true ) {
859- unsigned MiddleIndex = (GreaterIndex-LessIndex)/2 +LessIndex;
860- SourceLocation::UIntTy MidOffset =
861- getLocalSLocEntry (MiddleIndex).getOffset ();
862-
863- ++NumProbes;
864-
865- // If the offset of the midpoint is too large, chop the high side of the
866- // range to the midpoint.
867- if (MidOffset > SLocOffset) {
868- GreaterIndex = MiddleIndex;
869- continue ;
870- }
859+ while (LessIndex < GreaterIndex) {
860+ ++NumBinaryProbes;
871861
872- // If the middle index contains the value, succeed and return.
873- if (MiddleIndex + 1 == LocalSLocEntryTable.size () ||
874- SLocOffset < getLocalSLocEntry (MiddleIndex + 1 ).getOffset ()) {
875- FileID Res = FileID::get (MiddleIndex);
862+ unsigned MiddleIndex = LessIndex + (GreaterIndex - LessIndex) / 2 ;
876863
877- // Remember it. We have good locality across FileID lookups.
878- LastFileIDLookup = Res;
879- NumBinaryProbes += NumProbes;
880- return Res;
881- }
864+ SourceLocation::UIntTy MidOffset =
865+ LocalSLocEntryTable[MiddleIndex].getOffset ();
882866
883- // Otherwise, move the low-side up to the middle index.
884- LessIndex = MiddleIndex;
867+ if (MidOffset <= SLocOffset)
868+ LessIndex = MiddleIndex + 1 ;
869+ else
870+ GreaterIndex = MiddleIndex;
885871 }
872+
873+ // At this point, LessIndex is the index of the *first element greater than*
874+ // SLocOffset. The element we are actually looking for is the one immediately
875+ // before it.
876+ return LastFileIDLookup = FileID::get (LessIndex - 1 );
886877}
887878
888879// / Return the FileID for a SourceLocation with a high offset.
0 commit comments