diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h --- a/clang/include/clang/Lex/HeaderSearch.h +++ b/clang/include/clang/Lex/HeaderSearch.h @@ -255,13 +255,13 @@ /// Keeps track of each lookup performed by LookupFile. struct LookupFileCacheInfo { - /// Starting index in SearchDirs that the cached search was performed from. - /// If there is a hit and this value doesn't match the current query, the - /// cache has to be ignored. - unsigned StartIdx = 0; + /// Starting search directory iterator that the cached search was performed + /// from. If there is a hit and this value doesn't match the current query, + /// the cache has to be ignored. + ConstSearchDirIterator StartIt = 0; - /// The entry in SearchDirs that satisfied the query. - unsigned HitIdx = 0; + /// The search directory iterator that satisfied the query. + ConstSearchDirIterator HitIt = 0; /// This is non-null if the original filename was mapped to a framework /// include via a headermap. @@ -270,9 +270,9 @@ /// Default constructor -- Initialize all members with zero. LookupFileCacheInfo() = default; - void reset(unsigned StartIdx) { - this->StartIdx = StartIdx; - this->MappedName = nullptr; + void reset(ConstSearchDirIterator NewStartIt) { + StartIt = NewStartIt; + MappedName = nullptr; } }; llvm::StringMap LookupFileCache; @@ -749,9 +749,11 @@ ModuleMap::KnownHeader *SuggestedModule); /// Cache the result of a successful lookup at the given include location - /// using the search path at index `HitIdx`. - void cacheLookupSuccess(LookupFileCacheInfo &CacheLookup, unsigned HitIdx, + /// using the search path at \c HitIt. + void cacheLookupSuccess(LookupFileCacheInfo &CacheLookup, + ConstSearchDirIterator HitIt, SourceLocation IncludeLoc); + /// Note that a lookup at the given include location was successful using the /// search path at index `HitIdx`. void noteLookupUsage(unsigned HitIdx, SourceLocation IncludeLoc); diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -704,9 +704,10 @@ } void HeaderSearch::cacheLookupSuccess(LookupFileCacheInfo &CacheLookup, - unsigned HitIdx, SourceLocation Loc) { - CacheLookup.HitIdx = HitIdx; - noteLookupUsage(HitIdx, Loc); + ConstSearchDirIterator HitIt, + SourceLocation Loc) { + CacheLookup.HitIt = HitIt; + noteLookupUsage(&*HitIt - &*search_dir_begin(), Loc); } void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) { @@ -964,12 +965,13 @@ CurDir = nullptr; // If this is a system #include, ignore the user #include locs. - unsigned i = isAngled ? AngledDirIdx : 0; + ConstSearchDirIterator It = + isAngled ? angled_dir_begin() : search_dir_begin(); // If this is a #include_next request, start searching after the directory the // file was found in. if (FromDir) - i = FromDir.Idx; + It = FromDir; // Cache all of the lookups performed by this method. Many headers are // multiply included, and the "pragma once" optimization prevents them from @@ -977,12 +979,14 @@ // (potentially huge) series of SearchDirs to find it. LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename]; + ConstSearchDirIterator NextIt = [](auto ItCopy) { return ++ItCopy; }(It); + // If the entry has been previously looked up, the first value will be // non-zero. If the value is equal to i (the start point of our search), then // this is a matching hit. - if (!SkipCache && CacheLookup.StartIdx == i+1) { + if (!SkipCache && CacheLookup.StartIt == NextIt) { // Skip querying potentially lots of directories for this lookup. - i = CacheLookup.HitIdx; + It = CacheLookup.HitIt; if (CacheLookup.MappedName) { Filename = CacheLookup.MappedName; if (IsMapped) @@ -992,17 +996,17 @@ // Otherwise, this is the first query, or the previous query didn't match // our search start. We will fill in our found location below, so prime the // start point value. - CacheLookup.reset(/*StartIdx=*/i+1); + CacheLookup.reset(/*NewStartIt=*/NextIt); } SmallString<64> MappedName; // Check each directory in sequence to see if it contains this file. - for (; i != SearchDirs.size(); ++i) { + for (; It != search_dir_end(); ++It) { bool InUserSpecifiedSystemFramework = false; bool IsInHeaderMap = false; bool IsFrameworkFoundInDir = false; - Optional File = SearchDirs[i].LookupFile( + Optional File = It->LookupFile( Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule, SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir, IsInHeaderMap, MappedName); @@ -1024,7 +1028,7 @@ if (!File) continue; - CurDir = ConstSearchDirIterator(*this, i); + CurDir = It; // This file is a system header or C++ unfriendly if the dir is. HeaderFileInfo &HFI = getFileInfo(&File->getFileEntry()); @@ -1077,7 +1081,7 @@ &File->getFileEntry(), isAngled, FoundByHeaderMap); // Remember this location for the next lookup we do. - cacheLookupSuccess(CacheLookup, i, IncludeLoc); + cacheLookupSuccess(CacheLookup, It, IncludeLoc); return File; } @@ -1108,7 +1112,7 @@ } cacheLookupSuccess(LookupFileCache[Filename], - LookupFileCache[ScratchFilename].HitIdx, IncludeLoc); + LookupFileCache[ScratchFilename].HitIt, IncludeLoc); // FIXME: SuggestedModule. return File; } @@ -1122,7 +1126,7 @@ } // Otherwise, didn't find it. Remember we didn't find this. - CacheLookup.HitIdx = SearchDirs.size(); + CacheLookup.HitIt = search_dir_end(); return None; }