Skip to content

Commit 17c9fcd

Browse files
committed
[clang][lex] Use ConstSearchDirIterator in lookup cache
This patch starts using the new iterator type in `LookupFileCacheInfo`. Depends on D117566. Reviewed By: ahoppen Differential Revision: https://reviews.llvm.org/D119721
1 parent 7631c36 commit 17c9fcd

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ class HeaderSearch {
255255

256256
/// Keeps track of each lookup performed by LookupFile.
257257
struct LookupFileCacheInfo {
258-
/// Starting index in SearchDirs that the cached search was performed from.
259-
/// If there is a hit and this value doesn't match the current query, the
260-
/// cache has to be ignored.
261-
unsigned StartIdx = 0;
258+
/// Starting search directory iterator that the cached search was performed
259+
/// from. If there is a hit and this value doesn't match the current query,
260+
/// the cache has to be ignored.
261+
ConstSearchDirIterator StartIt = nullptr;
262262

263-
/// The entry in SearchDirs that satisfied the query.
264-
unsigned HitIdx = 0;
263+
/// The search directory iterator that satisfied the query.
264+
ConstSearchDirIterator HitIt = nullptr;
265265

266266
/// This is non-null if the original filename was mapped to a framework
267267
/// include via a headermap.
@@ -270,9 +270,9 @@ class HeaderSearch {
270270
/// Default constructor -- Initialize all members with zero.
271271
LookupFileCacheInfo() = default;
272272

273-
void reset(unsigned StartIdx) {
274-
this->StartIdx = StartIdx;
275-
this->MappedName = nullptr;
273+
void reset(ConstSearchDirIterator NewStartIt) {
274+
StartIt = NewStartIt;
275+
MappedName = nullptr;
276276
}
277277
};
278278
llvm::StringMap<LookupFileCacheInfo, llvm::BumpPtrAllocator> LookupFileCache;
@@ -749,9 +749,11 @@ class HeaderSearch {
749749
ModuleMap::KnownHeader *SuggestedModule);
750750

751751
/// Cache the result of a successful lookup at the given include location
752-
/// using the search path at index `HitIdx`.
753-
void cacheLookupSuccess(LookupFileCacheInfo &CacheLookup, unsigned HitIdx,
752+
/// using the search path at \c HitIt.
753+
void cacheLookupSuccess(LookupFileCacheInfo &CacheLookup,
754+
ConstSearchDirIterator HitIt,
754755
SourceLocation IncludeLoc);
756+
755757
/// Note that a lookup at the given include location was successful using the
756758
/// search path at index `HitIdx`.
757759
void noteLookupUsage(unsigned HitIdx, SourceLocation IncludeLoc);

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,10 @@ Optional<FileEntryRef> DirectoryLookup::DoFrameworkLookup(
704704
}
705705

706706
void HeaderSearch::cacheLookupSuccess(LookupFileCacheInfo &CacheLookup,
707-
unsigned HitIdx, SourceLocation Loc) {
708-
CacheLookup.HitIdx = HitIdx;
709-
noteLookupUsage(HitIdx, Loc);
707+
ConstSearchDirIterator HitIt,
708+
SourceLocation Loc) {
709+
CacheLookup.HitIt = HitIt;
710+
noteLookupUsage(HitIt.Idx, Loc);
710711
}
711712

712713
void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) {
@@ -964,25 +965,28 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
964965
CurDir = nullptr;
965966

966967
// If this is a system #include, ignore the user #include locs.
967-
unsigned i = isAngled ? AngledDirIdx : 0;
968+
ConstSearchDirIterator It =
969+
isAngled ? angled_dir_begin() : search_dir_begin();
968970

969971
// If this is a #include_next request, start searching after the directory the
970972
// file was found in.
971973
if (FromDir)
972-
i = FromDir.Idx;
974+
It = FromDir;
973975

974976
// Cache all of the lookups performed by this method. Many headers are
975977
// multiply included, and the "pragma once" optimization prevents them from
976978
// being relex/pp'd, but they would still have to search through a
977979
// (potentially huge) series of SearchDirs to find it.
978980
LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
979981

982+
ConstSearchDirIterator NextIt = std::next(It);
983+
980984
// If the entry has been previously looked up, the first value will be
981985
// non-zero. If the value is equal to i (the start point of our search), then
982986
// this is a matching hit.
983-
if (!SkipCache && CacheLookup.StartIdx == i+1) {
987+
if (!SkipCache && CacheLookup.StartIt == NextIt) {
984988
// Skip querying potentially lots of directories for this lookup.
985-
i = CacheLookup.HitIdx;
989+
It = CacheLookup.HitIt;
986990
if (CacheLookup.MappedName) {
987991
Filename = CacheLookup.MappedName;
988992
if (IsMapped)
@@ -992,17 +996,17 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
992996
// Otherwise, this is the first query, or the previous query didn't match
993997
// our search start. We will fill in our found location below, so prime the
994998
// start point value.
995-
CacheLookup.reset(/*StartIdx=*/i+1);
999+
CacheLookup.reset(/*NewStartIt=*/NextIt);
9961000
}
9971001

9981002
SmallString<64> MappedName;
9991003

10001004
// Check each directory in sequence to see if it contains this file.
1001-
for (; i != SearchDirs.size(); ++i) {
1005+
for (; It != search_dir_end(); ++It) {
10021006
bool InUserSpecifiedSystemFramework = false;
10031007
bool IsInHeaderMap = false;
10041008
bool IsFrameworkFoundInDir = false;
1005-
Optional<FileEntryRef> File = SearchDirs[i].LookupFile(
1009+
Optional<FileEntryRef> File = It->LookupFile(
10061010
Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
10071011
SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
10081012
IsInHeaderMap, MappedName);
@@ -1024,7 +1028,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
10241028
if (!File)
10251029
continue;
10261030

1027-
CurDir = ConstSearchDirIterator(*this, i);
1031+
CurDir = It;
10281032

10291033
// This file is a system header or C++ unfriendly if the dir is.
10301034
HeaderFileInfo &HFI = getFileInfo(&File->getFileEntry());
@@ -1077,7 +1081,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
10771081
&File->getFileEntry(), isAngled, FoundByHeaderMap);
10781082

10791083
// Remember this location for the next lookup we do.
1080-
cacheLookupSuccess(CacheLookup, i, IncludeLoc);
1084+
cacheLookupSuccess(CacheLookup, It, IncludeLoc);
10811085
return File;
10821086
}
10831087

@@ -1108,7 +1112,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
11081112
}
11091113

11101114
cacheLookupSuccess(LookupFileCache[Filename],
1111-
LookupFileCache[ScratchFilename].HitIdx, IncludeLoc);
1115+
LookupFileCache[ScratchFilename].HitIt, IncludeLoc);
11121116
// FIXME: SuggestedModule.
11131117
return File;
11141118
}
@@ -1122,7 +1126,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
11221126
}
11231127

11241128
// Otherwise, didn't find it. Remember we didn't find this.
1125-
CacheLookup.HitIdx = SearchDirs.size();
1129+
CacheLookup.HitIt = search_dir_end();
11261130
return None;
11271131
}
11281132

0 commit comments

Comments
 (0)