@@ -559,6 +559,7 @@ class swift::ModuleSourceFileLocationMap {
559
559
unsigned numFiles = 0 ;
560
560
unsigned numAuxiliaryFiles = 0 ;
561
561
std::vector<SourceFile *> allSourceFiles;
562
+ SourceFile *lastSourceFile = nullptr ;
562
563
};
563
564
564
565
void ModuleDecl::updateSourceFileLocationMap () {
@@ -602,17 +603,28 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
602
603
if (loc.isInvalid ())
603
604
return nullptr ;
604
605
605
- SourceLoc adjustedLoc = loc;
606
606
607
607
// Check whether this location is in a "replaced" range, in which case
608
608
// we want to use the original source file.
609
609
auto &sourceMgr = getASTContext ().SourceMgr ;
610
+ SourceLoc adjustedLoc = loc;
610
611
for (const auto &pair : sourceMgr.getReplacedRanges ()) {
611
612
if (sourceMgr.rangeContainsTokenLoc (pair.second , loc)) {
612
613
adjustedLoc = pair.first .Start ;
613
614
break ;
614
615
}
615
616
}
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
+
616
628
updateSourceFileLocationMap ();
617
629
618
630
auto found = std::lower_bound (sourceFileLocationMap->allSourceFiles .begin (),
@@ -627,7 +639,8 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
627
639
if (!foundRange.contains (adjustedLoc))
628
640
return nullptr ;
629
641
630
-
642
+ // Update the last source file.
643
+ sourceFileLocationMap->lastSourceFile = foundSourceFile;
631
644
return foundSourceFile;
632
645
}
633
646
0 commit comments