[clang][lex] NFCI: Use DirectoryEntryRef in HeaderSearch::load*()

This patch removes uses of the deprecated `DirectoryEntry::getName()` from `HeaderSearch::load*()` functions by using `DirectoryEntryRef` instead.

Note that we bail out in one case and use the also deprecated `FileEntry::getLastRef()`. That's to prevent this patch from growing, and is addressed in a follow-up.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D123771
This commit is contained in:
Jan Svoboda 2022-04-20 18:52:24 +02:00
parent 2cca53c815
commit 1d3ba05e4a
2 changed files with 38 additions and 40 deletions

View File

@ -738,8 +738,7 @@ private:
/// frameworks. /// frameworks.
/// ///
/// \returns The module, if found; otherwise, null. /// \returns The module, if found; otherwise, null.
Module *loadFrameworkModule(StringRef Name, Module *loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
const DirectoryEntry *Dir,
bool IsSystem); bool IsSystem);
/// Load all of the module maps within the immediate subdirectories /// Load all of the module maps within the immediate subdirectories
@ -888,7 +887,7 @@ private:
LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File, LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
bool IsSystem, bool IsSystem,
const DirectoryEntry *Dir, DirectoryEntryRef Dir,
FileID ID = FileID(), FileID ID = FileID(),
unsigned *Offset = nullptr); unsigned *Offset = nullptr);
@ -912,8 +911,8 @@ private:
/// ///
/// \returns The result of attempting to load the module map file from the /// \returns The result of attempting to load the module map file from the
/// named directory. /// named directory.
LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir, LoadModuleMapResult loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
bool IsSystem, bool IsFramework); bool IsFramework);
}; };
/// Apply the header search options to get given HeaderSearch object. /// Apply the header search options to get given HeaderSearch object.

View File

@ -319,7 +319,8 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
SmallString<128> FrameworkDirName; SmallString<128> FrameworkDirName;
FrameworkDirName += Dir.getFrameworkDir()->getName(); FrameworkDirName += Dir.getFrameworkDir()->getName();
llvm::sys::path::append(FrameworkDirName, SearchName + ".framework"); llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
if (auto FrameworkDir = FileMgr.getDirectory(FrameworkDirName)) { if (auto FrameworkDir =
FileMgr.getOptionalDirectoryRef(FrameworkDirName)) {
bool IsSystem = Dir.getDirCharacteristic() != SrcMgr::C_User; bool IsSystem = Dir.getDirCharacteristic() != SrcMgr::C_User;
Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem); Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
if (Module) if (Module)
@ -334,8 +335,10 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
continue; continue;
bool IsSystem = Dir.isSystemHeaderDirectory(); bool IsSystem = Dir.isSystemHeaderDirectory();
// Only returns None if not a normal directory, which we just checked
DirectoryEntryRef NormalDir = *Dir.getDirRef();
// Search for a module map file in this directory. // Search for a module map file in this directory.
if (loadModuleMapFile(Dir.getDir(), IsSystem, if (loadModuleMapFile(NormalDir, IsSystem,
/*IsFramework*/false) == LMM_NewlyLoaded) { /*IsFramework*/false) == LMM_NewlyLoaded) {
// We just loaded a module map file; check whether the module is // We just loaded a module map file; check whether the module is
// available now. // available now.
@ -507,7 +510,7 @@ Optional<FileEntryRef> DirectoryLookup::LookupFile(
/// \param DirName The name of the framework directory. /// \param DirName The name of the framework directory.
/// \param SubmodulePath Will be populated with the submodule path from the /// \param SubmodulePath Will be populated with the submodule path from the
/// returned top-level module to the originally named framework. /// returned top-level module to the originally named framework.
static const DirectoryEntry * static Optional<DirectoryEntryRef>
getTopFrameworkDir(FileManager &FileMgr, StringRef DirName, getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
SmallVectorImpl<std::string> &SubmodulePath) { SmallVectorImpl<std::string> &SubmodulePath) {
assert(llvm::sys::path::extension(DirName) == ".framework" && assert(llvm::sys::path::extension(DirName) == ".framework" &&
@ -527,12 +530,10 @@ getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
// //
// Similar issues occur when a top-level framework has moved into an // Similar issues occur when a top-level framework has moved into an
// embedded framework. // embedded framework.
const DirectoryEntry *TopFrameworkDir = nullptr; auto TopFrameworkDir = FileMgr.getOptionalDirectoryRef(DirName);
if (auto TopFrameworkDirOrErr = FileMgr.getDirectory(DirName))
TopFrameworkDir = *TopFrameworkDirOrErr;
if (TopFrameworkDir) if (TopFrameworkDir)
DirName = FileMgr.getCanonicalName(TopFrameworkDir); DirName = FileMgr.getCanonicalName(*TopFrameworkDir);
do { do {
// Get the parent directory name. // Get the parent directory name.
DirName = llvm::sys::path::parent_path(DirName); DirName = llvm::sys::path::parent_path(DirName);
@ -540,7 +541,7 @@ getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
break; break;
// Determine whether this directory exists. // Determine whether this directory exists.
auto Dir = FileMgr.getDirectory(DirName); auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
if (!Dir) if (!Dir)
break; break;
@ -1476,13 +1477,13 @@ bool HeaderSearch::hasModuleMap(StringRef FileName,
return false; return false;
// Determine whether this directory exists. // Determine whether this directory exists.
auto Dir = FileMgr.getDirectory(DirName); auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
if (!Dir) if (!Dir)
return false; return false;
// Try to load the module map file in this directory. // Try to load the module map file in this directory.
switch (loadModuleMapFile(*Dir, IsSystem, switch (loadModuleMapFile(*Dir, IsSystem,
llvm::sys::path::extension((*Dir)->getName()) == llvm::sys::path::extension(Dir->getName()) ==
".framework")) { ".framework")) {
case LMM_NewlyLoaded: case LMM_NewlyLoaded:
case LMM_AlreadyLoaded: case LMM_AlreadyLoaded:
@ -1579,15 +1580,16 @@ bool HeaderSearch::findUsableModuleForFrameworkHeader(
if (needModuleLookup(RequestingModule, SuggestedModule)) { if (needModuleLookup(RequestingModule, SuggestedModule)) {
// Find the top-level framework based on this framework. // Find the top-level framework based on this framework.
SmallVector<std::string, 4> SubmodulePath; SmallVector<std::string, 4> SubmodulePath;
const DirectoryEntry *TopFrameworkDir Optional<DirectoryEntryRef> TopFrameworkDir =
= ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath); ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
assert(TopFrameworkDir && "Could not find the top-most framework dir");
// Determine the name of the top-level framework. // Determine the name of the top-level framework.
StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName()); StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
// Load this framework module. If that succeeds, find the suggested module // Load this framework module. If that succeeds, find the suggested module
// for this header, if any. // for this header, if any.
loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework); loadFrameworkModule(ModuleName, *TopFrameworkDir, IsSystemFramework);
// FIXME: This can find a module not part of ModuleName, which is // FIXME: This can find a module not part of ModuleName, which is
// important so that we're consistent about whether this header // important so that we're consistent about whether this header
@ -1618,39 +1620,38 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
StringRef OriginalModuleMapFile) { StringRef OriginalModuleMapFile) {
// Find the directory for the module. For frameworks, that may require going // Find the directory for the module. For frameworks, that may require going
// up from the 'Modules' directory. // up from the 'Modules' directory.
const DirectoryEntry *Dir = nullptr; Optional<DirectoryEntryRef> Dir;
if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) { if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
if (auto DirOrErr = FileMgr.getDirectory(".")) Dir = FileMgr.getOptionalDirectoryRef(".");
Dir = *DirOrErr;
} else { } else {
if (!OriginalModuleMapFile.empty()) { if (!OriginalModuleMapFile.empty()) {
// We're building a preprocessed module map. Find or invent the directory // We're building a preprocessed module map. Find or invent the directory
// that it originally occupied. // that it originally occupied.
auto DirOrErr = FileMgr.getDirectory( Dir = FileMgr.getOptionalDirectoryRef(
llvm::sys::path::parent_path(OriginalModuleMapFile)); llvm::sys::path::parent_path(OriginalModuleMapFile));
if (DirOrErr) { if (!Dir) {
Dir = *DirOrErr; auto FakeFile = FileMgr.getVirtualFileRef(OriginalModuleMapFile, 0, 0);
} else { Dir = FakeFile.getDir();
auto *FakeFile = FileMgr.getVirtualFile(OriginalModuleMapFile, 0, 0);
Dir = FakeFile->getDir();
} }
} else { } else {
Dir = File->getDir(); Dir = File->getLastRef().getDir();
} }
assert(Dir && "parent must exist");
StringRef DirName(Dir->getName()); StringRef DirName(Dir->getName());
if (llvm::sys::path::filename(DirName) == "Modules") { if (llvm::sys::path::filename(DirName) == "Modules") {
DirName = llvm::sys::path::parent_path(DirName); DirName = llvm::sys::path::parent_path(DirName);
if (DirName.endswith(".framework")) if (DirName.endswith(".framework"))
if (auto DirOrErr = FileMgr.getDirectory(DirName)) if (auto MaybeDir = FileMgr.getOptionalDirectoryRef(DirName))
Dir = *DirOrErr; Dir = *MaybeDir;
// FIXME: This assert can fail if there's a race between the above check // FIXME: This assert can fail if there's a race between the above check
// and the removal of the directory. // and the removal of the directory.
assert(Dir && "parent must exist"); assert(Dir && "parent must exist");
} }
} }
switch (loadModuleMapFileImpl(File, IsSystem, Dir, ID, Offset)) { assert(Dir && "module map home directory must exist");
switch (loadModuleMapFileImpl(File, IsSystem, *Dir, ID, Offset)) {
case LMM_AlreadyLoaded: case LMM_AlreadyLoaded:
case LMM_NewlyLoaded: case LMM_NewlyLoaded:
return false; return false;
@ -1663,7 +1664,7 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
HeaderSearch::LoadModuleMapResult HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem, HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
const DirectoryEntry *Dir, FileID ID, DirectoryEntryRef Dir, FileID ID,
unsigned *Offset) { unsigned *Offset) {
assert(File && "expected FileEntry"); assert(File && "expected FileEntry");
@ -1721,8 +1722,7 @@ HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
return nullptr; return nullptr;
} }
Module *HeaderSearch::loadFrameworkModule(StringRef Name, Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
const DirectoryEntry *Dir,
bool IsSystem) { bool IsSystem) {
if (Module *Module = ModMap.findModule(Name)) if (Module *Module = ModMap.findModule(Name))
return Module; return Module;
@ -1749,14 +1749,14 @@ Module *HeaderSearch::loadFrameworkModule(StringRef Name,
HeaderSearch::LoadModuleMapResult HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem, HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
bool IsFramework) { bool IsFramework) {
if (auto Dir = FileMgr.getDirectory(DirName)) if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName))
return loadModuleMapFile(*Dir, IsSystem, IsFramework); return loadModuleMapFile(*Dir, IsSystem, IsFramework);
return LMM_NoDirectory; return LMM_NoDirectory;
} }
HeaderSearch::LoadModuleMapResult HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem, HeaderSearch::loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
bool IsFramework) { bool IsFramework) {
auto KnownDir = DirectoryHasModuleMap.find(Dir); auto KnownDir = DirectoryHasModuleMap.find(Dir);
if (KnownDir != DirectoryHasModuleMap.end()) if (KnownDir != DirectoryHasModuleMap.end())
@ -1797,8 +1797,7 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
if (llvm::sys::path::extension(Dir->path()) != ".framework") if (llvm::sys::path::extension(Dir->path()) != ".framework")
continue; continue;
auto FrameworkDir = auto FrameworkDir = FileMgr.getOptionalDirectoryRef(Dir->path());
FileMgr.getDirectory(Dir->path());
if (!FrameworkDir) if (!FrameworkDir)
continue; continue;
@ -1814,7 +1813,7 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
continue; continue;
// Try to load a module map file for the search directory. // Try to load a module map file for the search directory.
loadModuleMapFile(DL.getDir(), IsSystem, /*IsFramework*/ false); loadModuleMapFile(*DL.getDirRef(), IsSystem, /*IsFramework*/ false);
// Try to load module map files for immediate subdirectories of this // Try to load module map files for immediate subdirectories of this
// search directory. // search directory.
@ -1838,7 +1837,7 @@ void HeaderSearch::loadTopLevelSystemModules() {
continue; continue;
// Try to load a module map file for the search directory. // Try to load a module map file for the search directory.
loadModuleMapFile(DL.getDir(), DL.isSystemHeaderDirectory(), loadModuleMapFile(*DL.getDirRef(), DL.isSystemHeaderDirectory(),
DL.isFramework()); DL.isFramework());
} }
} }