aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Lex/HeaderSearch.cpp
diff options
context:
space:
mode:
authorJan Svoboda <jan_svoboda@apple.com>2022-01-13 16:29:10 +0100
committerJan Svoboda <jan_svoboda@apple.com>2022-01-13 16:29:44 +0100
commitccd7e7830f0cef74dbb7e776f76f8ede36b55e8e (patch)
tree932b910f04095ef19a7967e73545303c43dbbd28 /clang/lib/Lex/HeaderSearch.cpp
parent01494c6a7318395f577f41bd57b4bf8ca2591309 (diff)
downloadllvm-ccd7e7830f0cef74dbb7e776f76f8ede36b55e8e.zip
llvm-ccd7e7830f0cef74dbb7e776f76f8ede36b55e8e.tar.gz
llvm-ccd7e7830f0cef74dbb7e776f76f8ede36b55e8e.tar.bz2
Revert "[clang][lex] Keep references to `DirectoryLookup` objects up-to-date"
This reverts commit 8503c688. This patch causes some issues with `#include_next`: https://github.com/llvm/llvm-project/issues/53161
Diffstat (limited to 'clang/lib/Lex/HeaderSearch.cpp')
-rw-r--r--clang/lib/Lex/HeaderSearch.cpp113
1 files changed, 53 insertions, 60 deletions
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index f588139..d4de4ed 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -115,24 +115,19 @@ void HeaderSearch::SetSearchPaths(
llvm::DenseMap<unsigned int, unsigned int> searchDirToHSEntry) {
assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
"Directory indices are unordered");
- SearchDirsAlloc.DestroyAll();
- SearchDirs.clear();
- for (const DirectoryLookup &Dir : dirs)
- SearchDirs.push_back(storeSearchDir(Dir));
- UsedSearchDirs.clear();
- SearchDirToHSEntry.clear();
- for (const auto &Entry : searchDirToHSEntry)
- SearchDirToHSEntry.insert({SearchDirs[Entry.first], Entry.second});
-
+ SearchDirs = std::move(dirs);
+ SearchDirsUsage.assign(SearchDirs.size(), false);
AngledDirIdx = angledDirIdx;
SystemDirIdx = systemDirIdx;
NoCurDirSearch = noCurDirSearch;
+ SearchDirToHSEntry = std::move(searchDirToHSEntry);
//LookupFileCache.clear();
}
void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
- SearchDirs.insert(SearchDirs.begin() + idx, storeSearchDir(dir));
+ SearchDirs.insert(SearchDirs.begin() + idx, dir);
+ SearchDirsUsage.insert(SearchDirsUsage.begin() + idx, false);
if (!isAngled)
AngledDirIdx++;
SystemDirIdx++;
@@ -140,9 +135,10 @@ void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
- for (const DirectoryLookup *SearchDir : UsedSearchDirs) {
- if (UsedSearchDirs.contains(SearchDir)) {
- auto UserEntryIdxIt = SearchDirToHSEntry.find(SearchDir);
+ for (unsigned I = 0, E = SearchDirsUsage.size(); I < E; ++I) {
+ // Check whether this DirectoryLookup has been successfully used.
+ if (SearchDirsUsage[I]) {
+ auto UserEntryIdxIt = SearchDirToHSEntry.find(I);
// Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
if (UserEntryIdxIt != SearchDirToHSEntry.end())
UserEntryUsage[UserEntryIdxIt->second] = true;
@@ -151,14 +147,6 @@ std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
return UserEntryUsage;
}
-std::vector<bool> HeaderSearch::getSearchDirUsage() const {
- std::vector<bool> SearchDirUsage(SearchDirs.size());
- for (unsigned I = 0, E = SearchDirs.size(); I < E; ++I)
- if (UsedSearchDirs.contains(SearchDirs[I]))
- SearchDirUsage[I] = true;
- return SearchDirUsage;
-}
-
/// CreateHeaderMap - This method returns a HeaderMap for the specified
/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
@@ -313,23 +301,21 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
SourceLocation ImportLoc,
bool AllowExtraModuleMapSearch) {
Module *Module = nullptr;
- DirectoryLookup *SearchDir = nullptr;
+ unsigned Idx;
// Look through the various header search paths to load any available module
// maps, searching for a module map that describes this module.
- for (unsigned Idx = 0; Idx != SearchDirs.size(); ++Idx) {
- SearchDir = SearchDirs[Idx];
-
- if (SearchDirs[Idx]->isFramework()) {
+ for (Idx = 0; Idx != SearchDirs.size(); ++Idx) {
+ if (SearchDirs[Idx].isFramework()) {
// Search for or infer a module map for a framework. Here we use
// SearchName rather than ModuleName, to permit finding private modules
// named FooPrivate in buggy frameworks named Foo.
SmallString<128> FrameworkDirName;
- FrameworkDirName += SearchDirs[Idx]->getFrameworkDir()->getName();
+ FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
if (auto FrameworkDir = FileMgr.getDirectory(FrameworkDirName)) {
bool IsSystem
- = SearchDirs[Idx]->getDirCharacteristic() != SrcMgr::C_User;
+ = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
if (Module)
break;
@@ -339,12 +325,12 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
// FIXME: Figure out how header maps and module maps will work together.
// Only deal with normal search directories.
- if (!SearchDirs[Idx]->isNormalDir())
+ if (!SearchDirs[Idx].isNormalDir())
continue;
- bool IsSystem = SearchDirs[Idx]->isSystemHeaderDirectory();
+ bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
// Search for a module map file in this directory.
- if (loadModuleMapFile(SearchDirs[Idx]->getDir(), IsSystem,
+ if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
/*IsFramework*/false) == LMM_NewlyLoaded) {
// We just loaded a module map file; check whether the module is
// available now.
@@ -356,7 +342,7 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
// Search for a module map in a subdirectory with the same name as the
// module.
SmallString<128> NestedModuleMapDirName;
- NestedModuleMapDirName = SearchDirs[Idx]->getDir()->getName();
+ NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
/*IsFramework*/false) == LMM_NewlyLoaded){
@@ -368,13 +354,13 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
// If we've already performed the exhaustive search for module maps in this
// search directory, don't do it again.
- if (SearchDirs[Idx]->haveSearchedAllModuleMaps())
+ if (SearchDirs[Idx].haveSearchedAllModuleMaps())
continue;
// Load all module maps in the immediate subdirectories of this search
// directory if ModuleName was from @import.
if (AllowExtraModuleMapSearch)
- loadSubdirectoryModuleMaps(*SearchDirs[Idx]);
+ loadSubdirectoryModuleMaps(SearchDirs[Idx]);
// Look again for the module.
Module = ModMap.findModule(ModuleName);
@@ -383,7 +369,7 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
}
if (Module)
- noteLookupUsage(SearchDir, ImportLoc);
+ noteLookupUsage(Idx, ImportLoc);
return Module;
}
@@ -509,7 +495,7 @@ Optional<FileEntryRef> DirectoryLookup::LookupFile(
// The case where the target file **exists** is handled by callee of this
// function as part of the regular logic that applies to include search paths.
// The case where the target file **does not exist** is handled here:
- HS.noteLookupUsage(this, IncludeLoc);
+ HS.noteLookupUsage(*HS.searchDirIdx(*this), IncludeLoc);
return None;
}
@@ -717,14 +703,13 @@ Optional<FileEntryRef> DirectoryLookup::DoFrameworkLookup(
void HeaderSearch::cacheLookupSuccess(LookupFileCacheInfo &CacheLookup,
unsigned HitIdx, SourceLocation Loc) {
CacheLookup.HitIdx = HitIdx;
- noteLookupUsage(SearchDirs[HitIdx], Loc);
+ noteLookupUsage(HitIdx, Loc);
}
-void HeaderSearch::noteLookupUsage(const DirectoryLookup *SearchDir,
- SourceLocation Loc) {
- UsedSearchDirs.insert(SearchDir);
+void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) {
+ SearchDirsUsage[HitIdx] = true;
- auto UserEntryIdxIt = SearchDirToHSEntry.find(SearchDir);
+ auto UserEntryIdxIt = SearchDirToHSEntry.find(HitIdx);
if (UserEntryIdxIt != SearchDirToHSEntry.end())
Diags.Report(Loc, diag::remark_pp_search_path_usage)
<< HSOpts->UserEntries[UserEntryIdxIt->second].Path;
@@ -978,7 +963,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
// If this is a #include_next request, start searching after the directory the
// file was found in.
if (FromDir)
- i = std::distance(SearchDirs.begin(), llvm::find(SearchDirs, FromDir));
+ i = FromDir-&SearchDirs[0];
// Cache all of the lookups performed by this method. Many headers are
// multiply included, and the "pragma once" optimization prevents them from
@@ -1011,7 +996,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
bool InUserSpecifiedSystemFramework = false;
bool IsInHeaderMap = false;
bool IsFrameworkFoundInDir = false;
- Optional<FileEntryRef> File = SearchDirs[i]->LookupFile(
+ Optional<FileEntryRef> File = SearchDirs[i].LookupFile(
Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
IsInHeaderMap, MappedName);
@@ -1033,7 +1018,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
if (!File)
continue;
- CurDir = SearchDirs[i];
+ CurDir = &SearchDirs[i];
// This file is a system header or C++ unfriendly if the dir is.
HeaderFileInfo &HFI = getFileInfo(&File->getFileEntry());
@@ -1455,6 +1440,13 @@ size_t HeaderSearch::getTotalMemory() const {
+ FrameworkMap.getAllocator().getTotalMemory();
}
+Optional<unsigned> HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
+ for (unsigned I = 0; I < SearchDirs.size(); ++I)
+ if (&SearchDirs[I] == &DL)
+ return I;
+ return None;
+}
+
StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
return FrameworkNames.insert(Framework).first->first();
}
@@ -1782,11 +1774,11 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
if (HSOpts->ImplicitModuleMaps) {
// Load module maps for each of the header search directories.
for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
- bool IsSystem = SearchDirs[Idx]->isSystemHeaderDirectory();
- if (SearchDirs[Idx]->isFramework()) {
+ bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
+ if (SearchDirs[Idx].isFramework()) {
std::error_code EC;
SmallString<128> DirNative;
- llvm::sys::path::native(SearchDirs[Idx]->getFrameworkDir()->getName(),
+ llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
DirNative);
// Search each of the ".framework" directories to load them as modules.
@@ -1810,16 +1802,16 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
}
// FIXME: Deal with header maps.
- if (SearchDirs[Idx]->isHeaderMap())
+ if (SearchDirs[Idx].isHeaderMap())
continue;
// Try to load a module map file for the search directory.
- loadModuleMapFile(SearchDirs[Idx]->getDir(), IsSystem,
+ loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
/*IsFramework*/ false);
// Try to load module map files for immediate subdirectories of this
// search directory.
- loadSubdirectoryModuleMaps(*SearchDirs[Idx]);
+ loadSubdirectoryModuleMaps(SearchDirs[Idx]);
}
}
@@ -1835,13 +1827,14 @@ void HeaderSearch::loadTopLevelSystemModules() {
// Load module maps for each of the header search directories.
for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
// We only care about normal header directories.
- if (!SearchDirs[Idx]->isNormalDir())
+ if (!SearchDirs[Idx].isNormalDir()) {
continue;
+ }
// Try to load a module map file for the search directory.
- loadModuleMapFile(SearchDirs[Idx]->getDir(),
- SearchDirs[Idx]->isSystemHeaderDirectory(),
- SearchDirs[Idx]->isFramework());
+ loadModuleMapFile(SearchDirs[Idx].getDir(),
+ SearchDirs[Idx].isSystemHeaderDirectory(),
+ SearchDirs[Idx].isFramework());
}
}
@@ -1939,15 +1932,15 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
bool BestPrefixIsFramework = false;
for (unsigned I = 0; I != SearchDirs.size(); ++I) {
- if (SearchDirs[I]->isNormalDir()) {
- StringRef Dir = SearchDirs[I]->getDir()->getName();
+ if (SearchDirs[I].isNormalDir()) {
+ StringRef Dir = SearchDirs[I].getDir()->getName();
if (CheckDir(Dir)) {
if (IsSystem)
*IsSystem = BestPrefixLength ? I >= SystemDirIdx : false;
BestPrefixIsFramework = false;
}
- } else if (SearchDirs[I]->isFramework()) {
- StringRef Dir = SearchDirs[I]->getFrameworkDir()->getName();
+ } else if (SearchDirs[I].isFramework()) {
+ StringRef Dir = SearchDirs[I].getFrameworkDir()->getName();
if (CheckDir(Dir)) {
if (IsSystem)
*IsSystem = BestPrefixLength ? I >= SystemDirIdx : false;
@@ -1968,11 +1961,11 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
// key from header name is user prefered name for the include file.
StringRef Filename = File.drop_front(BestPrefixLength);
for (unsigned I = 0; I != SearchDirs.size(); ++I) {
- if (!SearchDirs[I]->isHeaderMap())
+ if (!SearchDirs[I].isHeaderMap())
continue;
StringRef SpelledFilename =
- SearchDirs[I]->getHeaderMap()->reverseLookupFilename(Filename);
+ SearchDirs[I].getHeaderMap()->reverseLookupFilename(Filename);
if (!SpelledFilename.empty()) {
Filename = SpelledFilename;
BestPrefixIsFramework = false;