@@ -559,6 +559,7 @@ class swift::ModuleSourceFileLocationMap {
559559 unsigned numFiles = 0 ;
560560 unsigned numAuxiliaryFiles = 0 ;
561561 std::vector<SourceFile *> allSourceFiles;
562+ SourceFile *lastSourceFile = nullptr ;
562563};
563564
564565void ModuleDecl::updateSourceFileLocationMap () {
@@ -602,17 +603,28 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
602603 if (loc.isInvalid ())
603604 return nullptr ;
604605
605- SourceLoc adjustedLoc = loc;
606606
607607 // Check whether this location is in a "replaced" range, in which case
608608 // we want to use the original source file.
609609 auto &sourceMgr = getASTContext ().SourceMgr ;
610+ SourceLoc adjustedLoc = loc;
610611 for (const auto &pair : sourceMgr.getReplacedRanges ()) {
611612 if (sourceMgr.rangeContainsTokenLoc (pair.second , loc)) {
612613 adjustedLoc = pair.first .Start ;
613614 break ;
614615 }
615616 }
617+
618+ // Before we do any extra work, check the last source file we found a result
619+ // in to see if it contains this.
620+ if (sourceFileLocationMap) {
621+ if (auto lastSourceFile = sourceFileLocationMap->lastSourceFile ) {
622+ auto range = sourceMgr.getRangeForBuffer (*lastSourceFile->getBufferID ());
623+ if (range.contains (adjustedLoc))
624+ return lastSourceFile;
625+ }
626+ }
627+
616628 updateSourceFileLocationMap ();
617629
618630 auto found = std::lower_bound (sourceFileLocationMap->allSourceFiles .begin (),
@@ -627,7 +639,8 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
627639 if (!foundRange.contains (adjustedLoc))
628640 return nullptr ;
629641
630-
642+ // Update the last source file.
643+ sourceFileLocationMap->lastSourceFile = foundSourceFile;
631644 return foundSourceFile;
632645}
633646
0 commit comments